Back to index

lightning-sunbird  0.9+nobinonly
nsHttpConnection.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 nsHttpConnection_h__
00040 #define nsHttpConnection_h__
00041 
00042 #include "nsHttp.h"
00043 #include "nsHttpConnectionInfo.h"
00044 #include "nsAHttpConnection.h"
00045 #include "nsAHttpTransaction.h"
00046 #include "nsXPIDLString.h"
00047 #include "nsCOMPtr.h"
00048 #include "prlock.h"
00049 
00050 #include "nsIStreamListener.h"
00051 #include "nsISocketTransport.h"
00052 #include "nsIEventTarget.h"
00053 #include "nsIAsyncInputStream.h"
00054 #include "nsIAsyncOutputStream.h"
00055 #include "nsIInterfaceRequestor.h"
00056 
00057 //-----------------------------------------------------------------------------
00058 // nsHttpConnection - represents a connection to a HTTP server (or proxy)
00059 //
00060 // NOTE: this objects lives on the socket thread only.  it should not be
00061 // accessed from any other thread.
00062 //-----------------------------------------------------------------------------
00063 
00064 class nsHttpConnection : public nsAHttpSegmentReader
00065                        , public nsAHttpSegmentWriter
00066                        , public nsIInputStreamCallback
00067                        , public nsIOutputStreamCallback
00068                        , public nsITransportEventSink
00069                        , public nsIInterfaceRequestor
00070 {
00071 public:
00072     NS_DECL_ISUPPORTS
00073     NS_DECL_NSAHTTPSEGMENTREADER
00074     NS_DECL_NSAHTTPSEGMENTWRITER
00075     NS_DECL_NSIINPUTSTREAMCALLBACK
00076     NS_DECL_NSIOUTPUTSTREAMCALLBACK
00077     NS_DECL_NSITRANSPORTEVENTSINK
00078     NS_DECL_NSIINTERFACEREQUESTOR
00079 
00080     nsHttpConnection();
00081     virtual ~nsHttpConnection();
00082 
00083     // Initialize the connection:
00084     //  info        - specifies the connection parameters.
00085     //  maxHangTime - limits the amount of time this connection can spend on a
00086     //                single transaction before it should no longer be kept 
00087     //                alive.  a value of 0xffff indicates no limit.
00088     nsresult Init(nsHttpConnectionInfo *info, PRUint16 maxHangTime);
00089 
00090     // Activate causes the given transaction to be processed on this
00091     // connection.  It fails if there is already an existing transaction.
00092     nsresult Activate(nsAHttpTransaction *, PRUint8 caps);
00093 
00094     // Close the underlying socket transport.
00095     void Close(nsresult reason);
00096 
00097     //-------------------------------------------------------------------------
00098     // XXX document when these are ok to call
00099 
00100     PRBool   SupportsPipelining() { return mSupportsPipelining; }
00101     PRBool   IsKeepAlive() { return mKeepAliveMask && mKeepAlive; }
00102     PRBool   CanReuse();   // can this connection be reused?
00103     void     DontReuse()   { mKeepAliveMask = PR_FALSE;
00104                              mKeepAlive = PR_FALSE;
00105                              mIdleTimeout = 0; }
00106     void     DropTransport() { DontReuse(); mSocketTransport = 0; }
00107 
00108     nsAHttpTransaction   *Transaction()    { return mTransaction; }
00109     nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; }
00110 
00111     // nsAHttpConnection compatible methods (non-virtual):
00112     nsresult OnHeadersAvailable(nsAHttpTransaction *, nsHttpRequestHead *, nsHttpResponseHead *, PRBool *reset);
00113     void     CloseTransaction(nsAHttpTransaction *, nsresult reason);
00114     void     GetConnectionInfo(nsHttpConnectionInfo **ci) { NS_IF_ADDREF(*ci = mConnInfo); }
00115     void     GetSecurityInfo(nsISupports **);
00116     PRBool   IsPersistent() { return IsKeepAlive(); }
00117     PRBool   IsReused() { return mIsReused; }
00118     nsresult PushBack(const char *data, PRUint32 length) { NS_NOTREACHED("PushBack"); return NS_ERROR_UNEXPECTED; }
00119     nsresult ResumeSend();
00120     nsresult ResumeRecv();
00121 
00122     static NS_METHOD ReadFromStream(nsIInputStream *, void *, const char *,
00123                                     PRUint32, PRUint32, PRUint32 *);
00124 
00125 private:
00126     // called to cause the underlying socket to start speaking SSL
00127     nsresult ProxyStartSSL();
00128 
00129     nsresult CreateTransport();
00130     nsresult OnTransactionDone(nsresult reason);
00131     nsresult OnSocketWritable();
00132     nsresult OnSocketReadable();
00133 
00134     nsresult SetupSSLProxyConnect();
00135 
00136     PRBool   IsAlive();
00137     PRBool   SupportsPipelining(nsHttpResponseHead *);
00138     
00139 private:
00140     nsCOMPtr<nsISocketTransport>    mSocketTransport;
00141     nsCOMPtr<nsIAsyncInputStream>   mSocketIn;
00142     nsCOMPtr<nsIAsyncOutputStream>  mSocketOut;
00143 
00144     nsresult                        mSocketInCondition;
00145     nsresult                        mSocketOutCondition;
00146 
00147     nsCOMPtr<nsIInputStream>        mSSLProxyConnectStream;
00148     nsCOMPtr<nsIInputStream>        mRequestStream;
00149 
00150     nsAHttpTransaction             *mTransaction; // hard ref
00151     nsHttpConnectionInfo           *mConnInfo;    // hard ref
00152 
00153     PRLock                         *mLock;
00154 
00155     PRUint32                        mLastReadTime;
00156     PRUint16                        mMaxHangTime;    // max download time before dropping keep-alive status
00157     PRUint16                        mIdleTimeout;    // value of keep-alive: timeout=
00158 
00159     PRPackedBool                    mKeepAlive;
00160     PRPackedBool                    mKeepAliveMask;
00161     PRPackedBool                    mSupportsPipelining;
00162     PRPackedBool                    mIsReused;
00163     PRPackedBool                    mCompletedSSLConnect;
00164 };
00165 
00166 #endif // nsHttpConnection_h__