2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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 the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Darin Fisher <darin@netscape.com> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef nsHttpConnection_h__
|
|
|
|
#define nsHttpConnection_h__
|
|
|
|
|
|
|
|
#include "nsHttp.h"
|
|
|
|
#include "nsHttpConnectionInfo.h"
|
|
|
|
#include "nsAHttpTransaction.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2010-12-16 06:35:12 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2011-03-31 12:38:30 -07:00
|
|
|
#include "prinrval.h"
|
2011-12-13 07:55:50 -08:00
|
|
|
#include "SpdySession.h"
|
2012-03-22 16:39:31 -07:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2011-03-31 12:38:30 -07:00
|
|
|
#include "nsIEventTarget.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
class nsHttpRequestHead;
|
|
|
|
class nsHttpResponseHead;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsHttpConnection - represents a connection to a HTTP server (or proxy)
|
|
|
|
//
|
|
|
|
// NOTE: this objects lives on the socket thread only. it should not be
|
|
|
|
// accessed from any other thread.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class nsHttpConnection : public nsAHttpSegmentReader
|
|
|
|
, public nsAHttpSegmentWriter
|
|
|
|
, public nsIInputStreamCallback
|
|
|
|
, public nsIOutputStreamCallback
|
|
|
|
, public nsITransportEventSink
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSAHTTPSEGMENTREADER
|
|
|
|
NS_DECL_NSAHTTPSEGMENTWRITER
|
|
|
|
NS_DECL_NSIINPUTSTREAMCALLBACK
|
|
|
|
NS_DECL_NSIOUTPUTSTREAMCALLBACK
|
|
|
|
NS_DECL_NSITRANSPORTEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
|
|
|
nsHttpConnection();
|
|
|
|
virtual ~nsHttpConnection();
|
|
|
|
|
|
|
|
// Initialize the connection:
|
|
|
|
// info - specifies the connection parameters.
|
|
|
|
// maxHangTime - limits the amount of time this connection can spend on a
|
|
|
|
// single transaction before it should no longer be kept
|
|
|
|
// alive. a value of 0xffff indicates no limit.
|
2011-03-31 12:38:30 -07:00
|
|
|
nsresult Init(nsHttpConnectionInfo *info, PRUint16 maxHangTime,
|
|
|
|
nsISocketTransport *, nsIAsyncInputStream *,
|
|
|
|
nsIAsyncOutputStream *, nsIInterfaceRequestor *,
|
2012-03-22 16:39:31 -07:00
|
|
|
nsIEventTarget *, PRIntervalTime);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Activate causes the given transaction to be processed on this
|
2011-12-13 07:55:50 -08:00
|
|
|
// connection. It fails if there is already an existing transaction unless
|
|
|
|
// a multiplexing protocol such as SPDY is being used
|
|
|
|
nsresult Activate(nsAHttpTransaction *, PRUint8 caps, PRInt32 pri);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Close the underlying socket transport.
|
|
|
|
void Close(nsresult reason);
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// XXX document when these are ok to call
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
bool SupportsPipelining();
|
2011-12-13 07:55:50 -08:00
|
|
|
bool IsKeepAlive() { return mUsingSpdy ||
|
|
|
|
(mKeepAliveMask && mKeepAlive); }
|
2011-09-28 23:19:26 -07:00
|
|
|
bool CanReuse(); // can this connection be reused?
|
2011-12-13 07:55:50 -08:00
|
|
|
bool CanDirectlyActivate();
|
2010-10-14 17:28:43 -07:00
|
|
|
|
|
|
|
// Returns time in seconds for how long connection can be reused.
|
|
|
|
PRUint32 TimeToLive();
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
void DontReuse();
|
2007-03-22 10:30:00 -07:00
|
|
|
void DropTransport() { DontReuse(); mSocketTransport = 0; }
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
bool IsProxyConnectInProgress()
|
|
|
|
{
|
|
|
|
return mProxyConnectInProgress;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool LastTransactionExpectedNoContent()
|
2011-01-27 10:34:39 -08:00
|
|
|
{
|
|
|
|
return mLastTransactionExpectedNoContent;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void SetLastTransactionExpectedNoContent(bool val)
|
2011-01-27 10:34:39 -08:00
|
|
|
{
|
|
|
|
mLastTransactionExpectedNoContent = val;
|
|
|
|
}
|
|
|
|
|
2011-12-01 06:19:41 -08:00
|
|
|
nsISocketTransport *Transport() { return mSocketTransport; }
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAHttpTransaction *Transaction() { return mTransaction; }
|
|
|
|
nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; }
|
|
|
|
|
|
|
|
// nsAHttpConnection compatible methods (non-virtual):
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult OnHeadersAvailable(nsAHttpTransaction *, nsHttpRequestHead *, nsHttpResponseHead *, bool *reset);
|
2007-03-22 10:30:00 -07:00
|
|
|
void CloseTransaction(nsAHttpTransaction *, nsresult reason);
|
|
|
|
void GetConnectionInfo(nsHttpConnectionInfo **ci) { NS_IF_ADDREF(*ci = mConnInfo); }
|
2011-05-19 16:43:37 -07:00
|
|
|
nsresult TakeTransport(nsISocketTransport **,
|
|
|
|
nsIAsyncInputStream **,
|
|
|
|
nsIAsyncOutputStream **);
|
2007-03-22 10:30:00 -07:00
|
|
|
void GetSecurityInfo(nsISupports **);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsPersistent() { return IsKeepAlive(); }
|
|
|
|
bool IsReused();
|
2011-03-31 12:38:30 -07:00
|
|
|
void SetIsReusedAfter(PRUint32 afterMilliseconds);
|
2012-02-23 17:58:43 -08:00
|
|
|
void SetIdleTimeout(PRIntervalTime val) {mIdleTimeout = val;}
|
2011-05-19 16:43:37 -07:00
|
|
|
nsresult PushBack(const char *data, PRUint32 length);
|
2012-01-25 21:15:26 -08:00
|
|
|
nsresult ResumeSend();
|
|
|
|
nsresult ResumeRecv();
|
2011-04-08 11:36:47 -07:00
|
|
|
PRInt64 MaxBytesRead() {return mMaxBytesRead;}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static NS_METHOD ReadFromStream(nsIInputStream *, void *, const char *,
|
|
|
|
PRUint32, PRUint32, PRUint32 *);
|
|
|
|
|
2011-05-16 06:32:37 -07:00
|
|
|
// When a persistent connection is in the connection manager idle
|
|
|
|
// connection pool, the nsHttpConnection still reads errors and hangups
|
|
|
|
// on the socket so that it can be proactively released if the server
|
|
|
|
// initiates a termination. Only call on socket thread.
|
|
|
|
void BeginIdleMonitoring();
|
|
|
|
void EndIdleMonitoring();
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
bool UsingSpdy() { return mUsingSpdy; }
|
|
|
|
|
2012-05-03 17:32:12 -07:00
|
|
|
// true when connection SSL NPN phase is complete and we know
|
|
|
|
// authoritatively whether UsingSpdy() or not.
|
|
|
|
bool ReportedNPN() { return mReportedSpdy; }
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// When the connection is active this is called every 1 second
|
2012-02-23 17:56:57 -08:00
|
|
|
void ReadTimeoutTick(PRIntervalTime now);
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsAHttpTransaction::Classifier Classification() { return mClassification; }
|
|
|
|
void Classify(nsAHttpTransaction::Classifier newclass)
|
|
|
|
{
|
|
|
|
mClassification = newclass;
|
|
|
|
}
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// When the connection is active this is called every second
|
|
|
|
void ReadTimeoutTick();
|
|
|
|
|
2012-05-14 10:25:28 -07:00
|
|
|
PRInt64 BytesWritten() { return mTotalBytesWritten; }
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
// called to cause the underlying socket to start speaking SSL
|
|
|
|
nsresult ProxyStartSSL();
|
|
|
|
|
|
|
|
nsresult OnTransactionDone(nsresult reason);
|
|
|
|
nsresult OnSocketWritable();
|
|
|
|
nsresult OnSocketReadable();
|
|
|
|
|
2011-05-13 10:53:27 -07:00
|
|
|
nsresult SetupProxyConnect();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-23 17:58:43 -08:00
|
|
|
PRIntervalTime IdleTime();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsAlive();
|
|
|
|
bool SupportsPipelining(nsHttpResponseHead *);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
// Makes certain the SSL handshake is complete and NPN negotiation
|
|
|
|
// has had a chance to happen
|
|
|
|
bool EnsureNPNComplete();
|
|
|
|
void SetupNPN(PRUint8 caps);
|
|
|
|
|
|
|
|
// Inform the connection manager of any SPDY Alternate-Protocol
|
|
|
|
// redirections
|
|
|
|
void HandleAlternateProtocol(nsHttpResponseHead *);
|
|
|
|
|
2012-02-15 13:41:18 -08:00
|
|
|
// Start the Spdy transaction handler when NPN indicates spdy/2
|
|
|
|
void StartSpdy();
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
// Directly Add a transaction to an active connection for SPDY
|
|
|
|
nsresult AddTransaction(nsAHttpTransaction *, PRInt32);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsISocketTransport> mSocketTransport;
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mSocketIn;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mSocketOut;
|
|
|
|
|
|
|
|
nsresult mSocketInCondition;
|
|
|
|
nsresult mSocketOutCondition;
|
|
|
|
|
2011-05-13 10:53:27 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mProxyConnectStream;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIInputStream> mRequestStream;
|
|
|
|
|
2011-03-31 12:38:30 -07:00
|
|
|
// mTransaction only points to the HTTP Transaction callbacks if the
|
|
|
|
// transaction is open, otherwise it is null.
|
|
|
|
nsRefPtr<nsAHttpTransaction> mTransaction;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
|
|
|
|
nsCOMPtr<nsIEventTarget> mCallbackTarget;
|
|
|
|
|
|
|
|
nsRefPtr<nsHttpConnectionInfo> mConnInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
PRIntervalTime mLastReadTime;
|
2012-02-23 17:58:43 -08:00
|
|
|
PRIntervalTime mMaxHangTime; // max download time before dropping keep-alive status
|
|
|
|
PRIntervalTime mIdleTimeout; // value of keep-alive: timeout=
|
2011-03-31 12:38:30 -07:00
|
|
|
PRIntervalTime mConsiderReusedAfterInterval;
|
|
|
|
PRIntervalTime mConsiderReusedAfterEpoch;
|
2011-04-08 11:36:47 -07:00
|
|
|
PRInt64 mCurrentBytesRead; // data read per activation
|
|
|
|
PRInt64 mMaxBytesRead; // max read in 1 activation
|
2011-12-13 07:55:50 -08:00
|
|
|
PRInt64 mTotalBytesRead; // total data read
|
2012-05-14 10:25:28 -07:00
|
|
|
PRInt64 mTotalBytesWritten; // does not include CONNECT tunnel
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-19 16:43:37 -07:00
|
|
|
nsRefPtr<nsIAsyncInputStream> mInputOverflow;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
PRIntervalTime mRtt;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mKeepAlive;
|
|
|
|
bool mKeepAliveMask;
|
|
|
|
bool mSupportsPipelining;
|
|
|
|
bool mIsReused;
|
|
|
|
bool mCompletedProxyConnect;
|
|
|
|
bool mLastTransactionExpectedNoContent;
|
|
|
|
bool mIdleMonitoring;
|
2012-03-22 16:39:31 -07:00
|
|
|
bool mProxyConnectInProgress;
|
2011-12-13 07:55:50 -08:00
|
|
|
|
|
|
|
// The number of <= HTTP/1.1 transactions performed on this connection. This
|
|
|
|
// excludes spdy transactions.
|
|
|
|
PRUint32 mHttp1xTransactionCount;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
// Keep-Alive: max="mRemainingConnectionUses" provides the number of future
|
|
|
|
// transactions (including the current one) that the server expects to allow
|
|
|
|
// on this persistent connection.
|
|
|
|
PRUint32 mRemainingConnectionUses;
|
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsAHttpTransaction::Classifier mClassification;
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
// SPDY related
|
|
|
|
bool mNPNComplete;
|
|
|
|
bool mSetupNPNCalled;
|
|
|
|
bool mUsingSpdy;
|
|
|
|
nsRefPtr<mozilla::net::SpdySession> mSpdySession;
|
|
|
|
PRInt32 mPriority;
|
|
|
|
bool mReportedSpdy;
|
|
|
|
|
|
|
|
// mUsingSpdy is cleared when mSpdySession is freed, this is permanent
|
|
|
|
bool mEverUsedSpdy;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsHttpConnection_h__
|