2024-05-26 08:03:29 -04:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
#include <LibGC/Function.h>
|
2024-05-26 08:03:29 -04:00
|
|
|
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
|
|
|
|
#include <LibWeb/Fetch/Fetching/FetchedDataReceiver.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/FetchParams.h>
|
|
|
|
|
#include <LibWeb/Fetch/Infrastructure/Task.h>
|
|
|
|
|
#include <LibWeb/HTML/Scripting/ExceptionReporter.h>
|
|
|
|
|
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
|
|
|
|
|
#include <LibWeb/Streams/AbstractOperations.h>
|
|
|
|
|
#include <LibWeb/WebIDL/Promise.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::Fetch::Fetching {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(FetchedDataReceiver);
|
2024-05-26 08:03:29 -04:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
FetchedDataReceiver::FetchedDataReceiver(GC::Ref<Infrastructure::FetchParams const> fetch_params, GC::Ref<Streams::ReadableStream> stream)
|
2024-05-26 08:03:29 -04:00
|
|
|
|
: m_fetch_params(fetch_params)
|
|
|
|
|
, m_stream(stream)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FetchedDataReceiver::~FetchedDataReceiver() = default;
|
|
|
|
|
|
|
|
|
|
void FetchedDataReceiver::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_fetch_params);
|
|
|
|
|
visitor.visit(m_stream);
|
|
|
|
|
visitor.visit(m_pending_promise);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
void FetchedDataReceiver::set_pending_promise(GC::Ref<WebIDL::Promise> promise)
|
2024-05-26 08:03:29 -04:00
|
|
|
|
{
|
|
|
|
|
auto had_pending_promise = m_pending_promise != nullptr;
|
|
|
|
|
m_pending_promise = promise;
|
|
|
|
|
|
|
|
|
|
if (!had_pending_promise && !m_buffer.is_empty()) {
|
|
|
|
|
on_data_received(m_buffer);
|
|
|
|
|
m_buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This implements the parallel steps of the pullAlgorithm in HTTP-network-fetch.
|
|
|
|
|
// https://fetch.spec.whatwg.org/#ref-for-in-parallel④
|
|
|
|
|
void FetchedDataReceiver::on_data_received(ReadonlyBytes bytes)
|
|
|
|
|
{
|
|
|
|
|
// FIXME: 1. If the size of buffer is smaller than a lower limit chosen by the user agent and the ongoing fetch
|
|
|
|
|
// is suspended, resume the fetch.
|
|
|
|
|
// FIXME: 2. Wait until buffer is not empty.
|
|
|
|
|
|
|
|
|
|
// If the remote end sends data immediately after we receive headers, we will often get that data here before the
|
|
|
|
|
// stream tasks have all been queued internally. Just hold onto that data.
|
|
|
|
|
if (!m_pending_promise) {
|
|
|
|
|
m_buffer.append(bytes);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Queue a fetch task to run the following steps, with fetchParams’s task destination.
|
|
|
|
|
Infrastructure::queue_fetch_task(
|
|
|
|
|
m_fetch_params->controller(),
|
2024-11-15 04:01:23 +13:00
|
|
|
|
m_fetch_params->task_destination().get<GC::Ref<JS::Object>>(),
|
|
|
|
|
GC::create_function(heap(), [this, bytes = MUST(ByteBuffer::copy(bytes))]() mutable {
|
2024-10-24 20:39:18 +13:00
|
|
|
|
HTML::TemporaryExecutionContext execution_context { m_stream->realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
|
2024-05-26 08:03:29 -04:00
|
|
|
|
|
|
|
|
|
// 1. Pull from bytes buffer into stream.
|
2024-12-08 17:28:44 +13:00
|
|
|
|
if (auto result = m_stream->pull_from_bytes(move(bytes)); result.is_error()) {
|
2024-11-04 14:37:27 +01:00
|
|
|
|
auto throw_completion = Bindings::exception_to_throw_completion(m_stream->vm(), result.release_error());
|
2024-05-26 08:03:29 -04:00
|
|
|
|
|
|
|
|
|
dbgln("FetchedDataReceiver: Stream error pulling bytes");
|
|
|
|
|
HTML::report_exception(throw_completion, m_stream->realm());
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. If stream is errored, then terminate fetchParams’s controller.
|
|
|
|
|
if (m_stream->is_errored())
|
|
|
|
|
m_fetch_params->controller()->terminate();
|
|
|
|
|
|
|
|
|
|
// 3. Resolve promise with undefined.
|
|
|
|
|
WebIDL::resolve_promise(m_stream->realm(), *m_pending_promise, JS::js_undefined());
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|