This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
server.rs
292 lines (229 loc) · 9.14 KB
/
server.rs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use hyper::header::common::content_type::ContentType;
use hyper::server::request::Request;
use mime::{Mime, TopLevel, SubLevel, Attr, Value};
use super::{MultipartField, MultipartFile};
use std::io::{RefReader, BufferedReader, IoError, IoResult, EndOfFile, standard_error, OtherIoError};
use std::kinds::marker;
fn is_multipart_formdata(req: &Request) -> bool {
use mime::{Multipart};
req.headers.get::<ContentType>().map(|ct| {
let ContentType(ref mime) = *ct;
match *mime {
Mime(TopLevel::Multipart, SubLevel::Ext(ref subtype), _) => subtype[] == "form-data",
_ => false,
}
}).unwrap_or(false)
}
fn get_boundary(ct: &ContentType) -> Option<String> {
let ContentType(ref mime) = *ct;
let Mime(_, _, ref params) = *mime;
params.iter().find(|&&(ref name, _)|
if let Attr::Ext(ref name) = *name {
name[] == "boundary"
} else { false }
).and_then(|&(_, ref val)|
if let Value::Ext(ref val) = *val {
Some(val.clone())
} else { None }
)
}
pub struct Multipart {
source: BoundaryReader,
}
macro_rules! try_find(
($haystack:expr, $f:ident, $needle:expr, $err:expr, $line:expr) => (
try!($haystack.$f($needle).ok_or(line_error($err, $line.clone())))
)
)
impl Multipart {
/// If the given `Request` is of `Content-Type: multipart/form-data`, return
/// the wrapped request as `Ok(Multipart)`, otherwise `Err(Request)`.
pub fn from_request(req: Request) -> Result<Multipart, Request> {
if !is_multipart_formdata(&req) { return Err(req); }
let boundary = if let Some(boundary) = req.headers.get::<ContentType>()
.and_then(get_boundary) { boundary } else { return Err(req); };
Ok(Multipart { source: BoundaryReader::from_request(req, boundary) })
}
pub fn read_entry<'a>(&'a mut self) -> IoResult<(String, MultipartField<'a>)> {
let (disp_type, field_name, filename) = try!(self.read_content_disposition());
if &*disp_type != "form-data" {
return Err(IoError {
kind: OtherIoError,
desc: "Content-Disposition value was not \"form-data\"",
detail: Some(format!("Content-Disposition: {}", disp_type)),
});
}
if let Some(content_type) = try!(self.read_content_type()) {
let _ = try!(self.source.reader.read_line()); // Consume empty line
Ok((field_name, MultipartField::File(
MultipartFile::from_octet(filename, &mut self.source, content_type[])))
)
} else {
// Empty line consumed by read_content_type()
let text = try!(self.source.read_to_string());
Ok((field_name, MultipartField::Text(text)))
}
}
/// Call `f` for each entry in the multipart request.
/// This is a substitute for `Multipart` implementing `Iterator`,
/// since `Iterator::next()` can't use bound lifetimes.
/// See https://www.reddit.com/r/rust/comments/2lkk4i/concrete_lifetime_vs_bound_lifetime/
pub fn foreach_entry<F: for<'a> FnMut(String, MultipartField<'a>)>(&mut self, mut f: F) {
loop {
match self.read_entry() {
Ok((name, field)) => f(name, field),
Err(err) => {
if err.kind != EndOfFile {
error!("Error reading Multipart: {}", err);
}
break;
},
}
}
}
fn read_content_disposition(&mut self) -> IoResult<(String, String, Option<String>)> {
let line = try!(self.source.reader.read_line());
// Find the end of CONT_DISP in the line
let disp_type = {
const CONT_DISP: &'static str = "Content-Disposition:";
let disp_idx = try_find!(line[], find_str, CONT_DISP,
"Content-Disposition subheader not found!", line) + CONT_DISP.len();
let disp_type_end = try_find!(line[disp_idx..], find, ';',
"Error parsing Content-Disposition value!", line);
line[disp_idx .. disp_type_end].trim().into_string()
};
let field_name = {
const NAME: &'static str = "name=\"";
let name_idx = try_find!(line[], find_str, NAME,
"Error parsing field name!", line) + NAME.len();
let name_end = try_find!(line[name_idx ..], find, '"',
"Error parsing field name!", line);
line[name_idx .. name_end].into_string() // No trim here since it's in quotes.
};
let filename = {
const FILENAME: &'static str = "filename=\"";
let filename_idx = line[].find_str(FILENAME).map(|idx| idx + FILENAME.len());
let filename_idxs = with(filename_idx, |&start| line[start ..].find('"'));
filename_idxs.map(|(start, end)| line[start .. end].into_string())
};
Ok((disp_type, field_name, filename))
}
fn read_content_type(&mut self) -> IoResult<Option<String>> {
let line = try!(self.source.reader.read_line());
const CONTENT_TYPE: &'static str = "Content-Type:";
let type_idx = (&*line).find_str(CONTENT_TYPE);
// FIXME Will not properly parse for multiple files!
// Does not expect boundary=<boundary>
Ok(type_idx.map(|start| line[start + CONTENT_TYPE.len()..].trim().into_string()))
}
}
fn with<T, U>(left: Option<T>, right: |&T| -> Option<U>) -> Option<(T, U)> {
let temp = left.as_ref().and_then(right);
match (left, temp) {
(Some(lval), Some(rval)) => Some((lval, rval)),
_ => None,
}
}
fn line_error(msg: &'static str, line: String) -> IoError {
IoError {
kind: OtherIoError,
desc: msg,
detail: Some(line),
}
}
/* FIXME: Can't have an iterator return a borrowed reference
impl<'a> Iterator<(String, MultipartField<'a>)> for Multipart {
fn next(&mut self) -> Option<(String, MultipartField<'a>)> {
match self.read_entry() {
Ok(ok) => Some(ok),
Err(err) => {
if err.kind != EndOfFile {
error!("Error reading Multipart: {}", err);
}
None
},
}
}
}
*/
/// A `Reader` that will yield bytes until it sees a given sequence.
pub struct BoundaryReader {
reader: BufferedReader<Request>,
boundary: Vec<u8>,
last_search_idx: uint,
boundary_read: bool,
}
impl BoundaryReader {
fn from_request(request: Request, mut boundary: String) -> BoundaryReader {
boundary.prepend("--");
BoundaryReader {
reader: BufferedReader::new(request),
boundary: boundary.into_bytes(),
last_search_idx: 0,
boundary_read: false,
}
}
fn read_to_boundary(&mut self) -> IoResult<bool> {
if !self.boundary_read {
let lookahead = try!(self.reader.fill_buf());
self.last_search_idx = lookahead[self.last_search_idx..]
.position_elem(&b'-').unwrap_or(lookahead.len() - 1);
Ok(lookahead[self.last_search_idx..].starts_with(self.boundary[]))
} else if self.last_search_idx == 0 {
Err(standard_error(EndOfFile))
} else { Ok(true) }
}
fn consume_boundary(&mut self) {
self.reader.consume(self.last_search_idx + self.boundary.len());
self.last_search_idx = 0;
self.boundary_read = false;
}
fn set_boundary(&mut self, boundary: String) {
self.boundary = boundary.into_bytes();
}
}
impl Reader for BoundaryReader {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
use std::cmp;
self.boundary_read = try!(self.read_to_boundary());
let trunc_len = cmp::min(buf.len(), self.last_search_idx);
let trunc_buf = buf[mut ..trunc_len]; // Truncate the buffer so we don't read ahead
let bytes_read = self.reader.read(trunc_buf).unwrap();
self.last_search_idx -= bytes_read;
Ok(bytes_read)
}
}
trait Prepend<T> {
fn prepend(&mut self, t: T);
}
impl<S: Str> Prepend<S> for String {
fn prepend(&mut self, s: S) {
unsafe {
self.as_mut_vec().prepend(s.as_slice().as_bytes());
}
}
}
impl<'a, T> Prepend<&'a [T]> for Vec<T> {
fn prepend(&mut self, slice: &[T]) {
use std::ptr::copy_memory;
let old_len = self.len();
self.reserve(slice.len());
unsafe {
self.set_len(old_len + slice.len());
copy_memory(self[mut slice.len()..].as_mut_ptr(), self[..old_len].as_ptr(), old_len);
copy_memory(self.as_mut_ptr(), slice.as_ptr(), slice.len());
}
}
}
#[test]
fn test_prepend() {
let mut vec = vec![3u64, 4, 5];
vec.prepend(&[1u64, 2]);
assert_eq!(vec[], [1u64, 2, 3, 4, 5][]);
}
#[test]
fn test_prepend_string() {
let mut string = "World!".into_string();
string.prepend("Hello, ");
assert_eq!(&*string, "Hello, World!");
}