Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsHttpConnection_h__ |
michael@0 | 7 | #define nsHttpConnection_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsHttpConnectionInfo.h" |
michael@0 | 10 | #include "nsAHttpTransaction.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "nsProxyRelease.h" |
michael@0 | 14 | #include "prinrval.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nsIAsyncInputStream.h" |
michael@0 | 17 | #include "nsIAsyncOutputStream.h" |
michael@0 | 18 | #include "nsIInterfaceRequestor.h" |
michael@0 | 19 | #include "nsITimer.h" |
michael@0 | 20 | |
michael@0 | 21 | class nsISocketTransport; |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | namespace net { |
michael@0 | 25 | |
michael@0 | 26 | class nsHttpHandler; |
michael@0 | 27 | class ASpdySession; |
michael@0 | 28 | |
michael@0 | 29 | //----------------------------------------------------------------------------- |
michael@0 | 30 | // nsHttpConnection - represents a connection to a HTTP server (or proxy) |
michael@0 | 31 | // |
michael@0 | 32 | // NOTE: this objects lives on the socket thread only. it should not be |
michael@0 | 33 | // accessed from any other thread. |
michael@0 | 34 | //----------------------------------------------------------------------------- |
michael@0 | 35 | |
michael@0 | 36 | class nsHttpConnection : public nsAHttpSegmentReader |
michael@0 | 37 | , public nsAHttpSegmentWriter |
michael@0 | 38 | , public nsIInputStreamCallback |
michael@0 | 39 | , public nsIOutputStreamCallback |
michael@0 | 40 | , public nsITransportEventSink |
michael@0 | 41 | , public nsIInterfaceRequestor |
michael@0 | 42 | { |
michael@0 | 43 | public: |
michael@0 | 44 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 45 | NS_DECL_NSAHTTPSEGMENTREADER |
michael@0 | 46 | NS_DECL_NSAHTTPSEGMENTWRITER |
michael@0 | 47 | NS_DECL_NSIINPUTSTREAMCALLBACK |
michael@0 | 48 | NS_DECL_NSIOUTPUTSTREAMCALLBACK |
michael@0 | 49 | NS_DECL_NSITRANSPORTEVENTSINK |
michael@0 | 50 | NS_DECL_NSIINTERFACEREQUESTOR |
michael@0 | 51 | |
michael@0 | 52 | nsHttpConnection(); |
michael@0 | 53 | virtual ~nsHttpConnection(); |
michael@0 | 54 | |
michael@0 | 55 | // Initialize the connection: |
michael@0 | 56 | // info - specifies the connection parameters. |
michael@0 | 57 | // maxHangTime - limits the amount of time this connection can spend on a |
michael@0 | 58 | // single transaction before it should no longer be kept |
michael@0 | 59 | // alive. a value of 0xffff indicates no limit. |
michael@0 | 60 | nsresult Init(nsHttpConnectionInfo *info, uint16_t maxHangTime, |
michael@0 | 61 | nsISocketTransport *, nsIAsyncInputStream *, |
michael@0 | 62 | nsIAsyncOutputStream *, nsIInterfaceRequestor *, |
michael@0 | 63 | PRIntervalTime); |
michael@0 | 64 | |
michael@0 | 65 | // Activate causes the given transaction to be processed on this |
michael@0 | 66 | // connection. It fails if there is already an existing transaction unless |
michael@0 | 67 | // a multiplexing protocol such as SPDY is being used |
michael@0 | 68 | nsresult Activate(nsAHttpTransaction *, uint32_t caps, int32_t pri); |
michael@0 | 69 | |
michael@0 | 70 | // Close the underlying socket transport. |
michael@0 | 71 | void Close(nsresult reason); |
michael@0 | 72 | |
michael@0 | 73 | //------------------------------------------------------------------------- |
michael@0 | 74 | // XXX document when these are ok to call |
michael@0 | 75 | |
michael@0 | 76 | bool SupportsPipelining(); |
michael@0 | 77 | bool IsKeepAlive() { return mUsingSpdyVersion || |
michael@0 | 78 | (mKeepAliveMask && mKeepAlive); } |
michael@0 | 79 | bool CanReuse(); // can this connection be reused? |
michael@0 | 80 | bool CanDirectlyActivate(); |
michael@0 | 81 | |
michael@0 | 82 | // Returns time in seconds for how long connection can be reused. |
michael@0 | 83 | uint32_t TimeToLive(); |
michael@0 | 84 | |
michael@0 | 85 | void DontReuse(); |
michael@0 | 86 | |
michael@0 | 87 | bool IsProxyConnectInProgress() |
michael@0 | 88 | { |
michael@0 | 89 | return mProxyConnectInProgress; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | bool LastTransactionExpectedNoContent() |
michael@0 | 93 | { |
michael@0 | 94 | return mLastTransactionExpectedNoContent; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void SetLastTransactionExpectedNoContent(bool val) |
michael@0 | 98 | { |
michael@0 | 99 | mLastTransactionExpectedNoContent = val; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | nsISocketTransport *Transport() { return mSocketTransport; } |
michael@0 | 103 | nsAHttpTransaction *Transaction() { return mTransaction; } |
michael@0 | 104 | nsHttpConnectionInfo *ConnectionInfo() { return mConnInfo; } |
michael@0 | 105 | |
michael@0 | 106 | // nsAHttpConnection compatible methods (non-virtual): |
michael@0 | 107 | nsresult OnHeadersAvailable(nsAHttpTransaction *, nsHttpRequestHead *, nsHttpResponseHead *, bool *reset); |
michael@0 | 108 | void CloseTransaction(nsAHttpTransaction *, nsresult reason); |
michael@0 | 109 | void GetConnectionInfo(nsHttpConnectionInfo **ci) { NS_IF_ADDREF(*ci = mConnInfo); } |
michael@0 | 110 | nsresult TakeTransport(nsISocketTransport **, |
michael@0 | 111 | nsIAsyncInputStream **, |
michael@0 | 112 | nsIAsyncOutputStream **); |
michael@0 | 113 | void GetSecurityInfo(nsISupports **); |
michael@0 | 114 | bool IsPersistent() { return IsKeepAlive(); } |
michael@0 | 115 | bool IsReused(); |
michael@0 | 116 | void SetIsReusedAfter(uint32_t afterMilliseconds); |
michael@0 | 117 | nsresult PushBack(const char *data, uint32_t length); |
michael@0 | 118 | nsresult ResumeSend(); |
michael@0 | 119 | nsresult ResumeRecv(); |
michael@0 | 120 | int64_t MaxBytesRead() {return mMaxBytesRead;} |
michael@0 | 121 | uint8_t GetLastHttpResponseVersion() { return mLastHttpResponseVersion; } |
michael@0 | 122 | |
michael@0 | 123 | friend class nsHttpConnectionForceRecv; |
michael@0 | 124 | nsresult ForceRecv(); |
michael@0 | 125 | |
michael@0 | 126 | static NS_METHOD ReadFromStream(nsIInputStream *, void *, const char *, |
michael@0 | 127 | uint32_t, uint32_t, uint32_t *); |
michael@0 | 128 | |
michael@0 | 129 | // When a persistent connection is in the connection manager idle |
michael@0 | 130 | // connection pool, the nsHttpConnection still reads errors and hangups |
michael@0 | 131 | // on the socket so that it can be proactively released if the server |
michael@0 | 132 | // initiates a termination. Only call on socket thread. |
michael@0 | 133 | void BeginIdleMonitoring(); |
michael@0 | 134 | void EndIdleMonitoring(); |
michael@0 | 135 | |
michael@0 | 136 | bool UsingSpdy() { return !!mUsingSpdyVersion; } |
michael@0 | 137 | uint8_t GetSpdyVersion() { return mUsingSpdyVersion; } |
michael@0 | 138 | bool EverUsedSpdy() { return mEverUsedSpdy; } |
michael@0 | 139 | PRIntervalTime Rtt() { return mRtt; } |
michael@0 | 140 | |
michael@0 | 141 | // true when connection SSL NPN phase is complete and we know |
michael@0 | 142 | // authoritatively whether UsingSpdy() or not. |
michael@0 | 143 | bool ReportedNPN() { return mReportedSpdy; } |
michael@0 | 144 | |
michael@0 | 145 | // When the connection is active this is called up to once every 1 second |
michael@0 | 146 | // return the interval (in seconds) that the connection next wants to |
michael@0 | 147 | // have this invoked. It might happen sooner depending on the needs of |
michael@0 | 148 | // other connections. |
michael@0 | 149 | uint32_t ReadTimeoutTick(PRIntervalTime now); |
michael@0 | 150 | |
michael@0 | 151 | // For Active and Idle connections, this will be called when |
michael@0 | 152 | // mTCPKeepaliveTransitionTimer fires, to check if the TCP keepalive config |
michael@0 | 153 | // should move from short-lived (fast-detect) to long-lived. |
michael@0 | 154 | static void UpdateTCPKeepalive(nsITimer *aTimer, void *aClosure); |
michael@0 | 155 | |
michael@0 | 156 | nsAHttpTransaction::Classifier Classification() { return mClassification; } |
michael@0 | 157 | void Classify(nsAHttpTransaction::Classifier newclass) |
michael@0 | 158 | { |
michael@0 | 159 | mClassification = newclass; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | // When the connection is active this is called every second |
michael@0 | 163 | void ReadTimeoutTick(); |
michael@0 | 164 | |
michael@0 | 165 | int64_t BytesWritten() { return mTotalBytesWritten; } |
michael@0 | 166 | |
michael@0 | 167 | void SetSecurityCallbacks(nsIInterfaceRequestor* aCallbacks); |
michael@0 | 168 | void PrintDiagnostics(nsCString &log); |
michael@0 | 169 | |
michael@0 | 170 | void SetTransactionCaps(uint32_t aCaps) { mTransactionCaps = aCaps; } |
michael@0 | 171 | |
michael@0 | 172 | // IsExperienced() returns true when the connection has started at least one |
michael@0 | 173 | // non null HTTP transaction of any version. |
michael@0 | 174 | bool IsExperienced() { return mExperienced; } |
michael@0 | 175 | |
michael@0 | 176 | private: |
michael@0 | 177 | // Value (set in mTCPKeepaliveConfig) indicates which set of prefs to use. |
michael@0 | 178 | enum TCPKeepaliveConfig { |
michael@0 | 179 | kTCPKeepaliveDisabled = 0, |
michael@0 | 180 | kTCPKeepaliveShortLivedConfig, |
michael@0 | 181 | kTCPKeepaliveLongLivedConfig |
michael@0 | 182 | }; |
michael@0 | 183 | |
michael@0 | 184 | // called to cause the underlying socket to start speaking SSL |
michael@0 | 185 | nsresult ProxyStartSSL(); |
michael@0 | 186 | |
michael@0 | 187 | nsresult OnTransactionDone(nsresult reason); |
michael@0 | 188 | nsresult OnSocketWritable(); |
michael@0 | 189 | nsresult OnSocketReadable(); |
michael@0 | 190 | |
michael@0 | 191 | nsresult SetupProxyConnect(); |
michael@0 | 192 | |
michael@0 | 193 | PRIntervalTime IdleTime(); |
michael@0 | 194 | bool IsAlive(); |
michael@0 | 195 | bool SupportsPipelining(nsHttpResponseHead *); |
michael@0 | 196 | |
michael@0 | 197 | // Makes certain the SSL handshake is complete and NPN negotiation |
michael@0 | 198 | // has had a chance to happen |
michael@0 | 199 | bool EnsureNPNComplete(); |
michael@0 | 200 | void SetupSSL(uint32_t caps); |
michael@0 | 201 | |
michael@0 | 202 | // Start the Spdy transaction handler when NPN indicates spdy/* |
michael@0 | 203 | void StartSpdy(uint8_t versionLevel); |
michael@0 | 204 | |
michael@0 | 205 | // Directly Add a transaction to an active connection for SPDY |
michael@0 | 206 | nsresult AddTransaction(nsAHttpTransaction *, int32_t); |
michael@0 | 207 | |
michael@0 | 208 | // Used to set TCP keepalives for fast detection of dead connections during |
michael@0 | 209 | // an initial period, and slower detection for long-lived connections. |
michael@0 | 210 | nsresult StartShortLivedTCPKeepalives(); |
michael@0 | 211 | nsresult StartLongLivedTCPKeepalives(); |
michael@0 | 212 | nsresult DisableTCPKeepalives(); |
michael@0 | 213 | |
michael@0 | 214 | private: |
michael@0 | 215 | nsCOMPtr<nsISocketTransport> mSocketTransport; |
michael@0 | 216 | nsCOMPtr<nsIAsyncInputStream> mSocketIn; |
michael@0 | 217 | nsCOMPtr<nsIAsyncOutputStream> mSocketOut; |
michael@0 | 218 | |
michael@0 | 219 | nsresult mSocketInCondition; |
michael@0 | 220 | nsresult mSocketOutCondition; |
michael@0 | 221 | |
michael@0 | 222 | nsCOMPtr<nsIInputStream> mProxyConnectStream; |
michael@0 | 223 | nsCOMPtr<nsIInputStream> mRequestStream; |
michael@0 | 224 | |
michael@0 | 225 | // mTransaction only points to the HTTP Transaction callbacks if the |
michael@0 | 226 | // transaction is open, otherwise it is null. |
michael@0 | 227 | nsRefPtr<nsAHttpTransaction> mTransaction; |
michael@0 | 228 | |
michael@0 | 229 | nsRefPtr<nsHttpHandler> mHttpHandler; // keep gHttpHandler alive |
michael@0 | 230 | |
michael@0 | 231 | Mutex mCallbacksLock; |
michael@0 | 232 | nsMainThreadPtrHandle<nsIInterfaceRequestor> mCallbacks; |
michael@0 | 233 | |
michael@0 | 234 | nsRefPtr<nsHttpConnectionInfo> mConnInfo; |
michael@0 | 235 | |
michael@0 | 236 | PRIntervalTime mLastReadTime; |
michael@0 | 237 | PRIntervalTime mLastWriteTime; |
michael@0 | 238 | PRIntervalTime mMaxHangTime; // max download time before dropping keep-alive status |
michael@0 | 239 | PRIntervalTime mIdleTimeout; // value of keep-alive: timeout= |
michael@0 | 240 | PRIntervalTime mConsiderReusedAfterInterval; |
michael@0 | 241 | PRIntervalTime mConsiderReusedAfterEpoch; |
michael@0 | 242 | int64_t mCurrentBytesRead; // data read per activation |
michael@0 | 243 | int64_t mMaxBytesRead; // max read in 1 activation |
michael@0 | 244 | int64_t mTotalBytesRead; // total data read |
michael@0 | 245 | int64_t mTotalBytesWritten; // does not include CONNECT tunnel |
michael@0 | 246 | |
michael@0 | 247 | nsRefPtr<nsIAsyncInputStream> mInputOverflow; |
michael@0 | 248 | |
michael@0 | 249 | PRIntervalTime mRtt; |
michael@0 | 250 | |
michael@0 | 251 | bool mKeepAlive; |
michael@0 | 252 | bool mKeepAliveMask; |
michael@0 | 253 | bool mDontReuse; |
michael@0 | 254 | bool mSupportsPipelining; |
michael@0 | 255 | bool mIsReused; |
michael@0 | 256 | bool mCompletedProxyConnect; |
michael@0 | 257 | bool mLastTransactionExpectedNoContent; |
michael@0 | 258 | bool mIdleMonitoring; |
michael@0 | 259 | bool mProxyConnectInProgress; |
michael@0 | 260 | bool mExperienced; |
michael@0 | 261 | |
michael@0 | 262 | // The number of <= HTTP/1.1 transactions performed on this connection. This |
michael@0 | 263 | // excludes spdy transactions. |
michael@0 | 264 | uint32_t mHttp1xTransactionCount; |
michael@0 | 265 | |
michael@0 | 266 | // Keep-Alive: max="mRemainingConnectionUses" provides the number of future |
michael@0 | 267 | // transactions (including the current one) that the server expects to allow |
michael@0 | 268 | // on this persistent connection. |
michael@0 | 269 | uint32_t mRemainingConnectionUses; |
michael@0 | 270 | |
michael@0 | 271 | nsAHttpTransaction::Classifier mClassification; |
michael@0 | 272 | |
michael@0 | 273 | // SPDY related |
michael@0 | 274 | bool mNPNComplete; |
michael@0 | 275 | bool mSetupSSLCalled; |
michael@0 | 276 | |
michael@0 | 277 | // version level in use, 0 if unused |
michael@0 | 278 | uint8_t mUsingSpdyVersion; |
michael@0 | 279 | |
michael@0 | 280 | nsRefPtr<ASpdySession> mSpdySession; |
michael@0 | 281 | int32_t mPriority; |
michael@0 | 282 | bool mReportedSpdy; |
michael@0 | 283 | |
michael@0 | 284 | // mUsingSpdyVersion is cleared when mSpdySession is freed, this is permanent |
michael@0 | 285 | bool mEverUsedSpdy; |
michael@0 | 286 | |
michael@0 | 287 | // mLastHttpResponseVersion stores the last response's http version seen. |
michael@0 | 288 | uint8_t mLastHttpResponseVersion; |
michael@0 | 289 | |
michael@0 | 290 | // The capabailities associated with the most recent transaction |
michael@0 | 291 | uint32_t mTransactionCaps; |
michael@0 | 292 | |
michael@0 | 293 | bool mResponseTimeoutEnabled; |
michael@0 | 294 | |
michael@0 | 295 | // Flag to indicate connection is in inital keepalive period (fast detect). |
michael@0 | 296 | uint32_t mTCPKeepaliveConfig; |
michael@0 | 297 | nsCOMPtr<nsITimer> mTCPKeepaliveTransitionTimer; |
michael@0 | 298 | }; |
michael@0 | 299 | |
michael@0 | 300 | }} // namespace mozilla::net |
michael@0 | 301 | |
michael@0 | 302 | #endif // nsHttpConnection_h__ |