Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-implement simple CORS middleware for blackd (#2500)
* re-implement simple CORS middleware for blackd * remove aiohttp-cors from setup.py * Remove aiohttp-cors from Pipfile.lock Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
91 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# Change Log | ||
|
||
## Unreleased | ||
|
||
### _Blackd_ | ||
|
||
- Remove dependency on aiohttp-cors (#2500) | ||
|
||
## 21.9b0 | ||
|
||
### Packaging | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Iterable, Awaitable, Callable | ||
from aiohttp.web_response import StreamResponse | ||
from aiohttp.web_request import Request | ||
from aiohttp.web_middlewares import middleware | ||
|
||
Handler = Callable[[Request], Awaitable[StreamResponse]] | ||
Middleware = Callable[[Request, Handler], Awaitable[StreamResponse]] | ||
|
||
|
||
def cors(allow_headers: Iterable[str]) -> Middleware: | ||
@middleware | ||
async def impl(request: Request, handler: Handler) -> StreamResponse: | ||
is_options = request.method == "OPTIONS" | ||
is_preflight = is_options and "Access-Control-Request-Method" in request.headers | ||
if is_preflight: | ||
resp = StreamResponse() | ||
else: | ||
resp = await handler(request) | ||
|
||
origin = request.headers.get("Origin") | ||
if not origin: | ||
return resp | ||
|
||
resp.headers["Access-Control-Allow-Origin"] = "*" | ||
resp.headers["Access-Control-Expose-Headers"] = "*" | ||
if is_options: | ||
resp.headers["Access-Control-Allow-Headers"] = ", ".join(allow_headers) | ||
resp.headers["Access-Control-Allow-Methods"] = ", ".join( | ||
("OPTIONS", "POST") | ||
) | ||
|
||
return resp | ||
|
||
return impl # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters