Skip to main content

base_web_multipart/
multipart_limits.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4/// The structural limits a parser applies, so that a hostile body cannot make it buffer or count without bound.
5///
6/// Content is not limited here: a part's content passes through as it arrives, and limits on content, such as for form fields and uploads, belong to its consumer.
7///
8/// ```
9/// use base_web_multipart::MultipartLimits;
10///
11/// let limits = MultipartLimits { part_count_limit: 16, ..MultipartLimits::default() };
12/// assert_eq!(limits.header_size_limit, MultipartLimits::HEADER_SIZE_LIMIT);
13/// ```
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15pub struct MultipartLimits {
16    /// The bytes before the first boundary, which are ignored.
17    pub preamble_size_limit: usize,
18    /// The bytes of one part's header section, including its line breaks, and of any padding after the boundary before it. Padding after the closing boundary is limited too.
19    pub header_size_limit: usize,
20    /// The fields in one part's header section.
21    pub header_count_limit: usize,
22    /// The parts in the body.
23    pub part_count_limit: usize,
24}
25
26impl MultipartLimits {
27    /// The default preamble size limit: 64 KiB.
28    pub const PREAMBLE_SIZE_LIMIT: usize = 64 * 1024;
29    /// The default header size limit: 64 KiB.
30    pub const HEADER_SIZE_LIMIT: usize = 64 * 1024;
31    /// The default header count limit.
32    pub const HEADER_COUNT_LIMIT: usize = 64;
33    /// The default part count limit.
34    pub const PART_COUNT_LIMIT: usize = 128;
35}
36
37impl Default for MultipartLimits {
38    fn default() -> Self {
39        Self {
40            preamble_size_limit: Self::PREAMBLE_SIZE_LIMIT,
41            header_size_limit: Self::HEADER_SIZE_LIMIT,
42            header_count_limit: Self::HEADER_COUNT_LIMIT,
43            part_count_limit: Self::PART_COUNT_LIMIT,
44        }
45    }
46}