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
use std::error::Error;
use std::path::{Path, Component};
use iron::prelude::*;
use iron::middleware::Handler;
use iron::{status, Url};
use iron::typemap;
use sequence_trie::SequenceTrie;
use std::fmt;
#[derive(Copy, Clone)]
pub struct OriginalUrl;
impl typemap::Key for OriginalUrl { type Value = Url; }
pub struct Mount {
inner: SequenceTrie<String, Match>
}
struct Match {
handler: Box<Handler>,
length: usize
}
#[derive(Debug)]
pub struct NoMatch;
impl Error for NoMatch {
fn description(&self) -> &'static str { "No Match" }
}
impl fmt::Display for NoMatch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.description())
}
}
impl Mount {
pub fn new() -> Mount {
Mount {
inner: SequenceTrie::new()
}
}
pub fn mount<H: Handler>(&mut self, route: &str, handler: H) -> &mut Mount {
let key: Vec<&str> = Path::new(route).components().flat_map(|c|
match c {
Component::RootDir => None,
c => Some(c.as_os_str().to_str().unwrap())
}
).collect();
let match_length = key.len();
self.inner.insert(key, Match {
handler: Box::new(handler) as Box<Handler>,
length: match_length,
});
self
}
}
impl Handler for Mount {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
let matched = {
let path = req.url.path();
let key = match path.last() {
Some(s) if s.is_empty() => &path[..path.len() - 1],
_ => &path
};
let key: Vec<_> = key.into_iter().map(|s| String::from(*s)).collect();
match self.inner.get_ancestor(&key) {
Some(matched) => matched,
None => return Err(IronError::new(NoMatch, status::NotFound))
}
};
let is_outer_mount = !req.extensions.contains::<OriginalUrl>();
if is_outer_mount {
req.extensions.insert::<OriginalUrl>(req.url.clone());
}
let path = req.url.path()[matched.length..].join("/");
req.url.as_mut().set_path(&path);
let res = matched.handler.handle(req);
req.url = match req.extensions.get::<OriginalUrl>() {
Some(original) => original.clone(),
None => panic!("OriginalUrl unexpectedly removed from req.extensions.")
};
if is_outer_mount {
req.extensions.remove::<OriginalUrl>();
}
res
}
}