Comments (6)
👍🏾+1
from warp.
Is there any concept already how it will look in code?
from warp.
To support CORS we need to add specific headers only if Origin
header is provided.
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 Origin
header?
// 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"))
})
from warp.
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 Origin
header is presented and just Response::builder()
if isn't.
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?
from warp.
Is there any concept already how it will look in code?
What I've had in mind is to make use the sealed Wrap
trait, allowing usage with Filter::with
:
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);
borrowed value does not live long enough
That's because as of 0.1.x, http::response::Builder
is a by-ref builder, so methods like header
returns &mut Self
. Therefore, in your example, you tried to return &mut Builder
, which the compiler is saying won't work, because its a pointer to the builder living on the stack, which will be dropped once the closure ends.
from warp.
Hi @seanmonstar, thanks for the example. Filter::with
looks perfect for this case.
I'm trying to figure out how to implement the cors
filter using sealed Wrap
trait. If I understand it right, I should seal a function that consumes route
and response
objects and returns a modified response
. But it looks like I can't move out the modified response
from route::with(|_route| {...})
closure.
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 with<F, R>(func: F)
from Fn
to FnOnce
. Shall we do that?
I've opened a PR with all the changes I described to view them at once.
from warp.
Related Issues (20)
- Make `Option<F>` a filter when F is a filter
- Feature request: `warp::make_service()` or `warp::service_with_addr()`
- CVE-2023-43669/GHSA-9mcr-873m-xcxp: tungstenite <= 0.20.0 DoS vulnerability HOT 1
- [feature request] [low prio] Non-Metal fallback option HOT 2
- SSE gives up on Streams that return Pending HOT 3
- Server::run should return ! (never type)
- Default OS / self signed certificate without create it HOT 1
- websocket disconnect (code: 1006, reason: "") HOT 2
- Add `rust-version` into `Cargo.toml` HOT 1
- Error `connection closed before message completed` after 60s HOT 2
- Websocket connection closes immediately, but not with firefox HOT 4
- Navigation between panes using vim commands HOT 1
- examples/tls/cert.pem has expired
- Upgrade to hyper v1 HOT 6
- key contains no private key while using ecc key HOT 1
- integrate with monoio
- HELP: filters inside .then never get executed HOT 1
- Chinese input method following error in notebook HOT 2
- Graceful shutdown hangs when I use a Chromium based browser HOT 1
- Sending a JSON post with a string or bytes that contain the words <meta> causes a 403 HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from warp.