summary refs log tree commit diff stats
path: root/lib/pure/uri.nim
diff options
context:
space:
mode:
authorFederico Ceratto <federico.ceratto@gmail.com>2017-10-31 18:52:52 +0000
committerFederico Ceratto <federico.ceratto@gmail.com>2017-11-14 02:28:13 +0000
commitbd71d4205bfb6dc317dc5cf6edbf1816178192d6 (patch)
tree0c1030b4392585c3a7237fe1160b3127adbd16a9 /lib/pure/uri.nim
parent22ceab0fbb29241f72149fe4d53749d57861ba6f (diff)
downloadNim-bd71d4205bfb6dc317dc5cf6edbf1816178192d6.tar.gz
Make Uri rendering more lenient
When the hostname and path fields are set, handle missing or extra
slashes to generate valid URLs.
Diffstat (limited to 'lib/pure/uri.nim')
-rw-r--r--lib/pure/uri.nim37
1 files changed, 35 insertions, 2 deletions
diff --git a/lib/pure/uri.nim b/lib/pure/uri.nim
index c702b054c..164a57ecf 100644
--- a/lib/pure/uri.nim
+++ b/lib/pure/uri.nim
@@ -308,11 +308,16 @@ proc `$`*(u: Uri): string =
       result.add(":")
       result.add(u.password)
     result.add("@")
-  result.add(u.hostname)
+  if u.hostname.endswith('/'):
+    result.add(u.hostname[0..^2])
+  else:
+    result.add(u.hostname)
   if u.port.len > 0:
     result.add(":")
     result.add(u.port)
   if u.path.len > 0:
+    if u.hostname.len > 0 and u.path[0] != '/':
+      result.add('/')
     result.add(u.path)
   if u.query.len > 0:
     result.add("?")
@@ -483,6 +488,34 @@ when isMainModule:
     let foo = parseUri("http://localhost:9515") / "status"
     doAssert $foo == "http://localhost:9515/status"
 
+  # bug #6649 #6652
+  block:
+    var foo = parseUri("http://example.com")
+    foo.hostname = "example.com"
+    foo.path = "baz"
+    doAssert $foo == "http://example.com/baz"
+
+    foo.hostname = "example.com/"
+    foo.path = "baz"
+    doAssert $foo == "http://example.com/baz"
+
+    foo.hostname = "example.com"
+    foo.path = "/baz"
+    doAssert $foo == "http://example.com/baz"
+
+    foo.hostname = "example.com/"
+    foo.path = "/baz"
+    doAssert $foo == "http://example.com/baz"
+
+    foo.hostname = "example.com/"
+    foo.port = "8000"
+    foo.path = "baz"
+    doAssert $foo == "http://example.com:8000/baz"
+
+    foo = parseUri("file:/dir/file")
+    foo.path = "relative"
+    doAssert $foo == "file:relative"
+
   # isAbsolute tests
   block:
     doAssert "www.google.com".parseUri().isAbsolute() == false
@@ -524,4 +557,4 @@ when isMainModule:
     doAssert "https://example.com/about/staff.html?".parseUri().isAbsolute == true
     doAssert "https://example.com/about/staff.html?parameters".parseUri().isAbsolute == true
 
-  echo("All good!")
\ No newline at end of file
+  echo("All good!")