OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/bidirectional_stream_quic_impl.h" | 5 #include "net/quic/bidirectional_stream_quic_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
11 #include "net/http/bidirectional_stream_request_info.h" | 11 #include "net/http/bidirectional_stream_request_info.h" |
| 12 #include "net/quic/quic_connection.h" |
12 #include "net/socket/next_proto.h" | 13 #include "net/socket/next_proto.h" |
13 #include "net/spdy/spdy_header_block.h" | 14 #include "net/spdy/spdy_header_block.h" |
14 #include "net/spdy/spdy_http_utils.h" | 15 #include "net/spdy/spdy_http_utils.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( | 19 BidirectionalStreamQuicImpl::BidirectionalStreamQuicImpl( |
19 const base::WeakPtr<QuicChromiumClientSession>& session) | 20 const base::WeakPtr<QuicChromiumClientSession>& session) |
20 : session_(session), | 21 : session_(session), |
21 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()), | 22 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()), |
22 stream_(nullptr), | 23 stream_(nullptr), |
23 request_info_(nullptr), | 24 request_info_(nullptr), |
24 delegate_(nullptr), | 25 delegate_(nullptr), |
25 response_status_(OK), | 26 response_status_(OK), |
26 negotiated_protocol_(kProtoUnknown), | 27 negotiated_protocol_(kProtoUnknown), |
27 read_buffer_len_(0), | 28 read_buffer_len_(0), |
28 headers_bytes_received_(0), | 29 headers_bytes_received_(0), |
29 headers_bytes_sent_(0), | 30 headers_bytes_sent_(0), |
30 closed_stream_received_bytes_(0), | 31 closed_stream_received_bytes_(0), |
31 closed_stream_sent_bytes_(0), | 32 closed_stream_sent_bytes_(0), |
32 has_sent_headers_(false), | 33 has_sent_headers_(false), |
33 has_received_headers_(false), | 34 has_received_headers_(false), |
| 35 disable_auto_flush_(false), |
34 weak_factory_(this) { | 36 weak_factory_(this) { |
35 DCHECK(session_); | 37 DCHECK(session_); |
36 session_->AddObserver(this); | 38 session_->AddObserver(this); |
37 } | 39 } |
38 | 40 |
39 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { | 41 BidirectionalStreamQuicImpl::~BidirectionalStreamQuicImpl() { |
40 Cancel(); | 42 Cancel(); |
41 if (session_) | 43 if (session_) |
42 session_->RemoveObserver(this); | 44 session_->RemoveObserver(this); |
43 } | 45 } |
44 | 46 |
45 void BidirectionalStreamQuicImpl::Start( | 47 void BidirectionalStreamQuicImpl::Start( |
46 const BidirectionalStreamRequestInfo* request_info, | 48 const BidirectionalStreamRequestInfo* request_info, |
47 const BoundNetLog& net_log, | 49 const BoundNetLog& net_log, |
| 50 bool disable_auto_flush, |
48 BidirectionalStreamImpl::Delegate* delegate, | 51 BidirectionalStreamImpl::Delegate* delegate, |
49 std::unique_ptr<base::Timer> /* timer */) { | 52 std::unique_ptr<base::Timer> /* timer */) { |
50 DCHECK(!stream_); | 53 DCHECK(!stream_); |
51 | 54 |
| 55 disable_auto_flush_ = disable_auto_flush; |
52 if (!session_) { | 56 if (!session_) { |
53 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR | 57 NotifyError(was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
54 : ERR_QUIC_HANDSHAKE_FAILED); | 58 : ERR_QUIC_HANDSHAKE_FAILED); |
55 return; | 59 return; |
56 } | 60 } |
57 | 61 |
58 delegate_ = delegate; | 62 delegate_ = delegate; |
59 request_info_ = request_info; | 63 request_info_ = request_info; |
60 | 64 |
61 int rv = stream_request_.StartRequest( | 65 int rv = stream_request_.StartRequest( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 109 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
106 weak_factory_.GetWeakPtr())); | 110 weak_factory_.GetWeakPtr())); |
107 DCHECK(rv == OK || rv == ERR_IO_PENDING); | 111 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
108 if (rv == OK) { | 112 if (rv == OK) { |
109 base::ThreadTaskRunnerHandle::Get()->PostTask( | 113 base::ThreadTaskRunnerHandle::Get()->PostTask( |
110 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, | 114 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
111 weak_factory_.GetWeakPtr(), OK)); | 115 weak_factory_.GetWeakPtr(), OK)); |
112 } | 116 } |
113 } | 117 } |
114 | 118 |
| 119 void BidirectionalStreamQuicImpl::SendvData( |
| 120 const std::vector<IOBuffer*>& buffers, |
| 121 const std::vector<int>& lengths, |
| 122 bool end_stream) { |
| 123 DCHECK(stream_); |
| 124 DCHECK_EQ(buffers.size(), lengths.size()); |
| 125 |
| 126 QuicConnection::ScopedPacketBundler bundler( |
| 127 session_->connection(), QuicConnection::SEND_ACK_IF_PENDING); |
| 128 if (!has_sent_headers_) { |
| 129 SendRequestHeaders(); |
| 130 } |
| 131 |
| 132 int rv = stream_->WritevStreamData( |
| 133 buffers, lengths, end_stream, |
| 134 base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
| 135 weak_factory_.GetWeakPtr())); |
| 136 |
| 137 DCHECK(rv == OK || rv == ERR_IO_PENDING); |
| 138 if (rv == OK) { |
| 139 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 140 FROM_HERE, base::Bind(&BidirectionalStreamQuicImpl::OnSendDataComplete, |
| 141 weak_factory_.GetWeakPtr(), OK)); |
| 142 } |
| 143 } |
| 144 |
115 void BidirectionalStreamQuicImpl::Cancel() { | 145 void BidirectionalStreamQuicImpl::Cancel() { |
116 if (stream_) { | 146 if (stream_) { |
117 stream_->SetDelegate(nullptr); | 147 stream_->SetDelegate(nullptr); |
118 stream_->Reset(QUIC_STREAM_CANCELLED); | 148 stream_->Reset(QUIC_STREAM_CANCELLED); |
119 ResetStream(); | 149 ResetStream(); |
120 } | 150 } |
121 } | 151 } |
122 | 152 |
123 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { | 153 NextProto BidirectionalStreamQuicImpl::GetProtocol() const { |
124 return negotiated_protocol_; | 154 return negotiated_protocol_; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 DCHECK_NE(OK, error); | 231 DCHECK_NE(OK, error); |
202 session_.reset(); | 232 session_.reset(); |
203 NotifyError(error); | 233 NotifyError(error); |
204 } | 234 } |
205 | 235 |
206 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { | 236 void BidirectionalStreamQuicImpl::OnStreamReady(int rv) { |
207 DCHECK_NE(ERR_IO_PENDING, rv); | 237 DCHECK_NE(ERR_IO_PENDING, rv); |
208 DCHECK(rv == OK || !stream_); | 238 DCHECK(rv == OK || !stream_); |
209 if (rv == OK) { | 239 if (rv == OK) { |
210 stream_->SetDelegate(this); | 240 stream_->SetDelegate(this); |
211 SendRequestHeaders(); | 241 if (!disable_auto_flush_) { |
| 242 SendRequestHeaders(); |
| 243 } |
| 244 delegate_->OnStreamReady(); |
212 } else { | 245 } else { |
213 NotifyError(rv); | 246 NotifyError(rv); |
214 } | 247 } |
215 } | 248 } |
216 | 249 |
217 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { | 250 void BidirectionalStreamQuicImpl::OnSendDataComplete(int rv) { |
218 DCHECK(rv == OK || !stream_); | 251 DCHECK(rv == OK || !stream_); |
219 if (rv == OK) { | 252 if (rv == OK) { |
220 delegate_->OnDataSent(); | 253 delegate_->OnDataSent(); |
221 } else { | 254 } else { |
(...skipping 11 matching lines...) Expand all Loading... |
233 http_request_info.method = request_info_->method; | 266 http_request_info.method = request_info_->method; |
234 http_request_info.extra_headers = request_info_->extra_headers; | 267 http_request_info.extra_headers = request_info_->extra_headers; |
235 | 268 |
236 CreateSpdyHeadersFromHttpRequest(http_request_info, | 269 CreateSpdyHeadersFromHttpRequest(http_request_info, |
237 http_request_info.extra_headers, HTTP2, true, | 270 http_request_info.extra_headers, HTTP2, true, |
238 &headers); | 271 &headers); |
239 size_t frame_len = stream_->WriteHeaders( | 272 size_t frame_len = stream_->WriteHeaders( |
240 headers, request_info_->end_stream_on_headers, nullptr); | 273 headers, request_info_->end_stream_on_headers, nullptr); |
241 headers_bytes_sent_ += frame_len; | 274 headers_bytes_sent_ += frame_len; |
242 has_sent_headers_ = true; | 275 has_sent_headers_ = true; |
243 delegate_->OnHeadersSent(); | |
244 } | 276 } |
245 | 277 |
246 void BidirectionalStreamQuicImpl::NotifyError(int error) { | 278 void BidirectionalStreamQuicImpl::NotifyError(int error) { |
247 DCHECK_NE(OK, error); | 279 DCHECK_NE(OK, error); |
248 DCHECK_NE(ERR_IO_PENDING, error); | 280 DCHECK_NE(ERR_IO_PENDING, error); |
249 | 281 |
250 response_status_ = error; | 282 response_status_ = error; |
251 ResetStream(); | 283 ResetStream(); |
252 delegate_->OnFailed(error); | 284 delegate_->OnFailed(error); |
253 } | 285 } |
254 | 286 |
255 void BidirectionalStreamQuicImpl::ResetStream() { | 287 void BidirectionalStreamQuicImpl::ResetStream() { |
256 if (!stream_) | 288 if (!stream_) |
257 return; | 289 return; |
258 closed_stream_received_bytes_ = stream_->stream_bytes_read(); | 290 closed_stream_received_bytes_ = stream_->stream_bytes_read(); |
259 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); | 291 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); |
260 stream_->SetDelegate(nullptr); | 292 stream_->SetDelegate(nullptr); |
261 stream_ = nullptr; | 293 stream_ = nullptr; |
262 } | 294 } |
263 | 295 |
264 } // namespace net | 296 } // namespace net |
OLD | NEW |