Source Edit

This module implements URI parsing as specified by RFC 3986.

A Uniform Resource Identifier (URI) provides a simple and extensible means for identifying a resource. A URI can be further classified as a locator, a name, or both. The term “Uniform Resource Locator” (URL) refers to the subset of URIs.

Warning: URI parsers in this module do not perform security validation.

Basic usage

Combine URIs

Example:

  1. import std/uri
  2. let host = parseUri("https://nim-lang.org")
  3. assert $host == "https://nim-lang.org"
  4. assert $(host / "/blog.html") == "https://nim-lang.org/blog.html"
  5. assert $(host / "blog2.html") == "https://nim-lang.org/blog2.html"

Access URI item

Example:

  1. import std/uri
  2. let res = parseUri("sftp://127.0.0.1:4343")
  3. assert isAbsolute(res)
  4. assert res.port == "4343"

Data URI Base64

Example:

  1. import std/uri
  2. assert getDataUri("Hello World", "text/plain") == "data:text/plain;charset=utf-8;base64,SGVsbG8gV29ybGQ="
  3. assert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt"

Imports

strutils, parseutils, base64, since, decode_helpers

Types

  1. Uri = object
  2. scheme*, username*, password*: string
  3. hostname*, port*, path*, query*, anchor*: string
  4. opaque*: bool
  5. isIpv6*: bool

Source Edit

  1. UriParseError = object of ValueError

Source Edit

  1. Url = distinct string

Source Edit

Procs

  1. func `$`(u: Uri): string {....raises: [], tags: [], forbids: [].}

Returns the string representation of the specified URI object.

Example:

  1. assert $parseUri("https://nim-lang.org") == "https://nim-lang.org"

Source Edit

  1. func `/`(x: Uri; path: string): Uri {....raises: [], tags: [], forbids: [].}

Concatenates the path specified to the specified URIs path.

Contrary to the combine func you do not have to worry about the slashes at the beginning and end of the path and URIs path respectively.

See also:

Example:

  1. let foo = parseUri("https://nim-lang.org/foo/bar") / "/baz"
  2. assert foo.path == "/foo/bar/baz"
  3. let bar = parseUri("https://nim-lang.org/foo/bar") / "baz"
  4. assert bar.path == "/foo/bar/baz"
  5. let qux = parseUri("https://nim-lang.org/foo/bar/") / "baz"
  6. assert qux.path == "/foo/bar/baz"

Source Edit

  1. func `?`(u: Uri; query: openArray[(string, string)]): Uri {....raises: [],
  2. tags: [], forbids: [].}

Concatenates the query parameters to the specified URI object.

Example:

  1. let foo = parseUri("https://example.com") / "foo" ? {"bar": "qux"}
  2. assert $foo == "https://example.com/foo?bar=qux"

Source Edit

  1. func combine(base: Uri; reference: Uri): Uri {....raises: [], tags: [], forbids: [].}

Combines a base URI with a reference URI.

This uses the algorithm specified in section 5.2.2 of RFC 3986.

This means that the slashes inside the base URIs path as well as reference URIs path affect the resulting URI.

See also:

Example:

  1. let foo = combine(parseUri("https://nim-lang.org/foo/bar"), parseUri("/baz"))
  2. assert foo.path == "/baz"
  3. let bar = combine(parseUri("https://nim-lang.org/foo/bar"), parseUri("baz"))
  4. assert bar.path == "/foo/baz"
  5. let qux = combine(parseUri("https://nim-lang.org/foo/bar/"), parseUri("baz"))
  6. assert qux.path == "/foo/bar/baz"

Source Edit

  1. func combine(uris: varargs[Uri]): Uri {....raises: [], tags: [], forbids: [].}

Combines multiple URIs together.

See also:

Example:

  1. let foo = combine(parseUri("https://nim-lang.org/"), parseUri("docs/"),
  2. parseUri("manual.html"))
  3. assert foo.hostname == "nim-lang.org"
  4. assert foo.path == "/docs/manual.html"

Source Edit

  1. func decodeUrl(s: string; decodePlus = true): string {....raises: [], tags: [],
  2. forbids: [].}

Decodes a URL according to RFC3986.

This means that any %xx (where xx denotes a hexadecimal value) are converted to the character with ordinal number xx, and every other character is carried over. If xx is not a valid hexadecimal value, it is left intact.

As a special rule, when the value of decodePlus is true, + characters are converted to a space.

See also:

Example:

  1. assert decodeUrl("https%3A%2F%2Fnim-lang.org") == "https://nim-lang.org"
  2. assert decodeUrl("https%3A%2F%2Fnim-lang.org%2Fthis+is+a+test") == "https://nim-lang.org/this is a test"
  3. assert decodeUrl("https%3A%2F%2Fnim-lang.org%2Fthis%20is%20a%20test",
  4. false) == "https://nim-lang.org/this is a test"
  5. assert decodeUrl("abc%xyz") == "abc%xyz"

