module std::urlsource
URLs as HTTP takes them (RFC 3986's subset scheme://host:port/path?query):
parsing one, resolving a reference against one (a link, a redirection's
Location), percent-encoding a query component and building a query.
import std::option;
import std::string;
const url = if (Url::parse(&"https://Tickets.example:8443/api/shows?q=paris#top") is ::Some |move u|) move u else {
expect(false);
return;
};
expect(url.scheme.view() == "https" && url.host.view() == "tickets.example" && url.port == 8443);
expect(url.path.view() == "/api/shows" && url.query.view() == "q=paris");
expect_equal(&url.target(), &String::from(&"/api/shows?q=paris"));
if (url.resolve(&"../holds") is ::Some |&next|) {
expect_equal(&f"{next}", &String::from(&"https://tickets.example:8443/holds"));
}
var query = Query::empty();
query.add(&"q", &"Saint-Étienne & co");
query.add(&"page", &"2");
expect_equal(&query.text, &String::from(&"?q=Saint-%C3%89tienne%20%26%20co&page=2"));
type Urlsource
exp type Url : Format = struct
A URL with an authority: its scheme and host in lowercase, the port the scheme's default when none is written (80 for http, 443 for https, 0 for others), the path ("/" at least) and query as written, escapes kept, and no fragment.
Fields
exp scheme: Stringexp host: Stringan IPv6 address without its brackets
exp port: u16exp path: Stringexp query: Stringafter the '?', which is not kept; empty for none
Functions
fn Url::parsesource
exp fn Url::parse(text: &[u8]) -> Option<Url>
The URL the text holds, None when it is not one of the form
scheme://host[:port][/path][?query][#fragment]. User information
(user@) is not taken.
fn resolvesource
exp fn resolve(self base: &Url, reference: &[u8]) -> Option<Url>
The URL a reference names from this one: a whole URL, //host/path,
an absolute path, a relative path (../a, b), or a query alone, its
. and .. segments removed (RFC 3986, 5.2).
fn targetsource
exp fn target(self url: &Url) -> String
What a request names on its server: the path, with the query after a '?' when there is one.
fn originsource
exp fn origin(self url: &Url) -> String
scheme://host, with the port when it is not the scheme's default:
what two URLs share when they are of one origin.
fn authoritysource
exp fn authority(self url: &Url) -> String
The host as HTTP's Host header gives it: with the port when it is not the scheme's default, an IPv6 address in brackets.
fn formatsource
exp fn format(self url: &Url, out: &var String)
fn encodedsource
exp fn encoded(text: &[u8]) -> String
The text percent-encoded for a query's name or value, or a path's
segment: every byte but letters, digits and -._~ as %XX.
fn decodedsource
exp fn decoded(text: &[u8], plus: bool) -> Option<String>
The text with its %XX escapes decoded, and + as a space when
plus (a form's encoding); None when an escape is not two hex digits.
type Querysource
exp type Query = struct
A query built a pair at a time, each name and value percent-encoded:
?a=1&b=two, empty while it has none.
Fields
exp text: String
Functions
fn Query::emptysource
exp fn Query::empty() -> Query
fn addsource
exp fn add(self query: &var Query, name: &[u8], value: &[u8])