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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use rocket::fairing::{Fairing, Info, Kind};
use rocket::http::hyper::header::{CacheControl, CacheDirective};
use rocket::http::{ContentType, Status};
use rocket::request::{FromRequest, Outcome};
use rocket::response::{Redirect, Responder, Result as ResponseResult};
use rocket::{Request, Rocket, State};
use serde::{Deserialize, Serialize};
use snafu::{ensure, OptionExt, ResultExt, Snafu};
use std::fmt::Display;
use std::fs::File;
use std::io;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
#[derive(Debug, Snafu)]
#[non_exhaustive]
enum Error {
OutOfBounds,
Utf8,
Io { source: std::io::Error },
}
impl<'r> Responder<'r> for Error {
fn respond_to(self, _: &Request) -> ResponseResult<'r> {
match self {
Error::Io { source } if source.kind() != io::ErrorKind::NotFound => {
Err(Status::InternalServerError)
}
_ => Err(Status::NotFound),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
struct Config {
serve_from: PathBuf,
path_prefix: String,
}
#[derive(Debug)]
struct Inner {
config: Config,
hashes: &'static phf::Map<&'static str, &'static str>,
}
#[derive(Debug, Clone)]
pub struct StaticFiles(Arc<Inner>);
impl StaticFiles {
pub fn fairing(hashes: &'static phf::Map<&'static str, &'static str>) -> impl Fairing {
StaticFilesFairing { hashes }
}
pub fn to<D: Display>(&self, path: D) -> String {
let path = path.to_string();
let hash = self
.0
.hashes
.get(&path[1..])
.map(|x| format!("?v={}", x))
.unwrap_or_default();
format!("{}{}{}", self.0.config.path_prefix, path, hash)
}
}
impl<'a, 'r> FromRequest<'a, 'r> for StaticFiles {
type Error = ();
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
request
.guard::<State<StaticFiles>>()
.map(|x| x.inner().clone())
}
}
struct StaticFilesFairing {
hashes: &'static phf::Map<&'static str, &'static str>,
}
impl Fairing for StaticFilesFairing {
fn info(&self) -> Info {
Info {
name: "Static Files",
kind: Kind::Attach,
}
}
fn on_attach(&self, mut rocket: Rocket) -> Result<Rocket, Rocket> {
let orig_config = match rocket.config().get_extra("static_files") {
Ok(c) => c.clone(),
Err(_) => return Err(rocket),
};
let orig_config: Config = match orig_config.try_into() {
Ok(c) => c,
Err(_) => return Err(rocket),
};
let canon = rocket
.config()
.root_relative(orig_config.serve_from)
.canonicalize();
let serve_from = match canon {
Ok(s) => s,
Err(_) => return Err(rocket),
};
rocket = rocket.mount(&orig_config.path_prefix, routes![serve_static]);
Ok(rocket.manage(StaticFiles(Arc::new(Inner {
hashes: self.hashes,
config: Config {
path_prefix: orig_config.path_prefix,
serve_from,
},
}))))
}
}
#[derive(Debug, Responder)]
struct FileResponse {
file: File,
content_type: ContentType,
cache_control: CacheControl,
}
impl FileResponse {
pub fn new<P: AsRef<Path>>(path: P, cache: bool) -> Result<Self, Error> {
Self::new_path(path.as_ref(), cache)
}
fn cache_control(cache: bool) -> CacheControl {
if cache {
CacheControl(vec![CacheDirective::MaxAge(31536000)])
} else {
CacheControl(vec![])
}
}
fn new_path(path: &Path, cache: bool) -> Result<Self, Error> {
let file = File::open(path).context(Io)?;
let mime = mime_guess::from_path(path).first_or_octet_stream();
let content_type = ContentType::from_str(&mime.to_string()).unwrap();
Ok(FileResponse {
file,
content_type,
cache_control: Self::cache_control(cache),
})
}
}
#[derive(Debug, Responder)]
enum RedirectOrFile {
Redirect(Redirect),
File(FileResponse),
}
#[get("/<path..>?<v>")]
fn serve_static(
path: PathBuf,
v: Option<String>,
static_files: StaticFiles,
) -> Result<RedirectOrFile, Error> {
let expected_revision = v.as_deref();
let text = path.to_str().context(Utf8)?;
let target = static_files
.0
.config
.serve_from
.join(&path)
.canonicalize()
.context(Io)?;
ensure!(
target.starts_with(&static_files.0.config.serve_from),
OutOfBounds,
);
let current_revision = static_files.0.hashes.get(text).copied();
let resp = match (expected_revision, current_revision) {
(Some(expected), Some(current)) if expected == current => {
RedirectOrFile::File(FileResponse::new(target, true)?)
}
(_, Some(current)) => {
let url = format!(
"{}{}",
static_files.0.config.path_prefix,
uri!(serve_static: path, current)
);
let redir = Redirect::to(url);
RedirectOrFile::Redirect(redir)
}
(_, None) => RedirectOrFile::File(FileResponse::new(target, false)?),
};
Ok(resp)
}