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(message<isRequest,
80 vector_body, Fields>& m)
87 std::uint64_t> const& length, error_code& ec)
91 if(static_cast<std::size_t>(*length) != *length)
93 ec = error::buffer_overflow;
99 static_cast<std::size_t>(*length));
101 catch(std::exception const&)
103 ec = error::buffer_overflow;
107 ec.assign(0, ec.category());
110 template<class ConstBufferSequence>
112 put(ConstBufferSequence const& buffers,
115 using boost::asio::buffer_size;
116 using boost::asio::buffer_copy;
117 auto const n = buffer_size(buffers);
118 auto const len = body_.size();
121 body_.resize(len + n);
123 catch(std::exception const&)
125 ec = error::buffer_overflow;
128 ec.assign(0, ec.category());
129 return buffer_copy(boost::asio::buffer(
130 &body_[0] + len, n), buffers);
134 finish(error_code& ec)
136 ec.assign(0, ec.category());
141 /** The algorithm for serializing the body
143 Meets the requirements of @b BodyWriter.
145 #if BOOST_BEAST_DOXYGEN
146 using writer = implementation_defined;
150 value_type const& body_;
153 using const_buffers_type =
154 boost::asio::const_buffer;
156 template<bool isRequest, class Fields>
158 writer(message<isRequest,
159 vector_body, Fields> const& msg)
167 ec.assign(0, ec.category());
170 boost::optional<std::pair<const_buffers_type, bool>>
173 ec.assign(0, ec.category());
174 return {{const_buffers_type{
175 body_.data(), body_.size()}, false}};