trillium_caching_headers/
modified.rs

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