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
use std::collections::{HashMap,HashSet};
use std::fs::{File};
use std::io::{BufReader, Read};
use std::path::PathBuf;
use futures::future::ok;
use hyper::{Get, Post, Put, Delete, StatusCode, Error};
use hyper::server::{Service, Request, Response};
use hyper::header::{ContentLength};
use super::Callback;
pub struct Pony {
pub gets: HashMap<String, Callback>,
pub posts: HashMap<String, Callback>,
pub puts: HashMap<String, Callback>,
pub deletes: HashMap<String, Callback>,
pub static_path: String,
pub static_enabled: bool,
pub not_found_path: String,
pub custom_not_found: bool,
pub known_extensions: HashSet<String>
}
impl Pony {
fn get(&self, req: Request) -> super::HyperResult {
match self.gets.get(req.path()) {
Some(cb) => {
cb(req)
},
None => {
if self.static_enabled {
self.static_file(req.path())
} else {
self.not_found()
}
},
}
}
fn static_file(&self, path: &str) -> super::HyperResult {
let mut incoming = String::from(path);
if incoming.ends_with('/') {
incoming += "index.html";
} else if !self.check_for_known_ext(&incoming) {
incoming += "/index.html";
}
if self.static_path.ends_with('/') && incoming.starts_with('/') {
incoming.remove(0);
}
let static_path = self.static_path.clone() + &incoming;
let pb = PathBuf::from(static_path);
let file = if let Ok(f) = File::open(pb) {
f
} else {
return self.not_found()
};
let mut reader = BufReader::new(file);
let mut contents: Vec<u8> = vec!();
if let Ok(_) = reader.read_to_end(&mut contents) {
Box::new(
ok(
Response::new()
.with_body(contents)
)
)
} else {
self.not_found()
}
}
fn check_for_known_ext(&self, path: &str) -> bool {
if path.ends_with("/") {
return false;
}
let ext = path.split('.').last().expect("failed to get last item in path");
self.known_extensions.contains(ext)
}
}
impl Service for Pony {
type Request = Request;
type Response = Response;
type Error = Error;
type Future = super::HyperResult;
fn call(&self, req: Request) -> Self::Future {
match req.method() {
&Get => {
self.get(req)
},
&Post => {
match self.posts.get(req.path()) {
Some(cb) => cb(req),
None => self.not_found(),
}
},
&Put => {
match self.puts.get(req.path()) {
Some(cb) => cb(req),
None => self.not_found(),
}
},
&Delete => {
match self.deletes.get(req.path()) {
Some(cb) => cb(req),
None => self.not_found(),
}
}
_ => {
self.not_found()
}
}
}
}
impl Pony {
fn not_found(&self) -> super::HyperResult {
if self.custom_not_found {
let path = PathBuf::from(&self.not_found_path);
let file = if let Ok(f) = File::open(path) {
f
} else {
panic!(format!("Unable to find file 404 file: {:?}", self.not_found_path));
};
let mut reader = BufReader::new(file);
let mut bytes: Vec<u8> = vec!();
if let Ok(size) = reader.read_to_end(&mut bytes) {
Box::new(
ok(
Response::new()
.with_status(StatusCode::Ok)
.with_header(ContentLength(size as u64))
.with_body(bytes)
)
)
} else {
Pony::default_not_found()
}
} else {
Pony::default_not_found()
}
}
fn default_not_found() -> super::HyperResult {
Box::new(
ok(
Response::new()
.with_status(StatusCode::NotFound)
)
)
}
}
#[cfg(test)]
mod tests {
}