actix-form-data 0.7.0-rc.3

Multipart Form Data for Actix Web
Documentation
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * This file is part of Actix Form Data.
 *
 * Copyright © 2026 asonix
 *
 * Actix Form Data is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Actix Form Data is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Actix Form Data.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::{
    error::{Error, ErrorKind, ResultExt},
    types::{
        ContentDisposition, FieldTerminator, FileFn, FileMeta, Form, MultipartContent,
        MultipartForm, MultipartHash, NamePart, Value,
    },
};
use actix_web::web::BytesMut;
use std::{collections::HashMap, path::Path, rc::Rc};
use streem::IntoStreamer;
use tokio::task::JoinSet;
use tracing::Instrument;

fn consolidate<T>(mf: MultipartForm<T>) -> Value<T> {
    mf.into_iter().fold(
        Value::Map(HashMap::new()),
        |mut acc, (mut nameparts, content)| {
            let start_value = Value::from(content);

            nameparts.reverse();
            let value = nameparts
                .into_iter()
                .fold(start_value, |acc, namepart| match namepart {
                    NamePart::Map(name) => {
                        let mut hm = HashMap::new();

                        hm.insert(name, acc);

                        Value::Map(hm)
                    }
                    NamePart::Array => Value::Array(vec![acc]),
                });

            acc.merge(value);
            acc
        },
    )
}

fn parse_multipart_name(name: String) -> Result<Vec<NamePart>, Error> {
    name.split('[')
        .map(|part| {
            if part.len() == 1 && part.ends_with(']') {
                NamePart::Array
            } else if part.ends_with(']') {
                NamePart::Map(part.trim_end_matches(']').to_owned())
            } else {
                NamePart::Map(part.to_owned())
            }
        })
        .try_fold(vec![], |mut v, part| {
            if v.is_empty() && !part.is_map() {
                return Err(Error::new(ErrorKind::ContentDisposition));
            }

            v.push(part);
            Ok(v)
        })
}

fn parse_content_disposition(field: &actix_multipart::Field) -> ContentDisposition {
    let content_disposition = field.content_disposition();

    ContentDisposition {
        name: content_disposition
            .and_then(|cd| cd.get_name())
            .map(|v| v.to_string()),
        filename: content_disposition
            .and_then(|cd| cd.get_filename())
            .map(|v| v.to_string()),
    }
}

async fn handle_file_upload<T, E>(
    field: actix_multipart::Field,
    filename: Option<String>,
    form: &Form<T, E>,
    file_fn: &FileFn<T, E>,
) -> Result<Result<MultipartContent<T>, E>, Error>
where
    T: 'static,
    E: 'static,
{
    let filename = filename.ok_or_else(|| Error::new(ErrorKind::Filename))?;
    let path: &Path = filename.as_ref();

    let filename = path.file_name().and_then(|filename| filename.to_str());

    let filename = filename
        .ok_or_else(|| Error::new(ErrorKind::Filename))?
        .to_owned();

    let content_type = field.content_type().cloned();

    let max_file_size = form.max_file_size;

    let field_stream = streem::try_from_fn(move |yielder| async move {
        let mut file_size = 0;

        let mut stream = field.into_streamer();

        while let Some(bytes) = stream.try_next().await? {
            tracing::trace!("Bytes from field");

            file_size += bytes.len();

            if file_size > max_file_size {
                drop(bytes);

                while stream.try_next().await?.is_some() {
                    tracing::trace!("Dropping oversized bytes");
                }

                return Err(Error::new(ErrorKind::FileSize));
            }

            yielder.yield_ok(bytes).await;
        }

        tracing::debug!("Finished consuming field");

        Ok(())
    });

    let result = file_fn(
        filename.clone(),
        content_type.clone(),
        Box::pin(field_stream),
    )
    .await;

    match result {
        Ok(result) => Ok(Ok(MultipartContent::File(FileMeta {
            filename,
            content_type,
            result,
        }))),
        Err(e) => Ok(Err(e)),
    }
}

async fn handle_form_data<'a, T, E>(
    field: actix_multipart::Field,
    term: FieldTerminator<'a, T, E>,
    form: &Form<T, E>,
) -> Result<MultipartContent<T>, Error>
where
    T: 'static,
    E: 'static,
{
    tracing::trace!("In handle_form_data, term: {:?}", term);
    let mut buf = Vec::new();

    let mut stream = field.into_streamer();

    while let Some(bytes) = stream.try_next().await? {
        tracing::trace!("bytes from field");

        if buf.len() + bytes.len() > form.max_field_size {
            drop(buf);

            while stream.try_next().await?.is_some() {
                tracing::trace!("Dropping oversized bytes");
            }

            return Err(Error::new(ErrorKind::FieldSize));
        }

        buf.push(bytes);
    }

    let bytes = match buf.len() {
        0 => return Err(Error::new(ErrorKind::FieldSize)),
        1 => buf.pop().expect("contains an element"),
        _ => {
            let total_length = buf.iter().map(|b| b.len()).sum();

            let mut bytes = BytesMut::with_capacity(total_length);

            for b in buf {
                bytes.extend(b);
            }

            bytes.freeze()
        }
    };

    tracing::debug!("Finished consuming field");

    if let FieldTerminator::Bytes = term {
        return Ok(MultipartContent::Bytes(bytes));
    }

    let s = std::str::from_utf8(&bytes).or_raise(ErrorKind::ParseField)?;

    match term {
        FieldTerminator::Bytes | FieldTerminator::File(_) => Err(Error::new(ErrorKind::FieldType)),
        FieldTerminator::Text => Ok(MultipartContent::Text(String::from(s))),
        FieldTerminator::Float => s
            .parse()
            .or_raise(ErrorKind::ParseFloat)
            .map(MultipartContent::Float),
        FieldTerminator::Int => s
            .parse()
            .or_raise(ErrorKind::ParseInt)
            .map(MultipartContent::Int),
    }
}

