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
//! Mappers that extract information from HTTP requests.

use super::Mapper;
use crate::mappers::sequence::KV;

/// Extract the method from the HTTP request and pass it to the next mapper.
pub fn method<M>(inner: M) -> Method<M> {
    Method(inner)
}
/// The `Method` mapper returned by [method()](fn.method.html)
#[derive(Debug)]
pub struct Method<M>(M);
impl<M, B> Mapper<http::Request<B>> for Method<M>
where
    M: Mapper<str>,
{
    type Out = M::Out;

    fn map(&mut self, input: &http::Request<B>) -> M::Out {
        self.0.map(input.method().as_str())
    }
}

/// Extract the path from the HTTP request and pass it to the next mapper.
pub fn path<M>(inner: M) -> Path<M> {
    Path(inner)
}
/// The `Path` mapper returned by [path()](fn.path.html)
#[derive(Debug)]
pub struct Path<M>(M);
impl<M, B> Mapper<http::Request<B>> for Path<M>
where
    M: Mapper<str>,
{
    type Out = M::Out;

    fn map(&mut self, input: &http::Request<B>) -> M::Out {
        self.0.map(input.uri().path())
    }
}

/// Extract the query from the HTTP request and pass it to the next mapper.
pub fn query<M>(inner: M) -> Query<M> {
    Query(inner)
}
/// The `Query` mapper returned by [query()](fn.query.html)
#[derive(Debug)]
pub struct Query<M>(M);
impl<M, B> Mapper<http::Request<B>> for Query<M>
where
    M: Mapper<str>,
{
    type Out = M::Out;

    fn map(&mut self, input: &http::Request<B>) -> M::Out {
        self.0.map(input.uri().query().unwrap_or(""))
    }
}

/// Extract the headers from the HTTP request and pass the sequence to the next
/// mapper.
pub fn headers<M>(inner: M) -> Headers<M> {
    Headers(inner)
}
/// The `Headers` mapper returned by [headers()](fn.headers.html)
#[derive(Debug)]
pub struct Headers<M>(M);
impl<M, B> Mapper<http::Request<B>> for Headers<M>
where
    M: Mapper<[KV<str, [u8]>]>,
{
    type Out = M::Out;

    fn map(&mut self, input: &http::Request<B>) -> M::Out {
        let headers: Vec<KV<str, [u8]>> = input
            .headers()
            .iter()
            .map(|(k, v)| KV {
                k: k.as_str().to_owned(),
                v: v.as_bytes().to_owned(),
            })
            .collect();
        self.0.map(&headers)
    }
}

/// Extract the body from the HTTP request and pass it to the next mapper.
pub fn body<M>(inner: M) -> Body<M> {
    Body(inner)
}
/// The `Body` mapper returned by [body()](fn.body.html)
#[derive(Debug)]
pub struct Body<M>(M);
impl<M, B> Mapper<http::Request<B>> for Body<M>
where
    B: ToOwned,
    M: Mapper<B::Owned>,
{
    type Out = M::Out;

    fn map(&mut self, input: &http::Request<B>) -> M::Out {
        self.0.map(&input.body().to_owned())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mappers::*;

    #[test]
    fn test_path() {
        let req = http::Request::get("https://example.com/foo")
            .body("")
            .unwrap();
        assert!(path(eq("/foo")).map(&req));

        let req = http::Request::get("https://example.com/foobar")
            .body("")
            .unwrap();
        assert!(path(eq("/foobar")).map(&req))
    }

    #[test]
    fn test_query() {
        let req = http::Request::get("https://example.com/path?foo=bar&baz=bat")
            .body("")
            .unwrap();
        assert!(query(eq("foo=bar&baz=bat")).map(&req));
        let req = http::Request::get("https://example.com/path?search=1")
            .body("")
            .unwrap();
        assert!(query(eq("search=1")).map(&req));
    }

    #[test]
    fn test_method() {
        let req = http::Request::get("https://example.com/foo")
            .body("")
            .unwrap();
        assert!(method(eq("GET")).map(&req));
        let req = http::Request::post("https://example.com/foobar")
            .body("")
            .unwrap();
        assert!(method(eq("POST")).map(&req));
    }

    #[test]
    fn test_headers() {
        let expected = vec![
            KV {
                k: "host".to_owned(),
                v: Vec::from("example.com"),
            },
            KV {
                k: "content-length".to_owned(),
                v: Vec::from("101"),
            },
        ];
        let mut req = http::Request::get("https://example.com/path?key%201=value%201&key2")
            .body("")
            .unwrap();
        req.headers_mut().extend(vec![
            (
                hyper::header::HOST,
                hyper::header::HeaderValue::from_static("example.com"),
            ),
            (
                hyper::header::CONTENT_LENGTH,
                hyper::header::HeaderValue::from_static("101"),
            ),
        ]);

        assert!(headers(eq(expected)).map(&req));
    }

    #[test]
    fn test_body() {
        let req = http::Request::get("https://example.com/foo")
            .body("my request body")
            .unwrap();
        assert!(body(eq("my request body")).map(&req));
    }
}