From 66959bcc094e0739f7849d3fc25ca3b5e6675d46 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 28 Jan 2024 14:21:32 -0500 Subject: [PATCH] LibWeb: Use the stream teeing methods for cloning Fetch response bodies --- .../Fetch/Infrastructure/HTTP/Bodies.cpp | 27 ++++--------------- .../LibWeb/Fetch/Infrastructure/HTTP/Bodies.h | 2 +- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 48d7426118..0702a58d9f 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -4,7 +4,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include @@ -47,32 +46,16 @@ void Body::visit_edges(Cell::Visitor& visitor) } // https://fetch.spec.whatwg.org/#concept-body-clone -JS::NonnullGCPtr Body::clone(JS::Realm& realm) const +JS::NonnullGCPtr Body::clone(JS::Realm& realm) { HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(realm), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes }; // To clone a body body, run these steps: - // FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream. - // FIXME: 2. Set body’s stream to out1. - JS::GCPtr out2; + // 1. Let « out1, out2 » be the result of teeing body’s stream. + auto [out1, out2] = m_stream->tee().release_value_but_fixme_should_propagate_errors(); - auto start_algorithm = JS::create_heap_function(realm.heap(), []() -> WebIDL::ExceptionOr { - return JS::js_undefined(); - }); - - auto pull_algorithm = JS::create_heap_function(realm.heap(), [&realm]() -> WebIDL::ExceptionOr> { - return WebIDL::create_resolved_promise(realm, JS::js_undefined()); - }); - - auto cancel_algorithm = JS::create_heap_function(realm.heap(), [&realm](JS::Value) -> WebIDL::ExceptionOr> { - return WebIDL::create_resolved_promise(realm, JS::js_undefined()); - }); - - if (m_stream->controller()->has>()) { - out2 = Streams::create_readable_stream(realm, start_algorithm, pull_algorithm, cancel_algorithm).release_value_but_fixme_should_propagate_errors(); - } else { - out2 = Streams::create_readable_byte_stream(realm, start_algorithm, pull_algorithm, cancel_algorithm).release_value_but_fixme_should_propagate_errors(); - } + // 2. Set body’s stream to out1. + m_stream = out1; // 3. Return a body whose stream is out2 and other members are copied from body. return Body::create(realm.vm(), *out2, m_source, m_length); diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h index dac79909c3..817dd9dc9f 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h @@ -39,7 +39,7 @@ public: [[nodiscard]] SourceType const& source() const { return m_source; } [[nodiscard]] Optional const& length() const { return m_length; } - [[nodiscard]] JS::NonnullGCPtr clone(JS::Realm&) const; + [[nodiscard]] JS::NonnullGCPtr clone(JS::Realm&); WebIDL::ExceptionOr fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;