summaryrefslogtreecommitdiffstats
path: root/netwerk/base/ProxyAutoConfig.h
blob: e01450eadda9abed0c796089c8ac55c4235b3b4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef ProxyAutoConfig_h__
#define ProxyAutoConfig_h__

#include <functional>
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsTArray.h"

class nsIEventTarget;
class nsITimer;
class nsIThread;
namespace JS {
class CallArgs;
}  // namespace JS

namespace mozilla {
namespace net {

class JSContextWrapper;
class ProxyAutoConfigParent;
union NetAddr;

class ProxyAutoConfigBase {
 public:
  virtual ~ProxyAutoConfigBase() = default;
  virtual nsresult Init(nsIThread* aPACThread) { return NS_OK; }
  virtual nsresult ConfigurePAC(const nsACString& aPACURI,
                                const nsACString& aPACScriptData,
                                bool aIncludePath, uint32_t aExtraHeapSize,
                                nsISerialEventTarget* aEventTarget) = 0;
  virtual void SetThreadLocalIndex(uint32_t index) {}
  virtual void Shutdown() = 0;
  virtual void GC() = 0;
  virtual void GetProxyForURIWithCallback(
      const nsACString& aTestURI, const nsACString& aTestHost,
      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
          aCallback) = 0;
};

// The ProxyAutoConfig class is meant to be created and run on a
// non main thread. It synchronously resolves PAC files by blocking that
// thread and running nested event loops. GetProxyForURI is not re-entrant.

class ProxyAutoConfig : public ProxyAutoConfigBase {
 public:
  ProxyAutoConfig();
  virtual ~ProxyAutoConfig();

  nsresult ConfigurePAC(const nsACString& aPACURI,
                        const nsACString& aPACScriptData, bool aIncludePath,
                        uint32_t aExtraHeapSize,
                        nsISerialEventTarget* aEventTarget) override;
  void SetThreadLocalIndex(uint32_t index) override;
  void Shutdown() override;
  void GC() override;
  bool MyIPAddress(const JS::CallArgs& aArgs);
  bool ResolveAddress(const nsACString& aHostName, NetAddr* aNetAddr,
                      unsigned int aTimeout);

  /**
   * Get the proxy string for the specified URI.  The proxy string is
   * given by the following:
   *
   *   result      = proxy-spec *( proxy-sep proxy-spec )
   *   proxy-spec  = direct-type | proxy-type LWS proxy-host [":" proxy-port]
   *   direct-type = "DIRECT"
   *   proxy-type  = "PROXY" | "HTTP" | "HTTPS" | "SOCKS" | "SOCKS4" | "SOCKS5"
   *   proxy-sep   = ";" LWS
   *   proxy-host  = hostname | ipv4-address-literal
   *   proxy-port  = <any 16-bit unsigned integer>
   *   LWS         = *( SP | HT )
   *   SP          = <US-ASCII SP, space (32)>
   *   HT          = <US-ASCII HT, horizontal-tab (9)>
   *
   * NOTE: direct-type and proxy-type are case insensitive
   * NOTE: SOCKS implies SOCKS4
   *
   * Examples:
   *   "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT"
   *   "SOCKS socksproxy"
   *   "DIRECT"
   *
   * XXX add support for IPv6 address literals.
   * XXX quote whatever the official standard is for PAC.
   *
   * @param aTestURI
   *        The URI as an ASCII string to test.
   * @param aTestHost
   *        The ASCII hostname to test.
   *
   * @param result
   *        result string as defined above.
   */
  nsresult GetProxyForURI(const nsACString& aTestURI,
                          const nsACString& aTestHost, nsACString& result);

  void GetProxyForURIWithCallback(
      const nsACString& aTestURI, const nsACString& aTestHost,
      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
          aCallback) override;

 private:
  // allow 665ms for myipaddress dns queries. That's 95th percentile.
  const static unsigned int kTimeout = 665;

  // used to compile the PAC file and setup the execution context
  nsresult SetupJS();

  bool SrcAddress(const NetAddr* remoteAddress, nsCString& localAddress);
  bool MyIPAddressTryHost(const nsACString& hostName, unsigned int timeout,
                          const JS::CallArgs& aArgs, bool* aResult);

  JSContextWrapper* mJSContext{nullptr};
  bool mJSNeedsSetup{false};
  bool mShutdown{true};
  nsCString mConcatenatedPACData;
  nsCString mPACURI;
  bool mIncludePath{false};
  uint32_t mExtraHeapSize{0};
  nsCString mRunningHost;
  nsCOMPtr<nsITimer> mTimer;
  nsCOMPtr<nsISerialEventTarget> mMainThreadEventTarget;
};

class RemoteProxyAutoConfig : public ProxyAutoConfigBase {
 public:
  RemoteProxyAutoConfig();
  virtual ~RemoteProxyAutoConfig();

  nsresult Init(nsIThread* aPACThread) override;
  nsresult ConfigurePAC(const nsACString& aPACURI,
                        const nsACString& aPACScriptData, bool aIncludePath,
                        uint32_t aExtraHeapSize,
                        nsISerialEventTarget* aEventTarget) override;
  void Shutdown() override;
  void GC() override;
  void GetProxyForURIWithCallback(
      const nsACString& aTestURI, const nsACString& aTestHost,
      std::function<void(nsresult aStatus, const nsACString& aResult)>&&
          aCallback) override;

 private:
  RefPtr<ProxyAutoConfigParent> mProxyAutoConfigParent;
};

}  // namespace net
}  // namespace mozilla

#endif  // ProxyAutoConfig_h__