Skip to content

Commit

Permalink
Apply UTF-8 globally (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarsil authored Jul 1, 2024
1 parent 6ede2be commit 8affd68
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lilya/_internal/_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _user_safe_decode(src: bytes, codec: str) -> str:
try:
return src.decode(codec)
except (UnicodeDecodeError, LookupError):
return src.decode("latin-1")
return src.decode("utf-8")


class MultiPartException(Exception):
Expand Down Expand Up @@ -172,8 +172,8 @@ async def parse(self) -> FormData:
elif message_type == FormMessage.FIELD_DATA:
field_value += message_bytes
elif message_type == FormMessage.FIELD_END:
name = unquote_plus(field_name.decode("latin-1"))
value = unquote_plus(field_value.decode("latin-1"))
name = unquote_plus(field_name.decode("utf-8"))
value = unquote_plus(field_value.decode("utf-8"))
items.append((name, value))

return FormData(items)
Expand Down Expand Up @@ -434,7 +434,7 @@ def _parse_content_type_header(self, params: Any) -> None:
params (Any): Parameters from the Content-Type header.
"""
charset = params.get(b"charset", "utf-8")
self._charset = charset.decode("latin-1") if isinstance(charset, bytes) else charset
self._charset = charset.decode("utf-8") if isinstance(charset, bytes) else charset

def _get_multipart_boundary(self, params: dict[bytes, bytes]) -> bytes:
"""
Expand Down
12 changes: 6 additions & 6 deletions lilya/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ def parse_headers(self, value: Any) -> list[tuple[str, Any]]:

if isinstance(value, dict):
for k, v in value.items():
key = k.decode("latin-1") if isinstance(k, bytes) else k
value = v.decode("latin-1") if isinstance(v, bytes) else v
key = k.decode("utf-8") if isinstance(k, bytes) else k
value = v.decode("utf-8") if isinstance(v, bytes) else v
headers.append((key, value))
if isinstance(value, list):
for k, v in value:
key = k.decode("latin-1") if isinstance(k, bytes) else k
value = v.decode("latin-1") if isinstance(v, bytes) else v
key = k.decode("utf-8") if isinstance(k, bytes) else k
value = v.decode("utf-8") if isinstance(v, bytes) else v
headers.append((key, value))

return headers
Expand Down Expand Up @@ -347,7 +347,7 @@ def build_from_scope(cls, scope: Scope) -> URL:
host_header = None
for key, value in scope["headers"]:
if key == b"host":
host_header = value.decode("latin-1")
host_header = value.decode("utf-8")
break

if host_header is not None:
Expand Down Expand Up @@ -625,7 +625,7 @@ def __init__(
if isinstance(value, str): # type: ignore
super().__init__(parse_qsl(value, keep_blank_values=True)) # type: ignore
elif isinstance(value, bytes): # type: ignore
super().__init__(parse_qsl(value.decode("latin-1"), keep_blank_values=True)) # type: ignore
super().__init__(parse_qsl(value.decode("utf-8"), keep_blank_values=True)) # type: ignore
else:
super().__init__(*args)

Expand Down
2 changes: 1 addition & 1 deletion lilya/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ async def send_push_promise(self, path: str) -> None:
raw_headers: list[tuple[bytes, bytes]] = []
for name in SERVER_PUSH_HEADERS:
for value in self.headers.getlist(name):
raw_headers.append((name.encode("latin-1"), value.encode("latin-1")))
raw_headers.append((name.encode("utf-8"), value.encode("utf-8")))
await self._send({"type": "http.response.push", "path": path, "headers": raw_headers})

def __repr__(self) -> str:
Expand Down
10 changes: 4 additions & 6 deletions lilya/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def make_headers(
headers.setdefault("content-type", content_type)

raw_headers = [
(name.encode("latin-1"), f"{value}".encode(errors="surrogateescape"))
(name.encode("utf-8"), f"{value}".encode(errors="surrogateescape"))
for name, value in headers.items()
]
self.raw_headers = raw_headers
Expand Down Expand Up @@ -173,7 +173,7 @@ def set_cookie(
], "samesite must be either 'strict', 'lax' or 'none'"
cookie[key]["samesite"] = samesite
cookie_val = cookie.output(header="").strip()
self.headers.add("set-cookie", cookie_val.encode("latin-1"))
self.headers.add("set-cookie", cookie_val.encode("utf-8"))

def delete_cookie(
self,
Expand Down Expand Up @@ -212,11 +212,9 @@ def encoded_headers(self) -> list[Any]:

for name, value in list(self.headers.multi_items()):
if not isinstance(value, bytes):
headers.append(
(name.encode("latin-1"), f"{value}".encode(errors="surrogateescape"))
)
headers.append((name.encode("utf-8"), f"{value}".encode(errors="surrogateescape")))
else:
headers.append((name.encode("latin-1"), value))
headers.append((name.encode("utf-8"), value))
return headers

def message(self, prefix: str) -> dict[str, Any]:
Expand Down

0 comments on commit 8affd68

Please sign in to comment.