Skip to content

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

wasi-http: expose `http::Extensions` on p3 `Request` and `Response` (#15225)

John VanEnk1d10469d7a95
diff --git a/crates/test-programs/src/bin/p3_http_extensions.rs b/crates/test-programs/src/bin/p3_http_extensions.rs
new file mode 100644
index 0000000000..9c0d09c5c3
--- /dev/null
+++ b/crates/test-programs/src/bin/p3_http_extensions.rs
@@ -0,0 +1,25 @@
+use test_programs::p3::{
+ service::exports::wasi::http::handler::Guest as Handler,
+ wasi::http::{
+ client,
+ types::{ErrorCode, Request, Response, Scheme},
+ },
+};
+
+struct Component;
+
+test_programs::p3::service::export!(Component);
+
+impl Handler for Component {
+ // Forward the incoming request resource itself, rather than a new request,
+ // and return the upstream response resource unchanged, so the host can
+ // check what survives each trip through the guest.
+ async fn handle(request: Request) -> Result<Response, ErrorCode> {
+ request.set_scheme(Some(&Scheme::Http)).unwrap();
+ request.set_authority(Some("p3-test-extensions")).unwrap();
+ request.set_path_with_query(Some("/")).unwrap();
+ client::send(request).await
+ }
+}
+
+fn main() {}
diff --git a/crates/wasi-http/src/handler.rs b/crates/wasi-http/src/handler.rs
index 95b3b3eca7..1778c7c9eb 100644
--- a/crates/wasi-http/src/handler.rs
+++ b/crates/wasi-http/src/handler.rs
@@ -1095,6 +1095,7 @@ impl<'a, T: Send> Prepared<'a, T> {
method,
uri,
headers: request_headers,
+ extensions,
..
} = parts;
let http::uri::Parts { path_and_query, .. } = uri.into_parts();
@@ -1102,7 +1103,7 @@ impl<'a, T: Send> Prepared<'a, T> {
let headers = crate::FieldMap::new_immutable(hooks, request_headers);
// `serve` speaks plaintext HTTP, so that is the scheme of every
// request, whatever scheme its request target names (as in p2).
- let (request, request_io_result) = p3::Request::new(
+ let (mut request, request_io_result) = p3::Request::new(
method,
Some(http::uri::Scheme::HTTP),
Some(authority),
@@ -1111,6 +1112,7 @@ impl<'a, T: Send> Prepared<'a, T> {
None,
body,
);
+ request.extensions = extensions;
let request = view(store.data_mut()).table.push(request)?;
Ok(Prepared::P3 {
diff --git a/crates/wasi-http/src/p3/host/handler.rs b/crates/wasi-http/src/p3/host/handler.rs
index a73b280a02..240ef20601 100644
--- a/crates/wasi-http/src/p3/host/handler.rs
+++ b/crates/wasi-http/src/p3/host/handler.rs
@@ -113,7 +113,10 @@ where
};
let (
http::response::Parts {
- status, headers, ..
+ status,
+ headers,
+ extensions,
+ ..
},
body,
) = res.into_parts();
@@ -158,6 +161,7 @@ where
let res = Response {
status,
headers: FieldMap::new_immutable(view.hooks, headers),
+ extensions,
body: Body::Host {
body,
result_tx: res_result_tx,
diff --git a/crates/wasi-http/src/p3/host/types.rs b/crates/wasi-http/src/p3/host/types.rs
index 6ebf7481f8..153c591e29 100644
--- a/crates/wasi-http/src/p3/host/types.rs
+++ b/crates/wasi-http/src/p3/host/types.rs
@@ -238,6 +238,7 @@ where
path_with_query: None,
headers,
options: options.map(Into::into),
+ extensions: http::Extensions::new(),
body,
};
let req = cx
@@ -503,6 +504,7 @@ where
let res = Response {
status: http::StatusCode::OK,
headers,
+ extensions: http::Extensions::new(),
body,
};
let res = cx.table.push(res)?;
diff --git a/crates/wasi-http/src/p3/request.rs b/crates/wasi-http/src/p3/request.rs
index 4f2b41a0c8..53038307ad 100644
--- a/crates/wasi-http/src/p3/request.rs
+++ b/crates/wasi-http/src/p3/request.rs
@@ -30,6 +30,9 @@ pub struct Request {
pub headers: FieldMap,
/// Request options.
pub options: Option<Arc<RequestOptions>>,
+ /// Data associated with this request. Cannot be read by the guest, but can
+ /// be forwarded by the guest.
+ pub extensions: http::Extensions,
/// Request body.
pub(crate) body: Body,
}
@@ -66,6 +69,7 @@ impl Request {
path_with_query,
headers: headers.into(),
options,
+ extensions: http::Extensions::new(),
body: Body::Host {
body: body.map_err(Into::into).boxed_unsync(),
result_tx: tx,
@@ -100,6 +104,7 @@ impl Request {
method,
uri,
headers,
+ extensions,
..
},
body,
@@ -110,7 +115,7 @@ impl Request {
path_and_query,
..
} = uri.into_parts();
- Self::new(
+ let (mut req, fut) = Self::new(
method,
scheme,
authority,
@@ -118,7 +123,9 @@ impl Request {
FieldMap::new_immutable(hooks, headers),
None,
body,
- )
+ );
+ req.extensions = extensions;
+ (req, fut)
}
/// Convert this [`Request`] into an [`http::Request<UnsyncBoxBody<Bytes, ErrorCode>>`].
@@ -154,6 +161,7 @@ impl Request {
path_with_query,
mut headers,
options,
+ extensions,
body,
} = self;
// `Content-Length` header value is validated in `fields` implementation
@@ -237,6 +245,7 @@ impl Request {
})?;
let mut req = http::Request::builder();
*req.headers_mut().unwrap() = headers.into();
+ *req.extensions_mut().unwrap() = extensions;
let req = req
.method(method)
.uri(uri)
diff --git a/crates/wasi-http/src/p3/response.rs b/crates/wasi-http/src/p3/response.rs
index 65a9279513..7bb11a9732 100644
--- a/crates/wasi-http/src/p3/response.rs
+++ b/crates/wasi-http/src/p3/response.rs
@@ -15,6 +15,9 @@ pub struct Response {
pub status: StatusCode,
/// The headers of the response.
pub headers: FieldMap,
+ /// Data associated with this response. Cannot be read by the guest, but can
+ /// be forwarded by the guest.
+ pub extensions: http::Extensions,
/// Response body.
pub(crate) body: Body,
}
@@ -26,11 +29,13 @@ impl TryFrom<Response> for http::Response<Body> {
Response {
status,
headers,
+ extensions,
body,
}: Response,
) -> Result<Self, Self::Error> {
let mut res = http::Response::builder().status(status);
*res.headers_mut().unwrap() = headers.into();
+ *res.extensions_mut().unwrap() = extensions;
res.body(body)
}
}
@@ -121,6 +126,7 @@ impl Response {
let wasi_response = Response {
status: parts.status,
headers: FieldMap::new_immutable(hooks, parts.headers),
+ extensions: parts.extensions,
body: Body::Host {
body: body.map_err(Into::into).boxed_unsync(),
result_tx,
diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rs
index 9ba83972de..19e7115e16 100644
--- a/crates/wasi-http/tests/all/p3/mod.rs
+++ b/crates/wasi-http/tests/all/p3/mod.rs
@@ -35,6 +35,15 @@ foreach_p3_http!(assert_test_exists);
/// collecting it.
type ForwardedRequestTx = oneshot::Sender<Result<http::Request<Collected<Bytes>>, Error>>;
+/// An extension the host attaches to a request passed to the guest.
+#[derive(Clone, Debug, PartialEq)]
+struct RequestMarker(&'static str);
+
+/// An extension `TestHooks::send_request` attaches to the response for
+/// `p3-test-extensions`.
+#[derive(Clone, Debug, PartialEq)]
+struct ResponseMarker(&'static str);
+
#[derive(Default)]
struct TestHooks {
request_tx: Option<ForwardedRequestTx>,
@@ -136,6 +145,21 @@ impl WasiHttpHooks for TestHooks {
}) as Box<dyn Future<Output = _> + Send>,
))
})
+ } else if let Some("p3-test-extensions") = request.uri().authority().map(|v| v.as_str()) {
+ // Like `p3-test`, but the response carries an extension, for
+ // checking that extensions survive a trip through the guest.
+ let request_tx = self.request_tx.take();
+ Box::new(async move {
+ collect_request(request, request_tx).await?;
+ let mut response = http::Response::new(Default::default());
+ response
+ .extensions_mut()
+ .insert(ResponseMarker("from send_request"));
+ Ok((
+ response,
+ Box::new(async { Ok(()) }) as Box<dyn Future<Output = _> + Send>,
+ ))
+ })
} else if let Some("p3-test-trailers") = request.uri().authority().map(|v| v.as_str()) {
let request_tx = self.request_tx.take();
let body = http_body_util::StreamBody::new(futures::stream::iter([
@@ -1048,6 +1072,67 @@ async fn p3_http_forbidden_headers() -> Result<()> {
Ok(())
}
+/// Ensure that extensions survive a trip through the guest in both directions
+/// when the guest forwards the HTTP request or response rather than making a
+/// new one.
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p3_http_extensions() -> Result<()> {
+ let request = http::Request::builder()
+ .uri("http://localhost/")
+ .method(http::Method::GET)
+ .extension(RequestMarker("from host"));
+
+ let (request_tx, request_rx) = oneshot::channel();
+ let response = run_http_forwarded(
+ P3_HTTP_EXTENSIONS_COMPONENT,
+ request.body(Empty::new())?,
+ request_tx,
+ )
+ .await?
+ .unwrap();
+ assert_eq!(response.status().as_u16(), 200);
+ assert_eq!(
+ response.extensions().get::<ResponseMarker>(),
+ Some(&ResponseMarker("from send_request")),
+ );
+
+ let outbound = request_rx.await.unwrap().unwrap();
+ assert_eq!(
+ outbound.extensions().get::<RequestMarker>(),
+ Some(&RequestMarker("from host")),
+ );
+
+ Ok(())
+}
+
+/// A non-canonical http/1 reason phrase from the backend, which `hyper`'s
+/// client records as a `ReasonPhrase` extension, survives the guest returning
+/// the backend's response.
+#[test_log::test(tokio::test(flavor = "multi_thread"))]
+async fn p3_http_proxy_reason_phrase() -> Result<()> {
+ let (server, _requests) = Server::raw(1, |_head| {
+ b"HTTP/1.1 200 Quite Ok\r\ncontent-length: 0\r\n\r\n".to_vec()
+ })?;
+
+ let request = http::Request::builder()
+ .uri("http://localhost/")
+ .method(http::Method::GET)
+ .header("url", format!("http://{}/", server.addr()));
+ let response = run_http(P3_HTTP_PROXY_COMPONENT, request.body(Empty::new())?)
+ .await?
+ .unwrap();
+ assert_eq!(response.status().as_u16(), 200);
+ assert_eq!(
+ response
+ .extensions()
+ .get::<hyper::ext::ReasonPhrase>()
+ .map(|r| r.as_bytes()),
+ Some(b"Quite Ok".as_slice()),
+ );
+
+ Ok(())
+}
+
// Custom body wrapper that sends a configurable frame at EOS while reporting is_end_stream() = true
struct BodyWithFrameAtEos {
inner: http_body_util::StreamBody<