Skip to content

Truly independent web browser

LibWeb: Add FetchByteChannel

Luke Wildef143997b3557
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index b3d73cfcee4..baf05c18fd0 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -365,6 +365,7 @@ set(SOURCES
EventTiming/PerformanceEventTiming.cpp
Fetch/Body.cpp
Fetch/BodyInit.cpp
+ Fetch/Engine/FetchByteChannel.cpp
Fetch/Engine/FetchExecutor.cpp
Fetch/Engine/InlineFetchExecutor.cpp
Fetch/Fetching/Checks.cpp
diff --git a/Libraries/LibWeb/Fetch/Engine/BodyPresentation.h b/Libraries/LibWeb/Fetch/Engine/BodyPresentation.h
index 04673ac7f16..ad29736ca2c 100644
--- a/Libraries/LibWeb/Fetch/Engine/BodyPresentation.h
+++ b/Libraries/LibWeb/Fetch/Engine/BodyPresentation.h
@@ -6,8 +6,10 @@
#pragma once
+#include <AK/NonnullRefPtr.h>
#include <AK/Variant.h>
#include <LibCore/ImmutableBytes.h>
+#include <LibWeb/Fetch/Engine/FetchByteChannel.h>
namespace Web::Fetch::Engine {
@@ -15,6 +17,23 @@ struct NullBody {
bool operator==(NullBody const&) const = default;
};
+inline bool is_present_body_handle(Core::ImmutableBytes const& bytes)
+{
+ return bytes.is_valid();
+}
+
+template<typename T>
+bool is_present_body_handle(NonnullRefPtr<T> const&)
+{
+ return true;
+}
+
+template<typename... Ts>
+bool is_present_body_handle(Variant<Ts...> const& handle)
+{
+ return handle.visit([](auto const& arm) { return is_present_body_handle(arm); });
+}
+
// A delivered body is never null.
template<typename Handle>
class Delivered {
@@ -22,7 +41,7 @@ public:
explicit Delivered(Handle handle)
: m_handle(move(handle))
{
- VERIFY(m_handle.is_valid());
+ VERIFY(is_present_body_handle(m_handle));
}
Handle const& handle() const { return m_handle; }
@@ -31,7 +50,7 @@ private:
Handle m_handle;
};
-using NonNullLocalBodyHandle = Core::ImmutableBytes;
+using NonNullLocalBodyHandle = Variant<Core::ImmutableBytes, NonnullRefPtr<FetchByteChannel>>;
template<typename Handle>
using BodyPresentation = Variant<NullBody, Delivered<Handle>>;
diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp
new file mode 100644
index 00000000000..1fbd095f4d2
--- /dev/null
+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp
@@ -0,0 +1,258 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/Fetch/Engine/FetchByteChannel.h>
+
+namespace Web::Fetch::Engine {
+
+NonnullRefPtr<FetchByteChannel> FetchByteChannel::create(Watermarks watermarks, NonnullRefPtr<FetchByteChannelConsumerSink> consumer_sink, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink)
+{
+ return adopt_ref(*new FetchByteChannel(watermarks, move(consumer_sink), move(producer_sink)));
+}
+
+FetchByteChannel::FetchByteChannel(Watermarks watermarks, NonnullRefPtr<FetchByteChannelConsumerSink> consumer_sink, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink)
+ : m_watermarks(watermarks)
+ , m_consumer_sink(move(consumer_sink))
+ , m_producer_sink(move(producer_sink))
+ , m_outstanding_transport_credit(watermarks.high)
+{
+ VERIFY(m_watermarks.low > 0);
+ VERIFY(m_watermarks.low <= m_watermarks.high);
+}
+
+// A sink may reenter the channel.
+void FetchByteChannel::dispatch(PendingNotifications const& notifications)
+{
+ for (auto const& notification : notifications) {
+ notification.visit(
+ [&](Wake const& wake) { m_consumer_sink->wake(wake.ticket); },
+ [&](GrantCredit const& grant) { m_producer_sink->grant_credit(grant.byte_count); },
+ [&](ConsumerCancelledNotice) { m_producer_sink->consumer_cancelled(); },
+ [&](Misbehaved const& misbehaved) { m_producer_sink->misbehaved(misbehaved.reason); });
+ }
+}
+
+void FetchByteChannelProducerSink::misbehaved(StringView reason)
+{
+ dbgln("FetchByteChannel: The transport misbehaved: {}", reason);
+ VERIFY_NOT_REACHED();
+}
+
+void FetchByteChannel::wake_armed_waiter_locked(PendingNotifications& notifications)
+{
+ if (!m_waiter_armed)
+ return;
+ m_waiter_armed = false;
+ notifications.append(Wake { m_armed_ticket });
+}
+
+FetchByteChannel::WriteResult FetchByteChannel::write(Core::ImmutableBytes bytes)
+{
+ return write_bytes(move(bytes));
+}
+
+FetchByteChannel::WriteResult FetchByteChannel::write_copy(ReadonlyBytes bytes)
+{
+ return write_bytes(bytes);
+}
+
+// This implements the buffer-filling steps of HTTP-network fetch's transmission loop.
+// https://fetch.spec.whatwg.org/#concept-http-network-fetch
+FetchByteChannel::WriteResult FetchByteChannel::write_bytes(Variant<Core::ImmutableBytes, ReadonlyBytes> bytes)
+{
+ auto size = bytes.visit([](auto const& written) -> u64 { return written.size(); });
+
+ PendingNotifications notifications;
+ auto result = WriteResult::Rejected;
+ {
+ MutexLocker locker { m_mutex };
+ if (m_transport_ended) {
+ notifications.append(Misbehaved { "The transport delivered after ending the body"sv });
+ } else if (!m_state.has<Open>()) {
+ // The consumer cancelled and the transport has not heard yet.
+ } else if (size > m_outstanding_transport_credit) {
+ notifications.append(Misbehaved { "The transport delivered more than it was granted"sv });
+ } else {
+ result = WriteResult::Accepted;
+ if (size > 0) {
+ // 12. Append bytes to buffer.
+ m_buffered_byte_count += size;
+ bytes.visit(
+ [&](Core::ImmutableBytes& chunk) {
+ seal_copied_tail_locked();
+ m_chunks.append(move(chunk));
+ },
+ [&](ReadonlyBytes copied) { m_copied_tail.append(copied); });
+
+ // 13. If the size of buffer is larger than an upper limit chosen by the user agent, ask the user agent
+ // to suspend the ongoing fetch.
+ // Spent credit is the suspension: the transport delivers only what it was granted.
+ m_outstanding_transport_credit -= size;
+ wake_armed_waiter_locked(notifications);
+ }
+ }
+ }
+ dispatch(notifications);
+ return result;
+}
+
+void FetchByteChannel::seal_copied_tail_locked()
+{
+ if (m_copied_tail.is_empty())
+ return;
+ m_chunks.append(Core::ImmutableBytes::adopt(move(m_copied_tail)));
+ m_copied_tail = {};
+}
+
+FetchByteChannel::TerminateResult FetchByteChannel::terminate_locked(State terminal_state, PendingNotifications& notifications)
+{
+ VERIFY(!terminal_state.has<Open>());
+ if (!m_state.has<Open>())
+ return TerminateResult::AlreadyTerminated;
+
+ if (terminal_state.has<Errored>()) {
+ m_chunks.clear();
+ m_copied_tail.clear();
+ m_buffered_byte_count = 0;
+ }
+ m_state = move(terminal_state);
+ wake_armed_waiter_locked(notifications);
+ return TerminateResult::Terminated;
+}
+
+FetchByteChannel::TerminateResult FetchByteChannel::close()
+{
+ PendingNotifications notifications;
+ auto result = TerminateResult::AlreadyTerminated;
+ {
+ MutexLocker locker { m_mutex };
+ if (m_transport_ended) {
+ notifications.append(Misbehaved { "The transport ended the body twice"sv });
+ } else {
+ m_transport_ended = true;
+ result = terminate_locked(Closed {}, notifications);
+ }
+ }
+ dispatch(notifications);
+ return result;
+}
+
+FetchByteChannel::TerminateResult FetchByteChannel::error(Errored errored)
+{
+ PendingNotifications notifications;
+ auto result = TerminateResult::AlreadyTerminated;
+ {
+ MutexLocker locker { m_mutex };
+ if (m_transport_ended) {
+ notifications.append(Misbehaved { "The transport ended the body twice"sv });
+ } else {
+ m_transport_ended = true;
+ result = terminate_locked(move(errored), notifications);
+ }
+ }
+ dispatch(notifications);
+ return result;
+}
+
+FetchByteChannel::TerminateResult FetchByteChannel::cancel()
+{
+ PendingNotifications notifications;
+ auto result = TerminateResult::AlreadyTerminated;
+ {
+ MutexLocker locker { m_mutex };
+ // A Closed channel keeps its undrained buffer, which a Streams cancellation may still discard.
+ if (m_state.has<Open>() || (m_state.has<Closed>() && m_buffered_byte_count > 0)) {
+ m_state = ConsumerCancelled {};
+ m_chunks.clear();
+ m_copied_tail.clear();
+ m_buffered_byte_count = 0;
+ wake_armed_waiter_locked(notifications);
+ if (!m_transport_ended)
+ notifications.append(ConsumerCancelledNotice {});
+ result = TerminateResult::Terminated;
+ }
+ }
+ dispatch(notifications);
+ return result;
+}
+
+// This implements the buffer-draining half of the pullAlgorithm in HTTP-network fetch.
+// https://fetch.spec.whatwg.org/#concept-http-network-fetch
+FetchByteChannel::TakeResult FetchByteChannel::take_locked(u64 limit, PendingNotifications& notifications)
+{
+ seal_copied_tail_locked();
+
+ TakeResult result;
+
+ u64 remaining = limit;
+ while (remaining > 0 && !m_chunks.is_empty()) {
+ auto& front = m_chunks.first();
+ auto available = front.size();
+ auto length = static_cast<size_t>(min<u64>(available, remaining));
+
+ result.taken.append(front.slice(0, length));
+ remaining -= length;
+ m_buffered_byte_count -= length;
+
+ if (length == available)
+ m_chunks.take_first();
+ else
+ front = front.slice(length, available - length);
+ }
+
+ result.state = m_state;
+ result.buffered_byte_count = m_buffered_byte_count;
+
+ // 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.
+ if (m_state.has<Open>() && m_buffered_byte_count < m_watermarks.low) {
+ auto target = m_watermarks.high - m_buffered_byte_count;
+ if (target > m_outstanding_transport_credit) {
+ notifications.append(GrantCredit { target - m_outstanding_transport_credit });
+ m_outstanding_transport_credit = target;
+ }
+ }
+
+ return result;
+}
+
+FetchByteChannel::TakeResult FetchByteChannel::take_up_to(u64 limit)
+{
+ PendingNotifications notifications;
+ TakeResult result;
+ {
+ MutexLocker locker { m_mutex };
+ result = take_locked(limit, notifications);
+ }
+ dispatch(notifications);
+ return result;
+}
+
+FetchByteChannel::TakeResult FetchByteChannel::take_all()
+{
+ return take_up_to(NumericLimits<u64>::max());
+}
+
+WakeTicket FetchByteChannel::arm_waiter()
+{
+ PendingNotifications notifications;
+ WakeTicket ticket { 0 };
+ {
+ MutexLocker locker { m_mutex };
+ VERIFY(!m_waiter_armed);
+ ticket = WakeTicket { m_next_ticket++ };
+ if (!m_state.has<Open>() || m_buffered_byte_count > 0) {
+ notifications.append(Wake { ticket });
+ } else {
+ m_waiter_armed = true;
+ m_armed_ticket = ticket;
+ }
+ }
+ dispatch(notifications);
+ return ticket;
+}
+
+}
diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h
new file mode 100644
index 00000000000..3a157385393
--- /dev/null
+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/AtomicRefCounted.h>
+#include <AK/ByteBuffer.h>
+#include <AK/Mutex.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/StringView.h>
+#include <AK/Variant.h>
+#include <AK/Vector.h>
+#include <LibCore/ImmutableBytes.h>
+#include <LibWeb/Export.h>
+#include <LibWeb/Fetch/Engine/Ids.h>
+
+namespace Web::Fetch::Engine {
+
+// Invoked outside the channel lock, possibly from the peer's thread; holds no GC state.
+class WEB_API FetchByteChannelConsumerSink : public AtomicRefCounted<FetchByteChannelConsumerSink> {
+public:
+ virtual ~FetchByteChannelConsumerSink() = default;
+
+ // The ticket names the arm_waiter() call this wake answers; a stale wake is rejected.
+ virtual void wake(WakeTicket) = 0;
+};
+
+class WEB_API FetchByteChannelProducerSink : public AtomicRefCounted<FetchByteChannelProducerSink> {
+public:
+ virtual ~FetchByteChannelProducerSink() = default;
+
+ // Grants the transport permission to deliver this many further bytes.
+ virtual void grant_credit(u64 byte_count) = 0;
+
+ virtual void consumer_cancelled() = 0;
+
+ // Called for a delivery no honest transport could have made.
+ virtual void misbehaved(StringView reason);
+};
+
+// The buffer HTTP-network fetch keeps between the network layer and a response body's stream; credit grants are
+// its suspend and resume.
+// https://fetch.spec.whatwg.org/#concept-http-network-fetch
+class WEB_API FetchByteChannel final : public AtomicRefCounted<FetchByteChannel> {
+public:
+ struct Watermarks {
+ u64 low { 0 };
+ u64 high { 0 };
+ };
+
+ struct Open { };
+ struct Closed { };
+ struct Errored {
+ // The error a consumer turns into an exception in its own realm.
+ ByteBuffer message;
+ };
+ struct ConsumerCancelled { };
+ using State = Variant<Open, Closed, Errored, ConsumerCancelled>;
+
+ // Snapshotted atomically with the removal of the bytes.
+ struct TakeResult {
+ Vector<Core::ImmutableBytes> taken;
+ State state { Open {} };
+ u64 buffered_byte_count { 0 };
+ };
+
+ [[nodiscard]] static NonnullRefPtr<FetchByteChannel> create(Watermarks, NonnullRefPtr<FetchByteChannelConsumerSink>, NonnullRefPtr<FetchByteChannelProducerSink>);
+
+ // The credit the producer should hand the transport before the first write.
+ [[nodiscard]] u64 initial_credit() const { return m_watermarks.high; }
+
+ enum class WriteResult : u8 {
+ Accepted,
+ Rejected,
+ };
+
+ // A close or error is the transport's one ending; cancel() also overrides a Closed channel with undrained bytes.
+ enum class TerminateResult : u8 {
+ Terminated,
+ AlreadyTerminated,
+ };
+
+ // Producer side.
+ WriteResult write(Core::ImmutableBytes);
+ // Copies the bytes; copies written one after another share a buffer until they are taken.
+ WriteResult write_copy(ReadonlyBytes);
+ TerminateResult close();
+ TerminateResult error(Errored);
+
+ // Consumer side.
+ [[nodiscard]] TakeResult take_up_to(u64 limit);
+ [[nodiscard]] TakeResult take_all();
+
+ // Either side may cancel; the producer hears of it unless it has already ended the body.
+ TerminateResult cancel();
+
+ // Returns the ticket the wake will carry; a wake already due fires before this returns, outside the lock.
+ WakeTicket arm_waiter();
+
+private:
+ FetchByteChannel(Watermarks, NonnullRefPtr<FetchByteChannelConsumerSink>, NonnullRefPtr<FetchByteChannelProducerSink>);
+
+ // Sink calls decided under the lock and made after it is released, in order.
+ struct Wake {
+ WakeTicket ticket { 0 };
+ };
+ struct GrantCredit {
+ u64 byte_count { 0 };
+ };
+ struct ConsumerCancelledNotice { };
+ struct Misbehaved {
+ StringView reason;
+ };
+ using Notification = Variant<Wake, GrantCredit, ConsumerCancelledNotice, Misbehaved>;
+ using PendingNotifications = Vector<Notification, 2>;
+
+ [[nodiscard]] WriteResult write_bytes(Variant<Core::ImmutableBytes, ReadonlyBytes>);
+ void seal_copied_tail_locked();
+ [[nodiscard]] TakeResult take_locked(u64 limit, PendingNotifications&);
+ [[nodiscard]] TerminateResult terminate_locked(State, PendingNotifications&);
+ void wake_armed_waiter_locked(PendingNotifications&);
+ void dispatch(PendingNotifications const&);
+
+ Watermarks const m_watermarks;
+ NonnullRefPtr<FetchByteChannelConsumerSink> const m_consumer_sink;
+ NonnullRefPtr<FetchByteChannelProducerSink> const m_producer_sink;
+
+ mutable Mutex m_mutex;
+ Vector<Core::ImmutableBytes> m_chunks;
+ // The write_copy() bytes that follow the last chunk.
+ ByteBuffer m_copied_tail;
+ u64 m_buffered_byte_count { 0 };
+ u64 m_outstanding_transport_credit { 0 };
+ State m_state { Open {} };
+ bool m_transport_ended { false };
+ bool m_waiter_armed { false };
+ WakeTicket m_armed_ticket { 0 };
+ u64 m_next_ticket { 1 };
+};
+
+}
diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt
index 7e87ad400dd..7484471e168 100644
--- a/Tests/LibWeb/CMakeLists.txt
+++ b/Tests/LibWeb/CMakeLists.txt
@@ -11,6 +11,7 @@ set(TEST_SOURCES
TestDump.cpp
TestEventLoop.cpp
TestFaviconDecoding.cpp
+ TestFetchByteChannel.cpp
TestFetchEngineMessages.cpp
TestFetchExecutor.cpp
TestFetchResponse.cpp
@@ -72,6 +73,7 @@ if (NOT WIN32)
endif()
target_link_libraries(TestDownloadReader PRIVATE LibGC LibJS LibURL)
target_link_libraries(TestFaviconDecoding PRIVATE LibCore LibGC LibGfx LibJS)
+target_link_libraries(TestFetchByteChannel PRIVATE LibThreading)
target_link_libraries(TestFetchResponse PRIVATE LibGC LibHTTP LibJS LibRequests LibURL)
target_link_libraries(TestFetchURL PRIVATE LibURL)
target_link_libraries(TestImageData PRIVATE LibGC LibJS)
diff --git a/Tests/LibWeb/TestFetchByteChannel.cpp b/Tests/LibWeb/TestFetchByteChannel.cpp
new file mode 100644
index 00000000000..e8ac011ac48
--- /dev/null
+++ b/Tests/LibWeb/TestFetchByteChannel.cpp
@@ -0,0 +1,608 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Atomic.h>
+#include <AK/Time.h>
+#include <AK/kmalloc.h>
+#include <LibTest/TestCase.h>
+#include <LibThreading/Thread.h>
+#include <LibWeb/Fetch/Engine/FetchByteChannel.h>
+
+namespace {
+
+using Web::Fetch::Engine::FetchByteChannel;
+using Web::Fetch::Engine::FetchByteChannelConsumerSink;
+using Web::Fetch::Engine::FetchByteChannelProducerSink;
+using Web::Fetch::Engine::WakeTicket;
+using TerminateResult = FetchByteChannel::TerminateResult;
+using WriteResult = FetchByteChannel::WriteResult;
+using Closed = FetchByteChannel::Closed;
+using ConsumerCancelled = FetchByteChannel::ConsumerCancelled;
+using Errored = FetchByteChannel::Errored;
+using Open = FetchByteChannel::Open;
+
+struct SinkEvent {
+ enum class Kind : u8 {
+ Wake,
+ CreditGrant,
+ ConsumerCancelled,
+ };
+ Kind kind;
+ u64 value { 0 };
+};
+
+class RecordingConsumerSink final : public FetchByteChannelConsumerSink {
+public:
+ explicit RecordingConsumerSink(Vector<SinkEvent>& events)
+ : m_events(events)
+ {
+ }
+
+ virtual void wake(WakeTicket ticket) override
+ {
+ m_events.append({ SinkEvent::Kind::Wake, ticket.value() });
+ if (on_wake)
+ on_wake(ticket);
+ }
+
+ Function<void(WakeTicket)> on_wake;
+
+private:
+ Vector<SinkEvent>& m_events;
+};
+
+class RecordingProducerSink final : public FetchByteChannelProducerSink {
+public:
+ explicit RecordingProducerSink(Vector<SinkEvent>& events)
+ : m_events(events)
+ {
+ }
+
+ virtual void grant_credit(u64 byte_count) override
+ {
+ m_events.append({ SinkEvent::Kind::CreditGrant, byte_count });
+ if (on_grant_credit)
+ on_grant_credit(byte_count);
+ }
+
+ virtual void consumer_cancelled() override
+ {
+ m_events.append({ SinkEvent::Kind::ConsumerCancelled });
+ }
+
+ Function<void(u64)> on_grant_credit;
+
+private:
+ Vector<SinkEvent>& m_events;
+};
+
+struct ChannelHarness {
+ AK_ALLOC_WITH_KMALLOC;
+
+ explicit ChannelHarness(FetchByteChannel::Watermarks watermarks = { .low = 16, .high = 64 })
+ : consumer_sink(adopt_ref(*new RecordingConsumerSink(events)))
+ , producer_sink(adopt_ref(*new RecordingProducerSink(events)))
+ , channel(FetchByteChannel::create(watermarks, *consumer_sink, *producer_sink))
+ {
+ }
+
+ Vector<SinkEvent> take_events() { return move(events); }
+
+ Vector<SinkEvent> events;
+ NonnullRefPtr<RecordingConsumerSink> consumer_sink;
+ NonnullRefPtr<RecordingProducerSink> producer_sink;
+ NonnullRefPtr<FetchByteChannel> channel;
+};
+
+struct LatestTicketSink final : FetchByteChannelConsumerSink {
+ virtual void wake(WakeTicket ticket) override { latest.store(ticket.value(), AK::MemoryOrder::memory_order_release); }
+ Atomic<u64> latest { 0 };
+};
+
+struct CountingProducerSink final : FetchByteChannelProducerSink {
+ virtual void grant_credit(u64 byte_count) override { credit.fetch_add(byte_count, AK::MemoryOrder::memory_order_acq_rel); }
+ virtual void consumer_cancelled() override { cancellations.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel); }
+ Atomic<u64> credit { 0 };
+ Atomic<u64> cancellations { 0 };
+};
+
+Core::ImmutableBytes bytes_of(StringView text)
+{
+ return MUST(Core::ImmutableBytes::copy(text.bytes()));
+}
+
+// Spins until the condition holds, or returns false once a minute has passed.
+template<typename Condition>
+bool spin_until_or_timeout(Condition condition)
+{
+ auto deadline = MonotonicTime::now() + AK::Duration::from_seconds(60);
+ for (u64 spins = 0; !condition(); ++spins) {
+ AK::atomic_pause();
+ if ((spins % 4096) == 0 && MonotonicTime::now() > deadline)
+ return false;
+ }
+ return true;
+}
+
+ByteString flatten(FetchByteChannel::TakeResult const& result)
+{
+ ByteBuffer flat;
+ for (auto const& taken : result.taken)
+ flat.append(taken.bytes());
+ return ByteString { flat.bytes() };
+}
+
+}
+
+TEST_CASE(write_take_ordering_and_chunk_integrity)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("hello "sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("byte "sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("channel"sv)), WriteResult::Accepted);
+
+ auto result = harness.channel->take_all();
+ EXPECT_EQ(flatten(result), "hello byte channel"sv);
+ EXPECT(result.state.has<Open>());
+ EXPECT_EQ(result.buffered_byte_count, 0u);
+}
+
+TEST_CASE(copied_writes_share_one_chunk)
+{
+ ChannelHarness harness;
+ for (auto byte : "copied"sv)
+ EXPECT_EQ(harness.channel->write_copy({ &byte, 1 }), WriteResult::Accepted);
+
+ auto result = harness.channel->take_all();
+ EXPECT_EQ(result.taken.size(), 1u);
+ EXPECT_EQ(flatten(result), "copied"sv);
+}
+
+TEST_CASE(copied_and_adopted_writes_keep_their_order)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write_copy("one "sv.bytes()), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("two "sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write_copy("three"sv.bytes()), WriteResult::Accepted);
+
+ auto first = harness.channel->take_up_to(6);
+ EXPECT_EQ(flatten(first), "one tw"sv);
+ EXPECT_EQ(first.buffered_byte_count, 7u);
+
+ // Copies written after a take start a new shared chunk.
+ EXPECT_EQ(harness.channel->write_copy(" four"sv.bytes()), WriteResult::Accepted);
+ auto rest = harness.channel->take_all();
+ EXPECT_EQ(flatten(rest), "o three four"sv);
+}
+
+TEST_CASE(copied_writes_spend_credit_like_adopted_ones)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+ auto credit_worth = MUST(String::repeated('x', 64));
+ EXPECT_EQ(harness.channel->write_copy(credit_worth.bytes()), WriteResult::Accepted);
+ EXPECT_DEATH("the transport delivers only what it was granted", (void)harness.channel->write_copy("x"sv.bytes()));
+}
+
+TEST_CASE(a_channel_needs_a_positive_low_watermark)
+{
+ Vector<SinkEvent> events;
+ auto consumer_sink = adopt_ref(*new RecordingConsumerSink(events));
+ auto producer_sink = adopt_ref(*new RecordingProducerSink(events));
+ EXPECT_DEATH("a channel without a low watermark never refills its credit", (void)FetchByteChannel::create({ .low = 0, .high = 64 }, *consumer_sink, *producer_sink));
+}
+
+TEST_CASE(take_up_to_leaves_correctly_offset_remainder)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("0123456789"sv)), WriteResult::Accepted);
+
+ auto first = harness.channel->take_up_to(4);
+ EXPECT_EQ(flatten(first), "0123"sv);
+ EXPECT_EQ(first.buffered_byte_count, 6u);
+
+ auto second = harness.channel->take_up_to(3);
+ EXPECT_EQ(flatten(second), "456"sv);
+ EXPECT_EQ(second.buffered_byte_count, 3u);
+
+ auto rest = harness.channel->take_all();
+ EXPECT_EQ(flatten(rest), "789"sv);
+ EXPECT_EQ(rest.buffered_byte_count, 0u);
+}
+
+TEST_CASE(take_up_to_spanning_multiple_chunks)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("abc"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("def"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("ghi"sv)), WriteResult::Accepted);
+
+ auto result = harness.channel->take_up_to(5);
+ EXPECT_EQ(flatten(result), "abcde"sv);
+ EXPECT_EQ(result.buffered_byte_count, 4u);
+}
+
+TEST_CASE(credit_accounting_with_batched_replenishment)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+ EXPECT_EQ(harness.channel->initial_credit(), 64u);
+
+ // Writes consume transport credit; nothing is replenished while the buffer sits at or
+ // above the low watermark.
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('x', 40)))), WriteResult::Accepted);
+ auto result = harness.channel->take_up_to(20);
+ EXPECT_EQ(result.buffered_byte_count, 20u);
+ EXPECT(harness.take_events().is_empty());
+
+ // Draining below low replenishes back up to a high watermark's worth of
+ // buffered-plus-outstanding bytes in one batch.
+ result = harness.channel->take_up_to(10);
+ EXPECT_EQ(result.buffered_byte_count, 10u);
+ auto events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::CreditGrant);
+ EXPECT_EQ(events[0].value, 30u); // 64 high - 10 buffered - 24 outstanding
+
+ // No further grant until the buffer crosses the low watermark again.
+ result = harness.channel->take_up_to(1);
+ EXPECT_EQ(result.buffered_byte_count, 9u);
+ events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].value, 1u);
+}
+
+TEST_CASE(exact_watermark_edge_transitions)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('x', 64)))), WriteResult::Accepted);
+ (void)harness.take_events();
+
+ // Draining to exactly the low watermark does not replenish; below it does.
+ auto result = harness.channel->take_up_to(48);
+ EXPECT_EQ(result.buffered_byte_count, 16u);
+ EXPECT(harness.take_events().is_empty());
+
+ result = harness.channel->take_up_to(1);
+ EXPECT_EQ(result.buffered_byte_count, 15u);
+ auto events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::CreditGrant);
+ EXPECT_EQ(events[0].value, 49u);
+}
+
+TEST_CASE(a_write_beyond_the_outstanding_credit_dies)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('x', 64)))), WriteResult::Accepted);
+ EXPECT_DEATH("the transport delivers only what it was granted", (void)harness.channel->write(bytes_of("x"sv)));
+}
+
+TEST_CASE(a_write_after_the_transport_ended_the_body_dies)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+ EXPECT_DEATH("the transport already ended the body", (void)harness.channel->write(bytes_of("late"sv)));
+}
+
+TEST_CASE(ending_the_body_twice_dies)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->error({}), TerminateResult::Terminated);
+ EXPECT_DEATH("the transport already ended the body", (void)harness.channel->close());
+}
+
+TEST_CASE(a_write_after_consumer_cancellation_is_dropped)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::Terminated);
+ EXPECT_EQ(harness.channel->write(bytes_of("in flight"sv)), WriteResult::Rejected);
+ EXPECT_EQ(harness.channel->close(), TerminateResult::AlreadyTerminated);
+}
+
+TEST_CASE(arming_the_waiter_twice_dies)
+{
+ // The death leaves the channel locked.
+ static auto& harness = *new ChannelHarness;
+ (void)harness.channel->arm_waiter();
+ EXPECT_DEATH("a waiter is armed once per pull", (void)harness.channel->arm_waiter());
+}
+
+TEST_CASE(armed_waiter_fires_on_write_and_arms_late)
+{
+ ChannelHarness harness;
+
+ auto ticket = harness.channel->arm_waiter();
+ EXPECT(harness.take_events().is_empty());
+
+ EXPECT_EQ(harness.channel->write(bytes_of("x"sv)), WriteResult::Accepted);
+ auto events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::Wake);
+ EXPECT_EQ(events[0].value, ticket.value());
+
+ // An arm_waiter() call with bytes already pending fires at once, with the fresh ticket.
+ auto late_ticket = harness.channel->arm_waiter();
+ EXPECT(late_ticket != ticket);
+ events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].value, late_ticket.value());
+}
+
+TEST_CASE(waiter_is_one_shot)
+{
+ ChannelHarness harness;
+
+ (void)harness.channel->arm_waiter();
+ EXPECT_EQ(harness.channel->write(bytes_of("a"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write(bytes_of("b"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.take_events().size(), 1u);
+}
+
+TEST_CASE(a_drained_close_leaves_nothing_to_cancel)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::AlreadyTerminated);
+
+ auto result = harness.channel->take_all();
+ EXPECT(result.state.has<Closed>());
+}
+
+TEST_CASE(cancellation_supersedes_an_undrained_close)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("still buffered"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+
+ // A cancellation discards the bytes a closed channel still holds. The transport has ended the body and hears
+ // nothing.
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::Terminated);
+ EXPECT(harness.take_events().is_empty());
+
+ auto result = harness.channel->take_all();
+ EXPECT(result.taken.is_empty());
+ EXPECT(result.state.has<ConsumerCancelled>());
+ EXPECT_EQ(result.buffered_byte_count, 0u);
+
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::AlreadyTerminated);
+}
+
+TEST_CASE(cancellation_after_error_changes_nothing)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("junk"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->error({}), TerminateResult::Terminated);
+
+ // The error already discarded the buffer and settled the consumer.
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::AlreadyTerminated);
+ auto result = harness.channel->take_all();
+ EXPECT(result.state.has<Errored>());
+}
+
+TEST_CASE(close_preserves_buffered_bytes_until_drained)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("tail"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+
+ auto result = harness.channel->take_all();
+ EXPECT_EQ(flatten(result), "tail"sv);
+ EXPECT(result.state.has<Closed>());
+ EXPECT_EQ(result.buffered_byte_count, 0u);
+}
+
+TEST_CASE(error_discards_buffered_bytes_immediately)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("junk"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->write_copy("copied junk"sv.bytes()), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->error({ .message = MUST(ByteBuffer::copy("connection reset"sv.bytes())) }), TerminateResult::Terminated);
+
+ auto result = harness.channel->take_all();
+ EXPECT(result.taken.is_empty());
+ EXPECT_EQ(result.buffered_byte_count, 0u);
+ EXPECT(result.state.has<Errored>());
+ EXPECT_EQ(StringView { result.state.get<Errored>().message }, "connection reset"sv);
+}
+
+TEST_CASE(every_ending_wakes_a_waiter)
+{
+ enum class Ending : u8 {
+ Close,
+ Error,
+ Cancel,
+ };
+ for (auto ending : { Ending::Close, Ending::Error, Ending::Cancel }) {
+ ChannelHarness harness;
+ auto ticket = harness.channel->arm_waiter();
+
+ switch (ending) {
+ case Ending::Close:
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+ break;
+ case Ending::Error:
+ EXPECT_EQ(harness.channel->error({}), TerminateResult::Terminated);
+ break;
+ case Ending::Cancel:
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::Terminated);
+ break;
+ }
+
+ auto events = harness.take_events();
+ EXPECT(!events.is_empty());
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::Wake);
+ EXPECT_EQ(events[0].value, ticket.value());
+ }
+}
+
+TEST_CASE(cancellation_notifies_the_producer)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->write(bytes_of("in flight"sv)), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::Terminated);
+
+ auto events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::ConsumerCancelled);
+
+ auto result = harness.channel->take_all();
+ EXPECT(result.taken.is_empty());
+ EXPECT(result.state.has<ConsumerCancelled>());
+}
+
+TEST_CASE(no_credit_replenishment_after_terminal)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('x', 40)))), WriteResult::Accepted);
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+ (void)harness.take_events();
+
+ auto result = harness.channel->take_all();
+ EXPECT(result.state.has<Closed>());
+ EXPECT(harness.take_events().is_empty());
+}
+
+TEST_CASE(sink_reentrancy_does_not_deadlock)
+{
+ ChannelHarness harness { { .low = 16, .high = 64 } };
+
+ harness.consumer_sink->on_wake = [&](WakeTicket) {
+ auto result = harness.channel->take_all();
+ EXPECT(!result.taken.is_empty());
+ };
+ (void)harness.channel->arm_waiter();
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('x', 40)))), WriteResult::Accepted);
+
+ harness.consumer_sink->on_wake = nullptr;
+ harness.producer_sink->on_grant_credit = [&](u64) {
+ EXPECT_EQ(harness.channel->write(bytes_of("reentrant"sv)), WriteResult::Accepted);
+ };
+ // Draining below the low watermark grants credit, and the grant hook writes back into the channel.
+ EXPECT_EQ(harness.channel->write(bytes_of(MUST(String::repeated('y', 40)))), WriteResult::Accepted);
+ (void)harness.channel->take_all();
+ auto result = harness.channel->take_all();
+ EXPECT_EQ(flatten(result), "reentrant"sv);
+}
+
+TEST_CASE(concurrent_writer_never_loses_a_wakeup)
+{
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new LatestTicketSink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };
+ producer_sink->credit.store(channel->initial_credit(), AK::MemoryOrder::memory_order_release);
+
+ // The writer delivers only what it was granted.
+ static constexpr u64 write_count = 20'000;
+ auto writer = Threading::Thread::construct("channel writer"sv, [&channel, &producer_sink, &timed_out]() -> intptr_t {
+ for (u64 i = 0; i < write_count; ++i) {
+ auto has_credit = spin_until_or_timeout([&] {
+ return producer_sink->credit.load(AK::MemoryOrder::memory_order_acquire) > 0 || timed_out.load();
+ });
+ if (!has_credit || timed_out.load()) {
+ timed_out.store(true);
+ return 1;
+ }
+ producer_sink->credit.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
+ (void)channel->write(MUST(Core::ImmutableBytes::copy("x"sv.bytes())));
+ }
+ (void)channel->close();
+ return 0;
+ });
+ writer->start();
+
+ // Every arm_waiter() call is owed exactly one wake carrying its own ticket, fired immediately when bytes or an
+ // ending are already pending, from the writer's thread otherwise.
+ u64 total_bytes_taken = 0;
+ for (;;) {
+ auto ticket = channel->arm_waiter();
+ auto woken = spin_until_or_timeout([&] {
+ return consumer_sink->latest.load(AK::MemoryOrder::memory_order_acquire) >= ticket.value() || timed_out.load();
+ });
+ if (!woken || timed_out.load()) {
+ timed_out.store(true);
+ warnln("No wake arrived for ticket {}", ticket.value());
+ break;
+ }
+ auto result = channel->take_all();
+ for (auto const& taken : result.taken)
+ total_bytes_taken += taken.size();
+ if (!result.state.has<Open>() && result.buffered_byte_count == 0)
+ break;
+ }
+ MUST(writer->join());
+ EXPECT(!timed_out.load());
+ EXPECT_EQ(total_bytes_taken, write_count);
+}
+
+TEST_CASE(concurrent_cancellation_rejects_every_later_write)
+{
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new LatestTicketSink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);
+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };
+ IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<u64> accepted_after_rejection { 0 };
+ producer_sink->credit.store(channel->initial_credit(), AK::MemoryOrder::memory_order_release);
+
+ // The writer writes until a write is rejected, then checks that the next ones are rejected too.
+ auto writer = Threading::Thread::construct("channel writer"sv, [&channel, &producer_sink, &timed_out, &accepted_after_rejection]() -> intptr_t {
+ for (;;) {
+ auto may_write = spin_until_or_timeout([&] {
+ return producer_sink->credit.load(AK::MemoryOrder::memory_order_acquire) > 0 || producer_sink->cancellations.load() > 0;
+ });
+ if (!may_write) {
+ timed_out.store(true);
+ return 1;
+ }
+ if (producer_sink->cancellations.load() == 0)
+ producer_sink->credit.fetch_sub(1, AK::MemoryOrder::memory_order_acq_rel);
+ if (channel->write(MUST(Core::ImmutableBytes::copy("x"sv.bytes()))) == WriteResult::Rejected)
+ break;
+ }
+ for (int i = 0; i < 16; ++i) {
+ if (channel->write(MUST(Core::ImmutableBytes::copy("x"sv.bytes()))) == WriteResult::Accepted)
+ accepted_after_rejection.fetch_add(1);
+ }
+ return 0;
+ });
+ writer->start();
+
+ // The consumer drains a while, then cancels while the writer is mid-stream.
+ u64 total_bytes_taken = 0;
+ while (total_bytes_taken < 5'000) {
+ auto ticket = channel->arm_waiter();
+ auto woken = spin_until_or_timeout([&] {
+ return consumer_sink->latest.load(AK::MemoryOrder::memory_order_acquire) >= ticket.value();
+ });
+ if (!woken) {
+ timed_out.store(true);
+ warnln("No wake arrived for ticket {}", ticket.value());
+ break;
+ }
+ for (auto const& taken : channel->take_all().taken)
+ total_bytes_taken += taken.size();
+ }
+ EXPECT_EQ(channel->cancel(), TerminateResult::Terminated);
+
+ MUST(writer->join());
+ EXPECT(!timed_out.load());
+ EXPECT_EQ(accepted_after_rejection.load(), 0u);
+ EXPECT_EQ(producer_sink->cancellations.load(), 1u);
+ EXPECT(channel->take_all().state.has<ConsumerCancelled>());
+}
+
+TEST_CASE(arming_after_an_ending_fires_at_once)
+{
+ ChannelHarness harness;
+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);
+
+ auto ticket = harness.channel->arm_waiter();
+ auto events = harness.take_events();
+ EXPECT_EQ(events.size(), 1u);
+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::Wake);
+ EXPECT_EQ(events[0].value, ticket.value());
+}
diff --git a/Tests/LibWeb/TestFetchEngineMessages.cpp b/Tests/LibWeb/TestFetchEngineMessages.cpp
index 2c44d7c4b72..9c466121890 100644
--- a/Tests/LibWeb/TestFetchEngineMessages.cpp
+++ b/Tests/LibWeb/TestFetchEngineMessages.cpp
@@ -96,6 +96,29 @@ TEST_CASE(delivered_body_requires_a_valid_handle)
EXPECT_DEATH("a delivered body is never null", (void)Delivered<NonNullLocalBodyHandle> { Core::ImmutableBytes {} });
}
+namespace {
+
+class IgnoringConsumerSink final : public FetchByteChannelConsumerSink {
+ virtual void wake(WakeTicket) override { }
+};
+
+class IgnoringProducerSink final : public FetchByteChannelProducerSink {
+ virtual void grant_credit(u64) override { }
+ virtual void consumer_cancelled() override { }
+};
+
+}
+
+TEST_CASE(delivered_body_carries_a_channel_still_being_written)
+{
+ auto channel = FetchByteChannel::create({ .low = 1, .high = 2 }, adopt_ref(*new IgnoringConsumerSink), adopt_ref(*new IgnoringProducerSink));
+ LocalBodyPresentation body = Delivered<NonNullLocalBodyHandle> { channel };
+
+ auto const& delivered = body.get<Delivered<NonNullLocalBodyHandle>>();
+ EXPECT(delivered.handle().has<NonnullRefPtr<FetchByteChannel>>());
+ EXPECT_EQ(delivered.handle().get<NonnullRefPtr<FetchByteChannel>>().ptr(), channel.ptr());
+}
+
TEST_CASE(events_round_trip_their_payloads)
{
FetchTimingSeed timing_seed;