-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfields.hpp
More file actions
387 lines (303 loc) · 8.59 KB
/
fields.hpp
File metadata and controls
387 lines (303 loc) · 8.59 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
// Copyright (c) 2024 Christian Mazakas
// Copyright (c) 2025 Mohammad Nejati
//
// 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/cppalliance/http_proto
//
#ifndef BOOST_HTTP_PROTO_FIELDS_HPP
#define BOOST_HTTP_PROTO_FIELDS_HPP
#include <boost/http_proto/detail/config.hpp>
#include <boost/http_proto/fields_base.hpp>
namespace boost {
namespace http_proto {
/** A modifiable container of HTTP fields.
This container owns a collection of HTTP
fields, represented by a buffer which is
managed by performing dynamic memory
allocations as needed. The contents may be
inspected and modified, and the implementation
maintains a useful invariant: changes to the
fields always leave it in a valid state.
@par Example
@code
fields fs;
fs.set(field::host, "example.com");
fs.set(field::accept_encoding, "gzip, deflate, br");
fs.set(field::cache_control, "no-cache");
assert(fs.buffer() ==
"Host: example.com\r\n"
"Accept-Encoding: gzip, deflate, br\r\n"
"Cache-Control: no-cache\r\n"
"\r\n");
@endcode
@see
@ref fields_base.
*/
class fields final
: public fields_base
{
public:
//--------------------------------------------
//
// Special Members
//
//--------------------------------------------
/** Constructor.
A default-constructed fields container
contain no name-value pairs.
@par Example
@code
fields fs;
@endcode
@par Postconditions
@code
this->buffer() == "\r\n"
@endcode
@par Complexity
Constant.
*/
fields() noexcept
: fields_base(detail::kind::fields)
{
}
/** Constructor.
Constructs a fields container from the string
`s`, which must contain valid HTTP headers or
else an exception is thrown.
The new fields container retains ownership by
allocating a copy of the passed string.
@par Example
@code
fields f(
"Server: Boost.HttpProto\r\n"
"Content-Type: text/plain\r\n"
"Connection: close\r\n"
"Content-Length: 73\r\n"
"\r\n");
@endcode
@par Postconditions
@code
this->buffer() == s && this->buffer().data() != s.data()
@endcode
@par Complexity
Linear in `s.size()`.
@par Exception Safety
Calls to allocate may throw.
Exception thrown on invalid input.
@throw system_error
Input is invalid.
@param s The string to parse.
*/
explicit
fields(
core::string_view s)
: fields_base(detail::kind::fields, s)
{
}
/** Constructor.
Allocates `cap` bytes initially, with an
upper limit of `max_cap`. Growing beyond
`max_cap` will throw an exception.
Useful when an estimated initial size is
known, but further growth up to a
maximum is allowed.
@par Preconditions
@code
max_cap >= cap
@endcode
@par Exception Safety
Calls to allocate may throw.
Exception thrown on invalid input.
@throw system_error
Input is invalid.
@param cap Initial capacity in bytes (may be `0`).
@param max_cap Maximum allowed capacity in bytes.
*/
explicit
fields(
std::size_t cap,
std::size_t max_cap = std::size_t(-1))
: fields()
{
reserve_bytes(cap);
set_max_capacity_in_bytes(max_cap);
}
/** Constructor.
The contents of `f` are transferred
to the newly constructed object,
which includes the underlying
character buffer.
After construction, the moved-from
object is as if default-constructed.
@par Postconditions
@code
f.buffer() == "\r\n"
@endcode
@par Complexity
Constant.
@param f The fields to move from.
*/
fields(fields&& f) noexcept
: fields_base(f.h_.kind)
{
swap(f);
}
/** Constructor.
The newly constructed object contains
a copy of `f`.
@par Postconditions
@code
this->buffer() == f.buffer() && this->buffer().data() != f.buffer().data()
@endcode
@par Complexity
Linear in `f.size()`.
@par Exception Safety
Calls to allocate may throw.
@param f The fields to copy.
*/
fields(fields const& f) = default;
/** Assignment.
The contents of `f` are transferred to
`this`, including the underlying
character buffer. The previous contents
of `this` are destroyed.
After assignment, the moved-from
object is as if default-constructed.
@par Postconditions
@code
f.buffer() == "\r\n"
@endcode
@par Complexity
Constant.
@param f The fields to assign from.
@return A reference to this object.
*/
fields&
operator=(fields&& f) noexcept
{
fields tmp(std::move(f));
tmp.swap(*this);
return *this;
}
/** Assignment.
The contents of `f` are copied and
the previous contents of `this` are
discarded.
@par Postconditions
@code
this->buffer() == f.buffer() && this->buffer().data() != f.buffer().data()
@endcode
@par Complexity
Linear in `f.size()`.
@par Exception Safety
Strong guarantee.
Calls to allocate may throw.
Exception thrown if max capacity exceeded.
@throw std::length_error
Max capacity would be exceeded.
@return A reference to this object.
@param f The fields to copy.
*/
fields&
operator=(fields const& f) noexcept
{
copy_impl(f.h_);
return *this;
}
//--------------------------------------------
/** Swap.
Exchanges the contents of this fields
object with another. All views, iterators
and references remain valid.
If `this == &other`, this function call has no effect.
@par Example
@code
fields f1;
f1.set(field::accept, "text/html");
fields f2;
f2.set(field::connection, "keep-alive");
f1.swap(f2);
assert(f1.buffer() == "Connection: keep-alive\r\n\r\n" );
assert(f2.buffer() == "Accept: text/html\r\n\r\n" );
@endcode
@par Complexity
Constant.
@param other The object to swap with.
*/
void
swap(fields& other) noexcept
{
h_.swap(other.h_);
std::swap(max_cap_, other.max_cap_);
}
/** Swap.
Exchanges the contents of `v0` with
another `v1`. All views, iterators and
references remain valid.
If `&v0 == &v1`, this function call has no effect.
@par Example
@code
fields f1;
f1.set(field::accept, "text/html");
fields f2;
f2.set(field::connection, "keep-alive");
std::swap(f1, f2);
assert(f1.buffer() == "Connection: keep-alive\r\n\r\n" );
assert(f2.buffer() == "Accept: text/html\r\n\r\n" );
@endcode
@par Effects
@code
v0.swap(v1);
@endcode
@par Complexity
Constant.
@param v0 The first object to swap.
@param v1 The second object to swap.
@see
@ref fields::swap.
*/
friend
void
swap(
fields& v0,
fields& v1) noexcept
{
v0.swap(v1);
}
};
/** Format the container to the output stream
This function serializes the container to
the specified output stream.
@par Example
@code
fields f;
f.set(field::host, "example.com");
std::stringstream ss;
ss << f;
assert( ss.str() == "Host: example.com\n" );
@endcode
@par Effects
Each field is written to the output stream with
CRLF line endings converted to LF. The trailing
CRLF that indicates the end of headers is not
written.
@par Complexity
Linear in `f.buffer().size()`
@par Exception Safety
Basic guarantee.
@return A reference to the output stream, for chaining
@param os The output stream to write to.
@param f The container to write.
*/
BOOST_HTTP_PROTO_DECL
std::ostream&
operator<<(
std::ostream& os,
const fields& f);
} // http_proto
} // boost
#endif