lib.rs 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // Copyright 2013-2014 Simon Sapin.
  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. <a href="https://github.com/servo/rust-url"><img style="position: absolute; top: 0; left: 0; border: 0;" src="../github.png" alt="Fork me on GitHub"></a>
  10. <style>.sidebar { margin-top: 53px }</style>
  11. rust-url is an implementation of the [URL Standard](http://url.spec.whatwg.org/)
  12. for the [Rust](http://rust-lang.org/) programming language.
  13. It builds with [Cargo](http://crates.io/).
  14. To use it in your project, add this to your `Cargo.toml` file:
  15. ```Cargo
  16. [dependencies.url]
  17. git = "https://github.com/servo/rust-url"
  18. ```
  19. Supporting encodings other than UTF-8 in query strings is an optional feature
  20. that requires [rust-encoding](https://github.com/lifthrasiir/rust-encoding)
  21. and is off by default.
  22. You can enable it with
  23. [Cargo’s *features* mechanism](http://doc.crates.io/manifest.html#the-[features]-section):
  24. ```Cargo
  25. [dependencies.url]
  26. git = "https://github.com/servo/rust-url"
  27. features = ["query_encoding"]
  28. ```
  29. … or by passing `--cfg 'feature="query_encoding"'` to rustc.
  30. # URL parsing and data structures
  31. First, URL parsing may fail for various reasons and therefore returns a `Result`.
  32. ```
  33. use url::{Url, ParseError};
  34. assert!(Url::parse("http://[:::1]") == Err(ParseError::InvalidIpv6Address))
  35. ```
  36. Let’s parse a valid URL and look at its components.
  37. ```
  38. use url::{Url, SchemeData};
  39. let issue_list_url = Url::parse(
  40. "https://github.com/rust-lang/rust/issues?labels=E-easy&state=open"
  41. ).unwrap();
  42. assert!(issue_list_url.scheme == "https".to_string());
  43. assert!(issue_list_url.domain() == Some("github.com"));
  44. assert!(issue_list_url.port() == None);
  45. assert!(issue_list_url.path() == Some(["rust-lang".to_string(),
  46. "rust".to_string(),
  47. "issues".to_string()].as_slice()));
  48. assert!(issue_list_url.query == Some("labels=E-easy&state=open".to_string()));
  49. assert!(issue_list_url.fragment == None);
  50. match issue_list_url.scheme_data {
  51. SchemeData::Relative(..) => {}, // Expected
  52. SchemeData::NonRelative(..) => panic!(),
  53. }
  54. ```
  55. The `scheme`, `query`, and `fragment` are directly fields of the `Url` struct:
  56. they apply to all URLs.
  57. Every other components has accessors because they only apply to URLs said to be
  58. “in a relative scheme”. `https` is a relative scheme, but `data` is not:
  59. ```
  60. use url::{Url, SchemeData};
  61. let data_url = Url::parse("data:text/plain,Hello#").unwrap();
  62. assert!(data_url.scheme == "data".to_string());
  63. assert!(data_url.scheme_data == SchemeData::NonRelative("text/plain,Hello".to_string()));
  64. assert!(data_url.non_relative_scheme_data() == Some("text/plain,Hello"));
  65. assert!(data_url.query == None);
  66. assert!(data_url.fragment == Some("".to_string()));
  67. ```
  68. # Base URL
  69. Many contexts allow URL *references* that can be relative to a *base URL*:
  70. ```html
  71. <link rel="stylesheet" href="../main.css">
  72. ```
  73. Since parsed URL are absolute, giving a base is required:
  74. ```
  75. use url::{Url, ParseError};
  76. assert!(Url::parse("../main.css") == Err(ParseError::RelativeUrlWithoutBase))
  77. ```
  78. `UrlParser` is a method-chaining API to provide various optional parameters
  79. to URL parsing, including a base URL.
  80. ```
  81. use url::{Url, UrlParser};
  82. let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html").unwrap();
  83. let css_url = UrlParser::new().base_url(&this_document).parse("../main.css").unwrap();
  84. assert!(css_url.serialize() == "http://servo.github.io/rust-url/main.css".to_string());
  85. ```
  86. */
  87. #![allow(unstable)]
  88. extern crate "rustc-serialize" as rustc_serialize;
  89. #[macro_use]
  90. extern crate matches;
  91. use std::fmt::{self, Formatter};
  92. use std::hash;
  93. use std::path;
  94. pub use host::{Host, Ipv6Address};
  95. pub use parser::{ErrorHandler, ParseResult, ParseError};
  96. #[deprecated = "Moved to the `percent_encoding` module"]
  97. pub use percent_encoding::{
  98. percent_decode, percent_decode_to, percent_encode, percent_encode_to,
  99. utf8_percent_encode, utf8_percent_encode_to, lossy_utf8_percent_decode,
  100. SIMPLE_ENCODE_SET, QUERY_ENCODE_SET, DEFAULT_ENCODE_SET, USERINFO_ENCODE_SET,
  101. PASSWORD_ENCODE_SET, USERNAME_ENCODE_SET, FORM_URLENCODED_ENCODE_SET, EncodeSet,
  102. };
  103. use format::{PathFormatter, UserInfoFormatter, UrlNoFragmentFormatter};
  104. use encoding::EncodingOverride;
  105. mod encoding;
  106. mod host;
  107. mod parser;
  108. mod urlutils;
  109. pub mod percent_encoding;
  110. pub mod form_urlencoded;
  111. pub mod punycode;
  112. pub mod format;
  113. #[cfg(test)]
  114. mod tests;
  115. /// The parsed representation of an absolute URL.
  116. #[derive(PartialEq, Eq, Clone, Show)]
  117. pub struct Url {
  118. /// The scheme (a.k.a. protocol) of the URL, in ASCII lower case.
  119. pub scheme: String,
  120. /// The components of the URL whose representation depends on where the scheme is *relative*.
  121. pub scheme_data: SchemeData,
  122. /// The query string of the URL.
  123. ///
  124. /// `None` if the `?` delimiter character was not part of the parsed input,
  125. /// otherwise a possibly empty, pecent-encoded string.
  126. ///
  127. /// Percent encoded strings are within the ASCII range.
  128. ///
  129. /// See also the `query_pairs`, `set_query_from_pairs`,
  130. /// and `lossy_percent_decode_query` methods.
  131. pub query: Option<String>,
  132. /// The fragment identifier of the URL.
  133. ///
  134. /// `None` if the `#` delimiter character was not part of the parsed input,
  135. /// otherwise a possibly empty, pecent-encoded string.
  136. ///
  137. /// Percent encoded strings are within the ASCII range.
  138. ///
  139. /// See also the `lossy_percent_decode_fragment` method.
  140. pub fragment: Option<String>,
  141. }
  142. /// The components of the URL whose representation depends on where the scheme is *relative*.
  143. #[derive(PartialEq, Eq, Clone, Show)]
  144. pub enum SchemeData {
  145. /// Components for URLs in a *relative* scheme such as HTTP.
  146. Relative(RelativeSchemeData),
  147. /// No further structure is assumed for *non-relative* schemes such as `data` and `mailto`.
  148. ///
  149. /// This is a single percent-encoded string, whose interpretation depends on the scheme.
  150. ///
  151. /// Percent encoded strings are within the ASCII range.
  152. NonRelative(String),
  153. }
  154. /// Components for URLs in a *relative* scheme such as HTTP.
  155. #[derive(PartialEq, Eq, Clone, Show)]
  156. pub struct RelativeSchemeData {
  157. /// The username of the URL, as a possibly empty, pecent-encoded string.
  158. ///
  159. /// Percent encoded strings are within the ASCII range.
  160. ///
  161. /// See also the `lossy_percent_decode_username` method.
  162. pub username: String,
  163. /// The password of the URL.
  164. ///
  165. /// `None` if the `:` delimiter character was not part of the parsed input,
  166. /// otherwise a possibly empty, pecent-encoded string.
  167. ///
  168. /// Percent encoded strings are within the ASCII range.
  169. ///
  170. /// See also the `lossy_percent_decode_password` method.
  171. pub password: Option<String>,
  172. /// The host of the URL, either a domain name or an IPv4 address
  173. pub host: Host,
  174. /// The port number of the URL.
  175. /// `None` for file-like schemes, or to indicate the default port number.
  176. pub port: Option<u16>,
  177. /// The default port number for the URL’s scheme.
  178. /// `None` for file-like schemes.
  179. pub default_port: Option<u16>,
  180. /// The path of the URL, as vector of pecent-encoded strings.
  181. ///
  182. /// Percent encoded strings are within the ASCII range.
  183. ///
  184. /// See also the `serialize_path` method and,
  185. /// for URLs in the `file` scheme, the `to_file_path` method.
  186. pub path: Vec<String>,
  187. }
  188. impl<H: hash::Hasher + hash::Writer> hash::Hash<H> for Url {
  189. fn hash(&self, state: &mut H) {
  190. self.serialize().hash(state)
  191. }
  192. }
  193. /// A set of optional parameters for URL parsing.
  194. pub struct UrlParser<'a> {
  195. base_url: Option<&'a Url>,
  196. query_encoding_override: EncodingOverride,
  197. error_handler: ErrorHandler,
  198. scheme_type_mapper: fn(scheme: &str) -> SchemeType,
  199. }
  200. /// A method-chaining API to provide a set of optional parameters for URL parsing.
  201. impl<'a> UrlParser<'a> {
  202. /// Return a new UrlParser with default parameters.
  203. #[inline]
  204. pub fn new() -> UrlParser<'a> {
  205. fn silent_handler(_reason: ParseError) -> ParseResult<()> { Ok(()) }
  206. UrlParser {
  207. base_url: None,
  208. query_encoding_override: EncodingOverride::utf8(),
  209. error_handler: silent_handler,
  210. scheme_type_mapper: whatwg_scheme_type_mapper,
  211. }
  212. }
  213. /// Set the base URL used for resolving relative URL references, and return the `UrlParser`.
  214. /// The default is no base URL, so that relative URLs references fail to parse.
  215. #[inline]
  216. pub fn base_url<'b>(&'b mut self, value: &'a Url) -> &'b mut UrlParser<'a> {
  217. self.base_url = Some(value);
  218. self
  219. }
  220. /// Set the character encoding the query string is encoded as before percent-encoding,
  221. /// and return the `UrlParser`.
  222. ///
  223. /// This legacy quirk is only relevant to HTML.
  224. ///
  225. /// This method is only available if the `query_encoding` Cargo feature is enabled.
  226. #[cfg(feature = "query_encoding")]
  227. #[inline]
  228. pub fn query_encoding_override<'b>(&'b mut self, value: encoding::EncodingRef)
  229. -> &'b mut UrlParser<'a> {
  230. self.query_encoding_override = EncodingOverride::from_encoding(value);
  231. self
  232. }
  233. /// Set an error handler for non-fatal parse errors, and return the `UrlParser`.
  234. ///
  235. /// Non-fatal parse errors are normally ignored by the parser,
  236. /// but indicate violations of authoring requirements.
  237. /// An error handler can be used, for example, to log these errors in the console
  238. /// of a browser’s developer tools.
  239. ///
  240. /// The error handler can choose to make the error fatal by returning `Err(..)`
  241. #[inline]
  242. pub fn error_handler<'b>(&'b mut self, value: ErrorHandler) -> &'b mut UrlParser<'a> {
  243. self.error_handler = value;
  244. self
  245. }
  246. /// Set a *scheme type mapper*, and return the `UrlParser`.
  247. ///
  248. /// The URL parser behaves differently based on the `SchemeType` of the URL.
  249. /// See the documentation for `SchemeType` for more details.
  250. /// A *scheme type mapper* returns a `SchemeType`
  251. /// based on the scheme as an ASCII lower case string,
  252. /// as found in the `scheme` field of an `Url` struct.
  253. ///
  254. /// The default scheme type mapper is as follows:
  255. ///
  256. /// ```ignore
  257. /// fn whatwg_scheme_type_mapper(scheme: &str) -> SchemeType {
  258. /// match scheme {
  259. /// "file" => SchemeType::FileLike,
  260. /// "ftp" => SchemeType::Relative(21),
  261. /// "gopher" => SchemeType::Relative(70),
  262. /// "http" => SchemeType::Relative(80),
  263. /// "https" => SchemeType::Relative(443),
  264. /// "ws" => SchemeType::Relative(80),
  265. /// "wss" => SchemeType::Relative(443),
  266. /// _ => NonRelative,
  267. /// }
  268. /// }
  269. /// ```
  270. ///
  271. /// Note that unknown schemes default to non-relative.
  272. /// Overriding the scheme type mapper can allow, for example,
  273. /// parsing URLs in the `git` or `irc` scheme as relative.
  274. #[inline]
  275. pub fn scheme_type_mapper<'b>(&'b mut self, value: fn(scheme: &str) -> SchemeType)
  276. -> &'b mut UrlParser<'a> {
  277. self.scheme_type_mapper = value;
  278. self
  279. }
  280. /// Parse `input` as an URL, with all the parameters previously set in the `UrlParser`.
  281. #[inline]
  282. pub fn parse(&self, input: &str) -> ParseResult<Url> {
  283. parser::parse_url(input, self)
  284. }
  285. /// Parse `input` as a “standalone” URL path,
  286. /// with an optional query string and fragment identifier.
  287. ///
  288. /// This is typically found in the start line of an HTTP header.
  289. ///
  290. /// Note that while the start line has no fragment identifier in the HTTP RFC,
  291. /// servers typically parse it and ignore it
  292. /// (rather than having it be part of the path or query string.)
  293. ///
  294. /// On success, return `(path, query_string, fragment_identifier)`
  295. #[inline]
  296. pub fn parse_path(&self, input: &str)
  297. -> ParseResult<(Vec<String>, Option<String>, Option<String>)> {
  298. parser::parse_standalone_path(input, self)
  299. }
  300. }
  301. /// Parse `input` as a “standalone” URL path,
  302. /// with an optional query string and fragment identifier.
  303. ///
  304. /// This is typically found in the start line of an HTTP header.
  305. ///
  306. /// Note that while the start line has no fragment identifier in the HTTP RFC,
  307. /// servers typically parse it and ignore it
  308. /// (rather than having it be part of the path or query string.)
  309. ///
  310. /// On success, return `(path, query_string, fragment_identifier)`
  311. ///
  312. /// ```rust
  313. /// let (path, query, fragment) = url::parse_path("/foo/bar/../baz?q=42").unwrap();
  314. /// assert_eq!(path, vec!["foo".to_string(), "baz".to_string()]);
  315. /// assert_eq!(query, Some("q=42".to_string()));
  316. /// assert_eq!(fragment, None);
  317. /// ```
  318. #[inline]
  319. pub fn parse_path(input: &str)
  320. -> ParseResult<(Vec<String>, Option<String>, Option<String>)> {
  321. UrlParser::new().parse_path(input)
  322. }
  323. /// Private convenience methods for use in parser.rs
  324. impl<'a> UrlParser<'a> {
  325. #[inline]
  326. fn parse_error(&self, error: ParseError) -> ParseResult<()> {
  327. (self.error_handler)(error)
  328. }
  329. #[inline]
  330. fn get_scheme_type(&self, scheme: &str) -> SchemeType {
  331. (self.scheme_type_mapper)(scheme)
  332. }
  333. }
  334. /// Determines the behavior of the URL parser for a given scheme.
  335. #[derive(PartialEq, Eq, Copy, Show)]
  336. pub enum SchemeType {
  337. /// Indicate that the scheme is *non-relative*.
  338. ///
  339. /// The *scheme data* of the URL
  340. /// (everything other than the scheme, query string, and fragment identifier)
  341. /// is parsed as a single percent-encoded string of which no structure is assumed.
  342. /// That string may need to be parsed further, per a scheme-specific format.
  343. NonRelative,
  344. /// Indicate that the scheme is *relative*, and what the default port number is.
  345. ///
  346. /// The *scheme data* is structured as
  347. /// *username*, *password*, *host*, *port number*, and *path*.
  348. /// Relative URL references are supported, if a base URL was given.
  349. /// The string value indicates the default port number as a string of ASCII digits,
  350. /// or the empty string to indicate no default port number.
  351. Relative(u16),
  352. /// Indicate a *relative* scheme similar to the *file* scheme.
  353. ///
  354. /// For example, you might want to have distinct `git+file` and `hg+file` URL schemes.
  355. ///
  356. /// This is like `Relative` except the host can be empty, there is no port number,
  357. /// and path parsing has (platform-independent) quirks to support Windows filenames.
  358. FileLike,
  359. }
  360. impl SchemeType {
  361. pub fn default_port(&self) -> Option<u16> {
  362. match self {
  363. &SchemeType::Relative(default_port) => Some(default_port),
  364. _ => None,
  365. }
  366. }
  367. }
  368. /// http://url.spec.whatwg.org/#relative-scheme
  369. pub fn whatwg_scheme_type_mapper(scheme: &str) -> SchemeType {
  370. match scheme {
  371. "file" => SchemeType::FileLike,
  372. "ftp" => SchemeType::Relative(21),
  373. "gopher" => SchemeType::Relative(70),
  374. "http" => SchemeType::Relative(80),
  375. "https" => SchemeType::Relative(443),
  376. "ws" => SchemeType::Relative(80),
  377. "wss" => SchemeType::Relative(443),
  378. _ => SchemeType::NonRelative,
  379. }
  380. }
  381. impl Url {
  382. /// Parse an URL with the default `UrlParser` parameters.
  383. ///
  384. /// In particular, relative URL references are parse errors since no base URL is provided.
  385. #[inline]
  386. pub fn parse(input: &str) -> ParseResult<Url> {
  387. UrlParser::new().parse(input)
  388. }
  389. /// Convert a file name as `std::path::Path` into an URL in the `file` scheme.
  390. ///
  391. /// This returns `Err` if the given path is not absolute
  392. /// or, with a Windows path, if the prefix is not a disk prefix (e.g. `C:`).
  393. pub fn from_file_path<T: ToUrlPath>(path: &T) -> Result<Url, ()> {
  394. let path = try!(path.to_url_path());
  395. Ok(Url::from_path_common(path))
  396. }
  397. /// Convert a directory name as `std::path::Path` into an URL in the `file` scheme.
  398. ///
  399. /// This returns `Err` if the given path is not absolute
  400. /// or, with a Windows path, if the prefix is not a disk prefix (e.g. `C:`).
  401. ///
  402. /// Compared to `from_file_path`, this adds an empty component to the path
  403. /// (or, in terms of URL syntax, adds a trailing slash)
  404. /// so that the entire path is considered when using this URL as a base URL.
  405. ///
  406. /// For example:
  407. ///
  408. /// * `"index.html"` parsed with `Url::from_directory_path(Path::new("/var/www"))`
  409. /// as the base URL is `file:///var/www/index.html`
  410. /// * `"index.html"` parsed with `Url::from_file_path(Path::new("/var/www/"))`
  411. /// as the base URL is `file:///var/index.html`, which might not be what was intended.
  412. ///
  413. /// (Note that `Path::new` removes any trailing slash.)
  414. pub fn from_directory_path<T: ToUrlPath>(path: &T) -> Result<Url, ()> {
  415. let mut path = try!(path.to_url_path());
  416. // Add an empty path component (i.e. a trailing slash in serialization)
  417. // so that the entire path is used as a base URL.
  418. path.push("".to_string());
  419. Ok(Url::from_path_common(path))
  420. }
  421. fn from_path_common(path: Vec<String>) -> Url {
  422. Url {
  423. scheme: "file".to_string(),
  424. scheme_data: SchemeData::Relative(RelativeSchemeData {
  425. username: "".to_string(),
  426. password: None,
  427. port: None,
  428. default_port: None,
  429. host: Host::Domain("".to_string()),
  430. path: path,
  431. }),
  432. query: None,
  433. fragment: None,
  434. }
  435. }
  436. /// Assuming the URL is in the `file` scheme or similar,
  437. /// convert its path to an absolute `std::path::Path`.
  438. ///
  439. /// **Note:** This does not actually check the URL’s `scheme`,
  440. /// and may give nonsensical results for other schemes.
  441. /// It is the user’s responsibility to check the URL’s scheme before calling this.
  442. ///
  443. /// The return type (when `Ok()`) is generic and can be either `std::path::posix::Path`
  444. /// or `std::path::windows::Path`.
  445. /// (Use `std::path::Path` to pick one of them depending on the local system.)
  446. /// If the compiler can not infer the desired type from context, you may have to specifiy it:
  447. ///
  448. /// ```ignore
  449. /// let path = url.to_file_path::<std::path::posix::Path>();
  450. /// ```
  451. ///
  452. /// Returns `Err` if the host is neither empty nor `"localhost"`,
  453. /// or if `Path::new_opt()` returns `None`.
  454. /// (That is, if the percent-decoded path contains a NUL byte or,
  455. /// for a Windows path, is not UTF-8.)
  456. #[inline]
  457. pub fn to_file_path<T: FromUrlPath>(&self) -> Result<T, ()> {
  458. match self.scheme_data {
  459. SchemeData::Relative(ref scheme_data) => scheme_data.to_file_path(),
  460. SchemeData::NonRelative(..) => Err(()),
  461. }
  462. }
  463. /// Return the serialization of this URL as a string.
  464. pub fn serialize(&self) -> String {
  465. self.to_string()
  466. }
  467. /// Return the serialization of this URL, without the fragment identifier, as a string
  468. pub fn serialize_no_fragment(&self) -> String {
  469. UrlNoFragmentFormatter{ url: self }.to_string()
  470. }
  471. /// If the URL is *non-relative*, return the string scheme data.
  472. #[inline]
  473. pub fn non_relative_scheme_data<'a>(&'a self) -> Option<&'a str> {
  474. match self.scheme_data {
  475. SchemeData::Relative(..) => None,
  476. SchemeData::NonRelative(ref scheme_data) => Some(scheme_data.as_slice()),
  477. }
  478. }
  479. /// If the URL is *non-relative*, return a mutable reference to the string scheme data.
  480. #[inline]
  481. pub fn non_relative_scheme_data_mut<'a>(&'a mut self) -> Option<&'a mut String> {
  482. match self.scheme_data {
  483. SchemeData::Relative(..) => None,
  484. SchemeData::NonRelative(ref mut scheme_data) => Some(scheme_data),
  485. }
  486. }
  487. /// If the URL is in a *relative scheme*, return the structured scheme data.
  488. #[inline]
  489. pub fn relative_scheme_data<'a>(&'a self) -> Option<&'a RelativeSchemeData> {
  490. match self.scheme_data {
  491. SchemeData::Relative(ref scheme_data) => Some(scheme_data),
  492. SchemeData::NonRelative(..) => None,
  493. }
  494. }
  495. /// If the URL is in a *relative scheme*,
  496. /// return a mutable reference to the structured scheme data.
  497. #[inline]
  498. pub fn relative_scheme_data_mut<'a>(&'a mut self) -> Option<&'a mut RelativeSchemeData> {
  499. match self.scheme_data {
  500. SchemeData::Relative(ref mut scheme_data) => Some(scheme_data),
  501. SchemeData::NonRelative(..) => None,
  502. }
  503. }
  504. /// If the URL is in a *relative scheme*, return its username.
  505. #[inline]
  506. pub fn username<'a>(&'a self) -> Option<&'a str> {
  507. self.relative_scheme_data().map(|scheme_data| scheme_data.username.as_slice())
  508. }
  509. /// If the URL is in a *relative scheme*, return a mutable reference to its username.
  510. #[inline]
  511. pub fn username_mut<'a>(&'a mut self) -> Option<&'a mut String> {
  512. self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.username)
  513. }
  514. /// Percent-decode the URL’s username, if any.
  515. ///
  516. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  517. /// will be replaced � U+FFFD, the replacement character.
  518. #[inline]
  519. pub fn lossy_percent_decode_username(&self) -> Option<String> {
  520. self.relative_scheme_data().map(|scheme_data| scheme_data.lossy_percent_decode_username())
  521. }
  522. /// If the URL is in a *relative scheme*, return its password, if any.
  523. #[inline]
  524. pub fn password<'a>(&'a self) -> Option<&'a str> {
  525. self.relative_scheme_data().and_then(|scheme_data|
  526. scheme_data.password.as_ref().map(|password| password.as_slice()))
  527. }
  528. /// If the URL is in a *relative scheme*, return a mutable reference to its password, if any.
  529. #[inline]
  530. pub fn password_mut<'a>(&'a mut self) -> Option<&'a mut String> {
  531. self.relative_scheme_data_mut().and_then(|scheme_data| scheme_data.password.as_mut())
  532. }
  533. /// Percent-decode the URL’s password, if any.
  534. ///
  535. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  536. /// will be replaced � U+FFFD, the replacement character.
  537. #[inline]
  538. pub fn lossy_percent_decode_password(&self) -> Option<String> {
  539. self.relative_scheme_data().and_then(|scheme_data|
  540. scheme_data.lossy_percent_decode_password())
  541. }
  542. /// Serialize the URL's username and password, if any.
  543. ///
  544. /// Format: "<username>:<password>@"
  545. #[inline]
  546. pub fn serialize_userinfo<'a>(&'a mut self) -> Option<String> {
  547. self.relative_scheme_data().map(|scheme_data| scheme_data.serialize_userinfo())
  548. }
  549. /// If the URL is in a *relative scheme*, return its structured host.
  550. #[inline]
  551. pub fn host<'a>(&'a self) -> Option<&'a Host> {
  552. self.relative_scheme_data().map(|scheme_data| &scheme_data.host)
  553. }
  554. /// If the URL is in a *relative scheme*, return a mutable reference to its structured host.
  555. #[inline]
  556. pub fn host_mut<'a>(&'a mut self) -> Option<&'a mut Host> {
  557. self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.host)
  558. }
  559. /// If the URL is in a *relative scheme* and its host is a domain,
  560. /// return the domain as a string.
  561. #[inline]
  562. pub fn domain<'a>(&'a self) -> Option<&'a str> {
  563. self.relative_scheme_data().and_then(|scheme_data| scheme_data.domain())
  564. }
  565. /// If the URL is in a *relative scheme* and its host is a domain,
  566. /// return a mutable reference to the domain string.
  567. #[inline]
  568. pub fn domain_mut<'a>(&'a mut self) -> Option<&'a mut String> {
  569. self.relative_scheme_data_mut().and_then(|scheme_data| scheme_data.domain_mut())
  570. }
  571. /// If the URL is in a *relative scheme*, serialize its host as a string.
  572. ///
  573. /// A domain a returned as-is, an IPv6 address between [] square brackets.
  574. #[inline]
  575. pub fn serialize_host(&self) -> Option<String> {
  576. self.relative_scheme_data().map(|scheme_data| scheme_data.host.serialize())
  577. }
  578. /// If the URL is in a *relative scheme* and has a port number, return it.
  579. #[inline]
  580. pub fn port<'a>(&'a self) -> Option<u16> {
  581. self.relative_scheme_data().and_then(|scheme_data| scheme_data.port)
  582. }
  583. /// If the URL is in a *relative scheme*, return a mutable reference to its port.
  584. #[inline]
  585. pub fn port_mut<'a>(&'a mut self) -> Option<&'a mut Option<u16>> {
  586. self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.port)
  587. }
  588. /// If the URL is in a *relative scheme* that is not a file-like,
  589. /// return its port number, even if it is the default.
  590. #[inline]
  591. pub fn port_or_default(&self) -> Option<u16> {
  592. self.relative_scheme_data().and_then(|scheme_data| scheme_data.port_or_default())
  593. }
  594. /// If the URL is in a *relative scheme*, return its path components.
  595. #[inline]
  596. pub fn path<'a>(&'a self) -> Option<&'a [String]> {
  597. self.relative_scheme_data().map(|scheme_data| scheme_data.path.as_slice())
  598. }
  599. /// If the URL is in a *relative scheme*, return a mutable reference to its path components.
  600. #[inline]
  601. pub fn path_mut<'a>(&'a mut self) -> Option<&'a mut Vec<String>> {
  602. self.relative_scheme_data_mut().map(|scheme_data| &mut scheme_data.path)
  603. }
  604. /// If the URL is in a *relative scheme*, serialize its path as a string.
  605. ///
  606. /// The returned string starts with a "/" slash, and components are separated by slashes.
  607. /// A trailing slash represents an empty last component.
  608. #[inline]
  609. pub fn serialize_path(&self) -> Option<String> {
  610. self.relative_scheme_data().map(|scheme_data| scheme_data.serialize_path())
  611. }
  612. /// Parse the URL’s query string, if any, as `application/x-www-form-urlencoded`
  613. /// and return a vector of (key, value) pairs.
  614. #[inline]
  615. pub fn query_pairs(&self) -> Option<Vec<(String, String)>> {
  616. self.query.as_ref().map(|query| form_urlencoded::parse(query.as_bytes()))
  617. }
  618. /// Serialize an iterator of (key, value) pairs as `application/x-www-form-urlencoded`
  619. /// and set it as the URL’s query string.
  620. #[inline]
  621. pub fn set_query_from_pairs<'a, I: Iterator<Item = (&'a str, &'a str)>>(&mut self, pairs: I) {
  622. self.query = Some(form_urlencoded::serialize(pairs));
  623. }
  624. /// Percent-decode the URL’s query string, if any.
  625. ///
  626. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  627. /// will be replaced � U+FFFD, the replacement character.
  628. #[inline]
  629. pub fn lossy_percent_decode_query(&self) -> Option<String> {
  630. self.query.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
  631. }
  632. /// Percent-decode the URL’s fragment identifier, if any.
  633. ///
  634. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  635. /// will be replaced � U+FFFD, the replacement character.
  636. #[inline]
  637. pub fn lossy_percent_decode_fragment(&self) -> Option<String> {
  638. self.fragment.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
  639. }
  640. }
  641. impl rustc_serialize::Encodable for Url {
  642. fn encode<S: rustc_serialize::Encoder>(&self, encoder: &mut S) -> Result<(), S::Error> {
  643. encoder.emit_str(self.to_string().as_slice())
  644. }
  645. }
  646. impl rustc_serialize::Decodable for Url {
  647. fn decode<D: rustc_serialize::Decoder>(decoder: &mut D) -> Result<Url, D::Error> {
  648. Url::parse(try!(decoder.read_str()).as_slice()).map_err(|error| {
  649. decoder.error(format!("URL parsing error: {}", error).as_slice())
  650. })
  651. }
  652. }
  653. impl fmt::String for Url {
  654. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
  655. try!(UrlNoFragmentFormatter{ url: self }.fmt(formatter));
  656. if let Some(ref fragment) = self.fragment {
  657. try!(formatter.write_str("#"));
  658. try!(formatter.write_str(fragment.as_slice()));
  659. }
  660. Ok(())
  661. }
  662. }
  663. impl fmt::String for SchemeData {
  664. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
  665. match *self {
  666. SchemeData::Relative(ref scheme_data) => scheme_data.fmt(formatter),
  667. SchemeData::NonRelative(ref scheme_data) => scheme_data.fmt(formatter),
  668. }
  669. }
  670. }
  671. impl RelativeSchemeData {
  672. /// Percent-decode the URL’s username.
  673. ///
  674. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  675. /// will be replaced � U+FFFD, the replacement character.
  676. #[inline]
  677. pub fn lossy_percent_decode_username(&self) -> String {
  678. lossy_utf8_percent_decode(self.username.as_bytes())
  679. }
  680. /// Percent-decode the URL’s password, if any.
  681. ///
  682. /// This is “lossy”: invalid UTF-8 percent-encoded byte sequences
  683. /// will be replaced � U+FFFD, the replacement character.
  684. #[inline]
  685. pub fn lossy_percent_decode_password(&self) -> Option<String> {
  686. self.password.as_ref().map(|value| lossy_utf8_percent_decode(value.as_bytes()))
  687. }
  688. /// Assuming the URL is in the `file` scheme or similar,
  689. /// convert its path to an absolute `std::path::Path`.
  690. ///
  691. /// **Note:** This does not actually check the URL’s `scheme`,
  692. /// and may give nonsensical results for other schemes.
  693. /// It is the user’s responsibility to check the URL’s scheme before calling this.
  694. ///
  695. /// The return type (when `Ok()`) is generic and can be either `std::path::posix::Path`
  696. /// or `std::path::windows::Path`.
  697. /// (Use `std::path::Path` to pick one of them depending on the local system.)
  698. /// If the compiler can not infer the desired type from context, you may have to specifiy it:
  699. ///
  700. /// ```ignore
  701. /// let path = url.to_file_path::<std::path::posix::Path>();
  702. /// ```
  703. ///
  704. /// Returns `Err` if the host is neither empty nor `"localhost"`,
  705. /// or if `Path::new_opt()` returns `None`.
  706. /// (That is, if the percent-decoded path contains a NUL byte or,
  707. /// for a Windows path, is not UTF-8.)
  708. #[inline]
  709. pub fn to_file_path<T: FromUrlPath>(&self) -> Result<T, ()> {
  710. // FIXME: Figure out what to do w.r.t host.
  711. match self.domain() {
  712. Some("") | Some("localhost") => FromUrlPath::from_url_path(self.path.as_slice()),
  713. _ => Err(())
  714. }
  715. }
  716. /// If the host is a domain, return the domain as a string.
  717. #[inline]
  718. pub fn domain<'a>(&'a self) -> Option<&'a str> {
  719. match self.host {
  720. Host::Domain(ref domain) => Some(domain.as_slice()),
  721. _ => None,
  722. }
  723. }
  724. /// If the host is a domain, return a mutable reference to the domain string.
  725. #[inline]
  726. pub fn domain_mut<'a>(&'a mut self) -> Option<&'a mut String> {
  727. match self.host {
  728. Host::Domain(ref mut domain) => Some(domain),
  729. _ => None,
  730. }
  731. }
  732. /// Return the port number of the URL, even if it is the default.
  733. /// Return `None` for file-like URLs.
  734. #[inline]
  735. pub fn port_or_default(&self) -> Option<u16> {
  736. self.port.or(self.default_port)
  737. }
  738. /// Serialize the path as a string.
  739. ///
  740. /// The returned string starts with a "/" slash, and components are separated by slashes.
  741. /// A trailing slash represents an empty last component.
  742. pub fn serialize_path(&self) -> String {
  743. PathFormatter {
  744. path: self.path.as_slice()
  745. }.to_string()
  746. }
  747. /// Serialize the userinfo as a string.
  748. ///
  749. /// Format: "<username>:<password>@".
  750. pub fn serialize_userinfo(&self) -> String {
  751. UserInfoFormatter {
  752. username: self.username.as_slice(),
  753. password: self.password.as_ref().map(|s| s.as_slice())
  754. }.to_string()
  755. }
  756. }
  757. impl fmt::String for RelativeSchemeData {
  758. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
  759. // Write the scheme-trailing double slashes.
  760. try!(formatter.write_str("//"));
  761. // Write the user info.
  762. try!(UserInfoFormatter {
  763. username: self.username.as_slice(),
  764. password: self.password.as_ref().map(|s| s.as_slice())
  765. }.fmt(formatter));
  766. // Write the host.
  767. try!(self.host.fmt(formatter));
  768. // Write the port.
  769. match self.port {
  770. Some(port) => {
  771. try!(write!(formatter, ":{}", port));
  772. },
  773. None => {}
  774. }
  775. // Write the path.
  776. PathFormatter {
  777. path: self.path.as_slice()
  778. }.fmt(formatter)
  779. }
  780. }
  781. pub trait ToUrlPath {
  782. fn to_url_path(&self) -> Result<Vec<String>, ()>;
  783. }
  784. impl ToUrlPath for path::posix::Path {
  785. fn to_url_path(&self) -> Result<Vec<String>, ()> {
  786. if !self.is_absolute() {
  787. return Err(())
  788. }
  789. Ok(self.components().map(|c| percent_encode(c, DEFAULT_ENCODE_SET)).collect())
  790. }
  791. }
  792. impl ToUrlPath for path::windows::Path {
  793. fn to_url_path(&self) -> Result<Vec<String>, ()> {
  794. if !self.is_absolute() {
  795. return Err(())
  796. }
  797. if path::windows::prefix(self) != Some(path::windows::PathPrefix::DiskPrefix) {
  798. // FIXME: do something with UNC and other prefixes?
  799. return Err(())
  800. }
  801. // Start with the prefix, e.g. "C:"
  802. let mut path = vec![self.as_str().unwrap().slice_to(2).to_string()];
  803. // self.components() does not include the prefix
  804. for component in self.components() {
  805. path.push(percent_encode(component, DEFAULT_ENCODE_SET));
  806. }
  807. Ok(path)
  808. }
  809. }
  810. pub trait FromUrlPath {
  811. fn from_url_path(path: &[String]) -> Result<Self, ()>;
  812. }
  813. impl FromUrlPath for path::posix::Path {
  814. fn from_url_path(path: &[String]) -> Result<path::posix::Path, ()> {
  815. if path.is_empty() {
  816. return Ok(path::posix::Path::new("/"))
  817. }
  818. let mut bytes = Vec::new();
  819. for path_part in path.iter() {
  820. bytes.push(b'/');
  821. percent_decode_to(path_part.as_bytes(), &mut bytes);
  822. }
  823. match path::posix::Path::new_opt(bytes) {
  824. None => Err(()), // Path contains a NUL byte
  825. Some(path) => {
  826. debug_assert!(path.is_absolute(),
  827. "to_file_path() failed to produce an absolute Path");
  828. Ok(path)
  829. }
  830. }
  831. }
  832. }
  833. impl FromUrlPath for path::windows::Path {
  834. fn from_url_path(path: &[String]) -> Result<path::windows::Path, ()> {
  835. if path.is_empty() {
  836. return Err(())
  837. }
  838. let prefix = &path[0][];
  839. if prefix.len() != 2 || !parser::starts_with_ascii_alpha(prefix)
  840. || prefix.as_bytes()[1] != b':' {
  841. return Err(())
  842. }
  843. let mut bytes = prefix.as_bytes().to_vec();
  844. for path_part in path[1..].iter() {
  845. bytes.push(b'\\');
  846. percent_decode_to(path_part.as_bytes(), &mut bytes);
  847. }
  848. match path::windows::Path::new_opt(bytes) {
  849. None => Err(()), // Path contains a NUL byte or invalid UTF-8
  850. Some(path) => {
  851. debug_assert!(path.is_absolute(),
  852. "to_file_path() failed to produce an absolute Path");
  853. debug_assert!(path::windows::prefix(&path) == Some(path::windows::PathPrefix::DiskPrefix),
  854. "to_file_path() failed to produce a Path with a disk prefix");
  855. Ok(path)
  856. }
  857. }
  858. }
  859. }