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