mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-06 01:26:22 +00:00
LibWeb: Propagate OOM in Body::fully_read() through its error callback
Fetched bodies can be on the order of gigabytes, so rather than crashing when we hit OOM here, we can simply invoke the error callback with a DOM exception. We use "UnknownError" here as the spec directly supports this for OOM errors: UnknownError: The operation failed for an unknown transient reason (e.g. out of memory). This is still an ad-hoc implementation. We should be using streams, and we do have the AOs available to do so. But they need to be massaged to be compatible with callers of Body::fully_read. And once we do use streams, this function will become infallible - so making it infallible here is at least a step in the right direction.
This commit is contained in:
parent
ec5988d56b
commit
1ffda6a805
Notes:
sideshowbarker
2024-07-16 18:03:21 +09:00
Author: https://github.com/trflynn89
Commit: 1ffda6a805
Pull-request: https://github.com/SerenityOS/serenity/pull/24124
10 changed files with 39 additions and 42 deletions
|
@ -62,17 +62,15 @@ JS::NonnullGCPtr<Body> Body::clone(JS::Realm& realm)
|
|||
}
|
||||
|
||||
// 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
|
||||
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 = 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 = [&realm, process_body, task_destination_object = task_destination_object](ByteBuffer const& bytes) mutable -> ErrorOr<void> {
|
||||
auto success_steps = [&realm, process_body, task_destination_object = task_destination_object](ReadonlyBytes bytes) -> 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, JS::create_heap_function(realm.heap(), [process_body, bytes_copy = move(bytes_copy)]() mutable {
|
||||
|
@ -82,25 +80,27 @@ WebIDL::ExceptionOr<void> Body::fully_read(JS::Realm& realm, Web::Fetch::Infrast
|
|||
};
|
||||
|
||||
// 3. Let errorSteps optionally given an exception exception be to queue a fetch task to run processBodyError given exception, with taskDestination.
|
||||
auto error_steps = [&realm, process_body_error, task_destination_object](JS::GCPtr<WebIDL::DOMException> exception) mutable {
|
||||
queue_fetch_task(*task_destination_object, JS::create_heap_function(realm.heap(), [process_body_error = move(process_body_error), exception]() {
|
||||
auto error_steps = [&realm, process_body_error, task_destination_object](JS::GCPtr<WebIDL::DOMException> exception) {
|
||||
queue_fetch_task(*task_destination_object, JS::create_heap_function(realm.heap(), [process_body_error, exception]() {
|
||||
process_body_error->function()(*exception);
|
||||
}));
|
||||
};
|
||||
|
||||
// 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>()) {
|
||||
TRY_OR_THROW_OOM(vm, success_steps(*byte_buffer));
|
||||
} else if (auto const* blob_handle = m_source.get_pointer<JS::Handle<FileAPI::Blob>>()) {
|
||||
auto byte_buffer = TRY_OR_THROW_OOM(vm, ByteBuffer::copy((*blob_handle)->bytes()));
|
||||
TRY_OR_THROW_OOM(vm, success_steps(move(byte_buffer)));
|
||||
} else {
|
||||
// Empty, Blob, FormData
|
||||
error_steps(WebIDL::DOMException::create(realm, "DOMException"_fly_string, "Reading from Blob, FormData or null source is not yet implemented"_fly_string));
|
||||
}
|
||||
return {};
|
||||
// FIXME: Use streams for these steps.
|
||||
m_source.visit(
|
||||
[&](ByteBuffer const& byte_buffer) {
|
||||
if (auto result = success_steps(byte_buffer); result.is_error())
|
||||
error_steps(WebIDL::UnknownError::create(realm, "Out-of-memory"_fly_string));
|
||||
},
|
||||
[&](JS::Handle<FileAPI::Blob> const& blob) {
|
||||
if (auto result = success_steps(blob->bytes()); result.is_error())
|
||||
error_steps(WebIDL::UnknownError::create(realm, "Out-of-memory"_fly_string));
|
||||
},
|
||||
[&](Empty) {
|
||||
error_steps(WebIDL::DOMException::create(realm, "DOMException"_fly_string, "Reading from Blob, FormData or null source is not yet implemented"_fly_string));
|
||||
});
|
||||
}
|
||||
|
||||
// https://fetch.spec.whatwg.org/#byte-sequence-as-a-body
|
||||
|
|
|
@ -41,7 +41,7 @@ public:
|
|||
|
||||
[[nodiscard]] JS::NonnullGCPtr<Body> clone(JS::Realm&);
|
||||
|
||||
WebIDL::ExceptionOr<void> fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
|
||||
void fully_read(JS::Realm&, ProcessBodyCallback process_body, ProcessBodyErrorCallback process_body_error, TaskDestination task_destination) const;
|
||||
|
||||
virtual void visit_edges(JS::Cell::Visitor&) override;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue