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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
mod functions;
mod file_resources;
mod static_resources;
mod manager;
mod fairing;
mod macros;
extern crate mime;
extern crate mime_guess;
extern crate crc_any;
extern crate rocket;
extern crate rocket_etag_if_none_match;
use std::io::Cursor;
#[cfg(debug_assertions)]
use std::sync::MutexGuard;
use mime::Mime;
use rocket::State;
use rocket::request::Request;
use rocket::response::{self, Response, Responder};
use rocket::http::Status;
use rocket::fairing::Fairing;
pub use rocket_etag_if_none_match::{EntityTag, EtagIfNoneMatch};
pub use file_resources::FileResources;
pub use static_resources::StaticResources;
pub use manager::StaticContextManager;
use fairing::StaticResponseFairing;
#[derive(Debug)]
pub struct StaticResponse {
client_etag: EtagIfNoneMatch,
etag: Option<EntityTag>,
name: &'static str,
}
impl StaticResponse {
#[inline]
pub fn build(client_etag: EtagIfNoneMatch, etag: Option<EntityTag>, name: &'static str) -> StaticResponse {
StaticResponse {
client_etag,
etag,
name,
}
}
}
impl StaticResponse {
#[cfg(debug_assertions)]
#[inline]
pub fn fairing<F>(f: F) -> impl Fairing where F: Fn(&mut MutexGuard<FileResources>) + Send + Sync + 'static {
StaticResponseFairing {
custom_callback: Box::new(f)
}
}
#[cfg(not(debug_assertions))]
#[inline]
pub fn fairing<F>(f: F) -> impl Fairing where F: Fn(&mut StaticResources) + Send + Sync + 'static {
StaticResponseFairing {
custom_callback: Box::new(f)
}
}
}
impl<'a> Responder<'a> for StaticResponse {
#[cfg(debug_assertions)]
fn respond_to(self, request: &Request) -> response::Result<'a> {
let mut response = Response::build();
let cm = request.guard::<State<StaticContextManager>>().expect("StaticContextManager registered in on_attach");
let (mime, data, etag) = {
let mut resources = cm.resources.lock().unwrap();
match resources.get_resource(self.name, true) {
Ok((mime, data, etag)) => {
let is_etag_match = self.client_etag.weak_eq(&etag);
if is_etag_match {
response.status(Status::NotModified);
return response.ok();
} else {
let etag = self.etag.map(|etag| etag.to_string()).unwrap_or(etag.to_string());
(mime.to_string(), data.to_vec(), etag)
}
}
Err(_) => {
response.status(Status::InternalServerError);
return response.ok();
}
}
};
response
.raw_header("ETag", etag)
.raw_header("Content-Type", mime)
.sized_body(Cursor::new(data));
response.ok()
}
#[cfg(not(debug_assertions))]
fn respond_to(self, request: &Request) -> response::Result<'a> {
let mut response = Response::build();
let cm = request.guard::<State<StaticContextManager>>().expect("StaticContextManager registered in on_attach");
let (mime, data, etag) = {
let resources: &StaticResources = &cm.resources;
match resources.get_resource(self.name) {
Some((mime, data, etag)) => {
let is_etag_match = self.client_etag.weak_eq(&etag);
if is_etag_match {
response.status(Status::NotModified);
return response.ok();
} else {
let etag = self.etag.map(|etag| etag.to_string()).unwrap_or(etag.to_string());
(mime.to_string(), data, etag)
}
}
None => {
response.status(Status::InternalServerError);
return response.ok();
}
}
};
response
.raw_header("ETag", etag)
.raw_header("Content-Type", mime)
.sized_body(Cursor::new(data));
response.ok()
}
}