diff options
author | Joey <jyapayne@gmail.com> | 2021-01-27 17:07:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-28 00:07:08 +0000 |
commit | 0d1c5f413794cdc9b84b5f6738df235bc648d195 (patch) | |
tree | e4d18ca27f9d4ec460758549ff378e43140b9682 | |
parent | e1129749207d25597e81fe06cd7e3099b5ecb61e (diff) | |
download | Nim-0d1c5f413794cdc9b84b5f6738df235bc648d195.tar.gz |
A new request should always have a new content-length (#16667)
* A new request should always have a new content-length In [my last PR](https://github.com/nim-lang/Nim/pull/16618) I made a mistake by assuming that the client.headers were cleared on every request, like the Python version. So, due to the fact that Nim keeps the client headers, we need to clear the Content-Length header on each request (which makes sense because you almost never want to use the same Content-Length twice, but you may want to reuse other headers) * Move content-length to newHeaders instead of in the global client headers * Use single backticks
-rw-r--r-- | lib/pure/httpclient.nim | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim index b6e6ec0fe..5508f0dbf 100644 --- a/lib/pure/httpclient.nim +++ b/lib/pure/httpclient.nim @@ -978,15 +978,6 @@ proc requestAux(client: HttpClient | AsyncHttpClient, url: Uri, if url.scheme == "": raise newException(ValueError, "No uri scheme supplied.") - var data: seq[string] - if multipart != nil and multipart.content.len > 0: - data = await client.format(multipart) - else: - if body.len != 0: - client.headers["Content-Length"] = $body.len - elif httpMethod notin [HttpGet, HttpHead] and not client.headers.hasKey("Content-Length"): - client.headers["Content-Length"] = "0" - when client is AsyncHttpClient: if not client.parseBodyFut.isNil: # let the current operation finish before making another request @@ -996,6 +987,18 @@ proc requestAux(client: HttpClient | AsyncHttpClient, url: Uri, await newConnection(client, url) let newHeaders = client.headers.override(headers) + + var data: seq[string] + if multipart != nil and multipart.content.len > 0: + data = await client.format(multipart) + else: + # Only change headers if they have not been specified already + if not newHeaders.hasKey("Content-Length"): + if body.len != 0: + newHeaders["Content-Length"] = $body.len + elif httpMethod notin {HttpGet, HttpHead}: + newHeaders["Content-Length"] = "0" + if not newHeaders.hasKey("user-agent") and client.userAgent.len > 0: newHeaders["User-Agent"] = client.userAgent @@ -1043,6 +1046,9 @@ proc request*(client: HttpClient | AsyncHttpClient, url: Uri | string, ## You need to make sure that the ``url`` doesn't contain any newline ## characters. Failing to do so will raise ``AssertionDefect``. ## + ## `headers` are HTTP headers that override the `client.headers` for + ## this specific request only and will not be persisted. + ## ## **Deprecated since v1.5**: use HttpMethod enum instead; string parameter httpMethod is deprecated when url is string: doAssert(not url.contains({'\c', '\L'}), "url shouldn't contain any newline characters") |