1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//
#ifndef BOOST_BEAST_HTTP_BUFFER_BODY_HPP
#define BOOST_BEAST_HTTP_BUFFER_BODY_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/http/error.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/http/type_traits.hpp>
#include <boost/optional.hpp>
#include <type_traits>
#include <utility>
namespace boost {
namespace beast {
namespace http {
/** A @b Body using a caller provided buffer
Messages using this body type may be serialized and parsed.
To use this class, the caller must initialize the members
of @ref buffer_body::value_type to appropriate values before
each call to read or write during a stream operation.
*/
struct buffer_body
{
/// The type of the body member when used in a message.
struct value_type
{
/** A pointer to a contiguous area of memory of @ref size octets, else `nullptr`.
@par When Serializing
If this is `nullptr` and `more` is `true`, the error
@ref error::need_buffer will be returned from @ref serializer::get
Otherwise, the serializer will use the memory pointed to
by `data` having `size` octets of valid storage as the
next buffer representing the body.
@par When Parsing
If this is `nullptr`, the error @ref error::need_buffer
will be returned from @ref parser::put. Otherwise, the
parser will store body octets into the memory pointed to
by `data` having `size` octets of valid storage. After
octets are stored, the `data` and `size` members are
adjusted: `data` is incremented to point to the next
octet after the data written, while `size` is decremented
to reflect the remaining space at the memory location
pointed to by `data`.
*/
void* data = nullptr;
/** The number of octets in the buffer pointed to by @ref data.
@par When Serializing
If `data` is `nullptr` during serialization, this value
is ignored. Otherwise, it represents the number of valid
body octets pointed to by `data`.
@par When Parsing
The value of this field will be decremented during parsing
to indicate the number of remaining free octets in the
buffer pointed to by `data`. When it reaches zero, the
parser will return @ref error::need_buffer, indicating to
the caller that the values of `data` and `size` should be
updated to point to a new memory buffer.
*/
std::size_t size = 0;
/** `true` if this is not the last buffer.
@par When Serializing
If this is `true` and `data` is `nullptr`, the error
@ref error::need_buffer will be returned from @ref serializer::get
@par When Parsing
This field is not used during parsing.
*/
bool more = true;
};
/** The algorithm for parsing the body
Meets the requirements of @b BodyReader.
*/
#if BOOST_BEAST_DOXYGEN
using reader = implementation_defined;
#else
class reader
{
value_type& body_;
public:
template<bool isRequest, class Fields>
explicit
reader(message<isRequest, buffer_body, Fields>& m)
: body_(m.body())
{
}
void
init(boost::optional<std::uint64_t> const&, error_code& ec)
{
ec.assign(0, ec.category());
}
template<class ConstBufferSequence>
std::size_t
put(ConstBufferSequence const& buffers,
error_code& ec)
{
using boost::asio::buffer_size;
using boost::asio::buffer_copy;
if(! body_.data)
{
ec = error::need_buffer;
return 0;
}
auto const bytes_transferred =
buffer_copy(boost::asio::buffer(
body_.data, body_.size), buffers);
body_.data = reinterpret_cast<char*>(
body_.data) + bytes_transferred;
body_.size -= bytes_transferred;
if(bytes_transferred == buffer_size(buffers))
ec.assign(0, ec.category());
else
ec = error::need_buffer;
return bytes_transferred;
}
void
finish(error_code& ec)
{
ec.assign(0, ec.category());
}
};
#endif
/** The algorithm for serializing the body
Meets the requirements of @b BodyWriter.
*/
#if BOOST_BEAST_DOXYGEN
using writer = implementation_defined;
#else
class writer
{
bool toggle_ = false;
value_type const& body_;
public:
using const_buffers_type =
boost::asio::const_buffer;
template<bool isRequest, class Fields>
explicit
writer(message<isRequest,
buffer_body, Fields> const& msg)
: body_(msg.body())
{
}
void
init(error_code& ec)
{
ec.assign(0, ec.category());
}
boost::optional<
std::pair<const_buffers_type, bool>>
get(error_code& ec)
{
if(toggle_)
{
if(body_.more)
{
toggle_ = false;
ec = error::need_buffer;
}
else
{
ec.assign(0, ec.category());
}
return boost::none;
}
if(body_.data)
{
ec.assign(0, ec.category());
toggle_ = true;
return {{const_buffers_type{
body_.data, body_.size}, body_.more}};
}
if(body_.more)
ec = error::need_buffer;
else
ec.assign(0, ec.category());
return boost::none;
}
};
#endif
};
#if ! BOOST_BEAST_DOXYGEN
// operator<< is not supported for buffer_body
template<bool isRequest, class Fields>
std::ostream&
operator<<(std::ostream& os, message<isRequest,
buffer_body, Fields> const& msg) = delete;
#endif
} // http
} // beast
} // boost
#endif
|