Truly independent web browser
LibWeb: Split fetch Body into a plain FetchBody and a GC stream view
Luke Wilde26c34df37b50
diff --git a/Libraries/LibWeb/Fetch/BodyInit.cpp b/Libraries/LibWeb/Fetch/BodyInit.cppindex 138e14bed13..d2c5f81067a 100644--- a/Libraries/LibWeb/Fetch/BodyInit.cpp+++ b/Libraries/LibWeb/Fetch/BodyInit.cpp@@ -170,7 +170,7 @@ WebIDL::ExceptionOr<Infrastructure::BodyWithType> extract_body(JS::Realm& realm,}// 13. Let body be a body whose stream is stream, source is source, and length is length.- auto body = Infrastructure::Body::create(*stream, move(source), move(length));+ auto body = Infrastructure::Body::create(*stream, { move(source), length });// 14. Return (body, type).return Infrastructure::BodyWithType { .body = body, .type = move(type) };diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cppindex 87668c41712..589177a5bfa 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp@@ -20,42 +20,19 @@ namespace Web::Fetch::Infrastructure {GC_DEFINE_ALLOCATOR(Body);-GC::Ref<Body> Body::create(GC::Ref<Streams::ReadableStream> stream)-{- return GC::Heap::the().allocate<Body>(stream);-}--GC::Ref<Body> Body::create(GC::Ref<Streams::ReadableStream> stream, Optional<Core::ImmutableBytes> source, Optional<u64> length)-{- return GC::Heap::the().allocate<Body>(stream, move(source), length);-}--Body::Body(GC::Ref<Streams::ReadableStream> stream)- : m_stream(stream)-{-}--Body::Body(GC::Ref<Streams::ReadableStream> stream, Optional<Core::ImmutableBytes> source, Optional<u64> length)- : m_stream(stream)- , m_source(move(source))+FetchBody::FetchBody(Optional<Core::ImmutableBytes> source, Optional<u64> length)+ : m_source(move(source)), m_length(length){}-void Body::set_source(Core::ImmutableBytes source, Optional<u64> length)+void FetchBody::set_source(Core::ImmutableBytes source, Optional<u64> length){m_source = move(source);m_length = length;}-void Body::visit_edges(Cell::Visitor& visitor)-{- Base::visit_edges(visitor);- visitor.visit(m_stream);- visitor.visit(m_sniff_bytes_callback);-}--void Body::append_sniff_bytes(ReadonlyBytes bytes)+void FetchBody::append_sniff_bytes(ReadonlyBytes bytes){if (m_sniff_outcome.has_value())return;@@ -73,29 +50,19 @@ void Body::append_sniff_bytes(ReadonlyBytes bytes)set_sniff_bytes_complete();}-void Body::set_sniff_bytes_complete()+void FetchBody::set_sniff_bytes_complete(){- if (m_sniff_outcome.has_value())- return;- m_sniff_outcome = SniffOutcome::Complete;- if (m_sniff_bytes_callback) {- auto callback = exchange(m_sniff_bytes_callback, nullptr);- callback->function()({ m_sniff_bytes, SniffOutcome::Complete });- }+ if (!m_sniff_outcome.has_value())+ m_sniff_outcome = SniffOutcome::Complete;}-void Body::set_sniff_bytes_failed()+void FetchBody::set_sniff_bytes_failed(){- if (m_sniff_outcome.has_value())- return;- m_sniff_outcome = SniffOutcome::Failed;- if (m_sniff_bytes_callback) {- auto callback = exchange(m_sniff_bytes_callback, nullptr);- callback->function()({ m_sniff_bytes, SniffOutcome::Failed });- }+ if (!m_sniff_outcome.has_value())+ m_sniff_outcome = SniffOutcome::Failed;}-Optional<Body::SniffBytes> Body::sniff_bytes_if_available() const+Optional<FetchBody::SniffBytes> FetchBody::sniff_bytes_if_available() const{// Non-streaming body: source has bytesif (m_source.has_value()) {@@ -111,6 +78,48 @@ Optional<Body::SniffBytes> Body::sniff_bytes_if_available() constreturn {};}+GC::Ref<Body> Body::create(GC::Ref<Streams::ReadableStream> stream, FetchBody fetch_body)+{+ return GC::Heap::the().allocate<Body>(stream, move(fetch_body));+}++Body::Body(GC::Ref<Streams::ReadableStream> stream, FetchBody fetch_body)+ : m_stream(stream)+ , m_fetch_body(move(fetch_body))+{+}++void Body::visit_edges(Cell::Visitor& visitor)+{+ Base::visit_edges(visitor);+ visitor.visit(m_stream);+ visitor.visit(m_sniff_bytes_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);+ notify_sniff_bytes_waiter();+}++void Body::set_sniff_bytes_complete()+{+ m_fetch_body.set_sniff_bytes_complete();+ notify_sniff_bytes_waiter();+}++void Body::set_sniff_bytes_failed()+{+ m_fetch_body.set_sniff_bytes_failed();+ notify_sniff_bytes_waiter();+}+void Body::wait_for_sniff_bytes(SniffBytesCallback on_ready){if (auto sniff_bytes = sniff_bytes_if_available(); sniff_bytes.has_value()) {@@ -122,6 +131,17 @@ void Body::wait_for_sniff_bytes(SniffBytesCallback on_ready)m_sniff_bytes_callback = on_ready;}+void Body::notify_sniff_bytes_waiter()+{+ if (!m_sniff_bytes_callback)+ 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());+}+// https://fetch.spec.whatwg.org/#concept-body-cloneGC::Ref<Body> Body::clone(JS::Realm& realm){@@ -135,7 +155,7 @@ GC::Ref<Body> Body::clone(JS::Realm& realm)m_stream = out1;// 3. Return a body whose stream is out2 and other members are copied from body.- return Body::create(*out2, m_source, m_length);+ return Body::create(*out2, m_fetch_body);}// https://fetch.spec.whatwg.org/#body-fully-readdiff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.hindex 6b2c9a5b0db..e9215d0cd07 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h@@ -25,26 +25,12 @@ namespace Web::Fetch::Infrastructure {// https://mimesniff.spec.whatwg.org/#reading-the-resource-headerstatic constexpr size_t MAX_SNIFF_BYTES = 1445;-// https://fetch.spec.whatwg.org/#concept-body-class WEB_API Body final : public JS::Cell {- GC_CELL(Body, JS::Cell);- GC_DECLARE_ALLOCATOR(Body);-+// A body's source, length and sniff bytes, without its stream.+class WEB_API FetchBody {public:- // processBody must be an algorithm accepting a byte sequence.- using ProcessBodyCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;- // processBodyError must be an algorithm optionally accepting an exception.- using ProcessBodyErrorCallback = GC::Ref<GC::Function<void(JS::Value)>>;- // processBodyChunk must be an algorithm accepting a byte sequence.- using ProcessBodyChunkCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;- // processEndOfBody must be an algorithm accepting no arguments- using ProcessEndOfBodyCallback = GC::Ref<GC::Function<void()>>;-- [[nodiscard]] static GC::Ref<Body> create(GC::Ref<Streams::ReadableStream>);- [[nodiscard]] static GC::Ref<Body> create(GC::Ref<Streams::ReadableStream>, Optional<Core::ImmutableBytes>, Optional<u64>);+ FetchBody() = default;+ FetchBody(Optional<Core::ImmutableBytes> source, Optional<u64> length);- [[nodiscard]] GC::Ref<Streams::ReadableStream> stream() const { return *m_stream; }- void set_stream(GC::Ref<Streams::ReadableStream> value) { m_stream = value; }[[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; }@@ -54,7 +40,7 @@ public:// The spec defines resource header as the byte sequence to sniff, obtained by reading// "until [...] 1445 or more bytes have been read" or end of resource is reached.// For a body with a source, bytes are available immediately.- // For streaming bodies, bytes are captured during fetch and delivered via callback.+ // For streaming bodies, bytes are captured during fetch.enum class SniffOutcome : u8 {Complete,Failed,@@ -63,12 +49,58 @@ public:ReadonlyBytes bytes;SniffOutcome outcome { SniffOutcome::Complete };};- using SniffBytesCallback = GC::Ref<GC::Function<void(SniffBytes)>>;Optional<SniffBytes> sniff_bytes_if_available() const;++ // Failed reports a response that died mid-body, whose partial bytes are not sniffable.+ void append_sniff_bytes(ReadonlyBytes bytes);+ void set_sniff_bytes_complete();+ void set_sniff_bytes_failed();++private:+ // https://fetch.spec.whatwg.org/#concept-body-source+ // A source (null, a byte sequence, a Blob object, or a FormData object), initially null.+ // NB: A Blob or FormData source is held as its bytes.+ Optional<Core::ImmutableBytes> m_source;++ // https://fetch.spec.whatwg.org/#concept-body-total-bytes+ // A length (null or an integer), initially null.+ Optional<u64> m_length;++ // https://mimesniff.spec.whatwg.org/#reading-the-resource-header+ // Non-standard: Captured "resource header" bytes for MIME type sniffing.+ ByteBuffer m_sniff_bytes;+ Optional<SniffOutcome> m_sniff_outcome;+};++// https://fetch.spec.whatwg.org/#concept-body+class WEB_API Body final : public JS::Cell {+ GC_CELL(Body, JS::Cell);+ GC_DECLARE_ALLOCATOR(Body);++public:+ // processBody must be an algorithm accepting a byte sequence.+ using ProcessBodyCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;+ // processBodyError must be an algorithm optionally accepting an exception.+ using ProcessBodyErrorCallback = GC::Ref<GC::Function<void(JS::Value)>>;+ // processBodyChunk must be an algorithm accepting a byte sequence.+ using ProcessBodyChunkCallback = GC::Ref<GC::Function<void(ByteBuffer)>>;+ // processEndOfBody must be an algorithm accepting no arguments+ using ProcessEndOfBodyCallback = GC::Ref<GC::Function<void()>>;++ [[nodiscard]] static GC::Ref<Body> create(GC::Ref<Streams::ReadableStream>, FetchBody = {});++ [[nodiscard]] GC::Ref<Streams::ReadableStream> stream() const { return *m_stream; }+ 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)>>;+ Optional<FetchBody::SniffBytes> sniff_bytes_if_available() const { return m_fetch_body.sniff_bytes_if_available(); }void wait_for_sniff_bytes(SniffBytesCallback on_ready);- // Provides sniff bytes during a streaming fetch. An ending wakes a pending waiter once: Complete delivers the- // captured resource header, and Failed reports a response that died mid-body, whose partial bytes are not sniffable.+ // Sniff bytes becoming available wake a pending waiter once.void append_sniff_bytes(ReadonlyBytes bytes);void set_sniff_bytes_complete();void set_sniff_bytes_failed();@@ -83,27 +115,17 @@ public:virtual void visit_edges(JS::Cell::Visitor&) override;private:- explicit Body(GC::Ref<Streams::ReadableStream>);- Body(GC::Ref<Streams::ReadableStream>, Optional<Core::ImmutableBytes>, Optional<u64>);+ Body(GC::Ref<Streams::ReadableStream>, FetchBody);++ void notify_sniff_bytes_waiter();// https://fetch.spec.whatwg.org/#concept-body-stream// A stream (a ReadableStream object).GC::Ref<Streams::ReadableStream> m_stream;- // https://fetch.spec.whatwg.org/#concept-body-source- // A source (null, a byte sequence, a Blob object, or a FormData object), initially null.- // NB: A Blob or FormData source is held as its bytes.- Optional<Core::ImmutableBytes> m_source;+ FetchBody m_fetch_body;- // https://fetch.spec.whatwg.org/#concept-body-total-bytes- // A length (null or an integer), initially null.- Optional<u64> m_length;-- // https://mimesniff.spec.whatwg.org/#reading-the-resource-header- // Non-standard: Captured "resource header" bytes for MIME type sniffing.- ByteBuffer m_sniff_bytes;- Optional<SniffOutcome> m_sniff_outcome;- GC::Ptr<GC::Function<void(SniffBytes)>> m_sniff_bytes_callback;+ GC::Ptr<GC::Function<void(FetchBody::SniffBytes)>> m_sniff_bytes_callback;};// https://fetch.spec.whatwg.org/#body-with-typediff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex b78135d9898..3d2b9dd3a33 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -3143,20 +3143,20 @@ void LocalNavigable::queue_navigation_and_traversal_task_for_session_history_ent// Get sniff bytes for MIME type detection. For streaming responses where bytes// haven't arrived yet, we must wait asynchronously.- auto sniff_bytes = body ? body->sniff_bytes_if_available() : Optional<Fetch::Infrastructure::Body::SniffBytes> { Fetch::Infrastructure::Body::SniffBytes {} };+ auto sniff_bytes = body ? body->sniff_bytes_if_available() : Optional<Fetch::Infrastructure::FetchBody::SniffBytes> { Fetch::Infrastructure::FetchBody::SniffBytes {} };if (!sniff_bytes.has_value()) {// Async path: bytes not yet available, wait for themnav_params->response->resume_body_delivery_up_to(Fetch::Infrastructure::MAX_SNIFF_BYTES);body->wait_for_sniff_bytes(GC::create_function(heap(),- [output, nav_params, navigation_params, completion_steps, source_allows_downloading, source_interface_origin, url](Fetch::Infrastructure::Body::SniffBytes sniff_bytes) {+ [output, nav_params, navigation_params, completion_steps, source_allows_downloading, source_interface_origin, url](Fetch::Infrastructure::FetchBody::SniffBytes sniff_bytes) {// AD-HOC: The document may have been destroyed between when the fetch started and when the// bytes arrived. A response that failed mid-sniff must not be loaded as a document// off its partial bytes either.- if (nav_params->navigable->active_browsing_context() && sniff_bytes.outcome == Fetch::Infrastructure::Body::SniffOutcome::Complete) {+ if (nav_params->navigable->active_browsing_context() && sniff_bytes.outcome == Fetch::Infrastructure::FetchBody::SniffOutcome::Complete) {load_document_or_handle_download(nav_params, sniff_bytes.bytes, url, source_allows_downloading, source_interface_origin, *output);} else {stop_or_resume_response_body_delivery(navigation_params);- if (sniff_bytes.outcome == Fetch::Infrastructure::Body::SniffOutcome::Failed)+ if (sniff_bytes.outcome == Fetch::Infrastructure::FetchBody::SniffOutcome::Failed)output->save_extra_document_state = false;}output->navigation_params = navigation_params;@@ -3168,7 +3168,7 @@ void LocalNavigable::queue_navigation_and_traversal_task_for_session_history_ent}// Sync path: bytes available immediately- if (sniff_bytes->outcome == Fetch::Infrastructure::Body::SniffOutcome::Complete) {+ if (sniff_bytes->outcome == Fetch::Infrastructure::FetchBody::SniffOutcome::Complete) {load_document_or_handle_download(nav_params, sniff_bytes->bytes, url, source_allows_downloading, source_interface_origin, *output);} else {stop_or_resume_response_body_delivery(navigation_params);diff --git a/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp b/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cppindex 3621ba06230..49d575e1bb0 100644--- a/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp+++ b/Libraries/LibWeb/HTML/Parser/IncrementalDocumentParser.cpp@@ -62,7 +62,7 @@ void IncrementalDocumentParser::start()// FIXME: The spec allows starting the parse after 500 ms or 1024 bytes, whichever comes first.// We only honor the byte threshold.auto parser = GC::Ref { *this };- m_body->wait_for_sniff_bytes(GC::create_function(GC::Heap::the(), [parser](Web::Fetch::Infrastructure::Body::SniffBytes sniff_bytes) {+ m_body->wait_for_sniff_bytes(GC::create_function(GC::Heap::the(), [parser](Web::Fetch::Infrastructure::FetchBody::SniffBytes sniff_bytes) {// A failed response initializes the parser too.parser->initialize_parser(sniff_bytes.bytes);}));