params_encoded_view.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_ENCODED_VIEW_HPP
  11. #define BOOST_URL_PARAMS_ENCODED_VIEW_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/error_types.hpp>
  14. #include <boost/url/params_encoded_base.hpp>
  15. #include <boost/url/params_view.hpp>
  16. #include <boost/url/string_view.hpp>
  17. #include <iosfwd>
  18. #include <utility>
  19. namespace boost {
  20. namespace urls {
  21. /** A view representing query parameters in a URL
  22. Objects of this type are used to interpret
  23. the query parameters as a bidirectional view
  24. of key/value pairs.
  25. The view does not retain ownership of the
  26. elements and instead references the original
  27. character buffer. The caller is responsible
  28. for ensuring that the lifetime of the buffer
  29. extends until it is no longer referenced.
  30. @par Example
  31. @code
  32. url_view u( "?first=John&last=Doe" );
  33. params_encoded_view p = u.encoded_params();
  34. @endcode
  35. Strings produced when elements are returned
  36. have type @ref param_pct_view and represent
  37. encoded strings. Strings passed to member
  38. functions may contain percent escapes, and
  39. throw exceptions on invalid inputs.
  40. @par Iterator Invalidation
  41. Changes to the underlying character buffer
  42. can invalidate iterators which reference it.
  43. */
  44. class params_encoded_view
  45. : public params_encoded_base
  46. {
  47. friend class url_view_base;
  48. friend class params_view;
  49. friend class params_encoded_ref;
  50. friend struct query_rule_t;
  51. params_encoded_view(
  52. detail::query_ref const& ref) noexcept;
  53. public:
  54. /** Constructor
  55. Default-constructed params have
  56. zero elements.
  57. @par Example
  58. @code
  59. params_encoded_view qp;
  60. @endcode
  61. @par Effects
  62. @code
  63. return params_encoded_view( "" );
  64. @endcode
  65. @par Complexity
  66. Constant.
  67. @par Exception Safety
  68. Throws nothing.
  69. */
  70. params_encoded_view() = default;
  71. /** Constructor
  72. After construction both views
  73. reference the same character buffer.
  74. Ownership is not transferred; the caller
  75. is responsible for ensuring the lifetime
  76. of the buffer extends until it is no
  77. longer referenced.
  78. @par Postconditions
  79. @code
  80. this->buffer().data() == other.buffer().data()
  81. @endcode
  82. @par Complexity
  83. Constant.
  84. @par Exception Safety
  85. Throws nothing
  86. */
  87. params_encoded_view(
  88. params_encoded_view const& other) = default;
  89. /** Constructor
  90. This function constructs params from
  91. a valid query parameter string, which
  92. can contain percent escapes. Unlike
  93. the parameters in URLs, the string
  94. passed here should not start with "?".
  95. Upon construction, the view
  96. references the character buffer pointed
  97. to by `s`. The caller is responsible
  98. for ensuring that the lifetime of the
  99. buffer extends until it is no longer
  100. referenced.
  101. @par Example
  102. @code
  103. params_encoded_view qp( "first=John&last=Doe" );
  104. @endcode
  105. @par Effects
  106. @code
  107. return parse_query( s ).value();
  108. @endcode
  109. @par Postconditions
  110. @code
  111. this->buffer().data() == s.data()
  112. @endcode
  113. @par Complexity
  114. Linear in `s`.
  115. @par Exception Safety
  116. Exceptions thrown on invalid input.
  117. @throw system_error
  118. `s` contains an invalid query parameter
  119. string.
  120. @param s The string to parse.
  121. @par BNF
  122. @code
  123. query-params = [ query-param ] *( "&" query-param )
  124. query-param = key [ "=" value ]
  125. @endcode
  126. @par Specification
  127. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  128. >3.4. Query</a>
  129. */
  130. BOOST_URL_DECL
  131. params_encoded_view(
  132. string_view s);
  133. /** Assignment
  134. After assignment, both views
  135. reference the same underlying character
  136. buffer.
  137. Ownership is not transferred; the caller
  138. is responsible for ensuring the lifetime
  139. of the buffer extends until it is no
  140. longer referenced.
  141. @par Postconditions
  142. @code
  143. this->buffer().data() == other.buffer().data()
  144. @endcode
  145. @par Complexity
  146. Constant
  147. @par Exception Safety
  148. Throws nothing
  149. */
  150. params_encoded_view&
  151. operator=(
  152. params_encoded_view const&) = default;
  153. /** Conversion
  154. This conversion returns a new view which
  155. references the same underlying character
  156. buffer, and whose iterators and members
  157. return ordinary strings with decoding
  158. applied to any percent escapes.
  159. Ownership is not transferred; the caller
  160. is responsible for ensuring the lifetime
  161. of the buffer extends until it is no
  162. longer referenced.
  163. @par Example
  164. @code
  165. params_view qp = parse_path( "/path/to/file.txt" ).value();
  166. @endcode
  167. @par Postconditions
  168. @code
  169. params_view( *this ).buffer().data() == this->buffer().data()
  170. @endcode
  171. @par Complexity
  172. Constant
  173. @par Exception Safety
  174. Throws nothing
  175. */
  176. BOOST_URL_DECL
  177. operator
  178. params_view() const noexcept;
  179. //--------------------------------------------
  180. BOOST_URL_DECL
  181. friend
  182. result<params_encoded_view>
  183. parse_query(string_view s) noexcept;
  184. };
  185. } // urls
  186. } // boost
  187. #endif