Skip to main content

trillium_caching_headers/
modified.rs

1use crate::CachingHeadersExt;
2use trillium::{Conn, Handler, Status};
3
4/// # A handler for the `Last-Modified` and `If-Modified-Since` header interaction.
5///
6/// This handler does not set a `Last-Modified` header on its own, but
7/// relies on other handlers doing so.
8#[derive(Debug, Clone, Copy, Default)]
9pub struct Modified {
10    _private: (),
11}
12
13impl Modified {
14    /// constructs a new Modified handler
15    pub fn new() -> Self {
16        Self { _private: () }
17    }
18}
19
20impl Handler for Modified {
21    async fn before_send(&self, conn: Conn) -> Conn {
22        match (conn.if_modified_since(), conn.last_modified()) {
23            (Some(if_modified_since), Some(last_modified))
24                if last_modified <= if_modified_since =>
25            {
26                conn.with_status(Status::NotModified)
27            }
28
29            _ => conn,
30        }
31    }
32
33    async fn run(&self, conn: Conn) -> Conn {
34        conn
35    }
36}