Skip to content

Commit 2c3e062

Browse files
committed
Avoid adding Content-Type to non-body responses
The current code sets the content-type header for all responses to the result's content_type property if upstream does not set a content_type. The default value for content_type is "application/octet-stream". For responses that do not have a body (like 204 No Content or 304 Not Modified), setting a content-type header is unnecessary and potentially misleading. Follow HTTP standards by only adding the content-type header to responses that actually contain a body.
1 parent b903e11 commit 2c3e062

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

supervisor/api/ingress.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,24 @@ async def _handle_request(
253253
skip_auto_headers={hdrs.CONTENT_TYPE},
254254
) as result:
255255
headers = _response_header(result)
256+
257+
# Empty body responses (304, 204, HEAD, etc.) should not be streamed,
258+
# otherwise aiohttp < 3.9.0 may generate an invalid "0\r\n\r\n" chunk
259+
# This also avoids setting content_type for empty responses.
260+
if must_be_empty_body(request.method, result.status):
261+
return web.Response(
262+
headers=headers,
263+
status=result.status,
264+
)
265+
256266
# Avoid parsing content_type in simple cases for better performance
257267
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
258268
content_type = (maybe_content_type.partition(";"))[0].strip()
259269
else:
260270
content_type = result.content_type
261271
# Simple request
262272
if (
263-
# empty body responses should not be streamed,
264-
# otherwise aiohttp < 3.9.0 may generate
265-
# an invalid "0\r\n\r\n" chunk instead of an empty response.
266-
must_be_empty_body(request.method, result.status)
267-
or hdrs.CONTENT_LENGTH in result.headers
273+
hdrs.CONTENT_LENGTH in result.headers
268274
and int(result.headers.get(hdrs.CONTENT_LENGTH, 0)) < 4_194_000
269275
):
270276
# Return Response

0 commit comments

Comments
 (0)