rocket_cache_response/
lib.rs

1/*!
2# Cache Response for Rocket Framework
3
4This crate provides a response struct used for HTTP cache control.
5
6See `examples`.
7*/
8
9use std::marker::PhantomData;
10
11use rocket::{
12    request::Request,
13    response::{Responder, Response, Result},
14};
15
16/// The responder with a `Cache-Control` header.
17pub type CacheResponse<R> = CacheResponsePro<'static, 'static, R>;
18
19/// The responder with a `Cache-Control` header.
20#[derive(Debug)]
21pub enum CacheResponsePro<'r, 'o: 'r, R: Responder<'r, 'o>> {
22    Public {
23        responder:       R,
24        max_age:         u32,
25        must_revalidate: bool,
26    },
27    Private {
28        responder: R,
29        max_age:   u32,
30    },
31    NoCache(R),
32    NoStore(R),
33    NoCacheControl(R),
34    #[doc(hidden)]
35    _Phantom(PhantomData<(&'r R, &'o R)>),
36}
37
38impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for CacheResponsePro<'r, 'o, R> {
39    fn respond_to(self, req: &'r Request<'_>) -> Result<'o> {
40        match self {
41            CacheResponsePro::Public {
42                responder,
43                max_age,
44                must_revalidate,
45            } => Response::build_from(responder.respond_to(req)?)
46                .raw_header(
47                    "Cache-Control",
48                    if must_revalidate {
49                        format!("must-revalidate, public, max-age={}", max_age)
50                    } else {
51                        format!("public, max-age={}", max_age)
52                    },
53                )
54                .ok(),
55            CacheResponsePro::Private {
56                responder,
57                max_age,
58            } => Response::build_from(responder.respond_to(req)?)
59                .raw_header("Cache-Control", format!("private, max-age={}", max_age))
60                .ok(),
61            CacheResponsePro::NoCache(responder) => {
62                Response::build_from(responder.respond_to(req)?)
63                    .raw_header("Cache-Control", "no-cache")
64                    .ok()
65            },
66            CacheResponsePro::NoStore(responder) => {
67                Response::build_from(responder.respond_to(req)?)
68                    .raw_header("Cache-Control", "no-store")
69                    .ok()
70            },
71            CacheResponsePro::NoCacheControl(responder) => {
72                Response::build_from(responder.respond_to(req)?).ok()
73            },
74            _ => unimplemented!(),
75        }
76    }
77}
78
79impl<'r, 'o: 'r, R: Responder<'r, 'o>> CacheResponsePro<'r, 'o, R> {
80    /// Use public cache only when this program is built on the **release** mode.
81    #[cfg(debug_assertions)]
82    pub fn public_only_release(
83        responder: R,
84        _max_age: u32,
85        _must_revalidate: bool,
86    ) -> CacheResponsePro<'r, 'o, R> {
87        CacheResponsePro::NoCacheControl(responder)
88    }
89
90    /// Use public cache only when this program is built on the **release** mode.
91    #[cfg(not(debug_assertions))]
92    pub fn public_only_release(
93        responder: R,
94        max_age: u32,
95        must_revalidate: bool,
96    ) -> CacheResponsePro<'r, 'o, R> {
97        CacheResponsePro::Public {
98            responder,
99            max_age,
100            must_revalidate,
101        }
102    }
103
104    /// Use private cache only when this program is built on the **release** mode.
105    #[cfg(debug_assertions)]
106    pub fn private_only_release(responder: R, _max_age: u32) -> CacheResponsePro<'r, 'o, R> {
107        CacheResponsePro::NoCacheControl(responder)
108    }
109
110    /// Use private cache only when this program is built on the **release** mode.
111    #[cfg(not(debug_assertions))]
112    pub fn private_only_release(responder: R, max_age: u32) -> CacheResponsePro<'r, 'o, R> {
113        CacheResponsePro::Private {
114            responder,
115            max_age,
116        }
117    }
118}