Skip to content

std/url ​

URL parsing and manipulation.

milo
from "std/url" import { Url }

Example ​

milo
let url = Url.parse("https://example.com:8080/api?name=milo&v=1#top")!
print(url.scheme)  // https
print(url.host)    // example.com
print(url.port)    // 8080

match url.queryGet("name") {
    Some(val) => print(val),  // milo
    None => {},
}

API reference ​

Url ​

milo
pub struct Url

A parsed URL, split into its components. raw keeps the original string.

Fields: scheme: string, host: string, port: i32, path: string, query: string, fragment: string, raw: string.

Url.parse ​

milo
fn Url.parse(s: string): Result<Url>

Parse a URL string into its components.

Url.queryGet ​

milo
fn Url.queryGet(self: &Url, key: &string): Option<string>

The value of query parameter key, or None if the query has no such key.

Url.toString ​

milo
fn Url.toString(self: &Url): string

Reassemble the URL from its components.

Functions ​

decodeUriComponent ​

milo
pub fn decodeUriComponent(s: &string): Result<string>

JavaScript's decodeURIComponent: each %XX back to its byte, '+' left alone. An error (naming the byte offset) where JS throws URIError: a '%' without two hex digits after it, or escapes that do not spell valid UTF-8 (a stray continuation byte, an overlong form, a surrogate, a code point past U+10FFFF). A request path decoded with a lenient decoder instead would let malformed input through as if it were text.

resolveUrl ​

milo
pub fn resolveUrl(base: &string, ref: &string): string

The absolute URL ref names when it appears in a document at base, per RFC 3986 §5.2: "../img.png", "//cdn.example/x", "?page=2" and "#top" all resolve as a browser resolves an href. An absolute ref comes back with its dot segments removed. No percent-decoding, case folding or validation: a malformed input resolves to whatever the algorithm makes of it, so check the result before using it.