ゴミ箱
fields.hpp
Go to the documentation of this file.
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_FIELDS_HPP
11 #define BOOST_BEAST_HTTP_FIELDS_HPP
12 
13 #include <boost/beast/config.hpp>
18 #include <boost/asio/buffer.hpp>
19 #include <boost/intrusive/list.hpp>
20 #include <boost/intrusive/set.hpp>
21 #include <boost/optional.hpp>
22 #include <algorithm>
23 #include <cctype>
24 #include <cstring>
25 #include <memory>
26 #include <string>
27 #include <type_traits>
28 #include <utility>
29 
30 namespace boost {
31 namespace beast {
32 namespace http {
33 
52 template<class Allocator>
54 {
55  friend class fields_test; // for `header`
56 
57  static std::size_t constexpr max_static_buffer = 4096;
58 
59  using off_t = std::uint16_t;
60 
61 public:
63  using allocator_type = Allocator;
64 
66  class value_type
67  {
68  friend class basic_fields;
69 
70  boost::asio::const_buffer
71  buffer() const;
72 
75 
76  boost::intrusive::list_member_hook<
77  boost::intrusive::link_mode<
78  boost::intrusive::normal_link>>
79  list_hook_;
80  boost::intrusive::set_member_hook<
81  boost::intrusive::link_mode<
82  boost::intrusive::normal_link>>
83  set_hook_;
84  off_t off_;
85  off_t len_;
86  field f_;
87 
88  public:
90  value_type(value_type const&) = delete;
91 
93  value_type& operator=(value_type const&) = delete;
94 
96  field
97  name() const;
98 
101  name_string() const;
102 
105  value() const;
106  };
107 
113  {
115  template<class String>
116  bool
118  String const& lhs,
119  value_type const& rhs) const noexcept
120  {
121  if(lhs.size() < rhs.name_string().size())
122  return true;
123  if(lhs.size() > rhs.name_string().size())
124  return false;
125  return iless::operator()(lhs, rhs.name_string());
126  }
127 
129  template<class String>
130  bool
132  value_type const& lhs,
133  String const& rhs) const noexcept
134  {
135  if(lhs.name_string().size() < rhs.size())
136  return true;
137  if(lhs.name_string().size() > rhs.size())
138  return false;
139  return iless::operator()(lhs.name_string(), rhs);
140  }
141 
143  bool
145  value_type const& lhs,
146  value_type const& rhs) const noexcept
147  {
148  if(lhs.name_string().size() < rhs.name_string().size())
149  return true;
150  if(lhs.name_string().size() > rhs.name_string().size())
151  return false;
152  return iless::operator()(lhs.name_string(), rhs.name_string());
153  }
154  };
155 
157 #if BOOST_BEAST_DOXYGEN
158  using reader = implementation_defined;
159 #else
160  class reader;
161 #endif
162 
163 private:
164  using list_t = typename boost::intrusive::make_list<
165  value_type, boost::intrusive::member_hook<
166  value_type, boost::intrusive::list_member_hook<
167  boost::intrusive::link_mode<
168  boost::intrusive::normal_link>>,
169  &value_type::list_hook_>,
170  boost::intrusive::constant_time_size<
171  false>>::type;
172 
173  using set_t = typename boost::intrusive::make_multiset<
174  value_type, boost::intrusive::member_hook<value_type,
175  boost::intrusive::set_member_hook<
176  boost::intrusive::link_mode<
177  boost::intrusive::normal_link>>,
178  &value_type::set_hook_>,
179  boost::intrusive::constant_time_size<true>,
180  boost::intrusive::compare<key_compare>>::type;
181 
182 
183 public:
185  ~basic_fields();
186 
188  basic_fields() = default;
189 
194  explicit
195  basic_fields(Allocator const& alloc);
196 
203 
211  basic_fields(basic_fields&&, Allocator const& alloc);
212 
214  basic_fields(basic_fields const&);
215 
220  basic_fields(basic_fields const&, Allocator const& alloc);
221 
223  template<class OtherAlloc>
225 
230  template<class OtherAlloc>
232  Allocator const& alloc);
233 
240 
243 
245  template<class OtherAlloc>
247 
248 public:
250 #if BOOST_BEAST_DOXYGEN
251  using const_iterator = implementation_defined;
252 #else
253  using const_iterator = typename list_t::const_iterator;
254 #endif
255 
258 
262  {
263  return alloc_;
264  }
265 
266  //--------------------------------------------------------------------------
267  //
268  // Element access
269  //
270  //--------------------------------------------------------------------------
271 
284  at(field name) const;
285 
298  at(string_view name) const;
299 
308  operator[](field name) const;
309 
318  operator[](string_view name) const;
319 
320  //--------------------------------------------------------------------------
321  //
322  // Iterators
323  //
324  //--------------------------------------------------------------------------
325 
328  begin() const
329  {
330  return list_.cbegin();
331  }
332 
335  end() const
336  {
337  return list_.cend();
338  }
339 
342  cbegin() const
343  {
344  return list_.cbegin();
345  }
346 
349  cend() const
350  {
351  return list_.cend();
352  }
353 
354  //--------------------------------------------------------------------------
355  //
356  // Capacity
357  //
358  //--------------------------------------------------------------------------
359 
360 private:
361  // VFALCO Since the header and message derive from Fields,
362  // what does the expression m.empty() mean? Its confusing.
363  bool
364  empty() const
365  {
366  return list_.empty();
367  }
368 public:
369 
370  //--------------------------------------------------------------------------
371  //
372  // Modifiers
373  //
374  //--------------------------------------------------------------------------
375 
376 private:
377  // VFALCO But this leaves behind the method, target, and reason!
384  void
385  clear();
386 public:
387 
398  void
399  insert(field name, string_param const& value);
400 
411  void
412  insert(string_view name, string_param const& value);
413 
429  void
431  string_param const& value);
432 
444  void
445  set(field name, string_param const& value);
446 
456  void
457  set(string_view name, string_param const& value);
458 
472  erase(const_iterator pos);
473 
486  std::size_t
487  erase(field name);
488 
501  std::size_t
502  erase(string_view name);
503 
514  void
516  swap(basic_fields& other);
517 
519  template<class Alloc>
520  friend
521  void
523 
524  //--------------------------------------------------------------------------
525  //
526  // Lookup
527  //
528  //--------------------------------------------------------------------------
529 
534  std::size_t
535  count(field name) const;
536 
541  std::size_t
542  count(string_view name) const;
543 
555  find(field name) const;
556 
568  find(string_view name) const;
569 
577  std::pair<const_iterator, const_iterator>
578  equal_range(field name) const;
579 
587  std::pair<const_iterator, const_iterator>
588  equal_range(string_view name) const;
589 
590  //--------------------------------------------------------------------------
591  //
592  // Observers
593  //
594  //--------------------------------------------------------------------------
595 
598  key_comp() const
599  {
600  return key_compare{};
601  }
602 
603 protected:
609  get_method_impl() const;
610 
616  get_target_impl() const;
617 
623  get_reason_impl() const;
624 
627  bool
628  get_chunked_impl() const;
629 
632  bool
633  get_keep_alive_impl(unsigned version) const;
634 
639  void
641 
646  void
648 
653  void
655 
658  void
659  set_chunked_impl(bool value);
660 
663  void
665  boost::optional<std::uint64_t> const& value);
666 
669  void
671  unsigned version, bool keep_alive);
672 
673 private:
674  template<class OtherAlloc>
675  friend class basic_fields;
676 
677  using base_alloc_type = typename
679  template rebind_alloc<value_type>;
680 
681  using alloc_traits =
683 
684  using size_type = typename
686 
687  value_type&
688  new_element(field name,
689  string_view sname, string_view value);
690 
691  void
692  delete_element(value_type& e);
693 
694  void
695  set_element(value_type& e);
696 
697  void
698  realloc_string(string_view& dest, string_view s);
699 
700  void
701  realloc_target(
702  string_view& dest, string_view s);
703 
704  template<class OtherAlloc>
705  void
706  copy_all(basic_fields<OtherAlloc> const&);
707 
708  void
709  clear_all();
710 
711  void
712  delete_list();
713 
714  void
715  move_assign(basic_fields&, std::true_type);
716 
717  void
718  move_assign(basic_fields&, std::false_type);
719 
720  void
721  copy_assign(basic_fields const&, std::true_type);
722 
723  void
724  copy_assign(basic_fields const&, std::false_type);
725 
726  void
727  swap(basic_fields& other, std::true_type);
728 
729  void
730  swap(basic_fields& other, std::false_type);
731 
732  base_alloc_type alloc_;
733  set_t set_;
734  list_t list_;
735  string_view method_;
736  string_view target_or_reason_;
737 };
738 
741 
742 } // http
743 } // beast
744 } // boost
745 
747 
748 #endif
void set_keep_alive_impl(unsigned version, bool keep_alive)
Definition: fields.ipp:1084
void set_target_impl(string_view s)
Definition: fields.ipp:956
Definition: async_result.hpp:20
field
Definition: field.hpp:21
std::pair< const_iterator, const_iterator > equal_range(field name) const
Definition: fields.ipp:713
bool operator()(String const &lhs, value_type const &rhs) const noexcept
Returns true if lhs is less than rhs using a strict ordering.
Definition: fields.hpp:117
const_iterator iterator
A constant iterator to the field sequence.
Definition: fields.hpp:257
string_view get_method_impl() const
Definition: fields.ipp:880
string_view get_reason_impl() const
Definition: fields.ipp:902
string_view value() const
Returns the value of the field.
Definition: fields.ipp:321
Definition: beast_common.hpp:6
const_iterator begin() const
Return a const iterator to the beginning of the field sequence.
Definition: fields.hpp:328
Definition: string_param.hpp:42
bool operator()(value_type const &lhs, value_type const &rhs) const noexcept
Returns true if lhs is less than rhs using a strict ordering.
Definition: fields.hpp:144
Definition: fields.hpp:53
const_iterator end() const
Return a const iterator to the end of the field sequence.
Definition: fields.hpp:335
field name() const
Returns the field enum, which can be field::unknown.
Definition: fields.ipp:299
bool operator()(value_type const &lhs, String const &rhs) const noexcept
Returns true if lhs is less than rhs using a strict ordering.
Definition: fields.hpp:131
The type of element used to represent a field.
Definition: fields.hpp:66
bool get_chunked_impl() const
Definition: fields.ipp:910
string_view at(field name) const
Definition: fields.ipp:472
void set_method_impl(string_view s)
Definition: fields.ipp:947
void insert(field name, string_param const &value)
Definition: fields.ipp:537
value_type & operator=(value_type const &)=delete
Assignment (deleted)
boost::string_ref string_view
The type of string view used by the library.
Definition: string.hpp:36
~basic_fields()
Destructor.
Definition: fields.ipp:344
string_view get_target_impl() const
Definition: fields.ipp:889
void set_content_length_impl(boost::optional< std::uint64_t > const &value)
Definition: fields.ipp:1072
allocator_type get_allocator() const
Return a copy of the allocator associated with the container.
Definition: fields.hpp:261
const_iterator find(field name) const
Definition: fields.ipp:689
Allocator allocator_type
The type of allocator used.
Definition: fields.hpp:63
void swap(basic_fields &other)
Swap this container with another.
Definition: fields.ipp:646
std::allocator_traits< Alloc > allocator_traits
Definition: allocator.hpp:34
void set_reason_impl(string_view s)
Definition: fields.ipp:966
bool get_keep_alive_impl(unsigned version) const
Definition: fields.ipp:927
Definition: string.hpp:118
string_view operator[](field name) const
Definition: fields.ipp:497
string_view name_string() const
Returns the field name as a string.
Definition: fields.ipp:309
friend class fields_test
Definition: fields.hpp:55
const_iterator erase(const_iterator pos)
Definition: fields.ipp:607
typename list_t::const_iterator const_iterator
A constant iterator to the field sequence.
Definition: fields.hpp:253
key_compare key_comp() const
Returns a copy of the key comparison function.
Definition: fields.hpp:598
void set_chunked_impl(bool value)
Definition: fields.ipp:975
const_iterator cend() const
Return a const iterator to the end of the field sequence.
Definition: fields.hpp:349
friend class basic_fields
Definition: fields.hpp:68
const_iterator cbegin() const
Return a const iterator to the beginning of the field sequence.
Definition: fields.hpp:342
bool operator()(string_view lhs, string_view rhs) const
Definition: string.hpp:121
std::size_t count(field name) const
Definition: fields.ipp:671