Implement suggestions from @dom96
This commit is contained in:
parent
52f4590e98
commit
4753149227
2 changed files with 19 additions and 20 deletions
|
|
@ -133,9 +133,9 @@ proc processClient(client: AsyncSocket, address: string,
|
||||||
assert client != nil
|
assert client != nil
|
||||||
request.client = client
|
request.client = client
|
||||||
|
|
||||||
# We should skip empty lines before the request
|
# We should skip at least one empty line before the request
|
||||||
# https://tools.ietf.org/html/rfc7230#section-3.5
|
# https://tools.ietf.org/html/rfc7230#section-3.5
|
||||||
while true:
|
for i in 0..1:
|
||||||
lineFut.mget().setLen(0)
|
lineFut.mget().setLen(0)
|
||||||
lineFut.clean()
|
lineFut.clean()
|
||||||
await client.recvLineInto(lineFut) # TODO: Timeouts.
|
await client.recvLineInto(lineFut) # TODO: Timeouts.
|
||||||
|
|
|
||||||
|
|
@ -989,8 +989,11 @@ proc newConnection(client: HttpClient | AsyncHttpClient,
|
||||||
client.currentURL = url
|
client.currentURL = url
|
||||||
client.connected = true
|
client.connected = true
|
||||||
|
|
||||||
proc withFallback(override, fallback: HttpHeaders): HttpHeaders =
|
proc override(fallback, override: HttpHeaders): HttpHeaders =
|
||||||
# Left-biased map union for `HttpHeaders`
|
# Right-biased map union for `HttpHeaders`
|
||||||
|
if override.isNil:
|
||||||
|
return fallback
|
||||||
|
|
||||||
result = newHttpHeaders()
|
result = newHttpHeaders()
|
||||||
# Copy by value
|
# Copy by value
|
||||||
result.table[] = fallback.table[]
|
result.table[] = fallback.table[]
|
||||||
|
|
@ -999,7 +1002,7 @@ proc withFallback(override, fallback: HttpHeaders): HttpHeaders =
|
||||||
|
|
||||||
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
httpMethod: string, body = "",
|
httpMethod: string, body = "",
|
||||||
overrideHeaders: HttpHeaders = nil): Future[Response] {.multisync.} =
|
headers: 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``.
|
||||||
##
|
##
|
||||||
|
|
@ -1032,19 +1035,15 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
else:
|
else:
|
||||||
await newConnection(client, connectionUrl)
|
await newConnection(client, connectionUrl)
|
||||||
|
|
||||||
var effectiveHeaders: HttpHeaders
|
let effectiveHeaders = client.headers.override(headers)
|
||||||
if overrideHeaders != nil:
|
|
||||||
effectiveHeaders = overrideHeaders.withFallback(client.headers)
|
|
||||||
else:
|
|
||||||
effectiveHeaders = client.headers
|
|
||||||
|
|
||||||
if not effectiveHeaders.hasKey("user-agent") and client.userAgent != "":
|
if not effectiveHeaders.hasKey("user-agent") and client.userAgent != "":
|
||||||
effectiveHeaders["User-Agent"] = client.userAgent
|
effectiveHeaders["User-Agent"] = client.userAgent
|
||||||
|
|
||||||
var headers = generateHeaders(requestUrl, httpMethod,
|
var headersString = generateHeaders(requestUrl, httpMethod,
|
||||||
effectiveHeaders, body, client.proxy)
|
effectiveHeaders, body, client.proxy)
|
||||||
|
|
||||||
await client.socket.send(headers)
|
await client.socket.send(headersString)
|
||||||
if body != "":
|
if body != "":
|
||||||
await client.socket.send(body)
|
await client.socket.send(body)
|
||||||
|
|
||||||
|
|
@ -1056,7 +1055,7 @@ proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
|
|
||||||
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
proc request*(client: HttpClient | AsyncHttpClient, url: string,
|
||||||
httpMethod = HttpGET, body = "",
|
httpMethod = HttpGET, body = "",
|
||||||
overrideHeaders: HttpHeaders = nil): Future[Response] {.multisync.} =
|
headers: 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.
|
||||||
##
|
##
|
||||||
|
|
@ -1067,7 +1066,7 @@ 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)
|
headers = headers)
|
||||||
|
|
||||||
proc get*(client: HttpClient | AsyncHttpClient,
|
proc get*(client: HttpClient | AsyncHttpClient,
|
||||||
url: string): Future[Response] {.multisync.} =
|
url: string): Future[Response] {.multisync.} =
|
||||||
|
|
@ -1115,13 +1114,13 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
x
|
x
|
||||||
var xb = mpBody.withNewLine() & body
|
var xb = mpBody.withNewLine() & body
|
||||||
|
|
||||||
var overrideHeaders = newHttpHeaders()
|
var headers = newHttpHeaders()
|
||||||
if multipart != nil:
|
if multipart != nil:
|
||||||
overrideHeaders["Content-Type"] = mpHeader.split(": ")[1]
|
headers["Content-Type"] = mpHeader.split(": ")[1]
|
||||||
overrideHeaders["Content-Length"] = $len(xb)
|
headers["Content-Length"] = $len(xb)
|
||||||
|
|
||||||
result = await client.request(url, HttpPOST, xb,
|
result = await client.request(url, HttpPOST, xb,
|
||||||
overrideHeaders = overrideHeaders)
|
headers = headers)
|
||||||
# Handle redirects.
|
# Handle redirects.
|
||||||
var lastURL = url
|
var lastURL = url
|
||||||
for i in 1..client.maxRedirects:
|
for i in 1..client.maxRedirects:
|
||||||
|
|
@ -1129,7 +1128,7 @@ proc post*(client: HttpClient | AsyncHttpClient, url: string, body = "",
|
||||||
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)
|
headers = headers)
|
||||||
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