Truly independent web browser
LibWeb: Keep response bodies readable byte streams
Andreas Kling9c5f0310ecf2
diff --git a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rsindex 42beebadedd..6fb37d67279 100644--- a/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/streams/exports.rs@@ -158,6 +158,21 @@ pub unsafe extern "C" fn web_rust_readable_stream_close_requested(stream: *mut c}}+/// Whether the stream's controller is a ReadableByteStreamController.+///+/// # Safety+///+/// `stream` must be a live ReadableStream.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn web_rust_readable_stream_is_readable_byte_stream(stream: *mut c_void) -> bool {+ // SAFETY: The caller passes a live ReadableStream.+ let stream = unsafe { cell_from_abi::<ReadableStream>(stream) };+ matches!(+ stream.controller(),+ Some(ReadableStreamController::ReadableByteStreamController(_))+ )+}+/// https://streams.spec.whatwg.org/#readablestream-enqueue////// # Safetydiff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex c581458faee..aa590794d26 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -101,6 +101,62 @@ static bool g_disk_cache_enabled_for_navigations_for_testing = false;_temporary_result.release_value(); \})+// AD-HOC: Reads one chunk of a response body's readable byte stream and forwards it into another readable byte stream,+// running processResponseEndOfBody once the stream has no more chunks. See fetch_response_handover().+class ResponseBodyForwardingReadRequest final : public Streams::ReadRequest {+ GC_CELL(ResponseBodyForwardingReadRequest, Streams::ReadRequest);+ GC_DECLARE_ALLOCATOR(ResponseBodyForwardingReadRequest);++public:+ ResponseBodyForwardingReadRequest(GC::Ref<Streams::ReadableStream> destination, GC::Ref<WebIDL::Promise> pull_promise, GC::Ref<GC::Function<void()>> process_response_end_of_body)+ : m_destination(destination)+ , m_pull_promise(pull_promise)+ , m_process_response_end_of_body(process_response_end_of_body)+ {+ }++ virtual void on_chunk(JS::Value chunk) override+ {+ if (m_destination->enqueue(chunk).is_error())+ m_destination->error(JS::TypeError::create(WebIDL::promise_realm(*m_pull_promise), "Response body chunk could not be enqueued"sv));+ WebIDL::resolve_promise(*m_pull_promise);+ }++ virtual void on_close() override+ {+ // NB: Canceling the destination cancels the source, which closes this read request. As with a TransformStream+ // whose readable side is canceled, processResponseEndOfBody does not run then.+ if (m_destination->is_readable()) {+ // NB: Like the flush algorithm of a TransformStream, run processResponseEndOfBody before the stream closes,+ // so that it runs before anything that waits for the stream to close.+ m_process_response_end_of_body->function()();+ m_destination->close();+ }+ WebIDL::resolve_promise(*m_pull_promise);+ }++ virtual void on_error(JS::Value error) override+ {+ m_destination->error(error);+ WebIDL::resolve_promise(*m_pull_promise);+ }++private:+ virtual void visit_edges(Visitor& visitor) override+ {+ Base::visit_edges(visitor);+ visitor.visit(m_destination);+ visitor.visit(m_pull_promise);+ visitor.visit(m_process_response_end_of_body);+ }++ GC::Ref<Streams::ReadableStream> m_destination;+ GC::Ref<WebIDL::Promise> m_pull_promise;+ GC::Ref<GC::Function<void()>> m_process_response_end_of_body;+};++GC_DEFINE_ALLOCATOR(ResponseBodyForwardingReadRequest);+class HTTPCache {public:AK_ALLOC_WITH_KMALLOC;@@ -1001,27 +1057,52 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams constcase Engine::BodyIntent::ConsumeForCallback: {HTML::TemporaryExecutionContext const execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };- // 1. Let transformStream be a new TransformStream.- auto transform_stream = Streams::TransformStream::create(realm);+ // AD-HOC: Piping a readable byte stream through a TransformStream results in a stream without byte reading+ // support, which can no longer be read with a BYOB reader. Response bodies remain readable byte streams+ // in other engines, so forward the chunks of such a stream into a new readable byte stream instead, and+ // run processResponseEndOfBody once there are no more chunks, as the flush algorithm below would.+ if (auto stream = internal_response->body()->stream(); stream->is_readable_byte_stream() && !stream->is_locked()) {+ auto reader = MUST(stream->get_a_reader());+ auto forwarded_stream = Streams::ReadableStream::create(realm);+ auto end_of_body_steps = GC::create_function(GC::Heap::the(), [process_response_end_of_body] {+ process_response_end_of_body();+ });- // 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.- auto identity_transform_algorithm = GC::create_function(GC::Heap::the(), [&realm, transform_stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise> {- if (auto result = transform_stream->enqueue(chunk); result.is_error())- return WebIDL::create_rejected_promise_from_exception(realm, result.release_error());+ auto pull_algorithm = GC::create_function(GC::Heap::the(), [&realm, reader, forwarded_stream, end_of_body_steps] {+ auto promise = WebIDL::create_promise(realm);+ reader->read_a_chunk(*GC::Heap::the().allocate<ResponseBodyForwardingReadRequest>(forwarded_stream, promise, end_of_body_steps));+ return promise;+ });- return WebIDL::create_resolved_promise(realm, JS::js_undefined());- });+ auto cancel_algorithm = GC::create_function(GC::Heap::the(), [reader](JS::Value reason) {+ return reader->cancel(reason);+ });- // 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm set- // to processResponseEndOfBody.- auto flush_algorithm = GC::create_function(GC::Heap::the(), [&realm, process_response_end_of_body]() -> GC::Ref<WebIDL::Promise> {- process_response_end_of_body();- return WebIDL::create_resolved_promise(realm, JS::js_undefined());- });- transform_stream->set_up(realm, identity_transform_algorithm, flush_algorithm);+ forwarded_stream->set_up_with_byte_reading_support(realm, pull_algorithm, cancel_algorithm);+ internal_response->body()->set_stream(forwarded_stream);+ } else {+ // 1. Let transformStream be a new TransformStream.+ auto transform_stream = Streams::TransformStream::create(realm);++ // 2. Let identityTransformAlgorithm be an algorithm which, given chunk, enqueues chunk in transformStream.+ auto identity_transform_algorithm = GC::create_function(GC::Heap::the(), [&realm, transform_stream](JS::Value chunk) -> GC::Ref<WebIDL::Promise> {+ if (auto result = transform_stream->enqueue(chunk); result.is_error())+ return WebIDL::create_rejected_promise_from_exception(realm, result.release_error());++ return WebIDL::create_resolved_promise(realm, JS::js_undefined());+ });++ // 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm set+ // to processResponseEndOfBody.+ auto flush_algorithm = GC::create_function(GC::Heap::the(), [&realm, process_response_end_of_body]() -> GC::Ref<WebIDL::Promise> {+ process_response_end_of_body();+ return WebIDL::create_resolved_promise(realm, JS::js_undefined());+ });+ transform_stream->set_up(realm, identity_transform_algorithm, flush_algorithm);- // 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.- internal_response->body()->set_stream(internal_response->body()->stream()->piped_through(transform_stream));+ // 4. Set internalResponse’s body’s stream to the result of internalResponse’s body’s stream piped through transformStream.+ internal_response->body()->set_stream(internal_response->body()->stream()->piped_through(transform_stream));+ }break;}case Engine::BodyIntent::DrainAndDiscard: {diff --git a/Libraries/LibWeb/Streams/ReadableStream.cpp b/Libraries/LibWeb/Streams/ReadableStream.cppindex 7c86958c1ba..1642f3a1802 100644--- a/Libraries/LibWeb/Streams/ReadableStream.cpp+++ b/Libraries/LibWeb/Streams/ReadableStream.cpp@@ -36,6 +36,7 @@ u8 web_rust_readable_stream_state(void* stream);bool web_rust_readable_stream_is_locked(void* stream);bool web_rust_readable_stream_is_disturbed(void* stream);bool web_rust_readable_stream_close_requested(void* stream);+bool web_rust_readable_stream_is_readable_byte_stream(void* stream);JSCompletion web_rust_readable_stream_enqueue(JSVM*, void* stream, JSValue chunk);JSCompletion web_rust_readable_stream_enqueue_native_bytes(JSVM*, void* stream, ReadonlyBytes);bool web_rust_readable_stream_current_byob_request_view_byte_length(JSVM*, void* stream, u64* byte_length);@@ -110,6 +111,11 @@ void ReadableStream::set_up_with_byte_reading_support(JS::Realm& realm, GC::Ptr<web_rust_readable_stream_set_up_with_byte_reading_support(vm_abi(), &realm, this, pull_algorithm.ptr(), cancel_algorithm.ptr(), high_water_mark);}+bool ReadableStream::is_readable_byte_stream() const+{+ return web_rust_readable_stream_is_readable_byte_stream(const_cast<ReadableStream*>(this));+}+bool ReadableStream::is_readable() const{return web_rust_readable_stream_state(const_cast<ReadableStream*>(this)) == 0;diff --git a/Libraries/LibWeb/Streams/ReadableStream.h b/Libraries/LibWeb/Streams/ReadableStream.hindex 7fa20a5b4dd..73f6bfb5068 100644--- a/Libraries/LibWeb/Streams/ReadableStream.h+++ b/Libraries/LibWeb/Streams/ReadableStream.h@@ -37,6 +37,9 @@ public:// https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-supportvoid set_up_with_byte_reading_support(JS::Realm&, GC::Ptr<PullAlgorithm> = {}, GC::Ptr<CancelAlgorithm> = {}, double high_water_mark = 0);+ // Whether the stream's controller is a ReadableByteStreamController.+ bool is_readable_byte_stream() const;+// https://streams.spec.whatwg.org/#readablestream-readablebool is_readable() const;diff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.sharedworker.txtindex 0e9f7ba08a1..4bd1cda79ad 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.sharedworker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.sharedworker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large br data should be decompressed successfully-Fail large br data should be decompressed successfully with byte stream\ No newline at end of file+Pass large br data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.txtindex 0e9f7ba08a1..4bd1cda79ad 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large br data should be decompressed successfully-Fail large br data should be decompressed successfully with byte stream\ No newline at end of file+Pass large br data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.worker.txtindex 0e9f7ba08a1..4bd1cda79ad 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/br/big-br-body.https.any.worker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large br data should be decompressed successfully-Fail large br data should be decompressed successfully with byte stream\ No newline at end of file+Pass large br data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.sharedworker.txtindex c20ed3553fd..ad1a1aaa001 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.sharedworker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.sharedworker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large gzip data should be decompressed successfully-Fail large gzip data should be decompressed successfully with byte stream\ No newline at end of file+Pass large gzip data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.txtindex c20ed3553fd..ad1a1aaa001 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large gzip data should be decompressed successfully-Fail large gzip data should be decompressed successfully with byte stream\ No newline at end of file+Pass large gzip data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.worker.txtindex c20ed3553fd..ad1a1aaa001 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/gzip/big-gzip-body.https.any.worker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large gzip data should be decompressed successfully-Fail large gzip data should be decompressed successfully with byte stream\ No newline at end of file+Pass large gzip data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.sharedworker.txtindex 025e830e6b0..8f55c70757b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.sharedworker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.sharedworker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large zstd data should be decompressed successfully-Fail large zstd data should be decompressed successfully with byte stream\ No newline at end of file+Pass large zstd data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.txtindex 025e830e6b0..8f55c70757b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large zstd data should be decompressed successfully-Fail large zstd data should be decompressed successfully with byte stream\ No newline at end of file+Pass large zstd data should be decompressed successfully with byte stream\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.worker.txtindex 025e830e6b0..8f55c70757b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.worker.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/fetch/content-encoding/zstd/big-zstd-body.https.any.worker.txt@@ -2,7 +2,6 @@ Harness status: OKFound 2 tests-1 Pass-1 Fail+2 PassPass large zstd data should be decompressed successfully-Fail large zstd data should be decompressed successfully with byte stream\ No newline at end of file+Pass large zstd data should be decompressed successfully with byte stream\ No newline at end of file