michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsISystemProxySettings.h" michael@0: #include "mozilla/ModuleUtils.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIURI.h" michael@0: #include "nsString.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nspr.h" michael@0: michael@0: extern "C" { michael@0: #include michael@0: } michael@0: michael@0: class nsUnixSystemProxySettings : public nsISystemProxySettings { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSISYSTEMPROXYSETTINGS michael@0: michael@0: nsUnixSystemProxySettings() { mProxyFactory = nullptr; } michael@0: nsresult Init(); michael@0: michael@0: private: michael@0: ~nsUnixSystemProxySettings() { michael@0: if (mProxyFactory) michael@0: px_proxy_factory_free(mProxyFactory); michael@0: } michael@0: michael@0: pxProxyFactory *mProxyFactory; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings) michael@0: michael@0: NS_IMETHODIMP michael@0: nsUnixSystemProxySettings::GetMainThreadOnly(bool *aMainThreadOnly) michael@0: { michael@0: *aMainThreadOnly = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsUnixSystemProxySettings::Init() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) michael@0: { michael@0: // Make sure we return an empty result. michael@0: aResult.Truncate(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsUnixSystemProxySettings::GetProxyForURI(const nsACString & aSpec, michael@0: const nsACString & aScheme, michael@0: const nsACString & aHost, michael@0: const int32_t aPort, michael@0: nsACString & aResult) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (!mProxyFactory) { michael@0: mProxyFactory = px_proxy_factory_new(); michael@0: } michael@0: NS_ENSURE_TRUE(mProxyFactory, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: char **proxyArray = nullptr; michael@0: proxyArray = px_proxy_factory_get_proxies(mProxyFactory, michael@0: PromiseFlatCString(aSpec).get()); michael@0: NS_ENSURE_TRUE(proxyArray, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: // Translate libproxy's output to PAC string as expected michael@0: // libproxy returns an array of proxies in the format: michael@0: // ://[username:password@]proxy:port michael@0: // or michael@0: // direct:// michael@0: // michael@0: // PAC format: "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT" michael@0: // but nsISystemProxySettings allows "PROXY http://proxy.foo.com:8080" as well. michael@0: michael@0: int c = 0; michael@0: while (proxyArray[c] != nullptr) { michael@0: if (!aResult.IsEmpty()) { michael@0: aResult.AppendLiteral("; "); michael@0: } michael@0: michael@0: // figure out the scheme, and we can't use nsIIOService::NewURI because michael@0: // this is not the main thread. michael@0: char *colon = strchr (proxyArray[c], ':'); michael@0: uint32_t schemelen = colon ? colon - proxyArray[c] : 0; michael@0: if (schemelen < 1) { michael@0: c++; michael@0: continue; michael@0: } michael@0: michael@0: if (schemelen == 6 && !strncasecmp(proxyArray[c], "direct", 6)) { michael@0: aResult.AppendLiteral("DIRECT"); michael@0: } michael@0: else { michael@0: aResult.AppendLiteral("PROXY "); michael@0: aResult.Append(proxyArray[c]); michael@0: } michael@0: michael@0: c++; michael@0: } michael@0: michael@0: PR_Free(proxyArray); michael@0: return NS_OK; michael@0: } michael@0: michael@0: #define NS_UNIXSYSTEMPROXYSERVICE_CID /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\ michael@0: { 0x0fa3158c, 0xd5a7, 0x43de, \ michael@0: {0x91, 0x81, 0xa2, 0x85, 0xe7, 0x4c, 0xf1, 0xd4 } } michael@0: michael@0: NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUnixSystemProxySettings, Init) michael@0: NS_DEFINE_NAMED_CID(NS_UNIXSYSTEMPROXYSERVICE_CID); michael@0: michael@0: static const mozilla::Module::CIDEntry kUnixProxyCIDs[] = { michael@0: { &kNS_UNIXSYSTEMPROXYSERVICE_CID, false, nullptr, nsUnixSystemProxySettingsConstructor }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module::ContractIDEntry kUnixProxyContracts[] = { michael@0: { NS_SYSTEMPROXYSETTINGS_CONTRACTID, &kNS_UNIXSYSTEMPROXYSERVICE_CID }, michael@0: { nullptr } michael@0: }; michael@0: michael@0: static const mozilla::Module kUnixProxyModule = { michael@0: mozilla::Module::kVersion, michael@0: kUnixProxyCIDs, michael@0: kUnixProxyContracts michael@0: }; michael@0: michael@0: NSMODULE_DEFN(nsUnixProxyModule) = &kUnixProxyModule; michael@0: