1 // 2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // Official repository: https://github.com/boostorg/beast 8 // 9 10 #ifndef BOOST_BEAST_HTTP_EMPTY_BODY_HPP 11 #define BOOST_BEAST_HTTP_EMPTY_BODY_HPP 12 13 #include <boost/beast/core/detail/config.hpp> 14 #include <boost/beast/http/error.hpp> 15 #include <boost/beast/http/message.hpp> 16 #include <boost/optional.hpp> 17 18 namespace boost { 19 namespace beast { 20 namespace http { 21 22 /** An empty <em>Body</em> 23 24 This body is used to represent messages which do not have a 25 message body. If this body is used with a parser, and the 26 parser encounters octets corresponding to a message body, 27 the parser will fail with the error @ref http::unexpected_body. 28 29 The Content-Length of this body is always 0. 30 */ 31 struct empty_body 32 { 33 /** The type of container used for the body 34 35 This determines the type of @ref message::body 36 when this body type is used with a message container. 37 */ 38 struct value_type 39 { 40 }; 41 42 /** Returns the payload size of the body 43 44 When this body is used with @ref message::prepare_payload, 45 the Content-Length will be set to the payload size, and 46 any chunked Transfer-Encoding will be removed. 47 */ 48 static 49 std::uint64_t sizeboost::beast::http::empty_body50 size(value_type) 51 { 52 return 0; 53 } 54 55 /** The algorithm for parsing the body 56 57 Meets the requirements of <em>BodyReader</em>. 58 */ 59 #if BOOST_BEAST_DOXYGEN 60 using reader = __implementation_defined__; 61 #else 62 struct reader 63 { 64 template<bool isRequest, class Fields> 65 explicit readerboost::beast::http::empty_body::reader66 reader(header<isRequest, Fields>&, value_type&) 67 { 68 } 69 70 void initboost::beast::http::empty_body::reader71 init(boost::optional<std::uint64_t> const&, error_code& ec) 72 { 73 ec = {}; 74 } 75 76 template<class ConstBufferSequence> 77 std::size_t putboost::beast::http::empty_body::reader78 put(ConstBufferSequence const&, 79 error_code& ec) 80 { 81 ec = error::unexpected_body; 82 return 0; 83 } 84 85 void finishboost::beast::http::empty_body::reader86 finish(error_code& ec) 87 { 88 ec = {}; 89 } 90 }; 91 #endif 92 93 /** The algorithm for serializing the body 94 95 Meets the requirements of <em>BodyWriter</em>. 96 */ 97 #if BOOST_BEAST_DOXYGEN 98 using writer = __implementation_defined__; 99 #else 100 struct writer 101 { 102 using const_buffers_type = 103 net::const_buffer; 104 105 template<bool isRequest, class Fields> 106 explicit writerboost::beast::http::empty_body::writer107 writer(header<isRequest, Fields> const&, value_type const&) 108 { 109 } 110 111 void initboost::beast::http::empty_body::writer112 init(error_code& ec) 113 { 114 ec = {}; 115 } 116 117 boost::optional<std::pair<const_buffers_type, bool>> getboost::beast::http::empty_body::writer118 get(error_code& ec) 119 { 120 ec = {}; 121 return boost::none; 122 } 123 }; 124 #endif 125 }; 126 127 } // http 128 } // beast 129 } // boost 130 131 #endif 132