Skip to content
CryoCryo home
Stdlibnet::address

url

import std::net::address::url; · source

Types

Url

type struct Url {
    scheme:   Str;
    host:     Str;
    port:     u16;
    path:     Str;
    query:    Option<Str>;
    fragment: Option<Str>;
}

Url::parse reads absolute URLs — scheme://[user@]host[:port][/path][?query][#fragment] — into borrowed parts: every field is a slice of the source string, so parsing allocates nothing and the source has to outlive the Url. path is never empty ("/" when the URL had none); query and fragment come back without their leading ? and #. An IPv6 literal host [::1] is unwrapped to ::1, and any user@ userinfo is skipped. A missing port falls back to the scheme's well-known default — 80 for http and ws, 443 for https and wss, 21 for ftp — and to 0 for a scheme it does not know.

Three methods build text back from the parts. authority() is host[:port] as a Host header carries it, with an IPv6 host in brackets and the port only when it is not the scheme's default. path_and_query() is the target an HTTP request line names. join(reference) resolves a link or a Location header against this URL and returns the absolute URL it names; . and .. segments in a path it builds are removed.

import std::collections::str::{ Str };
import std::collections::string::{ String };
import std::net::address::url::{ Url };

const base: Url = Url::parse(Str::new("http://example.com/a/b?x=1")).unwrap();
const next: String = base.join(Str::new("../c"));    // http://example.com/c

Parts come back verbatim otherwise — no %XX decoding. Display writes the URL back out. The HTTP client takes a Url for every request.

Properties

PropertyType
schemeStr
hostStr
portu16
pathStr
queryOption<Str>
fragmentOption<Str>

Constructors

static parse(source: Str) -> Result<Url, UrlError>;

Parse an absolute URL. See the module docs for the accepted shape.

Methods

authority(&this) -> String;

host[:port], the form a request's Host header carries: an IPv6 host in brackets, the port only when it is not the scheme's default.

path_and_query(&this) -> String;

The path and, when there is one, ?query: the target an HTTP request line names. The fragment is never part of it.

join(&this, reference: Str) -> String;

Resolve reference against this URL, as a link or a Location header is resolved against the page it came from, and return the absolute URL it names.

UrlError

type enum UrlError with [Eq, Clone, Debug] {
    MissingScheme;
    EmptyScheme;
    EmptyHost;
    InvalidPort;
}

The four ways a URL can fail to parse; message() is the human-readable form, and Display prints the same.

Variants
VariantNotes
MissingSchemeNo scheme:// - the input is not an absolute URL.
EmptySchemeA :// with nothing before it.
EmptyHostThe authority had no host (scheme:///path, scheme://:80).
InvalidPortA :port that was empty, non-numeric, or out of u16 range.
Methods
message(&this) -> Str;

A short human-readable reason, for diagnostics.