All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypeVar
3 from aiohttp.web_request import Request
4 from aiohttp.web_response import StreamResponse
7 F = TypeVar("F", bound=Callable[..., Any])
8 middleware: Callable[[F], F]
11 from aiohttp.web_middlewares import middleware
13 # @middleware is deprecated and its behaviour is the default since aiohttp 4.0
14 # so if it doesn't exist anymore, define a no-op for forward compatibility.
15 middleware = lambda x: x # noqa: E731
17 Handler = Callable[[Request], Awaitable[StreamResponse]]
18 Middleware = Callable[[Request, Handler], Awaitable[StreamResponse]]
21 def cors(allow_headers: Iterable[str]) -> Middleware:
23 async def impl(request: Request, handler: Handler) -> StreamResponse:
24 is_options = request.method == "OPTIONS"
25 is_preflight = is_options and "Access-Control-Request-Method" in request.headers
27 resp = StreamResponse()
29 resp = await handler(request)
31 origin = request.headers.get("Origin")
35 resp.headers["Access-Control-Allow-Origin"] = "*"
36 resp.headers["Access-Control-Expose-Headers"] = "*"
38 resp.headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
39 resp.headers["Access-Control-Allow-Methods"] = ", ".join(