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 | |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 5 | #include "net/spdy/bidirectional_stream_spdy_impl.h" |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 6 | |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/location.h" |
| 11 | #include "base/logging.h" |
fdoray | a19b770 | 2016-12-23 14:19:31 | [diff] [blame] | 12 | #include "base/threading/thread_task_runner_handle.h" |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 13 | #include "base/time/time.h" |
| 14 | #include "base/timer/timer.h" |
| 15 | #include "net/http/bidirectional_stream_request_info.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 16 | #include "net/spdy/spdy_buffer.h" |
| 17 | #include "net/spdy/spdy_http_utils.h" |
| 18 | #include "net/spdy/spdy_stream.h" |
Ryan Hamilton | ea4fa19 | 2022-04-12 18:30:49 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quiche/spdy/core/spdy_header_block.h" |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | // Time to wait in millisecond to notify |delegate_| of data received. |
| 26 | // Handing small chunks of data to the caller creates measurable overhead. |
| 27 | // So buffer data in short time-spans and send a single read notification. |
mef | e666e9a | 2015-12-30 23:41:30 | [diff] [blame] | 28 | const int kBufferTimeMs = 1; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 29 | |
| 30 | } // namespace |
| 31 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 32 | BidirectionalStreamSpdyImpl::BidirectionalStreamSpdyImpl( |
bnc | 3d95ca9 | 2017-03-29 13:20:34 | [diff] [blame] | 33 | const base::WeakPtr<SpdySession>& spdy_session, |
| 34 | NetLogSource source_dependency) |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 35 | : spdy_session_(spdy_session), |
| 36 | request_info_(nullptr), |
| 37 | delegate_(nullptr), |
Tsuyoshi Horo | b2022b5 | 2022-06-09 01:44:07 | [diff] [blame] | 38 | source_dependency_(source_dependency) {} |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 39 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 40 | BidirectionalStreamSpdyImpl::~BidirectionalStreamSpdyImpl() { |
xunjieli | 9ff75c56 | 2016-08-10 20:26:16 | [diff] [blame] | 41 | // Sends a RST to the remote if the stream is destroyed before it completes. |
| 42 | ResetStream(); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 43 | } |
| 44 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 45 | void BidirectionalStreamSpdyImpl::Start( |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 46 | const BidirectionalStreamRequestInfo* request_info, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 47 | const NetLogWithSource& net_log, |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 48 | bool /*send_request_headers_automatically*/, |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 49 | BidirectionalStreamImpl::Delegate* delegate, |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 50 | std::unique_ptr<base::OneShotTimer> timer, |
Ramin Halavati | 3c96c6d | 2018-03-11 13:29:44 | [diff] [blame] | 51 | const NetworkTrafficAnnotationTag& traffic_annotation) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 52 | DCHECK(!stream_); |
| 53 | DCHECK(timer); |
| 54 | |
| 55 | delegate_ = delegate; |
| 56 | timer_ = std::move(timer); |
| 57 | |
| 58 | if (!spdy_session_) { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 59 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 60 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 61 | base::BindOnce(&BidirectionalStreamSpdyImpl::NotifyError, |
| 62 | weak_factory_.GetWeakPtr(), ERR_CONNECTION_CLOSED)); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | |
| 66 | request_info_ = request_info; |
| 67 | |
| 68 | int rv = stream_request_.StartRequest( |
| 69 | SPDY_BIDIRECTIONAL_STREAM, spdy_session_, request_info_->url, |
Steven Valdez | 1c185917 | 2019-04-10 15:33:28 | [diff] [blame] | 70 | false /* no early data */, request_info_->priority, |
| 71 | request_info_->socket_tag, net_log, |
Yannic Bonenberger | 00e0984 | 2019-08-31 20:46:19 | [diff] [blame] | 72 | base::BindOnce(&BidirectionalStreamSpdyImpl::OnStreamInitialized, |
| 73 | weak_factory_.GetWeakPtr()), |
Stefano Duo | 90efd49 | 2022-01-17 13:31:13 | [diff] [blame] | 74 | traffic_annotation, request_info_->detect_broken_connection, |
| 75 | request_info_->heartbeat_interval); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 76 | if (rv != ERR_IO_PENDING) |
| 77 | OnStreamInitialized(rv); |
| 78 | } |
| 79 | |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 80 | void BidirectionalStreamSpdyImpl::SendRequestHeaders() { |
| 81 | // Request headers will be sent automatically. |
| 82 | NOTREACHED(); |
| 83 | } |
| 84 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 85 | int BidirectionalStreamSpdyImpl::ReadData(IOBuffer* buf, int buf_len) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 86 | if (stream_) |
| 87 | DCHECK(!stream_->IsIdle()); |
| 88 | |
| 89 | DCHECK(buf); |
| 90 | DCHECK(buf_len); |
| 91 | DCHECK(!timer_->IsRunning()) << "There should be only one ReadData in flight"; |
| 92 | |
| 93 | // If there is data buffered, complete the IO immediately. |
| 94 | if (!read_data_queue_.IsEmpty()) { |
| 95 | return read_data_queue_.Dequeue(buf->data(), buf_len); |
| 96 | } else if (stream_closed_) { |
| 97 | return closed_stream_status_; |
| 98 | } |
| 99 | // Read will complete asynchronously and Delegate::OnReadCompleted will be |
| 100 | // called upon completion. |
| 101 | read_buffer_ = buf; |
| 102 | read_buffer_len_ = buf_len; |
| 103 | return ERR_IO_PENDING; |
| 104 | } |
| 105 | |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 106 | void BidirectionalStreamSpdyImpl::SendvData( |
xunjieli | 2328a268 | 2016-05-16 19:38:25 | [diff] [blame] | 107 | const std::vector<scoped_refptr<IOBuffer>>& buffers, |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 108 | const std::vector<int>& lengths, |
| 109 | bool end_stream) { |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 110 | DCHECK_EQ(buffers.size(), lengths.size()); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 111 | DCHECK(!write_pending_); |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 112 | |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 113 | if (written_end_of_stream_) { |
| 114 | LOG(ERROR) << "Writing after end of stream is written."; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 115 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 116 | FROM_HERE, base::BindOnce(&BidirectionalStreamSpdyImpl::NotifyError, |
| 117 | weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 121 | write_pending_ = true; |
| 122 | written_end_of_stream_ = end_stream; |
| 123 | if (MaybeHandleStreamClosedInSendData()) |
| 124 | return; |
| 125 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 126 | DCHECK(!stream_closed_); |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 127 | int total_len = 0; |
| 128 | for (int len : lengths) { |
| 129 | total_len += len; |
| 130 | } |
| 131 | |
rch | ad39988f | 2017-06-02 05:34:32 | [diff] [blame] | 132 | if (buffers.size() == 1) { |
| 133 | pending_combined_buffer_ = buffers[0]; |
| 134 | } else { |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 135 | pending_combined_buffer_ = base::MakeRefCounted<net::IOBuffer>(total_len); |
rch | ad39988f | 2017-06-02 05:34:32 | [diff] [blame] | 136 | int len = 0; |
| 137 | // TODO(xunjieli): Get rid of extra copy. Coalesce headers and data frames. |
| 138 | for (size_t i = 0; i < buffers.size(); ++i) { |
| 139 | memcpy(pending_combined_buffer_->data() + len, buffers[i]->data(), |
| 140 | lengths[i]); |
| 141 | len += lengths[i]; |
| 142 | } |
xunjieli | 07a42ce | 2016-04-26 20:05:31 | [diff] [blame] | 143 | } |
| 144 | stream_->SendData(pending_combined_buffer_.get(), total_len, |
| 145 | end_stream ? NO_MORE_DATA_TO_SEND : MORE_DATA_TO_SEND); |
| 146 | } |
| 147 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 148 | NextProto BidirectionalStreamSpdyImpl::GetProtocol() const { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 149 | return negotiated_protocol_; |
| 150 | } |
| 151 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 152 | int64_t BidirectionalStreamSpdyImpl::GetTotalReceivedBytes() const { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 153 | if (stream_closed_) |
| 154 | return closed_stream_received_bytes_; |
| 155 | |
| 156 | if (!stream_) |
| 157 | return 0; |
| 158 | |
| 159 | return stream_->raw_received_bytes(); |
| 160 | } |
| 161 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 162 | int64_t BidirectionalStreamSpdyImpl::GetTotalSentBytes() const { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 163 | if (stream_closed_) |
| 164 | return closed_stream_sent_bytes_; |
| 165 | |
| 166 | if (!stream_) |
| 167 | return 0; |
| 168 | |
| 169 | return stream_->raw_sent_bytes(); |
| 170 | } |
| 171 | |
xunjieli | ba8de432 | 2016-09-20 21:57:54 | [diff] [blame] | 172 | bool BidirectionalStreamSpdyImpl::GetLoadTimingInfo( |
| 173 | LoadTimingInfo* load_timing_info) const { |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 174 | if (stream_closed_) { |
| 175 | if (!closed_has_load_timing_info_) |
| 176 | return false; |
| 177 | *load_timing_info = closed_load_timing_info_; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // If |stream_| isn't created or has ID 0, return false. This is to match |
| 182 | // the implementation in SpdyHttpStream. |
| 183 | if (!stream_ || stream_->stream_id() == 0) |
| 184 | return false; |
| 185 | |
| 186 | return stream_->GetLoadTimingInfo(load_timing_info); |
xunjieli | ba8de432 | 2016-09-20 21:57:54 | [diff] [blame] | 187 | } |
| 188 | |
Brad Lassey | 5f488db4 | 2017-07-19 00:30:46 | [diff] [blame] | 189 | void BidirectionalStreamSpdyImpl::PopulateNetErrorDetails( |
| 190 | NetErrorDetails* details) {} |
| 191 | |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 192 | void BidirectionalStreamSpdyImpl::OnHeadersSent() { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 193 | DCHECK(stream_); |
| 194 | |
bnc | bca843ba | 2016-07-14 13:05:48 | [diff] [blame] | 195 | negotiated_protocol_ = kProtoHTTP2; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 196 | if (delegate_) |
| 197 | delegate_->OnStreamReady(/*request_headers_sent=*/true); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 198 | } |
| 199 | |
Kenichi Ishibashi | 7415553 | 2021-03-13 01:38:06 | [diff] [blame] | 200 | void BidirectionalStreamSpdyImpl::OnEarlyHintsReceived( |
| 201 | const spdy::Http2HeaderBlock& headers) { |
| 202 | DCHECK(stream_); |
| 203 | // TODO(crbug.com/671310): Plumb Early Hints to `delegate_` if needed. |
| 204 | } |
| 205 | |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 206 | void BidirectionalStreamSpdyImpl::OnHeadersReceived( |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 207 | const spdy::Http2HeaderBlock& response_headers, |
| 208 | const spdy::Http2HeaderBlock* pushed_request_headers) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 209 | DCHECK(stream_); |
| 210 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 211 | if (delegate_) |
| 212 | delegate_->OnHeadersReceived(response_headers); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 213 | } |
| 214 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 215 | void BidirectionalStreamSpdyImpl::OnDataReceived( |
danakj | aee3e1ec | 2016-04-16 00:23:18 | [diff] [blame] | 216 | std::unique_ptr<SpdyBuffer> buffer) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 217 | DCHECK(stream_); |
| 218 | DCHECK(!stream_closed_); |
| 219 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 220 | // If |buffer| is null, BidirectionalStreamSpdyImpl::OnClose will be invoked |
| 221 | // by SpdyStream to indicate the end of stream. |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 222 | if (!buffer) |
| 223 | return; |
| 224 | |
| 225 | // When buffer is consumed, SpdyStream::OnReadBufferConsumed will adjust |
| 226 | // recv window size accordingly. |
| 227 | read_data_queue_.Enqueue(std::move(buffer)); |
| 228 | if (read_buffer_) { |
| 229 | // Handing small chunks of data to the caller creates measurable overhead. |
| 230 | // So buffer data in short time-spans and send a single read notification. |
| 231 | ScheduleBufferedRead(); |
| 232 | } |
| 233 | } |
| 234 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 235 | void BidirectionalStreamSpdyImpl::OnDataSent() { |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 236 | DCHECK(write_pending_); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 237 | |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 238 | pending_combined_buffer_ = nullptr; |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 239 | write_pending_ = false; |
| 240 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 241 | if (delegate_) |
| 242 | delegate_->OnDataSent(); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 243 | } |
| 244 | |
Ryan Hamilton | 0239aac | 2018-05-19 00:03:13 | [diff] [blame] | 245 | void BidirectionalStreamSpdyImpl::OnTrailers( |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 246 | const spdy::Http2HeaderBlock& trailers) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 247 | DCHECK(stream_); |
| 248 | DCHECK(!stream_closed_); |
| 249 | |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 250 | if (delegate_) |
| 251 | delegate_->OnTrailersReceived(trailers); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 252 | } |
| 253 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 254 | void BidirectionalStreamSpdyImpl::OnClose(int status) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 255 | DCHECK(stream_); |
| 256 | |
| 257 | stream_closed_ = true; |
| 258 | closed_stream_status_ = status; |
| 259 | closed_stream_received_bytes_ = stream_->raw_received_bytes(); |
| 260 | closed_stream_sent_bytes_ = stream_->raw_sent_bytes(); |
xunjieli | 6d0b944d | 2016-09-21 01:53:57 | [diff] [blame] | 261 | closed_has_load_timing_info_ = |
| 262 | stream_->GetLoadTimingInfo(&closed_load_timing_info_); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 263 | |
| 264 | if (status != OK) { |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 265 | NotifyError(status); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 266 | return; |
| 267 | } |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 268 | ResetStream(); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 269 | // Complete any remaining read, as all data has been buffered. |
| 270 | // If user has not called ReadData (i.e |read_buffer_| is nullptr), this will |
| 271 | // do nothing. |
| 272 | timer_->Stop(); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 273 | |
| 274 | // |this| might get destroyed after calling into |delegate_| in |
| 275 | // DoBufferedRead(). |
| 276 | auto weak_this = weak_factory_.GetWeakPtr(); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 277 | DoBufferedRead(); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 278 | if (weak_this.get() && write_pending_) |
| 279 | OnDataSent(); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 280 | } |
| 281 | |
Bence Béky | 21755dd6 | 2019-11-19 13:47:35 | [diff] [blame] | 282 | bool BidirectionalStreamSpdyImpl::CanGreaseFrameType() const { |
| 283 | return false; |
| 284 | } |
| 285 | |
bnc | 3d95ca9 | 2017-03-29 13:20:34 | [diff] [blame] | 286 | NetLogSource BidirectionalStreamSpdyImpl::source_dependency() const { |
| 287 | return source_dependency_; |
| 288 | } |
| 289 | |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 290 | int BidirectionalStreamSpdyImpl::SendRequestHeadersHelper() { |
Bence Béky | 4c325e5 | 2020-10-22 20:48:01 | [diff] [blame] | 291 | spdy::Http2HeaderBlock headers; |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 292 | HttpRequestInfo http_request_info; |
| 293 | http_request_info.url = request_info_->url; |
| 294 | http_request_info.method = request_info_->method; |
| 295 | http_request_info.extra_headers = request_info_->extra_headers; |
| 296 | |
Bence Béky | 1af94d6f | 2018-02-08 00:40:14 | [diff] [blame] | 297 | CreateSpdyHeadersFromHttpRequest(http_request_info, |
| 298 | http_request_info.extra_headers, &headers); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 299 | written_end_of_stream_ = request_info_->end_stream_on_headers; |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 300 | return stream_->SendRequestHeaders(std::move(headers), |
| 301 | request_info_->end_stream_on_headers |
| 302 | ? NO_MORE_DATA_TO_SEND |
| 303 | : MORE_DATA_TO_SEND); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 304 | } |
| 305 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 306 | void BidirectionalStreamSpdyImpl::OnStreamInitialized(int rv) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 307 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 308 | if (rv == OK) { |
| 309 | stream_ = stream_request_.ReleaseStream(); |
| 310 | stream_->SetDelegate(this); |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 311 | rv = SendRequestHeadersHelper(); |
| 312 | if (rv == OK) { |
bnc | 4c21431 | 2016-11-28 16:49:15 | [diff] [blame] | 313 | OnHeadersSent(); |
xunjieli | bcb0f86e | 2016-06-03 00:49:29 | [diff] [blame] | 314 | return; |
| 315 | } else if (rv == ERR_IO_PENDING) { |
| 316 | return; |
| 317 | } |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 318 | } |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 319 | NotifyError(rv); |
| 320 | } |
| 321 | |
| 322 | void BidirectionalStreamSpdyImpl::NotifyError(int rv) { |
| 323 | ResetStream(); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 324 | write_pending_ = false; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 325 | if (delegate_) { |
| 326 | BidirectionalStreamImpl::Delegate* delegate = delegate_; |
| 327 | delegate_ = nullptr; |
| 328 | // Cancel any pending callback. |
| 329 | weak_factory_.InvalidateWeakPtrs(); |
| 330 | delegate->OnFailed(rv); |
| 331 | // |this| can be null when returned from delegate. |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void BidirectionalStreamSpdyImpl::ResetStream() { |
| 336 | if (!stream_) |
| 337 | return; |
| 338 | if (!stream_->IsClosed()) { |
| 339 | // This sends a RST to the remote. |
| 340 | stream_->DetachDelegate(); |
| 341 | DCHECK(!stream_); |
| 342 | } else { |
| 343 | // Stream is already closed, so it is not legal to call DetachDelegate. |
| 344 | stream_.reset(); |
| 345 | } |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 346 | } |
| 347 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 348 | void BidirectionalStreamSpdyImpl::ScheduleBufferedRead() { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 349 | // If there is already a scheduled DoBufferedRead, don't issue |
| 350 | // another one. Mark that we have received more data and return. |
| 351 | if (timer_->IsRunning()) { |
| 352 | more_read_data_pending_ = true; |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | more_read_data_pending_ = false; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 357 | timer_->Start(FROM_HERE, base::Milliseconds(kBufferTimeMs), |
Yannic Bonenberger | 00e0984 | 2019-08-31 20:46:19 | [diff] [blame] | 358 | base::BindOnce(&BidirectionalStreamSpdyImpl::DoBufferedRead, |
| 359 | weak_factory_.GetWeakPtr())); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 360 | } |
| 361 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 362 | void BidirectionalStreamSpdyImpl::DoBufferedRead() { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 363 | DCHECK(!timer_->IsRunning()); |
| 364 | // Check to see that the stream has not errored out. |
| 365 | DCHECK(stream_ || stream_closed_); |
| 366 | DCHECK(!stream_closed_ || closed_stream_status_ == OK); |
| 367 | |
| 368 | // When |more_read_data_pending_| is true, it means that more data has arrived |
| 369 | // since started waiting. Wait a little longer and continue to buffer. |
| 370 | if (more_read_data_pending_ && ShouldWaitForMoreBufferedData()) { |
| 371 | ScheduleBufferedRead(); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | int rv = 0; |
| 376 | if (read_buffer_) { |
| 377 | rv = ReadData(read_buffer_.get(), read_buffer_len_); |
| 378 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 379 | read_buffer_ = nullptr; |
| 380 | read_buffer_len_ = 0; |
xunjieli | 707f895 | 2016-06-06 15:22:06 | [diff] [blame] | 381 | if (delegate_) |
| 382 | delegate_->OnDataRead(rv); |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 386 | bool BidirectionalStreamSpdyImpl::ShouldWaitForMoreBufferedData() const { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 387 | if (stream_closed_) |
| 388 | return false; |
| 389 | DCHECK_GT(read_buffer_len_, 0); |
| 390 | return read_data_queue_.GetTotalSize() < |
| 391 | static_cast<size_t>(read_buffer_len_); |
| 392 | } |
| 393 | |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 394 | bool BidirectionalStreamSpdyImpl::MaybeHandleStreamClosedInSendData() { |
| 395 | if (stream_) |
| 396 | return false; |
| 397 | // If |stream_| is closed without an error before client half closes, |
| 398 | // blackhole any pending write data. crbug.com/650438. |
| 399 | if (stream_closed_ && closed_stream_status_ == OK) { |
| 400 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 401 | FROM_HERE, base::BindOnce(&BidirectionalStreamSpdyImpl::OnDataSent, |
| 402 | weak_factory_.GetWeakPtr())); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 403 | return true; |
| 404 | } |
| 405 | LOG(ERROR) << "Trying to send data after stream has been destroyed."; |
| 406 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 407 | FROM_HERE, base::BindOnce(&BidirectionalStreamSpdyImpl::NotifyError, |
| 408 | weak_factory_.GetWeakPtr(), ERR_UNEXPECTED)); |
xunjieli | 4f8b6bb6 | 2016-10-31 23:16:00 | [diff] [blame] | 409 | return true; |
| 410 | } |
| 411 | |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 412 | } // namespace net |