lib.rs 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  1. // Copyright 2013-2015 The rust-url developers.
  2. //
  3. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  4. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  5. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  6. // option. This file may not be copied, modified, or distributed
  7. // except according to those terms.
  8. /*!
  9. rust-url is an implementation of the [URL Standard](http://url.spec.whatwg.org/)
  10. for the [Rust](http://rust-lang.org/) programming language.
  11. # URL parsing and data structures
  12. First, URL parsing may fail for various reasons and therefore returns a `Result`.
  13. ```
  14. use url::{Url, ParseError};
  15. assert!(Url::parse("http://[:::1]") == Err(ParseError::InvalidIpv6Address))
  16. ```
  17. Let’s parse a valid URL and look at its components.
  18. ```
  19. use url::{Url, Host, Position};
  20. # use url::ParseError;
  21. # fn run() -> Result<(), ParseError> {
  22. let issue_list_url = Url::parse(
  23. "https://github.com/rust-lang/rust/issues?labels=E-easy&state=open"
  24. )?;
  25. assert!(issue_list_url.scheme() == "https");
  26. assert!(issue_list_url.username() == "");
  27. assert!(issue_list_url.password() == None);
  28. assert!(issue_list_url.host_str() == Some("github.com"));
  29. assert!(issue_list_url.host() == Some(Host::Domain("github.com")));
  30. assert!(issue_list_url.port() == None);
  31. assert!(issue_list_url.path() == "/rust-lang/rust/issues");
  32. assert!(issue_list_url.path_segments().map(|c| c.collect::<Vec<_>>()) ==
  33. Some(vec!["rust-lang", "rust", "issues"]));
  34. assert!(issue_list_url.query() == Some("labels=E-easy&state=open"));
  35. assert!(&issue_list_url[Position::BeforePath..] == "/rust-lang/rust/issues?labels=E-easy&state=open");
  36. assert!(issue_list_url.fragment() == None);
  37. assert!(!issue_list_url.cannot_be_a_base());
  38. # Ok(())
  39. # }
  40. # run().unwrap();
  41. ```
  42. Some URLs are said to be *cannot-be-a-base*:
  43. they don’t have a username, password, host, or port,
  44. and their "path" is an arbitrary string rather than slash-separated segments:
  45. ```
  46. use url::Url;
  47. # use url::ParseError;
  48. # fn run() -> Result<(), ParseError> {
  49. let data_url = Url::parse("data:text/plain,Hello?World#")?;
  50. assert!(data_url.cannot_be_a_base());
  51. assert!(data_url.scheme() == "data");
  52. assert!(data_url.path() == "text/plain,Hello");
  53. assert!(data_url.path_segments().is_none());
  54. assert!(data_url.query() == Some("World"));
  55. assert!(data_url.fragment() == Some(""));
  56. # Ok(())
  57. # }
  58. # run().unwrap();
  59. ```
  60. ## Serde
  61. Enable the `serde` feature to include `Deserialize` and `Serialize` implementations for `url::Url`.
  62. # Base URL
  63. Many contexts allow URL *references* that can be relative to a *base URL*:
  64. ```html
  65. <link rel="stylesheet" href="../main.css">
  66. ```
  67. Since parsed URLs are absolute, giving a base is required for parsing relative URLs:
  68. ```
  69. use url::{Url, ParseError};
  70. assert!(Url::parse("../main.css") == Err(ParseError::RelativeUrlWithoutBase))
  71. ```
  72. Use the `join` method on an `Url` to use it as a base URL:
  73. ```
  74. use url::Url;
  75. # use url::ParseError;
  76. # fn run() -> Result<(), ParseError> {
  77. let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html")?;
  78. let css_url = this_document.join("../main.css")?;
  79. assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");
  80. # Ok(())
  81. # }
  82. # run().unwrap();
  83. ```
  84. # Feature: `serde`
  85. If you enable the `serde` feature, [`Url`](struct.Url.html) will implement
  86. [`serde::Serialize`](https://docs.rs/serde/1/serde/trait.Serialize.html) and
  87. [`serde::Deserialize`](https://docs.rs/serde/1/serde/trait.Deserialize.html).
  88. See [serde documentation](https://serde.rs) for more information.
  89. ```toml
  90. url = { version = "2", features = ["serde"] }
  91. ```
  92. # Feature: `idna`
  93. You can opt out [idna](https://en.wikipedia.org/wiki/Internationalized_domain_name) support
  94. to reduce final binary size.
  95. ```toml
  96. url = { version = "2", default-features = false }
  97. ```
  98. */
  99. #![doc(html_root_url = "https://docs.rs/url/2.2.2")]
  100. #[macro_use]
  101. extern crate matches;
  102. pub use form_urlencoded;
  103. #[cfg(feature = "serde")]
  104. extern crate serde;
  105. use crate::host::HostInternal;
  106. use crate::parser::{to_u32, Context, Parser, SchemeType, PATH_SEGMENT, USERINFO};
  107. use percent_encoding::{percent_decode, percent_encode, utf8_percent_encode};
  108. use std::borrow::Borrow;
  109. use std::cmp;
  110. use std::fmt::{self, Write};
  111. use std::hash;
  112. use std::io;
  113. use std::mem;
  114. use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
  115. use std::ops::{Range, RangeFrom, RangeTo};
  116. use std::path::{Path, PathBuf};
  117. use std::str;
  118. use std::convert::TryFrom;
  119. pub use crate::host::Host;
  120. pub use crate::origin::{OpaqueOrigin, Origin};
  121. pub use crate::parser::{ParseError, SyntaxViolation};
  122. pub use crate::path_segments::PathSegmentsMut;
  123. pub use crate::slicing::Position;
  124. pub use form_urlencoded::EncodingOverride;
  125. mod host;
  126. mod origin;
  127. mod parser;
  128. mod path_segments;
  129. mod slicing;
  130. #[doc(hidden)]
  131. pub mod quirks;
  132. /// A parsed URL record.
  133. #[derive(Clone)]
  134. pub struct Url {
  135. /// Syntax in pseudo-BNF:
  136. ///
  137. /// url = scheme ":" [ hierarchical | non-hierarchical ] [ "?" query ]? [ "#" fragment ]?
  138. /// non-hierarchical = non-hierarchical-path
  139. /// non-hierarchical-path = /* Does not start with "/" */
  140. /// hierarchical = authority? hierarchical-path
  141. /// authority = "//" userinfo? host [ ":" port ]?
  142. /// userinfo = username [ ":" password ]? "@"
  143. /// hierarchical-path = [ "/" path-segment ]+
  144. serialization: String,
  145. // Components
  146. scheme_end: u32, // Before ':'
  147. username_end: u32, // Before ':' (if a password is given) or '@' (if not)
  148. host_start: u32,
  149. host_end: u32,
  150. host: HostInternal,
  151. port: Option<u16>,
  152. path_start: u32, // Before initial '/', if any
  153. query_start: Option<u32>, // Before '?', unlike Position::QueryStart
  154. fragment_start: Option<u32>, // Before '#', unlike Position::FragmentStart
  155. }
  156. /// Full configuration for the URL parser.
  157. #[derive(Copy, Clone)]
  158. pub struct ParseOptions<'a> {
  159. base_url: Option<&'a Url>,
  160. encoding_override: EncodingOverride<'a>,
  161. violation_fn: Option<&'a dyn Fn(SyntaxViolation)>,
  162. }
  163. impl<'a> ParseOptions<'a> {
  164. /// Change the base URL
  165. pub fn base_url(mut self, new: Option<&'a Url>) -> Self {
  166. self.base_url = new;
  167. self
  168. }
  169. /// Override the character encoding of query strings.
  170. /// This is a legacy concept only relevant for HTML.
  171. pub fn encoding_override(mut self, new: EncodingOverride<'a>) -> Self {
  172. self.encoding_override = new;
  173. self
  174. }
  175. /// Call the provided function or closure for a non-fatal `SyntaxViolation`
  176. /// when it occurs during parsing. Note that since the provided function is
  177. /// `Fn`, the caller might need to utilize _interior mutability_, such as with
  178. /// a `RefCell`, to collect the violations.
  179. ///
  180. /// ## Example
  181. /// ```
  182. /// use std::cell::RefCell;
  183. /// use url::{Url, SyntaxViolation};
  184. /// # use url::ParseError;
  185. /// # fn run() -> Result<(), url::ParseError> {
  186. /// let violations = RefCell::new(Vec::new());
  187. /// let url = Url::options()
  188. /// .syntax_violation_callback(Some(&|v| violations.borrow_mut().push(v)))
  189. /// .parse("https:////example.com")?;
  190. /// assert_eq!(url.as_str(), "https://example.com/");
  191. /// assert_eq!(violations.into_inner(),
  192. /// vec!(SyntaxViolation::ExpectedDoubleSlash));
  193. /// # Ok(())
  194. /// # }
  195. /// # run().unwrap();
  196. /// ```
  197. pub fn syntax_violation_callback(mut self, new: Option<&'a dyn Fn(SyntaxViolation)>) -> Self {
  198. self.violation_fn = new;
  199. self
  200. }
  201. /// Parse an URL string with the configuration so far.
  202. pub fn parse(self, input: &str) -> Result<Url, crate::ParseError> {
  203. Parser {
  204. serialization: String::with_capacity(input.len()),
  205. base_url: self.base_url,
  206. query_encoding_override: self.encoding_override,
  207. violation_fn: self.violation_fn,
  208. context: Context::UrlParser,
  209. }
  210. .parse_url(input)
  211. }
  212. }
  213. impl Url {
  214. /// Parse an absolute URL from a string.
  215. ///
  216. /// # Examples
  217. ///
  218. /// ```rust
  219. /// use url::Url;
  220. /// # use url::ParseError;
  221. ///
  222. /// # fn run() -> Result<(), ParseError> {
  223. /// let url = Url::parse("https://example.net")?;
  224. /// # Ok(())
  225. /// # }
  226. /// # run().unwrap();
  227. /// ```
  228. ///
  229. /// # Errors
  230. ///
  231. /// If the function can not parse an absolute URL from the given string,
  232. /// a [`ParseError`] variant will be returned.
  233. ///
  234. /// [`ParseError`]: enum.ParseError.html
  235. #[inline]
  236. pub fn parse(input: &str) -> Result<Url, crate::ParseError> {
  237. Url::options().parse(input)
  238. }
  239. /// Parse an absolute URL from a string and add params to its query string.
  240. ///
  241. /// Existing params are not removed.
  242. ///
  243. /// # Examples
  244. ///
  245. /// ```rust
  246. /// use url::Url;
  247. /// # use url::ParseError;
  248. ///
  249. /// # fn run() -> Result<(), ParseError> {
  250. /// let url = Url::parse_with_params("https://example.net?dont=clobberme",
  251. /// &[("lang", "rust"), ("browser", "servo")])?;
  252. /// assert_eq!("https://example.net/?dont=clobberme&lang=rust&browser=servo", url.as_str());
  253. /// # Ok(())
  254. /// # }
  255. /// # run().unwrap();
  256. /// ```
  257. ///
  258. /// # Errors
  259. ///
  260. /// If the function can not parse an absolute URL from the given string,
  261. /// a [`ParseError`] variant will be returned.
  262. ///
  263. /// [`ParseError`]: enum.ParseError.html
  264. #[inline]
  265. pub fn parse_with_params<I, K, V>(input: &str, iter: I) -> Result<Url, crate::ParseError>
  266. where
  267. I: IntoIterator,
  268. I::Item: Borrow<(K, V)>,
  269. K: AsRef<str>,
  270. V: AsRef<str>,
  271. {
  272. let mut url = Url::options().parse(input);
  273. if let Ok(ref mut url) = url {
  274. url.query_pairs_mut().extend_pairs(iter);
  275. }
  276. url
  277. }
  278. /// Parse a string as an URL, with this URL as the base URL.
  279. ///
  280. /// The inverse of this is [`make_relative`].
  281. ///
  282. /// Note: a trailing slash is significant.
  283. /// Without it, the last path component is considered to be a “file” name
  284. /// to be removed to get at the “directory” that is used as the base:
  285. ///
  286. /// # Examples
  287. ///
  288. /// ```rust
  289. /// use url::Url;
  290. /// # use url::ParseError;
  291. ///
  292. /// # fn run() -> Result<(), ParseError> {
  293. /// let base = Url::parse("https://example.net/a/b.html")?;
  294. /// let url = base.join("c.png")?;
  295. /// assert_eq!(url.as_str(), "https://example.net/a/c.png"); // Not /a/b.html/c.png
  296. ///
  297. /// let base = Url::parse("https://example.net/a/b/")?;
  298. /// let url = base.join("c.png")?;
  299. /// assert_eq!(url.as_str(), "https://example.net/a/b/c.png");
  300. /// # Ok(())
  301. /// # }
  302. /// # run().unwrap();
  303. /// ```
  304. ///
  305. /// # Errors
  306. ///
  307. /// If the function can not parse an URL from the given string
  308. /// with this URL as the base URL, a [`ParseError`] variant will be returned.
  309. ///
  310. /// [`ParseError`]: enum.ParseError.html
  311. /// [`make_relative`]: #method.make_relative
  312. #[inline]
  313. pub fn join(&self, input: &str) -> Result<Url, crate::ParseError> {
  314. Url::options().base_url(Some(self)).parse(input)
  315. }
  316. /// Creates a relative URL if possible, with this URL as the base URL.
  317. ///
  318. /// This is the inverse of [`join`].
  319. ///
  320. /// # Examples
  321. ///
  322. /// ```rust
  323. /// use url::Url;
  324. /// # use url::ParseError;
  325. ///
  326. /// # fn run() -> Result<(), ParseError> {
  327. /// let base = Url::parse("https://example.net/a/b.html")?;
  328. /// let url = Url::parse("https://example.net/a/c.png")?;
  329. /// let relative = base.make_relative(&url);
  330. /// assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
  331. ///
  332. /// let base = Url::parse("https://example.net/a/b/")?;
  333. /// let url = Url::parse("https://example.net/a/b/c.png")?;
  334. /// let relative = base.make_relative(&url);
  335. /// assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("c.png"));
  336. ///
  337. /// let base = Url::parse("https://example.net/a/b/")?;
  338. /// let url = Url::parse("https://example.net/a/d/c.png")?;
  339. /// let relative = base.make_relative(&url);
  340. /// assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("../d/c.png"));
  341. ///
  342. /// let base = Url::parse("https://example.net/a/b.html?c=d")?;
  343. /// let url = Url::parse("https://example.net/a/b.html?e=f")?;
  344. /// let relative = base.make_relative(&url);
  345. /// assert_eq!(relative.as_ref().map(|s| s.as_str()), Some("?e=f"));
  346. /// # Ok(())
  347. /// # }
  348. /// # run().unwrap();
  349. /// ```
  350. ///
  351. /// # Errors
  352. ///
  353. /// If this URL can't be a base for the given URL, `None` is returned.
  354. /// This is for example the case if the scheme, host or port are not the same.
  355. ///
  356. /// [`join`]: #method.join
  357. pub fn make_relative(&self, url: &Url) -> Option<String> {
  358. if self.cannot_be_a_base() {
  359. return None;
  360. }
  361. // Scheme, host and port need to be the same
  362. if self.scheme() != url.scheme() || self.host() != url.host() || self.port() != url.port() {
  363. return None;
  364. }
  365. // We ignore username/password at this point
  366. // The path has to be transformed
  367. let mut relative = String::new();
  368. // Extract the filename of both URIs, these need to be handled separately
  369. fn extract_path_filename(s: &str) -> (&str, &str) {
  370. let last_slash_idx = s.rfind('/').unwrap_or(0);
  371. let (path, filename) = s.split_at(last_slash_idx);
  372. if filename.is_empty() {
  373. (path, "")
  374. } else {
  375. (path, &filename[1..])
  376. }
  377. }
  378. let (base_path, base_filename) = extract_path_filename(self.path());
  379. let (url_path, url_filename) = extract_path_filename(url.path());
  380. let mut base_path = base_path.split('/').peekable();
  381. let mut url_path = url_path.split('/').peekable();
  382. // Skip over the common prefix
  383. while base_path.peek().is_some() && base_path.peek() == url_path.peek() {
  384. base_path.next();
  385. url_path.next();
  386. }
  387. // Add `..` segments for the remainder of the base path
  388. for base_path_segment in base_path {
  389. // Skip empty last segments
  390. if base_path_segment.is_empty() {
  391. break;
  392. }
  393. if !relative.is_empty() {
  394. relative.push('/');
  395. }
  396. relative.push_str("..");
  397. }
  398. // Append the remainder of the other URI
  399. for url_path_segment in url_path {
  400. if !relative.is_empty() {
  401. relative.push('/');
  402. }
  403. relative.push_str(url_path_segment);
  404. }
  405. // Add the filename if they are not the same
  406. if base_filename != url_filename {
  407. // If the URIs filename is empty this means that it was a directory
  408. // so we'll have to append a '/'.
  409. //
  410. // Otherwise append it directly as the new filename.
  411. if url_filename.is_empty() {
  412. relative.push('/');
  413. } else {
  414. if !relative.is_empty() {
  415. relative.push('/');
  416. }
  417. relative.push_str(url_filename);
  418. }
  419. }
  420. // Query and fragment are only taken from the other URI
  421. if let Some(query) = url.query() {
  422. relative.push('?');
  423. relative.push_str(query);
  424. }
  425. if let Some(fragment) = url.fragment() {
  426. relative.push('#');
  427. relative.push_str(fragment);
  428. }
  429. Some(relative)
  430. }
  431. /// Return a default `ParseOptions` that can fully configure the URL parser.
  432. ///
  433. /// # Examples
  434. ///
  435. /// Get default `ParseOptions`, then change base url
  436. ///
  437. /// ```rust
  438. /// use url::Url;
  439. /// # use url::ParseError;
  440. /// # fn run() -> Result<(), ParseError> {
  441. /// let options = Url::options();
  442. /// let api = Url::parse("https://api.example.com")?;
  443. /// let base_url = options.base_url(Some(&api));
  444. /// let version_url = base_url.parse("version.json")?;
  445. /// assert_eq!(version_url.as_str(), "https://api.example.com/version.json");
  446. /// # Ok(())
  447. /// # }
  448. /// # run().unwrap();
  449. /// ```
  450. pub fn options<'a>() -> ParseOptions<'a> {
  451. ParseOptions {
  452. base_url: None,
  453. encoding_override: None,
  454. violation_fn: None,
  455. }
  456. }
  457. /// Return the serialization of this URL.
  458. ///
  459. /// This is fast since that serialization is already stored in the `Url` struct.
  460. ///
  461. /// # Examples
  462. ///
  463. /// ```rust
  464. /// use url::Url;
  465. /// # use url::ParseError;
  466. ///
  467. /// # fn run() -> Result<(), ParseError> {
  468. /// let url_str = "https://example.net/";
  469. /// let url = Url::parse(url_str)?;
  470. /// assert_eq!(url.as_str(), url_str);
  471. /// # Ok(())
  472. /// # }
  473. /// # run().unwrap();
  474. /// ```
  475. #[inline]
  476. pub fn as_str(&self) -> &str {
  477. &self.serialization
  478. }
  479. /// Return the serialization of this URL.
  480. ///
  481. /// This consumes the `Url` and takes ownership of the `String` stored in it.
  482. ///
  483. /// # Examples
  484. ///
  485. /// ```rust
  486. /// use url::Url;
  487. /// # use url::ParseError;
  488. ///
  489. /// # fn run() -> Result<(), ParseError> {
  490. /// let url_str = "https://example.net/";
  491. /// let url = Url::parse(url_str)?;
  492. /// assert_eq!(String::from(url), url_str);
  493. /// # Ok(())
  494. /// # }
  495. /// # run().unwrap();
  496. /// ```
  497. #[inline]
  498. #[deprecated(since = "2.3.0", note = "use Into<String>")]
  499. pub fn into_string(self) -> String {
  500. self.into()
  501. }
  502. /// For internal testing, not part of the public API.
  503. ///
  504. /// Methods of the `Url` struct assume a number of invariants.
  505. /// This checks each of these invariants and panic if one is not met.
  506. /// This is for testing rust-url itself.
  507. #[doc(hidden)]
  508. pub fn check_invariants(&self) -> Result<(), String> {
  509. macro_rules! assert {
  510. ($x: expr) => {
  511. if !$x {
  512. return Err(format!(
  513. "!( {} ) for URL {:?}",
  514. stringify!($x),
  515. self.serialization
  516. ));
  517. }
  518. };
  519. }
  520. macro_rules! assert_eq {
  521. ($a: expr, $b: expr) => {
  522. {
  523. let a = $a;
  524. let b = $b;
  525. if a != b {
  526. return Err(format!("{:?} != {:?} ({} != {}) for URL {:?}",
  527. a, b, stringify!($a), stringify!($b),
  528. self.serialization))
  529. }
  530. }
  531. }
  532. }
  533. assert!(self.scheme_end >= 1);
  534. assert!(matches!(self.byte_at(0), b'a'..=b'z' | b'A'..=b'Z'));
  535. assert!(self
  536. .slice(1..self.scheme_end)
  537. .chars()
  538. .all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.')));
  539. assert_eq!(self.byte_at(self.scheme_end), b':');
  540. if self.slice(self.scheme_end + 1..).starts_with("//") {
  541. // URL with authority
  542. if self.username_end != self.serialization.len() as u32 {
  543. match self.byte_at(self.username_end) {
  544. b':' => {
  545. assert!(self.host_start >= self.username_end + 2);
  546. assert_eq!(self.byte_at(self.host_start - 1), b'@');
  547. }
  548. b'@' => assert!(self.host_start == self.username_end + 1),
  549. _ => assert_eq!(self.username_end, self.scheme_end + 3),
  550. }
  551. }
  552. assert!(self.host_start >= self.username_end);
  553. assert!(self.host_end >= self.host_start);
  554. let host_str = self.slice(self.host_start..self.host_end);
  555. match self.host {
  556. HostInternal::None => assert_eq!(host_str, ""),
  557. HostInternal::Ipv4(address) => assert_eq!(host_str, address.to_string()),
  558. HostInternal::Ipv6(address) => {
  559. let h: Host<String> = Host::Ipv6(address);
  560. assert_eq!(host_str, h.to_string())
  561. }
  562. HostInternal::Domain => {
  563. if SchemeType::from(self.scheme()).is_special() {
  564. assert!(!host_str.is_empty())
  565. }
  566. }
  567. }
  568. if self.path_start == self.host_end {
  569. assert_eq!(self.port, None);
  570. } else {
  571. assert_eq!(self.byte_at(self.host_end), b':');
  572. let port_str = self.slice(self.host_end + 1..self.path_start);
  573. assert_eq!(
  574. self.port,
  575. Some(port_str.parse::<u16>().expect("Couldn't parse port?"))
  576. );
  577. }
  578. assert!(
  579. self.path_start as usize == self.serialization.len()
  580. || matches!(self.byte_at(self.path_start), b'/' | b'#' | b'?')
  581. );
  582. } else {
  583. // Anarchist URL (no authority)
  584. assert_eq!(self.username_end, self.scheme_end + 1);
  585. assert_eq!(self.host_start, self.scheme_end + 1);
  586. assert_eq!(self.host_end, self.scheme_end + 1);
  587. assert_eq!(self.host, HostInternal::None);
  588. assert_eq!(self.port, None);
  589. assert_eq!(self.path_start, self.scheme_end + 1);
  590. }
  591. if let Some(start) = self.query_start {
  592. assert!(start >= self.path_start);
  593. assert_eq!(self.byte_at(start), b'?');
  594. }
  595. if let Some(start) = self.fragment_start {
  596. assert!(start >= self.path_start);
  597. assert_eq!(self.byte_at(start), b'#');
  598. }
  599. if let (Some(query_start), Some(fragment_start)) = (self.query_start, self.fragment_start) {
  600. assert!(fragment_start > query_start);
  601. }
  602. let other = Url::parse(self.as_str()).expect("Failed to parse myself?");
  603. assert_eq!(&self.serialization, &other.serialization);
  604. assert_eq!(self.scheme_end, other.scheme_end);
  605. assert_eq!(self.username_end, other.username_end);
  606. assert_eq!(self.host_start, other.host_start);
  607. assert_eq!(self.host_end, other.host_end);
  608. assert!(
  609. self.host == other.host ||
  610. // XXX No host round-trips to empty host.
  611. // See https://github.com/whatwg/url/issues/79
  612. (self.host_str(), other.host_str()) == (None, Some(""))
  613. );
  614. assert_eq!(self.port, other.port);
  615. assert_eq!(self.path_start, other.path_start);
  616. assert_eq!(self.query_start, other.query_start);
  617. assert_eq!(self.fragment_start, other.fragment_start);
  618. Ok(())
  619. }
  620. /// Return the origin of this URL (<https://url.spec.whatwg.org/#origin>)
  621. ///
  622. /// Note: this returns an opaque origin for `file:` URLs, which causes
  623. /// `url.origin() != url.origin()`.
  624. ///
  625. /// # Examples
  626. ///
  627. /// URL with `ftp` scheme:
  628. ///
  629. /// ```rust
  630. /// use url::{Host, Origin, Url};
  631. /// # use url::ParseError;
  632. ///
  633. /// # fn run() -> Result<(), ParseError> {
  634. /// let url = Url::parse("ftp://example.com/foo")?;
  635. /// assert_eq!(url.origin(),
  636. /// Origin::Tuple("ftp".into(),
  637. /// Host::Domain("example.com".into()),
  638. /// 21));
  639. /// # Ok(())
  640. /// # }
  641. /// # run().unwrap();
  642. /// ```
  643. ///
  644. /// URL with `blob` scheme:
  645. ///
  646. /// ```rust
  647. /// use url::{Host, Origin, Url};
  648. /// # use url::ParseError;
  649. ///
  650. /// # fn run() -> Result<(), ParseError> {
  651. /// let url = Url::parse("blob:https://example.com/foo")?;
  652. /// assert_eq!(url.origin(),
  653. /// Origin::Tuple("https".into(),
  654. /// Host::Domain("example.com".into()),
  655. /// 443));
  656. /// # Ok(())
  657. /// # }
  658. /// # run().unwrap();
  659. /// ```
  660. ///
  661. /// URL with `file` scheme:
  662. ///
  663. /// ```rust
  664. /// use url::{Host, Origin, Url};
  665. /// # use url::ParseError;
  666. ///
  667. /// # fn run() -> Result<(), ParseError> {
  668. /// let url = Url::parse("file:///tmp/foo")?;
  669. /// assert!(!url.origin().is_tuple());
  670. ///
  671. /// let other_url = Url::parse("file:///tmp/foo")?;
  672. /// assert!(url.origin() != other_url.origin());
  673. /// # Ok(())
  674. /// # }
  675. /// # run().unwrap();
  676. /// ```
  677. ///
  678. /// URL with other scheme:
  679. ///
  680. /// ```rust
  681. /// use url::{Host, Origin, Url};
  682. /// # use url::ParseError;
  683. ///
  684. /// # fn run() -> Result<(), ParseError> {
  685. /// let url = Url::parse("foo:bar")?;
  686. /// assert!(!url.origin().is_tuple());
  687. /// # Ok(())
  688. /// # }
  689. /// # run().unwrap();
  690. /// ```
  691. #[inline]
  692. pub fn origin(&self) -> Origin {
  693. origin::url_origin(self)
  694. }
  695. /// Return the scheme of this URL, lower-cased, as an ASCII string without the ':' delimiter.
  696. ///
  697. /// # Examples
  698. ///
  699. /// ```
  700. /// use url::Url;
  701. /// # use url::ParseError;
  702. ///
  703. /// # fn run() -> Result<(), ParseError> {
  704. /// let url = Url::parse("file:///tmp/foo")?;
  705. /// assert_eq!(url.scheme(), "file");
  706. /// # Ok(())
  707. /// # }
  708. /// # run().unwrap();
  709. /// ```
  710. #[inline]
  711. pub fn scheme(&self) -> &str {
  712. self.slice(..self.scheme_end)
  713. }
  714. /// Return whether the URL has an 'authority',
  715. /// which can contain a username, password, host, and port number.
  716. ///
  717. /// URLs that do *not* are either path-only like `unix:/run/foo.socket`
  718. /// or cannot-be-a-base like `data:text/plain,Stuff`.
  719. ///
  720. /// # Examples
  721. ///
  722. /// ```
  723. /// use url::Url;
  724. /// # use url::ParseError;
  725. ///
  726. /// # fn run() -> Result<(), ParseError> {
  727. /// let url = Url::parse("ftp://rms@example.com")?;
  728. /// assert!(url.has_authority());
  729. ///
  730. /// let url = Url::parse("unix:/run/foo.socket")?;
  731. /// assert!(!url.has_authority());
  732. ///
  733. /// let url = Url::parse("data:text/plain,Stuff")?;
  734. /// assert!(!url.has_authority());
  735. /// # Ok(())
  736. /// # }
  737. /// # run().unwrap();
  738. /// ```
  739. #[inline]
  740. pub fn has_authority(&self) -> bool {
  741. debug_assert!(self.byte_at(self.scheme_end) == b':');
  742. self.slice(self.scheme_end..).starts_with("://")
  743. }
  744. /// Return whether this URL is a cannot-be-a-base URL,
  745. /// meaning that parsing a relative URL string with this URL as the base will return an error.
  746. ///
  747. /// This is the case if the scheme and `:` delimiter are not followed by a `/` slash,
  748. /// as is typically the case of `data:` and `mailto:` URLs.
  749. ///
  750. /// # Examples
  751. ///
  752. /// ```
  753. /// use url::Url;
  754. /// # use url::ParseError;
  755. ///
  756. /// # fn run() -> Result<(), ParseError> {
  757. /// let url = Url::parse("ftp://rms@example.com")?;
  758. /// assert!(!url.cannot_be_a_base());
  759. ///
  760. /// let url = Url::parse("unix:/run/foo.socket")?;
  761. /// assert!(!url.cannot_be_a_base());
  762. ///
  763. /// let url = Url::parse("data:text/plain,Stuff")?;
  764. /// assert!(url.cannot_be_a_base());
  765. /// # Ok(())
  766. /// # }
  767. /// # run().unwrap();
  768. /// ```
  769. #[inline]
  770. pub fn cannot_be_a_base(&self) -> bool {
  771. !self.slice(self.scheme_end + 1..).starts_with('/')
  772. }
  773. /// Return the username for this URL (typically the empty string)
  774. /// as a percent-encoded ASCII string.
  775. ///
  776. /// # Examples
  777. ///
  778. /// ```
  779. /// use url::Url;
  780. /// # use url::ParseError;
  781. ///
  782. /// # fn run() -> Result<(), ParseError> {
  783. /// let url = Url::parse("ftp://rms@example.com")?;
  784. /// assert_eq!(url.username(), "rms");
  785. ///
  786. /// let url = Url::parse("ftp://:secret123@example.com")?;
  787. /// assert_eq!(url.username(), "");
  788. ///
  789. /// let url = Url::parse("https://example.com")?;
  790. /// assert_eq!(url.username(), "");
  791. /// # Ok(())
  792. /// # }
  793. /// # run().unwrap();
  794. /// ```
  795. pub fn username(&self) -> &str {
  796. let scheme_separator_len = "://".len() as u32;
  797. if self.has_authority() && self.username_end > self.scheme_end + scheme_separator_len {
  798. self.slice(self.scheme_end + scheme_separator_len..self.username_end)
  799. } else {
  800. ""
  801. }
  802. }
  803. /// Return the password for this URL, if any, as a percent-encoded ASCII string.
  804. ///
  805. /// # Examples
  806. ///
  807. /// ```
  808. /// use url::Url;
  809. /// # use url::ParseError;
  810. ///
  811. /// # fn run() -> Result<(), ParseError> {
  812. /// let url = Url::parse("ftp://rms:secret123@example.com")?;
  813. /// assert_eq!(url.password(), Some("secret123"));
  814. ///
  815. /// let url = Url::parse("ftp://:secret123@example.com")?;
  816. /// assert_eq!(url.password(), Some("secret123"));
  817. ///
  818. /// let url = Url::parse("ftp://rms@example.com")?;
  819. /// assert_eq!(url.password(), None);
  820. ///
  821. /// let url = Url::parse("https://example.com")?;
  822. /// assert_eq!(url.password(), None);
  823. /// # Ok(())
  824. /// # }
  825. /// # run().unwrap();
  826. /// ```
  827. pub fn password(&self) -> Option<&str> {
  828. // This ':' is not the one marking a port number since a host can not be empty.
  829. // (Except for file: URLs, which do not have port numbers.)
  830. if self.has_authority()
  831. && self.username_end != self.serialization.len() as u32
  832. && self.byte_at(self.username_end) == b':'
  833. {
  834. debug_assert!(self.byte_at(self.host_start - 1) == b'@');
  835. Some(self.slice(self.username_end + 1..self.host_start - 1))
  836. } else {
  837. None
  838. }
  839. }
  840. /// Equivalent to `url.host().is_some()`.
  841. ///
  842. /// # Examples
  843. ///
  844. /// ```
  845. /// use url::Url;
  846. /// # use url::ParseError;
  847. ///
  848. /// # fn run() -> Result<(), ParseError> {
  849. /// let url = Url::parse("ftp://rms@example.com")?;
  850. /// assert!(url.has_host());
  851. ///
  852. /// let url = Url::parse("unix:/run/foo.socket")?;
  853. /// assert!(!url.has_host());
  854. ///
  855. /// let url = Url::parse("data:text/plain,Stuff")?;
  856. /// assert!(!url.has_host());
  857. /// # Ok(())
  858. /// # }
  859. /// # run().unwrap();
  860. /// ```
  861. pub fn has_host(&self) -> bool {
  862. !matches!(self.host, HostInternal::None)
  863. }
  864. /// Return the string representation of the host (domain or IP address) for this URL, if any.
  865. ///
  866. /// Non-ASCII domains are punycode-encoded per IDNA if this is the host
  867. /// of a special URL, or percent encoded for non-special URLs.
  868. /// IPv6 addresses are given between `[` and `]` brackets.
  869. ///
  870. /// Cannot-be-a-base URLs (typical of `data:` and `mailto:`) and some `file:` URLs
  871. /// don’t have a host.
  872. ///
  873. /// See also the `host` method.
  874. ///
  875. /// # Examples
  876. ///
  877. /// ```
  878. /// use url::Url;
  879. /// # use url::ParseError;
  880. ///
  881. /// # fn run() -> Result<(), ParseError> {
  882. /// let url = Url::parse("https://127.0.0.1/index.html")?;
  883. /// assert_eq!(url.host_str(), Some("127.0.0.1"));
  884. ///
  885. /// let url = Url::parse("ftp://rms@example.com")?;
  886. /// assert_eq!(url.host_str(), Some("example.com"));
  887. ///
  888. /// let url = Url::parse("unix:/run/foo.socket")?;
  889. /// assert_eq!(url.host_str(), None);
  890. ///
  891. /// let url = Url::parse("data:text/plain,Stuff")?;
  892. /// assert_eq!(url.host_str(), None);
  893. /// # Ok(())
  894. /// # }
  895. /// # run().unwrap();
  896. /// ```
  897. pub fn host_str(&self) -> Option<&str> {
  898. if self.has_host() {
  899. Some(self.slice(self.host_start..self.host_end))
  900. } else {
  901. None
  902. }
  903. }
  904. /// Return the parsed representation of the host for this URL.
  905. /// Non-ASCII domain labels are punycode-encoded per IDNA if this is the host
  906. /// of a special URL, or percent encoded for non-special URLs.
  907. ///
  908. /// Cannot-be-a-base URLs (typical of `data:` and `mailto:`) and some `file:` URLs
  909. /// don’t have a host.
  910. ///
  911. /// See also the `host_str` method.
  912. ///
  913. /// # Examples
  914. ///
  915. /// ```
  916. /// use url::Url;
  917. /// # use url::ParseError;
  918. ///
  919. /// # fn run() -> Result<(), ParseError> {
  920. /// let url = Url::parse("https://127.0.0.1/index.html")?;
  921. /// assert!(url.host().is_some());
  922. ///
  923. /// let url = Url::parse("ftp://rms@example.com")?;
  924. /// assert!(url.host().is_some());
  925. ///
  926. /// let url = Url::parse("unix:/run/foo.socket")?;
  927. /// assert!(url.host().is_none());
  928. ///
  929. /// let url = Url::parse("data:text/plain,Stuff")?;
  930. /// assert!(url.host().is_none());
  931. /// # Ok(())
  932. /// # }
  933. /// # run().unwrap();
  934. /// ```
  935. pub fn host(&self) -> Option<Host<&str>> {
  936. match self.host {
  937. HostInternal::None => None,
  938. HostInternal::Domain => Some(Host::Domain(self.slice(self.host_start..self.host_end))),
  939. HostInternal::Ipv4(address) => Some(Host::Ipv4(address)),
  940. HostInternal::Ipv6(address) => Some(Host::Ipv6(address)),
  941. }
  942. }
  943. /// If this URL has a host and it is a domain name (not an IP address), return it.
  944. /// Non-ASCII domains are punycode-encoded per IDNA if this is the host
  945. /// of a special URL, or percent encoded for non-special URLs.
  946. ///
  947. /// # Examples
  948. ///
  949. /// ```
  950. /// use url::Url;
  951. /// # use url::ParseError;
  952. ///
  953. /// # fn run() -> Result<(), ParseError> {
  954. /// let url = Url::parse("https://127.0.0.1/")?;
  955. /// assert_eq!(url.domain(), None);
  956. ///
  957. /// let url = Url::parse("mailto:rms@example.net")?;
  958. /// assert_eq!(url.domain(), None);
  959. ///
  960. /// let url = Url::parse("https://example.com/")?;
  961. /// assert_eq!(url.domain(), Some("example.com"));
  962. /// # Ok(())
  963. /// # }
  964. /// # run().unwrap();
  965. /// ```
  966. pub fn domain(&self) -> Option<&str> {
  967. match self.host {
  968. HostInternal::Domain => Some(self.slice(self.host_start..self.host_end)),
  969. _ => None,
  970. }
  971. }
  972. /// Return the port number for this URL, if any.
  973. ///
  974. /// Note that default port numbers are never reflected by the serialization,
  975. /// use the `port_or_known_default()` method if you want a default port number returned.
  976. ///
  977. /// # Examples
  978. ///
  979. /// ```
  980. /// use url::Url;
  981. /// # use url::ParseError;
  982. ///
  983. /// # fn run() -> Result<(), ParseError> {
  984. /// let url = Url::parse("https://example.com")?;
  985. /// assert_eq!(url.port(), None);
  986. ///
  987. /// let url = Url::parse("https://example.com:443/")?;
  988. /// assert_eq!(url.port(), None);
  989. ///
  990. /// let url = Url::parse("ssh://example.com:22")?;
  991. /// assert_eq!(url.port(), Some(22));
  992. /// # Ok(())
  993. /// # }
  994. /// # run().unwrap();
  995. /// ```
  996. #[inline]
  997. pub fn port(&self) -> Option<u16> {
  998. self.port
  999. }
  1000. /// Return the port number for this URL, or the default port number if it is known.
  1001. ///
  1002. /// This method only knows the default port number
  1003. /// of the `http`, `https`, `ws`, `wss` and `ftp` schemes.
  1004. ///
  1005. /// For URLs in these schemes, this method always returns `Some(_)`.
  1006. /// For other schemes, it is the same as `Url::port()`.
  1007. ///
  1008. /// # Examples
  1009. ///
  1010. /// ```
  1011. /// use url::Url;
  1012. /// # use url::ParseError;
  1013. ///
  1014. /// # fn run() -> Result<(), ParseError> {
  1015. /// let url = Url::parse("foo://example.com")?;
  1016. /// assert_eq!(url.port_or_known_default(), None);
  1017. ///
  1018. /// let url = Url::parse("foo://example.com:1456")?;
  1019. /// assert_eq!(url.port_or_known_default(), Some(1456));
  1020. ///
  1021. /// let url = Url::parse("https://example.com")?;
  1022. /// assert_eq!(url.port_or_known_default(), Some(443));
  1023. /// # Ok(())
  1024. /// # }
  1025. /// # run().unwrap();
  1026. /// ```
  1027. #[inline]
  1028. pub fn port_or_known_default(&self) -> Option<u16> {
  1029. self.port.or_else(|| parser::default_port(self.scheme()))
  1030. }
  1031. /// Resolve a URL’s host and port number to `SocketAddr`.
  1032. ///
  1033. /// If the URL has the default port number of a scheme that is unknown to this library,
  1034. /// `default_port_number` provides an opportunity to provide the actual port number.
  1035. /// In non-example code this should be implemented either simply as `|| None`,
  1036. /// or by matching on the URL’s `.scheme()`.
  1037. ///
  1038. /// If the host is a domain, it is resolved using the standard library’s DNS support.
  1039. ///
  1040. /// # Examples
  1041. ///
  1042. /// ```no_run
  1043. /// let url = url::Url::parse("https://example.net/").unwrap();
  1044. /// let addrs = url.socket_addrs(|| None).unwrap();
  1045. /// std::net::TcpStream::connect(&*addrs)
  1046. /// # ;
  1047. /// ```
  1048. ///
  1049. /// ```
  1050. /// /// With application-specific known default port numbers
  1051. /// fn socket_addrs(url: url::Url) -> std::io::Result<Vec<std::net::SocketAddr>> {
  1052. /// url.socket_addrs(|| match url.scheme() {
  1053. /// "socks5" | "socks5h" => Some(1080),
  1054. /// _ => None,
  1055. /// })
  1056. /// }
  1057. /// ```
  1058. pub fn socket_addrs(
  1059. &self,
  1060. default_port_number: impl Fn() -> Option<u16>,
  1061. ) -> io::Result<Vec<SocketAddr>> {
  1062. // Note: trying to avoid the Vec allocation by returning `impl AsRef<[SocketAddr]>`
  1063. // causes borrowck issues because the return value borrows `default_port_number`:
  1064. //
  1065. // https://github.com/rust-lang/rfcs/blob/master/text/1951-expand-impl-trait.md#scoping-for-type-and-lifetime-parameters
  1066. //
  1067. // > This RFC proposes that *all* type parameters are considered in scope
  1068. // > for `impl Trait` in return position
  1069. fn io_result<T>(opt: Option<T>, message: &str) -> io::Result<T> {
  1070. opt.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, message))
  1071. }
  1072. let host = io_result(self.host(), "No host name in the URL")?;
  1073. let port = io_result(
  1074. self.port_or_known_default().or_else(default_port_number),
  1075. "No port number in the URL",
  1076. )?;
  1077. Ok(match host {
  1078. Host::Domain(domain) => (domain, port).to_socket_addrs()?.collect(),
  1079. Host::Ipv4(ip) => vec![(ip, port).into()],
  1080. Host::Ipv6(ip) => vec![(ip, port).into()],
  1081. })
  1082. }
  1083. /// Return the path for this URL, as a percent-encoded ASCII string.
  1084. /// For cannot-be-a-base URLs, this is an arbitrary string that doesn’t start with '/'.
  1085. /// For other URLs, this starts with a '/' slash
  1086. /// and continues with slash-separated path segments.
  1087. ///
  1088. /// # Examples
  1089. ///
  1090. /// ```rust
  1091. /// use url::{Url, ParseError};
  1092. ///
  1093. /// # fn run() -> Result<(), ParseError> {
  1094. /// let url = Url::parse("https://example.com/api/versions?page=2")?;
  1095. /// assert_eq!(url.path(), "/api/versions");
  1096. ///
  1097. /// let url = Url::parse("https://example.com")?;
  1098. /// assert_eq!(url.path(), "/");
  1099. ///
  1100. /// let url = Url::parse("https://example.com/countries/việt nam")?;
  1101. /// assert_eq!(url.path(), "/countries/vi%E1%BB%87t%20nam");
  1102. /// # Ok(())
  1103. /// # }
  1104. /// # run().unwrap();
  1105. /// ```
  1106. pub fn path(&self) -> &str {
  1107. match (self.query_start, self.fragment_start) {
  1108. (None, None) => self.slice(self.path_start..),
  1109. (Some(next_component_start), _) | (None, Some(next_component_start)) => {
  1110. self.slice(self.path_start..next_component_start)
  1111. }
  1112. }
  1113. }
  1114. /// Unless this URL is cannot-be-a-base,
  1115. /// return an iterator of '/' slash-separated path segments,
  1116. /// each as a percent-encoded ASCII string.
  1117. ///
  1118. /// Return `None` for cannot-be-a-base URLs.
  1119. ///
  1120. /// When `Some` is returned, the iterator always contains at least one string
  1121. /// (which may be empty).
  1122. ///
  1123. /// # Examples
  1124. ///
  1125. /// ```
  1126. /// use url::Url;
  1127. /// # use std::error::Error;
  1128. ///
  1129. /// # fn run() -> Result<(), Box<dyn Error>> {
  1130. /// let url = Url::parse("https://example.com/foo/bar")?;
  1131. /// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
  1132. /// assert_eq!(path_segments.next(), Some("foo"));
  1133. /// assert_eq!(path_segments.next(), Some("bar"));
  1134. /// assert_eq!(path_segments.next(), None);
  1135. ///
  1136. /// let url = Url::parse("https://example.com")?;
  1137. /// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
  1138. /// assert_eq!(path_segments.next(), Some(""));
  1139. /// assert_eq!(path_segments.next(), None);
  1140. ///
  1141. /// let url = Url::parse("data:text/plain,HelloWorld")?;
  1142. /// assert!(url.path_segments().is_none());
  1143. ///
  1144. /// let url = Url::parse("https://example.com/countries/việt nam")?;
  1145. /// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
  1146. /// assert_eq!(path_segments.next(), Some("countries"));
  1147. /// assert_eq!(path_segments.next(), Some("vi%E1%BB%87t%20nam"));
  1148. /// # Ok(())
  1149. /// # }
  1150. /// # run().unwrap();
  1151. /// ```
  1152. pub fn path_segments(&self) -> Option<str::Split<'_, char>> {
  1153. let path = self.path();
  1154. path.strip_prefix('/').map(|remainder| remainder.split('/'))
  1155. }
  1156. /// Return this URL’s query string, if any, as a percent-encoded ASCII string.
  1157. ///
  1158. /// # Examples
  1159. ///
  1160. /// ```rust
  1161. /// use url::Url;
  1162. /// # use url::ParseError;
  1163. ///
  1164. /// fn run() -> Result<(), ParseError> {
  1165. /// let url = Url::parse("https://example.com/products?page=2")?;
  1166. /// let query = url.query();
  1167. /// assert_eq!(query, Some("page=2"));
  1168. ///
  1169. /// let url = Url::parse("https://example.com/products")?;
  1170. /// let query = url.query();
  1171. /// assert!(query.is_none());
  1172. ///
  1173. /// let url = Url::parse("https://example.com/?country=español")?;
  1174. /// let query = url.query();
  1175. /// assert_eq!(query, Some("country=espa%C3%B1ol"));
  1176. /// # Ok(())
  1177. /// # }
  1178. /// # run().unwrap();
  1179. /// ```
  1180. pub fn query(&self) -> Option<&str> {
  1181. match (self.query_start, self.fragment_start) {
  1182. (None, _) => None,
  1183. (Some(query_start), None) => {
  1184. debug_assert!(self.byte_at(query_start) == b'?');
  1185. Some(self.slice(query_start + 1..))
  1186. }
  1187. (Some(query_start), Some(fragment_start)) => {
  1188. debug_assert!(self.byte_at(query_start) == b'?');
  1189. Some(self.slice(query_start + 1..fragment_start))
  1190. }
  1191. }
  1192. }
  1193. /// Parse the URL’s query string, if any, as `application/x-www-form-urlencoded`
  1194. /// and return an iterator of (key, value) pairs.
  1195. ///
  1196. /// # Examples
  1197. ///
  1198. /// ```rust
  1199. /// use std::borrow::Cow;
  1200. ///
  1201. /// use url::Url;
  1202. /// # use url::ParseError;
  1203. ///
  1204. /// # fn run() -> Result<(), ParseError> {
  1205. /// let url = Url::parse("https://example.com/products?page=2&sort=desc")?;
  1206. /// let mut pairs = url.query_pairs();
  1207. ///
  1208. /// assert_eq!(pairs.count(), 2);
  1209. ///
  1210. /// assert_eq!(pairs.next(), Some((Cow::Borrowed("page"), Cow::Borrowed("2"))));
  1211. /// assert_eq!(pairs.next(), Some((Cow::Borrowed("sort"), Cow::Borrowed("desc"))));
  1212. /// # Ok(())
  1213. /// # }
  1214. /// # run().unwrap();
  1215. ///
  1216. #[inline]
  1217. pub fn query_pairs(&self) -> form_urlencoded::Parse<'_> {
  1218. form_urlencoded::parse(self.query().unwrap_or("").as_bytes())
  1219. }
  1220. /// Return this URL’s fragment identifier, if any.
  1221. ///
  1222. /// A fragment is the part of the URL after the `#` symbol.
  1223. /// The fragment is optional and, if present, contains a fragment identifier
  1224. /// that identifies a secondary resource, such as a section heading
  1225. /// of a document.
  1226. ///
  1227. /// In HTML, the fragment identifier is usually the id attribute of a an element
  1228. /// that is scrolled to on load. Browsers typically will not send the fragment portion
  1229. /// of a URL to the server.
  1230. ///
  1231. /// **Note:** the parser did *not* percent-encode this component,
  1232. /// but the input may have been percent-encoded already.
  1233. ///
  1234. /// # Examples
  1235. ///
  1236. /// ```rust
  1237. /// use url::Url;
  1238. /// # use url::ParseError;
  1239. ///
  1240. /// # fn run() -> Result<(), ParseError> {
  1241. /// let url = Url::parse("https://example.com/data.csv#row=4")?;
  1242. ///
  1243. /// assert_eq!(url.fragment(), Some("row=4"));
  1244. ///
  1245. /// let url = Url::parse("https://example.com/data.csv#cell=4,1-6,2")?;
  1246. ///
  1247. /// assert_eq!(url.fragment(), Some("cell=4,1-6,2"));
  1248. /// # Ok(())
  1249. /// # }
  1250. /// # run().unwrap();
  1251. /// ```
  1252. pub fn fragment(&self) -> Option<&str> {
  1253. self.fragment_start.map(|start| {
  1254. debug_assert!(self.byte_at(start) == b'#');
  1255. self.slice(start + 1..)
  1256. })
  1257. }
  1258. fn mutate<F: FnOnce(&mut Parser<'_>) -> R, R>(&mut self, f: F) -> R {
  1259. let mut parser = Parser::for_setter(mem::take(&mut self.serialization));
  1260. let result = f(&mut parser);
  1261. self.serialization = parser.serialization;
  1262. result
  1263. }
  1264. /// Change this URL’s fragment identifier.
  1265. ///
  1266. /// # Examples
  1267. ///
  1268. /// ```rust
  1269. /// use url::Url;
  1270. /// # use url::ParseError;
  1271. ///
  1272. /// # fn run() -> Result<(), ParseError> {
  1273. /// let mut url = Url::parse("https://example.com/data.csv")?;
  1274. /// assert_eq!(url.as_str(), "https://example.com/data.csv");
  1275. /// url.set_fragment(Some("cell=4,1-6,2"));
  1276. /// assert_eq!(url.as_str(), "https://example.com/data.csv#cell=4,1-6,2");
  1277. /// assert_eq!(url.fragment(), Some("cell=4,1-6,2"));
  1278. ///
  1279. /// url.set_fragment(None);
  1280. /// assert_eq!(url.as_str(), "https://example.com/data.csv");
  1281. /// assert!(url.fragment().is_none());
  1282. /// # Ok(())
  1283. /// # }
  1284. /// # run().unwrap();
  1285. /// ```
  1286. pub fn set_fragment(&mut self, fragment: Option<&str>) {
  1287. // Remove any previous fragment
  1288. if let Some(start) = self.fragment_start {
  1289. debug_assert!(self.byte_at(start) == b'#');
  1290. self.serialization.truncate(start as usize);
  1291. }
  1292. // Write the new one
  1293. if let Some(input) = fragment {
  1294. self.fragment_start = Some(to_u32(self.serialization.len()).unwrap());
  1295. self.serialization.push('#');
  1296. self.mutate(|parser| parser.parse_fragment(parser::Input::no_trim(input)))
  1297. } else {
  1298. self.fragment_start = None
  1299. }
  1300. }
  1301. fn take_fragment(&mut self) -> Option<String> {
  1302. self.fragment_start.take().map(|start| {
  1303. debug_assert!(self.byte_at(start) == b'#');
  1304. let fragment = self.slice(start + 1..).to_owned();
  1305. self.serialization.truncate(start as usize);
  1306. fragment
  1307. })
  1308. }
  1309. fn restore_already_parsed_fragment(&mut self, fragment: Option<String>) {
  1310. if let Some(ref fragment) = fragment {
  1311. assert!(self.fragment_start.is_none());
  1312. self.fragment_start = Some(to_u32(self.serialization.len()).unwrap());
  1313. self.serialization.push('#');
  1314. self.serialization.push_str(fragment);
  1315. }
  1316. }
  1317. /// Change this URL’s query string.
  1318. ///
  1319. /// # Examples
  1320. ///
  1321. /// ```rust
  1322. /// use url::Url;
  1323. /// # use url::ParseError;
  1324. ///
  1325. /// # fn run() -> Result<(), ParseError> {
  1326. /// let mut url = Url::parse("https://example.com/products")?;
  1327. /// assert_eq!(url.as_str(), "https://example.com/products");
  1328. ///
  1329. /// url.set_query(Some("page=2"));
  1330. /// assert_eq!(url.as_str(), "https://example.com/products?page=2");
  1331. /// assert_eq!(url.query(), Some("page=2"));
  1332. /// # Ok(())
  1333. /// # }
  1334. /// # run().unwrap();
  1335. /// ```
  1336. pub fn set_query(&mut self, query: Option<&str>) {
  1337. let fragment = self.take_fragment();
  1338. // Remove any previous query
  1339. if let Some(start) = self.query_start.take() {
  1340. debug_assert!(self.byte_at(start) == b'?');
  1341. self.serialization.truncate(start as usize);
  1342. }
  1343. // Write the new query, if any
  1344. if let Some(input) = query {
  1345. self.query_start = Some(to_u32(self.serialization.len()).unwrap());
  1346. self.serialization.push('?');
  1347. let scheme_type = SchemeType::from(self.scheme());
  1348. let scheme_end = self.scheme_end;
  1349. self.mutate(|parser| {
  1350. let vfn = parser.violation_fn;
  1351. parser.parse_query(
  1352. scheme_type,
  1353. scheme_end,
  1354. parser::Input::trim_tab_and_newlines(input, vfn),
  1355. )
  1356. });
  1357. }
  1358. self.restore_already_parsed_fragment(fragment);
  1359. }
  1360. /// Manipulate this URL’s query string, viewed as a sequence of name/value pairs
  1361. /// in `application/x-www-form-urlencoded` syntax.
  1362. ///
  1363. /// The return value has a method-chaining API:
  1364. ///
  1365. /// ```rust
  1366. /// # use url::{Url, ParseError};
  1367. ///
  1368. /// # fn run() -> Result<(), ParseError> {
  1369. /// let mut url = Url::parse("https://example.net?lang=fr#nav")?;
  1370. /// assert_eq!(url.query(), Some("lang=fr"));
  1371. ///
  1372. /// url.query_pairs_mut().append_pair("foo", "bar");
  1373. /// assert_eq!(url.query(), Some("lang=fr&foo=bar"));
  1374. /// assert_eq!(url.as_str(), "https://example.net/?lang=fr&foo=bar#nav");
  1375. ///
  1376. /// url.query_pairs_mut()
  1377. /// .clear()
  1378. /// .append_pair("foo", "bar & baz")
  1379. /// .append_pair("saisons", "\u{00C9}t\u{00E9}+hiver");
  1380. /// assert_eq!(url.query(), Some("foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver"));
  1381. /// assert_eq!(url.as_str(),
  1382. /// "https://example.net/?foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver#nav");
  1383. /// # Ok(())
  1384. /// # }
  1385. /// # run().unwrap();
  1386. /// ```
  1387. ///
  1388. /// Note: `url.query_pairs_mut().clear();` is equivalent to `url.set_query(Some(""))`,
  1389. /// not `url.set_query(None)`.
  1390. ///
  1391. /// The state of `Url` is unspecified if this return value is leaked without being dropped.
  1392. pub fn query_pairs_mut(&mut self) -> form_urlencoded::Serializer<'_, UrlQuery<'_>> {
  1393. let fragment = self.take_fragment();
  1394. let query_start;
  1395. if let Some(start) = self.query_start {
  1396. debug_assert!(self.byte_at(start) == b'?');
  1397. query_start = start as usize;
  1398. } else {
  1399. query_start = self.serialization.len();
  1400. self.query_start = Some(to_u32(query_start).unwrap());
  1401. self.serialization.push('?');
  1402. }
  1403. let query = UrlQuery {
  1404. url: Some(self),
  1405. fragment,
  1406. };
  1407. form_urlencoded::Serializer::for_suffix(query, query_start + "?".len())
  1408. }
  1409. fn take_after_path(&mut self) -> String {
  1410. match (self.query_start, self.fragment_start) {
  1411. (Some(i), _) | (None, Some(i)) => {
  1412. let after_path = self.slice(i..).to_owned();
  1413. self.serialization.truncate(i as usize);
  1414. after_path
  1415. }
  1416. (None, None) => String::new(),
  1417. }
  1418. }
  1419. /// Change this URL’s path.
  1420. ///
  1421. /// # Examples
  1422. ///
  1423. /// ```rust
  1424. /// use url::Url;
  1425. /// # use url::ParseError;
  1426. ///
  1427. /// # fn run() -> Result<(), ParseError> {
  1428. /// let mut url = Url::parse("https://example.com")?;
  1429. /// url.set_path("api/comments");
  1430. /// assert_eq!(url.as_str(), "https://example.com/api/comments");
  1431. /// assert_eq!(url.path(), "/api/comments");
  1432. ///
  1433. /// let mut url = Url::parse("https://example.com/api")?;
  1434. /// url.set_path("data/report.csv");
  1435. /// assert_eq!(url.as_str(), "https://example.com/data/report.csv");
  1436. /// assert_eq!(url.path(), "/data/report.csv");
  1437. ///
  1438. /// // `set_path` percent-encodes the given string if it's not already percent-encoded.
  1439. /// let mut url = Url::parse("https://example.com")?;
  1440. /// url.set_path("api/some comments");
  1441. /// assert_eq!(url.as_str(), "https://example.com/api/some%20comments");
  1442. /// assert_eq!(url.path(), "/api/some%20comments");
  1443. ///
  1444. /// // `set_path` will not double percent-encode the string if it's already percent-encoded.
  1445. /// let mut url = Url::parse("https://example.com")?;
  1446. /// url.set_path("api/some%20comments");
  1447. /// assert_eq!(url.as_str(), "https://example.com/api/some%20comments");
  1448. /// assert_eq!(url.path(), "/api/some%20comments");
  1449. ///
  1450. /// # Ok(())
  1451. /// # }
  1452. /// # run().unwrap();
  1453. /// ```
  1454. pub fn set_path(&mut self, mut path: &str) {
  1455. let after_path = self.take_after_path();
  1456. let old_after_path_pos = to_u32(self.serialization.len()).unwrap();
  1457. let cannot_be_a_base = self.cannot_be_a_base();
  1458. let scheme_type = SchemeType::from(self.scheme());
  1459. self.serialization.truncate(self.path_start as usize);
  1460. self.mutate(|parser| {
  1461. if cannot_be_a_base {
  1462. if path.starts_with('/') {
  1463. parser.serialization.push_str("%2F");
  1464. path = &path[1..];
  1465. }
  1466. parser.parse_cannot_be_a_base_path(parser::Input::new(path));
  1467. } else {
  1468. let mut has_host = true; // FIXME
  1469. parser.parse_path_start(scheme_type, &mut has_host, parser::Input::new(path));
  1470. }
  1471. });
  1472. self.restore_after_path(old_after_path_pos, &after_path);
  1473. }
  1474. /// Return an object with methods to manipulate this URL’s path segments.
  1475. ///
  1476. /// Return `Err(())` if this URL is cannot-be-a-base.
  1477. #[allow(clippy::result_unit_err)]
  1478. pub fn path_segments_mut(&mut self) -> Result<PathSegmentsMut<'_>, ()> {
  1479. if self.cannot_be_a_base() {
  1480. Err(())
  1481. } else {
  1482. Ok(path_segments::new(self))
  1483. }
  1484. }
  1485. fn restore_after_path(&mut self, old_after_path_position: u32, after_path: &str) {
  1486. let new_after_path_position = to_u32(self.serialization.len()).unwrap();
  1487. let adjust = |index: &mut u32| {
  1488. *index -= old_after_path_position;
  1489. *index += new_after_path_position;
  1490. };
  1491. if let Some(ref mut index) = self.query_start {
  1492. adjust(index)
  1493. }
  1494. if let Some(ref mut index) = self.fragment_start {
  1495. adjust(index)
  1496. }
  1497. self.serialization.push_str(after_path)
  1498. }
  1499. /// Change this URL’s port number.
  1500. ///
  1501. /// Note that default port numbers are not reflected in the serialization.
  1502. ///
  1503. /// If this URL is cannot-be-a-base, does not have a host, or has the `file` scheme;
  1504. /// do nothing and return `Err`.
  1505. ///
  1506. /// # Examples
  1507. ///
  1508. /// ```
  1509. /// use url::Url;
  1510. /// # use std::error::Error;
  1511. ///
  1512. /// # fn run() -> Result<(), Box<dyn Error>> {
  1513. /// let mut url = Url::parse("ssh://example.net:2048/")?;
  1514. ///
  1515. /// url.set_port(Some(4096)).map_err(|_| "cannot be base")?;
  1516. /// assert_eq!(url.as_str(), "ssh://example.net:4096/");
  1517. ///
  1518. /// url.set_port(None).map_err(|_| "cannot be base")?;
  1519. /// assert_eq!(url.as_str(), "ssh://example.net/");
  1520. /// # Ok(())
  1521. /// # }
  1522. /// # run().unwrap();
  1523. /// ```
  1524. ///
  1525. /// Known default port numbers are not reflected:
  1526. ///
  1527. /// ```rust
  1528. /// use url::Url;
  1529. /// # use std::error::Error;
  1530. ///
  1531. /// # fn run() -> Result<(), Box<dyn Error>> {
  1532. /// let mut url = Url::parse("https://example.org/")?;
  1533. ///
  1534. /// url.set_port(Some(443)).map_err(|_| "cannot be base")?;
  1535. /// assert!(url.port().is_none());
  1536. /// # Ok(())
  1537. /// # }
  1538. /// # run().unwrap();
  1539. /// ```
  1540. ///
  1541. /// Cannot set port for cannot-be-a-base URLs:
  1542. ///
  1543. /// ```
  1544. /// use url::Url;
  1545. /// # use url::ParseError;
  1546. ///
  1547. /// # fn run() -> Result<(), ParseError> {
  1548. /// let mut url = Url::parse("mailto:rms@example.net")?;
  1549. ///
  1550. /// let result = url.set_port(Some(80));
  1551. /// assert!(result.is_err());
  1552. ///
  1553. /// let result = url.set_port(None);
  1554. /// assert!(result.is_err());
  1555. /// # Ok(())
  1556. /// # }
  1557. /// # run().unwrap();
  1558. /// ```
  1559. #[allow(clippy::result_unit_err)]
  1560. pub fn set_port(&mut self, mut port: Option<u16>) -> Result<(), ()> {
  1561. // has_host implies !cannot_be_a_base
  1562. if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
  1563. return Err(());
  1564. }
  1565. if port.is_some() && port == parser::default_port(self.scheme()) {
  1566. port = None
  1567. }
  1568. self.set_port_internal(port);
  1569. Ok(())
  1570. }
  1571. fn set_port_internal(&mut self, port: Option<u16>) {
  1572. match (self.port, port) {
  1573. (None, None) => {}
  1574. (Some(_), None) => {
  1575. self.serialization
  1576. .drain(self.host_end as usize..self.path_start as usize);
  1577. let offset = self.path_start - self.host_end;
  1578. self.path_start = self.host_end;
  1579. if let Some(ref mut index) = self.query_start {
  1580. *index -= offset
  1581. }
  1582. if let Some(ref mut index) = self.fragment_start {
  1583. *index -= offset
  1584. }
  1585. }
  1586. (Some(old), Some(new)) if old == new => {}
  1587. (_, Some(new)) => {
  1588. let path_and_after = self.slice(self.path_start..).to_owned();
  1589. self.serialization.truncate(self.host_end as usize);
  1590. write!(&mut self.serialization, ":{}", new).unwrap();
  1591. let old_path_start = self.path_start;
  1592. let new_path_start = to_u32(self.serialization.len()).unwrap();
  1593. self.path_start = new_path_start;
  1594. let adjust = |index: &mut u32| {
  1595. *index -= old_path_start;
  1596. *index += new_path_start;
  1597. };
  1598. if let Some(ref mut index) = self.query_start {
  1599. adjust(index)
  1600. }
  1601. if let Some(ref mut index) = self.fragment_start {
  1602. adjust(index)
  1603. }
  1604. self.serialization.push_str(&path_and_after);
  1605. }
  1606. }
  1607. self.port = port;
  1608. }
  1609. /// Change this URL’s host.
  1610. ///
  1611. /// Removing the host (calling this with `None`)
  1612. /// will also remove any username, password, and port number.
  1613. ///
  1614. /// # Examples
  1615. ///
  1616. /// Change host:
  1617. ///
  1618. /// ```
  1619. /// use url::Url;
  1620. /// # use url::ParseError;
  1621. ///
  1622. /// # fn run() -> Result<(), ParseError> {
  1623. /// let mut url = Url::parse("https://example.net")?;
  1624. /// let result = url.set_host(Some("rust-lang.org"));
  1625. /// assert!(result.is_ok());
  1626. /// assert_eq!(url.as_str(), "https://rust-lang.org/");
  1627. /// # Ok(())
  1628. /// # }
  1629. /// # run().unwrap();
  1630. /// ```
  1631. ///
  1632. /// Remove host:
  1633. ///
  1634. /// ```
  1635. /// use url::Url;
  1636. /// # use url::ParseError;
  1637. ///
  1638. /// # fn run() -> Result<(), ParseError> {
  1639. /// let mut url = Url::parse("foo://example.net")?;
  1640. /// let result = url.set_host(None);
  1641. /// assert!(result.is_ok());
  1642. /// assert_eq!(url.as_str(), "foo:/");
  1643. /// # Ok(())
  1644. /// # }
  1645. /// # run().unwrap();
  1646. /// ```
  1647. ///
  1648. /// Cannot remove host for 'special' schemes (e.g. `http`):
  1649. ///
  1650. /// ```
  1651. /// use url::Url;
  1652. /// # use url::ParseError;
  1653. ///
  1654. /// # fn run() -> Result<(), ParseError> {
  1655. /// let mut url = Url::parse("https://example.net")?;
  1656. /// let result = url.set_host(None);
  1657. /// assert!(result.is_err());
  1658. /// assert_eq!(url.as_str(), "https://example.net/");
  1659. /// # Ok(())
  1660. /// # }
  1661. /// # run().unwrap();
  1662. /// ```
  1663. ///
  1664. /// Cannot change or remove host for cannot-be-a-base URLs:
  1665. ///
  1666. /// ```
  1667. /// use url::Url;
  1668. /// # use url::ParseError;
  1669. ///
  1670. /// # fn run() -> Result<(), ParseError> {
  1671. /// let mut url = Url::parse("mailto:rms@example.net")?;
  1672. ///
  1673. /// let result = url.set_host(Some("rust-lang.org"));
  1674. /// assert!(result.is_err());
  1675. /// assert_eq!(url.as_str(), "mailto:rms@example.net");
  1676. ///
  1677. /// let result = url.set_host(None);
  1678. /// assert!(result.is_err());
  1679. /// assert_eq!(url.as_str(), "mailto:rms@example.net");
  1680. /// # Ok(())
  1681. /// # }
  1682. /// # run().unwrap();
  1683. /// ```
  1684. ///
  1685. /// # Errors
  1686. ///
  1687. /// If this URL is cannot-be-a-base or there is an error parsing the given `host`,
  1688. /// a [`ParseError`] variant will be returned.
  1689. ///
  1690. /// [`ParseError`]: enum.ParseError.html
  1691. pub fn set_host(&mut self, host: Option<&str>) -> Result<(), ParseError> {
  1692. if self.cannot_be_a_base() {
  1693. return Err(ParseError::SetHostOnCannotBeABaseUrl);
  1694. }
  1695. if let Some(host) = host {
  1696. if host.is_empty() && SchemeType::from(self.scheme()).is_special() {
  1697. return Err(ParseError::EmptyHost);
  1698. }
  1699. let mut host_substr = host;
  1700. // Otherwise, if c is U+003A (:) and the [] flag is unset, then
  1701. if !host.starts_with('[') || !host.ends_with(']') {
  1702. match host.find(':') {
  1703. Some(0) => {
  1704. // If buffer is the empty string, validation error, return failure.
  1705. return Err(ParseError::InvalidDomainCharacter);
  1706. }
  1707. // Let host be the result of host parsing buffer
  1708. Some(colon_index) => {
  1709. host_substr = &host[..colon_index];
  1710. }
  1711. None => {}
  1712. }
  1713. }
  1714. if SchemeType::from(self.scheme()).is_special() {
  1715. self.set_host_internal(Host::parse(host_substr)?, None);
  1716. } else {
  1717. self.set_host_internal(Host::parse_opaque(host_substr)?, None);
  1718. }
  1719. } else if self.has_host() {
  1720. let scheme_type = SchemeType::from(self.scheme());
  1721. if scheme_type.is_special() {
  1722. return Err(ParseError::EmptyHost);
  1723. } else if self.serialization.len() == self.path_start as usize {
  1724. self.serialization.push('/');
  1725. }
  1726. debug_assert!(self.byte_at(self.scheme_end) == b':');
  1727. debug_assert!(self.byte_at(self.path_start) == b'/');
  1728. let new_path_start = self.scheme_end + 1;
  1729. self.serialization
  1730. .drain(new_path_start as usize..self.path_start as usize);
  1731. let offset = self.path_start - new_path_start;
  1732. self.path_start = new_path_start;
  1733. self.username_end = new_path_start;
  1734. self.host_start = new_path_start;
  1735. self.host_end = new_path_start;
  1736. self.port = None;
  1737. if let Some(ref mut index) = self.query_start {
  1738. *index -= offset
  1739. }
  1740. if let Some(ref mut index) = self.fragment_start {
  1741. *index -= offset
  1742. }
  1743. }
  1744. Ok(())
  1745. }
  1746. /// opt_new_port: None means leave unchanged, Some(None) means remove any port number.
  1747. fn set_host_internal(&mut self, host: Host<String>, opt_new_port: Option<Option<u16>>) {
  1748. let old_suffix_pos = if opt_new_port.is_some() {
  1749. self.path_start
  1750. } else {
  1751. self.host_end
  1752. };
  1753. let suffix = self.slice(old_suffix_pos..).to_owned();
  1754. self.serialization.truncate(self.host_start as usize);
  1755. if !self.has_authority() {
  1756. debug_assert!(self.slice(self.scheme_end..self.host_start) == ":");
  1757. debug_assert!(self.username_end == self.host_start);
  1758. self.serialization.push('/');
  1759. self.serialization.push('/');
  1760. self.username_end += 2;
  1761. self.host_start += 2;
  1762. }
  1763. write!(&mut self.serialization, "{}", host).unwrap();
  1764. self.host_end = to_u32(self.serialization.len()).unwrap();
  1765. self.host = host.into();
  1766. if let Some(new_port) = opt_new_port {
  1767. self.port = new_port;
  1768. if let Some(port) = new_port {
  1769. write!(&mut self.serialization, ":{}", port).unwrap();
  1770. }
  1771. }
  1772. let new_suffix_pos = to_u32(self.serialization.len()).unwrap();
  1773. self.serialization.push_str(&suffix);
  1774. let adjust = |index: &mut u32| {
  1775. *index -= old_suffix_pos;
  1776. *index += new_suffix_pos;
  1777. };
  1778. adjust(&mut self.path_start);
  1779. if let Some(ref mut index) = self.query_start {
  1780. adjust(index)
  1781. }
  1782. if let Some(ref mut index) = self.fragment_start {
  1783. adjust(index)
  1784. }
  1785. }
  1786. /// Change this URL’s host to the given IP address.
  1787. ///
  1788. /// If this URL is cannot-be-a-base, do nothing and return `Err`.
  1789. ///
  1790. /// Compared to `Url::set_host`, this skips the host parser.
  1791. ///
  1792. /// # Examples
  1793. ///
  1794. /// ```rust
  1795. /// use url::{Url, ParseError};
  1796. ///
  1797. /// # fn run() -> Result<(), ParseError> {
  1798. /// let mut url = Url::parse("http://example.com")?;
  1799. /// url.set_ip_host("127.0.0.1".parse().unwrap());
  1800. /// assert_eq!(url.host_str(), Some("127.0.0.1"));
  1801. /// assert_eq!(url.as_str(), "http://127.0.0.1/");
  1802. /// # Ok(())
  1803. /// # }
  1804. /// # run().unwrap();
  1805. /// ```
  1806. ///
  1807. /// Cannot change URL's from mailto(cannot-be-base) to ip:
  1808. ///
  1809. /// ```rust
  1810. /// use url::{Url, ParseError};
  1811. ///
  1812. /// # fn run() -> Result<(), ParseError> {
  1813. /// let mut url = Url::parse("mailto:rms@example.com")?;
  1814. /// let result = url.set_ip_host("127.0.0.1".parse().unwrap());
  1815. ///
  1816. /// assert_eq!(url.as_str(), "mailto:rms@example.com");
  1817. /// assert!(result.is_err());
  1818. /// # Ok(())
  1819. /// # }
  1820. /// # run().unwrap();
  1821. /// ```
  1822. ///
  1823. #[allow(clippy::result_unit_err)]
  1824. pub fn set_ip_host(&mut self, address: IpAddr) -> Result<(), ()> {
  1825. if self.cannot_be_a_base() {
  1826. return Err(());
  1827. }
  1828. let address = match address {
  1829. IpAddr::V4(address) => Host::Ipv4(address),
  1830. IpAddr::V6(address) => Host::Ipv6(address),
  1831. };
  1832. self.set_host_internal(address, None);
  1833. Ok(())
  1834. }
  1835. /// Change this URL’s password.
  1836. ///
  1837. /// If this URL is cannot-be-a-base or does not have a host, do nothing and return `Err`.
  1838. ///
  1839. /// # Examples
  1840. ///
  1841. /// ```rust
  1842. /// use url::{Url, ParseError};
  1843. ///
  1844. /// # fn run() -> Result<(), ParseError> {
  1845. /// let mut url = Url::parse("mailto:rmz@example.com")?;
  1846. /// let result = url.set_password(Some("secret_password"));
  1847. /// assert!(result.is_err());
  1848. ///
  1849. /// let mut url = Url::parse("ftp://user1:secret1@example.com")?;
  1850. /// let result = url.set_password(Some("secret_password"));
  1851. /// assert_eq!(url.password(), Some("secret_password"));
  1852. ///
  1853. /// let mut url = Url::parse("ftp://user2:@example.com")?;
  1854. /// let result = url.set_password(Some("secret2"));
  1855. /// assert!(result.is_ok());
  1856. /// assert_eq!(url.password(), Some("secret2"));
  1857. /// # Ok(())
  1858. /// # }
  1859. /// # run().unwrap();
  1860. /// ```
  1861. #[allow(clippy::result_unit_err)]
  1862. pub fn set_password(&mut self, password: Option<&str>) -> Result<(), ()> {
  1863. // has_host implies !cannot_be_a_base
  1864. if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
  1865. return Err(());
  1866. }
  1867. if let Some(password) = password {
  1868. let host_and_after = self.slice(self.host_start..).to_owned();
  1869. self.serialization.truncate(self.username_end as usize);
  1870. self.serialization.push(':');
  1871. self.serialization
  1872. .extend(utf8_percent_encode(password, USERINFO));
  1873. self.serialization.push('@');
  1874. let old_host_start = self.host_start;
  1875. let new_host_start = to_u32(self.serialization.len()).unwrap();
  1876. let adjust = |index: &mut u32| {
  1877. *index -= old_host_start;
  1878. *index += new_host_start;
  1879. };
  1880. self.host_start = new_host_start;
  1881. adjust(&mut self.host_end);
  1882. adjust(&mut self.path_start);
  1883. if let Some(ref mut index) = self.query_start {
  1884. adjust(index)
  1885. }
  1886. if let Some(ref mut index) = self.fragment_start {
  1887. adjust(index)
  1888. }
  1889. self.serialization.push_str(&host_and_after);
  1890. } else if self.byte_at(self.username_end) == b':' {
  1891. // If there is a password to remove
  1892. let has_username_or_password = self.byte_at(self.host_start - 1) == b'@';
  1893. debug_assert!(has_username_or_password);
  1894. let username_start = self.scheme_end + 3;
  1895. let empty_username = username_start == self.username_end;
  1896. let start = self.username_end; // Remove the ':'
  1897. let end = if empty_username {
  1898. self.host_start // Remove the '@' as well
  1899. } else {
  1900. self.host_start - 1 // Keep the '@' to separate the username from the host
  1901. };
  1902. self.serialization.drain(start as usize..end as usize);
  1903. let offset = end - start;
  1904. self.host_start -= offset;
  1905. self.host_end -= offset;
  1906. self.path_start -= offset;
  1907. if let Some(ref mut index) = self.query_start {
  1908. *index -= offset
  1909. }
  1910. if let Some(ref mut index) = self.fragment_start {
  1911. *index -= offset
  1912. }
  1913. }
  1914. Ok(())
  1915. }
  1916. /// Change this URL’s username.
  1917. ///
  1918. /// If this URL is cannot-be-a-base or does not have a host, do nothing and return `Err`.
  1919. /// # Examples
  1920. ///
  1921. /// Cannot setup username from mailto(cannot-be-base)
  1922. ///
  1923. /// ```rust
  1924. /// use url::{Url, ParseError};
  1925. ///
  1926. /// # fn run() -> Result<(), ParseError> {
  1927. /// let mut url = Url::parse("mailto:rmz@example.com")?;
  1928. /// let result = url.set_username("user1");
  1929. /// assert_eq!(url.as_str(), "mailto:rmz@example.com");
  1930. /// assert!(result.is_err());
  1931. /// # Ok(())
  1932. /// # }
  1933. /// # run().unwrap();
  1934. /// ```
  1935. ///
  1936. /// Setup username to user1
  1937. ///
  1938. /// ```rust
  1939. /// use url::{Url, ParseError};
  1940. ///
  1941. /// # fn run() -> Result<(), ParseError> {
  1942. /// let mut url = Url::parse("ftp://:secre1@example.com/")?;
  1943. /// let result = url.set_username("user1");
  1944. /// assert!(result.is_ok());
  1945. /// assert_eq!(url.username(), "user1");
  1946. /// assert_eq!(url.as_str(), "ftp://user1:secre1@example.com/");
  1947. /// # Ok(())
  1948. /// # }
  1949. /// # run().unwrap();
  1950. /// ```
  1951. #[allow(clippy::result_unit_err)]
  1952. pub fn set_username(&mut self, username: &str) -> Result<(), ()> {
  1953. // has_host implies !cannot_be_a_base
  1954. if !self.has_host() || self.host() == Some(Host::Domain("")) || self.scheme() == "file" {
  1955. return Err(());
  1956. }
  1957. let username_start = self.scheme_end + 3;
  1958. debug_assert!(self.slice(self.scheme_end..username_start) == "://");
  1959. if self.slice(username_start..self.username_end) == username {
  1960. return Ok(());
  1961. }
  1962. let after_username = self.slice(self.username_end..).to_owned();
  1963. self.serialization.truncate(username_start as usize);
  1964. self.serialization
  1965. .extend(utf8_percent_encode(username, USERINFO));
  1966. let mut removed_bytes = self.username_end;
  1967. self.username_end = to_u32(self.serialization.len()).unwrap();
  1968. let mut added_bytes = self.username_end;
  1969. let new_username_is_empty = self.username_end == username_start;
  1970. match (new_username_is_empty, after_username.chars().next()) {
  1971. (true, Some('@')) => {
  1972. removed_bytes += 1;
  1973. self.serialization.push_str(&after_username[1..]);
  1974. }
  1975. (false, Some('@')) | (_, Some(':')) | (true, _) => {
  1976. self.serialization.push_str(&after_username);
  1977. }
  1978. (false, _) => {
  1979. added_bytes += 1;
  1980. self.serialization.push('@');
  1981. self.serialization.push_str(&after_username);
  1982. }
  1983. }
  1984. let adjust = |index: &mut u32| {
  1985. *index -= removed_bytes;
  1986. *index += added_bytes;
  1987. };
  1988. adjust(&mut self.host_start);
  1989. adjust(&mut self.host_end);
  1990. adjust(&mut self.path_start);
  1991. if let Some(ref mut index) = self.query_start {
  1992. adjust(index)
  1993. }
  1994. if let Some(ref mut index) = self.fragment_start {
  1995. adjust(index)
  1996. }
  1997. Ok(())
  1998. }
  1999. /// Change this URL’s scheme.
  2000. ///
  2001. /// Do nothing and return `Err` under the following circumstances:
  2002. ///
  2003. /// * If the new scheme is not in `[a-zA-Z][a-zA-Z0-9+.-]+`
  2004. /// * If this URL is cannot-be-a-base and the new scheme is one of
  2005. /// `http`, `https`, `ws`, `wss` or `ftp`
  2006. /// * If either the old or new scheme is `http`, `https`, `ws`,
  2007. /// `wss` or `ftp` and the other is not one of these
  2008. /// * If the new scheme is `file` and this URL includes credentials
  2009. /// or has a non-null port
  2010. /// * If this URL's scheme is `file` and its host is empty or null
  2011. ///
  2012. /// See also [the URL specification's section on legal scheme state
  2013. /// overrides](https://url.spec.whatwg.org/#scheme-state).
  2014. ///
  2015. /// # Examples
  2016. ///
  2017. /// Change the URL’s scheme from `https` to `http`:
  2018. ///
  2019. /// ```
  2020. /// use url::Url;
  2021. /// # use url::ParseError;
  2022. ///
  2023. /// # fn run() -> Result<(), ParseError> {
  2024. /// let mut url = Url::parse("https://example.net")?;
  2025. /// let result = url.set_scheme("http");
  2026. /// assert_eq!(url.as_str(), "http://example.net/");
  2027. /// assert!(result.is_ok());
  2028. /// # Ok(())
  2029. /// # }
  2030. /// # run().unwrap();
  2031. /// ```
  2032. /// Change the URL’s scheme from `foo` to `bar`:
  2033. ///
  2034. /// ```
  2035. /// use url::Url;
  2036. /// # use url::ParseError;
  2037. ///
  2038. /// # fn run() -> Result<(), ParseError> {
  2039. /// let mut url = Url::parse("foo://example.net")?;
  2040. /// let result = url.set_scheme("bar");
  2041. /// assert_eq!(url.as_str(), "bar://example.net");
  2042. /// assert!(result.is_ok());
  2043. /// # Ok(())
  2044. /// # }
  2045. /// # run().unwrap();
  2046. /// ```
  2047. ///
  2048. /// Cannot change URL’s scheme from `https` to `foõ`:
  2049. ///
  2050. /// ```
  2051. /// use url::Url;
  2052. /// # use url::ParseError;
  2053. ///
  2054. /// # fn run() -> Result<(), ParseError> {
  2055. /// let mut url = Url::parse("https://example.net")?;
  2056. /// let result = url.set_scheme("foõ");
  2057. /// assert_eq!(url.as_str(), "https://example.net/");
  2058. /// assert!(result.is_err());
  2059. /// # Ok(())
  2060. /// # }
  2061. /// # run().unwrap();
  2062. /// ```
  2063. ///
  2064. /// Cannot change URL’s scheme from `mailto` (cannot-be-a-base) to `https`:
  2065. ///
  2066. /// ```
  2067. /// use url::Url;
  2068. /// # use url::ParseError;
  2069. ///
  2070. /// # fn run() -> Result<(), ParseError> {
  2071. /// let mut url = Url::parse("mailto:rms@example.net")?;
  2072. /// let result = url.set_scheme("https");
  2073. /// assert_eq!(url.as_str(), "mailto:rms@example.net");
  2074. /// assert!(result.is_err());
  2075. /// # Ok(())
  2076. /// # }
  2077. /// # run().unwrap();
  2078. /// ```
  2079. /// Cannot change the URL’s scheme from `foo` to `https`:
  2080. ///
  2081. /// ```
  2082. /// use url::Url;
  2083. /// # use url::ParseError;
  2084. ///
  2085. /// # fn run() -> Result<(), ParseError> {
  2086. /// let mut url = Url::parse("foo://example.net")?;
  2087. /// let result = url.set_scheme("https");
  2088. /// assert_eq!(url.as_str(), "foo://example.net");
  2089. /// assert!(result.is_err());
  2090. /// # Ok(())
  2091. /// # }
  2092. /// # run().unwrap();
  2093. /// ```
  2094. /// Cannot change the URL’s scheme from `http` to `foo`:
  2095. ///
  2096. /// ```
  2097. /// use url::Url;
  2098. /// # use url::ParseError;
  2099. ///
  2100. /// # fn run() -> Result<(), ParseError> {
  2101. /// let mut url = Url::parse("http://example.net")?;
  2102. /// let result = url.set_scheme("foo");
  2103. /// assert_eq!(url.as_str(), "http://example.net/");
  2104. /// assert!(result.is_err());
  2105. /// # Ok(())
  2106. /// # }
  2107. /// # run().unwrap();
  2108. /// ```
  2109. #[allow(clippy::result_unit_err, clippy::suspicious_operation_groupings)]
  2110. pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
  2111. let mut parser = Parser::for_setter(String::new());
  2112. let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
  2113. let new_scheme_type = SchemeType::from(&parser.serialization);
  2114. let old_scheme_type = SchemeType::from(self.scheme());
  2115. // If url’s scheme is a special scheme and buffer is not a special scheme, then return.
  2116. if (new_scheme_type.is_special() && !old_scheme_type.is_special()) ||
  2117. // If url’s scheme is not a special scheme and buffer is a special scheme, then return.
  2118. (!new_scheme_type.is_special() && old_scheme_type.is_special()) ||
  2119. // If url includes credentials or has a non-null port, and buffer is "file", then return.
  2120. // If url’s scheme is "file" and its host is an empty host or null, then return.
  2121. (new_scheme_type.is_file() && self.has_authority())
  2122. {
  2123. return Err(());
  2124. }
  2125. if !remaining.is_empty() || (!self.has_host() && new_scheme_type.is_special()) {
  2126. return Err(());
  2127. }
  2128. let old_scheme_end = self.scheme_end;
  2129. let new_scheme_end = to_u32(parser.serialization.len()).unwrap();
  2130. let adjust = |index: &mut u32| {
  2131. *index -= old_scheme_end;
  2132. *index += new_scheme_end;
  2133. };
  2134. self.scheme_end = new_scheme_end;
  2135. adjust(&mut self.username_end);
  2136. adjust(&mut self.host_start);
  2137. adjust(&mut self.host_end);
  2138. adjust(&mut self.path_start);
  2139. if let Some(ref mut index) = self.query_start {
  2140. adjust(index)
  2141. }
  2142. if let Some(ref mut index) = self.fragment_start {
  2143. adjust(index)
  2144. }
  2145. parser.serialization.push_str(self.slice(old_scheme_end..));
  2146. self.serialization = parser.serialization;
  2147. // Update the port so it can be removed
  2148. // If it is the scheme's default
  2149. // we don't mind it silently failing
  2150. // if there was no port in the first place
  2151. let previous_port = self.port();
  2152. let _ = self.set_port(previous_port);
  2153. Ok(())
  2154. }
  2155. /// Convert a file name as `std::path::Path` into an URL in the `file` scheme.
  2156. ///
  2157. /// This returns `Err` if the given path is not absolute or,
  2158. /// on Windows, if the prefix is not a disk prefix (e.g. `C:`) or a UNC prefix (`\\`).
  2159. ///
  2160. /// # Examples
  2161. ///
  2162. /// On Unix-like platforms:
  2163. ///
  2164. /// ```
  2165. /// # if cfg!(unix) {
  2166. /// use url::Url;
  2167. ///
  2168. /// # fn run() -> Result<(), ()> {
  2169. /// let url = Url::from_file_path("/tmp/foo.txt")?;
  2170. /// assert_eq!(url.as_str(), "file:///tmp/foo.txt");
  2171. ///
  2172. /// let url = Url::from_file_path("../foo.txt");
  2173. /// assert!(url.is_err());
  2174. ///
  2175. /// let url = Url::from_file_path("https://google.com/");
  2176. /// assert!(url.is_err());
  2177. /// # Ok(())
  2178. /// # }
  2179. /// # run().unwrap();
  2180. /// # }
  2181. /// ```
  2182. #[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))]
  2183. #[allow(clippy::result_unit_err)]
  2184. pub fn from_file_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
  2185. let mut serialization = "file://".to_owned();
  2186. let host_start = serialization.len() as u32;
  2187. let (host_end, host) = path_to_file_url_segments(path.as_ref(), &mut serialization)?;
  2188. Ok(Url {
  2189. serialization,
  2190. scheme_end: "file".len() as u32,
  2191. username_end: host_start,
  2192. host_start,
  2193. host_end,
  2194. host,
  2195. port: None,
  2196. path_start: host_end,
  2197. query_start: None,
  2198. fragment_start: None,
  2199. })
  2200. }
  2201. /// Convert a directory name as `std::path::Path` into an URL in the `file` scheme.
  2202. ///
  2203. /// This returns `Err` if the given path is not absolute or,
  2204. /// on Windows, if the prefix is not a disk prefix (e.g. `C:`) or a UNC prefix (`\\`).
  2205. ///
  2206. /// Compared to `from_file_path`, this ensure that URL’s the path has a trailing slash
  2207. /// so that the entire path is considered when using this URL as a base URL.
  2208. ///
  2209. /// For example:
  2210. ///
  2211. /// * `"index.html"` parsed with `Url::from_directory_path(Path::new("/var/www"))`
  2212. /// as the base URL is `file:///var/www/index.html`
  2213. /// * `"index.html"` parsed with `Url::from_file_path(Path::new("/var/www"))`
  2214. /// as the base URL is `file:///var/index.html`, which might not be what was intended.
  2215. ///
  2216. /// Note that `std::path` does not consider trailing slashes significant
  2217. /// and usually does not include them (e.g. in `Path::parent()`).
  2218. #[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))]
  2219. #[allow(clippy::result_unit_err)]
  2220. pub fn from_directory_path<P: AsRef<Path>>(path: P) -> Result<Url, ()> {
  2221. let mut url = Url::from_file_path(path)?;
  2222. if !url.serialization.ends_with('/') {
  2223. url.serialization.push('/')
  2224. }
  2225. Ok(url)
  2226. }
  2227. /// Serialize with Serde using the internal representation of the `Url` struct.
  2228. ///
  2229. /// The corresponding `deserialize_internal` method sacrifices some invariant-checking
  2230. /// for speed, compared to the `Deserialize` trait impl.
  2231. ///
  2232. /// This method is only available if the `serde` Cargo feature is enabled.
  2233. #[cfg(feature = "serde")]
  2234. #[deny(unused)]
  2235. pub fn serialize_internal<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  2236. where
  2237. S: serde::Serializer,
  2238. {
  2239. use serde::Serialize;
  2240. // Destructuring first lets us ensure that adding or removing fields forces this method
  2241. // to be updated
  2242. let Url {
  2243. ref serialization,
  2244. ref scheme_end,
  2245. ref username_end,
  2246. ref host_start,
  2247. ref host_end,
  2248. ref host,
  2249. ref port,
  2250. ref path_start,
  2251. ref query_start,
  2252. ref fragment_start,
  2253. } = *self;
  2254. (
  2255. serialization,
  2256. scheme_end,
  2257. username_end,
  2258. host_start,
  2259. host_end,
  2260. host,
  2261. port,
  2262. path_start,
  2263. query_start,
  2264. fragment_start,
  2265. )
  2266. .serialize(serializer)
  2267. }
  2268. /// Serialize with Serde using the internal representation of the `Url` struct.
  2269. ///
  2270. /// The corresponding `deserialize_internal` method sacrifices some invariant-checking
  2271. /// for speed, compared to the `Deserialize` trait impl.
  2272. ///
  2273. /// This method is only available if the `serde` Cargo feature is enabled.
  2274. #[cfg(feature = "serde")]
  2275. #[deny(unused)]
  2276. pub fn deserialize_internal<'de, D>(deserializer: D) -> Result<Self, D::Error>
  2277. where
  2278. D: serde::Deserializer<'de>,
  2279. {
  2280. use serde::de::{Deserialize, Error, Unexpected};
  2281. let (
  2282. serialization,
  2283. scheme_end,
  2284. username_end,
  2285. host_start,
  2286. host_end,
  2287. host,
  2288. port,
  2289. path_start,
  2290. query_start,
  2291. fragment_start,
  2292. ) = Deserialize::deserialize(deserializer)?;
  2293. let url = Url {
  2294. serialization,
  2295. scheme_end,
  2296. username_end,
  2297. host_start,
  2298. host_end,
  2299. host,
  2300. port,
  2301. path_start,
  2302. query_start,
  2303. fragment_start,
  2304. };
  2305. if cfg!(debug_assertions) {
  2306. url.check_invariants().map_err(|reason| {
  2307. let reason: &str = &reason;
  2308. Error::invalid_value(Unexpected::Other("value"), &reason)
  2309. })?
  2310. }
  2311. Ok(url)
  2312. }
  2313. /// Assuming the URL is in the `file` scheme or similar,
  2314. /// convert its path to an absolute `std::path::Path`.
  2315. ///
  2316. /// **Note:** This does not actually check the URL’s `scheme`,
  2317. /// and may give nonsensical results for other schemes.
  2318. /// It is the user’s responsibility to check the URL’s scheme before calling this.
  2319. ///
  2320. /// ```
  2321. /// # use url::Url;
  2322. /// # let url = Url::parse("file:///etc/passwd").unwrap();
  2323. /// let path = url.to_file_path();
  2324. /// ```
  2325. ///
  2326. /// Returns `Err` if the host is neither empty nor `"localhost"` (except on Windows, where
  2327. /// `file:` URLs may have a non-local host),
  2328. /// or if `Path::new_opt()` returns `None`.
  2329. /// (That is, if the percent-decoded path contains a NUL byte or,
  2330. /// for a Windows path, is not UTF-8.)
  2331. #[inline]
  2332. #[cfg(any(unix, windows, target_os = "redox", target_os = "wasi"))]
  2333. #[allow(clippy::result_unit_err)]
  2334. pub fn to_file_path(&self) -> Result<PathBuf, ()> {
  2335. if let Some(segments) = self.path_segments() {
  2336. let host = match self.host() {
  2337. None | Some(Host::Domain("localhost")) => None,
  2338. Some(_) if cfg!(windows) && self.scheme() == "file" => {
  2339. Some(&self.serialization[self.host_start as usize..self.host_end as usize])
  2340. }
  2341. _ => return Err(()),
  2342. };
  2343. return file_url_segments_to_pathbuf(host, segments);
  2344. }
  2345. Err(())
  2346. }
  2347. // Private helper methods:
  2348. #[inline]
  2349. fn slice<R>(&self, range: R) -> &str
  2350. where
  2351. R: RangeArg,
  2352. {
  2353. range.slice_of(&self.serialization)
  2354. }
  2355. #[inline]
  2356. fn byte_at(&self, i: u32) -> u8 {
  2357. self.serialization.as_bytes()[i as usize]
  2358. }
  2359. }
  2360. /// Parse a string as an URL, without a base URL or encoding override.
  2361. impl str::FromStr for Url {
  2362. type Err = ParseError;
  2363. #[inline]
  2364. fn from_str(input: &str) -> Result<Url, crate::ParseError> {
  2365. Url::parse(input)
  2366. }
  2367. }
  2368. impl<'a> TryFrom<&'a str> for Url {
  2369. type Error = ParseError;
  2370. fn try_from(s: &'a str) -> Result<Self, Self::Error> {
  2371. Url::parse(s)
  2372. }
  2373. }
  2374. /// Display the serialization of this URL.
  2375. impl fmt::Display for Url {
  2376. #[inline]
  2377. fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
  2378. fmt::Display::fmt(&self.serialization, formatter)
  2379. }
  2380. }
  2381. /// String conversion.
  2382. impl From<Url> for String {
  2383. fn from(value: Url) -> String {
  2384. value.serialization
  2385. }
  2386. }
  2387. /// Debug the serialization of this URL.
  2388. impl fmt::Debug for Url {
  2389. #[inline]
  2390. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  2391. formatter
  2392. .debug_struct("Url")
  2393. .field("scheme", &self.scheme())
  2394. .field("cannot_be_a_base", &self.cannot_be_a_base())
  2395. .field("username", &self.username())
  2396. .field("password", &self.password())
  2397. .field("host", &self.host())
  2398. .field("port", &self.port())
  2399. .field("path", &self.path())
  2400. .field("query", &self.query())
  2401. .field("fragment", &self.fragment())
  2402. .finish()
  2403. }
  2404. }
  2405. /// URLs compare like their serialization.
  2406. impl Eq for Url {}
  2407. /// URLs compare like their serialization.
  2408. impl PartialEq for Url {
  2409. #[inline]
  2410. fn eq(&self, other: &Self) -> bool {
  2411. self.serialization == other.serialization
  2412. }
  2413. }
  2414. /// URLs compare like their serialization.
  2415. impl Ord for Url {
  2416. #[inline]
  2417. fn cmp(&self, other: &Self) -> cmp::Ordering {
  2418. self.serialization.cmp(&other.serialization)
  2419. }
  2420. }
  2421. /// URLs compare like their serialization.
  2422. impl PartialOrd for Url {
  2423. #[inline]
  2424. fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
  2425. self.serialization.partial_cmp(&other.serialization)
  2426. }
  2427. }
  2428. /// URLs hash like their serialization.
  2429. impl hash::Hash for Url {
  2430. #[inline]
  2431. fn hash<H>(&self, state: &mut H)
  2432. where
  2433. H: hash::Hasher,
  2434. {
  2435. hash::Hash::hash(&self.serialization, state)
  2436. }
  2437. }
  2438. /// Return the serialization of this URL.
  2439. impl AsRef<str> for Url {
  2440. #[inline]
  2441. fn as_ref(&self) -> &str {
  2442. &self.serialization
  2443. }
  2444. }
  2445. trait RangeArg {
  2446. fn slice_of<'a>(&self, s: &'a str) -> &'a str;
  2447. }
  2448. impl RangeArg for Range<u32> {
  2449. #[inline]
  2450. fn slice_of<'a>(&self, s: &'a str) -> &'a str {
  2451. &s[self.start as usize..self.end as usize]
  2452. }
  2453. }
  2454. impl RangeArg for RangeFrom<u32> {
  2455. #[inline]
  2456. fn slice_of<'a>(&self, s: &'a str) -> &'a str {
  2457. &s[self.start as usize..]
  2458. }
  2459. }
  2460. impl RangeArg for RangeTo<u32> {
  2461. #[inline]
  2462. fn slice_of<'a>(&self, s: &'a str) -> &'a str {
  2463. &s[..self.end as usize]
  2464. }
  2465. }
  2466. /// Serializes this URL into a `serde` stream.
  2467. ///
  2468. /// This implementation is only available if the `serde` Cargo feature is enabled.
  2469. #[cfg(feature = "serde")]
  2470. impl serde::Serialize for Url {
  2471. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  2472. where
  2473. S: serde::Serializer,
  2474. {
  2475. serializer.serialize_str(self.as_str())
  2476. }
  2477. }
  2478. /// Deserializes this URL from a `serde` stream.
  2479. ///
  2480. /// This implementation is only available if the `serde` Cargo feature is enabled.
  2481. #[cfg(feature = "serde")]
  2482. impl<'de> serde::Deserialize<'de> for Url {
  2483. fn deserialize<D>(deserializer: D) -> Result<Url, D::Error>
  2484. where
  2485. D: serde::Deserializer<'de>,
  2486. {
  2487. use serde::de::{Error, Unexpected, Visitor};
  2488. struct UrlVisitor;
  2489. impl<'de> Visitor<'de> for UrlVisitor {
  2490. type Value = Url;
  2491. fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  2492. formatter.write_str("a string representing an URL")
  2493. }
  2494. fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
  2495. where
  2496. E: Error,
  2497. {
  2498. Url::parse(s).map_err(|err| {
  2499. let err_s = format!("{}", err);
  2500. Error::invalid_value(Unexpected::Str(s), &err_s.as_str())
  2501. })
  2502. }
  2503. }
  2504. deserializer.deserialize_str(UrlVisitor)
  2505. }
  2506. }
  2507. #[cfg(any(unix, target_os = "redox", target_os = "wasi"))]
  2508. fn path_to_file_url_segments(
  2509. path: &Path,
  2510. serialization: &mut String,
  2511. ) -> Result<(u32, HostInternal), ()> {
  2512. #[cfg(any(unix, target_os = "redox"))]
  2513. use std::os::unix::prelude::OsStrExt;
  2514. #[cfg(target_os = "wasi")]
  2515. use std::os::wasi::prelude::OsStrExt;
  2516. if !path.is_absolute() {
  2517. return Err(());
  2518. }
  2519. let host_end = to_u32(serialization.len()).unwrap();
  2520. let mut empty = true;
  2521. // skip the root component
  2522. for component in path.components().skip(1) {
  2523. empty = false;
  2524. serialization.push('/');
  2525. serialization.extend(percent_encode(
  2526. component.as_os_str().as_bytes(),
  2527. PATH_SEGMENT,
  2528. ));
  2529. }
  2530. if empty {
  2531. // An URL’s path must not be empty.
  2532. serialization.push('/');
  2533. }
  2534. Ok((host_end, HostInternal::None))
  2535. }
  2536. #[cfg(windows)]
  2537. fn path_to_file_url_segments(
  2538. path: &Path,
  2539. serialization: &mut String,
  2540. ) -> Result<(u32, HostInternal), ()> {
  2541. path_to_file_url_segments_windows(path, serialization)
  2542. }
  2543. // Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
  2544. #[cfg_attr(not(windows), allow(dead_code))]
  2545. fn path_to_file_url_segments_windows(
  2546. path: &Path,
  2547. serialization: &mut String,
  2548. ) -> Result<(u32, HostInternal), ()> {
  2549. use std::path::{Component, Prefix};
  2550. if !path.is_absolute() {
  2551. return Err(());
  2552. }
  2553. let mut components = path.components();
  2554. let host_start = serialization.len() + 1;
  2555. let host_end;
  2556. let host_internal;
  2557. match components.next() {
  2558. Some(Component::Prefix(ref p)) => match p.kind() {
  2559. Prefix::Disk(letter) | Prefix::VerbatimDisk(letter) => {
  2560. host_end = to_u32(serialization.len()).unwrap();
  2561. host_internal = HostInternal::None;
  2562. serialization.push('/');
  2563. serialization.push(letter as char);
  2564. serialization.push(':');
  2565. }
  2566. Prefix::UNC(server, share) | Prefix::VerbatimUNC(server, share) => {
  2567. let host = Host::parse(server.to_str().ok_or(())?).map_err(|_| ())?;
  2568. write!(serialization, "{}", host).unwrap();
  2569. host_end = to_u32(serialization.len()).unwrap();
  2570. host_internal = host.into();
  2571. serialization.push('/');
  2572. let share = share.to_str().ok_or(())?;
  2573. serialization.extend(percent_encode(share.as_bytes(), PATH_SEGMENT));
  2574. }
  2575. _ => return Err(()),
  2576. },
  2577. _ => return Err(()),
  2578. }
  2579. let mut path_only_has_prefix = true;
  2580. for component in components {
  2581. if component == Component::RootDir {
  2582. continue;
  2583. }
  2584. path_only_has_prefix = false;
  2585. // FIXME: somehow work with non-unicode?
  2586. let component = component.as_os_str().to_str().ok_or(())?;
  2587. serialization.push('/');
  2588. serialization.extend(percent_encode(component.as_bytes(), PATH_SEGMENT));
  2589. }
  2590. // A windows drive letter must end with a slash.
  2591. if serialization.len() > host_start
  2592. && parser::is_windows_drive_letter(&serialization[host_start..])
  2593. && path_only_has_prefix
  2594. {
  2595. serialization.push('/');
  2596. }
  2597. Ok((host_end, host_internal))
  2598. }
  2599. #[cfg(any(unix, target_os = "redox", target_os = "wasi"))]
  2600. fn file_url_segments_to_pathbuf(
  2601. host: Option<&str>,
  2602. segments: str::Split<'_, char>,
  2603. ) -> Result<PathBuf, ()> {
  2604. use std::ffi::OsStr;
  2605. #[cfg(any(unix, target_os = "redox"))]
  2606. use std::os::unix::prelude::OsStrExt;
  2607. #[cfg(target_os = "wasi")]
  2608. use std::os::wasi::prelude::OsStrExt;
  2609. if host.is_some() {
  2610. return Err(());
  2611. }
  2612. let mut bytes = if cfg!(target_os = "redox") {
  2613. b"file:".to_vec()
  2614. } else {
  2615. Vec::new()
  2616. };
  2617. for segment in segments {
  2618. bytes.push(b'/');
  2619. bytes.extend(percent_decode(segment.as_bytes()));
  2620. }
  2621. // A windows drive letter must end with a slash.
  2622. if bytes.len() > 2
  2623. && matches!(bytes[bytes.len() - 2], b'a'..=b'z' | b'A'..=b'Z')
  2624. && matches!(bytes[bytes.len() - 1], b':' | b'|')
  2625. {
  2626. bytes.push(b'/');
  2627. }
  2628. let os_str = OsStr::from_bytes(&bytes);
  2629. let path = PathBuf::from(os_str);
  2630. debug_assert!(
  2631. path.is_absolute(),
  2632. "to_file_path() failed to produce an absolute Path"
  2633. );
  2634. Ok(path)
  2635. }
  2636. #[cfg(windows)]
  2637. fn file_url_segments_to_pathbuf(
  2638. host: Option<&str>,
  2639. segments: str::Split<char>,
  2640. ) -> Result<PathBuf, ()> {
  2641. file_url_segments_to_pathbuf_windows(host, segments)
  2642. }
  2643. // Build this unconditionally to alleviate https://github.com/servo/rust-url/issues/102
  2644. #[cfg_attr(not(windows), allow(dead_code))]
  2645. fn file_url_segments_to_pathbuf_windows(
  2646. host: Option<&str>,
  2647. mut segments: str::Split<'_, char>,
  2648. ) -> Result<PathBuf, ()> {
  2649. let mut string = if let Some(host) = host {
  2650. r"\\".to_owned() + host
  2651. } else {
  2652. let first = segments.next().ok_or(())?;
  2653. match first.len() {
  2654. 2 => {
  2655. if !first.starts_with(parser::ascii_alpha) || first.as_bytes()[1] != b':' {
  2656. return Err(());
  2657. }
  2658. first.to_owned()
  2659. }
  2660. 4 => {
  2661. if !first.starts_with(parser::ascii_alpha) {
  2662. return Err(());
  2663. }
  2664. let bytes = first.as_bytes();
  2665. if bytes[1] != b'%' || bytes[2] != b'3' || (bytes[3] != b'a' && bytes[3] != b'A') {
  2666. return Err(());
  2667. }
  2668. first[0..1].to_owned() + ":"
  2669. }
  2670. _ => return Err(()),
  2671. }
  2672. };
  2673. for segment in segments {
  2674. string.push('\\');
  2675. // Currently non-unicode windows paths cannot be represented
  2676. match String::from_utf8(percent_decode(segment.as_bytes()).collect()) {
  2677. Ok(s) => string.push_str(&s),
  2678. Err(..) => return Err(()),
  2679. }
  2680. }
  2681. let path = PathBuf::from(string);
  2682. debug_assert!(
  2683. path.is_absolute(),
  2684. "to_file_path() failed to produce an absolute Path"
  2685. );
  2686. Ok(path)
  2687. }
  2688. /// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
  2689. #[derive(Debug)]
  2690. pub struct UrlQuery<'a> {
  2691. url: Option<&'a mut Url>,
  2692. fragment: Option<String>,
  2693. }
  2694. // `as_mut_string` string here exposes the internal serialization of an `Url`,
  2695. // which should not be exposed to users.
  2696. // We achieve that by not giving users direct access to `UrlQuery`:
  2697. // * Its fields are private
  2698. // (and so can not be constructed with struct literal syntax outside of this crate),
  2699. // * It has no constructor
  2700. // * It is only visible (on the type level) to users in the return type of
  2701. // `Url::query_pairs_mut` which is `Serializer<UrlQuery>`
  2702. // * `Serializer` keeps its target in a private field
  2703. // * Unlike in other `Target` impls, `UrlQuery::finished` does not return `Self`.
  2704. impl<'a> form_urlencoded::Target for UrlQuery<'a> {
  2705. fn as_mut_string(&mut self) -> &mut String {
  2706. &mut self.url.as_mut().unwrap().serialization
  2707. }
  2708. fn finish(mut self) -> &'a mut Url {
  2709. let url = self.url.take().unwrap();
  2710. url.restore_already_parsed_fragment(self.fragment.take());
  2711. url
  2712. }
  2713. type Finished = &'a mut Url;
  2714. }
  2715. impl<'a> Drop for UrlQuery<'a> {
  2716. fn drop(&mut self) {
  2717. if let Some(url) = self.url.take() {
  2718. url.restore_already_parsed_fragment(self.fragment.take())
  2719. }
  2720. }
  2721. }