Skip to content
Myr

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

2 files+32 added−14 removed

modified:httpcore/models.py+19 added-14 removed
67 unmodified lines
68 def authority(self) -> str:69 return self.components.authority or ""7071 @property72 def path(self) -> str:73 return self.components.path or "/"7475 @property76 def query(self) -> str:77 return self.components.query or ""7879 @property80 def fragment(self) -> str:81 return self.components.fragment or ""8283 @property72 def host(self) -> str:73 return self.components.host or ""7475 @property
3 unmodified lines
79 return {"https": 443, "http": 80}[self.scheme]80 return int(port)8182 @property83 def path(self) -> str:84 return self.components.path or "/"8586 @property87 def query(self) -> str:88 return self.components.query or ""8990 @property91 def full_path(self) -> str:92 path = self.path93 if self.query:94 path += "?" + self.query95 return path9697 @property98 def fragment(self) -> str:99 return self.components.fragment or ""100101 @property102 def is_ssl(self) -> bool:103 return self.components.scheme == "https"104105 @property
402 unmodified lines
508 content_type = self.headers.get("Content-Type")509 if content_type is None:510 return None511512 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)519520 @property521 def apparent_encoding(self) -> typing.Optional[str]:522 """
90 unmodified lines
modified:tests/models/test_responses.py+13 added
45 unmodified lines
46 assert response.text == "おはようございます。"47 assert response.encoding == "EUC-JP"484950def 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!"616263def test_response_default_encoding():64 """65 Default to utf-8 if all else fails.66 """
115 unmodified lines