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