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
pub use hyper::server::{Request, Response, Service};
pub use hyper::{header, Error, Method};
pub use futures_cpupool::CpuPool;
pub use tokio_core::reactor::Handle;
pub use url::percent_encoding::percent_decode;
pub use super::{Exception, ExceptionHandlerService, ExceptionHandlerServiceAsync};
pub use super::FutureObject;
pub use super::Config;
#[cfg(feature = "default")]
use super::content_type_maker;
#[cfg(feature = "default")]
use super::ExceptionHandler;
#[cfg(feature = "default")]
use super::StaticFile;
#[cfg(feature = "default")]
use super::StaticIndex;
pub use std::path::PathBuf;
#[macro_export]
macro_rules! static_fs {
($typo_fs: ident, $typo_file: ident, $fn_content_type_maker:ident, $typo_index: ident, $typo_exception_handler: ident) => {
pub struct $typo_fs<C> {
url: String,
path: PathBuf,
handle: Handle,
pool: CpuPool,
headers_file: Option<header::Headers>,
headers_index: Option<header::Headers>,
config: C,
}
impl<C> $typo_fs<C>
where
C: AsRef<Config> + Clone + Send,
{
pub fn new<U, P>(handle: Handle, pool: CpuPool, url: U, path: P, config: C) -> Self
where
U: Into<String>,
P: Into<PathBuf>,
{
Self {
url: url.into(),
path: path.into(),
handle: handle,
pool: pool,
config: config,
headers_index: None,
headers_file: None,
}
}
pub fn config(&self) -> &Config {
self.config.as_ref()
}
pub fn headers_file_mut(&mut self) -> &mut Option<header::Headers> {
&mut self.headers_file
}
pub fn headers_index_mut(&mut self) -> &mut Option<header::Headers> {
&mut self.headers_index
}
}
impl<C> Service for $typo_fs<C>
where
C: AsRef<Config> + Clone + Send + 'static,
{
type Request = Request;
type Response = Response;
type Error = Error;
type Future = FutureObject;
fn call(&self, req: Request) -> Self::Future {
match *req.method() {
Method::Head | Method::Get => {}
_ => return $typo_exception_handler::call_async(Exception::Method, req),
}
debug!(
"\nurl : {:?}\npath: {:?}\nreqRaw: {:?}\nreqDec: {:?}",
self.url,
self.path,
req.path(),
route(req.path(), &self.url, &self.path),
);
let (req_path,fspath) = match route(req.path(), &self.url, &self.path) {
Ok(p) => p,
Err(e) => return $typo_exception_handler::call_async(e, req),
};
let metadata = if self.config().get_follow_links() {
fspath.metadata()
} else {
fspath.symlink_metadata()
};
match metadata {
Ok(md) => {
let config = self.config.clone();
if md.is_file() {
let mut file_server = $typo_file::new(
self.handle.clone(),
self.pool.clone(),
fspath,
config.as_ref().clone(),
);
if self.headers_file.is_some() {
*file_server.headers_mut() = self.headers_file.clone();
}
file_server.headers_maker($fn_content_type_maker);
file_server.call(&self.pool, req)
} else if md.is_dir() {
let mut index_server = $typo_index::new(req_path, fspath, config.clone());
if self.headers_index.is_some() {
*index_server.headers_mut() = self.headers_index.clone();
}
index_server.call(&self.pool, req)
} else {
$typo_exception_handler::call_async(Exception::Typo, req)
}
}
Err(e) => $typo_exception_handler::call_async(e, req),
}
}
}
fn route(req_path: &str, base: &str, path: &PathBuf) -> Result<(String, PathBuf), Exception> {
let req_path_dec = percent_decode(req_path.as_bytes())
.decode_utf8()
.unwrap()
.into_owned()
.to_owned();
debug!("{}", req_path_dec);
let mut components = req_path_dec.split('/')
.filter(|c| !c.is_empty() && c != &".")
.collect::<Vec<_>>();
(0..components.len())
.into_iter()
.rev()
.for_each(|idx| if idx<components.len()&& components[idx] == ".." {
components.remove(idx);
if idx > 0 {
components.remove(idx-1);
}
});
let req_path =|| {
let mut tmp = components.iter().fold(String::with_capacity(req_path_dec.len()),| mut p, c| {
p.push('/');
p.push_str(c);
p
}
);
if req_path_dec.ends_with('/') {
tmp.push('/');
}
tmp
};
let mut base_components = base.split('/').filter(|c| !c.is_empty());
let mut components2 = components.iter();
loop {
match (components2.next(), base_components.next()) {
(Some(c), Some(b)) => {
if c != &b {
return Err(Exception::Route);
}
}
(Some(c), None) => {
let mut out = path.clone();
out.push(c);
components2.for_each(|cc| out.push(cc));
if out.exists() {
return Ok( (req_path(),out));
} else {
return Err(Exception::not_found());
}
}
(None, None) => {
return Ok( (req_path(), path.clone())) },
(None, Some(_)) => return Err(Exception::Route),
}
}
}
};
}
#[cfg(feature = "default")]
static_fs!(
StaticFs,
StaticFile,
content_type_maker,
StaticIndex,
ExceptionHandler
);