Jelajahi Sumber

LibWeb: Stub out Fetch::Infrastructure::Body::clone()

Linus Groh 2 tahun lalu
induk
melakukan
a7164f2674

+ 18 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
 
 namespace Web::Fetch::Infrastructure {
@@ -20,4 +21,21 @@ Body::Body(JS::Handle<Streams::ReadableStream> stream, SourceType source, Option
 {
 }
 
+// https://fetch.spec.whatwg.org/#concept-body-clone
+WebIDL::ExceptionOr<Body> 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<HTML::Window>(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<Streams::ReadableStream>(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 };
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h

@@ -29,6 +29,8 @@ public:
     [[nodiscard]] SourceType const& source() const { return m_source; }
     [[nodiscard]] Optional<u64> const& length() const { return m_length; }
 
+    [[nodiscard]] WebIDL::ExceptionOr<Body> clone() const;
+
 private:
     // https://fetch.spec.whatwg.org/#concept-body-stream
     // A stream (a ReadableStream object).

+ 9 - 7
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.cpp

@@ -158,17 +158,19 @@ ErrorOr<ByteBuffer> Request::byte_serialize_origin() const
 }
 
 // https://fetch.spec.whatwg.org/#concept-request-clone
-NonnullOwnPtr<Request> Request::clone() const
+WebIDL::ExceptionOr<NonnullOwnPtr<Request>> 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<BodyType&>(m_body));
-    auto new_request = adopt_own(*new Infrastructure::Request(*this));
-    swap(body, const_cast<BodyType&>(m_body));
-
-    // FIXME: 2. If request’s body is non-null, set newRequest’s body to the result of cloning request’s body.
+    BodyType tmp_body;
+    swap(tmp_body, const_cast<BodyType&>(m_body));
+    auto new_request = make<Infrastructure::Request>(*this);
+    swap(tmp_body, const_cast<BodyType&>(m_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<Body>())
+        new_request->set_body(TRY(body->clone()));
 
     // 3. Return newRequest.
     return new_request;

+ 1 - 1
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Requests.h

@@ -287,7 +287,7 @@ public:
     [[nodiscard]] String serialize_origin() const;
     [[nodiscard]] ErrorOr<ByteBuffer> byte_serialize_origin() const;
 
-    [[nodiscard]] NonnullOwnPtr<Request> clone() const;
+    [[nodiscard]] WebIDL::ExceptionOr<NonnullOwnPtr<Request>> clone() const;
 
     [[nodiscard]] ErrorOr<void> add_range_reader(u64 first, Optional<u64> const& last);
 

+ 9 - 7
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.cpp

@@ -78,19 +78,21 @@ ErrorOr<Optional<AK::URL>> Response::location_url(Optional<String> const& reques
 }
 
 // https://fetch.spec.whatwg.org/#concept-response-clone
-NonnullOwnPtr<Response> Response::clone() const
+WebIDL::ExceptionOr<NonnullOwnPtr<Response>> 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> body;
-    swap(body, const_cast<Optional<Body>&>(m_body));
-    auto new_response = adopt_own(*new Infrastructure::Response(*this));
-    swap(body, const_cast<Optional<Body>&>(m_body));
-
-    // FIXME: 3. If response’s body is non-null, then set newResponse’s body to the result of cloning response’s body.
+    Optional<Body> tmp_body;
+    swap(tmp_body, const_cast<Optional<Body>&>(m_body));
+    auto new_response = make<Infrastructure::Response>(*this);
+    swap(tmp_body, const_cast<Optional<Body>&>(m_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;

+ 1 - 1
Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Responses.h

@@ -100,7 +100,7 @@ public:
     [[nodiscard]] Optional<AK::URL const&> url() const;
     [[nodiscard]] ErrorOr<Optional<AK::URL>> location_url(Optional<String> const& request_fragment) const;
 
-    [[nodiscard]] NonnullOwnPtr<Response> clone() const;
+    [[nodiscard]] WebIDL::ExceptionOr<NonnullOwnPtr<Response>> clone() const;
 
 private:
     // https://fetch.spec.whatwg.org/#concept-response-type