2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
7 // Official repository: https://github.com/boostorg/beast
10 #ifndef BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP
11 #define BOOST_BEAST_HTTP_BASIC_DYNAMIC_BODY_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/buffer_traits.hpp>
15 #include <boost/beast/core/detail/buffer.hpp>
16 #include <boost/beast/core/detail/clamp.hpp>
17 #include <boost/beast/http/error.hpp>
18 #include <boost/beast/http/message.hpp>
19 #include <boost/optional.hpp>
28 /** A <em>Body</em> using a <em>DynamicBuffer</em>
30 This body uses a <em>DynamicBuffer</em> as a memory-based container
31 for holding message payloads. Messages using this body type
32 may be serialized and parsed.
34 template<class DynamicBuffer>
35 struct basic_dynamic_body
38 net::is_dynamic_buffer<DynamicBuffer>::value,
39 "DynamicBuffer type requirements not met");
41 /** The type of container used for the body
43 This determines the type of @ref message::body
44 when this body type is used with a message container.
46 using value_type = DynamicBuffer;
48 /** Returns the payload size of the body
50 When this body is used with @ref message::prepare_payload,
51 the Content-Length will be set to the payload size, and
52 any chunked Transfer-Encoding will be removed.
56 size(value_type const& v)
61 /** The algorithm for parsing the body
63 Meets the requirements of <em>BodyReader</em>.
65 #if BOOST_BEAST_DOXYGEN
66 using reader = __implementation_defined__;
73 template<bool isRequest, class Fields>
75 reader(header<isRequest, Fields>&, value_type& b)
82 std::uint64_t> const&, error_code& ec)
87 template<class ConstBufferSequence>
89 put(ConstBufferSequence const& buffers,
92 auto const n = buffer_bytes(buffers);
93 if(beast::detail::sum_exceeds(body_.size(), n, body_.max_size()))
95 ec = error::buffer_overflow;
99 beast::detail::dynamic_buffer_prepare(
101 body_.max_size() - body_.size()),
102 ec, error::buffer_overflow);
105 auto const bytes_transferred =
106 net::buffer_copy(*mb, buffers);
107 body_.commit(bytes_transferred);
108 return bytes_transferred;
112 finish(error_code& ec)
119 /** The algorithm for serializing the body
121 Meets the requirements of <em>BodyWriter</em>.
123 #if BOOST_BEAST_DOXYGEN
124 using writer = __implementation_defined__;
128 DynamicBuffer const& body_;
131 using const_buffers_type =
132 typename DynamicBuffer::const_buffers_type;
134 template<bool isRequest, class Fields>
136 writer(header<isRequest, Fields> const&, value_type const& b)
147 boost::optional<std::pair<const_buffers_type, bool>>
151 return {{body_.data(), false}};