URL
import "std/net" (not included in import "std") · namespace std.net
Url takes a URL apart, builds new ones, resolves links the way a browser does, and percent-encodes text for use in them.
grav
import "std"
import "std/net"
fn main() {
let u = Url.parse("https://ada@Example.com:8443/docs/guide/intro.html?q=a+b&n=1#top")
print(u.host + " " + "${u.port}" + " " + u.user)
print(u.pathOnly() + " | " + u.query() + " | " + u.fragment)
print(u.queryValue("q"))
print(u.resolve("../img/logo.png").toString())
print(u.resolve("?page=2").path)
print(Url.parse("http://example.com/search").withParam("q", "grav & c").toString())
print(Url.encode("a b/c") + " " + Url.encodePath("/a b/c"))
}example.com 8443 ada
/docs/guide/intro.html | q=a+b&n=1 | top
a b
https://ada@example.com:8443/docs/img/logo.png
/docs/guide/intro.html?page=2
http://example.com/search?q=grav%20%26%20c
a%20b%2Fc /a%20b/c- Parts:
scheme,host(lower case; IPv6 without brackets),port(the scheme's default when not written),path(the path and query, as sent to a server),fragment,userandpassword.pathOnly(),query(),queryParams(),queryValue(name),hostHeader(),origin(),isSecure(). - Building:
withParam(name, value),withQuery(map),withPath(target),copy(), andtoString()(the default port is left out). - Resolving:
resolve(ref)follows a link found on the page: an absolute URL,//host/x,/abs,rel,../up,?queryor#frag(RFC 3986). - Encoding:
encodefor query values and path segments,encodePathfor a whole path (keeps/),decode(also turns+into a space, as forms do) anddecodePath(leaves+alone). - Queries:
buildQuery(map)(sorted, encoded),parseQuery(text),parseQueryAll(text)for repeated names,pairs(text)in order. Url.parsethrowsValueErrorfor a URL without a host or with a bad port;Url.isValid(text)checks first.Url.defaultPort(scheme)knows http, https, ws, wss, ftp, smtp and smtps.
API
class Url
A parsed URL: scheme://user:password@host:port/path?query#fragment.
Constructors
new Url(scheme: string, host: string, port: int, path: string)
Static members
static fn parse(text: string) -> Url— Parses "scheme://[user[:password]@]host[:port][/path][?query][#fragment]". Without a scheme, "http" is assumed. ValueError when there is no host.static fn isValid(text: string) -> bool— Whethertextparses as a URL with a scheme and a host.static fn defaultPort(scheme: string) -> int— The usual port for a scheme: 80 for http and ws, 443 for https and wss, 21 ftp, 25 smtp, 465 smtps; 80 otherwise.static fn encode(s: string) -> string— Percent-encodes everything except unreserved characters (RFC 3986): for query values and path segments.static fn encodePath(s: string) -> string— Percent-encodes a whole path, keeping its '/' separators.static fn decode(s: string) -> string— Decodes %XX escapes and '+' as a space (query strings and forms).static fn decodePath(s: string) -> string— Decodes %XX escapes only ('+' stays): for paths.static fn buildQuery(fields: HashMap<string, string>) -> string— "a=1&b=x%20y": the fields URL-encoded and joined with &, in key order.static fn parseQuery(q: string) -> HashMap<string, string>— "a=1&b=two" -> {a: "1", b: "two"} (decoded; the last of repeated names wins).static fn parseQueryAll(q: string) -> HashMap<string, Array<string>>— Every value of every name: "tag=a&tag=b" -> {tag: ["a", "b"]}.static fn pairs(q: string) -> Array<Array<string>>— The decoded [name, value] pairs of a query string, in order.
Fields
scheme: string— "http", "https", "ws", ... (lower case).host: string— The host name or address (an IPv6 address without its brackets).port: int— The port: the given one, else the scheme's default (80, 443, ...).path: string— The request target: path and query string ("/search?q=x"), "/" at least.fragment: string— The part after '#' ("" when absent); never sent to a server.user: string— The user name before '@' ("" when absent), decoded.password: string— The password after "user:" ("" when absent), decoded.
Methods
fn isSecure() -> bool— Whether the scheme is a TLS one (https, wss, smtps).fn pathOnly() -> string— The path without the query string ("/search").fn query() -> string— The query string without '?' ("q=x&n=2"), "" when there is none.fn queryParams() -> HashMap<string, string>— The query string's fields, decoded (the last value wins for repeats).fn queryValue(name: string) -> string— One query field ("" when absent).fn hostText() -> string— The host as written in a URL: IPv6 addresses in brackets.fn hostHeader() -> string— The Host header value: host, plus ":port" unless it is the default.fn origin() -> string— "scheme://host[:port]", the part that decides which server is talked to.fn withQuery(fields: HashMap<string, string>) -> Url— This URL withfieldsadded to its query string (URL-encoded).fn withParam(name: string, value: string) -> Url— This URL with one more query field.fn withPath(target: string) -> Url— This URL with a different path (and query): "/v2/items?page=3".fn copy() -> Url— A copy of this URL.fn resolve(ref: string) -> Url— The URL a linkrefon this page points to (RFC 3986): an absolute URL, "//host/x", "/abs/path", "rel/path", "../up", "?query" or "#frag".fn toString() -> string— The URL as text (the port is left out when it is the scheme's default; the password is kept, so take care when logging).