Skip to content

Commit

Permalink
Ensure empty body responses never generate an invalid chunked response (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 15, 2023
1 parent 928aff3 commit cca74da
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion supervisor/api/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@
)


# from https://github.com/aio-libs/aiohttp/blob/8ae650bee4add9f131d49b96a0a150311ea58cd1/aiohttp/helpers.py#L1059C1-L1079C1
def must_be_empty_body(method: str, code: int) -> bool:
"""Check if a request must return an empty body."""
return (
status_code_must_be_empty_body(code)
or method_must_be_empty_body(method)
or (200 <= code < 300 and method.upper() == hdrs.METH_CONNECT)
)


def method_must_be_empty_body(method: str) -> bool:
"""Check if a method must return an empty body."""
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.1
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.2
return method.upper() == hdrs.METH_HEAD


def status_code_must_be_empty_body(code: int) -> bool:
"""Check if a status code must return an empty body."""
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.1
return code in {204, 304} or 100 <= code < 200


class APIIngress(CoreSysAttributes):
"""Ingress view to handle add-on webui routing."""

Expand Down Expand Up @@ -232,7 +255,11 @@ async def _handle_request(
content_type = result.content_type
# Simple request
if (
hdrs.CONTENT_LENGTH in result.headers
# empty body responses should not be streamed,
# otherwise aiohttp < 3.9.0 may generate
# an invalid "0\r\n\r\n" chunk instead of an empty response.
must_be_empty_body(request.method, result.status)
or hdrs.CONTENT_LENGTH in result.headers
and int(result.headers.get(hdrs.CONTENT_LENGTH, 0)) < 4_194_000
):
# Return Response
Expand Down

0 comments on commit cca74da

Please sign in to comment.