OLD | NEW |
(Empty) | |
| 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 |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "net/http/bidirectional_stream_job.h" |
| 14 #include "net/http/http_stream_factory.h" |
| 15 #include "net/log/net_log.h" |
| 16 |
| 17 class GURL; |
| 18 |
| 19 namespace net { |
| 20 |
| 21 class HttpAuthController; |
| 22 class HttpNetworkSession; |
| 23 class HttpStream; |
| 24 class HttpStreamRequest; |
| 25 class IOBuffer; |
| 26 class ProxyInfo; |
| 27 class SpdyHeaderBlock; |
| 28 struct BidirectionalStreamRequestInfo; |
| 29 struct SSLConfig; |
| 30 |
| 31 // A class to do HTTP/2 bidirectional streaming. Note that at most one each of |
| 32 // ReadData or SendData should be in flight until the operation completes. |
| 33 // The BidirectionalStream must be torn down before the HttpNetworkSession. |
| 34 class NET_EXPORT BidirectionalStream |
| 35 : public NON_EXPORTED_BASE(BidirectionalStreamJob::Delegate), |
| 36 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) { |
| 37 public: |
| 38 // Delegate interface to get notified of success of failure. Callbacks will be |
| 39 // invoked asynchronously. |
| 40 class NET_EXPORT Delegate { |
| 41 public: |
| 42 Delegate(); |
| 43 |
| 44 // Called when headers have been sent. This is called at most once for |
| 45 // the lifetime of a stream. |
| 46 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 47 // or call BidirectionalStream::SendData to send data. |
| 48 // The delegate should not call BidirectionalStream::Cancel |
| 49 // during this callback. |
| 50 virtual void OnHeadersSent() = 0; |
| 51 |
| 52 // Called when headers are received. This is called at most once for the |
| 53 // lifetime of a stream. |
| 54 // The delegate may call BidirectionalStream::ReadData to start reading, |
| 55 // call BidirectionalStream::SendData to send data, |
| 56 // or call BidirectionalStream::Cancel to cancel the stream. |
| 57 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0; |
| 58 |
| 59 // Called when a pending read is completed asynchronously. |
| 60 // |bytes_read| specifies how much data is read. |
| 61 // The delegate may call BidirectionalStream::ReadData to continue |
| 62 // reading, call BidirectionalStream::SendData to send data, |
| 63 // or call BidirectionalStream::Cancel to cancel the stream. |
| 64 virtual void OnDataRead(int bytes_read) = 0; |
| 65 |
| 66 // Called when the entire buffer passed through SendData is sent. |
| 67 // The delegate may call BidirectionalStream::ReadData to continue |
| 68 // reading, call BidirectionalStream::SendData to send data, |
| 69 // The delegate should not call BidirectionalStream::Cancel |
| 70 // during this callback. |
| 71 virtual void OnDataSent() = 0; |
| 72 |
| 73 // Called when trailers are received. This is called as soon as trailers |
| 74 // are received, which can happen before a read completes. |
| 75 // The delegate is able to continue reading if there is no pending read and |
| 76 // EOF has not been received, or to send data if there is no pending send. |
| 77 virtual void OnTrailersReceived(const SpdyHeaderBlock& trailers) = 0; |
| 78 |
| 79 // Called when the stream is closed or an error occurred. |
| 80 // No other delegate functions will be called after this. |
| 81 virtual void OnFailed(int error) = 0; |
| 82 |
| 83 protected: |
| 84 virtual ~Delegate(); |
| 85 |
| 86 private: |
| 87 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 88 }; |
| 89 |
| 90 // Constructs a BidirectionalStream. |request_info| contains information about |
| 91 // the request, and must be non-NULL. |session| is the http network session |
| 92 // with which this request will be made. |delegate| must be non-NULL. |
| 93 // |session| and |delegate| must outlive |this|. |
| 94 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, |
| 95 HttpNetworkSession* session, |
| 96 Delegate* delegate); |
| 97 |
| 98 // Constructor that accepts a Timer, which can be used in tests to control |
| 99 // the buffering of received data. |
| 100 BidirectionalStream(scoped_ptr<BidirectionalStreamRequestInfo> request_info, |
| 101 HttpNetworkSession* session, |
| 102 Delegate* delegate, |
| 103 scoped_ptr<base::Timer> timer); |
| 104 |
| 105 // Cancels |stream_request_| or |stream_job_| if applicable. |
| 106 // |this| should not be destroyed during Delegate::OnHeadersSent or |
| 107 // Delegate::OnDataSent. |
| 108 ~BidirectionalStream() override; |
| 109 |
| 110 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read, |
| 111 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an |
| 112 // error code if any error occurred. If returns 0, there is no more data to |
| 113 // read. This should not be called before Delegate::OnHeadersReceived is |
| 114 // invoked, and should not be called again unless it returns with number |
| 115 // greater than 0 or until Delegate::OnDataRead is invoked. |
| 116 int ReadData(IOBuffer* buf, int buf_len); |
| 117 |
| 118 // Sends data. This should not be called before Delegate::OnHeadersSent is |
| 119 // invoked, and should not be called again until Delegate::OnDataSent is |
| 120 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM |
| 121 // flag. |
| 122 void SendData(IOBuffer* data, int length, bool end_stream); |
| 123 |
| 124 // If |stream_request_| is non-NULL, cancel it. If |stream_job_| is |
| 125 // established, cancel it. No delegate method will be called after Cancel(). |
| 126 // Any pending operations may or may not succeed. |
| 127 void Cancel(); |
| 128 |
| 129 // Returns the protocol used by this stream. If stream has not been |
| 130 // established, return kProtoUnknown. |
| 131 NextProto GetProtocol() const; |
| 132 |
| 133 // Total number of bytes received over the network of SPDY data, headers, and |
| 134 // push_promise frames associated with this stream, including the size of |
| 135 // frame headers, after SSL decryption and not including proxy overhead. |
| 136 // If stream has not been established, return 0. |
| 137 int64_t GetTotalReceivedBytes() const; |
| 138 |
| 139 // Total number of bytes sent over the network of SPDY frames associated with |
| 140 // this stream, including the size of frame headers, before SSL encryption and |
| 141 // not including proxy overhead. Note that some SPDY frames such as pings are |
| 142 // not associated with any stream, and are not included in this value. |
| 143 int64_t GetTotalSentBytes() const; |
| 144 |
| 145 // TODO(xunjieli): Implement a method to do flow control and a method to ping |
| 146 // remote end point. |
| 147 |
| 148 private: |
| 149 // BidirectionalStreamJob::Delegate implementation: |
| 150 void OnHeadersSent() override; |
| 151 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override; |
| 152 void OnDataRead(int bytes_read) override; |
| 153 void OnDataSent() override; |
| 154 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override; |
| 155 void OnFailed(int error) override; |
| 156 |
| 157 // HttpStreamRequest::Delegate implementation: |
| 158 void OnStreamReady(const SSLConfig& used_ssl_config, |
| 159 const ProxyInfo& used_proxy_info, |
| 160 HttpStream* stream) override; |
| 161 void OnBidirectionalStreamJobReady(const SSLConfig& used_ssl_config, |
| 162 const ProxyInfo& used_proxy_info, |
| 163 BidirectionalStreamJob* stream) override; |
| 164 void OnWebSocketHandshakeStreamReady( |
| 165 const SSLConfig& used_ssl_config, |
| 166 const ProxyInfo& used_proxy_info, |
| 167 WebSocketHandshakeStreamBase* stream) override; |
| 168 void OnStreamFailed(int status, |
| 169 const SSLConfig& used_ssl_config, |
| 170 SSLFailureState ssl_failure_state) override; |
| 171 void OnCertificateError(int status, |
| 172 const SSLConfig& used_ssl_config, |
| 173 const SSLInfo& ssl_info) override; |
| 174 void OnNeedsProxyAuth(const HttpResponseInfo& response_info, |
| 175 const SSLConfig& used_ssl_config, |
| 176 const ProxyInfo& used_proxy_info, |
| 177 HttpAuthController* auth_controller) override; |
| 178 void OnNeedsClientAuth(const SSLConfig& used_ssl_config, |
| 179 SSLCertRequestInfo* cert_info) override; |
| 180 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info, |
| 181 const SSLConfig& used_ssl_config, |
| 182 const ProxyInfo& used_proxy_info, |
| 183 HttpStream* stream) override; |
| 184 void OnQuicBroken() override; |
| 185 |
| 186 // BidirectionalStreamRequestInfo used when requesting the stream. |
| 187 scoped_ptr<BidirectionalStreamRequestInfo> request_info_; |
| 188 const BoundNetLog net_log_; |
| 189 |
| 190 Delegate* const delegate_; |
| 191 |
| 192 // Timer used to buffer data received in short time-spans and send a single |
| 193 // read completion notification. |
| 194 scoped_ptr<base::Timer> timer_; |
| 195 // HttpStreamRequest used to request a BidirectionalStreamJob. This is NULL if |
| 196 // the request has been canceled or completed. |
| 197 scoped_ptr<HttpStreamRequest> stream_request_; |
| 198 // The underlying BidirectioanlStreamJob used for this stream. It is non-NULL, |
| 199 // if the |stream_request_| successfully finishes. |
| 200 scoped_ptr<BidirectionalStreamJob> stream_job_; |
| 201 |
| 202 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream); |
| 203 }; |
| 204 |
| 205 } // namespace net |
| 206 |
| 207 #endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_ |
OLD | NEW |