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
use hyper::net::Fresh;
use hyper::header::ContentType;
use hyper::method::Method;
use hyper::server::{Handler, Request, Response};
pub use hyper::server::Request as HyperRequest;
use mime::{Mime, TopLevel, SubLevel, Attr, Value};
use super::{Multipart, HttpRequest};
pub struct Switch<H, M> {
normal: H,
multipart: M,
}
impl<H, M> Switch<H, M> where H: Handler, M: MultipartHandler {
pub fn new(normal: H, multipart: M) -> Switch<H, M> {
Switch {
normal: normal,
multipart: multipart,
}
}
}
impl<H, M> Handler for Switch<H, M> where H: Handler, M: MultipartHandler {
fn handle<'a, 'k>(&'a self, req: Request<'a, 'k>, res: Response<'a, Fresh>) {
match Multipart::from_request(req) {
Ok(multi) => self.multipart.handle_multipart(multi, res),
Err(req) => self.normal.handle(req, res),
}
}
}
pub trait MultipartHandler: Send + Sync {
fn handle_multipart<'a, 'k>(&self,
multipart: Multipart<Request<'a, 'k>>,
response: Response<'a, Fresh>);
}
impl<F> MultipartHandler for F
where F: Fn(Multipart<Request>, Response<Fresh>), F: Send + Sync {
fn handle_multipart<'a, 'k>(&self,
multipart: Multipart<Request<'a, 'k>>,
response: Response<'a, Fresh>) {
(*self)(multipart, response);
}
}
impl<'a, 'b> HttpRequest for HyperRequest<'a, 'b> {
type Body = Self;
fn multipart_boundary(&self) -> Option<&str> {
if self.method != Method::Post {
return None;
}
self.headers.get::<ContentType>().and_then(|ct| {
let ContentType(ref mime) = *ct;
let params = match *mime {
Mime(TopLevel::Multipart, SubLevel::FormData, ref params) => params,
_ => return None,
};
params.iter().find(|&&(ref name, _)|
match *name {
Attr::Boundary => true,
_ => false,
}
).and_then(|&(_, ref val)|
match *val {
Value::Ext(ref val) => Some(&**val),
_ => None,
}
)
})
}
fn body(self) -> Self {
self
}
}
impl<'r, 'a, 'b> HttpRequest for &'r mut HyperRequest<'a, 'b> {
type Body = Self;
fn multipart_boundary(&self) -> Option<&str> {
if self.method != Method::Post {
return None;
}
self.headers.get::<ContentType>().and_then(|ct| {
let ContentType(ref mime) = *ct;
let params = match *mime {
Mime(TopLevel::Multipart, SubLevel::FormData, ref params) => params,
_ => return None,
};
params.iter().find(|&&(ref name, _)|
match *name {
Attr::Boundary => true,
_ => false,
}
).and_then(|&(_, ref val)|
match *val {
Value::Ext(ref val) => Some(&**val),
_ => None,
}
)
})
}
fn body(self) -> Self::Body {
self
}
}