authority_view.hpp 37 KB

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