Truly independent web browser
LibWeb: Keep a file-backed body's mapping in its FetchByteChannel
Luke Wilde3447a412520c
diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cppindex 6245c5c064a..fe140bc692f 100644--- a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp@@ -127,6 +127,7 @@ FetchByteChannel::TerminateResult FetchByteChannel::terminate_locked(State termim_chunks.clear();m_copied_tail.clear();m_buffered_byte_count = 0;+ m_whole_body.clear();}m_state = move(terminal_state);wake_due_waiters_locked(notifications);@@ -167,6 +168,19 @@ FetchByteChannel::TerminateResult FetchByteChannel::error(Errored errored)return result;}+void FetchByteChannel::keep_whole_body(Core::ImmutableBytes whole_body)+{+ MutexLocker locker { m_mutex };+ if (m_state.has<Open>() && !m_whole_body.has_value())+ m_whole_body = move(whole_body);+}++Optional<Core::ImmutableBytes> FetchByteChannel::whole_body() const+{+ MutexLocker locker { m_mutex };+ return m_whole_body;+}+FetchByteChannel::TerminateResult FetchByteChannel::cancel(){PendingNotifications notifications;@@ -179,6 +193,7 @@ FetchByteChannel::TerminateResult FetchByteChannel::cancel()m_chunks.clear();m_copied_tail.clear();m_buffered_byte_count = 0;+ m_whole_body.clear();wake_due_waiters_locked(notifications);if (!m_transport_ended)notifications.append(ConsumerCancelledNotice {});@@ -201,6 +216,7 @@ Vector<Core::ImmutableBytes> FetchByteChannel::transfer_out()seal_copied_tail_locked();undrained_bytes = move(m_chunks);m_buffered_byte_count = 0;+ m_whole_body.clear();m_state = Transferred {};wake_due_waiters_locked(notifications);}diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.hindex 578ba36c7bc..6a755cc7f2d 100644--- a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h@@ -91,6 +91,11 @@ public:TerminateResult close();TerminateResult error(Errored);+ // The bytes of a transport that delivers slices of one whole body. The first handed over while the channel is open+ // is kept until it errors, is cancelled or is transferred.+ void keep_whole_body(Core::ImmutableBytes);+ [[nodiscard]] Optional<Core::ImmutableBytes> whole_body() const;+// Consumer side.// The consumer attaches its sink once, before any arm_waiter() call.void attach_consumer(NonnullRefPtr<FetchByteChannelConsumerSink>);@@ -162,6 +167,7 @@ private:u64 m_outstanding_transport_credit { 0 };State m_state { Open {} };bool m_transport_ended { false };+ Optional<Core::ImmutableBytes> m_whole_body;Vector<ArmedWaiter, 2> m_armed_waiters;u64 m_next_ticket { 1 };};diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex f1ed72d2d1d..b78b84aed83 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -1255,6 +1255,13 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams constreturn;}}+ // A body the transport delivered as slices of one mapping keeps that mapping instead of the bytes read.+ if (auto const& network_transport = response.network_transport(); network_transport.has_value()) {+ if (auto whole_body = network_transport->body_channel->whole_body(); whole_body.has_value()) {+ (algorithms->process_response_consume_body())(response_view, whole_body.release_value());+ return;+ }+ }(algorithms->process_response_consume_body())(response_view, Core::ImmutableBytes::adopt(move(bytes)));});@@ -1300,7 +1307,9 @@ void fetch_response_handover(JS::Realm& realm, Infrastructure::FetchParams const// NB: A body that carries its full contents as an in-memory source (data: URLs, cached responses, and such)// is read from that directly: Its stream may be populated thru the event loop (Blob::get_stream()// enqueues from a queued global task, e.g.) — which can't happen while the loop is paused.- if (internal_response->body()->source().has_value()) {+ auto const& network_transport = internal_response->network_transport();+ auto has_whole_body = network_transport.has_value() && network_transport->body_channel->whole_body().has_value();+ if (internal_response->body()->source().has_value() || has_whole_body) {// NB: process_body() reads the body's source itself and ignores the bytes passed here.success_steps->function()({});fetch_params.controller()->stop_body_transport();diff --git a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cppindex f3b9850d16c..82ef2a631b8 100644--- a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp@@ -106,15 +106,15 @@ void NetworkBodyPump::handle_network_data(Requests::ResponseData data)// 12. Append bytes to buffer. (Steps 12 and 13 live in FetchByteChannel's writes.)auto const* file_backed = data.file_backed_slice();- if (file_backed)+ if (file_backed) {m_channel->write(file_backed->slice);- else+ m_channel->keep_whole_body(file_backed->payload);+ } else {m_channel->write_copy(bytes);+ }// Capture bytes for MIME sniffingif (m_body) {- if (file_backed && !m_body->source().has_value())- m_body->set_source(file_backed->payload, static_cast<u64>(file_backed->payload.size()));m_body->append_sniff_bytes(bytes);} else if (m_pre_body_sniff_buffer.size() < Infrastructure::MAX_SNIFF_BYTES) {auto space_remaining = Infrastructure::MAX_SNIFF_BYTES - m_pre_body_sniff_buffer.size();diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cppindex ab963a9b110..cac0b552bdc 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp@@ -27,12 +27,6 @@ FetchBody::FetchBody(Optional<Core::ImmutableBytes> source, Optional<u64> length{}-void FetchBody::set_source(Core::ImmutableBytes source, Optional<u64> length)-{- m_source = move(source);- m_length = length;-}-void FetchBody::append_sniff_bytes(ReadonlyBytes bytes){if (m_sniff_outcome.has_value())@@ -100,12 +94,6 @@ void Body::visit_edges(Cell::Visitor& visitor)visitor.visit(m_network_complete_callback);}-void Body::set_source(Core::ImmutableBytes source, Optional<u64> length)-{- m_fetch_body.set_source(move(source), length);- notify_sniff_bytes_waiter();-}-void Body::append_sniff_bytes(ReadonlyBytes bytes){m_fetch_body.append_sniff_bytes(bytes);diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.hindex 1f7e95623c8..dea57deb0ed 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h@@ -38,7 +38,6 @@ public:}[[nodiscard]] Optional<Core::ImmutableBytes> const& source() const { return m_source; }- void set_source(Core::ImmutableBytes, Optional<u64> length);[[nodiscard]] Optional<u64> const& length() const { return m_length; }// https://mimesniff.spec.whatwg.org/#reading-the-resource-header@@ -99,7 +98,6 @@ public:void set_stream(GC::Ref<Streams::ReadableStream> value) { m_stream = value; }[[nodiscard]] FetchBody const& fetch_body() const { return m_fetch_body; }[[nodiscard]] Optional<Core::ImmutableBytes> const& source() const { return m_fetch_body.source(); }- void set_source(Core::ImmutableBytes, Optional<u64> length);[[nodiscard]] Optional<u64> const& length() const { return m_fetch_body.length(); }using SniffBytesCallback = GC::Ref<GC::Function<void(FetchBody::SniffBytes)>>;diff --git a/Tests/LibWeb/TestFetchByteChannel.cpp b/Tests/LibWeb/TestFetchByteChannel.cppindex 25f2134ab71..853d7378dcc 100644--- a/Tests/LibWeb/TestFetchByteChannel.cpp+++ b/Tests/LibWeb/TestFetchByteChannel.cpp@@ -1063,3 +1063,45 @@ TEST_CASE(peek_reports_how_the_channel_ended)EXPECT(errored_peek.taken.is_empty());EXPECT_EQ(StringView { errored_peek.state.get<Errored>().message }, "failed"sv);}++TEST_CASE(whole_body_is_kept_through_close_and_reads)+{+ ChannelHarness harness;+ auto whole_body = bytes_of("whole body"sv);+ harness.channel->keep_whole_body(whole_body);+ harness.channel->keep_whole_body(bytes_of("later"sv));+ EXPECT_EQ(harness.channel->write(bytes_of("whole"sv)), WriteResult::Accepted);+ EXPECT_EQ(harness.channel->close(), TerminateResult::Terminated);+ EXPECT_EQ(flatten(harness.channel->take_all()), "whole"sv);++ auto kept = harness.channel->whole_body();+ EXPECT(kept.has_value());+ EXPECT_EQ(kept->bytes().data(), whole_body.bytes().data());+ EXPECT(harness.channel->whole_body().has_value());+}++TEST_CASE(whole_body_is_dropped_when_the_body_errors_is_cancelled_or_is_transferred)+{+ ChannelHarness errored;+ errored.channel->keep_whole_body(bytes_of("whole body"sv));+ EXPECT_EQ(errored.channel->error({}), TerminateResult::Terminated);+ EXPECT(!errored.channel->whole_body().has_value());++ ChannelHarness cancelled;+ cancelled.channel->keep_whole_body(bytes_of("whole body"sv));+ EXPECT_EQ(cancelled.channel->cancel(), TerminateResult::Terminated);+ EXPECT(!cancelled.channel->whole_body().has_value());++ ChannelHarness transferred;+ transferred.channel->keep_whole_body(bytes_of("whole body"sv));+ (void)transferred.channel->transfer_out();+ EXPECT(!transferred.channel->whole_body().has_value());+}++TEST_CASE(whole_body_handed_over_after_the_body_ends_is_ignored)+{+ ChannelHarness harness;+ EXPECT_EQ(harness.channel->cancel(), TerminateResult::Terminated);+ harness.channel->keep_whole_body(bytes_of("whole body"sv));+ EXPECT(!harness.channel->whole_body().has_value());+}