fields.hpp 21 KB

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