Back to index

lightning-sunbird  0.9+nobinonly
nsHttpHandler.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.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@netscape.com> (original author)
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 nsHttpHandler_h__
00040 #define nsHttpHandler_h__
00041 
00042 #include "nsHttp.h"
00043 #include "nsHttpAuthCache.h"
00044 #include "nsHttpConnection.h"
00045 #include "nsHttpConnectionMgr.h"
00046 
00047 #include "nsXPIDLString.h"
00048 #include "nsString.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsWeakReference.h"
00051 #include "nsVoidArray.h"
00052 
00053 #include "nsIHttpProtocolHandler.h"
00054 #include "nsIProtocolProxyService.h"
00055 #include "nsIIOService.h"
00056 #include "nsIObserver.h"
00057 #include "nsIObserverService.h"
00058 #include "nsIProxyObjectManager.h"
00059 #include "nsIStreamConverterService.h"
00060 #include "nsICacheSession.h"
00061 #include "nsIEventQueueService.h"
00062 #include "nsICookieService.h"
00063 #include "nsIIDNService.h"
00064 #include "nsITimer.h"
00065 
00066 class nsHttpConnectionInfo;
00067 class nsHttpHeaderArray;
00068 class nsHttpTransaction;
00069 class nsAHttpTransaction;
00070 class nsIHttpChannel;
00071 class nsIPrefBranch;
00072 
00073 //-----------------------------------------------------------------------------
00074 // nsHttpHandler - protocol handler for HTTP and HTTPS
00075 //-----------------------------------------------------------------------------
00076 
00077 class nsHttpHandler : public nsIHttpProtocolHandler
00078                     , public nsIObserver
00079                     , public nsSupportsWeakReference
00080 {
00081 public:
00082     NS_DECL_ISUPPORTS
00083     NS_DECL_NSIPROTOCOLHANDLER
00084     NS_DECL_NSIPROXIEDPROTOCOLHANDLER
00085     NS_DECL_NSIHTTPPROTOCOLHANDLER
00086     NS_DECL_NSIOBSERVER
00087 
00088     nsHttpHandler();
00089     virtual ~nsHttpHandler();
00090 
00091     nsresult Init();
00092     nsresult AddStandardRequestHeaders(nsHttpHeaderArray *,
00093                                        PRUint8 capabilities,
00094                                        PRBool useProxy);
00095     PRBool   IsAcceptableEncoding(const char *encoding);
00096 
00097     const nsAFlatCString &UserAgent();
00098 
00099     nsHttpVersion  HttpVersion()             { return mHttpVersion; }
00100     nsHttpVersion  ProxyHttpVersion()        { return mProxyHttpVersion; }
00101     PRUint8        ReferrerLevel()           { return mReferrerLevel; }
00102     PRBool         SendSecureXSiteReferrer() { return mSendSecureXSiteReferrer; }
00103     PRUint8        RedirectionLimit()        { return mRedirectionLimit; }
00104     PRUint16       IdleTimeout()             { return mIdleTimeout; }
00105     PRUint16       MaxRequestAttempts()      { return mMaxRequestAttempts; }
00106     const char    *DefaultSocketType()       { return mDefaultSocketType.get(); /* ok to return null */ }
00107     nsIIDNService *IDNConverter()            { return mIDNConverter; }
00108     PRUint32       PhishyUserPassLength()    { return mPhishyUserPassLength; }
00109     
00110     PRBool         IsPersistentHttpsCachingEnabled() { return mEnablePersistentHttpsCaching; }
00111 
00112     nsHttpAuthCache     *AuthCache() { return &mAuthCache; }
00113     nsHttpConnectionMgr *ConnMgr()   { return mConnMgr; }
00114 
00115     // cache support
00116     nsresult GetCacheSession(nsCacheStoragePolicy, nsICacheSession **);
00117     PRUint32 GenerateUniqueID() { return ++mLastUniqueID; }
00118     PRUint32 SessionStartTime() { return mSessionStartTime; }
00119 
00120     //
00121     // Connection management methods:
00122     //
00123     // - the handler only owns idle connections; it does not own active
00124     //   connections.
00125     //
00126     // - the handler keeps a count of active connections to enforce the
00127     //   steady-state max-connections pref.
00128     // 
00129 
00130     // Called to kick-off a new transaction, by default the transaction
00131     // will be put on the pending transaction queue if it cannot be 
00132     // initiated at this time.  Callable from any thread.
00133     nsresult InitiateTransaction(nsHttpTransaction *trans, PRInt32 priority)
00134     {
00135         return mConnMgr->AddTransaction(trans, priority);
00136     }
00137 
00138     // Called to change the priority of an existing transaction that has
00139     // already been initiated.
00140     nsresult RescheduleTransaction(nsHttpTransaction *trans, PRInt32 priority)
00141     {
00142         return mConnMgr->RescheduleTransaction(trans, priority);
00143     }
00144 
00145     // Called to cancel a transaction, which may or may not be assigned to
00146     // a connection.  Callable from any thread.
00147     nsresult CancelTransaction(nsHttpTransaction *trans, nsresult reason)
00148     {
00149         return mConnMgr->CancelTransaction(trans, reason);
00150     }
00151 
00152     // Called when a connection is done processing a transaction.  Callable
00153     // from any thread.
00154     nsresult ReclaimConnection(nsHttpConnection *conn)
00155     {
00156         return mConnMgr->ReclaimConnection(conn);
00157     }
00158 
00159     nsresult ProcessPendingQ(nsHttpConnectionInfo *cinfo)
00160     {
00161         return mConnMgr->ProcessPendingQ(cinfo);
00162     }
00163 
00164     nsresult GetSocketThreadEventTarget(nsIEventTarget **target)
00165     {
00166         return mConnMgr->GetSocketThreadEventTarget(target);
00167     }
00168 
00169     //
00170     // The HTTP handler caches pointers to specific XPCOM services, and
00171     // provides the following helper routines for accessing those services:
00172     //
00173     nsresult GetCurrentEventQ(nsIEventQueue **);
00174     nsresult GetStreamConverterService(nsIStreamConverterService **);
00175     nsresult GetIOService(nsIIOService** service);
00176     nsICookieService * GetCookieService(); // not addrefed
00177 
00178     // Called by the channel before writing a request
00179     void OnModifyRequest(nsIHttpChannel *chan)
00180     {
00181         NotifyObservers(chan, NS_HTTP_ON_MODIFY_REQUEST_TOPIC);
00182     }
00183 
00184     // Called by the channel once headers are available
00185     void OnExamineResponse(nsIHttpChannel *chan)
00186     {
00187         NotifyObservers(chan, NS_HTTP_ON_EXAMINE_RESPONSE_TOPIC);
00188     }
00189 
00190     // Called by the channel once headers have been merged with cached headers
00191     void OnExamineMergedResponse(nsIHttpChannel *chan)
00192     {
00193         NotifyObservers(chan, NS_HTTP_ON_EXAMINE_MERGED_RESPONSE_TOPIC);
00194     }
00195 
00196     // Called by channels before a redirect happens. This notifies both the
00197     // channel's and the global redirect observers.
00198     nsresult OnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan,
00199                                PRUint32 flags);
00200 private:
00201 
00202     //
00203     // Useragent/prefs helper methods
00204     //
00205     void     BuildUserAgent();
00206     void     InitUserAgentComponents();
00207     void     PrefsChanged(nsIPrefBranch *prefs, const char *pref);
00208 
00209     nsresult SetAccept(const char *);
00210     nsresult SetAcceptLanguages(const char *);
00211     nsresult SetAcceptEncodings(const char *);
00212     nsresult SetAcceptCharsets(const char *);
00213 
00214     nsresult InitConnectionMgr();
00215     void     StartPruneDeadConnectionsTimer();
00216     void     StopPruneDeadConnectionsTimer();
00217 
00218     void     NotifyObservers(nsIHttpChannel *chan, const char *event);
00219 
00220 private:
00221 
00222     // cached services
00223     nsCOMPtr<nsIIOService>              mIOService;
00224     nsCOMPtr<nsIEventQueueService>      mEventQueueService;
00225     nsCOMPtr<nsIStreamConverterService> mStreamConvSvc;
00226     nsCOMPtr<nsIObserverService>        mObserverService;
00227     nsCOMPtr<nsICookieService>          mCookieService;
00228     nsCOMPtr<nsIIDNService>             mIDNConverter;
00229     nsCOMPtr<nsITimer>                  mTimer;
00230 
00231     // the authentication credentials cache
00232     nsHttpAuthCache mAuthCache;
00233 
00234     // the connection manager
00235     nsHttpConnectionMgr *mConnMgr;
00236 
00237     //
00238     // prefs
00239     //
00240 
00241     PRUint8  mHttpVersion;
00242     PRUint8  mProxyHttpVersion;
00243     PRUint8  mCapabilities;
00244     PRUint8  mProxyCapabilities;
00245     PRUint8  mReferrerLevel;
00246 
00247     PRUint16 mIdleTimeout;
00248     PRUint16 mMaxRequestAttempts;
00249     PRUint16 mMaxRequestDelay;
00250 
00251     PRUint16 mMaxConnections;
00252     PRUint8  mMaxConnectionsPerServer;
00253     PRUint8  mMaxPersistentConnectionsPerServer;
00254     PRUint8  mMaxPersistentConnectionsPerProxy;
00255     PRUint8  mMaxPipelinedRequests;
00256 
00257     PRUint8  mRedirectionLimit;
00258 
00259     // we'll warn the user if we load an URL containing a userpass field
00260     // unless its length is less than this threshold.  this warning is
00261     // intended to protect the user against spoofing attempts that use
00262     // the userpass field of the URL to obscure the actual origin server.
00263     PRUint8  mPhishyUserPassLength;
00264 
00265     nsCString mAccept;
00266     nsCString mAcceptLanguages;
00267     nsCString mAcceptEncodings;
00268     nsCString mAcceptCharsets;
00269 
00270     nsXPIDLCString mDefaultSocketType;
00271 
00272     // cache support
00273     nsCOMPtr<nsICacheSession> mCacheSession_ANY;
00274     nsCOMPtr<nsICacheSession> mCacheSession_MEM;
00275     PRUint32                  mLastUniqueID;
00276     PRUint32                  mSessionStartTime;
00277 
00278     // useragent components
00279     nsXPIDLCString mAppName;
00280     nsXPIDLCString mAppVersion;
00281     nsCString      mPlatform;
00282     nsCString      mOscpu;
00283     nsXPIDLCString mSecurity;
00284     nsCString      mLanguage;
00285     nsCString      mMisc;
00286     nsXPIDLCString mVendor;
00287     nsXPIDLCString mVendorSub;
00288     nsXPIDLCString mVendorComment;
00289     nsXPIDLCString mProduct;
00290     nsXPIDLCString mProductSub;
00291     nsXPIDLCString mProductComment;
00292     nsCString      mExtraUA;
00293 
00294     nsCString      mUserAgent;
00295     nsXPIDLCString mUserAgentOverride;
00296     PRPackedBool   mUserAgentIsDirty; // true if mUserAgent should be rebuilt
00297 
00298     PRPackedBool   mUseCache;
00299     // mSendSecureXSiteReferrer: default is false, 
00300     // if true allow referrer headers between secure non-matching hosts
00301     PRPackedBool   mSendSecureXSiteReferrer;
00302 
00303     // Persistent HTTPS caching flag
00304     PRPackedBool   mEnablePersistentHttpsCaching;
00305 };
00306 
00307 //-----------------------------------------------------------------------------
00308 
00309 extern nsHttpHandler *gHttpHandler;
00310 
00311 //-----------------------------------------------------------------------------
00312 // nsHttpsHandler - thin wrapper to distinguish the HTTP handler from the
00313 //                  HTTPS handler (even though they share the same impl).
00314 //-----------------------------------------------------------------------------
00315 
00316 class nsHttpsHandler : public nsIHttpProtocolHandler
00317                      , public nsSupportsWeakReference
00318 {
00319 public:
00320     // we basically just want to override GetScheme and GetDefaultPort...
00321     // all other methods should be forwarded to the nsHttpHandler instance.
00322     
00323     NS_DECL_ISUPPORTS
00324     NS_DECL_NSIPROTOCOLHANDLER
00325     NS_FORWARD_NSIPROXIEDPROTOCOLHANDLER (gHttpHandler->)
00326     NS_FORWARD_NSIHTTPPROTOCOLHANDLER    (gHttpHandler->)
00327 
00328     nsHttpsHandler() { }
00329     virtual ~nsHttpsHandler() { }
00330 
00331     nsresult Init();
00332 };
00333 
00334 #endif // nsHttpHandler_h__