blob: e72e7fe44c6401da9f3b15825fe012b56110d956 [file] [log] [blame]
xunjieli11834f02015-12-22 04:27:081// 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
danakj1fd259a02016-04-16 03:17:0910#include <memory>
Bence Béky6d05ebd2017-05-16 00:09:0111#include <vector>
danakj1fd259a02016-04-16 03:17:0912
xunjieli11834f02015-12-22 04:27:0813#include "base/compiler_specific.h"
14#include "base/macros.h"
xunjielibe07785e2016-04-14 21:15:2915#include "base/memory/ref_counted.h"
xunjieli3b6ac702016-06-06 21:36:2816#include "base/memory/weak_ptr.h"
xunjielid58621f2016-08-12 18:33:0017#include "base/time/time.h"
xunjieli369d0922016-09-23 18:45:0618#include "net/base/load_timing_info.h"
bnc81c46c1f2016-10-04 16:25:5919#include "net/base/net_export.h"
xunjieli5749218c2016-03-22 16:43:0620#include "net/http/bidirectional_stream_impl.h"
xunjieli11834f02015-12-22 04:27:0821#include "net/http/http_stream_factory.h"
mikecironef22f9812016-10-04 03:40:1922#include "net/log/net_log_with_source.h"
Nico Weber13d106e2015-12-22 21:41:5623
xunjieli11834f02015-12-22 04:27:0824namespace net {
25
26class HttpAuthController;
27class HttpNetworkSession;
28class HttpStream;
29class HttpStreamRequest;
30class IOBuffer;
31class ProxyInfo;
32class SpdyHeaderBlock;
33struct BidirectionalStreamRequestInfo;
34struct SSLConfig;
35
36// A class to do HTTP/2 bidirectional streaming. Note that at most one each of
xunjieli07a42ce2016-04-26 20:05:3137// ReadData or SendData/SendvData should be in flight until the operation
38// completes. The BidirectionalStream must be torn down before the
39// HttpNetworkSession.
xunjieli11834f02015-12-22 04:27:0840class NET_EXPORT BidirectionalStream
xunjieli5749218c2016-03-22 16:43:0641 : public NON_EXPORTED_BASE(BidirectionalStreamImpl::Delegate),
xunjieli11834f02015-12-22 04:27:0842 public NON_EXPORTED_BASE(HttpStreamRequest::Delegate) {
43 public:
44 // Delegate interface to get notified of success of failure. Callbacks will be
45 // invoked asynchronously.
46 class NET_EXPORT Delegate {
47 public:
48 Delegate();
49
xunjieli07a42ce2016-04-26 20:05:3150 // Called when the stream is ready for writing and reading. This is called
51 // at most once for the lifetime of a stream.
xunjieli11834f02015-12-22 04:27:0852 // The delegate may call BidirectionalStream::ReadData to start reading,
53 // or call BidirectionalStream::SendData to send data.
54 // The delegate should not call BidirectionalStream::Cancel
55 // during this callback.
xunjielibcb0f86e2016-06-03 00:49:2956 // |request_headers_sent| if true, request headers have been sent. If false,
57 // SendRequestHeaders() needs to be explicitly called.
58 virtual void OnStreamReady(bool request_headers_sent) = 0;
xunjieli11834f02015-12-22 04:27:0859
60 // Called when headers are received. This is called at most once for the
61 // lifetime of a stream.
62 // The delegate may call BidirectionalStream::ReadData to start reading,
63 // call BidirectionalStream::SendData to send data,
64 // or call BidirectionalStream::Cancel to cancel the stream.
65 virtual void OnHeadersReceived(const SpdyHeaderBlock& response_headers) = 0;
66
67 // Called when a pending read is completed asynchronously.
68 // |bytes_read| specifies how much data is read.
69 // The delegate may call BidirectionalStream::ReadData to continue
70 // reading, call BidirectionalStream::SendData to send data,
71 // or call BidirectionalStream::Cancel to cancel the stream.
72 virtual void OnDataRead(int bytes_read) = 0;
73
74 // Called when the entire buffer passed through SendData is sent.
75 // The delegate may call BidirectionalStream::ReadData to continue
76 // reading, call BidirectionalStream::SendData to send data,
77 // The delegate should not call BidirectionalStream::Cancel
78 // during this callback.
79 virtual void OnDataSent() = 0;
80
81 // Called when trailers are received. This is called as soon as trailers
82 // are received, which can happen before a read completes.
83 // The delegate is able to continue reading if there is no pending read and
84 // EOF has not been received, or to send data if there is no pending send.
85 virtual void OnTrailersReceived(const SpdyHeaderBlock& trailers) = 0;
86
xunjieli707f8952016-06-06 15:22:0687 // Called when an error occurred. Do not call into the stream after this
88 // point. No other delegate functions will be called after this.
xunjieli11834f02015-12-22 04:27:0889 virtual void OnFailed(int error) = 0;
90
91 protected:
92 virtual ~Delegate();
93
94 private:
95 DISALLOW_COPY_AND_ASSIGN(Delegate);
96 };
97
98 // Constructs a BidirectionalStream. |request_info| contains information about
99 // the request, and must be non-NULL. |session| is the http network session
100 // with which this request will be made. |delegate| must be non-NULL.
101 // |session| and |delegate| must outlive |this|.
xunjielibcb0f86e2016-06-03 00:49:29102 // |send_request_headers_automatically| if true, request headers will be sent
103 // automatically when stream is negotiated. If false, request headers will be
104 // sent only when SendRequestHeaders() is invoked or with
105 // next SendData/SendvData.
danakj1fd259a02016-04-16 03:17:09106 BidirectionalStream(
107 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
108 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29109 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09110 Delegate* delegate);
xunjieli11834f02015-12-22 04:27:08111
112 // Constructor that accepts a Timer, which can be used in tests to control
113 // the buffering of received data.
danakj1fd259a02016-04-16 03:17:09114 BidirectionalStream(
115 std::unique_ptr<BidirectionalStreamRequestInfo> request_info,
116 HttpNetworkSession* session,
xunjielibcb0f86e2016-06-03 00:49:29117 bool send_request_headers_automatically,
danakj1fd259a02016-04-16 03:17:09118 Delegate* delegate,
119 std::unique_ptr<base::Timer> timer);
xunjieli11834f02015-12-22 04:27:08120
xunjieli5749218c2016-03-22 16:43:06121 // Cancels |stream_request_| or |stream_impl_| if applicable.
xunjieli11834f02015-12-22 04:27:08122 // |this| should not be destroyed during Delegate::OnHeadersSent or
123 // Delegate::OnDataSent.
124 ~BidirectionalStream() override;
125
xunjielibcb0f86e2016-06-03 00:49:29126 // Sends request headers to server.
127 // When |send_request_headers_automatically_| is
128 // false and OnStreamReady() is invoked with request_headers_sent = false,
129 // headers will be combined with next SendData/SendvData unless this
130 // method is called first, in which case headers will be sent separately
131 // without delay.
132 // (This method cannot be called when |send_request_headers_automatically_| is
133 // true nor when OnStreamReady() is invoked with request_headers_sent = true,
134 // since headers have been sent by the stream when stream is negotiated
135 // successfully.)
136 void SendRequestHeaders();
137
xunjieli11834f02015-12-22 04:27:08138 // Reads at most |buf_len| bytes into |buf|. Returns the number of bytes read,
139 // or ERR_IO_PENDING if the read is to be completed asynchronously, or an
140 // error code if any error occurred. If returns 0, there is no more data to
141 // read. This should not be called before Delegate::OnHeadersReceived is
142 // invoked, and should not be called again unless it returns with number
143 // greater than 0 or until Delegate::OnDataRead is invoked.
144 int ReadData(IOBuffer* buf, int buf_len);
145
146 // Sends data. This should not be called before Delegate::OnHeadersSent is
147 // invoked, and should not be called again until Delegate::OnDataSent is
148 // invoked. If |end_stream| is true, the DATA frame will have an END_STREAM
149 // flag.
xunjieli2328a2682016-05-16 19:38:25150 void SendvData(const std::vector<scoped_refptr<IOBuffer>>& buffers,
xunjieli07a42ce2016-04-26 20:05:31151 const std::vector<int>& lengths,
152 bool end_stream);
153
xunjieli11834f02015-12-22 04:27:08154 // Returns the protocol used by this stream. If stream has not been
155 // established, return kProtoUnknown.
156 NextProto GetProtocol() const;
157
158 // Total number of bytes received over the network of SPDY data, headers, and
159 // push_promise frames associated with this stream, including the size of
160 // frame headers, after SSL decryption and not including proxy overhead.
161 // If stream has not been established, return 0.
162 int64_t GetTotalReceivedBytes() const;
163
164 // Total number of bytes sent over the network of SPDY frames associated with
165 // this stream, including the size of frame headers, before SSL encryption and
166 // not including proxy overhead. Note that some SPDY frames such as pings are
167 // not associated with any stream, and are not included in this value.
168 int64_t GetTotalSentBytes() const;
169
xunjieli369d0922016-09-23 18:45:06170 // Gets LoadTimingInfo of this stream.
171 void GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const;
xunjieli11834f02015-12-22 04:27:08172
173 private:
xunjieli5749218c2016-03-22 16:43:06174 // BidirectionalStreamImpl::Delegate implementation:
xunjielibcb0f86e2016-06-03 00:49:29175 void OnStreamReady(bool request_headers_sent) override;
xunjieli11834f02015-12-22 04:27:08176 void OnHeadersReceived(const SpdyHeaderBlock& response_headers) override;
177 void OnDataRead(int bytes_read) override;
178 void OnDataSent() override;
179 void OnTrailersReceived(const SpdyHeaderBlock& trailers) override;
180 void OnFailed(int error) override;
181
182 // HttpStreamRequest::Delegate implementation:
183 void OnStreamReady(const SSLConfig& used_ssl_config,
184 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00185 std::unique_ptr<HttpStream> stream) override;
xunjieli5749218c2016-03-22 16:43:06186 void OnBidirectionalStreamImplReady(
187 const SSLConfig& used_ssl_config,
188 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00189 std::unique_ptr<BidirectionalStreamImpl> stream) override;
xunjieli11834f02015-12-22 04:27:08190 void OnWebSocketHandshakeStreamReady(
191 const SSLConfig& used_ssl_config,
192 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00193 std::unique_ptr<WebSocketHandshakeStreamBase> stream) override;
davidben849365422016-06-24 19:04:13194 void OnStreamFailed(int status, const SSLConfig& used_ssl_config) override;
xunjieli11834f02015-12-22 04:27:08195 void OnCertificateError(int status,
196 const SSLConfig& used_ssl_config,
197 const SSLInfo& ssl_info) override;
198 void OnNeedsProxyAuth(const HttpResponseInfo& response_info,
199 const SSLConfig& used_ssl_config,
200 const ProxyInfo& used_proxy_info,
201 HttpAuthController* auth_controller) override;
202 void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
203 SSLCertRequestInfo* cert_info) override;
204 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
205 const SSLConfig& used_ssl_config,
206 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01207 std::unique_ptr<HttpStream> stream) override;
xunjieli11834f02015-12-22 04:27:08208 void OnQuicBroken() override;
209
xunjieli3b6ac702016-06-06 21:36:28210 // Helper method to notify delegate if there is an error.
211 void NotifyFailed(int error);
212
xunjielid58621f2016-08-12 18:33:00213 void UpdateHistograms();
214
xunjieli11834f02015-12-22 04:27:08215 // BidirectionalStreamRequestInfo used when requesting the stream.
danakj1fd259a02016-04-16 03:17:09216 std::unique_ptr<BidirectionalStreamRequestInfo> request_info_;
tfarina428341112016-09-22 13:38:20217 const NetLogWithSource net_log_;
xunjieli11834f02015-12-22 04:27:08218
xunjielib3a648e2016-03-22 03:39:51219 HttpNetworkSession* session_;
220
xunjielibcb0f86e2016-06-03 00:49:29221 bool send_request_headers_automatically_;
222 // Whether request headers have been sent, as indicated in OnStreamReady()
223 // callback.
224 bool request_headers_sent_;
225
xunjieli11834f02015-12-22 04:27:08226 Delegate* const delegate_;
227
228 // Timer used to buffer data received in short time-spans and send a single
229 // read completion notification.
danakj1fd259a02016-04-16 03:17:09230 std::unique_ptr<base::Timer> timer_;
xunjieli5749218c2016-03-22 16:43:06231 // HttpStreamRequest used to request a BidirectionalStreamImpl. This is NULL
232 // if the request has been canceled or completed.
danakj1fd259a02016-04-16 03:17:09233 std::unique_ptr<HttpStreamRequest> stream_request_;
xunjieli5749218c2016-03-22 16:43:06234 // The underlying BidirectioanlStreamImpl used for this stream. It is
235 // non-NULL, if the |stream_request_| successfully finishes.
danakj1fd259a02016-04-16 03:17:09236 std::unique_ptr<BidirectionalStreamImpl> stream_impl_;
xunjieli11834f02015-12-22 04:27:08237
xunjielibe07785e2016-04-14 21:15:29238 // Buffer used for reading.
239 scoped_refptr<IOBuffer> read_buffer_;
xunjieli07a42ce2016-04-26 20:05:31240 // List of buffers used for writing.
241 std::vector<scoped_refptr<IOBuffer>> write_buffer_list_;
242 // List of buffer length.
243 std::vector<int> write_buffer_len_list_;
xunjielibe07785e2016-04-14 21:15:29244
xunjieli369d0922016-09-23 18:45:06245 // TODO(xunjieli): Remove this once LoadTimingInfo has response end.
xunjielid58621f2016-08-12 18:33:00246 base::TimeTicks read_end_time_;
xunjieli369d0922016-09-23 18:45:06247
248 // Load timing info of this stream. |connect_timing| is obtained when headers
249 // are received. Other fields are populated at different stages of the request
250 LoadTimingInfo load_timing_info_;
xunjielid58621f2016-08-12 18:33:00251
xunjieli3b6ac702016-06-06 21:36:28252 base::WeakPtrFactory<BidirectionalStream> weak_factory_;
253
xunjieli11834f02015-12-22 04:27:08254 DISALLOW_COPY_AND_ASSIGN(BidirectionalStream);
255};
256
257} // namespace net
258
259#endif // NET_HTTP_BIDIRECTIONAL_STREAM_H_