Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

wasi-http: use UnsyncBoxBody to support axum (#11941)

Jelle van den Hooff8e22ff89f6af
diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rs
index 1d74ffb3f9..2dec765c45 100644
--- a/crates/wasi-http/src/p3/body.rs
+++ b/crates/wasi-http/src/p3/body.rs
@@ -8,7 +8,7 @@ use core::pin::Pin;
use core::task::{Context, Poll, ready};
use http::HeaderMap;
use http_body::Body as _;
-use http_body_util::combinators::BoxBody;
+use http_body_util::combinators::UnsyncBoxBody;
use std::any::{Any, TypeId};
use std::io::Cursor;
use std::sync::Arc;
@@ -34,7 +34,7 @@ pub(crate) enum Body {
/// Body constructed by the host.
Host {
/// The [`http_body::Body`]
- body: BoxBody<Bytes, ErrorCode>,
+ body: UnsyncBoxBody<Bytes, ErrorCode>,
/// Channel, on which transmission result will be written
result_tx: oneshot::Sender<Box<dyn Future<Output = Result<(), ErrorCode>> + Send>>,
},
@@ -441,7 +441,7 @@ where
/// [StreamProducer] implementation for bodies originating in the host.
pub(crate) struct HostBodyStreamProducer<T> {
- pub(crate) body: BoxBody<Bytes, ErrorCode>,
+ pub(crate) body: UnsyncBoxBody<Bytes, ErrorCode>,
trailers: Option<oneshot::Sender<Result<Option<Resource<Trailers>>, ErrorCode>>>,
getter: fn(&mut T) -> WasiHttpCtxView<'_>,
}
diff --git a/crates/wasi-http/src/p3/host/handler.rs b/crates/wasi-http/src/p3/host/handler.rs
index f3706b4713..7918d8e333 100644
--- a/crates/wasi-http/src/p3/host/handler.rs
+++ b/crates/wasi-http/src/p3/host/handler.rs
@@ -240,7 +240,7 @@ impl HostWithStore for WasiHttp {
getter,
)
.with_state(io_task_rx)
- .boxed(),
+ .boxed_unsync(),
Body::Host { body, result_tx } => {
if let Some(limit) = content_length {
let (http_result_tx, http_result_rx) = oneshot::channel();
@@ -256,10 +256,10 @@ impl HostWithStore for WasiHttp {
ErrorCode::HttpRequestBodySize,
)
.with_state(io_task_rx)
- .boxed()
+ .boxed_unsync()
} else {
_ = result_tx.send(Box::new(io_task_result(io_result_rx)));
- body.with_state(io_task_rx).boxed()
+ body.with_state(io_task_rx).boxed_unsync()
}
}
};
@@ -331,7 +331,7 @@ impl HostWithStore for WasiHttp {
let io = Arc::new(AbortOnDropJoinHandle(io));
_ = io_result_tx.send((Arc::clone(&io), rx));
_ = io_task_tx.send(Arc::clone(&io));
- body.with_state(io).boxed()
+ body.with_state(io).boxed_unsync()
}
};
let res = Response {
diff --git a/crates/wasi-http/src/p3/mod.rs b/crates/wasi-http/src/p3/mod.rs
index 88c6a58cd3..352fdd8ee9 100644
--- a/crates/wasi-http/src/p3/mod.rs
+++ b/crates/wasi-http/src/p3/mod.rs
@@ -28,7 +28,7 @@ use bytes::Bytes;
use core::ops::Deref;
use http::HeaderName;
use http::uri::Scheme;
-use http_body_util::combinators::BoxBody;
+use http_body_util::combinators::UnsyncBoxBody;
use std::sync::Arc;
use wasmtime::component::{HasData, Linker, ResourceTable};
use wasmtime_wasi::TrappableError;
@@ -95,13 +95,13 @@ pub trait WasiHttpCtx: Send {
#[cfg(feature = "default-send-request")]
fn send_request(
&mut self,
- request: http::Request<BoxBody<Bytes, ErrorCode>>,
+ request: http::Request<UnsyncBoxBody<Bytes, ErrorCode>>,
options: Option<RequestOptions>,
fut: Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
) -> Box<
dyn Future<
Output = HttpResult<(
- http::Response<BoxBody<Bytes, ErrorCode>>,
+ http::Response<UnsyncBoxBody<Bytes, ErrorCode>>,
Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
)>,
> + Send,
@@ -112,7 +112,7 @@ pub trait WasiHttpCtx: Send {
let (res, io) = default_send_request(request, options).await?;
Ok((
- res.map(BodyExt::boxed),
+ res.map(BodyExt::boxed_unsync),
Box::new(io) as Box<dyn Future<Output = _> + Send>,
))
})
@@ -137,13 +137,13 @@ pub trait WasiHttpCtx: Send {
#[cfg(not(feature = "default-send-request"))]
fn send_request(
&mut self,
- request: http::Request<BoxBody<Bytes, ErrorCode>>,
+ request: http::Request<UnsyncBoxBody<Bytes, ErrorCode>>,
options: Option<RequestOptions>,
fut: Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
) -> Box<
dyn Future<
Output = HttpResult<(
- http::Response<BoxBody<Bytes, ErrorCode>>,
+ http::Response<UnsyncBoxBody<Bytes, ErrorCode>>,
Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
)>,
> + Send,
diff --git a/crates/wasi-http/src/p3/request.rs b/crates/wasi-http/src/p3/request.rs
index 75164922db..89d1ceb81a 100644
--- a/crates/wasi-http/src/p3/request.rs
+++ b/crates/wasi-http/src/p3/request.rs
@@ -5,7 +5,7 @@ use core::time::Duration;
use http::uri::{Authority, PathAndQuery, Scheme};
use http::{HeaderMap, Method};
use http_body_util::BodyExt as _;
-use http_body_util::combinators::BoxBody;
+use http_body_util::combinators::UnsyncBoxBody;
use std::sync::Arc;
use tokio::sync::oneshot;
@@ -52,7 +52,7 @@ impl Request {
path_with_query: Option<PathAndQuery>,
headers: impl Into<Arc<HeaderMap>>,
options: Option<Arc<RequestOptions>>,
- body: impl Into<BoxBody<Bytes, ErrorCode>>,
+ body: impl Into<UnsyncBoxBody<Bytes, ErrorCode>>,
) -> (
Self,
impl Future<Output = Result<(), ErrorCode>> + Send + 'static,
@@ -91,7 +91,7 @@ impl Request {
impl Future<Output = Result<(), ErrorCode>> + Send + 'static,
)
where
- T: http_body::Body<Data = Bytes> + Send + Sync + 'static,
+ T: http_body::Body<Data = Bytes> + Send + 'static,
T::Error: Into<ErrorCode>,
{
let (
@@ -116,7 +116,7 @@ impl Request {
path_and_query,
headers,
None,
- body.map_err(Into::into).boxed(),
+ body.map_err(Into::into).boxed_unsync(),
)
}
}
diff --git a/crates/wasi-http/src/p3/response.rs b/crates/wasi-http/src/p3/response.rs
index 4f3ec7953d..2585cc3f3a 100644
--- a/crates/wasi-http/src/p3/response.rs
+++ b/crates/wasi-http/src/p3/response.rs
@@ -6,7 +6,7 @@ use anyhow::Context as _;
use bytes::Bytes;
use http::{HeaderMap, StatusCode};
use http_body_util::BodyExt as _;
-use http_body_util::combinators::BoxBody;
+use http_body_util::combinators::UnsyncBoxBody;
use std::sync::Arc;
use wasmtime::AsContextMut;
@@ -47,7 +47,7 @@ impl Response {
self,
store: impl AsContextMut<Data = T>,
fut: impl Future<Output = Result<(), ErrorCode>> + Send + 'static,
- ) -> wasmtime::Result<http::Response<BoxBody<Bytes, ErrorCode>>> {
+ ) -> wasmtime::Result<http::Response<UnsyncBoxBody<Bytes, ErrorCode>>> {
self.into_http_with_getter(store, fut, T::http)
}
@@ -58,7 +58,7 @@ impl Response {
store: impl AsContextMut<Data = T>,
fut: impl Future<Output = Result<(), ErrorCode>> + Send + 'static,
getter: fn(&mut T) -> WasiHttpCtxView<'_>,
- ) -> wasmtime::Result<http::Response<BoxBody<Bytes, ErrorCode>>> {
+ ) -> wasmtime::Result<http::Response<UnsyncBoxBody<Bytes, ErrorCode>>> {
let res = http::Response::try_from(self)?;
let (res, body) = res.into_parts();
let body = match body {
@@ -80,7 +80,7 @@ impl Response {
ErrorCode::HttpResponseBodySize,
getter,
)
- .boxed()
+ .boxed_unsync()
}
Body::Host { body, result_tx } => {
_ = result_tx.send(Box::new(fut));
diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rs
index 279d928c46..41fc350071 100644
--- a/crates/wasi-http/tests/all/p3/mod.rs
+++ b/crates/wasi-http/tests/all/p3/mod.rs
@@ -8,7 +8,7 @@ use futures::SinkExt;
use futures::channel::oneshot;
use http::HeaderValue;
use http_body::Body;
-use http_body_util::{BodyExt as _, Collected, Empty, combinators::BoxBody};
+use http_body_util::{BodyExt as _, Collected, Empty, combinators::UnsyncBoxBody};
use std::io::Write;
use std::path::Path;
use test_programs_artifacts::*;
@@ -29,7 +29,7 @@ use wasmtime_wasi_http::types::DEFAULT_FORBIDDEN_HEADERS;
foreach_p3_http!(assert_test_exists);
struct TestHttpCtx {
- request_body_tx: Option<oneshot::Sender<BoxBody<Bytes, ErrorCode>>>,
+ request_body_tx: Option<oneshot::Sender<UnsyncBoxBody<Bytes, ErrorCode>>>,
}
impl WasiHttpCtx for TestHttpCtx {
@@ -39,14 +39,14 @@ impl WasiHttpCtx for TestHttpCtx {
fn send_request(
&mut self,
- request: http::Request<BoxBody<Bytes, ErrorCode>>,
+ request: http::Request<UnsyncBoxBody<Bytes, ErrorCode>>,
options: Option<RequestOptions>,
fut: Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
) -> Box<
dyn Future<
Output = Result<
(
- http::Response<BoxBody<Bytes, ErrorCode>>,
+ http::Response<UnsyncBoxBody<Bytes, ErrorCode>>,
Box<dyn Future<Output = Result<(), ErrorCode>> + Send>,
),
TrappableError<ErrorCode>,
@@ -72,7 +72,7 @@ impl WasiHttpCtx for TestHttpCtx {
let (res, io) = p3::default_send_request(request, options).await?;
Ok((
- res.map(BodyExt::boxed),
+ res.map(BodyExt::boxed_unsync),
Box::new(io) as Box<dyn Future<Output = _> + Send>,
))
})
@@ -87,7 +87,7 @@ struct Ctx {
}
impl Ctx {
- fn new(request_body_tx: oneshot::Sender<BoxBody<Bytes, ErrorCode>>) -> Self {
+ fn new(request_body_tx: oneshot::Sender<UnsyncBoxBody<Bytes, ErrorCode>>) -> Self {
Self {
table: ResourceTable::default(),
wasi: WasiCtxBuilder::new().inherit_stdio().build(),
@@ -150,7 +150,7 @@ async fn run_cli(path: &str, server: &Server) -> anyhow::Result<()> {
async fn run_http<E: Into<ErrorCode> + 'static>(
component_filename: &str,
req: http::Request<impl Body<Data = Bytes, Error = E> + Send + Sync + 'static>,
- request_body_tx: oneshot::Sender<BoxBody<Bytes, ErrorCode>>,
+ request_body_tx: oneshot::Sender<UnsyncBoxBody<Bytes, ErrorCode>>,
) -> anyhow::Result<Result<http::Response<Collected<Bytes>>, Option<ErrorCode>>> {
let engine = test_programs_artifacts::engine(|config| {
config.wasm_backtrace_details(wasmtime::WasmBacktraceDetails::Enable);
diff --git a/src/commands/serve.rs b/src/commands/serve.rs
index 8d776a1f95..00f5e2a89c 100644
--- a/src/commands/serve.rs
+++ b/src/commands/serve.rs
@@ -5,7 +5,7 @@ use clap::Parser;
use futures::future::FutureExt;
use http::{Response, StatusCode};
use http_body_util::BodyExt as _;
-use http_body_util::combinators::BoxBody;
+use http_body_util::combinators::UnsyncBoxBody;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::pin::Pin;
@@ -568,7 +568,7 @@ impl ServeCommand {
.body(
Full::new(bytes::Bytes::from(error_html))
.map_err(|_| unreachable!())
- .boxed(),
+ .boxed_unsync(),
)
.unwrap())
}
@@ -826,7 +826,7 @@ type Request = hyper::Request<hyper::body::Incoming>;
async fn handle_request(
handler: ProxyHandler<HostHandlerState>,
req: Request,
-) -> Result<hyper::Response<BoxBody<Bytes, anyhow::Error>>> {
+) -> Result<hyper::Response<UnsyncBoxBody<Bytes, anyhow::Error>>> {
use tokio::sync::oneshot;
let req_id = handler.next_req_id();
@@ -847,7 +847,7 @@ async fn handle_request(
hyper::Response<wasmtime_wasi_http::body::HyperOutgoingBody>,
p2::http::types::ErrorCode,
>;
- type P3Response = hyper::Response<BoxBody<Bytes, anyhow::Error>>;
+ type P3Response = hyper::Response<UnsyncBoxBody<Bytes, anyhow::Error>>;
enum Sender {
P2(oneshot::Sender<P2Response>),
@@ -908,7 +908,7 @@ async fn handle_request(
let (res, task) = proxy.handle(store, request).await??;
let res = store
.with(|mut store| res.into_http(&mut store, request_io_result))?;
- _ = tx.send(res.map(|body| body.map_err(|e| e.into()).boxed()));
+ _ = tx.send(res.map(|body| body.map_err(|e| e.into()).boxed_unsync()));
// Wait for the task to finish.
task.block(store).await;
@@ -930,7 +930,7 @@ async fn handle_request(
.await
.context("guest never invoked `response-outparam::set` method")?
.map_err(|e| anyhow::Error::from(e))?
- .map(|body| body.map_err(|e| e.into()).boxed()),
+ .map(|body| body.map_err(|e| e.into()).boxed_unsync()),
Receiver::P3(rx) => rx.await?,
})
}