/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim:set ts=2 sw=2 sts=2 et cindent: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// These pointers are declared in nsProtocolProxyService.cpp externconstchar kProxyType_HTTPS[]; externconstchar kProxyType_DIRECT[];
// The PAC thread does evaluations of both PAC files and // nsISystemProxySettings because they can both block the calling thread and we // don't want that on the main thread
// Check to see if the underlying request was not an error page in the case of // a HTTP request. For other types of channels, just return true. staticbool HttpRequestSucceeded(nsIStreamLoader* loader) {
nsCOMPtr<nsIRequest> request;
loader->GetRequest(getter_AddRefs(request));
bool result = true; // default to assuming success
// Read preference setting of extra JavaScript context heap size. // PrefService tends to be run on main thread, where ProxyAutoConfig runs on // ProxyResolution thread, so it's read here and passed to ProxyAutoConfig. static uint32_t GetExtraJSContextHeapSize() {
MOZ_ASSERT(NS_IsMainThread());
if (prefs &&
NS_SUCCEEDED(prefs->GetIntPref( "network.proxy.autoconfig_extra_jscontext_heap_size", &value))) {
LOG(("autoconfig_extra_jscontext_heap_size: %d\n", value));
extraSize = value;
}
}
return extraSize < 0 ? 0 : extraSize;
}
// Read network proxy type from preference // Used to verify that the preference is WPAD in nsPACMan::ConfigureWPAD
nsresult GetNetworkProxyTypeFromPref(int32_t* type) {
*type = 0;
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
if (!prefs) {
LOG(("Failed to get a preference service object")); return NS_ERROR_FACTORY_NOT_REGISTERED;
}
nsresult rv = prefs->GetIntPref("network.proxy.type", type); if (NS_FAILED(rv)) {
LOG(("Failed to retrieve network.proxy.type from prefs")); return rv;
}
LOG(("network.proxy.type pref retrieved: %d\n", *type)); return NS_OK;
}
// The ExecuteCallback runnable is triggered by // nsPACManCallback::OnQueryComplete on the Main thread when its completion is // discovered on the pac thread
class ExecuteCallback final : public Runnable { public:
ExecuteCallback(nsPACManCallback* aCallback, nsresult status)
: Runnable("net::ExecuteCallback"),
mCallback(aCallback),
mStatus(status) {}
// The PAC thread must be deleted from the main thread, this class // acts as a proxy to do that, as the PACMan is reference counted // and might be destroyed on either thread
class ShutdownThread final : public Runnable { public: explicit ShutdownThread(nsIThread* thread)
: Runnable("net::ShutdownThread"), mThread(thread) {}
// Dispatch this to wait until the PAC thread shuts down.
class WaitForThreadShutdown final : public Runnable { public: explicit WaitForThreadShutdown(nsPACMan* aPACMan)
: Runnable("net::WaitForThreadShutdown"), mPACMan(aPACMan) {}
// PACLoadComplete allows the PAC thread to tell the main thread that // the javascript PAC file has been installed (perhaps unsuccessfully) // and that there is no reason to queue executions anymore
class PACLoadComplete final : public Runnable { public: explicit PACLoadComplete(nsPACMan* aPACMan)
: Runnable("net::PACLoadComplete"), mPACMan(aPACMan) {}
// ConfigureWPADComplete allows the PAC thread to tell the main thread that // the URL for the PAC file has been found class ConfigureWPADComplete final : public Runnable { public:
ConfigureWPADComplete(nsPACMan* aPACMan, const nsACString& aPACURISpec)
: Runnable("net::ConfigureWPADComplete"),
mPACMan(aPACMan),
mPACURISpec(aPACURISpec) {}
// ExecutePACThreadAction is used to proxy actions from the main // thread onto the PAC thread. There are 4 options: process the queue, // cancel the queue, query DHCP for the PAC option // and setup the javascript context with a new PAC file
class ExecutePACThreadAction final : public Runnable { public: // by default we just process the queue explicit ExecutePACThreadAction(nsPACMan* aPACMan)
: Runnable("net::ExecutePACThreadAction"),
mPACMan(aPACMan),
mCancel(false),
mCancelStatus(NS_OK),
mSetupPAC(false),
mExtraHeapSize(0),
mConfigureWPAD(false),
mShutdown(false) {}
nsPACMan::nsPACMan(nsISerialEventTarget* mainThreadEventTarget)
: NeckoTargetHolder(mainThreadEventTarget),
mLoader("nsPACMan::mLoader"),
mLoadPending(false),
mShutdown(false),
mLoadFailureCount(0),
mInProgress(false),
mAutoDetect(false),
mWPADOverDHCPEnabled(false),
mProxyConfigType(0) {
MOZ_ASSERT(NS_IsMainThread(), "pacman must be created on main thread");
mIncludePath = Preferences::GetBool(kPACIncludePath, false); if (StaticPrefs::network_proxy_parse_pac_on_socket_process() &&
gIOService->SocketProcessReady()) {
mPAC = MakeUnique<RemoteProxyAutoConfig>();
} else {
mPAC = MakeUnique<ProxyAutoConfig>(); if (!sThreadLocalSetup) {
sThreadLocalSetup = true;
PR_NewThreadPrivateIndex(&sThreadLocalIndex, nullptr);
}
mPAC->SetThreadLocalIndex(sThreadLocalIndex);
}
}
nsPACMan::~nsPACMan() {
MOZ_ASSERT(mShutdown, "Shutdown must be called before dtor.");
if (mPACThread) { if (NS_IsMainThread()) {
mPACThread->Shutdown();
mPACThread = nullptr;
} else {
RefPtr<ShutdownThread> runnable = new ShutdownThread(mPACThread);
Dispatch(runnable.forget());
}
}
#ifdef DEBUG
{ auto loader = mLoader.Lock();
NS_ASSERTION(loader.ref() == nullptr, "pac man not shutdown properly");
} #endif
NS_ASSERTION(mPendingQ.isEmpty(), "pac man not shutdown properly");
}
void nsPACMan::Shutdown() {
MOZ_ASSERT(NS_IsMainThread(), "pacman must be shutdown on main thread"); if (mShutdown) { return;
}
CancelExistingLoad();
if (mPACThread) {
PostCancelPendingQ(NS_ERROR_ABORT, /*aShutdown =*/true);
// Shutdown is initiated from an observer. We don't want to block the // observer service on thread shutdown so we post a shutdown runnable that // will run after we return instead.
RefPtr<WaitForThreadShutdown> runnable = new WaitForThreadShutdown(this);
Dispatch(runnable.forget());
}
// Lazily create the PAC thread. This method is main-thread only so we don't // have to worry about threading issues here. if (!mPACThread) {
MOZ_TRY(NS_NewNamedThread("ProxyResolution", getter_AddRefs(mPACThread)));
nsresult rv = mPAC->Init(mPACThread); if (NS_FAILED(rv)) { return rv;
}
}
if (mShutdown) {
query->Complete(NS_ERROR_NOT_AVAILABLE, ""_ns); return NS_OK;
}
// add a reference to the query while it is in the pending list
RefPtr<PendingPACQuery> addref(query);
mPendingQ.insertBack(addref.forget().take());
ProcessPendingQ(); return NS_OK;
}
// check if proxy settings are configured for WPAD bool IsProxyConfigValidForWPAD(int proxyConfigType, bool wpadSystemSettings) { return proxyConfigType == nsIProtocolProxyService::PROXYCONFIG_WPAD ||
(proxyConfigType == nsIProtocolProxyService::PROXYCONFIG_SYSTEM &&
wpadSystemSettings);
}
{ auto locked = mLoader.Lock();
locked.ref() = loader.forget();
}
mPACURIRedirectSpec.Truncate();
mNormalPACURISpec.Truncate(); // set at load time if (aResetLoadFailureCount) {
mLoadFailureCount = 0;
}
mAutoDetect = aSpec.IsEmpty();
mPACURISpec.Assign(aSpec);
// reset to Null
mScheduledReload = TimeStamp();
// if we're on the main thread here so we can get hold of prefs, // we check that we have WPAD preffed on if we're auto-detecting if (mAutoDetect && NS_IsMainThread()) {
nsresult rv = GetNetworkProxyTypeFromPref(&mProxyConfigType); if (NS_FAILED(rv)) { return rv;
} if (!IsProxyConfigValidForWPAD(mProxyConfigType, mAutoDetect)) {
LOG(
("LoadPACFromURI - Aborting WPAD autodetection because the pref " "doesn't match anymore")); return NS_BINDING_ABORTED;
}
} // Since we might get called from nsProtocolProxyService::Init, we need to // post an event back to the main thread before we try to use the IO service. // // But, we need to flag ourselves as loading, so that we queue up any PAC // queries the enter between now and when we actually load the PAC file.
if (!IsProxyConfigValidForWPAD(mProxyConfigType, mAutoDetect)) {
LOG(
("ConfigureWPAD - Aborting WPAD autodetection because the pref " "doesn't match anymore")); return NS_BINDING_ABORTED;
}
aSpec.Truncate(); if (mWPADOverDHCPEnabled) {
GetPACFromDHCP(aSpec);
}
if (aSpec.IsEmpty()) { // We diverge from the WPAD spec here in that we don't walk the // hosts's FQDN, stripping components until we hit a TLD. Doing so // is dangerous in the face of an incomplete list of TLDs, and TLDs // get added over time. We could consider doing only a single // substitution of the first component, if that proves to help // compatibility.
aSpec.AssignLiteral(MOZ_WPAD_URL);
} return NS_OK;
}
// CancelExistingLoad was called... if (!loader) {
PostCancelPendingQ(NS_ERROR_ABORT); return;
} if (NS_SUCCEEDED(loader->Init(this, nullptr))) { // Always hit the origin server when loading PAC.
nsCOMPtr<nsIIOService> ios = do_GetIOService(); if (ios) {
nsCOMPtr<nsIChannel> channel;
nsCOMPtr<nsIURI> pacURI;
NS_NewURI(getter_AddRefs(pacURI), mPACURISpec);
// NOTE: This results in GetProxyForURI being called if (pacURI) {
nsresult rv = pacURI->GetSpec(mNormalPACURISpec);
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
NS_NewChannel(getter_AddRefs(channel), pacURI,
nsContentUtils::GetSystemPrincipal(),
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
nsIContentPolicy::TYPE_OTHER,
nullptr, // nsICookieJarSettings
nullptr, // PerformanceStorage
nullptr, // aLoadGroup
nullptr, // aCallbacks
nsIRequest::LOAD_NORMAL, ios);
} else {
LOG(("nsPACMan::StartLoading Failed pacspec uri conversion %s\n",
mPACURISpec.get()));
}
if (channel) { // allow deprecated HTTP request from SystemPrincipal
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
loadInfo->SetAllowDeprecatedSystemRequests(true);
loadInfo->SetHttpsOnlyStatus(nsILoadInfo::HTTPS_ONLY_EXEMPT);
channel->SetLoadFlags(nsIRequest::LOAD_BYPASS_CACHE);
channel->SetNotificationCallbacks(this);
channel->SetTRRMode(nsIRequest::TRR_DISABLED_MODE); if (NS_SUCCEEDED(channel->AsyncOpen(loader))) return;
}
}
}
if (mShutdown) {
mPAC->Shutdown();
} else { // do GC while the thread has nothing pending
mPAC->GC();
}
}
// returns true if progress was made by shortening the queue bool nsPACMan::ProcessPending() { if (mPendingQ.isEmpty()) returnfalse;
// queue during normal load, but if we are retrying a failed load then // fast fail the queries if (mInProgress || (IsLoading() && !mLoadFailureCount)) returnfalse;
// Having |mLoadFailureCount > 0| means we haven't had a sucessful PAC load // yet. We should use DIRECT instead. if (mShutdown || IsLoading() || mLoadFailureCount > 0) {
query->Complete(NS_ERROR_NOT_AVAILABLE, ""_ns); returntrue;
}
// first we need to consider the system proxy changing the pac url if (mSystemProxySettings &&
NS_SUCCEEDED(mSystemProxySettings->GetPACURI(PACURI)) &&
!PACURI.IsEmpty() && !PACURI.Equals(mPACURISpec)) {
query->UseAlternatePACFile(PACURI);
LOG(("Use PAC from system settings: %s\n", PACURI.get()));
completed = true;
}
// now try the system proxy settings for this particular url if // PAC was not specified if (!completed && mSystemProxySettings && PACURI.IsEmpty() &&
NS_SUCCEEDED(mSystemProxySettings->GetProxyForURI(
query->mSpec, query->mScheme, query->mHost, query->mPort,
pacString))) { if (query->mFlags & nsIProtocolProxyService::RESOLVE_PREFER_SOCKS_PROXY &&
query->mFlags & nsIProtocolProxyService::RESOLVE_PREFER_HTTPS_PROXY) { if (StringBeginsWith(pacString, nsDependentCString(kProxyType_DIRECT),
nsCaseInsensitiveUTF8StringComparator)) { // DIRECT indicates that system proxy settings are not configured to use // SOCKS proxy. Try https proxy as a secondary preferrable proxy. This // is mainly for websocket whose precedence is SOCKS > HTTPS > DIRECT.
NS_SUCCEEDED(mSystemProxySettings->GetProxyForURI(
query->mSpec, nsDependentCString(kProxyType_HTTPS), query->mHost,
query->mPort, pacString));
}
}
LOG(("Use proxy from system settings: %s\n", pacString.get()));
query->Complete(NS_OK, pacString);
completed = true;
}
// the systemproxysettings didn't complete the resolution. try via PAC if (!completed) { auto callback = [query(query)](nsresult aStatus, const nsACString& aResult) {
LOG(("Use proxy from PAC: %s\n", PromiseFlatCString(aResult).get()));
query->Complete(aStatus, aResult);
};
mPAC->GetProxyForURIWithCallback(query->mSpec, query->mHost,
std::move(callback));
}
bool loadSucceeded = NS_SUCCEEDED(status) && HttpRequestSucceeded(loader);
{ auto locked = mLoader.Lock(); if (locked.ref() != loader) { // If this happens, then it means that LoadPACFromURI was called more // than once before the initial call completed. In this case, status // should be NS_ERROR_ABORT, and if so, then we know that we can and // should delay any processing.
LOG(("OnStreamComplete: called more than once\n")); if (status == NS_ERROR_ABORT) { return NS_OK;
}
} elseif (!loadSucceeded) { // We have to clear the loader to indicate that we are not loading PAC // currently. // Note that we can only clear the loader when |loader| and |mLoader| are // the same one.
locked.ref() = nullptr;
}
}
LOG(("OnStreamComplete: entry\n"));
if (loadSucceeded) { // Get the URI spec used to load this PAC script.
nsAutoCString pacURI;
{
nsCOMPtr<nsIRequest> request;
loader->GetRequest(getter_AddRefs(request));
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request); if (channel) {
nsCOMPtr<nsIURI> uri;
channel->GetURI(getter_AddRefs(uri)); if (uri) uri->GetAsciiSpec(pacURI);
}
}
nsCOMPtr<nsIProtocolProxyService> pps =
do_GetService(NS_PROTOCOLPROXYSERVICE_CONTRACTID);
MOZ_ASSERT(pps); if (pps) {
pps->NotifyProxyConfigChangedInternal();
}
// We succeeded in loading the pac file using a bunch of interfaces that are // main thread only. Unfortunately, we have to initialize the instance of // the PAC evaluator (NS_PROXYAUTOCONFIG_CONTRACTID) on the PAC thread, // because that's where it will be used.
RefPtr<ExecutePACThreadAction> pending = new ExecutePACThreadAction(this);
pending->SetupPAC(reinterpret_cast<constchar*>(data), dataLen, pacURI,
GetExtraJSContextHeapSize());
DispatchToPAC(pending.forget());
LOG(("OnStreamComplete: process the PAC contents\n"));
// Even if the PAC file could not be parsed, we did succeed in loading the // data for it.
mLoadFailureCount = 0;
} else { // We were unable to load the PAC file (presumably because of a network // failure). Try again a little later.
LOG(("OnStreamComplete: unable to load PAC, retry later\n"));
OnLoadFailure();
}
if (NS_SUCCEEDED(status)) {
PostProcessPendingQ();
} else {
PostCancelPendingQ(status);
}
return NS_OK;
}
NS_IMETHODIMP
nsPACMan::GetInterface(const nsIID& iid, void** result) { // In case loading the PAC file requires authentication. if (iid.Equals(NS_GET_IID(nsIAuthPrompt))) {
nsCOMPtr<nsIPromptFactory> promptFac =
do_GetService("@mozilla.org/prompter;1");
NS_ENSURE_TRUE(promptFac, NS_ERROR_NO_INTERFACE);
nsresult rv =
promptFac->GetPrompt(nullptr, iid, reinterpret_cast<void**>(result)); if (NS_FAILED(rv)) { return NS_ERROR_NO_INTERFACE;
} return NS_OK;
}
// In case loading the PAC file results in a redirect. if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
NS_ADDREF_THIS();
*result = static_cast<nsIChannelEventSink*>(this); return NS_OK;
}
rv = pacURI->GetSpec(mPACURIRedirectSpec); if (NS_FAILED(rv)) return rv;
LOG(("nsPACMan redirect from original %s to redirected %s\n",
mPACURISpec.get(), mPACURIRedirectSpec.get()));
// do not update mPACURISpec - that needs to stay as the // configured URI so that we can determine when the config changes. // However do track the most recent URI in the redirect change // as mPACURIRedirectSpec so that URI can be allowed to bypass // the proxy and actually fetch the pac file.
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.