From b2237678fba9cea93406fab6e62a2cf0916db5ee Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Thu, 19 Jan 2017 15:06:08 +0200 Subject: [PATCH 1/3] Redirects support in request proc --- lib/pure/httpclient.nim | 46 +++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index ba967b14fc..847114d582 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -1000,15 +1000,10 @@ proc override(fallback, override: HttpHeaders): HttpHeaders = for k, vs in override.table: result[k] = vs -proc request*(client: HttpClient | AsyncHttpClient, url: string, +proc requestAux(client: HttpClient | AsyncHttpClient, url: string, httpMethod: string, body = "", headers: HttpHeaders = nil): Future[Response] {.multisync.} = - ## Connects to the hostname specified by the URL and performs a request - ## using the custom method string specified by ``httpMethod``. - ## - ## Connection will kept alive. Further requests on the same ``client`` to - ## the same hostname will not require a new connection to be made. The - ## connection can be closed by using the ``close`` procedure. + # Helper that actually makes the request. Does not handle redirects. let connectionUrl = if client.proxy.isNil: parseUri(url) else: client.proxy.url let requestUrl = parseUri(url) @@ -1021,7 +1016,7 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string, var connectUrl = requestUrl connectUrl.scheme = "http" connectUrl.port = "443" - let proxyResp = await request(client, $connectUrl, $HttpConnect) + let proxyResp = await requestAux(client, $connectUrl, $HttpConnect) if not proxyResp.status.startsWith("200"): raise newException(HttpRequestError, @@ -1053,6 +1048,29 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string, # Restore the clients proxy in case it was overwritten. client.proxy = savedProxy + +proc request*(client: HttpClient | AsyncHttpClient, url: string, + httpMethod: string, body = "", + headers: HttpHeaders = nil): Future[Response] {.multisync.} = + ## Connects to the hostname specified by the URL and performs a request + ## using the custom method string specified by ``httpMethod``. + ## + ## Connection will kept alive. Further requests on the same ``client`` to + ## the same hostname will not require a new connection to be made. The + ## connection can be closed by using the ``close`` procedure. + ## + ## This procedure will follow redirects up to a maximum number of redirects + ## specified in ``client.maxRedirects``. + result = await client.requestAux(url, httpMethod, body, headers) + + var lastURL = url + for i in 1..client.maxRedirects: + if result.status.redirection(): + let redirectTo = getNewLocation(lastURL, result.headers) + result = await client.request(redirectTo, httpMethod, body, headers) + lastURL = redirectTo + + proc request*(client: HttpClient | AsyncHttpClient, url: string, httpMethod = HttpGET, body = "", headers: HttpHeaders = nil): Future[Response] {.multisync.} = @@ -1076,14 +1094,6 @@ proc get*(client: HttpClient | AsyncHttpClient, ## specified in ``client.maxRedirects``. result = await client.request(url, HttpGET) - # Handle redirects. - var lastURL = url - for i in 1..client.maxRedirects: - if result.status.redirection(): - let redirectTo = getNewLocation(lastURL, result.headers) - result = await client.request(redirectTo, HttpGET) - lastURL = redirectTo - proc getContent*(client: HttpClient | AsyncHttpClient, url: string): Future[string] {.multisync.} = ## Connects to the hostname specified by the URL and performs a GET request. @@ -1119,7 +1129,7 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "", headers["Content-Type"] = mpHeader.split(": ")[1] headers["Content-Length"] = $len(xb) - result = await client.request(url, HttpPOST, xb, + result = await client.requestAux(url, $HttpPOST, xb, headers = headers) # Handle redirects. var lastURL = url @@ -1127,7 +1137,7 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "", if result.status.redirection(): let redirectTo = getNewLocation(lastURL, result.headers) var meth = if result.status != "307": HttpGet else: HttpPost - result = await client.request(redirectTo, meth, xb, + result = await client.requestAux(redirectTo, $meth, xb, headers = headers) lastURL = redirectTo From 2f320fb28617de70f7bbfea95bf7817d5acfb129 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Fri, 20 Jan 2017 12:59:13 +0200 Subject: [PATCH 2/3] Fixed query and anchor during relative redirection --- lib/pure/httpclient.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index 847114d582..760e97049d 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -540,6 +540,8 @@ proc getNewLocation(lastURL: string, headers: HttpHeaders): string = if r.hostname == "" and r.path != "": var parsed = parseUri(lastURL) parsed.path = r.path + parsed.query = r.query + parsed.anchor = r.anchor result = $parsed proc get*(url: string, extraHeaders = "", maxRedirects = 5, From 0d735aa73c835cf8d2a9eed91c5e1cac38fbbcc4 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Fri, 20 Jan 2017 13:06:03 +0200 Subject: [PATCH 3/3] Added news --- web/news.rst | 3 +++ web/news/e031_version_0_16_2.rst | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 web/news/e031_version_0_16_2.rst diff --git a/web/news.rst b/web/news.rst index e677d02afe..5086bc682c 100644 --- a/web/news.rst +++ b/web/news.rst @@ -2,6 +2,9 @@ News ==== +`2017-01-08 Nim Version 0.16.2 released `_ +=================================== + `2017-01-08 Nim Version 0.16.0 released `_ =================================== diff --git a/web/news/e031_version_0_16_2.rst b/web/news/e031_version_0_16_2.rst new file mode 100644 index 0000000000..4d9d99d165 --- /dev/null +++ b/web/news/e031_version_0_16_2.rst @@ -0,0 +1,27 @@ +Version 0.16.2 released +======================= + +Changelog +~~~~~~~~~ + +Changes affecting backwards compatibility +----------------------------------------- + +- ``httpclient.request`` now respects ``maxRedirects`` option. Previously + redirects were handled only by ``get`` and ``post`` procs. + +Library Additions +----------------- + + +Tool Additions +-------------- + + +Compiler Additions +------------------ + + +Language Additions +------------------ +