-
-
Notifications
You must be signed in to change notification settings - Fork 474
Cookieparser attributes #409
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76d7ae3
Add attribute support to cookie parser
dranikpg e15675f
Add cookie formatting tests
dranikpg 9889266
Support zero max-age for instant expiration
dranikpg a01e39c
Use std::tm instead of boost
dranikpg 51bb606
Rename format, refactor dump
dranikpg 337aff4
Add cookie_parser example
dranikpg 078227a
Unify docs page for additional middleware
dranikpg 81ae1c9
Replace optional with empty string
dranikpg 84effa5
Update docs
dranikpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
Crow contains some middlewares that are ready to be used in your application. | ||
<br> | ||
Make sure you understand how to enable and use [middleware](../middleware/). | ||
|
||
## Cookies | ||
Include: `crow/middlewares/cookie_parser.h` <br> | ||
Examples: `examples/middlewars/example_cookies.cpp` | ||
|
||
This middleware allows to read and write cookies by using `CookieParser`. Once enabled, it parses all incoming cookies. | ||
|
||
Cookies can be read and written with the middleware context. All cookie attributes can be changed as well. | ||
|
||
```cpp | ||
auto& ctx = app.get_context<crow::CookieParser>(request); | ||
std::string value = ctx.get_cookie("key"); | ||
ctx.set_cookie("key", "value") | ||
.path("/") | ||
.max_age(120); | ||
``` | ||
|
||
!!! note | ||
|
||
Make sure `CookieParser` is listed before any other middleware that relies on it. | ||
|
||
## CORS | ||
Include: `crow/middlewares/cors.h` <br> | ||
Examples: `examples/middlewars/example_cors.cpp` | ||
|
||
This middleware allows to set CORS policies by using `CORSHandler`. Once enabled, it will apply the default CORS rules globally. | ||
|
||
The CORS rules can be modified by first getting the middleware via `#!cpp auto& cors = app.get_middleware<crow::CORSHandler>();`. The rules can be set per URL prefix using `prefix()`, per blueprint using `blueprint()`, or globally via `global()`. These will return a `CORSRules` object which contains the actual rules for the prefix, blueprint, or application. For more details go [here](../../reference/structcrow_1_1_c_o_r_s_handler.html). | ||
|
||
`CORSRules` can be modified using the methods `origin()`, `methods()`, `headers()`, `max_age()`, `allow_credentials()`, or `ignore()`. For more details on these methods and what default values they take go [here](../../reference/structcrow_1_1_c_o_r_s_rules.html). | ||
|
||
```cpp | ||
auto& cors = app.get_middleware<crow::CORSHandler>(); | ||
cors | ||
.global() | ||
.headers("X-Custom-Header", "Upgrade-Insecure-Requests") | ||
.methods("POST"_method, "GET"_method) | ||
.prefix("/cors") | ||
.origin("example.com"); | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "crow.h" | ||
#include "crow/middlewares/cookie_parser.h" | ||
|
||
int main() | ||
{ | ||
// Include CookieParser middleware | ||
crow::App<crow::CookieParser> app; | ||
|
||
CROW_ROUTE(app, "/read") | ||
([&](const crow::request& req) { | ||
auto& ctx = app.get_context<crow::CookieParser>(req); | ||
// Read cookies with get_cookie | ||
auto value = ctx.get_cookie("key"); | ||
return "value: " + value; | ||
}); | ||
|
||
CROW_ROUTE(app, "/write") | ||
([&](const crow::request& req) { | ||
auto& ctx = app.get_context<crow::CookieParser>(req); | ||
// Store cookies with set_cookie | ||
ctx.set_cookie("key", "word") | ||
// configure additional parameters | ||
.path("/") | ||
.max_age(120) | ||
.httponly(); | ||
return "ok!"; | ||
}); | ||
|
||
app.port(18080).run(); | ||
|
||
return 0; | ||
} |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.