fields.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. //
  2. // Copyright (c) 2016-2017 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/core/detail/empty_base_optimization.hpp>
  16. #include <boost/beast/http/field.hpp>
  17. #include <boost/asio/buffer.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 @b Fields
  42. @tparam Allocator The allocator to use. This must meet the
  43. requirements of @b Allocator.
  44. */
  45. template<class Allocator>
  46. class basic_fields
  47. #if ! BOOST_BEAST_DOXYGEN
  48. : private beast::detail::empty_base_optimization<Allocator>
  49. #endif
  50. {
  51. // Fancy pointers are not supported
  52. static_assert(std::is_pointer<typename
  53. std::allocator_traits<Allocator>::pointer>::value,
  54. "Allocator must use regular pointers");
  55. friend class fields_test; // for `header`
  56. static std::size_t constexpr max_static_buffer = 4096;
  57. using off_t = std::uint16_t;
  58. public:
  59. /// The type of allocator used.
  60. using allocator_type = Allocator;
  61. /// The type of element used to represent a field
  62. class value_type
  63. {
  64. friend class basic_fields;
  65. boost::asio::const_buffer
  66. buffer() const;
  67. value_type(field name,
  68. string_view sname, string_view value);
  69. boost::intrusive::list_member_hook<
  70. boost::intrusive::link_mode<
  71. boost::intrusive::normal_link>>
  72. list_hook_;
  73. boost::intrusive::set_member_hook<
  74. boost::intrusive::link_mode<
  75. boost::intrusive::normal_link>>
  76. set_hook_;
  77. off_t off_;
  78. off_t len_;
  79. field f_;
  80. public:
  81. /// Constructor (deleted)
  82. value_type(value_type const&) = delete;
  83. /// Assignment (deleted)
  84. value_type& operator=(value_type const&) = delete;
  85. /// Returns the field enum, which can be @ref field::unknown
  86. field
  87. name() const;
  88. /// Returns the field name as a string
  89. string_view const
  90. name_string() const;
  91. /// Returns the value of the field
  92. string_view const
  93. value() const;
  94. };
  95. /** A strictly less predicate for comparing keys, using a case-insensitive comparison.
  96. The case-comparison operation is defined only for low-ASCII characters.
  97. */
  98. struct key_compare : beast::iless
  99. {
  100. /// Returns `true` if lhs is less than rhs using a strict ordering
  101. template<class String>
  102. bool
  103. operator()(
  104. String const& 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. template<class String>
  115. bool
  116. operator()(
  117. value_type const& lhs,
  118. String const& rhs) const noexcept
  119. {
  120. if(lhs.name_string().size() < rhs.size())
  121. return true;
  122. if(lhs.name_string().size() > rhs.size())
  123. return false;
  124. return iless::operator()(lhs.name_string(), rhs);
  125. }
  126. /// Returns `true` if lhs is less than rhs using a strict ordering
  127. bool
  128. operator()(
  129. value_type const& lhs,
  130. value_type const& rhs) const noexcept
  131. {
  132. if(lhs.name_string().size() < rhs.name_string().size())
  133. return true;
  134. if(lhs.name_string().size() > rhs.name_string().size())
  135. return false;
  136. return iless::operator()(lhs.name_string(), rhs.name_string());
  137. }
  138. };
  139. /// The algorithm used to serialize the header
  140. #if BOOST_BEAST_DOXYGEN
  141. using writer = implementation_defined;
  142. #else
  143. class writer;
  144. #endif
  145. private:
  146. using list_t = typename boost::intrusive::make_list<
  147. value_type, boost::intrusive::member_hook<
  148. value_type, boost::intrusive::list_member_hook<
  149. boost::intrusive::link_mode<
  150. boost::intrusive::normal_link>>,
  151. &value_type::list_hook_>,
  152. boost::intrusive::constant_time_size<
  153. false>>::type;
  154. using set_t = typename boost::intrusive::make_multiset<
  155. value_type, boost::intrusive::member_hook<value_type,
  156. boost::intrusive::set_member_hook<
  157. boost::intrusive::link_mode<
  158. boost::intrusive::normal_link>>,
  159. &value_type::set_hook_>,
  160. boost::intrusive::constant_time_size<true>,
  161. boost::intrusive::compare<key_compare>>::type;
  162. using align_type = typename
  163. boost::type_with_alignment<alignof(value_type)>::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->member();
  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_param
  335. */
  336. void
  337. insert(field name, string_param const& value);
  338. /** Insert a field.
  339. If one or more fields with the same name already exist,
  340. the new field will be inserted after the last field with
  341. the matching name, in serialization order.
  342. @param name The field name.
  343. @param value The value of the field, as a @ref string_param
  344. */
  345. void
  346. insert(string_view name, string_param const& value);
  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. @param name The field name.
  352. @param name_string The literal text corresponding to the
  353. field name. If `name != field::unknown`, then this value
  354. must be equal to `to_string(name)` using a case-insensitive
  355. comparison, otherwise the behavior is undefined.
  356. @param value The value of the field, as a @ref string_param
  357. */
  358. void
  359. insert(field name, string_view name_string,
  360. string_param const& value);
  361. /** Set a field value, removing any other instances of that field.
  362. First removes any values with matching field names, then
  363. inserts the new field value.
  364. @param name The field name.
  365. @param value The value of the field, as a @ref string_param
  366. @return The field value.
  367. */
  368. void
  369. set(field name, string_param const& value);
  370. /** Set a field value, removing any other instances of that field.
  371. First removes any values with matching field names, then
  372. inserts the new field value.
  373. @param name The field name.
  374. @param value The value of the field, as a @ref string_param
  375. */
  376. void
  377. set(string_view name, string_param const& value);
  378. /** Remove a field.
  379. References and iterators to the erased elements are
  380. invalidated. Other references and iterators are not
  381. affected.
  382. @param pos An iterator to the element to remove.
  383. @return An iterator following the last removed element.
  384. If the iterator refers to the last element, the end()
  385. iterator is returned.
  386. */
  387. const_iterator
  388. erase(const_iterator pos);
  389. /** Remove all fields with the specified name.
  390. All fields with the same field name are erased from the
  391. container.
  392. References and iterators to the erased elements are
  393. invalidated. Other references and iterators are not
  394. affected.
  395. @param name The field name.
  396. @return The number of fields removed.
  397. */
  398. std::size_t
  399. erase(field name);
  400. /** Remove all fields with the specified name.
  401. All fields with the same field name are erased from the
  402. container.
  403. References and iterators to the erased elements are
  404. invalidated. Other references and iterators are not
  405. affected.
  406. @param name The field name.
  407. @return The number of fields removed.
  408. */
  409. std::size_t
  410. erase(string_view name);
  411. /** Return a buffer sequence representing the trailers.
  412. This function returns a buffer sequence holding the
  413. serialized representation of the trailer fields promised
  414. in the Accept field. Before calling this function the
  415. Accept field must contain the exact trailer fields
  416. desired. Each field must also exist.
  417. */
  418. /// Swap this container with another
  419. void
  420. swap(basic_fields& other);
  421. /// Swap two field containers
  422. template<class Alloc>
  423. friend
  424. void
  425. swap(basic_fields<Alloc>& lhs, basic_fields<Alloc>& rhs);
  426. //--------------------------------------------------------------------------
  427. //
  428. // Lookup
  429. //
  430. //--------------------------------------------------------------------------
  431. /** Return the number of fields with the specified name.
  432. @param name The field name.
  433. */
  434. std::size_t
  435. count(field name) const;
  436. /** Return the number of fields with the specified name.
  437. @param name The field name.
  438. */
  439. std::size_t
  440. count(string_view name) const;
  441. /** Returns an iterator to the case-insensitive matching field.
  442. If more than one field with the specified name exists, the
  443. first field defined by insertion order is returned.
  444. @param name The field name.
  445. @return An iterator to the matching field, or `end()` if
  446. no match was found.
  447. */
  448. const_iterator
  449. find(field name) const;
  450. /** Returns an iterator to the case-insensitive matching field name.
  451. If more than one field with the specified name exists, the
  452. first field defined by insertion order is returned.
  453. @param name The field name.
  454. @return An iterator to the matching field, or `end()` if
  455. no match was found.
  456. */
  457. const_iterator
  458. find(string_view name) const;
  459. /** Returns a range of iterators to the fields with the specified name.
  460. @param name The field name.
  461. @return A range of iterators to fields with the same name,
  462. otherwise an empty range.
  463. */
  464. std::pair<const_iterator, const_iterator>
  465. equal_range(field name) const;
  466. /** Returns a range of iterators to the fields with the specified name.
  467. @param name The field name.
  468. @return A range of iterators to fields with the same name,
  469. otherwise an empty range.
  470. */
  471. std::pair<const_iterator, const_iterator>
  472. equal_range(string_view name) const;
  473. //--------------------------------------------------------------------------
  474. //
  475. // Observers
  476. //
  477. //--------------------------------------------------------------------------
  478. /// Returns a copy of the key comparison function
  479. key_compare
  480. key_comp() const
  481. {
  482. return key_compare{};
  483. }
  484. protected:
  485. /** Returns the request-method string.
  486. @note Only called for requests.
  487. */
  488. string_view
  489. get_method_impl() const;
  490. /** Returns the request-target string.
  491. @note Only called for requests.
  492. */
  493. string_view
  494. get_target_impl() const;
  495. /** Returns the response reason-phrase string.
  496. @note Only called for responses.
  497. */
  498. string_view
  499. get_reason_impl() const;
  500. /** Returns the chunked Transfer-Encoding setting
  501. */
  502. bool
  503. get_chunked_impl() const;
  504. /** Returns the keep-alive setting
  505. */
  506. bool
  507. get_keep_alive_impl(unsigned version) const;
  508. /** Returns `true` if the Content-Length field is present.
  509. */
  510. bool
  511. has_content_length_impl() const;
  512. /** Set or clear the method string.
  513. @note Only called for requests.
  514. */
  515. void
  516. set_method_impl(string_view s);
  517. /** Set or clear the target string.
  518. @note Only called for requests.
  519. */
  520. void
  521. set_target_impl(string_view s);
  522. /** Set or clear the reason string.
  523. @note Only called for responses.
  524. */
  525. void
  526. set_reason_impl(string_view s);
  527. /** Adjusts the chunked Transfer-Encoding value
  528. */
  529. void
  530. set_chunked_impl(bool value);
  531. /** Sets or clears the Content-Length field
  532. */
  533. void
  534. set_content_length_impl(
  535. boost::optional<std::uint64_t> const& value);
  536. /** Adjusts the Connection field
  537. */
  538. void
  539. set_keep_alive_impl(
  540. unsigned version, bool keep_alive);
  541. private:
  542. template<class OtherAlloc>
  543. friend class basic_fields;
  544. value_type&
  545. new_element(field name,
  546. string_view sname, string_view value);
  547. void
  548. delete_element(value_type& e);
  549. void
  550. set_element(value_type& e);
  551. void
  552. realloc_string(string_view& dest, string_view s);
  553. void
  554. realloc_target(
  555. string_view& dest, string_view s);
  556. template<class OtherAlloc>
  557. void
  558. copy_all(basic_fields<OtherAlloc> const&);
  559. void
  560. clear_all();
  561. void
  562. delete_list();
  563. void
  564. move_assign(basic_fields&, std::true_type);
  565. void
  566. move_assign(basic_fields&, std::false_type);
  567. void
  568. copy_assign(basic_fields const&, std::true_type);
  569. void
  570. copy_assign(basic_fields const&, std::false_type);
  571. void
  572. swap(basic_fields& other, std::true_type);
  573. void
  574. swap(basic_fields& other, std::false_type);
  575. set_t set_;
  576. list_t list_;
  577. string_view method_;
  578. string_view target_or_reason_;
  579. };
  580. /// A typical HTTP header fields container
  581. using fields = basic_fields<std::allocator<char>>;
  582. } // http
  583. } // beast
  584. } // boost
  585. #include <boost/beast/http/impl/fields.ipp>
  586. #endif