2 // Copyright (c) 2016-2017 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/http/error.hpp>
15 #include <boost/beast/http/message.hpp>
16 #include <boost/beast/core/detail/type_traits.hpp>
17 #include <boost/asio/buffer.hpp>
18 #include <boost/optional.hpp>
30 /** A @b Body using `std::vector`
32 This body uses `std::vector` as a memory-based container
33 for holding message payloads. Messages using this body type
34 may be serialized and parsed.
36 template<class T, class Allocator = std::allocator<T>>
40 static_assert(sizeof(T) == 1 &&
41 std::is_integral<T>::value,
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 @b BodyReader.
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(static_cast<std::size_t>(*length) != *length)
92 ec = error::buffer_overflow;
98 static_cast<std::size_t>(*length));
100 catch(std::exception const&)
102 ec = error::buffer_overflow;
106 ec.assign(0, ec.category());
109 template<class ConstBufferSequence>
111 put(ConstBufferSequence const& buffers,
114 using boost::asio::buffer_size;
115 using boost::asio::buffer_copy;
116 auto const n = buffer_size(buffers);
117 auto const len = body_.size();
120 body_.resize(len + n);
122 catch(std::exception const&)
124 ec = error::buffer_overflow;
127 ec.assign(0, ec.category());
128 return buffer_copy(boost::asio::buffer(
129 &body_[0] + len, n), buffers);
133 finish(error_code& ec)
135 ec.assign(0, ec.category());
140 /** The algorithm for serializing the body
142 Meets the requirements of @b BodyWriter.
144 #if BOOST_BEAST_DOXYGEN
145 using writer = implementation_defined;
149 value_type const& body_;
152 using const_buffers_type =
153 boost::asio::const_buffer;
155 template<bool isRequest, class Fields>
157 writer(header<isRequest, Fields> const&, value_type const& b)
165 ec.assign(0, ec.category());
168 boost::optional<std::pair<const_buffers_type, bool>>
171 ec.assign(0, ec.category());
172 return {{const_buffers_type{
173 body_.data(), body_.size()}, false}};