seanmonstar / warp

A super-easy, composable, web server framework for warp speeds.
https://seanmonstar.com/post/176530511587/warp
MIT License
9.61k stars 724 forks source link

Support for HTTP extension methods #871

Closed outergod closed 3 years ago

outergod commented 3 years ago

Is your feature request related to a problem? Please describe. I'm trying to implement WebDAV handlers using warp, which requires filters for non-standard HTTP methods, as documented in RFC 4918. While http::Method allows defining custom methods, warp only provides a pre-defined set of filters for the standard methods and keeps method_is in filters::method private, so I couldn't figure out any other way to filter for custom methods.

Describe the solution you'd like Making method_is public would probably be enough already. warp could provide some sugar for Method::from_bytes, but that is not absolutely necessary.

Describe alternatives you've considered I could't figure out any other way to filter for the method in warp.

Additional context N/A

outergod commented 3 years ago

Update, I was now able to write my own filter but it's not particularly pretty vs. what could be achieved if warp supported it directly.

fn method(name: &str) -> impl Filter<Extract = (), Error = Rejection> + Clone {
    let method =
        Method::from_str(name).expect(&format!("Method name {} could not be converted", name));

    warp::method()
        .and_then(move |m: Method| {
            let method = method.clone();
            async move {
                if m == method {
                    Ok(())
                } else {
                    Err(reject::custom(GatewayHandlerError::MethodNotAllowed))
                }
            }
        })
        .untuple_one()
}