url
Parse URLs, percent-encode text, and build or read query strings.
url.parse splits a URL into its parts following RFC 3986 and works for any scheme (https:, mailto:, file:). The http module has its own, narrower http.Url for the addresses it connects to; http.get and friends take the URL as text.
Take a URL apart
Section titled “Take a URL apart”import url
let u = try url.parse("HTTPS://me@Example.com:8443/docs/a%20b?q=brass&page=2#top")print(u.scheme())print(u.user())print(u.host())print(u.port().unwrap_or(0))print(u.path())print(u.query())print(u.fragment())for (name, value) in try u.query_pairs() { print(f"{name} = {value}")}httpsmeexample.com8443/docs/a%20bq=brass&page=2topq = brasspage = 2Build a query string
Section titled “Build a query string”query encodes every name and value, so any text is safe to put in.
import url
let q = url.query(&[("q", "fish & chips"), ("lang", "en")])print("https://example.com/search?" + q)https://example.com/search?q=fish%20%26%20chips&lang=enEncode and decode one value
Section titled “Encode and decode one value”import url
let e = url.encode("a b/c?d")print(e)print(try url.decode(&e))a%20b%2Fc%3Fda b/c?dReference
Section titled “Reference”import urlFunctions
Section titled “Functions”encode
Section titled “encode”pub fn encode(text: &Str) -> StrPercent-encodes everything but letters, digits and -._~, so the text is safe in any part of a URL.
decode
Section titled “decode”pub fn decode(text: &Str) -> Str ! UrlErrorUndoes encode; a % without two hex digits after it is an error, and + stays +.
+ stays + here; only parse_query reads + as a space, the way HTML forms send it. The result can hold any bytes, including ones that are not valid UTF-8 (%FF).
pub fn query(pairs: &Vec<(Str, Str)>) -> Strname=value pairs joined by &, every name and value encoded.
parse_query
Section titled “parse_query”pub fn parse_query(text: &Str) -> Vec<(Str, Str)> ! UrlErrorReads a=1&b=two+words (a leading ? is skipped); + means a space, as browsers send forms.
A leading ? is skipped, empty parts are skipped, and a name without = gets the value "". The pairs keep their order, and a name that appears twice gives two pairs.
pub fn parse(text: &Str) -> Url ! UrlErrorSplits scheme://user@host:port/path?query#fragment (RFC 3986); refuses spaces and control bytes.
The scheme and host are lower-cased; everything else is kept as written, still percent-encoded. Use decode on the path when you need the text. A URL with an authority but no path gets the path /. A missing scheme, spaces, control characters, a bad port or an empty host after // are errors.
import url
for text in ["https://Example.com", "https://exa mple.com", "http://example.com:99999/", "example.com/page"] { let u = url.parse(&text) catch { url.UrlError(e) => { print(f"refused: {e.message}") continue } } print(u.host())}example.comrefused: a URL cannot contain spaces or control charactersrefused: bad port in http://example.com:99999/refused: a URL starts with a scheme such as `https:`: example.com/pageUrlError
Section titled “UrlError”pub struct UrlError { pub message: Str}pub struct Url {}Made only by parse; the parts are kept as written, still percent-encoded.
It also has 7 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.
Url.scheme
Section titled “Url.scheme”pub fn scheme(&self) -> StrAlways lower case, such as https.
Url.user
Section titled “Url.user”pub fn user(&self) -> StrThe part before @, or empty.
Url.host
Section titled “Url.host”pub fn host(&self) -> StrWithout brackets for IPv6 addresses, lower case.
Url.port
Section titled “Url.port”pub fn port(&self) -> Option<i64>The port written in the URL, if any.
None when the URL does not name a port. It does not fill in the scheme’s default (443 for https).
Url.path
Section titled “Url.path”pub fn path(&self) -> StrThe path, / when the URL has an authority but no path.
Url.query
Section titled “Url.query”pub fn query(&self) -> StrThe text after ?, without it.
Url.fragment
Section titled “Url.fragment”pub fn fragment(&self) -> StrThe text after #, without it.
Url.query_pairs
Section titled “Url.query_pairs”pub fn query_pairs(&self) -> Vec<(Str, Str)> ! UrlErrorThe query read into name/value pairs.
Url.to_str
Section titled “Url.to_str”pub fn to_str(&self) -> StrThe URL written back out from its parts.