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/. */
7 #ifndef ProxyAutoConfig_h__
8 #define ProxyAutoConfig_h__
25 class JSContextWrapper
;
26 class ProxyAutoConfigParent
;
29 class ProxyAutoConfigBase
{
31 virtual ~ProxyAutoConfigBase() = default;
32 virtual nsresult
Init(nsIThread
* aPACThread
) { return NS_OK
; }
33 virtual nsresult
ConfigurePAC(const nsACString
& aPACURI
,
34 const nsACString
& aPACScriptData
,
35 bool aIncludePath
, uint32_t aExtraHeapSize
,
36 nsISerialEventTarget
* aEventTarget
) = 0;
37 virtual void SetThreadLocalIndex(uint32_t index
) {}
38 virtual void Shutdown() = 0;
39 virtual void GC() = 0;
40 virtual void GetProxyForURIWithCallback(
41 const nsACString
& aTestURI
, const nsACString
& aTestHost
,
42 std::function
<void(nsresult aStatus
, const nsACString
& aResult
)>&&
46 // The ProxyAutoConfig class is meant to be created and run on a
47 // non main thread. It synchronously resolves PAC files by blocking that
48 // thread and running nested event loops. GetProxyForURI is not re-entrant.
50 class ProxyAutoConfig
: public ProxyAutoConfigBase
{
53 virtual ~ProxyAutoConfig();
55 nsresult
ConfigurePAC(const nsACString
& aPACURI
,
56 const nsACString
& aPACScriptData
, bool aIncludePath
,
57 uint32_t aExtraHeapSize
,
58 nsISerialEventTarget
* aEventTarget
) override
;
59 void SetThreadLocalIndex(uint32_t index
) override
;
60 void Shutdown() override
;
62 bool MyIPAddress(const JS::CallArgs
& aArgs
);
63 bool ResolveAddress(const nsACString
& aHostName
, NetAddr
* aNetAddr
,
64 unsigned int aTimeout
);
67 * Get the proxy string for the specified URI. The proxy string is
68 * given by the following:
70 * result = proxy-spec *( proxy-sep proxy-spec )
71 * proxy-spec = direct-type | proxy-type LWS proxy-host [":" proxy-port]
72 * direct-type = "DIRECT"
73 * proxy-type = "PROXY" | "HTTP" | "HTTPS" | "SOCKS" | "SOCKS4" | "SOCKS5"
75 * proxy-host = hostname | ipv4-address-literal
76 * proxy-port = <any 16-bit unsigned integer>
78 * SP = <US-ASCII SP, space (32)>
79 * HT = <US-ASCII HT, horizontal-tab (9)>
81 * NOTE: direct-type and proxy-type are case insensitive
82 * NOTE: SOCKS implies SOCKS4
85 * "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT"
89 * XXX add support for IPv6 address literals.
90 * XXX quote whatever the official standard is for PAC.
93 * The URI as an ASCII string to test.
95 * The ASCII hostname to test.
98 * result string as defined above.
100 nsresult
GetProxyForURI(const nsACString
& aTestURI
,
101 const nsACString
& aTestHost
, nsACString
& result
);
103 void GetProxyForURIWithCallback(
104 const nsACString
& aTestURI
, const nsACString
& aTestHost
,
105 std::function
<void(nsresult aStatus
, const nsACString
& aResult
)>&&
109 // allow 665ms for myipaddress dns queries. That's 95th percentile.
110 const static unsigned int kTimeout
= 665;
112 // used to compile the PAC file and setup the execution context
115 bool SrcAddress(const NetAddr
* remoteAddress
, nsCString
& localAddress
);
116 bool MyIPAddressTryHost(const nsACString
& hostName
, unsigned int timeout
,
117 const JS::CallArgs
& aArgs
, bool* aResult
);
119 JSContextWrapper
* mJSContext
{nullptr};
120 bool mJSNeedsSetup
{false};
121 bool mShutdown
{true};
122 nsCString mConcatenatedPACData
;
124 bool mIncludePath
{false};
125 uint32_t mExtraHeapSize
{0};
126 nsCString mRunningHost
;
127 nsCOMPtr
<nsITimer
> mTimer
;
128 nsCOMPtr
<nsISerialEventTarget
> mMainThreadEventTarget
;
131 class RemoteProxyAutoConfig
: public ProxyAutoConfigBase
{
133 RemoteProxyAutoConfig();
134 virtual ~RemoteProxyAutoConfig();
136 nsresult
Init(nsIThread
* aPACThread
) override
;
137 nsresult
ConfigurePAC(const nsACString
& aPACURI
,
138 const nsACString
& aPACScriptData
, bool aIncludePath
,
139 uint32_t aExtraHeapSize
,
140 nsISerialEventTarget
* aEventTarget
) override
;
141 void Shutdown() override
;
143 void GetProxyForURIWithCallback(
144 const nsACString
& aTestURI
, const nsACString
& aTestHost
,
145 std::function
<void(nsresult aStatus
, const nsACString
& aResult
)>&&
149 RefPtr
<ProxyAutoConfigParent
> mProxyAutoConfigParent
;
153 } // namespace mozilla
155 #endif // ProxyAutoConfig_h__