From a7164f2674fa19222bbfa3f2d14be8418ba19b1a Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 25 Sep 2022 19:25:53 +0100 Subject: [PATCH] LibWeb: Stub out Fetch::Infrastructure::Body::clone() --- .../Fetch/Infrastructure/HTTP/Bodies.cpp | 18 ++++++++++++++++++ .../LibWeb/Fetch/Infrastructure/HTTP/Bodies.h | 2 ++ .../Fetch/Infrastructure/HTTP/Requests.cpp | 14 ++++++++------ .../Fetch/Infrastructure/HTTP/Requests.h | 2 +- .../Fetch/Infrastructure/HTTP/Responses.cpp | 14 ++++++++------ .../Fetch/Infrastructure/HTTP/Responses.h | 2 +- 6 files changed, 38 insertions(+), 14 deletions(-) diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 2cf23df0b6..32737dc9ca 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include namespace Web::Fetch::Infrastructure { @@ -20,4 +21,21 @@ Body::Body(JS::Handle stream, SourceType source, Option { } +// https://fetch.spec.whatwg.org/#concept-body-clone +WebIDL::ExceptionOr Body::clone() const +{ + // To clone a body body, run these steps: + + auto& vm = Bindings::main_thread_vm(); + auto& realm = *vm.current_realm(); + auto& window = verify_cast(realm.global_object()); + + // FIXME: 1. Let « out1, out2 » be the result of teeing body’s stream. + // FIXME: 2. Set body’s stream to out1. + auto* out2 = vm.heap().allocate(realm, window); + + // 3. Return a body whose stream is out2 and other members are copied from body. + return Body { JS::make_handle(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 186b229e3c..9c5a742902 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h @@ -29,6 +29,8 @@ public: [[nodiscard]] SourceType const& source() const { return m_source; } [[nodiscard]] Optional const& length() const { return m_length; } + [[nodiscard]] WebIDL::ExceptionOr clone() const; + private: // https://fetch.spec.whatwg.org/#concept-body-stream // A stream (a ReadableStream object). diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp index 74f4ec7bc9..cee01c4fe3 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp @@ -158,17 +158,19 @@ ErrorOr Request::byte_serialize_origin() const } // https://fetch.spec.whatwg.org/#concept-request-clone -NonnullOwnPtr Request::clone() const +WebIDL::ExceptionOr> Request::clone() const { // To clone a request request, run these steps: // 1. Let newRequest be a copy of request, except for its body. - BodyType body; - swap(body, const_cast(m_body)); - auto new_request = adopt_own(*new Infrastructure::Request(*this)); - swap(body, const_cast(m_body)); + BodyType tmp_body; + swap(tmp_body, const_cast(m_body)); + auto new_request = make(*this); + swap(tmp_body, const_cast(m_body)); - // FIXME: 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body. + // 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body. + if (auto const* body = m_body.get_pointer()) + new_request->set_body(TRY(body->clone())); // 3. Return newRequest. return new_request; diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h index 3ac271c361..72c2232088 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h @@ -287,7 +287,7 @@ public: [[nodiscard]] String serialize_origin() const; [[nodiscard]] ErrorOr byte_serialize_origin() const; - [[nodiscard]] NonnullOwnPtr clone() const; + [[nodiscard]] WebIDL::ExceptionOr> clone() const; [[nodiscard]] ErrorOr add_range_reader(u64 first, Optional const& last); diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp index cd179682ba..0be7e4e758 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp @@ -78,19 +78,21 @@ ErrorOr> Response::location_url(Optional const& reques } // https://fetch.spec.whatwg.org/#concept-response-clone -NonnullOwnPtr Response::clone() const +WebIDL::ExceptionOr> Response::clone() const { // To clone a response response, run these steps: // FIXME: 1. If response is a filtered response, then return a new identical filtered response whose internal response is a clone of response’s internal response. // 2. Let newResponse be a copy of response, except for its body. - Optional body; - swap(body, const_cast&>(m_body)); - auto new_response = adopt_own(*new Infrastructure::Response(*this)); - swap(body, const_cast&>(m_body)); + Optional tmp_body; + swap(tmp_body, const_cast&>(m_body)); + auto new_response = make(*this); + swap(tmp_body, const_cast&>(m_body)); - // FIXME: 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. + // 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. + if (m_body.has_value()) + new_response->set_body(TRY(m_body->clone())); // 4. Return newResponse. return new_response; diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h index 825f23793c..879f0ecf32 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h @@ -100,7 +100,7 @@ public: [[nodiscard]] Optional url() const; [[nodiscard]] ErrorOr> location_url(Optional const& request_fragment) const; - [[nodiscard]] NonnullOwnPtr clone() const; + [[nodiscard]] WebIDL::ExceptionOr> clone() const; private: // https://fetch.spec.whatwg.org/#concept-response-type