Use content-type fast path for common case (#4685)

This is a port of https://github.com/home-assistant/core/pull/103477
from core
This commit is contained in:
J. Nick Koston 2023-11-10 07:31:01 -06:00 committed by GitHub
parent d634273b48
commit 78870186d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -225,7 +225,11 @@ class APIIngress(CoreSysAttributes):
skip_auto_headers={hdrs.CONTENT_TYPE},
) as result:
headers = _response_header(result)
# Avoid parsing content_type in simple cases for better performance
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
content_type = (maybe_content_type.partition(";"))[0].strip()
else:
content_type = result.content_type
# Simple request
if (
hdrs.CONTENT_LENGTH in result.headers
@ -236,13 +240,13 @@ class APIIngress(CoreSysAttributes):
return web.Response(
headers=headers,
status=result.status,
content_type=result.content_type,
content_type=content_type,
body=body,
)
# Stream response
response = web.StreamResponse(status=result.status, headers=headers)
response.content_type = result.content_type
response.content_type = content_type
try:
await response.prepare(request)