Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ jobs:
- name: wasmtime-wasi-http
checks: |
-p wasmtime-wasi-http --no-default-features
-p wasmtime-wasi-http --no-default-features --features p2
-p wasmtime-wasi-http --no-default-features --features p3
-p wasmtime-wasi-http --no-default-features --features p3 --all-targets
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ serve = [
"dep:http-body-util",
"dep:http",
"wasmtime-cli-flags/async",
"wasmtime-wasi-http?/p2",
]
explore = ["dep:wasmtime-explorer", "dep:tempfile"]
wast = ["dep:wasmtime-wast"]
Expand All @@ -589,6 +590,7 @@ run = [
"dep:wasi-common",
"dep:tokio",
"wasmtime-cli-flags/async",
"wasmtime-wasi-http?/p2",
]
completion = ["dep:clap_complete"]
objdump = [
Expand Down
3 changes: 2 additions & 1 deletion crates/wasi-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ workspace = true
all-features = true

[features]
default = ["default-send-request"]
default = ["default-send-request", "p2"]
default-send-request = ["dep:tokio-rustls", "dep:rustls", "dep:webpki-roots"]
p2 = ["wasmtime-wasi/p2"]
p3 = ["wasmtime-wasi/p3", "dep:tokio-util"]
component-model-async = ["futures/alloc", "wasmtime/component-model-async"]

Expand Down
41 changes: 41 additions & 0 deletions crates/wasi-http/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Default maximum size for the contents of a fields resource.
///
/// Typically, HTTP proxies limit headers to 8k. This number is higher than that
/// because it not only includes the wire-size of headers but it additionally
/// includes factors for the in-memory representation of `HeaderMap`. This is in
/// theory high enough that no one runs into it but low enough such that a
/// completely full `HeaderMap` doesn't break the bank in terms of memory
/// consumption.
const DEFAULT_FIELD_SIZE_LIMIT: usize = 128 * 1024;

/// Capture the state necessary for use in the wasi-http API implementation.
#[derive(Debug, Clone)]
pub struct WasiHttpCtx {
pub(crate) field_size_limit: usize,
}

impl WasiHttpCtx {
/// Create a new context.
pub fn new() -> Self {
Self {
field_size_limit: DEFAULT_FIELD_SIZE_LIMIT,
}
}

/// Set the maximum size for any fields resources created by this context.
///
/// The limit specified here is roughly a byte limit for the size of the
/// in-memory representation of headers. This means that the limit needs to
/// be larger than the literal representation of headers on the wire to
/// account for in-memory Rust-side data structures representing the header
/// names/values/etc.
pub fn set_field_size_limit(&mut self, limit: usize) {
self.field_size_limit = limit;
}
}

impl Default for WasiHttpCtx {
fn default() -> Self {
Self::new()
}
}
6 changes: 5 additions & 1 deletion crates/wasi-http/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use wasmtime::{Result, Store, StoreContextMut, format_err};

/// Alternative p2 bindings generated with `exports: { default: async | store }`
/// so we can use `TypedFunc::call_concurrent` with both p2 and p3 instances.
#[cfg(feature = "p2")]
pub mod p2 {
#[expect(missing_docs, reason = "bindgen-generated code")]
pub mod bindings {
Expand All @@ -35,7 +36,7 @@ pub mod p2 {
require_store_data_send: true,
with: {
// http is in this crate
"wasi:http": crate::bindings::http,
"wasi:http": crate::p2::bindings::http,
// Upstream package dependencies
"wasi:io": wasmtime_wasi::p2::bindings::io,
}
Expand All @@ -49,6 +50,7 @@ pub mod p2 {
/// `wasi:http/handler@0.3.x` pre-instance.
pub enum ProxyPre<T: 'static> {
/// A `wasi:http/incoming-handler@0.2.x` pre-instance.
#[cfg(feature = "p2")]
P2(p2::bindings::ProxyPre<T>),
/// A `wasi:http/handler@0.3.x` pre-instance.
#[cfg(feature = "p3")]
Expand All @@ -61,6 +63,7 @@ impl<T: 'static> ProxyPre<T> {
T: Send,
{
Ok(match self {
#[cfg(feature = "p2")]
Self::P2(pre) => Proxy::P2(pre.instantiate_async(store).await?),
#[cfg(feature = "p3")]
Self::P3(pre) => Proxy::P3(pre.instantiate_async(store).await?),
Expand All @@ -72,6 +75,7 @@ impl<T: 'static> ProxyPre<T> {
/// `wasi:http/handler@0.3.x` instance.
pub enum Proxy {
/// A `wasi:http/incoming-handler@0.2.x` instance.
#[cfg(feature = "p2")]
P2(p2::bindings::Proxy),
/// A `wasi:http/handler@0.3.x` instance.
#[cfg(feature = "p3")]
Expand Down
Loading