]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/http/vector_body.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / http / vector_body.hpp
1 //
2 // Copyright (c) 2016-2017 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/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>
19 #include <cstdint>
20 #include <limits>
21 #include <memory>
22 #include <stdexcept>
23 #include <string>
24 #include <utility>
25
26 namespace boost {
27 namespace beast {
28 namespace http {
29
30 /** A @b Body using `std::vector`
31
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.
35 */
36 template<class T, class Allocator = std::allocator<T>>
37 struct vector_body
38 {
39 private:
40 static_assert(sizeof(T) == 1 &&
41 std::is_integral<T>::value,
42 "T requirements not met");
43
44 public:
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 @b BodyReader.
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(message<isRequest,
80 vector_body, Fields>& m)
81 : body_(m.body())
82 {
83 }
84
85 void
86 init(boost::optional<
87 std::uint64_t> const& length, error_code& ec)
88 {
89 if(length)
90 {
91 if(static_cast<std::size_t>(*length) != *length)
92 {
93 ec = error::buffer_overflow;
94 return;
95 }
96 try
97 {
98 body_.reserve(
99 static_cast<std::size_t>(*length));
100 }
101 catch(std::exception const&)
102 {
103 ec = error::buffer_overflow;
104 return;
105 }
106 }
107 ec.assign(0, ec.category());
108 }
109
110 template<class ConstBufferSequence>
111 std::size_t
112 put(ConstBufferSequence const& buffers,
113 error_code& ec)
114 {
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();
119 try
120 {
121 body_.resize(len + n);
122 }
123 catch(std::exception const&)
124 {
125 ec = error::buffer_overflow;
126 return 0;
127 }
128 ec.assign(0, ec.category());
129 return buffer_copy(boost::asio::buffer(
130 &body_[0] + len, n), buffers);
131 }
132
133 void
134 finish(error_code& ec)
135 {
136 ec.assign(0, ec.category());
137 }
138 };
139 #endif
140
141 /** The algorithm for serializing the body
142
143 Meets the requirements of @b BodyWriter.
144 */
145 #if BOOST_BEAST_DOXYGEN
146 using writer = implementation_defined;
147 #else
148 class writer
149 {
150 value_type const& body_;
151
152 public:
153 using const_buffers_type =
154 boost::asio::const_buffer;
155
156 template<bool isRequest, class Fields>
157 explicit
158 writer(message<isRequest,
159 vector_body, Fields> const& msg)
160 : body_(msg.body())
161 {
162 }
163
164 void
165 init(error_code& ec)
166 {
167 ec.assign(0, ec.category());
168 }
169
170 boost::optional<std::pair<const_buffers_type, bool>>
171 get(error_code& ec)
172 {
173 ec.assign(0, ec.category());
174 return {{const_buffers_type{
175 body_.data(), body_.size()}, false}};
176 }
177 };
178 #endif
179 };
180
181 } // http
182 } // beast
183 } // boost
184
185 #endif