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.
910//! 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.
1415use std::error;
16use std::fmt;
1718use Request;
19use RequestBody;
2021use multipart::server::Multipart as InnerMultipart;
2223// TODO: provide wrappers around these
24pub use multipart::server::MultipartData;
25pub use multipart::server::MultipartField;
2627/// 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.
32WrongContentType,
3334/// Can't parse the body of the request because it was already extracted.
35BodyAlreadyExtracted,
36}
3738impl error::Error for MultipartError {}
3940impl fmt::Display for MultipartError {
41#[inline]
42fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
43let 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 };
5253write!(fmt, "{}", description)
54 }
55}
5657/// Attempts to decode the content of the request as `multipart/form-data` data.
58pub fn get_multipart_input(request: &Request) -> Result<Multipart, MultipartError> {
59let boundary = match multipart_boundary(request) {
60Some(b) => b,
61None => return Err(MultipartError::WrongContentType),
62 };
6364let request_body = if let Some(body) = request.data() {
65 body
66 } else {
67return Err(MultipartError::BodyAlreadyExtracted);
68 };
6970Ok(Multipart {
71 inner: InnerMultipart::with_body(request_body, boundary),
72 })
73}
7475/// Allows you to inspect the content of the multipart input of a request.
76pub struct Multipart<'a> {
77 inner: InnerMultipart<RequestBody<'a>>,
78}
7980impl<'a> Multipart<'a> {
81#[allow(clippy::should_implement_trait)]
82pub fn next(&mut self) -> Option<MultipartField<&mut InnerMultipart<RequestBody<'a>>>> {
83self.inner.read_entry().unwrap_or(None)
84 }
85}
8687fn multipart_boundary(request: &Request) -> Option<String> {
88const BOUNDARY: &str = "boundary=";
8990let content_type = match request.header("Content-Type") {
91None => return None,
92Some(c) => c,
93 };
9495let start = match content_type.find(BOUNDARY) {
96Some(pos) => pos + BOUNDARY.len(),
97None => return None,
98 };
99100let end = content_type[start..]
101 .find(';')
102 .map_or(content_type.len(), |end| start + end);
103Some(content_type[start..end].to_owned())
104}