Fix Response:bodyUsed behavior to return false if the body is null (#39287)

This PR fixes some test-cases in
https://wpt.fyi/results/fetch/api/response/response-consume-empty.any.html?product=servo

Fetch standard indicates `Response:bodyUsed` should return `false` if
the body content is null , even if the stream was disturbed.
https://fetch.spec.whatwg.org/#dom-body-bodyused

> The bodyUsed getter steps are to return true if
[this](https://webidl.spec.whatwg.org/#this)’s
[body](https://fetch.spec.whatwg.org/#concept-body-body) is non-null and
[this](https://webidl.spec.whatwg.org/#this)’s
[body](https://fetch.spec.whatwg.org/#concept-body-body)’s
[stream](https://fetch.spec.whatwg.org/#concept-body-stream) is
[disturbed](https://streams.spec.whatwg.org/#is-readable-stream-disturbed);
otherwise false.

---------

Signed-off-by: araya <araya@araya.dev>
This commit is contained in:
araya 2025-09-14 17:33:54 +09:00 committed by GitHub
parent a0c3dcefe4
commit 2947476bdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 43 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::rc::Rc;
use std::str::FromStr;
@ -48,6 +49,7 @@ pub(crate) struct Response {
#[ignore_malloc_size_of = "StreamConsumer"]
stream_consumer: DomRefCell<Option<StreamConsumer>>,
redirected: DomRefCell<bool>,
is_body_empty: Cell<bool>,
}
#[allow(non_snake_case)]
@ -69,6 +71,7 @@ impl Response {
body_stream: MutNullableDom::new(Some(&*stream)),
stream_consumer: DomRefCell::new(None),
redirected: DomRefCell::new(false),
is_body_empty: Cell::new(true),
}
}
@ -153,6 +156,9 @@ impl ResponseMethods<crate::DomTypeHolder> for Response {
// 1. Set thiss response to a new response.
// Our Response/Body types don't actually hold onto an internal fetch Response.
let response = Response::new_with_proto(global, proto, can_gc);
if body_init.is_some() {
response.is_body_empty.set(false);
}
// 2. Set thiss headers to a new Headers object with thiss relevant realm,
// whose header list is thiss responses header list and guard is "response".
@ -324,6 +330,7 @@ impl ResponseMethods<crate::DomTypeHolder> for Response {
if let Some(stream) = self.body_stream.get().clone() {
new_response.body_stream.set(Some(&*stream));
}
new_response.is_body_empty.set(self.is_body_empty.get());
// Step 3
// TODO: This step relies on promises, which are still unimplemented.
@ -334,7 +341,8 @@ impl ResponseMethods<crate::DomTypeHolder> for Response {
/// <https://fetch.spec.whatwg.org/#dom-body-bodyused>
fn BodyUsed(&self) -> bool {
self.is_disturbed()
// bodyUsed returns true only if body is non-null
!self.is_body_empty() && self.is_disturbed()
}
/// <https://fetch.spec.whatwg.org/#dom-body-body>
@ -421,6 +429,7 @@ fn initialize_response(
// 6.2 Set responses body to bodys body.
response.body_stream.set(Some(&*body.stream));
response.is_body_empty.set(false);
// 6.3 If bodys type is non-null and responses header list does not contain `Content-Type`,
// then append (`Content-Type`, bodys type) to responses header list.
@ -509,6 +518,7 @@ impl Response {
}
pub(crate) fn stream_chunk(&self, chunk: Vec<u8>, can_gc: CanGc) {
self.is_body_empty.set(false);
// Note, are these two actually mutually exclusive?
if let Some(stream_consumer) = self.stream_consumer.borrow().as_ref() {
stream_consumer.consume_chunk(chunk.as_slice());
@ -527,4 +537,8 @@ impl Response {
stream_consumer.stream_end();
}
}
pub(crate) fn is_body_empty(&self) -> bool {
self.is_body_empty.get()
}
}

View file

@ -5,52 +5,10 @@
expected: ERROR
[response-consume-empty.any.worker.html]
[Consume response's body as text]
expected: FAIL
[Consume response's body as json (error case)]
expected: FAIL
[Consume response's body as blob]
expected: FAIL
[Consume response's body as formData without correct type (error case)]
expected: FAIL
[Consume response's body as arrayBuffer]
expected: FAIL
[Consume empty FormData response body as text]
expected: FAIL
[Consume response's body as formData with correct multipart type (error case)]
expected: FAIL
[Consume response's body as formData with correct urlencoded type]
expected: FAIL
[response-consume-empty.any.html]
[Consume response's body as text]
expected: FAIL
[Consume response's body as json (error case)]
expected: FAIL
[Consume response's body as blob]
expected: FAIL
[Consume response's body as formData without correct type (error case)]
expected: FAIL
[Consume response's body as arrayBuffer]
expected: FAIL
[Consume empty FormData response body as text]
expected: FAIL
[Consume response's body as formData with correct multipart type (error case)]
expected: FAIL
[Consume response's body as formData with correct urlencoded type]
expected: FAIL