params_view.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  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. @param other The object to copy
  80. */
  81. params_view(
  82. params_view const& other) = default;
  83. /** Constructor
  84. After construction both views will
  85. reference the same character buffer
  86. but this instance will use the specified
  87. @ref encoding_opts when the values
  88. are decoded.
  89. Ownership is not transferred; the caller
  90. is responsible for ensuring the lifetime
  91. of the buffer extends until it is no
  92. longer referenced.
  93. @par Postconditions
  94. @code
  95. this->buffer().data() == other.buffer().data()
  96. @endcode
  97. @par Complexity
  98. Constant.
  99. @par Exception Safety
  100. Throws nothing
  101. @param other The object to copy
  102. @param opt The options for decoding
  103. */
  104. params_view(
  105. params_view const& other,
  106. encoding_opts opt) noexcept;
  107. /** Constructor
  108. This function constructs params from
  109. a valid query parameter string, which
  110. can contain percent escapes. Unlike
  111. the parameters in URLs, the string
  112. passed here should not start with "?".
  113. Upon construction, the view references
  114. the character buffer pointed to by `s`.
  115. The caller is responsible for ensuring
  116. that the lifetime of the buffer extends
  117. until it is no longer referenced.
  118. @par Example
  119. @code
  120. params_view qp( "first=John&last=Doe" );
  121. @endcode
  122. @par Effects
  123. @code
  124. return parse_query( s ).value();
  125. @endcode
  126. @par Postconditions
  127. @code
  128. this->buffer().data() == s.data()
  129. @endcode
  130. @par Complexity
  131. Linear in `s`.
  132. @par Exception Safety
  133. Exceptions thrown on invalid input.
  134. @throw system_error
  135. `s` contains an invalid query parameter
  136. string.
  137. @param s The string to parse.
  138. @par BNF
  139. @code
  140. query-params = [ query-param ] *( "&" query-param )
  141. query-param = key [ "=" value ]
  142. @endcode
  143. @par Specification
  144. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  145. >3.4. Query</a>
  146. */
  147. BOOST_URL_DECL
  148. params_view(
  149. core::string_view s);
  150. /** Constructor
  151. This function constructs params from
  152. a valid query parameter string, which
  153. can contain percent escapes.
  154. This instance will use the specified
  155. @ref encoding_opts when the values
  156. are decoded.
  157. Unlike the parameters in URLs, the string
  158. passed here should not start with "?".
  159. Upon construction, the view will
  160. reference the character buffer pointed
  161. to by `s`. The caller is responsible
  162. for ensuring that the lifetime of the
  163. buffer extends until it is no longer
  164. referenced.
  165. @par Example
  166. @code
  167. encoding_opts opt;
  168. opt.space_as_plus = true;
  169. params_view qp( "name=John+Doe", opt );
  170. @endcode
  171. @par Effects
  172. @code
  173. return params_view(parse_query( s ).value(), opt);
  174. @endcode
  175. @par Postconditions
  176. @code
  177. this->buffer().data() == s.data()
  178. @endcode
  179. @par Complexity
  180. Linear in `s`.
  181. @par Exception Safety
  182. Exceptions thrown on invalid input.
  183. @throw system_error
  184. `s` contains an invalid query parameter
  185. string.
  186. @param s The string to parse.
  187. @param opt The options for decoding. If
  188. this parameter is omitted, `space_as_plus`
  189. is used.
  190. @par BNF
  191. @code
  192. query-params = [ query-param ] *( "&" query-param )
  193. query-param = key [ "=" value ]
  194. @endcode
  195. @par Specification
  196. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  197. >3.4. Query</a>
  198. */
  199. BOOST_URL_DECL
  200. params_view(
  201. core::string_view s,
  202. encoding_opts opt);
  203. /** Assignment
  204. After assignment, both views reference
  205. the same underlying character buffer.
  206. Ownership is not transferred; the caller
  207. is responsible for ensuring the lifetime
  208. of the buffer extends until it is no
  209. longer referenced.
  210. @par Postconditions
  211. @code
  212. this->buffer().data() == other.buffer().data()
  213. @endcode
  214. @par Complexity
  215. Constant
  216. @par Exception Safety
  217. Throws nothing
  218. @param other The object to assign
  219. @return A reference to this object
  220. */
  221. params_view&
  222. operator=(
  223. params_view const& other) = default;
  224. };
  225. } // urls
  226. } // boost
  227. #endif