PageRenderTime 56ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/http/proxy.h

http://github.com/mozy/mordor
C Header | 143 lines | 81 code | 31 blank | 31 comment | 0 complexity | 8a2bfb15fa17e0353455d37ef5e7e2ab MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_HTTP_PROXY_H__
  2. #define __MORDOR_HTTP_PROXY_H__
  3. // Copyright (c) 2009 - Mozy, Inc.
  4. #include "mordor/uri.h"
  5. #ifdef WINDOWS
  6. #include <winhttp.h>
  7. #elif defined (OSX)
  8. #include <queue>
  9. #include <boost/thread.hpp>
  10. #include <SystemConfiguration/SystemConfiguration.h>
  11. #include "mordor/util.h"
  12. #include "mordor/http/broker.h"
  13. #endif
  14. namespace Mordor {
  15. class Stream;
  16. namespace HTTP {
  17. class RequestBroker;
  18. // Functions to use for the proxyForURIDg
  19. // The default; if you've done Config::loadFromEnvironment, this will use the
  20. // HTTP_PROXY environment variable (passing it to proxyFromList)
  21. std::vector<URI> proxyFromConfig(const URI &uri);
  22. // This parses proxy and bypassList according to the WINHTTP_PROXY_INFO
  23. // structure; additionally if bypassList is blank, it will look for a !
  24. // in the proxy, and use that to separate the proxy from the
  25. // bypassList
  26. std::vector<URI> proxyFromList(const URI &uri,
  27. std::string proxy,
  28. std::string bypassList = std::string());
  29. #ifdef WINDOWS
  30. std::vector<URI> proxyFromMachineDefault(const URI &uri);
  31. struct ProxySettings
  32. {
  33. bool autoDetect;
  34. std::string pacScript;
  35. std::string proxy;
  36. std::string bypassList;
  37. };
  38. ProxySettings getUserProxySettings();
  39. class ProxyCache
  40. {
  41. public:
  42. ProxyCache(const std::string &userAgent = std::string());
  43. ~ProxyCache();
  44. // Use the user's global proxy settings (which may specify a
  45. // proxy server, url to a configuration script or autodetection)
  46. std::vector<URI> proxyFromUserSettings(const URI &uri);
  47. // Determine the Proxy URIs, if any, to use to reach the specified uri
  48. // If no pacScript url is specified the system will attempt to autodetect one
  49. bool autoDetectProxy(const URI &uri, const std::string &pacScript,
  50. std::vector<URI> &proxyList);
  51. // Depending on the settings the proxyFromUserSettings() method
  52. // may attempt to autodetect a proxy.
  53. // This autodetection can be slow, so the results are cached.
  54. // This method should be called if network configuration changes are
  55. // detected to force a new discovery
  56. static void resetDetectionResultCache();
  57. private:
  58. HINTERNET m_hHttpSession;
  59. // The follow members are static so that the cache state can be shared by
  60. // all of the ProxyCache instances. This cache is invalided by calling
  61. // resetDetectionResultCache(), defined above.
  62. //
  63. // The lock held when accessing the static member variables.
  64. static boost::mutex s_cacheMutex;
  65. // True if WPAD failed.
  66. static bool s_failedAutoDetect;
  67. // The list of failed PAC file URLs.
  68. static std::set<std::string> s_invalidConfigURLs;
  69. };
  70. #elif defined (OSX)
  71. class ProxyCache
  72. {
  73. public:
  74. ProxyCache(boost::shared_ptr<RequestBroker> requestBroker);
  75. ~ProxyCache();
  76. std::vector<URI> proxyFromSystemConfiguration(const URI &uri);
  77. private:
  78. ScopedCFRef<SCDynamicStoreRef> m_dynamicStore;
  79. boost::shared_ptr<RequestBroker> m_requestBroker;
  80. std::map<URI, ScopedCFRef<CFStringRef> > m_cachedScripts;
  81. struct PacMessage {
  82. ScopedCFRef<CFStringRef> pacScript;
  83. ScopedCFRef<CFURLRef> targeturl;
  84. ScopedCFRef<CFArrayRef> result;
  85. bool processed;
  86. };
  87. boost::thread m_pacThread;
  88. boost::condition_variable m_pacCond;
  89. boost::mutex m_pacMut;
  90. std::queue<PacMessage*> m_pacQueue;
  91. bool m_pacThreadCancelled;
  92. void runPacWorker();
  93. std::vector<URI> proxyFromPacScript(CFURLRef cfurl, ScopedCFRef<CFURLRef> targeturl,
  94. RequestBroker::ptr requestBroker,
  95. std::map<URI, ScopedCFRef<CFStringRef> > &cachedScripts);
  96. std::vector<URI> proxyFromCFArray(CFArrayRef proxies, ScopedCFRef<CFURLRef> targeturl,
  97. RequestBroker::ptr requestBroker,
  98. std::map<URI, ScopedCFRef<CFStringRef> > &cachedScripts);
  99. };
  100. #endif
  101. /// Establish a tunnel via an HTTPS proxy
  102. ///
  103. /// @note This is *broken* if the ConnectionCache this RequestBroker is using
  104. /// attempts to re-use the connection. We can't set forceNewConnection,
  105. /// because that would break NTLM authentication, and ConnectionCache
  106. /// hasn't been improved yet to do allowPipelining instead of
  107. /// forceNewConnection
  108. boost::shared_ptr<Stream>
  109. tunnel(boost::shared_ptr<RequestBroker> requestBroker, const URI &proxy,
  110. const URI &target);
  111. }}
  112. #endif