/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is Mozilla. * * The Initial Developer of the Original Code is Netscape * Communications. Portions created by Netscape Communications are * Copyright (C) 2001 by Netscape Communications. All * Rights Reserved. * * Contributor(s): * Darin Fisher (original author) */ #ifndef nsHttpChannel_h__ #define nsHttpChannel_h__ #include "nsHttpTransaction.h" #include "nsHttpRequestHead.h" #include "nsHttpAuthCache.h" #include "nsXPIDLString.h" #include "nsCOMPtr.h" #include "nsIHttpChannel.h" #include "nsIHttpChannelInternal.h" #include "nsIHttpHeaderVisitor.h" #include "nsIHttpEventSink.h" #include "nsIStreamListener.h" #include "nsIIOService.h" #include "nsIURI.h" #include "nsILoadGroup.h" #include "nsIInterfaceRequestor.h" #include "nsIInterfaceRequestorUtils.h" #include "nsIInputStream.h" #include "nsIProgressEventSink.h" #include "nsICachingChannel.h" #include "nsICacheEntryDescriptor.h" #include "nsICacheListener.h" #include "nsIEncodedChannel.h" #include "nsITransport.h" #include "nsIUploadChannel.h" #include "nsIStringEnumerator.h" #include "nsIOutputStream.h" #include "nsIAsyncInputStream.h" #include "nsIInputStreamPump.h" #include "nsIPrompt.h" class nsHttpResponseHead; class nsAHttpConnection; class nsIHttpAuthenticator; class nsIProxyInfo; //----------------------------------------------------------------------------- // nsHttpChannel //----------------------------------------------------------------------------- class nsHttpChannel : public nsIHttpChannel , public nsIHttpChannelInternal , public nsIStreamListener , public nsICachingChannel , public nsIUploadChannel , public nsICacheListener , public nsIEncodedChannel , public nsITransportEventSink { public: NS_DECL_ISUPPORTS NS_DECL_NSIREQUEST NS_DECL_NSICHANNEL NS_DECL_NSIHTTPCHANNEL NS_DECL_NSIREQUESTOBSERVER NS_DECL_NSISTREAMLISTENER NS_DECL_NSICACHINGCHANNEL NS_DECL_NSIUPLOADCHANNEL NS_DECL_NSICACHELISTENER NS_DECL_NSIENCODEDCHANNEL NS_DECL_NSIHTTPCHANNELINTERNAL NS_DECL_NSITRANSPORTEVENTSINK nsHttpChannel(); virtual ~nsHttpChannel(); nsresult Init(nsIURI *uri, PRUint8 capabilities, nsIProxyInfo* proxyInfo); public: /* internal; workaround lame compilers */ typedef void (nsHttpChannel:: *nsAsyncCallback)(void); private: // // AsyncCall may be used to call a member function asynchronously. // struct nsAsyncCallEvent : PLEvent { nsAsyncCallback mFuncPtr; }; nsresult AsyncCall(nsAsyncCallback funcPtr); nsresult Connect(PRBool firstTime = PR_TRUE); nsresult AsyncAbort(nsresult status); nsresult SetupTransaction(); void AddCookiesToRequest(); void ApplyContentConversions(); nsresult CallOnStartRequest(); nsresult ProcessResponse(); nsresult ProcessNormal(); nsresult ProcessNotModified(); nsresult ProcessRedirection(PRUint32 httpStatus); nsresult ProcessAuthentication(PRUint32 httpStatus); void GetCallback(const nsIID &aIID, void **aResult); PRBool ResponseWouldVary(); // redirection specific methods void HandleAsyncRedirect(); void HandleAsyncNotModified(); nsresult PromptTempRedirect(); nsresult ProxyFailover(); nsresult SetupReplacementChannel(nsIURI *, nsIChannel *, PRBool preserveMethod); // cache specific methods nsresult OpenCacheEntry(PRBool offline, PRBool *delayed); nsresult GenerateCacheKey(nsACString &key); nsresult UpdateExpirationTime(); nsresult CheckCache(); nsresult ReadFromCache(); nsresult CloseCacheEntry(nsresult status); nsresult InitCacheEntry(); nsresult StoreAuthorizationMetaData(); nsresult FinalizeCacheEntry(); nsresult InstallCacheListener(PRUint32 offset = 0); // byte range request specific methods nsresult SetupByteRangeRequest(PRUint32 partialLen); nsresult ProcessPartialContent(); nsresult OnDoneReadingPartialCacheEntry(PRBool *streamDone); // auth specific methods 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 &session, char **result); nsresult GetCredentials(const char *challenges, PRBool proxyAuth, nsAFlatCString &creds); nsresult GetCredentialsForChallenge(const char *challenge, const char *scheme, PRBool proxyAuth, nsIHttpAuthenticator *auth, nsAFlatCString &creds); nsresult GetAuthenticator(const char *challenge, nsCString &scheme, nsIHttpAuthenticator **auth); void ParseRealm(const char *challenge, nsACString &realm); void GetIdentityFromURI(PRUint32 authFlags, nsHttpAuthIdentity&); nsresult PromptForIdentity(const char *scheme, const char *host, PRInt32 port, PRBool proxyAuth, const char *realm, const char *authType, PRUint32 authFlags, nsHttpAuthIdentity &); PRBool ConfirmAuth(const nsString &bundleKey, PRBool doYesNoPrompt); void CheckForSuperfluousAuth(); void SetAuthorizationHeader(nsHttpAuthCache *, nsHttpAtom header, const char *scheme, const char *host, PRInt32 port, const char *path, nsHttpAuthIdentity &ident); void AddAuthorizationHeaders(); nsresult GetCurrentPath(nsACString &); void ClearPasswordManagerEntry(const char *scheme, const char *host, PRInt32 port, const char *realm, const PRUnichar *user); nsresult DoAuthRetry(nsAHttpConnection *); static void *PR_CALLBACK AsyncCall_EventHandlerFunc(PLEvent *); static void PR_CALLBACK AsyncCall_EventCleanupFunc(PLEvent *); private: nsCOMPtr mOriginalURI; nsCOMPtr mURI; nsCOMPtr mDocumentURI; nsCOMPtr mListener; nsCOMPtr mListenerContext; nsCOMPtr mLoadGroup; nsCOMPtr mOwner; nsCOMPtr mCallbacks; nsCOMPtr mProgressSink; nsCOMPtr mHttpEventSink; nsCOMPtr mUploadStream; nsCOMPtr mReferrer; nsCOMPtr mSecurityInfo; nsCOMPtr mEventQ; nsHttpRequestHead mRequestHead; nsHttpResponseHead *mResponseHead; nsCOMPtr mTransactionPump; nsHttpTransaction *mTransaction; // hard ref nsHttpConnectionInfo *mConnectionInfo; // hard ref nsCString mSpec; // ASCII encoded URL spec PRUint32 mLoadFlags; PRUint32 mStatus; PRUint32 mLogicalOffset; PRUint8 mCaps; nsCString mContentTypeHint; nsCString mContentCharsetHint; // mProperties must be nsISupports, not nsIProperties, due to aggregation nsCOMPtr mProperties; // cache specific data nsCOMPtr mCacheEntry; nsCOMPtr mCachePump; nsHttpResponseHead *mCachedResponseHead; nsCacheAccessMode mCacheAccess; PRUint32 mPostID; PRUint32 mRequestTime; // auth specific data nsISupports *mAuthContinuationState; nsCString mAuthType; nsHttpAuthIdentity mIdent; nsHttpAuthIdentity mProxyIdent; // redirection specific data. PRUint8 mRedirectionLimit; // state flags PRUint32 mIsPending : 1; PRUint32 mApplyConversion : 1; PRUint32 mAllowPipelining : 1; PRUint32 mCachedContentIsValid : 1; PRUint32 mCachedContentIsPartial : 1; PRUint32 mResponseHeadersModified : 1; PRUint32 mCanceled : 1; PRUint32 mTransactionReplaced : 1; PRUint32 mUploadStreamHasHeaders : 1; PRUint32 mAuthRetryPending : 1; PRUint32 mSuppressDefensiveAuth : 1; class nsContentEncodings : public nsIUTF8StringEnumerator { public: NS_DECL_ISUPPORTS NS_DECL_NSIUTF8STRINGENUMERATOR nsContentEncodings(nsIHttpChannel* aChannel, const char* aEncodingHeader); virtual ~nsContentEncodings(); private: nsresult PrepareForNext(void); // We do not own the buffer. The channel owns it. const char* mEncodingHeader; const char* mCurStart; // points to start of current header const char* mCurEnd; // points to end of current header // Hold a ref to our channel so that it can't go away and take the // header with it. nsCOMPtr mChannel; PRPackedBool mReady; }; }; #endif // nsHttpChannel_h__