fields.hpp 21 KB

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