Skip to main content

Content

Trait Content 

Source
pub trait Content: Declaration {
    // Provided method
    fn body_size_limit(format: ContentFormat) -> Option<usize> { ... }
}
Expand description

Content an application declares that it accepts, with limits on everything in it. Derive it with #[derive(Content)] on a struct with named fields; it must not be implemented by hand, since what the derive implements is not public API.

Each field declares one form field or nested group, named by its identifier unless rename gives the name a form submits:

Field typeSubmitted as
StringText, which must be valid UTF-8.
Vec<u8>The submitted bytes, unchanged.
An integer, f32, f64, or boolText in the type’s strict syntax, such as -12, 1.5e3, or true.
A FieldValue, such as an enumeration with #[derive(FieldValue)]Text converted by its code.
Upload<H>A file, with the caller’s storage handle H.
A struct deriving ContentA nested group, whose fields are submitted as name[field].
Option<T>A field the content may omit.
Vec<T>, other than Vec<u8>An array, submitted as name[], or name[][field] for an array of groups.

Every field without Option is required. Names are matched exactly, and a name the declaration does not accept is a fatal error before its content is read.

Each field and the struct take #[content(...)] attributes, whose limits are constant usize expressions:

AttributeOnDefault
rename = "name"A fieldThe field’s identifier
size_limit = NA scalar or upload, or each element of an array64 KiB, or 128 MiB for an upload
count_limit = NAn array128
accept("image/png", "image/*"), matched against the media type the upload’s part declares, so that a part without one is refused even by */*An uploadAny upload
formats("multipart/form-data", "application/x-www-form-urlencoded")The structBoth
depth_limit = N, the components of a nameThe struct8
content_size_limit = N, the content of every field togetherThe struct256 MiB
part_count_limit = NThe struct128
header_size_limit = N, one part’s header sectionThe struct8 KiB
header_count_limit = N, the fields of one part’s header sectionThe struct64
preamble_size_limit = NThe struct64 KiB
epilogue_size_limit = NThe struct1 KiB
pair_count_limit = N, the pairs of a URL-encoded bodyThe struct1,024
url_encoded_size_limit = NThe struct2 MiB

A struct’s limits apply when it is the top level of a body, not when it is nested in another. Every upload in a declaration, nested groups included, must have the same handle type H, which ContentHandles names.

use base_web_content::{Content, ContentFormat, FieldValue, Upload};

#[derive(FieldValue)]
enum Plan {
    #[content(rename = "free")]
    Free,
    #[content(rename = "pro")]
    Pro,
}

#[derive(Content)]
struct Address {
    city: String,
    postcode: Option<String>,
}

#[derive(Content)]
#[content(formats("multipart/form-data"))]
struct Signup {
    #[content(size_limit = 256)]
    email: String,
    plan: Plan,
    address: Option<Address>,
    #[content(count_limit = 8)]
    interests: Vec<String>,
    #[content(accept("image/png", "image/jpeg"), size_limit = 4 * 1024 * 1024)]
    avatar: Option<Upload<std::path::PathBuf>>,
}

assert!(Signup::body_size_limit(ContentFormat::MultipartFormData).is_some());
assert_eq!(Signup::body_size_limit(ContentFormat::UrlEncoded), None);

Provided Methods§

Source

fn body_size_limit(format: ContentFormat) -> Option<usize>

The largest body in format the declaration could need, computed from its fields and limits, or None when it does not accept the format.

A server can refuse a request whose Content-Length exceeds it with 413 Content Too Large before reading the body. The parser enforces it too, as a running total, whatever the request declared.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§