topcoat-router 0.7.0

A modular, batteries-included Rust web framework for server-rendered apps.
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#![doc = include_str!("../../docs/content/multipart.md")]

use std::{
    pin::Pin,
    task::{Context, Poll},
};

use bytes::Bytes;
use futures_util::Stream;
use http::HeaderMap;
use http_body_util::{LengthLimitError, Limited};
use topcoat_core::{
    context::Cx,
    error::{Error, Result},
};

use crate::{
    Body, body_limit,
    error::{bad_request, content_too_large, internal_server_error},
    request::{FromRequest, OptionalFromRequest, content_type},
};

/// `multipart/form-data` request extractor, commonly used for file uploads.
///
/// Iterate the request's parts with
/// [`next_field`](Multipart::next_field); each [`Field`] exposes its metadata
/// ([`name`](Field::name), [`file_name`](Field::file_name),
/// [`content_type`](Field::content_type), [`headers`](Field::headers)) and its
/// data ([`bytes`](Field::bytes), [`text`](Field::text),
/// [`chunk`](Field::chunk)). A [`Field`] also implements [`Stream`], so its
/// chunks can be consumed with the usual stream combinators.
///
/// Wrap it in [`Option`] to make the body optional: the extractor yields
/// [`None`] when the request carries no `multipart/form-data` body.
///
/// The stream reads at most the request's body limit across all fields;
/// register a [`BodyLimit`](crate::BodyLimit) layer to raise it for an
/// upload route.
///
/// # Examples
///
/// ```rust
/// use topcoat::{
///     Result,
///     router::{content::multipart::Multipart, route},
/// };
///
/// #[route(POST "/api/upload")]
/// async fn upload(mut multipart: Multipart) -> Result<&'static str> {
///     while let Some(field) = multipart.next_field().await? {
///         let name = field.name().map(str::to_owned);
///         let data = field.bytes().await?;
///
///         println!("field `{name:?}` is {} bytes", data.len());
///     }
///
///     Ok("received")
/// }
/// ```
#[derive(Debug)]
#[must_use]
pub struct Multipart {
    inner: multer::Multipart<'static>,
}

impl FromRequest for Multipart {
    fn from_request(cx: &Cx, body: Body) -> impl Future<Output = Result<Self>> {
        core::future::ready(
            content_type(cx)
                .and_then(|content_type| multer::parse_boundary(content_type).ok())
                .map(|boundary| Self {
                    inner: multer::Multipart::new(limited(cx, body).into_data_stream(), boundary),
                })
                .ok_or_else(invalid_boundary),
        )
    }
}

impl OptionalFromRequest for Multipart {
    fn from_request(cx: &Cx, body: Body) -> impl Future<Output = Result<Option<Self>>> {
        let Some(content_type) = content_type(cx) else {
            return std::future::ready(Ok(None));
        };

        std::future::ready(match multer::parse_boundary(content_type) {
            Ok(boundary) => Ok(Some(Self {
                inner: multer::Multipart::new(limited(cx, body).into_data_stream(), boundary),
            })),
            Err(multer::Error::NoMultipart) => Ok(None),
            Err(_) => Err(invalid_boundary()),
        })
    }
}

impl Multipart {
    /// Yields the next [`Field`] if available.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the next field from the multipart stream
    /// fails; a body over the request's body limit is classified as
    /// `413 Content Too Large`, malformed requests as `400 Bad Request`, and
    /// other failures as `500 Internal Server Error`.
    pub async fn next_field(&mut self) -> Result<Option<Field<'_>>> {
        let field = self.inner.next_field().await.map_err(multipart_error)?;

        Ok(field.map(|inner| Field {
            inner,
            _multipart: self,
        }))
    }
}

/// A single field in a multipart stream.
#[derive(Debug)]
#[must_use]
pub struct Field<'a> {
    inner: multer::Field<'static>,
    // `multer` requires there to only be one live `multer::Field` at any point.
    // Borrowing the `Multipart` mutably enforces that statically.
    _multipart: &'a mut Multipart,
}

