LibWeb: Implement Web::Fetch::Body::fully_read() closer to spec

By actually using streams, they get marked as disturbed and the
`.bodyUsed` API starts to work. Fixes at least 94 subtests in the WPT
`fetch/api/request` test suite.

Co-authored-by: Timothy Flynn <trflynn89@pm.me>
This commit is contained in:
Jelle Raaijmakers 2024-11-04 14:11:07 +01:00 committed by Andrew Kaster
parent 9396a643b8
commit 17d5dfe597
Notes: github-actions[bot] 2024-12-10 03:03:47 +00:00

View file

@ -1,10 +1,12 @@
/*
* Copyright (c) 2022-2023, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2024, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/BodyInit.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
@ -12,7 +14,6 @@
#include <LibWeb/Fetch/Infrastructure/Task.h>
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
#include <LibWeb/Streams/AbstractOperations.h>
#include <LibWeb/WebIDL/Promise.h>
namespace Web::Fetch::Infrastructure {
@ -65,43 +66,40 @@ GC::Ref<Body> Body::clone(JS::Realm& realm)
// https://fetch.spec.whatwg.org/#body-fully-read
void Body::fully_read(JS::Realm& realm, Web::Fetch::Infrastructure::Body::ProcessBodyCallback process_body, Web::Fetch::Infrastructure::Body::ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const
{
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
// FIXME: 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
// FIXME: Handle 'parallel queue' task destination
VERIFY(!task_destination.has<Empty>());
auto task_destination_object = task_destination.get<GC::Ref<JS::Object>>();
// 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
auto success_steps = [&realm, process_body, task_destination_object = task_destination_object](ReadonlyBytes bytes) -> ErrorOr<void> {
// Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
auto bytes_copy = TRY(ByteBuffer::copy(bytes));
queue_fetch_task(*task_destination_object, GC::create_function(realm.heap(), [process_body, bytes_copy = move(bytes_copy)]() mutable {
process_body->function()(move(bytes_copy));
auto success_steps = [&realm, process_body, task_destination_object](ByteBuffer bytes) {
queue_fetch_task(*task_destination_object, GC::create_function(realm.heap(), [process_body, bytes = move(bytes)]() mutable {
process_body->function()(move(bytes));
}));
return {};
};
// 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given exception, with taskDestination.
auto error_steps = [&realm, process_body_error, task_destination_object](GC::Ptr<WebIDL::DOMException> exception) {
// 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given
// exception, with taskDestination.
auto error_steps = [&realm, process_body_error, task_destination_object](JS::Value exception) {
queue_fetch_task(*task_destination_object, GC::create_function(realm.heap(), [process_body_error, exception]() {
process_body_error->function()(exception);
}));
};
// 4. Let reader be the result of getting a reader for bodys stream. If that threw an exception, then run errorSteps with that exception and return.
// 4. Let reader be the result of getting a reader for bodys stream. If that threw an exception, then run errorSteps
// with that exception and return.
auto reader = Streams::acquire_readable_stream_default_reader(m_stream);
if (reader.is_exception()) {
auto throw_completion = Bindings::dom_exception_to_throw_completion(realm.vm(), reader.release_error());
error_steps(throw_completion.release_value().value());
return;
}
// 5. Read all bytes from reader, given successSteps and errorSteps.
// FIXME: Use streams for these steps.
m_source.visit(
[&](ByteBuffer const& byte_buffer) {
if (auto result = success_steps(byte_buffer); result.is_error())
error_steps(WebIDL::UnknownError::create(realm, "Out-of-memory"_string));
},
[&](GC::Root<FileAPI::Blob> const& blob) {
if (auto result = success_steps(blob->raw_bytes()); result.is_error())
error_steps(WebIDL::UnknownError::create(realm, "Out-of-memory"_string));
},
[&](Empty) {
error_steps(WebIDL::DOMException::create(realm, "DOMException"_fly_string, "Reading from Blob, FormData or null source is not yet implemented"_string));
});
reader.value()->read_all_bytes(GC::create_function(realm.heap(), move(success_steps)), GC::create_function(realm.heap(), move(error_steps)));
}
// https://fetch.spec.whatwg.org/#body-incrementally-read
@ -123,7 +121,6 @@ void Body::incrementally_read(ProcessBodyChunkCallback process_body_chunk, Proce
// https://fetch.spec.whatwg.org/#incrementally-read-loop
void Body::incrementally_read_loop(Streams::ReadableStreamDefaultReader& reader, GC::Ref<JS::Object> task_destination, ProcessBodyChunkCallback process_body_chunk, ProcessEndOfBodyCallback process_end_of_body, ProcessBodyErrorCallback process_body_error)
{
auto& realm = reader.realm();
// 1. Let readRequest be the following read request: