Don't persist generated headers between requests
This commit is contained in:
parent
860264a382
commit
52f4590e98
1 changed files with 33 additions and 12 deletions
|
|
@ -117,7 +117,7 @@
|
||||||
## only basic authentication is supported at the moment.
|
## only basic authentication is supported at the moment.
|
||||||
|
|
||||||
import net, strutils, uri, parseutils, strtabs, base64, os, mimetypes,
|
import net, strutils, uri, parseutils, strtabs, base64, os, mimetypes,
|
||||||
math, random, httpcore, times
|
math, random, httpcore, times, tables
|
||||||
import asyncnet, asyncdispatch
|
import asyncnet, asyncdispatch
|
||||||
import nativesockets
|
import nativesockets
|
||||||
|
|
||||||
|
|
@ -989,8 +989,17 @@ proc newConnection(client: HttpClient | AsyncHttpClient,
|
||||||
client.currentURL = url
|
client.currentURL = url
|
||||||
client.connected = true
|
client.connected = true
|
||||||
|
|
||||||
|
proc withFallback(override, fallback: HttpHeaders): HttpHeaders =
|
||||||
|
# Left-biased map union for `HttpHeaders`
|
||||||
|
result = newHttpHeaders()
|
||||||
|
# Copy by value
|
||||||
|
result.table[] = fallback.table[]
|
||||||
|
for k, vs in override.table:
|
||||||
|
result[k] = vs
|
||||||
|
|
||||||
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
httpMethod: string, body = ""): Future[Response] {.multisync.} =
|
httpMethod: string, body = "",
|
||||||
|
overrideHeaders: HttpHeaders = nil): Future[Response] {.multisync.} =
|
||||||
## Connects to the hostname specified by the URL and performs a request
|
## Connects to the hostname specified by the URL and performs a request
|
||||||
## using the custom method string specified by ``httpMethod``.
|
## using the custom method string specified by ``httpMethod``.
|
||||||
##
|
##
|
||||||
|
|
@ -1023,11 +1032,17 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
else:
|
else:
|
||||||
await newConnection(client, connectionUrl)
|
await newConnection(client, connectionUrl)
|
||||||
|
|
||||||
if not client.headers.hasKey("user-agent") and client.userAgent != "":
|
var effectiveHeaders: HttpHeaders
|
||||||
client.headers["User-Agent"] = client.userAgent
|
if overrideHeaders != nil:
|
||||||
|
effectiveHeaders = overrideHeaders.withFallback(client.headers)
|
||||||
|
else:
|
||||||
|
effectiveHeaders = client.headers
|
||||||
|
|
||||||
|
if not effectiveHeaders.hasKey("user-agent") and client.userAgent != "":
|
||||||
|
effectiveHeaders["User-Agent"] = client.userAgent
|
||||||
|
|
||||||
var headers = generateHeaders(requestUrl, httpMethod,
|
var headers = generateHeaders(requestUrl, httpMethod,
|
||||||
client.headers, body, client.proxy)
|
effectiveHeaders, body, client.proxy)
|
||||||
|
|
||||||
await client.socket.send(headers)
|
await client.socket.send(headers)
|
||||||
if body != "":
|
if body != "":
|
||||||
|
|
@ -1040,7 +1055,8 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
client.proxy = savedProxy
|
client.proxy = savedProxy
|
||||||
|
|
||||||
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
httpMethod = HttpGET, body = ""): Future[Response] {.multisync.} =
|
httpMethod = HttpGET, body = "",
|
||||||
|
overrideHeaders: HttpHeaders = nil): Future[Response] {.multisync.} =
|
||||||
## Connects to the hostname specified by the URL and performs a request
|
## Connects to the hostname specified by the URL and performs a request
|
||||||
## using the method specified.
|
## using the method specified.
|
||||||
##
|
##
|
||||||
|
|
@ -1050,7 +1066,8 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
##
|
##
|
||||||
## When a request is made to a different hostname, the current connection will
|
## When a request is made to a different hostname, the current connection will
|
||||||
## be closed.
|
## be closed.
|
||||||
result = await request(client, url, $httpMethod, body)
|
result = await request(client, url, $httpMethod, body,
|
||||||
|
overrideHeaders = overrideHeaders)
|
||||||
|
|
||||||
proc get*(client: HttpClient | AsyncHttpClient,
|
proc get*(client: HttpClient | AsyncHttpClient,
|
||||||
url: string): Future[Response] {.multisync.} =
|
url: string): Future[Response] {.multisync.} =
|
||||||
|
|
@ -1097,18 +1114,22 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
else:
|
else:
|
||||||
x
|
x
|
||||||
var xb = mpBody.withNewLine() & body
|
var xb = mpBody.withNewLine() & body
|
||||||
if multipart != nil:
|
|
||||||
client.headers["Content-Type"] = mpHeader.split(": ")[1]
|
|
||||||
client.headers["Content-Length"] = $len(xb)
|
|
||||||
|
|
||||||
result = await client.request(url, HttpPOST, xb)
|
var overrideHeaders = newHttpHeaders()
|
||||||
|
if multipart != nil:
|
||||||
|
overrideHeaders["Content-Type"] = mpHeader.split(": ")[1]
|
||||||
|
overrideHeaders["Content-Length"] = $len(xb)
|
||||||
|
|
||||||
|
result = await client.request(url, HttpPOST, xb,
|
||||||
|
overrideHeaders = overrideHeaders)
|
||||||
# Handle redirects.
|
# Handle redirects.
|
||||||
var lastURL = url
|
var lastURL = url
|
||||||
for i in 1..client.maxRedirects:
|
for i in 1..client.maxRedirects:
|
||||||
if result.status.redirection():
|
if result.status.redirection():
|
||||||
let redirectTo = getNewLocation(lastURL, result.headers)
|
let redirectTo = getNewLocation(lastURL, result.headers)
|
||||||
var meth = if result.status != "307": HttpGet else: HttpPost
|
var meth = if result.status != "307": HttpGet else: HttpPost
|
||||||
result = await client.request(redirectTo, meth, xb)
|
result = await client.request(redirectTo, meth, xb,
|
||||||
|
overrideHeaders = overrideHeaders)
|
||||||
lastURL = redirectTo
|
lastURL = redirectTo
|
||||||
|
|
||||||
proc postContent*(client: HttpClient | AsyncHttpClient, url: string,
|
proc postContent*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue