pub fn exact(
name: &'static str,
value: &'static str
) -> impl Filter<Extract = (), Error = Rejection> + Copy
Expand description
Create a Filter
that requires a header to match the value exactly.
This Filter
will look for a header with supplied name and the exact
value, otherwise rejects the request.
Example
// Require `dnt: 1` header to be set.
let must_dnt = warp::header::exact("dnt", "1");
Examples found in repository?
examples/headers.rs (line 20)
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
async fn main() {
pretty_env_logger::init();
// For this example, we assume no DNS was used,
// so the Host header should be an address.
let host = warp::header::<SocketAddr>("host");
// Match when we get `accept: */*` exactly.
let accept_stars = warp::header::exact("accept", "*/*");
let routes = host
.and(accept_stars)
.map(|addr| format!("accepting stars on {}", addr));
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
More examples
examples/todos.rs (line 86)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
pub fn todos_delete(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
// We'll make one of our endpoints admin-only to show how authentication filters are used
let admin_only = warp::header::exact("authorization", "Bearer admin");
warp::path!("todos" / u64)
// It is important to put the auth check _after_ the path filters.
// If we put the auth check before, the request `PUT /todos/invalid-string`
// would try this filter and reject because the authorization header doesn't match,
// rather because the param is wrong for that other path.
.and(admin_only)
.and(warp::delete())
.and(with_db(db))
.and_then(handlers::delete_todo)
}
src/filters/ws.rs (line 57)
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pub fn ws() -> impl Filter<Extract = One<Ws>, Error = Rejection> + Copy {
let connection_has_upgrade = header::header2()
.and_then(|conn: ::headers::Connection| {
if conn.contains("upgrade") {
future::ok(())
} else {
future::err(crate::reject::known(MissingConnectionUpgrade))
}
})
.untuple_one();
crate::get()
.and(connection_has_upgrade)
.and(header::exact_ignore_case("upgrade", "websocket"))
.and(header::exact("sec-websocket-version", "13"))
//.and(header::exact2(Upgrade::websocket()))
//.and(header::exact2(SecWebsocketVersion::V13))
.and(header::header2::<SecWebsocketKey>())
.and(on_upgrade())
.map(
move |key: SecWebsocketKey, on_upgrade: Option<OnUpgrade>| Ws {
config: None,
key,
on_upgrade,
},
)
}