axum_extra/response/multiple.rs
1//! Generate forms to use in responses.
2
3use axum::response::{IntoResponse, Response};
4use fastrand;
5use http::{header, HeaderMap, StatusCode};
6use mime::Mime;
7
8/// Create multipart forms to be used in API responses.
9///
10/// This struct implements [`IntoResponse`], and so it can be returned from a handler.
11#[derive(Debug)]
12pub struct MultipartForm {
13 parts: Vec<Part>,
14}
15
16impl MultipartForm {
17 /// Initialize a new multipart form with the provided vector of parts.
18 ///
19 /// # Examples
20 ///
21 /// ```rust
22 /// use axum_extra::response::multiple::{MultipartForm, Part};
23 ///
24 /// let parts: Vec<Part> = vec![Part::text("foo".to_string(), "abc"), Part::text("bar".to_string(), "def")];
25 /// let form = MultipartForm::with_parts(parts);
26 /// ```
27 pub fn with_parts(parts: Vec<Part>) -> Self {
28 MultipartForm { parts }
29 }
30}
31
32impl IntoResponse for MultipartForm {
33 fn into_response(self) -> Response {
34 // see RFC5758 for details
35 let boundary = generate_boundary();
36 let mut headers = HeaderMap::new();
37 let mime_type: Mime = match format!("multipart/form-data; boundary={boundary}").parse() {
38 Ok(m) => m,
39 // Realistically this should never happen unless the boundary generation code
40 // is modified, and that will be caught by unit tests
41 Err(_) => {
42 return (
43 StatusCode::INTERNAL_SERVER_ERROR,
44 "Invalid multipart boundary generated",
45 )
46 .into_response()
47 }
48 };
49 // The use of unwrap is safe here because mime types are inherently string representable
50 headers.insert(header::CONTENT_TYPE, mime_type.to_string().parse().unwrap());
51 let mut serialized_form: Vec<u8> = Vec::new();
52 for part in self.parts {
53 // for each part, the boundary is preceded by two dashes
54 serialized_form.extend_from_slice(format!("--{boundary}\r\n").as_bytes());
55 serialized_form.extend_from_slice(&part.serialize());
56 }
57 serialized_form.extend_from_slice(format!("--{boundary}--").as_bytes());
58 (headers, serialized_form).into_response()
59 }
60}
61
62// Valid settings for that header are: "base64", "quoted-printable", "8bit", "7bit", and "binary".
63/// A single part of a multipart form as defined by
64/// <https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4>
65/// and RFC5758.
66#[derive(Debug)]
67pub struct Part {
68 // Every part is expected to contain:
69 // - a [Content-Disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition
70 // header, where `Content-Disposition` is set to `form-data`, with a parameter of `name` that is set to
71 // the name of the field in the form. In the below example, the name of the field is `user`:
72 // ```
73 // Content-Disposition: form-data; name="user"
74 // ```
75 // If the field contains a file, then the `filename` parameter may be set to the name of the file.
76 // Handling for non-ascii field names is not done here, support for non-ascii characters may be encoded using
77 // methodology described in RFC 2047.
78 // - (optionally) a `Content-Type` header, which if not set, defaults to `text/plain`.
79 // If the field contains a file, then the file should be identified with that file's MIME type (eg: `image/gif`).
80 // If the `MIME` type is not known or specified, then the MIME type should be set to `application/octet-stream`.
81 /// The name of the part in question
82 name: String,
83 /// If the part should be treated as a file, the filename that should be attached that part
84 filename: Option<String>,
85 /// The `Content-Type` header. While not strictly required, it is always set here
86 mime_type: Mime,
87 /// The content/body of the part
88 contents: Vec<u8>,
89}
90
91impl Part {
92 /// Create a new part with `Content-Type` of `text/plain` with the supplied name and contents.
93 ///
94 /// This form will not have a defined file name.
95 ///
96 /// # Examples
97 ///
98 /// ```rust
99 /// use axum_extra::response::multiple::{MultipartForm, Part};
100 ///
101 /// // create a form with a single part that has a field with a name of "foo",
102 /// // and a value of "abc"
103 /// let parts: Vec<Part> = vec![Part::text("foo".to_string(), "abc")];
104 /// let form = MultipartForm::from_iter(parts);
105 /// ```
106 pub fn text(name: String, contents: &str) -> Self {
107 Self {
108 name,
109 filename: None,
110 mime_type: mime::TEXT_PLAIN_UTF_8,
111 contents: contents.as_bytes().to_vec(),
112 }
113 }
114
115 /// Create a new part containing a generic file, with a `Content-Type` of `application/octet-stream`
116 /// using the provided file name, field name, and contents.
117 ///
118 /// If the MIME type of the file is known, consider using `Part::raw_part`.
119 ///
120 /// # Examples
121 ///
122 /// ```rust
123 /// use axum_extra::response::multiple::{MultipartForm, Part};
124 ///
125 /// // create a form with a single part that has a field with a name of "foo",
126 /// // with a file name of "foo.txt", and with the specified contents
127 /// let parts: Vec<Part> = vec![Part::file("foo", "foo.txt", vec![0x68, 0x68, 0x20, 0x6d, 0x6f, 0x6d])];
128 /// let form = MultipartForm::from_iter(parts);
129 /// ```
130 pub fn file(field_name: &str, file_name: &str, contents: Vec<u8>) -> Self {
131 Self {
132 name: field_name.to_owned(),
133 filename: Some(file_name.to_owned()),
134 // If the `MIME` type is not known or specified, then the MIME type should be set to `application/octet-stream`.
135 // See RFC2388 section 3 for specifics.
136 mime_type: mime::APPLICATION_OCTET_STREAM,
137 contents,
138 }
139 }
140
141 /// Create a new part with more fine-grained control over the semantics of that part.
142 ///
143 /// The caller is assumed to have set a valid MIME type.
144 ///
145 /// This function will return an error if the provided MIME type is not valid.
146 ///
147 /// # Examples
148 ///
149 /// ```rust
150 /// use axum_extra::response::multiple::{MultipartForm, Part};
151 ///
152 /// // create a form with a single part that has a field with a name of "part_name",
153 /// // with a MIME type of "application/json", and the supplied contents.
154 /// let parts: Vec<Part> = vec![Part::raw_part("part_name", "application/json", vec![0x68, 0x68, 0x20, 0x6d, 0x6f, 0x6d], None).expect("MIME type must be valid")];
155 /// let form = MultipartForm::from_iter(parts);
156 /// ```
157 pub fn raw_part(
158 name: &str,
159 mime_type: &str,
160 contents: Vec<u8>,
161 filename: Option<&str>,
162 ) -> Result<Self, &'static str> {
163 let mime_type = mime_type.parse().map_err(|_| "Invalid MIME type")?;
164 Ok(Self {
165 name: name.to_owned(),
166 filename: filename.map(|f| f.to_owned()),
167 mime_type,
168 contents,
169 })
170 }
171
172 /// Serialize this part into a chunk that can be easily inserted into a larger form
173 pub(super) fn serialize(&self) -> Vec<u8> {
174 // A part is serialized in this general format:
175 // // the filename is optional
176 // Content-Disposition: form-data; name="FIELD_NAME"; filename="FILENAME"\r\n
177 // // the mime type (not strictly required by the spec, but always sent here)
178 // Content-Type: mime/type\r\n
179 // // a blank line, then the contents of the file start
180 // \r\n
181 // CONTENTS\r\n
182
183 // Format what we can as a string, then handle the rest at a byte level
184 let mut serialized_part = format!("Content-Disposition: form-data; name=\"{}\"", self.name);
185 // specify a filename if one was set
186 if let Some(filename) = &self.filename {
187 serialized_part += &format!("; filename=\"{filename}\"");
188 }
189 serialized_part += "\r\n";
190 // specify the MIME type
191 serialized_part += &format!("Content-Type: {}\r\n", self.mime_type);
192 serialized_part += "\r\n";
193 let mut part_bytes = serialized_part.as_bytes().to_vec();
194 part_bytes.extend_from_slice(&self.contents);
195 part_bytes.extend_from_slice(b"\r\n");
196
197 part_bytes
198 }
199}
200
201impl FromIterator<Part> for MultipartForm {
202 fn from_iter<T: IntoIterator<Item = Part>>(iter: T) -> Self {
203 Self {
204 parts: iter.into_iter().collect(),
205 }
206 }
207}
208
209/// A boundary is defined as a user defined (arbitrary) value that does not occur in any of the data.
210///
211/// Because the specification does not clearly define a methodology for generating boundaries, this implementation
212/// follow's Reqwest's, and generates a boundary in the format of `XXXXXXXX-XXXXXXXX-XXXXXXXX-XXXXXXXX` where `XXXXXXXX`
213/// is a hexadecimal representation of a pseudo randomly generated u64.
214fn generate_boundary() -> String {
215 let a = fastrand::u64(0..u64::MAX);
216 let b = fastrand::u64(0..u64::MAX);
217 let c = fastrand::u64(0..u64::MAX);
218 let d = fastrand::u64(0..u64::MAX);
219 format!("{a:016x}-{b:016x}-{c:016x}-{d:016x}")
220}
221
222#[cfg(test)]
223mod tests {
224 use super::{generate_boundary, MultipartForm, Part};
225 use axum::{body::Body, http};
226 use axum::{routing::get, Router};
227 use http::{Request, Response};
228 use http_body_util::BodyExt;
229 use mime::Mime;
230 use tower::ServiceExt;
231
232 #[tokio::test]
233 async fn process_form() -> Result<(), Box<dyn std::error::Error>> {
234 // create a boilerplate handle that returns a form
235 async fn handle() -> MultipartForm {
236 let parts: Vec<Part> = vec![
237 Part::text("part1".to_owned(), "basictext"),
238 Part::file(
239 "part2",
240 "file.txt",
241 vec![0x68, 0x69, 0x20, 0x6d, 0x6f, 0x6d],
242 ),
243 Part::raw_part("part3", "text/plain", b"rawpart".to_vec(), None).unwrap(),
244 ];
245 MultipartForm::from_iter(parts)
246 }
247
248 // make a request to that handle
249 let app = Router::new().route("/", get(handle));
250 let response: Response<_> = app
251 .oneshot(Request::builder().uri("/").body(Body::empty())?)
252 .await?;
253 // content_type header
254 let ct_header = response.headers().get("content-type").unwrap().to_str()?;
255 let boundary = ct_header.split("boundary=").nth(1).unwrap().to_owned();
256 let body: &[u8] = &response.into_body().collect().await?.to_bytes();
257 assert_eq!(
258 std::str::from_utf8(body)?,
259 format!(
260 "--{boundary}\r\n\
261 Content-Disposition: form-data; name=\"part1\"\r\n\
262 Content-Type: text/plain; charset=utf-8\r\n\
263 \r\n\
264 basictext\r\n\
265 --{boundary}\r\n\
266 Content-Disposition: form-data; name=\"part2\"; filename=\"file.txt\"\r\n\
267 Content-Type: application/octet-stream\r\n\
268 \r\n\
269 hi mom\r\n\
270 --{boundary}\r\n\
271 Content-Disposition: form-data; name=\"part3\"\r\n\
272 Content-Type: text/plain\r\n\
273 \r\n\
274 rawpart\r\n\
275 --{boundary}--",
276 )
277 );
278
279 Ok(())
280 }
281
282 #[test]
283 fn valid_boundary_generation() {
284 for _ in 0..256 {
285 let boundary = generate_boundary();
286 let mime_type: Result<Mime, _> =
287 format!("multipart/form-data; boundary={boundary}").parse();
288 assert!(
289 mime_type.is_ok(),
290 "The generated boundary was unable to be parsed into a valid mime type."
291 );
292 }
293 }
294}