Back to index

lightning-sunbird  0.9+nobinonly
nsProtocolProxyService.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Malcolm Smith <malsmith@cs.rmit.edu.au>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038  
00039 #ifndef nsProtocolProxyService_h__
00040 #define nsProtocolProxyService_h__
00041 
00042 #include "plevent.h"
00043 #include "nsString.h"
00044 #include "nsCOMPtr.h"
00045 #include "nsAutoPtr.h"
00046 #include "nsVoidArray.h"
00047 #include "nsIPrefBranch.h"
00048 #include "nsPIProtocolProxyService.h"
00049 #include "nsIProtocolProxyFilter.h"
00050 #include "nsIProxyAutoConfig.h"
00051 #include "nsIProxyInfo.h"
00052 #include "nsIObserver.h"
00053 #include "nsDataHashtable.h"
00054 #include "nsHashKeys.h"
00055 #include "nsPACMan.h"
00056 #include "prtime.h"
00057 #include "prmem.h"
00058 #include "prio.h"
00059 
00060 typedef nsDataHashtable<nsCStringHashKey, PRUint32> nsFailedProxyTable;
00061 
00062 class nsProxyInfo;
00063 struct nsProtocolInfo;
00064 
00065 class nsProtocolProxyService : public nsPIProtocolProxyService
00066                              , public nsIObserver
00067 {
00068 public:
00069     NS_DECL_ISUPPORTS
00070     NS_DECL_NSPIPROTOCOLPROXYSERVICE
00071     NS_DECL_NSIPROTOCOLPROXYSERVICE
00072     NS_DECL_NSIOBSERVER
00073 
00074     nsProtocolProxyService() NS_HIDDEN;
00075 
00076     NS_HIDDEN_(nsresult) Init();
00077 
00078 protected:
00079     friend class nsAsyncResolveRequest;
00080 
00081     ~nsProtocolProxyService() NS_HIDDEN;
00082 
00093     NS_HIDDEN_(void) PrefsChanged(nsIPrefBranch *prefs, const char *name);
00094 
00108     NS_HIDDEN_(const char *) ExtractProxyInfo(const char *proxy,
00109                                               nsProxyInfo **result);
00110 
00120     NS_HIDDEN_(void) ProcessPACString(const nsCString &pacString,
00121                                       nsIProxyInfo **result);
00122 
00132     NS_HIDDEN_(void) GetProxyKey(nsProxyInfo *pi, nsCString &result);
00133 
00137     NS_HIDDEN_(PRUint32) SecondsSinceSessionStart();
00138 
00145     NS_HIDDEN_(void) EnableProxy(nsProxyInfo *pi);
00146 
00153     NS_HIDDEN_(void) DisableProxy(nsProxyInfo *pi);
00154 
00163     NS_HIDDEN_(PRBool) IsProxyDisabled(nsProxyInfo *pi);
00164 
00176     NS_HIDDEN_(nsresult) GetProtocolInfo(nsIURI *uri, nsProtocolInfo *result);
00177 
00197     NS_HIDDEN_(nsresult) NewProxyInfo_Internal(const char *type,
00198                                                const nsACString &host,
00199                                                PRInt32 port,
00200                                                PRUint32 flags,
00201                                                PRUint32 timeout,
00202                                                nsIProxyInfo *next,
00203                                                nsIProxyInfo **result);
00204 
00221     NS_HIDDEN_(nsresult) Resolve_Internal(nsIURI *uri,
00222                                           const nsProtocolInfo &info,
00223                                           PRBool *usePAC, 
00224                                           nsIProxyInfo **result);
00225 
00237     NS_HIDDEN_(void) ApplyFilters(nsIURI *uri, const nsProtocolInfo &info,
00238                                   nsIProxyInfo **proxyInfo);
00239 
00244     inline void ApplyFilters(nsIURI *uri, const nsProtocolInfo &info,
00245                              nsCOMPtr<nsIProxyInfo> &proxyInfo)
00246     {
00247       nsIProxyInfo *pi = nsnull;
00248       proxyInfo.swap(pi);
00249       ApplyFilters(uri, info, &pi);
00250       proxyInfo.swap(pi);
00251     }
00252 
00262     NS_HIDDEN_(void) PruneProxyInfo(const nsProtocolInfo &info,
00263                                     nsIProxyInfo **proxyInfo);
00264 
00271     NS_HIDDEN_(void) LoadHostFilters(const char *hostFilters);
00272 
00283     NS_HIDDEN_(PRBool) CanUseProxy(nsIURI *uri, PRInt32 defaultPort);
00284 
00285     static PRBool PR_CALLBACK CleanupFilterArray(void *aElement, void *aData);
00286     static void*  PR_CALLBACK HandlePACLoadEvent(PLEvent* aEvent);
00287     static void   PR_CALLBACK DestroyPACLoadEvent(PLEvent* aEvent);
00288 
00289 public:
00290     // The Sun Forte compiler and others implement older versions of the
00291     // C++ standard's rules on access and nested classes.  These structs
00292     // need to be public in order to deal with those compilers.
00293 
00294     struct HostInfoIP {
00295         PRUint16   family;
00296         PRUint16   mask_len;
00297         PRIPv6Addr addr; // possibly IPv4-mapped address
00298     };
00299 
00300     struct HostInfoName {
00301         char    *host;
00302         PRUint32 host_len;
00303     };
00304 
00305 protected:
00306 
00307     enum ProxyConfig {
00308         eProxyConfig_Direct,
00309         eProxyConfig_Manual,
00310         eProxyConfig_PAC,
00311         eProxyConfig_Direct4x,
00312         eProxyConfig_WPAD,
00313         eProxyConfig_Last
00314     };
00315 
00316     // simplified array of filters defined by this struct
00317     struct HostInfo {
00318         PRBool  is_ipaddr;
00319         PRInt32 port;
00320         union {
00321             HostInfoIP   ip;
00322             HostInfoName name;
00323         };
00324 
00325         HostInfo()
00326             : is_ipaddr(PR_FALSE)
00327             { /* other members intentionally uninitialized */ }
00328        ~HostInfo() {
00329             if (!is_ipaddr && name.host)
00330                 nsMemory::Free(name.host);
00331         }
00332     };
00333 
00334     // This structure is allocated for each registered nsIProtocolProxyFilter.
00335     struct FilterLink {
00336       struct FilterLink                *next;
00337       PRUint32                          position;
00338       nsCOMPtr<nsIProtocolProxyFilter>  filter;
00339 
00340       FilterLink(PRUint32 p, nsIProtocolProxyFilter *f)
00341         : next(nsnull), position(p), filter(f) {}
00342 
00343       // Chain deletion to simplify cleaning up the filter links
00344       ~FilterLink() { if (next) delete next; }
00345     };
00346 
00347     // Holds an array of HostInfo objects
00348     nsVoidArray                  mHostFiltersArray;
00349 
00350     // Points to the start of a sorted by position, singly linked list
00351     // of FilterLink objects.
00352     FilterLink                  *mFilters;
00353 
00354     ProxyConfig                  mProxyConfig;
00355 
00356     nsCString                    mHTTPProxyHost;
00357     PRInt32                      mHTTPProxyPort;
00358 
00359     nsCString                    mFTPProxyHost;
00360     PRInt32                      mFTPProxyPort;
00361 
00362     nsCString                    mGopherProxyHost;
00363     PRInt32                      mGopherProxyPort;
00364 
00365     nsCString                    mHTTPSProxyHost;
00366     PRInt32                      mHTTPSProxyPort;
00367     
00368     nsCString                    mSOCKSProxyHost;
00369     PRInt32                      mSOCKSProxyPort;
00370     PRInt32                      mSOCKSProxyVersion;
00371     PRBool                       mSOCKSProxyRemoteDNS;
00372 
00373     nsRefPtr<nsPACMan>           mPACMan;  // non-null if we are using PAC
00374 
00375     PRTime                       mSessionStart;
00376     nsFailedProxyTable           mFailedProxies;
00377     PRInt32                      mFailedProxyTimeout;
00378 };
00379 
00380 #endif // !nsProtocolProxyService_h__