mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 18:02:05 +00:00
Make Uri rendering more lenient
When the hostname and path fields are set, handle missing or extra slashes to generate valid URLs.
This commit is contained in:
@@ -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!")
|
||||
echo("All good!")
|
||||
|
||||
Reference in New Issue
Block a user