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
//! Multipart request parsing.

use std::{
    convert::TryFrom,
    io::BufRead,
    ops::{Deref, DerefMut},
};

use crate::{header::Headers, Header, Request};

/// A multipart request.
pub struct MultipartData<'a> {
    /// The content type of the request.
    pub content_type: &'a str,
    /// The lists of entries.
    entries: Vec<MultipartEntry<'a>>,
}

/// An entry in a multipart request.
pub struct MultipartEntry<'a> {
    /// The name of the entry.
    pub name: String,
    /// The name of the uploaded file, if applicable.
    pub filename: Option<String>,
    /// Other headers of the entry.
    pub headers: Headers,
    /// The data of the entry.
    pub data: &'a [u8],
}

/// Errors that can occur when parsing a multipart request.
#[derive(Debug)]
pub enum MultipartError {
    /// The request is not a multipart request.
    InvalidContentType,
    /// The request is a multipart request, no boundary is defined.
    InvalidBoundary,
    /// The request is a multipart request, but the boundary is missing.
    InvalidData,
    /// An entry is invalid.
    InvalidEntry,
}

impl<'a> MultipartData<'a> {
    /// Get an entry by name, returns `None` if the entry does not exist.
    pub fn get(&self, name: impl AsRef<str>) -> Option<&MultipartEntry> {
        self.entries.iter().find(|x| x.name == name.as_ref())
    }

    /// Gets a mutable reference to an entry by name, returns `None` if the entry does not exist.
    pub fn get_mut(&'a mut self, name: impl AsRef<str>) -> Option<&mut MultipartEntry> {
        self.entries.iter_mut().find(|x| x.name == name.as_ref())
    }
}

impl<'a> Deref for MultipartData<'a> {
    type Target = Vec<MultipartEntry<'a>>;

    fn deref(&self) -> &Self::Target {
        &self.entries
    }
}

impl DerefMut for MultipartData<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.entries
    }
}

impl<'a> TryFrom<&'a Request> for MultipartData<'a> {
    type Error = MultipartError;

    fn try_from(req: &'a Request) -> Result<Self, Self::Error> {
        let content_type = req
            .headers
            .get_header("Content-Type")
            .ok_or(MultipartError::InvalidContentType)?
            .params();

        let body_type = &content_type.value;
        let boundary = content_type
            .get("boundary")
            .ok_or(MultipartError::InvalidBoundary)?;

        if *body_type != "multipart/form-data" {
            return Err(MultipartError::InvalidContentType);
        }

        let boundary = [b"--", boundary.as_bytes()].concat();
        let data = split_boundary(&req.body, &boundary);

        if data.len() < 3 {
            return Err(MultipartError::InvalidData);
        }

        let entries = data[1..data.len() - 1]
            .iter()
            .map(|entry| MultipartEntry::try_from(*entry))
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Self {
            content_type: body_type,
            entries,
        })
    }
}

impl<'a> TryFrom<&'a [u8]> for MultipartEntry<'a> {
    type Error = MultipartError;

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        // Split the headers from the data.
        let index = value
            .windows(4)
            .position(|x| x == b"\r\n\r\n")
            .ok_or(MultipartError::InvalidEntry)?
            + 4;

        if index > value.len() {
            return Err(MultipartError::InvalidEntry);
        }

        let (raw_headers, data) = value.split_at(index);

        let mut headers = Vec::new();

        for i in raw_headers
            .lines()
            .map(|x| x.unwrap())
            .filter(|x| !x.is_empty())
        {
            let header = Header::from_string(i)
                .ok()
                .ok_or(MultipartError::InvalidEntry)?;
            headers.push(header);
        }

        let headers = Headers(headers);
        let content = headers
            .get_header("Content-Disposition")
            .ok_or(MultipartError::InvalidEntry)?;
        let content_params = content.params();

        Ok(Self {
            name: content_params
                .get("name")
                .ok_or(MultipartError::InvalidEntry)?
                .strip_prefix('"')
                .and_then(|x| x.strip_suffix('"'))
                .ok_or(MultipartError::InvalidEntry)?
                .to_string(),
            filename: content_params.get("filename").map(|x| x.to_string()),
            headers,
            data,
        })
    }
}

fn split_boundary<'a>(data: &'a [u8], boundary: &[u8]) -> Vec<&'a [u8]> {
    let indexes = data
        .windows(boundary.len())
        .enumerate()
        .filter(|(_, x)| x == &boundary)
        .map(|(i, _)| (i, i + boundary.len()))
        .collect::<Vec<_>>();

    let mut out = Vec::with_capacity(indexes.len() + 1);
    let mut start = 0;

    for (s, e) in indexes {
        out.push(&data[start..s]);
        start = e;
    }

    out.push(&data[start..]);
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_split_boundary() {
        let data = b"gomangogotomato";
        let boundary = b"go";
        let out = split_boundary(data, boundary);

        assert_eq!(out.len(), 4);
        assert_eq!(out[0], b"");
        assert_eq!(out[1], b"man");
        assert_eq!(out[2], b"");
        assert_eq!(out[3], b"tomato");
    }
}