http_types/conditional/
last_modified.rs

1use crate::headers::{HeaderName, HeaderValue, Headers, ToHeaderValues, LAST_MODIFIED};
2use crate::utils::{fmt_http_date, parse_http_date};
3
4use std::fmt::Debug;
5use std::option;
6use std::time::SystemTime;
7
8/// The last modification date of a resource.
9///
10/// # Specifications
11///
12/// - [RFC 7232, section 2.2: Last-Modified](https://tools.ietf.org/html/rfc7232#section-2.2)
13///
14/// # Examples
15///
16/// ```
17/// # fn main() -> http_types::Result<()> {
18/// #
19/// use http_types::Response;
20/// use http_types::conditional::LastModified;
21/// use std::time::{SystemTime, Duration};
22///
23/// let time = SystemTime::now() + Duration::from_secs(5 * 60);
24/// let last_modified = LastModified::new(time);
25///
26/// let mut res = Response::new(200);
27/// last_modified.apply(&mut res);
28///
29/// let last_modified = LastModified::from_headers(res)?.unwrap();
30///
31/// // HTTP dates only have second-precision.
32/// let elapsed = time.duration_since(last_modified.modified())?;
33/// assert_eq!(elapsed.as_secs(), 0);
34/// #
35/// # Ok(()) }
36/// ```
37#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
38pub struct LastModified {
39    instant: SystemTime,
40}
41
42impl LastModified {
43    /// Create a new instance of `LastModified`.
44    pub fn new(instant: SystemTime) -> Self {
45        Self { instant }
46    }
47
48    /// Returns the last modification time listed.
49    pub fn modified(&self) -> SystemTime {
50        self.instant
51    }
52
53    /// Create an instance of `LastModified` from a `Headers` instance.
54    pub fn from_headers(headers: impl AsRef<Headers>) -> crate::Result<Option<Self>> {
55        let headers = match headers.as_ref().get(LAST_MODIFIED) {
56            Some(headers) => headers,
57            None => return Ok(None),
58        };
59
60        // If we successfully parsed the header then there's always at least one
61        // entry. We want the last entry.
62        let header = headers.iter().last().unwrap();
63
64        let instant = parse_http_date(header.as_str())?;
65        Ok(Some(Self { instant }))
66    }
67
68    /// Insert a `HeaderName` + `HeaderValue` pair into a `Headers` instance.
69    pub fn apply(&self, mut headers: impl AsMut<Headers>) {
70        headers.as_mut().insert(LAST_MODIFIED, self.value());
71    }
72
73    /// Get the `HeaderName`.
74    pub fn name(&self) -> HeaderName {
75        LAST_MODIFIED
76    }
77
78    /// Get the `HeaderValue`.
79    pub fn value(&self) -> HeaderValue {
80        let output = fmt_http_date(self.instant);
81
82        // SAFETY: the internal string is validated to be ASCII.
83        unsafe { HeaderValue::from_bytes_unchecked(output.into()) }
84    }
85}
86
87impl ToHeaderValues for LastModified {
88    type Iter = option::IntoIter<HeaderValue>;
89    fn to_header_values(&self) -> crate::Result<Self::Iter> {
90        // A HeaderValue will always convert into itself.
91        Ok(self.value().to_header_values().unwrap())
92    }
93}
94
95#[cfg(test)]
96mod test {
97    use super::*;
98    use crate::headers::Headers;
99    use std::time::Duration;
100
101    #[test]
102    fn smoke() -> crate::Result<()> {
103        let time = SystemTime::now() + Duration::from_secs(5 * 60);
104        let last_modified = LastModified::new(time);
105
106        let mut headers = Headers::new();
107        last_modified.apply(&mut headers);
108
109        let last_modified = LastModified::from_headers(headers)?.unwrap();
110
111        // HTTP dates only have second-precision
112        let elapsed = time.duration_since(last_modified.modified())?;
113        assert_eq!(elapsed.as_secs(), 0);
114        Ok(())
115    }
116
117    #[test]
118    fn bad_request_on_parse_error() {
119        let mut headers = Headers::new();
120        headers.insert(LAST_MODIFIED, "<nori ate the tag. yum.>");
121        let err = LastModified::from_headers(headers).unwrap_err();
122        assert_eq!(err.status(), 400);
123    }
124}