• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_SPDY_SPDY_STREAM_H_
6 #define NET_SPDY_SPDY_STREAM_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/memory/raw_ptr.h"
16 #include "base/memory/scoped_refptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/time/time.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_export.h"
21 #include "net/base/request_priority.h"
22 #include "net/log/net_log_source.h"
23 #include "net/log/net_log_with_source.h"
24 #include "net/socket/next_proto.h"
25 #include "net/socket/ssl_client_socket.h"
26 #include "net/spdy/spdy_buffer.h"
27 #include "net/third_party/quiche/src/quiche/spdy/core/http2_header_block.h"
28 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_framer.h"
29 #include "net/third_party/quiche/src/quiche/spdy/core/spdy_protocol.h"
30 #include "net/traffic_annotation/network_traffic_annotation.h"
31 #include "url/gurl.h"
32 
33 namespace net {
34 
35 namespace test {
36 class SpdyStreamTest;
37 }
38 
39 class IPEndPoint;
40 struct LoadTimingInfo;
41 class SSLInfo;
42 class SpdySession;
43 
44 enum SpdyStreamType {
45   // The most general type of stream; there are no restrictions on
46   // when data can be sent and received.
47   SPDY_BIDIRECTIONAL_STREAM,
48   // A stream where the client sends a request with possibly a body,
49   // and the server then sends a response with a body.
50   SPDY_REQUEST_RESPONSE_STREAM,
51 };
52 
53 // Passed to some SpdyStream functions to indicate whether there's
54 // more data to send.
55 enum SpdySendStatus {
56   MORE_DATA_TO_SEND,
57   NO_MORE_DATA_TO_SEND
58 };
59 
60 // SpdyStream is owned by SpdySession and is used to represent each stream known
61 // on the SpdySession.  This class provides interfaces for SpdySession to use.
62 // Streams can be created either by the client or by the server.  When they
63 // are initiated by the client, both the SpdySession and client object (such as
64 // a SpdyNetworkTransaction) will maintain a reference to the stream.  When
65 // initiated by the server, only the SpdySession will maintain any reference,
66 // until such a time as a client object requests a stream for the path.
67 class NET_EXPORT_PRIVATE SpdyStream {
68  public:
69   // Delegate handles protocol specific behavior of spdy stream.
70   class NET_EXPORT_PRIVATE Delegate {
71    public:
72     Delegate() = default;
73 
74     Delegate(const Delegate&) = delete;
75     Delegate& operator=(const Delegate&) = delete;
76 
77     // Called when the request headers have been sent. Never called
78     // for push streams. Must not cause the stream to be closed.
79     virtual void OnHeadersSent() = 0;
80 
81     // OnEarlyHintsReceived(), OnHeadersReceived(), OnDataReceived(),
82     // OnTrailers(), and OnClose() are guaranteed to be called in the following
83     // order:
84     //   - OnEarlyHintsReceived() zero or more times;
85     //   - OnHeadersReceived() exactly once;
86     //   - OnDataReceived() zero or more times;
87     //   - OnTrailers() zero or one times;
88     //   - OnClose() exactly once.
89 
90     // Called when a 103 Early Hints response is received.
91     virtual void OnEarlyHintsReceived(
92         const spdy::Http2HeaderBlock& headers) = 0;
93 
94     // Called when response headers have been received.
95     virtual void OnHeadersReceived(
96         const spdy::Http2HeaderBlock& response_headers) = 0;
97 
98     // Called when data is received.  |buffer| may be NULL, which signals EOF.
99     // May cause the stream to be closed.
100     virtual void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) = 0;
101 
102     // Called when data is sent.  Must not cause the stream to be closed.
103     virtual void OnDataSent() = 0;
104 
105     // Called when trailers are received.
106     virtual void OnTrailers(const spdy::Http2HeaderBlock& trailers) = 0;
107 
108     // Called when SpdyStream is closed. No other delegate functions
109     // will be called after this is called, and the delegate must not
110     // access the stream after this is called. Must not cause the
111     // stream to be (re-)closed.
112     //
113     // TODO(akalin): Allow this function to re-close the stream and
114     // handle it gracefully.
115     virtual void OnClose(int status) = 0;
116 
117     // Returns whether it is allowed to send greased (reserved type) frames on
118     // the HTTP/2 stream.
119     virtual bool CanGreaseFrameType() const = 0;
120 
121     virtual NetLogSource source_dependency() const = 0;
122 
123    protected:
124     virtual ~Delegate() = default;
125   };
126 
127   // SpdyStream constructor
128   SpdyStream(SpdyStreamType type,
129              const base::WeakPtr<SpdySession>& session,
130              const GURL& url,
131              RequestPriority priority,
132              int32_t initial_send_window_size,
133              int32_t max_recv_window_size,
134              const NetLogWithSource& net_log,
135              const NetworkTrafficAnnotationTag& traffic_annotation,
136              bool detect_broken_connection);
137 
138   SpdyStream(const SpdyStream&) = delete;
139   SpdyStream& operator=(const SpdyStream&) = delete;
140 
141   ~SpdyStream();
142 
143   // Set the delegate, which must not be NULL. Must not be called more
144   // than once. For push streams, calling this may cause buffered data
145   // to be sent to the delegate (from a posted task).
146   void SetDelegate(Delegate* delegate);
147 
148   // Detach the delegate from the stream, which must not yet be
149   // closed, and cancel it.
150   void DetachDelegate();
151 
152   // The time at which the first bytes of the response were received
153   // from the server, or null if the response hasn't been received
154   // yet.
response_time()155   base::Time response_time() const { return response_time_; }
156 
type()157   SpdyStreamType type() const { return type_; }
158 
stream_id()159   spdy::SpdyStreamId stream_id() const { return stream_id_; }
set_stream_id(spdy::SpdyStreamId stream_id)160   void set_stream_id(spdy::SpdyStreamId stream_id) { stream_id_ = stream_id; }
161 
url()162   const GURL& url() const { return url_; }
163 
priority()164   RequestPriority priority() const { return priority_; }
165 
166   // Update priority and send PRIORITY frames on the wire if necessary.
167   void SetPriority(RequestPriority priority);
168 
send_window_size()169   int32_t send_window_size() const { return send_window_size_; }
170 
recv_window_size()171   int32_t recv_window_size() const { return recv_window_size_; }
172 
send_stalled_by_flow_control()173   bool send_stalled_by_flow_control() const {
174     return send_stalled_by_flow_control_;
175   }
176 
set_send_stalled_by_flow_control(bool stalled)177   void set_send_stalled_by_flow_control(bool stalled) {
178     send_stalled_by_flow_control_ = stalled;
179   }
180 
181   // Called by the session to adjust this stream's send window size by
182   // |delta_window_size|, which is the difference between the
183   // spdy::SETTINGS_INITIAL_WINDOW_SIZE in the most recent SETTINGS frame
184   // and the previous initial send window size, possibly unstalling
185   // this stream. Although |delta_window_size| may cause this stream's
186   // send window size to go negative, it must not cause it to wrap
187   // around in either direction. Does nothing if the stream is already
188   // closed.
189   // Returns true if successful.  Returns false if |send_window_size_|
190   // would exceed 2^31-1 after the update, see RFC7540 Section 6.9.2.
191   // Note that |send_window_size_| should not possibly underflow.
192   [[nodiscard]] bool AdjustSendWindowSize(int32_t delta_window_size);
193 
194   // Called when bytes are consumed from a SpdyBuffer for a DATA frame
195   // that is to be written or is being written. Increases the send
196   // window size accordingly if some or all of the SpdyBuffer is being
197   // discarded.
198   //
199   // If stream flow control is turned off, this must not be called.
200   void OnWriteBufferConsumed(size_t frame_payload_size,
201                              size_t consume_size,
202                              SpdyBuffer::ConsumeSource consume_source);
203 
204   // Called by the session to increase this stream's send window size
205   // by |delta_window_size| (which must be at least 1) from a received
206   // WINDOW_UPDATE frame or from a dropped DATA frame that was
207   // intended to be sent, possibly unstalling this stream. If
208   // |delta_window_size| would cause this stream's send window size to
209   // overflow, calls into the session to reset this stream. Does
210   // nothing if the stream is already closed.
211   //
212   // If stream flow control is turned off, this must not be called.
213   void IncreaseSendWindowSize(int32_t delta_window_size);
214 
215   // If stream flow control is turned on, called by the session to
216   // decrease this stream's send window size by |delta_window_size|,
217   // which must be at least 0 and at most kMaxSpdyFrameChunkSize.
218   // |delta_window_size| must not cause this stream's send window size
219   // to go negative. Does nothing if the stream is already closed.
220   //
221   // If stream flow control is turned off, this must not be called.
222   void DecreaseSendWindowSize(int32_t delta_window_size);
223 
224   // Called when bytes are consumed by the delegate from a SpdyBuffer
225   // containing received data. Increases the receive window size
226   // accordingly.
227   //
228   // If stream flow control is turned off, this must not be called.
229   void OnReadBufferConsumed(size_t consume_size,
230                             SpdyBuffer::ConsumeSource consume_source);
231 
232   // Called by OnReadBufferConsume to increase this stream's receive
233   // window size by |delta_window_size|, which must be at least 1 and
234   // must not cause this stream's receive window size to overflow,
235   // possibly also sending a WINDOW_UPDATE frame. Does nothing if the
236   // stream is not active.
237   //
238   // If stream flow control is turned off, this must not be called.
239   void IncreaseRecvWindowSize(int32_t delta_window_size);
240 
241   // Called by OnDataReceived or OnPaddingConsumed (which are in turn called by
242   // the session) to decrease this stream's receive window size by
243   // |delta_window_size|, which must be at least 1.  May close the stream on
244   // flow control error.
245   //
246   // If stream flow control is turned off or the stream is not active,
247   // this must not be called.
248   void DecreaseRecvWindowSize(int32_t delta_window_size);
249 
250   int GetPeerAddress(IPEndPoint* address) const;
251   int GetLocalAddress(IPEndPoint* address) const;
252 
253   // Returns true if the underlying transport socket ever had any reads or
254   // writes.
255   bool WasEverUsed() const;
256 
net_log()257   const NetLogWithSource& net_log() const { return net_log_; }
258 
259   base::Time GetRequestTime() const;
260   void SetRequestTime(base::Time t);
261 
262   // Called by SpdySession when headers are received for this stream.  May close
263   // the stream.
264   void OnHeadersReceived(const spdy::Http2HeaderBlock& response_headers,
265                          base::Time response_time,
266                          base::TimeTicks recv_first_byte_time);
267 
268   // Called by the SpdySession when response data has been received
269   // for this stream.  This callback may be called multiple times as
270   // data arrives from the network, and will never be called prior to
271   // OnResponseHeadersReceived.
272   //
273   // |buffer| contains the data received, or NULL if the stream is
274   //          being closed.  The stream must copy any data from this
275   //          buffer before returning from this callback.
276   //
277   // |length| is the number of bytes received (at most 2^24 - 1) or 0 if
278   //          the stream is being closed.
279   void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer);
280 
281   // Called by the SpdySession when padding is consumed to allow for the stream
282   // receiving window to be updated.
283   void OnPaddingConsumed(size_t len);
284 
285   // Called by the SpdySession when a frame has been successfully and completely
286   // written. |frame_size| is the total size of the logical frame in bytes,
287   // including framing overhead.  For fragmented headers, this is the total size
288   // of the HEADERS or PUSH_PROMISE frame and subsequent CONTINUATION frames.
289   void OnFrameWriteComplete(spdy::SpdyFrameType frame_type, size_t frame_size);
290 
291   // HEADERS-specific write handler invoked by OnFrameWriteComplete().
292   int OnHeadersSent();
293 
294   // DATA-specific write handler invoked by OnFrameWriteComplete().
295   // If more data is already available to be written, the next write is
296   // queued and ERR_IO_PENDING is returned. Returns OK otherwise.
297   int OnDataSent(size_t frame_size);
298 
299   // Called by the SpdySession when the request is finished.  This callback
300   // will always be called at the end of the request and signals to the
301   // stream that the stream has no more network events.  No further callbacks
302   // to the stream will be made after this call.  Must be called before
303   // SpdyStream is destroyed.
304   // |status| is an error code or OK.
305   void OnClose(int status);
306 
307   // Called by the SpdySession to log stream related errors.
308   void LogStreamError(int error, base::StringPiece description);
309 
310   // If this stream is active, reset it, and close it otherwise. In
311   // either case the stream is deleted.
312   void Cancel(int error);
313 
314   // Close this stream without sending a RST_STREAM and delete
315   // it.
316   void Close();
317 
318   // Must be used only by |session_|.
319   base::WeakPtr<SpdyStream> GetWeakPtr();
320 
321   // Interface for the delegate to use.
322 
323   // Only one send can be in flight at a time, except for push
324   // streams, which must not send anything.
325 
326   // Sends the request headers. The delegate is called back via OnHeadersSent()
327   // when the request headers have completed sending. |send_status| must be
328   // MORE_DATA_TO_SEND for bidirectional streams; for request/response streams,
329   // it must be MORE_DATA_TO_SEND if the request has data to upload, or
330   // NO_MORE_DATA_TO_SEND if not.
331   int SendRequestHeaders(spdy::Http2HeaderBlock request_headers,
332                          SpdySendStatus send_status);
333 
334   // Sends a DATA frame. The delegate will be notified via
335   // OnDataSent() when the send is complete. |send_status| must be
336   // MORE_DATA_TO_SEND for bidirectional streams; for request/response
337   // streams, it must be MORE_DATA_TO_SEND if there is more data to
338   // upload, or NO_MORE_DATA_TO_SEND if not.
339   // Must not be called until Delegate::OnHeadersSent() is called.
340   void SendData(IOBuffer* data, int length, SpdySendStatus send_status);
341 
342   // Fills SSL info in |ssl_info| and returns true when SSL is in use.
343   bool GetSSLInfo(SSLInfo* ssl_info) const;
344 
345   // Returns the protocol negotiated via ALPN for the underlying socket.
346   NextProto GetNegotiatedProtocol() const;
347 
348   // If the stream is stalled on sending data, but the session is not
349   // stalled on sending data and |send_window_size_| is positive, then
350   // set |send_stalled_by_flow_control_| to false and unstall the data
351   // sending. Called by the session or by the stream itself. Must be
352   // called only when the stream is still open.
353   enum ShouldRequeueStream { Requeue, DoNotRequeue };
354   ShouldRequeueStream PossiblyResumeIfSendStalled();
355 
356   // Returns whether or not this stream is closed. Note that the only
357   // time a stream is closed and not deleted is in its delegate's
358   // OnClose() method.
359   bool IsClosed() const;
360 
361   // Returns whether the streams local endpoint is closed.
362   // The remote endpoint may still be active.
363   bool IsLocallyClosed() const;
364 
365   // Returns whether this stream is IDLE: request and response headers
366   // have neither been sent nor receieved.
367   bool IsIdle() const;
368 
369   // Returns whether or not this stream is fully open: that request and
370   // response headers are complete, and it is not in a half-closed state.
371   bool IsOpen() const;
372 
373   // Returns whether the stream is reserved by remote endpoint: server has sent
374   // intended request headers for a pushed stream, but haven't started response
375   // yet.
376   bool IsReservedRemote() const;
377 
378   void AddRawReceivedBytes(size_t received_bytes);
379   void AddRawSentBytes(size_t sent_bytes);
380 
raw_received_bytes()381   int64_t raw_received_bytes() const { return raw_received_bytes_; }
raw_sent_bytes()382   int64_t raw_sent_bytes() const { return raw_sent_bytes_; }
recv_bytes()383   int recv_bytes() const { return recv_bytes_; }
384 
385   bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
386 
request_headers()387   const spdy::Http2HeaderBlock& request_headers() const {
388     return request_headers_;
389   }
response_headers()390   const spdy::Http2HeaderBlock& response_headers() const {
391     return response_headers_;
392   }
393 
traffic_annotation()394   const NetworkTrafficAnnotationTag traffic_annotation() const {
395     return traffic_annotation_;
396   }
397 
detect_broken_connection()398   bool detect_broken_connection() const { return detect_broken_connection_; }
399 
400  private:
401   friend class test::SpdyStreamTest;
402 
403   class HeadersBufferProducer;
404 
405   // SpdyStream states and transitions are modeled
406   // on the HTTP/2 stream state machine. All states and transitions
407   // are modeled, with the exceptions of RESERVED_LOCAL (the client
408   // cannot initate push streams), and the transition to OPEN due to
409   // a remote HEADERS (the client can only initate streams).
410   enum State {
411     STATE_IDLE,
412     STATE_OPEN,
413     STATE_HALF_CLOSED_LOCAL,
414     STATE_HALF_CLOSED_REMOTE,
415     STATE_RESERVED_REMOTE,
416     STATE_CLOSED,
417   };
418 
419   // Per RFC 7540 Section 8.1, an HTTP response consists of:
420   // * zero or more header blocks with informational (1xx) HTTP status,
421   // * one header block,
422   // * zero or more DATA frames,
423   // * zero or one header block ("trailers").
424   // Each header block must have a ":status" header field.  SpdyStream enforces
425   // these requirements, and resets the stream if they are not met.
426   enum ResponseState {
427     READY_FOR_HEADERS,
428     READY_FOR_DATA_OR_TRAILERS,
429     TRAILERS_RECEIVED
430   };
431 
432   // Produces the HEADERS frame for the stream. The stream must
433   // already be activated.
434   std::unique_ptr<spdy::SpdySerializedFrame> ProduceHeadersFrame();
435 
436   // Queues the send for next frame of the remaining data in
437   // |pending_send_data_|. Must be called only when
438   // |pending_send_data_| is set.
439   void QueueNextDataFrame();
440 
441   void OnEarlyHintsReceived(const spdy::Http2HeaderBlock& response_headers,
442                             base::TimeTicks recv_first_byte_time);
443 
444   // Saves the given headers into |response_headers_| and calls
445   // OnHeadersReceived() on the delegate if attached.
446   void SaveResponseHeaders(const spdy::Http2HeaderBlock& response_headers,
447                            int status);
448 
449   static std::string DescribeState(State state);
450 
451   const SpdyStreamType type_;
452 
453   spdy::SpdyStreamId stream_id_ = 0;
454   const GURL url_;
455   RequestPriority priority_;
456 
457   bool send_stalled_by_flow_control_ = false;
458 
459   // Current send window size.
460   int32_t send_window_size_;
461 
462   // Maximum receive window size.  Each time a WINDOW_UPDATE is sent, it
463   // restores the receive window size to this value.
464   int32_t max_recv_window_size_;
465 
466   // Sum of |session_unacked_recv_window_bytes_| and current receive window
467   // size.
468   // TODO(bnc): Rename or change semantics so that |window_size_| is actual
469   // window size.
470   int32_t recv_window_size_;
471 
472   // When bytes are consumed, SpdyIOBuffer destructor calls back to SpdySession,
473   // and this member keeps count of them until the corresponding WINDOW_UPDATEs
474   // are sent.
475   int32_t unacked_recv_window_bytes_ = 0;
476 
477   // Time of the last WINDOW_UPDATE for the receive window
478   base::TimeTicks last_recv_window_update_;
479 
480   const base::WeakPtr<SpdySession> session_;
481 
482   // The transaction should own the delegate.
483   raw_ptr<SpdyStream::Delegate> delegate_ = nullptr;
484 
485   // The headers for the request to send.
486   bool request_headers_valid_ = false;
487   spdy::Http2HeaderBlock request_headers_;
488 
489   // Data waiting to be sent, and the close state of the local endpoint
490   // after the data is fully written.
491   scoped_refptr<DrainableIOBuffer> pending_send_data_;
492   SpdySendStatus pending_send_status_ = MORE_DATA_TO_SEND;
493 
494   // Data waiting to be received, and the close state of the remote endpoint
495   // after the data is fully read. Specifically, data received before the
496   // delegate is attached must be buffered and later replayed. A remote FIN
497   // is represented by a final, zero-length buffer.
498   std::vector<std::unique_ptr<SpdyBuffer>> pending_recv_data_;
499 
500   // The time at which the request was made that resulted in this response.
501   // For cached responses, this time could be "far" in the past.
502   base::Time request_time_;
503 
504   spdy::Http2HeaderBlock response_headers_;
505   ResponseState response_state_ = READY_FOR_HEADERS;
506   base::Time response_time_;
507 
508   State io_state_ = STATE_IDLE;
509 
510   NetLogWithSource net_log_;
511 
512   base::TimeTicks send_time_;
513 
514   // The time at which the first / last byte of the HTTP headers were received.
515   //
516   // These correspond to |LoadTimingInfo::receive_headers_start| and
517   // |LoadTimingInfo::receive_headers_end|. See also comments there.
518   base::TimeTicks recv_first_byte_time_;
519   base::TimeTicks recv_last_byte_time_;
520 
521   // The time at which the first byte of the HTTP headers for the
522   // non-informational response (non-1xx). This corresponds to
523   // |LoadTimingInfo::receive_non_informational_headers_start|. See also
524   // comments there.
525   base::TimeTicks recv_first_byte_time_for_non_informational_response_;
526 
527   // The time at which the first 103 Early Hints response is received.
528   base::TimeTicks first_early_hints_time_;
529 
530   // Number of bytes that have been received on this stream, including frame
531   // overhead and headers.
532   int64_t raw_received_bytes_ = 0;
533   // Number of bytes that have been sent on this stream, including frame
534   // overhead and headers.
535   int64_t raw_sent_bytes_ = 0;
536 
537   // Number of data bytes that have been received on this stream, not including
538   // frame overhead. Note that this does not count headers.
539   int recv_bytes_ = 0;
540 
541   // Guards calls of delegate write handlers ensuring |this| is not destroyed.
542   // TODO(jgraettinger): Consider removing after crbug.com/35511 is tracked
543   // down.
544   bool write_handler_guard_ = false;
545 
546   const NetworkTrafficAnnotationTag traffic_annotation_;
547 
548   // Used by SpdySession to remember if this stream requested broken connection
549   // detection.
550   bool detect_broken_connection_;
551 
552   base::WeakPtrFactory<SpdyStream> weak_ptr_factory_{this};
553 };
554 
555 }  // namespace net
556 
557 #endif  // NET_SPDY_SPDY_STREAM_H_
558