forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LibWeb: Support unbuffered fetch requests
Supporting unbuffered fetches is actually part of the fetch spec in its HTTP-network-fetch algorithm. We had previously implemented this method in a very ad-hoc manner as a simple wrapper around ResourceLoader. This is still the case, but we now implement a good amount of these steps according to spec, using ResourceLoader's unbuffered API. The response data is forwarded through to the fetch response using streams. This will eventually let us remove the use of ResourceLoader's buffered API, as all responses should just be streamed this way. The streams spec then supplies ways to wait for completion, thus allowing fully buffered responses. However, we have more work to do to make the other parts of our fetch implementation (namely, Body::fully_read) use streams before we can do this.
- Loading branch information
1 parent
1e97ae6
commit 6056428
Showing
8 changed files
with
266 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Userland/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2024, Tim Flynn <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <LibJS/Heap/HeapFunction.h> | ||
#include <LibWeb/Bindings/ExceptionOrUtils.h> | ||
#include <LibWeb/Bindings/HostDefined.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 { | ||
|
||
JS_DEFINE_ALLOCATOR(FetchedDataReceiver); | ||
|
||
FetchedDataReceiver::FetchedDataReceiver(JS::NonnullGCPtr<Infrastructure::FetchParams const> fetch_params, JS::NonnullGCPtr<Streams::ReadableStream> stream) | ||
: 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); | ||
} | ||
|
||
void FetchedDataReceiver::set_pending_promise(JS::NonnullGCPtr<WebIDL::Promise> promise) | ||
{ | ||
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(), | ||
m_fetch_params->task_destination().get<JS::NonnullGCPtr<JS::Object>>(), | ||
JS::create_heap_function(heap(), [this, bytes = MUST(ByteBuffer::copy(bytes))]() mutable { | ||
HTML::TemporaryExecutionContext execution_context { Bindings::host_defined_environment_settings_object(m_stream->realm()), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes }; | ||
|
||
// 1. Pull from bytes buffer into stream. | ||
if (auto result = Streams::readable_stream_pull_from_bytes(m_stream, move(bytes)); result.is_error()) { | ||
auto throw_completion = Bindings::dom_exception_to_throw_completion(m_stream->vm(), result.release_error()); | ||
|
||
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()); | ||
})); | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
Userland/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) 2024, Tim Flynn <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AK/ByteBuffer.h> | ||
#include <LibJS/Heap/Cell.h> | ||
#include <LibJS/Heap/CellAllocator.h> | ||
#include <LibWeb/Forward.h> | ||
|
||
namespace Web::Fetch::Fetching { | ||
|
||
class FetchedDataReceiver final : public JS::Cell { | ||
JS_CELL(FetchedDataReceiver, JS::Cell); | ||
JS_DECLARE_ALLOCATOR(FetchedDataReceiver); | ||
|
||
public: | ||
virtual ~FetchedDataReceiver() override; | ||
|
||
void set_pending_promise(JS::NonnullGCPtr<WebIDL::Promise>); | ||
void on_data_received(ReadonlyBytes); | ||
|
||
private: | ||
FetchedDataReceiver(JS::NonnullGCPtr<Infrastructure::FetchParams const>, JS::NonnullGCPtr<Streams::ReadableStream>); | ||
|
||
virtual void visit_edges(Visitor& visitor) override; | ||
|
||
JS::NonnullGCPtr<Infrastructure::FetchParams const> m_fetch_params; | ||
JS::NonnullGCPtr<Streams::ReadableStream> m_stream; | ||
JS::GCPtr<WebIDL::Promise> m_pending_promise; | ||
ByteBuffer m_buffer; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters