michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_Http2Stream_h michael@0: #define mozilla_net_Http2Stream_h michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsAHttpTransaction.h" michael@0: #include "nsISupportsPriority.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: class Http2Session; michael@0: class Http2Decompressor; michael@0: michael@0: class Http2Stream michael@0: : public nsAHttpSegmentReader michael@0: , public nsAHttpSegmentWriter michael@0: { michael@0: public: michael@0: NS_DECL_NSAHTTPSEGMENTREADER michael@0: NS_DECL_NSAHTTPSEGMENTWRITER michael@0: michael@0: enum stateType { michael@0: IDLE, michael@0: RESERVED_BY_REMOTE, michael@0: OPEN, michael@0: CLOSED_BY_LOCAL, michael@0: CLOSED_BY_REMOTE, michael@0: CLOSED michael@0: }; michael@0: michael@0: const static int32_t kNormalPriority = 0x1000; michael@0: const static int32_t kWorstPriority = kNormalPriority + nsISupportsPriority::PRIORITY_LOWEST; michael@0: const static int32_t kBestPriority = kNormalPriority + nsISupportsPriority::PRIORITY_HIGHEST; michael@0: michael@0: Http2Stream(nsAHttpTransaction *, Http2Session *, int32_t); michael@0: michael@0: uint32_t StreamID() { return mStreamID; } michael@0: Http2PushedStream *PushSource() { return mPushSource; } michael@0: michael@0: stateType HTTPState() { return mState; } michael@0: void SetHTTPState(stateType val) { mState = val; } michael@0: michael@0: virtual nsresult ReadSegments(nsAHttpSegmentReader *, uint32_t, uint32_t *); michael@0: virtual nsresult WriteSegments(nsAHttpSegmentWriter *, uint32_t, uint32_t *); michael@0: virtual bool DeferCleanupOnSuccess() { return false; } michael@0: michael@0: const nsAFlatCString &Origin() const { return mOrigin; } michael@0: michael@0: bool RequestBlockedOnRead() michael@0: { michael@0: return static_cast(mRequestBlockedOnRead); michael@0: } michael@0: michael@0: bool HasRegisteredID() { return mStreamID != 0; } michael@0: michael@0: nsAHttpTransaction *Transaction() { return mTransaction; } michael@0: virtual nsILoadGroupConnectionInfo *LoadGroupConnectionInfo() michael@0: { michael@0: return mTransaction ? mTransaction->LoadGroupConnectionInfo() : nullptr; michael@0: } michael@0: michael@0: void Close(nsresult reason); michael@0: michael@0: void SetRecvdFin(bool aStatus); michael@0: bool RecvdFin() { return mRecvdFin; } michael@0: michael@0: void SetSentFin(bool aStatus); michael@0: bool SentFin() { return mSentFin; } michael@0: michael@0: void SetRecvdReset(bool aStatus); michael@0: bool RecvdReset() { return mRecvdReset; } michael@0: michael@0: void SetSentReset(bool aStatus); michael@0: bool SentReset() { return mSentReset; } michael@0: michael@0: void SetCountAsActive(bool aStatus) { mCountAsActive = aStatus ? 1 : 0; } michael@0: bool CountAsActive() { return mCountAsActive; } michael@0: michael@0: void SetAllHeadersReceived(bool aStatus) { mAllHeadersReceived = aStatus ? 1 : 0; } michael@0: bool AllHeadersReceived() { return mAllHeadersReceived; } michael@0: michael@0: void UpdateTransportSendEvents(uint32_t count); michael@0: void UpdateTransportReadEvents(uint32_t count); michael@0: michael@0: nsresult ConvertResponseHeaders(Http2Decompressor *, nsACString &, nsACString &); michael@0: nsresult ConvertPushHeaders(Http2Decompressor *, nsACString &, nsACString &); michael@0: michael@0: bool AllowFlowControlledWrite(); michael@0: void UpdateServerReceiveWindow(int32_t delta); michael@0: int64_t ServerReceiveWindow() { return mServerReceiveWindow; } michael@0: michael@0: void DecrementClientReceiveWindow(uint32_t delta) { michael@0: mClientReceiveWindow -= delta; michael@0: mLocalUnacked += delta; michael@0: } michael@0: michael@0: void IncrementClientReceiveWindow(uint32_t delta) { michael@0: mClientReceiveWindow += delta; michael@0: mLocalUnacked -= delta; michael@0: } michael@0: michael@0: uint64_t LocalUnAcked() { return mLocalUnacked; } michael@0: int64_t ClientReceiveWindow() { return mClientReceiveWindow; } michael@0: michael@0: bool BlockedOnRwin() { return mBlockedOnRwin; } michael@0: michael@0: uint32_t Priority() { return mPriority; } michael@0: void SetPriority(uint32_t); michael@0: michael@0: // A pull stream has an implicit sink, a pushed stream has a sink michael@0: // once it is matched to a pull stream. michael@0: virtual bool HasSink() { return true; } michael@0: michael@0: virtual ~Http2Stream(); michael@0: michael@0: protected: michael@0: static void CreatePushHashKey(const nsCString &scheme, michael@0: const nsCString &hostHeader, michael@0: uint64_t serial, michael@0: const nsCSubstring &pathInfo, michael@0: nsCString &outOrigin, michael@0: nsCString &outKey); michael@0: michael@0: // These internal states track request generation michael@0: enum upstreamStateType { michael@0: GENERATING_HEADERS, michael@0: GENERATING_BODY, michael@0: SENDING_BODY, michael@0: SENDING_FIN_STREAM, michael@0: UPSTREAM_COMPLETE michael@0: }; michael@0: michael@0: uint32_t mStreamID; michael@0: michael@0: // The session that this stream is a subset of michael@0: Http2Session *mSession; michael@0: michael@0: nsCString mOrigin; michael@0: nsCString mHeaderHost; michael@0: nsCString mHeaderScheme; michael@0: nsCString mHeaderPath; michael@0: michael@0: // Each stream goes from generating_headers to upstream_complete, perhaps michael@0: // looping on multiple instances of generating_body and michael@0: // sending_body for each frame in the upload. michael@0: enum upstreamStateType mUpstreamState; michael@0: michael@0: // The HTTP/2 state for the stream from section 5.1 michael@0: enum stateType mState; michael@0: michael@0: // Flag is set when all http request headers have been read and ID is stable michael@0: uint32_t mAllHeadersSent : 1; michael@0: michael@0: // Flag is set when all http request headers have been read and ID is stable michael@0: uint32_t mAllHeadersReceived : 1; michael@0: michael@0: void ChangeState(enum upstreamStateType); michael@0: michael@0: private: michael@0: friend class nsAutoPtr; michael@0: michael@0: nsresult ParseHttpRequestHeaders(const char *, uint32_t, uint32_t *); michael@0: void AdjustPushedPriority(); michael@0: void AdjustInitialWindow(); michael@0: nsresult TransmitFrame(const char *, uint32_t *, bool forceCommitment); michael@0: void GenerateDataFrameHeader(uint32_t, bool); michael@0: michael@0: // The underlying HTTP transaction. This pointer is used as the key michael@0: // in the Http2Session mStreamTransactionHash so it is important to michael@0: // keep a reference to it as long as this stream is a member of that hash. michael@0: // (i.e. don't change it or release it after it is set in the ctor). michael@0: nsRefPtr mTransaction; michael@0: michael@0: // The underlying socket transport object is needed to propogate some events michael@0: nsISocketTransport *mSocketTransport; michael@0: michael@0: // These are temporary state variables to hold the argument to michael@0: // Read/WriteSegments so it can be accessed by On(read/write)segment michael@0: // further up the stack. michael@0: nsAHttpSegmentReader *mSegmentReader; michael@0: nsAHttpSegmentWriter *mSegmentWriter; michael@0: michael@0: // The quanta upstream data frames are chopped into michael@0: uint32_t mChunkSize; michael@0: michael@0: // Flag is set when the HTTP processor has more data to send michael@0: // but has blocked in doing so. michael@0: uint32_t mRequestBlockedOnRead : 1; michael@0: michael@0: // Flag is set after the response frame bearing the fin bit has michael@0: // been processed. (i.e. after the server has closed). michael@0: uint32_t mRecvdFin : 1; michael@0: michael@0: // Flag is set after RST_STREAM has been received for this stream michael@0: uint32_t mRecvdReset : 1; michael@0: michael@0: // Flag is set after RST_STREAM has been generated for this stream michael@0: uint32_t mSentReset : 1; michael@0: michael@0: // Flag is set when stream is counted towards MAX_CONCURRENT streams in session michael@0: uint32_t mCountAsActive : 1; michael@0: michael@0: // Flag is set when a FIN has been placed on a data or header frame michael@0: // (i.e after the client has closed) michael@0: uint32_t mSentFin : 1; michael@0: michael@0: // Flag is set after the WAITING_FOR Transport event has been generated michael@0: uint32_t mSentWaitingFor : 1; michael@0: michael@0: // Flag is set after TCP send autotuning has been disabled michael@0: uint32_t mSetTCPSocketBuffer : 1; michael@0: michael@0: // The InlineFrame and associated data is used for composing control michael@0: // frames and data frame headers. michael@0: nsAutoArrayPtr mTxInlineFrame; michael@0: uint32_t mTxInlineFrameSize; michael@0: uint32_t mTxInlineFrameUsed; michael@0: michael@0: // mTxStreamFrameSize tracks the progress of michael@0: // transmitting a request body data frame. The data frame itself michael@0: // is never copied into the spdy layer. michael@0: uint32_t mTxStreamFrameSize; michael@0: michael@0: // Buffer for request header compression. michael@0: nsCString mFlatHttpRequestHeaders; michael@0: michael@0: // Track the content-length of a request body so that we can michael@0: // place the fin flag on the last data packet instead of waiting michael@0: // for a stream closed indication. Relying on stream close results michael@0: // in an extra 0-length runt packet and seems to have some interop michael@0: // problems with the google servers. michael@0: int64_t mRequestBodyLenRemaining; michael@0: michael@0: // 0 is highest.. up to 2^31 - 1 as lowest michael@0: uint32_t mPriority; michael@0: michael@0: // mClientReceiveWindow, mServerReceiveWindow, and mLocalUnacked are for flow control. michael@0: // *window are signed because the race conditions in asynchronous SETTINGS michael@0: // messages can force them temporarily negative. michael@0: michael@0: // mClientReceiveWindow is how much data the server will send without getting a michael@0: // window update michael@0: int64_t mClientReceiveWindow; michael@0: michael@0: // mServerReceiveWindow is how much data the client is allowed to send without michael@0: // getting a window update michael@0: int64_t mServerReceiveWindow; michael@0: michael@0: // LocalUnacked is the number of bytes received by the client but not michael@0: // yet reflected in a window update. Sending that update will increment michael@0: // ClientReceiveWindow michael@0: uint64_t mLocalUnacked; michael@0: michael@0: // True when sending is suspended becuase the server receive window is michael@0: // <= 0 michael@0: bool mBlockedOnRwin; michael@0: michael@0: // For Progress Events michael@0: uint64_t mTotalSent; michael@0: uint64_t mTotalRead; michael@0: michael@0: // For Http2Push michael@0: Http2PushedStream *mPushSource; michael@0: }; michael@0: michael@0: } // namespace mozilla::net michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_net_Http2Stream_h