Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
blobdff3431d88935c8c6f0326a433320120d33eb500
1 // Copyright (c) 2012 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.
5 #include "net/quic/quic_http_stream.h"
7 #include "base/callback_helpers.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/stringprintf.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_response_headers.h"
13 #include "net/http/http_util.h"
14 #include "net/quic/quic_chromium_client_session.h"
15 #include "net/quic/quic_http_utils.h"
16 #include "net/quic/quic_reliable_client_stream.h"
17 #include "net/quic/quic_utils.h"
18 #include "net/quic/spdy_utils.h"
19 #include "net/socket/next_proto.h"
20 #include "net/spdy/spdy_frame_builder.h"
21 #include "net/spdy/spdy_framer.h"
22 #include "net/spdy/spdy_http_utils.h"
23 #include "net/ssl/ssl_info.h"
25 namespace net {
27 QuicHttpStream::QuicHttpStream(
28 const base::WeakPtr<QuicChromiumClientSession>& session)
29 : next_state_(STATE_NONE),
30 session_(session),
31 session_error_(OK),
32 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()),
33 stream_(nullptr),
34 request_info_(nullptr),
35 request_body_stream_(nullptr),
36 priority_(MINIMUM_PRIORITY),
37 response_info_(nullptr),
38 response_status_(OK),
39 response_headers_received_(false),
40 closed_stream_received_bytes_(0),
41 closed_stream_sent_bytes_(0),
42 user_buffer_len_(0),
43 weak_factory_(this) {
44 DCHECK(session_);
45 session_->AddObserver(this);
48 QuicHttpStream::~QuicHttpStream() {
49 Close(false);
50 if (session_)
51 session_->RemoveObserver(this);
54 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
55 RequestPriority priority,
56 const BoundNetLog& stream_net_log,
57 const CompletionCallback& callback) {
58 DCHECK(!stream_);
59 if (!session_)
60 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
61 ERR_QUIC_HANDSHAKE_FAILED;
63 stream_net_log.AddEvent(
64 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
65 session_->net_log().source().ToEventParametersCallback());
67 if (request_info->url.SchemeIsCryptographic()) {
68 SSLInfo ssl_info;
69 bool secure_session =
70 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
71 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
72 secure_session);
73 if (!secure_session)
74 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
77 stream_net_log_ = stream_net_log;
78 request_info_ = request_info;
79 request_time_ = base::Time::Now();
80 priority_ = priority;
82 int rv = stream_request_.StartRequest(
83 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
84 weak_factory_.GetWeakPtr()));
85 if (rv == ERR_IO_PENDING) {
86 callback_ = callback;
87 } else if (rv == OK) {
88 stream_->SetDelegate(this);
89 } else if (!was_handshake_confirmed_) {
90 rv = ERR_QUIC_HANDSHAKE_FAILED;
93 return rv;
96 void QuicHttpStream::OnStreamReady(int rv) {
97 DCHECK(rv == OK || !stream_);
98 if (rv == OK) {
99 stream_->SetDelegate(this);
100 } else if (!was_handshake_confirmed_) {
101 rv = ERR_QUIC_HANDSHAKE_FAILED;
104 ResetAndReturn(&callback_).Run(rv);
107 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
108 HttpResponseInfo* response,
109 const CompletionCallback& callback) {
110 CHECK(!request_body_stream_);
111 CHECK(!response_info_);
112 CHECK(!callback.is_null());
113 CHECK(response);
115 // TODO(rch): remove this once we figure out why channel ID is not being
116 // sent when it should be.
117 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
118 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
119 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
120 SSLInfo ssl_info;
121 bool secure_session =
122 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
123 DCHECK(secure_session);
124 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
125 ssl_info.channel_id_sent);
127 if (!stream_) {
128 return ERR_CONNECTION_CLOSED;
131 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
132 stream_->set_priority(priority);
133 // Store the serialized request headers.
134 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
135 GetSpdyVersion(),
136 /*direct=*/true, &request_headers_);
138 // Store the request body.
139 request_body_stream_ = request_info_->upload_data_stream;
140 if (request_body_stream_) {
141 // TODO(rch): Can we be more precise about when to allocate
142 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
143 // was being called even if we didn't yet allocate raw_request_body_buf_.
144 // && (request_body_stream_->size() ||
145 // request_body_stream_->is_chunked()))
146 // Use 10 packets as the body buffer size to give enough space to
147 // help ensure we don't often send out partial packets.
148 raw_request_body_buf_ =
149 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
150 // The request body buffer is empty at first.
151 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
154 // Store the response info.
155 response_info_ = response;
157 next_state_ = STATE_SEND_HEADERS;
158 int rv = DoLoop(OK);
159 if (rv == ERR_IO_PENDING)
160 callback_ = callback;
162 return rv > 0 ? OK : rv;
165 UploadProgress QuicHttpStream::GetUploadProgress() const {
166 if (!request_body_stream_)
167 return UploadProgress();
169 return UploadProgress(request_body_stream_->position(),
170 request_body_stream_->size());
173 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
174 CHECK(!callback.is_null());
176 if (stream_ == nullptr)
177 return response_status_;
179 // Check if we already have the response headers. If so, return synchronously.
180 if (response_headers_received_)
181 return OK;
183 // Still waiting for the response, return IO_PENDING.
184 CHECK(callback_.is_null());
185 callback_ = callback;
186 return ERR_IO_PENDING;
189 int QuicHttpStream::ReadResponseBody(
190 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
191 if (!stream_) {
192 // If the stream is already closed, there is no body to read.
193 return response_status_;
196 int rv = ReadAvailableData(buf, buf_len);
197 if (rv != ERR_IO_PENDING)
198 return rv;
200 CHECK(callback_.is_null());
201 CHECK(!user_buffer_.get());
202 CHECK_EQ(0, user_buffer_len_);
204 callback_ = callback;
205 user_buffer_ = buf;
206 user_buffer_len_ = buf_len;
207 return ERR_IO_PENDING;
210 void QuicHttpStream::Close(bool not_reusable) {
211 // Note: the not_reusable flag has no meaning for SPDY streams.
212 if (stream_) {
213 stream_->SetDelegate(nullptr);
214 stream_->Reset(QUIC_STREAM_CANCELLED);
215 ResetStream();
216 response_status_ = was_handshake_confirmed_ ?
217 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
221 HttpStream* QuicHttpStream::RenewStreamForAuth() {
222 return nullptr;
225 bool QuicHttpStream::IsResponseBodyComplete() const {
226 return next_state_ == STATE_OPEN && !stream_;
229 bool QuicHttpStream::IsConnectionReused() const {
230 // TODO(rch): do something smarter here.
231 return stream_ && stream_->id() > 1;
234 void QuicHttpStream::SetConnectionReused() {
235 // QUIC doesn't need an indicator here.
238 bool QuicHttpStream::CanReuseConnection() const {
239 // QUIC streams aren't considered reusable.
240 return false;
243 int64 QuicHttpStream::GetTotalReceivedBytes() const {
244 // TODO(sclittle): Currently, this only includes response body bytes. Change
245 // this to include headers and QUIC overhead as well.
246 if (stream_) {
247 return stream_->stream_bytes_read();
250 return closed_stream_received_bytes_;
253 int64_t QuicHttpStream::GetTotalSentBytes() const {
254 // TODO(sclittle): Currently, this only includes request body bytes. Change
255 // this to include headers and QUIC overhead as well.
256 if (stream_) {
257 return stream_->stream_bytes_written();
260 return closed_stream_sent_bytes_;
263 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
264 // TODO(mmenke): Figure out what to do here.
265 return true;
268 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
269 DCHECK(stream_);
270 session_->GetSSLInfo(ssl_info);
273 void QuicHttpStream::GetSSLCertRequestInfo(
274 SSLCertRequestInfo* cert_request_info) {
275 DCHECK(stream_);
276 NOTIMPLEMENTED();
279 void QuicHttpStream::Drain(HttpNetworkSession* session) {
280 NOTREACHED();
281 Close(false);
282 delete this;
285 void QuicHttpStream::SetPriority(RequestPriority priority) {
286 priority_ = priority;
289 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock& headers) {
290 int rv = ProcessResponseHeaders(headers);
291 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
292 DoCallback(rv);
296 void QuicHttpStream::OnDataAvailable() {
297 if (callback_.is_null()) {
298 // Data is available, but can't be delivered
299 return;
302 CHECK(user_buffer_.get());
303 CHECK_NE(0, user_buffer_len_);
304 int rv = ReadAvailableData(user_buffer_.get(), user_buffer_len_);
305 if (rv == ERR_IO_PENDING) {
306 // This was a spurrious notification. Wait for the next one.
307 return;
310 CHECK(!callback_.is_null());
311 user_buffer_ = nullptr;
312 user_buffer_len_ = 0;
313 DoCallback(rv);
316 void QuicHttpStream::OnClose(QuicErrorCode error) {
317 if (error != QUIC_NO_ERROR) {
318 response_status_ = was_handshake_confirmed_ ?
319 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
320 } else if (!response_headers_received_) {
321 response_status_ = ERR_ABORTED;
324 ResetStream();
325 if (!callback_.is_null())
326 DoCallback(response_status_);
329 void QuicHttpStream::OnError(int error) {
330 ResetStream();
331 response_status_ = was_handshake_confirmed_ ?
332 error : ERR_QUIC_HANDSHAKE_FAILED;
333 if (!callback_.is_null())
334 DoCallback(response_status_);
337 bool QuicHttpStream::HasSendHeadersComplete() {
338 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
341 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
342 was_handshake_confirmed_ = true;
345 void QuicHttpStream::OnSessionClosed(int error) {
346 Close(false);
347 session_error_ = error;
348 session_.reset();
351 void QuicHttpStream::OnIOComplete(int rv) {
352 rv = DoLoop(rv);
354 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
355 DoCallback(rv);
359 void QuicHttpStream::DoCallback(int rv) {
360 CHECK_NE(rv, ERR_IO_PENDING);
361 CHECK(!callback_.is_null());
363 // The client callback can do anything, including destroying this class,
364 // so any pending callback must be issued after everything else is done.
365 base::ResetAndReturn(&callback_).Run(rv);
368 int QuicHttpStream::DoLoop(int rv) {
369 do {
370 State state = next_state_;
371 next_state_ = STATE_NONE;
372 switch (state) {
373 case STATE_SEND_HEADERS:
374 CHECK_EQ(OK, rv);
375 rv = DoSendHeaders();
376 break;
377 case STATE_SEND_HEADERS_COMPLETE:
378 rv = DoSendHeadersComplete(rv);
379 break;
380 case STATE_READ_REQUEST_BODY:
381 CHECK_EQ(OK, rv);
382 rv = DoReadRequestBody();
383 break;
384 case STATE_READ_REQUEST_BODY_COMPLETE:
385 rv = DoReadRequestBodyComplete(rv);
386 break;
387 case STATE_SEND_BODY:
388 CHECK_EQ(OK, rv);
389 rv = DoSendBody();
390 break;
391 case STATE_SEND_BODY_COMPLETE:
392 rv = DoSendBodyComplete(rv);
393 break;
394 case STATE_OPEN:
395 CHECK_EQ(OK, rv);
396 break;
397 default:
398 NOTREACHED() << "next_state_: " << next_state_;
399 break;
401 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
402 rv != ERR_IO_PENDING);
404 return rv;
407 int QuicHttpStream::DoSendHeaders() {
408 if (!stream_)
409 return ERR_UNEXPECTED;
411 // Log the actual request with the URL Request's net log.
412 stream_net_log_.AddEvent(
413 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
414 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
415 priority_));
416 // Also log to the QuicSession's net log.
417 stream_->net_log().AddEvent(
418 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
419 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
420 priority_));
422 bool has_upload_data = request_body_stream_ != nullptr;
424 next_state_ = STATE_SEND_HEADERS_COMPLETE;
425 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
426 request_headers_.clear();
427 return rv;
430 int QuicHttpStream::DoSendHeadersComplete(int rv) {
431 if (rv < 0)
432 return rv;
434 next_state_ = request_body_stream_ ?
435 STATE_READ_REQUEST_BODY : STATE_OPEN;
437 return OK;
440 int QuicHttpStream::DoReadRequestBody() {
441 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
442 return request_body_stream_->Read(
443 raw_request_body_buf_.get(),
444 raw_request_body_buf_->size(),
445 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
448 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
449 // |rv| is the result of read from the request body from the last call to
450 // DoSendBody().
451 if (rv < 0)
452 return rv;
454 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
455 if (rv == 0) { // Reached the end.
456 DCHECK(request_body_stream_->IsEOF());
459 next_state_ = STATE_SEND_BODY;
460 return OK;
463 int QuicHttpStream::DoSendBody() {
464 if (!stream_)
465 return ERR_UNEXPECTED;
467 CHECK(request_body_stream_);
468 CHECK(request_body_buf_.get());
469 const bool eof = request_body_stream_->IsEOF();
470 int len = request_body_buf_->BytesRemaining();
471 if (len > 0 || eof) {
472 next_state_ = STATE_SEND_BODY_COMPLETE;
473 base::StringPiece data(request_body_buf_->data(), len);
474 return stream_->WriteStreamData(
475 data, eof,
476 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
479 next_state_ = STATE_OPEN;
480 return OK;
483 int QuicHttpStream::DoSendBodyComplete(int rv) {
484 if (rv < 0)
485 return rv;
487 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
489 if (!request_body_stream_->IsEOF()) {
490 next_state_ = STATE_READ_REQUEST_BODY;
491 return OK;
494 next_state_ = STATE_OPEN;
495 return OK;
498 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock& headers) {
499 // The URLRequest logs these headers, so only log to the QuicSession's
500 // net log.
501 stream_->net_log().AddEvent(
502 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
503 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
505 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
506 DLOG(WARNING) << "Invalid headers";
507 return ERR_QUIC_PROTOCOL_ERROR;
509 // Put the peer's IP address and port into the response.
510 IPEndPoint address = session_->peer_address();
511 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
512 response_info_->connection_info =
513 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
514 response_info_->vary_data
515 .Init(*request_info_, *response_info_->headers.get());
516 response_info_->was_npn_negotiated = true;
517 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
518 response_info_->response_time = base::Time::Now();
519 response_info_->request_time = request_time_;
520 response_headers_received_ = true;
522 return OK;
525 int QuicHttpStream::ReadAvailableData(IOBuffer* buf, int buf_len) {
526 int rv = stream_->Read(buf, buf_len);
527 if (stream_->IsDoneReading()) {
528 stream_->SetDelegate(nullptr);
529 stream_->OnFinRead();
530 ResetStream();
532 return rv;
535 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
536 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
539 void QuicHttpStream::ResetStream() {
540 if (!stream_)
541 return;
542 closed_stream_received_bytes_ = stream_->stream_bytes_read();
543 closed_stream_sent_bytes_ = stream_->stream_bytes_written();
544 stream_ = nullptr;
546 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress
547 // read.
548 if (request_body_stream_)
549 request_body_stream_->Reset();
552 } // namespace net