hyperlane_plugin_multipart/struct.rs
1use super::*;
2
3/// One validated uploaded file extracted from a `multipart/form-data` request.
4#[derive(Clone, Debug, Eq, Getter, PartialEq)]
5pub struct UploadedFile {
6 /// Form field name.
7 pub(crate) field_name: String,
8 /// Traversal-safe sanitized filename.
9 pub(crate) filename: String,
10 /// Effective MIME type (sniffed magic MIME when available, else declared `Content-Type`).
11 pub(crate) content_type: String,
12 /// Raw uploaded file bytes.
13 pub(crate) data: Vec<u8>,
14}
15
16/// Validated `multipart/form-data` payload attached to [`Context`] under [`ATTR_PARSED_MULTIPART`].
17#[derive(Clone, Debug, Default, Eq, Getter, PartialEq)]
18pub struct ParsedMultipart {
19 /// Non-file `(name, value)` UTF-8 text fields in arrival order.
20 pub(crate) fields: Vec<(String, String)>,
21 /// Validated uploaded files in arrival order.
22 pub(crate) files: Vec<UploadedFile>,
23}
24
25/// Configuration for [`MultipartPlugin`].
26#[derive(Clone, Data, Debug, Eq, PartialEq)]
27pub struct MultipartConfig {
28 /// Maximum total bytes of the multipart request body.
29 #[get(type(copy))]
30 pub(crate) max_total_bytes: usize,
31 /// Maximum total parts (fields + files).
32 #[get(type(copy))]
33 pub(crate) max_parts: usize,
34 /// Maximum uploaded file parts.
35 #[get(type(copy))]
36 pub(crate) max_files: usize,
37 /// Maximum bytes per uploaded file.
38 #[get(type(copy))]
39 pub(crate) max_file_bytes: usize,
40 /// Maximum bytes per text field.
41 #[get(type(copy))]
42 pub(crate) max_field_bytes: usize,
43 /// Allowed MIME type prefixes for uploaded files (empty allows all MIME types).
44 pub(crate) allowed_mime_prefixes: Vec<String>,
45 /// When `true`, magic-byte sniffing overrides client-declared MIME when a known signature matches.
46 #[get(type(copy))]
47 pub(crate) sniff_mime: bool,
48}
49
50/// Compiled bounded multipart parser & guard backing [`MultipartHook`].
51#[derive(Clone, Debug)]
52pub struct MultipartPlugin {
53 /// Retained configuration.
54 pub(crate) config: MultipartConfig,
55 /// Lowercased allowed MIME prefixes.
56 pub(crate) allowed_mime_lower: Box<[String]>,
57 /// Prebuilt `400 Bad Request` wire response.
58 pub(crate) bad_request_wire: Vec<u8>,
59 /// Prebuilt `413 Payload Too Large` wire response.
60 pub(crate) too_large_wire: Vec<u8>,
61 /// Prebuilt `415 Unsupported Media Type` wire response.
62 pub(crate) disallowed_mime_wire: Vec<u8>,
63}
64
65/// Opt-in request middleware validating and parsing `multipart/form-data` uploads into [`Context`].
66#[derive(Clone, Copy, Debug, Default)]
67#[hyperlane::request_middleware(-76)]
68pub struct MultipartHook;