electron/patches/chromium/feat_expose_raw_response_he...

159 строки
8.3 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: deepak1556 <hop2deep@gmail.com>
Date: Tue, 17 Aug 2021 22:42:42 -0700
Subject: feat: expose raw response headers from URLLoader
With https://chromium-review.googlesource.com/c/chromium/src/+/2856099
unfiltered response headers are only available via trusted channel
through //services/network/public/mojom/devtools_observer.mojom.
https://github.com/electron/electron/pull/30503/commits/28f4da1582d046e96cb58f3cbb590503e89dfd0d
was an attempt to use this interface but given the original response is
signalled on a different interface via URLLoaderClient::OnReceiveResponse
it is harder to sync these data from two different channels for a synchronous
event emitted on the SimpleURLLoaderWrapper::OnResponseStarted.
This patch does the minimal approach to add back the raw response
headers, moving forward we should find a way in upstream to provide
access to these headers for loader clients created on the browser process.
diff --git a/services/network/public/cpp/resource_request.cc b/services/network/public/cpp/resource_request.cc
index f14c99863eb3f31ebc9ffd30a55f7536585fb561..d1f11d2d13445c9814d2e7227e5e02001377329b 100644
--- a/services/network/public/cpp/resource_request.cc
+++ b/services/network/public/cpp/resource_request.cc
@@ -234,6 +234,7 @@ bool ResourceRequest::EqualsForTesting(const ResourceRequest& request) const {
do_not_prompt_for_login == request.do_not_prompt_for_login &&
is_main_frame == request.is_main_frame &&
transition_type == request.transition_type &&
+ report_raw_headers == request.report_raw_headers &&
previews_state == request.previews_state &&
upgrade_if_insecure == request.upgrade_if_insecure &&
is_revalidating == request.is_revalidating &&
diff --git a/services/network/public/cpp/resource_request.h b/services/network/public/cpp/resource_request.h
index 7c835d244420afe1c9ab6eac267c8789fb2afb01..564caebc55e1338e7124731d639cd6f01aebf595 100644
--- a/services/network/public/cpp/resource_request.h
+++ b/services/network/public/cpp/resource_request.h
@@ -157,6 +157,7 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE) ResourceRequest {
bool do_not_prompt_for_login = false;
bool is_main_frame = false;
int transition_type = 0;
+ bool report_raw_headers = false;
int previews_state = 0;
bool upgrade_if_insecure = false;
bool is_revalidating = false;
diff --git a/services/network/public/cpp/url_request_mojom_traits.cc b/services/network/public/cpp/url_request_mojom_traits.cc
index 9fefb0ca05e8f461d22b7f9875cc722232ccaed4..9c020bd1272ad2894f0b5af4c504d551b9927af0 100644
--- a/services/network/public/cpp/url_request_mojom_traits.cc
+++ b/services/network/public/cpp/url_request_mojom_traits.cc
@@ -210,6 +210,7 @@ bool StructTraits<
out->do_not_prompt_for_login = data.do_not_prompt_for_login();
out->is_main_frame = data.is_main_frame();
out->transition_type = data.transition_type();
+ out->report_raw_headers = data.report_raw_headers();
out->previews_state = data.previews_state();
out->upgrade_if_insecure = data.upgrade_if_insecure();
out->is_revalidating = data.is_revalidating();
diff --git a/services/network/public/cpp/url_request_mojom_traits.h b/services/network/public/cpp/url_request_mojom_traits.h
index 53b1cd261ec419dd68906186a3da651fe792ede0..5945e6d9749ad2a888edfa0c4b00377a7190881b 100644
--- a/services/network/public/cpp/url_request_mojom_traits.h
+++ b/services/network/public/cpp/url_request_mojom_traits.h
@@ -272,6 +272,9 @@ struct COMPONENT_EXPORT(NETWORK_CPP_BASE)
static int32_t transition_type(const network::ResourceRequest& request) {
return request.transition_type;
}
+ static bool report_raw_headers(const network::ResourceRequest& request) {
+ return request.report_raw_headers;
+ }
static int32_t previews_state(const network::ResourceRequest& request) {
return request.previews_state;
}
diff --git a/services/network/public/mojom/url_request.mojom b/services/network/public/mojom/url_request.mojom
index 27477d595fd22dc49329631a0b52ebdaafe1edb1..a4fd22381738648040cd72dce747285f5fcb698a 100644
--- a/services/network/public/mojom/url_request.mojom
+++ b/services/network/public/mojom/url_request.mojom
@@ -318,6 +318,9 @@ struct URLRequest {
// about this.
int32 transition_type;
+ // Whether to provide unfiltered response headers.
+ bool report_raw_headers;
+
// Whether or not to request a Preview version of the resource or let the
// browser decide.
// Note: this is an enum of type PreviewsState.
diff --git a/services/network/public/mojom/url_response_head.mojom b/services/network/public/mojom/url_response_head.mojom
index 80f26b62b25bd6aa46886e3112b2ffd2698693fe..e72cab84db36eec86309160d4f7416ee431f5e34 100644
--- a/services/network/public/mojom/url_response_head.mojom
+++ b/services/network/public/mojom/url_response_head.mojom
@@ -7,6 +7,7 @@ module network.mojom;
import "mojo/public/mojom/base/time.mojom";
import "mojo/public/mojom/base/unguessable_token.mojom";
import "services/network/public/mojom/fetch_api.mojom";
+import "services/network/public/mojom/http_raw_headers.mojom";
import "services/network/public/mojom/ip_endpoint.mojom";
import "services/network/public/mojom/load_timing_info.mojom";
import "services/network/public/mojom/network_param.mojom";
@@ -28,6 +29,9 @@ struct URLResponseHead {
// The response headers or NULL if the URL type does not support headers.
HttpResponseHeaders headers;
+ // Actual response headers, as obtained from the network stack.
+ array<HttpRawHeaderPair> raw_response_headers;
+
// The mime type of the response. This may be a derived value.
string mime_type;
diff --git a/services/network/url_loader.cc b/services/network/url_loader.cc
index 59a89d471326069e183cdcc42af862fe19d7448e..39cd96d5e5785879bd3d82fb4c71fdf80c48e5ee 100644
--- a/services/network/url_loader.cc
+++ b/services/network/url_loader.cc
@@ -541,6 +541,7 @@ URLLoader::URLLoader(
peer_closed_handle_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL,
base::SequencedTaskRunnerHandle::Get()),
+ report_raw_headers_(request.report_raw_headers),
devtools_request_id_(request.devtools_request_id),
request_mode_(request.mode),
request_credentials_mode_(request.credentials_mode),
@@ -699,7 +700,7 @@ URLLoader::URLLoader(
url_request_->SetRequestHeadersCallback(base::BindRepeating(
&URLLoader::SetRawRequestHeadersAndNotify, base::Unretained(this)));
- if (devtools_request_id()) {
+ if (devtools_request_id() || report_raw_headers_) {
url_request_->SetResponseHeadersCallback(base::BindRepeating(
&URLLoader::SetRawResponseHeaders, base::Unretained(this)));
}
@@ -1349,6 +1350,19 @@ void URLLoader::OnResponseStarted(net::URLRequest* url_request, int net_error) {
response_ = network::mojom::URLResponseHead::New();
PopulateResourceResponse(url_request_.get(), is_load_timing_enabled_,
options_, response_.get());
+ if (raw_response_headers_ && report_raw_headers_) {
+ std::vector<network::mojom::HttpRawHeaderPairPtr> header_array;
+ size_t iterator = 0;
+ std::string name, value;
+ while (raw_response_headers_->EnumerateHeaderLines(&iterator, &name, &value)) {
+ network::mojom::HttpRawHeaderPairPtr pair =
+ network::mojom::HttpRawHeaderPair::New();
+ pair->key = name;
+ pair->value = value;
+ header_array.push_back(std::move(pair));
+ }
+ response_->raw_response_headers = std::move(header_array);
+ }
DispatchOnRawResponse();
// Parse and remove the Trust Tokens response headers, if any are expected,
diff --git a/services/network/url_loader.h b/services/network/url_loader.h
index fcc47a2389c5172e3c674e413baa857183e0f2f8..af952b03ca87c4022400ba93d2e1fb8d3759960a 100644
--- a/services/network/url_loader.h
+++ b/services/network/url_loader.h
@@ -503,6 +503,8 @@ class COMPONENT_EXPORT(NETWORK_SERVICE) URLLoader
std::unique_ptr<ResourceScheduler::ScheduledResourceRequest>
resource_scheduler_request_handle_;
+ // Whether client requested raw headers.
+ bool report_raw_headers_ = false;
bool enable_reporting_raw_headers_ = false;
bool seen_raw_request_headers_ = false;
scoped_refptr<const net::HttpResponseHeaders> raw_response_headers_;