ProxyAutoConfig.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifndef ProxyAutoConfig_h__
  7. #define ProxyAutoConfig_h__
  8. #include "nsString.h"
  9. #include "nsCOMPtr.h"
  10. class nsITimer;
  11. namespace JS {
  12. class CallArgs;
  13. } // namespace JS
  14. namespace mozilla { namespace net {
  15. class JSContextWrapper;
  16. union NetAddr;
  17. // The ProxyAutoConfig class is meant to be created and run on a
  18. // non main thread. It synchronously resolves PAC files by blocking that
  19. // thread and running nested event loops. GetProxyForURI is not re-entrant.
  20. class ProxyAutoConfig {
  21. public:
  22. ProxyAutoConfig();
  23. ~ProxyAutoConfig();
  24. nsresult Init(const nsCString &aPACURI,
  25. const nsCString &aPACScript,
  26. bool aIncludePath);
  27. void SetThreadLocalIndex(uint32_t index);
  28. void Shutdown();
  29. void GC();
  30. bool MyIPAddress(const JS::CallArgs &aArgs);
  31. bool ResolveAddress(const nsCString &aHostName,
  32. NetAddr *aNetAddr, unsigned int aTimeout);
  33. /**
  34. * Get the proxy string for the specified URI. The proxy string is
  35. * given by the following:
  36. *
  37. * result = proxy-spec *( proxy-sep proxy-spec )
  38. * proxy-spec = direct-type | proxy-type LWS proxy-host [":" proxy-port]
  39. * direct-type = "DIRECT"
  40. * proxy-type = "PROXY" | "HTTP" | "HTTPS" | "SOCKS" | "SOCKS4" | "SOCKS5"
  41. * proxy-sep = ";" LWS
  42. * proxy-host = hostname | ipv4-address-literal
  43. * proxy-port = <any 16-bit unsigned integer>
  44. * LWS = *( SP | HT )
  45. * SP = <US-ASCII SP, space (32)>
  46. * HT = <US-ASCII HT, horizontal-tab (9)>
  47. *
  48. * NOTE: direct-type and proxy-type are case insensitive
  49. * NOTE: SOCKS implies SOCKS4
  50. *
  51. * Examples:
  52. * "PROXY proxy1.foo.com:8080; PROXY proxy2.foo.com:8080; DIRECT"
  53. * "SOCKS socksproxy"
  54. * "DIRECT"
  55. *
  56. * XXX add support for IPv6 address literals.
  57. * XXX quote whatever the official standard is for PAC.
  58. *
  59. * @param aTestURI
  60. * The URI as an ASCII string to test.
  61. * @param aTestHost
  62. * The ASCII hostname to test.
  63. *
  64. * @param result
  65. * result string as defined above.
  66. */
  67. nsresult GetProxyForURI(const nsCString &aTestURI,
  68. const nsCString &aTestHost,
  69. nsACString &result);
  70. private:
  71. // allow 665ms for myipaddress dns queries. That's 95th percentile.
  72. const static unsigned int kTimeout = 665;
  73. // used to compile the PAC file and setup the execution context
  74. nsresult SetupJS();
  75. bool SrcAddress(const NetAddr *remoteAddress, nsCString &localAddress);
  76. bool MyIPAddressTryHost(const nsCString &hostName, unsigned int timeout,
  77. const JS::CallArgs &aArgs, bool* aResult);
  78. JSContextWrapper *mJSContext;
  79. bool mJSNeedsSetup;
  80. bool mShutdown;
  81. nsCString mPACScript;
  82. nsCString mPACURI;
  83. bool mIncludePath;
  84. nsCString mRunningHost;
  85. nsCOMPtr<nsITimer> mTimer;
  86. };
  87. } // namespace net
  88. } // namespace mozilla
  89. #endif // ProxyAutoConfig_h__