url_base.hpp 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952
  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_BASE_HPP
  11. #define BOOST_URL_URL_BASE_HPP
  12. #include <boost/url/detail/config.hpp>
  13. #include <boost/url/ipv4_address.hpp>
  14. #include <boost/url/ipv6_address.hpp>
  15. #include <boost/url/params_encoded_ref.hpp>
  16. #include <boost/url/params_ref.hpp>
  17. #include <boost/url/pct_string_view.hpp>
  18. #include <boost/url/scheme.hpp>
  19. #include <boost/url/segments_encoded_ref.hpp>
  20. #include <boost/url/segments_ref.hpp>
  21. #include <boost/url/url_view_base.hpp>
  22. #include <cstdint>
  23. #include <initializer_list>
  24. #include <memory>
  25. #include <string>
  26. #include <utility>
  27. namespace boost {
  28. namespace urls {
  29. #ifndef BOOST_URL_DOCS
  30. namespace detail {
  31. struct any_params_iter;
  32. struct any_segments_iter;
  33. struct params_iter_impl;
  34. struct segments_iter_impl;
  35. struct pattern;
  36. }
  37. #endif
  38. /** Common functionality for containers
  39. This base class is used by the library
  40. to provide common member functions for
  41. containers. This cannot be instantiated
  42. directly; Instead, use one of the
  43. containers or functions:
  44. @par Containers
  45. @li @ref url
  46. @li @ref url_view
  47. @li @ref static_url
  48. @par Functions
  49. @li @ref parse_absolute_uri
  50. @li @ref parse_origin_form
  51. @li @ref parse_relative_ref
  52. @li @ref parse_uri
  53. @li @ref parse_uri_reference
  54. */
  55. class BOOST_SYMBOL_VISIBLE
  56. url_base
  57. : public url_view_base
  58. {
  59. char* s_ = nullptr;
  60. std::size_t cap_ = 0;
  61. friend class url;
  62. friend class static_url_base;
  63. friend class params_ref;
  64. friend class segments_ref;
  65. friend class segments_encoded_ref;
  66. friend class params_encoded_ref;
  67. friend struct detail::pattern;
  68. struct op_t
  69. {
  70. ~op_t();
  71. op_t(url_base&,
  72. string_view* = nullptr,
  73. string_view* = nullptr) noexcept;
  74. void move(char*, char const*,
  75. std::size_t) noexcept;
  76. url_base& u;
  77. string_view* s0 = nullptr;
  78. string_view* s1 = nullptr;
  79. char* old = nullptr;
  80. };
  81. virtual ~url_base() noexcept = default;
  82. url_base() noexcept = default;
  83. url_base(detail::url_impl const&) noexcept;
  84. explicit url_base(string_view);
  85. BOOST_URL_DECL void reserve_impl(std::size_t n);
  86. BOOST_URL_DECL void copy(url_view_base const&);
  87. BOOST_URL_DECL virtual void clear_impl() noexcept = 0;
  88. BOOST_URL_DECL virtual void reserve_impl(
  89. std::size_t, op_t&) = 0;
  90. BOOST_URL_DECL virtual void cleanup(op_t&) = 0;
  91. public:
  92. //--------------------------------------------
  93. //
  94. // Observers
  95. //
  96. //--------------------------------------------
  97. /** Return the url as a null-terminated string
  98. This function returns a pointer to a null
  99. terminated string representing the url,
  100. which may contain percent escapes.
  101. @par Example
  102. @code
  103. assert( std::strlen( url( "http://www.example.com" ).c_str() ) == 22 );
  104. @endcode
  105. @par Complexity
  106. Constant.
  107. @par Exception Safety
  108. Throws nothing.
  109. */
  110. char const*
  111. c_str() const noexcept
  112. {
  113. return pi_->cs_;
  114. }
  115. /** Return the number of characters that can be stored without reallocating
  116. This does not include the null terminator,
  117. which is always present.
  118. @par Complexity
  119. Constant.
  120. @par Exception Safety
  121. Throws nothing.
  122. */
  123. std::size_t
  124. capacity() const noexcept
  125. {
  126. return cap_;
  127. }
  128. /** Clear the contents while preserving the capacity
  129. @par Postconditions
  130. @code
  131. this->empty() == true
  132. @endcode
  133. @par Complexity
  134. Constant.
  135. @par Exception Safety
  136. No-throw guarantee.
  137. */
  138. void
  139. clear() noexcept
  140. {
  141. this->clear_impl();
  142. }
  143. /** Adjust the capacity without changing the size
  144. This function adjusts the capacity
  145. of the container in characters, without
  146. affecting the current contents. Has
  147. no effect if `n <= this->capacity()`.
  148. @par Exception Safety
  149. Strong guarantee.
  150. Calls to allocate may throw.
  151. @throw bad_alloc Allocation failure
  152. @param n The capacity in characters,
  153. excluding any null terminator.
  154. */
  155. void
  156. reserve(std::size_t n)
  157. {
  158. reserve_impl(n);
  159. }
  160. //--------------------------------------------
  161. //
  162. // Fluent API
  163. //
  164. //--------------------------------------------
  165. //
  166. // Scheme
  167. //
  168. //--------------------------------------------
  169. /** Set the scheme
  170. The scheme is set to the specified
  171. string, which must contain a valid
  172. scheme without any trailing colon
  173. (':').
  174. Note that schemes are case-insensitive,
  175. and the canonical form is lowercased.
  176. @par Example
  177. @code
  178. assert( url( "http://www.example.com" ).set_scheme( "https" ).scheme_id() == scheme::https );
  179. @endcode
  180. @par Complexity
  181. Linear in `this->size() + s.size()`.
  182. @par Exception Safety
  183. Strong guarantee.
  184. Calls to allocate may throw.
  185. Exceptions thrown on invalid input.
  186. @throw system_error
  187. `s` contains an invalid scheme.
  188. @param s The scheme to set.
  189. @par BNF
  190. @code
  191. scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
  192. @endcode
  193. @par Specification
  194. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">
  195. 3.1. Scheme (rfc3986)</a>
  196. @see
  197. @ref remove_scheme.
  198. */
  199. BOOST_URL_DECL
  200. url_base&
  201. set_scheme(string_view s);
  202. /** Set the scheme
  203. This function sets the scheme to the specified
  204. known @ref urls::scheme id, which may not be
  205. @ref scheme::unknown or else an exception is
  206. thrown. If the id is @ref scheme::none, this
  207. function behaves as if @ref remove_scheme
  208. were called.
  209. @par Example
  210. @code
  211. assert( url( "http://example.com/echo.cgi" ).set_scheme_id( scheme::wss ).buffer() == "wss://example.com/echo.cgi" );
  212. @endcode
  213. @par Complexity
  214. Linear in `this->size()`.
  215. @par Exception Safety
  216. Strong guarantee.
  217. Calls to allocate may throw.
  218. Exceptions thrown on invalid input.
  219. @throw system_error
  220. The scheme is invalid.
  221. @param id The scheme to set.
  222. @par Specification
  223. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">
  224. 3.1. Scheme (rfc3986)</a>
  225. */
  226. BOOST_URL_DECL
  227. url_base&
  228. #ifndef BOOST_URL_DOCS
  229. set_scheme_id(urls::scheme id);
  230. #else
  231. set_scheme_id(scheme id);
  232. #endif
  233. /** Remove the scheme
  234. This function removes the scheme if it
  235. is present.
  236. @par Example
  237. @code
  238. assert( url("http://www.example.com/index.htm" ).remove_scheme().buffer() == "//www.example.com/index.htm" );
  239. @endcode
  240. @par Postconditions
  241. @code
  242. this->has_scheme() == false && this->scheme_id() == scheme::none
  243. @endcode
  244. @par Complexity
  245. Linear in `this->size()`.
  246. @par Exception Safety
  247. Throws nothing.
  248. @par BNF
  249. @code
  250. URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
  251. @endcode
  252. @par Specification
  253. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.1">
  254. 3.1. Scheme (rfc3986)</a>
  255. @see
  256. @ref set_scheme.
  257. */
  258. BOOST_URL_DECL
  259. url_base&
  260. remove_scheme();
  261. //--------------------------------------------
  262. //
  263. // Authority
  264. //
  265. //--------------------------------------------
  266. /** Set the authority
  267. This function sets the authority
  268. to the specified string.
  269. The string may contain percent-escapes.
  270. @par Example
  271. @code
  272. assert( url().set_encoded_authority( "My%20Computer" ).has_authority() );
  273. @endcode
  274. @par Exception Safety
  275. Strong guarantee.
  276. Calls to allocate may throw.
  277. Exceptions thrown on invalid input.
  278. @throw system_eror
  279. The string contains an invalid percent-encoding.
  280. @param s The authority string to set.
  281. @par BNF
  282. @code
  283. authority = [ userinfo "@" ] host [ ":" port ]
  284. userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  285. host = IP-literal / IPv4address / reg-name
  286. port = *DIGIT
  287. @endcode
  288. @par Specification
  289. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2">
  290. 3.2. Authority (rfc3986)</a>
  291. @see
  292. @ref remove_authority.
  293. */
  294. BOOST_URL_DECL
  295. url_base&
  296. set_encoded_authority(
  297. pct_string_view s);
  298. /** Remove the authority
  299. This function removes the authority,
  300. which includes the userinfo, host, and
  301. a port if present.
  302. @par Example
  303. @code
  304. assert( url( "http://example.com/echo.cgi" ).remove_authority().buffer() == "http:/echo.cgi" );
  305. @endcode
  306. @par Postconditions
  307. @code
  308. this->has_authority() == false && this->has_userinfo() == false && this->has_port() == false
  309. @endcode
  310. @par Complexity
  311. Linear in `this->size()`.
  312. @par Exception Safety
  313. Throws nothing.
  314. @par BNF
  315. @code
  316. authority = [ userinfo "@" ] host [ ":" port ]
  317. userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
  318. host = IP-literal / IPv4address / reg-name
  319. port = *DIGIT
  320. @endcode
  321. @par Specification
  322. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2">
  323. 3.2. Authority (rfc3986)</a>
  324. @see
  325. @ref set_encoded_authority.
  326. */
  327. BOOST_URL_DECL
  328. url_base&
  329. remove_authority();
  330. //--------------------------------------------
  331. //
  332. // Userinfo
  333. //
  334. //--------------------------------------------
  335. /** Set the userinfo
  336. The userinfo is set to the given string,
  337. which may contain percent-escapes.
  338. Any special or reserved characters in the
  339. string are automatically percent-encoded.
  340. The effects on the user and password
  341. depend on the presence of a colon (':')
  342. in the string:
  343. @li If an unescaped colon exists, the
  344. characters up to the colon become
  345. the user and the rest of the characters
  346. after the colon become the password.
  347. In this case @ref has_password returns
  348. true. Otherwise,
  349. @li If there is no colon, the user is
  350. set to the string. The function
  351. @ref has_password returns false.
  352. @note
  353. The interpretation of the userinfo as
  354. individual user and password components
  355. is scheme-dependent. Transmitting
  356. passwords in URLs is deprecated.
  357. @par Example
  358. @code
  359. assert( url( "http://example.com" ).set_userinfo( "user:pass" ).encoded_user() == "user" );
  360. @endcode
  361. @par Complexity
  362. Linear in `this->size() + s.size()`.
  363. @par Exception Safety
  364. Strong guarantee.
  365. Calls to allocate may throw.
  366. @param s The string to set.
  367. @par BNF
  368. @code
  369. userinfo = [ [ user ] [ ':' password ] ]
  370. user = *( unreserved / pct-encoded / sub-delims )
  371. password = *( unreserved / pct-encoded / sub-delims / ":" )
  372. @endcode
  373. @par Specification
  374. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  375. 3.2.1. User Information (rfc3986)</a>
  376. @see
  377. @ref remove_userinfo,
  378. @ref set_encoded_userinfo.
  379. */
  380. BOOST_URL_DECL
  381. url_base&
  382. set_userinfo(
  383. string_view s);
  384. /** Set the userinfo.
  385. The userinfo is set to the given string,
  386. which may contain percent-escapes.
  387. Escapes in the string are preserved,
  388. and reserved characters in the string
  389. are percent-escaped in the result.
  390. The effects on the user and password
  391. depend on the presence of a colon (':')
  392. in the string:
  393. @li If an unescaped colon exists, the
  394. characters up to the colon become
  395. the user and the rest of the characters
  396. after the colon become the password.
  397. In this case @ref has_password returns
  398. true. Otherwise,
  399. @li If there is no colon, the user is
  400. set to the string. The function
  401. @ref has_password returns false.
  402. @note
  403. The interpretation of the userinfo as
  404. individual user and password components
  405. is scheme-dependent. Transmitting
  406. passwords in URLs is deprecated.
  407. @par Example
  408. @code
  409. assert( url( "http://example.com" ).set_encoded_userinfo( "john%20doe" ).user() == "john doe" );
  410. @endcode
  411. @par Complexity
  412. Linear in `this->size() + s.size()`.
  413. @par Exception Safety
  414. Strong guarantee.
  415. Calls to allocate may throw.
  416. Exceptions thrown on invalid input.
  417. @throw system_error
  418. `s` contains an invalid percent-encoding.
  419. @param s The string to set.
  420. @par BNF
  421. @code
  422. userinfo = [ [ user ] [ ':' password ] ]
  423. user = *( unreserved / pct-encoded / sub-delims )
  424. password = *( unreserved / pct-encoded / sub-delims / ":" )
  425. @endcode
  426. @par Specification
  427. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  428. 3.2.1. User Information (rfc3986)</a>
  429. @see
  430. @ref remove_userinfo,
  431. @ref set_userinfo.
  432. */
  433. BOOST_URL_DECL
  434. url_base&
  435. set_encoded_userinfo(
  436. pct_string_view s);
  437. /** Remove the userinfo
  438. This function removes the userinfo if
  439. present, without removing any authority.
  440. @par Example
  441. @code
  442. assert( url( "http://user@example.com" ).remove_userinfo().has_userinfo() == false );
  443. @endcode
  444. @par Postconditions
  445. @code
  446. this->has_userinfo() == false && this->encoded_userinfo().empty == true
  447. @endcode
  448. @par Complexity
  449. Linear in `this->size()`.
  450. @par Exception Safety
  451. Throws nothing.
  452. @par BNF
  453. @code
  454. userinfo = [ [ user ] [ ':' password ] ]
  455. user = *( unreserved / pct-encoded / sub-delims )
  456. password = *( unreserved / pct-encoded / sub-delims / ":" )
  457. @endcode
  458. @par Specification
  459. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  460. 3.2.1. User Information (rfc3986)</a>
  461. @see
  462. @ref set_encoded_userinfo,
  463. @ref set_userinfo.
  464. */
  465. BOOST_URL_DECL
  466. url_base&
  467. remove_userinfo() noexcept;
  468. //--------------------------------------------
  469. /** Set the user
  470. This function sets the user part of the
  471. userinfo to the string.
  472. Any special or reserved characters in the
  473. string are automatically percent-encoded.
  474. @par Example
  475. @code
  476. assert( url().set_user("john doe").encoded_userinfo() == "john%20doe" );
  477. @endcode
  478. @par Postconditions
  479. @code
  480. this->has_authority() == true && this->has_userinfo() == true
  481. @endcode
  482. @par Complexity
  483. Linear in `this->size() + s.size()`.
  484. @par Exception Safety
  485. Strong guarantee.
  486. Calls to allocate may throw.
  487. @param s The string to set.
  488. @par BNF
  489. @code
  490. userinfo = [ [ user ] [ ':' password ] ]
  491. user = *( unreserved / pct-encoded / sub-delims )
  492. password = *( unreserved / pct-encoded / sub-delims / ":" )
  493. @endcode
  494. @par Specification
  495. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  496. 3.2.1. User Information (rfc3986)</a>
  497. @see
  498. @ref remove_password,
  499. @ref set_encoded_password,
  500. @ref set_encoded_user,
  501. @ref set_password.
  502. */
  503. BOOST_URL_DECL
  504. url_base&
  505. set_user(
  506. string_view s);
  507. /** Set the user
  508. This function sets the user part of the
  509. userinfo the the string, which may
  510. contain percent-escapes.
  511. Escapes in the string are preserved,
  512. and reserved characters in the string
  513. are percent-escaped in the result.
  514. @par Example
  515. @code
  516. assert( url().set_encoded_user("john%20doe").userinfo() == "john doe" );
  517. @endcode
  518. @par Postconditions
  519. @code
  520. this->has_authority() == true && this->has_userinfo() == true
  521. @endcode
  522. @par Complexity
  523. Linear in `this->size() + s.size()`.
  524. @par Exception Safety
  525. Strong guarantee.
  526. Calls to allocate may throw.
  527. @throw system_error
  528. `s` contains an invalid percent-encoding.
  529. @param s The string to set.
  530. @par BNF
  531. @code
  532. userinfo = [ [ user ] [ ':' password ] ]
  533. user = *( unreserved / pct-encoded / sub-delims )
  534. password = *( unreserved / pct-encoded / sub-delims / ":" )
  535. @endcode
  536. @par Specification
  537. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  538. 3.2.1. User Information (rfc3986)</a>
  539. @see
  540. @ref remove_password,
  541. @ref set_encoded_password,
  542. @ref set_password,
  543. @ref set_user.
  544. */
  545. BOOST_URL_DECL
  546. url_base&
  547. set_encoded_user(
  548. pct_string_view s);
  549. /** Set the password.
  550. This function sets the password in
  551. the userinfo to the string.
  552. Reserved characters in the string are
  553. percent-escaped in the result.
  554. @note
  555. The interpretation of the userinfo as
  556. individual user and password components
  557. is scheme-dependent. Transmitting
  558. passwords in URLs is deprecated.
  559. @par Example
  560. @code
  561. assert( url("http://user@example.com").set_password( "pass" ).encoded_userinfo() == "user:pass" );
  562. @endcode
  563. @par Postconditions
  564. @code
  565. this->has_password() == true && this->password() == s
  566. @endcode
  567. @par Exception Safety
  568. Strong guarantee.
  569. Calls to allocate may throw.
  570. @param s The string to set. This string may
  571. contain any characters, including nulls.
  572. @par BNF
  573. @code
  574. userinfo = [ [ user ] [ ':' password ] ]
  575. user = *( unreserved / pct-encoded / sub-delims )
  576. password = *( unreserved / pct-encoded / sub-delims / ":" )
  577. @endcode
  578. @par Specification
  579. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  580. 3.2.1. User Information (rfc3986)</a>
  581. @see
  582. @ref remove_password,
  583. @ref set_encoded_password,
  584. @ref set_encoded_user,
  585. @ref set_user.
  586. */
  587. BOOST_URL_DECL
  588. url_base&
  589. set_password(
  590. string_view s);
  591. /** Set the password.
  592. This function sets the password in
  593. the userinfo to the string, which
  594. may contain percent-escapes.
  595. Escapes in the string are preserved,
  596. and reserved characters in the string
  597. are percent-escaped in the result.
  598. @note
  599. The interpretation of the userinfo as
  600. individual user and password components
  601. is scheme-dependent. Transmitting
  602. passwords in URLs is deprecated.
  603. @par Example
  604. @code
  605. assert( url("http://user@example.com").set_encoded_password( "pass" ).encoded_userinfo() == "user:pass" );
  606. @endcode
  607. @par Postconditions
  608. @code
  609. this->has_password() == true
  610. @endcode
  611. @par Exception Safety
  612. Strong guarantee.
  613. Calls to allocate may throw.
  614. @throw system_error
  615. `s` contains an invalid percent-encoding.
  616. @param s The string to set. This string may
  617. contain any characters, including nulls.
  618. @par BNF
  619. @code
  620. userinfo = [ [ user ] [ ':' password ] ]
  621. user = *( unreserved / pct-encoded / sub-delims )
  622. password = *( unreserved / pct-encoded / sub-delims / ":" )
  623. @endcode
  624. @par Specification
  625. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  626. 3.2.1. User Information (rfc3986)</a>
  627. @see
  628. @ref remove_password,
  629. @ref set_encoded_password,
  630. @ref set_encoded_user,
  631. @ref set_user.
  632. */
  633. BOOST_URL_DECL
  634. url_base&
  635. set_encoded_password(
  636. pct_string_view s);
  637. /** Remove the password
  638. This function removes the password from
  639. the userinfo if a password exists. If
  640. there is no userinfo or no authority,
  641. the call has no effect.
  642. @note
  643. The interpretation of the userinfo as
  644. individual user and password components
  645. is scheme-dependent. Transmitting
  646. passwords in URLs is deprecated.
  647. @par Example
  648. @code
  649. assert( url( "http://user:pass@example.com" ).remove_password().authority().buffer() == "user@example.com" );
  650. @endcode
  651. @par Postconditions
  652. @code
  653. this->has_password() == false && this->encoded_password().empty() == true
  654. @endcode
  655. @par Complexity
  656. Linear in `this->size()`.
  657. @par Exception Safety
  658. Throws nothing.
  659. @par BNF
  660. @code
  661. userinfo = [ [ user ] [ ':' password ] ]
  662. user = *( unreserved / pct-encoded / sub-delims )
  663. password = *( unreserved / pct-encoded / sub-delims / ":" )
  664. @endcode
  665. @par Specification
  666. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.1">
  667. 3.2.1. User Information (rfc3986)</a>
  668. @see
  669. @ref set_encoded_password,
  670. @ref set_encoded_user,
  671. @ref set_password,
  672. @ref set_user.
  673. */
  674. BOOST_URL_DECL
  675. url_base&
  676. remove_password() noexcept;
  677. //--------------------------------------------
  678. //
  679. // Host
  680. //
  681. //--------------------------------------------
  682. /** Set the host
  683. Depending on the contents of the passed
  684. string, this function sets the host:
  685. @li If the string is a valid IPv4 address,
  686. then the host is set to the address.
  687. The host type is @ref host_type::ipv4.
  688. @li If the string is a valid IPv6 address
  689. enclosed in square brackets, then the
  690. host is set to that address.
  691. The host type is @ref host_type::ipv6.
  692. @li If the string is a valid IPvFuture
  693. address enclosed in square brackets, then
  694. the host is set to that address.
  695. The host type is @ref host_type::ipvfuture.
  696. @li Otherwise, the host name is set to
  697. the string, which may be empty.
  698. Reserved characters in the string are
  699. percent-escaped in the result.
  700. The host type is @ref host_type::name.
  701. In all cases, when this function returns,
  702. the URL contains an authority.
  703. @par Example
  704. @code
  705. assert( url( "http://www.example.com" ).set_host( "127.0.0.1" ).buffer() == "http://127.0.0.1" );
  706. @endcode
  707. @par Postconditions
  708. @code
  709. this->has_authority() == true
  710. @endcode
  711. @par Complexity
  712. Linear in `this->size() + s.size()`.
  713. @par Exception Safety
  714. Strong guarantee.
  715. Calls to allocate may throw.
  716. @param s The string to set.
  717. @par BNF
  718. @code
  719. host = IP-literal / IPv4address / reg-name
  720. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  721. reg-name = *( unreserved / pct-encoded / "-" / ".")
  722. @endcode
  723. @par Specification
  724. @li <a href="https://en.wikipedia.org/wiki/IPv4"
  725. >IPv4 (Wikipedia)</a>
  726. @li <a href="https://datatracker.ietf.org/doc/html/rfc4291"
  727. >IP Version 6 Addressing Architecture (rfc4291)</a>
  728. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  729. 3.2.2. Host (rfc3986)</a>
  730. @see
  731. @ref set_encoded_host,
  732. @ref set_encoded_host_address,
  733. @ref set_encoded_host_name,
  734. @ref set_host_address,
  735. @ref set_host_ipv4,
  736. @ref set_host_ipv6,
  737. @ref set_host_ipvfuture,
  738. @ref set_host_name.
  739. */
  740. BOOST_URL_DECL
  741. url_base&
  742. set_host(
  743. string_view s);
  744. /** Set the host
  745. Depending on the contents of the passed
  746. string, this function sets the host:
  747. @li If the string is a valid IPv4 address,
  748. then the host is set to the address.
  749. The host type is @ref host_type::ipv4.
  750. @li If the string is a valid IPv6 address
  751. enclosed in square brackets, then the
  752. host is set to that address.
  753. The host type is @ref host_type::ipv6.
  754. @li If the string is a valid IPvFuture
  755. address enclosed in square brackets, then
  756. the host is set to that address.
  757. The host type is @ref host_type::ipvfuture.
  758. @li Otherwise, the host name is set to
  759. the string. This string can contain percent
  760. escapes, or can be empty.
  761. Escapes in the string are preserved,
  762. and reserved characters in the string
  763. are percent-escaped in the result.
  764. The host type is @ref host_type::name.
  765. In all cases, when this function returns,
  766. the URL contains an authority.
  767. @par Example
  768. @code
  769. assert( url( "http://www.example.com" ).set_host( "127.0.0.1" ).buffer() == "http://127.0.0.1" );
  770. @endcode
  771. @par Postconditions
  772. @code
  773. this->has_authority() == true
  774. @endcode
  775. @par Complexity
  776. Linear in `this->size() + s.size()`.
  777. @par Exception Safety
  778. Strong guarantee.
  779. Calls to allocate may throw.
  780. Exceptions thrown on invalid input.
  781. @throw system_error
  782. `s` contains an invalid percent-encoding.
  783. @param s The string to set.
  784. @par BNF
  785. @code
  786. host = IP-literal / IPv4address / reg-name
  787. IP-literal = "[" ( IPv6address / IPvFuture ) "]"
  788. reg-name = *( unreserved / pct-encoded / "-" / ".")
  789. @endcode
  790. @par Specification
  791. @li <a href="https://en.wikipedia.org/wiki/IPv4"
  792. >IPv4 (Wikipedia)</a>
  793. @li <a href="https://datatracker.ietf.org/doc/html/rfc4291"
  794. >IP Version 6 Addressing Architecture (rfc4291)</a>
  795. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  796. 3.2.2. Host (rfc3986)</a>
  797. @see
  798. @ref set_encoded_host_address,
  799. @ref set_encoded_host_name,
  800. @ref set_host,
  801. @ref set_host_address,
  802. @ref set_host_ipv4,
  803. @ref set_host_ipv6,
  804. @ref set_host_ipvfuture,
  805. @ref set_host_name.
  806. */
  807. BOOST_URL_DECL
  808. url_base&
  809. set_encoded_host(pct_string_view s);
  810. /** Set the host to an address
  811. Depending on the contents of the passed
  812. string, this function sets the host:
  813. @li If the string is a valid IPv4 address,
  814. then the host is set to the address.
  815. The host type is @ref host_type::ipv4.
  816. @li If the string is a valid IPv6 address,
  817. then the host is set to that address.
  818. The host type is @ref host_type::ipv6.
  819. @li If the string is a valid IPvFuture,
  820. then the host is set to that address.
  821. The host type is @ref host_type::ipvfuture.
  822. @li Otherwise, the host name is set to
  823. the string, which may be empty.
  824. Reserved characters in the string are
  825. percent-escaped in the result.
  826. The host type is @ref host_type::name.
  827. In all cases, when this function returns,
  828. the URL contains an authority.
  829. @par Example
  830. @code
  831. assert( url( "http://www.example.com" ).set_host_address( "127.0.0.1" ).buffer() == "http://127.0.0.1" );
  832. @endcode
  833. @par Postconditions
  834. @code
  835. this->has_authority() == true
  836. @endcode
  837. @par Complexity
  838. Linear in `s.size()`.
  839. @par Exception Safety
  840. Strong guarantee.
  841. Calls to allocate may throw.
  842. @param s The string to set.
  843. @par BNF
  844. @code
  845. IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  846. dec-octet = DIGIT ; 0-9
  847. / %x31-39 DIGIT ; 10-99
  848. / "1" 2DIGIT ; 100-199
  849. / "2" %x30-34 DIGIT ; 200-249
  850. / "25" %x30-35 ; 250-255
  851. IPv6address = 6( h16 ":" ) ls32
  852. / "::" 5( h16 ":" ) ls32
  853. / [ h16 ] "::" 4( h16 ":" ) ls32
  854. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  855. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  856. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  857. / [ *4( h16 ":" ) h16 ] "::" ls32
  858. / [ *5( h16 ":" ) h16 ] "::" h16
  859. / [ *6( h16 ":" ) h16 ] "::"
  860. ls32 = ( h16 ":" h16 ) / IPv4address
  861. ; least-significant 32 bits of address
  862. h16 = 1*4HEXDIG
  863. ; 16 bits of address represented in hexadecimal
  864. IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  865. reg-name = *( unreserved / pct-encoded / "-" / ".")
  866. @endcode
  867. @par Specification
  868. @li <a href="https://en.wikipedia.org/wiki/IPv4"
  869. >IPv4 (Wikipedia)</a>
  870. @li <a href="https://datatracker.ietf.org/doc/html/rfc4291"
  871. >IP Version 6 Addressing Architecture (rfc4291)</a>
  872. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  873. 3.2.2. Host (rfc3986)</a>
  874. @see
  875. @ref set_encoded_host,
  876. @ref set_encoded_host_address,
  877. @ref set_encoded_host_name,
  878. @ref set_host,
  879. @ref set_host_address,
  880. @ref set_host_ipv4,
  881. @ref set_host_ipv6,
  882. @ref set_host_ipvfuture,
  883. @ref set_host_name.
  884. */
  885. BOOST_URL_DECL
  886. url_base&
  887. set_host_address(string_view s);
  888. /** Set the host to an address
  889. Depending on the contents of the passed
  890. string, this function sets the host:
  891. @li If the string is a valid IPv4 address,
  892. then the host is set to the address.
  893. The host type is @ref host_type::ipv4.
  894. @li If the string is a valid IPv6 address,
  895. then the host is set to that address.
  896. The host type is @ref host_type::ipv6.
  897. @li If the string is a valid IPvFuture,
  898. then the host is set to that address.
  899. The host type is @ref host_type::ipvfuture.
  900. @li Otherwise, the host name is set to
  901. the string. This string can contain percent
  902. escapes, or can be empty.
  903. Escapes in the string are preserved,
  904. and reserved characters in the string
  905. are percent-escaped in the result.
  906. The host type is @ref host_type::name.
  907. In all cases, when this function returns,
  908. the URL contains an authority.
  909. @par Example
  910. @code
  911. assert( url( "http://www.example.com" ).set_host( "127.0.0.1" ).buffer() == "http://127.0.0.1" );
  912. @endcode
  913. @par Postconditions
  914. @code
  915. this->has_authority() == true
  916. @endcode
  917. @par Complexity
  918. Linear in `this->size() + s.size()`.
  919. @par Exception Safety
  920. Strong guarantee.
  921. Calls to allocate may throw.
  922. Exceptions thrown on invalid input.
  923. @throw system_error
  924. `s` contains an invalid percent-encoding.
  925. @param s The string to set.
  926. @par BNF
  927. @code
  928. IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  929. dec-octet = DIGIT ; 0-9
  930. / %x31-39 DIGIT ; 10-99
  931. / "1" 2DIGIT ; 100-199
  932. / "2" %x30-34 DIGIT ; 200-249
  933. / "25" %x30-35 ; 250-255
  934. IPv6address = 6( h16 ":" ) ls32
  935. / "::" 5( h16 ":" ) ls32
  936. / [ h16 ] "::" 4( h16 ":" ) ls32
  937. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  938. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  939. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  940. / [ *4( h16 ":" ) h16 ] "::" ls32
  941. / [ *5( h16 ":" ) h16 ] "::" h16
  942. / [ *6( h16 ":" ) h16 ] "::"
  943. ls32 = ( h16 ":" h16 ) / IPv4address
  944. ; least-significant 32 bits of address
  945. h16 = 1*4HEXDIG
  946. ; 16 bits of address represented in hexadecimal
  947. IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  948. reg-name = *( unreserved / pct-encoded / "-" / ".")
  949. @endcode
  950. @par Specification
  951. @li <a href="https://en.wikipedia.org/wiki/IPv4"
  952. >IPv4 (Wikipedia)</a>
  953. @li <a href="https://datatracker.ietf.org/doc/html/rfc4291"
  954. >IP Version 6 Addressing Architecture (rfc4291)</a>
  955. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  956. 3.2.2. Host (rfc3986)</a>
  957. @see
  958. @ref set_encoded_host,
  959. @ref set_encoded_host_name,
  960. @ref set_host,
  961. @ref set_host_address,
  962. @ref set_host_ipv4,
  963. @ref set_host_ipv6,
  964. @ref set_host_ipvfuture,
  965. @ref set_host_name.
  966. */
  967. BOOST_URL_DECL
  968. url_base&
  969. set_encoded_host_address(
  970. pct_string_view s);
  971. /** Set the host to an address
  972. The host is set to the specified IPv4
  973. address.
  974. The host type is @ref host_type::ipv4.
  975. @par Example
  976. @code
  977. assert( url("http://www.example.com").set_host_ipv4( ipv4_address( "127.0.0.1" ) ).buffer() == "http://127.0.0.1" );
  978. @endcode
  979. @par Complexity
  980. Linear in `this->size()`.
  981. @par Postconditions
  982. @code
  983. this->has_authority() == true && this->host_ipv4_address() == addr && this->host_type() == host_type::ipv4
  984. @endcode
  985. @par Exception Safety
  986. Strong guarantee.
  987. Calls to allocate may throw.
  988. @param addr The address to set.
  989. @par BNF
  990. @code
  991. IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
  992. dec-octet = DIGIT ; 0-9
  993. / %x31-39 DIGIT ; 10-99
  994. / "1" 2DIGIT ; 100-199
  995. / "2" %x30-34 DIGIT ; 200-249
  996. / "25" %x30-35 ; 250-255
  997. @endcode
  998. @par Specification
  999. @li <a href="https://en.wikipedia.org/wiki/IPv4"
  1000. >IPv4 (Wikipedia)</a>
  1001. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  1002. 3.2.2. Host (rfc3986)</a>
  1003. @see
  1004. @ref set_encoded_host,
  1005. @ref set_encoded_host_address,
  1006. @ref set_encoded_host_name,
  1007. @ref set_host,
  1008. @ref set_host_address,
  1009. @ref set_host_ipv6,
  1010. @ref set_host_ipvfuture,
  1011. @ref set_host_name.
  1012. */
  1013. BOOST_URL_DECL
  1014. url_base&
  1015. set_host_ipv4(
  1016. ipv4_address const& addr);
  1017. /** Set the host to an address
  1018. The host is set to the specified IPv6
  1019. address.
  1020. The host type is @ref host_type::ipv6.
  1021. @par Example
  1022. @code
  1023. assert( url().set_host_ipv6( ipv6_address( "1::6:c0a8:1" ) ).authority().buffer() == "[1::6:c0a8:1]" );
  1024. @endcode
  1025. @par Postconditions
  1026. @code
  1027. this->has_authority() == true && this->host_ipv6_address() == addr && this->host_type() == host_type::ipv6
  1028. @endcode
  1029. @par Complexity
  1030. Linear in `this->size()`.
  1031. @par Exception Safety
  1032. Strong guarantee.
  1033. Calls to allocate may throw.
  1034. @param addr The address to set.
  1035. @par BNF
  1036. @code
  1037. IPv6address = 6( h16 ":" ) ls32
  1038. / "::" 5( h16 ":" ) ls32
  1039. / [ h16 ] "::" 4( h16 ":" ) ls32
  1040. / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
  1041. / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
  1042. / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
  1043. / [ *4( h16 ":" ) h16 ] "::" ls32
  1044. / [ *5( h16 ":" ) h16 ] "::" h16
  1045. / [ *6( h16 ":" ) h16 ] "::"
  1046. ls32 = ( h16 ":" h16 ) / IPv4address
  1047. ; least-significant 32 bits of address
  1048. h16 = 1*4HEXDIG
  1049. ; 16 bits of address represented in hexadecimal
  1050. @endcode
  1051. @par Specification
  1052. @li <a href="https://datatracker.ietf.org/doc/html/rfc4291"
  1053. >IP Version 6 Addressing Architecture (rfc4291)</a>
  1054. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  1055. 3.2.2. Host (rfc3986)</a>
  1056. @see
  1057. @ref set_encoded_host,
  1058. @ref set_encoded_host_address,
  1059. @ref set_encoded_host_name,
  1060. @ref set_host,
  1061. @ref set_host_address,
  1062. @ref set_host_ipv4,
  1063. @ref set_host_ipvfuture,
  1064. @ref set_host_name.
  1065. */
  1066. BOOST_URL_DECL
  1067. url_base&
  1068. set_host_ipv6(
  1069. ipv6_address const& addr);
  1070. /** Set the host to an address
  1071. The host is set to the specified IPvFuture
  1072. string.
  1073. The host type is @ref host_type::ipvfuture.
  1074. @par Example
  1075. @code
  1076. assert( url().set_host_ipvfuture( "v42.bis" ).buffer() == "//[v42.bis]" );
  1077. @endcode
  1078. @par Complexity
  1079. Linear in `this->size() + s.size()`.
  1080. @par Postconditions
  1081. @code
  1082. this->has_authority() == true && this->host_ipvfuture) == s && this->host_type() == host_type::ipvfuture
  1083. @endcode
  1084. @par Exception Safety
  1085. Strong guarantee.
  1086. Calls to allocate may throw.
  1087. Exceptions thrown on invalid input.
  1088. @throw system_error
  1089. `s` contains an invalid percent-encoding.
  1090. @param s The string to set.
  1091. @par BNF
  1092. @code
  1093. IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
  1094. @endcode
  1095. @par Specification
  1096. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  1097. 3.2.2. Host (rfc3986)</a>
  1098. @see
  1099. @ref set_encoded_host,
  1100. @ref set_encoded_host_address,
  1101. @ref set_encoded_host_name,
  1102. @ref set_host,
  1103. @ref set_host_address,
  1104. @ref set_host_ipv4,
  1105. @ref set_host_ipv6,
  1106. @ref set_host_name.
  1107. */
  1108. BOOST_URL_DECL
  1109. url_base&
  1110. set_host_ipvfuture(
  1111. string_view s);
  1112. /** Set the host to a name
  1113. The host is set to the specified string,
  1114. which may be empty.
  1115. Reserved characters in the string are
  1116. percent-escaped in the result.
  1117. The host type is @ref host_type::name.
  1118. @par Example
  1119. @code
  1120. assert( url( "http://www.example.com/index.htm").set_host_name( "localhost" ).host_address() == "localhost" );
  1121. @endcode
  1122. @par Postconditions
  1123. @code
  1124. this->has_authority() == true && this->host_ipv6_address() == addr && this->host_type() == host_type::name
  1125. @endcode
  1126. @par Exception Safety
  1127. Strong guarantee.
  1128. Calls to allocate may throw.
  1129. @param s The string to set.
  1130. @par BNF
  1131. @code
  1132. reg-name = *( unreserved / pct-encoded / "-" / ".")
  1133. @endcode
  1134. @par Specification
  1135. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  1136. 3.2.2. Host (rfc3986)</a>
  1137. @see
  1138. @ref set_encoded_host,
  1139. @ref set_encoded_host_address,
  1140. @ref set_encoded_host_name,
  1141. @ref set_host,
  1142. @ref set_host_address,
  1143. @ref set_host_ipv4,
  1144. @ref set_host_ipv6,
  1145. @ref set_host_ipvfuture.
  1146. */
  1147. BOOST_URL_DECL
  1148. url_base&
  1149. set_host_name(
  1150. string_view s);
  1151. /** Set the host to a name
  1152. The host is set to the specified string,
  1153. which may contain percent-escapes and
  1154. can be empty.
  1155. Escapes in the string are preserved,
  1156. and reserved characters in the string
  1157. are percent-escaped in the result.
  1158. The host type is @ref host_type::name.
  1159. @par Example
  1160. @code
  1161. assert( url( "http://www.example.com/index.htm").set_encoded_host_name( "localhost" ).host_address() == "localhost" );
  1162. @endcode
  1163. @par Postconditions
  1164. @code
  1165. this->has_authority() == true && this->host_ipv6_address() == addr && this->host_type() == host_type::name
  1166. @endcode
  1167. @par Exception Safety
  1168. Strong guarantee.
  1169. Calls to allocate may throw.
  1170. Exceptions thrown on invalid input.
  1171. @throw system_error
  1172. `s` contains an invalid percent-encoding.
  1173. @param s The string to set.
  1174. @par BNF
  1175. @code
  1176. reg-name = *( unreserved / pct-encoded / "-" / ".")
  1177. @endcode
  1178. @par Specification
  1179. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2">
  1180. 3.2.2. Host (rfc3986)</a>
  1181. @see
  1182. @ref set_encoded_host,
  1183. @ref set_encoded_host_address,
  1184. @ref set_host,
  1185. @ref set_host_address,
  1186. @ref set_host_ipv4,
  1187. @ref set_host_ipv6,
  1188. @ref set_host_ipvfuture,
  1189. @ref set_host_name.
  1190. */
  1191. BOOST_URL_DECL
  1192. url_base&
  1193. set_encoded_host_name(
  1194. pct_string_view s);
  1195. //--------------------------------------------
  1196. /** Set the port
  1197. The port is set to the specified integer.
  1198. @par Example
  1199. @code
  1200. assert( url( "http://www.example.com" ).set_port_number( 8080 ).authority().buffer() == "www.example.com:8080" );
  1201. @endcode
  1202. @par Postconditions
  1203. @code
  1204. this->has_authority() == true && this->has_port() == true && this->port_number() == n
  1205. @endcode
  1206. @par Complexity
  1207. Linear in `this->size()`.
  1208. @par Exception Safety
  1209. Strong guarantee.
  1210. Calls to allocate may throw.
  1211. @param n The port number to set.
  1212. @par BNF
  1213. @code
  1214. authority = [ userinfo "@" ] host [ ":" port ]
  1215. port = *DIGIT
  1216. @endcode
  1217. @par Specification
  1218. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">
  1219. 3.2.3. Port (rfc3986)</a>
  1220. @see
  1221. @ref remove_port,
  1222. @ref set_port.
  1223. */
  1224. BOOST_URL_DECL
  1225. url_base&
  1226. set_port_number(std::uint16_t n);
  1227. /** Set the port
  1228. This port is set to the string, which
  1229. must contain only digits or be empty.
  1230. An empty port string is distinct from
  1231. having no port.
  1232. @par Example
  1233. @code
  1234. assert( url( "http://www.example.com" ).set_port( "8080" ).authority().buffer() == "www.example.com:8080" );
  1235. @endcode
  1236. @par Postconditions
  1237. @code
  1238. this->has_port() == true && this->port_number() == n && this->port() == std::to_string(n)
  1239. @endcode
  1240. @par Exception Safety
  1241. Strong guarantee.
  1242. Calls to allocate may throw.
  1243. Exceptions thrown on invalid input.
  1244. @throw system_error
  1245. `s` does not contain a valid port.
  1246. @param s The port string to set.
  1247. @par BNF
  1248. @code
  1249. port = *DIGIT
  1250. @endcode
  1251. @par Specification
  1252. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">
  1253. 3.2.3. Port (rfc3986)</a>
  1254. @see
  1255. @ref remove_port,
  1256. @ref set_port.
  1257. */
  1258. BOOST_URL_DECL
  1259. url_base&
  1260. set_port(string_view s);
  1261. /** Remove the port
  1262. If a port exists, it is removed. The rest
  1263. of the authority is unchanged.
  1264. @par Example
  1265. @code
  1266. assert( url( "http://www.example.com:80" ).remove_port().authority().buffer() == "www.example.com" );
  1267. @endcode
  1268. @par Postconditions
  1269. @code
  1270. this->has_port() == false && this->port_number() == 0 && this->port() == ""
  1271. @endcode
  1272. @par Complexity
  1273. Linear in `this->size()`.
  1274. @par Exception Safety
  1275. Throws nothing.
  1276. @par BNF
  1277. @code
  1278. authority = [ userinfo "@" ] host [ ":" port ]
  1279. port = *DIGIT
  1280. @endcode
  1281. @par Specification
  1282. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.3">
  1283. 3.2.3. Port (rfc3986)</a>
  1284. @see
  1285. @ref set_port.
  1286. */
  1287. BOOST_URL_DECL
  1288. url_base&
  1289. remove_port() noexcept;
  1290. //--------------------------------------------
  1291. //
  1292. // Path
  1293. //
  1294. //--------------------------------------------
  1295. /** Set if the path is absolute
  1296. This function adjusts the path to make
  1297. it absolute or not, depending on the
  1298. parameter.
  1299. @note
  1300. If an authority is present, the path
  1301. is always absolute. In this case, the
  1302. function has no effect.
  1303. @par Example
  1304. @code
  1305. url u( "path/to/file.txt" );
  1306. assert( u.set_path_absolute( true ) );
  1307. assert( u.buffer() == "/path/to/file.txt" );
  1308. @endcode
  1309. @par Postconditions
  1310. @code
  1311. this->is_path_absolute() == true && this->encoded_path().front() == '/'
  1312. @endcode
  1313. @return true on success.
  1314. @par Complexity
  1315. Linear in `this->size()`.
  1316. @par BNF
  1317. @code
  1318. path = path-abempty ; begins with "/" or is empty
  1319. / path-absolute ; begins with "/" but not "//"
  1320. / path-noscheme ; begins with a non-colon segment
  1321. / path-rootless ; begins with a segment
  1322. / path-empty ; zero characters
  1323. path-abempty = *( "/" segment )
  1324. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1325. path-noscheme = segment-nz-nc *( "/" segment )
  1326. path-rootless = segment-nz *( "/" segment )
  1327. path-empty = 0<pchar>
  1328. @endcode
  1329. @par Specification
  1330. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  1331. >3.3. Path (rfc3986)</a>
  1332. @see
  1333. @ref encoded_segments,
  1334. @ref segments,
  1335. @ref set_encoded_path,
  1336. @ref set_path.
  1337. */
  1338. BOOST_URL_DECL
  1339. bool
  1340. set_path_absolute(bool absolute);
  1341. /** Set the path.
  1342. This function sets the path to the
  1343. string, which may be empty.
  1344. Reserved characters in the string are
  1345. percent-escaped in the result.
  1346. @note
  1347. The library may adjust the final result
  1348. to ensure that no other parts of the url
  1349. is semantically affected.
  1350. @par Example
  1351. @code
  1352. url u( "http://www.example.com" );
  1353. u.set_path( "path/to/file.txt" );
  1354. assert( u.path() == "/path/to/file.txt" );
  1355. @endcode
  1356. @par Complexity
  1357. Linear in `this->size() + s.size()`.
  1358. @par Exception Safety
  1359. Strong guarantee.
  1360. Calls to allocate may throw.
  1361. @param s The string to set.
  1362. @par BNF
  1363. @code
  1364. path = path-abempty ; begins with "/" or is empty
  1365. / path-absolute ; begins with "/" but not "//"
  1366. / path-noscheme ; begins with a non-colon segment
  1367. / path-rootless ; begins with a segment
  1368. / path-empty ; zero characters
  1369. path-abempty = *( "/" segment )
  1370. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1371. path-noscheme = segment-nz-nc *( "/" segment )
  1372. path-rootless = segment-nz *( "/" segment )
  1373. path-empty = 0<pchar>
  1374. @endcode
  1375. @par Specification
  1376. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  1377. >3.3. Path (rfc3986)</a>
  1378. @see
  1379. @ref encoded_segments,
  1380. @ref segments,
  1381. @ref set_encoded_path,
  1382. @ref set_path_absolute.
  1383. */
  1384. BOOST_URL_DECL
  1385. url_base&
  1386. set_path(
  1387. string_view s);
  1388. /** Set the path.
  1389. This function sets the path to the
  1390. string, which may contain percent-escapes
  1391. and can be empty.
  1392. Escapes in the string are preserved,
  1393. and reserved characters in the string
  1394. are percent-escaped in the result.
  1395. @note
  1396. The library may adjust the final result
  1397. to ensure that no other parts of the url
  1398. is semantically affected.
  1399. @par Example
  1400. @code
  1401. url u( "http://www.example.com" );
  1402. u.set_encoded_path( "path/to/file.txt" );
  1403. assert( u.encoded_path() == "/path/to/file.txt" );
  1404. @endcode
  1405. @par Complexity
  1406. Linear in `this->size() + s.size()`.
  1407. @par Exception Safety
  1408. Strong guarantee.
  1409. Calls to allocate may throw.
  1410. Exceptions thrown on invalid input.
  1411. @throw system_error
  1412. `s` contains an invalid percent-encoding.
  1413. @param s The string to set.
  1414. @par BNF
  1415. @code
  1416. path = path-abempty ; begins with "/" or is empty
  1417. / path-absolute ; begins with "/" but not "//"
  1418. / path-noscheme ; begins with a non-colon segment
  1419. / path-rootless ; begins with a segment
  1420. / path-empty ; zero characters
  1421. path-abempty = *( "/" segment )
  1422. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1423. path-noscheme = segment-nz-nc *( "/" segment )
  1424. path-rootless = segment-nz *( "/" segment )
  1425. path-empty = 0<pchar>
  1426. @endcode
  1427. @par Specification
  1428. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  1429. >3.3. Path (rfc3986)</a>
  1430. @see
  1431. @ref encoded_segments,
  1432. @ref segments,
  1433. @ref set_path,
  1434. @ref set_path_absolute.
  1435. */
  1436. BOOST_URL_DECL
  1437. url_base&
  1438. set_encoded_path(
  1439. pct_string_view s);
  1440. /** Return the path as a container of segments
  1441. This function returns a bidirectional
  1442. view of segments over the path.
  1443. The returned view references the same
  1444. underlying character buffer; ownership
  1445. is not transferred.
  1446. Any percent-escapes in strings returned
  1447. when iterating the view are decoded first.
  1448. The container is modifiable; changes
  1449. to the container are reflected in the
  1450. underlying URL.
  1451. @par Example
  1452. @code
  1453. url u( "http://example.com/path/to/file.txt" );
  1454. segments sv = u.segments();
  1455. @endcode
  1456. @par Complexity
  1457. Constant.
  1458. @par Exception Safety
  1459. Throws nothing.
  1460. @par BNF
  1461. @code
  1462. path = path-abempty ; begins with "/" or is empty
  1463. / path-absolute ; begins with "/" but not "//"
  1464. / path-noscheme ; begins with a non-colon segment
  1465. / path-rootless ; begins with a segment
  1466. / path-empty ; zero characters
  1467. path-abempty = *( "/" segment )
  1468. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1469. path-noscheme = segment-nz-nc *( "/" segment )
  1470. path-rootless = segment-nz *( "/" segment )
  1471. path-empty = 0<pchar>
  1472. @endcode
  1473. @par Specification
  1474. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  1475. >3.3. Path (rfc3986)</a>
  1476. @see
  1477. @ref encoded_segments,
  1478. @ref set_encoded_path,
  1479. @ref set_path,
  1480. @ref set_path_absolute.
  1481. */
  1482. BOOST_URL_DECL
  1483. urls::segments_ref
  1484. segments() noexcept;
  1485. /// @copydoc url_view_base::segments
  1486. segments_view
  1487. segments() const noexcept
  1488. {
  1489. return url_view_base::segments();
  1490. }
  1491. /** Return the path as a container of segments
  1492. This function returns a bidirectional
  1493. view of segments over the path.
  1494. The returned view references the same
  1495. underlying character buffer; ownership
  1496. is not transferred.
  1497. Strings returned when iterating the
  1498. range may contain percent escapes.
  1499. The container is modifiable; changes
  1500. to the container are reflected in the
  1501. underlying URL.
  1502. @par Example
  1503. @code
  1504. url u( "http://example.com/path/to/file.txt" );
  1505. segments_encoded_ref sv = u.encoded_segments();
  1506. @endcode
  1507. @par Complexity
  1508. Constant.
  1509. @par Exception Safety
  1510. Throws nothing.
  1511. @par BNF
  1512. @code
  1513. path = path-abempty ; begins with "/" or is empty
  1514. / path-absolute ; begins with "/" but not "//"
  1515. / path-noscheme ; begins with a non-colon segment
  1516. / path-rootless ; begins with a segment
  1517. / path-empty ; zero characters
  1518. path-abempty = *( "/" segment )
  1519. path-absolute = "/" [ segment-nz *( "/" segment ) ]
  1520. path-noscheme = segment-nz-nc *( "/" segment )
  1521. path-rootless = segment-nz *( "/" segment )
  1522. path-empty = 0<pchar>
  1523. @endcode
  1524. @par Specification
  1525. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.3"
  1526. >3.3. Path (rfc3986)</a>
  1527. @see
  1528. @ref encoded_segments,
  1529. @ref set_encoded_path,
  1530. @ref set_path,
  1531. @ref set_path_absolute.
  1532. */
  1533. BOOST_URL_DECL
  1534. segments_encoded_ref
  1535. encoded_segments() noexcept;
  1536. /// @copydoc url_view_base::encoded_segments
  1537. segments_encoded_view
  1538. encoded_segments() const noexcept
  1539. {
  1540. return url_view_base::encoded_segments();
  1541. }
  1542. //--------------------------------------------
  1543. //
  1544. // Query
  1545. //
  1546. //--------------------------------------------
  1547. /** Set the query
  1548. This sets the query to the string, which
  1549. can be empty.
  1550. An empty query is distinct from having
  1551. no query.
  1552. Reserved characters in the string are
  1553. percent-escaped in the result.
  1554. @par Example
  1555. @code
  1556. assert( url( "http://example.com" ).set_query( "id=42" ).query() == "id=42" );
  1557. @endcode
  1558. @par Postconditions
  1559. @code
  1560. this->has_query() == true && this->query() == s
  1561. @endcode
  1562. @par Exception Safety
  1563. Strong guarantee.
  1564. Calls to allocate may throw.
  1565. @param s The string to set.
  1566. @par BNF
  1567. @code
  1568. query = *( pchar / "/" / "?" )
  1569. query-param = key [ "=" value ]
  1570. query-params = [ query-param ] *( "&" query-param )
  1571. @endcode
  1572. @par Specification
  1573. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1574. >3.4. Query (rfc3986)</a>
  1575. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1576. >Query string (Wikipedia)</a>
  1577. @see
  1578. @ref encoded_params,
  1579. @ref params,
  1580. @ref remove_query,
  1581. @ref set_encoded_query.
  1582. */
  1583. BOOST_URL_DECL
  1584. url_base&
  1585. set_query(
  1586. string_view s);
  1587. /** Set the query
  1588. This sets the query to the string, which
  1589. may contain percent-escapes and can be
  1590. empty.
  1591. An empty query is distinct from having
  1592. no query.
  1593. Escapes in the string are preserved,
  1594. and reserved characters in the string
  1595. are percent-escaped in the result.
  1596. @par Example
  1597. @code
  1598. assert( url( "http://example.com" ).set_encoded_query( "id=42" ).encoded_query() == "id=42" );
  1599. @endcode
  1600. @par Postconditions
  1601. @code
  1602. this->has_query() == true && this->query() == decode_view( s );
  1603. @endcode
  1604. @par Exception Safety
  1605. Strong guarantee.
  1606. Calls to allocate may throw.
  1607. Exceptions thrown on invalid input.
  1608. @param s The string to set.
  1609. @throws system_error
  1610. `s` contains an invalid percent-encoding.
  1611. @par BNF
  1612. @code
  1613. query = *( pchar / "/" / "?" )
  1614. query-param = key [ "=" value ]
  1615. query-params = [ query-param ] *( "&" query-param )
  1616. @endcode
  1617. @par Specification
  1618. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1619. >3.4. Query (rfc3986)</a>
  1620. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1621. >Query string (Wikipedia)</a>
  1622. @see
  1623. @ref encoded_params,
  1624. @ref params,
  1625. @ref remove_query,
  1626. @ref set_query.
  1627. */
  1628. BOOST_URL_DECL
  1629. url_base&
  1630. set_encoded_query(
  1631. pct_string_view s);
  1632. /** Return the query as a container of parameters
  1633. This function returns a bidirectional
  1634. view of key/value pairs over the query.
  1635. The returned view references the same
  1636. underlying character buffer; ownership
  1637. is not transferred.
  1638. Any percent-escapes in strings returned
  1639. when iterating the view are decoded first.
  1640. The container is modifiable; changes
  1641. to the container are reflected in the
  1642. underlying URL.
  1643. @par Example
  1644. @code
  1645. params_ref pv = url( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).params();
  1646. @endcode
  1647. @par Complexity
  1648. Constant.
  1649. @par Exception Safety
  1650. Throws nothing.
  1651. @par BNF
  1652. @code
  1653. query = *( pchar / "/" / "?" )
  1654. query-param = key [ "=" value ]
  1655. query-params = [ query-param ] *( "&" query-param )
  1656. @endcode
  1657. @par Specification
  1658. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1659. >3.4. Query (rfc3986)</a>
  1660. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1661. >Query string (Wikipedia)</a>
  1662. @see
  1663. @ref encoded_params,
  1664. @ref remove_query,
  1665. @ref set_encoded_query,
  1666. @ref set_query.
  1667. */
  1668. BOOST_URL_DECL
  1669. params_ref
  1670. params() noexcept;
  1671. /// @copydoc url_view_base::params
  1672. params_view
  1673. params() const noexcept
  1674. {
  1675. return url_view_base::params();
  1676. }
  1677. /** Return the query as a container of parameters
  1678. This function returns a bidirectional
  1679. view of key/value pairs over the query.
  1680. The returned view references the same
  1681. underlying character buffer; ownership
  1682. is not transferred.
  1683. Any percent-escapes in strings returned
  1684. when iterating the view are decoded first.
  1685. The container is modifiable; changes
  1686. to the container are reflected in the
  1687. underlying URL.
  1688. @par Example
  1689. @code
  1690. encoding_opts opt;
  1691. opt.space_as_plus = true;
  1692. params_ref pv = url( "/sql?id=42&name=jane+doe&page+size=20" ).params(opt);
  1693. @endcode
  1694. @par Complexity
  1695. Constant.
  1696. @par Exception Safety
  1697. Throws nothing.
  1698. @param opt The options for decoding. If
  1699. this parameter is omitted, the `space_as_plus`
  1700. is used.
  1701. @par BNF
  1702. @code
  1703. query = *( pchar / "/" / "?" )
  1704. query-param = key [ "=" value ]
  1705. query-params = [ query-param ] *( "&" query-param )
  1706. @endcode
  1707. @par Specification
  1708. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1709. >3.4. Query (rfc3986)</a>
  1710. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1711. >Query string (Wikipedia)</a>
  1712. @see
  1713. @ref encoded_params,
  1714. @ref remove_query,
  1715. @ref set_encoded_query,
  1716. @ref set_query.
  1717. */
  1718. BOOST_URL_DECL
  1719. params_ref
  1720. params(encoding_opts opt) noexcept;
  1721. /// @copydoc url_view_base::encoded_params
  1722. params_encoded_view
  1723. encoded_params() const noexcept
  1724. {
  1725. return url_view_base::encoded_params();
  1726. }
  1727. /** Return the query as a container of parameters
  1728. This function returns a bidirectional
  1729. view of key/value pairs over the query.
  1730. The returned view references the same
  1731. underlying character buffer; ownership
  1732. is not transferred.
  1733. Strings returned when iterating the
  1734. range may contain percent escapes.
  1735. The container is modifiable; changes
  1736. to the container are reflected in the
  1737. underlying URL.
  1738. @par Example
  1739. @code
  1740. params_encoded_ref pv = url( "/sql?id=42&name=jane%2Ddoe&page+size=20" ).encoded_params();
  1741. @endcode
  1742. @par Complexity
  1743. Constant.
  1744. @par Exception Safety
  1745. Throws nothing.
  1746. @par BNF
  1747. @code
  1748. query = *( pchar / "/" / "?" )
  1749. query-param = key [ "=" value ]
  1750. query-params = [ query-param ] *( "&" query-param )
  1751. @endcode
  1752. @par Specification
  1753. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1754. >3.4. Query (rfc3986)</a>
  1755. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1756. >Query string (Wikipedia)</a>
  1757. @see
  1758. @ref params,
  1759. @ref remove_query,
  1760. @ref set_encoded_query,
  1761. @ref set_query.
  1762. */
  1763. BOOST_URL_DECL
  1764. params_encoded_ref
  1765. encoded_params() noexcept;
  1766. /** Set the query params
  1767. This sets the query params to the list
  1768. of param_view, which can be empty.
  1769. An empty list of params is distinct from
  1770. having no params.
  1771. Reserved characters in the string are
  1772. percent-escaped in the result.
  1773. @par Example
  1774. @code
  1775. assert( url( "http://example.com" ).set_params( {"id", "42"} ).query() == "id=42" );
  1776. @endcode
  1777. @par Postconditions
  1778. @code
  1779. this->has_query() == true
  1780. @endcode
  1781. @par Exception Safety
  1782. Strong guarantee.
  1783. Calls to allocate may throw.
  1784. @par Complexity
  1785. Linear.
  1786. @param ps The params to set.
  1787. @par BNF
  1788. @code
  1789. query = *( pchar / "/" / "?" )
  1790. query-param = key [ "=" value ]
  1791. query-params = [ query-param ] *( "&" query-param )
  1792. @endcode
  1793. @par Specification
  1794. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4
  1795. >3.4. Query (rfc3986)</a>
  1796. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1797. >Query string (Wikipedia)</a>
  1798. @see
  1799. @ref encoded_params,
  1800. @ref remove_query,
  1801. @ref set_encoded_query,
  1802. @ref set_query.
  1803. */
  1804. BOOST_URL_DECL
  1805. url_base&
  1806. set_params( std::initializer_list<param_view> ps ) noexcept;
  1807. /** Set the query params
  1808. This sets the query params to the elements
  1809. in the list, which may contain
  1810. percent-escapes and can be empty.
  1811. An empty list of params is distinct from
  1812. having no query.
  1813. Escapes in the string are preserved,
  1814. and reserved characters in the string
  1815. are percent-escaped in the result.
  1816. @par Example
  1817. @code
  1818. assert( url( "http://example.com" ).set_encoded_params( {"id", "42"} ).encoded_query() == "id=42" );
  1819. @endcode
  1820. @par Postconditions
  1821. @code
  1822. this->has_query() == true
  1823. @endcode
  1824. @par Complexity
  1825. Linear.
  1826. @par Exception Safety
  1827. Strong guarantee.
  1828. Calls to allocate may throw.
  1829. Exceptions thrown on invalid input.
  1830. @param ps The params to set.
  1831. @throws system_error
  1832. some element in `ps` contains an invalid percent-encoding.
  1833. @par BNF
  1834. @code
  1835. query = *( pchar / "/" / "?" )
  1836. query-param = key [ "=" value ]
  1837. query-params = [ query-param ] *( "&" query-param )
  1838. @endcode
  1839. @par Specification
  1840. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1841. >3.4. Query (rfc3986)</a>
  1842. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1843. >Query string (Wikipedia)</a>
  1844. @see
  1845. @ref set_params,
  1846. @ref params,
  1847. @ref remove_query,
  1848. @ref set_encoded_query,
  1849. @ref set_query.
  1850. */
  1851. BOOST_URL_DECL
  1852. url_base&
  1853. set_encoded_params( std::initializer_list< param_pct_view > ps ) noexcept;
  1854. /** Remove the query
  1855. If a query is present, it is removed.
  1856. An empty query is distinct from having
  1857. no query.
  1858. @par Example
  1859. @code
  1860. assert( url( "http://www.example.com?id=42" ).remove_query().buffer() == "http://www.example.com" );
  1861. @endcode
  1862. @par Postconditions
  1863. @code
  1864. this->has_query() == false && this->params().empty()
  1865. @endcode
  1866. @par Exception Safety
  1867. Throws nothing.
  1868. @par BNF
  1869. @code
  1870. query = *( pchar / "/" / "?" )
  1871. query-param = key [ "=" value ]
  1872. query-params = [ query-param ] *( "&" query-param )
  1873. @endcode
  1874. @par Specification
  1875. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.4"
  1876. >3.4. Query (rfc3986)</a>
  1877. @li <a href="https://en.wikipedia.org/wiki/Query_string"
  1878. >Query string (Wikipedia)</a>
  1879. @see
  1880. @ref encoded_params,
  1881. @ref params,
  1882. @ref set_encoded_query,
  1883. @ref set_query.
  1884. */
  1885. BOOST_URL_DECL
  1886. url_base&
  1887. remove_query() noexcept;
  1888. //--------------------------------------------
  1889. //
  1890. // Fragment
  1891. //
  1892. //--------------------------------------------
  1893. /** Remove the fragment
  1894. This function removes the fragment.
  1895. An empty fragment is distinct from
  1896. having no fragment.
  1897. @par Example
  1898. @code
  1899. assert( url( "?first=john&last=doe#anchor" ).remove_fragment().buffer() == "?first=john&last=doe" );
  1900. @endcode
  1901. @par Postconditions
  1902. @code
  1903. this->has_fragment() == false && this->encoded_fragment() == ""
  1904. @endcode
  1905. @par Complexity
  1906. Constant.
  1907. @par Exception Safety
  1908. Throws nothing.
  1909. @par BNF
  1910. @code
  1911. fragment = *( pchar / "/" / "?" )
  1912. @endcode
  1913. @par Specification
  1914. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5"
  1915. >3.5. Fragment</a>
  1916. @see
  1917. @ref remove_fragment,
  1918. @ref set_encoded_fragment,
  1919. @ref set_fragment.
  1920. */
  1921. BOOST_URL_DECL
  1922. url_base&
  1923. remove_fragment() noexcept;
  1924. /** Set the fragment.
  1925. This function sets the fragment to the
  1926. specified string, which may be empty.
  1927. An empty fragment is distinct from
  1928. having no fragment.
  1929. Reserved characters in the string are
  1930. percent-escaped in the result.
  1931. @par Example
  1932. @code
  1933. assert( url("?first=john&last=doe" ).set_encoded_fragment( "john doe" ).encoded_fragment() == "john%20doe" );
  1934. @endcode
  1935. @par Postconditions
  1936. @code
  1937. this->has_fragment() == true && this->fragment() == s
  1938. @endcode
  1939. @par Complexity
  1940. Linear in `this->size() + s.size()`.
  1941. @par Exception Safety
  1942. Strong guarantee.
  1943. Calls to allocate may throw.
  1944. @param s The string to set.
  1945. @par BNF
  1946. @code
  1947. fragment = *( pchar / "/" / "?" )
  1948. @endcode
  1949. @par Specification
  1950. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5"
  1951. >3.5. Fragment</a>
  1952. @see
  1953. @ref remove_fragment,
  1954. @ref set_encoded_fragment.
  1955. */
  1956. BOOST_URL_DECL
  1957. url_base&
  1958. set_fragment(
  1959. string_view s);
  1960. /** Set the fragment.
  1961. This function sets the fragment to the
  1962. specified string, which may contain
  1963. percent-escapes and which may be empty.
  1964. An empty fragment is distinct from
  1965. having no fragment.
  1966. Escapes in the string are preserved,
  1967. and reserved characters in the string
  1968. are percent-escaped in the result.
  1969. @par Example
  1970. @code
  1971. assert( url("?first=john&last=doe" ).set_encoded_fragment( "john%2Ddoe" ).fragment() == "john-doe" );
  1972. @endcode
  1973. @par Postconditions
  1974. @code
  1975. this->has_fragment() == true && this->fragment() == decode_view( s )
  1976. @endcode
  1977. @par Complexity
  1978. Linear in `this->size() + s.size()`.
  1979. @par Exception Safety
  1980. Strong guarantee.
  1981. Calls to allocate may throw.
  1982. Exceptions thrown on invalid input.
  1983. @throw system_error
  1984. `s` contains an invalid percent-encoding.
  1985. @param s The string to set.
  1986. @par BNF
  1987. @code
  1988. fragment = *( pchar / "/" / "?" )
  1989. @endcode
  1990. @par Specification
  1991. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-3.5"
  1992. >3.5. Fragment</a>
  1993. @see
  1994. @ref remove_fragment,
  1995. @ref set_fragment.
  1996. */
  1997. BOOST_URL_DECL
  1998. url_base&
  1999. set_encoded_fragment(
  2000. pct_string_view s);
  2001. //--------------------------------------------
  2002. //
  2003. // Compound Fields
  2004. //
  2005. //--------------------------------------------
  2006. /** Remove the origin component
  2007. This function removes the origin, which
  2008. consists of the scheme and authority.
  2009. @par Example
  2010. @code
  2011. assert( url( "http://www.example.com/index.htm" ).remove_origin().buffer() == "/index.htm" );
  2012. @endcode
  2013. @par Postconditions
  2014. @code
  2015. this->scheme_id() == scheme::none && this->has_authority() == false
  2016. @endcode
  2017. @par Complexity
  2018. Linear in `this->size()`.
  2019. @par Exception Safety
  2020. Throws nothing.
  2021. */
  2022. BOOST_URL_DECL
  2023. url_base&
  2024. remove_origin();
  2025. //--------------------------------------------
  2026. //
  2027. // Normalization
  2028. //
  2029. //--------------------------------------------
  2030. /** Normalize the URL components
  2031. Applies Syntax-based normalization to
  2032. all components of the URL.
  2033. @par Exception Safety
  2034. Strong guarantee.
  2035. Calls to allocate may throw.
  2036. @par Specification
  2037. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2038. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2039. */
  2040. BOOST_URL_DECL
  2041. url_base&
  2042. normalize();
  2043. /** Normalize the URL scheme
  2044. Applies Syntax-based normalization to the
  2045. URL scheme.
  2046. The scheme is normalized to lowercase.
  2047. @par Exception Safety
  2048. Strong guarantee.
  2049. Calls to allocate may throw.
  2050. @par Specification
  2051. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2052. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2053. */
  2054. BOOST_URL_DECL
  2055. url_base&
  2056. normalize_scheme();
  2057. /** Normalize the URL authority
  2058. Applies Syntax-based normalization to the
  2059. URL authority.
  2060. Percent-encoding triplets are normalized
  2061. to uppercase letters. Percent-encoded
  2062. octets that correspond to unreserved
  2063. characters are decoded.
  2064. @par Exception Safety
  2065. Strong guarantee.
  2066. Calls to allocate may throw.
  2067. @par Specification
  2068. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2069. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2070. */
  2071. BOOST_URL_DECL
  2072. url_base&
  2073. normalize_authority();
  2074. /** Normalize the URL path
  2075. Applies Syntax-based normalization to the
  2076. URL path.
  2077. Percent-encoding triplets are normalized
  2078. to uppercase letters. Percent-encoded
  2079. octets that correspond to unreserved
  2080. characters are decoded. Redundant
  2081. path-segments are removed.
  2082. @par Exception Safety
  2083. Strong guarantee.
  2084. Calls to allocate may throw.
  2085. @par Specification
  2086. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2087. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2088. */
  2089. BOOST_URL_DECL
  2090. url_base&
  2091. normalize_path();
  2092. /** Normalize the URL query
  2093. Applies Syntax-based normalization to the
  2094. URL query.
  2095. Percent-encoding triplets are normalized
  2096. to uppercase letters. Percent-encoded
  2097. octets that correspond to unreserved
  2098. characters are decoded.
  2099. @par Exception Safety
  2100. Strong guarantee.
  2101. Calls to allocate may throw.
  2102. @par Specification
  2103. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2104. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2105. */
  2106. BOOST_URL_DECL
  2107. url_base&
  2108. normalize_query();
  2109. /** Normalize the URL fragment
  2110. Applies Syntax-based normalization to the
  2111. URL fragment.
  2112. Percent-encoding triplets are normalized
  2113. to uppercase letters. Percent-encoded
  2114. octets that correspond to unreserved
  2115. characters are decoded.
  2116. @par Exception Safety
  2117. Strong guarantee.
  2118. Calls to allocate may throw.
  2119. @par Specification
  2120. @li <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2"
  2121. >6.2.2 Syntax-Based Normalization (rfc3986)</a>
  2122. */
  2123. BOOST_URL_DECL
  2124. url_base&
  2125. normalize_fragment();
  2126. //
  2127. // (end of fluent API)
  2128. //
  2129. //--------------------------------------------
  2130. //--------------------------------------------
  2131. //
  2132. // Resolution
  2133. //
  2134. //--------------------------------------------
  2135. /** Resolve a URL reference against this base URL
  2136. This function attempts to resolve a URL
  2137. reference `ref` against this base URL
  2138. in a manner similar to that of a web browser
  2139. resolving an anchor tag.
  2140. This URL must satisfy the <em>URI</em>
  2141. grammar. In other words, it must contain
  2142. a scheme.
  2143. Relative references are only usable when
  2144. in the context of a base absolute URI.
  2145. This process of resolving a relative
  2146. <em>reference</em> within the context of
  2147. a <em>base</em> URI is defined in detail
  2148. in rfc3986 (see below).
  2149. The resolution process works as if the
  2150. relative reference is appended to the base
  2151. URI and the result is normalized.
  2152. Given the input base URL, this function
  2153. resolves the relative reference
  2154. as if performing the following steps:
  2155. @li Ensure the base URI has at least a scheme
  2156. @li Normalizing the reference path
  2157. @li Merge base and reference paths
  2158. @li Normalize the merged path
  2159. This function places the result of the
  2160. resolution into this URL in place.
  2161. If an error occurs, the contents of
  2162. this URL are unspecified and a @ref result
  2163. with an @ref error_code is returned.
  2164. @par Example
  2165. @code
  2166. url base1( "/one/two/three" );
  2167. base1.resolve("four");
  2168. assert( base1.buffer() == "/one/two/four" );
  2169. url base2( "http://example.com/" )
  2170. base2.resolve("/one");
  2171. assert( base2.buffer() == "http://example.com/one" );
  2172. url base3( "http://example.com/one" );
  2173. base3.resolve("/two");
  2174. assert( base3.buffer() == "http://example.com/two" );
  2175. url base4( "http://a/b/c/d;p?q" );
  2176. base4.resolve("g#s");
  2177. assert( base4.buffer() == "http://a/b/c/g#s" );
  2178. @endcode
  2179. @par BNF
  2180. @code
  2181. absolute-URI = scheme ":" hier-part [ "?" query ]
  2182. @endcode
  2183. @par Exception Safety
  2184. Basic guarantee.
  2185. Calls to allocate may throw.
  2186. @return An empty @ref result upon success,
  2187. otherwise an error code if `!base.has_scheme()`.
  2188. @param ref The URL reference to resolve.
  2189. @par Specification
  2190. <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-5"
  2191. >5. Reference Resolution (rfc3986)</a>
  2192. @see
  2193. @ref url,
  2194. @ref url_view.
  2195. */
  2196. BOOST_URL_DECL
  2197. result<void>
  2198. resolve(
  2199. url_view_base const& ref);
  2200. friend
  2201. result<void>
  2202. resolve(
  2203. url_view_base const& base,
  2204. url_view_base const& ref,
  2205. url_base& dest);
  2206. private:
  2207. //--------------------------------------------
  2208. //
  2209. // implementation
  2210. //
  2211. //--------------------------------------------
  2212. void check_invariants() const noexcept;
  2213. char* resize_impl(int, std::size_t, op_t&);
  2214. char* resize_impl(int, int, std::size_t, op_t&);
  2215. char* shrink_impl(int, std::size_t, op_t&);
  2216. char* shrink_impl(int, int, std::size_t, op_t&);
  2217. void set_scheme_impl(string_view, urls::scheme);
  2218. char* set_user_impl(std::size_t n, op_t& op);
  2219. char* set_password_impl(std::size_t n, op_t& op);
  2220. char* set_userinfo_impl(std::size_t n, op_t& op);
  2221. char* set_host_impl(std::size_t n, op_t& op);
  2222. char* set_port_impl(std::size_t n, op_t& op);
  2223. string_view
  2224. first_segment() const noexcept;
  2225. BOOST_URL_DECL
  2226. detail::segments_iter_impl
  2227. edit_segments(
  2228. detail::segments_iter_impl const&,
  2229. detail::segments_iter_impl const&,
  2230. detail::any_segments_iter&& it0,
  2231. int absolute = -1);
  2232. BOOST_URL_DECL
  2233. auto
  2234. edit_params(
  2235. detail::params_iter_impl const&,
  2236. detail::params_iter_impl const&,
  2237. detail::any_params_iter&&) ->
  2238. detail::params_iter_impl;
  2239. BOOST_URL_DECL
  2240. result<void>
  2241. resolve_impl(
  2242. url_view_base const& base,
  2243. url_view_base const& ref);
  2244. template<class CharSet>
  2245. void normalize_octets_impl(int,
  2246. CharSet const& allowed, op_t&) noexcept;
  2247. void decoded_to_lower_impl(int id) noexcept;
  2248. void to_lower_impl(int id) noexcept;
  2249. };
  2250. //------------------------------------------------
  2251. /** Resolve a URL reference against a base URL
  2252. This function attempts to resolve a URL
  2253. reference `ref` against the base URL `base`
  2254. in a manner similar to that of a web browser
  2255. resolving an anchor tag.
  2256. The base URL must satisfy the <em>URI</em>
  2257. grammar. In other words, it must contain
  2258. a scheme.
  2259. Relative references are only usable when
  2260. in the context of a base absolute URI.
  2261. This process of resolving a relative
  2262. <em>reference</em> within the context of
  2263. a <em>base</em> URI is defined in detail
  2264. in rfc3986 (see below).
  2265. The resolution process works as if the
  2266. relative reference is appended to the base
  2267. URI and the result is normalized.
  2268. Given the input base URL, this function
  2269. resolves the relative reference
  2270. as if performing the following steps:
  2271. @li Ensure the base URI has at least a scheme
  2272. @li Normalizing the reference path
  2273. @li Merge base and reference paths
  2274. @li Normalize the merged path
  2275. This function places the result of the
  2276. resolution into `dest`, which can be
  2277. any of the url containers that inherit
  2278. from @ref url_base.
  2279. If an error occurs, the contents of
  2280. `dest` is unspecified and `ec` is set.
  2281. @par Example
  2282. @code
  2283. url dest;
  2284. error_code ec;
  2285. resolve("/one/two/three", "four", dest, ec);
  2286. assert( dest.str() == "/one/two/four" );
  2287. resolve("http://example.com/", "/one", dest, ec);
  2288. assert( dest.str() == "http://example.com/one" );
  2289. resolve("http://example.com/one", "/two", dest, ec);
  2290. assert( dest.str() == "http://example.com/two" );
  2291. resolve("http://a/b/c/d;p?q", "g#s", dest, ec);
  2292. assert( dest.str() == "http://a/b/c/g#s" );
  2293. @endcode
  2294. @par BNF
  2295. @code
  2296. absolute-URI = scheme ":" hier-part [ "?" query ]
  2297. @endcode
  2298. @par Exception Safety
  2299. Basic guarantee.
  2300. Calls to allocate may throw.
  2301. @return An empty @ref result upon success,
  2302. otherwise an error code if `!base.has_scheme()`.
  2303. @param base The base URL to resolve against.
  2304. @param ref The URL reference to resolve.
  2305. @param dest The container where the result
  2306. is written, upon success.
  2307. @par Specification
  2308. <a href="https://datatracker.ietf.org/doc/html/rfc3986#section-5"
  2309. >5. Reference Resolution (rfc3986)</a>
  2310. @see
  2311. @ref url,
  2312. @ref url_view.
  2313. */
  2314. inline
  2315. result<void>
  2316. resolve(
  2317. url_view_base const& base,
  2318. url_view_base const& ref,
  2319. url_base& dest)
  2320. {
  2321. if (&dest != &base)
  2322. dest.copy(base);
  2323. return dest.resolve(ref);
  2324. }
  2325. } // urls
  2326. } // boost
  2327. // These are here because of circular references
  2328. #include <boost/url/impl/params_ref.hpp>
  2329. #include <boost/url/impl/params_encoded_ref.hpp>
  2330. #include <boost/url/impl/segments_ref.hpp>
  2331. #include <boost/url/impl/segments_encoded_ref.hpp>
  2332. #endif