wasmtime_wasi_http/ctx.rs
1/// Default maximum size for the contents of a fields resource.
2///
3/// Typically, HTTP proxies limit headers to 8k. This number is higher than that
4/// because it not only includes the wire-size of headers but it additionally
5/// includes factors for the in-memory representation of `HeaderMap`. This is in
6/// theory high enough that no one runs into it but low enough such that a
7/// completely full `HeaderMap` doesn't break the bank in terms of memory
8/// consumption.
9const DEFAULT_FIELD_SIZE_LIMIT: usize = 128 * 1024;
10
11/// Capture the state necessary for use in the wasi-http API implementation.
12#[derive(Debug, Clone)]
13pub struct WasiHttpCtx {
14 pub(crate) field_size_limit: usize,
15}
16
17impl WasiHttpCtx {
18 /// Create a new context.
19 pub fn new() -> Self {
20 Self {
21 field_size_limit: DEFAULT_FIELD_SIZE_LIMIT,
22 }
23 }
24
25 /// Set the maximum size for any fields resources created by this context.
26 ///
27 /// The limit specified here is roughly a byte limit for the size of the
28 /// in-memory representation of headers. This means that the limit needs to
29 /// be larger than the literal representation of headers on the wire to
30 /// account for in-memory Rust-side data structures representing the header
31 /// names/values/etc.
32 pub fn set_field_size_limit(&mut self, limit: usize) {
33 self.field_size_limit = limit;
34 }
35}
36
37impl Default for WasiHttpCtx {
38 fn default() -> Self {
39 Self::new()
40 }
41}