Skip to content
Myr

Carefulness with encoding everywhere

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by Tom Christie · parent b948798 · Browse files · 5 files+56 added−21 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+56 added−21 removed

  • tests:+42 added−6 removed
  • other:+14 added−15 removed

5 files+56 added−21 removed

modified:httpcore/dispatch/http11.py+3 added-3 removed
52 unmodified lines
53 stream = options.get("stream", False)54 assert timeout is None or isinstance(timeout, TimeoutConfig)5556 #  Start sending the request.57 method = request.method.encode()58 target = request.url.full_path57 method = request.method.encode('ascii')58 target = request.url.full_path.encode('ascii')59 headers = request.headers.raw60 event = h11.Request(method=method, target=target, headers=headers)61 await self._send_event(event, timeout)62
11 unmodified lines
74 if isinstance(event, h11.InformationalResponse):75 event = await self._receive_event(timeout)7677 assert isinstance(event, h11.Response)78 reason_phrase = event.reason.decode("latin1")78 reason_phrase = event.reason.decode("ascii", errors="ignore")79 status_code = event.status_code80 headers = event.headers81 content = self._body_iter(timeout)82
66 unmodified lines
modified:httpcore/dispatch/http2.py+5 added-5 removed
60 unmodified lines
61 status_code = 20062 headers = []63 for k, v in event.headers:64 if k == b":status":65 status_code = int(v.decode())65 status_code = int(v.decode('ascii', errors='ignore'))66 elif not k.startswith(b":"):67 headers.append((k, v))6869 content = self.body_iter(stream_id, timeout)
27 unmodified lines
9798 async def send_headers(self, request: Request, timeout: OptionalTimeout) -> int:99 stream_id = self.h2_state.get_next_available_stream_id()100 headers = [101 (b":method", request.method.encode()),102 (b":authority", request.url.host.encode()),103 (b":scheme", request.url.scheme.encode()),104 (b":path", request.url.full_path.encode()),101 (b":method", request.method.encode('ascii')),102 (b":authority", request.url.authority.encode('ascii')),103 (b":scheme", request.url.scheme.encode('ascii')),104 (b":path", request.url.full_path.encode('ascii')),105 ] + request.headers.raw106 self.h2_state.send_headers(stream_id, headers)107 data_to_send = self.h2_state.data_to_send()108 await self.writer.write(data_to_send, timeout)
48 unmodified lines
modified:httpcore/models.py+6 added-7 removed
87 unmodified lines
88 def port(self) -> int:89 port = self.components.port90 if port is None:91 return {"https": 443, "http": 80}[self.scheme]92 return port92 return int(port)9394 @property95 def full_path(self) -> str:96 path = self.path or "/"97 query = self.query98 if query:99 return path + "?" + query96 path = self.path97 if self.query:98 path += "?" + self.query99 return path100101 @property103 def is_secure(self) -> bool:102 def is_ssl(self) -> bool:103 return self.components.scheme == "https"104105 @property106 def is_absolute_url(self) -> bool:
46 unmodified lines
153class Origin:154 def __init__(self, url: URLTypes) -> None:155 if not isinstance(url, URL):156 url = URL(url)158 self.is_ssl = url.scheme == "https"157 self.is_ssl = url.is_ssl158 self.host = url.host159 self.port = url.port160161 def __eq__(self, other: typing.Any) -> bool:
446 unmodified lines
modified:tests/models/test_responses.py+26 added-6 removed
14 unmodified lines
15 assert response.text == "Hello, world!"161718def test_response_content_type_encoding():19 """20 Use the charset encoding in the Content-Type header if possible.21 """22 headers = {"Content-Type": "text-plain; charset=latin-1"}20 response = httpcore.Response(21 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers22 )23 content = "Latin 1: ÿ".encode("latin-1")24 response = httpcore.Response(200, content=content, headers=headers)25 assert response.text == "Latin 1: ÿ"26 assert response.encoding == "latin-1"272829def test_response_autodetect_encoding():30 """31 Autodetect encoding if there is no charset info in a Content-Type header.32 """33 content = "おはようございます。".encode("EUC-JP")28 response = httpcore.Response(200, content="Snowmen: ☃☃☃".encode("utf-8"))29 assert response.text == "Snowmen: ☃☃☃"30 assert response.encoding == "utf-8"34 response = httpcore.Response(200, content=content)35 assert response.text == "おはようございます。"36 assert response.encoding == "EUC-JP"373839def test_response_fallback_to_autodetect():40 """41 Fallback to autodetection if we get an invalid charset in the Content-Type header.42 """43 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}44 content = "おはようございます。".encode("EUC-JP")45 response = httpcore.Response(200, content=content, headers=headers)46 assert response.text == "おはようございます。"47 assert response.encoding == "EUC-JP"484950def test_response_default_encoding():51 """52 Default to utf-8 if all else fails.53 """54 response = httpcore.Response(200, content=b"")55 assert response.text == ""56 assert response.encoding == "utf-8"57
111 unmodified lines
modified:tests/models/test_url.py+16 added
3 unmodified lines
4def test_idna_url():5 url = URL("http://中国.icom.museum:80/")6 assert url == URL("http://xn--fiqs8s.icom.museum:80/")7 assert url.host == "xn--fiqs8s.icom.museum"8910def test_url():11 url = URL("https://example.org:123/path/to/somewhere?abc=123#anchor")12 assert url.scheme == "https"13 assert url.host == "example.org"14 assert url.port == 12315 assert url.authority == "example.org:123"16 assert url.path == "/path/to/somewhere"17 assert url.query == "abc=123"18 assert url.fragment == "anchor"19 assert repr(url) == "URL('https://example.org:123/path/to/somewhere?abc=123#anchor')"2021 new = url.copy_with(scheme="http")22 assert new == URL("http://example.org:123/path/to/somewhere?abc=123#anchor")23 assert new.scheme == "http"