blob: e7fdd6e4ae9d9f4029b456778512e02c91d68254 [file] [log] [blame]
xunjieli11834f02015-12-22 04:27:081// Copyright 2015 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_HTTP_BIDIRECTIONAL_STREAM_H_
6#define NET_HTTP_BIDIRECTIONAL_STREAM_H_
7
8#include <stdint.h>
9
danakj1fd259a02016-04-16 03:17:0910#include <memory>
11
xunjieli11834f02015-12-22 04:27:0812#include "base/compiler_specific.h"
13#include "base/macros.h"
xunjielibe07785e2016-04-14 21:15:2914#include "base/memory/ref_counted.h"
xunjieli3b6ac702016-06-06 21:36:2815#include "base/memory/weak_ptr.h"
xunjielid58621f2016-08-12 18:33:0016#include "base/time/time.h"
xunjieli369d0922016-09-23 18:45:0617#include "net/base/load_timing_info.h"
bnc81c46c1f2016-10-04 16:25:5918#include "net/base/net_export.h"
xunjieli5749218c2016-03-22 16:43:0619#include "net/http/bidirectional_stream_impl.h"
xunjieli11834f02015-12-22 04:27:0820#include "net/http/http_stream_factory.h"
mikecironef22f9812016-10-04 03:40:1921#include "net/log/net_log_with_source.h"
Nico Weber13d106e2015-12-22 21:41:5622
xunjieli11834f02015-12-22 04:27:0823class GURL;
24
25namespace net {
26
27class HttpAuthController;
28class HttpNetworkSession;
29class HttpStream;
30class HttpStreamRequest;
31class IOBuffer;
32class ProxyInfo;
33class SpdyHeaderBlock;
34struct BidirectionalStreamRequestInfo;
35struct SSLConfig;
36
37// A class to do HTTP/2 bidirectional streaming. Note that at most one each of
xunjieli07a42ce2016-04-26 20:05:3138// ReadData or SendData/SendvData should be in flight until the operation
39// completes. The BidirectionalStream must be torn down before the
40// HttpNetworkSession.
xunjieli11834f02015-12-22 04:27:0841class NET_EXPORT BidirectionalStream
xunjieli5749218c2016-03-22 16:43:0642 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate),
xunjieli11834f02015-12-22 04:27:0843 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) {
44 public:
45 // Delegate interface to get notified of success of failure. Callbacks will be
46 // invoked asynchronously.
47 class NET_EXPORT Delegate {
48 public:
49 Delegate();
50
xunjieli07a42ce2016-04-26 20:05:3151 // Called when the stream is ready for writing and reading. This is called
52 // at most once for the lifetime of a stream.
xunjieli11834f02015-12-22 04:27:0853 // The delegate may call BidirectionalStream::ReadData to start reading,
54 // or call BidirectionalStream::SendData to send data.
55 // The delegate should not call BidirectionalStream::Cancel
56 // during this callback.
xunjielibcb0f86e2016-06-03 00:49:2957 // |request_headers_sent| if true, request headers have been sent. If false,
58 // SendRequestHeaders() needs to be explicitly called.
59 virtual void OnStreamReady(bool request_headers_sent) = 0;
xunjieli11834f02015-12-22 04:27:0860
61 // Called when headers are received. This is called at most once for the
62 // lifetime of a stream.
63 // The delegate may call BidirectionalStream::ReadData to start reading,
64 // call BidirectionalStream::SendData to send data,
65 // or call BidirectionalStream::Cancel to cancel the stream.
66 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0;
67
68 // Called when a pending read is completed asynchronously.
69 // |bytes_read| specifies how much data is read.
70 // The delegate may call BidirectionalStream::ReadData to continue
71 // reading, call BidirectionalStream::SendData to send data,
72 // or call BidirectionalStream::Cancel to cancel the stream.
73 virtual void OnDataRead(int bytes_read) = 0;
74
75 // Called when the entire buffer passed through SendData is sent.
76 // The delegate may call BidirectionalStream::ReadData to continue
77 // reading, call BidirectionalStream::SendData to send data,
78 // The delegate should not call BidirectionalStream::Cancel
79 // during this callback.
80 virtual void OnDataSent() = 0;
81
82 // Called when trailers are received. This is called as soon as trailers
83 // are received, which can happen before a read completes.
84 // The delegate is able to continue reading if there is no pending read and
85 // EOF has not been received, or to send data if there is no pending send.
86 virtual void OnTrailersReceived(const SpdyHeaderBlock& trailers) = 0;
87
xunjieli707f8952016-06-06 15:22:0688 // Called when an error occurred. Do not call into the stream after this
89 // point. No other delegate functions will be called after this.
xunjieli11834f02015-12-22 04:27:0890 virtual void OnFailed(int error) = 0;
91
92 protected:
93 virtual ~Delegate();
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(Delegate);
97 };
98
99 // Constructs a BidirectionalStream. |request_info| contains information about
100 // the request, and must be non-NULL. |session| is the http network session
101 // with which this request will be made. |delegate| must be non-NULL.
102 // |session| and |delegate| must outlive |this|.
xunjielibcb0f86e2016-06-03 00:49:29103 // |send_request_headers_automatically| if true, request headers will be sent
104 // automatically when stream is negotiated. If false, request headers will be
105 // sent only when SendRequestHeaders() is invoked or with
106 // next SendData/SendvData.
danakj1fd259a02016-04-16 03:17:09107 BidirectionalStream(
108 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
109 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29110 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09111 Delegate* delegate);
xunjieli11834f02015-12-22 04:27:08112
113 // Constructor that accepts a Timer, which can be used in tests to control
114 // the buffering of received data.
danakj1fd259a02016-04-16 03:17:09115 BidirectionalStream(
116 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
117 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29118 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09119 Delegate* delegate,
120 std::unique_ptr<base::Timer> timer);
xunjieli11834f02015-12-22 04:27:08121
xunjieli5749218c2016-03-22 16:43:06122 // Cancels |stream_request_| or |stream_impl_| if applicable.
xunjieli11834f02015-12-22 04:27:08123 // |this| should not be destroyed during Delegate::OnHeadersSent or
124 // Delegate::OnDataSent.
125 ~BidirectionalStream() override;
126
xunjielibcb0f86e2016-06-03 00:49:29127 // Sends request headers to server.
128 // When |send_request_headers_automatically_| is
129 // false and OnStreamReady() is invoked with request_headers_sent = false,
130 // headers will be combined with next SendData/SendvData unless this
131 // method is called first, in which case headers will be sent separately
132 // without delay.
133 // (This method cannot be called when |send_request_headers_automatically_| is
134 // true nor when OnStreamReady() is invoked with request_headers_sent = true,
135 // since headers have been sent by the stream when stream is negotiated
136 // successfully.)
137 void SendRequestHeaders();
138
xunjieli11834f02015-12-22 04:27:08139 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read,
140 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an
141 // error code if any error occurred. If returns 0, there is no more data to
142 // read. This should not be called before Delegate::OnHeadersReceived is
143 // invoked, and should not be called again unless it returns with number
144 // greater than 0 or until Delegate::OnDataRead is invoked.
145 int ReadData(IOBuffer* buf, int buf_len);
146
147 // Sends data. This should not be called before Delegate::OnHeadersSent is
148 // invoked, and should not be called again until Delegate::OnDataSent is
149 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM
150 // flag.
xunjieli2328a2682016-05-16 19:38:25151 void SendData(const scoped_refptr<IOBuffer>& data,
152 int length,
153 bool end_stream);
xunjieli11834f02015-12-22 04:27:08154
xunjieli07a42ce2016-04-26 20:05:31155 // Same as SendData except this takes in a vector of IOBuffers.
xunjieli2328a2682016-05-16 19:38:25156 void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
xunjieli07a42ce2016-04-26 20:05:31157 const std::vector<int>& lengths,
158 bool end_stream);
159
xunjieli11834f02015-12-22 04:27:08160 // Returns the protocol used by this stream. If stream has not been
161 // established, return kProtoUnknown.
162 NextProto GetProtocol() const;
163
164 // Total number of bytes received over the network of SPDY data, headers, and
165 // push_promise frames associated with this stream, including the size of
166 // frame headers, after SSL decryption and not including proxy overhead.
167 // If stream has not been established, return 0.
168 int64_t GetTotalReceivedBytes() const;
169
170 // Total number of bytes sent over the network of SPDY frames associated with
171 // this stream, including the size of frame headers, before SSL encryption and
172 // not including proxy overhead. Note that some SPDY frames such as pings are
173 // not associated with any stream, and are not included in this value.
174 int64_t GetTotalSentBytes() const;
175
xunjieli369d0922016-09-23 18:45:06176 // Gets LoadTimingInfo of this stream.
177 void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
xunjieli11834f02015-12-22 04:27:08178
179 private:
xunjieli5749218c2016-03-22 16:43:06180 // BidirectionalStreamImpl::Delegate implementation:
xunjielibcb0f86e2016-06-03 00:49:29181 void OnStreamReady(bool request_headers_sent) override;
xunjieli11834f02015-12-22 04:27:08182 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override;
183 void OnDataRead(int bytes_read) override;
184 void OnDataSent() override;
185 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override;
186 void OnFailed(int error) override;
187
188 // HttpStreamRequest::Delegate implementation:
189 void OnStreamReady(const SSLConfig& used_ssl_config,
190 const ProxyInfo& used_proxy_info,
191 HttpStream* stream) override;
xunjieli5749218c2016-03-22 16:43:06192 void OnBidirectionalStreamImplReady(
193 const SSLConfig& used_ssl_config,
194 const ProxyInfo& used_proxy_info,
195 BidirectionalStreamImpl* stream_impl) override;
xunjieli11834f02015-12-22 04:27:08196 void OnWebSocketHandshakeStreamReady(
197 const SSLConfig& used_ssl_config,
198 const ProxyInfo& used_proxy_info,
199 WebSocketHandshakeStreamBase* stream) override;
davidben849365422016-06-24 19:04:13200 void OnStreamFailed(int status, const SSLConfig& used_ssl_config) override;
xunjieli11834f02015-12-22 04:27:08201 void OnCertificateError(int status,
202 const SSLConfig& used_ssl_config,
203 const SSLInfo& ssl_info) override;
204 void OnNeedsProxyAuth(const HttpResponseInfo& response_info,
205 const SSLConfig& used_ssl_config,
206 const ProxyInfo& used_proxy_info,
207 HttpAuthController* auth_controller) override;
208 void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
209 SSLCertRequestInfo* cert_info) override;
210 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
211 const SSLConfig& used_ssl_config,
212 const ProxyInfo& used_proxy_info,
213 HttpStream* stream) override;
214 void OnQuicBroken() override;
215
xunjieli3b6ac702016-06-06 21:36:28216 // Helper method to notify delegate if there is an error.
217 void NotifyFailed(int error);
218
xunjielid58621f2016-08-12 18:33:00219 void UpdateHistograms();
220
xunjieli11834f02015-12-22 04:27:08221 // BidirectionalStreamRequestInfo used when requesting the stream.
danakj1fd259a02016-04-16 03:17:09222 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_;
tfarina428341112016-09-22 13:38:20223 const NetLogWithSource net_log_;
xunjieli11834f02015-12-22 04:27:08224
xunjielib3a648e2016-03-22 03:39:51225 HttpNetworkSession* session_;
226
xunjielibcb0f86e2016-06-03 00:49:29227 bool send_request_headers_automatically_;
228 // Whether request headers have been sent, as indicated in OnStreamReady()
229 // callback.
230 bool request_headers_sent_;
231
xunjieli11834f02015-12-22 04:27:08232 Delegate* const delegate_;
233
234 // Timer used to buffer data received in short time-spans and send a single
235 // read completion notification.
danakj1fd259a02016-04-16 03:17:09236 std::unique_ptr<base::Timer> timer_;
xunjieli5749218c2016-03-22 16:43:06237 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL
238 // if the request has been canceled or completed.
danakj1fd259a02016-04-16 03:17:09239 std::unique_ptr<HttpStreamRequest> stream_request_;
xunjieli5749218c2016-03-22 16:43:06240 // The underlying BidirectioanlStreamImpl used for this stream. It is
241 // non-NULL, if the |stream_request_| successfully finishes.
danakj1fd259a02016-04-16 03:17:09242 std::unique_ptr<BidirectionalStreamImpl> stream_impl_;
xunjieli11834f02015-12-22 04:27:08243
xunjielibe07785e2016-04-14 21:15:29244 // Buffer used for reading.
245 scoped_refptr<IOBuffer> read_buffer_;
xunjieli07a42ce2016-04-26 20:05:31246 // List of buffers used for writing.
247 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_;
248 // List of buffer length.
249 std::vector<int> write_buffer_len_list_;
xunjielibe07785e2016-04-14 21:15:29250
xunjieli369d0922016-09-23 18:45:06251 // TODO(xunjieli): Remove this once LoadTimingInfo has response end.
xunjielid58621f2016-08-12 18:33:00252 base::TimeTicks read_end_time_;
xunjieli369d0922016-09-23 18:45:06253
254 // Load timing info of this stream. |connect_timing| is obtained when headers
255 // are received. Other fields are populated at different stages of the request
256 LoadTimingInfo load_timing_info_;
xunjielid58621f2016-08-12 18:33:00257
xunjieli3b6ac702016-06-06 21:36:28258 base::WeakPtrFactory<BidirectionalStream> weak_factory_;
259
xunjieli11834f02015-12-22 04:27:08260 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream);
261};
262
263} // namespace net
264
265#endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_