authority_view.hpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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