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
extern crate rocket;
use rocket::response::{Response, Responder, Result};
use rocket::request::Request;
#[derive(Debug)]
pub enum CacheResponse<R: Responder<'static>> {
Public {
responder: R,
max_age: u32,
must_revalidate: bool,
},
Private {
responder: R,
max_age: u32,
},
NoCache(R),
NoStore(R),
NoCacheControl(R),
}
impl<R: Responder<'static>> Responder<'static> for CacheResponse<R> {
fn respond_to(self, req: &Request) -> Result<'static> {
return match self {
CacheResponse::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()
}
CacheResponse::Private { responder, max_age } => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", format!("private, max-age={}", max_age))
.ok()
}
CacheResponse::NoCache(responder) => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", "no-cache")
.ok()
}
CacheResponse::NoStore(responder) => {
Response::build_from(responder.respond_to(req)?)
.raw_header("Cache-Control", "no-store")
.ok()
}
CacheResponse::NoCacheControl(responder) => {
Response::build_from(responder.respond_to(req)?)
.ok()
}
};
}
}