std/url
URL parsing and manipulation.
from "std/url" import { Url }Example
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
pub struct UrlA 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
fn Url.parse(s: string): Result<Url>Parse a URL string into its components.
Url.queryGet
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
fn Url.toString(self: &Url): stringReassemble the URL from its components.
Functions
decodeUriComponent
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
pub fn resolveUrl(base: &string, ref: &string): stringThe 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.