merge initUri() overloads, improve uri examples (#17970)

* deprecate `uri.initUri` and improve uri examples

* remove `func initUri*()` and make isIpv6 optional

* fixup
This commit is contained in:
Timothee Cour 2021-05-08 07:58:30 -07:00 • committed by GitHub
commit eba1c3fd24
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,18 +20,15 @@
## ## Combine URIs ## ## Combine URIs
runnableExamples: runnableExamples:
let host = parseUri("https://nim-lang.org") let host = parseUri("https://nim-lang.org")
let blog = "/blog.html"
let bloguri = host / blog
assert $host == "https://nim-lang.org" assert $host == "https://nim-lang.org"
assert $bloguri == "https://nim-lang.org/blog.html" assert $(host / "/blog.html") == "https://nim-lang.org/blog.html"
assert $(host / "blog2.html") == "https://nim-lang.org/blog2.html"
## ## Access URI item ## ## Access URI item
runnableExamples: runnableExamples:
let res = parseUri("sftp://127.0.0.1:4343") let res = parseUri("sftp://127.0.0.1:4343")
if isAbsolute(res): assert isAbsolute(res)
assert res.port == "4343" assert res.port == "4343"
else:
echo "Wrong format"
## ## Data URI Base64 ## ## Data URI Base64
runnableExamples: runnableExamples:
@ -160,8 +157,8 @@ iterator decodeQuery*(data: string): tuple[key, value: string] =
## value, which was the behavior in Nim < 1.5.1 ## value, which was the behavior in Nim < 1.5.1
runnableExamples: runnableExamples:
import std/sequtils import std/sequtils
doAssert toSeq(decodeQuery("foo=1&bar=2=3")) == @[("foo", "1"), ("bar", "2=3")] assert toSeq(decodeQuery("foo=1&bar=2=3")) == @[("foo", "1"), ("bar", "2=3")]
doAssert toSeq(decodeQuery("&a&=b&=&&")) == @[("", ""), ("a", ""), ("", "b"), ("", ""), ("", "")] assert toSeq(decodeQuery("&a&=b&=&&")) == @[("", ""), ("a", ""), ("", "b"), ("", ""), ("", "")]
proc parseData(data: string, i: int, field: var string, sep: char): int = proc parseData(data: string, i: int, field: var string, sep: char): int =
result = i result = i
@ -227,7 +224,6 @@ func parseAuthority(authority: string, result: var Uri) =
i.inc i.inc
func parsePath(uri: string, i: var int, result: var Uri) = func parsePath(uri: string, i: var int, result: var Uri) =
i.inc parseUntil(uri, result.path, {'?', '#'}, i) i.inc parseUntil(uri, result.path, {'?', '#'}, i)
# The 'mailto' scheme's PATH actually contains the hostname/username # The 'mailto' scheme's PATH actually contains the hostname/username
@ -243,19 +239,7 @@ func parsePath(uri: string, i: var int, result: var Uri) =
i.inc # Skip '#' i.inc # Skip '#'
i.inc parseUntil(uri, result.anchor, {}, i) i.inc parseUntil(uri, result.anchor, {}, i)
func initUri*(): Uri = func initUri*(isIpv6 = false): Uri =
## Initializes a URI with `scheme`, `username`, `password`,
## `hostname`, `port`, `path`, `query` and `anchor`.
##
## **See also:**
## * `Uri type <#Uri>`_ for available fields in the URI type
runnableExamples:
var uri2: Uri
assert initUri() == uri2
result = Uri(scheme: "", username: "", password: "", hostname: "", port: "",
path: "", query: "", anchor: "")
func initUri*(isIpv6: bool): Uri {.since: (1, 3, 5).} =
## Initializes a URI with `scheme`, `username`, `password`, ## Initializes a URI with `scheme`, `username`, `password`,
## `hostname`, `port`, `path`, `query`, `anchor` and `isIpv6`. ## `hostname`, `port`, `path`, `query`, `anchor` and `isIpv6`.
## ##
@ -455,10 +439,8 @@ func combine*(uris: varargs[Uri]): Uri =
func isAbsolute*(uri: Uri): bool = func isAbsolute*(uri: Uri): bool =
## Returns true if URI is absolute, false otherwise. ## Returns true if URI is absolute, false otherwise.
runnableExamples: runnableExamples:
let foo = parseUri("https://nim-lang.org") assert parseUri("https://nim-lang.org").isAbsolute
assert isAbsolute(foo) == true assert not parseUri("nim-lang").isAbsolute
let bar = parseUri("nim-lang")
assert isAbsolute(bar) == false
return uri.scheme != "" and (uri.hostname != "" or uri.path != "") return uri.scheme != "" and (uri.hostname != "" or uri.path != "")
func `/`*(x: Uri, path: string): Uri = func `/`*(x: Uri, path: string): Uri =
@ -506,8 +488,7 @@ func `?`*(u: Uri, query: openArray[(string, string)]): Uri =
func `$`*(u: Uri): string = func `$`*(u: Uri): string =
## Returns the string representation of the specified URI object. ## Returns the string representation of the specified URI object.
runnableExamples: runnableExamples:
let foo = parseUri("https://nim-lang.org") assert $parseUri("https://nim-lang.org") == "https://nim-lang.org"
assert $foo == "https://nim-lang.org"
result = "" result = ""
if u.scheme.len > 0: if u.scheme.len > 0:
result.add(u.scheme) result.add(u.scheme)
@ -552,6 +533,6 @@ proc getDataUri*(data, mime: string, encoding = "utf-8"): string {.since: (1, 3)
## * `mimetypes <mimetypes.html>`_ for `mime` argument ## * `mimetypes <mimetypes.html>`_ for `mime` argument
## * https://tools.ietf.org/html/rfc2397 ## * https://tools.ietf.org/html/rfc2397
## * https://en.wikipedia.org/wiki/Data_URI_scheme ## * https://en.wikipedia.org/wiki/Data_URI_scheme
runnableExamples: static: doAssert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt" runnableExamples: static: assert getDataUri("Nim", "text/plain") == "data:text/plain;charset=utf-8;base64,Tmlt"
assert encoding.len > 0 and mime.len > 0 # Must *not* be URL-Safe, see RFC-2397 assert encoding.len > 0 and mime.len > 0 # Must *not* be URL-Safe, see RFC-2397
result = "data:" & mime & ";charset=" & encoding & ";base64," & base64.encode(data) result = "data:" & mime & ";charset=" & encoding & ";base64," & base64.encode(data)