Skip to content

Commit

Permalink
Add latin-1 fallback decoding for asgi headers
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky-ken committed Aug 28, 2024
1 parent 161acbb commit 118f259
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,17 @@ def get(
# ASGI header keys are in lower case
key = key.lower()
decoded = [
_value.decode("utf8")
_decode_header_item(_value)
for (_key, _value) in headers
if _key.decode("utf8").lower() == key
if _decode_header_item(_key).lower() == key
]
if not decoded:
return None
return decoded

def keys(self, carrier: dict) -> typing.List[str]:
headers = carrier.get("headers") or []
return [_key.decode("utf8") for (_key, _value) in headers]
return [_decode_header_item(_key) for (_key, _value) in headers]


asgi_getter = ASGIGetter()
Expand Down Expand Up @@ -410,7 +410,9 @@ def collect_custom_headers_attributes(
if raw_headers:
for key, value in raw_headers:
# Decode headers before processing.
headers[key.decode()].append(value.decode())
headers[_decode_header_item(key)].append(
_decode_header_item(value)
)

return sanitize.sanitize_header_values(
headers,
Expand Down Expand Up @@ -937,3 +939,10 @@ def _parse_active_request_count_attrs(
_server_active_requests_count_attrs_new,
sem_conv_opt_in_mode,
)


def _decode_header_item(value):
try:
return value.decode("utf-8")
except ValueError:
return value.decode("latin-1")

0 comments on commit 118f259

Please sign in to comment.