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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
|
//
// 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_FIELDS_HPP
#define BOOST_BEAST_HTTP_FIELDS_HPP
#include <boost/beast/core/detail/config.hpp>
#include <boost/beast/core/string_param.hpp>
#include <boost/beast/core/string.hpp>
#include <boost/beast/core/detail/allocator.hpp>
#include <boost/beast/http/field.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/core/empty_value.hpp>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/set.hpp>
#include <boost/optional.hpp>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
namespace boost {
namespace beast {
namespace http {
/** A container for storing HTTP header fields.
This container is designed to store the field value pairs that make
up the fields and trailers in an HTTP message. Objects of this type
are iterable, with each element holding the field name and field
value.
Field names are stored as-is, but comparisons are case-insensitive.
The container behaves as a `std::multiset`; there will be a separate
value for each occurrence of the same field name. When the container
is iterated the fields are presented in the order of insertion, with
fields having the same name following each other consecutively.
Meets the requirements of @b Fields
@tparam Allocator The allocator to use. This must meet the
requirements of @b Allocator.
*/
template<class Allocator>
class basic_fields
#if ! BOOST_BEAST_DOXYGEN
: private boost::empty_value<Allocator>
#endif
{
// Fancy pointers are not supported
static_assert(std::is_pointer<typename
std::allocator_traits<Allocator>::pointer>::value,
"Allocator must use regular pointers");
friend class fields_test; // for `header`
static std::size_t constexpr max_static_buffer = 4096;
struct element;
using off_t = std::uint16_t;
public:
/// The type of allocator used.
using allocator_type = Allocator;
/// The type of element used to represent a field
class value_type
{
friend class basic_fields;
off_t off_;
off_t len_;
field f_;
char*
data() const;
boost::asio::const_buffer
buffer() const;
protected:
value_type(field name,
string_view sname, string_view value);
public:
/// Constructor (deleted)
value_type(value_type const&) = delete;
/// Assignment (deleted)
value_type& operator=(value_type const&) = delete;
/// Returns the field enum, which can be @ref field::unknown
field
name() const;
/// Returns the field name as a string
string_view const
name_string() const;
/// Returns the value of the field
string_view const
value() const;
};
/** A strictly less predicate for comparing keys, using a case-insensitive comparison.
The case-comparison operation is defined only for low-ASCII characters.
*/
#if BOOST_BEAST_DOXYGEN
using key_compare = implementation_defined;
#else
struct key_compare : beast::iless
#endif
{
/// Returns `true` if lhs is less than rhs using a strict ordering
bool
operator()(
string_view lhs,
value_type const& rhs) const noexcept
{
if(lhs.size() < rhs.name_string().size())
return true;
if(lhs.size() > rhs.name_string().size())
return false;
return iless::operator()(lhs, rhs.name_string());
}
/// Returns `true` if lhs is less than rhs using a strict ordering
bool
operator()(
value_type const& lhs,
string_view rhs) const noexcept
{
if(lhs.name_string().size() < rhs.size())
return true;
if(lhs.name_string().size() > rhs.size())
return false;
return iless::operator()(lhs.name_string(), rhs);
}
/// Returns `true` if lhs is less than rhs using a strict ordering
bool
operator()(
value_type const& lhs,
value_type const& rhs) const noexcept
{
if(lhs.name_string().size() < rhs.name_string().size())
return true;
if(lhs.name_string().size() > rhs.name_string().size())
return false;
return iless::operator()(lhs.name_string(), rhs.name_string());
}
};
/// The algorithm used to serialize the header
#if BOOST_BEAST_DOXYGEN
using writer = implementation_defined;
#else
class writer;
#endif
private:
struct element
: public boost::intrusive::list_base_hook<
boost::intrusive::link_mode<
boost::intrusive::normal_link>>
, public boost::intrusive::set_base_hook<
boost::intrusive::link_mode<
boost::intrusive::normal_link>>
, public value_type
{
element(field name,
string_view sname, string_view value);
};
using list_t = typename boost::intrusive::make_list<
element,
boost::intrusive::constant_time_size<false>
>::type;
using set_t = typename boost::intrusive::make_multiset<
element,
boost::intrusive::constant_time_size<true>,
boost::intrusive::compare<key_compare>
>::type;
using align_type = typename
boost::type_with_alignment<alignof(element)>::type;
using rebind_type = typename
beast::detail::allocator_traits<Allocator>::
template rebind_alloc<element>;
using alloc_traits =
beast::detail::allocator_traits<rebind_type>;
using size_type = typename
beast::detail::allocator_traits<Allocator>::size_type;
public:
/// Destructor
~basic_fields();
/// Constructor.
basic_fields() = default;
/** Constructor.
@param alloc The allocator to use.
*/
explicit
basic_fields(Allocator const& alloc) noexcept;
/** Move constructor.
The state of the moved-from object is
as if constructed using the same allocator.
*/
basic_fields(basic_fields&&) noexcept;
/** Move constructor.
The state of the moved-from object is
as if constructed using the same allocator.
@param alloc The allocator to use.
*/
basic_fields(basic_fields&&, Allocator const& alloc);
/// Copy constructor.
basic_fields(basic_fields const&);
/** Copy constructor.
@param alloc The allocator to use.
*/
basic_fields(basic_fields const&, Allocator const& alloc);
/// Copy constructor.
template<class OtherAlloc>
basic_fields(basic_fields<OtherAlloc> const&);
/** Copy constructor.
@param alloc The allocator to use.
*/
template<class OtherAlloc>
basic_fields(basic_fields<OtherAlloc> const&,
Allocator const& alloc);
/** Move assignment.
The state of the moved-from object is
as if constructed using the same allocator.
*/
basic_fields& operator=(basic_fields&&) noexcept(
alloc_traits::propagate_on_container_move_assignment::value);
/// Copy assignment.
basic_fields& operator=(basic_fields const&);
/// Copy assignment.
template<class OtherAlloc>
basic_fields& operator=(basic_fields<OtherAlloc> const&);
public:
/// A constant iterator to the field sequence.
#if BOOST_BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator = typename list_t::const_iterator;
#endif
/// A constant iterator to the field sequence.
using iterator = const_iterator;
/// Return a copy of the allocator associated with the container.
allocator_type
get_allocator() const
{
return this->get();
}
//--------------------------------------------------------------------------
//
// Element access
//
//--------------------------------------------------------------------------
/** Returns the value for a field, or throws an exception.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The name of the field.
@return The field value.
@throws std::out_of_range if the field is not found.
*/
string_view const
at(field name) const;
/** Returns the value for a field, or throws an exception.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The name of the field.
@return The field value.
@throws std::out_of_range if the field is not found.
*/
string_view const
at(string_view name) const;
/** Returns the value for a field, or `""` if it does not exist.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The name of the field.
*/
string_view const
operator[](field name) const;
/** Returns the value for a case-insensitive matching header, or `""` if it does not exist.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The name of the field.
*/
string_view const
operator[](string_view name) const;
//--------------------------------------------------------------------------
//
// Iterators
//
//--------------------------------------------------------------------------
/// Return a const iterator to the beginning of the field sequence.
const_iterator
begin() const
{
return list_.cbegin();
}
/// Return a const iterator to the end of the field sequence.
const_iterator
end() const
{
return list_.cend();
}
/// Return a const iterator to the beginning of the field sequence.
const_iterator
cbegin() const
{
return list_.cbegin();
}
/// Return a const iterator to the end of the field sequence.
const_iterator
cend() const
{
return list_.cend();
}
//--------------------------------------------------------------------------
//
// Capacity
//
//--------------------------------------------------------------------------
private:
// VFALCO Since the header and message derive from Fields,
// what does the expression m.empty() mean? Its confusing.
bool
empty() const
{
return list_.empty();
}
public:
//--------------------------------------------------------------------------
//
// Modifiers
//
//--------------------------------------------------------------------------
/** Remove all fields from the container
All references, pointers, or iterators referring to contained
elements are invalidated. All past-the-end iterators are also
invalidated.
@par Postconditions:
@code
std::distance(this->begin(), this->end()) == 0
@endcode
*/
void
clear();
/** Insert a field.
If one or more fields with the same name already exist,
the new field will be inserted after the last field with
the matching name, in serialization order.
@param name The field name.
@param value The value of the field, as a @ref string_param
*/
void
insert(field name, string_param const& value);
/** Insert a field.
If one or more fields with the same name already exist,
the new field will be inserted after the last field with
the matching name, in serialization order.
@param name The field name.
@param value The value of the field, as a @ref string_param
*/
void
insert(string_view name, string_param const& value);
/** Insert a field.
If one or more fields with the same name already exist,
the new field will be inserted after the last field with
the matching name, in serialization order.
@param name The field name.
@param name_string The literal text corresponding to the
field name. If `name != field::unknown`, then this value
must be equal to `to_string(name)` using a case-insensitive
comparison, otherwise the behavior is undefined.
@param value The value of the field, as a @ref string_param
*/
void
insert(field name, string_view name_string,
string_param const& value);
/** Set a field value, removing any other instances of that field.
First removes any values with matching field names, then
inserts the new field value.
@param name The field name.
@param value The value of the field, as a @ref string_param
@return The field value.
*/
void
set(field name, string_param const& value);
/** Set a field value, removing any other instances of that field.
First removes any values with matching field names, then
inserts the new field value.
@param name The field name.
@param value The value of the field, as a @ref string_param
*/
void
set(string_view name, string_param const& value);
/** Remove a field.
References and iterators to the erased elements are
invalidated. Other references and iterators are not
affected.
@param pos An iterator to the element to remove.
@return An iterator following the last removed element.
If the iterator refers to the last element, the end()
iterator is returned.
*/
const_iterator
erase(const_iterator pos);
/** Remove all fields with the specified name.
All fields with the same field name are erased from the
container.
References and iterators to the erased elements are
invalidated. Other references and iterators are not
affected.
@param name The field name.
@return The number of fields removed.
*/
std::size_t
erase(field name);
/** Remove all fields with the specified name.
All fields with the same field name are erased from the
container.
References and iterators to the erased elements are
invalidated. Other references and iterators are not
affected.
@param name The field name.
@return The number of fields removed.
*/
std::size_t
erase(string_view name);
/** Return a buffer sequence representing the trailers.
This function returns a buffer sequence holding the
serialized representation of the trailer fields promised
in the Accept field. Before calling this function the
Accept field must contain the exact trailer fields
desired. Each field must also exist.
*/
/// Swap this container with another
void
swap(basic_fields& other);
/// Swap two field containers
template<class Alloc>
friend
void
swap(basic_fields<Alloc>& lhs, basic_fields<Alloc>& rhs);
//--------------------------------------------------------------------------
//
// Lookup
//
//--------------------------------------------------------------------------
/** Return the number of fields with the specified name.
@param name The field name.
*/
std::size_t
count(field name) const;
/** Return the number of fields with the specified name.
@param name The field name.
*/
std::size_t
count(string_view name) const;
/** Returns an iterator to the case-insensitive matching field.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The field name.
@return An iterator to the matching field, or `end()` if
no match was found.
*/
const_iterator
find(field name) const;
/** Returns an iterator to the case-insensitive matching field name.
If more than one field with the specified name exists, the
first field defined by insertion order is returned.
@param name The field name.
@return An iterator to the matching field, or `end()` if
no match was found.
*/
const_iterator
find(string_view name) const;
/** Returns a range of iterators to the fields with the specified name.
@param name The field name.
@return A range of iterators to fields with the same name,
otherwise an empty range.
*/
std::pair<const_iterator, const_iterator>
equal_range(field name) const;
/** Returns a range of iterators to the fields with the specified name.
@param name The field name.
@return A range of iterators to fields with the same name,
otherwise an empty range.
*/
std::pair<const_iterator, const_iterator>
equal_range(string_view name) const;
//--------------------------------------------------------------------------
//
// Observers
//
//--------------------------------------------------------------------------
/// Returns a copy of the key comparison function
key_compare
key_comp() const
{
return key_compare{};
}
protected:
/** Returns the request-method string.
@note Only called for requests.
*/
string_view
get_method_impl() const;
/** Returns the request-target string.
@note Only called for requests.
*/
string_view
get_target_impl() const;
/** Returns the response reason-phrase string.
@note Only called for responses.
*/
string_view
get_reason_impl() const;
/** Returns the chunked Transfer-Encoding setting
*/
bool
get_chunked_impl() const;
/** Returns the keep-alive setting
*/
bool
get_keep_alive_impl(unsigned version) const;
/** Returns `true` if the Content-Length field is present.
*/
bool
has_content_length_impl() const;
/** Set or clear the method string.
@note Only called for requests.
*/
void
set_method_impl(string_view s);
/** Set or clear the target string.
@note Only called for requests.
*/
void
set_target_impl(string_view s);
/** Set or clear the reason string.
@note Only called for responses.
*/
void
set_reason_impl(string_view s);
/** Adjusts the chunked Transfer-Encoding value
*/
void
set_chunked_impl(bool value);
/** Sets or clears the Content-Length field
*/
void
set_content_length_impl(
boost::optional<std::uint64_t> const& value);
/** Adjusts the Connection field
*/
void
set_keep_alive_impl(
unsigned version, bool keep_alive);
private:
template<class OtherAlloc>
friend class basic_fields;
element&
new_element(field name,
string_view sname, string_view value);
void
delete_element(element& e);
void
set_element(element& e);
void
realloc_string(string_view& dest, string_view s);
void
realloc_target(
string_view& dest, string_view s);
template<class OtherAlloc>
void
copy_all(basic_fields<OtherAlloc> const&);
void
clear_all();
void
delete_list();
void
move_assign(basic_fields&, std::true_type);
void
move_assign(basic_fields&, std::false_type);
void
copy_assign(basic_fields const&, std::true_type);
void
copy_assign(basic_fields const&, std::false_type);
void
swap(basic_fields& other, std::true_type);
void
swap(basic_fields& other, std::false_type);
set_t set_;
list_t list_;
string_view method_;
string_view target_or_reason_;
};
/// A typical HTTP header fields container
using fields = basic_fields<std::allocator<char>>;
} // http
} // beast
} // boost
#include <boost/beast/http/impl/fields.ipp>
#endif
|