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
use iron::*;
use iron::mime::*;
use iron::method::*;
use iron::headers::*;
use iron::modifiers::*;
use sha2::{Sha256, Digest};
pub struct StaticFile {
mime_type: String,
path: String,
etag: String,
content: Vec<u8>
}
impl StaticFile {
fn infer_mime_type<'a>(path: &str) -> &'a str {
if path.ends_with("html") || path.ends_with("htm") {
"text/html; charset=utf-8"
} else if path.ends_with("css") {
"text/css; charset=utf-8"
} else if path.ends_with("js") {
"text/javascript; charset=utf-8"
} else if path.ends_with("json") {
"application/json; charset=utf-8"
} else if path.ends_with("png") {
"image/png"
} else if path.ends_with("gif") {
"image/gif"
} else if path.ends_with("jpg") || path.ends_with("jpeg") {
"image/jpeg"
} else if path.ends_with("svg") {
"image/svg+xml; charset=utf-8"
} else if path.ends_with("txt") {
"text/plain; charset=utf-8"
} else if path.ends_with("woff2") {
"font/woff2"
} else {
"application/octet-stream"
}
}
fn etag_from_content(content: &[u8]) -> String {
let mut hasher = Sha256::default();
hasher.input(content);
let output = hasher.result();
output.iter()
.take(8)
.fold(String::new(), |so_far, next_byte| so_far + &format!("{:02x}", next_byte))
}
pub fn new_with_type(mime_type: &str, path: &str, content: &[u8]) -> StaticFile {
StaticFile {
mime_type: String::from(mime_type),
path: String::from(path),
etag: StaticFile::etag_from_content(content),
content: Vec::from(content)
}
}
pub fn new(path: &str, content: &[u8]) -> StaticFile {
StaticFile::new_with_type(
StaticFile::infer_mime_type(path),
path,
content
)
}
pub fn valid_paths(&self) -> Vec<String> {
let mut result = Vec::new();
result.push(self.path.clone());
if self.path.ends_with("/index.html") {
let no_chars_without_index = self.path.len() - "index.html".len();
result.push(String::from(&self.path[..no_chars_without_index]));
}
result
}
pub fn content<'a>(&'a self) -> &'a [u8] {
&self.content
}
pub fn mime_type<'a>(&'a self) -> &'a str {
&self.mime_type
}
pub fn etag(&self) -> String {
self.etag.clone()
}
}
impl Handler for StaticFile {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
if req.method == Method::Get || req.method == Method::Head {
let content_type = self.mime_type.parse::<Mime>().unwrap();
let etag = EntityTag::weak(self.etag());
let if_none_match = req.headers.get::<headers::IfNoneMatch>();
let not_modified = if let Some(&IfNoneMatch::Items(ref if_none_match)) = if_none_match {
if_none_match.iter().any(|possible_etag| possible_etag == &etag)
} else {
false
};
let mut response = Response::with((
Header(ContentType(content_type)),
Header(ContentLength(self.content().len() as u64)),
Header(ETag(etag)),
Header(CacheControl(vec![CacheDirective::Public, CacheDirective::MaxAge(60)]))));
if not_modified {
response = response.set(status::NotModified);
} else {
response = response.set(status::Ok);
if req.method != Method::Head {
response = response.set(Vec::from(self.content()));
}
}
Ok(response)
} else {
Ok(Response::with((status::BadRequest)))
}
}
}