lib.rs 86 KB

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