W3cubDocs

/Nim

Module uri

This module implements URI parsing as specified by RFC 3986.

Imports

strutils, parseutils

Types

Url = distinct string
Uri = object
  scheme*, username*, password*: string
  hostname*, port*, path*, query*, anchor*: string
  opaque*: bool

Procs

proc `$`(url: Url): string {.deprecated, raises: [], tags: [].}
Deprecated since 0.9.6: Use Uri instead.
proc `/`(a, b: Url): Url {.deprecated, raises: [], tags: [].}

Joins two URLs together, separating them with / if needed.

Deprecated since 0.9.6: Use Uri instead.

proc add(url: var Url; a: Url) {.deprecated, raises: [], tags: [].}

Appends url to url.

Deprecated since 0.9.6: Use Uri instead.

proc initUri(): Uri {.raises: [], tags: [].}
Initializes a URI.
proc parseUri(uri: string; result: var Uri) {.raises: [ValueError], tags: [].}
Parses a URI. The result variable will be cleared before.
proc parseUri(uri: string): Uri {.raises: [ValueError], tags: [].}
Parses a URI and returns it.
proc combine(base: Uri; reference: Uri): Uri {.raises: [], tags: [].}

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 URI's path as well as reference URI's path affect the resulting URI.

For building URIs you may wish to use `/` instead.

Examples:

let foo = combine(parseUri("http://example.com/foo/bar"), parseUri("/baz"))
assert foo.path == "/baz"

let bar = combine(parseUri("http://example.com/foo/bar"), parseUri("baz"))
assert bar.path == "/foo/baz"

let bar = combine(parseUri("http://example.com/foo/bar/"), parseUri("baz"))
assert bar.path == "/foo/bar/baz"
proc combine(uris: varargs[Uri]): Uri {.raises: [], tags: [].}
Combines multiple URIs together.
proc isAbsolute(uri: Uri): bool {.raises: [], tags: [].}
returns true if URI is absolute, false otherwise
proc `/`(x: Uri; path: string): Uri {.raises: [], tags: [].}

Concatenates the path specified to the specified URI's path.

Contrary to the combine procedure you do not have to worry about the slashes at the beginning and end of the path and URI's path respectively.

Examples:

let foo = parseUri("http://example.com/foo/bar") / "/baz"
assert foo.path == "/foo/bar/baz"

let bar = parseUri("http://example.com/foo/bar") / "baz"
assert bar.path == "/foo/bar/baz"

let bar = parseUri("http://example.com/foo/bar/") / "baz"
assert bar.path == "/foo/bar/baz"
proc `$`(u: Uri): string {.raises: [], tags: [].}
Returns the string representation of the specified URI object.

© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/uri.html