|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
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 #ifndef nsProtocolProxyService_h__ |
|
7 #define nsProtocolProxyService_h__ |
|
8 |
|
9 #include "nsString.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsAutoPtr.h" |
|
12 #include "nsTArray.h" |
|
13 #include "nsIProtocolProxyService2.h" |
|
14 #include "nsIProtocolProxyFilter.h" |
|
15 #include "nsIProxyInfo.h" |
|
16 #include "nsIObserver.h" |
|
17 #include "nsDataHashtable.h" |
|
18 #include "nsHashKeys.h" |
|
19 #include "prio.h" |
|
20 #include "mozilla/Attributes.h" |
|
21 |
|
22 typedef nsDataHashtable<nsCStringHashKey, uint32_t> nsFailedProxyTable; |
|
23 |
|
24 class nsProxyInfo; |
|
25 struct nsProtocolInfo; |
|
26 class nsIPrefBranch; |
|
27 class nsISystemProxySettings; |
|
28 class nsPACMan; |
|
29 |
|
30 // CID for the nsProtocolProxyService class |
|
31 // 091eedd8-8bae-4fe3-ad62-0c87351e640d |
|
32 #define NS_PROTOCOL_PROXY_SERVICE_IMPL_CID \ |
|
33 { 0x091eedd8, 0x8bae, 0x4fe3, \ |
|
34 { 0xad, 0x62, 0x0c, 0x87, 0x35, 0x1e, 0x64, 0x0d } } |
|
35 |
|
36 class nsProtocolProxyService MOZ_FINAL : public nsIProtocolProxyService2 |
|
37 , public nsIObserver |
|
38 { |
|
39 public: |
|
40 NS_DECL_ISUPPORTS |
|
41 NS_DECL_NSIPROTOCOLPROXYSERVICE2 |
|
42 NS_DECL_NSIPROTOCOLPROXYSERVICE |
|
43 NS_DECL_NSIOBSERVER |
|
44 |
|
45 NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROTOCOL_PROXY_SERVICE_IMPL_CID) |
|
46 |
|
47 nsProtocolProxyService() NS_HIDDEN; |
|
48 |
|
49 NS_HIDDEN_(nsresult) Init(); |
|
50 nsresult DeprecatedBlockingResolve(nsIChannel *aChannel, |
|
51 uint32_t aFlags, |
|
52 nsIProxyInfo **retval); |
|
53 |
|
54 protected: |
|
55 friend class nsAsyncResolveRequest; |
|
56 |
|
57 ~nsProtocolProxyService() NS_HIDDEN; |
|
58 |
|
59 /** |
|
60 * This method is called whenever a preference may have changed or |
|
61 * to initialize all preferences. |
|
62 * |
|
63 * @param prefs |
|
64 * This must be a pointer to the root pref branch. |
|
65 * @param name |
|
66 * This can be the name of a fully-qualified preference, or it can |
|
67 * be null, in which case all preferences will be initialized. |
|
68 */ |
|
69 NS_HIDDEN_(void) PrefsChanged(nsIPrefBranch *prefs, const char *name); |
|
70 |
|
71 /** |
|
72 * This method is called to create a nsProxyInfo instance from the given |
|
73 * PAC-style proxy string. It parses up to the end of the string, or to |
|
74 * the next ';' character. |
|
75 * |
|
76 * @param proxy |
|
77 * The PAC-style proxy string to parse. This must not be null. |
|
78 * @param aResolveFlags |
|
79 * The flags passed to Resolve or AsyncResolve that are stored in |
|
80 * proxyInfo. |
|
81 * @param result |
|
82 * Upon return this points to a newly allocated nsProxyInfo or null |
|
83 * if the proxy string was invalid. |
|
84 * |
|
85 * @return A pointer beyond the parsed proxy string (never null). |
|
86 */ |
|
87 NS_HIDDEN_(const char *) ExtractProxyInfo(const char *proxy, |
|
88 uint32_t aResolveFlags, |
|
89 nsProxyInfo **result); |
|
90 |
|
91 /** |
|
92 * Load the specified PAC file. |
|
93 * |
|
94 * @param pacURI |
|
95 * The URI spec of the PAC file to load. |
|
96 */ |
|
97 NS_HIDDEN_(nsresult) ConfigureFromPAC(const nsCString &pacURI, bool forceReload); |
|
98 |
|
99 /** |
|
100 * This method builds a list of nsProxyInfo objects from the given PAC- |
|
101 * style string. |
|
102 * |
|
103 * @param pacString |
|
104 * The PAC-style proxy string to parse. This may be empty. |
|
105 * @param aResolveFlags |
|
106 * The flags passed to Resolve or AsyncResolve that are stored in |
|
107 * proxyInfo. |
|
108 * @param result |
|
109 * The resulting list of proxy info objects. |
|
110 */ |
|
111 NS_HIDDEN_(void) ProcessPACString(const nsCString &pacString, |
|
112 uint32_t aResolveFlags, |
|
113 nsIProxyInfo **result); |
|
114 |
|
115 /** |
|
116 * This method generates a string valued identifier for the given |
|
117 * nsProxyInfo object. |
|
118 * |
|
119 * @param pi |
|
120 * The nsProxyInfo object from which to generate the key. |
|
121 * @param result |
|
122 * Upon return, this parameter holds the generated key. |
|
123 */ |
|
124 NS_HIDDEN_(void) GetProxyKey(nsProxyInfo *pi, nsCString &result); |
|
125 |
|
126 /** |
|
127 * @return Seconds since start of session. |
|
128 */ |
|
129 NS_HIDDEN_(uint32_t) SecondsSinceSessionStart(); |
|
130 |
|
131 /** |
|
132 * This method removes the specified proxy from the disabled list. |
|
133 * |
|
134 * @param pi |
|
135 * The nsProxyInfo object identifying the proxy to enable. |
|
136 */ |
|
137 NS_HIDDEN_(void) EnableProxy(nsProxyInfo *pi); |
|
138 |
|
139 /** |
|
140 * This method adds the specified proxy to the disabled list. |
|
141 * |
|
142 * @param pi |
|
143 * The nsProxyInfo object identifying the proxy to disable. |
|
144 */ |
|
145 NS_HIDDEN_(void) DisableProxy(nsProxyInfo *pi); |
|
146 |
|
147 /** |
|
148 * This method tests to see if the given proxy is disabled. |
|
149 * |
|
150 * @param pi |
|
151 * The nsProxyInfo object identifying the proxy to test. |
|
152 * |
|
153 * @return True if the specified proxy is disabled. |
|
154 */ |
|
155 NS_HIDDEN_(bool) IsProxyDisabled(nsProxyInfo *pi); |
|
156 |
|
157 /** |
|
158 * This method queries the protocol handler for the given scheme to check |
|
159 * for the protocol flags and default port. |
|
160 * |
|
161 * @param uri |
|
162 * The URI to query. |
|
163 * @param info |
|
164 * Holds information about the protocol upon return. Pass address |
|
165 * of structure when you call this method. This parameter must not |
|
166 * be null. |
|
167 */ |
|
168 NS_HIDDEN_(nsresult) GetProtocolInfo(nsIURI *uri, nsProtocolInfo *result); |
|
169 |
|
170 /** |
|
171 * This method is an internal version nsIProtocolProxyService::newProxyInfo |
|
172 * that expects a string literal for the type. |
|
173 * |
|
174 * @param type |
|
175 * The proxy type. |
|
176 * @param host |
|
177 * The proxy host name (UTF-8 ok). |
|
178 * @param port |
|
179 * The proxy port number. |
|
180 * @param username |
|
181 * The username for the proxy (ASCII). May be "", but not null. |
|
182 * @param password |
|
183 * The password for the proxy (ASCII). May be "", but not null. |
|
184 * @param flags |
|
185 * The proxy flags (nsIProxyInfo::flags). |
|
186 * @param timeout |
|
187 * The failover timeout for this proxy. |
|
188 * @param next |
|
189 * The next proxy to try if this one fails. |
|
190 * @param aResolveFlags |
|
191 * The flags passed to resolve (from nsIProtocolProxyService). |
|
192 * @param result |
|
193 * The resulting nsIProxyInfo object. |
|
194 */ |
|
195 NS_HIDDEN_(nsresult) NewProxyInfo_Internal(const char *type, |
|
196 const nsACString &host, |
|
197 int32_t port, |
|
198 const nsACString &username, |
|
199 const nsACString &password, |
|
200 uint32_t flags, |
|
201 uint32_t timeout, |
|
202 nsIProxyInfo *next, |
|
203 uint32_t aResolveFlags, |
|
204 nsIProxyInfo **result); |
|
205 |
|
206 /** |
|
207 * This method is an internal version of Resolve that does not query PAC. |
|
208 * It performs all of the built-in processing, and reports back to the |
|
209 * caller with either the proxy info result or a flag to instruct the |
|
210 * caller to use PAC instead. |
|
211 * |
|
212 * @param channel |
|
213 * The channel to test. |
|
214 * @param info |
|
215 * Information about the URI's protocol. |
|
216 * @param flags |
|
217 * The flags passed to either the resolve or the asyncResolve method. |
|
218 * @param usePAC |
|
219 * If this flag is set upon return, then PAC should be queried to |
|
220 * resolve the proxy info. |
|
221 * @param result |
|
222 * The resulting proxy info or null. |
|
223 */ |
|
224 NS_HIDDEN_(nsresult) Resolve_Internal(nsIChannel *channel, |
|
225 const nsProtocolInfo &info, |
|
226 uint32_t flags, |
|
227 bool *usePAC, |
|
228 nsIProxyInfo **result); |
|
229 |
|
230 /** |
|
231 * This method applies the registered filters to the given proxy info |
|
232 * list, and returns a possibly modified list. |
|
233 * |
|
234 * @param channel |
|
235 * The channel corresponding to this proxy info list. |
|
236 * @param info |
|
237 * Information about the URI's protocol. |
|
238 * @param proxyInfo |
|
239 * The proxy info list to be modified. This is an inout param. |
|
240 */ |
|
241 NS_HIDDEN_(void) ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info, |
|
242 nsIProxyInfo **proxyInfo); |
|
243 |
|
244 /** |
|
245 * This method is a simple wrapper around ApplyFilters that takes the |
|
246 * proxy info list inout param as a nsCOMPtr. |
|
247 */ |
|
248 inline void ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info, |
|
249 nsCOMPtr<nsIProxyInfo> &proxyInfo) |
|
250 { |
|
251 nsIProxyInfo *pi = nullptr; |
|
252 proxyInfo.swap(pi); |
|
253 ApplyFilters(channel, info, &pi); |
|
254 proxyInfo.swap(pi); |
|
255 } |
|
256 |
|
257 /** |
|
258 * This method prunes out disabled and disallowed proxies from a given |
|
259 * proxy info list. |
|
260 * |
|
261 * @param info |
|
262 * Information about the URI's protocol. |
|
263 * @param proxyInfo |
|
264 * The proxy info list to be modified. This is an inout param. |
|
265 */ |
|
266 NS_HIDDEN_(void) PruneProxyInfo(const nsProtocolInfo &info, |
|
267 nsIProxyInfo **proxyInfo); |
|
268 |
|
269 /** |
|
270 * This method populates mHostFiltersArray from the given string. |
|
271 * |
|
272 * @param hostFilters |
|
273 * A "no-proxy-for" exclusion list. |
|
274 */ |
|
275 NS_HIDDEN_(void) LoadHostFilters(const char *hostFilters); |
|
276 |
|
277 /** |
|
278 * This method checks the given URI against mHostFiltersArray. |
|
279 * |
|
280 * @param uri |
|
281 * The URI to test. |
|
282 * @param defaultPort |
|
283 * The default port for the given URI. |
|
284 * |
|
285 * @return True if the URI can use the specified proxy. |
|
286 */ |
|
287 NS_HIDDEN_(bool) CanUseProxy(nsIURI *uri, int32_t defaultPort); |
|
288 |
|
289 /** |
|
290 * Disable Prefetch in the DNS service if a proxy is in use. |
|
291 * |
|
292 * @param aProxy |
|
293 * The proxy information |
|
294 */ |
|
295 NS_HIDDEN_(void) MaybeDisableDNSPrefetch(nsIProxyInfo *aProxy); |
|
296 |
|
297 private: |
|
298 nsresult SetupPACThread(); |
|
299 nsresult ResetPACThread(); |
|
300 |
|
301 public: |
|
302 // The Sun Forte compiler and others implement older versions of the |
|
303 // C++ standard's rules on access and nested classes. These structs |
|
304 // need to be public in order to deal with those compilers. |
|
305 |
|
306 struct HostInfoIP { |
|
307 uint16_t family; |
|
308 uint16_t mask_len; |
|
309 PRIPv6Addr addr; // possibly IPv4-mapped address |
|
310 }; |
|
311 |
|
312 struct HostInfoName { |
|
313 char *host; |
|
314 uint32_t host_len; |
|
315 }; |
|
316 |
|
317 protected: |
|
318 |
|
319 // simplified array of filters defined by this struct |
|
320 struct HostInfo { |
|
321 bool is_ipaddr; |
|
322 int32_t port; |
|
323 union { |
|
324 HostInfoIP ip; |
|
325 HostInfoName name; |
|
326 }; |
|
327 |
|
328 HostInfo() |
|
329 : is_ipaddr(false) |
|
330 { /* other members intentionally uninitialized */ } |
|
331 ~HostInfo() { |
|
332 if (!is_ipaddr && name.host) |
|
333 nsMemory::Free(name.host); |
|
334 } |
|
335 }; |
|
336 |
|
337 // An instance of this struct is allocated for each registered |
|
338 // nsIProtocolProxyFilter and each nsIProtocolProxyChannelFilter. |
|
339 struct FilterLink { |
|
340 struct FilterLink *next; |
|
341 uint32_t position; |
|
342 nsCOMPtr<nsIProtocolProxyFilter> filter; |
|
343 nsCOMPtr<nsIProtocolProxyChannelFilter> channelFilter; |
|
344 FilterLink(uint32_t p, nsIProtocolProxyFilter *f) |
|
345 : next(nullptr), position(p), filter(f), channelFilter(nullptr) {} |
|
346 FilterLink(uint32_t p, nsIProtocolProxyChannelFilter *cf) |
|
347 : next(nullptr), position(p), filter(nullptr), channelFilter(cf) {} |
|
348 // Chain deletion to simplify cleaning up the filter links |
|
349 ~FilterLink() { if (next) delete next; } |
|
350 }; |
|
351 |
|
352 private: |
|
353 // Private methods to insert and remove FilterLinks from the FilterLink chain. |
|
354 nsresult InsertFilterLink(FilterLink *link, uint32_t position); |
|
355 nsresult RemoveFilterLink(nsISupports *givenObject); |
|
356 |
|
357 protected: |
|
358 // Indicates if local hosts (plain hostnames, no dots) should use the proxy |
|
359 bool mFilterLocalHosts; |
|
360 |
|
361 // Holds an array of HostInfo objects |
|
362 nsTArray<nsAutoPtr<HostInfo> > mHostFiltersArray; |
|
363 |
|
364 // Points to the start of a sorted by position, singly linked list |
|
365 // of FilterLink objects. |
|
366 FilterLink *mFilters; |
|
367 |
|
368 uint32_t mProxyConfig; |
|
369 |
|
370 nsCString mHTTPProxyHost; |
|
371 int32_t mHTTPProxyPort; |
|
372 |
|
373 nsCString mFTPProxyHost; |
|
374 int32_t mFTPProxyPort; |
|
375 |
|
376 nsCString mHTTPSProxyHost; |
|
377 int32_t mHTTPSProxyPort; |
|
378 |
|
379 nsCString mSOCKSProxyHost; |
|
380 int32_t mSOCKSProxyPort; |
|
381 int32_t mSOCKSProxyVersion; |
|
382 bool mSOCKSProxyRemoteDNS; |
|
383 nsCString mSOCKSProxyUsername; |
|
384 nsCString mSOCKSProxyPassword; |
|
385 |
|
386 nsRefPtr<nsPACMan> mPACMan; // non-null if we are using PAC |
|
387 nsCOMPtr<nsISystemProxySettings> mSystemProxySettings; |
|
388 |
|
389 PRTime mSessionStart; |
|
390 nsFailedProxyTable mFailedProxies; |
|
391 int32_t mFailedProxyTimeout; |
|
392 |
|
393 private: |
|
394 nsresult AsyncResolveInternal(nsIChannel *channel, uint32_t flags, |
|
395 nsIProtocolProxyCallback *callback, |
|
396 nsICancelable **result, |
|
397 bool isSyncOK); |
|
398 |
|
399 }; |
|
400 |
|
401 NS_DEFINE_STATIC_IID_ACCESSOR(nsProtocolProxyService, NS_PROTOCOL_PROXY_SERVICE_IMPL_CID) |
|
402 |
|
403 #endif // !nsProtocolProxyService_h__ |