Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpclient Response object references original Request; refs #16685 #16691

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion lib/pure/httpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,27 @@ import nativesockets
export httpcore except parseHeader # TODO: The ``except`` doesn't work

type
PreparedRequest* = object
reqMethod*: HttpMethod
headers*: HttpHeaders
url*: Uri
body*: string

Response* = ref object
version*: string
status*: string
headers*: HttpHeaders
body: string
bodyStream*: Stream
request*: PreparedRequest

AsyncResponse* = ref object
version*: string
status*: string
headers*: HttpHeaders
body: string
bodyStream*: FutureStream[string]
request*: PreparedRequest

proc code*(response: Response | AsyncResponse): HttpCode
{.raises: [ValueError, OverflowDefect].} =
Expand Down Expand Up @@ -896,10 +904,16 @@ proc newConnection(client: HttpClient | AsyncHttpClient,
connectUrl.hostname = url.hostname
connectUrl.port = if url.port != "": url.port else: "443"

let newHeaders = newHttpHeaders()
let proxyHeaderString = generateHeaders(connectUrl, HttpConnect,
newHttpHeaders(), client.proxy)
newHeaders, client.proxy)
await client.socket.send(proxyHeaderString)
let proxyResp = await parseResponse(client, false)
proxyResp.request = PreparedRequest(
reqMethod: HttpConnect,
headers: newHeaders,
url: connectUrl,
)

if not proxyResp.status.startsWith("200"):
raise newException(HttpRequestError,
Expand Down Expand Up @@ -1019,6 +1033,12 @@ proc requestAux(client: HttpClient | AsyncHttpClient, url: Uri,
let getBody = httpMethod notin {HttpHead, HttpConnect} and
client.getBody
result = await parseResponse(client, getBody)
result.request = PreparedRequest(
reqMethod: httpMethod,
headers: newHeaders,
url: url,
body: body,
)

proc request*(client: HttpClient | AsyncHttpClient, url: Uri | string,
httpMethod: HttpMethod | string = HttpGet, body = "",
Expand Down
10 changes: 9 additions & 1 deletion tests/stdlib/thttpclient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ discard """
import strutils
from net import TimeoutError

import nativesockets, os, httpclient, asyncdispatch
import nativesockets, os, httpclient, asyncdispatch, uri

const manualTests = false

Expand Down Expand Up @@ -41,6 +41,10 @@ proc asyncTest() {.async.} =
var client = newAsyncHttpClient()
var resp = await client.request("http://example.com/", HttpGet)
doAssert(resp.code.is2xx)
doAssert(resp.request.reqMethod == HttpGet)
doAssert($(resp.request.url) == "http://example.com/")
var clientAgent = $(resp.request.headers["user-agent"])
doAssert(clientAgent.contains("Nim httpclient"))
var body = await resp.body
body = await resp.body # Test caching
doAssert("<title>Example Domain</title>" in body)
Expand Down Expand Up @@ -104,6 +108,10 @@ proc syncTest() =
var client = newHttpClient()
var resp = client.request("http://example.com/", HttpGet)
doAssert(resp.code.is2xx)
doAssert(resp.request.reqMethod == HttpGet)
doAssert($(resp.request.url) == "http://example.com/")
var clientAgent = $(resp.request.headers["user-agent"])
doAssert(clientAgent.contains("Nim httpclient"))
doAssert("<title>Example Domain</title>" in resp.body)

resp = client.request("http://example.com/404")
Expand Down