]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Stream tweak (#1607)
authorTom Christie <tom@tomchristie.com>
Wed, 28 Apr 2021 19:39:11 +0000 (20:39 +0100)
committerGitHub <noreply@github.com>
Wed, 28 Apr 2021 19:39:11 +0000 (20:39 +0100)
httpx/_client.py
httpx/_models.py

index ae42e9eac62f42c6ab76e53d60aa7601ae66dd02..2928577b832969b3f87c2650d7539efe836005cc 100644 (file)
@@ -497,9 +497,8 @@ class BaseClient:
             # the origin.
             headers.pop("Authorization", None)
 
-            # Remove the Host header, so that a new one will be auto-populated on
-            # the request instance.
-            headers.pop("Host", None)
+            # Update the Host header.
+            headers["Host"] = url.netloc.decode("ascii")
 
         if method != request.method and method == "GET":
             # If we've switch to a 'GET' request, then strip any headers which
index a6157a8728beafc3b1593c2e11bfa123c5f8290e..e406907241ef6b4830538fedfd54689813c97210 100644 (file)
@@ -1089,14 +1089,21 @@ class Request:
         if cookies:
             Cookies(cookies).set_cookie_header(self)
 
-        if stream is not None:
+        if stream is None:
+            headers, stream = encode_request(content, data, files, json)
+            self._prepare(headers)
+            self.stream = stream
+            # Load the request body, except for streaming content.
+            if isinstance(stream, ByteStream):
+                self.read()
+        else:
             # There's an important distinction between `Request(content=...)`,
             # and `Request(stream=...)`.
             #
-            # Using `content=...` implies automatically populated content headers,
-            # of either `Content-Length: ...` or `Transfer-Encoding: chunked`.
+            # Using `content=...` implies automatically populated `Host` and content
+            # headers, of either `Content-Length: ...` or `Transfer-Encoding: chunked`.
             #
-            # Using `stream=...` will not automatically include any content headers.
+            # Using `stream=...` will not automatically include *any* auto-populated headers.
             #
             # As an end-user you don't really need `stream=...`. It's only
             # useful when:
@@ -1104,14 +1111,6 @@ class Request:
             # * Preserving the request stream when copying requests, eg for redirects.
             # * Creating request instances on the *server-side* of the transport API.
             self.stream = stream
-            self._prepare({})
-        else:
-            headers, stream = encode_request(content, data, files, json)
-            self._prepare(headers)
-            self.stream = stream
-            # Load the request body, except for streaming content.
-            if isinstance(stream, ByteStream):
-                self.read()
 
     def _prepare(self, default_headers: typing.Dict[str, str]) -> None:
         for key, value in default_headers.items():
@@ -1216,7 +1215,14 @@ class Response:
         self.is_closed = False
         self.is_stream_consumed = False
 
-        if stream is not None:
+        if stream is None:
+            headers, stream = encode_response(content, text, html, json)
+            self._prepare(headers)
+            self.stream = stream
+            if isinstance(stream, ByteStream):
+                # Load the response body, except for streaming content.
+                self.read()
+        else:
             # There's an important distinction between `Response(content=...)`,
             # and `Response(stream=...)`.
             #
@@ -1229,13 +1235,6 @@ class Response:
             # useful when creating response instances having received a stream
             # from the transport API.
             self.stream = stream
-        else:
-            headers, stream = encode_response(content, text, html, json)
-            self._prepare(headers)
-            self.stream = stream
-            if isinstance(stream, ByteStream):
-                # Load the response body, except for streaming content.
-                self.read()
 
         self._num_bytes_downloaded = 0