From 00b80a3489b314534d61f3b713f3b6cbc52bb915 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Sun, 22 Feb 2026 02:11:26 +0100 Subject: [PATCH] Work around invalid HTTP server behavior for status 204 and 304 responses Per spec, an HTTP server isn't allowed to send a body for these status code. Nothing's stopping it from doing so nevertheless, though. This is admittedly very niche and might only actually happen in test suites. It does happen in those, leading to flaky results because whether we report an error here or not depends on whether the body is contained in the same packet as the headers or not. --- crates/wasi-http/src/p3/request.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/wasi-http/src/p3/request.rs b/crates/wasi-http/src/p3/request.rs index 63072ff8e8b7..52472774b7ea 100644 --- a/crates/wasi-http/src/p3/request.rs +++ b/crates/wasi-http/src/p3/request.rs @@ -465,8 +465,15 @@ pub async fn default_send_request( match res { // `hyper` connection has successfully completed, optimistically poll for response Ok(()) => send.as_mut().poll(cx), - // `hyper` connection has failed, return the error - Err(err) => Poll::Ready(Err(ErrorCode::from_hyper_request_error(err))), + // `hyper` connection has failed. However, the error may have happened after the + // response was fully received. That can happen when a server sends a body for a + // 204 or 304 response, in violation of the HTTP spec. + // In that case, do what browsers do* and ignore the connection error. + // *: https://github.com/web-platform-tests/wpt/blob/master/fetch/api/basic/response-null-body.any.js + Err(err) => match send.as_mut().poll(cx) { + Poll::Ready(Ok(res)) => Poll::Ready(Ok(res)), + _ => Poll::Ready(Err(ErrorCode::from_hyper_request_error(err))), + }, } } })