Fixed Server header not being set on streaming responses.
CI / Formatting (push) Failing after 22s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 12s
CI / Tests (Python 3.13) (push) Successful in 12s
CI / Tests (Python 3.14) (push) Successful in 9s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 5s

This commit is contained in:
2026-04-06 16:18:47 -04:00
parent 100cbc3bef
commit f6c7340fa3
+7 -9
View File
@@ -18,7 +18,7 @@ from __future__ import annotations
import asyncio import asyncio
import logging import logging
from collections.abc import Awaitable, Callable, Coroutine from collections.abc import Callable, Coroutine
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
@@ -35,16 +35,13 @@ if TYPE_CHECKING:
logger = logging.getLogger("owlbot.web") logger = logging.getLogger("owlbot.web")
@web.middleware async def _on_response_prepare(
async def _server_header_middleware( request: web.Request, response: web.StreamResponse
request: web.Request, ) -> None:
handler: Callable[[web.Request], Awaitable[web.StreamResponse]], """Set the Server header on all responses before headers are sent."""
) -> web.StreamResponse:
response = await handler(request)
response.headers["Server"] = f"Owlbot/{__version__}" response.headers["Server"] = f"Owlbot/{__version__}"
if request.path.startswith("/owlbot/static/"): if request.path.startswith("/owlbot/static/"):
response.headers["Cache-Control"] = "max-age=86400" response.headers["Cache-Control"] = "max-age=86400"
return response
# Callback type for webhook dispatch (injected from Owlbot). # Callback type for webhook dispatch (injected from Owlbot).
@@ -80,7 +77,8 @@ class HttpServer:
# Runner is created by start() and cleaned up by stop(). # Runner is created by start() and cleaned up by stop().
self._runner: web.AppRunner | None = None self._runner: web.AppRunner | None = None
self.app = web.Application(middlewares=[_server_header_middleware]) self.app = web.Application()
self.app.on_response_prepare.append(_on_response_prepare)
self.app.on_shutdown.append(self._on_shutdown) self.app.on_shutdown.append(self._on_shutdown)
self.app.router.add_post(self.config.webhook_path, self._handle_webhook) self.app.router.add_post(self.config.webhook_path, self._handle_webhook)