nsProtocolProxyService.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsProtocolProxyService_h__
  6. #define nsProtocolProxyService_h__
  7. #include "nsString.h"
  8. #include "nsCOMPtr.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsTArray.h"
  11. #include "nsIProtocolProxyService2.h"
  12. #include "nsIProtocolProxyFilter.h"
  13. #include "nsIProxyInfo.h"
  14. #include "nsIObserver.h"
  15. #include "nsDataHashtable.h"
  16. #include "nsHashKeys.h"
  17. #include "prio.h"
  18. #include "mozilla/Attributes.h"
  19. class nsIPrefBranch;
  20. class nsISystemProxySettings;
  21. namespace mozilla {
  22. namespace net {
  23. typedef nsDataHashtable<nsCStringHashKey, uint32_t> nsFailedProxyTable;
  24. class nsPACMan;
  25. class nsProxyInfo;
  26. struct nsProtocolInfo;
  27. // CID for the nsProtocolProxyService class
  28. // 091eedd8-8bae-4fe3-ad62-0c87351e640d
  29. #define NS_PROTOCOL_PROXY_SERVICE_IMPL_CID \
  30. { 0x091eedd8, 0x8bae, 0x4fe3, \
  31. { 0xad, 0x62, 0x0c, 0x87, 0x35, 0x1e, 0x64, 0x0d } }
  32. class nsProtocolProxyService final : public nsIProtocolProxyService2
  33. , public nsIObserver
  34. {
  35. public:
  36. NS_DECL_ISUPPORTS
  37. NS_DECL_NSIPROTOCOLPROXYSERVICE2
  38. NS_DECL_NSIPROTOCOLPROXYSERVICE
  39. NS_DECL_NSIOBSERVER
  40. NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROTOCOL_PROXY_SERVICE_IMPL_CID)
  41. nsProtocolProxyService();
  42. nsresult Init();
  43. nsresult DeprecatedBlockingResolve(nsIChannel *aChannel,
  44. uint32_t aFlags,
  45. nsIProxyInfo **retval);
  46. protected:
  47. friend class nsAsyncResolveRequest;
  48. friend class TestProtocolProxyService_LoadHostFilters_Test; // for gtest
  49. ~nsProtocolProxyService();
  50. /**
  51. * This method is called whenever a preference may have changed or
  52. * to initialize all preferences.
  53. *
  54. * @param prefs
  55. * This must be a pointer to the root pref branch.
  56. * @param name
  57. * This can be the name of a fully-qualified preference, or it can
  58. * be null, in which case all preferences will be initialized.
  59. */
  60. void PrefsChanged(nsIPrefBranch *prefs, const char *name);
  61. /**
  62. * This method is called to create a nsProxyInfo instance from the given
  63. * PAC-style proxy string. It parses up to the end of the string, or to
  64. * the next ';' character.
  65. *
  66. * @param proxy
  67. * The PAC-style proxy string to parse. This must not be null.
  68. * @param aResolveFlags
  69. * The flags passed to Resolve or AsyncResolve that are stored in
  70. * proxyInfo.
  71. * @param result
  72. * Upon return this points to a newly allocated nsProxyInfo or null
  73. * if the proxy string was invalid.
  74. *
  75. * @return A pointer beyond the parsed proxy string (never null).
  76. */
  77. const char * ExtractProxyInfo(const char *proxy,
  78. uint32_t aResolveFlags,
  79. nsProxyInfo **result);
  80. /**
  81. * Load the specified PAC file.
  82. *
  83. * @param pacURI
  84. * The URI spec of the PAC file to load.
  85. */
  86. nsresult ConfigureFromPAC(const nsCString &pacURI, bool forceReload);
  87. /**
  88. * This method builds a list of nsProxyInfo objects from the given PAC-
  89. * style string.
  90. *
  91. * @param pacString
  92. * The PAC-style proxy string to parse. This may be empty.
  93. * @param aResolveFlags
  94. * The flags passed to Resolve or AsyncResolve that are stored in
  95. * proxyInfo.
  96. * @param result
  97. * The resulting list of proxy info objects.
  98. */
  99. void ProcessPACString(const nsCString &pacString,
  100. uint32_t aResolveFlags,
  101. nsIProxyInfo **result);
  102. /**
  103. * This method generates a string valued identifier for the given
  104. * nsProxyInfo object.
  105. *
  106. * @param pi
  107. * The nsProxyInfo object from which to generate the key.
  108. * @param result
  109. * Upon return, this parameter holds the generated key.
  110. */
  111. void GetProxyKey(nsProxyInfo *pi, nsCString &result);
  112. /**
  113. * @return Seconds since start of session.
  114. */
  115. uint32_t SecondsSinceSessionStart();
  116. /**
  117. * This method removes the specified proxy from the disabled list.
  118. *
  119. * @param pi
  120. * The nsProxyInfo object identifying the proxy to enable.
  121. */
  122. void EnableProxy(nsProxyInfo *pi);
  123. /**
  124. * This method adds the specified proxy to the disabled list.
  125. *
  126. * @param pi
  127. * The nsProxyInfo object identifying the proxy to disable.
  128. */
  129. void DisableProxy(nsProxyInfo *pi);
  130. /**
  131. * This method tests to see if the given proxy is disabled.
  132. *
  133. * @param pi
  134. * The nsProxyInfo object identifying the proxy to test.
  135. *
  136. * @return True if the specified proxy is disabled.
  137. */
  138. bool IsProxyDisabled(nsProxyInfo *pi);
  139. /**
  140. * This method queries the protocol handler for the given scheme to check
  141. * for the protocol flags and default port.
  142. *
  143. * @param uri
  144. * The URI to query.
  145. * @param info
  146. * Holds information about the protocol upon return. Pass address
  147. * of structure when you call this method. This parameter must not
  148. * be null.
  149. */
  150. nsresult GetProtocolInfo(nsIURI *uri, nsProtocolInfo *result);
  151. /**
  152. * This method is an internal version nsIProtocolProxyService::newProxyInfo
  153. * that expects a string literal for the type.
  154. *
  155. * @param type
  156. * The proxy type.
  157. * @param host
  158. * The proxy host name (UTF-8 ok).
  159. * @param port
  160. * The proxy port number.
  161. * @param username
  162. * The username for the proxy (ASCII). May be "", but not null.
  163. * @param password
  164. * The password for the proxy (ASCII). May be "", but not null.
  165. * @param flags
  166. * The proxy flags (nsIProxyInfo::flags).
  167. * @param timeout
  168. * The failover timeout for this proxy.
  169. * @param next
  170. * The next proxy to try if this one fails.
  171. * @param aResolveFlags
  172. * The flags passed to resolve (from nsIProtocolProxyService).
  173. * @param result
  174. * The resulting nsIProxyInfo object.
  175. */
  176. nsresult NewProxyInfo_Internal(const char *type,
  177. const nsACString &host,
  178. int32_t port,
  179. const nsACString &username,
  180. const nsACString &password,
  181. uint32_t flags,
  182. uint32_t timeout,
  183. nsIProxyInfo *next,
  184. uint32_t aResolveFlags,
  185. nsIProxyInfo **result);
  186. /**
  187. * This method is an internal version of Resolve that does not query PAC.
  188. * It performs all of the built-in processing, and reports back to the
  189. * caller with either the proxy info result or a flag to instruct the
  190. * caller to use PAC instead.
  191. *
  192. * @param channel
  193. * The channel to test.
  194. * @param info
  195. * Information about the URI's protocol.
  196. * @param flags
  197. * The flags passed to either the resolve or the asyncResolve method.
  198. * @param usePAC
  199. * If this flag is set upon return, then PAC should be queried to
  200. * resolve the proxy info.
  201. * @param result
  202. * The resulting proxy info or null.
  203. */
  204. nsresult Resolve_Internal(nsIChannel *channel,
  205. const nsProtocolInfo &info,
  206. uint32_t flags,
  207. bool *usePAC,
  208. nsIProxyInfo **result);
  209. /**
  210. * This method applies the registered filters to the given proxy info
  211. * list, and returns a possibly modified list.
  212. *
  213. * @param channel
  214. * The channel corresponding to this proxy info list.
  215. * @param info
  216. * Information about the URI's protocol.
  217. * @param proxyInfo
  218. * The proxy info list to be modified. This is an inout param.
  219. */
  220. void ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info,
  221. nsIProxyInfo **proxyInfo);
  222. /**
  223. * This method is a simple wrapper around ApplyFilters that takes the
  224. * proxy info list inout param as a nsCOMPtr.
  225. */
  226. inline void ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info,
  227. nsCOMPtr<nsIProxyInfo> &proxyInfo)
  228. {
  229. nsIProxyInfo *pi = nullptr;
  230. proxyInfo.swap(pi);
  231. ApplyFilters(channel, info, &pi);
  232. proxyInfo.swap(pi);
  233. }
  234. /**
  235. * This method prunes out disabled and disallowed proxies from a given
  236. * proxy info list.
  237. *
  238. * @param info
  239. * Information about the URI's protocol.
  240. * @param proxyInfo
  241. * The proxy info list to be modified. This is an inout param.
  242. */
  243. void PruneProxyInfo(const nsProtocolInfo &info,
  244. nsIProxyInfo **proxyInfo);
  245. /**
  246. * This method populates mHostFiltersArray from the given string.
  247. *
  248. * @param hostFilters
  249. * A "no-proxy-for" exclusion list.
  250. */
  251. void LoadHostFilters(const nsACString& hostFilters);
  252. /**
  253. * This method checks the given URI against mHostFiltersArray.
  254. *
  255. * @param uri
  256. * The URI to test.
  257. * @param defaultPort
  258. * The default port for the given URI.
  259. *
  260. * @return True if the URI can use the specified proxy.
  261. */
  262. bool CanUseProxy(nsIURI *uri, int32_t defaultPort);
  263. /**
  264. * Disable Prefetch in the DNS service if a proxy is in use.
  265. *
  266. * @param aProxy
  267. * The proxy information
  268. */
  269. void MaybeDisableDNSPrefetch(nsIProxyInfo *aProxy);
  270. private:
  271. nsresult SetupPACThread();
  272. nsresult ResetPACThread();
  273. nsresult ReloadNetworkPAC();
  274. public:
  275. // The Sun Forte compiler and others implement older versions of the
  276. // C++ standard's rules on access and nested classes. These structs
  277. // need to be public in order to deal with those compilers.
  278. struct HostInfoIP {
  279. uint16_t family;
  280. uint16_t mask_len;
  281. PRIPv6Addr addr; // possibly IPv4-mapped address
  282. };
  283. struct HostInfoName {
  284. char *host;
  285. uint32_t host_len;
  286. };
  287. protected:
  288. // simplified array of filters defined by this struct
  289. struct HostInfo {
  290. bool is_ipaddr;
  291. int32_t port;
  292. union {
  293. HostInfoIP ip;
  294. HostInfoName name;
  295. };
  296. HostInfo()
  297. : is_ipaddr(false)
  298. , port(0)
  299. { /* other members intentionally uninitialized */ }
  300. ~HostInfo() {
  301. if (!is_ipaddr && name.host)
  302. free(name.host);
  303. }
  304. };
  305. // An instance of this struct is allocated for each registered
  306. // nsIProtocolProxyFilter and each nsIProtocolProxyChannelFilter.
  307. struct FilterLink {
  308. struct FilterLink *next;
  309. uint32_t position;
  310. nsCOMPtr<nsIProtocolProxyFilter> filter;
  311. nsCOMPtr<nsIProtocolProxyChannelFilter> channelFilter;
  312. FilterLink(uint32_t p, nsIProtocolProxyFilter *f)
  313. : next(nullptr), position(p), filter(f), channelFilter(nullptr) {}
  314. FilterLink(uint32_t p, nsIProtocolProxyChannelFilter *cf)
  315. : next(nullptr), position(p), filter(nullptr), channelFilter(cf) {}
  316. // Chain deletion to simplify cleaning up the filter links
  317. ~FilterLink() { if (next) delete next; }
  318. };
  319. private:
  320. // Private methods to insert and remove FilterLinks from the FilterLink chain.
  321. nsresult InsertFilterLink(FilterLink *link, uint32_t position);
  322. nsresult RemoveFilterLink(nsISupports *givenObject);
  323. protected:
  324. // Indicates if local hosts (plain hostnames, no dots) should use the proxy
  325. bool mFilterLocalHosts;
  326. // Holds an array of HostInfo objects
  327. nsTArray<nsAutoPtr<HostInfo> > mHostFiltersArray;
  328. // Points to the start of a sorted by position, singly linked list
  329. // of FilterLink objects.
  330. FilterLink *mFilters;
  331. uint32_t mProxyConfig;
  332. nsCString mHTTPProxyHost;
  333. int32_t mHTTPProxyPort;
  334. nsCString mFTPProxyHost;
  335. int32_t mFTPProxyPort;
  336. nsCString mHTTPSProxyHost;
  337. int32_t mHTTPSProxyPort;
  338. // mSOCKSProxyTarget could be a host, a domain socket path,
  339. // or a named-pipe name.
  340. nsCString mSOCKSProxyTarget;
  341. int32_t mSOCKSProxyPort;
  342. int32_t mSOCKSProxyVersion;
  343. bool mSOCKSProxyRemoteDNS;
  344. bool mProxyOverTLS;
  345. RefPtr<nsPACMan> mPACMan; // non-null if we are using PAC
  346. nsCOMPtr<nsISystemProxySettings> mSystemProxySettings;
  347. PRTime mSessionStart;
  348. nsFailedProxyTable mFailedProxies;
  349. int32_t mFailedProxyTimeout;
  350. private:
  351. nsresult AsyncResolveInternal(nsIChannel *channel, uint32_t flags,
  352. nsIProtocolProxyCallback *callback,
  353. nsICancelable **result,
  354. bool isSyncOK);
  355. };
  356. NS_DEFINE_STATIC_IID_ACCESSOR(nsProtocolProxyService, NS_PROTOCOL_PROXY_SERVICE_IMPL_CID)
  357. } // namespace net
  358. } // namespace mozilla
  359. #endif // !nsProtocolProxyService_h__