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
#![deny(clippy::all)]
use kvarn::{extensions::*, prelude::*};
use wrappers::*;
#[cfg(feature = "reverse-proxy")]
#[path = "reverse-proxy.rs"]
pub mod reverse_proxy;
#[cfg(feature = "reverse-proxy")]
pub use reverse_proxy::{
localhost, static_connection, Connection as ReverseProxyConnection, Manager as ReverseProxy,
};
#[cfg(feature = "push")]
pub mod push;
#[cfg(feature = "push")]
pub use push::mount as mount_push;
#[cfg(feature = "kvarn-fastcgi-client")]
pub mod fastcgi;
#[cfg(feature = "php")]
pub mod php;
#[cfg(feature = "php")]
pub use php::mount_php as php;
#[cfg(feature = "templates")]
pub mod templates;
#[cfg(feature = "templates")]
pub use templates::templates;
pub fn new() -> Extensions {
let mut e = Extensions::new();
mount_all(&mut e);
e
}
pub fn mount_all(extensions: &mut Extensions) {
extensions.add_present_internal("download".to_string(), Box::new(download));
extensions.add_present_internal("cache".to_string(), Box::new(cache));
extensions.add_present_internal("hide".to_string(), Box::new(hide));
extensions.add_present_file("private".to_string(), Box::new(hide));
extensions.add_present_internal("allow-ips".to_string(), Box::new(ip_allow));
#[cfg(feature = "php")]
php(extensions);
#[cfg(feature = "templates")]
extensions.add_present_internal("tmpl".to_string(), Box::new(templates));
#[cfg(feature = "push")]
push::mount(extensions);
}
#[allow(dead_code)]
pub mod parse {
use super::*;
pub fn format_file_name<P: AsRef<Path>>(path: &P) -> Option<&str> {
path.as_ref().file_name().and_then(std::ffi::OsStr::to_str)
}
pub fn format_file_path<P: AsRef<Path>>(path: &P) -> Result<PathBuf, io::Error> {
let mut file_path = std::env::current_dir()?;
file_path.push(path);
Ok(file_path)
}
}
pub fn download(mut data: PresentDataWrapper) -> RetFut<()> {
let data = unsafe { data.get_inner() };
let headers = data.response_mut().headers_mut();
utils::replace_header_static(headers, "content-type", "application/octet-stream");
ready(())
}
pub fn cache(mut data: PresentDataWrapper) -> RetFut<()> {
fn parse<'a, I: Iterator<Item = &'a str>>(
iter: I,
) -> (Option<comprash::ClientCachePreference>, Option<comprash::ServerCachePreference>) {
let mut c = None;
let mut s = None;
for arg in iter {
let mut parts = arg.split(':');
let domain = parts.next();
let cache = parts.next();
if let (Some(domain), Some(cache)) = (domain, cache) {
match domain {
"client" => {
if let Ok(preference) = cache.parse() {
c = Some(preference)
}
}
"server" => {
if let Ok(preference) = cache.parse() {
s = Some(preference)
}
}
_ => {}
}
}
}
(c, s)
}
let data = unsafe { data.get_inner() };
let preference = parse(data.args().iter());
if let Some(c) = preference.0 {
*data.client_cache_preference() = c;
}
if let Some(s) = preference.1 {
*data.server_cache_preference() = s;
}
ready(())
}
pub fn hide(mut data: PresentDataWrapper) -> RetFut<()> {
box_fut!({
let data = unsafe { data.get_inner() };
let error = default_error(StatusCode::NOT_FOUND, Some(data.host()), None).await;
*data.response_mut() = error;
})
}
pub fn ip_allow(mut data: PresentDataWrapper) -> RetFut<()> {
box_fut!({
let data = unsafe { data.get_inner() };
let mut matched = false;
for denied in data.args().iter() {
if let Ok(ip) = denied.parse::<IpAddr>() {
if data.address().ip() == ip {
matched = true;
break;
}
}
}
*data.server_cache_preference() = comprash::ServerCachePreference::None;
*data.client_cache_preference() = comprash::ClientCachePreference::Changing;
if !matched {
let error = default_error(StatusCode::NOT_FOUND, Some(data.host()), None).await;
*data.response_mut() = error;
}
})
}
pub fn force_cache(
extensions: &mut Extensions,
rules: &'static [(&'static str, comprash::ClientCachePreference)],
) {
extensions.add_package(package!(response, req, _host {
let extension = req.uri().path().split('.').last();
let path = req.uri().path();
if let Some(extension) = extension {
for (rule, preference) in rules {
let replace = (rule.starts_with('/') && path.starts_with(rule))
|| rule.strip_prefix('.').map_or(false, |ext| ext == extension)
|| rule.strip_prefix('*').and_then(|rule| rule.strip_suffix('*')).map_or(false, |rule| path.contains(rule));
if replace {
utils::replace_header(response.headers_mut(), "cache-control", preference.as_header());
}
}
}
}), extensions::Id::new(16, "Adding cache-control header (force-cache)"));
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn all() {
let extensions = new();
let _server = kvarn_testing::ServerBuilder::from(extensions).run().await;
}
}