A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
wasi-http: restore p2 `http-error-code` for body stream errors (#14667)
Roman Volosatovs0a09e4711ddb
diff --git a/crates/test-programs/src/bin/p2_http_outbound_request_between_bytes_timeout.rs b/crates/test-programs/src/bin/p2_http_outbound_request_between_bytes_timeout.rsindex e9df552d5c..dbd431bc9e 100644--- a/crates/test-programs/src/bin/p2_http_outbound_request_between_bytes_timeout.rs+++ b/crates/test-programs/src/bin/p2_http_outbound_request_between_bytes_timeout.rs@@ -4,7 +4,7 @@//! timeout, so reading the second chunk has to fail rather than wait for it.//! `http.wit` documents these timeouts as transport-layer timeouts, so the expected//! failure is a read timeout — and `io.wit` warns that the debug string of an error-//! "should not be consumed mechanically", so this only asserts the error's kind.+//! "should not be consumed mechanically", so this asserts the code via `http-error-code`.use anyhow::{Result, anyhow};use test_programs::wasi::http::{outgoing_handler, types as http_types};use test_programs::wasi::io::streams;@@ -70,9 +70,13 @@ fn main() -> Result<()> {let err = stream.read(1024).expect_err("reading the stalled second chunk should have timed out");+ let streams::StreamError::LastOperationFailed(err) = err else {+ panic!("expected the read to fail with a last-operation failure: {err:?}");+ };+ let code = http_types::http_error_code(&err);assert!(- matches!(err, streams::StreamError::LastOperationFailed(_)),- "expected the read to fail with a last-operation failure: {err:?}"+ matches!(code, Some(http_types::ErrorCode::ConnectionReadTimeout)),+ "expected a connection read timeout: {code:?}");Ok(())diff --git a/crates/wasi-http/src/p2/error.rs b/crates/wasi-http/src/p2/error.rsindex 5423474cc1..15ae831a19 100644--- a/crates/wasi-http/src/p2/error.rs+++ b/crates/wasi-http/src/p2/error.rs@@ -1,5 +1,5 @@use crate::p2::bindings::http::types::{self, ErrorCode};-use crate::{Error, FieldMapError, WasiHttpCtxView};+use crate::{Error, FieldMapError, WasiHttpHooks};use std::error::Error as _;use std::fmt;use std::io::ErrorKind;@@ -273,8 +273,8 @@ impl From<ErrorCode> for Error {}}-impl WasiHttpCtxView<'_> {- pub(crate) fn error_to_p2(&mut self, e: Error) -> ErrorCode {+impl dyn WasiHttpHooks + '_ {+ pub(crate) fn error_to_p2(&mut self, e: &Error) -> ErrorCode {match e {Error::Hyper(err) => {// If there's a source, we might be able to extract a wasi-http error from it.@@ -282,9 +282,12 @@ impl WasiHttpCtxView<'_> {if let Some(err) = cause.downcast_ref::<ErrorCode>() {return err.clone();}+ if let Some(err) = cause.downcast_ref::<Error>() {+ return self.error_to_p2(err);+ }}- self.hooks.p2_error_from_hyper(&err)+ self.p2_error_from_hyper(err)}Error::Connect(err) => {if err.kind() == ErrorKind::AddrNotAvailable {@@ -304,15 +307,16 @@ impl WasiHttpCtxView<'_> {});}- self.hooks.p2_error_from_connect(&err)+ self.p2_error_from_connect(err)}- Error::Tls(err) => self.hooks.p2_error_from_tls(&err),+ Error::Tls(err) => self.p2_error_from_tls(err),#[cfg(feature = "default-send-request")]- Error::InvalidDnsNameError(err) => self.hooks.p2_error_from_dns(&err),+ Error::InvalidDnsNameError(err) => self.p2_error_from_dns(err),Error::DnsTimeout => ErrorCode::DnsTimeout,- Error::DnsError { rcode, info_code } => {- ErrorCode::DnsError(types::DnsErrorPayload { rcode, info_code })- }+ Error::DnsError { rcode, info_code } => ErrorCode::DnsError(types::DnsErrorPayload {+ rcode: rcode.clone(),+ info_code: *info_code,+ }),Error::DestinationNotFound => ErrorCode::DestinationNotFound,Error::DestinationUnavailable => ErrorCode::DestinationUnavailable,Error::DestinationIpProhibited => ErrorCode::DestinationIpProhibited,@@ -329,69 +333,69 @@ impl WasiHttpCtxView<'_> {alert_id,alert_message,} => ErrorCode::TlsAlertReceived(types::TlsAlertReceivedPayload {- alert_id,- alert_message,+ alert_id: *alert_id,+ alert_message: alert_message.clone(),}),Error::HttpRequestDenied => ErrorCode::HttpRequestDenied,Error::HttpRequestLengthRequired => ErrorCode::HttpRequestLengthRequired,- Error::HttpRequestBodySize(payload) => ErrorCode::HttpRequestBodySize(payload),+ Error::HttpRequestBodySize(payload) => ErrorCode::HttpRequestBodySize(*payload),Error::HttpRequestMethodInvalid => ErrorCode::HttpRequestMethodInvalid,Error::HttpRequestUriInvalid => ErrorCode::HttpRequestUriInvalid,Error::HttpRequestUriTooLong => ErrorCode::HttpRequestUriTooLong,Error::HttpRequestHeaderSectionSize(payload) => {- ErrorCode::HttpRequestHeaderSectionSize(payload)+ ErrorCode::HttpRequestHeaderSectionSize(*payload)}Error::HttpRequestHeaderSize {field_name,field_size,} => ErrorCode::HttpRequestHeaderSize(Some(types::FieldSizePayload {- field_name,- field_size,+ field_name: field_name.clone(),+ field_size: *field_size,})),Error::HttpRequestTrailerSectionSize(payload) => {- ErrorCode::HttpRequestTrailerSectionSize(payload)+ ErrorCode::HttpRequestTrailerSectionSize(*payload)}Error::HttpRequestTrailerSize {field_name,field_size,} => ErrorCode::HttpRequestTrailerSize(types::FieldSizePayload {- field_name,- field_size,+ field_name: field_name.clone(),+ field_size: *field_size,}),Error::HttpResponseIncomplete => ErrorCode::HttpResponseIncomplete,Error::HttpResponseHeaderSectionSize(payload) => {- ErrorCode::HttpResponseHeaderSectionSize(payload)+ ErrorCode::HttpResponseHeaderSectionSize(*payload)}Error::HttpResponseHeaderSize {field_name,field_size,} => ErrorCode::HttpResponseHeaderSize(types::FieldSizePayload {- field_name,- field_size,+ field_name: field_name.clone(),+ field_size: *field_size,}),- Error::HttpResponseBodySize(payload) => ErrorCode::HttpResponseBodySize(payload),+ Error::HttpResponseBodySize(payload) => ErrorCode::HttpResponseBodySize(*payload),Error::HttpResponseTrailerSectionSize(payload) => {- ErrorCode::HttpResponseTrailerSectionSize(payload)+ ErrorCode::HttpResponseTrailerSectionSize(*payload)}Error::HttpResponseTrailerSize {field_name,field_size,} => ErrorCode::HttpResponseTrailerSize(types::FieldSizePayload {- field_name,- field_size,+ field_name: field_name.clone(),+ field_size: *field_size,}),Error::HttpResponseTransferCoding(payload) => {- ErrorCode::HttpResponseTransferCoding(payload)+ ErrorCode::HttpResponseTransferCoding(payload.clone())}Error::HttpResponseContentCoding(payload) => {- ErrorCode::HttpResponseContentCoding(payload)+ ErrorCode::HttpResponseContentCoding(payload.clone())}Error::HttpResponseTimeout => ErrorCode::HttpResponseTimeout,Error::HttpUpgradeFailed => ErrorCode::HttpUpgradeFailed,Error::HttpProtocolError => ErrorCode::HttpProtocolError,Error::LoopDetected => ErrorCode::LoopDetected,Error::ConfigurationError => ErrorCode::ConfigurationError,- Error::InternalError(payload) => ErrorCode::InternalError(payload),+ Error::InternalError(payload) => ErrorCode::InternalError(payload.clone()),}}}diff --git a/crates/wasi-http/src/p2/types_impl.rs b/crates/wasi-http/src/p2/types_impl.rsindex 9bae88c367..7e968f469a 100644--- a/crates/wasi-http/src/p2/types_impl.rs+++ b/crates/wasi-http/src/p2/types_impl.rs@@ -28,6 +28,9 @@ impl types::Host for WasiHttpCtxView<'_> {err: wasmtime::component::Resource<types::IoError>,) -> wasmtime::Result<Option<types::ErrorCode>> {let e = self.table.get(&err)?;+ if let Some(e) = e.downcast_ref::<crate::Error>() {+ return Ok(Some(self.hooks.error_to_p2(e)));+ }Ok(e.downcast_ref::<types::ErrorCode>().cloned())}}@@ -475,7 +478,7 @@ impl types::HostFutureTrailers for WasiHttpCtxView<'_> {Ok(Some(fields)) => fields,Ok(None) => return Ok(Some(Ok(Ok(None)))),Err(e) => {- let e = self.error_to_p2(e);+ let e = self.hooks.error_to_p2(&e);return Ok(Some(Ok(Err(e))));}};@@ -623,7 +626,7 @@ impl types::HostFutureIncomingResponse for WasiHttpCtxView<'_> {match std::mem::replace(resp, HostFutureIncomingResponse::Consumed).unwrap_ready() {Ok(pair) => pair,Err(e) => {- let e = self.error_to_p2(e);+ let e = self.hooks.error_to_p2(&e);return Ok(Some(Ok(Err(e))));}};