1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/nsProtocolProxyService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,403 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsProtocolProxyService_h__ 1.10 +#define nsProtocolProxyService_h__ 1.11 + 1.12 +#include "nsString.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsAutoPtr.h" 1.15 +#include "nsTArray.h" 1.16 +#include "nsIProtocolProxyService2.h" 1.17 +#include "nsIProtocolProxyFilter.h" 1.18 +#include "nsIProxyInfo.h" 1.19 +#include "nsIObserver.h" 1.20 +#include "nsDataHashtable.h" 1.21 +#include "nsHashKeys.h" 1.22 +#include "prio.h" 1.23 +#include "mozilla/Attributes.h" 1.24 + 1.25 +typedef nsDataHashtable<nsCStringHashKey, uint32_t> nsFailedProxyTable; 1.26 + 1.27 +class nsProxyInfo; 1.28 +struct nsProtocolInfo; 1.29 +class nsIPrefBranch; 1.30 +class nsISystemProxySettings; 1.31 +class nsPACMan; 1.32 + 1.33 +// CID for the nsProtocolProxyService class 1.34 +// 091eedd8-8bae-4fe3-ad62-0c87351e640d 1.35 +#define NS_PROTOCOL_PROXY_SERVICE_IMPL_CID \ 1.36 +{ 0x091eedd8, 0x8bae, 0x4fe3, \ 1.37 + { 0xad, 0x62, 0x0c, 0x87, 0x35, 0x1e, 0x64, 0x0d } } 1.38 + 1.39 +class nsProtocolProxyService MOZ_FINAL : public nsIProtocolProxyService2 1.40 + , public nsIObserver 1.41 +{ 1.42 +public: 1.43 + NS_DECL_ISUPPORTS 1.44 + NS_DECL_NSIPROTOCOLPROXYSERVICE2 1.45 + NS_DECL_NSIPROTOCOLPROXYSERVICE 1.46 + NS_DECL_NSIOBSERVER 1.47 + 1.48 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROTOCOL_PROXY_SERVICE_IMPL_CID) 1.49 + 1.50 + nsProtocolProxyService() NS_HIDDEN; 1.51 + 1.52 + NS_HIDDEN_(nsresult) Init(); 1.53 + nsresult DeprecatedBlockingResolve(nsIChannel *aChannel, 1.54 + uint32_t aFlags, 1.55 + nsIProxyInfo **retval); 1.56 + 1.57 +protected: 1.58 + friend class nsAsyncResolveRequest; 1.59 + 1.60 + ~nsProtocolProxyService() NS_HIDDEN; 1.61 + 1.62 + /** 1.63 + * This method is called whenever a preference may have changed or 1.64 + * to initialize all preferences. 1.65 + * 1.66 + * @param prefs 1.67 + * This must be a pointer to the root pref branch. 1.68 + * @param name 1.69 + * This can be the name of a fully-qualified preference, or it can 1.70 + * be null, in which case all preferences will be initialized. 1.71 + */ 1.72 + NS_HIDDEN_(void) PrefsChanged(nsIPrefBranch *prefs, const char *name); 1.73 + 1.74 + /** 1.75 + * This method is called to create a nsProxyInfo instance from the given 1.76 + * PAC-style proxy string. It parses up to the end of the string, or to 1.77 + * the next ';' character. 1.78 + * 1.79 + * @param proxy 1.80 + * The PAC-style proxy string to parse. This must not be null. 1.81 + * @param aResolveFlags 1.82 + * The flags passed to Resolve or AsyncResolve that are stored in 1.83 + * proxyInfo. 1.84 + * @param result 1.85 + * Upon return this points to a newly allocated nsProxyInfo or null 1.86 + * if the proxy string was invalid. 1.87 + * 1.88 + * @return A pointer beyond the parsed proxy string (never null). 1.89 + */ 1.90 + NS_HIDDEN_(const char *) ExtractProxyInfo(const char *proxy, 1.91 + uint32_t aResolveFlags, 1.92 + nsProxyInfo **result); 1.93 + 1.94 + /** 1.95 + * Load the specified PAC file. 1.96 + * 1.97 + * @param pacURI 1.98 + * The URI spec of the PAC file to load. 1.99 + */ 1.100 + NS_HIDDEN_(nsresult) ConfigureFromPAC(const nsCString &pacURI, bool forceReload); 1.101 + 1.102 + /** 1.103 + * This method builds a list of nsProxyInfo objects from the given PAC- 1.104 + * style string. 1.105 + * 1.106 + * @param pacString 1.107 + * The PAC-style proxy string to parse. This may be empty. 1.108 + * @param aResolveFlags 1.109 + * The flags passed to Resolve or AsyncResolve that are stored in 1.110 + * proxyInfo. 1.111 + * @param result 1.112 + * The resulting list of proxy info objects. 1.113 + */ 1.114 + NS_HIDDEN_(void) ProcessPACString(const nsCString &pacString, 1.115 + uint32_t aResolveFlags, 1.116 + nsIProxyInfo **result); 1.117 + 1.118 + /** 1.119 + * This method generates a string valued identifier for the given 1.120 + * nsProxyInfo object. 1.121 + * 1.122 + * @param pi 1.123 + * The nsProxyInfo object from which to generate the key. 1.124 + * @param result 1.125 + * Upon return, this parameter holds the generated key. 1.126 + */ 1.127 + NS_HIDDEN_(void) GetProxyKey(nsProxyInfo *pi, nsCString &result); 1.128 + 1.129 + /** 1.130 + * @return Seconds since start of session. 1.131 + */ 1.132 + NS_HIDDEN_(uint32_t) SecondsSinceSessionStart(); 1.133 + 1.134 + /** 1.135 + * This method removes the specified proxy from the disabled list. 1.136 + * 1.137 + * @param pi 1.138 + * The nsProxyInfo object identifying the proxy to enable. 1.139 + */ 1.140 + NS_HIDDEN_(void) EnableProxy(nsProxyInfo *pi); 1.141 + 1.142 + /** 1.143 + * This method adds the specified proxy to the disabled list. 1.144 + * 1.145 + * @param pi 1.146 + * The nsProxyInfo object identifying the proxy to disable. 1.147 + */ 1.148 + NS_HIDDEN_(void) DisableProxy(nsProxyInfo *pi); 1.149 + 1.150 + /** 1.151 + * This method tests to see if the given proxy is disabled. 1.152 + * 1.153 + * @param pi 1.154 + * The nsProxyInfo object identifying the proxy to test. 1.155 + * 1.156 + * @return True if the specified proxy is disabled. 1.157 + */ 1.158 + NS_HIDDEN_(bool) IsProxyDisabled(nsProxyInfo *pi); 1.159 + 1.160 + /** 1.161 + * This method queries the protocol handler for the given scheme to check 1.162 + * for the protocol flags and default port. 1.163 + * 1.164 + * @param uri 1.165 + * The URI to query. 1.166 + * @param info 1.167 + * Holds information about the protocol upon return. Pass address 1.168 + * of structure when you call this method. This parameter must not 1.169 + * be null. 1.170 + */ 1.171 + NS_HIDDEN_(nsresult) GetProtocolInfo(nsIURI *uri, nsProtocolInfo *result); 1.172 + 1.173 + /** 1.174 + * This method is an internal version nsIProtocolProxyService::newProxyInfo 1.175 + * that expects a string literal for the type. 1.176 + * 1.177 + * @param type 1.178 + * The proxy type. 1.179 + * @param host 1.180 + * The proxy host name (UTF-8 ok). 1.181 + * @param port 1.182 + * The proxy port number. 1.183 + * @param username 1.184 + * The username for the proxy (ASCII). May be "", but not null. 1.185 + * @param password 1.186 + * The password for the proxy (ASCII). May be "", but not null. 1.187 + * @param flags 1.188 + * The proxy flags (nsIProxyInfo::flags). 1.189 + * @param timeout 1.190 + * The failover timeout for this proxy. 1.191 + * @param next 1.192 + * The next proxy to try if this one fails. 1.193 + * @param aResolveFlags 1.194 + * The flags passed to resolve (from nsIProtocolProxyService). 1.195 + * @param result 1.196 + * The resulting nsIProxyInfo object. 1.197 + */ 1.198 + NS_HIDDEN_(nsresult) NewProxyInfo_Internal(const char *type, 1.199 + const nsACString &host, 1.200 + int32_t port, 1.201 + const nsACString &username, 1.202 + const nsACString &password, 1.203 + uint32_t flags, 1.204 + uint32_t timeout, 1.205 + nsIProxyInfo *next, 1.206 + uint32_t aResolveFlags, 1.207 + nsIProxyInfo **result); 1.208 + 1.209 + /** 1.210 + * This method is an internal version of Resolve that does not query PAC. 1.211 + * It performs all of the built-in processing, and reports back to the 1.212 + * caller with either the proxy info result or a flag to instruct the 1.213 + * caller to use PAC instead. 1.214 + * 1.215 + * @param channel 1.216 + * The channel to test. 1.217 + * @param info 1.218 + * Information about the URI's protocol. 1.219 + * @param flags 1.220 + * The flags passed to either the resolve or the asyncResolve method. 1.221 + * @param usePAC 1.222 + * If this flag is set upon return, then PAC should be queried to 1.223 + * resolve the proxy info. 1.224 + * @param result 1.225 + * The resulting proxy info or null. 1.226 + */ 1.227 + NS_HIDDEN_(nsresult) Resolve_Internal(nsIChannel *channel, 1.228 + const nsProtocolInfo &info, 1.229 + uint32_t flags, 1.230 + bool *usePAC, 1.231 + nsIProxyInfo **result); 1.232 + 1.233 + /** 1.234 + * This method applies the registered filters to the given proxy info 1.235 + * list, and returns a possibly modified list. 1.236 + * 1.237 + * @param channel 1.238 + * The channel corresponding to this proxy info list. 1.239 + * @param info 1.240 + * Information about the URI's protocol. 1.241 + * @param proxyInfo 1.242 + * The proxy info list to be modified. This is an inout param. 1.243 + */ 1.244 + NS_HIDDEN_(void) ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info, 1.245 + nsIProxyInfo **proxyInfo); 1.246 + 1.247 + /** 1.248 + * This method is a simple wrapper around ApplyFilters that takes the 1.249 + * proxy info list inout param as a nsCOMPtr. 1.250 + */ 1.251 + inline void ApplyFilters(nsIChannel *channel, const nsProtocolInfo &info, 1.252 + nsCOMPtr<nsIProxyInfo> &proxyInfo) 1.253 + { 1.254 + nsIProxyInfo *pi = nullptr; 1.255 + proxyInfo.swap(pi); 1.256 + ApplyFilters(channel, info, &pi); 1.257 + proxyInfo.swap(pi); 1.258 + } 1.259 + 1.260 + /** 1.261 + * This method prunes out disabled and disallowed proxies from a given 1.262 + * proxy info list. 1.263 + * 1.264 + * @param info 1.265 + * Information about the URI's protocol. 1.266 + * @param proxyInfo 1.267 + * The proxy info list to be modified. This is an inout param. 1.268 + */ 1.269 + NS_HIDDEN_(void) PruneProxyInfo(const nsProtocolInfo &info, 1.270 + nsIProxyInfo **proxyInfo); 1.271 + 1.272 + /** 1.273 + * This method populates mHostFiltersArray from the given string. 1.274 + * 1.275 + * @param hostFilters 1.276 + * A "no-proxy-for" exclusion list. 1.277 + */ 1.278 + NS_HIDDEN_(void) LoadHostFilters(const char *hostFilters); 1.279 + 1.280 + /** 1.281 + * This method checks the given URI against mHostFiltersArray. 1.282 + * 1.283 + * @param uri 1.284 + * The URI to test. 1.285 + * @param defaultPort 1.286 + * The default port for the given URI. 1.287 + * 1.288 + * @return True if the URI can use the specified proxy. 1.289 + */ 1.290 + NS_HIDDEN_(bool) CanUseProxy(nsIURI *uri, int32_t defaultPort); 1.291 + 1.292 + /** 1.293 + * Disable Prefetch in the DNS service if a proxy is in use. 1.294 + * 1.295 + * @param aProxy 1.296 + * The proxy information 1.297 + */ 1.298 + NS_HIDDEN_(void) MaybeDisableDNSPrefetch(nsIProxyInfo *aProxy); 1.299 + 1.300 +private: 1.301 + nsresult SetupPACThread(); 1.302 + nsresult ResetPACThread(); 1.303 + 1.304 +public: 1.305 + // The Sun Forte compiler and others implement older versions of the 1.306 + // C++ standard's rules on access and nested classes. These structs 1.307 + // need to be public in order to deal with those compilers. 1.308 + 1.309 + struct HostInfoIP { 1.310 + uint16_t family; 1.311 + uint16_t mask_len; 1.312 + PRIPv6Addr addr; // possibly IPv4-mapped address 1.313 + }; 1.314 + 1.315 + struct HostInfoName { 1.316 + char *host; 1.317 + uint32_t host_len; 1.318 + }; 1.319 + 1.320 +protected: 1.321 + 1.322 + // simplified array of filters defined by this struct 1.323 + struct HostInfo { 1.324 + bool is_ipaddr; 1.325 + int32_t port; 1.326 + union { 1.327 + HostInfoIP ip; 1.328 + HostInfoName name; 1.329 + }; 1.330 + 1.331 + HostInfo() 1.332 + : is_ipaddr(false) 1.333 + { /* other members intentionally uninitialized */ } 1.334 + ~HostInfo() { 1.335 + if (!is_ipaddr && name.host) 1.336 + nsMemory::Free(name.host); 1.337 + } 1.338 + }; 1.339 + 1.340 + // An instance of this struct is allocated for each registered 1.341 + // nsIProtocolProxyFilter and each nsIProtocolProxyChannelFilter. 1.342 + struct FilterLink { 1.343 + struct FilterLink *next; 1.344 + uint32_t position; 1.345 + nsCOMPtr<nsIProtocolProxyFilter> filter; 1.346 + nsCOMPtr<nsIProtocolProxyChannelFilter> channelFilter; 1.347 + FilterLink(uint32_t p, nsIProtocolProxyFilter *f) 1.348 + : next(nullptr), position(p), filter(f), channelFilter(nullptr) {} 1.349 + FilterLink(uint32_t p, nsIProtocolProxyChannelFilter *cf) 1.350 + : next(nullptr), position(p), filter(nullptr), channelFilter(cf) {} 1.351 + // Chain deletion to simplify cleaning up the filter links 1.352 + ~FilterLink() { if (next) delete next; } 1.353 + }; 1.354 + 1.355 +private: 1.356 + // Private methods to insert and remove FilterLinks from the FilterLink chain. 1.357 + nsresult InsertFilterLink(FilterLink *link, uint32_t position); 1.358 + nsresult RemoveFilterLink(nsISupports *givenObject); 1.359 + 1.360 +protected: 1.361 + // Indicates if local hosts (plain hostnames, no dots) should use the proxy 1.362 + bool mFilterLocalHosts; 1.363 + 1.364 + // Holds an array of HostInfo objects 1.365 + nsTArray<nsAutoPtr<HostInfo> > mHostFiltersArray; 1.366 + 1.367 + // Points to the start of a sorted by position, singly linked list 1.368 + // of FilterLink objects. 1.369 + FilterLink *mFilters; 1.370 + 1.371 + uint32_t mProxyConfig; 1.372 + 1.373 + nsCString mHTTPProxyHost; 1.374 + int32_t mHTTPProxyPort; 1.375 + 1.376 + nsCString mFTPProxyHost; 1.377 + int32_t mFTPProxyPort; 1.378 + 1.379 + nsCString mHTTPSProxyHost; 1.380 + int32_t mHTTPSProxyPort; 1.381 + 1.382 + nsCString mSOCKSProxyHost; 1.383 + int32_t mSOCKSProxyPort; 1.384 + int32_t mSOCKSProxyVersion; 1.385 + bool mSOCKSProxyRemoteDNS; 1.386 + nsCString mSOCKSProxyUsername; 1.387 + nsCString mSOCKSProxyPassword; 1.388 + 1.389 + nsRefPtr<nsPACMan> mPACMan; // non-null if we are using PAC 1.390 + nsCOMPtr<nsISystemProxySettings> mSystemProxySettings; 1.391 + 1.392 + PRTime mSessionStart; 1.393 + nsFailedProxyTable mFailedProxies; 1.394 + int32_t mFailedProxyTimeout; 1.395 + 1.396 +private: 1.397 + nsresult AsyncResolveInternal(nsIChannel *channel, uint32_t flags, 1.398 + nsIProtocolProxyCallback *callback, 1.399 + nsICancelable **result, 1.400 + bool isSyncOK); 1.401 + 1.402 +}; 1.403 + 1.404 +NS_DEFINE_STATIC_IID_ACCESSOR(nsProtocolProxyService, NS_PROTOCOL_PROXY_SERVICE_IMPL_CID) 1.405 + 1.406 +#endif // !nsProtocolProxyService_h__