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_VECTOR_BODY_HPP
11#define BOOST_BEAST_HTTP_VECTOR_BODY_HPP
12
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>
20#include <cstdint>
21#include <limits>
22#include <memory>
23#include <stdexcept>
24#include <string>
25#include <utility>
26
27namespace boost {
28namespace beast {
29namespace http {
30
31/** A <em>Body</em> using `std::vector`
32
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.
36*/
37template<class T, class Allocator = std::allocator<T>>
38struct vector_body
39{
40private:
41 static_assert(sizeof(T) == 1,
42 "T requirements not met");
43
44public:
45 /** The type of container used for the body
46
47 This determines the type of @ref message::body
48 when this body type is used with a message container.
49 */
50 using value_type = std::vector<T, Allocator>;
51
52 /** Returns the payload size of the body
53
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.
57 */
58 static
59 std::uint64_t
60 size(value_type const& body)
61 {
62 return body.size();
63 }
64
65 /** The algorithm for parsing the body
66
67 Meets the requirements of <em>BodyReader</em>.
68 */
69#if BOOST_BEAST_DOXYGEN
70 using reader = __implementation_defined__;
71#else
72 class reader
73 {
74 value_type& body_;
75
76 public:
77 template<bool isRequest, class Fields>
78 explicit
79 reader(header<isRequest, Fields>&, value_type& b)
80 : body_(b)
81 {
82 }
83
84 void
85 init(boost::optional<
86 std::uint64_t> const& length, error_code& ec)
87 {
88 if(length)
89 {
90 if(*length > body_.max_size())
91 {
92 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
93 return;
94 }
95 body_.reserve(beast::detail::clamp(x: *length));
96 }
97 ec = {};
98 }
99
100 template<class ConstBufferSequence>
101 std::size_t
102 put(ConstBufferSequence const& buffers,
103 error_code& ec)
104 {
105 auto const n = buffer_bytes(buffers);
106 auto const len = body_.size();
107 if (n > body_.max_size() - len)
108 {
109 BOOST_BEAST_ASSIGN_EC(ec, error::buffer_overflow);
110 return 0;
111 }
112
113 body_.resize(len + n);
114 ec = {};
115 return net::buffer_copy(net::buffer(
116 &body_[0] + len, n), buffers);
117 }
118
119 void
120 finish(error_code& ec)
121 {
122 ec = {};
123 }
124 };
125#endif
126
127 /** The algorithm for serializing the body
128
129 Meets the requirements of <em>BodyWriter</em>.
130 */
131#if BOOST_BEAST_DOXYGEN
132 using writer = __implementation_defined__;
133#else
134 class writer
135 {
136 value_type const& body_;
137
138 public:
139 using const_buffers_type =
140 net::const_buffer;
141
142 template<bool isRequest, class Fields>
143 explicit
144 writer(header<isRequest, Fields> const&, value_type const& b)
145 : body_(b)
146 {
147 }
148
149 void
150 init(error_code& ec)
151 {
152 ec = {};
153 }
154
155 boost::optional<std::pair<const_buffers_type, bool>>
156 get(error_code& ec)
157 {
158 ec = {};
159 return {{const_buffers_type{
160 body_.data(), body_.size()}, false}};
161 }
162 };
163#endif
164};
165
166} // http
167} // beast
168} // boost
169
170#endif
171

source code of boost/libs/beast/include/boost/beast/http/vector_body.hpp