tower_async_http/services/fs/serve_dir/
headers.rs

1use http::header::HeaderValue;
2use httpdate::HttpDate;
3use std::time::SystemTime;
4
5pub(super) struct LastModified(pub(super) HttpDate);
6
7impl From<SystemTime> for LastModified {
8    fn from(time: SystemTime) -> Self {
9        LastModified(time.into())
10    }
11}
12
13pub(super) struct IfModifiedSince(HttpDate);
14
15impl IfModifiedSince {
16    /// Check if the supplied time means the resource has been modified.
17    pub(super) fn is_modified(&self, last_modified: &LastModified) -> bool {
18        self.0 < last_modified.0
19    }
20
21    /// convert a header value into a IfModifiedSince, invalid values are silently ignored
22    pub(super) fn from_header_value(value: &HeaderValue) -> Option<IfModifiedSince> {
23        std::str::from_utf8(value.as_bytes())
24            .ok()
25            .and_then(|value| httpdate::parse_http_date(value).ok())
26            .map(|time| IfModifiedSince(time.into()))
27    }
28}
29
30pub(super) struct IfUnmodifiedSince(HttpDate);
31
32impl IfUnmodifiedSince {
33    /// Check if the supplied time passes the precondition.
34    pub(super) fn precondition_passes(&self, last_modified: &LastModified) -> bool {
35        self.0 >= last_modified.0
36    }
37
38    /// Convert a header value into a IfModifiedSince, invalid values are silently ignored
39    pub(super) fn from_header_value(value: &HeaderValue) -> Option<IfUnmodifiedSince> {
40        std::str::from_utf8(value.as_bytes())
41            .ok()
42            .and_then(|value| httpdate::parse_http_date(value).ok())
43            .map(|time| IfUnmodifiedSince(time.into()))
44    }
45}