fields.hpp 20 KB

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