src/blackd/middlewares.py
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from typing import TYPE_CHECKING, Any, Awaitable, Callable, Iterable, TypeVar
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from aiohttp.web_request import Request
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from aiohttp.web_response import StreamResponse
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | if TYPE_CHECKING:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | F = TypeVar("F", bound=Callable[..., Any])
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | middleware: Callable[[F], F]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | else:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | try:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | from aiohttp.web_middlewares import middleware
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | except ImportError:
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # @middleware is deprecated and its behaviour is the default since aiohttp 4.0
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | # so if it doesn't exist anymore, define a no-op for forward compatibility.
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | middleware = lambda x: x # noqa: E731
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Handler = Callable[[Request], Awaitable[StreamResponse]]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- | Middleware = Callable[[Request, Handler], Awaitable[StreamResponse]]
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
---- ---- ---- ---- ---- ---- ---- -- -- --- --- --- --- --- --- ------- ------- ------- |
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | def cors(allow_headers: Iterable[str]) -> Middleware:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | @middleware
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | async def impl(request: Request, handler: Handler) -> StreamResponse:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | is_options = request.method == "OPTIONS"
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | is_preflight = is_options and "Access-Control-Request-Method" in request.headers
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | if is_preflight:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp = StreamResponse()
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | else:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp = await handler(request)
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | origin = request.headers.get("Origin")
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | if not origin:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | return resp
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp.headers["Access-Control-Allow-Origin"] = "*"
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp.headers["Access-Control-Expose-Headers"] = "*"
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | if is_options:
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp.headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers)
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | resp.headers["Access-Control-Allow-Methods"] = ", ".join(
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | ("OPTIONS", "POST")
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | )
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | return resp
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 |
0025 0019 0021 0000 0000 0004 0000 -- 01 004 007 004 007 011 011 0038.05 0076.11 0002.00 | return impl