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.free();
}

Contents

Functions

Function parse

pub fn parse(s: string) -> Url

Parse a URL string.

Panics if the string is not a valid URL.

Examples

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;

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 decode error.

Examples

import std::net::url;

let decoded = url.decode("hello%20world");  // "hello world"

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;

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

Types

Struct Url

A parsed URL.

Created by url.parse(s). Must be freed with free() when no longer needed to avoid leaking memory.

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

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

fn free(self: Self)

Release the parsed URL resources.