authority_view.hpp 37 KB

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