Truly independent web browser
LibWeb: Wake a response body's sniff waiter from a task
Luke Wildebe34840e32df
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cppindex 0bc6043411c..ab963a9b110 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp@@ -11,6 +11,7 @@#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>#include <LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.h>#include <LibWeb/Fetch/Infrastructure/Task.h>+#include <LibWeb/HTML/EventLoop/EventLoop.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>#include <LibWeb/Streams/ReadableStream.h>@@ -136,13 +137,19 @@ void Body::wait_for_sniff_bytes(SniffBytesCallback on_ready)void Body::notify_sniff_bytes_waiter(){- if (!m_sniff_bytes_callback)+ if (!m_sniff_bytes_callback || m_sniff_bytes_wake_queued || !sniff_bytes_if_available().has_value())return;- auto sniff_bytes = sniff_bytes_if_available();- if (!sniff_bytes.has_value())- return;- auto callback = exchange(m_sniff_bytes_callback, nullptr);- callback->function()(sniff_bytes.value());++ m_sniff_bytes_wake_queued = true;+ auto steps = GC::create_function(heap(), [body = GC::Ref { *this }] {+ body->m_sniff_bytes_wake_queued = false;+ auto sniff_bytes = body->sniff_bytes_if_available();+ if (!body->m_sniff_bytes_callback || !sniff_bytes.has_value())+ return;+ auto callback = exchange(body->m_sniff_bytes_callback, nullptr);+ callback->function()(sniff_bytes.value());+ });+ HTML::queue_a_task(HTML::Task::Source::Networking, HTML::main_thread_event_loop(), nullptr, steps);}void Body::when_network_complete(ProcessEndOfBodyCallback callback)diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.hindex 129d8015a5d..1f7e95623c8 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h@@ -106,7 +106,7 @@ public:Optional<FetchBody::SniffBytes> sniff_bytes_if_available() const { return m_fetch_body.sniff_bytes_if_available(); }void wait_for_sniff_bytes(SniffBytesCallback on_ready);- // Sniff bytes becoming available wake a pending waiter once.+ // Sniff bytes becoming available wake a pending waiter once, from a networking task.void append_sniff_bytes(ReadonlyBytes bytes);void set_sniff_bytes_complete();void set_sniff_bytes_failed();@@ -137,6 +137,7 @@ private:GC::Ptr<GC::Function<void(FetchBody::SniffBytes)>> m_sniff_bytes_callback;GC::Ptr<GC::Function<void()>> m_network_complete_callback;bool m_network_complete { false };+ bool m_sniff_bytes_wake_queued { false };};// https://fetch.spec.whatwg.org/#body-with-typediff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txtindex 31e8cec3ffd..639c23ba2c8 100644--- a/Tests/LibWeb/CMakeLists.txt+++ b/Tests/LibWeb/CMakeLists.txt@@ -27,6 +27,7 @@ set(TEST_SOURCESTestEventLoop.cppTestFaviconDecoding.cppTestFetchBodyDeliveryGate.cpp+ TestFetchBodySniffBytes.cppTestFetchByteChannel.cppTestFetchEngineMessages.cppTestFetchExecutor.cpp@@ -92,6 +93,7 @@ endif()target_link_libraries(TestDownloadReader PRIVATE LibGC LibJS LibURL)target_link_libraries(TestFaviconDecoding PRIVATE LibCore LibGC LibGfx LibJS)target_link_libraries(TestFetchBodyDeliveryGate PRIVATE LibCore LibThreading)+target_link_libraries(TestFetchBodySniffBytes PRIVATE LibCore LibGC LibJS)target_link_libraries(TestFetchByteChannel PRIVATE LibThreading)target_link_libraries(TestFetchResponse PRIVATE LibGC LibHTTP LibJS LibRequests LibURL)target_link_libraries(TestFetchURL PRIVATE LibURL)diff --git a/Tests/LibWeb/TestFetchBodySniffBytes.cpp b/Tests/LibWeb/TestFetchBodySniffBytes.cppnew file mode 100644index 00000000000..b84eeb8d866--- /dev/null+++ b/Tests/LibWeb/TestFetchBodySniffBytes.cpp@@ -0,0 +1,131 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/EventLoop.h>+#include <LibTest/TestCase.h>+#include <LibWeb/Bindings/MainThreadVM.h>+#include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>+#include <LibWeb/HTML/EventLoop/EventLoop.h>+#include <LibWeb/Platform/FontPlugin.h>++using Web::Fetch::Infrastructure::Body;+using Web::Fetch::Infrastructure::FetchBody;+using Web::Fetch::Infrastructure::MAX_SNIFF_BYTES;++namespace {++struct SniffWaiter {+ size_t wake_count { 0 };+ ByteBuffer bytes;+ Optional<FetchBody::SniffOutcome> outcome;++ GC::Ref<GC::Function<void(FetchBody::SniffBytes)>> callback(GC::Heap& heap)+ {+ return GC::create_function(heap, [this](FetchBody::SniffBytes sniff_bytes) {+ ++wake_count;+ bytes = MUST(ByteBuffer::copy(sniff_bytes.bytes));+ outcome = sniff_bytes.outcome;+ });+ }+};++GC::Ref<Body> create_body_without_source(JS::Realm& realm)+{+ auto stream = Web::Fetch::Infrastructure::byte_sequence_as_body(realm, {})->stream();+ return Body::create(stream);+}++void install_font_plugin()+{+ static Web::Platform::FontPlugin font_plugin { false };+ static bool installed = false;+ if (installed)+ return;+ Web::Platform::FontPlugin::install(font_plugin);+ installed = true;+}++void spin_until(GC::Heap& heap, Function<bool()> condition)+{+ Web::HTML::main_thread_event_loop().spin_until(GC::create_function(heap, move(condition)));+}++// Runs every networking task queued so far.+void run_queued_tasks(GC::Heap& heap)+{+ IGNORE_USE_IN_ESCAPING_LAMBDA bool reached = false;+ Web::HTML::queue_a_task(Web::HTML::Task::Source::Networking, Web::HTML::main_thread_event_loop(), nullptr, GC::create_function(heap, [&reached] {+ reached = true;+ }));+ spin_until(heap, [&reached] { return reached; });+}++}++TEST_CASE(sniff_waiter_runs_from_a_task_after_its_bytes_arrive)+{+ Core::EventLoop event_loop;+ install_font_plugin();+ auto realm = Web::Bindings::create_a_principal_javascript_realm();+ auto& heap = realm->heap();+ auto body = create_body_without_source(*realm);++ SniffWaiter waiter;+ body->wait_for_sniff_bytes(waiter.callback(heap));++ auto bytes = MUST(ByteBuffer::create_uninitialized(MAX_SNIFF_BYTES));+ bytes.bytes().fill('a');+ body->append_sniff_bytes(bytes);+ EXPECT_EQ(waiter.wake_count, 0u);++ spin_until(heap, [&waiter] { return waiter.wake_count > 0; });+ EXPECT_EQ(waiter.wake_count, 1u);+ EXPECT_EQ(waiter.bytes, bytes);+ EXPECT(waiter.outcome == FetchBody::SniffOutcome::Complete);+}++TEST_CASE(sniff_waiter_runs_once_however_often_its_bytes_are_completed)+{+ Core::EventLoop event_loop;+ install_font_plugin();+ auto realm = Web::Bindings::create_a_principal_javascript_realm();+ auto& heap = realm->heap();+ auto body = create_body_without_source(*realm);++ SniffWaiter waiter;+ body->wait_for_sniff_bytes(waiter.callback(heap));++ body->append_sniff_bytes("short"sv.bytes());+ body->set_sniff_bytes_complete();+ body->set_sniff_bytes_complete();+ body->append_sniff_bytes("ignored"sv.bytes());+ EXPECT_EQ(waiter.wake_count, 0u);++ run_queued_tasks(heap);+ EXPECT_EQ(waiter.wake_count, 1u);+ EXPECT_EQ(waiter.bytes, MUST(ByteBuffer::copy("short"sv.bytes())));+ EXPECT(waiter.outcome == FetchBody::SniffOutcome::Complete);+}++TEST_CASE(sniff_waiter_hears_a_failed_body_from_a_task)+{+ Core::EventLoop event_loop;+ install_font_plugin();+ auto realm = Web::Bindings::create_a_principal_javascript_realm();+ auto& heap = realm->heap();+ auto body = create_body_without_source(*realm);++ SniffWaiter waiter;+ body->wait_for_sniff_bytes(waiter.callback(heap));++ body->append_sniff_bytes("partial"sv.bytes());+ body->set_sniff_bytes_failed();+ EXPECT_EQ(waiter.wake_count, 0u);++ run_queued_tasks(heap);+ EXPECT_EQ(waiter.wake_count, 1u);+ EXPECT(waiter.outcome == FetchBody::SniffOutcome::Failed);+}