authority_view.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/url
  8. //
  9. #ifndef BOOST_URL_AUTHORITY_VIEW_HPP
  10. #define BOOST_URL_AUTHORITY_VIEW_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/host_type.hpp>
  13. #include <boost/url/ipv4_address.hpp>
  14. #include <boost/url/ipv6_address.hpp>
  15. #include <boost/url/pct_string_view.hpp>
  16. #include <boost/url/detail/except.hpp>
  17. #include <boost/url/detail/url_impl.hpp>
  18. #include <boost/assert.hpp>
  19. #include <cstddef>
  20. #include <iosfwd>
  21. #include <utility>
  22. namespace boost {
  23. namespace urls {
  24. /** A non-owning reference to a valid authority
  25. Objects of this type represent valid authority
  26. strings constructed from a parsed, external
  27. character buffer whose storage is managed
  28. by the caller. That is, it acts like a
  29. `core::string_view` in terms of ownership.
  30. The caller is responsible for ensuring
  31. that the lifetime of the underlying
  32. character buffer extends until it is no
  33. longer referenced.
  34. @par Example 1
  35. Construction from a string parses the input
  36. as an <em>authority</em> and throws an
  37. exception on error. Upon success, the
  38. constructed object points to the passed
  39. character buffer; ownership is not
  40. transferred.
  41. @code
  42. authority_view a( "user:pass@www.example.com:8080" );
  43. @endcode
  44. @par Example 2
  45. The parsing function @ref parse_authority returns
  46. a `boost::system::result` containing either a valid
  47. @ref authority_view upon success, otherwise it
  48. contains an error. The error can be converted to
  49. an exception by the caller if desired:
  50. @code
  51. system::result< authority_view > rv = parse_authority( "user:pass@www.example.com:8080" );
  52. @endcode
  53. @par BNF
  54. @code
  55. authority = [ userinfo "@" ] host [ ":" port ]
  56. userinfo = user [ ":" [ password ] ]
  57. user = *( unreserved / pct-encoded / sub-delims )
  58. password = *( unreserved / pct-encoded / sub-delims / ":" )
  59. host = IP-literal / IPv4address / reg-name
  60. port = *DIGIT
  61. @endcode
  62. @par Specification
  63. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
  64. >3.2. Authority (rfc3986)</a>
  65. @see
  66. @ref parse_authority.
  67. */
  68. class BOOST_URL_DECL
  69. authority_view
  70. : private detail::parts_base
  71. {
  72. detail::url_impl u_;
  73. friend struct detail::url_impl;
  74. explicit
  75. authority_view(
  76. detail::url_impl const& u) noexcept;
  77. public:
  78. //--------------------------------------------
  79. //
  80. // Special Members
  81. //
  82. //--------------------------------------------
  83. /** Destructor
  84. */
  85. virtual
  86. ~authority_view();
  87. /** Constructor
  88. Default constructed authorities
  89. refer to a string with zero length,
  90. which is always valid. This matches
  91. the grammar for a zero-length host.
  92. @par Exception Safety
  93. Throws nothing.
  94. @par Specification
  95. */
  96. authority_view() noexcept;
  97. /** Construct from a string.
  98. This function attempts to construct
  99. an authority from the string `s`,
  100. which must be a valid authority or
  101. else an exception is thrown. Upon
  102. successful construction, the view
  103. refers to the characters in the
  104. buffer pointed to by `s`.
  105. Ownership is not transferred; the
  106. caller is responsible for ensuring
  107. that the lifetime of the buffer
  108. extends until the view is destroyed.
  109. @param s The string to parse
  110. @par BNF
  111. @code
  112. authority = [ userinfo "@" ] host [ ":" port ]
  113. userinfo = user [ ":" [ password ] ]
  114. user = *( unreserved / pct-encoded / sub-delims )
  115. password = *( unreserved / pct-encoded / sub-delims / ":" )
  116. host = IP-literal / IPv4address / reg-name
  117. port = *DIGIT
  118. @endcode
  119. @par Specification
  120. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
  121. >3.2. Authority (rfc3986)</a>
  122. @see
  123. @ref parse_authority.
  124. */
  125. explicit
  126. authority_view(core::string_view s);
  127. /** Constructor
  128. */
  129. authority_view(
  130. authority_view const&) noexcept;
  131. /** Assignment
  132. This function assigns the contents of
  133. `other` to this object.
  134. @param other The object to assign
  135. @return A reference to this object
  136. @par Exception Safety
  137. Throws nothing.
  138. */
  139. authority_view&
  140. operator=(
  141. authority_view const& other) noexcept;
  142. //--------------------------------------------
  143. //
  144. // Observers
  145. //
  146. //--------------------------------------------
  147. /** Return the number of characters in the authority
  148. This function returns the number of
  149. characters in the authority.
  150. @return The number of characters in the authority
  151. @par Example
  152. @code
  153. assert( authority_view( "user:pass@www.example.com:8080" ).size() == 30 );
  154. @endcode
  155. @par Exception Safety
  156. Throws nothing.
  157. */
  158. std::size_t
  159. size() const noexcept
  160. {
  161. return u_.offset(id_end);
  162. }
  163. /** Return true if the authority is empty
  164. An empty authority has an empty host,
  165. no userinfo, and no port.
  166. @return `true` if the authority is empty
  167. @par Example
  168. @code
  169. assert( authority_view( "" ).empty() );
  170. @endcode
  171. @par Exception Safety
  172. Throws nothing.
  173. */
  174. bool
  175. empty() const noexcept
  176. {
  177. return size() == 0;
  178. }
  179. /** Return a pointer to the first character
  180. This function returns a pointer to the
  181. beginning of the view, which is not
  182. guaranteed to be null-terminated.
  183. @return A pointer to the first character
  184. @par Exception Safety
  185. Throws nothing.
  186. */
  187. char const*
  188. data() const noexcept
  189. {
  190. return u_.cs_;
  191. }
  192. /** Return the complete authority
  193. This function returns the authority
  194. as a percent-encoded string.
  195. @return The complete authority
  196. @par Example
  197. @code
  198. assert( parse_authority( "www.example.com" ).value().buffer() == "www.example.com" );
  199. @endcode
  200. @par BNF
  201. @code
  202. authority = [ userinfo "@" ] host [ ":" port ]
  203. @endcode
  204. @par Exception Safety
  205. Throws nothing.
  206. @par Specification
  207. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
  208. >3.2. Authority (rfc3986)</a>
  209. */
  210. core::string_view
  211. buffer() const noexcept
  212. {
  213. return core::string_view(data(), size());
  214. }
  215. //--------------------------------------------
  216. //
  217. // Userinfo
  218. //
  219. //--------------------------------------------
  220. /** Return true if a userinfo is present
  221. This function returns true if this
  222. contains a userinfo.
  223. @return `true` if a userinfo is present
  224. @par Example
  225. @code
  226. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_userinfo() );
  227. @endcode
  228. @par Complexity
  229. Constant.
  230. @par Exception Safety
  231. Throws nothing.
  232. @par BNF
  233. @code
  234. userinfo = user [ ":" [ password ] ]
  235. authority = [ userinfo "@" ] host [ ":" port ]
  236. @endcode
  237. @par Specification
  238. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  239. >3.2.1. User Information (rfc3986)</a>
  240. @see
  241. @ref has_password,
  242. @ref encoded_password,
  243. @ref encoded_user,
  244. @ref encoded_userinfo,
  245. @ref password,
  246. @ref user,
  247. @ref userinfo.
  248. */
  249. bool
  250. has_userinfo() const noexcept;
  251. /** Return the userinfo
  252. If present, this function returns a
  253. string representing the userinfo (which
  254. may be empty).
  255. Otherwise it returns an empty string.
  256. Any percent-escapes in the string are
  257. decoded first.
  258. @param token A string token to receive the result.
  259. @return The userinfo
  260. @par Example
  261. @code
  262. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).userinfo() == "jane-doe:pass" );
  263. @endcode
  264. @par Complexity
  265. Linear in `this->userinfo().size()`.
  266. @par Exception Safety
  267. Calls to allocate may throw.
  268. @par BNF
  269. @code
  270. userinfo = user [ ":" [ password ] ]
  271. authority = [ userinfo "@" ] host [ ":" port ]
  272. @endcode
  273. @par Specification
  274. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  275. >3.2.1. User Information (rfc3986)</a>
  276. @see
  277. @ref has_password,
  278. @ref has_userinfo,
  279. @ref encoded_password,
  280. @ref encoded_user,
  281. @ref encoded_userinfo,
  282. @ref password,
  283. @ref user.
  284. */
  285. template<BOOST_URL_STRTOK_TPARAM>
  286. BOOST_URL_STRTOK_RETURN
  287. userinfo(
  288. BOOST_URL_STRTOK_ARG(token)) const
  289. {
  290. encoding_opts opt;
  291. opt.space_as_plus = false;
  292. return encoded_userinfo().decode(
  293. opt, std::move(token));
  294. }
  295. /** Return the userinfo
  296. If present, this function returns a
  297. string representing the userinfo (which
  298. may be empty).
  299. Otherwise it returns an empty string.
  300. The returned string may contain
  301. percent escapes.
  302. @return The userinfo
  303. @par Example
  304. @code
  305. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_userinfo() == "jane%2Ddoe:pass" );
  306. @endcode
  307. @par Complexity
  308. Constant.
  309. @par Exception Safety
  310. Throws nothing
  311. @par BNF
  312. @code
  313. userinfo = user [ ":" [ password ] ]
  314. authority = [ userinfo "@" ] host [ ":" port ]
  315. @endcode
  316. @par Specification
  317. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  318. >3.2.1. User Information (rfc3986)</a>
  319. @see
  320. @ref has_password,
  321. @ref has_userinfo,
  322. @ref encoded_password,
  323. @ref encoded_user,
  324. @ref password,
  325. @ref user,
  326. @ref userinfo.
  327. */
  328. pct_string_view
  329. encoded_userinfo() const noexcept;
  330. //--------------------------------------------
  331. /** Return the user
  332. If present, this function returns a
  333. string representing the user (which
  334. may be empty).
  335. Otherwise it returns an empty string.
  336. Any percent-escapes in the string are
  337. decoded first.
  338. @param token A string token to receive the result.
  339. @return The user
  340. @par Example
  341. @code
  342. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).user() == "jane-doe" );
  343. @endcode
  344. @par Complexity
  345. Linear in `this->user().size()`.
  346. @par Exception Safety
  347. Calls to allocate may throw.
  348. @par BNF
  349. @code
  350. userinfo = user [ ":" [ password ] ]
  351. user = *( unreserved / pct-encoded / sub-delims )
  352. password = *( unreserved / pct-encoded / sub-delims / ":" )
  353. @endcode
  354. @par Specification
  355. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  356. >3.2.1. User Information (rfc3986)</a>
  357. @see
  358. @ref has_password,
  359. @ref has_userinfo,
  360. @ref encoded_password,
  361. @ref encoded_user,
  362. @ref encoded_userinfo,
  363. @ref password,
  364. @ref userinfo.
  365. */
  366. template<BOOST_URL_STRTOK_TPARAM>
  367. BOOST_URL_STRTOK_RETURN
  368. user(
  369. BOOST_URL_STRTOK_ARG(token)) const
  370. {
  371. encoding_opts opt;
  372. opt.space_as_plus = false;
  373. return encoded_user().decode(
  374. opt, std::move(token));
  375. }
  376. /** Return the user
  377. If present, this function returns a
  378. string representing the user (which
  379. may be empty).
  380. Otherwise it returns an empty string.
  381. The returned string may contain
  382. percent escapes.
  383. @return The user
  384. @par Example
  385. @code
  386. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_user() == "jane%2Ddoe" );
  387. @endcode
  388. @par Complexity
  389. Constant.
  390. @par Exception Safety
  391. Throws nothing.
  392. @par BNF
  393. @code
  394. userinfo = user [ ":" [ password ] ]
  395. user = *( unreserved / pct-encoded / sub-delims )
  396. password = *( unreserved / pct-encoded / sub-delims / ":" )
  397. @endcode
  398. @par Specification
  399. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  400. >3.2.1. User Information (rfc3986)</a>
  401. @see
  402. @ref has_password,
  403. @ref has_userinfo,
  404. @ref encoded_password,
  405. @ref encoded_userinfo,
  406. @ref password,
  407. @ref user,
  408. @ref userinfo.
  409. */
  410. pct_string_view
  411. encoded_user() const noexcept;
  412. /** Return true if a password is present
  413. This function returns true if the
  414. userinfo is present and contains
  415. a password.
  416. @return `true` if a password is present
  417. @par Example
  418. @code
  419. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_password() );
  420. @endcode
  421. @par Complexity
  422. Constant.
  423. @par Exception Safety
  424. Throws nothing.
  425. @par BNF
  426. @code
  427. userinfo = user [ ":" [ password ] ]
  428. user = *( unreserved / pct-encoded / sub-delims )
  429. password = *( unreserved / pct-encoded / sub-delims / ":" )
  430. @endcode
  431. @par Specification
  432. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  433. >3.2.1. User Information (rfc3986)</a>
  434. @see
  435. @ref has_userinfo,
  436. @ref encoded_password,
  437. @ref encoded_user,
  438. @ref encoded_userinfo,
  439. @ref password,
  440. @ref user,
  441. @ref userinfo.
  442. */
  443. bool
  444. has_password() const noexcept;
  445. /** Return the password
  446. If present, this function returns a
  447. string representing the password (which
  448. may be an empty string).
  449. Otherwise it returns an empty string.
  450. Any percent-escapes in the string are
  451. decoded first.
  452. @param token A string token to receive the result.
  453. @return The password
  454. @par Example
  455. @code
  456. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).password() == "pass" );
  457. @endcode
  458. @par Complexity
  459. Linear in `this->password().size()`.
  460. @par Exception Safety
  461. Calls to allocate may throw.
  462. @par BNF
  463. @code
  464. userinfo = user [ ":" [ password ] ]
  465. user = *( unreserved / pct-encoded / sub-delims )
  466. password = *( unreserved / pct-encoded / sub-delims / ":" )
  467. @endcode
  468. @par Specification
  469. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  470. >3.2.1. User Information (rfc3986)</a>
  471. @see
  472. @ref has_password,
  473. @ref has_userinfo,
  474. @ref encoded_password,
  475. @ref encoded_user,
  476. @ref encoded_userinfo,
  477. @ref user,
  478. @ref userinfo.
  479. */
  480. template<BOOST_URL_STRTOK_TPARAM>
  481. BOOST_URL_STRTOK_RETURN
  482. password(
  483. BOOST_URL_STRTOK_ARG(token)) const
  484. {
  485. encoding_opts opt;
  486. opt.space_as_plus = false;
  487. return encoded_password().decode(
  488. opt, std::move(token));
  489. }
  490. /** Return the password
  491. This function returns the password portion
  492. of the userinfo as a percent-encoded string.
  493. @return The password
  494. @par Example
  495. @code
  496. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_password() == "pass" );
  497. @endcode
  498. @par Complexity
  499. Constant.
  500. @par Exception Safety
  501. Throws nothing.
  502. @par BNF
  503. @code
  504. userinfo = user [ ":" [ password ] ]
  505. user = *( unreserved / pct-encoded / sub-delims )
  506. password = *( unreserved / pct-encoded / sub-delims / ":" )
  507. @endcode
  508. @par Specification
  509. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1"
  510. >3.2.1. User Information (rfc3986)</a>
  511. @see
  512. @ref has_password,
  513. @ref has_userinfo,
  514. @ref encoded_user,
  515. @ref encoded_userinfo,
  516. @ref password,
  517. @ref user,
  518. @ref userinfo.
  519. */
  520. pct_string_view
  521. encoded_password() const noexcept;
  522. //--------------------------------------------
  523. //
  524. // Host
  525. //
  526. //--------------------------------------------
  527. /** Return the host type
  528. This function returns one of the
  529. following constants representing the
  530. type of host present.
  531. @li @ref host_type::ipv4
  532. @li @ref host_type::ipv6
  533. @li @ref host_type::ipvfuture
  534. @li @ref host_type::name
  535. @return The host type
  536. @par Example
  537. @code
  538. assert( url_view( "https://192.168.0.1/local.htm" ).host_type() == host_type::ipv4 );
  539. @endcode
  540. @par Complexity
  541. Constant.
  542. @par Exception Safety
  543. Throws nothing.
  544. @par Specification
  545. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  546. >3.2.2. Host (rfc3986)</a>
  547. */
  548. urls::host_type
  549. host_type() const noexcept
  550. {
  551. return u_.host_type_;
  552. }
  553. /** Return the host
  554. This function returns the host portion
  555. of the authority as a string, or the
  556. empty string if there is no authority.
  557. Any percent-escapes in the string are
  558. decoded first.
  559. @param token A string token to receive the result.
  560. @return The host
  561. @par Example
  562. @code
  563. assert( url_view( "https://www%2droot.example.com/" ).host() == "www-root.example.com" );
  564. @endcode
  565. @par Complexity
  566. Linear in `this->host().size()`.
  567. @par Exception Safety
  568. Calls to allocate may throw.
  569. @par BNF
  570. @code
  571. host = IP-literal / IPv4address / reg-name
  572. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  573. reg-name = *( unreserved / pct-encoded / "-" / ".")
  574. @endcode
  575. @par Specification
  576. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  577. >3.2.2. Host (rfc3986)</a>
  578. */
  579. template<BOOST_URL_STRTOK_TPARAM>
  580. BOOST_URL_STRTOK_RETURN
  581. host(
  582. BOOST_URL_STRTOK_ARG(token)) const
  583. {
  584. encoding_opts opt;
  585. opt.space_as_plus = false;
  586. return encoded_host().decode(
  587. opt, std::move(token));
  588. }
  589. /** Return the host
  590. This function returns the host portion
  591. of the authority as a string, or the
  592. empty string if there is no authority.
  593. The returned string may contain
  594. percent escapes.
  595. @return The host
  596. @par Example
  597. @code
  598. assert( url_view( "https://www%2droot.example.com/" ).encoded_host() == "www%2droot.example.com" );
  599. @endcode
  600. @par Complexity
  601. Constant.
  602. @par Exception Safety
  603. Throws nothing.
  604. @par BNF
  605. @code
  606. host = IP-literal / IPv4address / reg-name
  607. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  608. reg-name = *( unreserved / pct-encoded / "-" / ".")
  609. @endcode
  610. @par Specification
  611. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  612. >3.2.2. Host (rfc3986)</a>
  613. */
  614. pct_string_view
  615. encoded_host() const noexcept;
  616. /** Return the host
  617. The value returned by this function
  618. depends on the type of host returned
  619. from the function @ref host_type.
  620. @li If the type is @ref host_type::ipv4,
  621. then the IPv4 address string is returned.
  622. @li If the type is @ref host_type::ipv6,
  623. then the IPv6 address string is returned,
  624. without any enclosing brackets.
  625. @li If the type is @ref host_type::ipvfuture,
  626. then the IPvFuture address string is returned,
  627. without any enclosing brackets.
  628. @li If the type is @ref host_type::name,
  629. then the host name string is returned.
  630. Any percent-escapes in the string are
  631. decoded first.
  632. @li If the type is @ref host_type::none,
  633. then an empty string is returned.
  634. @param token A string token to receive the result.
  635. @return The host address
  636. @par Example
  637. @code
  638. assert( url_view( "https://[1::6:c0a8:1]/" ).host_address() == "1::6:c0a8:1" );
  639. @endcode
  640. @par Complexity
  641. Linear in `this->host_address().size()`.
  642. @par Exception Safety
  643. Calls to allocate may throw.
  644. @par BNF
  645. @code
  646. host = IP-literal / IPv4address / reg-name
  647. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  648. reg-name = *( unreserved / pct-encoded / "-" / ".")
  649. @endcode
  650. @par Specification
  651. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  652. >3.2.2. Host (rfc3986)</a>
  653. */
  654. template<BOOST_URL_STRTOK_TPARAM>
  655. BOOST_URL_STRTOK_RETURN
  656. host_address(
  657. BOOST_URL_STRTOK_ARG(token)) const
  658. {
  659. encoding_opts opt;
  660. opt.space_as_plus = false;
  661. return encoded_host_address().decode(
  662. opt, std::move(token));
  663. }
  664. /** Return the host
  665. The value returned by this function
  666. depends on the type of host returned
  667. from the function @ref host_type.
  668. @li If the type is @ref host_type::ipv4,
  669. then the IPv4 address string is returned.
  670. @li If the type is @ref host_type::ipv6,
  671. then the IPv6 address string is returned,
  672. without any enclosing brackets.
  673. @li If the type is @ref host_type::ipvfuture,
  674. then the IPvFuture address string is returned,
  675. without any enclosing brackets.
  676. @li If the type is @ref host_type::name,
  677. then the host name string is returned.
  678. Any percent-escapes in the string are
  679. decoded first.
  680. @li If the type is @ref host_type::none,
  681. then an empty string is returned.
  682. The returned string may contain
  683. percent escapes.
  684. @return The host address
  685. @par Example
  686. @code
  687. assert( url_view( "https://www%2droot.example.com/" ).encoded_host_address() == "www%2droot.example.com" );
  688. @endcode
  689. @par Complexity
  690. Constant.
  691. @par Exception Safety
  692. Throws nothing.
  693. @par BNF
  694. @code
  695. host = IP-literal / IPv4address / reg-name
  696. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  697. reg-name = *( unreserved / pct-encoded / "-" / ".")
  698. @endcode
  699. @par Specification
  700. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  701. >3.2.2. Host (rfc3986)</a>
  702. */
  703. pct_string_view
  704. encoded_host_address() const noexcept;
  705. /** Return the host IPv4 address
  706. If the host type is @ref host_type::ipv4,
  707. this function returns the address as
  708. a value of type @ref ipv4_address.
  709. Otherwise, if the host type is not an IPv4
  710. address, it returns a default-constructed
  711. value which is equal to the unspecified
  712. address "0.0.0.0".
  713. @return The host IPv4 address
  714. @par Example
  715. @code
  716. assert( url_view( "http://127.0.0.1/index.htm?user=win95" ).host_ipv4_address() == ipv4_address( "127.0.0.1" ) );
  717. @endcode
  718. @par Complexity
  719. Constant.
  720. @par Exception Safety
  721. Throws nothing.
  722. @par BNF
  723. @code
  724. IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  725. dec-octet = DIGIT ; 0-9
  726. / %x31-39 DIGIT ; 10-99
  727. / "1" 2DIGIT ; 100-199
  728. / "2" %x30-34 DIGIT ; 200-249
  729. / "25" %x30-35 ; 250-255
  730. @endcode
  731. @par Specification
  732. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  733. >3.2.2. Host (rfc3986)</a>
  734. */
  735. ipv4_address
  736. host_ipv4_address() const noexcept;
  737. /** Return the host IPv6 address
  738. If the host type is @ref host_type::ipv6,
  739. this function returns the address as
  740. a value of type @ref ipv6_address.
  741. Otherwise, if the host type is not an IPv6
  742. address, it returns a default-constructed
  743. value which is equal to the unspecified
  744. address "0:0:0:0:0:0:0:0".
  745. @return The host IPv6 address
  746. @par Example
  747. @code
  748. assert( url_view( "ftp://[::1]/" ).host_ipv6_address() == ipv6_address( "::1" ) );
  749. @endcode
  750. @par Complexity
  751. Constant.
  752. @par Exception Safety
  753. Throws nothing.
  754. @par BNF
  755. @code
  756. IPv6address = 6( h16 ":" ) ls32
  757. / "::" 5( h16 ":" ) ls32
  758. / [ h16 ] "::" 4( h16 ":" ) ls32
  759. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  760. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  761. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  762. / [ *4( h16 ":" ) h16 ] "::" ls32
  763. / [ *5( h16 ":" ) h16 ] "::" h16
  764. / [ *6( h16 ":" ) h16 ] "::"
  765. ls32 = ( h16 ":" h16 ) / IPv4address
  766. ; least-significant 32 bits of address
  767. h16 = 1*4HEXDIG
  768. ; 16 bits of address represented in hexadecimal
  769. @endcode
  770. @par Specification
  771. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  772. >3.2.2. Host (rfc3986)</a>
  773. */
  774. ipv6_address
  775. host_ipv6_address() const noexcept;
  776. /** Return the host IPvFuture address
  777. If the host type is @ref host_type::ipvfuture,
  778. this function returns the address as
  779. a string.
  780. Otherwise, if the host type is not an
  781. IPvFuture address, it returns an
  782. empty string.
  783. @return The host IPvFuture address
  784. @par Example
  785. @code
  786. assert( url_view( "http://[v1fe.d:9]/index.htm" ).host_ipvfuture() == "v1fe.d:9" );
  787. @endcode
  788. @par Complexity
  789. Constant.
  790. @par Exception Safety
  791. Throws nothing.
  792. @par BNF
  793. @code
  794. IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  795. @endcode
  796. @par Specification
  797. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  798. >3.2.2. Host (rfc3986)</a>
  799. */
  800. core::string_view
  801. host_ipvfuture() const noexcept;
  802. /** Return the host name
  803. If the host type is @ref host_type::name,
  804. this function returns the name as
  805. a string.
  806. Otherwise, if the host type is not a
  807. name, it returns an empty string.
  808. Any percent-escapes in the string are
  809. decoded first.
  810. @param token A string token to receive the result
  811. @return The host name
  812. @par Example
  813. @code
  814. assert( url_view( "https://www%2droot.example.com/" ).host_name() == "www-root.example.com" );
  815. @endcode
  816. @par Complexity
  817. Linear in `this->host_name().size()`.
  818. @par Exception Safety
  819. Calls to allocate may throw.
  820. @par BNF
  821. @code
  822. host = IP-literal / IPv4address / reg-name
  823. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  824. reg-name = *( unreserved / pct-encoded / "-" / ".")
  825. @endcode
  826. @par Specification
  827. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  828. >3.2.2. Host (rfc3986)</a>
  829. */
  830. template<BOOST_URL_STRTOK_TPARAM>
  831. BOOST_URL_STRTOK_RETURN
  832. host_name(
  833. BOOST_URL_STRTOK_ARG(token)) const
  834. {
  835. encoding_opts opt;
  836. opt.space_as_plus = false;
  837. return encoded_host_name().decode(
  838. opt, std::move(token));
  839. }
  840. /** Return the host name
  841. If the host type is @ref host_type::name,
  842. this function returns the name as
  843. a string.
  844. Otherwise, if the host type is not an
  845. name, it returns an empty string.
  846. The returned string may contain
  847. percent escapes.
  848. @return The host name
  849. @par Example
  850. @code
  851. assert( url_view( "https://www%2droot.example.com/" ).encoded_host_name() == "www%2droot.example.com" );
  852. @endcode
  853. @par Complexity
  854. Constant.
  855. @par Exception Safety
  856. Throws nothing.
  857. @par BNF
  858. @code
  859. host = IP-literal / IPv4address / reg-name
  860. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  861. reg-name = *( unreserved / pct-encoded / "-" / ".")
  862. @endcode
  863. @par Specification
  864. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  865. >3.2.2. Host (rfc3986)</a>
  866. */
  867. pct_string_view
  868. encoded_host_name() const noexcept;
  869. //--------------------------------------------
  870. //
  871. // Port
  872. //
  873. //--------------------------------------------
  874. /** Return true if a port is present
  875. This function returns true if an
  876. authority is present and contains a port.
  877. @return `true` if a port is present, otherwise `false`
  878. @par Example
  879. @code
  880. assert( url_view( "wss://www.example.com:443" ).has_port() );
  881. @endcode
  882. @par Complexity
  883. Constant.
  884. @par Exception Safety
  885. Throws nothing.
  886. @par BNF
  887. @code
  888. authority = [ userinfo "@" ] host [ ":" port ]
  889. port = *DIGIT
  890. @endcode
  891. @par Specification
  892. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
  893. >3.2.3. Port (rfc3986)</a>
  894. @see
  895. @ref encoded_host_and_port,
  896. @ref port,
  897. @ref port_number.
  898. */
  899. bool
  900. has_port() const noexcept;
  901. /** Return the port
  902. If present, this function returns a
  903. string representing the port (which
  904. may be empty).
  905. Otherwise it returns an empty string.
  906. @return The port as a string
  907. @par Example
  908. @code
  909. assert( url_view( "http://localhost.com:8080" ).port() == "8080" );
  910. @endcode
  911. @par Complexity
  912. Constant.
  913. @par Exception Safety
  914. Throws nothing.
  915. @par BNF
  916. @code
  917. port = *DIGIT
  918. @endcode
  919. @par Specification
  920. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
  921. >3.2.3. Port (rfc3986)</a>
  922. @see
  923. @ref encoded_host_and_port,
  924. @ref has_port,
  925. @ref port_number.
  926. */
  927. core::string_view
  928. port() const noexcept;
  929. /** Return the port
  930. If a port is present and the numerical
  931. value is representable, it is returned
  932. as an unsigned integer. Otherwise, the
  933. number zero is returned.
  934. @return The port number
  935. @par Example
  936. @code
  937. assert( url_view( "http://localhost.com:8080" ).port_number() == 8080 );
  938. @endcode
  939. @par Complexity
  940. Constant.
  941. @par Exception Safety
  942. Throws nothing.
  943. @par BNF
  944. @code
  945. port = *DIGIT
  946. @endcode
  947. @par Specification
  948. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
  949. >3.2.3. Port (rfc3986)</a>
  950. @see
  951. @ref encoded_host_and_port,
  952. @ref has_port,
  953. @ref port.
  954. */
  955. std::uint16_t
  956. port_number() const noexcept;
  957. /** Return the host and port
  958. If an authority is present, this
  959. function returns the host and optional
  960. port as a string, which may be empty.
  961. Otherwise it returns an empty string.
  962. The returned string may contain
  963. percent escapes.
  964. @par Example
  965. @code
  966. assert( url_view( "http://www.example.com:8080/index.htm" ).encoded_host_and_port() == "www.example.com:8080" );
  967. @endcode
  968. @par Complexity
  969. Constant.
  970. @par Exception Safety
  971. Throws nothing.
  972. @par BNF
  973. @code
  974. authority = [ userinfo "@" ] host [ ":" port ]
  975. @endcode
  976. @par Specification
  977. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2"
  978. >3.2.2. Host (rfc3986)</a>
  979. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3"
  980. >3.2.3. Port (rfc3986)</a>
  981. @see
  982. @ref has_port,
  983. @ref port,
  984. @ref port_number.
  985. @return The host and port
  986. */
  987. pct_string_view
  988. encoded_host_and_port() const noexcept;
  989. //--------------------------------------------
  990. //
  991. // Comparison
  992. //
  993. //--------------------------------------------
  994. /** Return the result of comparing this with another authority
  995. This function compares two authorities
  996. according to Syntax-Based comparison
  997. algorithm.
  998. @par Exception Safety
  999. Throws nothing.
  1000. @param other The authority to compare
  1001. @return `-1` if `*this < other`, `0` if
  1002. `this == other`, and 1 if `this > other`.
  1003. @par Specification
  1004. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  1005. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  1006. */
  1007. int
  1008. compare(authority_view const& other) const noexcept;
  1009. /** Return the result of comparing two authorities.
  1010. The authorities are compared component
  1011. by component as if they were first
  1012. normalized.
  1013. @par Complexity
  1014. Linear in `min( a0.size(), a1.size() )`
  1015. @par Exception Safety
  1016. Throws nothing
  1017. @param a0 The first authority to compare
  1018. @param a1 The second authority to compare
  1019. @return `true` if `a0 == a1`, otherwise `false`
  1020. */
  1021. friend
  1022. bool
  1023. operator==(
  1024. authority_view const& a0,
  1025. authority_view const& a1) noexcept
  1026. {
  1027. return a0.compare(a1) == 0;
  1028. }
  1029. /** Return the result of comparing two authorities.
  1030. The authorities are compared component
  1031. by component as if they were first
  1032. normalized.
  1033. @par Complexity
  1034. Linear in `min( a0.size(), a1.size() )`
  1035. @par Exception Safety
  1036. Throws nothing
  1037. @param a0 The first authority to compare
  1038. @param a1 The second authority to compare
  1039. @return `true` if `a0 != a1`, otherwise `false`
  1040. */
  1041. friend
  1042. bool
  1043. operator!=(
  1044. authority_view const& a0,
  1045. authority_view const& a1) noexcept
  1046. {
  1047. return ! (a0 == a1);
  1048. }
  1049. /** Return the result of comparing two authorities.
  1050. The authorities are compared component
  1051. by component as if they were first
  1052. normalized.
  1053. @par Complexity
  1054. Linear in `min( a0.size(), a1.size() )`
  1055. @par Exception Safety
  1056. Throws nothing
  1057. @param a0 The first authority to compare
  1058. @param a1 The second authority to compare
  1059. @return `true` if `a0 < a1`, otherwise `false`
  1060. */
  1061. friend
  1062. bool
  1063. operator<(
  1064. authority_view const& a0,
  1065. authority_view const& a1) noexcept
  1066. {
  1067. return a0.compare(a1) < 0;
  1068. }
  1069. /** Return the result of comparing two authorities.
  1070. The authorities are compared component
  1071. by component as if they were first
  1072. normalized.
  1073. @par Complexity
  1074. Linear in `min( a0.size(), a1.size() )`
  1075. @par Exception Safety
  1076. Throws nothing
  1077. @param a0 The first authority to compare
  1078. @param a1 The second authority to compare
  1079. @return `true` if `a0 <= a1`, otherwise `false`
  1080. */
  1081. friend
  1082. bool
  1083. operator<=(
  1084. authority_view const& a0,
  1085. authority_view const& a1) noexcept
  1086. {
  1087. return a0.compare(a1) <= 0;
  1088. }
  1089. /** Return the result of comparing two authorities.
  1090. The authorities are compared component
  1091. by component as if they were first
  1092. normalized.
  1093. @par Complexity
  1094. Linear in `min( a0.size(), a1.size() )`
  1095. @par Exception Safety
  1096. Throws nothing
  1097. @param a0 The first authority to compare
  1098. @param a1 The second authority to compare
  1099. @return `true` if `a0 > a1`, otherwise `false`
  1100. */
  1101. friend
  1102. bool
  1103. operator>(
  1104. authority_view const& a0,
  1105. authority_view const& a1) noexcept
  1106. {
  1107. return a0.compare(a1) > 0;
  1108. }
  1109. /** Return the result of comparing two authorities.
  1110. The authorities are compared component
  1111. by component as if they were first
  1112. normalized.
  1113. @par Complexity
  1114. Linear in `min( a0.size(), a1.size() )`
  1115. @par Exception Safety
  1116. Throws nothing
  1117. @param a0 The first authority to compare
  1118. @param a1 The second authority to compare
  1119. @return `true` if `a0 >= a1`, otherwise `false`
  1120. */
  1121. friend
  1122. bool
  1123. operator>=(
  1124. authority_view const& a0,
  1125. authority_view const& a1) noexcept
  1126. {
  1127. return a0.compare(a1) >= 0;
  1128. }
  1129. //--------------------------------------------
  1130. /** Format the encoded authority to the output stream
  1131. This hidden friend function serializes the encoded URL
  1132. to the output stream.
  1133. @par Example
  1134. @code
  1135. authority_view a( "www.example.com" );
  1136. std::cout << a << std::endl;
  1137. @endcode
  1138. @return A reference to the output stream, for chaining
  1139. @param os The output stream to write to
  1140. @param a The URL to write
  1141. */
  1142. friend
  1143. std::ostream&
  1144. operator<<(
  1145. std::ostream& os,
  1146. authority_view const& a)
  1147. {
  1148. return os << a.buffer();
  1149. }
  1150. };
  1151. /** Format the encoded authority to the output stream
  1152. This function serializes the encoded URL
  1153. to the output stream.
  1154. @par Example
  1155. @code
  1156. authority_view a( "www.example.com" );
  1157. std::cout << a << std::endl;
  1158. @endcode
  1159. @return A reference to the output stream, for chaining
  1160. @param os The output stream to write to
  1161. @param a The URL to write
  1162. */
  1163. std::ostream&
  1164. operator<<(
  1165. std::ostream& os,
  1166. authority_view const& a);
  1167. //------------------------------------------------
  1168. /** Parse an authority
  1169. This function parses a string according to
  1170. the authority grammar below, and returns an
  1171. @ref authority_view referencing the string.
  1172. Ownership of the string is not transferred;
  1173. the caller is responsible for ensuring that
  1174. the lifetime of the string extends until the
  1175. view is no longer being accessed.
  1176. @par BNF
  1177. @code
  1178. authority = [ userinfo "@" ] host [ ":" port ]
  1179. userinfo = user [ ":" [ password ] ]
  1180. user = *( unreserved / pct-encoded / sub-delims )
  1181. password = *( unreserved / pct-encoded / sub-delims / ":" )
  1182. host = IP-literal / IPv4address / reg-name
  1183. port = *DIGIT
  1184. @endcode
  1185. @par Exception Safety
  1186. Throws nothing.
  1187. @return A view to the parsed authority
  1188. @param s The string to parse
  1189. @par Specification
  1190. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2"
  1191. >3.2. Authority (rfc3986)</a>
  1192. @see
  1193. @ref authority_view.
  1194. */
  1195. BOOST_URL_DECL
  1196. system::result<authority_view>
  1197. parse_authority(
  1198. core::string_view s) noexcept;
  1199. //------------------------------------------------
  1200. } // urls
  1201. } // boost
  1202. #endif