summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2020-09-24 14:54:13 +0800
committerGitHub <noreply@github.com>2020-09-24 08:54:13 +0200
commite85c5ad3797b378cf6b93f35cc1d63e2d066b57a (patch)
tree1dccf6c79097d21cdcc01caa6487fb3b9b8db8ee /lib
parent7a6f4eca1987f41fccf2eea0896f4a433208a355 (diff)
downloadNim-e85c5ad3797b378cf6b93f35cc1d63e2d066b57a.tar.gz
fix #15333 (#15336)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/uri.nim28
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/pure/uri.nim b/lib/pure/uri.nim
index 04a9d97bd..4b84a8902 100644
--- a/lib/pure/uri.nim
+++ b/lib/pure/uri.nim
@@ -49,6 +49,7 @@ import std/private/since
 import strutils, parseutils, base64
 include includes/decode_helpers
 
+
 type
   Url* = distinct string
 
@@ -56,6 +57,7 @@ type
     scheme*, username*, password*: string
     hostname*, port*, path*, query*, anchor*: string
     opaque*: bool
+    isIpv6: bool # not expose it for compatibility.
 
 proc encodeUrl*(s: string, usePlus = true): string =
   ## Encodes a URL according to RFC3986.
@@ -170,6 +172,7 @@ proc parseAuthority(authority: string, result: var Uri) =
         inPort = true
     of '[':
       inIPv6 = true
+      result.isIpv6 = true
     of ']':
       inIPv6 = false
     else:
@@ -208,6 +211,21 @@ proc initUri*(): Uri =
   result = Uri(scheme: "", username: "", password: "", hostname: "", port: "",
                 path: "", query: "", anchor: "")
 
+proc initUri*(isIpv6: bool): Uri {.since: (1, 3, 5).} =
+  ## Initializes a URI with ``scheme``, ``username``, ``password``,
+  ## ``hostname``, ``port``, ``path``, ``query``, ``anchor`` and ``isIpv6``.
+  ##
+  ## **See also:**
+  ## * `Uri type <#Uri>`_ for available fields in the URI type
+  runnableExamples:
+    var uri2 = initUri(isIpv6 = true)
+    uri2.scheme = "tcp"
+    uri2.hostname = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
+    uri2.port = "8080"
+    assert $uri2 == "tcp://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080"
+  result = Uri(scheme: "", username: "", password: "", hostname: "", port: "",
+                path: "", query: "", anchor: "", isIpv6: isIpv6)
+
 proc resetUri(uri: var Uri) =
   for f in uri.fields:
     when f is string:
@@ -452,9 +470,15 @@ proc `$`*(u: Uri): string =
       result.add(u.password)
     result.add("@")
   if u.hostname.endsWith('/'):
-    result.add(u.hostname[0..^2])
+    if u.isIpv6:
+      result.add("[" & u.hostname[0 .. ^2] & "]")
+    else:
+      result.add(u.hostname[0 .. ^2])
   else:
-    result.add(u.hostname)
+    if u.isIpv6:
+      result.add("[" & u.hostname & "]")
+    else:
+      result.add(u.hostname)
   if u.port.len > 0:
     result.add(":")
     result.add(u.port)