]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/http/vector_body.hpp
40c885e43ec1a8786b4b0d899d94a75ccc8a56e9
[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(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(static_cast<std::size_t>(*length) != *length)
91 {
92 ec = error::buffer_overflow;
93 return;
94 }
95 try
96 {
97 body_.reserve(
98 static_cast<std::size_t>(*length));
99 }
100 catch(std::exception const&)
101 {
102 ec = error::buffer_overflow;
103 return;
104 }
105 }
106 ec.assign(0, ec.category());
107 }
108
109 template<class ConstBufferSequence>
110 std::size_t
111 put(ConstBufferSequence const& buffers,
112 error_code& ec)
113 {
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();
118 try
119 {
120 body_.resize(len + n);
121 }
122 catch(std::exception const&)
123 {
124 ec = error::buffer_overflow;
125 return 0;
126 }
127 ec.assign(0, ec.category());
128 return buffer_copy(boost::asio::buffer(
129 &body_[0] + len, n), buffers);
130 }
131
132 void
133 finish(error_code& ec)
134 {
135 ec.assign(0, ec.category());
136 }
137 };
138 #endif
139
140 /** The algorithm for serializing the body
141
142 Meets the requirements of @b BodyWriter.
143 */
144 #if BOOST_BEAST_DOXYGEN
145 using writer = implementation_defined;
146 #else
147 class writer
148 {
149 value_type const& body_;
150
151 public:
152 using const_buffers_type =
153 boost::asio::const_buffer;
154
155 template<bool isRequest, class Fields>
156 explicit
157 writer(header<isRequest, Fields> const&, value_type const& b)
158 : body_(b)
159 {
160 }
161
162 void
163 init(error_code& ec)
164 {
165 ec.assign(0, ec.category());
166 }
167
168 boost::optional<std::pair<const_buffers_type, bool>>
169 get(error_code& ec)
170 {
171 ec.assign(0, ec.category());
172 return {{const_buffers_type{
173 body_.data(), body_.size()}, false}};
174 }
175 };
176 #endif
177 };
178
179 } // http
180 } // beast
181 } // boost
182
183 #endif