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