xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 1 | // 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 | |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 10 | #include <memory> |
Bence Béky | 6d05ebd | 2017-05-16 00:09:01 | [diff] [blame] | 11 | #include <vector> |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 12 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
| 14 | #include "base/macros.h" |
xunjieli | be07785e | 2016-04-14 21:15:29 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
xunjieli | 3b6ac70 | 2016-06-06 21:36:28 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
xunjieli | d58621f | 2016-08-12 18:33:00 | [diff] [blame] | 17 | #include "base/time/time.h" |
xunjieli | 369d092 | 2016-09-23 18:45:06 | [diff] [blame] | 18 | #include "net/base/load_timing_info.h" |
bnc | 81c46c1f | 2016-10-04 16:25:59 | [diff] [blame] | 19 | #include "net/base/net_export.h" |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 20 | #include "net/http/bidirectional_stream_impl.h" |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 21 | #include "net/http/http_stream_factory.h" |
Bence Béky | 6b44abf | 2018-04-11 10:32:51 | [diff] [blame] | 22 | #include "net/http/http_stream_request.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 23 | #include "net/log/net_log_with_source.h" |
Nico Weber | 13d106e | 2015-12-22 21:41:56 | [diff] [blame] | 24 | |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 25 | namespace base { |
| 26 | class OneShotTimer; |
| 27 | } // namespace base |
| 28 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 29 | namespace spdy { |
| 30 | class SpdyHeaderBlock; |
| 31 | } // namespace spdy |
| 32 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 33 | namespace net { |
| 34 | |
| 35 | class HttpAuthController; |
| 36 | class HttpNetworkSession; |
| 37 | class HttpStream; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 38 | class IOBuffer; |
| 39 | class ProxyInfo; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 40 | struct BidirectionalStreamRequestInfo; |
Brad Lassey | 5f488db4 | 2017-07-19 00:30:46 | [diff] [blame] | 41 | struct NetErrorDetails; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 42 | struct SSLConfig; |
| 43 | |
| 44 | // A class to do HTTP/2 bidirectional streaming. Note that at most one each of |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 45 | // ReadData or SendData/SendvData should be in flight until the operation |
| 46 | // completes. The BidirectionalStream must be torn down before the |
| 47 | // HttpNetworkSession. |
Nico Weber | 43ddd7a3 | 2017-08-15 19:19:27 | [diff] [blame] | 48 | class NET_EXPORT BidirectionalStream : public BidirectionalStreamImpl::Delegate, |
| 49 | public HttpStreamRequest::Delegate { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 50 | public: |
| 51 | // Delegate interface to get notified of success of failure. Callbacks will be |
| 52 | // invoked asynchronously. |
| 53 | class NET_EXPORT Delegate { |
| 54 | public: |
| 55 | Delegate(); |
| 56 | |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 57 | // Called when the stream is ready for writing and reading. This is called |
| 58 | // at most once for the lifetime of a stream. |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 59 | // The delegate may call BidirectionalStream::ReadData to start reading, |
| 60 | // or call BidirectionalStream::SendData to send data. |
| 61 | // The delegate should not call BidirectionalStream::Cancel |
| 62 | // during this callback. |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 63 | // |request_headers_sent| if true, request headers have been sent. If false, |
| 64 | // SendRequestHeaders() needs to be explicitly called. |
| 65 | virtual void OnStreamReady(bool request_headers_sent) = 0; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 66 | |
| 67 | // Called when headers are received. This is called at most once for the |
| 68 | // lifetime of a stream. |
| 69 | // The delegate may call BidirectionalStream::ReadData to start reading, |
| 70 | // call BidirectionalStream::SendData to send data, |
| 71 | // or call BidirectionalStream::Cancel to cancel the stream. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 72 | virtual void OnHeadersReceived( |
| 73 | const spdy::SpdyHeaderBlock& response_headers) = 0; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 74 | |
| 75 | // Called when a pending read is completed asynchronously. |
| 76 | // |bytes_read| specifies how much data is read. |
| 77 | // The delegate may call BidirectionalStream::ReadData to continue |
| 78 | // reading, call BidirectionalStream::SendData to send data, |
| 79 | // or call BidirectionalStream::Cancel to cancel the stream. |
| 80 | virtual void OnDataRead(int bytes_read) = 0; |
| 81 | |
| 82 | // Called when the entire buffer passed through SendData is sent. |
| 83 | // The delegate may call BidirectionalStream::ReadData to continue |
| 84 | // reading, call BidirectionalStream::SendData to send data, |
| 85 | // The delegate should not call BidirectionalStream::Cancel |
| 86 | // during this callback. |
| 87 | virtual void OnDataSent() = 0; |
| 88 | |
| 89 | // Called when trailers are received. This is called as soon as trailers |
| 90 | // are received, which can happen before a read completes. |
| 91 | // The delegate is able to continue reading if there is no pending read and |
| 92 | // EOF has not been received, or to send data if there is no pending send. |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 93 | virtual void OnTrailersReceived(const spdy::SpdyHeaderBlock& trailers) = 0; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 94 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 95 | // Called when an error occurred. Do not call into the stream after this |
| 96 | // point. No other delegate functions will be called after this. |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 97 | virtual void OnFailed(int error) = 0; |
| 98 | |
| 99 | protected: |
| 100 | virtual ~Delegate(); |
| 101 | |
| 102 | private: |
| 103 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 104 | }; |
| 105 | |
| 106 | // Constructs a BidirectionalStream. |request_info| contains information about |
| 107 | // the request, and must be non-NULL. |session| is the http network session |
| 108 | // with which this request will be made. |delegate| must be non-NULL. |
| 109 | // |session| and |delegate| must outlive |this|. |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 110 | // |send_request_headers_automatically| if true, request headers will be sent |
| 111 | // automatically when stream is negotiated. If false, request headers will be |
| 112 | // sent only when SendRequestHeaders() is invoked or with |
| 113 | // next SendData/SendvData. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 114 | BidirectionalStream( |
| 115 | std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 116 | HttpNetworkSession* session, |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 117 | bool send_request_headers_automatically, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 118 | Delegate* delegate); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 119 | |
| 120 | // Constructor that accepts a Timer, which can be used in tests to control |
| 121 | // the buffering of received data. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 122 | BidirectionalStream( |
| 123 | std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 124 | HttpNetworkSession* session, |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 125 | bool send_request_headers_automatically, |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 126 | Delegate* delegate, |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 127 | std::unique_ptr<base::OneShotTimer> timer); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 128 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 129 | // Cancels |stream_request_| or |stream_impl_| if applicable. |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 130 | // |this| should not be destroyed during Delegate::OnHeadersSent or |
| 131 | // Delegate::OnDataSent. |
| 132 | ~BidirectionalStream() override; |
| 133 | |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 134 | // Sends request headers to server. |
| 135 | // When |send_request_headers_automatically_| is |
| 136 | // false and OnStreamReady() is invoked with request_headers_sent = false, |
| 137 | // headers will be combined with next SendData/SendvData unless this |
| 138 | // method is called first, in which case headers will be sent separately |
| 139 | // without delay. |
| 140 | // (This method cannot be called when |send_request_headers_automatically_| is |
| 141 | // true nor when OnStreamReady() is invoked with request_headers_sent = true, |
| 142 | // since headers have been sent by the stream when stream is negotiated |
| 143 | // successfully.) |
| 144 | void SendRequestHeaders(); |
| 145 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 146 | // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, |
| 147 | // or ERR_IO_PENDING if the read is to be completed asynchronously, or an |
| 148 | // error code if any error occurred. If returns 0, there is no more data to |
Helen Li | 77b0acd | 2017-11-02 14:25:10 | [diff] [blame] | 149 | // read. This should not be called before Delegate::OnStreamReady is |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 150 | // invoked, and should not be called again unless it returns with number |
| 151 | // greater than 0 or until Delegate::OnDataRead is invoked. |
| 152 | int ReadData(IOBuffer* buf, int buf_len); |
| 153 | |
Helen Li | 77b0acd | 2017-11-02 14:25:10 | [diff] [blame] | 154 | // Sends data. This should not be called before Delegate::OnStreamReady is |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 155 | // invoked, and should not be called again until Delegate::OnDataSent is |
| 156 | // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM |
| 157 | // flag. |
xunjieli | 2328a268 | 2016-05-16 19:38:25 | [diff] [blame] | 158 | void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers, |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 159 | const std::vector<int>& lengths, |
| 160 | bool end_stream); |
| 161 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 162 | // Returns the protocol used by this stream. If stream has not been |
| 163 | // established, return kProtoUnknown. |
| 164 | NextProto GetProtocol() const; |
| 165 | |
| 166 | // Total number of bytes received over the network of SPDY data, headers, and |
| 167 | // push_promise frames associated with this stream, including the size of |
| 168 | // frame headers, after SSL decryption and not including proxy overhead. |
| 169 | // If stream has not been established, return 0. |
| 170 | int64_t GetTotalReceivedBytes() const; |
| 171 | |
| 172 | // Total number of bytes sent over the network of SPDY frames associated with |
| 173 | // this stream, including the size of frame headers, before SSL encryption and |
| 174 | // not including proxy overhead. Note that some SPDY frames such as pings are |
| 175 | // not associated with any stream, and are not included in this value. |
| 176 | int64_t GetTotalSentBytes() const; |
| 177 | |
xunjieli | 369d092 | 2016-09-23 18:45:06 | [diff] [blame] | 178 | // Gets LoadTimingInfo of this stream. |
| 179 | void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 180 | |
Brad Lassey | 5f488db4 | 2017-07-19 00:30:46 | [diff] [blame] | 181 | // Get the network error details this stream is encountering. |
| 182 | // Fills in |details| if it is available; leaves |details| unchanged if it |
| 183 | // is unavailable. |
| 184 | void PopulateNetErrorDetails(NetErrorDetails* details); |
| 185 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 186 | private: |
Helen Li | 43e3bee | 2018-03-21 16:57:11 | [diff] [blame] | 187 | void StartRequest(const SSLConfig& ssl_config); |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 188 | // BidirectionalStreamImpl::Delegate implementation: |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 189 | void OnStreamReady(bool request_headers_sent) override; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 190 | void OnHeadersReceived( |
| 191 | const spdy::SpdyHeaderBlock& response_headers) override; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 192 | void OnDataRead(int bytes_read) override; |
| 193 | void OnDataSent() override; |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 194 | void OnTrailersReceived(const spdy::SpdyHeaderBlock& trailers) override; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 195 | void OnFailed(int error) override; |
| 196 | |
| 197 | // HttpStreamRequest::Delegate implementation: |
| 198 | void OnStreamReady(const SSLConfig& used_ssl_config, |
| 199 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 200 | std::unique_ptr<HttpStream> stream) override; |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 201 | void OnBidirectionalStreamImplReady( |
| 202 | const SSLConfig& used_ssl_config, |
| 203 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 204 | std::unique_ptr<BidirectionalStreamImpl> stream) override; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 205 | void OnWebSocketHandshakeStreamReady( |
| 206 | const SSLConfig& used_ssl_config, |
| 207 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 208 | std::unique_ptr<WebSocketHandshakeStreamBase> stream) override; |
Ryan Hamilton | 75f19726 | 2017-08-17 14:00:07 | [diff] [blame] | 209 | void OnStreamFailed(int status, |
| 210 | const NetErrorDetails& net_error_details, |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame^] | 211 | const SSLConfig& used_ssl_config, |
| 212 | const ProxyInfo& used_proxy_info) override; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 213 | void OnCertificateError(int status, |
| 214 | const SSLConfig& used_ssl_config, |
| 215 | const SSLInfo& ssl_info) override; |
| 216 | void OnNeedsProxyAuth(const HttpResponseInfo& response_info, |
| 217 | const SSLConfig& used_ssl_config, |
| 218 | const ProxyInfo& used_proxy_info, |
| 219 | HttpAuthController* auth_controller) override; |
| 220 | void OnNeedsClientAuth(const SSLConfig& used_ssl_config, |
| 221 | SSLCertRequestInfo* cert_info) override; |
Eric Roman | 74103c7 | 2019-02-21 00:23:12 | [diff] [blame] | 222 | void OnHttpsProxyTunnelResponseRedirect( |
| 223 | const HttpResponseInfo& response_info, |
| 224 | const SSLConfig& used_ssl_config, |
| 225 | const ProxyInfo& used_proxy_info, |
| 226 | std::unique_ptr<HttpStream> stream) override; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 227 | void OnQuicBroken() override; |
| 228 | |
xunjieli | 3b6ac70 | 2016-06-06 21:36:28 | [diff] [blame] | 229 | // Helper method to notify delegate if there is an error. |
| 230 | void NotifyFailed(int error); |
| 231 | |
xunjieli | d58621f | 2016-08-12 18:33:00 | [diff] [blame] | 232 | void UpdateHistograms(); |
| 233 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 234 | // BidirectionalStreamRequestInfo used when requesting the stream. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 235 | std::unique_ptr<BidirectionalStreamRequestInfo> request_info_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 236 | const NetLogWithSource net_log_; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 237 | |
xunjieli | b3a648e | 2016-03-22 03:39:51 | [diff] [blame] | 238 | HttpNetworkSession* session_; |
| 239 | |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 240 | bool send_request_headers_automatically_; |
| 241 | // Whether request headers have been sent, as indicated in OnStreamReady() |
| 242 | // callback. |
| 243 | bool request_headers_sent_; |
| 244 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 245 | Delegate* const delegate_; |
| 246 | |
| 247 | // Timer used to buffer data received in short time-spans and send a single |
| 248 | // read completion notification. |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 249 | std::unique_ptr<base::OneShotTimer> timer_; |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 250 | // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL |
| 251 | // if the request has been canceled or completed. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 252 | std::unique_ptr<HttpStreamRequest> stream_request_; |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 253 | // The underlying BidirectioanlStreamImpl used for this stream. It is |
| 254 | // non-NULL, if the |stream_request_| successfully finishes. |
danakj | 1fd259a0 | 2016-04-16 03:17:09 | [diff] [blame] | 255 | std::unique_ptr<BidirectionalStreamImpl> stream_impl_; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 256 | |
xunjieli | be07785e | 2016-04-14 21:15:29 | [diff] [blame] | 257 | // Buffer used for reading. |
| 258 | scoped_refptr<IOBuffer> read_buffer_; |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 259 | // List of buffers used for writing. |
| 260 | std::vector<scoped_refptr<IOBuffer>> write_buffer_list_; |
| 261 | // List of buffer length. |
| 262 | std::vector<int> write_buffer_len_list_; |
xunjieli | be07785e | 2016-04-14 21:15:29 | [diff] [blame] | 263 | |
xunjieli | 369d092 | 2016-09-23 18:45:06 | [diff] [blame] | 264 | // TODO(xunjieli): Remove this once LoadTimingInfo has response end. |
xunjieli | d58621f | 2016-08-12 18:33:00 | [diff] [blame] | 265 | base::TimeTicks read_end_time_; |
xunjieli | 369d092 | 2016-09-23 18:45:06 | [diff] [blame] | 266 | |
| 267 | // Load timing info of this stream. |connect_timing| is obtained when headers |
| 268 | // are received. Other fields are populated at different stages of the request |
| 269 | LoadTimingInfo load_timing_info_; |
xunjieli | d58621f | 2016-08-12 18:33:00 | [diff] [blame] | 270 | |
xunjieli | 3b6ac70 | 2016-06-06 21:36:28 | [diff] [blame] | 271 | base::WeakPtrFactory<BidirectionalStream> weak_factory_; |
| 272 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 273 | DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
| 274 | }; |
| 275 | |
| 276 | } // namespace net |
| 277 | |
| 278 | #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ |