OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // The base class for streams which deliver data to/from an application. | |
6 // In each direction, the data on such a stream first contains compressed | |
7 // headers then body data. | |
8 | |
9 #ifndef NET_QUIC_QUIC_SPDY_STREAM_H_ | |
10 #define NET_QUIC_QUIC_SPDY_STREAM_H_ | |
11 | |
12 #include <stddef.h> | |
13 #include <sys/types.h> | |
14 | |
15 #include <list> | |
16 #include <string> | |
17 | |
18 #include "base/macros.h" | |
19 #include "base/strings/string_piece.h" | |
20 #include "net/base/iovec.h" | |
21 #include "net/base/ip_endpoint.h" | |
22 #include "net/base/net_export.h" | |
23 #include "net/quic/quic_flags.h" | |
24 #include "net/quic/quic_header_list.h" | |
25 #include "net/quic/quic_protocol.h" | |
26 #include "net/quic/quic_stream_sequencer.h" | |
27 #include "net/quic/reliable_quic_stream.h" | |
28 #include "net/spdy/spdy_framer.h" | |
29 | |
30 namespace net { | |
31 | |
32 namespace test { | |
33 class QuicSpdyStreamPeer; | |
34 class ReliableQuicStreamPeer; | |
35 } // namespace test | |
36 | |
37 class QuicSpdySession; | |
38 | |
39 // This is somewhat arbitrary. It's possible, but unlikely, we will either fail | |
40 // to set a priority client-side, or cancel a stream before stripping the | |
41 // priority from the wire server-side. In either case, start out with a | |
42 // priority in the middle. | |
43 const SpdyPriority kDefaultPriority = 3; | |
44 | |
45 // A QUIC stream that can send and receive HTTP2 (SPDY) headers. | |
46 class NET_EXPORT_PRIVATE QuicSpdyStream : public ReliableQuicStream { | |
47 public: | |
48 // Visitor receives callbacks from the stream. | |
49 class NET_EXPORT_PRIVATE Visitor { | |
50 public: | |
51 Visitor() {} | |
52 | |
53 // Called when the stream is closed. | |
54 virtual void OnClose(QuicSpdyStream* stream) = 0; | |
55 | |
56 // Allows subclasses to override and do work. | |
57 virtual void OnPromiseHeadersComplete(QuicStreamId promised_id, | |
58 size_t frame_len) {} | |
59 | |
60 protected: | |
61 virtual ~Visitor() {} | |
62 | |
63 private: | |
64 DISALLOW_COPY_AND_ASSIGN(Visitor); | |
65 }; | |
66 | |
67 QuicSpdyStream(QuicStreamId id, QuicSpdySession* spdy_session); | |
68 ~QuicSpdyStream() override; | |
69 | |
70 // Override the base class to send QUIC_STREAM_NO_ERROR to the peer | |
71 // when the stream has not received all the data. | |
72 void CloseWriteSide() override; | |
73 void StopReading() override; | |
74 | |
75 // ReliableQuicStream implementation | |
76 void OnClose() override; | |
77 | |
78 // Called by the session when decompressed headers data is received | |
79 // for this stream. | |
80 // May be called multiple times, with each call providing additional headers | |
81 // data until OnStreamHeadersComplete is called. | |
82 virtual void OnStreamHeaders(base::StringPiece headers_data); | |
83 | |
84 // Called by the session when headers with a priority have been received | |
85 // for this stream. This method will only be called for server streams. | |
86 virtual void OnStreamHeadersPriority(SpdyPriority priority); | |
87 | |
88 // Called by the session when decompressed headers have been completely | |
89 // delivered to this stream. If |fin| is true, then this stream | |
90 // should be closed; no more data will be sent by the peer. | |
91 virtual void OnStreamHeadersComplete(bool fin, size_t frame_len); | |
92 | |
93 // Called by the session when decompressed headers have been completely | |
94 // delivered to this stream. If |fin| is true, then this stream | |
95 // should be closed; no more data will be sent by the peer. | |
96 virtual void OnStreamHeaderList(bool fin, | |
97 size_t frame_len, | |
98 const QuicHeaderList& header_list); | |
99 | |
100 // Called by the session when decompressed PUSH_PROMISE headers data | |
101 // is received for this stream. | |
102 // May be called multiple times, with each call providing additional headers | |
103 // data until OnPromiseHeadersComplete is called. | |
104 virtual void OnPromiseHeaders(base::StringPiece headers_data); | |
105 | |
106 // Called by the session when decompressed push promise headers have | |
107 // been completely delivered to this stream. | |
108 virtual void OnPromiseHeadersComplete(QuicStreamId promised_id, | |
109 size_t frame_len); | |
110 | |
111 // Called by the session when decompressed push promise headers have | |
112 // been completely delivered to this stream. | |
113 virtual void OnPromiseHeaderList(QuicStreamId promised_id, | |
114 size_t frame_len, | |
115 const QuicHeaderList& header_list); | |
116 | |
117 // Override the base class to not discard response when receiving | |
118 // QUIC_STREAM_NO_ERROR. | |
119 void OnStreamReset(const QuicRstStreamFrame& frame) override; | |
120 | |
121 // Writes the headers contained in |header_block| to the dedicated | |
122 // headers stream. | |
123 virtual size_t WriteHeaders(SpdyHeaderBlock header_block, | |
124 bool fin, | |
125 QuicAckListenerInterface* ack_notifier_delegate); | |
126 | |
127 // Sends |data| to the peer, or buffers if it can't be sent immediately. | |
128 void WriteOrBufferBody(const std::string& data, | |
129 bool fin, | |
130 QuicAckListenerInterface* ack_notifier_delegate); | |
131 | |
132 // Writes the trailers contained in |trailer_block| to the dedicated | |
133 // headers stream. Trailers will always have the FIN set. | |
134 virtual size_t WriteTrailers(SpdyHeaderBlock trailer_block, | |
135 QuicAckListenerInterface* ack_notifier_delegate); | |
136 | |
137 // Marks |bytes_consumed| of the headers data as consumed. | |
138 void MarkHeadersConsumed(size_t bytes_consumed); | |
139 | |
140 // Marks |bytes_consumed| of the trailers data as consumed. | |
141 void MarkTrailersConsumed(size_t bytes_consumed); | |
142 | |
143 // Marks the trailers as consumed. | |
144 void MarkTrailersDelivered(); | |
145 | |
146 // Clears |header_list_|. | |
147 void ConsumeHeaderList(); | |
148 | |
149 // This block of functions wraps the sequencer's functions of the same | |
150 // name. These methods return uncompressed data until that has | |
151 // been fully processed. Then they simply delegate to the sequencer. | |
152 virtual size_t Readv(const struct iovec* iov, size_t iov_len); | |
153 virtual int GetReadableRegions(iovec* iov, size_t iov_len) const; | |
154 void MarkConsumed(size_t num_bytes); | |
155 | |
156 // Returns true if header contains a valid 3-digit status and parse the status | |
157 // code to |status_code|. | |
158 bool ParseHeaderStatusCode(const SpdyHeaderBlock& header, | |
159 int* status_code) const; | |
160 | |
161 // Returns true when all data has been read from the peer, including the fin. | |
162 bool IsDoneReading() const; | |
163 bool HasBytesToRead() const; | |
164 | |
165 void set_visitor(Visitor* visitor) { visitor_ = visitor; } | |
166 | |
167 bool headers_decompressed() const { return headers_decompressed_; } | |
168 | |
169 const std::string& decompressed_headers() const { | |
170 return decompressed_headers_; | |
171 } | |
172 | |
173 const QuicHeaderList& header_list() const { return header_list_; } | |
174 | |
175 bool trailers_decompressed() const { return trailers_decompressed_; } | |
176 | |
177 const std::string& decompressed_trailers() const { | |
178 return decompressed_trailers_; | |
179 } | |
180 | |
181 // Returns whatever trailers have been received for this stream. | |
182 const SpdyHeaderBlock& received_trailers() const { | |
183 return received_trailers_; | |
184 } | |
185 | |
186 virtual SpdyPriority priority() const; | |
187 | |
188 // Sets priority_ to priority. This should only be called before bytes are | |
189 // written to the server. | |
190 void SetPriority(SpdyPriority priority); | |
191 | |
192 // Called when owning session is getting deleted to avoid subsequent | |
193 // use of the spdy_session_ member. | |
194 void ClearSession(); | |
195 | |
196 // Returns true if the sequencer has delivered the FIN, and no more body bytes | |
197 // will be available. | |
198 bool IsClosed() { return sequencer()->IsClosed(); } | |
199 | |
200 protected: | |
201 // Called by OnStreamHeadersComplete depending on which type (initial or | |
202 // trailing) headers are expected next. | |
203 virtual void OnInitialHeadersComplete(bool fin, size_t frame_len); | |
204 virtual void OnTrailingHeadersComplete(bool fin, size_t frame_len); | |
205 virtual void OnInitialHeadersComplete(bool fin, | |
206 size_t frame_len, | |
207 const QuicHeaderList& header_list); | |
208 virtual void OnTrailingHeadersComplete(bool fin, | |
209 size_t frame_len, | |
210 const QuicHeaderList& header_list); | |
211 QuicSpdySession* spdy_session() const { return spdy_session_; } | |
212 Visitor* visitor() { return visitor_; } | |
213 | |
214 // Returns true if headers have been fully read and consumed. | |
215 bool FinishedReadingHeaders() const; | |
216 | |
217 // Redirects to the headers stream if force HOL blocking enabled, | |
218 // otherwise just pass through. | |
219 QuicConsumedData WritevDataInner( | |
220 QuicIOVector iov, | |
221 QuicStreamOffset offset, | |
222 bool fin, | |
223 QuicAckListenerInterface* ack_notifier_delegate) override; | |
224 | |
225 private: | |
226 friend class test::QuicSpdyStreamPeer; | |
227 friend class test::ReliableQuicStreamPeer; | |
228 friend class QuicStreamUtils; | |
229 | |
230 // Returns true if trailers have been fully read and consumed. | |
231 bool FinishedReadingTrailers() const; | |
232 | |
233 QuicSpdySession* spdy_session_; | |
234 | |
235 Visitor* visitor_; | |
236 // True if the headers have been completely decompressed. | |
237 bool headers_decompressed_; | |
238 // The priority of the stream, once parsed. | |
239 SpdyPriority priority_; | |
240 // Contains a copy of the decompressed headers until they are consumed | |
241 // via ProcessData or Readv. | |
242 std::string decompressed_headers_; | |
243 // Contains a copy of the decompressed header (name, value) pairs until they | |
244 // are consumed via Readv. | |
245 QuicHeaderList header_list_; | |
246 | |
247 // True if the trailers have been completely decompressed. | |
248 bool trailers_decompressed_; | |
249 // True if the trailers have been consumed. | |
250 bool trailers_delivered_; | |
251 // Contains a copy of the decompressed trailers until they are consumed | |
252 // via ProcessData or Readv. | |
253 std::string decompressed_trailers_; | |
254 // The parsed trailers received from the peer. | |
255 SpdyHeaderBlock received_trailers_; | |
256 | |
257 DISALLOW_COPY_AND_ASSIGN(QuicSpdyStream); | |
258 }; | |
259 | |
260 } // namespace net | |
261 | |
262 #endif // NET_QUIC_QUIC_SPDY_STREAM_H_ | |
OLD | NEW |