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
//! Provides the [`SputnikParts`] trait.

use mime::Mime;
use serde::Deserialize;
use std::str::Split;
use std::time::Duration;

use crate::http::{header, request::Parts, HeaderMap};
use crate::response::{delete_cookie, Cookie, SputnikHeaders};

/// Adds convenience methods to [`http::request::Parts`](Parts).
pub trait SputnikParts {
    /// Parses the query string of the request into a given struct.
    fn query<'a, X: Deserialize<'a>>(&'a self) -> Result<X, QueryError>;

    /// Parses the cookies of the request.
    fn cookies(&self) -> CookieIter;

    /// Enforces a specific Content-Type.
    fn enforce_content_type(&self, mime: Mime) -> Result<(), WrongContentTypeError>;

    /// A map of response headers to allow methods of this trait to set response
    /// headers without needing to take a [`Response`](crate::http::response::Response) as an argument.
    ///
    /// You need to take care to append these headers to the response yourself.
    /// This is intended to be done after your routing logic so that your
    /// individual request handlers don't have to worry about it.
    fn response_headers(&mut self) -> &mut HeaderMap;
}

pub struct CookieIter<'a>(Split<'a, char>);

impl<'a> Iterator for CookieIter<'a> {
    type Item = (&'a str, &'a str);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().and_then(|str| {
            let mut iter = str.splitn(2, '=');
            let name = iter.next().expect("first splitn().next() returns Some");
            let value = iter.next();
            match value {
                None => self.next(),
                Some(mut value) => {
                    if value.starts_with('"') && value.ends_with('"') && value.len() >= 2 {
                        value = &value[1..value.len() - 1];
                    }
                    Some((name, value))
                }
            }
        })
    }
}

impl SputnikParts for Parts {
    fn query<'a, T: Deserialize<'a>>(&'a self) -> Result<T, QueryError> {
        serde_urlencoded::from_str::<T>(self.uri.query().unwrap_or("")).map_err(QueryError)
    }

    fn response_headers(&mut self) -> &mut HeaderMap {
        if self.extensions.get::<HeaderMap>().is_none() {
            self.extensions.insert(HeaderMap::new());
        }
        self.extensions.get_mut::<HeaderMap>().unwrap()
    }

    fn cookies(&self) -> CookieIter {
        CookieIter(
            self.headers
                .get(header::COOKIE)
                .and_then(|h| std::str::from_utf8(h.as_bytes()).ok())
                .unwrap_or("")
                .split(';'),
        )
    }

    fn enforce_content_type(&self, mime: Mime) -> Result<(), WrongContentTypeError> {
        if let Some(content_type) = self.headers.get(header::CONTENT_TYPE) {
            if *content_type == mime.to_string() {
                return Ok(());
            }
        }
        Err(WrongContentTypeError {
            expected: mime,
            received: self
                .headers
                .get(header::CONTENT_TYPE)
                .as_ref()
                .and_then(|h| h.to_str().ok().map(|s| s.to_owned())),
        })
    }
}

const FLASH_COOKIE_NAME: &str = "flash";

/// Show the user a message after redirecting them.
pub struct Flash {
    name: String,
    message: String,
}

impl From<Flash> for Cookie {
    fn from(flash: Flash) -> Self {
        Cookie {
            name: FLASH_COOKIE_NAME.into(),
            value: format!("{}:{}", flash.name, flash.message),
            max_age: Some(Duration::from_secs(5 * 60)),
            ..Default::default()
        }
    }
}

impl Flash {
    /// If the request has a flash cookie retrieve it and append a set-cookie
    /// header to delete the cookie to [`SputnikParts::response_headers`].
    pub fn from_request(req: &mut Parts) -> Option<Self> {
        let value = req
            .cookies()
            .find(|(name, _value)| *name == FLASH_COOKIE_NAME)?
            .1
            .to_owned();
        req.response_headers()
            .set_cookie(delete_cookie(FLASH_COOKIE_NAME));
        let mut iter = value.splitn(2, ':');
        if let (Some(name), Some(message)) = (iter.next(), iter.next()) {
            return Some(Flash {
                name: name.to_owned(),
                message: message.to_owned(),
            });
        }
        None
    }

    /// Constructs a new Flash message. The name must not contain a colon (`:`).
    pub fn new(name: String, message: String) -> Self {
        Flash { name, message }
    }

    /// Constructs a new "success" Flash message.
    pub fn success(message: String) -> Self {
        Flash {
            name: "success".to_owned(),
            message,
        }
    }

    /// Constructs a new "warning" Flash message.
    pub fn warning(message: String) -> Self {
        Flash {
            name: "warning".to_owned(),
            message,
        }
    }

    /// Constructs a new "error" Flash message.
    pub fn error(message: String) -> Self {
        Flash {
            name: "error".to_owned(),
            message,
        }
    }

    /// Returns the name of the Flash message.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the message of the Flash message.
    pub fn message(&self) -> &str {
        &self.message
    }
}

#[derive(thiserror::Error, Debug)]
#[error("query deserialize error: {0}")]
pub struct QueryError(pub serde_urlencoded::de::Error);

#[derive(thiserror::Error, Debug)]
#[error("expected Content-Type {expected} but received {}", received.as_ref().unwrap_or(&"nothing".to_owned()))]
pub struct WrongContentTypeError {
    pub expected: Mime,
    pub received: Option<String>,
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;

    use crate::http::{header, Request};

    use super::SputnikParts;

    #[test]
    fn test_enforce_content_type() {
        let (mut parts, _body) = Request::new("").into_parts();
        assert!(parts.enforce_content_type(mime::APPLICATION_JSON).is_err());

        parts
            .headers
            .append(header::CONTENT_TYPE, "application/json".try_into().unwrap());
        assert!(parts.enforce_content_type(mime::APPLICATION_JSON).is_ok());

        parts
            .headers
            .insert(header::CONTENT_TYPE, "text/html".try_into().unwrap());
        assert!(parts.enforce_content_type(mime::APPLICATION_JSON).is_err());
    }
}