Skip to content

Commit

Permalink
get_encoding_from_headers fails if charset name not specified psf#6646
Browse files Browse the repository at this point in the history
  • Loading branch information
LAPTOP-8CGF3UCA\Alain Khalil committed Mar 8, 2024
1 parent eea3bbf commit e2cb51d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,22 @@ def get_encoding_from_headers(headers):
content_type, params = _parse_content_type_header(content_type)

if "charset" in params:
return params["charset"].strip("'\"")
charset = params["charset"]
if charset is True: # Check if charset is a boolean value
return "ISO-8859-1"
elif charset is False: # Check if charset is explicitly False
return None
else:
return charset.strip("'\"")

if "text" in content_type:
return "ISO-8859-1"

if "application/json" in content_type:
# Assume UTF-8 based on RFC 4627: https://www.ietf.org/rfc/rfc4627.txt since the charset was unset
return "utf-8"


return None

def stream_decode_response_unicode(iterator, r):
"""Stream decodes an iterator."""
Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ def test__parse_content_type_header(value, expected):
"utf-8",
),
(CaseInsensitiveDict({"content-type": "text/plain"}), "ISO-8859-1"),
(CaseInsensitiveDict({"content-type": "text/html; charset"}), "ISO-8859-1"),
(CaseInsensitiveDict({"content-type": "application/json; charset"}), "ISO-8859-1"),
),
)
def test_get_encoding_from_headers(value, expected):
Expand Down

0 comments on commit e2cb51d

Please sign in to comment.