• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 #ifndef NET_SPDY_SPDY_STREAM_H_
6 #define NET_SPDY_SPDY_STREAM_H_
7 
8 #include <deque>
9 #include <string>
10 #include <vector>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h"
17 #include "net/base/bandwidth_metrics.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/net_export.h"
20 #include "net/base/net_log.h"
21 #include "net/base/request_priority.h"
22 #include "net/socket/ssl_client_socket.h"
23 #include "net/spdy/spdy_buffer.h"
24 #include "net/spdy/spdy_framer.h"
25 #include "net/spdy/spdy_header_block.h"
26 #include "net/spdy/spdy_protocol.h"
27 #include "net/ssl/server_bound_cert_service.h"
28 #include "net/ssl/ssl_client_cert_type.h"
29 #include "url/gurl.h"
30 
31 namespace net {
32 
33 class AddressList;
34 class IPEndPoint;
35 struct LoadTimingInfo;
36 class SSLCertRequestInfo;
37 class SSLInfo;
38 class SpdySession;
39 
40 enum SpdyStreamType {
41   // The most general type of stream; there are no restrictions on
42   // when data can be sent and received.
43   SPDY_BIDIRECTIONAL_STREAM,
44   // A stream where the client sends a request with possibly a body,
45   // and the server then sends a response with a body.
46   SPDY_REQUEST_RESPONSE_STREAM,
47   // A server-initiated stream where the server just sends a response
48   // with a body and the client does not send anything.
49   SPDY_PUSH_STREAM
50 };
51 
52 // Passed to some SpdyStream functions to indicate whether there's
53 // more data to send.
54 enum SpdySendStatus {
55   MORE_DATA_TO_SEND,
56   NO_MORE_DATA_TO_SEND
57 };
58 
59 // Returned by SpdyStream::OnResponseHeadersUpdated() to indicate
60 // whether the current response headers are complete or not.
61 enum SpdyResponseHeadersStatus {
62   RESPONSE_HEADERS_ARE_INCOMPLETE,
63   RESPONSE_HEADERS_ARE_COMPLETE
64 };
65 
66 // The SpdyStream is used by the SpdySession to represent each stream known
67 // on the SpdySession.  This class provides interfaces for SpdySession to use.
68 // Streams can be created either by the client or by the server.  When they
69 // are initiated by the client, both the SpdySession and client object (such as
70 // a SpdyNetworkTransaction) will maintain a reference to the stream.  When
71 // initiated by the server, only the SpdySession will maintain any reference,
72 // until such a time as a client object requests a stream for the path.
73 class NET_EXPORT_PRIVATE SpdyStream {
74  public:
75   // Delegate handles protocol specific behavior of spdy stream.
76   class NET_EXPORT_PRIVATE Delegate {
77    public:
Delegate()78     Delegate() {}
79 
80     // Called when the request headers have been sent. Never called
81     // for push streams. Must not cause the stream to be closed.
82     virtual void OnRequestHeadersSent() = 0;
83 
84     // WARNING: This function is complicated! Be sure to read the
85     // whole comment below if you're working with code that implements
86     // or calls this function.
87     //
88     // Called when the response headers are updated from the
89     // server. |response_headers| contains the set of all headers
90     // received up to this point; delegates can assume that any
91     // headers previously received remain unchanged.
92     //
93     // This is called at least once before any data is received. If
94     // RESPONSE_HEADERS_ARE_INCOMPLETE is returned, this will be
95     // called again when more headers are received until
96     // RESPONSE_HEADERS_ARE_COMPLETE is returned, and any data
97     // received before then will be treated as a protocol error.
98     //
99     // If RESPONSE_HEADERS_ARE_INCOMPLETE is returned, the delegate
100     // must not have closed the stream. Otherwise, if
101     // RESPONSE_HEADERS_ARE_COMPLETE is returned, the delegate has
102     // processed the headers successfully. However, it still may have
103     // closed the stream, e.g. if the headers indicated an error
104     // condition.
105     //
106     // Some type-specific behavior:
107     //
108     //   - For bidirectional streams, this may be called even after
109     //     data is received, but it is expected that
110     //     RESPONSE_HEADERS_ARE_COMPLETE is always returned. If
111     //     RESPONSE_HEADERS_ARE_INCOMPLETE is returned, this is
112     //     treated as a protocol error.
113     //
114     //   - For request/response streams, this function is called
115     //     exactly once before data is received, and it is expected
116     //     that RESPONSE_HEADERS_ARE_COMPLETE is returned. If
117     //     RESPONSE_HEADERS_ARE_INCOMPLETE is returned, this is
118     //     treated as a protocol error.
119     //
120     //   - For push streams, it is expected that this function will be
121     //     called until RESPONSE_HEADERS_ARE_COMPLETE is returned
122     //     before any data is received; any deviation from this is
123     //     treated as a protocol error.
124     //
125     // TODO(akalin): Treat headers received after data has been
126     // received as a protocol error for non-bidirectional streams.
127     // TODO(jgraettinger): This should be at the semantic (HTTP) rather
128     // than stream layer. Streams shouldn't have a notion of header
129     // completeness. Move to SpdyHttpStream/SpdyWebsocketStream.
130     virtual SpdyResponseHeadersStatus OnResponseHeadersUpdated(
131         const SpdyHeaderBlock& response_headers) = 0;
132 
133     // Called when data is received after all required response
134     // headers have been received. |buffer| may be NULL, which signals
135     // EOF.  Must return OK if the data was received successfully, or
136     // a network error code otherwise.
137     //
138     // May cause the stream to be closed.
139     virtual void OnDataReceived(scoped_ptr<SpdyBuffer> buffer) = 0;
140 
141     // Called when data is sent. Must not cause the stream to be
142     // closed.
143     virtual void OnDataSent() = 0;
144 
145     // Called when SpdyStream is closed. No other delegate functions
146     // will be called after this is called, and the delegate must not
147     // access the stream after this is called. Must not cause the
148     // stream to be be (re-)closed.
149     //
150     // TODO(akalin): Allow this function to re-close the stream and
151     // handle it gracefully.
152     virtual void OnClose(int status) = 0;
153 
154    protected:
~Delegate()155     virtual ~Delegate() {}
156 
157    private:
158     DISALLOW_COPY_AND_ASSIGN(Delegate);
159   };
160 
161   // SpdyStream constructor
162   SpdyStream(SpdyStreamType type,
163              const base::WeakPtr<SpdySession>& session,
164              const GURL& url,
165              RequestPriority priority,
166              int32 initial_send_window_size,
167              int32 initial_recv_window_size,
168              const BoundNetLog& net_log);
169 
170   ~SpdyStream();
171 
172   // Set the delegate, which must not be NULL. Must not be called more
173   // than once. For push streams, calling this may cause buffered data
174   // to be sent to the delegate (from a posted task).
175   void SetDelegate(Delegate* delegate);
176 
177   // Detach the delegate from the stream, which must not yet be
178   // closed, and cancel it.
179   void DetachDelegate();
180 
181   // The time at which the first bytes of the response were received
182   // from the server, or null if the response hasn't been received
183   // yet.
response_time()184   base::Time response_time() const { return response_time_; }
185 
type()186   SpdyStreamType type() const { return type_; }
187 
stream_id()188   SpdyStreamId stream_id() const { return stream_id_; }
set_stream_id(SpdyStreamId stream_id)189   void set_stream_id(SpdyStreamId stream_id) { stream_id_ = stream_id; }
190 
url()191   const GURL& url() const { return url_; }
192 
priority()193   RequestPriority priority() const { return priority_; }
194 
send_window_size()195   int32 send_window_size() const { return send_window_size_; }
196 
recv_window_size()197   int32 recv_window_size() const { return recv_window_size_; }
198 
send_stalled_by_flow_control()199   bool send_stalled_by_flow_control() const {
200     return send_stalled_by_flow_control_;
201   }
202 
set_send_stalled_by_flow_control(bool stalled)203   void set_send_stalled_by_flow_control(bool stalled) {
204     send_stalled_by_flow_control_ = stalled;
205   }
206 
207   // Called by the session to adjust this stream's send window size by
208   // |delta_window_size|, which is the difference between the
209   // SETTINGS_INITIAL_WINDOW_SIZE in the most recent SETTINGS frame
210   // and the previous initial send window size, possibly unstalling
211   // this stream. Although |delta_window_size| may cause this stream's
212   // send window size to go negative, it must not cause it to wrap
213   // around in either direction. Does nothing if the stream is already
214   // closed.
215   //
216   // If stream flow control is turned off, this must not be called.
217   void AdjustSendWindowSize(int32 delta_window_size);
218 
219   // Called when bytes are consumed from a SpdyBuffer for a DATA frame
220   // that is to be written or is being written. Increases the send
221   // window size accordingly if some or all of the SpdyBuffer is being
222   // discarded.
223   //
224   // If stream flow control is turned off, this must not be called.
225   void OnWriteBufferConsumed(size_t frame_payload_size,
226                              size_t consume_size,
227                              SpdyBuffer::ConsumeSource consume_source);
228 
229   // Called by the session to increase this stream's send window size
230   // by |delta_window_size| (which must be at least 1) from a received
231   // WINDOW_UPDATE frame or from a dropped DATA frame that was
232   // intended to be sent, possibly unstalling this stream. If
233   // |delta_window_size| would cause this stream's send window size to
234   // overflow, calls into the session to reset this stream. Does
235   // nothing if the stream is already closed.
236   //
237   // If stream flow control is turned off, this must not be called.
238   void IncreaseSendWindowSize(int32 delta_window_size);
239 
240   // If stream flow control is turned on, called by the session to
241   // decrease this stream's send window size by |delta_window_size|,
242   // which must be at least 0 and at most kMaxSpdyFrameChunkSize.
243   // |delta_window_size| must not cause this stream's send window size
244   // to go negative. Does nothing if the stream is already closed.
245   //
246   // If stream flow control is turned off, this must not be called.
247   void DecreaseSendWindowSize(int32 delta_window_size);
248 
249   // Called when bytes are consumed by the delegate from a SpdyBuffer
250   // containing received data. Increases the receive window size
251   // accordingly.
252   //
253   // If stream flow control is turned off, this must not be called.
254   void OnReadBufferConsumed(size_t consume_size,
255                             SpdyBuffer::ConsumeSource consume_source);
256 
257   // Called by OnReadBufferConsume to increase this stream's receive
258   // window size by |delta_window_size|, which must be at least 1 and
259   // must not cause this stream's receive window size to overflow,
260   // possibly also sending a WINDOW_UPDATE frame. Does nothing if the
261   // stream is not active.
262   //
263   // If stream flow control is turned off, this must not be called.
264   void IncreaseRecvWindowSize(int32 delta_window_size);
265 
266   // Called by OnDataReceived (which is in turn called by the session)
267   // to decrease this stream's receive window size by
268   // |delta_window_size|, which must be at least 1 and must not cause
269   // this stream's receive window size to go negative.
270   //
271   // If stream flow control is turned off or the stream is not active,
272   // this must not be called.
273   void DecreaseRecvWindowSize(int32 delta_window_size);
274 
275   int GetPeerAddress(IPEndPoint* address) const;
276   int GetLocalAddress(IPEndPoint* address) const;
277 
278   // Returns true if the underlying transport socket ever had any reads or
279   // writes.
280   bool WasEverUsed() const;
281 
net_log()282   const BoundNetLog& net_log() const { return net_log_; }
283 
284   base::Time GetRequestTime() const;
285   void SetRequestTime(base::Time t);
286 
287   // Called at most once by the SpdySession when the initial response
288   // headers have been received for this stream, i.e., a SYN_REPLY (or
289   // SYN_STREAM for push streams) frame has been received. Returns a status
290   // code; if it is an error, the stream was closed by this function.
291   int OnInitialResponseHeadersReceived(const SpdyHeaderBlock& response_headers,
292                                        base::Time response_time,
293                                        base::TimeTicks recv_first_byte_time);
294 
295   // Called by the SpdySession (only after
296   // OnInitialResponseHeadersReceived() has been called) when
297   // late-bound headers are received for a stream. Returns a status
298   // code; if it is an error, the stream was closed by this function.
299   int OnAdditionalResponseHeadersReceived(
300       const SpdyHeaderBlock& additional_response_headers);
301 
302   // Called by the SpdySession when a frame carrying request headers opening a
303   // push stream is received. Stream transits to STATE_RESERVED_REMOTE state.
304   void OnPushPromiseHeadersReceived(const SpdyHeaderBlock& headers);
305 
306   // Called by the SpdySession when response data has been received
307   // for this stream.  This callback may be called multiple times as
308   // data arrives from the network, and will never be called prior to
309   // OnResponseHeadersReceived.
310   //
311   // |buffer| contains the data received, or NULL if the stream is
312   //          being closed.  The stream must copy any data from this
313   //          buffer before returning from this callback.
314   //
315   // |length| is the number of bytes received (at most 2^24 - 1) or 0 if
316   //          the stream is being closed.
317   void OnDataReceived(scoped_ptr<SpdyBuffer> buffer);
318 
319   // Called by the SpdySession when a frame has been successfully and
320   // completely written. |frame_size| is the total size of the frame
321   // in bytes, including framing overhead.
322   void OnFrameWriteComplete(SpdyFrameType frame_type, size_t frame_size);
323 
324   // SYN_STREAM-specific write handler invoked by OnFrameWriteComplete().
325   int OnRequestHeadersSent();
326 
327   // DATA-specific write handler invoked by OnFrameWriteComplete().
328   // If more data is already available to be written, the next write is
329   // queued and ERR_IO_PENDING is returned. Returns OK otherwise.
330   int OnDataSent(size_t frame_size);
331 
332   // Called by the SpdySession when the request is finished.  This callback
333   // will always be called at the end of the request and signals to the
334   // stream that the stream has no more network events.  No further callbacks
335   // to the stream will be made after this call.
336   // |status| is an error code or OK.
337   void OnClose(int status);
338 
339   // Called by the SpdySession to log stream related errors.
340   void LogStreamError(int status, const std::string& description);
341 
342   // If this stream is active, reset it, and close it otherwise. In
343   // either case the stream is deleted.
344   void Cancel();
345 
346   // Close this stream without sending a RST_STREAM and delete
347   // it.
348   void Close();
349 
350   // Must be used only by |session_|.
351   base::WeakPtr<SpdyStream> GetWeakPtr();
352 
353   // Interface for the delegate to use.
354 
355   // Only one send can be in flight at a time, except for push
356   // streams, which must not send anything.
357 
358   // Sends the request headers. The delegate is called back via
359   // OnRequestHeadersSent() when the request headers have completed
360   // sending. |send_status| must be MORE_DATA_TO_SEND for
361   // bidirectional streams; for request/response streams, it must be
362   // MORE_DATA_TO_SEND if the request has data to upload, or
363   // NO_MORE_DATA_TO_SEND if not.
364   int SendRequestHeaders(scoped_ptr<SpdyHeaderBlock> request_headers,
365                          SpdySendStatus send_status);
366 
367   // Sends a DATA frame. The delegate will be notified via
368   // OnDataSent() when the send is complete. |send_status| must be
369   // MORE_DATA_TO_SEND for bidirectional streams; for request/response
370   // streams, it must be MORE_DATA_TO_SEND if there is more data to
371   // upload, or NO_MORE_DATA_TO_SEND if not.
372   void SendData(IOBuffer* data, int length, SpdySendStatus send_status);
373 
374   // Fills SSL info in |ssl_info| and returns true when SSL is in use.
375   bool GetSSLInfo(SSLInfo* ssl_info,
376                   bool* was_npn_negotiated,
377                   NextProto* protocol_negotiated);
378 
379   // Fills SSL Certificate Request info |cert_request_info| and returns
380   // true when SSL is in use.
381   bool GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info);
382 
383   // If the stream is stalled on sending data, but the session is not
384   // stalled on sending data and |send_window_size_| is positive, then
385   // set |send_stalled_by_flow_control_| to false and unstall the data
386   // sending. Called by the session or by the stream itself. Must be
387   // called only when the stream is still open.
388   void PossiblyResumeIfSendStalled();
389 
390   // Returns whether or not this stream is closed. Note that the only
391   // time a stream is closed and not deleted is in its delegate's
392   // OnClose() method.
393   bool IsClosed() const;
394 
395   // Returns whether the streams local endpoint is closed.
396   // The remote endpoint may still be active.
397   bool IsLocallyClosed() const;
398 
399   // Returns whether this stream is IDLE: request and response headers
400   // have neither been sent nor receieved.
401   bool IsIdle() const;
402 
403   // Returns whether or not this stream is fully open: that request and
404   // response headers are complete, and it is not in a half-closed state.
405   bool IsOpen() const;
406 
407   // Returns whether the stream is reserved by remote endpoint: server has sent
408   // intended request headers for a pushed stream, but haven't started response
409   // yet.
410   bool IsReservedRemote() const;
411 
412   // Returns the protocol used by this stream. Always between
413   // kProtoSPDYMinimumVersion and kProtoSPDYMaximumVersion.
414   NextProto GetProtocol() const;
415 
response_status()416   int response_status() const { return response_status_; }
417 
IncrementRawReceivedBytes(size_t received_bytes)418   void IncrementRawReceivedBytes(size_t received_bytes) {
419     raw_received_bytes_ += received_bytes;
420   }
421 
raw_received_bytes()422   int64 raw_received_bytes() const { return raw_received_bytes_; }
423 
424   bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
425 
426   // Get the URL from the appropriate stream headers, or the empty
427   // GURL() if it is unknown.
428   //
429   // TODO(akalin): Figure out if we really need this function,
430   // i.e. can we just use the URL this stream was created with and/or
431   // one we receive headers validate that the URL from them is the
432   // same.
433   GURL GetUrlFromHeaders() const;
434 
435   // Returns whether the URL for this stream is known.
436   //
437   // TODO(akalin): Remove this, as it's only used in tests.
438   bool HasUrlFromHeaders() const;
439 
440   SpdyMajorVersion GetProtocolVersion() const;
441 
442  private:
443   class SynStreamBufferProducer;
444   class HeaderBufferProducer;
445 
446   // SpdyStream states and transitions are modeled
447   // on the HTTP/2 stream state machine. All states and transitions
448   // are modeled, with the exceptions of RESERVED_LOCAL (the client
449   // cannot initate push streams), and the transition to OPEN due to
450   // a remote SYN_STREAM (the client can only initate streams).
451   enum State {
452     STATE_IDLE,
453     STATE_OPEN,
454     STATE_HALF_CLOSED_LOCAL_UNCLAIMED,
455     STATE_HALF_CLOSED_LOCAL,
456     STATE_HALF_CLOSED_REMOTE,
457     STATE_RESERVED_REMOTE,
458     STATE_CLOSED,
459   };
460 
461   // Update the histograms.  Can safely be called repeatedly, but should only
462   // be called after the stream has completed.
463   void UpdateHistograms();
464 
465   // When a server-push stream is claimed by SetDelegate(), this function is
466   // posted on the current MessageLoop to replay everything the server has sent.
467   // From the perspective of SpdyStream's state machine, headers, data, and
468   // FIN states received prior to the delegate being attached have not yet been
469   // read. While buffered by |pending_recv_data_| it's not until
470   // PushedStreamReplay() is invoked that reads are considered
471   // to have occurred, driving the state machine forward.
472   void PushedStreamReplay();
473 
474   // Produces the SYN_STREAM frame for the stream. The stream must
475   // already be activated.
476   scoped_ptr<SpdyFrame> ProduceSynStreamFrame();
477 
478   // Produce the initial HEADER frame for the stream with the given
479   // block. The stream must already be activated.
480   scoped_ptr<SpdyFrame> ProduceHeaderFrame(
481       scoped_ptr<SpdyHeaderBlock> header_block);
482 
483   // Queues the send for next frame of the remaining data in
484   // |pending_send_data_|. Must be called only when
485   // |pending_send_data_| is set.
486   void QueueNextDataFrame();
487 
488   // Merge the given headers into |response_headers_| and calls
489   // OnResponseHeadersUpdated() on the delegate (if attached).
490   // Returns a status code; if it is an error, the stream was closed
491   // by this function.
492   int MergeWithResponseHeaders(const SpdyHeaderBlock& new_response_headers);
493 
494   static std::string DescribeState(State state);
495 
496   const SpdyStreamType type_;
497 
498   SpdyStreamId stream_id_;
499   const GURL url_;
500   const RequestPriority priority_;
501 
502   // Flow control variables.
503   bool send_stalled_by_flow_control_;
504   int32 send_window_size_;
505   int32 recv_window_size_;
506   int32 unacked_recv_window_bytes_;
507 
508   ScopedBandwidthMetrics metrics_;
509 
510   const base::WeakPtr<SpdySession> session_;
511 
512   // The transaction should own the delegate.
513   SpdyStream::Delegate* delegate_;
514 
515   // The headers for the request to send.
516   //
517   // TODO(akalin): Hang onto this only until we send it. This
518   // necessitates stashing the URL separately.
519   scoped_ptr<SpdyHeaderBlock> request_headers_;
520 
521   // Data waiting to be sent, and the close state of the local endpoint
522   // after the data is fully written.
523   scoped_refptr<DrainableIOBuffer> pending_send_data_;
524   SpdySendStatus pending_send_status_;
525 
526   // Data waiting to be received, and the close state of the remote endpoint
527   // after the data is fully read. Specifically, data received before the
528   // delegate is attached must be buffered and later replayed. A remote FIN
529   // is represented by a final, zero-length buffer.
530   ScopedVector<SpdyBuffer> pending_recv_data_;
531 
532   // The time at which the request was made that resulted in this response.
533   // For cached responses, this time could be "far" in the past.
534   base::Time request_time_;
535 
536   SpdyHeaderBlock response_headers_;
537   SpdyResponseHeadersStatus response_headers_status_;
538   base::Time response_time_;
539 
540   State io_state_;
541 
542   // Since we buffer the response, we also buffer the response status.
543   // Not valid until the stream is closed.
544   int response_status_;
545 
546   BoundNetLog net_log_;
547 
548   base::TimeTicks send_time_;
549   base::TimeTicks recv_first_byte_time_;
550   base::TimeTicks recv_last_byte_time_;
551 
552   // Number of bytes that have been received on this stream, including frame
553   // overhead and headers.
554   int64 raw_received_bytes_;
555 
556   // Number of data bytes that have been sent/received on this stream, not
557   // including frame overhead. Note that this does not count headers.
558   int send_bytes_;
559   int recv_bytes_;
560 
561   std::string domain_bound_private_key_;
562   std::string domain_bound_cert_;
563   ServerBoundCertService::RequestHandle domain_bound_cert_request_handle_;
564 
565   // Guards calls of delegate write handlers ensuring |this| is not destroyed.
566   // TODO(jgraettinger): Consider removing after crbug.com/35511 is tracked
567   // down.
568   bool write_handler_guard_;
569 
570   base::WeakPtrFactory<SpdyStream> weak_ptr_factory_;
571 
572   DISALLOW_COPY_AND_ASSIGN(SpdyStream);
573 };
574 
575 }  // namespace net
576 
577 #endif  // NET_SPDY_SPDY_STREAM_H_
578