diff options
author | def <dennis@felsin9.de> | 2016-02-23 04:41:13 +0100 |
---|---|---|
committer | def <dennis@felsin9.de> | 2016-02-23 15:18:17 +0100 |
commit | 9afe2c1d8fefb762af360d76b747d67640b3d04d (patch) | |
tree | e86f1c07caef29e539d1d82d68aa94fa550ee5cb /lib | |
parent | 41e1f3241b71152f0cfe41ef77c4c2ac85b9086b (diff) | |
download | Nim-9afe2c1d8fefb762af360d76b747d67640b3d04d.tar.gz |
Fix redirection in httpclient (fixes #3895)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/httpclient.nim | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 1b91132db..e254622c2 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -455,14 +455,15 @@ proc redirection(status: string): bool = if status.startsWith(i): return true -proc getNewLocation(lastUrl: string, headers: StringTableRef): string = +proc getNewLocation(lastURL: string, headers: StringTableRef): string = result = headers.getOrDefault"Location" if result == "": httpError("location header expected") # Relative URLs. (Not part of the spec, but soon will be.) let r = parseUri(result) if r.hostname == "" and r.path != "": - let origParsed = parseUri(lastUrl) - result = origParsed.hostname & "/" & r.path + var parsed = parseUri(lastURL) + parsed.path = r.path + result = $parsed proc get*(url: string, extraHeaders = "", maxRedirects = 5, sslContext: SSLContext = defaultSSLContext, @@ -481,7 +482,7 @@ proc get*(url: string, extraHeaders = "", maxRedirects = 5, let redirectTo = getNewLocation(lastURL, result.headers) result = request(redirectTo, httpGET, extraHeaders, "", sslContext, timeout, userAgent, proxy) - lastUrl = redirectTo + lastURL = redirectTo proc getContent*(url: string, extraHeaders = "", maxRedirects = 5, sslContext: SSLContext = defaultSSLContext, @@ -528,14 +529,15 @@ proc post*(url: string, extraHeaders = "", body = "", result = request(url, httpPOST, xh, xb, sslContext, timeout, userAgent, proxy) - var lastUrl = "" + var lastURL = url for i in 1..maxRedirects: if result.status.redirection(): + echo "lastURL: ", lastURL let redirectTo = getNewLocation(lastURL, result.headers) var meth = if result.status != "307": httpGet else: httpPost result = request(redirectTo, meth, xh, xb, sslContext, timeout, userAgent, proxy) - lastUrl = redirectTo + lastURL = redirectTo proc postContent*(url: string, extraHeaders = "", body = "", maxRedirects = 5, @@ -827,7 +829,7 @@ proc get*(client: AsyncHttpClient, url: string): Future[Response] {.async.} = if result.status.redirection(): let redirectTo = getNewLocation(lastURL, result.headers) result = await client.request(redirectTo, httpGET) - lastUrl = redirectTo + lastURL = redirectTo proc post*(client: AsyncHttpClient, url: string, body = "", multipart: MultipartData = nil): Future[Response] {.async.} = ## Connects to the hostname specified by the URL and performs a POST request. |