A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
p3-http: rework `content-length` handling (#11658)
Roman Volosatovs5a2860ce5615
diff --git a/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs b/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rsindex 2bae98ec76..d1642323e9 100644--- a/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs+++ b/crates/test-programs/src/bin/p3_http_outbound_request_content_length.rs@@ -51,11 +51,7 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {async { transmit.await },async {let remaining = contents_tx.write_all(b"long enough".to_vec()).await;- assert!(- remaining.is_empty(),- "{}",- String::from_utf8_lossy(&remaining)- );+ assert_eq!(String::from_utf8_lossy(&remaining), "");trailers_tx.write(Ok(None)).await.unwrap();drop(contents_tx);},@@ -72,19 +68,19 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {async { transmit.await },async {let remaining = contents_tx.write_all(b"msg".to_vec()).await;- assert!(- remaining.is_empty(),- "{}",- String::from_utf8_lossy(&remaining)- );- drop(contents_tx);+ assert_eq!(String::from_utf8_lossy(&remaining), "");trailers_tx.write(Ok(None)).await.unwrap();+ drop(contents_tx);},);- let err = handle.expect_err("should have failed to send request");+ // The request body will be polled before `handle` returns.+ // Due to the way implementation is structured, by the time it happens+ // the error will be already available in most cases and `handle` will fail,+ // but it is a race condition, since `handle` may also succeed if+ // polling body returns `Poll::Pending`assert!(- matches!(err, ErrorCode::HttpProtocolError),- "unexpected error: {err:#?}"+ matches!(handle, Ok(..) | Err(ErrorCode::HttpProtocolError)),+ "unexpected handle result: {handle:#?}");let err = transmit.expect_err("request transmission should have failed");assert!(@@ -101,15 +97,22 @@ impl test_programs::p3::exports::wasi::cli::run::Guest for Component {async { transmit.await },async {let remaining = contents_tx.write_all(b"more than 11 bytes".to_vec()).await;- assert_eq!(String::from_utf8_lossy(&remaining), "more than 11 bytes",);- drop(contents_tx);+ assert_eq!(String::from_utf8_lossy(&remaining), "more than 11 bytes");_ = trailers_tx.write(Ok(None)).await;},);-- // The the error returned by `handle` in this case is non-deterministic,- // so just assert that it fails- let _err = handle.expect_err("should have failed to send request");+ // The request body will be polled before `handle` returns.+ // Due to the way implementation is structured, by the time it happens+ // the error will be already available in most cases and `handle` will fail,+ // but it is a race condition, since `handle` may also succeed if+ // polling body returns `Poll::Pending`+ assert!(+ matches!(+ handle,+ Ok(..) | Err(ErrorCode::HttpRequestBodySize(Some(18)))+ ),+ "unexpected handle result: {handle:#?}"+ );let err = transmit.expect_err("request transmission should have failed");assert!(matches!(err, ErrorCode::HttpRequestBodySize(Some(18))),diff --git a/crates/wasi-http/src/lib.rs b/crates/wasi-http/src/lib.rsindex 38a3fd18de..9e058df306 100644--- a/crates/wasi-http/src/lib.rs+++ b/crates/wasi-http/src/lib.rs@@ -240,6 +240,7 @@ pub use crate::types::{DEFAULT_OUTGOING_BODY_BUFFER_CHUNKS, DEFAULT_OUTGOING_BODY_CHUNK_SIZE, WasiHttpCtx,WasiHttpImpl, WasiHttpView,};+use http::header::CONTENT_LENGTH;use wasmtime::component::{HasData, Linker};/// Add all of the `wasi:http/proxy` world's interfaces to a [`wasmtime::component::Linker`].@@ -391,3 +392,15 @@ whereOk(())}++/// Extract the `Content-Length` header value from a [`http::HeaderMap`], returning `None` if it's not+/// present. This function will return `Err` if it's not possible to parse the `Content-Length`+/// header.+fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>> {+ let Some(v) = headers.get(CONTENT_LENGTH) else {+ return Ok(None);+ };+ let v = v.to_str()?;+ let v = v.parse()?;+ Ok(Some(v))+}diff --git a/crates/wasi-http/src/p3/body.rs b/crates/wasi-http/src/p3/body.rsindex 54299e95dc..dc1f10d311 100644--- a/crates/wasi-http/src/p3/body.rs+++ b/crates/wasi-http/src/p3/body.rs@@ -122,75 +122,44 @@ impl Body {}}-/// The kind of body, used for error reporting-pub(crate) enum BodyKind {- Request,- Response,-}--/// Represents `Content-Length` limit and state-#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]-struct ContentLength {+/// [StreamConsumer] implementation for bodies originating in the guest with `Content-Length`+/// header set.+struct LimitedGuestBodyConsumer {+ contents_tx: PollSender<Result<Bytes, ErrorCode>>,+ error_tx: Option<oneshot::Sender<ErrorCode>>,+ make_error: fn(Option<u64>) -> ErrorCode,/// Limit of bytes to be sentlimit: u64,/// Number of bytes sentsent: u64,-}--impl ContentLength {- /// Constructs new [ContentLength]- fn new(limit: u64) -> Self {- Self { limit, sent: 0 }- }-}--/// [StreamConsumer] implementation for bodies originating in the guest.-struct GuestBodyConsumer {- contents_tx: PollSender<Result<Bytes, ErrorCode>>,- result_tx: Option<oneshot::Sender<Result<(), ErrorCode>>>,- content_length: Option<ContentLength>,- kind: BodyKind,// `true` when the other side of `contents_tx` was unexpectedly closedclosed: bool,}-impl GuestBodyConsumer {- /// Constructs the approprite body size error given the [BodyKind]- fn body_size_error(&self, n: Option<u64>) -> ErrorCode {- match self.kind {- BodyKind::Request => ErrorCode::HttpRequestBodySize(n),- BodyKind::Response => ErrorCode::HttpResponseBodySize(n),- }- }-- // Sends the corresponding error constructed by [Self::body_size_error] on both- // error channels.- // [`PollSender::poll_reserve`] on `contents_tx` must have succeeed prior to this being called.- fn send_body_size_error(&mut self, n: Option<u64>) {- if let Some(result_tx) = self.result_tx.take() {- _ = result_tx.send(Err(self.body_size_error(n)));- _ = self.contents_tx.send_item(Err(self.body_size_error(n)));+impl LimitedGuestBodyConsumer {+ /// Sends the error constructed by [Self::make_error] on both error channels.+ /// Does nothing if an error has already been sent on [Self::error_tx].+ fn send_error(&mut self, sent: Option<u64>) {+ if let Some(error_tx) = self.error_tx.take() {+ _ = error_tx.send((self.make_error)(sent));+ self.contents_tx.abort_send();+ if let Some(tx) = self.contents_tx.get_ref() {+ _ = tx.try_send(Err((self.make_error)(sent)))+ }+ self.contents_tx.close();}}}-impl Drop for GuestBodyConsumer {+impl Drop for LimitedGuestBodyConsumer {fn drop(&mut self) {- if let Some(result_tx) = self.result_tx.take() {- if let Some(ContentLength { limit, sent }) = self.content_length {- if !self.closed && limit != sent {- _ = result_tx.send(Err(self.body_size_error(Some(sent))));- self.contents_tx.abort_send();- if let Some(tx) = self.contents_tx.get_ref() {- _ = tx.try_send(Err(self.body_size_error(Some(sent))))- }- }- }+ if !self.closed && self.limit != self.sent {+ self.send_error(Some(self.sent))}}}-impl<D> StreamConsumer<D> for GuestBodyConsumer {+impl<D> StreamConsumer<D> for LimitedGuestBodyConsumer {type Item = u8;fn poll_consume(@@ -201,27 +170,31 @@ impl<D> StreamConsumer<D> for GuestBodyConsumer {finish: bool,) -> Poll<wasmtime::Result<StreamResult>> {debug_assert!(!self.closed);+ let mut src = src.as_direct(store);+ let buf = src.remaining();+ let n = buf.len();++ // Perform `content-length` check early and precompute the next value+ let Ok(sent) = n.try_into() else {+ self.send_error(None);+ return Poll::Ready(Ok(StreamResult::Dropped));+ };+ let Some(sent) = self.sent.checked_add(sent) else {+ self.send_error(None);+ return Poll::Ready(Ok(StreamResult::Dropped));+ };+ if sent > self.limit {+ self.send_error(Some(sent));+ return Poll::Ready(Ok(StreamResult::Dropped));+ }match self.contents_tx.poll_reserve(cx) {Poll::Ready(Ok(())) => {- let mut src = src.as_direct(store);- let buf = src.remaining();- if let Some(ContentLength { limit, sent }) = self.content_length.as_mut() {- let Some(n) = buf.len().try_into().ok().and_then(|n| sent.checked_add(n))- else {- self.send_body_size_error(None);- return Poll::Ready(Ok(StreamResult::Dropped));- };- if n > *limit {- self.send_body_size_error(Some(n));- return Poll::Ready(Ok(StreamResult::Dropped));- }- *sent = n;- }let buf = Bytes::copy_from_slice(buf);- let n = buf.len();match self.contents_tx.send_item(Ok(buf)) {Ok(()) => {src.mark_read(n);+ // Record new `content-length` only on successful send+ self.sent = sent;Poll::Ready(Ok(StreamResult::Completed))}Err(..) => {@@ -240,6 +213,41 @@ impl<D> StreamConsumer<D> for GuestBodyConsumer {}}+/// [StreamConsumer] implementation for bodies originating in the guest without `Content-Length`+/// header set.+struct UnlimitedGuestBodyConsumer(PollSender<Result<Bytes, ErrorCode>>);++impl<D> StreamConsumer<D> for UnlimitedGuestBodyConsumer {+ type Item = u8;++ fn poll_consume(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ store: StoreContextMut<D>,+ src: Source<Self::Item>,+ finish: bool,+ ) -> Poll<wasmtime::Result<StreamResult>> {+ match self.0.poll_reserve(cx) {+ Poll::Ready(Ok(())) => {+ let mut src = src.as_direct(store);+ let buf = src.remaining();+ let n = buf.len();+ let buf = Bytes::copy_from_slice(buf);+ match self.0.send_item(Ok(buf)) {+ Ok(()) => {+ src.mark_read(n);+ Poll::Ready(Ok(StreamResult::Completed))+ }+ Err(..) => Poll::Ready(Ok(StreamResult::Dropped)),+ }+ }+ Poll::Ready(Err(..)) => Poll::Ready(Ok(StreamResult::Dropped)),+ Poll::Pending if finish => Poll::Ready(Ok(StreamResult::Cancelled)),+ Poll::Pending => Poll::Pending,+ }+ }+}+/// [http_body::Body] implementation for bodies originating in the guest.pub(crate) struct GuestBody {contents_rx: Option<mpsc::Receiver<Result<Bytes, ErrorCode>>>,@@ -253,9 +261,10 @@ impl GuestBody {mut store: impl AsContextMut<Data = T>,contents_rx: Option<StreamReader<u8>>,trailers_rx: FutureReader<Result<Option<Resource<Trailers>>, ErrorCode>>,- result_tx: oneshot::Sender<Result<(), ErrorCode>>,+ result_tx: oneshot::Sender<Box<dyn Future<Output = Result<(), ErrorCode>> + Send>>,+ result_fut: impl Future<Output = Result<(), ErrorCode>> + Send + 'static,content_length: Option<u64>,- kind: BodyKind,+ make_error: fn(Option<u64>) -> ErrorCode,getter: fn(&mut T) -> WasiHttpCtxView<'_>,) -> Self {let (trailers_http_tx, trailers_http_rx) = oneshot::channel();@@ -266,20 +275,38 @@ impl GuestBody {getter,},);- let contents_rx = contents_rx.map(|rx| {++ let contents_rx = if let Some(rx) = contents_rx {let (http_tx, http_rx) = mpsc::channel(1);- rx.pipe(- store,- GuestBodyConsumer {- contents_tx: PollSender::new(http_tx),- result_tx: Some(result_tx),- content_length: content_length.map(ContentLength::new),- kind,- closed: false,- },- );- http_rx- });+ let contents_tx = PollSender::new(http_tx);+ if let Some(limit) = content_length {+ let (error_tx, error_rx) = oneshot::channel();+ _ = result_tx.send(Box::new(async move {+ if let Ok(err) = error_rx.await {+ return Err(err);+ };+ result_fut.await+ }));+ rx.pipe(+ store,+ LimitedGuestBodyConsumer {+ contents_tx,+ error_tx: Some(error_tx),+ make_error,+ limit,+ sent: 0,+ closed: false,+ },+ );+ } else {+ _ = result_tx.send(Box::new(result_fut));+ rx.pipe(store, UnlimitedGuestBodyConsumer(contents_tx));+ };+ Some(http_rx)+ } else {+ _ = result_tx.send(Box::new(result_fut));+ None+ };Self {trailers_rx: Some(trailers_http_rx),contents_rx,@@ -303,7 +330,7 @@ impl http_body::Body for GuestBody {Ok(buf) => {if let Some(n) = self.content_length.as_mut() {// Substract frame length from `content_length`,- // [GuestBodyConsumer] already performs the validation, so+ // [LimitedGuestBodyConsumer] already performs the validation, so// just keep count as optimization for// `is_end_stream` and `size_hint`*n = n.saturating_sub(buf.len().try_into().unwrap_or(u64::MAX));diff --git a/crates/wasi-http/src/p3/host/handler.rs b/crates/wasi-http/src/p3/host/handler.rsindex 026133ca7a..60affada21 100644--- a/crates/wasi-http/src/p3/host/handler.rs+++ b/crates/wasi-http/src/p3/host/handler.rs@@ -1,10 +1,12 @@+use crate::get_content_length;use crate::p3::bindings::http::handler::{Host, HostWithStore};use crate::p3::bindings::http::types::{ErrorCode, Request, Response};-use crate::p3::body::{Body, BodyKind, ConsumedBody, GuestBody};-use crate::p3::{HttpError, HttpResult, WasiHttp, WasiHttpCtxView, get_content_length};+use crate::p3::body::{Body, ConsumedBody, GuestBody};+use crate::p3::{HttpError, HttpResult, WasiHttp, WasiHttpCtxView};use anyhow::Context as _;+use bytes::Bytes;use core::pin::Pin;-use core::task::{Context, Poll, Waker};+use core::task::{Context, Poll, Waker, ready};use http::header::HOST;use http::{HeaderValue, Uri};use http_body_util::BodyExt as _;@@ -56,6 +58,87 @@ where}}+/// A wrapper around [http_body::Body], which validates `Content-Length`+struct BodyWithContentLength<T, E> {+ body: T,+ error_tx: Option<oneshot::Sender<E>>,+ make_error: fn(Option<u64>) -> E,+ /// Limit of bytes to be sent+ limit: u64,+ /// Number of bytes sent+ sent: u64,+}++impl<T, E> BodyWithContentLength<T, E> {+ /// Sends the error constructed by [Self::make_error] on [Self::error_tx].+ /// Does nothing if an error has already been sent on [Self::error_tx].+ fn send_error<V>(&mut self, sent: Option<u64>) -> Poll<Option<Result<V, E>>> {+ if let Some(error_tx) = self.error_tx.take() {+ _ = error_tx.send((self.make_error)(sent));+ }+ Poll::Ready(Some(Err((self.make_error)(sent))))+ }+}++impl<T, E> http_body::Body for BodyWithContentLength<T, E>+where+ T: http_body::Body<Data = Bytes, Error = E> + Unpin,+{+ type Data = T::Data;+ type Error = T::Error;++ #[inline]+ fn poll_frame(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {+ match ready!(Pin::new(&mut self.as_mut().body).poll_frame(cx)) {+ Some(Ok(frame)) => {+ let Some(data) = frame.data_ref() else {+ return Poll::Ready(Some(Ok(frame)));+ };+ let Ok(sent) = data.len().try_into() else {+ return self.send_error(None);+ };+ let Some(sent) = self.sent.checked_add(sent) else {+ return self.send_error(None);+ };+ if sent > self.limit {+ return self.send_error(Some(sent));+ }+ self.sent = sent;+ Poll::Ready(Some(Ok(frame)))+ }+ Some(Err(err)) => Poll::Ready(Some(Err(err))),+ None if self.limit != self.sent => {+ // short write+ let sent = self.sent;+ self.send_error(Some(sent))+ }+ None => Poll::Ready(None),+ }+ }++ #[inline]+ fn is_end_stream(&self) -> bool {+ self.body.is_end_stream()+ }++ #[inline]+ fn size_hint(&self) -> http_body::SizeHint {+ let n = self.limit.saturating_sub(self.sent);+ let mut hint = self.body.size_hint();+ if hint.lower() >= n {+ hint.set_exact(n)+ } else if let Some(max) = hint.upper() {+ hint.set_upper(n.min(max))+ } else {+ hint.set_upper(n)+ }+ hint+ }+}+trait BodyExt {fn with_state<T>(self, state: T) -> BodyWithState<Self, T>where@@ -66,6 +149,24 @@ trait BodyExt {_state: state,}}++ fn with_content_length<E>(+ self,+ limit: u64,+ error_tx: oneshot::Sender<E>,+ make_error: fn(Option<u64>) -> E,+ ) -> BodyWithContentLength<Self, E>+ where+ Self: Sized,+ {+ BodyWithContentLength {+ body: self,+ error_tx: Some(error_tx),+ make_error,+ limit,+ sent: 0,+ }+ }}impl<T> BodyExt for T {}@@ -127,38 +228,52 @@ impl HostWithStore for WasiHttp {.delete(req).context("failed to delete request from table").map_err(HttpError::trap)?;+ // `Content-Length` header value is validated in `fields` implementation+ let content_length = match get_content_length(&headers) {+ Ok(content_length) => content_length,+ Err(err) => {+ body.drop(&mut store);+ return Err(ErrorCode::InternalError(Some(format!("{err:#}"))).into());+ }+ };let mut headers = Arc::unwrap_or_clone(headers);let body = match body {Body::Guest {contents_rx,trailers_rx,result_tx,- } => {- let (http_result_tx, http_result_rx) = oneshot::channel();- // `Content-Length` header value is validated in `fields` implementation- let content_length = get_content_length(&headers)- .map_err(|err| ErrorCode::InternalError(Some(format!("{err:#}"))))?;- _ = result_tx.send(Box::new(async move {- if let Ok(Err(err)) = http_result_rx.await {- return Err(err);- };- io_task_result(io_result_rx).await- }));- GuestBody::new(- &mut store,- contents_rx,- trailers_rx,- http_result_tx,- content_length,- BodyKind::Request,- getter,- )- .with_state(io_task_rx)- .boxed()- }+ } => GuestBody::new(+ &mut store,+ contents_rx,+ trailers_rx,+ result_tx,+ io_task_result(io_result_rx),+ content_length,+ ErrorCode::HttpRequestBodySize,+ getter,+ )+ .with_state(io_task_rx)+ .boxed(),Body::Host { body, result_tx } => {- _ = result_tx.send(Box::new(io_task_result(io_result_rx)));- body.with_state(io_task_rx).boxed()+ if let Some(limit) = content_length {+ let (http_result_tx, http_result_rx) = oneshot::channel();+ _ = result_tx.send(Box::new(async move {+ if let Ok(err) = http_result_rx.await {+ return Err(err);+ };+ io_task_result(io_result_rx).await+ }));+ body.with_content_length(+ limit,+ http_result_tx,+ ErrorCode::HttpRequestBodySize,+ )+ .with_state(io_task_rx)+ .boxed()+ } else {+ _ = result_tx.send(Box::new(io_task_result(io_result_rx)));+ body.with_state(io_task_rx).boxed()+ }}Body::Consumed => ConsumedBody.boxed(),};diff --git a/crates/wasi-http/src/p3/host/types.rs b/crates/wasi-http/src/p3/host/types.rsindex 165e4e4e12..840702546b 100644--- a/crates/wasi-http/src/p3/host/types.rs+++ b/crates/wasi-http/src/p3/host/types.rs@@ -9,7 +9,7 @@ use crate::p3::{HeaderResult, HttpError, RequestOptionsResult, WasiHttp, WasiHttuse anyhow::Context as _;use core::mem;use core::pin::Pin;-use core::task::{Context, Poll};+use core::task::{Context, Poll, ready};use http::header::CONTENT_LENGTH;use std::sync::Arc;use tokio::sync::oneshot;@@ -134,6 +134,18 @@ enum GuestBodyResultProducer {Future(Pin<Box<dyn Future<Output = Result<(), ErrorCode>> + Send>>),}+fn poll_future<T>(+ cx: &mut Context<'_>,+ fut: Pin<&mut (impl Future<Output = T> + ?Sized)>,+ finish: bool,+) -> Poll<Option<T>> {+ match fut.poll(cx) {+ Poll::Ready(v) => Poll::Ready(Some(v)),+ Poll::Pending if finish => Poll::Ready(None),+ Poll::Pending => Poll::Pending,+ }+}+impl<D> FutureProducer<D> for GuestBodyResultProducer {type Item = Result<(), ErrorCode>;@@ -144,26 +156,23 @@ impl<D> FutureProducer<D> for GuestBodyResultProducer {finish: bool,) -> Poll<wasmtime::Result<Option<Self::Item>>> {match &mut *self {- Self::Receiver(rx) => match Pin::new(rx).poll(cx) {- Poll::Ready(Ok(fut)) => {- let mut fut = Box::into_pin(fut);- match fut.as_mut().poll(cx) {- Poll::Ready(res) => Poll::Ready(Ok(Some(res))),- Poll::Pending => {- *self = Self::Future(fut);- Poll::Pending- }+ Self::Receiver(rx) => {+ match ready!(poll_future(cx, Pin::new(rx), finish)) {+ Some(Ok(fut)) => {+ let mut fut = Box::into_pin(fut);+ // poll the received future once and update state+ let res = poll_future(cx, fut.as_mut(), finish);+ *self = Self::Future(fut);+ res.map(Ok)}+ Some(Err(..)) => {+ // oneshot sender dropped, treat as success+ Poll::Ready(Ok(Some(Ok(()))))+ }+ None => Poll::Ready(Ok(None)),}- Poll::Ready(Err(..)) => Poll::Ready(Ok(Some(Ok(())))),- Poll::Pending if finish => Poll::Ready(Ok(None)),- Poll::Pending => Poll::Pending,- },- Self::Future(fut) => match fut.as_mut().poll(cx) {- Poll::Ready(res) => Poll::Ready(Ok(Some(res))),- Poll::Pending if finish => Poll::Ready(Ok(None)),- Poll::Pending => Poll::Pending,- },+ }+ Self::Future(fut) => poll_future(cx, fut.as_mut(), finish).map(Ok),}}}diff --git a/crates/wasi-http/src/p3/mod.rs b/crates/wasi-http/src/p3/mod.rsindex bc4a9aa8ba..96a4b6d74c 100644--- a/crates/wasi-http/src/p3/mod.rs+++ b/crates/wasi-http/src/p3/mod.rs@@ -27,7 +27,6 @@ use bindings::http::{handler, types};use bytes::Bytes;use core::ops::Deref;use http::HeaderName;-use http::header::CONTENT_LENGTH;use http::uri::Scheme;use http_body_util::combinators::BoxBody;use std::sync::Arc;@@ -43,18 +42,6 @@ pub(crate) type HeaderError = TrappableError<types::HeaderError>;pub(crate) type RequestOptionsResult<T> = Result<T, RequestOptionsError>;pub(crate) type RequestOptionsError = TrappableError<types::RequestOptionsError>;-/// Extract the `Content-Length` header value from a [`http::HeaderMap`], returning `None` if it's not-/// present. This function will return `Err` if it's not possible to parse the `Content-Length`-/// header.-fn get_content_length(headers: &http::HeaderMap) -> wasmtime::Result<Option<u64>> {- let Some(v) = headers.get(CONTENT_LENGTH) else {- return Ok(None);- };- let v = v.to_str()?;- let v = v.parse()?;- Ok(Some(v))-}-pub(crate) struct WasiHttp;impl HasData for WasiHttp {@@ -90,17 +77,20 @@ pub trait WasiHttpCtx: Send {/// Send an outgoing request.///- /// This implementation will be used by the `wasi:http/handler#handle` implementation.+ /// This function will be used by the `wasi:http/handler#handle` implementation.///- /// The specified [Future] `fut` can be used to communicate- /// a request processing error, if any, to the constructor of the request.- /// For example, if the request was constructed via `wasi:http/types.request#new`,- /// a result sent on `fut` will be forwarded to the guest on the future handle returned.+ /// The specified [Future] `fut` will be used to communicate+ /// a response processing error, if any.+ /// For example, if the response body is consumed via `wasi:http/types.response#consume-body`,+ /// a result will be sent on `fut`.////// The returned [Future] can be used to communicate/// a request processing error, if any, to the constructor of the request./// For example, if the request was constructed via `wasi:http/types.request#new`,/// a result resolved from it will be forwarded to the guest on the future handle returned.+ ///+ /// `Content-Length` of the request passed to this function will be validated, however no+ /// `Content-Length` validation will be performed for the received response.#[cfg(feature = "default-send-request")]fn send_request(&mut self,@@ -128,6 +118,21 @@ pub trait WasiHttpCtx: Send {}/// Send an outgoing request.+ ///+ /// This function will be used by the `wasi:http/handler#handle` implementation.+ ///+ /// The specified [Future] `fut` will be used to communicate+ /// a response processing error, if any.+ /// For example, if the response body is consumed via `wasi:http/types.response#consume-body`,+ /// a result will be sent on `fut`.+ ///+ /// The returned [Future] can be used to communicate+ /// a request processing error, if any, to the constructor of the request.+ /// For example, if the request was constructed via `wasi:http/types.request#new`,+ /// a result resolved from it will be forwarded to the guest on the future handle returned.+ ///+ /// `Content-Length` of the request passed to this function will be validated, however no+ /// `Content-Length` validation will be performed for the received response.#[cfg(not(feature = "default-send-request"))]fn send_request(&mut self,diff --git a/crates/wasi-http/src/p3/request.rs b/crates/wasi-http/src/p3/request.rsindex e4e728bb9d..75164922db 100644--- a/crates/wasi-http/src/p3/request.rs+++ b/crates/wasi-http/src/p3/request.rs@@ -43,6 +43,8 @@ impl Request {////// This returns a [Future] that the will be used to communicate/// a request processing error, if any.+ ///+ /// Requests constructed this way will not perform any `Content-Length` validation.pub fn new(method: Method,scheme: Option<Scheme>,@@ -80,6 +82,8 @@ impl Request {////// This returns a [Future] that will be used to communicate/// a request processing error, if any.+ ///+ /// Requests constructed this way will not perform any `Content-Length` validation.pub fn from_http<T>(req: http::Request<T>,) -> (@@ -126,6 +130,8 @@ impl Request {/// a request processing error, if any, to the constructor of the request./// For example, if the request was constructed via `wasi:http/types.request#new`,/// a result resolved from it will be forwarded to the guest on the future handle returned.+///+/// This function performs no `Content-Length` validation.#[cfg(feature = "default-send-request")]pub async fn default_send_request(mut req: http::Request<impl http_body::Body<Data = Bytes, Error = ErrorCode> + Send + 'static>,diff --git a/crates/wasi-http/src/p3/response.rs b/crates/wasi-http/src/p3/response.rsindex a01f9a01de..09c7506794 100644--- a/crates/wasi-http/src/p3/response.rs+++ b/crates/wasi-http/src/p3/response.rs@@ -1,13 +1,13 @@+use crate::get_content_length;+use crate::p3::WasiHttpView;use crate::p3::bindings::http::types::ErrorCode;-use crate::p3::body::{Body, BodyKind, ConsumedBody, GuestBody};-use crate::p3::{WasiHttpView, get_content_length};+use crate::p3::body::{Body, ConsumedBody, GuestBody};use anyhow::Context as _;use bytes::Bytes;use http::{HeaderMap, StatusCode};use http_body_util::BodyExt as _;use http_body_util::combinators::BoxBody;use std::sync::Arc;-use tokio::sync::oneshot;use wasmtime::AsContextMut;/// The concrete type behind a `wasi:http/types/response` resource.@@ -56,23 +56,17 @@ impl Response {trailers_rx,result_tx,} => {- let (http_result_tx, http_result_rx) = oneshot::channel();// `Content-Length` header value is validated in `fields` implementationlet content_length =get_content_length(&res.headers).context("failed to parse `content-length`")?;- _ = result_tx.send(Box::new(async move {- if let Ok(Err(err)) = http_result_rx.await {- return Err(err);- };- fut.await- }));GuestBody::new(store,contents_rx,trailers_rx,- http_result_tx,+ result_tx,+ fut,content_length,- BodyKind::Response,+ ErrorCode::HttpResponseBodySize,T::http,).boxed()diff --git a/crates/wasi-http/src/types_impl.rs b/crates/wasi-http/src/types_impl.rsindex 2e00c61a83..802e405e92 100644--- a/crates/wasi-http/src/types_impl.rs+++ b/crates/wasi-http/src/types_impl.rs@@ -1,15 +1,12 @@//! Implementation for the `wasi:http/types` interface.-use crate::{- HttpError, HttpResult, WasiHttpImpl, WasiHttpView,- bindings::http::types::{self, Headers, Method, Scheme, StatusCode, Trailers},- body::{HostFutureTrailers, HostIncomingBody, HostOutgoingBody, StreamContext},- types::{- FieldMap, HostFields, HostFutureIncomingResponse, HostIncomingRequest,- HostIncomingResponse, HostOutgoingRequest, HostOutgoingResponse, HostResponseOutparam,- remove_forbidden_headers,- },+use crate::bindings::http::types::{self, Headers, Method, Scheme, StatusCode, Trailers};+use crate::body::{HostFutureTrailers, HostIncomingBody, HostOutgoingBody, StreamContext};+use crate::types::{+ FieldMap, HostFields, HostFutureIncomingResponse, HostIncomingRequest, HostIncomingResponse,+ HostOutgoingRequest, HostOutgoingResponse, HostResponseOutparam, remove_forbidden_headers,};+use crate::{HttpError, HttpResult, WasiHttpImpl, WasiHttpView, get_content_length};use anyhow::{Context, anyhow};use std::any::Any;use std::str::FromStr;@@ -33,26 +30,6 @@ where}}-/// Extract the `Content-Length` header value from a [`FieldMap`], returning `None` if it's not-/// present. This function will return `Err` if it's not possible to parse the `Content-Length`-/// header.-fn get_content_length(fields: &FieldMap) -> Result<Option<u64>, ()> {- let header_val = match fields.get(hyper::header::CONTENT_LENGTH) {- Some(val) => val,- None => return Ok(None),- };-- let header_str = match header_val.to_str() {- Ok(val) => val,- Err(_) => return Err(()),- };-- match header_str.parse() {- Ok(len) => Ok(Some(len)),- Err(_) => Err(()),- }-}-/// Take ownership of the underlying [`FieldMap`] associated with this fields resource. If the/// fields resource references another fields, the returned [`FieldMap`] will be cloned.fn move_fields(@@ -401,7 +378,7 @@ wherelet size = match get_content_length(&req.headers) {Ok(size) => size,- Err(e) => return Ok(Err(e)),+ Err(..) => return Ok(Err(())),};let (host_body, hyper_body) =@@ -767,7 +744,7 @@ wherelet size = match get_content_length(&resp.headers) {Ok(size) => size,- Err(e) => return Ok(Err(e)),+ Err(..) => return Ok(Err(())),};let (host, body) =diff --git a/crates/wasi-http/tests/all/http_server.rs b/crates/wasi-http/tests/all/http_server.rsindex 0f90abeb57..ce10353c7b 100644--- a/crates/wasi-http/tests/all/http_server.rs+++ b/crates/wasi-http/tests/all/http_server.rs@@ -30,7 +30,7 @@ async fn test(pub struct Server {conns: usize,addr: SocketAddr,- worker: Option<JoinHandle<Result<()>>>,+ worker: Option<JoinHandle<()>>,}impl Server {@@ -59,13 +59,18 @@ impl Server {rt.block_on(async move {for i in 0..conns {debug!(i, "preparing to accept connection");- let (stream, _) = listener.accept().await.map_err(anyhow::Error::from)?;- if let Err(err) = run(TokioIo::new(stream)).await {- warn!(i, ?err, "failed to serve connection");- return Err(err);- }+ match listener.accept().await {+ Ok((stream, ..)) => {+ debug!(i, "accepted connection");+ if let Err(err) = run(TokioIo::new(stream)).await {+ warn!(i, ?err, "failed to serve connection");+ }+ }+ Err(err) => {+ warn!(i, ?err, "failed to accept connection");+ }+ };}- Ok(())})});Ok(Self {@@ -123,16 +128,7 @@ impl Drop for Server {// Force a connection to happen in case one hasn't happened already.let _ = TcpStream::connect(&self.addr);}-- // If the worker fails with an error, report it here but don't panic.- // Some tests don't make a connection so the error will be that the tcp- // stream created above is closed immediately afterwards. Let the test- // independently decide if it failed or not, and this should be in the- // logs to assist with debugging if necessary.- let worker = self.worker.take().unwrap();- if let Err(e) = worker.join().unwrap() {- eprintln!("worker failed with error {e:?}");- }+ self.worker.take().unwrap().join().unwrap();}}diff --git a/crates/wasi-http/tests/all/p3/mod.rs b/crates/wasi-http/tests/all/p3/mod.rsindex 0ad5a264f4..d87b6dadfd 100644--- a/crates/wasi-http/tests/all/p3/mod.rs+++ b/crates/wasi-http/tests/all/p3/mod.rs@@ -220,7 +220,6 @@ async fn p3_http_outbound_request_response_build() -> anyhow::Result<()> {}#[test_log::test(tokio::test(flavor = "multi_thread"))]-#[ignore = "FIXME #11656"]async fn p3_http_outbound_request_content_length() -> anyhow::Result<()> {let server = Server::http1(3)?;run_cli(P3_HTTP_OUTBOUND_REQUEST_CONTENT_LENGTH_COMPONENT, &server).await