Source Edit

  1. func encodeQuery(query: openArray[(string, string)]; usePlus = true;
  2. omitEq = true; sep = '&'): string {....raises: [], tags: [],
  3. forbids: [].}

Encodes a set of (key, value) parameters into a URL query string.

Every (key, value) pair is URL-encoded and written as key=value. If the value is an empty string then the \= is omitted, unless omitEq is false. The pairs are joined together by the sep character.

The usePlus parameter is passed down to the encodeUrl function that is used for the URL encoding of the string values.

See also:

Example:

  1. assert encodeQuery({: }) == ""
  2. assert encodeQuery({"a": "1", "b": "2"}) == "a=1&b=2"
  3. assert encodeQuery({"a": "1", "b": ""}) == "a=1&b"
  4. assert encodeQuery({"a": "1", "b": ""}, omitEq = false, sep = ';') == "a=1;b="

Source Edit

  1. func encodeUrl(s: string; usePlus = true): string {....raises: [], tags: [],
  2. forbids: [].}

Encodes a URL according to RFC3986.

This means that characters in the set {‘a’..’z’, ‘A’..’Z’, ‘0’..’9’, ‘-‘, ‘.’, ‘_‘, ‘~’} are carried over to the result. All other characters are encoded as %xx where xx denotes its hexadecimal value.

As a special rule, when the value of usePlus is true, spaces are encoded as + instead of %20.

See also:

Example:

  1. assert encodeUrl("https://nim-lang.org") == "https%3A%2F%2Fnim-lang.org"
  2. assert encodeUrl("https://nim-lang.org/this is a test") == "https%3A%2F%2Fnim-lang.org%2Fthis+is+a+test"
  3. assert encodeUrl("https://nim-lang.org/this is a test", false) == "https%3A%2F%2Fnim-lang.org%2Fthis%20is%20a%20test"

Source Edit

  1. proc getDataUri(data, mime: string; encoding = "utf-8"): string {....raises: [],
  2. tags: [], forbids: [].}

Convenience proc for base64.encode returns a standard Base64 Data URI (RFC-2397)

See also:

Example:

  1. static: assert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt"

Source Edit

  1. func initUri(isIpv6 = false): Uri {....raises: [], tags: [], forbids: [].}

Initializes a URI with scheme, username, password, hostname, port, path, query, anchor and isIpv6.

See also:

  • Uri type for available fields in the URI type

Example:

  1. var uri2 = initUri(isIpv6 = true)
  2. uri2.scheme = "tcp"
  3. uri2.hostname = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
  4. uri2.port = "8080"
  5. assert $uri2 == "tcp://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"

Source Edit

  1. func isAbsolute(uri: Uri): bool {....raises: [], tags: [], forbids: [].}

Returns true if URI is absolute, false otherwise.

Example:

  1. assert parseUri("https://nim-lang.org").isAbsolute
  2. assert not parseUri("nim-lang").isAbsolute

Source Edit

  1. func parseUri(uri: string): Uri {....raises: [], tags: [], forbids: [].}

Parses a URI and returns it.

See also:

  • Uri type for available fields in the URI type

Example:

  1. let res = parseUri("ftp://Username:Password@Hostname")
  2. assert res.username == "Username"
  3. assert res.password == "Password"
  4. assert res.scheme == "ftp"

Source Edit

  1. func parseUri(uri: string; result: var Uri) {....raises: [], tags: [], forbids: [].}

Parses a URI. The result variable will be cleared before.

See also:

Example:

  1. var res = initUri()
  2. parseUri("https://nim-lang.org/docs/manual.html", res)
  3. assert res.scheme == "https"
  4. assert res.hostname == "nim-lang.org"
  5. assert res.path == "/docs/manual.html"

Source Edit

  1. proc uriParseError(msg: string) {.noreturn, ...raises: [UriParseError], tags: [],
  2. forbids: [].}

Raises a UriParseError exception with message msg. Source Edit

Iterators

  1. iterator decodeQuery(data: string; sep = '&'): tuple[key, value: string] {.
  2. ...raises: [], tags: [], forbids: [].}

Reads and decodes the query string data and yields the (key, value) pairs the data consists of. If compiled with -d:nimLegacyParseQueryStrict, a UriParseError is raised when there is an unencoded \= character in a decoded value, which was the behavior in Nim < 1.5.1.

Example:

  1. import std/sequtils
  2. assert toSeq(decodeQuery("foo=1&bar=2=3")) == @[("foo", "1"), ("bar", "2=3")]
  3. assert toSeq(decodeQuery("foo=1;bar=2=3", ';')) == @[("foo", "1"), ("bar", "2=3")]
  4. assert toSeq(decodeQuery("&a&=b&=&&")) == @[("", ""), ("a", ""), ("", "b"), ("", ""), ("", "")]

Source Edit