Add cache-control headers to supervisor entrypoint (#55493)

pull/55502/head
Joakim Sørensen 2021-08-31 14:45:28 +02:00 committed by GitHub
parent afc0a1f376
commit 3e38dc0fd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import aiohttp
from aiohttp import web
from aiohttp.client import ClientTimeout
from aiohttp.hdrs import (
CACHE_CONTROL,
CONTENT_ENCODING,
CONTENT_LENGTH,
CONTENT_TYPE,
@ -51,6 +52,8 @@ NO_AUTH = re.compile(
r"^(?:" r"|app/.*" r"|addons/[^/]+/logo" r"|addons/[^/]+/icon" r")$"
)
NO_STORE = re.compile(r"^(?:" r"|app/entrypoint.js" r")$")
class HassIOView(HomeAssistantView):
"""Hass.io view to handle base part."""
@ -104,7 +107,7 @@ class HassIOView(HomeAssistantView):
# Stream response
response = web.StreamResponse(
status=client.status, headers=_response_header(client)
status=client.status, headers=_response_header(client, path)
)
response.content_type = client.content_type
@ -139,7 +142,7 @@ def _init_header(request: web.Request) -> dict[str, str]:
return headers
def _response_header(response: aiohttp.ClientResponse) -> dict[str, str]:
def _response_header(response: aiohttp.ClientResponse, path: str) -> dict[str, str]:
"""Create response header."""
headers = {}
@ -153,6 +156,9 @@ def _response_header(response: aiohttp.ClientResponse) -> dict[str, str]:
continue
headers[name] = value
if NO_STORE.match(path):
headers[CACHE_CONTROL] = "no-store, max-age=0"
return headers

View File

@ -185,3 +185,21 @@ async def test_stream(hassio_client, aioclient_mock):
aioclient_mock.get("http://127.0.0.1/test")
await hassio_client.get("/api/hassio/test", data="test")
assert isinstance(aioclient_mock.mock_calls[-1][2], StreamReader)
async def test_entrypoint_cache_control(hassio_client, aioclient_mock):
"""Test that we return cache control for requests to the entrypoint only."""
aioclient_mock.get("http://127.0.0.1/app/entrypoint.js")
aioclient_mock.get("http://127.0.0.1/app/entrypoint.fdhkusd8y43r.js")
resp1 = await hassio_client.get("/api/hassio/app/entrypoint.js")
resp2 = await hassio_client.get("/api/hassio/app/entrypoint.fdhkusd8y43r.js")
# Check we got right response
assert resp1.status == 200
assert resp2.status == 200
assert len(aioclient_mock.mock_calls) == 2
assert resp1.headers["Cache-Control"] == "no-store, max-age=0"
assert "Cache-Control" not in resp2.headers