2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsHttpConnection_h__
|
|
|
|
#define nsHttpConnection_h__
|
|
|
|
|
|
|
|
#include "nsHttpConnectionInfo.h"
|
|
|
|
#include "nsAHttpTransaction.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2010-12-16 06:35:12 -08:00
|
|
|
#include "nsAutoPtr.h"
|
2013-03-12 06:58:00 -07:00
|
|
|
#include "nsProxyRelease.h"
|
2011-03-31 12:38:30 -07:00
|
|
|
#include "prinrval.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
2014-02-06 11:51:38 -08:00
|
|
|
#include "nsITimer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-09-22 20:35:52 -07:00
|
|
|
class nsISocketTransport;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
2014-01-07 14:05:56 -08:00
|
|
|
|
|
|
|
class nsHttpHandler;
|
2013-09-22 20:35:52 -07:00
|
|
|
class ASpdySession;
|
2012-03-22 16:39:31 -07:00
|
|
|
|
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:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
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
|
2013-05-16 06:30:42 -07:00
|
|
|
// single transaction before it should no longer be kept
|
2007-03-22 10:30:00 -07:00
|
|
|
// alive. a value of 0xffff indicates no limit.
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult Init(nsHttpConnectionInfo *info, uint16_t maxHangTime,
|
2011-03-31 12:38:30 -07:00
|
|
|
nsISocketTransport *, nsIAsyncInputStream *,
|
|
|
|
nsIAsyncOutputStream *, nsIInterfaceRequestor *,
|
2012-11-14 08:00:44 -08:00
|
|
|
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
|
2012-11-29 17:34:35 -08:00
|
|
|
nsresult Activate(nsAHttpTransaction *, uint32_t caps, int32_t 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();
|
2012-05-25 14:37:01 -07:00
|
|
|
bool IsKeepAlive() { return mUsingSpdyVersion ||
|
2011-12-13 07:55:50 -08:00
|
|
|
(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.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t TimeToLive();
|
2010-10-14 17:28:43 -07:00
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
void DontReuse();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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();
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetIsReusedAfter(uint32_t afterMilliseconds);
|
2012-02-23 17:58:43 -08:00
|
|
|
void SetIdleTimeout(PRIntervalTime val) {mIdleTimeout = val;}
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult PushBack(const char *data, uint32_t length);
|
2012-01-25 21:15:26 -08:00
|
|
|
nsresult ResumeSend();
|
|
|
|
nsresult ResumeRecv();
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t MaxBytesRead() {return mMaxBytesRead;}
|
2013-07-08 13:35:08 -07:00
|
|
|
uint8_t GetLastHttpResponseVersion() { return mLastHttpResponseVersion; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-28 21:07:03 -07:00
|
|
|
friend class nsHttpConnectionForceRecv;
|
|
|
|
nsresult ForceRecv();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static NS_METHOD ReadFromStream(nsIInputStream *, void *, const char *,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t, uint32_t, uint32_t *);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-05-16 06:30:42 -07:00
|
|
|
// When a persistent connection is in the connection manager idle
|
2011-05-16 06:32:37 -07:00
|
|
|
// 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();
|
|
|
|
|
2012-05-25 14:37:01 -07:00
|
|
|
bool UsingSpdy() { return !!mUsingSpdyVersion; }
|
2013-07-08 13:35:08 -07:00
|
|
|
uint8_t GetSpdyVersion() { return mUsingSpdyVersion; }
|
2012-05-15 13:31:29 -07:00
|
|
|
bool EverUsedSpdy() { return mEverUsedSpdy; }
|
2012-12-10 06:13:55 -08:00
|
|
|
PRIntervalTime Rtt() { return mRtt; }
|
2011-12-13 07:55:50 -08:00
|
|
|
|
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; }
|
|
|
|
|
2014-01-29 23:29:20 -08:00
|
|
|
// When the connection is active this is called up to once every 1 second
|
|
|
|
// return the interval (in seconds) that the connection next wants to
|
|
|
|
// have this invoked. It might happen sooner depending on the needs of
|
|
|
|
// other connections.
|
|
|
|
uint32_t ReadTimeoutTick(PRIntervalTime now);
|
2012-02-23 17:56:57 -08:00
|
|
|
|
2014-02-06 11:51:38 -08:00
|
|
|
// For Active and Idle connections, this will be called when
|
|
|
|
// mTCPKeepaliveTransitionTimer fires, to check if the TCP keepalive config
|
|
|
|
// should move from short-lived (fast-detect) to long-lived.
|
|
|
|
static void UpdateTCPKeepalive(nsITimer *aTimer, void *aClosure);
|
|
|
|
|
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-08-22 08:56:38 -07:00
|
|
|
int64_t BytesWritten() { return mTotalBytesWritten; }
|
2012-05-14 10:25:28 -07:00
|
|
|
|
2012-11-14 08:00:44 -08:00
|
|
|
void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks);
|
2012-07-09 15:07:57 -07:00
|
|
|
void PrintDiagnostics(nsCString &log);
|
|
|
|
|
2013-06-28 13:58:28 -07:00
|
|
|
void SetTransactionCaps(uint32_t aCaps) { mTransactionCaps = aCaps; }
|
2013-07-08 19:53:18 -07:00
|
|
|
|
|
|
|
// IsExperienced() returns true when the connection has started at least one
|
|
|
|
// non null HTTP transaction of any version.
|
|
|
|
bool IsExperienced() { return mExperienced; }
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
2014-02-06 11:51:38 -08:00
|
|
|
// Value (set in mTCPKeepaliveConfig) indicates which set of prefs to use.
|
|
|
|
enum TCPKeepaliveConfig {
|
|
|
|
kTCPKeepaliveDisabled = 0,
|
|
|
|
kTCPKeepaliveShortLivedConfig,
|
|
|
|
kTCPKeepaliveLongLivedConfig
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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 *);
|
2013-05-16 06:30:42 -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();
|
2013-06-28 13:58:28 -07:00
|
|
|
void SetupSSL(uint32_t caps);
|
2011-12-13 07:55:50 -08:00
|
|
|
|
2012-05-25 14:37:01 -07:00
|
|
|
// Start the Spdy transaction handler when NPN indicates spdy/*
|
2012-08-22 08:56:38 -07:00
|
|
|
void StartSpdy(uint8_t versionLevel);
|
2012-02-15 13:41:18 -08:00
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
// Directly Add a transaction to an active connection for SPDY
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult AddTransaction(nsAHttpTransaction *, int32_t);
|
2011-12-13 07:55:50 -08:00
|
|
|
|
2014-02-06 11:51:38 -08:00
|
|
|
// Used to set TCP keepalives for fast detection of dead connections during
|
|
|
|
// an initial period, and slower detection for long-lived connections.
|
|
|
|
nsresult StartShortLivedTCPKeepalives();
|
|
|
|
nsresult StartLongLivedTCPKeepalives();
|
|
|
|
nsresult DisableTCPKeepalives();
|
|
|
|
|
2014-02-25 17:39:08 -08:00
|
|
|
private:
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
|
|
|
|
2013-08-13 12:36:58 -07:00
|
|
|
nsRefPtr<nsHttpHandler> mHttpHandler; // keep gHttpHandler alive
|
|
|
|
|
2014-01-07 14:05:56 -08:00
|
|
|
Mutex mCallbacksLock;
|
2013-03-12 06:58:00 -07:00
|
|
|
nsMainThreadPtrHandle<nsIInterfaceRequestor> mCallbacks;
|
2011-03-31 12:38:30 -07:00
|
|
|
|
|
|
|
nsRefPtr<nsHttpConnectionInfo> mConnInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
PRIntervalTime mLastReadTime;
|
2013-09-04 13:39:25 -07:00
|
|
|
PRIntervalTime mLastWriteTime;
|
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;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t mCurrentBytesRead; // data read per activation
|
|
|
|
int64_t mMaxBytesRead; // max read in 1 activation
|
|
|
|
int64_t mTotalBytesRead; // total data read
|
|
|
|
int64_t 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;
|
2012-06-28 15:06:32 -07:00
|
|
|
bool mDontReuse;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mSupportsPipelining;
|
|
|
|
bool mIsReused;
|
|
|
|
bool mCompletedProxyConnect;
|
|
|
|
bool mLastTransactionExpectedNoContent;
|
|
|
|
bool mIdleMonitoring;
|
2012-03-22 16:39:31 -07:00
|
|
|
bool mProxyConnectInProgress;
|
2013-07-08 19:53:18 -07:00
|
|
|
bool mExperienced;
|
2011-12-13 07:55:50 -08:00
|
|
|
|
|
|
|
// The number of <= HTTP/1.1 transactions performed on this connection. This
|
|
|
|
// excludes spdy transactions.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mHttp1xTransactionCount;
|
2011-12-13 07:55:50 -08:00
|
|
|
|
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.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mRemainingConnectionUses;
|
2012-03-22 16:39:31 -07:00
|
|
|
|
2012-03-22 16:39:31 -07:00
|
|
|
nsAHttpTransaction::Classifier mClassification;
|
|
|
|
|
2011-12-13 07:55:50 -08:00
|
|
|
// SPDY related
|
|
|
|
bool mNPNComplete;
|
2013-06-28 13:58:28 -07:00
|
|
|
bool mSetupSSLCalled;
|
2012-05-25 14:37:01 -07:00
|
|
|
|
|
|
|
// version level in use, 0 if unused
|
2012-08-22 08:56:38 -07:00
|
|
|
uint8_t mUsingSpdyVersion;
|
2012-05-25 14:37:01 -07:00
|
|
|
|
2014-01-07 14:05:56 -08:00
|
|
|
nsRefPtr<ASpdySession> mSpdySession;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mPriority;
|
2011-12-13 07:55:50 -08:00
|
|
|
bool mReportedSpdy;
|
|
|
|
|
2012-05-25 14:37:01 -07:00
|
|
|
// mUsingSpdyVersion is cleared when mSpdySession is freed, this is permanent
|
2011-12-13 07:55:50 -08:00
|
|
|
bool mEverUsedSpdy;
|
2013-06-28 13:58:28 -07:00
|
|
|
|
2013-07-08 13:35:08 -07:00
|
|
|
// mLastHttpResponseVersion stores the last response's http version seen.
|
|
|
|
uint8_t mLastHttpResponseVersion;
|
|
|
|
|
2013-06-28 13:58:28 -07:00
|
|
|
// The capabailities associated with the most recent transaction
|
|
|
|
uint32_t mTransactionCaps;
|
2013-12-18 15:03:48 -08:00
|
|
|
|
|
|
|
bool mResponseTimeoutEnabled;
|
2014-02-06 11:51:38 -08:00
|
|
|
|
|
|
|
// Flag to indicate connection is in inital keepalive period (fast detect).
|
|
|
|
uint32_t mTCPKeepaliveConfig;
|
|
|
|
nsCOMPtr<nsITimer> mTCPKeepaliveTransitionTimer;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2014-01-07 14:05:56 -08:00
|
|
|
}} // namespace mozilla::net
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif // nsHttpConnection_h__
|