async fn handle_stream_field<T, E>(
    field: actix_multipart::Field,
    form: Rc<Form<T, E>>,
) -> Result<Result<MultipartHash<T>, E>, Error>
where
    T: 'static,
    E: 'static,
{
    let content_disposition = parse_content_disposition(&field);

    let name = content_disposition
        .name
        .ok_or_else(|| Error::new(ErrorKind::Field))?;
    let name = parse_multipart_name(name)?;

    let term = form
        .valid_field(name.iter().collect())
        .ok_or_else(|| Error::new(ErrorKind::FieldType))?;

    let content = match term {
        FieldTerminator::File(file_fn) => {
            match handle_file_upload(field, content_disposition.filename, &form, file_fn).await? {
                Ok(content) => content,
                Err(e) => return Ok(Err(e)),
            }
        }
        term => handle_form_data(field, term, &form).await?,
    };

    Ok(Ok((name, content)))
}

/// Handle multipart streams from Actix Web
#[tracing::instrument(level = "TRACE", skip_all)]
pub async fn handle_multipart<T, E>(
    m: actix_multipart::Multipart,
    form: Rc<Form<T, E>>,
) -> Result<Result<Value<T>, E>, Error>
where
    T: 'static,
    E: 'static,
{
    let mut multipart_form = Vec::new();
    let mut file_count: u32 = 0;
    let mut field_count: u32 = 0;

    let mut set = JoinSet::new();

    let mut m = m.into_streamer();

    let mut error: Option<Error> = None;
    let mut provided_error: Option<E> = None;
    let mut is_closed = false;
    let mut stream_error = false;

    'outer: loop {
        tracing::trace!("multipart loop");

        if error.is_some() || provided_error.is_some() {
            set.abort_all();

            if !stream_error {
                while let Some(res) = m.next().await {
                    tracing::trace!("draining multipart field");

                    if let Ok(field) = res {
                        let mut stream = field.into_streamer();
                        while stream.next().await.is_some() {
                            tracing::trace!("Throwing away uploaded bytes, we have an error");
                        }
                    } else {
                        break;
                    }
                }
            }

            while set.join_next().await.is_some() {
                tracing::trace!("Throwing away joined result");
            }

            break 'outer;
        }

        tokio::select! {
            opt = m.next(), if !is_closed => {
                tracing::trace!("Selected stream");
                is_closed = opt.is_none();

                if let Some(res) = opt {
                    match res {
                        Ok(field) => {
                            set.spawn_local(handle_stream_field(field, Rc::clone(&form)).instrument(tracing::trace_span!("multipart-field")));
                        },
                        Err(e) => {
                            is_closed = true;
                            stream_error = true;
                            error = Some(e.into());
                            continue 'outer;
                        }
                    }
                }
            }
            opt = set.join_next(), if !set.is_empty() => {
                tracing::trace!("Selected set");
                if let Some(res) = opt {
                    let (name_parts, content) = match res {
                        Ok(Ok(Ok(tup))) => tup,
                        Ok(Ok(Err(e))) => {
                            provided_error = Some(e);
                            continue 'outer;
                        }
                        Ok(Err(e)) => {
                            error = Some(e);
                            continue 'outer;
                        },
                        Err(e) => {
                            error = Some(Error::new_with(ErrorKind::Panicked, e));
                            continue 'outer;
                        },
                    };

                    let (l, r) = match count(&content, file_count, field_count, &form) {
                        Ok(tup) => tup,
                        Err(e) => {
                            error = Some(e);
                            continue 'outer;
                        }
                    };

                    file_count = l;
                    field_count = r;

                    multipart_form.push((name_parts, content));
                }
            }
            else => {
                break 'outer;
            }
        }
    }

    tracing::debug!("Finished consuming multipart");

    if let Some(e) = provided_error {
        return Ok(Err(e));
    }

    if let Some(e) = error {
        return Err(e);
    }

    Ok(Ok(consolidate(multipart_form)))
}

fn count<T, E>(
    content: &MultipartContent<T>,
    mut file_count: u32,
    mut field_count: u32,
    form: &Form<T, E>,
) -> Result<(u32, u32), Error> {
    match content {
        MultipartContent::File(_) => {
            file_count += 1;
            if file_count > form.max_files {
                return Err(Error::new(ErrorKind::FileCount));
            }
        }
        _ => {
            field_count += 1;
            if field_count > form.max_fields {
                return Err(Error::new(ErrorKind::FieldCount));
            }
        }
    }

    Ok((file_count, field_count))
}