From 0ca1c844cd23cc89fb55cef7a8dc8683ffe07d91 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 1 May 2019 12:39:16 +0100 Subject: [PATCH] Truckin' on --- httpcore/dispatch/http11.py | 4 ++-- httpcore/dispatch/http2.py | 10 +++++----- httpcore/models.py | 13 +++++++++---- tests/models/test_url.py | 4 +++- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/httpcore/dispatch/http11.py b/httpcore/dispatch/http11.py index fc3a0334..2a0cf2e0 100644 --- a/httpcore/dispatch/http11.py +++ b/httpcore/dispatch/http11.py @@ -54,8 +54,8 @@ class HTTP11Connection(Adapter): assert timeout is None or isinstance(timeout, TimeoutConfig) #  Start sending the request. - method = request.method.encode('ascii') - target = request.url.full_path.encode('ascii') + method = request.method.encode("ascii") + target = request.url.full_path.encode("ascii") headers = request.headers.raw event = h11.Request(method=method, target=target, headers=headers) await self._send_event(event, timeout) diff --git a/httpcore/dispatch/http2.py b/httpcore/dispatch/http2.py index 6d0a8f04..fb45d541 100644 --- a/httpcore/dispatch/http2.py +++ b/httpcore/dispatch/http2.py @@ -62,7 +62,7 @@ class HTTP2Connection(Adapter): headers = [] for k, v in event.headers: if k == b":status": - status_code = int(v.decode('ascii', errors='ignore')) + status_code = int(v.decode("ascii", errors="ignore")) elif not k.startswith(b":"): headers.append((k, v)) @@ -98,10 +98,10 @@ class HTTP2Connection(Adapter): async def send_headers(self, request: Request, timeout: OptionalTimeout) -> int: stream_id = self.h2_state.get_next_available_stream_id() headers = [ - (b":method", request.method.encode('ascii')), - (b":authority", request.url.authority.encode('ascii')), - (b":scheme", request.url.scheme.encode('ascii')), - (b":path", request.url.full_path.encode('ascii')), + (b":method", request.method.encode("ascii")), + (b":authority", request.url.authority.encode("ascii")), + (b":scheme", request.url.scheme.encode("ascii")), + (b":path", request.url.full_path.encode("ascii")), ] + request.headers.raw self.h2_state.send_headers(stream_id, headers) data_to_send = self.h2_state.data_to_send() diff --git a/httpcore/models.py b/httpcore/models.py index 1bdd0fe1..09921fcf 100644 --- a/httpcore/models.py +++ b/httpcore/models.py @@ -509,13 +509,18 @@ class Response: if content_type is None: return None + parsed = cgi.parse_header(content_type) + media_type, params = parsed[0], parsed[-1] + if "charset" in params: + return params["charset"].strip("'\"") + # RFC 2616 specifies that 'iso-8859-1' should be used as the default # for 'text/*' media types, if no charset is provided. # See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1 - parsed = cgi.parse_header(content_type) - media_type, info = parsed[0], parsed[-1] - default = "iso-8859-1" if media_type.startswith("text/") else None - return info.get("charset", default) + if media_type.startswith("text/"): + return "iso-8859-1" + + return None @property def apparent_encoding(self) -> typing.Optional[str]: diff --git a/tests/models/test_url.py b/tests/models/test_url.py index 6de64395..cf69a6fa 100644 --- a/tests/models/test_url.py +++ b/tests/models/test_url.py @@ -16,7 +16,9 @@ def test_url(): assert url.path == "/path/to/somewhere" assert url.query == "abc=123" assert url.fragment == "anchor" - assert repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')" + assert ( + repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')" + ) new = url.copy_with(scheme="http") assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor") -- 2.47.3