summaryrefslogtreecommitdiff
path: root/src/cache.rs
blob: 680d9dae434ede9b9305e9f605092da700d3ede8 (plain)
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
40
41
42
use std::borrow::Cow;

use rocket::{
    http::Header,
    request::{self, FromRequest},
    response::{self, Responder},
    Request,
};

pub(crate) struct Etagged<'r, T>(pub T, pub Cow<'r, str>);

impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for Etagged<'o, T> {
    fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> {
        let mut resp = self.0.respond_to(r)?;
        resp.set_header(Header::new("etag", self.1));
        Ok(resp)
    }
}

pub(crate) struct Immutable<T>(pub T);

impl<'r, 'o: 'r, T: Responder<'r, 'o>> Responder<'r, 'o> for Immutable<T> {
    fn respond_to(self, r: &'r Request<'_>) -> response::Result<'o> {
        let mut resp = self.0.respond_to(r)?;
        resp.set_header(Header::new("cache-control", "public, max-age=604800, immutable"));
        Ok(resp)
    }
}

pub(crate) struct IfNoneMatch<'r>(pub &'r str);

#[async_trait]
impl<'r> FromRequest<'r> for IfNoneMatch<'r> {
    type Error = ();

    async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
        match req.headers().get_one("if-none-match") {
            Some(h) => request::Outcome::Success(Self(h)),
            None => request::Outcome::Forward(()),
        }
    }
}