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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use mime::Mime;
use std::error::Error;
use std::io::{self, BufRead, Read};
use std::{fmt, str};
use std::sync::Arc;
use super::httparse::{self, Error as HttparseError, Header, Status, EMPTY_HEADER};
use self::ReadEntryResult::*;
use super::save::SaveBuilder;
const EMPTY_STR_HEADER: StrHeader<'static> = StrHeader { name: "", val: "" };
macro_rules! invalid_cont_disp {
($reason: expr, $cause: expr) => {
return Err(ParseHeaderError::InvalidContDisp(
$reason,
$cause.to_string(),
));
};
}
#[derive(Copy, Clone, Debug)]
pub struct StrHeader<'a> {
name: &'a str,
val: &'a str,
}
struct DisplayHeaders<'s, 'a: 's>(&'s [StrHeader<'a>]);
impl<'s, 'a: 's> fmt::Display for DisplayHeaders<'s, 'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for hdr in self.0 {
writeln!(f, "{}: {}", hdr.name, hdr.val)?;
}
Ok(())
}
}
fn with_headers<R, F, Ret>(r: &mut R, closure: F) -> Result<Ret, ParseHeaderError>
where
R: BufRead,
F: FnOnce(&[StrHeader]) -> Ret,
{
const HEADER_LEN: usize = 4;
let consume;
let ret;
let mut last_len = 0;
loop {
let buf = r.fill_buf()?;
if buf.len() == last_len {
return Err(ParseHeaderError::TooLarge);
}
let mut raw_headers = [EMPTY_HEADER; HEADER_LEN];
match httparse::parse_headers(buf, &mut raw_headers)? {
Status::Partial => last_len = buf.len(),
Status::Complete((consume_, raw_headers)) => {
let mut headers = [EMPTY_STR_HEADER; HEADER_LEN];
let headers = copy_headers(raw_headers, &mut headers)?;
debug!("Parsed headers: {:?}", headers);
consume = consume_;
ret = closure(headers);
break;
}
}
}
r.consume(consume);
Ok(ret)
}
fn copy_headers<'h, 'b: 'h>(
raw: &[Header<'b>],
headers: &'h mut [StrHeader<'b>],
) -> io::Result<&'h [StrHeader<'b>]> {
for (raw, header) in raw.iter().zip(&mut *headers) {
header.name = raw.name;
header.val = io_str_utf8(raw.value)?;
}
Ok(&headers[..raw.len()])
}
#[derive(Clone, Debug)]
pub struct FieldHeaders {
pub name: Arc<str>,
pub filename: Option<String>,
pub content_type: Option<Mime>,
}
impl FieldHeaders {
fn read_from<R: BufRead>(r: &mut R) -> Result<Self, ParseHeaderError> {
with_headers(r, Self::parse)?
}
fn parse(headers: &[StrHeader]) -> Result<FieldHeaders, ParseHeaderError> {
let cont_disp = ContentDisp::parse_required(headers)?;
Ok(FieldHeaders {
name: cont_disp.field_name.into(),
filename: cont_disp.filename,
content_type: parse_content_type(headers)?,
})
}
}
struct ContentDisp {
field_name: String,
filename: Option<String>,
}
impl ContentDisp {
fn parse_required(headers: &[StrHeader]) -> Result<ContentDisp, ParseHeaderError> {
let header = if let Some(header) = find_header(headers, "Content-Disposition") {
header
} else {
return Err(ParseHeaderError::MissingContentDisposition(
DisplayHeaders(headers).to_string(),
));
};
let after_disp_type = match split_once(header.val, ';') {
Some((disp_type, after_disp_type)) => {
if disp_type.trim() != "form-data" {
invalid_cont_disp!("unexpected Content-Disposition value", disp_type);
}
after_disp_type
}
None => invalid_cont_disp!(
"expected additional data after Content-Disposition type",
header.val
),
};
let (field_name, filename) = match get_str_after("name=", ';', after_disp_type) {
None => invalid_cont_disp!(
"expected field name and maybe filename, got",
after_disp_type
),
Some((field_name, after_field_name)) => {
let field_name = trim_quotes(field_name);
let filename = get_str_after("filename=", ';', after_field_name)
.map(|(filename, _)| trim_quotes(filename).to_owned());
(field_name, filename)
}
};
Ok(ContentDisp {
field_name: field_name.to_owned(),
filename,
})
}
}
fn parse_content_type(headers: &[StrHeader]) -> Result<Option<Mime>, ParseHeaderError> {
if let Some(header) = find_header(headers, "Content-Type") {
debug!("Found Content-Type: {:?}", header.val);
Ok(Some(header.val.parse::<Mime>().map_err(|_| {
ParseHeaderError::MimeError(header.val.into())
})?))
} else {
Ok(None)
}
}
#[derive(Debug)]
pub struct MultipartField<M: ReadEntry> {
pub headers: FieldHeaders,
pub data: MultipartData<M>,
}
impl<M: ReadEntry> MultipartField<M> {
pub fn is_text(&self) -> bool {
self.headers
.content_type
.as_ref()
.map_or(true, |ct| ct.type_() == mime::TEXT)
}
pub fn next_entry(self) -> ReadEntryResult<M> {
self.data.into_inner().read_entry()
}
pub fn next_entry_inplace(&mut self) -> io::Result<Option<&mut Self>>
where
for<'a> &'a mut M: ReadEntry,
{
let multipart = self.data.take_inner();
match multipart.read_entry() {
Entry(entry) => {
*self = entry;
Ok(Some(self))
}
End(multipart) => {
self.data.give_inner(multipart);
Ok(None)
}
Error(multipart, err) => {
self.data.give_inner(multipart);
Err(err)
}
}
}
}
#[derive(Debug)]
pub struct MultipartData<M> {
inner: Option<M>,
}
const DATA_INNER_ERR: &str = "MultipartFile::inner taken and not replaced; this is likely \
caused by a logic error in `multipart` or by resuming after \
a previously caught panic.\nPlease open an issue with the \
relevant backtrace and debug logs at \
https://github.com/abonander/multipart";
impl<M> MultipartData<M>
where
M: ReadEntry,
{
pub fn save(&mut self) -> SaveBuilder<&mut Self> {
SaveBuilder::new(self)
}
pub fn into_inner(self) -> M {
self.inner.expect(DATA_INNER_ERR)
}
pub fn set_min_buf_size(&mut self, min_buf_size: usize) {
self.inner_mut().set_min_buf_size(min_buf_size)
}
fn inner_mut(&mut self) -> &mut M {
self.inner.as_mut().expect(DATA_INNER_ERR)
}
fn take_inner(&mut self) -> M {
self.inner.take().expect(DATA_INNER_ERR)
}
fn give_inner(&mut self, inner: M) {
self.inner = Some(inner);
}
}
impl<M: ReadEntry> Read for MultipartData<M> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner_mut().source_mut().read(buf)
}
}
impl<M: ReadEntry> BufRead for MultipartData<M> {
fn fill_buf(&mut self) -> io::Result<&[u8]> {
self.inner_mut().source_mut().fill_buf()
}
fn consume(&mut self, amt: usize) {
self.inner_mut().source_mut().consume(amt)
}
}
fn split_once(s: &str, delim: char) -> Option<(&str, &str)> {
s.find(delim).map(|idx| s.split_at(idx))
}
fn trim_quotes(s: &str) -> &str {
s.trim_matches('"')
}
fn get_str_after<'a>(
needle: &str,
end_val_delim: char,
haystack: &'a str,
) -> Option<(&'a str, &'a str)> {
let val_start_idx = try_opt!(haystack.find(needle)) + needle.len();
let val_end_idx = haystack[val_start_idx..]
.find(end_val_delim)
.map_or(haystack.len(), |end_idx| end_idx + val_start_idx);
Some((
&haystack[val_start_idx..val_end_idx],
&haystack[val_end_idx..],
))
}
fn io_str_utf8(buf: &[u8]) -> io::Result<&str> {
str::from_utf8(buf).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
fn find_header<'a, 'b>(headers: &'a [StrHeader<'b>], name: &str) -> Option<&'a StrHeader<'b>> {
headers
.iter()
.find(|header| header.name.eq_ignore_ascii_case(name))
}
pub trait ReadEntry: PrivReadEntry + Sized {
fn read_entry(mut self) -> ReadEntryResult<Self> {
self.set_min_buf_size(super::boundary::MIN_BUF_SIZE);
debug!("ReadEntry::read_entry()");
if !try_read_entry!(self; self.consume_boundary()) {
return End(self);
}
let field_headers: FieldHeaders = try_read_entry!(self; self.read_headers());
if let Some(ct) = field_headers.content_type.as_ref() {
if ct.type_() == mime::MULTIPART {
info!(
"Found nested multipart field: {:?}:\r\n\
Please report this client's User-Agent and any other available details \
at https://github.com/abonander/multipart/issues/56",
field_headers
);
}
}
Entry(MultipartField {
headers: field_headers,
data: MultipartData { inner: Some(self) },
})
}
fn read_entry_mut(&mut self) -> ReadEntryResult<&mut Self> {
ReadEntry::read_entry(self)
}
}
impl<T> ReadEntry for T where T: PrivReadEntry {}
pub trait PrivReadEntry {
type Source: BufRead;
fn source_mut(&mut self) -> &mut Self::Source;
fn set_min_buf_size(&mut self, min_buf_size: usize);
fn consume_boundary(&mut self) -> io::Result<bool>;
fn read_headers(&mut self) -> Result<FieldHeaders, io::Error> {
FieldHeaders::read_from(self.source_mut())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
fn read_to_string(&mut self) -> io::Result<String> {
let mut buf = String::new();
match self.source_mut().read_to_string(&mut buf) {
Ok(_) => Ok(buf),
Err(err) => Err(err),
}
}
}
impl<'a, M: ReadEntry> PrivReadEntry for &'a mut M {
type Source = M::Source;
fn source_mut(&mut self) -> &mut M::Source {
(**self).source_mut()
}
fn set_min_buf_size(&mut self, min_buf_size: usize) {
(**self).set_min_buf_size(min_buf_size)
}
fn consume_boundary(&mut self) -> io::Result<bool> {
(**self).consume_boundary()
}
}
pub enum ReadEntryResult<M: ReadEntry, Entry = MultipartField<M>> {
Entry(Entry),
End(M),
Error(M, io::Error),
}
impl<M: ReadEntry, Entry> ReadEntryResult<M, Entry> {
pub fn into_result(self) -> io::Result<Option<Entry>> {
match self {
ReadEntryResult::Entry(entry) => Ok(Some(entry)),
ReadEntryResult::End(_) => Ok(None),
ReadEntryResult::Error(_, err) => Err(err),
}
}
pub fn unwrap(self) -> Entry {
self.expect_alt(
"`ReadEntryResult::unwrap()` called on `End` value",
"`ReadEntryResult::unwrap()` called on `Error` value: {:?}",
)
}
pub fn expect(self, msg: &str) -> Entry {
self.expect_alt(msg, msg)
}
pub fn expect_alt(self, end_msg: &str, err_msg: &str) -> Entry {
match self {
Entry(entry) => entry,
End(_) => panic!("{}", end_msg),
Error(_, err) => panic!("{}: {:?}", err_msg, err),
}
}
pub fn unwrap_opt(self) -> Option<Entry> {
self.expect_opt("`ReadEntryResult::unwrap_opt()` called on `Error` value")
}
pub fn expect_opt(self, msg: &str) -> Option<Entry> {
match self {
Entry(entry) => Some(entry),
End(_) => None,
Error(_, err) => panic!("{}: {:?}", msg, err),
}
}
}
const GENERIC_PARSE_ERR: &str = "an error occurred while parsing field headers";
quick_error! {
#[derive(Debug)]
enum ParseHeaderError {
MissingContentDisposition(headers: String) {
display(x) -> ("{}:\n{}", x.description(), headers)
description("\"Content-Disposition\" header not found in field headers")
}
InvalidContDisp(reason: &'static str, cause: String) {
display(x) -> ("{}: {}: {}", x.description(), reason, cause)
description("invalid \"Content-Disposition\" header")
}
TokenizeError(err: HttparseError) {
description(GENERIC_PARSE_ERR)
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
MimeError(cont_type: String) {
description("Failed to parse Content-Type")
display(this) -> ("{}: {}", this.description(), cont_type)
}
TooLarge {
description("field headers section ridiculously long or missing trailing CRLF-CRLF")
}
Io(err: io::Error) {
description("an io error occurred while parsing the headers")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
}
}
#[test]
fn test_find_header() {
let headers = [
StrHeader {
name: "Content-Type",
val: "text/plain",
},
StrHeader {
name: "Content-disposition",
val: "form-data",
},
StrHeader {
name: "content-transfer-encoding",
val: "binary",
},
];
assert_eq!(
find_header(&headers, "Content-Type").unwrap().val,
"text/plain"
);
assert_eq!(
find_header(&headers, "Content-Disposition").unwrap().val,
"form-data"
);
assert_eq!(
find_header(&headers, "Content-Transfer-Encoding")
.unwrap()
.val,
"binary"
);
}