Skip processing request body for HTTP HEAD requests (#147899)

* Skip processing request body for HTTP HEAD requests

* Use aiohttp's must_be_empty_body() to check whether ingress requests should be streamed

* Only call must_be_empty_body() once per request

* Fix incorrect use of walrus operator
This commit is contained in:
Space 2025-07-02 11:45:45 +02:00 committed by Franck Nijhof
parent b8c19f23f3
commit fa1bed1849
No known key found for this signature in database
GPG Key ID: AB33ADACE7101952

View File

@ -11,6 +11,7 @@ from urllib.parse import quote
import aiohttp import aiohttp
from aiohttp import ClientTimeout, ClientWebSocketResponse, hdrs, web from aiohttp import ClientTimeout, ClientWebSocketResponse, hdrs, web
from aiohttp.helpers import must_be_empty_body
from aiohttp.web_exceptions import HTTPBadGateway, HTTPBadRequest from aiohttp.web_exceptions import HTTPBadGateway, HTTPBadRequest
from multidict import CIMultiDict from multidict import CIMultiDict
from yarl import URL from yarl import URL
@ -184,13 +185,16 @@ class HassIOIngress(HomeAssistantView):
content_type = "application/octet-stream" content_type = "application/octet-stream"
# Simple request # Simple request
if result.status in (204, 304) or ( if (empty_body := must_be_empty_body(result.method, result.status)) or (
content_length is not UNDEFINED content_length is not UNDEFINED
and (content_length_int := int(content_length)) and (content_length_int := int(content_length))
<= MAX_SIMPLE_RESPONSE_SIZE <= MAX_SIMPLE_RESPONSE_SIZE
): ):
# Return Response # Return Response
body = await result.read() if empty_body:
body = None
else:
body = await result.read()
simple_response = web.Response( simple_response = web.Response(
headers=headers, headers=headers,
status=result.status, status=result.status,