Module std::net::url

URL parsing.

Parse URLs into their components (scheme, host, port, path, query, fragment) and reconstruct or join them.

Examples

import std.net.url;

fn main() {
    let u = url.parse("https://example.com:8080/path?key=val#frag");
    println(u.host());
    match u.port() {
        .Some(port) => println(port),
        .None => println("no port"),
    }
    u.close();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Url

Parse a URL string.

Panics if the string is not a valid URL.

Examples

import std.net.url;

fn main() {
    let u = url.parse("https://example.com/api/v1");
}

Function encode

pub fn encode(s: string) -> string

Percent-encode a string (RFC 3986 unreserved characters are left unchanged).

Examples

import std.net.url;

fn main() {
    let encoded = url.encode("hello world");  // "hello%20world"
}

Function decode

pub fn decode(s: string) -> string

Decode a percent-encoded string.

Returns an empty string on invalid UTF-8. Valid decoded NUL is preserved.

Examples

import std.net.url;

fn main() {
    let decoded = url.decode("hello%20world");  // "hello world"
}

Function decode_query

pub fn decode_query(params: string) -> Vec<(string, string)>

Decode an application/x-www-form-urlencoded query string into (key, value) pairs.

Entries are separated by &; each entry must contain = and entries without one are skipped, matching encode_query's invalid-line convention. Percent-escapes are decoded like [decode], and + decodes to a space.

Examples

import std.net.url;

fn main() {
    let pairs = url.decode_query("name=hello+world&x=a%2Bb");
    // [("name", "hello world"), ("x", "a+b")]
}

Function encode_query

pub fn encode_query(params: string) -> string

Build a percent-encoded query string from a newline-separated list of key=value pairs.

Each line should be key=value. Lines without = are skipped. Pairs are joined with &.

Examples

import std.net.url;

fn main() {
    let qs = url.encode_query("name=hello world\npage=1");
    // "name=hello%20world&page=1"
}

Types

Struct Url

Fields

handle: UrlHandle

Traits

Trait UrlMethods

Methods available on a parsed Url.

Methods

fn scheme(self: Self) -> string

Return the URL scheme (e.g. "https").

fn host(self: Self) -> string

Return the host name.

fn port(self: Self) -> Option<i64>

Return the explicit port number, if specified.

fn path(self: Self) -> string

Return the path component.

fn query(self: Self) -> string

Return the query string (without the leading ?).

fn fragment(self: Self) -> string

Return the fragment (without the leading #).

fn to_string(self: Self) -> string

Return the full URL as a string.

fn resolve(self: Self, path: string) -> Result<Url, string>

Resolve a relative path onto this URL, returning a new Url.