fields.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. //
  2. // Copyright (c) 2016-2019 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. #ifndef BOOST_BEAST_HTTP_FIELDS_HPP
  10. #define BOOST_BEAST_HTTP_FIELDS_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/string_param.hpp>
  13. #include <boost/beast/core/string.hpp>
  14. #include <boost/beast/core/detail/allocator.hpp>
  15. #include <boost/beast/http/field.hpp>
  16. #include <boost/asio/buffer.hpp>
  17. #include <boost/core/empty_value.hpp>
  18. #include <boost/intrusive/list.hpp>
  19. #include <boost/intrusive/set.hpp>
  20. #include <boost/optional.hpp>
  21. #include <algorithm>
  22. #include <cctype>
  23. #include <cstring>
  24. #include <memory>
  25. #include <string>
  26. #include <type_traits>
  27. #include <utility>
  28. namespace boost {
  29. namespace beast {
  30. namespace http {
  31. /** A container for storing HTTP header fields.
  32. This container is designed to store the field value pairs that make
  33. up the fields and trailers in an HTTP message. Objects of this type
  34. are iterable, with each element holding the field name and field
  35. value.
  36. Field names are stored as-is, but comparisons are case-insensitive.
  37. The container behaves as a `std::multiset`; there will be a separate
  38. value for each occurrence of the same field name. When the container
  39. is iterated the fields are presented in the order of insertion, with
  40. fields having the same name following each other consecutively.
  41. Meets the requirements of <em>Fields</em>
  42. @tparam Allocator The allocator to use.
  43. */
  44. template<class Allocator>
  45. class basic_fields
  46. #if ! BOOST_BEAST_DOXYGEN
  47. : private boost::empty_value<Allocator>
  48. #endif
  49. {
  50. // Fancy pointers are not supported
  51. static_assert(std::is_pointer<typename
  52. std::allocator_traits<Allocator>::pointer>::value,
  53. "Allocator must use regular pointers");
  54. friend class fields_test; // for `header`
  55. static std::size_t constexpr max_static_buffer = 4096;
  56. struct element;
  57. using off_t = std::uint16_t;
  58. public:
  59. /// The type of allocator used.
  60. using allocator_type = Allocator;
  61. /// The type of element used to represent a field
  62. class value_type
  63. {
  64. friend class basic_fields;
  65. off_t off_;
  66. off_t len_;
  67. field f_;
  68. char*
  69. data() const;
  70. net::const_buffer
  71. buffer() const;
  72. protected:
  73. value_type(field name,
  74. string_view sname, string_view value);
  75. public:
  76. /// Constructor (deleted)
  77. value_type(value_type const&) = delete;
  78. /// Assignment (deleted)
  79. value_type& operator=(value_type const&) = delete;
  80. /// Returns the field enum, which can be @ref field::unknown
  81. field
  82. name() const;
  83. /// Returns the field name as a string
  84. string_view const
  85. name_string() const;
  86. /// Returns the value of the field
  87. string_view const
  88. value() const;
  89. };
  90. /** A strictly less predicate for comparing keys, using a case-insensitive comparison.
  91. The case-comparison operation is defined only for low-ASCII characters.
  92. */
  93. #if BOOST_BEAST_DOXYGEN
  94. using key_compare = __implementation_defined__;
  95. #else
  96. struct key_compare : beast::iless
  97. #endif
  98. {
  99. /// Returns `true` if lhs is less than rhs using a strict ordering
  100. bool
  101. operator()(
  102. string_view lhs,
  103. value_type const& rhs) const noexcept
  104. {
  105. if(lhs.size() < rhs.name_string().size())
  106. return true;
  107. if(lhs.size() > rhs.name_string().size())
  108. return false;
  109. return iless::operator()(lhs, rhs.name_string());
  110. }
  111. /// Returns `true` if lhs is less than rhs using a strict ordering
  112. bool
  113. operator()(
  114. value_type const& lhs,
  115. string_view rhs) const noexcept
  116. {
  117. if(lhs.name_string().size() < rhs.size())
  118. return true;
  119. if(lhs.name_string().size() > rhs.size())
  120. return false;
  121. return iless::operator()(lhs.name_string(), rhs);
  122. }
  123. /// Returns `true` if lhs is less than rhs using a strict ordering
  124. bool
  125. operator()(
  126. value_type const& lhs,
  127. value_type const& rhs) const noexcept
  128. {
  129. if(lhs.name_string().size() < rhs.name_string().size())
  130. return true;
  131. if(lhs.name_string().size() > rhs.name_string().size())
  132. return false;
  133. return iless::operator()(lhs.name_string(), rhs.name_string());
  134. }
  135. };
  136. /// The algorithm used to serialize the header
  137. #if BOOST_BEAST_DOXYGEN
  138. using writer = __implementation_defined__;
  139. #else
  140. class writer;
  141. #endif
  142. private:
  143. struct element
  144. : public boost::intrusive::list_base_hook<
  145. boost::intrusive::link_mode<
  146. boost::intrusive::normal_link>>
  147. , public boost::intrusive::set_base_hook<
  148. boost::intrusive::link_mode<
  149. boost::intrusive::normal_link>>
  150. , public value_type
  151. {
  152. element(field name,
  153. string_view sname, string_view value);
  154. };
  155. using list_t = typename boost::intrusive::make_list<
  156. element,
  157. boost::intrusive::constant_time_size<false>
  158. >::type;
  159. using set_t = typename boost::intrusive::make_multiset<
  160. element,
  161. boost::intrusive::constant_time_size<true>,
  162. boost::intrusive::compare<key_compare>
  163. >::type;
  164. using align_type = typename
  165. boost::type_with_alignment<alignof(element)>::type;
  166. using rebind_type = typename
  167. beast::detail::allocator_traits<Allocator>::
  168. template rebind_alloc<element>;
  169. using alloc_traits =
  170. beast::detail::allocator_traits<rebind_type>;
  171. using size_type = typename
  172. beast::detail::allocator_traits<Allocator>::size_type;
  173. public:
  174. /// Destructor
  175. ~basic_fields();
  176. /// Constructor.
  177. basic_fields() = default;
  178. /** Constructor.
  179. @param alloc The allocator to use.
  180. */
  181. explicit
  182. basic_fields(Allocator const& alloc) noexcept;
  183. /** Move constructor.
  184. The state of the moved-from object is
  185. as if constructed using the same allocator.
  186. */
  187. basic_fields(basic_fields&&) noexcept;
  188. /** Move constructor.
  189. The state of the moved-from object is
  190. as if constructed using the same allocator.
  191. @param alloc The allocator to use.
  192. */
  193. basic_fields(basic_fields&&, Allocator const& alloc);
  194. /// Copy constructor.
  195. basic_fields(basic_fields const&);
  196. /** Copy constructor.
  197. @param alloc The allocator to use.
  198. */
  199. basic_fields(basic_fields const&, Allocator const& alloc);
  200. /// Copy constructor.
  201. template<class OtherAlloc>
  202. basic_fields(basic_fields<OtherAlloc> const&);
  203. /** Copy constructor.
  204. @param alloc The allocator to use.
  205. */
  206. template<class OtherAlloc>
  207. basic_fields(basic_fields<OtherAlloc> const&,
  208. Allocator const& alloc);
  209. /** Move assignment.
  210. The state of the moved-from object is
  211. as if constructed using the same allocator.
  212. */
  213. basic_fields& operator=(basic_fields&&) noexcept(
  214. alloc_traits::propagate_on_container_move_assignment::value);
  215. /// Copy assignment.
  216. basic_fields& operator=(basic_fields const&);
  217. /// Copy assignment.
  218. template<class OtherAlloc>
  219. basic_fields& operator=(basic_fields<OtherAlloc> const&);
  220. public:
  221. /// A constant iterator to the field sequence.
  222. #if BOOST_BEAST_DOXYGEN
  223. using const_iterator = __implementation_defined__;
  224. #else
  225. using const_iterator = typename list_t::const_iterator;
  226. #endif
  227. /// A constant iterator to the field sequence.
  228. using iterator = const_iterator;
  229. /// Return a copy of the allocator associated with the container.
  230. allocator_type
  231. get_allocator() const
  232. {
  233. return this->get();
  234. }
  235. //--------------------------------------------------------------------------
  236. //
  237. // Element access
  238. //
  239. //--------------------------------------------------------------------------
  240. /** Returns the value for a field, or throws an exception.
  241. If more than one field with the specified name exists, the
  242. first field defined by insertion order is returned.
  243. @param name The name of the field.
  244. @return The field value.
  245. @throws std::out_of_range if the field is not found.
  246. */
  247. string_view const
  248. at(field name) const;
  249. /** Returns the value for a field, or throws an exception.
  250. If more than one field with the specified name exists, the
  251. first field defined by insertion order is returned.
  252. @param name The name of the field.
  253. @return The field value.
  254. @throws std::out_of_range if the field is not found.
  255. */
  256. string_view const
  257. at(string_view name) const;
  258. /** Returns the value for a field, or `""` if it does not exist.
  259. If more than one field with the specified name exists, the
  260. first field defined by insertion order is returned.
  261. @param name The name of the field.
  262. */
  263. string_view const
  264. operator[](field name) const;
  265. /** Returns the value for a case-insensitive matching header, or `""` if it does not exist.
  266. If more than one field with the specified name exists, the
  267. first field defined by insertion order is returned.
  268. @param name The name of the field.
  269. */
  270. string_view const
  271. operator[](string_view name) const;
  272. //--------------------------------------------------------------------------
  273. //
  274. // Iterators
  275. //
  276. //--------------------------------------------------------------------------
  277. /// Return a const iterator to the beginning of the field sequence.
  278. const_iterator
  279. begin() const
  280. {
  281. return list_.cbegin();
  282. }
  283. /// Return a const iterator to the end of the field sequence.
  284. const_iterator
  285. end() const
  286. {
  287. return list_.cend();
  288. }
  289. /// Return a const iterator to the beginning of the field sequence.
  290. const_iterator
  291. cbegin() const
  292. {
  293. return list_.cbegin();
  294. }
  295. /// Return a const iterator to the end of the field sequence.
  296. const_iterator
  297. cend() const
  298. {
  299. return list_.cend();
  300. }
  301. //--------------------------------------------------------------------------
  302. //
  303. // Capacity
  304. //
  305. //--------------------------------------------------------------------------
  306. private:
  307. // VFALCO Since the header and message derive from Fields,
  308. // what does the expression m.empty() mean? Its confusing.
  309. bool
  310. empty() const
  311. {
  312. return list_.empty();
  313. }
  314. public:
  315. //--------------------------------------------------------------------------
  316. //
  317. // Modifiers
  318. //
  319. //--------------------------------------------------------------------------
  320. /** Remove all fields from the container
  321. All references, pointers, or iterators referring to contained
  322. elements are invalidated. All past-the-end iterators are also
  323. invalidated.
  324. @par Postconditions:
  325. @code
  326. std::distance(this->begin(), this->end()) == 0
  327. @endcode
  328. */
  329. void
  330. clear();
  331. /** Insert a field.
  332. If one or more fields with the same name already exist,
  333. the new field will be inserted after the last field with
  334. the matching name, in serialization order.
  335. @param name The field name.
  336. @param value The value of the field, as a @ref string_param
  337. */
  338. void
  339. insert(field name, string_param const& value);
  340. /** Insert a field.
  341. If one or more fields with the same name already exist,
  342. the new field will be inserted after the last field with
  343. the matching name, in serialization order.
  344. @param name The field name.
  345. @param value The value of the field, as a @ref string_param
  346. */
  347. void
  348. insert(string_view name, string_param const& value);
  349. /** Insert a field.
  350. If one or more fields with the same name already exist,
  351. the new field will be inserted after the last field with
  352. the matching name, in serialization order.
  353. @param name The field name.
  354. @param name_string The literal text corresponding to the
  355. field name. If `name != field::unknown`, then this value
  356. must be equal to `to_string(name)` using a case-insensitive
  357. comparison, otherwise the behavior is undefined.
  358. @param value The value of the field, as a @ref string_param
  359. */
  360. void
  361. insert(field name, string_view name_string,
  362. string_param const& value);
  363. /** Set a field value, removing any other instances of that field.
  364. First removes any values with matching field names, then
  365. inserts the new field value.
  366. @param name The field name.
  367. @param value The value of the field, as a @ref string_param
  368. @return The field value.
  369. */
  370. void
  371. set(field name, string_param const& value);
  372. /** Set a field value, removing any other instances of that field.
  373. First removes any values with matching field names, then
  374. inserts the new field value.
  375. @param name The field name.
  376. @param value The value of the field, as a @ref string_param
  377. */
  378. void
  379. set(string_view name, string_param const& value);
  380. /** Remove a field.
  381. References and iterators to the erased elements are
  382. invalidated. Other references and iterators are not
  383. affected.
  384. @param pos An iterator to the element to remove.
  385. @return An iterator following the last removed element.
  386. If the iterator refers to the last element, the end()
  387. iterator is returned.
  388. */
  389. const_iterator
  390. erase(const_iterator pos);
  391. /** Remove all fields with the specified name.
  392. All fields with the same field name are erased from the
  393. container.
  394. References and iterators to the erased elements are
  395. invalidated. Other references and iterators are not
  396. affected.
  397. @param name The field name.
  398. @return The number of fields removed.
  399. */
  400. std::size_t
  401. erase(field name);
  402. /** Remove all fields with the specified name.
  403. All fields with the same field name are erased from the
  404. container.
  405. References and iterators to the erased elements are
  406. invalidated. Other references and iterators are not
  407. affected.
  408. @param name The field name.
  409. @return The number of fields removed.
  410. */
  411. std::size_t
  412. erase(string_view name);
  413. /** Return a buffer sequence representing the trailers.
  414. This function returns a buffer sequence holding the
  415. serialized representation of the trailer fields promised
  416. in the Accept field. Before calling this function the
  417. Accept field must contain the exact trailer fields
  418. desired. Each field must also exist.
  419. */
  420. /// Swap this container with another
  421. void
  422. swap(basic_fields& other);
  423. /// Swap two field containers
  424. template<class Alloc>
  425. friend
  426. void
  427. swap(basic_fields<Alloc>& lhs, basic_fields<Alloc>& rhs);
  428. //--------------------------------------------------------------------------
  429. //
  430. // Lookup
  431. //
  432. //--------------------------------------------------------------------------
  433. /** Return the number of fields with the specified name.
  434. @param name The field name.
  435. */
  436. std::size_t
  437. count(field name) const;
  438. /** Return the number of fields with the specified name.
  439. @param name The field name.
  440. */
  441. std::size_t
  442. count(string_view name) const;
  443. /** Returns an iterator to the case-insensitive matching field.
  444. If more than one field with the specified name exists, the
  445. first field defined by insertion order is returned.
  446. @param name The field name.
  447. @return An iterator to the matching field, or `end()` if
  448. no match was found.
  449. */
  450. const_iterator
  451. find(field name) const;
  452. /** Returns an iterator to the case-insensitive matching field name.
  453. If more than one field with the specified name exists, the
  454. first field defined by insertion order is returned.
  455. @param name The field name.
  456. @return An iterator to the matching field, or `end()` if
  457. no match was found.
  458. */
  459. const_iterator
  460. find(string_view name) const;
  461. /** Returns a range of iterators to the fields with the specified name.
  462. @param name The field name.
  463. @return A range of iterators to fields with the same name,
  464. otherwise an empty range.
  465. */
  466. std::pair<const_iterator, const_iterator>
  467. equal_range(field name) const;
  468. /** Returns a range of iterators to the fields with the specified name.
  469. @param name The field name.
  470. @return A range of iterators to fields with the same name,
  471. otherwise an empty range.
  472. */
  473. std::pair<const_iterator, const_iterator>
  474. equal_range(string_view name) const;
  475. //--------------------------------------------------------------------------
  476. //
  477. // Observers
  478. //
  479. //--------------------------------------------------------------------------
  480. /// Returns a copy of the key comparison function
  481. key_compare
  482. key_comp() const
  483. {
  484. return key_compare{};
  485. }
  486. protected:
  487. /** Returns the request-method string.
  488. @note Only called for requests.
  489. */
  490. string_view
  491. get_method_impl() const;
  492. /** Returns the request-target string.
  493. @note Only called for requests.
  494. */
  495. string_view
  496. get_target_impl() const;
  497. /** Returns the response reason-phrase string.
  498. @note Only called for responses.
  499. */
  500. string_view
  501. get_reason_impl() const;
  502. /** Returns the chunked Transfer-Encoding setting
  503. */
  504. bool
  505. get_chunked_impl() const;
  506. /** Returns the keep-alive setting
  507. */
  508. bool
  509. get_keep_alive_impl(unsigned version) const;
  510. /** Returns `true` if the Content-Length field is present.
  511. */
  512. bool
  513. has_content_length_impl() const;
  514. /** Set or clear the method string.
  515. @note Only called for requests.
  516. */
  517. void
  518. set_method_impl(string_view s);
  519. /** Set or clear the target string.
  520. @note Only called for requests.
  521. */
  522. void
  523. set_target_impl(string_view s);
  524. /** Set or clear the reason string.
  525. @note Only called for responses.
  526. */
  527. void
  528. set_reason_impl(string_view s);
  529. /** Adjusts the chunked Transfer-Encoding value
  530. */
  531. void
  532. set_chunked_impl(bool value);
  533. /** Sets or clears the Content-Length field
  534. */
  535. void
  536. set_content_length_impl(
  537. boost::optional<std::uint64_t> const& value);
  538. /** Adjusts the Connection field
  539. */
  540. void
  541. set_keep_alive_impl(
  542. unsigned version, bool keep_alive);
  543. private:
  544. template<class OtherAlloc>
  545. friend class basic_fields;
  546. element&
  547. new_element(field name,
  548. string_view sname, string_view value);
  549. void
  550. delete_element(element& e);
  551. void
  552. set_element(element& e);
  553. void
  554. realloc_string(string_view& dest, string_view s);
  555. void
  556. realloc_target(
  557. string_view& dest, string_view s);
  558. template<class OtherAlloc>
  559. void
  560. copy_all(basic_fields<OtherAlloc> const&);
  561. void
  562. clear_all();
  563. void
  564. delete_list();
  565. void
  566. move_assign(basic_fields&, std::true_type);
  567. void
  568. move_assign(basic_fields&, std::false_type);
  569. void
  570. copy_assign(basic_fields const&, std::true_type);
  571. void
  572. copy_assign(basic_fields const&, std::false_type);
  573. void
  574. swap(basic_fields& other, std::true_type);
  575. void
  576. swap(basic_fields& other, std::false_type);
  577. set_t set_;
  578. list_t list_;
  579. string_view method_;
  580. string_view target_or_reason_;
  581. };
  582. /// A typical HTTP header fields container
  583. using fields = basic_fields<std::allocator<char>>;
  584. } // http
  585. } // beast
  586. } // boost
  587. #include <boost/beast/http/impl/fields.hpp>
  588. #endif