Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsISystemProxySettings.h" |
michael@0 | 7 | #include "mozilla/ModuleUtils.h" |
michael@0 | 8 | #include "nsIServiceManager.h" |
michael@0 | 9 | #include "nsIURI.h" |
michael@0 | 10 | #include "nsString.h" |
michael@0 | 11 | #include "nsNetUtil.h" |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nspr.h" |
michael@0 | 14 | |
michael@0 | 15 | extern "C" { |
michael@0 | 16 | #include <proxy.h> |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | class nsUnixSystemProxySettings : public nsISystemProxySettings { |
michael@0 | 20 | public: |
michael@0 | 21 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 22 | NS_DECL_NSISYSTEMPROXYSETTINGS |
michael@0 | 23 | |
michael@0 | 24 | nsUnixSystemProxySettings() { mProxyFactory = nullptr; } |
michael@0 | 25 | nsresult Init(); |
michael@0 | 26 | |
michael@0 | 27 | private: |
michael@0 | 28 | ~nsUnixSystemProxySettings() { |
michael@0 | 29 | if (mProxyFactory) |
michael@0 | 30 | px_proxy_factory_free(mProxyFactory); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | pxProxyFactory *mProxyFactory; |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | NS_IMPL_ISUPPORTS(nsUnixSystemProxySettings, nsISystemProxySettings) |
michael@0 | 37 | |
michael@0 | 38 | NS_IMETHODIMP |
michael@0 | 39 | nsUnixSystemProxySettings::GetMainThreadOnly(bool *aMainThreadOnly) |
michael@0 | 40 | { |
michael@0 | 41 | *aMainThreadOnly = false; |
michael@0 | 42 | return NS_OK; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | nsresult |
michael@0 | 46 | nsUnixSystemProxySettings::Init() |
michael@0 | 47 | { |
michael@0 | 48 | return NS_OK; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | nsresult |
michael@0 | 52 | nsUnixSystemProxySettings::GetPACURI(nsACString& aResult) |
michael@0 | 53 | { |
michael@0 | 54 | // Make sure we return an empty result. |
michael@0 | 55 | aResult.Truncate(); |
michael@0 | 56 | return NS_OK; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | nsresult |
michael@0 | 60 | nsUnixSystemProxySettings::GetProxyForURI(const nsACString & aSpec, |
michael@0 | 61 | const nsACString & aScheme, |
michael@0 | 62 | const nsACString & aHost, |
michael@0 | 63 | const int32_t aPort, |
michael@0 | 64 | nsACString & aResult) |
michael@0 | 65 | { |
michael@0 | 66 | nsresult rv; |
michael@0 | 67 | |
michael@0 | 68 | if (!mProxyFactory) { |
michael@0 | 69 | mProxyFactory = px_proxy_factory_new(); |
michael@0 | 70 | } |
michael@0 | 71 | NS_ENSURE_TRUE(mProxyFactory, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 72 | |
michael@0 | 73 | char **proxyArray = nullptr; |
michael@0 | 74 | proxyArray = px_proxy_factory_get_proxies(mProxyFactory, |
michael@0 | 75 | PromiseFlatCString(aSpec).get()); |
michael@0 | 76 | NS_ENSURE_TRUE(proxyArray, NS_ERROR_NOT_AVAILABLE); |
michael@0 | 77 | |
michael@0 | 78 | // Translate libproxy's output to PAC string as expected |
michael@0 | 79 | // libproxy returns an array of proxies in the format: |
michael@0 | 80 | // <procotol>://[username:password@]proxy:port |
michael@0 | 81 | // or |
michael@0 | 82 | // direct:// |
michael@0 | 83 | // |
michael@0 | 84 | // PAC format: "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT" |
michael@0 | 85 | // but nsISystemProxySettings allows "PROXY http://proxy.foo.com:8080" as well. |
michael@0 | 86 | |
michael@0 | 87 | int c = 0; |
michael@0 | 88 | while (proxyArray[c] != nullptr) { |
michael@0 | 89 | if (!aResult.IsEmpty()) { |
michael@0 | 90 | aResult.AppendLiteral("; "); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | // figure out the scheme, and we can't use nsIIOService::NewURI because |
michael@0 | 94 | // this is not the main thread. |
michael@0 | 95 | char *colon = strchr (proxyArray[c], ':'); |
michael@0 | 96 | uint32_t schemelen = colon ? colon - proxyArray[c] : 0; |
michael@0 | 97 | if (schemelen < 1) { |
michael@0 | 98 | c++; |
michael@0 | 99 | continue; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | if (schemelen == 6 && !strncasecmp(proxyArray[c], "direct", 6)) { |
michael@0 | 103 | aResult.AppendLiteral("DIRECT"); |
michael@0 | 104 | } |
michael@0 | 105 | else { |
michael@0 | 106 | aResult.AppendLiteral("PROXY "); |
michael@0 | 107 | aResult.Append(proxyArray[c]); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | c++; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | PR_Free(proxyArray); |
michael@0 | 114 | return NS_OK; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | #define NS_UNIXSYSTEMPROXYSERVICE_CID /* 0fa3158c-d5a7-43de-9181-a285e74cf1d4 */\ |
michael@0 | 118 | { 0x0fa3158c, 0xd5a7, 0x43de, \ |
michael@0 | 119 | {0x91, 0x81, 0xa2, 0x85, 0xe7, 0x4c, 0xf1, 0xd4 } } |
michael@0 | 120 | |
michael@0 | 121 | NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsUnixSystemProxySettings, Init) |
michael@0 | 122 | NS_DEFINE_NAMED_CID(NS_UNIXSYSTEMPROXYSERVICE_CID); |
michael@0 | 123 | |
michael@0 | 124 | static const mozilla::Module::CIDEntry kUnixProxyCIDs[] = { |
michael@0 | 125 | { &kNS_UNIXSYSTEMPROXYSERVICE_CID, false, nullptr, nsUnixSystemProxySettingsConstructor }, |
michael@0 | 126 | { nullptr } |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | static const mozilla::Module::ContractIDEntry kUnixProxyContracts[] = { |
michael@0 | 130 | { NS_SYSTEMPROXYSETTINGS_CONTRACTID, &kNS_UNIXSYSTEMPROXYSERVICE_CID }, |
michael@0 | 131 | { nullptr } |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | static const mozilla::Module kUnixProxyModule = { |
michael@0 | 135 | mozilla::Module::kVersion, |
michael@0 | 136 | kUnixProxyCIDs, |
michael@0 | 137 | kUnixProxyContracts |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | NSMODULE_DEFN(nsUnixProxyModule) = &kUnixProxyModule; |
michael@0 | 141 |