spdy_http_stream.h revision 868fa2fe829687343ffae624259930155e16dbd8
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_HTTP_STREAM_H_
6#define NET_SPDY_SPDY_HTTP_STREAM_H_
7
8#include <list>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
13#include "net/base/completion_callback.h"
14#include "net/base/net_log.h"
15#include "net/http/http_stream.h"
16#include "net/spdy/spdy_read_queue.h"
17#include "net/spdy/spdy_session.h"
18#include "net/spdy/spdy_stream.h"
19
20namespace net {
21
22class DrainableIOBuffer;
23struct HttpRequestInfo;
24class HttpResponseInfo;
25class IOBuffer;
26class SpdySession;
27class UploadDataStream;
28
29// The SpdyHttpStream is a HTTP-specific type of stream known to a SpdySession.
30class NET_EXPORT_PRIVATE SpdyHttpStream : public SpdyStream::Delegate,
31                                          public HttpStream {
32 public:
33  // |spdy_session| may be NULL, but in that case some functions must
34  // not be called (see comments below).
35  SpdyHttpStream(SpdySession* spdy_session, bool direct);
36  virtual ~SpdyHttpStream();
37
38  // Initializes this SpdyHttpStream by wrapping an existing
39  // SpdyStream. In particular, this must be called instead of
40  // InitializeStream() if a NULL SpdySession was passed into the
41  // constructor.
42  void InitializeWithExistingStream(
43      const base::WeakPtr<SpdyStream>& spdy_stream);
44
45  SpdyStream* stream() { return stream_.get(); }
46
47  // Cancels any callbacks from being invoked and deletes the stream.
48  void Cancel();
49
50  // HttpStream implementation.
51
52  // Must not be called if a NULL SpdySession was passed into the
53  // constructor.
54  virtual int InitializeStream(const HttpRequestInfo* request_info,
55                               RequestPriority priority,
56                               const BoundNetLog& net_log,
57                               const CompletionCallback& callback) OVERRIDE;
58
59  virtual int SendRequest(const HttpRequestHeaders& headers,
60                          HttpResponseInfo* response,
61                          const CompletionCallback& callback) OVERRIDE;
62  virtual UploadProgress GetUploadProgress() const OVERRIDE;
63  virtual int ReadResponseHeaders(const CompletionCallback& callback) OVERRIDE;
64  virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE;
65  virtual int ReadResponseBody(IOBuffer* buf,
66                               int buf_len,
67                               const CompletionCallback& callback) OVERRIDE;
68  virtual void Close(bool not_reusable) OVERRIDE;
69  virtual HttpStream* RenewStreamForAuth() OVERRIDE;
70  virtual bool IsResponseBodyComplete() const OVERRIDE;
71  virtual bool CanFindEndOfResponse() const OVERRIDE;
72
73  // Must not be called if a NULL SpdySession was pssed into the
74  // constructor.
75  virtual bool IsConnectionReused() const OVERRIDE;
76
77  virtual void SetConnectionReused() OVERRIDE;
78  virtual bool IsConnectionReusable() const OVERRIDE;
79  virtual bool GetLoadTimingInfo(
80      LoadTimingInfo* load_timing_info) const OVERRIDE;
81  virtual void GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
82  virtual void GetSSLCertRequestInfo(
83      SSLCertRequestInfo* cert_request_info) OVERRIDE;
84  virtual bool IsSpdyHttpStream() const OVERRIDE;
85  virtual void Drain(HttpNetworkSession* session) OVERRIDE;
86
87  // SpdyStream::Delegate implementation.
88  virtual void OnRequestHeadersSent() OVERRIDE;
89  virtual int OnResponseHeadersReceived(const SpdyHeaderBlock& response,
90                                        base::Time response_time,
91                                        int status) OVERRIDE;
92  virtual int OnDataReceived(scoped_ptr<SpdyBuffer> buffer) OVERRIDE;
93  virtual void OnDataSent() OVERRIDE;
94  virtual void OnClose(int status) OVERRIDE;
95
96 private:
97  // Must be called only when |request_info_| is non-NULL.
98  bool HasUploadData() const;
99
100  void OnStreamCreated(const CompletionCallback& callback, int rv);
101
102  // Reads the remaining data (whether chunked or not) from the
103  // request body stream and sends it if there's any. The read and
104  // subsequent sending may happen asynchronously. Must be called only
105  // when HasUploadData() is true.
106  void ReadAndSendRequestBodyData();
107
108  // Called when data has just been read from the request body stream;
109  // does the actual sending of data.
110  void OnRequestBodyReadCompleted(int status);
111
112  // Call the user callback.
113  void DoCallback(int rv);
114
115  void ScheduleBufferedReadCallback();
116
117  // Returns true if the callback is invoked.
118  bool DoBufferedReadCallback();
119  bool ShouldWaitForMoreBufferedData() const;
120
121  base::WeakPtrFactory<SpdyHttpStream> weak_factory_;
122
123  const scoped_refptr<SpdySession> spdy_session_;
124  SpdyStreamRequest stream_request_;
125  base::WeakPtr<SpdyStream> stream_;
126
127  bool stream_closed_;
128
129  // Set only when |stream_closed_| is true.
130  int closed_stream_status_;
131  SpdyStreamId closed_stream_id_;
132
133  // The request to send.
134  const HttpRequestInfo* request_info_;
135
136  // |response_info_| is the HTTP response data object which is filled in
137  // when a SYN_REPLY comes in for the stream.
138  // It is not owned by this stream object, or point to |push_response_info_|.
139  HttpResponseInfo* response_info_;
140
141  scoped_ptr<HttpResponseInfo> push_response_info_;
142
143  bool response_headers_received_;  // Indicates waiting for more HEADERS.
144
145  // We buffer the response body as it arrives asynchronously from the stream.
146  SpdyReadQueue response_body_queue_;
147
148  CompletionCallback callback_;
149
150  // User provided buffer for the ReadResponseBody() response.
151  scoped_refptr<IOBuffer> user_buffer_;
152  int user_buffer_len_;
153
154  // Temporary buffer used to read the request body from UploadDataStream.
155  scoped_refptr<IOBufferWithSize> request_body_buf_;
156  int request_body_buf_size_;
157
158  // Is there a scheduled read callback pending.
159  bool buffered_read_callback_pending_;
160  // Has more data been received from the network during the wait for the
161  // scheduled read callback.
162  bool more_read_data_pending_;
163
164  // Is this spdy stream direct to the origin server (or to a proxy).
165  bool direct_;
166
167  DISALLOW_COPY_AND_ASSIGN(SpdyHttpStream);
168};
169
170}  // namespace net
171
172#endif  // NET_SPDY_SPDY_HTTP_STREAM_H_
173