Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode headers as UTF-8 also in ASGI #2606

Merged
merged 17 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion sanic/http/http1.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async def http1_request_header(self): # no cov
# Parse header content
try:
head = buf[:pos]
raw_headers = head.decode(errors="surrogateescape")
raw_headers = head.decode("latin-1", errors="surrogateescape")
reqline, *split_headers = raw_headers.split("\r\n")
method, self.url, protocol = reqline.split(" ")

Expand Down
7 changes: 6 additions & 1 deletion sanic/http/http3.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ def get_receiver_by_stream_id(self, stream_id: int) -> Receiver:
return self.receivers[stream_id]

def _make_request(self, event: HeadersReceived) -> Request:
headers = Header(((k.decode(), v.decode()) for k, v in event.headers))
headers = Header(
(
(k.decode("latin-1"), v.decode("latin-1"))
for k, v in event.headers
)
)
method = headers[":method"]
path = headers[":path"]
scheme = headers.pop(":scheme", "")
Expand Down
10 changes: 5 additions & 5 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ def test_get_app_does_not_exist():
with pytest.raises(
SanicException,
match="Sanic app name 'does-not-exist' not found.\n"
"App instantiation must occur outside "
"if __name__ == '__main__' "
"block or by using an AppLoader.\nSee "
"https://sanic.dev/en/guide/deployment/app-loader.html"
" for more details."
"App instantiation must occur outside "
"if __name__ == '__main__' "
"block or by using an AppLoader.\nSee "
"https://sanic.dev/en/guide/deployment/app-loader.html"
" for more details.",
):
Sanic.get_app("does-not-exist")

Expand Down