]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Truckin' on
authorTom Christie <tom@tomchristie.com>
Wed, 1 May 2019 11:39:16 +0000 (12:39 +0100)
committerTom Christie <tom@tomchristie.com>
Wed, 1 May 2019 11:39:16 +0000 (12:39 +0100)
httpcore/dispatch/http11.py
httpcore/dispatch/http2.py
httpcore/models.py
tests/models/test_url.py

index fc3a0334ebb27b8035319af7a2c02abda4c22307..2a0cf2e00a5d06c1c9af9ab5db7234d6995ae07d 100644 (file)
@@ -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)
index 6d0a8f04daac96dde84b5c899b09b048e8eda9ec..fb45d5418f2d092fd3341fd43f4f69c021905e3f 100644 (file)
@@ -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()
index 1bdd0fe1a7505b10835ecb817868e76f9785d98c..09921fcf7f89038622c193e0f76e39ef8337698d 100644 (file)
@@ -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]:
index 6de64395f477b9a2f5630674e73c2d7d3c42d65a..cf69a6fa89d6726b5ae42388842ee04bbbcb9354 100644 (file)
@@ -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")