#url #parser

url

URL library for Rust, based on the WHATWG URL Standard

69 releases (17 stable)

1.7.1 Jul 5, 2018
1.7.0 Feb 20, 2018
1.6.1 Aug 23, 2018
1.6.0 Oct 31, 2017
0.2.1 Nov 24, 2014

#1 in Parser implementations

Download history 41261/week @ 2018-07-20 41976/week @ 2018-07-27 44632/week @ 2018-08-03 45454/week @ 2018-08-10 41103/week @ 2018-08-17 39861/week @ 2018-08-24 43324/week @ 2018-08-31 43428/week @ 2018-09-07 48372/week @ 2018-09-14 46278/week @ 2018-09-21 44859/week @ 2018-09-28 47718/week @ 2018-10-05 44514/week @ 2018-10-12

176,735 downloads per month
Used in 2,537 crates (789 directly)

MIT/Apache

791KB
17K SLoC

rust-url

Travis build Status Appveyor build status

URL library for Rust, based on the URL Standard.

Documentation

Please see UPGRADING.md if you are upgrading from 0.x to 1.x.


lib.rs:

rust-url is an implementation of the URL Standard for the Rust programming language.

URL parsing and data structures

First, URL parsing may fail for various reasons and therefore returns a Result.

use url::{Url, ParseError};

assert!(Url::parse("http://[:::1]") == Err(ParseError::InvalidIpv6Address))

Let’s parse a valid URL and look at its components.

use url::{Url, Host};
# use url::ParseError;
# fn run() -> Result<(), ParseError> {
let issue_list_url = Url::parse(
"https://github.com/rust-lang/rust/issues?labels=E-easy&state=open"
)?;


assert!(issue_list_url.scheme() == "https");
assert!(issue_list_url.username() == "");
assert!(issue_list_url.password() == None);
assert!(issue_list_url.host_str() == Some("github.com"));
assert!(issue_list_url.host() == Some(Host::Domain("github.com")));
assert!(issue_list_url.port() == None);
assert!(issue_list_url.path() == "/rust-lang/rust/issues");
assert!(issue_list_url.path_segments().map(|c| c.collect::<Vec<_>>()) ==
Some(vec!["rust-lang", "rust", "issues"]));
assert!(issue_list_url.query() == Some("labels=E-easy&state=open"));
assert!(issue_list_url.fragment() == None);
assert!(!issue_list_url.cannot_be_a_base());
# Ok(())
# }
# run().unwrap();

Some URLs are said to be cannot-be-a-base: they don’t have a username, password, host, or port, and their "path" is an arbitrary string rather than slash-separated segments:

use url::Url;
# use url::ParseError;

# fn run() -> Result<(), ParseError> {
let data_url = Url::parse("data:text/plain,Hello?World#")?;

assert!(data_url.cannot_be_a_base());
assert!(data_url.scheme() == "data");
assert!(data_url.path() == "text/plain,Hello");
assert!(data_url.path_segments().is_none());
assert!(data_url.query() == Some("World"));
assert!(data_url.fragment() == Some(""));
# Ok(())
# }
# run().unwrap();

Base URL

Many contexts allow URL references that can be relative to a base URL:

<link rel="stylesheet" href="../main.css">

Since parsed URL are absolute, giving a base is required for parsing relative URLs:

use url::{Url, ParseError};

assert!(Url::parse("../main.css") == Err(ParseError::RelativeUrlWithoutBase))

Use the join method on an Url to use it as a base URL:

use url::Url;
# use url::ParseError;

# fn run() -> Result<(), ParseError> {
let this_document = Url::parse("http://servo.github.io/rust-url/url/index.html")?;
let css_url = this_document.join("../main.css")?;
assert_eq!(css_url.as_str(), "http://servo.github.io/rust-url/main.css");
# Ok(())
# }
# run().unwrap();

Dependencies

~4.5MB
~127K SLoC