Back to index

lightning-sunbird  0.9+nobinonly
nsHttpTransaction.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 nsHttpTransaction_h__
00040 #define nsHttpTransaction_h__
00041 
00042 #include "nsHttp.h"
00043 #include "nsHttpHeaderArray.h"
00044 #include "nsAHttpTransaction.h"
00045 #include "nsAHttpConnection.h"
00046 #include "nsCOMPtr.h"
00047 #include "nsInt64.h"
00048 
00049 #include "nsIPipe.h"
00050 #include "nsIInputStream.h"
00051 #include "nsIOutputStream.h"
00052 #include "nsIInterfaceRequestor.h"
00053 #include "nsISocketTransportService.h"
00054 #include "nsITransport.h"
00055 #include "nsIEventQueue.h"
00056 
00057 //-----------------------------------------------------------------------------
00058 
00059 class nsHttpTransaction;
00060 class nsHttpRequestHead;
00061 class nsHttpResponseHead;
00062 class nsHttpChunkedDecoder;
00063 class nsIHttpActivityObserver;
00064 
00065 //-----------------------------------------------------------------------------
00066 // nsHttpTransaction represents a single HTTP transaction.  It is thread-safe,
00067 // intended to run on the socket thread.
00068 //-----------------------------------------------------------------------------
00069 
00070 class nsHttpTransaction : public nsAHttpTransaction
00071                         , public nsIInputStreamCallback
00072                         , public nsIOutputStreamCallback
00073 {
00074 public:
00075     NS_DECL_ISUPPORTS
00076     NS_DECL_NSAHTTPTRANSACTION
00077     NS_DECL_NSIINPUTSTREAMCALLBACK
00078     NS_DECL_NSIOUTPUTSTREAMCALLBACK
00079 
00080     nsHttpTransaction();
00081     virtual ~nsHttpTransaction();
00082 
00083     //
00084     // called to initialize the transaction
00085     // 
00086     // @param caps
00087     //        the transaction capabilities (see nsHttp.h)
00088     // @param connInfo
00089     //        the connection type for this transaction.
00090     // @param reqHeaders
00091     //        the request header struct
00092     // @param reqBody
00093     //        the request body (POST or PUT data stream)
00094     // @param reqBodyIncludesHeaders
00095     //        fun stuff to support NPAPI plugins.
00096     // @param eventQ
00097     //        the event queue were notifications should be sent.
00098     // @param callbacks
00099     //        the notification callbacks to be given to PSM.
00100     // @param responseBody
00101     //        the input stream that will contain the response data.  async
00102     //        wait on this input stream for data.  on first notification,
00103     //        headers should be available (check transaction status).
00104     //
00105     nsresult Init(PRUint8                caps,
00106                   nsHttpConnectionInfo  *connInfo,
00107                   nsHttpRequestHead     *reqHeaders,
00108                   nsIInputStream        *reqBody,
00109                   PRBool                 reqBodyIncludesHeaders,
00110                   nsIEventQueue         *consumerEventQ,
00111                   nsIInterfaceRequestor *callbacks,
00112                   nsITransportEventSink *eventsink,
00113                   nsIAsyncInputStream  **responseBody);
00114 
00115     // attributes
00116     PRUint8                Caps()           { return mCaps; }
00117     nsHttpConnectionInfo  *ConnectionInfo() { return mConnInfo; }
00118     nsHttpRequestHead     *RequestHead()    { return mRequestHead; }
00119     nsHttpResponseHead    *ResponseHead()   { return mHaveAllHeaders ? mResponseHead : nsnull; }
00120     nsISupports           *SecurityInfo()   { return mSecurityInfo; }
00121 
00122     nsIInterfaceRequestor *Callbacks()      { return mCallbacks; } 
00123     nsIEventQueue         *ConsumerEventQ() { return mConsumerEventQ; }
00124     nsAHttpConnection     *Connection()     { return mConnection; }
00125 
00126     // Called to take ownership of the response headers; the transaction
00127     // will drop any reference to the response headers after this call.
00128     nsHttpResponseHead *TakeResponseHead();
00129 
00130     // Called to find out if the transaction generated a complete response.
00131     PRBool ResponseIsComplete() { return mResponseIsComplete; }
00132 
00133     void   SetSSLConnectFailed() { mSSLConnectFailed = PR_TRUE; }
00134     PRBool    SSLConnectFailed() { return mSSLConnectFailed; }
00135 
00136     // These methods may only be used by the connection manager.
00137     void    SetPriority(PRInt32 priority) { mPriority = priority; }
00138     PRInt32    Priority()                 { return mPriority; }
00139 
00140 private:
00141     nsresult Restart();
00142     void     ParseLine(char *line);
00143     nsresult ParseLineSegment(char *seg, PRUint32 len);
00144     nsresult ParseHead(char *, PRUint32 count, PRUint32 *countRead);
00145     nsresult HandleContentStart();
00146     nsresult HandleContent(char *, PRUint32 count, PRUint32 *contentRead, PRUint32 *contentRemaining);
00147     nsresult ProcessData(char *, PRUint32, PRUint32 *);
00148     void     DeleteSelfOnConsumerThread();
00149 
00150     static void *PR_CALLBACK TransportStatus_Handler(PLEvent *);
00151     static void  PR_CALLBACK TransportStatus_Cleanup(PLEvent *);
00152     static void *PR_CALLBACK DeleteThis_Handler(PLEvent *);
00153     static void  PR_CALLBACK DeleteThis_Cleanup(PLEvent *);
00154 
00155     static NS_METHOD ReadRequestSegment(nsIInputStream *, void *, const char *,
00156                                         PRUint32, PRUint32, PRUint32 *);
00157     static NS_METHOD WritePipeSegment(nsIOutputStream *, void *, char *,
00158                                       PRUint32, PRUint32, PRUint32 *);
00159 
00160 private:
00161     nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
00162     nsCOMPtr<nsITransportEventSink> mTransportSink;
00163     nsCOMPtr<nsIEventQueue>         mConsumerEventQ;
00164     nsCOMPtr<nsISupports>           mSecurityInfo;
00165     nsCOMPtr<nsIAsyncInputStream>   mPipeIn;
00166     nsCOMPtr<nsIAsyncOutputStream>  mPipeOut;
00167 
00168     nsCOMPtr<nsISupports>             mChannel;
00169     nsCOMPtr<nsIHttpActivityObserver> mActivityDistributor;
00170 
00171     nsCString                       mReqHeaderBuf;    // flattened request headers
00172     nsCOMPtr<nsIInputStream>        mRequestStream;
00173     PRUint32                        mRequestSize;
00174 
00175     nsAHttpConnection              *mConnection;      // hard ref
00176     nsHttpConnectionInfo           *mConnInfo;        // hard ref
00177     nsHttpRequestHead              *mRequestHead;     // weak ref
00178     nsHttpResponseHead             *mResponseHead;    // hard ref
00179 
00180     nsAHttpSegmentReader           *mReader;
00181     nsAHttpSegmentWriter           *mWriter;
00182 
00183     nsCString                       mLineBuf;         // may contain a partial line
00184 
00185     nsInt64                         mContentLength;   // equals -1 if unknown
00186     nsInt64                         mContentRead;     // count of consumed content bytes
00187 
00188     nsHttpChunkedDecoder           *mChunkedDecoder;
00189 
00190     nsresult                        mStatus;
00191 
00192     PRInt16                         mPriority;
00193 
00194     PRUint16                        mRestartCount;        // the number of times this transaction has been restarted
00195     PRUint8                         mCaps;
00196 
00197     // state flags
00198     PRUint32                        mClosed             : 1;
00199     PRUint32                        mDestroying         : 1;
00200     PRUint32                        mConnected          : 1;
00201     PRUint32                        mHaveStatusLine     : 1;
00202     PRUint32                        mHaveAllHeaders     : 1;
00203     PRUint32                        mTransactionDone    : 1;
00204     PRUint32                        mResponseIsComplete : 1;
00205     PRUint32                        mDidContentStart    : 1;
00206     PRUint32                        mNoContent          : 1; // expecting an empty entity body
00207     PRUint32                        mSentData           : 1;
00208     PRUint32                        mReceivedData       : 1;
00209     PRUint32                        mStatusEventPending : 1;
00210     PRUint32                        mHasRequestBody     : 1;
00211     PRUint32                        mSSLConnectFailed   : 1;
00212 
00213     // mClosed           := transaction has been explicitly closed
00214     // mTransactionDone  := transaction ran to completion or was interrupted
00215     // mResponseComplete := transaction ran to completion
00216 };
00217 
00218 #endif // nsHttpTransaction_h__