]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/http/span_body.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / http / span_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_SPAN_BODY_HPP
11 #define BOOST_BEAST_HTTP_SPAN_BODY_HPP
12
13 #include <boost/beast/core/detail/config.hpp>
14 #include <boost/beast/core/span.hpp>
15 #include <boost/beast/http/error.hpp>
16 #include <boost/beast/http/message.hpp>
17 #include <boost/optional.hpp>
18
19 namespace boost {
20 namespace beast {
21 namespace http {
22
23 /** A @b Body using @ref span
24
25 This body uses @ref span as a memory-based container for
26 holding message payloads. The container represents a
27 non-owning reference to a continguous area of memory.
28 Messages using this body type may be serialized and
29 parsed.
30
31 Unlike @ref buffer_body, only one buffer may be provided
32 during a parse or serialize operation.
33 */
34 template<class T>
35 struct span_body
36 {
37 private:
38 static_assert(std::is_pod<T>::value,
39 "POD requirements not met");
40
41 public:
42 /** The type of container used for the body
43
44 This determines the type of @ref message::body
45 when this body type is used with a message container.
46 */
47 using value_type = span<T>;
48
49 /** Returns the payload size of the body
50
51 When this body is used with @ref message::prepare_payload,
52 the Content-Length will be set to the payload size, and
53 any chunked Transfer-Encoding will be removed.
54 */
55 static
56 std::uint64_t
57 size(value_type const& body)
58 {
59 return body.size();
60 }
61
62 /** The algorithm for parsing the body
63
64 Meets the requirements of @b BodyReader.
65 */
66 #if BOOST_BEAST_DOXYGEN
67 using reader = implementation_defined;
68 #else
69 class reader
70 {
71 value_type& body_;
72
73 public:
74 template<bool isRequest, class Fields>
75 explicit
76 reader(message<isRequest,
77 span_body, Fields>& m)
78 : body_(m.body())
79 {
80 }
81
82 void
83 init(boost::optional<
84 std::uint64_t> const& length, error_code& ec)
85 {
86 if(length && *length > body_.size())
87 {
88 ec = error::buffer_overflow;
89 return;
90 }
91 ec.assign(0, ec.category());
92 }
93
94 template<class ConstBufferSequence>
95 std::size_t
96 put(ConstBufferSequence const& buffers,
97 error_code& ec)
98 {
99 using boost::asio::buffer_size;
100 using boost::asio::buffer_copy;
101 auto const n = buffer_size(buffers);
102 auto const len = body_.size();
103 if(n > len)
104 {
105 ec = error::buffer_overflow;
106 return 0;
107 }
108 ec.assign(0, ec.category());
109 buffer_copy(boost::asio::buffer(
110 body_.data(), n), buffers);
111 body_ = value_type{
112 body_.data() + n, body_.size() - n};
113 return n;
114 }
115
116 void
117 finish(error_code& ec)
118 {
119 ec.assign(0, ec.category());
120 }
121 };
122 #endif
123
124 /** The algorithm for serializing the body
125
126 Meets the requirements of @b BodyWriter.
127 */
128 #if BOOST_BEAST_DOXYGEN
129 using writer = implementation_defined;
130 #else
131 class writer
132 {
133 value_type const& body_;
134
135 public:
136 using const_buffers_type =
137 boost::asio::const_buffer;
138
139 template<bool isRequest, class Fields>
140 explicit
141 writer(message<isRequest,
142 span_body, Fields> const& msg)
143 : body_(msg.body())
144 {
145 }
146
147 void
148 init(error_code& ec)
149 {
150 ec.assign(0, ec.category());
151 }
152
153 boost::optional<std::pair<const_buffers_type, bool>>
154 get(error_code& ec)
155 {
156 ec.assign(0, ec.category());
157 return {{
158 { body_.data(),
159 body_.size() * sizeof(typename
160 value_type::value_type)},
161 false}};
162 }
163 };
164 #endif
165 };
166
167 } // http
168 } // beast
169 } // boost
170
171 #endif