Source code

Revision control

Other Tools

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