std1.0.0-beta-7 SafeC docs

std :: encoding::url

Struct std::Url

#include <std/encoding/url.h> source

struct Url {
    struct String scheme;
    struct String username;
    struct String password;
    struct String host;
    int port;
    struct String path;
    struct String query;
    struct String fragment;
    int has_authority;
    int has_query;
    int has_fragment;
}

A URL split into its RFC 3986 components:

scheme://username:password@host:port/path?query#fragment

Components are stored as they appear in the text (still percent-encoded, so they can be serialized back unchanged), except that the scheme and a registered-name host are lowercased and an IPv6 literal's brackets are dropped from 'host' ("[::1]" is stored as "::1"). 'port' is -1 when the URL has none. has_authority / has_query / has_fragment tell "no query" apart from an empty one ("/p?" vs "/p").

Fields

§struct String schemesource

"https" (no ':'), empty for a relative reference

§struct String usernamesource
§struct String passwordsource
§struct String hostsource
§int portsource

-1: none given

§struct String pathsource

"/a/b" (may be empty, e.g. "mailto:x" has path "x")

§struct String querysource

without the '?'

§struct String fragmentsource

without the '#'

§int has_authoritysource

"//" was present

§int has_querysource
§int has_fragmentsource

Methods

§static struct Url new()source · definition

An empty relative reference (every component empty, port -1).

§static struct Url parse(const &char s, ?&int ok)source · definition

Parses an absolute URL or a relative reference. Fails (*ok = 0, and an empty Url) on a bad scheme, a non-numeric or > 65535 port, an unterminated '[' IPv6 literal, or a control character/space.

§struct String to_string() constsource · definition

Back to text (the RFC 3986 §5.3 recomposition).

§struct Url resolve(const &char ref, ?&int ok) constsource · definition

Resolves the reference 'ref' against this (absolute) base URL — RFC 3986 §5.2, including dot-segment removal: base "http://a/b/c/d;p?q" + "../g" = "http://a/b/g". Fails if 'ref' doesn't parse or this base has no scheme.

§int port_or_default() constsource · definition

'port', or the scheme's default (http/ws 80, https/wss 443, ftp 21) when none was given; -1 for an unknown scheme without a port.

§struct String host_port() constsource · definition

"host" or "host:port" (only an explicit port), IPv6 re-bracketed — what an HTTP Host header carries.

§struct String origin() constsource · definition

"scheme://host[:port]" — the web origin (for CORS, cookies, ...).

§struct String request_target() constsource · definition

path (or "/" when empty) plus "?query" — an HTTP request-target.

§struct QueryParams query_params() constsource · definition

The decoded query parameters.

§void set_query_params(const &QueryParams params)source · definition

Replaces the query with params.to_string() (has_query = 1, or 0 for an empty params).

§struct String path_decoded() constsource · definition

The path with each %XX decoded (for file lookups, routing, ...).

§int is_absolute() constsource · definition

has a scheme

§struct Url clone() constsource · definition
§void drop()source · definition
§void free() consumesource · definition

= drop()