New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
re-implement simple CORS middleware for blackd #2500
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a9bb52
re-implement simple CORS middleware for blackd
zsol d08f95a
remove aiohttp-cors from setup.py
zsol 7ca869a
Merge remote-tracking branch 'origin/main' into HEAD
zsol b5d7c6b
add changelog entry
zsol f076b04
remove @middleware decorator
zsol b839ba3
add back middleware decorator
zsol ef4afe2
Remove aiohttp-cors from Pipfile.lock*
ichard26 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mypy can't figure out the type of
impl
here unfortunately - it complains about returning anAny
value instead ofMiddleware
. @JelleZijlstra any ideas?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What version of aiohttp are we using? On master (https://github.com/aio-libs/aiohttp/blob/master/aiohttp/web_middlewares.py#L34) it looks like the decorator does nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like 3.6 and above:
black/setup.py
Line 89 in d08f95a
We could update this, but I'd prefer doing it in a separate PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For what it's worth the decorator does do something on aiohttp 3.6: https://github.com/aio-libs/aiohttp/blob/3.6/aiohttp/web_middlewares.py#L34. I'm not sure why mypy turns the function into Any here though; the type annotations seem like they should work.