params_encoded_view.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_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. /** 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 BOOST_URL_DECL 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. params_encoded_view(
  131. core::string_view s);
  132. /** Assignment
  133. After assignment, both views
  134. reference the same underlying character
  135. buffer.
  136. Ownership is not transferred; the caller
  137. is responsible for ensuring the lifetime
  138. of the buffer extends until it is no
  139. longer referenced.
  140. @par Postconditions
  141. @code
  142. this->buffer().data() == other.buffer().data()
  143. @endcode
  144. @par Complexity
  145. Constant
  146. @par Exception Safety
  147. Throws nothing
  148. */
  149. params_encoded_view&
  150. operator=(
  151. params_encoded_view const&) = default;
  152. /** Conversion
  153. This conversion returns a new view which
  154. references the same underlying character
  155. buffer, and whose iterators and members
  156. return ordinary strings with decoding
  157. applied to any percent escapes.
  158. Ownership is not transferred; the caller
  159. is responsible for ensuring the lifetime
  160. of the buffer extends until it is no
  161. longer referenced.
  162. @par Example
  163. @code
  164. params_view qp = parse_path( "/path/to/file.txt" ).value();
  165. @endcode
  166. @par Postconditions
  167. @code
  168. params_view( *this ).buffer().data() == this->buffer().data()
  169. @endcode
  170. @par Complexity
  171. Constant
  172. @par Exception Safety
  173. Throws nothing
  174. */
  175. operator
  176. params_view() const noexcept;
  177. //--------------------------------------------
  178. friend
  179. BOOST_URL_DECL
  180. system::result<params_encoded_view>
  181. parse_query(core::string_view s) noexcept;
  182. };
  183. } // urls
  184. } // boost
  185. #endif