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

This commit is contained in:
Linus Groh 2022-09-25 19:25:53 +01:00
parent ef5e2eb794
commit a7164f2674
Notes: sideshowbarker 2024-07-18 08:59:31 +09:00
6 changed files with 38 additions and 14 deletions

View file

@ -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 bodys stream.
// FIXME: 2. Set bodys 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 };
}
}

View file

@ -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).

View file

@ -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));
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));
// FIXME: 2. If requests body is non-null, set newRequests body to the result of cloning requests body.
// 2. If requests body is non-null, set newRequests body to the result of cloning requests body.
if (auto const* body = m_body.get_pointer<Body>())
new_request->set_body(TRY(body->clone()));
// 3. Return newRequest.
return new_request;

View file

@ -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);

View file

@ -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 responses 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));
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));
// FIXME: 3. If responses body is non-null, then set newResponses body to the result of cloning responses body.
// 3. If responses body is non-null, then set newResponses body to the result of cloning responses body.
if (m_body.has_value())
new_response->set_body(TRY(m_body->clone()));
// 4. Return newResponse.
return new_response;

View file

@ -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