weida_http_codec/limits.rs
1//! The caller's bounds, passed to every decode, and the numbers they default
2//! to.
3//!
4//! HTTP sets no limit of its own on a head, a field count or a chunk line
5//! (RFC 9110 §5.4 lets a recipient refuse with 431 and says nothing about
6//! where): every bound is the recipient's choice. So each is an **argument**,
7//! carried by value in [`Limits`], and each is checked from what the peer
8//! declared or from how far the decoder has searched, before anything is
9//! reserved for it.
10//!
11//! Four numbers, because a peer has four ways to make a decoder commit memory
12//! or time: a head (or a compressed field section) that never ends or expands
13//! without bound, a field section of unboundedly many tiny fields, a chunk-size
14//! line that never ends, and a frame whose length prefix announces megabytes.
15
16/// A starting value for [`Limits::max_head_bytes`]: 64 KiB.
17///
18/// A **named constant, not a cap**: the cap is the field, set by the caller.
19/// 64 KiB is four times the HTTP/2 default frame payload and well above what
20/// browsers send (cookies included); servers commonly refuse at 8 to 64 KiB.
21pub const DEFAULT_MAX_HEAD_BYTES: usize = 64 * 1024;
22
23/// A starting value for [`Limits::max_fields`]: 128.
24///
25/// A **named constant, not a cap**. `a:\r\n` is four octets, so a 64 KiB head
26/// can carry sixteen thousand fields, each costing a vector slot; 128 is an
27/// order of magnitude above what a browser sends.
28pub const DEFAULT_MAX_FIELDS: usize = 128;
29
30/// A starting value for [`Limits::max_chunk_line`]: 1 KiB.
31///
32/// A **named constant, not a cap**. A chunk-size line is sixteen hex digits at
33/// most; the rest is chunk extensions, which nobody uses and which this codec
34/// steps over, so the bound only has to be generous to them.
35pub const DEFAULT_MAX_CHUNK_LINE: usize = 1024;
36
37/// A starting value for [`Limits::max_frame_size`]: 16 384, the HTTP/2
38/// default and minimum of `SETTINGS_MAX_FRAME_SIZE` (RFC 9113 §6.5.2).
39///
40/// A **named constant, not a cap**.
41pub const DEFAULT_MAX_FRAME_SIZE: u32 = 16_384;
42
43/// Bounds a decoder applies to remote input.
44///
45/// Every decode entry point in this crate takes one, by value. There is no way
46/// to switch a bound off; `usize::MAX` still cannot make a decoder read past
47/// its input, but it lets a peer's declaration through, which is exactly what
48/// the type exists to stop.
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub struct Limits {
51 /// The largest head, in octets.
52 ///
53 /// An HTTP/1.1 request or status line with its field lines and the empty
54 /// line that ends them; a chunked body's trailer section; a decoded HTTP/2
55 /// or HTTP/3 field section counted as RFC 9113 §6.5.2 counts it (name plus
56 /// value plus 32 per field); and an HTTP/3 HEADERS frame payload.
57 pub max_head_bytes: usize,
58 /// The largest number of fields in one field section.
59 pub max_fields: usize,
60 /// The largest chunk-size line, chunk extensions and line ending included.
61 pub max_chunk_line: usize,
62 /// The largest HTTP/2 frame payload, and the largest HTTP/3 frame payload
63 /// other than DATA and HEADERS.
64 pub max_frame_size: u32,
65}
66
67impl Limits {
68 /// The defaults: [`DEFAULT_MAX_HEAD_BYTES`], [`DEFAULT_MAX_FIELDS`],
69 /// [`DEFAULT_MAX_CHUNK_LINE`] and [`DEFAULT_MAX_FRAME_SIZE`].
70 pub const DEFAULT: Self = Self {
71 max_head_bytes: DEFAULT_MAX_HEAD_BYTES,
72 max_fields: DEFAULT_MAX_FIELDS,
73 max_chunk_line: DEFAULT_MAX_CHUNK_LINE,
74 max_frame_size: DEFAULT_MAX_FRAME_SIZE,
75 };
76}
77
78impl Default for Limits {
79 fn default() -> Self {
80 Self::DEFAULT
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn the_defaults_are_the_documented_numbers() {
90 assert_eq!(Limits::default(), Limits::DEFAULT);
91 assert_eq!(Limits::DEFAULT.max_head_bytes, 65_536);
92 assert_eq!(Limits::DEFAULT.max_fields, 128);
93 assert_eq!(Limits::DEFAULT.max_chunk_line, 1024);
94 // RFC 9113 §6.5.2: the initial value of SETTINGS_MAX_FRAME_SIZE.
95 assert_eq!(Limits::DEFAULT.max_frame_size, 16_384);
96 }
97}