refactor: implement has_accept_encoding filter
Diff
src/filters.rs | 20 +++++++++++++++++---
src/rejection.rs | 14 ++++++++++++++
2 files changed, 31 insertions(+), 3 deletions(-)
@@ -1,6 +1,20 @@
use warp::{reject, Filter, Rejection};
use crate::rejection::InvalidHeader;
pub fn has_accept_encoding(
val: &'static str,
) -> impl warp::Filter<Extract = (), Error = warp::Rejection> + Copy {
warp::header::contains("accept-encoding", val)
occurrence: &'static str,
) -> impl Filter<Extract = (), Error = Rejection> + Copy {
warp::header::<String>("accept-encoding")
.and_then(move |s: String| async move {
if s.contains(occurrence) {
Ok(())
} else {
Err(reject::custom(InvalidHeader {
name: "accept-encoding",
}))
}
})
.untuple_one()
}
@@ -44,3 +44,17 @@ pub async fn handle_rejection(err: Rejection) -> Result<impl Reply, Infallible>
Ok(warp::reply::with_status(warp::reply::html(content), code))
}
#[derive(Debug)]
pub struct InvalidHeader {
pub name: &'static str,
}
impl ::std::fmt::Display for InvalidHeader {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "Invalid request header {:?}", self.name)
}
}
impl warp::reject::Reject for InvalidHeader {}