Merge pull request #1848 from def-/fix-uri

Fix uri & httpclient
This commit is contained in:
Dominik Picheta 2015-01-03 17:06:20 +00:00
commit 51f645d293
2 changed files with 43 additions and 16 deletions

View file

@ -294,7 +294,11 @@ proc request*(url: string, httpMethod = httpGET, extraHeaders = "",
var r = if proxy == nil: parseUri(url) else: proxy.url var r = if proxy == nil: parseUri(url) else: proxy.url
var headers = substr($httpMethod, len("http")) var headers = substr($httpMethod, len("http"))
if proxy == nil: if proxy == nil:
headers.add(" /" & r.path & r.query) echo url
headers.add(" " & r.path)
if r.query.len > 0:
headers.add("?" & r.query)
echo headers
else: else:
headers.add(" " & url) headers.add(" " & url)
@ -442,7 +446,9 @@ proc generateHeaders(r: Uri, httpMethod: HttpMethod,
headers: StringTableRef): string = headers: StringTableRef): string =
result = substr($httpMethod, len("http")) result = substr($httpMethod, len("http"))
# TODO: Proxies # TODO: Proxies
result.add(" /" & r.path & r.query) result.add(" " & r.path)
if r.query.len > 0:
result.add("?" & r.query)
result.add(" HTTP/1.1\c\L") result.add(" HTTP/1.1\c\L")
add(result, "Host: " & r.hostname & "\c\L") add(result, "Host: " & r.hostname & "\c\L")

View file

@ -16,6 +16,7 @@ type
Uri* = object Uri* = object
scheme*, username*, password*: string scheme*, username*, password*: string
hostname*, port*, path*, query*, anchor*: string hostname*, port*, path*, query*, anchor*: string
opaque*: bool
{.deprecated: [TUrl: Url, TUri: Uri].} {.deprecated: [TUrl: Url, TUri: Uri].}
@ -115,6 +116,8 @@ proc parseUri*(uri: string): Uri =
if authority == "": if authority == "":
raise newException(ValueError, "Expected authority got nothing.") raise newException(ValueError, "Expected authority got nothing.")
parseAuthority(authority, result) parseAuthority(authority, result)
else:
result.opaque = true
# Path # Path
parsePath(uri, i, result) parsePath(uri, i, result)
@ -256,6 +259,9 @@ proc `$`*(u: Uri): string =
result = "" result = ""
if u.scheme.len > 0: if u.scheme.len > 0:
result.add(u.scheme) result.add(u.scheme)
if u.opaque:
result.add(":")
else:
result.add("://") result.add("://")
if u.username.len > 0: if u.username.len > 0:
result.add(u.username) result.add(u.username)
@ -268,22 +274,28 @@ proc `$`*(u: Uri): string =
result.add(":") result.add(":")
result.add(u.port) result.add(u.port)
if u.path.len > 0: if u.path.len > 0:
if u.path[0] != '/': result.add("/")
result.add(u.path) result.add(u.path)
if u.query.len > 0:
result.add("?")
result.add(u.query) result.add(u.query)
if u.anchor.len > 0:
result.add("#")
result.add(u.anchor) result.add(u.anchor)
when isMainModule: when isMainModule:
block: block:
let test = parseUri("http://localhost:8080/test") let str = "http://localhost:8080/test"
let test = parseUri(str)
doAssert test.scheme == "http" doAssert test.scheme == "http"
doAssert test.port == "8080" doAssert test.port == "8080"
doAssert test.path == "/test" doAssert test.path == "/test"
doAssert test.hostname == "localhost" doAssert test.hostname == "localhost"
doAssert($test == str)
block: block:
let test = parseUri("foo://username:password@example.com:8042/over/there" & let str = "foo://username:password@example.com:8042/over/there" &
"/index.dtb?type=animal&name=narwhal#nose") "/index.dtb?type=animal&name=narwhal#nose"
let test = parseUri(str)
doAssert test.scheme == "foo" doAssert test.scheme == "foo"
doAssert test.username == "username" doAssert test.username == "username"
doAssert test.password == "password" doAssert test.password == "password"
@ -292,34 +304,45 @@ when isMainModule:
doAssert test.path == "/over/there/index.dtb" doAssert test.path == "/over/there/index.dtb"
doAssert test.query == "type=animal&name=narwhal" doAssert test.query == "type=animal&name=narwhal"
doAssert test.anchor == "nose" doAssert test.anchor == "nose"
doAssert($test == str)
block: block:
let test = parseUri("urn:example:animal:ferret:nose") let str = "urn:example:animal:ferret:nose"
let test = parseUri(str)
doAssert test.scheme == "urn" doAssert test.scheme == "urn"
doAssert test.path == "example:animal:ferret:nose" doAssert test.path == "example:animal:ferret:nose"
doAssert($test == str)
block: block:
let test = parseUri("mailto:username@example.com?subject=Topic") let str = "mailto:username@example.com?subject=Topic"
let test = parseUri(str)
doAssert test.scheme == "mailto" doAssert test.scheme == "mailto"
doAssert test.username == "username" doAssert test.username == "username"
doAssert test.hostname == "example.com" doAssert test.hostname == "example.com"
doAssert test.query == "subject=Topic" doAssert test.query == "subject=Topic"
doAssert($test == str)
block: block:
let test = parseUri("magnet:?xt=urn:sha1:72hsga62ba515sbd62&dn=foobar") let str = "magnet:?xt=urn:sha1:72hsga62ba515sbd62&dn=foobar"
let test = parseUri(str)
doAssert test.scheme == "magnet" doAssert test.scheme == "magnet"
doAssert test.query == "xt=urn:sha1:72hsga62ba515sbd62&dn=foobar" doAssert test.query == "xt=urn:sha1:72hsga62ba515sbd62&dn=foobar"
doAssert($test == str)
block: block:
let test = parseUri("/test/foo/bar?q=2#asdf") let str = "/test/foo/bar?q=2#asdf"
let test = parseUri(str)
doAssert test.scheme == "" doAssert test.scheme == ""
doAssert test.path == "/test/foo/bar" doAssert test.path == "/test/foo/bar"
doAssert test.query == "q=2" doAssert test.query == "q=2"
doAssert test.anchor == "asdf" doAssert test.anchor == "asdf"
doAssert($test == str)
block: block:
let test = parseUri("test/no/slash") let str = "test/no/slash"
let test = parseUri(str)
doAssert test.path == "test/no/slash" doAssert test.path == "test/no/slash"
doAssert($test == str)
# Remove dot segments tests # Remove dot segments tests
block: block:
@ -371,5 +394,3 @@ when isMainModule:
block: block:
let test = parseUri("http://example.com/foo/") / "/bar/asd" let test = parseUri("http://example.com/foo/") / "/bar/asd"
doAssert test.path == "/foo/bar/asd" doAssert test.path == "/foo/bar/asd"