Skip to content

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

add `p3::Response::into_http_with_getter` (#11696)

Joel Diceedae46b11160
diff --git a/crates/wasi-http/src/p3/response.rs b/crates/wasi-http/src/p3/response.rs
index 09c7506794..c7248c984e 100644
--- a/crates/wasi-http/src/p3/response.rs
+++ b/crates/wasi-http/src/p3/response.rs
@@ -1,7 +1,7 @@
use crate::get_content_length;
-use crate::p3::WasiHttpView;
use crate::p3::bindings::http::types::ErrorCode;
use crate::p3::body::{Body, ConsumedBody, GuestBody};
+use crate::p3::{WasiHttpCtxView, WasiHttpView};
use anyhow::Context as _;
use bytes::Bytes;
use http::{HeaderMap, StatusCode};
@@ -47,6 +47,17 @@ impl Response {
self,
store: impl AsContextMut<Data = T>,
fut: impl Future<Output = Result<(), ErrorCode>> + Send + 'static,
+ ) -> wasmtime::Result<http::Response<BoxBody<Bytes, ErrorCode>>> {
+ self.into_http_with_getter(store, fut, T::http)
+ }
+
+ /// Like [`Self::into_http`], but with a custom function for converting `T`
+ /// to a [`WasiHttpCtxView`].
+ pub fn into_http_with_getter<T: 'static>(
+ self,
+ 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>>> {
let res = http::Response::try_from(self)?;
let (res, body) = res.into_parts();
@@ -67,7 +78,7 @@ impl Response {
fut,
content_length,
ErrorCode::HttpResponseBodySize,
- T::http,
+ getter,
)
.boxed()
}