OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "net/quic/bidirectional_stream_quic_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/timer/timer.h" |
| 11 #include "net/http/bidirectional_stream_request_info.h" |
| 12 #include "net/socket/next_proto.h" |
| 13 #include "net/spdy/spdy_header_block.h" |
| 14 #include "net/spdy/spdy_http_utils.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( |
| 19 const base::WeakPtr<QuicChromiumClientSession>& session) |
| 20 : session_(session), |
| 21 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()), |
| 22 stream_(nullptr), |
| 23 request_info_(nullptr), |
| 24 delegate_(nullptr), |
| 25 response_status_(OK), |
| 26 negotiated_protocol_(kProtoUnknown), |
| 27 read_buffer_len_(0), |
| 28 headers_bytes_received_(0), |
| 29 headers_bytes_sent_(0), |
| 30 closed_stream_received_bytes_(0), |
| 31 closed_stream_sent_bytes_(0), |
| 32 has_sent_headers_(false), |
| 33 has_received_headers_(false), |
| 34 weak_factory_(this) { |
| 35 DCHECK(session_); |
| 36 session_->AddObserver(this); |
| 37 } |
| 38 |
| 39 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { |
| 40 Cancel(); |
| 41 if (session_) |
| 42 session_->RemoveObserver(this); |
| 43 } |
| 44 |
| 45 void BidirectionalStreamQuicImpl::Start( |
| 46 const BidirectionalStreamRequestInfo* request_info, |
| 47 const BoundNetLog& net_log, |
| 48 BidirectionalStreamJob::Delegate* delegate, |
| 49 scoped_ptr<base::Timer> /* timer */) { |
| 50 DCHECK(!stream_); |
| 51 |
| 52 if (!session_) { |
| 53 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
| 54 : ERR_QUIC_HANDSHAKE_FAILED); |
| 55 return; |
| 56 } |
| 57 |
| 58 delegate_ = delegate; |
| 59 request_info_ = request_info; |
| 60 |
| 61 int rv = stream_request_.StartRequest( |
| 62 session_, &stream_, |
| 63 base::Bind(&BidirectionalStreamQuicImpl::OnStreamReady, |
| 64 weak_factory_.GetWeakPtr())); |
| 65 if (rv == OK) { |
| 66 OnStreamReady(rv); |
| 67 } else if (!was_handshake_confirmed_) { |
| 68 NotifyError(ERR_QUIC_HANDSHAKE_FAILED); |
| 69 } |
| 70 } |
| 71 |
| 72 int BidirectionalStreamQuicImpl::ReadData(IOBuffer* buffer, int buffer_len) { |
| 73 DCHECK(buffer); |
| 74 DCHECK(buffer_len); |
| 75 |
| 76 if (!stream_) { |
| 77 // If the stream is already closed, there is no body to read. |
| 78 return response_status_; |
| 79 } |
| 80 int rv = stream_->Read(buffer, buffer_len); |
| 81 if (rv != ERR_IO_PENDING) { |
| 82 if (stream_->IsDoneReading()) { |
| 83 // If the write side is closed, OnFinRead() will call |
| 84 // BidirectionalStreamQuicImpl::OnClose(). |
| 85 stream_->OnFinRead(); |
| 86 } |
| 87 return rv; |
| 88 } |
| 89 // Read will complete asynchronously and Delegate::OnReadCompleted will be |
| 90 // called upon completion. |
| 91 read_buffer_ = buffer; |
| 92 read_buffer_len_ = buffer_len; |
| 93 return ERR_IO_PENDING; |
| 94 } |
| 95 |
| 96 void BidirectionalStreamQuicImpl::SendData(IOBuffer* data, |
| 97 int length, |
| 98 bool end_stream) { |
| 99 DCHECK(stream_); |
| 100 DCHECK(length > 0 || (length == 0 && end_stream)); |
| 101 |
| 102 base::StringPiece string_data(data->data(), length); |
| 103 int rv = stream_->WriteStreamData( |
| 104 string_data, end_stream, |
| 105 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
| 106 weak_factory_.GetWeakPtr())); |
| 107 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
| 108 if (rv == OK) { |
| 109 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 110 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
| 111 weak_factory_.GetWeakPtr(), OK)); |
| 112 } |
| 113 } |
| 114 |
| 115 void BidirectionalStreamQuicImpl::Cancel() { |
| 116 if (stream_) { |
| 117 stream_->SetDelegate(nullptr); |
| 118 stream_->Reset(QUIC_STREAM_CANCELLED); |
| 119 ResetStream(); |
| 120 } |
| 121 } |
| 122 |
| 123 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { |
| 124 return negotiated_protocol_; |
| 125 } |
| 126 |
| 127 int64_t BidirectionalStreamQuicImpl::GetTotalReceivedBytes() const { |
| 128 if (stream_) |
| 129 return headers_bytes_received_ + stream_->stream_bytes_read(); |
| 130 return headers_bytes_received_ + closed_stream_received_bytes_; |
| 131 } |
| 132 |
| 133 int64_t BidirectionalStreamQuicImpl::GetTotalSentBytes() const { |
| 134 if (stream_) |
| 135 return headers_bytes_sent_ + stream_->stream_bytes_written(); |
| 136 return headers_bytes_sent_ + closed_stream_sent_bytes_; |
| 137 } |
| 138 |
| 139 void BidirectionalStreamQuicImpl::OnHeadersAvailable( |
| 140 const SpdyHeaderBlock& headers, |
| 141 size_t frame_len) { |
| 142 headers_bytes_received_ += frame_len; |
| 143 negotiated_protocol_ = kProtoQUIC1SPDY3; |
| 144 if (!has_received_headers_) { |
| 145 has_received_headers_ = true; |
| 146 delegate_->OnHeadersReceived(headers); |
| 147 } else { |
| 148 if (stream_->IsDoneReading()) { |
| 149 // If the write side is closed, OnFinRead() will call |
| 150 // BidirectionalStreamQuicImpl::OnClose(). |
| 151 stream_->OnFinRead(); |
| 152 } |
| 153 delegate_->OnTrailersReceived(headers); |
| 154 } |
| 155 } |
| 156 |
| 157 void BidirectionalStreamQuicImpl::OnDataAvailable() { |
| 158 // Return early if ReadData has not been called. |
| 159 if (!read_buffer_) |
| 160 return; |
| 161 |
| 162 CHECK(read_buffer_); |
| 163 CHECK_NE(0, read_buffer_len_); |
| 164 int rv = ReadData(read_buffer_.get(), read_buffer_len_); |
| 165 if (rv == ERR_IO_PENDING) { |
| 166 // Spurrious notification. Wait for the next one. |
| 167 return; |
| 168 } |
| 169 read_buffer_ = nullptr; |
| 170 read_buffer_len_ = 0; |
| 171 delegate_->OnDataRead(rv); |
| 172 } |
| 173 |
| 174 void BidirectionalStreamQuicImpl::OnClose(QuicErrorCode error) { |
| 175 DCHECK(stream_); |
| 176 if (error == QUIC_NO_ERROR && |
| 177 stream_->stream_error() == QUIC_STREAM_NO_ERROR) { |
| 178 ResetStream(); |
| 179 return; |
| 180 } |
| 181 ResetStream(); |
| 182 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
| 183 : ERR_QUIC_HANDSHAKE_FAILED); |
| 184 } |
| 185 |
| 186 void BidirectionalStreamQuicImpl::OnError(int error) { |
| 187 NotifyError(error); |
| 188 } |
| 189 |
| 190 bool BidirectionalStreamQuicImpl::HasSendHeadersComplete() { |
| 191 return has_sent_headers_; |
| 192 } |
| 193 |
| 194 void BidirectionalStreamQuicImpl::OnCryptoHandshakeConfirmed() { |
| 195 was_handshake_confirmed_ = true; |
| 196 } |
| 197 |
| 198 void BidirectionalStreamQuicImpl::OnSessionClosed( |
| 199 int error, |
| 200 bool /*port_migration_detected*/) { |
| 201 DCHECK_NE(OK, error); |
| 202 session_.reset(); |
| 203 NotifyError(error); |
| 204 } |
| 205 |
| 206 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { |
| 207 DCHECK_NE(ERR_IO_PENDING, rv); |
| 208 DCHECK(rv == OK || !stream_); |
| 209 if (rv == OK) { |
| 210 stream_->SetDelegate(this); |
| 211 SendRequestHeaders(); |
| 212 } else { |
| 213 NotifyError(rv); |
| 214 } |
| 215 } |
| 216 |
| 217 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { |
| 218 DCHECK(rv == OK || !stream_); |
| 219 if (rv == OK) { |
| 220 delegate_->OnDataSent(); |
| 221 } else { |
| 222 NotifyError(rv); |
| 223 } |
| 224 } |
| 225 |
| 226 void BidirectionalStreamQuicImpl::SendRequestHeaders() { |
| 227 DCHECK(!has_sent_headers_); |
| 228 DCHECK(stream_); |
| 229 |
| 230 SpdyHeaderBlock headers; |
| 231 HttpRequestInfo http_request_info; |
| 232 http_request_info.url = request_info_->url; |
| 233 http_request_info.method = request_info_->method; |
| 234 http_request_info.extra_headers = request_info_->extra_headers; |
| 235 |
| 236 CreateSpdyHeadersFromHttpRequest(http_request_info, |
| 237 http_request_info.extra_headers, HTTP2, true, |
| 238 &headers); |
| 239 size_t frame_len = stream_->WriteHeaders( |
| 240 headers, request_info_->end_stream_on_headers, nullptr); |
| 241 headers_bytes_sent_ += frame_len; |
| 242 has_sent_headers_ = true; |
| 243 delegate_->OnHeadersSent(); |
| 244 } |
| 245 |
| 246 void BidirectionalStreamQuicImpl::NotifyError(int error) { |
| 247 DCHECK_NE(OK, error); |
| 248 DCHECK_NE(ERR_IO_PENDING, error); |
| 249 |
| 250 response_status_ = error; |
| 251 ResetStream(); |
| 252 delegate_->OnFailed(error); |
| 253 } |
| 254 |
| 255 void BidirectionalStreamQuicImpl::ResetStream() { |
| 256 if (!stream_) |
| 257 return; |
| 258 closed_stream_received_bytes_ = stream_->stream_bytes_read(); |
| 259 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); |
| 260 stream_->SetDelegate(nullptr); |
| 261 stream_ = nullptr; |
| 262 } |
| 263 |
| 264 } // namespace net |
OLD | NEW |