Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef ProxyAutoConfig_h__
8
#define ProxyAutoConfig_h__
9
10
#include "nsString.h"
11
#include "nsCOMPtr.h"
12
13
class nsIEventTarget;
14
class nsITimer;
15
namespace JS {
16
class CallArgs;
17
} // namespace JS
18
19
namespace mozilla {
20
namespace net {
21
22
class JSContextWrapper;
23
union NetAddr;
24
25
// The ProxyAutoConfig class is meant to be created and run on a
26
// non main thread. It synchronously resolves PAC files by blocking that
27
// thread and running nested event loops. GetProxyForURI is not re-entrant.
28
29
class ProxyAutoConfig {
30
public:
31
ProxyAutoConfig();
32
~ProxyAutoConfig();
33
34
nsresult Init(const nsCString& aPACURI, const nsCString& aPACScriptData,
35
bool aIncludePath, uint32_t aExtraHeapSize,
36
nsIEventTarget* aEventTarget);
37
void SetThreadLocalIndex(uint32_t index);
38
void Shutdown();
39
void GC();
40
bool MyIPAddress(const JS::CallArgs& aArgs);
41
bool ResolveAddress(const nsCString& aHostName, NetAddr* aNetAddr,
42
unsigned int aTimeout);
43
44
/**
45
* Get the proxy string for the specified URI. The proxy string is
46
* given by the following:
47
*
48
* result = proxy-spec *( proxy-sep proxy-spec )
49
* proxy-spec = direct-type | proxy-type LWS proxy-host [":" proxy-port]
50
* direct-type = "DIRECT"
51
* proxy-type = "PROXY" | "HTTP" | "HTTPS" | "SOCKS" | "SOCKS4" | "SOCKS5"
52
* proxy-sep = ";" LWS
53
* proxy-host = hostname | ipv4-address-literal
54
* proxy-port = <any 16-bit unsigned integer>
55
* LWS = *( SP | HT )
56
* SP = <US-ASCII SP, space (32)>
57
* HT = <US-ASCII HT, horizontal-tab (9)>
58
*
59
* NOTE: direct-type and proxy-type are case insensitive
60
* NOTE: SOCKS implies SOCKS4
61
*
62
* Examples:
63
* "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT"
64
* "SOCKS socksproxy"
65
* "DIRECT"
66
*
67
* XXX add support for IPv6 address literals.
68
* XXX quote whatever the official standard is for PAC.
69
*
70
* @param aTestURI
71
* The URI as an ASCII string to test.
72
* @param aTestHost
73
* The ASCII hostname to test.
74
*
75
* @param result
76
* result string as defined above.
77
*/
78
nsresult GetProxyForURI(const nsCString& aTestURI, const nsCString& aTestHost,
79
nsACString& result);
80
81
private:
82
// allow 665ms for myipaddress dns queries. That's 95th percentile.
83
const static unsigned int kTimeout = 665;
84
85
// used to compile the PAC file and setup the execution context
86
nsresult SetupJS();
87
88
bool SrcAddress(const NetAddr* remoteAddress, nsCString& localAddress);
89
bool MyIPAddressTryHost(const nsCString& hostName, unsigned int timeout,
90
const JS::CallArgs& aArgs, bool* aResult);
91
92
JSContextWrapper* mJSContext;
93
bool mJSNeedsSetup;
94
bool mShutdown;
95
nsCString mConcatenatedPACData;
96
nsCString mPACURI;
97
bool mIncludePath;
98
uint32_t mExtraHeapSize;
99
nsCString mRunningHost;
100
nsCOMPtr<nsITimer> mTimer;
101
nsCOMPtr<nsIEventTarget> mMainThreadEventTarget;
102
};
103
104
} // namespace net
105
} // namespace mozilla
106
107
#endif // ProxyAutoConfig_h__