2019-04-24 04:50:25 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. Portions
|
|
|
|
// Copyright (c) 2018 The Chromium Authors. All rights reserved. Use of this
|
|
|
|
// source code is governed by a BSD-style license that can be found in the
|
|
|
|
// LICENSE file.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/net_service/proxy_url_loader_factory.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2022-01-25 20:40:24 +01:00
|
|
|
#include <tuple>
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/barrier_closure.h"
|
2021-06-11 18:09:16 +02:00
|
|
|
#include "base/command_line.h"
|
2024-05-11 17:48:38 +02:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/context.h"
|
|
|
|
#include "cef/libcef/browser/origin_whitelist_impl.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
|
|
|
#include "cef/libcef/common/cef_switches.h"
|
|
|
|
#include "cef/libcef/common/net/scheme_registration.h"
|
|
|
|
#include "cef/libcef/common/net_service/net_service_util.h"
|
2020-03-04 01:29:39 +01:00
|
|
|
#include "components/safe_browsing/core/common/safebrowsing_constants.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/resource_context.h"
|
|
|
|
#include "content/public/browser/web_contents.h"
|
2023-11-17 00:19:27 +01:00
|
|
|
#include "content/public/common/referrer.h"
|
2019-10-01 15:55:16 +02:00
|
|
|
#include "mojo/public/cpp/base/big_buffer.h"
|
2021-03-04 23:36:57 +01:00
|
|
|
#include "mojo/public/cpp/bindings/receiver.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "net/http/http_status_code.h"
|
2020-09-09 21:59:33 +02:00
|
|
|
#include "net/url_request/redirect_util.h"
|
2020-08-29 00:39:23 +02:00
|
|
|
#include "net/url_request/url_request.h"
|
2019-06-25 22:47:34 +02:00
|
|
|
#include "services/network/public/cpp/cors/cors.h"
|
2020-08-14 21:28:23 +02:00
|
|
|
#include "services/network/public/cpp/features.h"
|
2021-07-23 18:40:13 +02:00
|
|
|
#include "services/network/public/mojom/early_hints.mojom.h"
|
2023-11-17 00:19:27 +01:00
|
|
|
#include "third_party/blink/public/common/loader/referrer_utils.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
namespace net_service {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// User data key for ResourceContextData.
|
|
|
|
const void* const kResourceContextUserDataKey = &kResourceContextUserDataKey;
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<std::string> GetHeaderString(
|
2020-08-14 21:28:23 +02:00
|
|
|
const net::HttpResponseHeaders* headers,
|
|
|
|
const std::string& header_name) {
|
|
|
|
std::string header_value;
|
|
|
|
if (!headers || !headers->GetNormalizedHeader(header_name, &header_value)) {
|
2024-03-19 22:11:42 +01:00
|
|
|
return std::nullopt;
|
2020-08-14 21:28:23 +02:00
|
|
|
}
|
|
|
|
return header_value;
|
|
|
|
}
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
void CreateProxyHelper(
|
|
|
|
content::WebContents::Getter web_contents_getter,
|
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader_receiver,
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler) {
|
|
|
|
ProxyURLLoaderFactory::CreateProxy(web_contents_getter,
|
|
|
|
std::move(loader_receiver),
|
|
|
|
std::move(request_handler));
|
|
|
|
}
|
|
|
|
|
2021-06-11 18:09:16 +02:00
|
|
|
bool DisableRequestHandlingForTesting() {
|
|
|
|
static bool disabled([]() -> bool {
|
|
|
|
return base::CommandLine::ForCurrentProcess()->HasSwitch(
|
|
|
|
switches::kDisableRequestHandlingForTesting);
|
|
|
|
}());
|
|
|
|
return disabled;
|
|
|
|
}
|
|
|
|
|
2023-11-17 00:19:27 +01:00
|
|
|
// Match logic in devtools_url_loader_interceptor.cc
|
|
|
|
// InterceptionJob::CalculateResponseTainting.
|
|
|
|
network::mojom::FetchResponseType CalculateResponseTainting(
|
|
|
|
bool should_check_cors,
|
|
|
|
network::mojom::RequestMode mode,
|
|
|
|
bool tainted_origin) {
|
|
|
|
if (should_check_cors) {
|
|
|
|
return network::mojom::FetchResponseType::kCors;
|
|
|
|
}
|
|
|
|
if (mode == network::mojom::RequestMode::kNoCors && tainted_origin) {
|
|
|
|
return network::mojom::FetchResponseType::kOpaque;
|
|
|
|
}
|
|
|
|
return network::mojom::FetchResponseType::kBasic;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Owns all of the ProxyURLLoaderFactorys for a given BrowserContext. Since
|
|
|
|
// these live on the IO thread this is done indirectly through the
|
|
|
|
// ResourceContext.
|
|
|
|
class ResourceContextData : public base::SupportsUserData::Data {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
ResourceContextData(const ResourceContextData&) = delete;
|
|
|
|
ResourceContextData& operator=(const ResourceContextData&) = delete;
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
~ResourceContextData() override = default;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
static void AddProxyOnUIThread(
|
|
|
|
ProxyURLLoaderFactory* proxy,
|
2019-10-01 15:55:16 +02:00
|
|
|
content::WebContents::Getter web_contents_getter) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
content::WebContents* web_contents = web_contents_getter.Run();
|
2019-06-18 14:28:37 +02:00
|
|
|
|
|
|
|
// Maybe the browser was destroyed while AddProxyOnUIThread was pending.
|
|
|
|
if (!web_contents) {
|
|
|
|
// Delete on the IO thread as expected by mojo bindings.
|
|
|
|
content::BrowserThread::DeleteSoon(content::BrowserThread::IO, FROM_HERE,
|
|
|
|
proxy);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
content::BrowserContext* browser_context =
|
|
|
|
web_contents->GetBrowserContext();
|
|
|
|
DCHECK(browser_context);
|
|
|
|
|
|
|
|
content::ResourceContext* resource_context =
|
|
|
|
browser_context->GetResourceContext();
|
|
|
|
DCHECK(resource_context);
|
|
|
|
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::BindOnce(ResourceContextData::AddProxy,
|
|
|
|
base::Unretained(proxy),
|
|
|
|
base::Unretained(resource_context)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddProxy(ProxyURLLoaderFactory* proxy,
|
|
|
|
content::ResourceContext* resource_context) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-06-14 18:52:28 +02:00
|
|
|
// Maybe the proxy was destroyed while AddProxyOnUIThread was pending.
|
|
|
|
if (proxy->destroyed_) {
|
|
|
|
delete proxy;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
auto* self = static_cast<ResourceContextData*>(
|
|
|
|
resource_context->GetUserData(kResourceContextUserDataKey));
|
|
|
|
if (!self) {
|
|
|
|
self = new ResourceContextData();
|
|
|
|
resource_context->SetUserData(kResourceContextUserDataKey,
|
|
|
|
base::WrapUnique(self));
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy->SetDisconnectCallback(base::BindOnce(
|
|
|
|
&ResourceContextData::RemoveProxy, self->weak_factory_.GetWeakPtr()));
|
|
|
|
self->proxies_.emplace(base::WrapUnique(proxy));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RemoveProxy(ProxyURLLoaderFactory* proxy) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
auto it = proxies_.find(proxy);
|
|
|
|
DCHECK(it != proxies_.end());
|
|
|
|
proxies_.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceContextData() : weak_factory_(this) {}
|
|
|
|
|
|
|
|
std::set<std::unique_ptr<ProxyURLLoaderFactory>, base::UniquePtrComparator>
|
|
|
|
proxies_;
|
|
|
|
|
|
|
|
base::WeakPtrFactory<ResourceContextData> weak_factory_;
|
|
|
|
};
|
|
|
|
|
2020-09-11 18:52:35 +02:00
|
|
|
// CORS preflight requests are handled in the network process, so we just need
|
|
|
|
// to continue all of the callbacks and then delete ourself.
|
|
|
|
class CorsPreflightRequest : public network::mojom::TrustedHeaderClient {
|
|
|
|
public:
|
|
|
|
explicit CorsPreflightRequest(
|
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver)
|
|
|
|
: weak_factory_(this) {
|
|
|
|
header_client_receiver_.Bind(std::move(receiver));
|
|
|
|
|
|
|
|
header_client_receiver_.set_disconnect_handler(base::BindOnce(
|
|
|
|
&CorsPreflightRequest::OnDestroy, weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:40:25 +01:00
|
|
|
CorsPreflightRequest(const CorsPreflightRequest&) = delete;
|
|
|
|
CorsPreflightRequest& operator=(const CorsPreflightRequest&) = delete;
|
|
|
|
|
2020-09-11 18:52:35 +02:00
|
|
|
// mojom::TrustedHeaderClient methods:
|
|
|
|
void OnBeforeSendHeaders(const net::HttpRequestHeaders& headers,
|
|
|
|
OnBeforeSendHeadersCallback callback) override {
|
2021-11-09 20:21:58 +01:00
|
|
|
std::move(callback).Run(net::OK, headers);
|
2020-09-11 18:52:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnHeadersReceived(const std::string& headers,
|
|
|
|
const net::IPEndPoint& remote_endpoint,
|
|
|
|
OnHeadersReceivedCallback callback) override {
|
2021-11-09 20:21:58 +01:00
|
|
|
std::move(callback).Run(net::OK, headers, /*redirect_url=*/GURL());
|
2020-09-11 18:52:35 +02:00
|
|
|
OnDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnDestroy() { delete this; }
|
|
|
|
|
|
|
|
mojo::Receiver<network::mojom::TrustedHeaderClient> header_client_receiver_{
|
|
|
|
this};
|
|
|
|
|
|
|
|
base::WeakPtrFactory<CorsPreflightRequest> weak_factory_;
|
|
|
|
};
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
//==============================
|
|
|
|
// InterceptedRequest
|
|
|
|
//=============================
|
|
|
|
|
|
|
|
// Handles intercepted, in-progress requests/responses, so that they can be
|
|
|
|
// controlled and modified accordingly.
|
|
|
|
class InterceptedRequest : public network::mojom::URLLoader,
|
|
|
|
public network::mojom::URLLoaderClient,
|
|
|
|
public network::mojom::TrustedHeaderClient {
|
|
|
|
public:
|
|
|
|
InterceptedRequest(
|
|
|
|
ProxyURLLoaderFactory* factory,
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t id,
|
2019-04-24 04:50:25 +02:00
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory);
|
2021-12-06 21:40:25 +01:00
|
|
|
|
|
|
|
InterceptedRequest(const InterceptedRequest&) = delete;
|
|
|
|
InterceptedRequest& operator=(const InterceptedRequest&) = delete;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
~InterceptedRequest() override;
|
|
|
|
|
|
|
|
// Restart the request. This happens on initial start and after redirect.
|
|
|
|
void Restart();
|
|
|
|
|
|
|
|
// Called from ProxyURLLoaderFactory::OnLoaderCreated.
|
2019-10-01 15:55:16 +02:00
|
|
|
void OnLoaderCreated(
|
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// Called from InterceptDelegate::OnInputStreamOpenFailed.
|
2024-05-13 23:36:09 +02:00
|
|
|
bool InputStreamFailed();
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// mojom::TrustedHeaderClient methods:
|
|
|
|
void OnBeforeSendHeaders(const net::HttpRequestHeaders& headers,
|
|
|
|
OnBeforeSendHeadersCallback callback) override;
|
|
|
|
void OnHeadersReceived(const std::string& headers,
|
2019-11-12 17:11:44 +01:00
|
|
|
const net::IPEndPoint& remote_endpoint,
|
2019-04-24 04:50:25 +02:00
|
|
|
OnHeadersReceivedCallback callback) override;
|
|
|
|
|
|
|
|
// mojom::URLLoaderClient methods:
|
2021-04-21 00:52:34 +02:00
|
|
|
void OnReceiveEarlyHints(network::mojom::EarlyHintsPtr early_hints) override;
|
2022-09-26 21:30:45 +02:00
|
|
|
void OnReceiveResponse(
|
|
|
|
network::mojom::URLResponseHeadPtr head,
|
|
|
|
mojo::ScopedDataPipeConsumerHandle body,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<mojo_base::BigBuffer> cached_metadata) override;
|
2019-04-24 04:50:25 +02:00
|
|
|
void OnReceiveRedirect(const net::RedirectInfo& redirect_info,
|
2019-10-01 15:55:16 +02:00
|
|
|
network::mojom::URLResponseHeadPtr head) override;
|
2019-04-24 04:50:25 +02:00
|
|
|
void OnUploadProgress(int64_t current_position,
|
|
|
|
int64_t total_size,
|
|
|
|
OnUploadProgressCallback callback) override;
|
|
|
|
void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
|
|
|
|
void OnComplete(const network::URLLoaderCompletionStatus& status) override;
|
|
|
|
|
|
|
|
// mojom::URLLoader methods:
|
2020-06-09 19:48:00 +02:00
|
|
|
void FollowRedirect(
|
|
|
|
const std::vector<std::string>& removed_headers,
|
|
|
|
const net::HttpRequestHeaders& modified_headers,
|
|
|
|
const net::HttpRequestHeaders& modified_cors_exempt_headers,
|
2024-03-19 22:11:42 +01:00
|
|
|
const std::optional<GURL>& new_url) override;
|
2019-04-24 04:50:25 +02:00
|
|
|
void SetPriority(net::RequestPriority priority,
|
|
|
|
int32_t intra_priority_value) override;
|
|
|
|
void PauseReadingBodyFromNet() override;
|
|
|
|
void ResumeReadingBodyFromNet() override;
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t id() const { return id_; }
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Helpers for determining the request handler.
|
|
|
|
void BeforeRequestReceived(const GURL& original_url,
|
|
|
|
bool intercept_request,
|
|
|
|
bool intercept_only);
|
|
|
|
void InterceptResponseReceived(const GURL& original_url,
|
2019-05-18 11:15:04 +02:00
|
|
|
std::unique_ptr<ResourceResponse> response);
|
2019-04-24 04:50:25 +02:00
|
|
|
void ContinueAfterIntercept();
|
|
|
|
void ContinueAfterInterceptWithOverride(
|
|
|
|
std::unique_ptr<ResourceResponse> response);
|
|
|
|
|
|
|
|
// Helpers for optionally overriding headers.
|
|
|
|
void HandleResponseOrRedirectHeaders(
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<net::RedirectInfo> redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
net::CompletionOnceCallback continuation);
|
|
|
|
void ContinueResponseOrRedirect(
|
|
|
|
net::CompletionOnceCallback continuation,
|
|
|
|
InterceptedRequestHandler::ResponseMode response_mode,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> override_headers,
|
|
|
|
const GURL& redirect_url);
|
|
|
|
void ContinueToHandleOverrideHeaders(int error_code);
|
|
|
|
net::RedirectInfo MakeRedirectResponseAndInfo(const GURL& new_location);
|
|
|
|
|
|
|
|
// Helpers for redirect handling.
|
|
|
|
void ContinueToBeforeRedirect(const net::RedirectInfo& redirect_info,
|
|
|
|
int error_code);
|
|
|
|
|
|
|
|
// Helpers for response handling.
|
|
|
|
void ContinueToResponseStarted(int error_code);
|
|
|
|
|
|
|
|
void OnDestroy();
|
|
|
|
|
|
|
|
void OnProcessRequestHeaders(const GURL& redirect_url,
|
|
|
|
net::HttpRequestHeaders* modified_headers,
|
|
|
|
std::vector<std::string>* removed_headers);
|
|
|
|
|
|
|
|
// This is called when the original URLLoaderClient has a connection error.
|
|
|
|
void OnURLLoaderClientError();
|
|
|
|
|
|
|
|
// This is called when the original URLLoader has a connection error.
|
|
|
|
void OnURLLoaderError(uint32_t custom_reason, const std::string& description);
|
|
|
|
|
|
|
|
// Call OnComplete on |target_client_|. If |wait_for_loader_error| is true
|
2021-03-04 23:36:57 +01:00
|
|
|
// then this object will wait for |proxied_loader_receiver_| to have a
|
2019-04-24 04:50:25 +02:00
|
|
|
// connection error before destructing.
|
|
|
|
void CallOnComplete(const network::URLLoaderCompletionStatus& status,
|
|
|
|
bool wait_for_loader_error);
|
|
|
|
|
|
|
|
void SendErrorAndCompleteImmediately(int error_code);
|
2020-08-14 21:28:23 +02:00
|
|
|
void SendErrorStatusAndCompleteImmediately(
|
|
|
|
const network::URLLoaderCompletionStatus& status);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
void SendErrorCallback(int error_code, bool safebrowsing_hit);
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
void OnUploadProgressACK();
|
|
|
|
|
2024-05-11 17:48:38 +02:00
|
|
|
const raw_ptr<ProxyURLLoaderFactory> factory_;
|
2021-04-21 00:52:34 +02:00
|
|
|
const int32_t id_;
|
2019-04-24 04:50:25 +02:00
|
|
|
const uint32_t options_;
|
|
|
|
bool request_was_redirected_ = false;
|
2020-08-13 20:45:26 +02:00
|
|
|
int redirect_limit_ = net::URLRequest::kMaxRedirects;
|
2022-05-25 15:43:30 +02:00
|
|
|
bool redirect_in_progress_ = false;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// To avoid sending multiple OnReceivedError callbacks.
|
|
|
|
bool sent_error_callback_ = false;
|
|
|
|
|
|
|
|
// When true, the loader will provide the option to intercept the request.
|
|
|
|
bool intercept_request_ = true;
|
|
|
|
|
|
|
|
// When true, the loader will not proceed unless the intercept request
|
|
|
|
// callback provided a non-null response.
|
|
|
|
bool intercept_only_ = false;
|
|
|
|
|
|
|
|
network::URLLoaderCompletionStatus status_;
|
|
|
|
bool got_loader_error_ = false;
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
// Used for rate limiting OnUploadProgress callbacks.
|
|
|
|
bool waiting_for_upload_progress_ack_ = false;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest request_;
|
2020-03-04 01:29:39 +01:00
|
|
|
network::mojom::URLResponseHeadPtr current_response_;
|
2022-02-21 23:23:40 +01:00
|
|
|
mojo::ScopedDataPipeConsumerHandle current_body_;
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<mojo_base::BigBuffer> current_cached_metadata_;
|
2020-03-04 01:29:39 +01:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> current_headers_;
|
2019-04-24 04:50:25 +02:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> override_headers_;
|
|
|
|
GURL original_url_;
|
|
|
|
GURL redirect_url_;
|
|
|
|
GURL header_client_redirect_url_;
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag traffic_annotation_;
|
2024-05-06 21:09:13 +02:00
|
|
|
std::optional<network::mojom::CredentialsMode> original_crendentials_mode_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-03-04 23:36:57 +01:00
|
|
|
mojo::Receiver<network::mojom::URLLoader> proxied_loader_receiver_;
|
|
|
|
mojo::Remote<network::mojom::URLLoaderClient> target_client_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-03-04 23:36:57 +01:00
|
|
|
mojo::Receiver<network::mojom::URLLoaderClient> proxied_client_receiver_{
|
|
|
|
this};
|
|
|
|
mojo::Remote<network::mojom::URLLoader> target_loader_;
|
|
|
|
mojo::Remote<network::mojom::URLLoaderFactory> target_factory_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
bool current_request_uses_header_client_ = false;
|
|
|
|
OnHeadersReceivedCallback on_headers_received_callback_;
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::Receiver<network::mojom::TrustedHeaderClient> header_client_receiver_{
|
|
|
|
this};
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
std::unique_ptr<StreamReaderURLLoader> stream_loader_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
base::WeakPtrFactory<InterceptedRequest> weak_factory_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InterceptDelegate : public StreamReaderURLLoader::Delegate {
|
|
|
|
public:
|
|
|
|
explicit InterceptDelegate(std::unique_ptr<ResourceResponse> response,
|
|
|
|
base::WeakPtr<InterceptedRequest> request)
|
|
|
|
: response_(std::move(response)), request_(request) {}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
bool OpenInputStream(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
OpenCallback callback) override {
|
|
|
|
return response_->OpenInputStream(request_id, request, std::move(callback));
|
|
|
|
}
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
bool OnInputStreamOpenFailed(int32_t request_id) override {
|
|
|
|
return request_->InputStreamFailed();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void GetResponseHeaders(int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
int* status_code,
|
|
|
|
std::string* reason_phrase,
|
|
|
|
std::string* mime_type,
|
|
|
|
std::string* charset,
|
|
|
|
int64_t* content_length,
|
|
|
|
HeaderMap* extra_headers) override {
|
|
|
|
response_->GetResponseHeaders(request_id, status_code, reason_phrase,
|
|
|
|
mime_type, charset, content_length,
|
|
|
|
extra_headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<ResourceResponse> response_;
|
|
|
|
base::WeakPtr<InterceptedRequest> request_;
|
|
|
|
};
|
|
|
|
|
|
|
|
InterceptedRequest::InterceptedRequest(
|
|
|
|
ProxyURLLoaderFactory* factory,
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t id,
|
2019-04-24 04:50:25 +02:00
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory)
|
2019-04-24 04:50:25 +02:00
|
|
|
: factory_(factory),
|
|
|
|
id_(id),
|
|
|
|
options_(options),
|
|
|
|
request_(request),
|
|
|
|
traffic_annotation_(traffic_annotation),
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_loader_receiver_(this, std::move(loader_receiver)),
|
2019-04-24 04:50:25 +02:00
|
|
|
target_client_(std::move(client)),
|
|
|
|
target_factory_(std::move(target_factory)),
|
|
|
|
weak_factory_(this) {
|
|
|
|
status_ = network::URLLoaderCompletionStatus(net::OK);
|
|
|
|
|
|
|
|
net::HttpRequestHeaders modified_headers;
|
|
|
|
std::vector<std::string> removed_headers;
|
|
|
|
OnProcessRequestHeaders(GURL() /* redirect_url */, &modified_headers,
|
|
|
|
&removed_headers);
|
|
|
|
|
|
|
|
// If there is a client error, clean up the request.
|
2021-03-04 23:36:57 +01:00
|
|
|
target_client_.set_disconnect_handler(base::BindOnce(
|
2019-04-24 04:50:25 +02:00
|
|
|
&InterceptedRequest::OnURLLoaderClientError, base::Unretained(this)));
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_loader_receiver_.set_disconnect_with_reason_handler(base::BindOnce(
|
|
|
|
&InterceptedRequest::OnURLLoaderError, base::Unretained(this)));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
InterceptedRequest::~InterceptedRequest() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (status_.error_code != net::OK) {
|
2019-04-24 04:50:25 +02:00
|
|
|
SendErrorCallback(status_.error_code, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
if (on_headers_received_callback_) {
|
|
|
|
std::move(on_headers_received_callback_)
|
2024-03-19 22:11:42 +01:00
|
|
|
.Run(net::ERR_ABORTED, std::nullopt, GURL());
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::Restart() {
|
2024-05-13 23:36:09 +02:00
|
|
|
// May exist if the previous stream resulted in a redirect.
|
|
|
|
if (stream_loader_) {
|
|
|
|
stream_loader_.reset();
|
|
|
|
}
|
|
|
|
|
2021-03-04 23:36:57 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
|
|
|
proxied_client_receiver_.reset();
|
2019-04-24 04:50:25 +02:00
|
|
|
target_loader_.reset();
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (header_client_receiver_.is_bound()) {
|
2022-01-25 20:40:24 +01:00
|
|
|
std::ignore = header_client_receiver_.Unbind();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
current_request_uses_header_client_ =
|
2021-03-04 23:36:57 +01:00
|
|
|
factory_->url_loader_header_client_receiver_.is_bound();
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2023-11-17 00:19:27 +01:00
|
|
|
const bool is_cross_origin =
|
|
|
|
request_.request_initiator &&
|
|
|
|
!request_.request_initiator->IsSameOriginWith(request_.url);
|
|
|
|
const bool is_cors_enabled_scheme =
|
|
|
|
scheme::IsCorsEnabledScheme(request_.url.scheme());
|
|
|
|
|
|
|
|
// Match logic in network::cors::ShouldCheckCors.
|
|
|
|
bool should_check_cors =
|
|
|
|
is_cross_origin &&
|
|
|
|
request_.mode != network::mojom::RequestMode::kNavigate &&
|
|
|
|
request_.mode != network::mojom::RequestMode::kNoCors;
|
|
|
|
|
|
|
|
if (should_check_cors && !is_cors_enabled_scheme &&
|
|
|
|
!HasCrossOriginWhitelistEntry(*request_.request_initiator,
|
|
|
|
url::Origin::Create(request_.url))) {
|
|
|
|
// Fail requests if a CORS check is required and the scheme is not CORS
|
|
|
|
// enabled. This matches the error condition that would be generated by
|
|
|
|
// CorsURLLoader::StartRequest in the network process.
|
|
|
|
SendErrorStatusAndCompleteImmediately(
|
|
|
|
network::URLLoaderCompletionStatus(network::CorsErrorStatus(
|
|
|
|
network::mojom::CorsError::kCorsDisabledScheme)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe update |credentials_mode| for fetch requests.
|
|
|
|
if (request_.credentials_mode ==
|
|
|
|
network::mojom::CredentialsMode::kSameOrigin) {
|
|
|
|
// Match logic in devtools_url_loader_interceptor.cc
|
|
|
|
// InterceptionJob::FollowRedirect.
|
|
|
|
bool tainted_origin = false;
|
|
|
|
if (redirect_in_progress_ && request_.request_initiator &&
|
|
|
|
!url::IsSameOriginWith(request_.url, original_url_) &&
|
|
|
|
!request_.request_initiator->IsSameOriginWith(original_url_)) {
|
|
|
|
tainted_origin = true;
|
2020-08-14 21:28:23 +02:00
|
|
|
}
|
2023-11-17 00:19:27 +01:00
|
|
|
|
|
|
|
// Match logic in CorsURLLoader::StartNetworkRequest.
|
|
|
|
const auto response_tainting = CalculateResponseTainting(
|
|
|
|
should_check_cors, request_.mode, tainted_origin);
|
2024-05-06 21:09:13 +02:00
|
|
|
original_crendentials_mode_ = request_.credentials_mode;
|
2023-11-17 00:19:27 +01:00
|
|
|
request_.credentials_mode =
|
|
|
|
network::cors::CalculateCredentialsFlag(request_.credentials_mode,
|
|
|
|
response_tainting)
|
|
|
|
? network::mojom::CredentialsMode::kInclude
|
|
|
|
: network::mojom::CredentialsMode::kOmit;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool should_add_origin_header =
|
|
|
|
// Cross-origin requests that are not kNavigate nor kNoCors.
|
|
|
|
should_check_cors ||
|
|
|
|
// Same-origin requests except for GET and HEAD.
|
|
|
|
(!is_cross_origin &&
|
|
|
|
request_.method != net::HttpRequestHeaders::kGetMethod &&
|
|
|
|
request_.method != net::HttpRequestHeaders::kHeadMethod);
|
|
|
|
|
|
|
|
if (should_add_origin_header) {
|
|
|
|
// Match logic in navigation_request.cc AddAdditionalRequestHeaders.
|
|
|
|
url::Origin origin_header_value =
|
|
|
|
request_.request_initiator.value_or(url::Origin());
|
|
|
|
origin_header_value = content::Referrer::SanitizeOriginForRequest(
|
|
|
|
request_.url, origin_header_value,
|
|
|
|
blink::ReferrerUtils::NetToMojoReferrerPolicy(
|
|
|
|
request_.referrer_policy));
|
|
|
|
|
|
|
|
request_.headers.SetHeaderIfMissing(net::HttpRequestHeaders::kOrigin,
|
|
|
|
origin_header_value.Serialize());
|
2020-08-14 21:28:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
const GURL original_url = request_.url;
|
|
|
|
|
|
|
|
factory_->request_handler_->OnBeforeRequest(
|
|
|
|
id_, &request_, request_was_redirected_,
|
|
|
|
base::BindOnce(&InterceptedRequest::BeforeRequestReceived,
|
2019-05-18 11:15:04 +02:00
|
|
|
weak_factory_.GetWeakPtr(), original_url),
|
|
|
|
base::BindOnce(&InterceptedRequest::SendErrorAndCompleteImmediately,
|
2019-05-19 21:21:20 +02:00
|
|
|
weak_factory_.GetWeakPtr()));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnLoaderCreated(
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver) {
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK(current_request_uses_header_client_);
|
|
|
|
|
|
|
|
// Only called if we're using the default loader.
|
2019-10-01 15:55:16 +02:00
|
|
|
header_client_receiver_.Bind(std::move(receiver));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
bool InterceptedRequest::InputStreamFailed() {
|
2019-04-24 04:50:25 +02:00
|
|
|
if (intercept_only_) {
|
|
|
|
// This can happen for unsupported schemes, when no proper
|
|
|
|
// response from the intercept handler is received, i.e.
|
|
|
|
// the provided input stream in response failed to load. In
|
|
|
|
// this case we send and error and stop loading.
|
|
|
|
SendErrorAndCompleteImmediately(net::ERR_UNKNOWN_URL_SCHEME);
|
2024-05-13 23:36:09 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2024-05-13 23:36:09 +02:00
|
|
|
return false;
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TrustedHeaderClient methods.
|
|
|
|
|
|
|
|
void InterceptedRequest::OnBeforeSendHeaders(
|
|
|
|
const net::HttpRequestHeaders& headers,
|
|
|
|
OnBeforeSendHeadersCallback callback) {
|
|
|
|
if (!current_request_uses_header_client_) {
|
2024-03-19 22:11:42 +01:00
|
|
|
std::move(callback).Run(net::OK, std::nullopt);
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request_.headers = headers;
|
2024-03-19 22:11:42 +01:00
|
|
|
std::move(callback).Run(net::OK, std::nullopt);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// Resume handling of client messages after continuing from an async callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.Resume();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-12 17:11:44 +01:00
|
|
|
void InterceptedRequest::OnHeadersReceived(
|
|
|
|
const std::string& headers,
|
|
|
|
const net::IPEndPoint& remote_endpoint,
|
|
|
|
OnHeadersReceivedCallback callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
if (!current_request_uses_header_client_) {
|
2024-03-19 22:11:42 +01:00
|
|
|
std::move(callback).Run(net::OK, std::nullopt, GURL());
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
current_headers_ = base::MakeRefCounted<net::HttpResponseHeaders>(headers);
|
2019-04-24 04:50:25 +02:00
|
|
|
on_headers_received_callback_ = std::move(callback);
|
|
|
|
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<net::RedirectInfo> redirect_info;
|
2019-04-24 04:50:25 +02:00
|
|
|
std::string location;
|
2020-03-04 01:29:39 +01:00
|
|
|
if (current_headers_->IsRedirect(&location)) {
|
2019-04-24 04:50:25 +02:00
|
|
|
const GURL new_url = request_.url.Resolve(location);
|
|
|
|
redirect_info =
|
2020-03-04 01:29:39 +01:00
|
|
|
MakeRedirectInfo(request_, current_headers_.get(), new_url, 0);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HandleResponseOrRedirectHeaders(
|
|
|
|
redirect_info,
|
|
|
|
base::BindOnce(&InterceptedRequest::ContinueToHandleOverrideHeaders,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLLoaderClient methods.
|
|
|
|
|
2021-04-21 00:52:34 +02:00
|
|
|
void InterceptedRequest::OnReceiveEarlyHints(
|
|
|
|
network::mojom::EarlyHintsPtr early_hints) {
|
|
|
|
target_client_->OnReceiveEarlyHints(std::move(early_hints));
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
void InterceptedRequest::OnReceiveResponse(
|
2022-02-21 23:23:40 +01:00
|
|
|
network::mojom::URLResponseHeadPtr head,
|
2022-09-26 21:30:45 +02:00
|
|
|
mojo::ScopedDataPipeConsumerHandle body,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<mojo_base::BigBuffer> cached_metadata) {
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_ = std::move(head);
|
2022-02-21 23:23:40 +01:00
|
|
|
current_body_ = std::move(body);
|
2022-09-26 21:30:45 +02:00
|
|
|
current_cached_metadata_ = std::move(cached_metadata);
|
2020-03-04 01:29:39 +01:00
|
|
|
|
2022-08-30 20:11:52 +02:00
|
|
|
// |current_headers_| may be null for cached responses where OnHeadersReceived
|
|
|
|
// is not called.
|
|
|
|
if (current_request_uses_header_client_ && current_headers_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Use the headers we got from OnHeadersReceived as that'll contain
|
|
|
|
// Set-Cookie if it existed.
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->headers = current_headers_;
|
|
|
|
current_headers_ = nullptr;
|
2019-04-24 04:50:25 +02:00
|
|
|
ContinueToResponseStarted(net::OK);
|
|
|
|
} else {
|
|
|
|
HandleResponseOrRedirectHeaders(
|
2024-03-19 22:11:42 +01:00
|
|
|
std::nullopt,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::BindOnce(&InterceptedRequest::ContinueToResponseStarted,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnReceiveRedirect(
|
|
|
|
const net::RedirectInfo& redirect_info,
|
2019-10-01 15:55:16 +02:00
|
|
|
network::mojom::URLResponseHeadPtr head) {
|
2022-05-25 15:43:30 +02:00
|
|
|
// Whether to notify the client. True by default so that we always notify for
|
|
|
|
// internal redirects that originate from the network process (for HSTS, etc).
|
|
|
|
// False while a redirect is in-progress to avoid duplicate notifications.
|
|
|
|
bool notify_client = !redirect_in_progress_;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_ = std::move(head);
|
2022-02-21 23:23:40 +01:00
|
|
|
current_body_.reset();
|
2022-09-26 21:30:45 +02:00
|
|
|
current_cached_metadata_.reset();
|
2020-03-04 01:29:39 +01:00
|
|
|
|
2022-08-30 20:11:52 +02:00
|
|
|
// |current_headers_| may be null for synthetic redirects where
|
|
|
|
// OnHeadersReceived is not called.
|
|
|
|
if (current_request_uses_header_client_ && current_headers_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Use the headers we got from OnHeadersReceived as that'll contain
|
2022-08-30 20:11:52 +02:00
|
|
|
// Set-Cookie if it existed.
|
|
|
|
current_response_->headers = current_headers_;
|
|
|
|
current_headers_ = nullptr;
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 20:45:26 +02:00
|
|
|
if (--redirect_limit_ == 0) {
|
|
|
|
SendErrorAndCompleteImmediately(net::ERR_TOO_MANY_REDIRECTS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
net::RedirectInfo new_redirect_info;
|
|
|
|
|
|
|
|
// When we redirect via ContinueToHandleOverrideHeaders the |redirect_info|
|
|
|
|
// value is sometimes nonsense (HTTP_OK). Also, we won't get another call to
|
2022-05-25 15:43:30 +02:00
|
|
|
// OnHeadersReceived for the new URL so we need to notify the client here.
|
2019-04-24 04:50:25 +02:00
|
|
|
if (header_client_redirect_url_.is_valid() &&
|
|
|
|
redirect_info.status_code == net::HTTP_OK) {
|
|
|
|
DCHECK(current_request_uses_header_client_);
|
2022-05-25 15:43:30 +02:00
|
|
|
notify_client = true;
|
2019-04-24 04:50:25 +02:00
|
|
|
new_redirect_info =
|
|
|
|
MakeRedirectResponseAndInfo(header_client_redirect_url_);
|
|
|
|
} else {
|
|
|
|
new_redirect_info = redirect_info;
|
|
|
|
}
|
|
|
|
|
2022-05-25 15:43:30 +02:00
|
|
|
if (notify_client) {
|
2019-04-24 04:50:25 +02:00
|
|
|
HandleResponseOrRedirectHeaders(
|
|
|
|
new_redirect_info,
|
|
|
|
base::BindOnce(&InterceptedRequest::ContinueToBeforeRedirect,
|
|
|
|
weak_factory_.GetWeakPtr(), new_redirect_info));
|
|
|
|
} else {
|
|
|
|
ContinueToBeforeRedirect(new_redirect_info, net::OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnUploadProgress(int64_t current_position,
|
|
|
|
int64_t total_size,
|
|
|
|
OnUploadProgressCallback callback) {
|
2019-05-11 00:14:48 +02:00
|
|
|
// Implement our own rate limiting for OnUploadProgress calls.
|
|
|
|
if (!waiting_for_upload_progress_ack_) {
|
|
|
|
waiting_for_upload_progress_ack_ = true;
|
|
|
|
target_client_->OnUploadProgress(
|
|
|
|
current_position, total_size,
|
|
|
|
base::BindOnce(&InterceptedRequest::OnUploadProgressACK,
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always execute the callback immediately to avoid a race between
|
|
|
|
// URLLoaderClient_OnUploadProgress_ProxyToResponder::Run() (which would
|
|
|
|
// otherwise be blocked on the target client executing the callback) and
|
|
|
|
// CallOnComplete(). If CallOnComplete() is executed first the interface pipe
|
|
|
|
// will be closed and the callback destructor will generate an assertion like:
|
|
|
|
// "URLLoaderClient::OnUploadProgressCallback was destroyed without first
|
|
|
|
// either being run or its corresponding binding being closed. It is an error
|
|
|
|
// to drop response callbacks which still correspond to an open interface
|
|
|
|
// pipe."
|
|
|
|
std::move(callback).Run();
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnTransferSizeUpdated(int32_t transfer_size_diff) {
|
|
|
|
target_client_->OnTransferSizeUpdated(transfer_size_diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnComplete(
|
|
|
|
const network::URLLoaderCompletionStatus& status) {
|
|
|
|
// Only wait for the original loader to possibly have a custom error if the
|
|
|
|
// target loader exists and succeeded. If the target loader failed, then it
|
|
|
|
// was a race as to whether that error or the safe browsing error would be
|
|
|
|
// reported.
|
|
|
|
CallOnComplete(status, !stream_loader_ && status.error_code == net::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
// URLLoader methods.
|
|
|
|
|
|
|
|
void InterceptedRequest::FollowRedirect(
|
|
|
|
const std::vector<std::string>& removed_headers_ext,
|
|
|
|
const net::HttpRequestHeaders& modified_headers_ext,
|
2020-06-09 19:48:00 +02:00
|
|
|
const net::HttpRequestHeaders& modified_cors_exempt_headers,
|
2024-03-19 22:11:42 +01:00
|
|
|
const std::optional<GURL>& new_url) {
|
2019-04-24 04:50:25 +02:00
|
|
|
std::vector<std::string> removed_headers = removed_headers_ext;
|
|
|
|
net::HttpRequestHeaders modified_headers = modified_headers_ext;
|
|
|
|
OnProcessRequestHeaders(new_url.value_or(GURL()), &modified_headers,
|
|
|
|
&removed_headers);
|
|
|
|
|
|
|
|
// If |OnURLLoaderClientError| was called then we're just waiting for the
|
2021-03-04 23:36:57 +01:00
|
|
|
// connection error handler of |proxied_loader_receiver_|. Don't restart the
|
2019-04-24 04:50:25 +02:00
|
|
|
// job since that'll create another URLLoader.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!target_client_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
// Normally we would call FollowRedirect on the target loader and it would
|
|
|
|
// begin loading the redirected request. However, the client might want to
|
|
|
|
// intercept that request so restart the job instead.
|
2019-04-24 04:50:25 +02:00
|
|
|
Restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::SetPriority(net::RequestPriority priority,
|
|
|
|
int32_t intra_priority_value) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (target_loader_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
target_loader_->SetPriority(priority, intra_priority_value);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::PauseReadingBodyFromNet() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (target_loader_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
target_loader_->PauseReadingBodyFromNet();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ResumeReadingBodyFromNet() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (target_loader_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
target_loader_->ResumeReadingBodyFromNet();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper methods.
|
|
|
|
|
|
|
|
void InterceptedRequest::BeforeRequestReceived(const GURL& original_url,
|
|
|
|
bool intercept_request,
|
|
|
|
bool intercept_only) {
|
|
|
|
intercept_request_ = intercept_request;
|
|
|
|
intercept_only_ = intercept_only;
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
if (!intercept_request_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Equivalent to no interception.
|
2019-05-18 11:15:04 +02:00
|
|
|
InterceptResponseReceived(original_url, nullptr);
|
2019-04-24 04:50:25 +02:00
|
|
|
} else {
|
|
|
|
// TODO(network): Verify the case when WebContents::RenderFrameDeleted is
|
|
|
|
// called before network request is intercepted (i.e. if that's possible
|
|
|
|
// and whether it can result in any issues).
|
|
|
|
factory_->request_handler_->ShouldInterceptRequest(
|
|
|
|
id_, &request_,
|
|
|
|
base::BindOnce(&InterceptedRequest::InterceptResponseReceived,
|
|
|
|
weak_factory_.GetWeakPtr(), original_url));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::InterceptResponseReceived(
|
|
|
|
const GURL& original_url,
|
2019-05-18 11:15:04 +02:00
|
|
|
std::unique_ptr<ResourceResponse> response) {
|
2019-04-24 04:50:25 +02:00
|
|
|
if (request_.url != original_url) {
|
|
|
|
// A response object shouldn't be created if we're redirecting.
|
|
|
|
DCHECK(!response);
|
|
|
|
|
|
|
|
// Perform the redirect.
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_ = network::mojom::URLResponseHead::New();
|
|
|
|
current_response_->request_start = base::TimeTicks::Now();
|
|
|
|
current_response_->response_start = base::TimeTicks::Now();
|
2022-02-21 23:23:40 +01:00
|
|
|
current_body_.reset();
|
2022-09-26 21:30:45 +02:00
|
|
|
current_cached_metadata_.reset();
|
2020-03-04 01:29:39 +01:00
|
|
|
|
|
|
|
auto headers = MakeResponseHeaders(
|
2019-04-24 04:50:25 +02:00
|
|
|
net::HTTP_TEMPORARY_REDIRECT, std::string(), std::string(),
|
|
|
|
std::string(), -1, {}, false /* allow_existing_header_override */);
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->headers = headers;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->encoded_data_length = headers->raw_headers().length();
|
2023-01-30 18:43:54 +01:00
|
|
|
current_response_->content_length = 0;
|
2024-01-21 20:18:09 +01:00
|
|
|
// Avoid incorrect replacement of 0 with nullptr. NOLINTNEXTLINE
|
2023-01-30 18:43:54 +01:00
|
|
|
current_response_->encoded_body_length = 0;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-25 22:47:34 +02:00
|
|
|
std::string origin;
|
|
|
|
if (request_.headers.GetHeader(net::HttpRequestHeaders::kOrigin, &origin) &&
|
|
|
|
origin != url::Origin().Serialize()) {
|
|
|
|
// Allow redirects of cross-origin resource loads.
|
2020-06-09 19:48:00 +02:00
|
|
|
headers->AddHeader(network::cors::header_names::kAccessControlAllowOrigin,
|
|
|
|
origin);
|
2019-06-25 22:47:34 +02:00
|
|
|
}
|
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
if (request_.credentials_mode ==
|
|
|
|
network::mojom::CredentialsMode::kInclude) {
|
2020-06-09 19:48:00 +02:00
|
|
|
headers->AddHeader(
|
|
|
|
network::cors::header_names::kAccessControlAllowCredentials, "true");
|
2019-07-16 23:42:04 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
const net::RedirectInfo& redirect_info =
|
2020-03-04 01:29:39 +01:00
|
|
|
MakeRedirectInfo(request_, headers.get(), request_.url, 0);
|
2019-04-24 04:50:25 +02:00
|
|
|
HandleResponseOrRedirectHeaders(
|
|
|
|
redirect_info,
|
|
|
|
base::BindOnce(&InterceptedRequest::ContinueToBeforeRedirect,
|
|
|
|
weak_factory_.GetWeakPtr(), redirect_info));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response) {
|
|
|
|
// Non-null response: make sure to use it as an override for the
|
|
|
|
// normal network data.
|
|
|
|
ContinueAfterInterceptWithOverride(std::move(response));
|
|
|
|
} else {
|
|
|
|
// Request was not intercepted/overridden. Proceed with loading
|
|
|
|
// from network, unless this is a special |intercept_only_| loader,
|
|
|
|
// which happens for external schemes (e.g. unsupported schemes).
|
|
|
|
if (intercept_only_) {
|
|
|
|
SendErrorAndCompleteImmediately(net::ERR_UNKNOWN_URL_SCHEME);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ContinueAfterIntercept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueAfterIntercept() {
|
|
|
|
if (!target_loader_ && target_factory_) {
|
|
|
|
// Even if this request does not use the header client, future redirects
|
|
|
|
// might, so we need to set the option on the loader.
|
|
|
|
uint32_t options = options_ | network::mojom::kURLLoadOptionUseHeaderClient;
|
|
|
|
target_factory_->CreateLoaderAndStart(
|
2021-04-21 00:52:34 +02:00
|
|
|
target_loader_.BindNewPipeAndPassReceiver(), id_, options, request_,
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.BindNewPipeAndPassRemote(),
|
|
|
|
traffic_annotation_);
|
2024-05-06 21:09:13 +02:00
|
|
|
if (original_crendentials_mode_) {
|
|
|
|
// Restore the original |credentials_mode| value after calling
|
|
|
|
// CreateLoaderAndStart. This matches the logic in CorsURLLoader::
|
|
|
|
// StartNetworkRequest and allows InterceptedRequest::Restart to compute
|
|
|
|
// the correct |credentials_mode| during a fetch request redirect.
|
|
|
|
request_.credentials_mode = *original_crendentials_mode_;
|
|
|
|
original_crendentials_mode_.reset();
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueAfterInterceptWithOverride(
|
|
|
|
std::unique_ptr<ResourceResponse> response) {
|
|
|
|
// StreamReaderURLLoader will synthesize TrustedHeaderClient callbacks to
|
|
|
|
// avoid having Set-Cookie headers stripped by the IPC layer.
|
|
|
|
current_request_uses_header_client_ = true;
|
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
DCHECK(!stream_loader_);
|
|
|
|
stream_loader_ = std::make_unique<StreamReaderURLLoader>(
|
2021-03-04 23:36:57 +01:00
|
|
|
id_, request_, proxied_client_receiver_.BindNewPipeAndPassRemote(),
|
|
|
|
header_client_receiver_.BindNewPipeAndPassRemote(), traffic_annotation_,
|
2022-09-26 21:30:45 +02:00
|
|
|
std::move(current_cached_metadata_),
|
2019-04-24 04:50:25 +02:00
|
|
|
std::make_unique<InterceptDelegate>(std::move(response),
|
|
|
|
weak_factory_.GetWeakPtr()));
|
|
|
|
stream_loader_->Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::HandleResponseOrRedirectHeaders(
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<net::RedirectInfo> redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
net::CompletionOnceCallback continuation) {
|
|
|
|
override_headers_ = nullptr;
|
|
|
|
redirect_url_ = redirect_info.has_value() ? redirect_info->new_url : GURL();
|
|
|
|
original_url_ = request_.url;
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!redirect_url_.is_empty()) {
|
2022-05-25 15:43:30 +02:00
|
|
|
redirect_in_progress_ = true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2022-05-25 15:43:30 +02:00
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
// |current_response_| may be nullptr when called from OnHeadersReceived.
|
|
|
|
auto headers =
|
|
|
|
current_response_ ? current_response_->headers : current_headers_;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Even though |head| is const we can get a non-const pointer to the headers
|
|
|
|
// and modifications we make are passed to the target client.
|
|
|
|
factory_->request_handler_->ProcessResponseHeaders(
|
2020-03-04 01:29:39 +01:00
|
|
|
id_, request_, redirect_url_, headers.get());
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// Pause handling of client messages before waiting on an async callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.Pause();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
factory_->request_handler_->OnRequestResponse(
|
2020-03-04 01:29:39 +01:00
|
|
|
id_, &request_, headers.get(), redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::BindOnce(&InterceptedRequest::ContinueResponseOrRedirect,
|
|
|
|
weak_factory_.GetWeakPtr(), std::move(continuation)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueResponseOrRedirect(
|
|
|
|
net::CompletionOnceCallback continuation,
|
|
|
|
InterceptedRequestHandler::ResponseMode response_mode,
|
|
|
|
scoped_refptr<net::HttpResponseHeaders> override_headers,
|
|
|
|
const GURL& redirect_url) {
|
|
|
|
if (response_mode == InterceptedRequestHandler::ResponseMode::CANCEL) {
|
|
|
|
std::move(continuation).Run(net::ERR_ABORTED);
|
|
|
|
return;
|
|
|
|
} else if (response_mode ==
|
|
|
|
InterceptedRequestHandler::ResponseMode::RESTART) {
|
|
|
|
Restart();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
override_headers_ = override_headers;
|
|
|
|
if (override_headers_) {
|
|
|
|
// Make sure to update current_response_, since when OnReceiveResponse
|
|
|
|
// is called we will not use its headers as it might be missing the
|
|
|
|
// Set-Cookie line (which gets stripped by the IPC layer).
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->headers = override_headers_;
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
redirect_url_ = redirect_url;
|
|
|
|
|
|
|
|
std::move(continuation).Run(net::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueToHandleOverrideHeaders(int error_code) {
|
|
|
|
if (error_code != net::OK) {
|
|
|
|
SendErrorAndCompleteImmediately(error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK(on_headers_received_callback_);
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<std::string> headers;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (override_headers_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
headers = override_headers_->raw_headers();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
header_client_redirect_url_ = redirect_url_;
|
|
|
|
std::move(on_headers_received_callback_).Run(net::OK, headers, redirect_url_);
|
|
|
|
|
|
|
|
override_headers_ = nullptr;
|
|
|
|
redirect_url_ = GURL();
|
|
|
|
|
|
|
|
// Resume handling of client messages after continuing from an async callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.Resume();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
net::RedirectInfo InterceptedRequest::MakeRedirectResponseAndInfo(
|
|
|
|
const GURL& new_location) {
|
|
|
|
// Clear the Content-Type values.
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->mime_type = current_response_->charset = std::string();
|
|
|
|
current_response_->headers->RemoveHeader(
|
2019-04-24 04:50:25 +02:00
|
|
|
net::HttpRequestHeaders::kContentType);
|
|
|
|
|
|
|
|
// Clear the Content-Length values.
|
2023-01-30 18:43:54 +01:00
|
|
|
current_response_->content_length = 0;
|
2024-01-21 20:18:09 +01:00
|
|
|
// Avoid incorrect replacement of 0 with nullptr. NOLINTNEXTLINE
|
2023-01-30 18:43:54 +01:00
|
|
|
current_response_->encoded_body_length = 0;
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->headers->RemoveHeader(
|
2019-04-24 04:50:25 +02:00
|
|
|
net::HttpRequestHeaders::kContentLength);
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
current_response_->encoded_data_length =
|
|
|
|
current_response_->headers->raw_headers().size();
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
const net::RedirectInfo& redirect_info = MakeRedirectInfo(
|
2020-03-04 01:29:39 +01:00
|
|
|
request_, current_response_->headers.get(), new_location, 0);
|
|
|
|
current_response_->headers->ReplaceStatusLine(
|
2019-04-24 04:50:25 +02:00
|
|
|
MakeStatusLine(redirect_info.status_code, std::string(), true));
|
|
|
|
|
|
|
|
return redirect_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueToBeforeRedirect(
|
|
|
|
const net::RedirectInfo& redirect_info,
|
|
|
|
int error_code) {
|
|
|
|
if (error_code != net::OK) {
|
|
|
|
SendErrorAndCompleteImmediately(error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
request_was_redirected_ = true;
|
2022-05-25 15:43:30 +02:00
|
|
|
redirect_in_progress_ = false;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (header_client_redirect_url_.is_valid()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
header_client_redirect_url_ = GURL();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
const GURL redirect_url = redirect_url_;
|
|
|
|
override_headers_ = nullptr;
|
|
|
|
redirect_url_ = GURL();
|
|
|
|
|
|
|
|
// Resume handling of client messages after continuing from an async callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.Resume();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2020-09-09 21:59:33 +02:00
|
|
|
const auto original_url = request_.url;
|
|
|
|
const auto original_method = request_.method;
|
|
|
|
|
|
|
|
net::RedirectInfo new_redirect_info = redirect_info;
|
2019-04-24 04:50:25 +02:00
|
|
|
if (redirect_url.is_valid()) {
|
|
|
|
new_redirect_info.new_url = redirect_url;
|
2020-09-09 21:59:33 +02:00
|
|
|
new_redirect_info.new_site_for_cookies =
|
|
|
|
net::SiteForCookies::FromUrl(redirect_url);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:33 +02:00
|
|
|
target_client_->OnReceiveRedirect(new_redirect_info,
|
|
|
|
std::move(current_response_));
|
2020-04-02 23:05:07 +02:00
|
|
|
|
2020-09-09 21:59:33 +02:00
|
|
|
request_.url = new_redirect_info.new_url;
|
|
|
|
request_.method = new_redirect_info.new_method;
|
|
|
|
request_.site_for_cookies = new_redirect_info.new_site_for_cookies;
|
|
|
|
request_.referrer = GURL(new_redirect_info.new_referrer);
|
|
|
|
request_.referrer_policy = new_redirect_info.new_referrer_policy;
|
2019-07-11 22:36:27 +02:00
|
|
|
|
2020-09-09 19:57:46 +02:00
|
|
|
if (request_.trusted_params) {
|
|
|
|
request_.trusted_params->isolation_info =
|
|
|
|
request_.trusted_params->isolation_info.CreateForRedirect(
|
|
|
|
url::Origin::Create(request_.url));
|
|
|
|
}
|
|
|
|
|
2020-09-09 21:59:33 +02:00
|
|
|
// Remove existing Cookie headers. They may be re-added after Restart().
|
|
|
|
const std::vector<std::string> remove_headers{
|
|
|
|
net::HttpRequestHeaders::kCookie};
|
2020-04-02 23:05:07 +02:00
|
|
|
|
2020-09-09 21:59:33 +02:00
|
|
|
// Use common logic for sanitizing request headers including Origin and
|
|
|
|
// Content-*.
|
|
|
|
bool should_clear_upload;
|
|
|
|
net::RedirectUtil::UpdateHttpRequest(original_url, original_method,
|
|
|
|
new_redirect_info,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::make_optional(remove_headers),
|
|
|
|
/*modified_headers=*/std::nullopt,
|
2020-09-09 21:59:33 +02:00
|
|
|
&request_.headers, &should_clear_upload);
|
|
|
|
|
|
|
|
if (should_clear_upload) {
|
|
|
|
request_.request_body = nullptr;
|
2020-04-02 23:05:07 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::ContinueToResponseStarted(int error_code) {
|
|
|
|
if (error_code != net::OK) {
|
|
|
|
SendErrorAndCompleteImmediately(error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GURL redirect_url = redirect_url_;
|
|
|
|
override_headers_ = nullptr;
|
|
|
|
redirect_url_ = GURL();
|
|
|
|
|
2020-08-14 21:28:23 +02:00
|
|
|
scoped_refptr<net::HttpResponseHeaders> headers =
|
|
|
|
current_response_ ? current_response_->headers : nullptr;
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
std::string location;
|
2020-08-14 21:28:23 +02:00
|
|
|
const bool is_redirect =
|
|
|
|
redirect_url.is_valid() || (headers && headers->IsRedirect(&location));
|
2019-04-24 04:50:25 +02:00
|
|
|
if (stream_loader_ && is_redirect) {
|
2024-05-13 23:36:09 +02:00
|
|
|
// Don't continue reading from the stream loader.
|
|
|
|
stream_loader_->Cancel();
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Redirecting from OnReceiveResponse generally isn't supported by the
|
|
|
|
// NetworkService, so we can only support it when using a custom loader.
|
|
|
|
// TODO(network): Remove this special case.
|
|
|
|
const GURL new_location = redirect_url.is_valid()
|
|
|
|
? redirect_url
|
|
|
|
: original_url_.Resolve(location);
|
|
|
|
const net::RedirectInfo& redirect_info =
|
|
|
|
MakeRedirectResponseAndInfo(new_location);
|
|
|
|
|
|
|
|
HandleResponseOrRedirectHeaders(
|
|
|
|
redirect_info,
|
|
|
|
base::BindOnce(&InterceptedRequest::ContinueToBeforeRedirect,
|
|
|
|
weak_factory_.GetWeakPtr(), redirect_info));
|
|
|
|
} else {
|
|
|
|
LOG_IF(WARNING, is_redirect) << "Redirect at this time is not supported by "
|
|
|
|
"the default network loader.";
|
|
|
|
|
2020-08-14 21:28:23 +02:00
|
|
|
// CORS check for requests that are handled by the client. Requests handled
|
|
|
|
// by the network process will be checked there.
|
2020-10-08 21:54:42 +02:00
|
|
|
if (stream_loader_ && !is_redirect && request_.request_initiator &&
|
2020-08-14 21:28:23 +02:00
|
|
|
network::cors::ShouldCheckCors(request_.url, request_.request_initiator,
|
|
|
|
request_.mode)) {
|
2022-08-23 03:37:40 +02:00
|
|
|
const auto result = network::cors::CheckAccess(
|
2020-08-14 21:28:23 +02:00
|
|
|
request_.url,
|
|
|
|
GetHeaderString(
|
|
|
|
headers.get(),
|
|
|
|
network::cors::header_names::kAccessControlAllowOrigin),
|
|
|
|
GetHeaderString(
|
|
|
|
headers.get(),
|
|
|
|
network::cors::header_names::kAccessControlAllowCredentials),
|
|
|
|
request_.credentials_mode, *request_.request_initiator);
|
2022-08-23 03:37:40 +02:00
|
|
|
if (!result.has_value() &&
|
2020-08-14 21:28:23 +02:00
|
|
|
!HasCrossOriginWhitelistEntry(*request_.request_initiator,
|
|
|
|
url::Origin::Create(request_.url))) {
|
2024-05-13 23:36:09 +02:00
|
|
|
// Don't continue reading from the stream loader.
|
|
|
|
stream_loader_->Cancel();
|
|
|
|
|
2020-08-14 21:28:23 +02:00
|
|
|
SendErrorStatusAndCompleteImmediately(
|
2022-08-23 03:37:40 +02:00
|
|
|
network::URLLoaderCompletionStatus(result.error()));
|
2020-08-14 21:28:23 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
// Resume handling of client messages after continuing from an async
|
|
|
|
// callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxied_client_receiver_.is_bound()) {
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.Resume();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2024-05-13 23:36:09 +02:00
|
|
|
if (stream_loader_) {
|
|
|
|
// Continue reading from the stream loader.
|
|
|
|
stream_loader_->Continue();
|
|
|
|
}
|
|
|
|
|
2022-06-17 15:28:55 +02:00
|
|
|
target_client_->OnReceiveResponse(
|
|
|
|
std::move(current_response_),
|
|
|
|
factory_->request_handler_->OnFilterResponseBody(
|
2022-09-26 21:30:45 +02:00
|
|
|
id_, request_, std::move(current_body_)),
|
|
|
|
std::move(current_cached_metadata_));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnDestroy() {
|
2019-06-19 15:45:23 +02:00
|
|
|
// We don't want any callbacks after this point.
|
|
|
|
weak_factory_.InvalidateWeakPtrs();
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
factory_->request_handler_->OnRequestComplete(id_, request_, status_);
|
|
|
|
|
|
|
|
// Destroys |this|.
|
|
|
|
factory_->RemoveRequest(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnProcessRequestHeaders(
|
|
|
|
const GURL& redirect_url,
|
|
|
|
net::HttpRequestHeaders* modified_headers,
|
|
|
|
std::vector<std::string>* removed_headers) {
|
|
|
|
factory_->request_handler_->ProcessRequestHeaders(
|
|
|
|
id_, request_, redirect_url, modified_headers, removed_headers);
|
|
|
|
|
|
|
|
if (!modified_headers->IsEmpty() || !removed_headers->empty()) {
|
|
|
|
request_.headers.MergeFrom(*modified_headers);
|
2023-01-02 23:59:03 +01:00
|
|
|
for (const std::string& name : *removed_headers) {
|
2019-04-24 04:50:25 +02:00
|
|
|
request_.headers.RemoveHeader(name);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnURLLoaderClientError() {
|
|
|
|
// We set |wait_for_loader_error| to true because if the loader did have a
|
|
|
|
// custom_reason error then the client would be reset as well and it would be
|
|
|
|
// a race as to which connection error we saw first.
|
|
|
|
CallOnComplete(network::URLLoaderCompletionStatus(net::ERR_ABORTED),
|
|
|
|
true /* wait_for_loader_error */);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::OnURLLoaderError(uint32_t custom_reason,
|
|
|
|
const std::string& description) {
|
2023-04-29 00:17:03 +02:00
|
|
|
if (custom_reason == network::mojom::URLLoader::kClientDisconnectReason &&
|
|
|
|
description == safe_browsing::kCustomCancelReasonForURLLoader) {
|
2020-06-09 19:48:00 +02:00
|
|
|
SendErrorCallback(safe_browsing::kNetErrorCodeForSafeBrowsing, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
got_loader_error_ = true;
|
|
|
|
|
|
|
|
// If CallOnComplete was already called, then this object is ready to be
|
|
|
|
// deleted.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!target_client_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
OnDestroy();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::CallOnComplete(
|
|
|
|
const network::URLLoaderCompletionStatus& status,
|
|
|
|
bool wait_for_loader_error) {
|
|
|
|
status_ = status;
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (target_client_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
target_client_->OnComplete(status);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2021-03-04 23:36:57 +01:00
|
|
|
if (proxied_loader_receiver_.is_bound() &&
|
2019-04-24 04:50:25 +02:00
|
|
|
(wait_for_loader_error && !got_loader_error_)) {
|
2021-03-04 23:36:57 +01:00
|
|
|
// Don't delete |this| yet, in case the |proxied_loader_receiver_|'s
|
2019-04-24 04:50:25 +02:00
|
|
|
// error_handler is called with a reason to indicate an error which we want
|
|
|
|
// to send to the client bridge. Also reset |target_client_| so we don't
|
|
|
|
// get its error_handler called and then delete |this|.
|
|
|
|
target_client_.reset();
|
|
|
|
|
|
|
|
// Since the original client is gone no need to continue loading the
|
|
|
|
// request.
|
2021-03-04 23:36:57 +01:00
|
|
|
proxied_client_receiver_.reset();
|
2019-10-01 15:55:16 +02:00
|
|
|
header_client_receiver_.reset();
|
2019-04-24 04:50:25 +02:00
|
|
|
target_loader_.reset();
|
|
|
|
|
|
|
|
// In case there are pending checks as to whether this request should be
|
|
|
|
// intercepted, we don't want that causing |target_client_| to be used
|
|
|
|
// later.
|
|
|
|
weak_factory_.InvalidateWeakPtrs();
|
|
|
|
} else {
|
|
|
|
OnDestroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::SendErrorAndCompleteImmediately(int error_code) {
|
2020-08-14 21:28:23 +02:00
|
|
|
SendErrorStatusAndCompleteImmediately(
|
|
|
|
network::URLLoaderCompletionStatus(error_code));
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::SendErrorStatusAndCompleteImmediately(
|
|
|
|
const network::URLLoaderCompletionStatus& status) {
|
|
|
|
status_ = status;
|
2019-04-24 04:50:25 +02:00
|
|
|
SendErrorCallback(status_.error_code, false);
|
|
|
|
target_client_->OnComplete(status_);
|
|
|
|
OnDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequest::SendErrorCallback(int error_code,
|
|
|
|
bool safebrowsing_hit) {
|
|
|
|
// Ensure we only send one error callback, e.g. to avoid sending two if
|
|
|
|
// there's both a networking error and safe browsing blocked the request.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (sent_error_callback_) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
sent_error_callback_ = true;
|
|
|
|
factory_->request_handler_->OnRequestError(id_, request_, error_code,
|
|
|
|
safebrowsing_hit);
|
|
|
|
}
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
void InterceptedRequest::OnUploadProgressACK() {
|
|
|
|
DCHECK(waiting_for_upload_progress_ack_);
|
|
|
|
waiting_for_upload_progress_ack_ = false;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
//==============================
|
|
|
|
// InterceptedRequestHandler
|
|
|
|
//==============================
|
|
|
|
|
2024-01-20 23:48:57 +01:00
|
|
|
InterceptedRequestHandler::InterceptedRequestHandler() = default;
|
|
|
|
InterceptedRequestHandler::~InterceptedRequestHandler() = default;
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
void InterceptedRequestHandler::OnBeforeRequest(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
bool request_was_redirected,
|
2019-05-18 11:15:04 +02:00
|
|
|
OnBeforeRequestResultCallback callback,
|
|
|
|
CancelRequestCallback cancel_callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
std::move(callback).Run(false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequestHandler::ShouldInterceptRequest(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
|
|
|
ShouldInterceptRequestResultCallback callback) {
|
2019-05-18 11:15:04 +02:00
|
|
|
std::move(callback).Run(nullptr);
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InterceptedRequestHandler::OnRequestResponse(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-04-24 04:50:25 +02:00
|
|
|
network::ResourceRequest* request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<net::RedirectInfo> redirect_info,
|
2019-04-24 04:50:25 +02:00
|
|
|
OnRequestResponseResultCallback callback) {
|
|
|
|
std::move(callback).Run(
|
|
|
|
ResponseMode::CONTINUE, nullptr,
|
|
|
|
redirect_info.has_value() ? redirect_info->new_url : GURL());
|
|
|
|
}
|
|
|
|
|
2019-05-19 21:21:20 +02:00
|
|
|
mojo::ScopedDataPipeConsumerHandle
|
|
|
|
InterceptedRequestHandler::OnFilterResponseBody(
|
2021-04-21 00:52:34 +02:00
|
|
|
int32_t request_id,
|
2019-05-19 21:21:20 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::ScopedDataPipeConsumerHandle body) {
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
//==============================
|
|
|
|
// ProxyURLLoaderFactory
|
|
|
|
//==============================
|
|
|
|
|
|
|
|
ProxyURLLoaderFactory::ProxyURLLoaderFactory(
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver,
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory_remote,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler)
|
2019-10-01 15:55:16 +02:00
|
|
|
: request_handler_(std::move(request_handler)), weak_factory_(this) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
DCHECK(request_handler_);
|
|
|
|
|
|
|
|
// Actual creation of the factory.
|
2022-03-26 02:12:30 +01:00
|
|
|
if (target_factory_remote) {
|
|
|
|
target_factory_.Bind(std::move(target_factory_remote));
|
2021-03-04 23:36:57 +01:00
|
|
|
target_factory_.set_disconnect_handler(base::BindOnce(
|
2019-09-04 17:13:32 +02:00
|
|
|
&ProxyURLLoaderFactory::OnTargetFactoryError, base::Unretained(this)));
|
|
|
|
}
|
2021-03-04 23:36:57 +01:00
|
|
|
proxy_receivers_.Add(this, std::move(factory_receiver));
|
|
|
|
proxy_receivers_.set_disconnect_handler(base::BindRepeating(
|
2019-04-24 04:50:25 +02:00
|
|
|
&ProxyURLLoaderFactory::OnProxyBindingError, base::Unretained(this)));
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (header_client_receiver) {
|
2019-10-01 15:55:16 +02:00
|
|
|
url_loader_header_client_receiver_.Bind(std::move(header_client_receiver));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-06-18 14:28:37 +02:00
|
|
|
ProxyURLLoaderFactory::~ProxyURLLoaderFactory() {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// static
|
|
|
|
void ProxyURLLoaderFactory::CreateOnIOThread(
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver,
|
2022-03-26 02:12:30 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
content::ResourceContext* resource_context,
|
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
auto proxy = new ProxyURLLoaderFactory(
|
2022-03-26 02:12:30 +01:00
|
|
|
std::move(factory_receiver), std::move(target_factory),
|
2019-10-01 15:55:16 +02:00
|
|
|
std::move(header_client_receiver), std::move(request_handler));
|
2019-04-24 04:50:25 +02:00
|
|
|
ResourceContextData::AddProxy(proxy, resource_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::SetDisconnectCallback(
|
|
|
|
DisconnectCallback on_disconnect) {
|
|
|
|
CEF_REQUIRE_IOT();
|
2019-06-14 18:52:28 +02:00
|
|
|
DCHECK(!destroyed_);
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK(!on_disconnect_);
|
|
|
|
on_disconnect_ = std::move(on_disconnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void ProxyURLLoaderFactory::CreateProxy(
|
|
|
|
content::BrowserContext* browser_context,
|
2024-02-22 19:36:15 +01:00
|
|
|
network::URLLoaderFactoryBuilder& factory_builder,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
|
|
|
|
header_client,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(request_handler);
|
|
|
|
|
2024-02-22 19:36:15 +01:00
|
|
|
auto [factory_receiver, target_factory_remote] = factory_builder.Append();
|
2019-10-01 15:55:16 +02:00
|
|
|
|
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>
|
|
|
|
header_client_receiver;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (header_client) {
|
2019-10-01 15:55:16 +02:00
|
|
|
header_client_receiver = header_client->InitWithNewPipeAndPassReceiver();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
content::ResourceContext* resource_context =
|
|
|
|
browser_context->GetResourceContext();
|
|
|
|
DCHECK(resource_context);
|
|
|
|
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_IOT,
|
|
|
|
base::BindOnce(
|
2024-02-22 19:36:15 +01:00
|
|
|
&ProxyURLLoaderFactory::CreateOnIOThread, std::move(factory_receiver),
|
2022-03-26 02:12:30 +01:00
|
|
|
std::move(target_factory_remote), std::move(header_client_receiver),
|
2019-04-24 04:50:25 +02:00
|
|
|
base::Unretained(resource_context), std::move(request_handler)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2020-09-25 03:40:47 +02:00
|
|
|
void ProxyURLLoaderFactory::CreateProxy(
|
2019-10-01 15:55:16 +02:00
|
|
|
content::WebContents::Getter web_contents_getter,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> loader_receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<InterceptedRequestHandler> request_handler) {
|
|
|
|
DCHECK(request_handler);
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (!CEF_CURRENTLY_ON_IOT()) {
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_IOT,
|
|
|
|
base::BindOnce(CreateProxyHelper, web_contents_getter,
|
|
|
|
std::move(loader_receiver), std::move(request_handler)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
auto proxy = new ProxyURLLoaderFactory(
|
2022-03-26 02:12:30 +01:00
|
|
|
std::move(loader_receiver),
|
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory>(),
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedURLLoaderHeaderClient>(),
|
|
|
|
std::move(request_handler));
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_POST_TASK(CEF_UIT,
|
|
|
|
base::BindOnce(ResourceContextData::AddProxyOnUIThread,
|
|
|
|
base::Unretained(proxy), web_contents_getter));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::CreateLoaderAndStart(
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoader> receiver,
|
2019-04-24 04:50:25 +02:00
|
|
|
int32_t request_id,
|
|
|
|
uint32_t options,
|
|
|
|
const network::ResourceRequest& request,
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderClient> client,
|
2019-04-24 04:50:25 +02:00
|
|
|
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) {
|
|
|
|
CEF_REQUIRE_IOT();
|
2019-05-29 19:01:08 +02:00
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
|
|
|
// Don't start a request while we're shutting down.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 10:03:24 +02:00
|
|
|
if (DisableRequestHandlingForTesting() && request.url.SchemeIsHTTPOrHTTPS()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// This is the so-called pass-through, no-op option.
|
2021-04-12 19:55:48 +02:00
|
|
|
if (target_factory_) {
|
2021-04-21 00:52:34 +02:00
|
|
|
target_factory_->CreateLoaderAndStart(std::move(receiver), request_id,
|
|
|
|
options, request, std::move(client),
|
|
|
|
traffic_annotation);
|
2021-04-12 19:55:48 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-10 18:10:17 +01:00
|
|
|
mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory_clone;
|
2021-04-12 19:55:48 +02:00
|
|
|
if (target_factory_) {
|
2020-02-10 18:10:17 +01:00
|
|
|
target_factory_->Clone(
|
|
|
|
target_factory_clone.InitWithNewPipeAndPassReceiver());
|
2021-04-12 19:55:48 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
InterceptedRequest* req = new InterceptedRequest(
|
2021-04-21 00:52:34 +02:00
|
|
|
this, request_id, options, request, traffic_annotation,
|
|
|
|
std::move(receiver), std::move(client), std::move(target_factory_clone));
|
2019-04-24 04:50:25 +02:00
|
|
|
requests_.insert(std::make_pair(request_id, base::WrapUnique(req)));
|
|
|
|
req->Restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::Clone(
|
2019-11-12 17:11:44 +01:00
|
|
|
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
2021-03-04 23:36:57 +01:00
|
|
|
proxy_receivers_.Add(this, std::move(factory));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::OnLoaderCreated(
|
|
|
|
int32_t request_id,
|
2019-10-01 15:55:16 +02:00
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
auto request_it = requests_.find(request_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (request_it != requests_.end()) {
|
2019-10-01 15:55:16 +02:00
|
|
|
request_it->second->OnLoaderCreated(std::move(receiver));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-12 17:11:44 +01:00
|
|
|
void ProxyURLLoaderFactory::OnLoaderForCorsPreflightCreated(
|
2020-09-11 18:52:35 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
mojo::PendingReceiver<network::mojom::TrustedHeaderClient> receiver) {
|
2019-11-12 17:11:44 +01:00
|
|
|
CEF_REQUIRE_IOT();
|
2020-09-11 18:52:35 +02:00
|
|
|
new CorsPreflightRequest(std::move(receiver));
|
2019-11-12 17:11:44 +01:00
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
void ProxyURLLoaderFactory::OnTargetFactoryError() {
|
|
|
|
// Stop calls to CreateLoaderAndStart() when |target_factory_| is invalid.
|
|
|
|
target_factory_.reset();
|
2021-03-04 23:36:57 +01:00
|
|
|
proxy_receivers_.Clear();
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
MaybeDestroySelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::OnProxyBindingError() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (proxy_receivers_.empty()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
target_factory_.reset();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
MaybeDestroySelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::RemoveRequest(InterceptedRequest* request) {
|
2021-04-21 00:52:34 +02:00
|
|
|
auto it = requests_.find(request->id());
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK(it != requests_.end());
|
|
|
|
requests_.erase(it);
|
|
|
|
|
|
|
|
MaybeDestroySelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyURLLoaderFactory::MaybeDestroySelf() {
|
|
|
|
// Even if all URLLoaderFactory pipes connected to this object have been
|
|
|
|
// closed it has to stay alive until all active requests have completed.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (target_factory_.is_bound() || !requests_.empty()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
2019-06-14 18:52:28 +02:00
|
|
|
destroyed_ = true;
|
|
|
|
|
|
|
|
// In some cases we may be destroyed before SetDisconnectCallback is called.
|
|
|
|
if (on_disconnect_) {
|
|
|
|
// Deletes |this|.
|
|
|
|
std::move(on_disconnect_).Run(this);
|
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace net_service
|