params_encoded_view.hpp 6.0 KB

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