I am a Rust beginner and I needed some help with creating path Filters. The stripped down code looks like this:
pub fn requests_filter() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
let authenticate = warp::get()
.and(warp::path("/auth")
.and_then(perform_authentication);
let get_request = warp::get()
.and(
warp::filters::query::raw()
.or(warp::any().map(|| String::default()))
.unify(),
)
.and(get_header)
.and_then(process_get_request);
authenticate.or(get_request)
}
I need to combine the 2 filters so that if I get a auth request(/auth) then it should execute just the first filter and not the second. For all other GET requests it should just execute 2nd path filter. The issue I am having in the above code is the second path filter gets executed when there is some authentication error. I want to it to return and not execute the second path filter. Could you please let me know how to solve this. I looked at routing.rs example but wasnt able to figure this out. Thanks in advance!
The issue I am having in the above code is the second path filter gets executed when there is some authentication error.
Sounds like auth
is returning a Rejection
on failure.
Sadly that's one part of warp which is very confusing1: rejections are not for expressing failure, they're the mechanism through which warp triggers "fallbacks" to the next handler in the chain. So by returning a rejection, you tell warp to check whether the next handler might accept the request. This can ultimately yield a failure anyway (if none of the handlers can do anything), but it can also match an run any number of subsequent handlers depending on your routing.
If you want a /auth
failure to immediately respond with a failure, you should return a Ok(reply)
with whatever failure status and payload you want (e.g. 403 or something), not an Err(failure)
which would signal that this handler is unable to handle the request.
1: hopefully this changes in 0.4 or 0.5 and filters get a clearer bespoke enum for this rather than override Result
.