1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/http/nsHttpConnection.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,302 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsHttpConnection_h__ 1.10 +#define nsHttpConnection_h__ 1.11 + 1.12 +#include "nsHttpConnectionInfo.h" 1.13 +#include "nsAHttpTransaction.h" 1.14 +#include "nsCOMPtr.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsProxyRelease.h" 1.17 +#include "prinrval.h" 1.18 + 1.19 +#include "nsIAsyncInputStream.h" 1.20 +#include "nsIAsyncOutputStream.h" 1.21 +#include "nsIInterfaceRequestor.h" 1.22 +#include "nsITimer.h" 1.23 + 1.24 +class nsISocketTransport; 1.25 + 1.26 +namespace mozilla { 1.27 +namespace net { 1.28 + 1.29 +class nsHttpHandler; 1.30 +class ASpdySession; 1.31 + 1.32 +//----------------------------------------------------------------------------- 1.33 +// nsHttpConnection - represents a connection to a HTTP server (or proxy) 1.34 +// 1.35 +// NOTE: this objects lives on the socket thread only. it should not be 1.36 +// accessed from any other thread. 1.37 +//----------------------------------------------------------------------------- 1.38 + 1.39 +class nsHttpConnection : public nsAHttpSegmentReader 1.40 + , public nsAHttpSegmentWriter 1.41 + , public nsIInputStreamCallback 1.42 + , public nsIOutputStreamCallback 1.43 + , public nsITransportEventSink 1.44 + , public nsIInterfaceRequestor 1.45 +{ 1.46 +public: 1.47 + NS_DECL_THREADSAFE_ISUPPORTS 1.48 + NS_DECL_NSAHTTPSEGMENTREADER 1.49 + NS_DECL_NSAHTTPSEGMENTWRITER 1.50 + NS_DECL_NSIINPUTSTREAMCALLBACK 1.51 + NS_DECL_NSIOUTPUTSTREAMCALLBACK 1.52 + NS_DECL_NSITRANSPORTEVENTSINK 1.53 + NS_DECL_NSIINTERFACEREQUESTOR 1.54 + 1.55 + nsHttpConnection(); 1.56 + virtual ~nsHttpConnection(); 1.57 + 1.58 + // Initialize the connection: 1.59 + // info - specifies the connection parameters. 1.60 + // maxHangTime - limits the amount of time this connection can spend on a 1.61 + // single transaction before it should no longer be kept 1.62 + // alive. a value of 0xffff indicates no limit. 1.63 + nsresult Init(nsHttpConnectionInfo *info, uint16_t maxHangTime, 1.64 + nsISocketTransport *, nsIAsyncInputStream *, 1.65 + nsIAsyncOutputStream *, nsIInterfaceRequestor *, 1.66 + PRIntervalTime); 1.67 + 1.68 + // Activate causes the given transaction to be processed on this 1.69 + // connection. It fails if there is already an existing transaction unless 1.70 + // a multiplexing protocol such as SPDY is being used 1.71 + nsresult Activate(nsAHttpTransaction *, uint32_t caps, int32_t pri); 1.72 + 1.73 + // Close the underlying socket transport. 1.74 + void Close(nsresult reason); 1.75 + 1.76 + //------------------------------------------------------------------------- 1.77 + // XXX document when these are ok to call 1.78 + 1.79 + bool SupportsPipelining(); 1.80 + bool IsKeepAlive() { return mUsingSpdyVersion || 1.81 + (mKeepAliveMask && mKeepAlive); } 1.82 + bool CanReuse(); // can this connection be reused? 1.83 + bool CanDirectlyActivate(); 1.84 + 1.85 + // Returns time in seconds for how long connection can be reused. 1.86 + uint32_t TimeToLive(); 1.87 + 1.88 + void DontReuse(); 1.89 + 1.90 + bool IsProxyConnectInProgress() 1.91 + { 1.92 + return mProxyConnectInProgress; 1.93 + } 1.94 + 1.95 + bool LastTransactionExpectedNoContent() 1.96 + { 1.97 + return mLastTransactionExpectedNoContent; 1.98 + } 1.99 + 1.100 + void SetLastTransactionExpectedNoContent(bool val) 1.101 + { 1.102 + mLastTransactionExpectedNoContent = val; 1.103 + } 1.104 + 1.105 + nsISocketTransport *Transport() { return mSocketTransport; } 1.106 + nsAHttpTransaction *Transaction() { return mTransaction; } 1.107 + nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; } 1.108 + 1.109 + // nsAHttpConnection compatible methods (non-virtual): 1.110 + nsresult OnHeadersAvailable(nsAHttpTransaction *, nsHttpRequestHead *, nsHttpResponseHead *, bool *reset); 1.111 + void CloseTransaction(nsAHttpTransaction *, nsresult reason); 1.112 + void GetConnectionInfo(nsHttpConnectionInfo **ci) { NS_IF_ADDREF(*ci = mConnInfo); } 1.113 + nsresult TakeTransport(nsISocketTransport **, 1.114 + nsIAsyncInputStream **, 1.115 + nsIAsyncOutputStream **); 1.116 + void GetSecurityInfo(nsISupports **); 1.117 + bool IsPersistent() { return IsKeepAlive(); } 1.118 + bool IsReused(); 1.119 + void SetIsReusedAfter(uint32_t afterMilliseconds); 1.120 + nsresult PushBack(const char *data, uint32_t length); 1.121 + nsresult ResumeSend(); 1.122 + nsresult ResumeRecv(); 1.123 + int64_t MaxBytesRead() {return mMaxBytesRead;} 1.124 + uint8_t GetLastHttpResponseVersion() { return mLastHttpResponseVersion; } 1.125 + 1.126 + friend class nsHttpConnectionForceRecv; 1.127 + nsresult ForceRecv(); 1.128 + 1.129 + static NS_METHOD ReadFromStream(nsIInputStream *, void *, const char *, 1.130 + uint32_t, uint32_t, uint32_t *); 1.131 + 1.132 + // When a persistent connection is in the connection manager idle 1.133 + // connection pool, the nsHttpConnection still reads errors and hangups 1.134 + // on the socket so that it can be proactively released if the server 1.135 + // initiates a termination. Only call on socket thread. 1.136 + void BeginIdleMonitoring(); 1.137 + void EndIdleMonitoring(); 1.138 + 1.139 + bool UsingSpdy() { return !!mUsingSpdyVersion; } 1.140 + uint8_t GetSpdyVersion() { return mUsingSpdyVersion; } 1.141 + bool EverUsedSpdy() { return mEverUsedSpdy; } 1.142 + PRIntervalTime Rtt() { return mRtt; } 1.143 + 1.144 + // true when connection SSL NPN phase is complete and we know 1.145 + // authoritatively whether UsingSpdy() or not. 1.146 + bool ReportedNPN() { return mReportedSpdy; } 1.147 + 1.148 + // When the connection is active this is called up to once every 1 second 1.149 + // return the interval (in seconds) that the connection next wants to 1.150 + // have this invoked. It might happen sooner depending on the needs of 1.151 + // other connections. 1.152 + uint32_t ReadTimeoutTick(PRIntervalTime now); 1.153 + 1.154 + // For Active and Idle connections, this will be called when 1.155 + // mTCPKeepaliveTransitionTimer fires, to check if the TCP keepalive config 1.156 + // should move from short-lived (fast-detect) to long-lived. 1.157 + static void UpdateTCPKeepalive(nsITimer *aTimer, void *aClosure); 1.158 + 1.159 + nsAHttpTransaction::Classifier Classification() { return mClassification; } 1.160 + void Classify(nsAHttpTransaction::Classifier newclass) 1.161 + { 1.162 + mClassification = newclass; 1.163 + } 1.164 + 1.165 + // When the connection is active this is called every second 1.166 + void ReadTimeoutTick(); 1.167 + 1.168 + int64_t BytesWritten() { return mTotalBytesWritten; } 1.169 + 1.170 + void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks); 1.171 + void PrintDiagnostics(nsCString &log); 1.172 + 1.173 + void SetTransactionCaps(uint32_t aCaps) { mTransactionCaps = aCaps; } 1.174 + 1.175 + // IsExperienced() returns true when the connection has started at least one 1.176 + // non null HTTP transaction of any version. 1.177 + bool IsExperienced() { return mExperienced; } 1.178 + 1.179 +private: 1.180 + // Value (set in mTCPKeepaliveConfig) indicates which set of prefs to use. 1.181 + enum TCPKeepaliveConfig { 1.182 + kTCPKeepaliveDisabled = 0, 1.183 + kTCPKeepaliveShortLivedConfig, 1.184 + kTCPKeepaliveLongLivedConfig 1.185 + }; 1.186 + 1.187 + // called to cause the underlying socket to start speaking SSL 1.188 + nsresult ProxyStartSSL(); 1.189 + 1.190 + nsresult OnTransactionDone(nsresult reason); 1.191 + nsresult OnSocketWritable(); 1.192 + nsresult OnSocketReadable(); 1.193 + 1.194 + nsresult SetupProxyConnect(); 1.195 + 1.196 + PRIntervalTime IdleTime(); 1.197 + bool IsAlive(); 1.198 + bool SupportsPipelining(nsHttpResponseHead *); 1.199 + 1.200 + // Makes certain the SSL handshake is complete and NPN negotiation 1.201 + // has had a chance to happen 1.202 + bool EnsureNPNComplete(); 1.203 + void SetupSSL(uint32_t caps); 1.204 + 1.205 + // Start the Spdy transaction handler when NPN indicates spdy/* 1.206 + void StartSpdy(uint8_t versionLevel); 1.207 + 1.208 + // Directly Add a transaction to an active connection for SPDY 1.209 + nsresult AddTransaction(nsAHttpTransaction *, int32_t); 1.210 + 1.211 + // Used to set TCP keepalives for fast detection of dead connections during 1.212 + // an initial period, and slower detection for long-lived connections. 1.213 + nsresult StartShortLivedTCPKeepalives(); 1.214 + nsresult StartLongLivedTCPKeepalives(); 1.215 + nsresult DisableTCPKeepalives(); 1.216 + 1.217 +private: 1.218 + nsCOMPtr<nsISocketTransport> mSocketTransport; 1.219 + nsCOMPtr<nsIAsyncInputStream> mSocketIn; 1.220 + nsCOMPtr<nsIAsyncOutputStream> mSocketOut; 1.221 + 1.222 + nsresult mSocketInCondition; 1.223 + nsresult mSocketOutCondition; 1.224 + 1.225 + nsCOMPtr<nsIInputStream> mProxyConnectStream; 1.226 + nsCOMPtr<nsIInputStream> mRequestStream; 1.227 + 1.228 + // mTransaction only points to the HTTP Transaction callbacks if the 1.229 + // transaction is open, otherwise it is null. 1.230 + nsRefPtr<nsAHttpTransaction> mTransaction; 1.231 + 1.232 + nsRefPtr<nsHttpHandler> mHttpHandler; // keep gHttpHandler alive 1.233 + 1.234 + Mutex mCallbacksLock; 1.235 + nsMainThreadPtrHandle<nsIInterfaceRequestor> mCallbacks; 1.236 + 1.237 + nsRefPtr<nsHttpConnectionInfo> mConnInfo; 1.238 + 1.239 + PRIntervalTime mLastReadTime; 1.240 + PRIntervalTime mLastWriteTime; 1.241 + PRIntervalTime mMaxHangTime; // max download time before dropping keep-alive status 1.242 + PRIntervalTime mIdleTimeout; // value of keep-alive: timeout= 1.243 + PRIntervalTime mConsiderReusedAfterInterval; 1.244 + PRIntervalTime mConsiderReusedAfterEpoch; 1.245 + int64_t mCurrentBytesRead; // data read per activation 1.246 + int64_t mMaxBytesRead; // max read in 1 activation 1.247 + int64_t mTotalBytesRead; // total data read 1.248 + int64_t mTotalBytesWritten; // does not include CONNECT tunnel 1.249 + 1.250 + nsRefPtr<nsIAsyncInputStream> mInputOverflow; 1.251 + 1.252 + PRIntervalTime mRtt; 1.253 + 1.254 + bool mKeepAlive; 1.255 + bool mKeepAliveMask; 1.256 + bool mDontReuse; 1.257 + bool mSupportsPipelining; 1.258 + bool mIsReused; 1.259 + bool mCompletedProxyConnect; 1.260 + bool mLastTransactionExpectedNoContent; 1.261 + bool mIdleMonitoring; 1.262 + bool mProxyConnectInProgress; 1.263 + bool mExperienced; 1.264 + 1.265 + // The number of <= HTTP/1.1 transactions performed on this connection. This 1.266 + // excludes spdy transactions. 1.267 + uint32_t mHttp1xTransactionCount; 1.268 + 1.269 + // Keep-Alive: max="mRemainingConnectionUses" provides the number of future 1.270 + // transactions (including the current one) that the server expects to allow 1.271 + // on this persistent connection. 1.272 + uint32_t mRemainingConnectionUses; 1.273 + 1.274 + nsAHttpTransaction::Classifier mClassification; 1.275 + 1.276 + // SPDY related 1.277 + bool mNPNComplete; 1.278 + bool mSetupSSLCalled; 1.279 + 1.280 + // version level in use, 0 if unused 1.281 + uint8_t mUsingSpdyVersion; 1.282 + 1.283 + nsRefPtr<ASpdySession> mSpdySession; 1.284 + int32_t mPriority; 1.285 + bool mReportedSpdy; 1.286 + 1.287 + // mUsingSpdyVersion is cleared when mSpdySession is freed, this is permanent 1.288 + bool mEverUsedSpdy; 1.289 + 1.290 + // mLastHttpResponseVersion stores the last response's http version seen. 1.291 + uint8_t mLastHttpResponseVersion; 1.292 + 1.293 + // The capabailities associated with the most recent transaction 1.294 + uint32_t mTransactionCaps; 1.295 + 1.296 + bool mResponseTimeoutEnabled; 1.297 + 1.298 + // Flag to indicate connection is in inital keepalive period (fast detect). 1.299 + uint32_t mTCPKeepaliveConfig; 1.300 + nsCOMPtr<nsITimer> mTCPKeepaliveTransitionTimer; 1.301 +}; 1.302 + 1.303 +}} // namespace mozilla::net 1.304 + 1.305 +#endif // nsHttpConnection_h__