fields.hpp 20 KB

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