URL
Source: lib/url.x
Guide: Time, Random Values, and Encodings
URL references and query strings.
Include it with #include "url.x".
Primary API
| Function | Summary |
|---|---|
Url.format_query | Returns fields as a query string of name=value pairs joined by &, in byte order of the names, with names and values percent-encoded by String.percent_encode. |
Url.parse | Returns the components of the URL or relative reference text. |
Url.parse_query | Returns the names and values of the query string query as a Map of Strings. |
Url.str | Returns url as text, in the order RFC 3986 section 5.3 joins the components. |
Url
Url.format_query
String Url.format_query(Map fields)
Returns fields as a query string of name=value pairs joined by
&, in byte order of the names, with names and values percent-encoded
by String.percent_encode. Names and values that are not Strings are
written with their str form.
Source: lib/url.x
Url.parse
Url Url.parse(String text)
Returns the components of the URL or relative reference text.
text must be printable ASCII with each % followed by two
hexadecimal digits, and a scheme must be a letter followed by letters,
digits, +, -, or .. A port above 65535 is out of range.
#include "url.x"
int main(void) {
Url url = Url.parse("https://example.com:8443/a%20b?q=1#top");
printf("%s %s %d %s\n", url.scheme, url.host, url.port,
url.path.percent_decode());
return 0;
}
https example.com 8443 /a b
Raises: <malformed> with reason and byte offset when text is not
a URL reference of that form.
Source: lib/url.x
Url.parse_query
Map Url.parse_query(String query)
Returns the names and values of the query string query as a Map of
Strings. Pairs are separated by & and split at their first =; a
pair without one has the value "", and empty pairs are skipped. Each
+ becomes a space before percent-decoding, as HTML forms encode. A
repeated name keeps its last value.
Raises: <malformed> from String.percent_decode for an invalid
escape.
Source: lib/url.x
Url.str
String Url.str(Url url)
Returns url as text, in the order RFC 3986 section 5.3 joins the
components. The authority is written when url has a host, userinfo,
or port, or has a scheme and a path that starts with /, so file:/tmp
formats as file:///tmp.
Source: lib/url.x
Public types
| Type | Kind | Summary |
|---|---|---|
Url | class | The components of a URL reference. |
Url
class Url { String scheme, userinfo, host; int port; String path, query, fragment; }
The components of a URL reference. host is written without the
brackets of an IPv6 literal, port is 0 when the reference names none,
and the other components keep their percent-encoding. A relative
reference such as /search?q=x has no scheme or host.
Source: lib/url.x
Design notes
Url.parse splits a URL or a relative reference into the components
RFC 3986 names and keeps each as written, percent-encoding included;
String.percent_decode decodes one. Url.str joins them again. The
empty String is the null pointer, so an empty query or fragment is
the same as an absent one, and formatting drops a bare ? or #.