1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! HTTP Method filters.
//!
//! The filters deal with the HTTP Method part of a request. Several here will
//! match the request `Method`, and if not matched, will reject the request
//! with a `405 Method Not Allowed`.
//!
//! There is also [`warp::method()`](method), which never rejects
//! a request, and just extracts the method to be used in your filter chains.
use http::Method;

use ::filter::{And, Filter, filter_fn, filter_fn_one, One};
use ::never::Never;
use ::reject::{CombineRejection, Rejection};

/// Wrap a `Filter` in a new one that requires the request method to be `GET`.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route = warp::any().map(warp::reply);
/// let get_only = warp::get(route);
/// ```
pub fn get<F>(filter: F) -> And<
    impl Filter<Extract=(), Error=Rejection> + Copy,
    F,
>
where
    F: Filter + Clone,
    F::Error: CombineRejection<Rejection>,
    <F::Error as CombineRejection<Rejection>>::Rejection: CombineRejection<Rejection>,
{
    method_is(|| &Method::GET)
        .and(filter)
}
/// Wrap a `Filter` in a new one that requires the request method to be `POST`.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route = warp::any().map(warp::reply);
/// let post_only = warp::post(route);
/// ```
pub fn post<F>(filter: F) -> And<
    impl Filter<Extract=(), Error=Rejection> + Copy,
    F,
>
where
    F: Filter + Clone,
    F::Error: CombineRejection<Rejection>,
    <F::Error as CombineRejection<Rejection>>::Rejection: CombineRejection<Rejection>,
{
    method_is(|| &Method::POST)
        .and(filter)
}
/// Wrap a `Filter` in a new one that requires the request method to be `PUT`.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route = warp::any().map(warp::reply);
/// let put_only = warp::put(route);
/// ```
pub fn put<F>(filter: F) -> And<
    impl Filter<Extract=(), Error=Rejection> + Copy,
    F,
>
where
    F: Filter + Clone,
    F::Error: CombineRejection<Rejection>,
    <F::Error as CombineRejection<Rejection>>::Rejection: CombineRejection<Rejection>,
{
    method_is(|| &Method::PUT)
        .and(filter)
}

/// Wrap a `Filter` in a new one that requires the request method to be `DELETE`.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route = warp::any().map(warp::reply);
/// let delete_only = warp::delete(route);
/// ```
pub fn delete<F>(filter: F) -> And<
    impl Filter<Extract=(), Error=Rejection> + Copy,
    F,
>
where
    F: Filter + Clone,
    F::Error: CombineRejection<Rejection>,
    <F::Error as CombineRejection<Rejection>>::Rejection: CombineRejection<Rejection>,
{
    method_is(|| &Method::DELETE)
        .and(filter)
}

/// Extract the `Method` from the request.
///
/// This never rejects a request.
///
/// # Example
///
/// ```
/// use warp::Filter;
///
/// let route = warp::method()
///     .map(|method| {
///         format!("You sent a {} request!", method)
///     });
/// ```
pub fn method() -> impl Filter<Extract=One<Method>, Error=Never> + Copy {
    filter_fn_one(|route| {
        Ok::<_, Never>(route.method().clone())
    })
}

fn method_is<F>(func: F) -> impl Filter<Extract=(), Error=Rejection> + Copy
where
    F: Fn() -> &'static Method + Copy,
{
    filter_fn(move |route| {
        let method = func();
        trace!("method::{:?}?: {:?}", method, route.method());
        if route.method() == method {
            Ok(())
        } else {
            Err(::reject::method_not_allowed())
        }
    })
}