Skip to main content

Limits

Struct Limits 

Source
#[non_exhaustive]
pub struct Limits {
Show 16 fields pub max_body_bytes: u64, pub max_fields: u32, pub max_name_bytes: u32, pub max_value_bytes: u32, pub max_text_bytes: u64, pub max_parts: u32, pub max_files: u32, pub max_file_bytes: u64, pub max_part_header_bytes: u32, pub max_part_headers: u16, pub max_filename_bytes: u32, pub max_preamble_bytes: u32, pub max_epilogue_bytes: u32, pub max_padding_bytes: u8, pub sniff_bytes: u32, pub max_token_bytes: u16,
}
Expand description

Every budget form-net enforces.

Each budget is checked before the bytes it governs are buffered, stored, or passed on. A limit of 0 means none allowed. A limit larger than the one containing it is capped by it (the effective value is the smaller), so a route sets only the limits it cares about.

FieldDefault
max_body_bytes10 MiB
max_fields1,000
max_name_bytes256
max_value_bytes1 MiB
max_text_bytes2 MiB
max_parts100
max_files20
max_file_bytes10 MiB
max_part_header_bytes8 KiB
max_part_headers8
max_filename_bytes1,024
max_preamble_bytes1 KiB
max_epilogue_bytes1 KiB
max_padding_bytes32
sniff_bytes0
max_token_bytes128

§Examples

use form_net::Limits;

// A login route: a small body, a few fields, no files.
const LOGIN: Limits = Limits::DEFAULT
    .with_max_body_bytes(4 * 1024)
    .with_max_fields(8)
    .with_max_files(0);
assert!(LOGIN.validate().is_ok());

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§max_body_bytes: u64

Every byte fed, both formats, preamble and epilogue included.

§max_fields: u32

urlencoded pairs; multipart text fields.

§max_name_bytes: u32

Raw (encoded) name length. Capped by max_body_bytes.

§max_value_bytes: u32

One urlencoded value or one multipart text field. Capped by max_text_bytes.

§max_text_bytes: u64

All text values of one body together. Capped by max_body_bytes.

§max_parts: u32

Multipart parts of either kind.

§max_files: u32

Multipart file parts.

§max_file_bytes: u64

One file part. Capped by max_body_bytes.

§max_part_header_bytes: u32

One part’s header block, CRLF CRLF included (at least 64).

§max_part_headers: u16

Header lines per part.

§max_filename_bytes: u32

A raw filename, or a decoded filename*. Capped by max_part_header_bytes.

§max_preamble_bytes: u32

Bytes before the first delimiter. Capped by max_body_bytes.

§max_epilogue_bytes: u32

Bytes after the close delimiter. Capped by max_body_bytes.

§max_padding_bytes: u8

Transport padding after a delimiter.

§sniff_bytes: u32

The sniff window per file part (0: off). Capped by max_file_bytes.

§max_token_bytes: u16

The captured anti-forgery token. Capped by max_value_bytes.

Implementations§

Source§

impl Limits

Source

pub const DEFAULT: Limits

The defaults in the table above.

Source

pub const fn with_max_body_bytes(self, value: u64) -> Self

Sets max_body_bytes.

Source

pub const fn with_max_fields(self, value: u32) -> Self

Sets max_fields.

Source

pub const fn with_max_name_bytes(self, value: u32) -> Self

Sets max_name_bytes.

Source

pub const fn with_max_value_bytes(self, value: u32) -> Self

Sets max_value_bytes.

Source

pub const fn with_max_text_bytes(self, value: u64) -> Self

Sets max_text_bytes.

Source

pub const fn with_max_parts(self, value: u32) -> Self

Sets max_parts.

Source

pub const fn with_max_files(self, value: u32) -> Self

Sets max_files.

Source

pub const fn with_max_file_bytes(self, value: u64) -> Self

Sets max_file_bytes.

Source

pub const fn with_max_part_header_bytes(self, value: u32) -> Self

Sets max_part_header_bytes.

Source

pub const fn with_max_part_headers(self, value: u16) -> Self

Sets max_part_headers.

Source

pub const fn with_max_filename_bytes(self, value: u32) -> Self

Sets max_filename_bytes.

Source

pub const fn with_max_preamble_bytes(self, value: u32) -> Self

Sets max_preamble_bytes.

Source

pub const fn with_max_epilogue_bytes(self, value: u32) -> Self

Sets max_epilogue_bytes.

Source

pub const fn with_max_padding_bytes(self, value: u8) -> Self

Sets max_padding_bytes.

Source

pub const fn with_sniff_bytes(self, value: u32) -> Self

Sets sniff_bytes.

Source

pub const fn with_max_token_bytes(self, value: u16) -> Self

Sets max_token_bytes.

Source

pub fn validate(&self) -> Result<(), FormError>

Refuses the values no body could satisfy, so a misconfiguration is found at startup: a max_part_header_bytes below 64 (the shortest useful Content-Disposition line must fit), or buffer sizes that do not fit in usize. Every parser constructor and every *_with function calls it.

§Errors

FormError::InvalidLimits naming the refused field.

§Examples
use form_net::Limits;

assert!(Limits::DEFAULT.validate().is_ok());
let broken = Limits::DEFAULT.with_max_part_header_bytes(16);
assert_eq!(broken.validate().unwrap_err().code(), "invalid_limits");
Source

pub const fn multipart_buffer_len(&self, policy: &Policy) -> usize

The buffer length the multipart parser needs with these limits and this policy: the header block, the decoded field name, the decoded filename* (only under ExtendedFilenamePolicy::AcceptAlone), the token (only with a token field), and 32 bytes for _charset_.

A const fn, so a stack buffer can be sized at compile time.

§Examples
use form_net::{Limits, Policy};

const LIMITS: Limits = Limits::DEFAULT.with_max_part_header_bytes(1024);
const LEN: usize = LIMITS.multipart_buffer_len(&Policy::DEFAULT);
assert_eq!(LEN, 1024 + 256 + 32);
let buffer = [0u8; LEN];
Source

pub const fn urlencoded_buffer_len(&self) -> usize

The scratch length the streaming urlencoded parser needs: max_name_bytes + max_value_bytes + 1 (effective values).

§Examples
use form_net::Limits;

const LIMITS: Limits = Limits::DEFAULT.with_max_text_bytes(1000);
assert_eq!(LIMITS.urlencoded_buffer_len(), 256 + 1000 + 1);

Trait Implementations§

Source§

impl Clone for Limits

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Limits

Source§

impl Debug for Limits

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Limits

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for Limits

Source§

impl PartialEq for Limits

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Limits

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.