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_SpdyStream3_h michael@0: #define mozilla_net_SpdyStream3_h michael@0: michael@0: #include "nsAHttpTransaction.h" michael@0: #include "zlib.h" michael@0: michael@0: class nsISocketTransport; michael@0: michael@0: namespace mozilla { namespace net { michael@0: michael@0: class SpdySession3; michael@0: class SpdyPushedStream3; michael@0: michael@0: class SpdyStream3 : 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: SpdyStream3(nsAHttpTransaction *, SpdySession3 *, int32_t); michael@0: michael@0: uint32_t StreamID() { return mStreamID; } michael@0: SpdyPushedStream3 *PushSource() { return mPushSource; } 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: // returns false if called more than once michael@0: bool GetFullyOpen() {return mFullyOpen;} michael@0: void SetFullyOpen() michael@0: { michael@0: MOZ_ASSERT(!mFullyOpen); michael@0: mFullyOpen = 1; 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) { mRecvdFin = aStatus ? 1 : 0; } michael@0: bool RecvdFin() { return mRecvdFin; } michael@0: michael@0: void SetRecvdData(bool aStatus) { mReceivedData = aStatus ? 1 : 0; } michael@0: bool RecvdData() { return mReceivedData; } michael@0: michael@0: void UpdateTransportSendEvents(uint32_t count); michael@0: void UpdateTransportReadEvents(uint32_t count); michael@0: michael@0: // The zlib header compression dictionary defined by SPDY. michael@0: static const unsigned char kDictionary[1423]; michael@0: michael@0: nsresult Uncompress(z_stream *, char *, uint32_t); michael@0: nsresult ConvertHeaders(nsACString &); michael@0: michael@0: void UpdateRemoteWindow(int32_t delta); michael@0: int64_t RemoteWindow() { return mRemoteWindow; } michael@0: michael@0: void DecrementLocalWindow(uint32_t delta) { michael@0: mLocalWindow -= delta; michael@0: mLocalUnacked += delta; michael@0: } michael@0: michael@0: void IncrementLocalWindow(uint32_t delta) { michael@0: mLocalWindow += delta; michael@0: mLocalUnacked -= delta; michael@0: } michael@0: michael@0: uint64_t LocalUnAcked() { return mLocalUnacked; } michael@0: int64_t LocalWindow() { return mLocalWindow; } michael@0: michael@0: bool BlockedOnRwin() { return mBlockedOnRwin; } 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 ~SpdyStream3(); michael@0: michael@0: protected: michael@0: nsresult FindHeader(nsCString, nsDependentCSubstring &); michael@0: 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: enum stateType { michael@0: GENERATING_SYN_STREAM, michael@0: GENERATING_REQUEST_BODY, michael@0: SENDING_REQUEST_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: SpdySession3 *mSession; michael@0: michael@0: nsCString mOrigin; michael@0: michael@0: // Each stream goes from syn_stream to upstream_complete, perhaps michael@0: // looping on multiple instances of generating_request_body and michael@0: // sending_request_body for each SPDY chunk in the upload. michael@0: enum stateType mUpstreamState; michael@0: michael@0: // Flag is set when all http request headers have been read and ID is stable michael@0: uint32_t mSynFrameComplete : 1; michael@0: michael@0: // Flag is set when a FIN has been placed on a data or syn packet michael@0: // (i.e after the client has closed) michael@0: uint32_t mSentFinOnData : 1; michael@0: michael@0: void ChangeState(enum stateType); michael@0: michael@0: private: michael@0: friend class nsAutoPtr; michael@0: michael@0: static PLDHashOperator hdrHashEnumerate(const nsACString &, michael@0: nsAutoPtr &, michael@0: void *); michael@0: michael@0: nsresult ParseHttpRequestHeaders(const char *, uint32_t, uint32_t *); 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: void CompressToFrame(const nsACString &); michael@0: void CompressToFrame(const nsACString *); michael@0: void CompressToFrame(const char *, uint32_t); michael@0: void CompressToFrame(uint32_t); michael@0: void CompressFlushFrame(); michael@0: void ExecuteCompress(uint32_t); michael@0: michael@0: // The underlying HTTP transaction. This pointer is used as the key michael@0: // in the SpdySession3 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 syn reply received michael@0: uint32_t mFullyOpen : 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 1st DATA frame has been passed to stream, after michael@0: // which additional HEADERS data is invalid michael@0: uint32_t mReceivedData : 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: // Compression context and buffer for request header compression. michael@0: // This is a copy of SpdySession3::mUpstreamZlib because it needs michael@0: // to remain the same in all streams of a session. michael@0: z_stream *mZlib; michael@0: nsCString mFlatHttpRequestHeaders; michael@0: michael@0: // These are used for decompressing downstream spdy response headers michael@0: uint32_t mDecompressBufferSize; michael@0: uint32_t mDecompressBufferUsed; michael@0: uint32_t mDecompressedBytes; michael@0: nsAutoArrayPtr mDecompressBuffer; 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: // based on nsISupportsPriority definitions michael@0: int32_t mPriority; michael@0: michael@0: // mLocalWindow, mRemoteWindow, and mLocalUnacked are for flow control. michael@0: // *window are signed because they race conditions in asynchronous SETTINGS michael@0: // messages can force them temporarily negative. michael@0: michael@0: // LocalWindow is how much data the server will send without getting a michael@0: // window update michael@0: int64_t mLocalWindow; michael@0: michael@0: // RemoteWindow is how much data the client is allowed to send without michael@0: // getting a window update michael@0: int64_t mRemoteWindow; 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: // LocalWindow michael@0: uint64_t mLocalUnacked; michael@0: michael@0: // True when sending is suspended becuase the remote flow control 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 SpdyPush michael@0: SpdyPushedStream3 *mPushSource; michael@0: }; michael@0: michael@0: }} // namespace mozilla::net michael@0: michael@0: #endif // mozilla_net_SpdyStream3_h