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