mirror of
https://github.com/RGBCube/serenity
synced 2025-05-28 14:35:08 +00:00
LibWeb/Fetch: Implement Body's "fully read" function from the spec
Required by XHR's reliance on Fetch.
This commit is contained in:
parent
9aca54091a
commit
ccdb1bcc4e
9 changed files with 121 additions and 42 deletions
|
@ -9,6 +9,8 @@
|
|||
#include <LibJS/Runtime/Completion.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/PromiseCapability.h>
|
||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||
#include <LibWeb/Bindings/HostDefined.h>
|
||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
#include <LibWeb/Fetch/Body.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
|
@ -47,7 +49,7 @@ bool BodyMixin::body_used() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::array_buffer() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::array_buffer() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -57,7 +59,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::array_buffer() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-blob
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::blob() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::blob() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -67,7 +69,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::blob() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-formdata
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::form_data() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::form_data() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -77,7 +79,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::form_data() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-json
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::json() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::json() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -87,7 +89,7 @@ JS::NonnullGCPtr<JS::Promise> BodyMixin::json() const
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#dom-body-text
|
||||
JS::NonnullGCPtr<JS::Promise> BodyMixin::text() const
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> BodyMixin::text() const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
|
@ -142,33 +144,74 @@ WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm& realm, ByteBuffer bytes,
|
|||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#concept-body-consume-body
|
||||
JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> consume_body(JS::Realm& realm, BodyMixin const& object, PackageDataType type)
|
||||
{
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// 1. If object is unusable, then return a promise rejected with a TypeError.
|
||||
if (object.is_unusable()) {
|
||||
auto promise_capability = WebIDL::create_rejected_promise(realm, JS::TypeError::create(realm, "Body is unusable"sv).release_allocated_value_but_fixme_should_propagate_errors());
|
||||
return verify_cast<JS::Promise>(*promise_capability->promise().ptr());
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise_capability->promise().ptr()) };
|
||||
}
|
||||
|
||||
// 2. Let promise be a promise resolved with an empty byte sequence.
|
||||
auto promise = WebIDL::create_resolved_promise(realm, JS::PrimitiveString::create(vm, String {}));
|
||||
// 2. Let promise be a new promise.
|
||||
auto promise = WebIDL::create_promise(realm);
|
||||
|
||||
// 3. If object’s body is non-null, then set promise to the result of fully reading body as promise given object’s body.
|
||||
auto const& body = object.body_impl();
|
||||
if (body.has_value())
|
||||
promise = body->fully_read_as_promise().release_value_but_fixme_should_propagate_errors();
|
||||
// 3. Let errorSteps given error be to reject promise with error.
|
||||
// NOTE: `promise` and `realm` is protected by JS::SafeFunction.
|
||||
auto error_steps = [promise, &realm](JS::Object& error) {
|
||||
// NOTE: Not part of the spec, but we need to have an execution context on the stack to call native functions.
|
||||
// (In this case, Promise's reject function)
|
||||
auto& environment_settings_object = Bindings::host_defined_environment_settings_object(realm);
|
||||
environment_settings_object.prepare_to_run_script();
|
||||
|
||||
// 4. Let steps be to return the result of package data with the first argument given, type, and object’s MIME type.
|
||||
auto steps = [&vm, &realm, &object, type](JS::Value value) -> WebIDL::ExceptionOr<JS::Value> {
|
||||
VERIFY(value.is_string());
|
||||
auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(TRY(value.as_string().deprecated_string()).bytes()));
|
||||
return package_data(realm, move(bytes), type, object.mime_type_impl().release_value_but_fixme_should_propagate_errors());
|
||||
WebIDL::reject_promise(realm, promise, &error);
|
||||
|
||||
// See above NOTE.
|
||||
environment_settings_object.clean_up_after_running_script();
|
||||
};
|
||||
|
||||
// 5. Return the result of upon fulfillment of promise given steps.
|
||||
return WebIDL::upon_fulfillment(promise, move(steps));
|
||||
// 4. Let successSteps given a byte sequence data be to resolve promise with the result of running convertBytesToJSValue
|
||||
// with data. If that threw an exception, then run errorSteps with that exception.
|
||||
// NOTE: `promise`, `realm` and `object` is protected by JS::SafeFunction.
|
||||
// FIXME: Refactor this to the new version of the spec introduced with https://github.com/whatwg/fetch/commit/464326e8eb6a602122c030cd40042480a3c0e265
|
||||
auto success_steps = [promise, &realm, &object, type](ByteBuffer const& data) {
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// NOTE: Not part of the spec, but we need to have an execution context on the stack to call native functions.
|
||||
// (In this case, Promise's reject function and JSON.parse)
|
||||
auto& environment_settings_object = Bindings::host_defined_environment_settings_object(realm);
|
||||
environment_settings_object.prepare_to_run_script();
|
||||
|
||||
ScopeGuard guard = [&]() {
|
||||
// See above NOTE.
|
||||
environment_settings_object.clean_up_after_running_script();
|
||||
};
|
||||
|
||||
auto value_or_error = Bindings::throw_dom_exception_if_needed(vm, [&]() -> WebIDL::ExceptionOr<JS::Value> {
|
||||
return package_data(realm, data, type, TRY_OR_THROW_OOM(vm, object.mime_type_impl()));
|
||||
});
|
||||
|
||||
if (value_or_error.is_error()) {
|
||||
// We can't call error_steps here without moving it into success_steps, causing a double move when we pause error_steps
|
||||
// to fully_read, so just reject the promise like error_steps does.
|
||||
WebIDL::reject_promise(realm, promise, value_or_error.release_error().value().value());
|
||||
return;
|
||||
}
|
||||
|
||||
WebIDL::resolve_promise(realm, promise, value_or_error.release_value());
|
||||
};
|
||||
|
||||
// 5. If object’s body is null, then run successSteps with an empty byte sequence.
|
||||
auto const& body = object.body_impl();
|
||||
if (!body.has_value()) {
|
||||
success_steps(ByteBuffer {});
|
||||
}
|
||||
// 6. Otherwise, fully read object’s body given successSteps, errorSteps, and object’s relevant global object.
|
||||
else {
|
||||
TRY(body->fully_read(realm, move(success_steps), move(error_steps), JS::NonnullGCPtr { HTML::relevant_global_object(object.as_platform_object()) }));
|
||||
}
|
||||
|
||||
// 7. Return promise.
|
||||
return JS::NonnullGCPtr { verify_cast<JS::Promise>(*promise->promise().ptr()) };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,20 +29,22 @@ public:
|
|||
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const = 0;
|
||||
virtual Optional<Infrastructure::Body&> body_impl() = 0;
|
||||
virtual Optional<Infrastructure::Body const&> body_impl() const = 0;
|
||||
virtual Bindings::PlatformObject& as_platform_object() = 0;
|
||||
virtual Bindings::PlatformObject const& as_platform_object() const = 0;
|
||||
|
||||
[[nodiscard]] bool is_unusable() const;
|
||||
[[nodiscard]] JS::GCPtr<Streams::ReadableStream> body() const;
|
||||
[[nodiscard]] bool body_used() const;
|
||||
|
||||
// JS API functions
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> array_buffer() const;
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> blob() const;
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> form_data() const;
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> json() const;
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> text() const;
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> array_buffer() const;
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> blob() const;
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> form_data() const;
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> json() const;
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> text() const;
|
||||
};
|
||||
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::Value> package_data(JS::Realm&, ByteBuffer, PackageDataType, Optional<MimeSniff::MimeType> const&);
|
||||
[[nodiscard]] JS::NonnullGCPtr<JS::Promise> consume_body(JS::Realm&, BodyMixin const&, PackageDataType);
|
||||
[[nodiscard]] WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Promise>> consume_body(JS::Realm&, BodyMixin const&, PackageDataType);
|
||||
|
||||
}
|
||||
|
|
|
@ -637,7 +637,7 @@ WebIDL::ExceptionOr<void> fetch_response_handover(JS::Realm& realm, Infrastructu
|
|||
|
||||
// 2. Let processBodyError be this step: run fetchParams’s process response consume body given response and
|
||||
// failure.
|
||||
auto process_body_error = [&fetch_params, &response] {
|
||||
auto process_body_error = [&fetch_params, &response](auto&) {
|
||||
(*fetch_params.algorithms()->process_response_consume_body())(response, Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag {});
|
||||
};
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include <LibWeb/Fetch/Infrastructure/FetchController.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/Task.h>
|
||||
|
||||
namespace Web::Fetch::Infrastructure {
|
||||
|
||||
|
@ -22,9 +23,6 @@ class FetchParams : public JS::Cell {
|
|||
JS_CELL(FetchParams, JS::Cell);
|
||||
|
||||
public:
|
||||
// FIXME: 'or a parallel queue'
|
||||
using TaskDestination = Variant<Empty, JS::NonnullGCPtr<JS::Object>>;
|
||||
|
||||
struct PreloadedResponseCandidatePendingTag { };
|
||||
using PreloadedResponseCandidate = Variant<Empty, PreloadedResponseCandidatePendingTag, JS::NonnullGCPtr<Response>>;
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||
#include <LibWeb/Fetch/BodyInit.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/Task.h>
|
||||
#include <LibWeb/WebIDL/Promise.h>
|
||||
|
||||
namespace Web::Fetch::Infrastructure {
|
||||
|
@ -36,20 +37,43 @@ WebIDL::ExceptionOr<Body> Body::clone(JS::Realm& realm) const
|
|||
return Body { JS::make_handle(out2), m_source, m_length };
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#fully-reading-body-as-promise
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> Body::fully_read_as_promise() const
|
||||
// https://fetch.spec.whatwg.org/#body-fully-read
|
||||
WebIDL::ExceptionOr<void> Body::fully_read(JS::Realm& realm, Web::Fetch::Infrastructure::Body::ProcessBodyCallback process_body, Web::Fetch::Infrastructure::Body::ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const
|
||||
{
|
||||
auto& vm = Bindings::main_thread_vm();
|
||||
auto& realm = *vm.current_realm();
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// FIXME: 1. If taskDestination is null, then set taskDestination to the result of starting a new parallel queue.
|
||||
// FIXME: Handle 'parallel queue' task destination
|
||||
VERIFY(!task_destination.has<Empty>());
|
||||
auto task_destination_object = task_destination.get<JS::NonnullGCPtr<JS::Object>>();
|
||||
|
||||
// 2. Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination.
|
||||
auto success_steps = [process_body = move(process_body), task_destination_object = JS::make_handle(task_destination_object)](ByteBuffer const& bytes) mutable -> ErrorOr<void> {
|
||||
// Make a copy of the bytes, as the source of the bytes may disappear between the time the task is queued and executed.
|
||||
auto bytes_copy = TRY(ByteBuffer::copy(bytes));
|
||||
queue_fetch_task(*task_destination_object, [process_body = move(process_body), bytes_copy = move(bytes_copy)]() {
|
||||
process_body(move(bytes_copy));
|
||||
});
|
||||
return {};
|
||||
};
|
||||
|
||||
// 3. Let errorSteps be to queue a fetch task to run processBodyError, with taskDestination.
|
||||
auto error_steps = [process_body_error = move(process_body_error), task_destination_object = JS::make_handle(task_destination_object)](JS::Error& error) mutable {
|
||||
queue_fetch_task(*task_destination_object, [process_body_error = move(process_body_error), error = JS::make_handle(error)]() {
|
||||
process_body_error(*error);
|
||||
});
|
||||
};
|
||||
|
||||
// 4. Let reader be the result of getting a reader for body’s stream. If that threw an exception, then run errorSteps with that exception and return.
|
||||
// 5. Read all bytes from reader, given successSteps and errorSteps.
|
||||
// FIXME: Implement the streams spec - this is completely made up for now :^)
|
||||
if (auto const* byte_buffer = m_source.get_pointer<ByteBuffer>()) {
|
||||
// FIXME: The buffer may or may not be valid UTF-8.
|
||||
auto result = TRY_OR_THROW_OOM(vm, String::from_utf8(*byte_buffer));
|
||||
return WebIDL::create_resolved_promise(realm, JS::PrimitiveString::create(vm, move(result)));
|
||||
}
|
||||
TRY_OR_THROW_OOM(vm, success_steps(*byte_buffer));
|
||||
} else {
|
||||
// Empty, Blob, FormData
|
||||
return WebIDL::create_rejected_promise(realm, JS::InternalError::create(realm, "Reading body isn't fully implemented"sv).release_allocated_value_but_fixme_should_propagate_errors());
|
||||
error_steps(TRY(JS::InternalError::create(realm, "Reading from Blob, FormData or null source is not yet implemented"sv)));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#byte-sequence-as-a-body
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <AK/Variant.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/Heap/Handle.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/Task.h>
|
||||
#include <LibWeb/FileAPI/Blob.h>
|
||||
#include <LibWeb/Streams/ReadableStream.h>
|
||||
#include <LibWeb/WebIDL/Promise.h>
|
||||
|
@ -23,6 +24,8 @@ namespace Web::Fetch::Infrastructure {
|
|||
class Body final {
|
||||
public:
|
||||
using SourceType = Variant<Empty, ByteBuffer, JS::Handle<FileAPI::Blob>>;
|
||||
using ProcessBodyCallback = JS::SafeFunction<void(ByteBuffer)>;
|
||||
using ProcessBodyErrorCallback = JS::SafeFunction<void(JS::Object&)>;
|
||||
|
||||
explicit Body(JS::Handle<Streams::ReadableStream>);
|
||||
Body(JS::Handle<Streams::ReadableStream>, SourceType, Optional<u64>);
|
||||
|
@ -33,7 +36,7 @@ public:
|
|||
|
||||
WebIDL::ExceptionOr<Body> clone(JS::Realm&) const;
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> fully_read_as_promise() const;
|
||||
WebIDL::ExceptionOr<void> fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
|
||||
|
||||
private:
|
||||
// https://fetch.spec.whatwg.org/#concept-body-stream
|
||||
|
|
|
@ -6,11 +6,16 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Variant.h>
|
||||
#include <LibJS/Forward.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibJS/SafeFunction.h>
|
||||
|
||||
namespace Web::Fetch::Infrastructure {
|
||||
|
||||
// FIXME: 'or a parallel queue'
|
||||
using TaskDestination = Variant<Empty, JS::NonnullGCPtr<JS::Object>>;
|
||||
|
||||
void queue_fetch_task(JS::Object&, JS::SafeFunction<void()>);
|
||||
|
||||
}
|
||||
|
|
|
@ -74,6 +74,8 @@ public:
|
|||
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
|
||||
virtual Optional<Infrastructure::Body&> body_impl() override;
|
||||
virtual Optional<Infrastructure::Body const&> body_impl() const override;
|
||||
virtual Bindings::PlatformObject& as_platform_object() override { return *this; }
|
||||
virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; }
|
||||
|
||||
[[nodiscard]] JS::NonnullGCPtr<Infrastructure::Request> request() const { return m_request; }
|
||||
|
||||
|
|
|
@ -41,6 +41,8 @@ public:
|
|||
virtual ErrorOr<Optional<MimeSniff::MimeType>> mime_type_impl() const override;
|
||||
virtual Optional<Infrastructure::Body&> body_impl() override;
|
||||
virtual Optional<Infrastructure::Body const&> body_impl() const override;
|
||||
virtual Bindings::PlatformObject& as_platform_object() override { return *this; }
|
||||
virtual Bindings::PlatformObject const& as_platform_object() const override { return *this; }
|
||||
|
||||
[[nodiscard]] JS::NonnullGCPtr<Infrastructure::Response> response() const { return m_response; }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue