1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use crate::CachingHeadersExt;
use trillium::{async_trait, Conn, Handler, Status};

/**
# A handler for the `Last-Modified` and `If-Modified-Since` header interaction.

This handler does not set a `Last-Modified` header on its own, but
relies on other handlers doing so.
*/
#[derive(Debug, Clone, Copy, Default)]
pub struct Modified {
    _private: (),
}

impl Modified {
    /// constructs a new Modified handler
    pub fn new() -> Self {
        Self { _private: () }
    }
}

#[async_trait]
impl Handler for Modified {
    async fn before_send(&self, conn: Conn) -> Conn {
        match (conn.if_modified_since(), conn.last_modified()) {
            (Some(if_modified_since), Some(last_modified))
                if last_modified <= if_modified_since =>
            {
                conn.with_status(Status::NotModified)
            }

            _ => conn,
        }
    }

    async fn run(&self, conn: Conn) -> Conn {
        conn
    }
}