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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! 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};

pub use self::v2::{
    get as get2,
    post as post2,
    put as put2,
    delete as delete2,
    head,
    options,
    patch,
};

#[doc(hidden)]
#[deprecated(note="warp::get2() is meant to replace get()")]
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)
}

#[doc(hidden)]
#[deprecated(note="warp::post2() is meant to replace post()")]
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)
}

#[doc(hidden)]
#[deprecated(note="warp::put2() is meant to replace put()")]
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)
}

#[doc(hidden)]
#[deprecated(note="warp::delete2() is meant to replace delete()")]
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())
    })
}

// NOTE: This takes a static function instead of `&'static Method` directly
// so that the `impl Filter` can be zero-sized. Moving it around should be
// cheaper than holding a single static pointer (which would make it 1 word).
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())
        }
    })
}

pub mod v2 {
    //! HTTP Method Filters
    //!
    //! These filters deal with the HTTP Method part of a request. They match
    //! the request `Method`, and if not matched, will reject the request with a
    //! `405 Method Not Allowed`.
    use http::Method;

    use filter::Filter;
    use reject::Rejection;

    use super::method_is;

    /// Create a `Filter` that requires the request method to be `GET`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let get_only = warp::get2().map(warp::reply);
    /// ```
    pub fn get() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::GET)
    }

    /// Create a `Filter` that requires the request method to be `POST`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let post_only = warp::post2().map(warp::reply);
    /// ```
    pub fn post() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::POST)
    }

    /// Create a `Filter` that requires the request method to be `PUT`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let put_only = warp::put2().map(warp::reply);
    /// ```
    pub fn put() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::PUT)
    }

    /// Create a `Filter` that requires the request method to be `DELETE`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let delete_only = warp::delete2().map(warp::reply);
    /// ```
    pub fn delete() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::DELETE)
    }

    /// Create a `Filter` that requires the request method to be `HEAD`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let head_only = warp::head().map(warp::reply);
    /// ```
    pub fn head() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::HEAD)
    }

    /// Create a `Filter` that requires the request method to be `OPTIONS`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let options_only = warp::options().map(warp::reply);
    /// ```
    pub fn options() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::OPTIONS)
    }

    /// Create a `Filter` that requires the request method to be `PATCH`.
    ///
    /// # Example
    ///
    /// ```
    /// use warp::Filter;
    ///
    /// let patch_only = warp::patch().map(warp::reply);
    /// ```
    pub fn patch() -> impl Filter<Extract=(), Error=Rejection> + Copy {
        method_is(|| &Method::PATCH)
    }
}