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
//! Defines the `ContentTypeHeaderRouteMatcher`.

use hyper::header::{HeaderMap, CONTENT_TYPE};
use hyper::StatusCode;
use log::trace;
use mime::Mime;

use super::{LookupTable, LookupTableFromTypes};
use crate::router::route::RouteMatcher;
use crate::router::RouteNonMatch;
use crate::state::{request_id, FromState, State};

/// A `RouteMatcher` that succeeds when the `Request` has been made with a `Content-Type` header
/// that includes a supported media type. The matcher will fail if the Content-Type
/// header is missing, unless you call `allow_no_type` on it.
///
/// # Examples
///
/// ```rust
/// # fn main() {
/// #   use hyper::header::{HeaderMap, CONTENT_TYPE};
/// #   use gotham::state::State;
/// #   use gotham::router::route::matcher::{ContentTypeHeaderRouteMatcher, RouteMatcher};
/// #
/// #   State::with_new(|state| {
/// #
/// let supported_media_types = vec![mime::APPLICATION_JSON];
/// let matcher = ContentTypeHeaderRouteMatcher::new(supported_media_types);
///
/// // No content type header
/// state.put(HeaderMap::new());
/// assert!(matcher.is_match(&state).is_err());
///
/// // Content type header of `application/json`
/// let mut headers = HeaderMap::new();
/// headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());
/// state.put(headers);
/// assert!(matcher.is_match(&state).is_ok());
///
/// // Not a valid Content-Type header
/// let mut headers = HeaderMap::new();
/// headers.insert(CONTENT_TYPE, "text/plain".parse().unwrap());
/// state.put(headers);
/// assert!(matcher.is_match(&state).is_err());
///
/// // At least one supported content type header
/// let mut headers = HeaderMap::new();
/// headers.insert(CONTENT_TYPE, "text/plain".parse().unwrap());
/// headers.insert(CONTENT_TYPE, "application/json".parse().unwrap());
/// state.put(headers);
/// assert!(matcher.is_match(&state).is_ok());
/// #
/// #   });
/// # }
/// ```
#[derive(Clone)]
pub struct ContentTypeHeaderRouteMatcher {
    supported_media_types: Vec<Mime>,
    lookup_table: LookupTable,
    allow_no_type: bool,
}

impl ContentTypeHeaderRouteMatcher {
    /// Creates a new `ContentTypeHeaderRouteMatcher` that does not allow requests
    /// that don't include a content-type header.
    pub fn new(supported_media_types: Vec<Mime>) -> Self {
        let lookup_table = LookupTable::from_types(supported_media_types.iter(), false);
        Self {
            supported_media_types,
            lookup_table,
            allow_no_type: false,
        }
    }

    /// Modify this matcher to allow requests that don't include a content-type header.
    pub fn allow_no_type(mut self) -> Self {
        self.allow_no_type = true;
        self
    }
}

#[inline]
fn err(state: &State) -> RouteNonMatch {
    trace!(
        "[{}] did not specify a Content-Type with a media type supported by this Route",
        request_id(state)
    );

    RouteNonMatch::new(StatusCode::UNSUPPORTED_MEDIA_TYPE)
}

impl RouteMatcher for ContentTypeHeaderRouteMatcher {
    /// Determines if the `Request` was made using a `Content-Type` header that includes a
    /// supported media type.
    fn is_match(&self, state: &State) -> Result<(), RouteNonMatch> {
        HeaderMap::borrow_from(state)
            .get(CONTENT_TYPE)
            .map(|ty| {
                // parse mime type from the content type header
                let mime: Mime = ty
                    .to_str()
                    .map_err(|_| err(state))?
                    .parse()
                    .map_err(|_| err(state))?;

                // get mime type candidates from the lookup table
                let essence = mime.essence_str();
                let candidates = self.lookup_table.get(essence).ok_or_else(|| err(state))?;
                for i in candidates {
                    let candidate = &self.supported_media_types[*i];

                    // check that the candidates have the same suffix - this is not included in the
                    // essence string
                    if candidate.suffix() != mime.suffix() {
                        continue;
                    }

                    // check that this candidate has at least the parameters that the content type
                    // has and that their values are equal
                    if candidate
                        .params()
                        .any(|(key, value)| mime.get_param(key) != Some(value))
                    {
                        continue;
                    }

                    // this candidate matches
                    return Ok(());
                }

                // no candidates found
                Err(err(state))
            })
            .unwrap_or_else(|| {
                // no type present
                if self.allow_no_type {
                    Ok(())
                } else {
                    Err(err(state))
                }
            })
    }
}

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

    fn with_state<F>(content_type: Option<&str>, block: F)
    where
        F: FnOnce(&mut State),
    {
        State::with_new(|state| {
            let mut headers = HeaderMap::new();
            if let Some(ty) = content_type {
                headers.insert(CONTENT_TYPE, ty.parse().unwrap());
            }
            state.put(headers);
            block(state);
        });
    }

    #[test]
    fn empty_type_list() {
        let matcher = ContentTypeHeaderRouteMatcher::new(Vec::new());
        with_state(None, |state| assert!(matcher.is_match(state).is_err()));
        with_state(Some("text/plain"), |state| {
            assert!(matcher.is_match(state).is_err())
        });

        let matcher = matcher.allow_no_type();
        with_state(None, |state| assert!(matcher.is_match(state).is_ok()));
    }

    #[test]
    fn simple_type() {
        let matcher = ContentTypeHeaderRouteMatcher::new(vec![mime::TEXT_PLAIN]);
        with_state(None, |state| assert!(matcher.is_match(state).is_err()));
        with_state(Some("text/plain"), |state| {
            assert!(matcher.is_match(state).is_ok())
        });
        with_state(Some("text/plain; charset=utf-8"), |state| {
            assert!(matcher.is_match(state).is_ok())
        });
    }

    #[test]
    fn complex_type() {
        let matcher = ContentTypeHeaderRouteMatcher::new(vec!["image/svg+xml; charset=utf-8"
            .parse()
            .unwrap()]);
        with_state(Some("image/svg"), |state| {
            assert!(matcher.is_match(state).is_err())
        });
        with_state(Some("image/svg+xml"), |state| {
            assert!(matcher.is_match(state).is_err())
        });
        with_state(Some("image/svg+xml; charset=utf-8"), |state| {
            assert!(matcher.is_match(state).is_ok())
        });
        with_state(Some("image/svg+xml; charset=utf-8; eol=lf"), |state| {
            assert!(matcher.is_match(state).is_ok())
        });
        with_state(Some("image/svg+xml; charset=us-ascii"), |state| {
            assert!(matcher.is_match(state).is_err())
        });
        with_state(Some("image/svg+json; charset=utf-8"), |state| {
            assert!(matcher.is_match(state).is_err())
        });
    }

    #[test]
    fn type_mismatch() {
        let matcher = ContentTypeHeaderRouteMatcher::new(vec![mime::TEXT_HTML]);
        with_state(Some("text/plain"), |state| {
            assert!(matcher.is_match(state).is_err())
        });
    }
}