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_VECTOR_BODY_HPP
11 #define BOOST_BEAST_HTTP_VECTOR_BODY_HPP
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/buffer_traits.hpp>
15 #include <boost/beast/http/error.hpp>
16 #include <boost/beast/http/message.hpp>
17 #include <boost/beast/core/detail/clamp.hpp>
18 #include <boost/asio/buffer.hpp>
19 #include <boost/optional.hpp>
31 /** A <em>Body</em> using `std::vector`
33 This body uses `std::vector` as a memory-based container
34 for holding message payloads. Messages using this body type
35 may be serialized and parsed.
37 template<class T, class Allocator = std::allocator<T>>
41 static_assert(sizeof(T) == 1,
42 "T requirements not met");
45 /** The type of container used for the body
47 This determines the type of @ref message::body
48 when this body type is used with a message container.
50 using value_type = std::vector<T, Allocator>;
52 /** Returns the payload size of the body
54 When this body is used with @ref message::prepare_payload,
55 the Content-Length will be set to the payload size, and
56 any chunked Transfer-Encoding will be removed.
60 size(value_type const& body)
65 /** The algorithm for parsing the body
67 Meets the requirements of <em>BodyReader</em>.
69 #if BOOST_BEAST_DOXYGEN
70 using reader = __implementation_defined__;
77 template<bool isRequest, class Fields>
79 reader(header<isRequest, Fields>&, value_type& b)
86 std::uint64_t> const& length, error_code& ec)
90 if(*length > body_.max_size())
92 ec = error::buffer_overflow;
95 body_.reserve(beast::detail::clamp(*length));
100 template<class ConstBufferSequence>
102 put(ConstBufferSequence const& buffers,
105 auto const n = buffer_bytes(buffers);
106 auto const len = body_.size();
107 if (n > body_.max_size() - len)
109 ec = error::buffer_overflow;
113 body_.resize(len + n);
115 return net::buffer_copy(net::buffer(
116 &body_[0] + len, n), buffers);
120 finish(error_code& ec)
127 /** The algorithm for serializing the body
129 Meets the requirements of <em>BodyWriter</em>.
131 #if BOOST_BEAST_DOXYGEN
132 using writer = __implementation_defined__;
136 value_type const& body_;
139 using const_buffers_type =
142 template<bool isRequest, class Fields>
144 writer(header<isRequest, Fields> const&, value_type const& b)
155 boost::optional<std::pair<const_buffers_type, bool>>
159 return {{const_buffers_type{
160 body_.data(), body_.size()}, false}};