impl Field<'_> {
    /// The field name found in the `Content-Disposition` header.
    #[must_use]
    pub fn name(&self) -> Option<&str> {
        self.inner.name()
    }

    /// The file name found in the `Content-Disposition` header.
    #[must_use]
    pub fn file_name(&self) -> Option<&str> {
        self.inner.file_name()
    }

    /// The `Content-Type` of the field, if present.
    #[must_use]
    pub fn content_type(&self) -> Option<&str> {
        self.inner.content_type().map(std::convert::AsRef::as_ref)
    }

    /// The headers of the field as a [`HeaderMap`].
    #[must_use]
    pub fn headers(&self) -> &HeaderMap {
        self.inner.headers()
    }

    /// Reads the full data of the field into [`Bytes`].
    ///
    /// # Errors
    ///
    /// Returns an error if reading the field data fails; a body over the request's body
    /// limit is classified as `413 Content Too Large`, malformed requests as
    /// `400 Bad Request`, and other failures as `500 Internal Server Error`.
    pub async fn bytes(self) -> Result<Bytes> {
        self.inner.bytes().await.map_err(multipart_error)
    }

    /// Reads the full data of the field as text.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the field text fails; a body over the request's body
    /// limit is classified as `413 Content Too Large`, malformed requests as
    /// `400 Bad Request`, and other failures as `500 Internal Server Error`.
    pub async fn text(self) -> Result<String> {
        self.inner.text().await.map_err(multipart_error)
    }

    /// Streams a chunk of the field data, returning [`None`] once exhausted.
    ///
    /// This does the same thing as the [`Stream`] implementation.
    ///
    /// # Errors
    ///
    /// Returns an error if reading the next chunk fails; a body over the request's body
    /// limit is classified as `413 Content Too Large`, malformed requests as
    /// `400 Bad Request`, and other failures as `500 Internal Server Error`.
    pub async fn chunk(&mut self) -> Result<Option<Bytes>> {
        self.inner.chunk().await.map_err(multipart_error)
    }
}

impl Stream for Field<'_> {
    type Item = Result<Bytes>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.inner)
            .poll_next(cx)
            .map_err(multipart_error)
    }
}

/// Caps `body` at the request's [`body_limit`], so reading past the limit
/// fails with an error classified as `413 Content Too Large`.
fn limited(cx: &Cx, body: Body) -> Body {
    Body::new(Limited::new(body, body_limit(cx)))
}

fn invalid_boundary() -> Error {
    bad_request("invalid `boundary` for `multipart/form-data` request").into()
}

fn multipart_error(error: multer::Error) -> Error {
    if is_length_limit_error(&error) {
        content_too_large().into()
    } else if is_client_error(&error) {
        bad_request(error.to_string()).into()
    } else {
        internal_server_error(error).into()
    }
}

/// Returns whether the multipart stream failed because the request body
/// exceeded the request's body limit.
fn is_length_limit_error(error: &multer::Error) -> bool {
    match error {
        multer::Error::StreamReadFailed(error) => error.is::<LengthLimitError>(),
        _ => false,
    }
}

