rfc7230.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_RFC7230_HPP
  10. #define BOOST_BEAST_HTTP_RFC7230_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/http/detail/rfc7230.hpp>
  13. #include <boost/beast/http/detail/basic_parsed_list.hpp>
  14. namespace boost {
  15. namespace beast {
  16. namespace http {
  17. /** A list of parameters in an HTTP extension field value.
  18. This container allows iteration of the parameter list in an HTTP
  19. extension. The parameter list is a series of name/value pairs
  20. with each pair starting with a semicolon. The value is optional.
  21. If a parsing error is encountered while iterating the string,
  22. the behavior of the container will be as if a string containing
  23. only characters up to but excluding the first invalid character
  24. was used to construct the list.
  25. @par BNF
  26. @code
  27. param-list = *( OWS ";" OWS param )
  28. param = token OWS [ "=" OWS ( token / quoted-string ) ]
  29. @endcode
  30. To use this class, construct with the string to be parsed and
  31. then use @ref begin and @ref end, or range-for to iterate each
  32. item:
  33. @par Example
  34. @code
  35. for(auto const& param : param_list{";level=9;no_context_takeover;bits=15"})
  36. {
  37. std::cout << ";" << param.first;
  38. if(! param.second.empty())
  39. std::cout << "=" << param.second;
  40. std::cout << "\n";
  41. }
  42. @endcode
  43. */
  44. class param_list
  45. {
  46. string_view s_;
  47. public:
  48. /** The type of each element in the list.
  49. The first string in the pair is the name of the parameter,
  50. and the second string in the pair is its value (which may
  51. be empty).
  52. */
  53. using value_type =
  54. std::pair<string_view, string_view>;
  55. /// A constant iterator to the list
  56. #if BOOST_BEAST_DOXYGEN
  57. using const_iterator = __implementation_defined__;
  58. #else
  59. class const_iterator;
  60. #endif
  61. /// Default constructor.
  62. param_list() = default;
  63. /** Construct a list.
  64. @param s A string containing the list contents. The string
  65. must remain valid for the lifetime of the container.
  66. */
  67. explicit
  68. param_list(string_view s)
  69. : s_(s)
  70. {
  71. }
  72. /// Return a const iterator to the beginning of the list
  73. const_iterator begin() const;
  74. /// Return a const iterator to the end of the list
  75. const_iterator end() const;
  76. /// Return a const iterator to the beginning of the list
  77. const_iterator cbegin() const;
  78. /// Return a const iterator to the end of the list
  79. const_iterator cend() const;
  80. };
  81. //------------------------------------------------------------------------------
  82. /** A list of extensions in a comma separated HTTP field value.
  83. This container allows iteration of the extensions in an HTTP
  84. field value. The extension list is a comma separated list of
  85. token parameter list pairs.
  86. If a parsing error is encountered while iterating the string,
  87. the behavior of the container will be as if a string containing
  88. only characters up to but excluding the first invalid character
  89. was used to construct the list.
  90. @par BNF
  91. @code
  92. ext-list = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
  93. ext = token param-list
  94. param-list = *( OWS ";" OWS param )
  95. param = token OWS [ "=" OWS ( token / quoted-string ) ]
  96. @endcode
  97. To use this class, construct with the string to be parsed and
  98. then use @ref begin and @ref end, or range-for to iterate each
  99. item:
  100. @par Example
  101. @code
  102. for(auto const& ext : ext_list{"none, 7z;level=9, zip;no_context_takeover;bits=15"})
  103. {
  104. std::cout << ext.first << "\n";
  105. for(auto const& param : ext.second)
  106. {
  107. std::cout << ";" << param.first;
  108. if(! param.second.empty())
  109. std::cout << "=" << param.second;
  110. std::cout << "\n";
  111. }
  112. }
  113. @endcode
  114. */
  115. class ext_list
  116. {
  117. using iter_type = string_view::const_iterator;
  118. string_view s_;
  119. public:
  120. /** The type of each element in the list.
  121. The first element of the pair is the extension token, and the
  122. second element of the pair is an iterable container holding the
  123. extension's name/value parameters.
  124. */
  125. using value_type = std::pair<string_view, param_list>;
  126. /// A constant iterator to the list
  127. #if BOOST_BEAST_DOXYGEN
  128. using const_iterator = __implementation_defined__;
  129. #else
  130. class const_iterator;
  131. #endif
  132. /** Construct a list.
  133. @param s A string containing the list contents. The string
  134. must remain valid for the lifetime of the container.
  135. */
  136. explicit
  137. ext_list(string_view s)
  138. : s_(s)
  139. {
  140. }
  141. /// Return a const iterator to the beginning of the list
  142. const_iterator begin() const;
  143. /// Return a const iterator to the end of the list
  144. const_iterator end() const;
  145. /// Return a const iterator to the beginning of the list
  146. const_iterator cbegin() const;
  147. /// Return a const iterator to the end of the list
  148. const_iterator cend() const;
  149. /** Find a token in the list.
  150. @param s The token to find. A case-insensitive comparison is used.
  151. @return An iterator to the matching token, or `end()` if no
  152. token exists.
  153. */
  154. BOOST_BEAST_DECL
  155. const_iterator
  156. find(string_view const& s);
  157. /** Return `true` if a token is present in the list.
  158. @param s The token to find. A case-insensitive comparison is used.
  159. */
  160. BOOST_BEAST_DECL
  161. bool
  162. exists(string_view const& s);
  163. };
  164. //------------------------------------------------------------------------------
  165. /** A list of tokens in a comma separated HTTP field value.
  166. This container allows iteration of a list of items in a
  167. header field value. The input is a comma separated list of
  168. tokens.
  169. If a parsing error is encountered while iterating the string,
  170. the behavior of the container will be as if a string containing
  171. only characters up to but excluding the first invalid character
  172. was used to construct the list.
  173. @par BNF
  174. @code
  175. token-list = *( "," OWS ) token *( OWS "," [ OWS token ] )
  176. @endcode
  177. To use this class, construct with the string to be parsed and
  178. then use @ref begin and @ref end, or range-for to iterate each
  179. item:
  180. @par Example
  181. @code
  182. for(auto const& token : token_list{"apple, pear, banana"})
  183. std::cout << token << "\n";
  184. @endcode
  185. */
  186. class token_list
  187. {
  188. using iter_type = string_view::const_iterator;
  189. string_view s_;
  190. public:
  191. /// The type of each element in the token list.
  192. using value_type = string_view;
  193. /// A constant iterator to the list
  194. #if BOOST_BEAST_DOXYGEN
  195. using const_iterator = __implementation_defined__;
  196. #else
  197. class const_iterator;
  198. #endif
  199. /** Construct a list.
  200. @param s A string containing the list contents. The string
  201. must remain valid for the lifetime of the container.
  202. */
  203. explicit
  204. token_list(string_view s)
  205. : s_(s)
  206. {
  207. }
  208. /// Return a const iterator to the beginning of the list
  209. const_iterator begin() const;
  210. /// Return a const iterator to the end of the list
  211. const_iterator end() const;
  212. /// Return a const iterator to the beginning of the list
  213. const_iterator cbegin() const;
  214. /// Return a const iterator to the end of the list
  215. const_iterator cend() const;
  216. /** Return `true` if a token is present in the list.
  217. @param s The token to find. A case-insensitive comparison is used.
  218. */
  219. BOOST_BEAST_DECL
  220. bool
  221. exists(string_view const& s);
  222. };
  223. /** A list of tokens in a comma separated HTTP field value.
  224. This container allows iteration of a list of items in a
  225. header field value. The input is a comma separated list of
  226. tokens.
  227. If a parsing error is encountered while iterating the string,
  228. the behavior of the container will be as if a string containing
  229. only characters up to but excluding the first invalid character
  230. was used to construct the list.
  231. @par BNF
  232. @code
  233. token-list = *( "," OWS ) token *( OWS "," [ OWS token ] )
  234. @endcode
  235. To use this class, construct with the string to be parsed and
  236. then use `begin` and `end`, or range-for to iterate each item:
  237. @par Example
  238. @code
  239. for(auto const& token : token_list{"apple, pear, banana"})
  240. std::cout << token << "\n";
  241. @endcode
  242. */
  243. using opt_token_list =
  244. detail::basic_parsed_list<
  245. detail::opt_token_list_policy>;
  246. /** Returns `true` if a parsed list is parsed without errors.
  247. This function iterates a single pass through a parsed list
  248. and returns `true` if there were no parsing errors, else
  249. returns `false`.
  250. */
  251. template<class Policy>
  252. bool
  253. validate_list(detail::basic_parsed_list<
  254. Policy> const& list);
  255. } // http
  256. } // beast
  257. } // boost
  258. #include <boost/beast/http/impl/rfc7230.hpp>
  259. #endif