parse.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_PARSE_HPP
  11. #define BOOST_URL_PARSE_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/error_types.hpp>
  14. #include <boost/url/url_view.hpp>
  15. namespace boost {
  16. namespace urls {
  17. /** Return a reference to a parsed URL string
  18. This function parses a string according
  19. to the grammar below and returns a view
  20. referencing the passed string upon success,
  21. else returns an error.
  22. Ownership of the string is not transferred;
  23. the caller is responsible for ensuring that
  24. the lifetime of the character buffer extends
  25. until the view is no longer being accessed.
  26. @par Example
  27. @code
  28. system::result< url_view > rv = parse_absolute_uri( "http://example.com/index.htm?id=1" );
  29. @endcode
  30. @par BNF
  31. @code
  32. absolute-URI = scheme ":" hier-part [ "?" query ]
  33. hier-part = "//" authority path-abempty
  34. / path-absolute
  35. / path-rootless
  36. / path-empty
  37. @endcode
  38. @throw std::length_error `s.size() > url_view::max_size`
  39. @return A @ref result containing a value or an error
  40. @param s The string to parse
  41. @par Specification
  42. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.3"
  43. >4.3. Absolute URI (rfc3986)</a>
  44. @see
  45. @ref parse_origin_form,
  46. @ref parse_relative_ref,
  47. @ref parse_uri,
  48. @ref parse_uri_reference,
  49. @ref url_view.
  50. */
  51. BOOST_URL_DECL
  52. system::result<url_view>
  53. parse_absolute_uri(
  54. core::string_view s);
  55. //------------------------------------------------
  56. /** Return a reference to a parsed URL string
  57. This function parses a string according
  58. to the grammar below and returns a view
  59. referencing the passed string upon success,
  60. else returns an error.
  61. Ownership of the string is not transferred;
  62. the caller is responsible for ensuring that
  63. the lifetime of the character buffer extends
  64. until the view is no longer being accessed.
  65. @par Example
  66. @code
  67. system::result< url_view > = parse_origin_form( "/index.htm?layout=mobile" );
  68. @endcode
  69. @par BNF
  70. @code
  71. origin-form = absolute-path [ "?" query ]
  72. absolute-path = 1*( "/" segment )
  73. @endcode
  74. @throw std::length_error `s.size() > url_view::max_size`
  75. @return A @ref result containing a value or an error
  76. @param s The string to parse
  77. @par Specification
  78. @li <a href="https://datatracker.ietf.org/doc/html/rfc7230#section-5.3.1"
  79. >5.3.1. origin-form (rfc7230)</a>
  80. @see
  81. @ref parse_absolute_uri,
  82. @ref parse_relative_ref,
  83. @ref parse_uri,
  84. @ref parse_uri_reference,
  85. @ref url_view.
  86. */
  87. BOOST_URL_DECL
  88. system::result<url_view>
  89. parse_origin_form(
  90. core::string_view s);
  91. //------------------------------------------------
  92. /** Return a reference to a parsed URL string
  93. This function parses a string according
  94. to the grammar below and returns a view
  95. referencing the passed string upon success,
  96. else returns an error.
  97. Ownership of the string is not transferred;
  98. the caller is responsible for ensuring that
  99. the lifetime of the character buffer extends
  100. until the view is no longer being accessed.
  101. @par Example
  102. @code
  103. system::result< url_view > = parse_relative_ref( "images/dot.gif?v=hide#a" );
  104. @endcode
  105. @par BNF
  106. @code
  107. relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  108. relative-part = "//" authority path-abempty
  109. / path-absolute
  110. / path-noscheme
  111. / path-abempty
  112. / path-empty
  113. @endcode
  114. @return A @ref result containing a value or an error
  115. @param s The string to parse
  116. @throw std::length_error `s.size() > url_view::max_size`
  117. @par Specification
  118. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.2"
  119. >4.2. Relative Reference (rfc3986)</a>
  120. @li <a href="https://www.rfc-editor.org/errata/eid5428"
  121. >Errata ID: 5428 (rfc3986)</a>
  122. @see
  123. @ref parse_absolute_uri,
  124. @ref parse_origin_form,
  125. @ref parse_uri,
  126. @ref parse_uri_reference,
  127. @ref url_view.
  128. */
  129. BOOST_URL_DECL
  130. system::result<url_view>
  131. parse_relative_ref(
  132. core::string_view s);
  133. //------------------------------------------------
  134. /** Return a reference to a parsed URL string
  135. This function parses a string according
  136. to the grammar below and returns a view
  137. referencing the passed string upon success,
  138. else returns an error.
  139. Ownership of the string is not transferred;
  140. the caller is responsible for ensuring that
  141. the lifetime of the character buffer extends
  142. until the view is no longer being accessed.
  143. @par Example
  144. @code
  145. system::result< url_view > = parse_uri( "https://www.example.com/index.htm?id=guest#s1" );
  146. @endcode
  147. @par BNF
  148. @code
  149. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  150. hier-part = "//" authority path-abempty
  151. / path-absolute
  152. / path-rootless
  153. / path-empty
  154. @endcode
  155. @throw std::length_error `s.size() > url_view::max_size`
  156. @return A @ref result containing a value or an error
  157. @param s The string to parse
  158. @par Specification
  159. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3"
  160. >3. Syntax Components (rfc3986)</a>
  161. @see
  162. @ref parse_absolute_uri,
  163. @ref parse_origin_form,
  164. @ref parse_relative_ref,
  165. @ref parse_uri_reference,
  166. @ref url_view.
  167. */
  168. BOOST_URL_DECL
  169. system::result<url_view>
  170. parse_uri(
  171. core::string_view s);
  172. //------------------------------------------------
  173. /** Return a reference to a parsed URL string
  174. This function parses a string according
  175. to the grammar below and returns a view
  176. referencing the passed string upon success,
  177. else returns an error.
  178. Ownership of the string is not transferred;
  179. the caller is responsible for ensuring that
  180. the lifetime of the character buffer extends
  181. until the view is no longer being accessed.
  182. @par Example
  183. @code
  184. system::result< url_view > = parse_uri_reference( "ws://echo.example.com/?name=boost#demo" );
  185. @endcode
  186. @par BNF
  187. @code
  188. URI-reference = URI / relative-ref
  189. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  190. relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  191. hier-part = "//" authority path-abempty
  192. / path-absolute
  193. / path-rootless
  194. / path-empty
  195. relative-part = "//" authority path-abempty
  196. / path-absolute
  197. / path-noscheme
  198. / path-abempty
  199. / path-empty
  200. @endcode
  201. @throw std::length_error `s.size() > url_view::max_size`
  202. @return A @ref result containing a value or an error
  203. @param s The string to parse
  204. @par Specification
  205. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.1"
  206. >4.1. URI Reference (rfc3986)</a>
  207. @li <a href="https://www.rfc-editor.org/errata/eid5428"
  208. >Errata ID: 5428 (rfc3986)</a>
  209. @see
  210. @ref parse_absolute_uri,
  211. @ref parse_origin_form,
  212. @ref parse_relative_ref,
  213. @ref parse_uri,
  214. @ref url_view.
  215. */
  216. BOOST_URL_DECL
  217. system::result<url_view>
  218. parse_uri_reference(
  219. core::string_view s);
  220. } // url
  221. } // boost
  222. #endif