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
use super::*;
impl Default for StaticFilesConfig {
/// Builds the configuration the static file plugin uses when none is
/// given.
///
/// # Returns
///
/// - `Self` - The default configuration.
fn default() -> Self {
Self {
root: PathBuf::from("."),
index: INDEX_DEFAULT.to_owned(),
}
}
}
impl StaticFilesConfig {
/// The rules the configuration works from.
///
/// # Returns
///
/// - `StaticFileResolver<'_>` - The rules of the configuration.
fn resolver(&self) -> StaticFileResolver<'_> {
StaticFileResolver {
root: self.get_root().as_path(),
index: self.get_index(),
}
}
/// The file a path names under the root, when the path is allowed.
///
/// A path that climbs out of the root is refused, so a request cannot
/// reach a file the root does not hold.
///
/// # Arguments
///
/// - `&str` - The path of the file, relative to the root.
///
/// # Returns
///
/// - `Option<PathBuf>` - The file, when the path is allowed.
pub fn resolve(&self, path: &str) -> Option<PathBuf> {
self.resolver().resolve(path)
}
/// The content type of a file, by the extension of its name.
///
/// A file whose extension the framework does not know is served as a
/// stream of bytes.
///
/// # Arguments
///
/// - `&Path` - The file that is served.
///
/// # Returns
///
/// - `String` - The content type of the file.
pub fn content_type(&self, file: &Path) -> String {
let path: String = file.to_string_lossy().to_string();
let extension: String = FileExtension::get_extension_name(path);
let content_type: &str = FileExtension::parse(extension).get_content_type();
if content_type.is_empty() {
return APPLICATION_OCTET_STREAM.to_owned();
}
content_type.to_owned()
}
}
impl StaticFilesPlugin {
/// Stores the configuration the plugin works from.
///
/// # Arguments
///
/// - `StaticFilesConfig` - The configuration to store.
///
/// # Returns
///
/// - `Result<(), String>` - Nothing, or the reason the configuration was
/// refused.
pub fn init(config: StaticFilesConfig) -> Result<(), String> {
CONFIG.set(config).map_err(|_: StaticFilesConfig| {
"the static file configuration was already set".to_owned()
})
}
/// The configuration the plugin works from.
///
/// # Returns
///
/// - `Option<&'static StaticFilesConfig>` - The configuration, when it was
/// set.
pub fn get() -> Option<&'static StaticFilesConfig> {
CONFIG.get()
}
/// The route pattern that serves the files under a prefix.
///
/// # Arguments
///
/// - `&str` - The path the files are served under.
///
/// # Returns
///
/// - `String` - The pattern of the route.
pub fn pattern(prefix: &str) -> String {
PATTERN_FORMAT.replace("{prefix}", prefix)
}
}
impl ServerHook for StaticFilesRoute {
/// Builds the route.
///
/// # Arguments
///
/// - `&mut Stream` - The stream the request arrived on.
/// - `&mut Context` - The context of the request.
///
/// # Returns
///
/// - `Self` - The route.
async fn new(_: &mut Stream, _: &mut Context) -> Self {
Self
}
/// Answers with the file the path names.
///
/// A path that names no file is answered with the status of a missing
/// file.
///
/// # Arguments
///
/// - `Self` - The route.
/// - `&mut Stream` - The stream the request arrived on.
/// - `&mut Context` - The context of the request.
///
/// # Returns
///
/// - `Status` - Whether the pipeline continues.
async fn handle(self, _: &mut Stream, ctx: &mut Context) -> Status {
let config: &'static StaticFilesConfig = match CONFIG.get() {
Some(config) => config,
None => return Status::Continue,
};
let path: String = ctx.get_route_param(PARAM_PATH);
let file: Option<PathBuf> = config.resolver().resolve(&path);
let file: PathBuf = match file {
Some(file) => file,
None => return Self::not_found(ctx),
};
let data: Vec<u8> = match fs::read(&file).await {
Ok(data) => data,
Err(_) => return Self::not_found(ctx),
};
let content_type: String = config.content_type(&file);
let response: &mut Response = ctx.get_mut_response();
response.set_status_code(HttpStatus::Ok.code());
response.set_header(CONTENT_TYPE, content_type);
response.set_body(data);
Status::Continue
}
}
impl StaticFilesRoute {
/// Answers with the status of a file that is not there.
///
/// # Arguments
///
/// - `&mut Context` - The context of the request.
///
/// # Returns
///
/// - `Status` - Whether the pipeline continues.
fn not_found(ctx: &mut Context) -> Status {
let response: &mut Response = ctx.get_mut_response();
response.set_status_code(HttpStatus::NotFound.code());
Status::Continue
}
}