rouille_ng/input/
multipart.rs1use std::error;
16use std::fmt;
17
18use Request;
19use RequestBody;
20
21use multipart::server::Multipart as InnerMultipart;
22
23pub use multipart::server::MultipartData;
25pub use multipart::server::MultipartField;
26
27#[derive(Clone, Debug)]
29pub enum MultipartError {
30 WrongContentType,
33
34 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
57pub 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
75pub struct Multipart<'a> {
77 inner: InnerMultipart<RequestBody<'a>>,
78}
79
80impl<'a> Multipart<'a> {
81 pub fn next(&mut self) -> Option<MultipartField<&mut InnerMultipart<RequestBody<'a>>>> {
82 self.inner.read_entry().unwrap_or(None)
83 }
84}
85
86fn multipart_boundary(request: &Request) -> Option<String> {
87 const BOUNDARY: &str = "boundary=";
88
89 let content_type = match request.header("Content-Type") {
90 None => return None,
91 Some(c) => c,
92 };
93
94 let start = match content_type.find(BOUNDARY) {
95 Some(pos) => pos + BOUNDARY.len(),
96 None => return None,
97 };
98
99 let end = content_type[start..]
100 .find(';')
101 .map_or(content_type.len(), |end| start + end);
102 Some(content_type[start..end].to_owned())
103}