Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 mozilla_net_Http2Stream_h |
michael@0 | 7 | #define mozilla_net_Http2Stream_h |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Attributes.h" |
michael@0 | 10 | #include "nsAHttpTransaction.h" |
michael@0 | 11 | #include "nsISupportsPriority.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace net { |
michael@0 | 15 | |
michael@0 | 16 | class Http2Session; |
michael@0 | 17 | class Http2Decompressor; |
michael@0 | 18 | |
michael@0 | 19 | class Http2Stream |
michael@0 | 20 | : public nsAHttpSegmentReader |
michael@0 | 21 | , public nsAHttpSegmentWriter |
michael@0 | 22 | { |
michael@0 | 23 | public: |
michael@0 | 24 | NS_DECL_NSAHTTPSEGMENTREADER |
michael@0 | 25 | NS_DECL_NSAHTTPSEGMENTWRITER |
michael@0 | 26 | |
michael@0 | 27 | enum stateType { |
michael@0 | 28 | IDLE, |
michael@0 | 29 | RESERVED_BY_REMOTE, |
michael@0 | 30 | OPEN, |
michael@0 | 31 | CLOSED_BY_LOCAL, |
michael@0 | 32 | CLOSED_BY_REMOTE, |
michael@0 | 33 | CLOSED |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | const static int32_t kNormalPriority = 0x1000; |
michael@0 | 37 | const static int32_t kWorstPriority = kNormalPriority + nsISupportsPriority::PRIORITY_LOWEST; |
michael@0 | 38 | const static int32_t kBestPriority = kNormalPriority + nsISupportsPriority::PRIORITY_HIGHEST; |
michael@0 | 39 | |
michael@0 | 40 | Http2Stream(nsAHttpTransaction *, Http2Session *, int32_t); |
michael@0 | 41 | |
michael@0 | 42 | uint32_t StreamID() { return mStreamID; } |
michael@0 | 43 | Http2PushedStream *PushSource() { return mPushSource; } |
michael@0 | 44 | |
michael@0 | 45 | stateType HTTPState() { return mState; } |
michael@0 | 46 | void SetHTTPState(stateType val) { mState = val; } |
michael@0 | 47 | |
michael@0 | 48 | virtual nsresult ReadSegments(nsAHttpSegmentReader *, uint32_t, uint32_t *); |
michael@0 | 49 | virtual nsresult WriteSegments(nsAHttpSegmentWriter *, uint32_t, uint32_t *); |
michael@0 | 50 | virtual bool DeferCleanupOnSuccess() { return false; } |
michael@0 | 51 | |
michael@0 | 52 | const nsAFlatCString &Origin() const { return mOrigin; } |
michael@0 | 53 | |
michael@0 | 54 | bool RequestBlockedOnRead() |
michael@0 | 55 | { |
michael@0 | 56 | return static_cast<bool>(mRequestBlockedOnRead); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool HasRegisteredID() { return mStreamID != 0; } |
michael@0 | 60 | |
michael@0 | 61 | nsAHttpTransaction *Transaction() { return mTransaction; } |
michael@0 | 62 | virtual nsILoadGroupConnectionInfo *LoadGroupConnectionInfo() |
michael@0 | 63 | { |
michael@0 | 64 | return mTransaction ? mTransaction->LoadGroupConnectionInfo() : nullptr; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void Close(nsresult reason); |
michael@0 | 68 | |
michael@0 | 69 | void SetRecvdFin(bool aStatus); |
michael@0 | 70 | bool RecvdFin() { return mRecvdFin; } |
michael@0 | 71 | |
michael@0 | 72 | void SetSentFin(bool aStatus); |
michael@0 | 73 | bool SentFin() { return mSentFin; } |
michael@0 | 74 | |
michael@0 | 75 | void SetRecvdReset(bool aStatus); |
michael@0 | 76 | bool RecvdReset() { return mRecvdReset; } |
michael@0 | 77 | |
michael@0 | 78 | void SetSentReset(bool aStatus); |
michael@0 | 79 | bool SentReset() { return mSentReset; } |
michael@0 | 80 | |
michael@0 | 81 | void SetCountAsActive(bool aStatus) { mCountAsActive = aStatus ? 1 : 0; } |
michael@0 | 82 | bool CountAsActive() { return mCountAsActive; } |
michael@0 | 83 | |
michael@0 | 84 | void SetAllHeadersReceived(bool aStatus) { mAllHeadersReceived = aStatus ? 1 : 0; } |
michael@0 | 85 | bool AllHeadersReceived() { return mAllHeadersReceived; } |
michael@0 | 86 | |
michael@0 | 87 | void UpdateTransportSendEvents(uint32_t count); |
michael@0 | 88 | void UpdateTransportReadEvents(uint32_t count); |
michael@0 | 89 | |
michael@0 | 90 | nsresult ConvertResponseHeaders(Http2Decompressor *, nsACString &, nsACString &); |
michael@0 | 91 | nsresult ConvertPushHeaders(Http2Decompressor *, nsACString &, nsACString &); |
michael@0 | 92 | |
michael@0 | 93 | bool AllowFlowControlledWrite(); |
michael@0 | 94 | void UpdateServerReceiveWindow(int32_t delta); |
michael@0 | 95 | int64_t ServerReceiveWindow() { return mServerReceiveWindow; } |
michael@0 | 96 | |
michael@0 | 97 | void DecrementClientReceiveWindow(uint32_t delta) { |
michael@0 | 98 | mClientReceiveWindow -= delta; |
michael@0 | 99 | mLocalUnacked += delta; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | void IncrementClientReceiveWindow(uint32_t delta) { |
michael@0 | 103 | mClientReceiveWindow += delta; |
michael@0 | 104 | mLocalUnacked -= delta; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | uint64_t LocalUnAcked() { return mLocalUnacked; } |
michael@0 | 108 | int64_t ClientReceiveWindow() { return mClientReceiveWindow; } |
michael@0 | 109 | |
michael@0 | 110 | bool BlockedOnRwin() { return mBlockedOnRwin; } |
michael@0 | 111 | |
michael@0 | 112 | uint32_t Priority() { return mPriority; } |
michael@0 | 113 | void SetPriority(uint32_t); |
michael@0 | 114 | |
michael@0 | 115 | // A pull stream has an implicit sink, a pushed stream has a sink |
michael@0 | 116 | // once it is matched to a pull stream. |
michael@0 | 117 | virtual bool HasSink() { return true; } |
michael@0 | 118 | |
michael@0 | 119 | virtual ~Http2Stream(); |
michael@0 | 120 | |
michael@0 | 121 | protected: |
michael@0 | 122 | static void CreatePushHashKey(const nsCString &scheme, |
michael@0 | 123 | const nsCString &hostHeader, |
michael@0 | 124 | uint64_t serial, |
michael@0 | 125 | const nsCSubstring &pathInfo, |
michael@0 | 126 | nsCString &outOrigin, |
michael@0 | 127 | nsCString &outKey); |
michael@0 | 128 | |
michael@0 | 129 | // These internal states track request generation |
michael@0 | 130 | enum upstreamStateType { |
michael@0 | 131 | GENERATING_HEADERS, |
michael@0 | 132 | GENERATING_BODY, |
michael@0 | 133 | SENDING_BODY, |
michael@0 | 134 | SENDING_FIN_STREAM, |
michael@0 | 135 | UPSTREAM_COMPLETE |
michael@0 | 136 | }; |
michael@0 | 137 | |
michael@0 | 138 | uint32_t mStreamID; |
michael@0 | 139 | |
michael@0 | 140 | // The session that this stream is a subset of |
michael@0 | 141 | Http2Session *mSession; |
michael@0 | 142 | |
michael@0 | 143 | nsCString mOrigin; |
michael@0 | 144 | nsCString mHeaderHost; |
michael@0 | 145 | nsCString mHeaderScheme; |
michael@0 | 146 | nsCString mHeaderPath; |
michael@0 | 147 | |
michael@0 | 148 | // Each stream goes from generating_headers to upstream_complete, perhaps |
michael@0 | 149 | // looping on multiple instances of generating_body and |
michael@0 | 150 | // sending_body for each frame in the upload. |
michael@0 | 151 | enum upstreamStateType mUpstreamState; |
michael@0 | 152 | |
michael@0 | 153 | // The HTTP/2 state for the stream from section 5.1 |
michael@0 | 154 | enum stateType mState; |
michael@0 | 155 | |
michael@0 | 156 | // Flag is set when all http request headers have been read and ID is stable |
michael@0 | 157 | uint32_t mAllHeadersSent : 1; |
michael@0 | 158 | |
michael@0 | 159 | // Flag is set when all http request headers have been read and ID is stable |
michael@0 | 160 | uint32_t mAllHeadersReceived : 1; |
michael@0 | 161 | |
michael@0 | 162 | void ChangeState(enum upstreamStateType); |
michael@0 | 163 | |
michael@0 | 164 | private: |
michael@0 | 165 | friend class nsAutoPtr<Http2Stream>; |
michael@0 | 166 | |
michael@0 | 167 | nsresult ParseHttpRequestHeaders(const char *, uint32_t, uint32_t *); |
michael@0 | 168 | void AdjustPushedPriority(); |
michael@0 | 169 | void AdjustInitialWindow(); |
michael@0 | 170 | nsresult TransmitFrame(const char *, uint32_t *, bool forceCommitment); |
michael@0 | 171 | void GenerateDataFrameHeader(uint32_t, bool); |
michael@0 | 172 | |
michael@0 | 173 | // The underlying HTTP transaction. This pointer is used as the key |
michael@0 | 174 | // in the Http2Session mStreamTransactionHash so it is important to |
michael@0 | 175 | // keep a reference to it as long as this stream is a member of that hash. |
michael@0 | 176 | // (i.e. don't change it or release it after it is set in the ctor). |
michael@0 | 177 | nsRefPtr<nsAHttpTransaction> mTransaction; |
michael@0 | 178 | |
michael@0 | 179 | // The underlying socket transport object is needed to propogate some events |
michael@0 | 180 | nsISocketTransport *mSocketTransport; |
michael@0 | 181 | |
michael@0 | 182 | // These are temporary state variables to hold the argument to |
michael@0 | 183 | // Read/WriteSegments so it can be accessed by On(read/write)segment |
michael@0 | 184 | // further up the stack. |
michael@0 | 185 | nsAHttpSegmentReader *mSegmentReader; |
michael@0 | 186 | nsAHttpSegmentWriter *mSegmentWriter; |
michael@0 | 187 | |
michael@0 | 188 | // The quanta upstream data frames are chopped into |
michael@0 | 189 | uint32_t mChunkSize; |
michael@0 | 190 | |
michael@0 | 191 | // Flag is set when the HTTP processor has more data to send |
michael@0 | 192 | // but has blocked in doing so. |
michael@0 | 193 | uint32_t mRequestBlockedOnRead : 1; |
michael@0 | 194 | |
michael@0 | 195 | // Flag is set after the response frame bearing the fin bit has |
michael@0 | 196 | // been processed. (i.e. after the server has closed). |
michael@0 | 197 | uint32_t mRecvdFin : 1; |
michael@0 | 198 | |
michael@0 | 199 | // Flag is set after RST_STREAM has been received for this stream |
michael@0 | 200 | uint32_t mRecvdReset : 1; |
michael@0 | 201 | |
michael@0 | 202 | // Flag is set after RST_STREAM has been generated for this stream |
michael@0 | 203 | uint32_t mSentReset : 1; |
michael@0 | 204 | |
michael@0 | 205 | // Flag is set when stream is counted towards MAX_CONCURRENT streams in session |
michael@0 | 206 | uint32_t mCountAsActive : 1; |
michael@0 | 207 | |
michael@0 | 208 | // Flag is set when a FIN has been placed on a data or header frame |
michael@0 | 209 | // (i.e after the client has closed) |
michael@0 | 210 | uint32_t mSentFin : 1; |
michael@0 | 211 | |
michael@0 | 212 | // Flag is set after the WAITING_FOR Transport event has been generated |
michael@0 | 213 | uint32_t mSentWaitingFor : 1; |
michael@0 | 214 | |
michael@0 | 215 | // Flag is set after TCP send autotuning has been disabled |
michael@0 | 216 | uint32_t mSetTCPSocketBuffer : 1; |
michael@0 | 217 | |
michael@0 | 218 | // The InlineFrame and associated data is used for composing control |
michael@0 | 219 | // frames and data frame headers. |
michael@0 | 220 | nsAutoArrayPtr<uint8_t> mTxInlineFrame; |
michael@0 | 221 | uint32_t mTxInlineFrameSize; |
michael@0 | 222 | uint32_t mTxInlineFrameUsed; |
michael@0 | 223 | |
michael@0 | 224 | // mTxStreamFrameSize tracks the progress of |
michael@0 | 225 | // transmitting a request body data frame. The data frame itself |
michael@0 | 226 | // is never copied into the spdy layer. |
michael@0 | 227 | uint32_t mTxStreamFrameSize; |
michael@0 | 228 | |
michael@0 | 229 | // Buffer for request header compression. |
michael@0 | 230 | nsCString mFlatHttpRequestHeaders; |
michael@0 | 231 | |
michael@0 | 232 | // Track the content-length of a request body so that we can |
michael@0 | 233 | // place the fin flag on the last data packet instead of waiting |
michael@0 | 234 | // for a stream closed indication. Relying on stream close results |
michael@0 | 235 | // in an extra 0-length runt packet and seems to have some interop |
michael@0 | 236 | // problems with the google servers. |
michael@0 | 237 | int64_t mRequestBodyLenRemaining; |
michael@0 | 238 | |
michael@0 | 239 | // 0 is highest.. up to 2^31 - 1 as lowest |
michael@0 | 240 | uint32_t mPriority; |
michael@0 | 241 | |
michael@0 | 242 | // mClientReceiveWindow, mServerReceiveWindow, and mLocalUnacked are for flow control. |
michael@0 | 243 | // *window are signed because the race conditions in asynchronous SETTINGS |
michael@0 | 244 | // messages can force them temporarily negative. |
michael@0 | 245 | |
michael@0 | 246 | // mClientReceiveWindow is how much data the server will send without getting a |
michael@0 | 247 | // window update |
michael@0 | 248 | int64_t mClientReceiveWindow; |
michael@0 | 249 | |
michael@0 | 250 | // mServerReceiveWindow is how much data the client is allowed to send without |
michael@0 | 251 | // getting a window update |
michael@0 | 252 | int64_t mServerReceiveWindow; |
michael@0 | 253 | |
michael@0 | 254 | // LocalUnacked is the number of bytes received by the client but not |
michael@0 | 255 | // yet reflected in a window update. Sending that update will increment |
michael@0 | 256 | // ClientReceiveWindow |
michael@0 | 257 | uint64_t mLocalUnacked; |
michael@0 | 258 | |
michael@0 | 259 | // True when sending is suspended becuase the server receive window is |
michael@0 | 260 | // <= 0 |
michael@0 | 261 | bool mBlockedOnRwin; |
michael@0 | 262 | |
michael@0 | 263 | // For Progress Events |
michael@0 | 264 | uint64_t mTotalSent; |
michael@0 | 265 | uint64_t mTotalRead; |
michael@0 | 266 | |
michael@0 | 267 | // For Http2Push |
michael@0 | 268 | Http2PushedStream *mPushSource; |
michael@0 | 269 | }; |
michael@0 | 270 | |
michael@0 | 271 | } // namespace mozilla::net |
michael@0 | 272 | } // namespace mozilla |
michael@0 | 273 | |
michael@0 | 274 | #endif // mozilla_net_Http2Stream_h |