/// Classifies a `multer` error as a client-side (`400`) error when the request
/// body or multipart headers are malformed.
fn is_client_error(error: &multer::Error) -> bool {
    match error {
        multer::Error::UnknownField { .. }
        | multer::Error::IncompleteFieldData { .. }
        | multer::Error::IncompleteHeaders
        | multer::Error::ReadHeaderFailed(..)
        | multer::Error::DecodeHeaderName { .. }
        | multer::Error::DecodeContentType(..)
        | multer::Error::NoBoundary
        | multer::Error::DecodeHeaderValue { .. }
        | multer::Error::NoMultipart
        | multer::Error::IncompleteStream
        | multer::Error::FieldSizeExceeded { .. }
        | multer::Error::StreamSizeExceeded { .. } => true,
        multer::Error::StreamReadFailed(error) => error
            .downcast_ref::<multer::Error>()
            .is_some_and(is_client_error),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use http::{Request, header::CONTENT_TYPE};
    use topcoat_core::context::{Cx, CxTestBuilder};

    use super::*;
    use crate::{
        Body,
        body_limit::DEFAULT_BODY_LIMIT,
        error::{BadRequestError, ContentTooLargeError},
        request::{FromRequest, OptionalFromRequest},
    };

    const BOUNDARY: &str = "X-TOPCOAT-BOUNDARY";

    /// The `Content-Type` header value for a `multipart/form-data` request using
    /// [`BOUNDARY`].
    fn multipart_content_type() -> String {
        format!("multipart/form-data; boundary={BOUNDARY}")
    }

    /// Builds a `Cx` carrying request `Parts` with the given `Content-Type`
    /// header, or no header at all when `content_type` is [`None`].
    fn cx_with_content_type(content_type: Option<&str>) -> Cx {
        let mut builder = Request::builder();
        if let Some(content_type) = content_type {
            builder = builder.header(CONTENT_TYPE, content_type);
        }

        let (parts, ()) = builder.body(()).expect("request should build").into_parts();

        CxTestBuilder::new().request_context(parts).build()
    }

    /// A two-field multipart body: a plain text `greeting` field and an
    /// `upload` file field with a filename and content type.
    fn sample_body() -> String {
        format!(
            "--{BOUNDARY}\r\n\
             Content-Disposition: form-data; name=\"greeting\"\r\n\
             \r\n\
             hello\r\n\
             --{BOUNDARY}\r\n\
             Content-Disposition: form-data; name=\"upload\"; filename=\"hello.txt\"\r\n\
             Content-Type: text/plain\r\n\
             \r\n\
             file body\r\n\
             --{BOUNDARY}--\r\n"
        )
    }

    #[tokio::test]
    async fn from_request_reads_fields_and_metadata() {
        let cx = cx_with_content_type(Some(&multipart_content_type()));
        let mut multipart =
            <Multipart as FromRequest>::from_request(&cx, Body::from(sample_body()))
                .await
                .expect("valid multipart request");

        let greeting = multipart
            .next_field()
            .await
            .expect("reading the first field succeeds")
            .expect("a first field is present");
        assert_eq!(greeting.name(), Some("greeting"));
        assert_eq!(greeting.file_name(), None);
        assert_eq!(greeting.content_type(), None);
        assert_eq!(greeting.text().await.expect("field text"), "hello");

        let upload = multipart
            .next_field()
            .await
            .expect("reading the second field succeeds")
            .expect("a second field is present");
        assert_eq!(upload.name(), Some("upload"));
        assert_eq!(upload.file_name(), Some("hello.txt"));
        assert_eq!(upload.content_type(), Some("text/plain"));
        assert_eq!(
            upload
                .headers()
                .get(CONTENT_TYPE)
                .map(http::HeaderValue::as_bytes),
            Some(b"text/plain".as_slice())
        );
        assert_eq!(
            &upload.bytes().await.expect("field bytes")[..],
            b"file body"
        );

        assert!(
            multipart
                .next_field()
                .await
                .expect("reading past the end succeeds")
                .is_none(),
            "the stream is exhausted after both fields"
        );
    }

    #[tokio::test]
    async fn field_chunk_streams_field_data() {
        let cx = cx_with_content_type(Some(&multipart_content_type()));
        let mut multipart =
            <Multipart as FromRequest>::from_request(&cx, Body::from(sample_body()))
                .await
                .expect("valid multipart request");

        let mut field = multipart
            .next_field()
            .await
            .expect("reading the first field succeeds")
            .expect("a first field is present");

        let mut data = Vec::new();
        while let Some(chunk) = field.chunk().await.expect("reading a chunk succeeds") {
            data.extend_from_slice(&chunk);
        }
        assert_eq!(data, b"hello");
    }

    #[tokio::test]
    async fn from_request_without_content_type_is_bad_request() {
        let cx = cx_with_content_type(None);
        let error = <Multipart as FromRequest>::from_request(&cx, Body::empty())
            .await
            .expect_err("missing content type is rejected");

        assert!(error.downcast_ref::<BadRequestError>().is_some());
    }

    #[tokio::test]
    async fn from_request_with_non_multipart_content_type_is_bad_request() {
        let cx = cx_with_content_type(Some("application/json"));
        let error = <Multipart as FromRequest>::from_request(&cx, Body::empty())
            .await
            .expect_err("a non-multipart content type is rejected");

        assert!(error.downcast_ref::<BadRequestError>().is_some());
    }

    #[tokio::test]
    async fn optional_from_request_without_content_type_is_none() {
        let cx = cx_with_content_type(None);
        let multipart = <Multipart as OptionalFromRequest>::from_request(&cx, Body::empty())
            .await
            .expect("an absent content type is not an error");

        assert!(multipart.is_none());
    }

    #[tokio::test]
    async fn optional_from_request_with_non_multipart_content_type_is_none() {
        let cx = cx_with_content_type(Some("application/json"));
        let multipart = <Multipart as OptionalFromRequest>::from_request(&cx, Body::empty())
            .await
            .expect("a non-multipart content type is not an error");

        assert!(multipart.is_none());
    }

    #[tokio::test]
    async fn optional_from_request_with_multipart_content_type_is_some() {
        let cx = cx_with_content_type(Some(&multipart_content_type()));
        let multipart =
            <Multipart as OptionalFromRequest>::from_request(&cx, Body::from(sample_body()))
                .await
                .expect("a valid multipart request is not an error");

        let mut multipart = multipart.expect("a multipart payload is present");
        let field = multipart
            .next_field()
            .await
            .expect("reading the first field succeeds")
            .expect("a first field is present");
        assert_eq!(field.name(), Some("greeting"));
    }

    #[tokio::test]
    async fn optional_from_request_with_multipart_without_boundary_is_bad_request() {
        let cx = cx_with_content_type(Some("multipart/form-data"));
        let error = <Multipart as OptionalFromRequest>::from_request(&cx, Body::empty())
            .await
            .expect_err("a multipart content type without a boundary is rejected");

        assert!(error.downcast_ref::<BadRequestError>().is_some());
    }

    #[tokio::test]
    async fn truncated_field_data_is_bad_request() {
        // The headers are complete, so `next_field` yields the field, but the
        // data is never terminated by a boundary, so reading it fails with a
        // client error that maps to a `400`.
        let body = format!(
            "--{BOUNDARY}\r\n\
             Content-Disposition: form-data; name=\"greeting\"\r\n\
             \r\n\
             hello"
        );
        let cx = cx_with_content_type(Some(&multipart_content_type()));
        let mut multipart = <Multipart as FromRequest>::from_request(&cx, Body::from(body))
            .await
            .expect("valid multipart request");

        let field = multipart
            .next_field()
            .await
            .expect("reading the first field succeeds")
            .expect("a first field is present");
        let error = field
            .bytes()
            .await
            .expect_err("reading truncated field data fails");

        assert!(error.downcast_ref::<BadRequestError>().is_some());
    }

    #[tokio::test]
    async fn a_body_over_the_limit_is_content_too_large() {
        let data = "x".repeat(DEFAULT_BODY_LIMIT + 1);
        let body = format!(
            "--{BOUNDARY}\r\n\
             Content-Disposition: form-data; name=\"upload\"\r\n\
             \r\n\
             {data}\r\n\
             --{BOUNDARY}--\r\n"
        );
        let cx = cx_with_content_type(Some(&multipart_content_type()));
        let mut multipart = <Multipart as FromRequest>::from_request(&cx, Body::from(body))
            .await
            .expect("valid multipart request");

        let error = multipart
            .next_field()
            .await
            .expect_err("a body over the limit is rejected");

        assert!(error.downcast_ref::<ContentTooLargeError>().is_some());
    }

    #[test]
    fn malformed_body_errors_are_classified_as_client_errors() {
        assert!(is_client_error(&multer::Error::NoMultipart));
        assert!(is_client_error(&multer::Error::IncompleteStream));
        assert!(is_client_error(&multer::Error::IncompleteHeaders));
    }
}