netwerk/protocol/http/SpdyStream3.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

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.)

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef mozilla_net_SpdyStream3_h
     7 #define mozilla_net_SpdyStream3_h
     9 #include "nsAHttpTransaction.h"
    10 #include "zlib.h"
    12 class nsISocketTransport;
    14 namespace mozilla { namespace net {
    16 class SpdySession3;
    17 class SpdyPushedStream3;
    19 class SpdyStream3 : public nsAHttpSegmentReader
    20                   , public nsAHttpSegmentWriter
    21 {
    22 public:
    23   NS_DECL_NSAHTTPSEGMENTREADER
    24   NS_DECL_NSAHTTPSEGMENTWRITER
    26   SpdyStream3(nsAHttpTransaction *, SpdySession3 *, int32_t);
    28   uint32_t StreamID() { return mStreamID; }
    29   SpdyPushedStream3 *PushSource() { return mPushSource; }
    31   virtual nsresult ReadSegments(nsAHttpSegmentReader *,  uint32_t, uint32_t *);
    32   virtual nsresult WriteSegments(nsAHttpSegmentWriter *, uint32_t, uint32_t *);
    33   virtual bool DeferCleanupOnSuccess() { return false; }
    35   const nsAFlatCString &Origin()   const { return mOrigin; }
    37   bool RequestBlockedOnRead()
    38   {
    39     return static_cast<bool>(mRequestBlockedOnRead);
    40   }
    42   // returns false if called more than once
    43   bool GetFullyOpen() {return mFullyOpen;}
    44   void SetFullyOpen()
    45   {
    46     MOZ_ASSERT(!mFullyOpen);
    47     mFullyOpen = 1;
    48   }
    50   bool HasRegisteredID() { return mStreamID != 0; }
    52   nsAHttpTransaction *Transaction() { return mTransaction; }
    53   virtual nsILoadGroupConnectionInfo *LoadGroupConnectionInfo()
    54   {
    55     return mTransaction ? mTransaction->LoadGroupConnectionInfo() : nullptr;
    56   }
    58   void Close(nsresult reason);
    60   void SetRecvdFin(bool aStatus) { mRecvdFin = aStatus ? 1 : 0; }
    61   bool RecvdFin() { return mRecvdFin; }
    63   void SetRecvdData(bool aStatus) { mReceivedData = aStatus ? 1 : 0; }
    64   bool RecvdData() { return mReceivedData; }
    66   void UpdateTransportSendEvents(uint32_t count);
    67   void UpdateTransportReadEvents(uint32_t count);
    69   // The zlib header compression dictionary defined by SPDY.
    70   static const unsigned char kDictionary[1423];
    72   nsresult Uncompress(z_stream *, char *, uint32_t);
    73   nsresult ConvertHeaders(nsACString &);
    75   void UpdateRemoteWindow(int32_t delta);
    76   int64_t RemoteWindow() { return mRemoteWindow; }
    78   void DecrementLocalWindow(uint32_t delta) {
    79     mLocalWindow -= delta;
    80     mLocalUnacked += delta;
    81   }
    83   void IncrementLocalWindow(uint32_t delta) {
    84     mLocalWindow += delta;
    85     mLocalUnacked -= delta;
    86   }
    88   uint64_t LocalUnAcked() { return mLocalUnacked; }
    89   int64_t  LocalWindow()  { return mLocalWindow; }
    91   bool     BlockedOnRwin() { return mBlockedOnRwin; }
    93   // A pull stream has an implicit sink, a pushed stream has a sink
    94   // once it is matched to a pull stream.
    95   virtual bool HasSink() { return true; }
    97   virtual ~SpdyStream3();
    99 protected:
   100   nsresult FindHeader(nsCString, nsDependentCSubstring &);
   102   static void CreatePushHashKey(const nsCString &scheme,
   103                                 const nsCString &hostHeader,
   104                                 uint64_t serial,
   105                                 const nsCSubstring &pathInfo,
   106                                 nsCString &outOrigin,
   107                                 nsCString &outKey);
   109   enum stateType {
   110     GENERATING_SYN_STREAM,
   111     GENERATING_REQUEST_BODY,
   112     SENDING_REQUEST_BODY,
   113     SENDING_FIN_STREAM,
   114     UPSTREAM_COMPLETE
   115   };
   117   uint32_t mStreamID;
   119   // The session that this stream is a subset of
   120   SpdySession3 *mSession;
   122   nsCString     mOrigin;
   124   // Each stream goes from syn_stream to upstream_complete, perhaps
   125   // looping on multiple instances of generating_request_body and
   126   // sending_request_body for each SPDY chunk in the upload.
   127   enum stateType mUpstreamState;
   129   // Flag is set when all http request headers have been read and ID is stable
   130   uint32_t                     mSynFrameComplete     : 1;
   132   // Flag is set when a FIN has been placed on a data or syn packet
   133   // (i.e after the client has closed)
   134   uint32_t                     mSentFinOnData        : 1;
   136   void     ChangeState(enum stateType);
   138 private:
   139   friend class nsAutoPtr<SpdyStream3>;
   141   static PLDHashOperator hdrHashEnumerate(const nsACString &,
   142                                           nsAutoPtr<nsCString> &,
   143                                           void *);
   145   nsresult ParseHttpRequestHeaders(const char *, uint32_t, uint32_t *);
   146   void     AdjustInitialWindow();
   147   nsresult TransmitFrame(const char *, uint32_t *, bool forceCommitment);
   148   void     GenerateDataFrameHeader(uint32_t, bool);
   150   void     CompressToFrame(const nsACString &);
   151   void     CompressToFrame(const nsACString *);
   152   void     CompressToFrame(const char *, uint32_t);
   153   void     CompressToFrame(uint32_t);
   154   void     CompressFlushFrame();
   155   void     ExecuteCompress(uint32_t);
   157   // The underlying HTTP transaction. This pointer is used as the key
   158   // in the SpdySession3 mStreamTransactionHash so it is important to
   159   // keep a reference to it as long as this stream is a member of that hash.
   160   // (i.e. don't change it or release it after it is set in the ctor).
   161   nsRefPtr<nsAHttpTransaction> mTransaction;
   163   // The underlying socket transport object is needed to propogate some events
   164   nsISocketTransport         *mSocketTransport;
   166   // These are temporary state variables to hold the argument to
   167   // Read/WriteSegments so it can be accessed by On(read/write)segment
   168   // further up the stack.
   169   nsAHttpSegmentReader        *mSegmentReader;
   170   nsAHttpSegmentWriter        *mSegmentWriter;
   172   // The quanta upstream data frames are chopped into
   173   uint32_t                    mChunkSize;
   175   // Flag is set when the HTTP processor has more data to send
   176   // but has blocked in doing so.
   177   uint32_t                     mRequestBlockedOnRead : 1;
   179   // Flag is set after the response frame bearing the fin bit has
   180   // been processed. (i.e. after the server has closed).
   181   uint32_t                     mRecvdFin             : 1;
   183   // Flag is set after syn reply received
   184   uint32_t                     mFullyOpen            : 1;
   186   // Flag is set after the WAITING_FOR Transport event has been generated
   187   uint32_t                     mSentWaitingFor       : 1;
   189   // Flag is set after 1st DATA frame has been passed to stream, after
   190   // which additional HEADERS data is invalid
   191   uint32_t                     mReceivedData         : 1;
   193   // Flag is set after TCP send autotuning has been disabled
   194   uint32_t                     mSetTCPSocketBuffer   : 1;
   196   // The InlineFrame and associated data is used for composing control
   197   // frames and data frame headers.
   198   nsAutoArrayPtr<uint8_t>      mTxInlineFrame;
   199   uint32_t                     mTxInlineFrameSize;
   200   uint32_t                     mTxInlineFrameUsed;
   202   // mTxStreamFrameSize tracks the progress of
   203   // transmitting a request body data frame. The data frame itself
   204   // is never copied into the spdy layer.
   205   uint32_t                     mTxStreamFrameSize;
   207   // Compression context and buffer for request header compression.
   208   // This is a copy of SpdySession3::mUpstreamZlib because it needs
   209   //  to remain the same in all streams of a session.
   210   z_stream                     *mZlib;
   211   nsCString                    mFlatHttpRequestHeaders;
   213   // These are used for decompressing downstream spdy response headers
   214   uint32_t             mDecompressBufferSize;
   215   uint32_t             mDecompressBufferUsed;
   216   uint32_t             mDecompressedBytes;
   217   nsAutoArrayPtr<char> mDecompressBuffer;
   219   // Track the content-length of a request body so that we can
   220   // place the fin flag on the last data packet instead of waiting
   221   // for a stream closed indication. Relying on stream close results
   222   // in an extra 0-length runt packet and seems to have some interop
   223   // problems with the google servers.
   224   int64_t                      mRequestBodyLenRemaining;
   226   // based on nsISupportsPriority definitions
   227   int32_t                      mPriority;
   229   // mLocalWindow, mRemoteWindow, and mLocalUnacked are for flow control.
   230   // *window are signed because they race conditions in asynchronous SETTINGS
   231   // messages can force them temporarily negative.
   233   // LocalWindow is how much data the server will send without getting a
   234   //   window update
   235   int64_t                      mLocalWindow;
   237   // RemoteWindow is how much data the client is allowed to send without
   238   //   getting a window update
   239   int64_t                      mRemoteWindow;
   241   // LocalUnacked is the number of bytes received by the client but not
   242   //   yet reflected in a window update. Sending that update will increment
   243   //   LocalWindow
   244   uint64_t                     mLocalUnacked;
   246   // True when sending is suspended becuase the remote flow control window is
   247   //   <= 0
   248   bool                         mBlockedOnRwin;
   250   // For Progress Events
   251   uint64_t                     mTotalSent;
   252   uint64_t                     mTotalRead;
   254   // For SpdyPush
   255   SpdyPushedStream3 *mPushSource;
   256 };
   258 }} // namespace mozilla::net
   260 #endif // mozilla_net_SpdyStream3_h

mercurial