-
-
Notifications
You must be signed in to change notification settings - Fork 719
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
Add CORS filter #2
Comments
👍🏾+1 |
Is there any concept already how it will look in code? |
To support CORS we need to add specific headers only if For preflight requests it's easy to implement using the filter: warp::options().and(warp::header("origin")).map(|origin| {
Ok(Response::builder()
.status(StatusCode::OK)
.header("access-control-allow-methods", "HEAD, GET")
.header("access-control-allow-headers", "authorization")
.header("access-control-allow-credentials", "true")
.header("access-control-max-age", "300")
.header("access-control-allow-origin", origin)
.header("vary", "origin")
.body(""))
}); What would be the right way to implement handlers for regular requests? Some kind of response extension to check on // If "Origin" header is presented
warp::get2().map(|| {
Ok(Response::builder()
.status(StatusCode::OK)
.header("access-control-allow-origin", origin)
.header("vary", "origin")
.body("foobar"))
})
// If it isn't
warp::get2().map(|| {
Ok(Response::builder()
.status(StatusCode::OK)
.body("foobar"))
}) |
Probably a handler for a regular request might also be implemented as a filter that initialize response with Response::builder()
.header("access-control-allow-origin", origin)
.header("vary", "origin") if let cors = warp::any()
.and(warp::header("origin").map(|origin: String| {
Response::builder()
.header("access-control-allow-origin", origin)
.header("vary", "origin")
}))
.or(warp::any().map(|| Response::builder()))
.unify();
warp::get2()
.and(cors)
.map(|response: &mut Builder| {
Ok(response
.status(StatusCode::OK)
.body("foobar"))
})
error[E0597]: borrowed value does not live long enough
--> src/main.rs:68:13
|
68 | Response::builder()
| ^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
71 | }))
| - temporary value dropped here while still borrowed
|
= note: values in a scope are dropped in the opposite order they are created Is there any way I can accomplish that? |
What I've had in mind is to make use the sealed let cors = warp::cors()
.allow_origin("my.foo.local")
.allow_max_age(60 * 10)
.allow_methods(&["GET", "POST", "DELETE"]);
// chained on any route:
let route = warp::path("api")
.map(generate_response)
.with(&cors);
That's because as of 0.1.x, |
Hi @seanmonstar, thanks for the example. I'm trying to figure out how to implement the rror[E0507]: cannot move out of captured outer variable in an `Fn` closure
--> /Users/manifest/projects/github/warp/src/filters/cors.rs:22:13
|
18 | let func = |resp: Response| {
| ---- captured outer variable
...
22 | resp
| ^^^^ cannot move out of captured outer variable in an `Fn` closure
error: aborting due to previous error
For more information about this error, try `rustc --explain E0507`. We can fix it by changing function type of I've opened a PR with all the changes I described to view them at once. |
The CORS wrapping filter is similar to `log`, in that it can "wrap" other filters, and provides support for the CORS protocol. Closes #2
The CORS wrapping filter is similar to `log`, in that it can "wrap" other filters, and provides support for the CORS protocol. Closes #2
The CORS wrapping filter is similar to `log`, in that it can "wrap" other filters, and provides support for the CORS protocol. Closes #2
No description provided.
The text was updated successfully, but these errors were encountered: