Skip to main content

rama_http_headers/common/
if_modified_since.rs

1use rama_core::telemetry::tracing;
2use rama_http_types::HeaderValue;
3
4use crate::util::HttpDate;
5use std::time::SystemTime;
6
7/// `If-Modified-Since` header, defined in
8/// [RFC7232](https://datatracker.ietf.org/doc/html/rfc7232#section-3.3)
9///
10/// The `If-Modified-Since` header field makes a GET or HEAD request
11/// method conditional on the selected representation's modification date
12/// being more recent than the date provided in the field-value.
13/// Transfer of the selected representation's data is avoided if that
14/// data has not changed.
15///
16/// # ABNF
17///
18/// ```text
19/// If-Modified-Since = HTTP-date
20/// ```
21///
22/// # Example values
23/// * `Sat, 29 Oct 1994 19:43:31 GMT`
24///
25/// # Example
26///
27/// ```
28/// use rama_http_headers::IfModifiedSince;
29/// use std::time::Duration;
30/// use rama_utils::time::now_system_time;
31///
32/// let time = now_system_time() - Duration::from_hours(24);
33/// let if_mod = IfModifiedSince::from(time);
34/// ```
35#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct IfModifiedSince(HttpDate);
37
38impl crate::TypedHeader for IfModifiedSince {
39    fn name() -> &'static ::rama_http_types::header::HeaderName {
40        &::rama_http_types::header::IF_MODIFIED_SINCE
41    }
42}
43
44impl crate::HeaderDecode for IfModifiedSince {
45    fn decode<'i, I>(values: &mut I) -> Result<Self, crate::Error>
46    where
47        I: Iterator<Item = &'i ::rama_http_types::header::HeaderValue>,
48    {
49        crate::util::TryFromValues::try_from_values(values).map(IfModifiedSince)
50    }
51}
52
53impl crate::HeaderEncode for IfModifiedSince {
54    fn encode<E: Extend<HeaderValue>>(&self, values: &mut E) {
55        match HeaderValue::try_from(&self.0) {
56            Ok(value) => values.extend(::std::iter::once(value)),
57            Err(err) => {
58                tracing::debug!("failed to encode if-modified-since value as header: {err}");
59            }
60        }
61    }
62}
63
64impl IfModifiedSince {
65    /// Check if the supplied time means the resource has been modified.
66    #[must_use]
67    pub fn is_modified(&self, last_modified: SystemTime) -> bool {
68        self.0 < last_modified.into()
69    }
70}
71
72impl From<SystemTime> for IfModifiedSince {
73    fn from(time: SystemTime) -> Self {
74        Self(time.into())
75    }
76}
77
78impl From<IfModifiedSince> for SystemTime {
79    fn from(date: IfModifiedSince) -> Self {
80        date.0.into()
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87    use std::time::Duration;
88
89    #[test]
90    fn is_modified() {
91        let newer = SystemTime::now();
92        let exact = newer - Duration::from_secs(2);
93        let older = newer - Duration::from_secs(4);
94
95        let if_mod = IfModifiedSince::from(exact);
96        assert!(if_mod.is_modified(newer));
97        assert!(!if_mod.is_modified(exact));
98        assert!(!if_mod.is_modified(older));
99    }
100}