gluri
Values
pub fn are_equivalent(uri1: uri.Uri, uri2: uri.Uri) -> Bool
Determines whether 2 Uris are equivalent, i.e. denote the same endpoint
This will perform normalisation if the Uris are not exactly the same
Examples
let uri = parse("Https://host.com:443?q=1#fragment")
let uri2 = parse("https://HOST.com/?q=1#fragment")
are_equivalent(uri, uri2)
// -> True
pub fn merge(
base: uri.Uri,
relative: uri.Uri,
) -> Result(uri.Uri, Nil)
Resolves a URI with respect to the given base URI.
The base URI must be an absolute URI or this function will return an error. The algorithm for merging uris is as described in RFC 3986.
pub fn normalise(uri: uri.Uri) -> uri.Uri
Normalises the Uri
This follows the normalisation process in RFC3986
- Case normalisation (scheme/host -> lowercase, percent-encoding -> uppercase)
- Percent-encoding normalisation (removal of non-necessary encoding)
- Path segement normalisation (processing of /, .. and .)
- Scheme based normalisation (removal of default ports for http/https/ftp/ws/wss, setting empty path to / for valid http(s) uri)
Examples
let uri = Uri(
scheme: Some("Https"),
userinfo: None,
host: Some("host.com"),
port: Some(443),
path: "",
query: Some("q=1"),
fragment: Some("fragment")
)
normalise(uri)
// -> "https://host.com/?q=1#fragment"
pub fn origin(uri: uri.Uri) -> Result(String, Nil)
Returns the origin of the passed URI.
Returns the origin of a uri based on RFC 6454
If the URI scheme is not http
and https
.
Error
will be returned.
Examples
let assert Ok(uri) = parse("https://blah.com/test?this#that")
origin(uri)
// -> Ok("https://blah.com")
pub fn parse(uri: String) -> Result(uri.Uri, Nil)
Parses a string to the RFC3986 standard.
Error
is returned if it fails parsing.
Examples
parse("https://me@host.com:9999/path?q=1#fragment")
// -> Ok(
// Uri(
// scheme: Some("https"),
// userinfo: Some("me"),
// host: Some("host.com"),
// port: Some(9999),
// path: "/path",
// query: Some("q=1"),
// fragment: Some("fragment")
// )
// )
pub fn parse_query(
query: String,
) -> Result(List(#(String, String)), Nil)
Takes a query string and returns a list of key/value pairs
As this decodes the keys & values an Error
may be returned if the
encoding is invalid
As in query_to_string entries with blank key and values are returned Empty entries (i.e. without a = separator) are omitted as they cannot be generated using query_to_string
Examples
parse_query("first=1&=&last=2")
// -> Ok([#("first", "1"), #("", ""), #("last", "2")])
pub fn percent_decode(value: String) -> Result(String, Nil)
Decodes a percent encoded string.
Will return an Error
if the encoding is not valid
Examples
percent_decode("This%20is%20worth%20%E2%82%AC1+")
// -> Ok("This is worth €1+")
pub fn percent_encode(value: String) -> String
Encodes a string into a percent encoded string.
Examples
percent_encode("This is worth €1+")
// -> "This%20is%20worth%20%E2%82%AC1+"
pub fn query_to_string(query: List(#(String, String))) -> String
Encodes a list of key/value pairs into a URI query string
Empty keys/values are encoded so would need to be filtered before passing into this function if required
Examples
query_to_string([#("first", "1"), #("", ""), #("last", "2")])
// -> "first=1&=&last=2"
pub fn to_string(uri: uri.Uri) -> String
Encodes a Uri
value as a URI string.
Examples
let uri = Uri(
scheme: Some("https"),
userinfo: Some("me"),
host: Some("host.com"),
port: Some(9999),
path: "/path",
query: Some("q=1"),
fragment: Some("fragment")
)
to_string(uri)
// -> "https://me@host.com:9999/path?q=1#fragment"