1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/system/unixproxy/nsLibProxySettings.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsISystemProxySettings.h" 1.10 +#include "mozilla/ModuleUtils.h" 1.11 +#include "nsIServiceManager.h" 1.12 +#include "nsIURI.h" 1.13 +#include "nsString.h" 1.14 +#include "nsNetUtil.h" 1.15 +#include "nsCOMPtr.h" 1.16 +#include "nspr.h" 1.17 + 1.18 +extern "C" { 1.19 +#include <proxy.h> 1.20 +} 1.21 + 1.22 +class nsUnixSystemProxySettings : public nsISystemProxySettings { 1.23 +public: 1.24 + NS_DECL_THREADSAFE_ISUPPORTS 1.25 + NS_DECL_NSISYSTEMPROXYSETTINGS 1.26 + 1.27 + nsUnixSystemProxySettings() { mProxyFactory = nullptr; } 1.28 + nsresult Init(); 1.29 + 1.30 +private: 1.31 + ~nsUnixSystemProxySettings() { 1.32 + if (mProxyFactory) 1.33 + px_proxy_factory_free(mProxyFactory); 1.34 + } 1.35 + 1.36 + pxProxyFactory *mProxyFactory; 1.37 +}; 1.38 + 1.39 +NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings) 1.40 + 1.41 +NS_IMETHODIMP 1.42 +nsUnixSystemProxySettings::GetMainThreadOnly(bool *aMainThreadOnly) 1.43 +{ 1.44 + *aMainThreadOnly = false; 1.45 + return NS_OK; 1.46 +} 1.47 + 1.48 +nsresult 1.49 +nsUnixSystemProxySettings::Init() 1.50 +{ 1.51 + return NS_OK; 1.52 +} 1.53 + 1.54 +nsresult 1.55 +nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) 1.56 +{ 1.57 + // Make sure we return an empty result. 1.58 + aResult.Truncate(); 1.59 + return NS_OK; 1.60 +} 1.61 + 1.62 +nsresult 1.63 +nsUnixSystemProxySettings::GetProxyForURI(const nsACString & aSpec, 1.64 + const nsACString & aScheme, 1.65 + const nsACString & aHost, 1.66 + const int32_t aPort, 1.67 + nsACString & aResult) 1.68 +{ 1.69 + nsresult rv; 1.70 + 1.71 + if (!mProxyFactory) { 1.72 + mProxyFactory = px_proxy_factory_new(); 1.73 + } 1.74 + NS_ENSURE_TRUE(mProxyFactory, NS_ERROR_NOT_AVAILABLE); 1.75 + 1.76 + char **proxyArray = nullptr; 1.77 + proxyArray = px_proxy_factory_get_proxies(mProxyFactory, 1.78 + PromiseFlatCString(aSpec).get()); 1.79 + NS_ENSURE_TRUE(proxyArray, NS_ERROR_NOT_AVAILABLE); 1.80 + 1.81 + // Translate libproxy's output to PAC string as expected 1.82 + // libproxy returns an array of proxies in the format: 1.83 + // <procotol>://[username:password@]proxy:port 1.84 + // or 1.85 + // direct:// 1.86 + // 1.87 + // PAC format: "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT" 1.88 + // but nsISystemProxySettings allows "PROXY http://proxy.foo.com:8080" as well. 1.89 + 1.90 + int c = 0; 1.91 + while (proxyArray[c] != nullptr) { 1.92 + if (!aResult.IsEmpty()) { 1.93 + aResult.AppendLiteral("; "); 1.94 + } 1.95 + 1.96 + // figure out the scheme, and we can't use nsIIOService::NewURI because 1.97 + // this is not the main thread. 1.98 + char *colon = strchr (proxyArray[c], ':'); 1.99 + uint32_t schemelen = colon ? colon - proxyArray[c] : 0; 1.100 + if (schemelen < 1) { 1.101 + c++; 1.102 + continue; 1.103 + } 1.104 + 1.105 + if (schemelen == 6 && !strncasecmp(proxyArray[c], "direct", 6)) { 1.106 + aResult.AppendLiteral("DIRECT"); 1.107 + } 1.108 + else { 1.109 + aResult.AppendLiteral("PROXY "); 1.110 + aResult.Append(proxyArray[c]); 1.111 + } 1.112 + 1.113 + c++; 1.114 + } 1.115 + 1.116 + PR_Free(proxyArray); 1.117 + return NS_OK; 1.118 +} 1.119 + 1.120 +#define NS_UNIXSYSTEMPROXYSERVICE_CID /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\ 1.121 + { 0x0fa3158c, 0xd5a7, 0x43de, \ 1.122 + {0x91, 0x81, 0xa2, 0x85, 0xe7, 0x4c, 0xf1, 0xd4 } } 1.123 + 1.124 +NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUnixSystemProxySettings, Init) 1.125 +NS_DEFINE_NAMED_CID(NS_UNIXSYSTEMPROXYSERVICE_CID); 1.126 + 1.127 +static const mozilla::Module::CIDEntry kUnixProxyCIDs[] = { 1.128 + { &kNS_UNIXSYSTEMPROXYSERVICE_CID, false, nullptr, nsUnixSystemProxySettingsConstructor }, 1.129 + { nullptr } 1.130 +}; 1.131 + 1.132 +static const mozilla::Module::ContractIDEntry kUnixProxyContracts[] = { 1.133 + { NS_SYSTEMPROXYSETTINGS_CONTRACTID, &kNS_UNIXSYSTEMPROXYSERVICE_CID }, 1.134 + { nullptr } 1.135 +}; 1.136 + 1.137 +static const mozilla::Module kUnixProxyModule = { 1.138 + mozilla::Module::kVersion, 1.139 + kUnixProxyCIDs, 1.140 + kUnixProxyContracts 1.141 +}; 1.142 + 1.143 +NSMODULE_DEFN(nsUnixProxyModule) = &kUnixProxyModule; 1.144 +