From: Tom Christie Date: Wed, 28 Apr 2021 19:39:11 +0000 (+0100) Subject: Stream tweak (#1607) X-Git-Tag: 0.18.1~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4fbf2751ec353b09fafe9e3b22cbe07e49a26442;p=thirdparty%2Fhttpx.git Stream tweak (#1607) --- diff --git a/httpx/_client.py b/httpx/_client.py index ae42e9ea..2928577b 100644 --- a/httpx/_client.py +++ b/httpx/_client.py @@ -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 diff --git a/httpx/_models.py b/httpx/_models.py index a6157a87..e4069072 100644 --- a/httpx/_models.py +++ b/httpx/_models.py @@ -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