Back to index

lightning-sunbird  0.9+nobinonly
nsHttpChannel.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:set et cin ts=4 sw=4 sts=4: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Darin Fisher <darin@netscape.com> (original author)
00025  *   Christian Biesinger <cbiesinger@web.de>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #ifndef nsHttpChannel_h__
00042 #define nsHttpChannel_h__
00043 
00044 #include "nsHttpTransaction.h"
00045 #include "nsHttpRequestHead.h"
00046 #include "nsHttpAuthCache.h"
00047 #include "nsInputStreamPump.h"
00048 #include "nsXPIDLString.h"
00049 #include "nsAutoPtr.h"
00050 #include "nsCOMPtr.h"
00051 #include "nsInt64.h"
00052 
00053 #include "nsHashPropertyBag.h"
00054 
00055 #include "nsIHttpChannel.h"
00056 #include "nsIHttpChannelInternal.h"
00057 #include "nsIHttpHeaderVisitor.h"
00058 #include "nsIHttpEventSink.h"
00059 #include "nsIChannelEventSink.h"
00060 #include "nsIStreamListener.h"
00061 #include "nsIIOService.h"
00062 #include "nsIURI.h"
00063 #include "nsILoadGroup.h"
00064 #include "nsIInterfaceRequestor.h"
00065 #include "nsIInterfaceRequestorUtils.h"
00066 #include "nsIInputStream.h"
00067 #include "nsIProgressEventSink.h"
00068 #include "nsICachingChannel.h"
00069 #include "nsICacheEntryDescriptor.h"
00070 #include "nsICacheListener.h"
00071 #include "nsIEncodedChannel.h"
00072 #include "nsITransport.h"
00073 #include "nsIUploadChannel.h"
00074 #include "nsIStringEnumerator.h"
00075 #include "nsIOutputStream.h"
00076 #include "nsIAsyncInputStream.h"
00077 #include "nsIPrompt.h"
00078 #include "nsIResumableChannel.h"
00079 #include "nsISupportsPriority.h"
00080 #include "nsIProtocolProxyCallback.h"
00081 #include "nsICancelable.h"
00082 
00083 class nsHttpResponseHead;
00084 class nsAHttpConnection;
00085 class nsIHttpAuthenticator;
00086 class nsProxyInfo;
00087 
00088 //-----------------------------------------------------------------------------
00089 // nsHttpChannel
00090 //-----------------------------------------------------------------------------
00091 
00092 class nsHttpChannel : public nsHashPropertyBag
00093                     , public nsIHttpChannel
00094                     , public nsIHttpChannelInternal
00095                     , public nsIStreamListener
00096                     , public nsICachingChannel
00097                     , public nsIUploadChannel
00098                     , public nsICacheListener
00099                     , public nsIEncodedChannel
00100                     , public nsITransportEventSink
00101                     , public nsIResumableChannel
00102                     , public nsISupportsPriority
00103                     , public nsIProtocolProxyCallback
00104 {
00105 public:
00106     NS_DECL_ISUPPORTS_INHERITED
00107     NS_DECL_NSIREQUEST
00108     NS_DECL_NSICHANNEL
00109     NS_DECL_NSIHTTPCHANNEL
00110     NS_DECL_NSIREQUESTOBSERVER
00111     NS_DECL_NSISTREAMLISTENER
00112     NS_DECL_NSICACHINGCHANNEL
00113     NS_DECL_NSIUPLOADCHANNEL
00114     NS_DECL_NSICACHELISTENER
00115     NS_DECL_NSIENCODEDCHANNEL
00116     NS_DECL_NSIHTTPCHANNELINTERNAL
00117     NS_DECL_NSITRANSPORTEVENTSINK
00118     NS_DECL_NSIRESUMABLECHANNEL
00119     NS_DECL_NSISUPPORTSPRIORITY
00120     NS_DECL_NSIPROTOCOLPROXYCALLBACK
00121 
00122     nsHttpChannel();
00123     virtual ~nsHttpChannel();
00124 
00125     nsresult Init(nsIURI *uri,
00126                   PRUint8 capabilities,
00127                   nsProxyInfo* proxyInfo);
00128 
00129 public: /* internal; workaround lame compilers */ 
00130     typedef void (nsHttpChannel:: *nsAsyncCallback)(void);
00131 
00132 private:
00133 
00134     // Helper function to simplify getting notification callbacks.
00135     template <class T>
00136     void GetCallback(nsCOMPtr<T> &aResult)
00137     {
00138         NS_QueryNotificationCallbacks(mCallbacks, mLoadGroup, NS_GET_IID(T),
00139                                       getter_AddRefs(aResult));
00140     }
00141 
00142     //
00143     // AsyncCall may be used to call a member function asynchronously.
00144     //
00145     struct nsAsyncCallEvent : PLEvent
00146     {
00147         nsAsyncCallback mFuncPtr;
00148     };
00149 
00150     nsresult AsyncCall(nsAsyncCallback funcPtr);
00151 
00152     PRBool   RequestIsConditional();
00153     nsresult Connect(PRBool firstTime = PR_TRUE);
00154     nsresult AsyncAbort(nsresult status);
00155     nsresult SetupTransaction();
00156     void     AddCookiesToRequest();
00157     void     ApplyContentConversions();
00158     nsresult CallOnStartRequest();
00159     nsresult ProcessResponse();
00160     nsresult ProcessNormal();
00161     nsresult ProcessNotModified();
00162     nsresult ProcessRedirection(PRUint32 httpStatus);
00163     nsresult ProcessAuthentication(PRUint32 httpStatus);
00164     PRBool   ResponseWouldVary();
00165 
00166     // redirection specific methods
00167     void     HandleAsyncRedirect();
00168     void     HandleAsyncNotModified();
00169     nsresult PromptTempRedirect();
00170     nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, PRBool preserveMethod);
00171 
00172     // proxy specific methods
00173     nsresult ProxyFailover();
00174     nsresult ReplaceWithProxy(nsIProxyInfo *);
00175     nsresult ResolveProxy();
00176 
00177     // cache specific methods
00178     nsresult OpenCacheEntry(PRBool offline, PRBool *delayed);
00179     nsresult GenerateCacheKey(nsACString &key);
00180     nsresult UpdateExpirationTime();
00181     nsresult CheckCache();
00182     nsresult ReadFromCache();
00183     nsresult CloseCacheEntry(nsresult status);
00184     nsresult InitCacheEntry();
00185     nsresult StoreAuthorizationMetaData();
00186     nsresult FinalizeCacheEntry();
00187     nsresult InstallCacheListener(PRUint32 offset = 0);
00188 
00189     // byte range request specific methods
00190     nsresult SetupByteRangeRequest(PRUint32 partialLen);
00191     nsresult ProcessPartialContent();
00192     nsresult OnDoneReadingPartialCacheEntry(PRBool *streamDone);
00193 
00194     // auth specific methods
00195     nsresult PrepareForAuthentication(PRBool proxyAuth);
00196     nsresult GenCredsAndSetEntry(nsIHttpAuthenticator *, PRBool proxyAuth, const char *scheme, const char *host, PRInt32 port, const char *dir, const char *realm, const char *challenge, const nsHttpAuthIdentity &ident, nsCOMPtr<nsISupports> &session, char **result);
00197     nsresult GetCredentials(const char *challenges, PRBool proxyAuth, nsAFlatCString &creds);
00198     nsresult GetCredentialsForChallenge(const char *challenge, const char *scheme,  PRBool proxyAuth, nsIHttpAuthenticator *auth, nsAFlatCString &creds);
00199     nsresult GetAuthenticator(const char *challenge, nsCString &scheme, nsIHttpAuthenticator **auth); 
00200     void     ParseRealm(const char *challenge, nsACString &realm);
00201     void     GetIdentityFromURI(PRUint32 authFlags, nsHttpAuthIdentity&);
00202     nsresult PromptForIdentity(const char *scheme, const char *host, PRInt32 port, PRBool proxyAuth, const char *realm, const char *authType, PRUint32 authFlags, nsHttpAuthIdentity &);
00203     PRBool   ConfirmAuth(const nsString &bundleKey, PRBool doYesNoPrompt);
00204     void     CheckForSuperfluousAuth();
00205     void     SetAuthorizationHeader(nsHttpAuthCache *, nsHttpAtom header, const char *scheme, const char *host, PRInt32 port, const char *path, nsHttpAuthIdentity &ident);
00206     void     AddAuthorizationHeaders();
00207     nsresult GetCurrentPath(nsACString &);
00208     void     ClearPasswordManagerEntry(const char *scheme, const char *host, PRInt32 port, const char *realm, const PRUnichar *user);
00209     nsresult DoAuthRetry(nsAHttpConnection *);
00210 
00211     static void *PR_CALLBACK AsyncCall_EventHandlerFunc(PLEvent *);
00212     static void  PR_CALLBACK AsyncCall_EventCleanupFunc(PLEvent *);
00213 
00214 private:
00215     nsCOMPtr<nsIURI>                  mOriginalURI;
00216     nsCOMPtr<nsIURI>                  mURI;
00217     nsCOMPtr<nsIURI>                  mDocumentURI;
00218     nsCOMPtr<nsIStreamListener>       mListener;
00219     nsCOMPtr<nsISupports>             mListenerContext;
00220     nsCOMPtr<nsILoadGroup>            mLoadGroup;
00221     nsCOMPtr<nsISupports>             mOwner;
00222     nsCOMPtr<nsIInterfaceRequestor>   mCallbacks;
00223     nsCOMPtr<nsIProgressEventSink>    mProgressSink;
00224     nsCOMPtr<nsIInputStream>          mUploadStream;
00225     nsCOMPtr<nsIURI>                  mReferrer;
00226     nsCOMPtr<nsISupports>             mSecurityInfo;
00227     nsCOMPtr<nsIEventQueue>           mEventQ;
00228     nsCOMPtr<nsICancelable>           mProxyRequest;
00229 
00230     nsHttpRequestHead                 mRequestHead;
00231     nsHttpResponseHead               *mResponseHead;
00232 
00233     nsRefPtr<nsInputStreamPump>       mTransactionPump;
00234     nsHttpTransaction                *mTransaction;     // hard ref
00235     nsHttpConnectionInfo             *mConnectionInfo;  // hard ref
00236 
00237     nsCString                         mSpec; // ASCII encoded URL spec
00238 
00239     PRUint32                          mLoadFlags;
00240     PRUint32                          mStatus;
00241     nsUint64                          mLogicalOffset;
00242     PRUint8                           mCaps;
00243     PRInt16                           mPriority;
00244 
00245     nsCString                         mContentTypeHint;
00246     nsCString                         mContentCharsetHint;
00247     nsCString                         mUserSetCookieHeader;
00248 
00249     // cache specific data
00250     nsCOMPtr<nsICacheEntryDescriptor> mCacheEntry;
00251     nsRefPtr<nsInputStreamPump>       mCachePump;
00252     nsHttpResponseHead               *mCachedResponseHead;
00253     nsCacheAccessMode                 mCacheAccess;
00254     PRUint32                          mPostID;
00255     PRUint32                          mRequestTime;
00256 
00257     // auth specific data
00258     nsISupports                      *mProxyAuthContinuationState;
00259     nsCString                         mProxyAuthType;
00260     nsISupports                      *mAuthContinuationState;
00261     nsCString                         mAuthType;
00262     nsHttpAuthIdentity                mIdent;
00263     nsHttpAuthIdentity                mProxyIdent;
00264 
00265     // Resumable channel specific data
00266     nsCString                         mEntityID;
00267     PRUint64                          mStartPos;
00268 
00269     // redirection specific data.
00270     PRUint8                           mRedirectionLimit;
00271 
00272     // state flags
00273     PRUint32                          mIsPending                : 1;
00274     PRUint32                          mApplyConversion          : 1;
00275     PRUint32                          mAllowPipelining          : 1;
00276     PRUint32                          mCachedContentIsValid     : 1;
00277     PRUint32                          mCachedContentIsPartial   : 1;
00278     PRUint32                          mResponseHeadersModified  : 1;
00279     PRUint32                          mCanceled                 : 1;
00280     PRUint32                          mTransactionReplaced      : 1;
00281     PRUint32                          mUploadStreamHasHeaders   : 1;
00282     PRUint32                          mAuthRetryPending         : 1;
00283     PRUint32                          mSuppressDefensiveAuth    : 1;
00284     PRUint32                          mResuming                 : 1;
00285     PRUint32                          mOpenedCacheForWriting    : 1;
00286 
00287     class nsContentEncodings : public nsIUTF8StringEnumerator
00288     {
00289     public:
00290         NS_DECL_ISUPPORTS
00291         NS_DECL_NSIUTF8STRINGENUMERATOR
00292 
00293         nsContentEncodings(nsIHttpChannel* aChannel, const char* aEncodingHeader);
00294         virtual ~nsContentEncodings();
00295         
00296     private:
00297         nsresult PrepareForNext(void);
00298         
00299         // We do not own the buffer.  The channel owns it.
00300         const char* mEncodingHeader;
00301         const char* mCurStart;  // points to start of current header
00302         const char* mCurEnd;  // points to end of current header
00303         
00304         // Hold a ref to our channel so that it can't go away and take the
00305         // header with it.
00306         nsCOMPtr<nsIHttpChannel> mChannel;
00307         
00308         PRPackedBool mReady;
00309     };
00310 };
00311 
00312 #endif // nsHttpChannel_h__