From ef5e2eb79466b113fb520214753b1a22482d5854 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 25 Sep 2022 19:23:35 +0100 Subject: [PATCH] LibWeb: Implement Fetch::Infrastructure::Response::clone() --- .../Fetch/Infrastructure/HTTP/Responses.cpp | 20 +++++++++++++++++++ .../Fetch/Infrastructure/HTTP/Responses.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp index 6483b75ee2..cd179682ba 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include namespace Web::Fetch::Infrastructure { @@ -76,6 +77,25 @@ ErrorOr> Response::location_url(Optional const& reques return location; } +// https://fetch.spec.whatwg.org/#concept-response-clone +NonnullOwnPtr 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)); + + // FIXME: 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body. + + // 4. Return newResponse. + return new_response; +} + FilteredResponse::FilteredResponse(Response& internal_response) : m_internal_response(internal_response) { diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h index 0ed41efe31..825f23793c 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h @@ -100,6 +100,8 @@ public: [[nodiscard]] Optional url() const; [[nodiscard]] ErrorOr> location_url(Optional const& request_fragment) const; + [[nodiscard]] NonnullOwnPtr clone() const; + private: // https://fetch.spec.whatwg.org/#concept-response-type // A response has an associated type which is "basic", "cors", "default", "error", "opaque", or "opaqueredirect". Unless stated otherwise, it is "default".