Skip to content

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.

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}")
}
https
me
example.com
8443
/docs/a%20b
q=brass&page=2
top
q = brass
page = 2

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=en
import url
let e = url.encode("a b/c?d")
print(e)
print(try url.decode(&e))
a%20b%2Fc%3Fd
a b/c?d
import url
pub fn encode(text: &Str) -> Str

Percent-encodes everything but letters, digits and -._~, so the text is safe in any part of a URL.

pub fn decode(text: &Str) -> Str ! UrlError

Undoes 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)>) -> Str

name=value pairs joined by &, every name and value encoded.

pub fn parse_query(text: &Str) -> Vec<(Str, Str)> ! UrlError

Reads 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 ! UrlError

Splits 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.com
refused: a URL cannot contain spaces or control characters
refused: bad port in http://example.com:99999/
refused: a URL starts with a scheme such as `https:`: example.com/page
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.

pub fn scheme(&self) -> Str

Always lower case, such as https.

pub fn user(&self) -> Str

The part before @, or empty.

pub fn host(&self) -> Str

Without brackets for IPv6 addresses, lower case.

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).

pub fn path(&self) -> Str

The path, / when the URL has an authority but no path.

pub fn query(&self) -> Str

The text after ?, without it.

pub fn fragment(&self) -> Str

The text after #, without it.

pub fn query_pairs(&self) -> Vec<(Str, Str)> ! UrlError

The query read into name/value pairs.

pub fn to_str(&self) -> Str

The URL written back out from its parts.