lib.rs 89 KB

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