params_view.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // Copyright (c) 2019 Vinnie Falco ([email protected])
  3. // Copyright (c) 2022 Alan de Freitas ([email protected])
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_PARAMS_VIEW_HPP
  11. #define BOOST_URL_PARAMS_VIEW_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/params_base.hpp>
  14. namespace boost {
  15. namespace urls {
  16. /** A view representing query parameters in a URL
  17. Objects of this type are used to interpret
  18. the query parameters as a bidirectional view
  19. of key/value pairs.
  20. The view does not retain ownership of the
  21. elements and instead references the original
  22. character buffer. The caller is responsible
  23. for ensuring that the lifetime of the buffer
  24. extends until it is no longer referenced.
  25. @par Example
  26. @code
  27. url_view u( "?first=John&last=Doe" );
  28. params_view p = u.params();
  29. @endcode
  30. Percent escapes in strings returned when
  31. dereferencing iterators are automatically
  32. decoded.
  33. @par Iterator Invalidation
  34. Changes to the underlying character buffer
  35. can invalidate iterators which reference it.
  36. */
  37. class params_view
  38. : public params_base
  39. {
  40. friend class url_view_base;
  41. friend class params_encoded_view;
  42. friend class params_ref;
  43. params_view(
  44. detail::query_ref const& ref,
  45. encoding_opts opt) noexcept;
  46. public:
  47. /** Constructor
  48. Default-constructed params have
  49. zero elements.
  50. @par Example
  51. @code
  52. params_view qp;
  53. @endcode
  54. @par Effects
  55. @code
  56. return params_view( "" );
  57. @endcode
  58. @par Complexity
  59. Constant.
  60. @par Exception Safety
  61. Throws nothing.
  62. */
  63. params_view() = default;
  64. /** Constructor
  65. After construction both views reference
  66. the same character buffer.
  67. Ownership is not transferred; the caller
  68. is responsible for ensuring the lifetime
  69. of the buffer extends until it is no
  70. longer referenced.
  71. @par Postconditions
  72. @code
  73. this->buffer().data() == other.buffer().data()
  74. @endcode
  75. @par Complexity
  76. Constant.
  77. @par Exception Safety
  78. Throws nothing
  79. */
  80. params_view(
  81. params_view const& other) = default;
  82. /** Constructor
  83. After construction both views will
  84. reference the same character buffer
  85. but this instance will use the specified
  86. @ref encoding_opts when the values
  87. are decoded.
  88. Ownership is not transferred; the caller
  89. is responsible for ensuring the lifetime
  90. of the buffer extends until it is no
  91. longer referenced.
  92. @par Postconditions
  93. @code
  94. this->buffer().data() == other.buffer().data()
  95. @endcode
  96. @par Complexity
  97. Constant.
  98. @par Exception Safety
  99. Throws nothing
  100. */
  101. params_view(
  102. params_view const& other,
  103. encoding_opts opt) noexcept;
  104. /** Constructor
  105. This function constructs params from
  106. a valid query parameter string, which
  107. can contain percent escapes. Unlike
  108. the parameters in URLs, the string
  109. passed here should not start with "?".
  110. Upon construction, the view references
  111. the character buffer pointed to by `s`.
  112. The caller is responsible for ensuring
  113. that the lifetime of the buffer extends
  114. until it is no longer referenced.
  115. @par Example
  116. @code
  117. params_view qp( "first=John&last=Doe" );
  118. @endcode
  119. @par Effects
  120. @code
  121. return parse_query( s ).value();
  122. @endcode
  123. @par Postconditions
  124. @code
  125. this->buffer().data() == s.data()
  126. @endcode
  127. @par Complexity
  128. Linear in `s`.
  129. @par Exception Safety
  130. Exceptions thrown on invalid input.
  131. @throw system_error
  132. `s` contains an invalid query parameter
  133. string.
  134. @param s The string to parse.
  135. @par BNF
  136. @code
  137. query-params = [ query-param ] *( "&" query-param )
  138. query-param = key [ "=" value ]
  139. @endcode
  140. @par Specification
  141. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  142. >3.4. Query</a>
  143. */
  144. BOOST_URL_DECL
  145. params_view(
  146. core::string_view s);
  147. /** Constructor
  148. This function constructs params from
  149. a valid query parameter string, which
  150. can contain percent escapes.
  151. This instance will use the specified
  152. @ref encoding_opts when the values
  153. are decoded.
  154. Unlike the parameters in URLs, the string
  155. passed here should not start with "?".
  156. Upon construction, the view will
  157. reference the character buffer pointed
  158. to by `s`. The caller is responsible
  159. for ensuring that the lifetime of the
  160. buffer extends until it is no longer
  161. referenced.
  162. @par Example
  163. @code
  164. encoding_opts opt;
  165. opt.space_as_plus = true;
  166. params_view qp( "name=John+Doe", opt );
  167. @endcode
  168. @par Effects
  169. @code
  170. return params_view(parse_query( s ).value(), opt);
  171. @endcode
  172. @par Postconditions
  173. @code
  174. this->buffer().data() == s.data()
  175. @endcode
  176. @par Complexity
  177. Linear in `s`.
  178. @par Exception Safety
  179. Exceptions thrown on invalid input.
  180. @throw system_error
  181. `s` contains an invalid query parameter
  182. string.
  183. @param s The string to parse.
  184. @param opt The options for decoding. If
  185. this parameter is omitted, `space_as_plus`
  186. is used.
  187. @par BNF
  188. @code
  189. query-params = [ query-param ] *( "&" query-param )
  190. query-param = key [ "=" value ]
  191. @endcode
  192. @par Specification
  193. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  194. >3.4. Query</a>
  195. */
  196. BOOST_URL_DECL
  197. params_view(
  198. core::string_view s,
  199. encoding_opts opt);
  200. /** Assignment
  201. After assignment, both views reference
  202. the same underlying character buffer.
  203. Ownership is not transferred; the caller
  204. is responsible for ensuring the lifetime
  205. of the buffer extends until it is no
  206. longer referenced.
  207. @par Postconditions
  208. @code
  209. this->buffer().data() == other.buffer().data()
  210. @endcode
  211. @par Complexity
  212. Constant
  213. @par Exception Safety
  214. Throws nothing
  215. */
  216. params_view&
  217. operator=(
  218. params_view const&) = default;
  219. };
  220. } // urls
  221. } // boost
  222. #endif