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
226
227
228
229
230
231
232
use http::*;
use utils::ToRegex;
use utils::RequestContinuation;
use regex::Regex;
use parking_lot::RwLock;
pub trait Controller: Send + Sync {
fn handle(&self, req: &mut SyncRequest, res: &mut SyncResponse);
fn base_path(&self) -> &str;
}
pub struct RequestGuardCollection {
guards: Vec<Box<RequestGuard>>
}
impl RequestGuardCollection {
pub fn new() -> Self {
RequestGuardCollection {
guards: Vec::new(),
}
}
pub fn add<G: 'static + RequestGuard>(&mut self, guard: G) {
self.guards.push(Box::new(guard));
}
}
impl<G: 'static + RequestGuard> From<G> for RequestGuardCollection {
fn from(guard: G) -> Self {
let mut reqg = RequestGuardCollection::new();
reqg.add(guard);
reqg
}
}
impl<'a, G: 'static + RequestGuard + Clone> From<&'a [G]> for RequestGuardCollection {
fn from(guards: &'a [G]) -> Self {
let mut reqg = RequestGuardCollection::new();
for guard in guards.to_vec() {
reqg.add(guard);
}
reqg
}
}
impl<G: 'static + RequestGuard> From<Vec<G>> for RequestGuardCollection {
fn from(guards: Vec<G>) -> Self {
let mut reqg = RequestGuardCollection::new();
for guard in guards {
reqg.add(guard);
}
reqg
}
}
use ::std::slice::Iter;
impl<'a> IntoIterator for &'a RequestGuardCollection {
type Item = &'a Box<RequestGuard>;
type IntoIter = Iter<'a, Box<RequestGuard>>;
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
self.guards.iter()
}
}
pub trait RequestGuard {
fn validate(&self, req: &mut SyncRequest, res: &mut SyncResponse) -> RequestContinuation;
}
type DelegateFunction<T> = Fn(&T, &SyncRequest, &mut SyncResponse);
type ControllerDelegate<T> = (Method, Regex, Option<RequestGuardCollection>, Box<DelegateFunction<T>>);
pub struct ControllerDispatch<T> {
delegate_context: T,
delegates: RwLock<Vec<ControllerDelegate<T>>>,
}
impl<T: Send + Sync> ControllerDispatch<T> {
pub fn new(delegate_context: T) -> Self {
ControllerDispatch {
delegate_context,
delegates: RwLock::new(Vec::new()),
}
}
pub fn add<F, R: ToRegex>(&self, method: Method, path: R, delegate_func: F)
where for<'r, 's, 't0> F: 'static + Fn(&'r T, &'s SyncRequest, &'t0 mut SyncResponse) {
self.delegates.write().push((method, reg!(path), None, Box::new(delegate_func)));
}
pub fn add_with_guards<F, R: ToRegex>(&self, method: Method, path: R, guards: RequestGuardCollection, delegate_func: F)
where for<'r, 's, 't0> F: 'static + Fn(&'r T, &'s SyncRequest, &'t0 mut SyncResponse) {
self.delegates.write().push((method, reg!(path), Some(guards), Box::new(delegate_func)));
}
pub fn dispatch(&self, req: &mut SyncRequest, res: &mut SyncResponse) {
use std::iter::FromIterator;
let delegates_list = self.delegates.read();
let method = req.method().clone();
let retained_delegate = Vec::from_iter(delegates_list.iter().filter(move |x| {
x.0 == method
}));
if retained_delegate.len() == 0 {
res.status(StatusCode::METHOD_NOT_ALLOWED);
return;
}
for del in retained_delegate {
let (_, ref reg, ref op_guards, ref boxed_func) = del;
if req.current_path_match_and_capture(reg) {
if let Some(ref guards) = op_guards {
for guard in guards {
use RequestContinuation::*;
if let Stop = guard.validate(req, res) {
return;
}
}
}
boxed_func(&self.delegate_context, req, res);
return;
}
}
res.status(StatusCode::BAD_REQUEST);
}
}
unsafe impl<T> Sync for ControllerDispatch<T> {}
unsafe impl<T> Send for ControllerDispatch<T> {}
pub struct BasicController<C> {
base_path: String,
dispatch: ControllerDispatch<C>,
}
impl<C: Send + Sync> Controller for BasicController<C> {
fn handle(&self, req: &mut SyncRequest, res: &mut SyncResponse) {
self.dispatch.dispatch(req, res);
}
fn base_path(&self) -> &str {
&self.base_path
}
}
impl<C: Send + Sync> BasicController<C> {
pub fn new(name: &str, controller_context: C) -> Self {
BasicController {
base_path: name.to_string(),
dispatch: ControllerDispatch::new(controller_context),
}
}
pub fn add<F, R: ToRegex>(&self, method: Method, path: R, delegate_func: F)
where for<'r, 's, 't0> F: 'static + Fn(&'r C, &'s SyncRequest, &'t0 mut SyncResponse) {
self.dispatch.add(method, path, delegate_func);
}
pub fn add_with_guards<F, R: ToRegex>(&self, method: Method, path: R, guards: RequestGuardCollection, delegate_func: F)
where for<'r, 's, 't0> F: 'static + Fn(&'r C, &'s SyncRequest, &'t0 mut SyncResponse) {
self.dispatch.add_with_guards(method, path, guards, delegate_func);
}
}
pub struct BodyGuard;
impl RequestGuard for BodyGuard {
fn validate(&self, req: &mut SyncRequest, _res: &mut SyncResponse) -> RequestContinuation {
if req.body().len() <= 0 {
return RequestContinuation::Stop
}
RequestContinuation::Continue
}
}