rouille/input/
multipart.rs

1// Copyright (c) 2016 The Rouille developers
2// Licensed under the Apache License, Version 2.0
3// <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
6// at your option. All files in the project carrying such
7// notice may not be copied, modified, or distributed except
8// according to those terms.
9
10//! Parsing data sent with `multipart/form-data`.
11//!
12//! > **Note**: You are encouraged to look at [the `post` module](../post/index.html) instead in
13//! > order to parse data from HTML forms.
14
15use std::error;
16use std::fmt;
17
18use Request;
19use RequestBody;
20
21use multipart::server::Multipart as InnerMultipart;
22
23// TODO: provide wrappers around these
24pub use multipart::server::MultipartData;
25pub use multipart::server::MultipartField;
26
27/// Error that can happen when decoding multipart data.
28#[derive(Clone, Debug)]
29pub enum MultipartError {
30    /// The `Content-Type` header of the request indicates that it doesn't contain multipart data
31    /// or is invalid.
32    WrongContentType,
33
34    /// Can't parse the body of the request because it was already extracted.
35    BodyAlreadyExtracted,
36}
37
38impl error::Error for MultipartError {}
39
40impl fmt::Display for MultipartError {
41    #[inline]
42    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
43        let description = match *self {
44            MultipartError::WrongContentType => {
45                "the `Content-Type` header of the request indicates that it doesn't contain \
46                 multipart data or is invalid"
47            }
48            MultipartError::BodyAlreadyExtracted => {
49                "can't parse the body of the request because it was already extracted"
50            }
51        };
52
53        write!(fmt, "{}", description)
54    }
55}
56
57/// Attempts to decode the content of the request as `multipart/form-data` data.
58pub fn get_multipart_input(request: &Request) -> Result<Multipart, MultipartError> {
59    let boundary = match multipart_boundary(request) {
60        Some(b) => b,
61        None => return Err(MultipartError::WrongContentType),
62    };
63
64    let request_body = if let Some(body) = request.data() {
65        body
66    } else {
67        return Err(MultipartError::BodyAlreadyExtracted);
68    };
69
70    Ok(Multipart {
71        inner: InnerMultipart::with_body(request_body, boundary),
72    })
73}
74
75/// Allows you to inspect the content of the multipart input of a request.
76pub struct Multipart<'a> {
77    inner: InnerMultipart<RequestBody<'a>>,
78}
79
80impl<'a> Multipart<'a> {
81    #[allow(clippy::should_implement_trait)]
82    pub fn next(&mut self) -> Option<MultipartField<&mut InnerMultipart<RequestBody<'a>>>> {
83        self.inner.read_entry().unwrap_or(None)
84    }
85}
86
87fn multipart_boundary(request: &Request) -> Option<String> {
88    const BOUNDARY: &str = "boundary=";
89
90    let content_type = match request.header("Content-Type") {
91        None => return None,
92        Some(c) => c,
93    };
94
95    let start = match content_type.find(BOUNDARY) {
96        Some(pos) => pos + BOUNDARY.len(),
97        None => return None,
98    };
99
100    let end = content_type[start..]
101        .find(';')
102        .map_or(content_type.len(), |end| start + end);
103    Some(content_type[start..end].to_owned())
104}