summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--lib/pure/httpclient.nim10
-rw-r--r--tests/stdlib/thttpclient.nim6
2 files changed, 12 insertions, 4 deletions
diff --git a/lib/pure/httpclient.nim b/lib/pure/httpclient.nim
index bcd10803e..0ca3ca97b 100644
--- a/lib/pure/httpclient.nim
+++ b/lib/pure/httpclient.nim
@@ -832,14 +832,16 @@ proc parseResponse(client: HttpClient | AsyncHttpClient,
   if not fullyRead:
     httpError("Connection was closed before full request has been made")
 
+  when client is HttpClient:
+    result.bodyStream = newStringStream()
+  else:
+    result.bodyStream = newFutureStream[string]("parseResponse")
+
   if getBody and result.code != Http204:
+    client.bodyStream = result.bodyStream
     when client is HttpClient:
-      client.bodyStream = newStringStream()
-      result.bodyStream = client.bodyStream
       parseBody(client, result.headers, result.version)
     else:
-      client.bodyStream = newFutureStream[string]("parseResponse")
-      result.bodyStream = client.bodyStream
       assert(client.parseBodyFut.isNil or client.parseBodyFut.finished)
       # do not wait here for the body request to complete
       client.parseBodyFut = parseBody(client, result.headers, result.version)
diff --git a/tests/stdlib/thttpclient.nim b/tests/stdlib/thttpclient.nim
index 0cef10e6d..e81590d95 100644
--- a/tests/stdlib/thttpclient.nim
+++ b/tests/stdlib/thttpclient.nim
@@ -148,6 +148,12 @@ proc syncTest() =
 
   client.close()
 
+  # SIGSEGV on HEAD body read: issue #16743
+  block:
+    let client = newHttpClient()
+    let resp = client.head("http://httpbin.org/head")
+    doAssert(resp.body == "")
+
   when false:
     # Disabled for now because it causes troubles with AppVeyor
     # Timeout test.