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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
extern crate rocket;
use std::marker::PhantomData;
use rocket::request::Request;
use rocket::response::{Responder, Response, Result};
pub type CacheResponse<R> = CacheResponsePro<'static, R>;
#[derive(Debug)]
pub enum CacheResponsePro<'r, R: Responder<'r>> {
Public {
responder: R,
max_age: u32,
must_revalidate: bool,
},
Private {
responder: R,
max_age: u32,
},
NoCache(R),
NoStore(R),
NoCacheControl(R),
#[doc(hidden)]
_Phantom(PhantomData<&'r R>),
}
impl<'r, R: Responder<'r>> Responder<'r> for CacheResponsePro<'r, R> {
fn respond_to(self, req: &Request) -> Result<'r> {
match self {
CacheResponsePro::Public {
responder,
max_age,
must_revalidate,
} => {
Response::build_from(responder.respond_to(req)?)
.raw_header(
"Cache-Control",
if must_revalidate {
format!("must-revalidate, public, max-age={}", max_age)
} else {
format!("public, max-age={}", max_age)
},
)
.ok()
}
CacheResponsePro::Private {
responder,
max_age,
} => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", format!("private, max-age={}", max_age))
.ok()
}
CacheResponsePro::NoCache(responder) => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", "no-cache")
.ok()
}
CacheResponsePro::NoStore(responder) => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", "no-store")
.ok()
}
CacheResponsePro::NoCacheControl(responder) => {
Response::build_from(responder.respond_to(req)?).ok()
}
_ => unimplemented!(),
}
}
}
impl<'r, R: Responder<'r>> CacheResponsePro<'r, R> {
#[cfg(debug_assertions)]
pub fn public_only_release(
responder: R,
_max_age: u32,
_must_revalidate: bool,
) -> CacheResponsePro<'r, R> {
CacheResponsePro::NoCacheControl(responder)
}
#[cfg(not(debug_assertions))]
pub fn public_only_release(
responder: R,
max_age: u32,
must_revalidate: bool,
) -> CacheResponsePro<'r, R> {
CacheResponsePro::Public {
responder,
max_age,
must_revalidate,
}
}
#[cfg(debug_assertions)]
pub fn private_only_release(responder: R, _max_age: u32) -> CacheResponsePro<'r, R> {
CacheResponsePro::NoCacheControl(responder)
}
#[cfg(not(debug_assertions))]
pub fn private_only_release(responder: R, max_age: u32) -> CacheResponsePro<'r, R> {
CacheResponsePro::Private {
responder,
max_age,
}
}
}