Skip to content

Commit

Permalink
Change default header value encoding to latin-1, close #354
Browse files Browse the repository at this point in the history
  • Loading branch information
perklet committed Jul 22, 2024
1 parent d1e55dd commit a6502c5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions curl_cffi/requests/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ def normalize_header_key(
return bytes_value.lower() if lower else bytes_value


def normalize_header_value(value: Union[str, bytes], encoding: Optional[str] = None) -> bytes:
def normalize_header_value(value: Union[str, bytes, int], encoding: Optional[str] = None) -> bytes:
"""
Coerce str/bytes into a strictly byte-wise HTTP header value.
"""
if isinstance(value, bytes):
return value
return value.encode(encoding or "ascii")
# The default encoding for header value should be latin-1
# See: RFC and https://github.com/python/cpython/blob/bc264eac3ad14dab748e33b3d714c2674872791f/Lib/http/client.py#L1309
if isinstance(value, int):
return str(value).encode()
else:
return value.encode(encoding or "latin-1")


class Headers(MutableMapping[str, str]):
Expand Down

0 comments on commit a6502c5

Please sign in to comment.