Media types with 'text/' should default to iso-8859-1
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 118d94b · Browse files · 2 files·+32 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−14 removed
- tests:+13 added−0 removed
- other:+19 added−14 removed
Provenance
- Commit
d7cf8bb- Tree
caf85f4- Author
Tom Christie <tom@tomchristie.com>- Committer
Tom Christie <tom@tomchristie.com>- Parent
118d94b
2 files changed+32 added−14 removed
modified:httpcore/models.py+19 added-14 removed
67 unmodified lines68 def authority(self) -> str:69 return self.components.authority or ""70 71 @property72- def path(self) -> str:73- return self.components.path or "/"74-75- @property76- def query(self) -> str:77- return self.components.query or ""78-79- @property80- def fragment(self) -> str:81- return self.components.fragment or ""82-83- @property72 def host(self) -> str:73 return self.components.host or ""74 75 @property3 unmodified lines79 return {"https": 443, "http": 80}[self.scheme]80 return int(port)81 82 @property83+ def path(self) -> str:84+ return self.components.path or "/"85+86+ @property87+ def query(self) -> str:88+ return self.components.query or ""89+90+ @property91 def full_path(self) -> str:92 path = self.path93 if self.query:94 path += "?" + self.query95 return path96 97 @property98+ def fragment(self) -> str:99+ return self.components.fragment or ""100+101+ @property102 def is_ssl(self) -> bool:103 return self.components.scheme == "https"104 105 @property402 unmodified lines508 content_type = self.headers.get("Content-Type")509 if content_type is None:510 return None511 512- parsed = cgi.parse_header(content_type)[-1]513- return parsed.get("charset")512+ # RFC 2616 specifies that 'iso-8859-1' should be used as the default513+ # for 'text/*' media types, if no charset is provided.514+ # See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.1515+ parsed = cgi.parse_header(content_type)516+ media_type, info = parsed[0], parsed[-1]517+ default = "iso-8859-1" if media_type.startswith("text/") else None518+ return info.get("charset", default)519 520 @property521 def apparent_encoding(self) -> typing.Optional[str]:522 """90 unmodified linesmodified:tests/models/test_responses.py+13 added
45 unmodified lines46 assert response.text == "おはようございます。"47 assert response.encoding == "EUC-JP"48 49 50+def test_response():51+ """52+ A media type of 'text/*' with no charset should default to ISO-8859-1.53+ See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.154+ """55+ content = b"Hello, world!"56+ headers = {"Content-Type": "text/plain"}57+ response = httpcore.Response(200, content=content, headers=headers)58+ assert response.status_code == 20059+ assert response.encoding == "iso-8859-1"60+ assert response.text == "Hello, world!"61+62+63 def test_response_default_encoding():64 """65 Default to utf-8 if all else fails.66 """115 unmodified lines