blob: bf0f323ff12c99f3f6e54ec344c42e2b53d36327 [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>
Bence Béky6d05ebd2017-05-16 00:09:0111#include <vector>
danakj1fd259a02016-04-16 03:17:0912
xunjieli11834f02015-12-22 04:27:0813#include "base/compiler_specific.h"
14#include "base/macros.h"
xunjielibe07785e2016-04-14 21:15:2915#include "base/memory/ref_counted.h"
xunjieli3b6ac702016-06-06 21:36:2816#include "base/memory/weak_ptr.h"
xunjielid58621f2016-08-12 18:33:0017#include "base/time/time.h"
xunjieli369d0922016-09-23 18:45:0618#include "net/base/load_timing_info.h"
bnc81c46c1f2016-10-04 16:25:5919#include "net/base/net_export.h"
xunjieli5749218c2016-03-22 16:43:0620#include "net/http/bidirectional_stream_impl.h"
xunjieli11834f02015-12-22 04:27:0821#include "net/http/http_stream_factory.h"
Bence Béky6b44abf2018-04-11 10:32:5122#include "net/http/http_stream_request.h"
mikecironef22f9812016-10-04 03:40:1923#include "net/log/net_log_with_source.h"
Nico Weber13d106e2015-12-22 21:41:5624
Ryan Hamilton0239aac2018-05-19 00:03:1325namespace spdy {
26class SpdyHeaderBlock;
27} // namespace spdy
28
xunjieli11834f02015-12-22 04:27:0829namespace net {
30
31class HttpAuthController;
32class HttpNetworkSession;
33class HttpStream;
xunjieli11834f02015-12-22 04:27:0834class IOBuffer;
35class ProxyInfo;
xunjieli11834f02015-12-22 04:27:0836struct BidirectionalStreamRequestInfo;
Brad Lassey5f488db42017-07-19 00:30:4637struct NetErrorDetails;
xunjieli11834f02015-12-22 04:27:0838struct SSLConfig;
39
40// A class to do HTTP/2 bidirectional streaming. Note that at most one each of
xunjieli07a42ce2016-04-26 20:05:3141// ReadData or SendData/SendvData should be in flight until the operation
42// completes. The BidirectionalStream must be torn down before the
43// HttpNetworkSession.
Nico Weber43ddd7a32017-08-15 19:19:2744class NET_EXPORT BidirectionalStream : public BidirectionalStreamImpl::Delegate,
45 public HttpStreamRequest::Delegate {
xunjieli11834f02015-12-22 04:27:0846 public:
47 // Delegate interface to get notified of success of failure. Callbacks will be
48 // invoked asynchronously.
49 class NET_EXPORT Delegate {
50 public:
51 Delegate();
52
xunjieli07a42ce2016-04-26 20:05:3153 // Called when the stream is ready for writing and reading. This is called
54 // at most once for the lifetime of a stream.
xunjieli11834f02015-12-22 04:27:0855 // The delegate may call BidirectionalStream::ReadData to start reading,
56 // or call BidirectionalStream::SendData to send data.
57 // The delegate should not call BidirectionalStream::Cancel
58 // during this callback.
xunjielibcb0f86e2016-06-03 00:49:2959 // |request_headers_sent| if true, request headers have been sent. If false,
60 // SendRequestHeaders() needs to be explicitly called.
61 virtual void OnStreamReady(bool request_headers_sent) = 0;
xunjieli11834f02015-12-22 04:27:0862
63 // Called when headers are received. This is called at most once for the
64 // lifetime of a stream.
65 // The delegate may call BidirectionalStream::ReadData to start reading,
66 // call BidirectionalStream::SendData to send data,
67 // or call BidirectionalStream::Cancel to cancel the stream.
Ryan Hamilton0239aac2018-05-19 00:03:1368 virtual void OnHeadersReceived(
69 const spdy::SpdyHeaderBlock& response_headers) = 0;
xunjieli11834f02015-12-22 04:27:0870
71 // Called when a pending read is completed asynchronously.
72 // |bytes_read| specifies how much data is read.
73 // The delegate may call BidirectionalStream::ReadData to continue
74 // reading, call BidirectionalStream::SendData to send data,
75 // or call BidirectionalStream::Cancel to cancel the stream.
76 virtual void OnDataRead(int bytes_read) = 0;
77
78 // Called when the entire buffer passed through SendData is sent.
79 // The delegate may call BidirectionalStream::ReadData to continue
80 // reading, call BidirectionalStream::SendData to send data,
81 // The delegate should not call BidirectionalStream::Cancel
82 // during this callback.
83 virtual void OnDataSent() = 0;
84
85 // Called when trailers are received. This is called as soon as trailers
86 // are received, which can happen before a read completes.
87 // The delegate is able to continue reading if there is no pending read and
88 // EOF has not been received, or to send data if there is no pending send.
Ryan Hamilton0239aac2018-05-19 00:03:1389 virtual void OnTrailersReceived(const spdy::SpdyHeaderBlock& trailers) = 0;
xunjieli11834f02015-12-22 04:27:0890
xunjieli707f8952016-06-06 15:22:0691 // Called when an error occurred. Do not call into the stream after this
92 // point. No other delegate functions will be called after this.
xunjieli11834f02015-12-22 04:27:0893 virtual void OnFailed(int error) = 0;
94
95 protected:
96 virtual ~Delegate();
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(Delegate);
100 };
101
102 // Constructs a BidirectionalStream. |request_info| contains information about
103 // the request, and must be non-NULL. |session| is the http network session
104 // with which this request will be made. |delegate| must be non-NULL.
105 // |session| and |delegate| must outlive |this|.
xunjielibcb0f86e2016-06-03 00:49:29106 // |send_request_headers_automatically| if true, request headers will be sent
107 // automatically when stream is negotiated. If false, request headers will be
108 // sent only when SendRequestHeaders() is invoked or with
109 // next SendData/SendvData.
danakj1fd259a02016-04-16 03:17:09110 BidirectionalStream(
111 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
112 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29113 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09114 Delegate* delegate);
xunjieli11834f02015-12-22 04:27:08115
116 // Constructor that accepts a Timer, which can be used in tests to control
117 // the buffering of received data.
danakj1fd259a02016-04-16 03:17:09118 BidirectionalStream(
119 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
120 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29121 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09122 Delegate* delegate,
123 std::unique_ptr<base::Timer> timer);
xunjieli11834f02015-12-22 04:27:08124
xunjieli5749218c2016-03-22 16:43:06125 // Cancels |stream_request_| or |stream_impl_| if applicable.
xunjieli11834f02015-12-22 04:27:08126 // |this| should not be destroyed during Delegate::OnHeadersSent or
127 // Delegate::OnDataSent.
128 ~BidirectionalStream() override;
129
xunjielibcb0f86e2016-06-03 00:49:29130 // Sends request headers to server.
131 // When |send_request_headers_automatically_| is
132 // false and OnStreamReady() is invoked with request_headers_sent = false,
133 // headers will be combined with next SendData/SendvData unless this
134 // method is called first, in which case headers will be sent separately
135 // without delay.
136 // (This method cannot be called when |send_request_headers_automatically_| is
137 // true nor when OnStreamReady() is invoked with request_headers_sent = true,
138 // since headers have been sent by the stream when stream is negotiated
139 // successfully.)
140 void SendRequestHeaders();
141
xunjieli11834f02015-12-22 04:27:08142 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read,
143 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an
144 // error code if any error occurred. If returns 0, there is no more data to
Helen Li77b0acd2017-11-02 14:25:10145 // read. This should not be called before Delegate::OnStreamReady is
xunjieli11834f02015-12-22 04:27:08146 // invoked, and should not be called again unless it returns with number
147 // greater than 0 or until Delegate::OnDataRead is invoked.
148 int ReadData(IOBuffer* buf, int buf_len);
149
Helen Li77b0acd2017-11-02 14:25:10150 // Sends data. This should not be called before Delegate::OnStreamReady is
xunjieli11834f02015-12-22 04:27:08151 // invoked, and should not be called again until Delegate::OnDataSent is
152 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM
153 // flag.
xunjieli2328a2682016-05-16 19:38:25154 void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
xunjieli07a42ce2016-04-26 20:05:31155 const std::vector<int>& lengths,
156 bool end_stream);
157
xunjieli11834f02015-12-22 04:27:08158 // Returns the protocol used by this stream. If stream has not been
159 // established, return kProtoUnknown.
160 NextProto GetProtocol() const;
161
162 // Total number of bytes received over the network of SPDY data, headers, and
163 // push_promise frames associated with this stream, including the size of
164 // frame headers, after SSL decryption and not including proxy overhead.
165 // If stream has not been established, return 0.
166 int64_t GetTotalReceivedBytes() const;
167
168 // Total number of bytes sent over the network of SPDY frames associated with
169 // this stream, including the size of frame headers, before SSL encryption and
170 // not including proxy overhead. Note that some SPDY frames such as pings are
171 // not associated with any stream, and are not included in this value.
172 int64_t GetTotalSentBytes() const;
173
xunjieli369d0922016-09-23 18:45:06174 // Gets LoadTimingInfo of this stream.
175 void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
xunjieli11834f02015-12-22 04:27:08176
Brad Lassey5f488db42017-07-19 00:30:46177 // Get the network error details this stream is encountering.
178 // Fills in |details| if it is available; leaves |details| unchanged if it
179 // is unavailable.
180 void PopulateNetErrorDetails(NetErrorDetails* details);
181
xunjieli11834f02015-12-22 04:27:08182 private:
Helen Li43e3bee2018-03-21 16:57:11183 void StartRequest(const SSLConfig& ssl_config);
xunjieli5749218c2016-03-22 16:43:06184 // BidirectionalStreamImpl::Delegate implementation:
xunjielibcb0f86e2016-06-03 00:49:29185 void OnStreamReady(bool request_headers_sent) override;
Ryan Hamilton0239aac2018-05-19 00:03:13186 void OnHeadersReceived(
187 const spdy::SpdyHeaderBlock& response_headers) override;
xunjieli11834f02015-12-22 04:27:08188 void OnDataRead(int bytes_read) override;
189 void OnDataSent() override;
Ryan Hamilton0239aac2018-05-19 00:03:13190 void OnTrailersReceived(const spdy::SpdyHeaderBlock& trailers) override;
xunjieli11834f02015-12-22 04:27:08191 void OnFailed(int error) override;
192
193 // HttpStreamRequest::Delegate implementation:
194 void OnStreamReady(const SSLConfig& used_ssl_config,
195 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00196 std::unique_ptr<HttpStream> stream) override;
xunjieli5749218c2016-03-22 16:43:06197 void OnBidirectionalStreamImplReady(
198 const SSLConfig& used_ssl_config,
199 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00200 std::unique_ptr<BidirectionalStreamImpl> stream) override;
xunjieli11834f02015-12-22 04:27:08201 void OnWebSocketHandshakeStreamReady(
202 const SSLConfig& used_ssl_config,
203 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00204 std::unique_ptr<WebSocketHandshakeStreamBase> stream) override;
Ryan Hamilton75f197262017-08-17 14:00:07205 void OnStreamFailed(int status,
206 const NetErrorDetails& net_error_details,
207 const SSLConfig& used_ssl_config) override;
xunjieli11834f02015-12-22 04:27:08208 void OnCertificateError(int status,
209 const SSLConfig& used_ssl_config,
210 const SSLInfo& ssl_info) override;
211 void OnNeedsProxyAuth(const HttpResponseInfo& response_info,
212 const SSLConfig& used_ssl_config,
213 const ProxyInfo& used_proxy_info,
214 HttpAuthController* auth_controller) override;
215 void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
216 SSLCertRequestInfo* cert_info) override;
217 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
218 const SSLConfig& used_ssl_config,
219 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01220 std::unique_ptr<HttpStream> stream) override;
xunjieli11834f02015-12-22 04:27:08221 void OnQuicBroken() override;
222
xunjieli3b6ac702016-06-06 21:36:28223 // Helper method to notify delegate if there is an error.
224 void NotifyFailed(int error);
225
xunjielid58621f2016-08-12 18:33:00226 void UpdateHistograms();
227
xunjieli11834f02015-12-22 04:27:08228 // BidirectionalStreamRequestInfo used when requesting the stream.
danakj1fd259a02016-04-16 03:17:09229 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_;
tfarina428341112016-09-22 13:38:20230 const NetLogWithSource net_log_;
xunjieli11834f02015-12-22 04:27:08231
xunjielib3a648e2016-03-22 03:39:51232 HttpNetworkSession* session_;
233
xunjielibcb0f86e2016-06-03 00:49:29234 bool send_request_headers_automatically_;
235 // Whether request headers have been sent, as indicated in OnStreamReady()
236 // callback.
237 bool request_headers_sent_;
238
xunjieli11834f02015-12-22 04:27:08239 Delegate* const delegate_;
240
241 // Timer used to buffer data received in short time-spans and send a single
242 // read completion notification.
danakj1fd259a02016-04-16 03:17:09243 std::unique_ptr<base::Timer> timer_;
xunjieli5749218c2016-03-22 16:43:06244 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL
245 // if the request has been canceled or completed.
danakj1fd259a02016-04-16 03:17:09246 std::unique_ptr<HttpStreamRequest> stream_request_;
xunjieli5749218c2016-03-22 16:43:06247 // The underlying BidirectioanlStreamImpl used for this stream. It is
248 // non-NULL, if the |stream_request_| successfully finishes.
danakj1fd259a02016-04-16 03:17:09249 std::unique_ptr<BidirectionalStreamImpl> stream_impl_;
xunjieli11834f02015-12-22 04:27:08250
xunjielibe07785e2016-04-14 21:15:29251 // Buffer used for reading.
252 scoped_refptr<IOBuffer> read_buffer_;
xunjieli07a42ce2016-04-26 20:05:31253 // List of buffers used for writing.
254 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_;
255 // List of buffer length.
256 std::vector<int> write_buffer_len_list_;
xunjielibe07785e2016-04-14 21:15:29257
xunjieli369d0922016-09-23 18:45:06258 // TODO(xunjieli): Remove this once LoadTimingInfo has response end.
xunjielid58621f2016-08-12 18:33:00259 base::TimeTicks read_end_time_;
xunjieli369d0922016-09-23 18:45:06260
261 // Load timing info of this stream. |connect_timing| is obtained when headers
262 // are received. Other fields are populated at different stages of the request
263 LoadTimingInfo load_timing_info_;
xunjielid58621f2016-08-12 18:33:00264
xunjieli3b6ac702016-06-06 21:36:28265 base::WeakPtrFactory<BidirectionalStream> weak_factory_;
266
xunjieli11834f02015-12-22 04:27:08267 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream);
268};
269
270} // namespace net
271
272#endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_