-
-
Notifications
You must be signed in to change notification settings - Fork 474
CORS Middleware #348
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
Merged
Merged
CORS Middleware #348
Conversation
This file contains hidden or 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
--- tests/unittest.cpp (before formatting)
+++ tests/unittest.cpp (after formatting)
@@ -1531,7 +1531,7 @@
auto& cors = app.get_middleware<crow::CORSHandler>();
cors.prefix("/origin")
- .origin("test.test");
+ .origin("test.test");
CROW_ROUTE(app, "/")
([&](const request& req) { |
dranikpg
commented
Feb 22, 2022
void set_header(const std::string& key, const std::string& value, crow::response& res) | ||
{ | ||
if (value.size() == 0) return; | ||
if (!get_header_value(res.headers, key).empty()) return; |
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.
Headers are not overwritten, this means that a handler can change the CORS behaviour if it wants to
The-EDev
requested changes
Mar 4, 2022
The-EDev
approved these changes
Mar 9, 2022
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.
Seems all good, merging now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi! I've implemented middleware for supporting CORS headers like discussed here.
What it looks like
CORSHandler is responsible for setting headers in after_handle. By default, it already enables CORS after being included as a middleware. The global rules and rules for certain prefixes can be changed as follows:
It resembles Flasks resource specific CORS to some extent. But with a builder instead of dicts 🙂
What I've changed
middlewares/cors.h
now contains the CORSHandler with CORSRules.examples/middlewares/cors.h
has a simple example for setting cors rules.I've also added a simple test.
Issues
auto& cors = app.get_middleware<crow::CORSHandler>();
is required for setting specific rules.
I also though of implementing a custom cors handler, which extends the default one
This makes the code less imperative, but it makes it more difficult to customize it's behaviour dynamically (i.e. use variables from the main scope). This would also allow overwriting rules for handlers with local middleware.
Only one origin can be specified (The origin header can return only one). But at least some web frameworks allow specifying multiple, from which the one is automatically selected (if its the same origin)
There is no inheritance from global to prefix rules, i.e. rules are applied either from a prefix or the global handler. Should global rules be applied always? Or should they be independent from prefix rules?