|
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/. */ |
|
5 |
|
6 #ifndef mozilla_net_SpdyStream3_h |
|
7 #define mozilla_net_SpdyStream3_h |
|
8 |
|
9 #include "nsAHttpTransaction.h" |
|
10 #include "zlib.h" |
|
11 |
|
12 class nsISocketTransport; |
|
13 |
|
14 namespace mozilla { namespace net { |
|
15 |
|
16 class SpdySession3; |
|
17 class SpdyPushedStream3; |
|
18 |
|
19 class SpdyStream3 : public nsAHttpSegmentReader |
|
20 , public nsAHttpSegmentWriter |
|
21 { |
|
22 public: |
|
23 NS_DECL_NSAHTTPSEGMENTREADER |
|
24 NS_DECL_NSAHTTPSEGMENTWRITER |
|
25 |
|
26 SpdyStream3(nsAHttpTransaction *, SpdySession3 *, int32_t); |
|
27 |
|
28 uint32_t StreamID() { return mStreamID; } |
|
29 SpdyPushedStream3 *PushSource() { return mPushSource; } |
|
30 |
|
31 virtual nsresult ReadSegments(nsAHttpSegmentReader *, uint32_t, uint32_t *); |
|
32 virtual nsresult WriteSegments(nsAHttpSegmentWriter *, uint32_t, uint32_t *); |
|
33 virtual bool DeferCleanupOnSuccess() { return false; } |
|
34 |
|
35 const nsAFlatCString &Origin() const { return mOrigin; } |
|
36 |
|
37 bool RequestBlockedOnRead() |
|
38 { |
|
39 return static_cast<bool>(mRequestBlockedOnRead); |
|
40 } |
|
41 |
|
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 } |
|
49 |
|
50 bool HasRegisteredID() { return mStreamID != 0; } |
|
51 |
|
52 nsAHttpTransaction *Transaction() { return mTransaction; } |
|
53 virtual nsILoadGroupConnectionInfo *LoadGroupConnectionInfo() |
|
54 { |
|
55 return mTransaction ? mTransaction->LoadGroupConnectionInfo() : nullptr; |
|
56 } |
|
57 |
|
58 void Close(nsresult reason); |
|
59 |
|
60 void SetRecvdFin(bool aStatus) { mRecvdFin = aStatus ? 1 : 0; } |
|
61 bool RecvdFin() { return mRecvdFin; } |
|
62 |
|
63 void SetRecvdData(bool aStatus) { mReceivedData = aStatus ? 1 : 0; } |
|
64 bool RecvdData() { return mReceivedData; } |
|
65 |
|
66 void UpdateTransportSendEvents(uint32_t count); |
|
67 void UpdateTransportReadEvents(uint32_t count); |
|
68 |
|
69 // The zlib header compression dictionary defined by SPDY. |
|
70 static const unsigned char kDictionary[1423]; |
|
71 |
|
72 nsresult Uncompress(z_stream *, char *, uint32_t); |
|
73 nsresult ConvertHeaders(nsACString &); |
|
74 |
|
75 void UpdateRemoteWindow(int32_t delta); |
|
76 int64_t RemoteWindow() { return mRemoteWindow; } |
|
77 |
|
78 void DecrementLocalWindow(uint32_t delta) { |
|
79 mLocalWindow -= delta; |
|
80 mLocalUnacked += delta; |
|
81 } |
|
82 |
|
83 void IncrementLocalWindow(uint32_t delta) { |
|
84 mLocalWindow += delta; |
|
85 mLocalUnacked -= delta; |
|
86 } |
|
87 |
|
88 uint64_t LocalUnAcked() { return mLocalUnacked; } |
|
89 int64_t LocalWindow() { return mLocalWindow; } |
|
90 |
|
91 bool BlockedOnRwin() { return mBlockedOnRwin; } |
|
92 |
|
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; } |
|
96 |
|
97 virtual ~SpdyStream3(); |
|
98 |
|
99 protected: |
|
100 nsresult FindHeader(nsCString, nsDependentCSubstring &); |
|
101 |
|
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); |
|
108 |
|
109 enum stateType { |
|
110 GENERATING_SYN_STREAM, |
|
111 GENERATING_REQUEST_BODY, |
|
112 SENDING_REQUEST_BODY, |
|
113 SENDING_FIN_STREAM, |
|
114 UPSTREAM_COMPLETE |
|
115 }; |
|
116 |
|
117 uint32_t mStreamID; |
|
118 |
|
119 // The session that this stream is a subset of |
|
120 SpdySession3 *mSession; |
|
121 |
|
122 nsCString mOrigin; |
|
123 |
|
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; |
|
128 |
|
129 // Flag is set when all http request headers have been read and ID is stable |
|
130 uint32_t mSynFrameComplete : 1; |
|
131 |
|
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; |
|
135 |
|
136 void ChangeState(enum stateType); |
|
137 |
|
138 private: |
|
139 friend class nsAutoPtr<SpdyStream3>; |
|
140 |
|
141 static PLDHashOperator hdrHashEnumerate(const nsACString &, |
|
142 nsAutoPtr<nsCString> &, |
|
143 void *); |
|
144 |
|
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); |
|
149 |
|
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); |
|
156 |
|
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; |
|
162 |
|
163 // The underlying socket transport object is needed to propogate some events |
|
164 nsISocketTransport *mSocketTransport; |
|
165 |
|
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; |
|
171 |
|
172 // The quanta upstream data frames are chopped into |
|
173 uint32_t mChunkSize; |
|
174 |
|
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; |
|
178 |
|
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; |
|
182 |
|
183 // Flag is set after syn reply received |
|
184 uint32_t mFullyOpen : 1; |
|
185 |
|
186 // Flag is set after the WAITING_FOR Transport event has been generated |
|
187 uint32_t mSentWaitingFor : 1; |
|
188 |
|
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; |
|
192 |
|
193 // Flag is set after TCP send autotuning has been disabled |
|
194 uint32_t mSetTCPSocketBuffer : 1; |
|
195 |
|
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; |
|
201 |
|
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; |
|
206 |
|
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; |
|
212 |
|
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; |
|
218 |
|
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; |
|
225 |
|
226 // based on nsISupportsPriority definitions |
|
227 int32_t mPriority; |
|
228 |
|
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. |
|
232 |
|
233 // LocalWindow is how much data the server will send without getting a |
|
234 // window update |
|
235 int64_t mLocalWindow; |
|
236 |
|
237 // RemoteWindow is how much data the client is allowed to send without |
|
238 // getting a window update |
|
239 int64_t mRemoteWindow; |
|
240 |
|
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; |
|
245 |
|
246 // True when sending is suspended becuase the remote flow control window is |
|
247 // <= 0 |
|
248 bool mBlockedOnRwin; |
|
249 |
|
250 // For Progress Events |
|
251 uint64_t mTotalSent; |
|
252 uint64_t mTotalRead; |
|
253 |
|
254 // For SpdyPush |
|
255 SpdyPushedStream3 *mPushSource; |
|
256 }; |
|
257 |
|
258 }} // namespace mozilla::net |
|
259 |
|
260 #endif // mozilla_net_SpdyStream3_h |