Skip to content

Truly independent web browser

LibWeb: Record response body sizes as Fetch receives the body

Andreas Kling4b36bc53b990
diff --git a/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp b/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp
index 4b718bf259d..a2d97514850 100644
--- a/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.cpp
@@ -67,6 +67,11 @@ void FetchedDataReceiver::handle_network_data(JS::Realm& realm, Requests::Respon
if (state == NetworkState::Complete) {
VERIFY(data.bytes().is_empty());
m_network_complete = true;
+ if (m_response) {
+ auto body_info = m_response->body_info();
+ body_info.decoded_size = m_decoded_body_size;
+ m_response->set_body_info(move(body_info));
+ }
// Mark sniff bytes as complete when the stream ends
if (m_body)
m_body->set_sniff_bytes_complete();
@@ -88,9 +93,12 @@ void FetchedDataReceiver::handle_network_data(JS::Realm& realm, Requests::Respon
// FIXME: 2. Let codings be the result of extracting header list values given `Content-Encoding` and response’s header list.
// FIXME: 3. Increase response’s body info’s encoded size by bytes’s length.
// FIXME: 4. Set bytes to the result of handling content codings given codings and bytes.
- // FIXME: 5. Increase response’s body info’s decoded size by bytes’s length.
// FIXME: 6. If bytes is failure, then terminate fetchParams’s controller.
+ // 5. Increase response’s body info’s decoded size by bytes’s length.
+ // NB: The response's body info is updated once the network completes.
+ m_decoded_body_size += bytes.size();
+
// Capture bytes for MIME sniffing
if (m_body) {
if (auto const* file_backed = data.file_backed_slice(); file_backed && !m_body->source().has_value())
diff --git a/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h b/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h
index a38f8c3e6b7..bbad190c5d0 100644
--- a/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h
+++ b/Libraries/LibWeb/Fetch/Fetching/FetchedDataReceiver.h
@@ -26,7 +26,7 @@ public:
explicit FetchedDataReceiver(GC::Ref<Streams::ReadableStream>);
virtual ~FetchedDataReceiver() override;
- void set_response(GC::Ref<Fetch::Infrastructure::Response const> response) { m_response = response; }
+ void set_response(GC::Ref<Fetch::Infrastructure::Response> response) { m_response = response; }
void set_body(GC::Ref<Fetch::Infrastructure::Body> body);
void set_network_request(RefPtr<Requests::Request> const& request) { m_network_request = request; }
@@ -48,7 +48,7 @@ private:
void close_stream(JS::Realm&);
GC::Ptr<Infrastructure::FetchParams const> m_fetch_params;
- GC::Ptr<Fetch::Infrastructure::Response const> m_response;
+ GC::Ptr<Fetch::Infrastructure::Response> m_response;
GC::Ptr<Fetch::Infrastructure::Body> m_body;
GC::Ref<Streams::ReadableStream> m_stream;
@@ -64,6 +64,7 @@ private:
Optional<Core::ImmutableBytes> m_cache_body;
bool m_cache_body_replaces_network_buffer { false };
+ u64 m_decoded_body_size { 0 };
bool m_network_complete { false };
// Bytes read off the network but not yet handed to the stream. They are delivered by a queued networking task
diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
index e61d2560a6f..1684d7f15a5 100644
--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp
@@ -2631,13 +2631,13 @@ GC::Ref<PendingResponse> nonstandard_resource_loader_file_or_http_network_fetch(
body_pump->set_cached_response_body(move(data));
});
- auto complete_load = GC::create_function(GC::Heap::the(), [&realm, pending_response, body_pump, pull_source, delivery_gate, fetch_timing_info, cross_origin_isolated_capability](bool success, Requests::RequestTimingInfo const& timing_info, Optional<StringView> error_message) {
+ auto complete_load = GC::create_function(GC::Heap::the(), [&realm, pending_response, body_pump, pull_source, delivery_gate, request, fetch_timing_info, cross_origin_isolated_capability](bool success, Requests::RequestTimingInfo const& timing_info, Optional<StringView> error_message) {
HTML::TemporaryExecutionContext execution_context { realm, HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
delivery_gate->detach_transport();
if (success) {
fetch_timing_info->update_final_timings(timing_info, cross_origin_isolated_capability);
- body_pump->handle_network_complete();
+ body_pump->handle_network_complete(Infrastructure::is_http_or_https_scheme(request->current_url().scheme()) ? Optional<u64>(max(timing_info.encoded_body_size, 0)) : Optional<u64> {});
} else {
auto error = Utf16String::formatted("Load failed: {}", error_message.value_or("Unknown error"sv));
body_pump->handle_network_error(error.to_utf8_but_should_be_ported_to_utf16());
diff --git a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
index 08a0a46e345..52ffa03eabb 100644
--- a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
+++ b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.cpp
@@ -26,6 +26,21 @@ NetworkBodyPump::NetworkBodyPump(GC::Ptr<Infrastructure::FetchParams const> fetc
NetworkBodyPump::~NetworkBodyPump() = default;
+void NetworkBodyPump::set_response(GC::Ref<Infrastructure::Response> response)
+{
+ m_response = response;
+ if (m_network_complete)
+ update_response_body_info();
+}
+
+void NetworkBodyPump::update_response_body_info()
+{
+ auto body_info = m_response->body_info();
+ body_info.encoded_size = m_encoded_body_size;
+ body_info.decoded_size = m_decoded_body_size;
+ m_response->set_body_info(move(body_info));
+}
+
void NetworkBodyPump::set_body(GC::Ref<Fetch::Infrastructure::Body> body)
{
m_body = body;
@@ -79,7 +94,6 @@ void NetworkBodyPump::handle_network_data(Requests::ResponseData data)
// FIXME: 7. Set response’s body info’s content encoding to filteredCoding.
// FIXME: 8. Increase response’s body info’s encoded size by bytes’s length.
// FIXME: 9. Set bytes to the result of handling content codings given codings and bytes.
- // FIXME: 10. Increase response’s body info’s decoded size by bytes’s length.
// FIXME: 11. If bytes is failure, then terminate fetchParams’s controller.
if (m_delivery_gate->did_deliver(bytes.size()) == Engine::FetchBodyDeliveryGate::DeliveryResult::Rejected)
@@ -110,6 +124,10 @@ void NetworkBodyPump::handle_network_data(Requests::ResponseData data)
}
}
+ // 10. Increase response’s body info’s decoded size by bytes’s length.
+ // NB: The response's body info is updated once the network completes.
+ m_decoded_body_size += bytes.size();
+
// 12. Append bytes to buffer. (Steps 12 and 13 live in FetchByteChannel's writes.)
if (file_backed)
m_channel->write(file_backed->slice);
@@ -117,9 +135,14 @@ void NetworkBodyPump::handle_network_data(Requests::ResponseData data)
m_channel->write_copy(bytes);
}
-void NetworkBodyPump::handle_network_complete()
+void NetworkBodyPump::handle_network_complete(Optional<u64> encoded_body_size)
{
m_network_complete = true;
+ // AD-HOC: RequestServer reports the encoded size of an HTTP body once it has been received. Other schemes have no
+ // content coding, so their encoded size is their decoded size.
+ m_encoded_body_size = encoded_body_size.value_or(m_decoded_body_size);
+ if (m_response)
+ update_response_body_info();
// Mark sniff bytes as complete when the stream ends
if (m_body)
m_body->set_sniff_bytes_complete();
diff --git a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.h b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.h
index 2e47bbe6f33..61e1262075a 100644
--- a/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.h
+++ b/Libraries/LibWeb/Fetch/Fetching/NetworkBodyPump.h
@@ -29,11 +29,11 @@ class NetworkBodyPump final : public JS::Cell {
public:
virtual ~NetworkBodyPump() override;
- void set_response(GC::Ref<Fetch::Infrastructure::Response const> response) { m_response = response; }
+ void set_response(GC::Ref<Fetch::Infrastructure::Response>);
void set_body(GC::Ref<Fetch::Infrastructure::Body> body);
void handle_network_data(Requests::ResponseData);
- void handle_network_complete();
+ void handle_network_complete(Optional<u64> encoded_body_size = {});
void handle_network_error(StringView error_message);
void set_cached_response_body(Core::ImmutableBytes);
@@ -46,9 +46,10 @@ private:
virtual void visit_edges(Visitor& visitor) override;
void finalize_cache_entry();
+ void update_response_body_info();
GC::Ptr<Infrastructure::FetchParams const> m_fetch_params;
- GC::Ptr<Fetch::Infrastructure::Response const> m_response;
+ GC::Ptr<Fetch::Infrastructure::Response> m_response;
GC::Ptr<Fetch::Infrastructure::Body> m_body;
NonnullRefPtr<Engine::FetchByteChannel> m_channel;
@@ -64,6 +65,8 @@ private:
Optional<Core::ImmutableBytes> m_cache_body;
bool m_cache_body_replaces_network_buffer { false };
+ u64 m_encoded_body_size { 0 };
+ u64 m_decoded_body_size { 0 };
bool m_network_complete { false };
bool m_network_failed { false };
};
diff --git a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
index ebc32c99c49..8e41c54ba7c 100644
--- a/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
+++ b/Libraries/LibWeb/HTML/NavigationParamsDescriptor.cpp
@@ -281,6 +281,7 @@ static GC::Ptr<Fetch::Infrastructure::Body> adopt_navigation_response_body(JS::R
auto receiver_root = GC::make_root(receiver);
auto cross_origin_isolated_capability = Bindings::principal_host_defined_environment_settings_object(realm).cross_origin_isolated_capability();
+ // NB: receiver_root traces response.
request->set_unbuffered_request_callbacks(
[](NonnullRefPtr<HTTP::HeaderList>, Optional<u32>, Optional<String> const&, Optional<Core::ImmutableBytes>, Optional<u64>, Requests::CacheState) { },
[receiver_root, &realm](Requests::ResponseData data) {
@@ -289,7 +290,7 @@ static GC::Ptr<Fetch::Infrastructure::Body> adopt_navigation_response_body(JS::R
[receiver_root](Core::ImmutableBytes data) {
receiver_root->set_cached_response_body(move(data));
},
- [receiver_root, stream = GC::make_root(stream), body = GC::make_root(body), timing_info, cross_origin_isolated_capability, &realm](u64, Requests::RequestTimingInfo const& request_timing_info, Optional<Requests::NetworkError> network_error) {
+ [receiver_root, stream = GC::make_root(stream), body = GC::make_root(body), response = GC::Ptr { &response }, timing_info, cross_origin_isolated_capability, &realm](u64, Requests::RequestTimingInfo const& request_timing_info, Optional<Requests::NetworkError> network_error) {
TemporaryExecutionContext execution_context { realm, TemporaryExecutionContext::CallbacksEnabled::Yes };
if (!network_error.has_value()) {
// AD-HOC: Nothing reports timing for a navigation fetch, so record the network phases and end time here
@@ -298,6 +299,9 @@ static GC::Ptr<Fetch::Infrastructure::Body> adopt_navigation_response_body(JS::R
timing_info->update_final_timings(request_timing_info, cross_origin_isolated_capability);
timing_info->set_end_time(HighResolutionTime::unsafe_shared_current_time());
}
+ auto body_info = response->body_info();
+ body_info.encoded_size = max(request_timing_info.encoded_body_size, 0);
+ response->set_body_info(move(body_info));
receiver_root->handle_network_data(realm, Requests::ResponseData::from_bytes({}), Fetch::Fetching::FetchedDataReceiver::NetworkState::Complete);
return;
}
diff --git a/Tests/LibWeb/Text/expected/Cache/resource-timing-cache-state.txt b/Tests/LibWeb/Text/expected/Cache/resource-timing-cache-state.txt
index 9643123236c..354d61b10ff 100644
--- a/Tests/LibWeb/Text/expected/Cache/resource-timing-cache-state.txt
+++ b/Tests/LibWeb/Text/expected/Cache/resource-timing-cache-state.txt
@@ -1,4 +1,4 @@
-Fetched over the network: deliveryType="" transferSize=300
+Fetched over the network: deliveryType="" transferSize=311
Served from the cache: deliveryType="cache" transferSize=0
-Fetched over the network: deliveryType="" transferSize=300
+Fetched over the network: deliveryType="" transferSize=311
Revalidated by the server: deliveryType="cache" transferSize=300