url_view_base.hpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792
  1. //
  2. // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  3. // Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/url
  9. //
  10. #ifndef BOOST_URL_URL_VIEW_BASE_HPP
  11. #define BOOST_URL_URL_VIEW_BASE_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/authority_view.hpp>
  14. #include <boost/url/host_type.hpp>
  15. #include <boost/url/ipv4_address.hpp>
  16. #include <boost/url/ipv6_address.hpp>
  17. #include <boost/url/params_view.hpp>
  18. #include <boost/url/params_encoded_view.hpp>
  19. #include <boost/url/pct_string_view.hpp>
  20. #include <boost/url/scheme.hpp>
  21. #include <boost/url/segments_encoded_view.hpp>
  22. #include <boost/url/segments_view.hpp>
  23. #include <boost/url/detail/url_impl.hpp>
  24. #include <boost/url/grammar/string_token.hpp>
  25. #include <boost/assert.hpp>
  26. #include <cstddef>
  27. #include <cstdint>
  28. #include <iosfwd>
  29. #include <memory>
  30. #include <string>
  31. #include <utility>
  32. namespace boost {
  33. namespace urls {
  34. #ifndef BOOST_URL_DOCS
  35. namespace detail {
  36. struct pattern;
  37. }
  38. #endif
  39. /** Common functionality for containers
  40. This base class is used by the library
  41. to provide common member functions for
  42. containers. This cannot be instantiated
  43. directly; Instead, use one of the
  44. containers or functions:
  45. @par Containers
  46. @li @ref url
  47. @li @ref url_view
  48. @li @ref static_url
  49. @par Functions
  50. @li @ref parse_absolute_uri
  51. @li @ref parse_origin_form
  52. @li @ref parse_relative_ref
  53. @li @ref parse_uri
  54. @li @ref parse_uri_reference
  55. */
  56. class BOOST_URL_DECL
  57. url_view_base
  58. : private detail::parts_base
  59. {
  60. detail::url_impl impl_;
  61. detail::url_impl const* pi_;
  62. friend class url;
  63. friend class url_base;
  64. friend class url_view;
  65. friend class static_url_base;
  66. friend class params_base;
  67. friend class params_encoded_base;
  68. friend class params_encoded_ref;
  69. friend class params_encoded_view;
  70. friend class params_ref;
  71. friend class params_view;
  72. friend class segments_base;
  73. friend class segments_encoded_base;
  74. friend class segments_encoded_ref;
  75. friend class segments_encoded_view;
  76. friend class segments_ref;
  77. friend class segments_view;
  78. friend struct detail::pattern;
  79. struct shared_impl;
  80. url_view_base() noexcept;
  81. explicit url_view_base(
  82. detail::url_impl const&) noexcept;
  83. ~url_view_base() = default;
  84. url_view_base(
  85. url_view_base const& o) noexcept
  86. : impl_(o.impl_)
  87. , pi_(o.pi_)
  88. {
  89. if (pi_ == &o.impl_)
  90. pi_ = &impl_;
  91. }
  92. url_view_base& operator=(
  93. url_view_base const&) = delete;
  94. protected:
  95. /** Calculate a hash of the url
  96. This function calculates a hash of the
  97. url as if it were always normalized.
  98. @par Complexity
  99. Linear in `this->size()`.
  100. @par Exception Safety
  101. Throws nothing.
  102. @param salt An initial value to add to
  103. the hash
  104. @return A hash value suitable for use
  105. in hash-based containers.
  106. */
  107. std::size_t
  108. digest(std::size_t salt = 0) const noexcept;
  109. public:
  110. //--------------------------------------------
  111. //
  112. // Observers
  113. //
  114. //--------------------------------------------
  115. /** Return the maximum number of characters possible
  116. This represents the largest number
  117. of characters that are theoretically
  118. possible to represent in a url,
  119. not including any null terminator.
  120. In practice the actual possible size
  121. may be lower than this number.
  122. @par Complexity
  123. Constant.
  124. @par Exception Safety
  125. Throws nothing.
  126. @return The maximum number of characters.
  127. */
  128. static
  129. constexpr
  130. std::size_t
  131. max_size() noexcept
  132. {
  133. return BOOST_URL_MAX_SIZE;
  134. }
  135. /** Return the number of characters in the url
  136. This function returns the number of
  137. characters in the url's encoded string,
  138. not including any null terminator,
  139. if present.
  140. @par Example
  141. @code
  142. assert( url_view( "file:///Program%20Files" ).size() == 23 );
  143. @endcode
  144. @par Complexity
  145. Constant.
  146. @par Exception Safety
  147. Throws nothing.
  148. @return The number of characters in the url.
  149. */
  150. std::size_t
  151. size() const noexcept
  152. {
  153. return pi_->offset(id_end);
  154. }
  155. /** Return true if the url is empty
  156. The empty string matches the
  157. <em>relative-ref</em> grammar.
  158. @par Example
  159. @code
  160. assert( url_view( "" ).empty() );
  161. @endcode
  162. @par Complexity
  163. Constant.
  164. @par Exception Safety
  165. Throws nothing.
  166. @par BNF
  167. @code
  168. relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  169. relative-part = "//" authority path-abempty
  170. / path-absolute
  171. / path-noscheme
  172. / path-empty
  173. @endcode
  174. @par Specification
  175. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-4.2">4.2. Relative Reference (rfc3986)</a>
  176. @return `true` if the url is empty.
  177. */
  178. bool
  179. empty() const noexcept
  180. {
  181. return pi_->offset(id_end) == 0;
  182. }
  183. /** Return a pointer to the url's character buffer
  184. This function returns a pointer to
  185. the first character of the url, which
  186. is not guaranteed to be null-terminated.
  187. @par Complexity
  188. Constant.
  189. @par Exception Safety
  190. Throws nothing.
  191. @return A pointer to the first character.
  192. */
  193. char const*
  194. data() const noexcept
  195. {
  196. return pi_->cs_;
  197. }
  198. /** Return the url string
  199. This function returns the entire url,
  200. which may contain percent escapes.
  201. @par Example
  202. @code
  203. assert( url_view( "http://www.example.com" ).buffer() == "http://www.example.com" );
  204. @endcode
  205. @par Complexity
  206. Constant.
  207. @par Exception Safety
  208. Throws nothing.
  209. @return The url as a string.
  210. */
  211. core::string_view
  212. buffer() const noexcept
  213. {
  214. return core::string_view(
  215. data(), size());
  216. }
  217. /** Return the URL as a core::string_view
  218. @par Complexity
  219. Constant.
  220. @par Exception Safety
  221. Throws nothing.
  222. @return A string view of the URL.
  223. */
  224. operator core::string_view() const noexcept
  225. {
  226. return buffer();
  227. }
  228. /** Return a shared, persistent copy of the url
  229. This function returns a read-only copy of
  230. the url, with shared lifetime. The returned
  231. value owns (persists) the underlying string.
  232. The algorithm used to create the value
  233. minimizes the number of individual memory
  234. allocations, making it more efficient than
  235. when using direct standard library functions.
  236. @par Example
  237. @code
  238. std::shared_ptr< url_view const > sp;
  239. {
  240. std::string s( "http://example.com" );
  241. url_view u( s ); // u references characters in s
  242. assert( u.data() == s.data() ); // same buffer
  243. sp = u.persist();
  244. assert( sp->data() != s.data() ); // different buffer
  245. assert( sp->buffer() == s); // same contents
  246. // s is destroyed and thus u
  247. // becomes invalid, but sp remains valid.
  248. }
  249. @endcode
  250. @par Complexity
  251. Linear in `this->size()`.
  252. @par Exception Safety
  253. Calls to allocate may throw.
  254. @return A shared pointer to a read-only url_view.
  255. */
  256. std::shared_ptr<
  257. url_view const> persist() const;
  258. //--------------------------------------------
  259. //
  260. // Scheme
  261. //
  262. //--------------------------------------------
  263. /** Return true a scheme is present
  264. This function returns true if this
  265. contains a scheme.
  266. @par Example
  267. @code
  268. assert( url_view( "http://www.example.com" ).has_scheme() );
  269. @endcode
  270. @par Complexity
  271. Constant.
  272. @par Exception Safety
  273. Throws nothing.
  274. @par BNF
  275. @code
  276. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  277. absolute-URI = scheme ":" hier-part [ "?" query ]
  278. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  279. @endcode
  280. @par Specification
  281. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">3.1. Scheme (rfc3986)</a>
  282. @see
  283. @ref scheme,
  284. @ref scheme_id.
  285. @return `true` if the url contains a scheme.
  286. */
  287. bool
  288. has_scheme() const noexcept;
  289. /** Return the scheme
  290. This function returns the scheme if it
  291. exists, without a trailing colon (':').
  292. Otherwise it returns an empty string.
  293. Note that schemes are case-insensitive,
  294. and the canonical form is lowercased.
  295. @par Example
  296. @code
  297. assert( url_view( "http://www.example.com" ).scheme() == "http" );
  298. @endcode
  299. @par Exception Safety
  300. Throws nothing.
  301. @par BNF
  302. @code
  303. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  304. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  305. absolute-URI = scheme ":" hier-part [ "?" query ]
  306. @endcode
  307. @par Specification
  308. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">3.1. Scheme (rfc3986)</a>
  309. @see
  310. @ref has_scheme,
  311. @ref scheme_id.
  312. @return The scheme as a string.
  313. */
  314. core::string_view
  315. scheme() const noexcept;
  316. /** Return the scheme
  317. This function returns a value which
  318. depends on the scheme in the url:
  319. @li If the scheme is a well-known
  320. scheme, corresponding value from
  321. the enumeration @ref urls::scheme
  322. is returned.
  323. @li If a scheme is present but is not
  324. a well-known scheme, the value
  325. returned is @ref urls::scheme::unknown.
  326. @li Otherwise, if the scheme is absent
  327. the value returned is
  328. @ref urls::scheme::none.
  329. @par Example
  330. @code
  331. assert( url_view( "wss://www.example.com/crypto.cgi" ).scheme_id() == scheme::wss );
  332. @endcode
  333. @par Complexity
  334. Constant.
  335. @par Exception Safety
  336. Throws nothing.
  337. @par BNF
  338. @code
  339. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  340. absolute-URI = scheme ":" hier-part [ "?" query ]
  341. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  342. @endcode
  343. @par Specification
  344. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">3.1. Scheme (rfc3986)</a>
  345. @see
  346. @ref has_scheme,
  347. @ref scheme.
  348. @return The scheme as an enumeration value.
  349. */
  350. urls::scheme
  351. scheme_id() const noexcept;
  352. //--------------------------------------------
  353. //
  354. // Authority
  355. //
  356. //--------------------------------------------
  357. /** Return true if an authority is present
  358. This function returns true if the url
  359. contains an authority. The presence of
  360. an authority is denoted by a double
  361. slash ("//") at the beginning or after
  362. the scheme.
  363. @par Example
  364. @code
  365. assert( url_view( "http://www.example.com/index.htm" ).has_authority() );
  366. @endcode
  367. @par Complexity
  368. Constant.
  369. @par Exception Safety
  370. Throws nothing.
  371. @par BNF
  372. @code
  373. authority = [ userinfo "@" ] host [ ":" port ]
  374. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  375. absolute-URI = scheme ":" hier-part [ "?" query ]
  376. URI-reference = URI / relative-ref
  377. relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  378. hier-part = "//" authority path-abempty
  379. ; (more...)
  380. relative-part = "//" authority path-abempty
  381. ; (more...)
  382. @endcode
  383. @par Specification
  384. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2">3.2. Authority (rfc3986)</a>
  385. @see
  386. @ref authority,
  387. @ref encoded_authority.
  388. @return `true` if the url contains an authority.
  389. */
  390. bool
  391. has_authority() const noexcept
  392. {
  393. return pi_->len(id_user) > 0;
  394. }
  395. /** Return the authority
  396. This function returns the authority as
  397. an @ref authority_view.
  398. @par Example
  399. @code
  400. authority_view a = url_view( "https://www.example.com:8080/index.htm" ).authority();
  401. @endcode
  402. @par Complexity
  403. Constant.
  404. @par Exception Safety
  405. Throws nothing.
  406. @par BNF
  407. @code
  408. authority = [ userinfo "@" ] host [ ":" port ]
  409. @endcode
  410. @par Specification
  411. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2">3.2. Authority (rfc3986)</a>
  412. @see
  413. @ref encoded_authority,
  414. @ref has_authority.
  415. @return An authority_view representing the authority.
  416. */
  417. authority_view
  418. authority() const noexcept;
  419. /** Return the authority.
  420. If present, this function returns a
  421. string representing the authority (which
  422. may be empty).
  423. Otherwise it returns an empty string.
  424. The returned string may contain
  425. percent escapes.
  426. @par Example
  427. @code
  428. assert( url_view( "file://Network%20Drive/My%2DFiles" ).encoded_authority() == "Network%20Drive" );
  429. @endcode
  430. @par Complexity
  431. Constant.
  432. @par Exception Safety
  433. Throws nothing.
  434. @par BNF
  435. @code
  436. authority = [ userinfo "@" ] host [ ":" port ]
  437. @endcode
  438. @par Specification
  439. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2">3.2. Authority (rfc3986)</a>
  440. @see
  441. @ref authority,
  442. @ref has_authority.
  443. @return The authority as a string.
  444. */
  445. pct_string_view
  446. encoded_authority() const noexcept;
  447. //--------------------------------------------
  448. //
  449. // Userinfo
  450. //
  451. //--------------------------------------------
  452. /** Return true if a userinfo is present
  453. This function returns true if this
  454. contains a userinfo.
  455. @par Example
  456. @code
  457. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_userinfo() );
  458. @endcode
  459. @par Complexity
  460. Constant.
  461. @par Exception Safety
  462. Throws nothing.
  463. @par BNF
  464. @code
  465. userinfo = user [ ":" [ password ] ]
  466. authority = [ userinfo "@" ] host [ ":" port ]
  467. @endcode
  468. @par Specification
  469. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  470. @see
  471. @ref has_password,
  472. @ref encoded_password,
  473. @ref encoded_user,
  474. @ref encoded_userinfo,
  475. @ref password,
  476. @ref user,
  477. @ref userinfo.
  478. @return `true` if the userinfo is present.
  479. */
  480. bool
  481. has_userinfo() const noexcept;
  482. /** Return true if a password is present
  483. This function returns true if the
  484. userinfo is present and contains
  485. a password.
  486. @par Example
  487. @code
  488. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).has_password() );
  489. @endcode
  490. @par Complexity
  491. Constant.
  492. @par Exception Safety
  493. Throws nothing.
  494. @par BNF
  495. @code
  496. userinfo = user [ ":" [ password ] ]
  497. user = *( unreserved / pct-encoded / sub-delims )
  498. password = *( unreserved / pct-encoded / sub-delims / ":" )
  499. @endcode
  500. @par Specification
  501. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  502. @see
  503. @ref has_userinfo,
  504. @ref encoded_password,
  505. @ref encoded_user,
  506. @ref encoded_userinfo,
  507. @ref password,
  508. @ref user,
  509. @ref userinfo.
  510. @return `true` if the userinfo contains a password.
  511. */
  512. bool
  513. has_password() const noexcept;
  514. /** Return the userinfo
  515. If present, this function returns a
  516. string representing the userinfo (which
  517. may be empty).
  518. Otherwise it returns an empty string.
  519. Any percent-escapes in the string are
  520. decoded first.
  521. @note
  522. This function uses the string token
  523. return type customization. Depending on
  524. the token passed, the return type and
  525. behavior of the function can be different.
  526. See @ref string_token::return_string
  527. for more information.
  528. @par Example
  529. @code
  530. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).userinfo() == "jane-doe:pass" );
  531. @endcode
  532. @par Complexity
  533. Linear in `this->userinfo().size()`.
  534. @par Exception Safety
  535. Calls to allocate may throw.
  536. @return When called with no arguments,
  537. a value of type `std::string` is
  538. returned. Otherwise, the return type
  539. and meaning depends on the string token
  540. passed to the function.
  541. @par BNF
  542. @code
  543. userinfo = user [ ":" [ password ] ]
  544. authority = [ userinfo "@" ] host [ ":" port ]
  545. @endcode
  546. @par Specification
  547. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  548. @see
  549. @ref has_password,
  550. @ref has_userinfo,
  551. @ref encoded_password,
  552. @ref encoded_user,
  553. @ref encoded_userinfo,
  554. @ref password,
  555. @ref user.
  556. @param token The string token to use.
  557. @return The userinfo as a string.
  558. */
  559. template<BOOST_URL_STRTOK_TPARAM>
  560. BOOST_URL_STRTOK_RETURN
  561. userinfo(
  562. StringToken&& token = {}) const
  563. {
  564. encoding_opts opt;
  565. opt.space_as_plus = false;
  566. return encoded_userinfo().decode(
  567. opt, std::forward<StringToken>(token));
  568. }
  569. /** Return the userinfo
  570. If present, this function returns a
  571. string representing the userinfo (which
  572. may be empty).
  573. Otherwise it returns an empty string.
  574. The returned string may contain
  575. percent escapes.
  576. @par Example
  577. @code
  578. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_userinfo() == "jane%2Ddoe:pass" );
  579. @endcode
  580. @par Complexity
  581. Constant.
  582. @par Exception Safety
  583. Throws nothing
  584. @par BNF
  585. @code
  586. userinfo = user [ ":" [ password ] ]
  587. authority = [ userinfo "@" ] host [ ":" port ]
  588. @endcode
  589. @par Specification
  590. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  591. @see
  592. @ref has_password,
  593. @ref has_userinfo,
  594. @ref encoded_password,
  595. @ref encoded_user,
  596. @ref password,
  597. @ref user,
  598. @ref userinfo.
  599. @return The userinfo as a string.
  600. */
  601. pct_string_view
  602. encoded_userinfo() const noexcept;
  603. //--------------------------------------------
  604. /** Return the user
  605. If present, this function returns a
  606. string representing the user (which
  607. may be empty).
  608. Otherwise it returns an empty string.
  609. Any percent-escapes in the string are
  610. decoded first.
  611. @par Example
  612. @code
  613. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).user() == "jane-doe" );
  614. @endcode
  615. @par Complexity
  616. Linear in `this->user().size()`.
  617. @par Exception Safety
  618. Calls to allocate may throw.
  619. @par BNF
  620. @code
  621. userinfo = user [ ":" [ password ] ]
  622. user = *( unreserved / pct-encoded / sub-delims )
  623. password = *( unreserved / pct-encoded / sub-delims / ":" )
  624. @endcode
  625. @par Specification
  626. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  627. @see
  628. @ref has_password,
  629. @ref has_userinfo,
  630. @ref encoded_password,
  631. @ref encoded_user,
  632. @ref encoded_userinfo,
  633. @ref password,
  634. @ref userinfo.
  635. @param token The string token to use.
  636. @return The user as a string.
  637. */
  638. template<BOOST_URL_STRTOK_TPARAM>
  639. BOOST_URL_STRTOK_RETURN
  640. user(
  641. StringToken&& token = {}) const
  642. {
  643. encoding_opts opt;
  644. opt.space_as_plus = false;
  645. return encoded_user().decode(
  646. opt, std::forward<StringToken>(token));
  647. }
  648. /** Return the user
  649. If present, this function returns a
  650. string representing the user (which
  651. may be empty).
  652. Otherwise it returns an empty string.
  653. The returned string may contain
  654. percent escapes.
  655. @par Example
  656. @code
  657. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_user() == "jane%2Ddoe" );
  658. @endcode
  659. @par Complexity
  660. Constant.
  661. @par Exception Safety
  662. Throws nothing.
  663. @par BNF
  664. @code
  665. userinfo = user [ ":" [ password ] ]
  666. user = *( unreserved / pct-encoded / sub-delims )
  667. password = *( unreserved / pct-encoded / sub-delims / ":" )
  668. @endcode
  669. @par Specification
  670. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  671. @see
  672. @ref has_password,
  673. @ref has_userinfo,
  674. @ref encoded_password,
  675. @ref encoded_userinfo,
  676. @ref password,
  677. @ref user,
  678. @ref userinfo.
  679. @return The user as a string.
  680. */
  681. pct_string_view
  682. encoded_user() const noexcept;
  683. /** Return the password
  684. If present, this function returns a
  685. string representing the password (which
  686. may be an empty string).
  687. Otherwise it returns an empty string.
  688. Any percent-escapes in the string are
  689. decoded first.
  690. @par Example
  691. @code
  692. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).password() == "pass" );
  693. @endcode
  694. @par Complexity
  695. Linear in `this->password().size()`.
  696. @par Exception Safety
  697. Calls to allocate may throw.
  698. @par BNF
  699. @code
  700. userinfo = user [ ":" [ password ] ]
  701. user = *( unreserved / pct-encoded / sub-delims )
  702. password = *( unreserved / pct-encoded / sub-delims / ":" )
  703. @endcode
  704. @par Specification
  705. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  706. @see
  707. @ref has_password,
  708. @ref has_userinfo,
  709. @ref encoded_password,
  710. @ref encoded_user,
  711. @ref encoded_userinfo,
  712. @ref user,
  713. @ref userinfo.
  714. @param token The string token to use.
  715. @return The password as a string.
  716. */
  717. template<BOOST_URL_STRTOK_TPARAM>
  718. BOOST_URL_STRTOK_RETURN
  719. password(
  720. StringToken&& token = {}) const
  721. {
  722. encoding_opts opt;
  723. opt.space_as_plus = false;
  724. return encoded_password().decode(
  725. opt, std::forward<StringToken>(token));
  726. }
  727. /** Return the password
  728. This function returns the password portion
  729. of the userinfo as a percent-encoded string.
  730. @par Example
  731. @code
  732. assert( url_view( "http://jane%2Ddoe:pass@example.com" ).encoded_password() == "pass" );
  733. @endcode
  734. @par Complexity
  735. Constant.
  736. @par Exception Safety
  737. Throws nothing.
  738. @par BNF
  739. @code
  740. userinfo = user [ ":" [ password ] ]
  741. user = *( unreserved / pct-encoded / sub-delims )
  742. password = *( unreserved / pct-encoded / sub-delims / ":" )
  743. @endcode
  744. @par Specification
  745. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">3.2.1. User Information (rfc3986)</a>
  746. @see
  747. @ref has_password,
  748. @ref has_userinfo,
  749. @ref encoded_user,
  750. @ref encoded_userinfo,
  751. @ref password,
  752. @ref user,
  753. @ref userinfo.
  754. @return The password as a string.
  755. */
  756. pct_string_view
  757. encoded_password() const noexcept;
  758. //--------------------------------------------
  759. //
  760. // Host
  761. //
  762. //--------------------------------------------
  763. /** Return the host type
  764. This function returns one of the
  765. following constants representing the
  766. type of host present.
  767. @li @ref host_type::ipv4
  768. @li @ref host_type::ipv6
  769. @li @ref host_type::ipvfuture
  770. @li @ref host_type::name
  771. @li @ref host_type::none
  772. When @ref has_authority is false, the
  773. host type is @ref host_type::none.
  774. @par Example
  775. @code
  776. assert( url_view( "https://192.168.0.1/local.htm" ).host_type() == host_type::ipv4 );
  777. @endcode
  778. @par Complexity
  779. Constant.
  780. @par Exception Safety
  781. Throws nothing.
  782. @par Specification
  783. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  784. @return The type of host present.
  785. */
  786. urls::host_type
  787. host_type() const noexcept
  788. {
  789. return pi_->host_type_;
  790. }
  791. /** Return the host
  792. This function returns the host portion
  793. of the authority as a string, or the
  794. empty string if there is no authority.
  795. Any percent-escapes in the string are
  796. decoded first.
  797. @par Example
  798. @code
  799. assert( url_view( "https://www%2droot.example.com/" ).host() == "www-root.example.com" );
  800. @endcode
  801. @par Complexity
  802. Linear in `this->host().size()`.
  803. @par Exception Safety
  804. Calls to allocate may throw.
  805. @par BNF
  806. @code
  807. host = IP-literal / IPv4address / reg-name
  808. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  809. reg-name = *( unreserved / pct-encoded / "-" / ".")
  810. @endcode
  811. @par Specification
  812. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  813. @param token A string token customization
  814. @return The host address as a string.
  815. */
  816. template<BOOST_URL_STRTOK_TPARAM>
  817. BOOST_URL_STRTOK_RETURN
  818. host(
  819. StringToken&& token = {}) const
  820. {
  821. encoding_opts opt;
  822. opt.space_as_plus = false;
  823. return encoded_host().decode(
  824. opt, std::forward<StringToken>(token));
  825. }
  826. /** Return the host
  827. This function returns the host portion
  828. of the authority as a string, or the
  829. empty string if there is no authority.
  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() == "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">3.2.2. Host (rfc3986)</a>
  848. @return The host address as a string.
  849. */
  850. pct_string_view
  851. encoded_host() const noexcept;
  852. /** Return the host
  853. The value returned by this function
  854. depends on the type of host returned
  855. from the function @ref host_type.
  856. @li If the type is @ref host_type::ipv4,
  857. then the IPv4 address string is returned.
  858. @li If the type is @ref host_type::ipv6,
  859. then the IPv6 address string is returned,
  860. without any enclosing brackets.
  861. @li If the type is @ref host_type::ipvfuture,
  862. then the IPvFuture address string is returned,
  863. without any enclosing brackets.
  864. @li If the type is @ref host_type::name,
  865. then the host name string is returned.
  866. Any percent-escapes in the string are
  867. decoded first.
  868. @li If the type is @ref host_type::none,
  869. then an empty string is returned.
  870. @par Example
  871. @code
  872. assert( url_view( "https://[1::6:c0a8:1]/" ).host_address() == "1::6:c0a8:1" );
  873. @endcode
  874. @par Complexity
  875. Linear in `this->host_address().size()`.
  876. @par Exception Safety
  877. Calls to allocate may throw.
  878. @par BNF
  879. @code
  880. host = IP-literal / IPv4address / reg-name
  881. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  882. reg-name = *( unreserved / pct-encoded / "-" / ".")
  883. @endcode
  884. @par Specification
  885. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  886. @param token A string token customization
  887. @return The host address as a string.
  888. */
  889. template<BOOST_URL_STRTOK_TPARAM>
  890. BOOST_URL_STRTOK_RETURN
  891. host_address(
  892. StringToken&& token = {}) const
  893. {
  894. encoding_opts opt;
  895. opt.space_as_plus = false;
  896. return encoded_host_address().decode(
  897. opt, std::forward<StringToken>(token));
  898. }
  899. /** Return the host
  900. The value returned by this function
  901. depends on the type of host returned
  902. from the function @ref host_type.
  903. @li If the type is @ref host_type::ipv4,
  904. then the IPv4 address string is returned.
  905. @li If the type is @ref host_type::ipv6,
  906. then the IPv6 address string is returned,
  907. without any enclosing brackets.
  908. @li If the type is @ref host_type::ipvfuture,
  909. then the IPvFuture address string is returned,
  910. without any enclosing brackets.
  911. @li If the type is @ref host_type::name,
  912. then the host name string is returned.
  913. Any percent-escapes in the string are
  914. decoded first.
  915. @li If the type is @ref host_type::none,
  916. then an empty string is returned.
  917. The returned string may contain
  918. percent escapes.
  919. @par Example
  920. @code
  921. assert( url_view( "https://www%2droot.example.com/" ).encoded_host_address() == "www%2droot.example.com" );
  922. @endcode
  923. @par Complexity
  924. Constant.
  925. @par Exception Safety
  926. Throws nothing.
  927. @par BNF
  928. @code
  929. host = IP-literal / IPv4address / reg-name
  930. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  931. reg-name = *( unreserved / pct-encoded / "-" / ".")
  932. @endcode
  933. @par Specification
  934. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  935. @return The host address as a string.
  936. */
  937. pct_string_view
  938. encoded_host_address() const noexcept;
  939. /** Return the host IPv4 address
  940. If the host type is @ref host_type::ipv4,
  941. this function returns the address as
  942. a value of type @ref ipv4_address.
  943. Otherwise, if the host type is not an IPv4
  944. address, it returns a default-constructed
  945. value which is equal to the unspecified
  946. address "0.0.0.0".
  947. @par Example
  948. @code
  949. assert( url_view( "http://127.0.0.1/index.htm?user=win95" ).host_ipv4_address() == ipv4_address( "127.0.0.1" ) );
  950. @endcode
  951. @par Complexity
  952. Constant.
  953. @par Exception Safety
  954. Throws nothing.
  955. @par BNF
  956. @code
  957. IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  958. dec-octet = DIGIT ; 0-9
  959. / %x31-39 DIGIT ; 10-99
  960. / "1" 2DIGIT ; 100-199
  961. / "2" %x30-34 DIGIT ; 200-249
  962. / "25" %x30-35 ; 250-255
  963. @endcode
  964. @par Specification
  965. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  966. @return The IPv4 address as a value of type @ref ipv4_address.
  967. */
  968. ipv4_address
  969. host_ipv4_address() const noexcept;
  970. /** Return the host IPv6 address
  971. If the host type is @ref host_type::ipv6,
  972. this function returns the address as
  973. a value of type @ref ipv6_address.
  974. Otherwise, if the host type is not an IPv6
  975. address, it returns a default-constructed
  976. value which is equal to the unspecified
  977. address "0:0:0:0:0:0:0:0".
  978. @par Example
  979. @code
  980. assert( url_view( "ftp://[::1]/" ).host_ipv6_address() == ipv6_address( "::1" ) );
  981. @endcode
  982. @par Complexity
  983. Constant.
  984. @par Exception Safety
  985. Throws nothing.
  986. @par BNF
  987. @code
  988. IPv6address = 6( h16 ":" ) ls32
  989. / "::" 5( h16 ":" ) ls32
  990. / [ h16 ] "::" 4( h16 ":" ) ls32
  991. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  992. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  993. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  994. / [ *4( h16 ":" ) h16 ] "::" ls32
  995. / [ *5( h16 ":" ) h16 ] "::" h16
  996. / [ *6( h16 ":" ) h16 ] "::"
  997. ls32 = ( h16 ":" h16 ) / IPv4address
  998. ; least-significant 32 bits of address
  999. h16 = 1*4HEXDIG
  1000. ; 16 bits of address represented in hexadecimal
  1001. @endcode
  1002. @par Specification
  1003. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  1004. @return The IPv6 address as a value of type @ref ipv6_address.
  1005. */
  1006. ipv6_address
  1007. host_ipv6_address() const noexcept;
  1008. /** Return the host IPvFuture address
  1009. If the host type is @ref host_type::ipvfuture,
  1010. this function returns the address as
  1011. a string.
  1012. Otherwise, if the host type is not an
  1013. IPvFuture address, it returns an
  1014. empty string.
  1015. @par Example
  1016. @code
  1017. assert( url_view( "http://[v1fe.d:9]/index.htm" ).host_ipvfuture() == "v1fe.d:9" );
  1018. @endcode
  1019. @par Complexity
  1020. Constant.
  1021. @par Exception Safety
  1022. Throws nothing.
  1023. @par BNF
  1024. @code
  1025. IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  1026. @endcode
  1027. @par Specification
  1028. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  1029. @return The IPvFuture address as a string.
  1030. */
  1031. core::string_view
  1032. host_ipvfuture() const noexcept;
  1033. /** Return the host name
  1034. If the host type is @ref host_type::name,
  1035. this function returns the name as
  1036. a string. Otherwise an empty string is returned.
  1037. Any percent-escapes in the string are
  1038. decoded first.
  1039. @par Example
  1040. @code
  1041. assert( url_view( "https://www%2droot.example.com/" ).host_name() == "www-root.example.com" );
  1042. @endcode
  1043. @par Complexity
  1044. Linear in `this->host_name().size()`.
  1045. @par Exception Safety
  1046. Calls to allocate may throw.
  1047. @par BNF
  1048. @code
  1049. host = IP-literal / IPv4address / reg-name
  1050. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  1051. reg-name = *( unreserved / pct-encoded / "-" / ".")
  1052. @endcode
  1053. @par Specification
  1054. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  1055. @param token A string token customization.
  1056. @return The host name as a string.
  1057. */
  1058. template<BOOST_URL_STRTOK_TPARAM>
  1059. BOOST_URL_STRTOK_RETURN
  1060. host_name(
  1061. StringToken&& token = {}) const
  1062. {
  1063. encoding_opts opt;
  1064. opt.space_as_plus = false;
  1065. return encoded_host_name().decode(
  1066. opt, std::forward<StringToken>(token));
  1067. }
  1068. /** Return the host name
  1069. If the host type is @ref host_type::name,
  1070. this function returns the name as
  1071. a string.
  1072. Otherwise, if the host type is not an
  1073. name, it returns an empty string.
  1074. The returned string may contain
  1075. percent escapes.
  1076. @par Example
  1077. @code
  1078. assert( url_view( "https://www%2droot.example.com/" ).encoded_host_name() == "www%2droot.example.com" );
  1079. @endcode
  1080. @par Complexity
  1081. Constant.
  1082. @par Exception Safety
  1083. Throws nothing.
  1084. @par BNF
  1085. @code
  1086. host = IP-literal / IPv4address / reg-name
  1087. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  1088. reg-name = *( unreserved / pct-encoded / "-" / ".")
  1089. @endcode
  1090. @par Specification
  1091. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  1092. @return The host name as a percent-encoded string.
  1093. */
  1094. pct_string_view
  1095. encoded_host_name() const noexcept;
  1096. /** Return the IPv6 Zone ID
  1097. If the host type is @ref host_type::ipv6,
  1098. this function returns the Zone ID as
  1099. a string. Otherwise an empty string is returned.
  1100. Any percent-escapes in the string are
  1101. decoded first.
  1102. @par Example
  1103. @code
  1104. assert( url_view( "http://[fe80::1%25eth0]/" ).zone_id() == "eth0" );
  1105. @endcode
  1106. @par Complexity
  1107. Linear in `this->encoded_zone_id().size()`.
  1108. @par Exception Safety
  1109. Calls to allocate may throw.
  1110. @par BNF
  1111. @code
  1112. host = IP-literal / IPv4address / reg-name
  1113. IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture ) "]"
  1114. ZoneID = 1*( unreserved / pct-encoded )
  1115. IPv6addrz = IPv6address "%25" ZoneID
  1116. @endcode
  1117. @par Specification
  1118. @li <a href="https://datatracker.ietf.org/doc/html/rfc6874">Representing IPv6 Zone Identifiers in Address Literals and Uniform Resource Identifiers</a>
  1119. @param token A string token customization.
  1120. @return The Zone ID as a string.
  1121. */
  1122. template<BOOST_URL_STRTOK_TPARAM>
  1123. BOOST_URL_STRTOK_RETURN
  1124. zone_id(
  1125. StringToken&& token = {}) const
  1126. {
  1127. encoding_opts opt;
  1128. opt.space_as_plus = false;
  1129. return encoded_zone_id().decode(
  1130. opt, std::forward<StringToken>(token));
  1131. }
  1132. /** Return the IPv6 Zone ID
  1133. If the host type is @ref host_type::ipv6,
  1134. this function returns the Zone ID as
  1135. a string. Otherwise an empty string is returned.
  1136. The returned string may contain
  1137. percent escapes.
  1138. @par Example
  1139. @code
  1140. assert( url_view( "http://[fe80::1%25eth0]/" ).encoded_zone_id() == "eth0" );
  1141. @endcode
  1142. @par Complexity
  1143. Constant.
  1144. @par Exception Safety
  1145. Throws nothing.
  1146. @par BNF
  1147. @code
  1148. host = IP-literal / IPv4address / reg-name
  1149. IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture ) "]"
  1150. ZoneID = 1*( unreserved / pct-encoded )
  1151. IPv6addrz = IPv6address "%25" ZoneID
  1152. @endcode
  1153. @par Specification
  1154. @li <a href="https://datatracker.ietf.org/doc/html/rfc6874">Representing IPv6 Zone Identifiers in Address Literals and Uniform Resource Identifiers</a>
  1155. @return The Zone ID as a percent-encoded string.
  1156. */
  1157. pct_string_view
  1158. encoded_zone_id() const noexcept;
  1159. //--------------------------------------------
  1160. //
  1161. // Port
  1162. //
  1163. //--------------------------------------------
  1164. /** Return true if a port is present
  1165. This function returns true if an
  1166. authority is present and contains a port.
  1167. @par Example
  1168. @code
  1169. assert( url_view( "wss://www.example.com:443" ).has_port() );
  1170. @endcode
  1171. @par Complexity
  1172. Constant.
  1173. @par Exception Safety
  1174. Throws nothing.
  1175. @par BNF
  1176. @code
  1177. authority = [ userinfo "@" ] host [ ":" port ]
  1178. port = *DIGIT
  1179. @endcode
  1180. @par Specification
  1181. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">3.2.3. Port (rfc3986)</a>
  1182. @see
  1183. @ref encoded_host_and_port,
  1184. @ref port,
  1185. @ref port_number.
  1186. @return `true` if a port is present, `false` otherwise.
  1187. */
  1188. bool
  1189. has_port() const noexcept;
  1190. /** Return the port
  1191. If present, this function returns a
  1192. string representing the port (which
  1193. may be empty).
  1194. Otherwise it returns an empty string.
  1195. @par Example
  1196. @code
  1197. assert( url_view( "http://localhost.com:8080" ).port() == "8080" );
  1198. @endcode
  1199. @par Complexity
  1200. Constant.
  1201. @par Exception Safety
  1202. Throws nothing.
  1203. @par BNF
  1204. @code
  1205. port = *DIGIT
  1206. @endcode
  1207. @par Specification
  1208. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">3.2.3. Port (rfc3986)</a>
  1209. @see
  1210. @ref encoded_host_and_port,
  1211. @ref has_port,
  1212. @ref port_number.
  1213. @return The port as a string.
  1214. */
  1215. core::string_view
  1216. port() const noexcept;
  1217. /** Return the port
  1218. If a port is present and the numerical
  1219. value is representable, it is returned
  1220. as an unsigned integer. Otherwise, the
  1221. number zero is returned.
  1222. @par Example
  1223. @code
  1224. assert( url_view( "http://localhost.com:8080" ).port_number() == 8080 );
  1225. @endcode
  1226. @par Complexity
  1227. Constant.
  1228. @par Exception Safety
  1229. Throws nothing.
  1230. @par BNF
  1231. @code
  1232. port = *DIGIT
  1233. @endcode
  1234. @par Specification
  1235. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">3.2.3. Port (rfc3986)</a>
  1236. @see
  1237. @ref encoded_host_and_port,
  1238. @ref has_port,
  1239. @ref port.
  1240. @return The port number as an unsigned integer.
  1241. */
  1242. std::uint16_t
  1243. port_number() const noexcept;
  1244. //--------------------------------------------
  1245. //
  1246. // Path
  1247. //
  1248. //--------------------------------------------
  1249. /** Return true if the path is absolute
  1250. This function returns true if the path
  1251. begins with a forward slash ('/').
  1252. @par Example
  1253. @code
  1254. assert( url_view( "/path/to/file.txt" ).is_path_absolute() );
  1255. @endcode
  1256. @par Complexity
  1257. Constant.
  1258. @par Exception Safety
  1259. Throws nothing.
  1260. @par BNF
  1261. @code
  1262. path = path-abempty ; begins with "/" or is empty
  1263. / path-absolute ; begins with "/" but not "//"
  1264. / path-noscheme ; begins with a non-colon segment
  1265. / path-rootless ; begins with a segment
  1266. / path-empty ; zero characters
  1267. path-abempty = *( "/" segment )
  1268. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1269. path-noscheme = segment-nz-nc *( "/" segment )
  1270. path-rootless = segment-nz *( "/" segment )
  1271. path-empty = 0<pchar>
  1272. @endcode
  1273. @par Specification
  1274. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1275. @see
  1276. @ref encoded_path,
  1277. @ref encoded_segments.
  1278. @ref path,
  1279. @ref segments.
  1280. @return `true` if the path is absolute, `false` otherwise.
  1281. */
  1282. bool
  1283. is_path_absolute() const noexcept
  1284. {
  1285. return
  1286. pi_->len(id_path) > 0 &&
  1287. pi_->cs_[pi_->offset(id_path)] == '/';
  1288. }
  1289. /** Return the path
  1290. This function returns the path as a
  1291. string. The path may be empty.
  1292. Any percent-escapes in the string are
  1293. decoded first.
  1294. @par Example
  1295. @code
  1296. assert( url_view( "file:///Program%20Files/Games/config.ini" ).path() == "/Program Files/Games/config.ini" );
  1297. @endcode
  1298. @par Complexity
  1299. Linear in `this->path().size()`.
  1300. @par Exception Safety
  1301. Calls to allocate may throw.
  1302. @par BNF
  1303. @code
  1304. path = path-abempty ; begins with "/" or is empty
  1305. / path-absolute ; begins with "/" but not "//"
  1306. / path-noscheme ; begins with a non-colon segment
  1307. / path-rootless ; begins with a segment
  1308. / path-empty ; zero characters
  1309. path-abempty = *( "/" segment )
  1310. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1311. path-noscheme = segment-nz-nc *( "/" segment )
  1312. path-rootless = segment-nz *( "/" segment )
  1313. path-empty = 0<pchar>
  1314. @endcode
  1315. @par Specification
  1316. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1317. @see
  1318. @ref is_path_absolute,
  1319. @ref encoded_path,
  1320. @ref encoded_segments.
  1321. @ref segments.
  1322. @param token A string token to use for the result.
  1323. @return The path as a string.
  1324. */
  1325. template<BOOST_URL_STRTOK_TPARAM>
  1326. BOOST_URL_STRTOK_RETURN
  1327. path(
  1328. StringToken&& token = {}) const
  1329. {
  1330. encoding_opts opt;
  1331. opt.space_as_plus = false;
  1332. return encoded_path().decode(
  1333. opt, std::forward<StringToken>(token));
  1334. }
  1335. /** Return the path
  1336. This function returns the path as a
  1337. string. The path may be empty.
  1338. Any percent-escapes in the string are
  1339. decoded first.
  1340. @par Example
  1341. @code
  1342. assert( url_view( "file:///Program%20Files/Games/config.ini" ).encoded_path() == "/Program%20Files/Games/config.ini" );
  1343. @endcode
  1344. @par Complexity
  1345. Constant.
  1346. @par Exception Safety
  1347. Throws nothing.
  1348. @par BNF
  1349. @code
  1350. path = path-abempty ; begins with "/" or is empty
  1351. / path-absolute ; begins with "/" but not "//"
  1352. / path-noscheme ; begins with a non-colon segment
  1353. / path-rootless ; begins with a segment
  1354. / path-empty ; zero characters
  1355. path-abempty = *( "/" segment )
  1356. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1357. path-noscheme = segment-nz-nc *( "/" segment )
  1358. path-rootless = segment-nz *( "/" segment )
  1359. path-empty = 0<pchar>
  1360. @endcode
  1361. @par Specification
  1362. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1363. @see
  1364. @ref is_path_absolute,
  1365. @ref encoded_segments.
  1366. @ref path,
  1367. @ref segments.
  1368. @return The path as a string.
  1369. */
  1370. pct_string_view
  1371. encoded_path() const noexcept;
  1372. /** Return the path as a container of segments
  1373. This function returns a bidirectional
  1374. view of strings over the path.
  1375. The returned view references the same
  1376. underlying character buffer; ownership
  1377. is not transferred.
  1378. Any percent-escapes in strings returned
  1379. when iterating the view are decoded first.
  1380. @par Example
  1381. @code
  1382. segments_view sv = url_view( "/path/to/file.txt" ).segments();
  1383. @endcode
  1384. @par Complexity
  1385. Constant.
  1386. @par Exception Safety
  1387. Throws nothing.
  1388. @par BNF
  1389. @code
  1390. path = [ "/" ] segment *( "/" segment )
  1391. @endcode
  1392. @par Specification
  1393. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1394. @see
  1395. @ref is_path_absolute,
  1396. @ref encoded_path,
  1397. @ref encoded_segments.
  1398. @ref path,
  1399. @ref segments_view.
  1400. @return A bidirectional view of segments.
  1401. */
  1402. segments_view
  1403. segments() const noexcept;
  1404. /** Return the path as a container of segments
  1405. This function returns a bidirectional
  1406. view of strings over the path.
  1407. The returned view references the same
  1408. underlying character buffer; ownership
  1409. is not transferred.
  1410. Strings returned when iterating the
  1411. range may contain percent escapes.
  1412. @par Example
  1413. @code
  1414. segments_encoded_view sv = url_view( "/path/to/file.txt" ).encoded_segments();
  1415. @endcode
  1416. @par Complexity
  1417. Constant.
  1418. @par Exception Safety
  1419. Throws nothing.
  1420. @par BNF
  1421. @code
  1422. path = path-abempty ; begins with "/" or is empty
  1423. / path-absolute ; begins with "/" but not "//"
  1424. / path-noscheme ; begins with a non-colon segment
  1425. / path-rootless ; begins with a segment
  1426. / path-empty ; zero characters
  1427. path-abempty = *( "/" segment )
  1428. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1429. path-noscheme = segment-nz-nc *( "/" segment )
  1430. path-rootless = segment-nz *( "/" segment )
  1431. path-empty = 0<pchar>
  1432. @endcode
  1433. @par Specification
  1434. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1435. @see
  1436. @ref is_path_absolute,
  1437. @ref encoded_path,
  1438. @ref path,
  1439. @ref segments,
  1440. @ref segments_encoded_view.
  1441. @return A bidirectional view of encoded segments.
  1442. */
  1443. segments_encoded_view
  1444. encoded_segments() const noexcept;
  1445. //--------------------------------------------
  1446. //
  1447. // Query
  1448. //
  1449. //--------------------------------------------
  1450. /** Return true if a query is present
  1451. This function returns true if this
  1452. contains a query. An empty query is
  1453. distinct from having no query.
  1454. @par Example
  1455. @code
  1456. assert( url_view( "/sql?id=42&col=name&page-size=20" ).has_query() );
  1457. @endcode
  1458. @par Complexity
  1459. Constant.
  1460. @par Exception Safety
  1461. Throws nothing.
  1462. @par BNF
  1463. @code
  1464. query = *( pchar / "/" / "?" )
  1465. query-param = key [ "=" value ]
  1466. query-params = [ query-param ] *( "&" query-param )
  1467. @endcode
  1468. @par Specification
  1469. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1470. @li <a href="https://en.wikipedia.org/wiki/Query_string">Query string (Wikipedia)</a>
  1471. @see
  1472. @ref encoded_params,
  1473. @ref encoded_query,
  1474. @ref params,
  1475. @ref query.
  1476. @return `true` if a query is present.
  1477. */
  1478. bool
  1479. has_query() const noexcept;
  1480. /** Return the query
  1481. If this contains a query, it is returned
  1482. as a string (which may be empty).
  1483. Otherwise, an empty string is returned.
  1484. Any percent-escapes in the string are
  1485. decoded first.
  1486. <br>
  1487. When plus signs appear in the query
  1488. portion of the url, they are converted
  1489. to spaces automatically upon decoding.
  1490. This behavior can be changed by setting
  1491. decode options.
  1492. @par Example
  1493. @code
  1494. assert( url_view( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).query() == "id=42&name=jane-doe&page size=20" );
  1495. @endcode
  1496. @par Complexity
  1497. Linear in `this->query().size()`.
  1498. @par Exception Safety
  1499. Calls to allocate may throw.
  1500. @par BNF
  1501. @code
  1502. query = *( pchar / "/" / "?" )
  1503. query-param = key [ "=" value ]
  1504. query-params = [ query-param ] *( "&" query-param )
  1505. @endcode
  1506. @par Specification
  1507. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1508. @li <a href="https://en.wikipedia.org/wiki/Query_string">Query string (Wikipedia)</a>
  1509. @see
  1510. @ref encoded_params,
  1511. @ref encoded_query,
  1512. @ref has_query,
  1513. @ref params.
  1514. @param token A token to use for the returned string.
  1515. @return The query as a string.
  1516. */
  1517. template<BOOST_URL_STRTOK_TPARAM>
  1518. BOOST_URL_STRTOK_RETURN
  1519. query(
  1520. StringToken&& token = {}) const
  1521. {
  1522. // When interacting with the query as
  1523. // an intact string, we do not treat
  1524. // the plus sign as an encoded space.
  1525. encoding_opts opt;
  1526. opt.space_as_plus = false;
  1527. return encoded_query().decode(
  1528. opt, std::forward<StringToken>(token));
  1529. }
  1530. /** Return the query
  1531. If this contains a query, it is returned
  1532. as a string (which may be empty).
  1533. Otherwise, an empty string is returned.
  1534. The returned string may contain
  1535. percent escapes.
  1536. @par Example
  1537. @code
  1538. assert( url_view( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).encoded_query() == "id=42&name=jane%2Ddoe&page+size=20" );
  1539. @endcode
  1540. @par Complexity
  1541. Constant.
  1542. @par Exception Safety
  1543. Throws nothing.
  1544. @par BNF
  1545. @code
  1546. query = *( pchar / "/" / "?" )
  1547. query-param = key [ "=" value ]
  1548. query-params = [ query-param ] *( "&" query-param )
  1549. @endcode
  1550. @par Specification
  1551. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1552. @li <a href="https://en.wikipedia.org/wiki/Query_string">Query string (Wikipedia)</a>
  1553. @see
  1554. @ref encoded_params,
  1555. @ref has_query,
  1556. @ref params,
  1557. @ref query.
  1558. @return The query as a string.
  1559. */
  1560. pct_string_view
  1561. encoded_query() const noexcept;
  1562. /** Return the query as a container of parameters
  1563. This function returns a bidirectional
  1564. view of key/value pairs over the query.
  1565. The returned view references the same
  1566. underlying character buffer; ownership
  1567. is not transferred.
  1568. Any percent-escapes in strings returned
  1569. when iterating the view are decoded first.
  1570. @par Example
  1571. @code
  1572. params_view pv = url_view( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).params();
  1573. @endcode
  1574. @par Complexity
  1575. Constant.
  1576. @par Exception Safety
  1577. Throws nothing.
  1578. @par BNF
  1579. @code
  1580. query = *( pchar / "/" / "?" )
  1581. query-param = key [ "=" value ]
  1582. query-params = [ query-param ] *( "&" query-param )
  1583. @endcode
  1584. @par Specification
  1585. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1586. @li <a href="https://en.wikipedia.org/wiki/Query_string">Query string (Wikipedia)</a>
  1587. @see
  1588. @ref encoded_params,
  1589. @ref encoded_query,
  1590. @ref has_query,
  1591. @ref query.
  1592. @return A bidirectional view of key/value pairs.
  1593. */
  1594. params_view
  1595. params() const noexcept;
  1596. params_view
  1597. params(encoding_opts opt) const noexcept;
  1598. /** Return the query as a container of parameters
  1599. This function returns a bidirectional
  1600. view of key/value pairs over the query.
  1601. The returned view references the same
  1602. underlying character buffer; ownership
  1603. is not transferred.
  1604. Strings returned when iterating the
  1605. range may contain percent escapes.
  1606. @par Example
  1607. @code
  1608. params_encoded_view pv = url_view( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).encoded_params();
  1609. @endcode
  1610. @par Complexity
  1611. Constant.
  1612. @par Exception Safety
  1613. Throws nothing.
  1614. @par BNF
  1615. @code
  1616. query = *( pchar / "/" / "?" )
  1617. query-param = key [ "=" value ]
  1618. query-params = [ query-param ] *( "&" query-param )
  1619. @endcode
  1620. @par Specification
  1621. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1622. @li <a href="https://en.wikipedia.org/wiki/Query_string">Query string (Wikipedia)</a>
  1623. @see
  1624. @ref encoded_query,
  1625. @ref has_query,
  1626. @ref params,
  1627. @ref query.
  1628. @return A bidirectional view of key/value pairs.
  1629. */
  1630. params_encoded_view
  1631. encoded_params() const noexcept;
  1632. //--------------------------------------------
  1633. //
  1634. // Fragment
  1635. //
  1636. //--------------------------------------------
  1637. /** Return true if a fragment is present
  1638. This function returns true if the url
  1639. contains a fragment.
  1640. An empty fragment is distinct from
  1641. no fragment.
  1642. @par Example
  1643. @code
  1644. assert( url_view( "http://www.example.com/index.htm#anchor" ).has_fragment() );
  1645. @endcode
  1646. @par Complexity
  1647. Constant.
  1648. @par Exception Safety
  1649. Throws nothing.
  1650. @par BNF
  1651. @code
  1652. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  1653. relative-ref = relative-part [ "?" query ] [ "#" fragment ]
  1654. @endcode
  1655. @par Specification
  1656. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5">3.5. Fragment (rfc3986)</a>
  1657. @see
  1658. @ref encoded_fragment,
  1659. @ref fragment.
  1660. @return `true` if the url contains a fragment.
  1661. */
  1662. bool
  1663. has_fragment() const noexcept;
  1664. /** Return the fragment
  1665. This function calculates the fragment
  1666. of the url, with percent escapes decoded
  1667. and without the leading pound sign ('#')
  1668. whose presence indicates that the url
  1669. contains a fragment.
  1670. <br>
  1671. This function accepts an optional
  1672. <em>StringToken</em> parameter which
  1673. controls the return type and behavior
  1674. of the function:
  1675. @li When called with no arguments,
  1676. the return type of the function is
  1677. `std::string`. Otherwise
  1678. @li When called with a string token,
  1679. the behavior and return type of the
  1680. function depends on the type of string
  1681. token being passed.
  1682. @par Example
  1683. @code
  1684. assert( url_view( "http://www.example.com/index.htm#a%2D1" ).fragment() == "a-1" );
  1685. @endcode
  1686. @par Complexity
  1687. Linear in `this->fragment().size()`.
  1688. @par Exception Safety
  1689. Calls to allocate may throw.
  1690. String tokens may throw exceptions.
  1691. @param token An optional string token to
  1692. use. If this parameter is omitted, the
  1693. function returns a new `std::string`.
  1694. @return The fragment portion of the url.
  1695. @par BNF
  1696. @code
  1697. fragment = *( pchar / "/" / "?" )
  1698. fragment-part = [ "#" fragment ]
  1699. @endcode
  1700. @par Specification
  1701. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5">3.5. Fragment (rfc3986)</a>
  1702. @see
  1703. @ref encoded_fragment,
  1704. @ref has_fragment.
  1705. */
  1706. template<BOOST_URL_STRTOK_TPARAM>
  1707. BOOST_URL_STRTOK_RETURN
  1708. fragment(
  1709. StringToken&& token = {}) const
  1710. {
  1711. encoding_opts opt;
  1712. opt.space_as_plus = false;
  1713. return encoded_fragment().decode(
  1714. opt, std::forward<StringToken>(token));
  1715. }
  1716. /** Return the fragment
  1717. This function returns the fragment as a
  1718. string with percent-escapes.
  1719. Ownership is not transferred; the
  1720. string returned references the underlying
  1721. character buffer, which must remain valid
  1722. or else undefined behavior occurs.
  1723. @par Example
  1724. @code
  1725. assert( url_view( "http://www.example.com/index.htm#a%2D1" ).encoded_fragment() == "a%2D1" );
  1726. @endcode
  1727. @par Complexity
  1728. Constant.
  1729. @par Exception Safety
  1730. Throws nothing.
  1731. @par BNF
  1732. @code
  1733. fragment = *( pchar / "/" / "?" )
  1734. pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
  1735. @endcode
  1736. @par Specification
  1737. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5">3.5. Fragment (rfc3986)</a>
  1738. @see
  1739. @ref fragment,
  1740. @ref has_fragment.
  1741. @return The fragment portion of the url.
  1742. */
  1743. pct_string_view
  1744. encoded_fragment() const noexcept;
  1745. //--------------------------------------------
  1746. //
  1747. // Compound Fields
  1748. //
  1749. //--------------------------------------------
  1750. /** Return the host and port
  1751. If an authority is present, this
  1752. function returns the host and optional
  1753. port as a string, which may be empty.
  1754. Otherwise it returns an empty string.
  1755. The returned string may contain
  1756. percent escapes.
  1757. @par Example
  1758. @code
  1759. assert( url_view( "http://www.example.com:8080/index.htm" ).encoded_host_and_port() == "www.example.com:8080" );
  1760. @endcode
  1761. @par Complexity
  1762. Constant.
  1763. @par Exception Safety
  1764. Throws nothing.
  1765. @par BNF
  1766. @code
  1767. authority = [ userinfo "@" ] host [ ":" port ]
  1768. @endcode
  1769. @par Specification
  1770. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">3.2.2. Host (rfc3986)</a>
  1771. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">3.2.3. Port (rfc3986)</a>
  1772. @see
  1773. @ref has_port,
  1774. @ref port,
  1775. @ref port_number.
  1776. @return The host and port portion of the url.
  1777. */
  1778. pct_string_view
  1779. encoded_host_and_port() const noexcept;
  1780. /** Return the origin
  1781. If an authority is present, this
  1782. function returns the scheme and
  1783. authority portion of the url.
  1784. Otherwise, an empty string is
  1785. returned.
  1786. The returned string may contain
  1787. percent escapes.
  1788. @par Example
  1789. @code
  1790. assert( url_view( "http://www.example.com:8080/index.htm?text=none#h1" ).encoded_origin() == "http://www.example.com:8080" );
  1791. @endcode
  1792. @par Complexity
  1793. Constant.
  1794. @par Exception Safety
  1795. Throws nothing.
  1796. @see
  1797. @ref encoded_resource,
  1798. @ref encoded_target.
  1799. @return The origin portion of the url.
  1800. */
  1801. pct_string_view
  1802. encoded_origin() const noexcept;
  1803. /** Return the resource
  1804. This function returns the resource, which
  1805. is the portion of the url that includes
  1806. only the path, query, and fragment.
  1807. The returned string may contain
  1808. percent escapes.
  1809. @par Example
  1810. @code
  1811. assert( url_view( "http://www.example.com/index.html?query#frag" ).encoded_resource() == "/index.html?query#frag" );
  1812. @endcode
  1813. @par Complexity
  1814. Constant.
  1815. @par Exception Safety
  1816. Throws nothing.
  1817. @par Specification
  1818. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1819. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1820. @see
  1821. @ref encoded_origin,
  1822. @ref encoded_target.
  1823. @return The resource portion of the url.
  1824. */
  1825. pct_string_view
  1826. encoded_resource() const noexcept;
  1827. /** Return the target
  1828. This function returns the target, which
  1829. is the portion of the url that includes
  1830. only the path and query.
  1831. The returned string may contain
  1832. percent escapes.
  1833. @par Example
  1834. @code
  1835. assert( url_view( "http://www.example.com/index.html?query#frag" ).encoded_target() == "/index.html?query" );
  1836. @endcode
  1837. @par Complexity
  1838. Constant.
  1839. @par Exception Safety
  1840. Throws nothing.
  1841. @par Specification
  1842. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3">3.3. Path (rfc3986)</a>
  1843. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4">3.4. Query (rfc3986)</a>
  1844. @see
  1845. @ref encoded_origin,
  1846. @ref encoded_resource.
  1847. @return The target portion of the url.
  1848. */
  1849. pct_string_view
  1850. encoded_target() const noexcept;
  1851. //--------------------------------------------
  1852. //
  1853. // Comparison
  1854. //
  1855. //--------------------------------------------
  1856. /** Return the result of comparing this with another url
  1857. This function compares two URLs
  1858. according to Syntax-Based comparison
  1859. algorithm.
  1860. @par Complexity
  1861. Linear in `min( u0.size(), u1.size() )`
  1862. @par Exception Safety
  1863. Throws nothing.
  1864. @par Specification
  1865. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  1866. @param other The url to compare
  1867. @return -1 if `*this < other`, 0 if `this == other`, and 1 if `this > other`.
  1868. */
  1869. int
  1870. compare(url_view_base const& other) const noexcept;
  1871. /** Return the result of comparing two URLs
  1872. The URLs are compared component by
  1873. component as if they were first
  1874. normalized.
  1875. @par Example
  1876. @code
  1877. url_view u0( "http://www.a.com/index.htm" );
  1878. url_view u1( "http://www.a.com/index.htm" );
  1879. assert( u0 == u1 );
  1880. @endcode
  1881. @par Effects
  1882. @code
  1883. url a(u0);
  1884. a.normalize();
  1885. url b(u1);
  1886. b.normalize();
  1887. return a.buffer() == b.buffer();
  1888. @endcode
  1889. @par Complexity
  1890. Linear in `min( u0.size(), u1.size() )`
  1891. @par Exception Safety
  1892. Throws nothing
  1893. @param u0 The first url to compare
  1894. @param u1 The second url to compare
  1895. @return `true` if `u0 == u1`
  1896. @par Specification
  1897. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  1898. */
  1899. friend
  1900. bool
  1901. operator==(
  1902. url_view_base const& u0,
  1903. url_view_base const& u1) noexcept
  1904. {
  1905. return u0.compare(u1) == 0;
  1906. }
  1907. /** Return the result of comparing two URLs
  1908. The URLs are compared component by
  1909. component as if they were first
  1910. normalized.
  1911. @par Example
  1912. @code
  1913. url_view u0( "http://www.a.com/index.htm" );
  1914. url_view u1( "http://www.b.com/index.htm" );
  1915. assert( u0 != u1 );
  1916. @endcode
  1917. @par Effects
  1918. @code
  1919. url a(u0);
  1920. a.normalize();
  1921. url b(u1);
  1922. b.normalize();
  1923. return a.buffer() != b.buffer();
  1924. @endcode
  1925. @par Complexity
  1926. Linear in `min( u0.size(), u1.size() )`
  1927. @par Exception Safety
  1928. Throws nothing
  1929. @param u0 The first url to compare
  1930. @param u1 The second url to compare
  1931. @return `true` if `u0 != u1`
  1932. @par Specification
  1933. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  1934. */
  1935. friend
  1936. bool
  1937. operator!=(
  1938. url_view_base const& u0,
  1939. url_view_base const& u1) noexcept
  1940. {
  1941. return ! (u0 == u1);
  1942. }
  1943. /** Return the result of comparing two URLs
  1944. The URLs are compared component by
  1945. component as if they were first
  1946. normalized.
  1947. @par Example
  1948. @code
  1949. url_view u0( "http://www.a.com/index.htm" );
  1950. url_view u1( "http://www.b.com/index.htm" );
  1951. assert( u0 < u1 );
  1952. @endcode
  1953. @par Effects
  1954. @code
  1955. url a(u0);
  1956. a.normalize();
  1957. url b(u1);
  1958. b.normalize();
  1959. return a.buffer() < b.buffer();
  1960. @endcode
  1961. @par Complexity
  1962. Linear in `min( u0.size(), u1.size() )`
  1963. @par Exception Safety
  1964. Throws nothing
  1965. @param u0 The first url to compare
  1966. @param u1 The second url to compare
  1967. @return `true` if `u0 < u1`
  1968. @par Specification
  1969. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  1970. */
  1971. friend
  1972. bool
  1973. operator<(
  1974. url_view_base const& u0,
  1975. url_view_base const& u1) noexcept
  1976. {
  1977. return u0.compare(u1) < 0;
  1978. }
  1979. /** Return the result of comparing two URLs
  1980. The URLs are compared component by
  1981. component as if they were first
  1982. normalized.
  1983. @par Example
  1984. @code
  1985. url_view u0( "http://www.b.com/index.htm" );
  1986. url_view u1( "http://www.b.com/index.htm" );
  1987. assert( u0 <= u1 );
  1988. @endcode
  1989. @par Effects
  1990. @code
  1991. url a(u0);
  1992. a.normalize();
  1993. url b(u1);
  1994. b.normalize();
  1995. return a.buffer() <= b.buffer();
  1996. @endcode
  1997. @par Complexity
  1998. Linear in `min( u0.size(), u1.size() )`
  1999. @par Exception Safety
  2000. Throws nothing
  2001. @param u0 The first url to compare
  2002. @param u1 The second url to compare
  2003. @return `true` if `u0 <= u1`
  2004. @par Specification
  2005. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2006. */
  2007. friend
  2008. bool
  2009. operator<=(
  2010. url_view_base const& u0,
  2011. url_view_base const& u1) noexcept
  2012. {
  2013. return u0.compare(u1) <= 0;
  2014. }
  2015. /** Return the result of comparing two URLs
  2016. The URLs are compared component by
  2017. component as if they were first
  2018. normalized.
  2019. @par Example
  2020. @code
  2021. url_view u0( "http://www.b.com/index.htm" );
  2022. url_view u1( "http://www.a.com/index.htm" );
  2023. assert( u0 > u1 );
  2024. @endcode
  2025. @par Effects
  2026. @code
  2027. url a(u0);
  2028. a.normalize();
  2029. url b(u1);
  2030. b.normalize();
  2031. return a.buffer() > b.buffer();
  2032. @endcode
  2033. @par Complexity
  2034. Linear in `min( u0.size(), u1.size() )`
  2035. @par Exception Safety
  2036. Throws nothing
  2037. @param u0 The first url to compare
  2038. @param u1 The second url to compare
  2039. @return `true` if `u0 > u1`
  2040. @par Specification
  2041. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2042. */
  2043. friend
  2044. bool
  2045. operator>(
  2046. url_view_base const& u0,
  2047. url_view_base const& u1) noexcept
  2048. {
  2049. return u0.compare(u1) > 0;
  2050. }
  2051. /** Return the result of comparing two URLs
  2052. The URLs are compared component by
  2053. component as if they were first
  2054. normalized.
  2055. @par Example
  2056. @code
  2057. url_view u0( "http://www.a.com/index.htm" );
  2058. url_view u1( "http://www.a.com/index.htm" );
  2059. assert( u0 >= u1 );
  2060. @endcode
  2061. @par Effects
  2062. @code
  2063. url a(u0);
  2064. a.normalize();
  2065. url b(u1);
  2066. b.normalize();
  2067. return a.buffer() >= b.buffer();
  2068. @endcode
  2069. @par Complexity
  2070. Linear in `min( u0.size(), u1.size() )`
  2071. @par Exception Safety
  2072. Throws nothing
  2073. @param u0 The first url to compare
  2074. @param u1 The second url to compare
  2075. @return `true` if `u0 >= u1`
  2076. @par Specification
  2077. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2">6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2078. */
  2079. friend
  2080. bool
  2081. operator>=(
  2082. url_view_base const& u0,
  2083. url_view_base const& u1) noexcept
  2084. {
  2085. return u0.compare(u1) >= 0;
  2086. }
  2087. /** Format the url to the output stream
  2088. This function serializes the url to
  2089. the specified output stream. Any
  2090. percent-escapes are emitted as-is;
  2091. no decoding is performed.
  2092. @par Example
  2093. @code
  2094. url_view u( "http://www.example.com/index.htm" );
  2095. std::stringstream ss;
  2096. ss << u;
  2097. assert( ss.str() == "http://www.example.com/index.htm" );
  2098. @endcode
  2099. @par Effects
  2100. @code
  2101. return os << u.buffer();
  2102. @endcode
  2103. @par Complexity
  2104. Linear in `u.buffer().size()`
  2105. @par Exception Safety
  2106. Basic guarantee.
  2107. @return A reference to the output stream, for chaining
  2108. @param os The output stream to write to.
  2109. @param u The url to write.
  2110. */
  2111. friend
  2112. std::ostream&
  2113. operator<<(
  2114. std::ostream& os,
  2115. url_view_base const& u)
  2116. {
  2117. return os << u.buffer();
  2118. }
  2119. private:
  2120. //--------------------------------------------
  2121. //
  2122. // implementation
  2123. //
  2124. //--------------------------------------------
  2125. static
  2126. int
  2127. segments_compare(
  2128. segments_encoded_view seg0,
  2129. segments_encoded_view seg1) noexcept;
  2130. };
  2131. //------------------------------------------------
  2132. /** Format the url to the output stream
  2133. This function serializes the url to
  2134. the specified output stream. Any
  2135. percent-escapes are emitted as-is;
  2136. no decoding is performed.
  2137. @par Example
  2138. @code
  2139. url_view u( "http://www.example.com/index.htm" );
  2140. std::stringstream ss;
  2141. ss << u;
  2142. assert( ss.str() == "http://www.example.com/index.htm" );
  2143. @endcode
  2144. @par Effects
  2145. @code
  2146. return os << u.buffer();
  2147. @endcode
  2148. @par Complexity
  2149. Linear in `u.buffer().size()`
  2150. @par Exception Safety
  2151. Basic guarantee.
  2152. @return A reference to the output stream, for chaining
  2153. @param os The output stream to write to.
  2154. @param u The url to write.
  2155. */
  2156. std::ostream&
  2157. operator<<(
  2158. std::ostream& os,
  2159. url_view_base const& u);
  2160. } // urls
  2161. } // boost
  2162. #endif