Skip to content

Commit

Permalink
fix asgi spec violation causing crash in lilya
Browse files Browse the repository at this point in the history
Detail:

The asgi spec only specifies that the headers are an iterable. Uvicorn
assumes that the headers are a list. This causes a crash at least in
lilya and can cause hard to debug bugs
  • Loading branch information
devkral committed Feb 5, 2025
1 parent 3695737 commit 886381c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,11 @@ async def send(self, message: ASGISendEvent) -> None:

status = message["status"]
headers = self.default_headers + list(message.get("headers", []))
# we need to make sure that the headers are a list, the spec requires only an iterator
scope_headers = list(self.scope["headers"])
self.scope["headers"] = scope_headers

if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
if CLOSE_HEADER in scope_headers and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]

if self.access_log:
Expand Down
5 changes: 4 additions & 1 deletion uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,11 @@ async def send(self, message: ASGISendEvent) -> None:

status_code = message["status"]
headers = self.default_headers + list(message.get("headers", []))
# we need to make sure that the headers are a list, the spec requires only an iterator
scope_headers = list(self.scope["headers"])
self.scope["headers"] = scope_headers

if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
if CLOSE_HEADER in scope_headers and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]

if self.access_log:
Expand Down

0 comments on commit 886381c

Please sign in to comment.