Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
bloba26a81e1876d224e1fe5599e5b2de263ad591615
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 #ifdef TEMP_INSTRUMENTATION_468529
9 #include "base/debug/alias.h"
10 #endif
11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/http/http_response_headers.h"
16 #include "net/http/http_util.h"
17 #include "net/quic/quic_chromium_client_session.h"
18 #include "net/quic/quic_http_utils.h"
19 #include "net/quic/quic_reliable_client_stream.h"
20 #include "net/quic/quic_utils.h"
21 #include "net/quic/spdy_utils.h"
22 #include "net/socket/next_proto.h"
23 #include "net/spdy/spdy_frame_builder.h"
24 #include "net/spdy/spdy_framer.h"
25 #include "net/spdy/spdy_http_utils.h"
26 #include "net/ssl/ssl_info.h"
28 namespace net {
30 QuicHttpStream::QuicHttpStream(
31 const base::WeakPtr<QuicChromiumClientSession>& session)
32 : next_state_(STATE_NONE),
33 session_(session),
34 session_error_(OK),
35 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()),
36 stream_(nullptr),
37 request_info_(nullptr),
38 request_body_stream_(nullptr),
39 priority_(MINIMUM_PRIORITY),
40 response_info_(nullptr),
41 response_status_(OK),
42 response_headers_received_(false),
43 closed_stream_received_bytes_(0),
44 closed_stream_sent_bytes_(0),
45 user_buffer_len_(0),
46 weak_factory_(this) {
47 DCHECK(session_);
48 session_->AddObserver(this);
51 QuicHttpStream::~QuicHttpStream() {
52 #ifdef TEMP_INSTRUMENTATION_468529
53 liveness_ = DEAD;
54 stack_trace_ = base::debug::StackTrace();
56 // Probably not necessary, but just in case compiler tries to optimize out the
57 // writes to liveness_ and stack_trace_.
58 base::debug::Alias(&liveness_);
59 base::debug::Alias(&stack_trace_);
60 #endif
62 Close(false);
63 if (session_)
64 session_->RemoveObserver(this);
67 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
68 RequestPriority priority,
69 const BoundNetLog& stream_net_log,
70 const CompletionCallback& callback) {
71 DCHECK(!stream_);
72 if (!session_)
73 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
74 ERR_QUIC_HANDSHAKE_FAILED;
76 stream_net_log.AddEvent(
77 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
78 session_->net_log().source().ToEventParametersCallback());
80 if (request_info->url.SchemeIsCryptographic()) {
81 SSLInfo ssl_info;
82 bool secure_session =
83 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
84 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
85 secure_session);
86 if (!secure_session)
87 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
90 stream_net_log_ = stream_net_log;
91 request_info_ = request_info;
92 request_time_ = base::Time::Now();
93 priority_ = priority;
95 int rv = stream_request_.StartRequest(
96 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
97 weak_factory_.GetWeakPtr()));
98 if (rv == ERR_IO_PENDING) {
99 callback_ = callback;
100 } else if (rv == OK) {
101 stream_->SetDelegate(this);
102 } else if (!was_handshake_confirmed_) {
103 rv = ERR_QUIC_HANDSHAKE_FAILED;
106 return rv;
109 void QuicHttpStream::OnStreamReady(int rv) {
110 DCHECK(rv == OK || !stream_);
111 if (rv == OK) {
112 stream_->SetDelegate(this);
113 } else if (!was_handshake_confirmed_) {
114 rv = ERR_QUIC_HANDSHAKE_FAILED;
117 ResetAndReturn(&callback_).Run(rv);
120 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
121 HttpResponseInfo* response,
122 const CompletionCallback& callback) {
123 CHECK(!request_body_stream_);
124 CHECK(!response_info_);
125 CHECK(!callback.is_null());
126 CHECK(response);
128 // TODO(rch): remove this once we figure out why channel ID is not being
129 // sent when it should be.
130 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
131 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
132 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
133 SSLInfo ssl_info;
134 bool secure_session =
135 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
136 DCHECK(secure_session);
137 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
138 ssl_info.channel_id_sent);
140 if (!stream_) {
141 return ERR_CONNECTION_CLOSED;
144 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
145 stream_->set_priority(priority);
146 // Store the serialized request headers.
147 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
148 GetSpdyVersion(),
149 /*direct=*/true, &request_headers_);
151 // Store the request body.
152 request_body_stream_ = request_info_->upload_data_stream;
153 if (request_body_stream_) {
154 // TODO(rch): Can we be more precise about when to allocate
155 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
156 // was being called even if we didn't yet allocate raw_request_body_buf_.
157 // && (request_body_stream_->size() ||
158 // request_body_stream_->is_chunked()))
159 // Use 10 packets as the body buffer size to give enough space to
160 // help ensure we don't often send out partial packets.
161 raw_request_body_buf_ =
162 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
163 // The request body buffer is empty at first.
164 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
167 // Store the response info.
168 response_info_ = response;
170 next_state_ = STATE_SEND_HEADERS;
171 int rv = DoLoop(OK);
172 if (rv == ERR_IO_PENDING)
173 callback_ = callback;
175 return rv > 0 ? OK : rv;
178 UploadProgress QuicHttpStream::GetUploadProgress() const {
179 if (!request_body_stream_)
180 return UploadProgress();
182 return UploadProgress(request_body_stream_->position(),
183 request_body_stream_->size());
186 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
187 CHECK(!callback.is_null());
189 if (stream_ == nullptr)
190 return response_status_;
192 // Check if we already have the response headers. If so, return synchronously.
193 if (response_headers_received_)
194 return OK;
196 // Still waiting for the response, return IO_PENDING.
197 CHECK(callback_.is_null());
198 callback_ = callback;
199 return ERR_IO_PENDING;
202 int QuicHttpStream::ReadResponseBody(
203 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
204 if (!stream_) {
205 // If the stream is already closed, there is no body to read.
206 return response_status_;
209 int rv = ReadAvailableData(buf, buf_len);
210 if (rv != ERR_IO_PENDING)
211 return rv;
213 CHECK(callback_.is_null());
214 CHECK(!user_buffer_.get());
215 CHECK_EQ(0, user_buffer_len_);
217 callback_ = callback;
218 user_buffer_ = buf;
219 user_buffer_len_ = buf_len;
220 return ERR_IO_PENDING;
223 void QuicHttpStream::Close(bool not_reusable) {
224 // Note: the not_reusable flag has no meaning for SPDY streams.
225 if (stream_) {
226 stream_->SetDelegate(nullptr);
227 stream_->Reset(QUIC_STREAM_CANCELLED);
228 ResetStream();
229 response_status_ = was_handshake_confirmed_ ?
230 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
234 HttpStream* QuicHttpStream::RenewStreamForAuth() {
235 return nullptr;
238 bool QuicHttpStream::IsResponseBodyComplete() const {
239 return next_state_ == STATE_OPEN && !stream_;
242 bool QuicHttpStream::IsConnectionReused() const {
243 // TODO(rch): do something smarter here.
244 return stream_ && stream_->id() > 1;
247 void QuicHttpStream::SetConnectionReused() {
248 // QUIC doesn't need an indicator here.
251 bool QuicHttpStream::CanReuseConnection() const {
252 // QUIC streams aren't considered reusable.
253 return false;
256 int64 QuicHttpStream::GetTotalReceivedBytes() const {
257 // TODO(sclittle): Currently, this only includes response body bytes. Change
258 // this to include headers and QUIC overhead as well.
259 if (stream_) {
260 return stream_->stream_bytes_read();
263 return closed_stream_received_bytes_;
266 int64_t QuicHttpStream::GetTotalSentBytes() const {
267 // TODO(sclittle): Currently, this only includes request body bytes. Change
268 // this to include headers and QUIC overhead as well.
269 if (stream_) {
270 return stream_->stream_bytes_written();
273 return closed_stream_sent_bytes_;
276 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
277 // TODO(mmenke): Figure out what to do here.
278 return true;
281 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
282 DCHECK(stream_);
283 session_->GetSSLInfo(ssl_info);
286 void QuicHttpStream::GetSSLCertRequestInfo(
287 SSLCertRequestInfo* cert_request_info) {
288 DCHECK(stream_);
289 NOTIMPLEMENTED();
292 void QuicHttpStream::Drain(HttpNetworkSession* session) {
293 NOTREACHED();
294 Close(false);
295 delete this;
298 void QuicHttpStream::SetPriority(RequestPriority priority) {
299 priority_ = priority;
302 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock& headers) {
303 int rv = ProcessResponseHeaders(headers);
304 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
305 DoCallback(rv);
309 void QuicHttpStream::OnDataAvailable() {
310 if (callback_.is_null()) {
311 // Data is available, but can't be delivered
312 return;
315 CHECK(user_buffer_.get());
316 CHECK_NE(0, user_buffer_len_);
317 int rv = ReadAvailableData(user_buffer_.get(), user_buffer_len_);
318 if (rv == ERR_IO_PENDING) {
319 // This was a spurrious notification. Wait for the next one.
320 return;
323 CHECK(!callback_.is_null());
324 user_buffer_ = nullptr;
325 user_buffer_len_ = 0;
326 DoCallback(rv);
329 void QuicHttpStream::OnClose(QuicErrorCode error) {
330 if (error != QUIC_NO_ERROR) {
331 response_status_ = was_handshake_confirmed_ ?
332 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
333 } else if (!response_headers_received_) {
334 response_status_ = ERR_ABORTED;
337 ResetStream();
338 if (!callback_.is_null())
339 DoCallback(response_status_);
342 void QuicHttpStream::OnError(int error) {
343 ResetStream();
344 response_status_ = was_handshake_confirmed_ ?
345 error : ERR_QUIC_HANDSHAKE_FAILED;
346 if (!callback_.is_null())
347 DoCallback(response_status_);
350 bool QuicHttpStream::HasSendHeadersComplete() {
351 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
354 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
355 was_handshake_confirmed_ = true;
358 void QuicHttpStream::OnSessionClosed(int error) {
359 Close(false);
360 session_error_ = error;
361 session_.reset();
364 void QuicHttpStream::OnIOComplete(int rv) {
365 rv = DoLoop(rv);
367 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
368 DoCallback(rv);
372 void QuicHttpStream::DoCallback(int rv) {
373 CHECK_NE(rv, ERR_IO_PENDING);
374 CHECK(!callback_.is_null());
376 // The client callback can do anything, including destroying this class,
377 // so any pending callback must be issued after everything else is done.
378 base::ResetAndReturn(&callback_).Run(rv);
381 int QuicHttpStream::DoLoop(int rv) {
382 do {
383 CrashIfInvalid();
385 State state = next_state_;
386 next_state_ = STATE_NONE;
387 switch (state) {
388 case STATE_SEND_HEADERS:
389 CHECK_EQ(OK, rv);
390 rv = DoSendHeaders();
391 break;
392 case STATE_SEND_HEADERS_COMPLETE:
393 rv = DoSendHeadersComplete(rv);
394 break;
395 case STATE_READ_REQUEST_BODY:
396 CHECK_EQ(OK, rv);
397 rv = DoReadRequestBody();
398 break;
399 case STATE_READ_REQUEST_BODY_COMPLETE:
400 rv = DoReadRequestBodyComplete(rv);
401 break;
402 case STATE_SEND_BODY:
403 CHECK_EQ(OK, rv);
404 rv = DoSendBody();
405 break;
406 case STATE_SEND_BODY_COMPLETE:
407 rv = DoSendBodyComplete(rv);
408 break;
409 case STATE_OPEN:
410 CHECK_EQ(OK, rv);
411 break;
412 default:
413 NOTREACHED() << "next_state_: " << next_state_;
414 break;
416 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
417 rv != ERR_IO_PENDING);
419 return rv;
422 int QuicHttpStream::DoSendHeaders() {
423 if (!stream_)
424 return ERR_UNEXPECTED;
426 // Log the actual request with the URL Request's net log.
427 stream_net_log_.AddEvent(
428 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
429 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
430 priority_));
431 // Also log to the QuicSession's net log.
432 stream_->net_log().AddEvent(
433 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
434 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
435 priority_));
437 bool has_upload_data = request_body_stream_ != nullptr;
439 next_state_ = STATE_SEND_HEADERS_COMPLETE;
440 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
441 request_headers_.clear();
442 return rv;
445 int QuicHttpStream::DoSendHeadersComplete(int rv) {
446 if (rv < 0)
447 return rv;
449 next_state_ = request_body_stream_ ?
450 STATE_READ_REQUEST_BODY : STATE_OPEN;
452 return OK;
455 int QuicHttpStream::DoReadRequestBody() {
456 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
457 return request_body_stream_->Read(
458 raw_request_body_buf_.get(),
459 raw_request_body_buf_->size(),
460 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
463 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
464 // |rv| is the result of read from the request body from the last call to
465 // DoSendBody().
466 if (rv < 0)
467 return rv;
469 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
470 if (rv == 0) { // Reached the end.
471 DCHECK(request_body_stream_->IsEOF());
474 next_state_ = STATE_SEND_BODY;
475 return OK;
478 int QuicHttpStream::DoSendBody() {
479 if (!stream_)
480 return ERR_UNEXPECTED;
482 CHECK(request_body_stream_);
483 CHECK(request_body_buf_.get());
484 const bool eof = request_body_stream_->IsEOF();
485 int len = request_body_buf_->BytesRemaining();
486 if (len > 0 || eof) {
487 next_state_ = STATE_SEND_BODY_COMPLETE;
488 base::StringPiece data(request_body_buf_->data(), len);
489 return stream_->WriteStreamData(
490 data, eof,
491 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
494 next_state_ = STATE_OPEN;
495 return OK;
498 int QuicHttpStream::DoSendBodyComplete(int rv) {
499 if (rv < 0)
500 return rv;
502 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
504 if (!request_body_stream_->IsEOF()) {
505 next_state_ = STATE_READ_REQUEST_BODY;
506 return OK;
509 next_state_ = STATE_OPEN;
510 return OK;
513 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock& headers) {
514 // The URLRequest logs these headers, so only log to the QuicSession's
515 // net log.
516 stream_->net_log().AddEvent(
517 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
518 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
520 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
521 DLOG(WARNING) << "Invalid headers";
522 return ERR_QUIC_PROTOCOL_ERROR;
524 // Put the peer's IP address and port into the response.
525 IPEndPoint address = session_->peer_address();
526 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
527 response_info_->connection_info =
528 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
529 response_info_->vary_data
530 .Init(*request_info_, *response_info_->headers.get());
531 response_info_->was_npn_negotiated = true;
532 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
533 response_info_->response_time = base::Time::Now();
534 response_info_->request_time = request_time_;
535 response_headers_received_ = true;
537 return OK;
540 int QuicHttpStream::ReadAvailableData(IOBuffer* buf, int buf_len) {
541 int rv = stream_->Read(buf, buf_len);
542 if (stream_->IsDoneReading()) {
543 stream_->SetDelegate(nullptr);
544 stream_->OnFinRead();
545 ResetStream();
547 return rv;
550 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
551 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
554 void QuicHttpStream::ResetStream() {
555 if (!stream_)
556 return;
557 closed_stream_received_bytes_ = stream_->stream_bytes_read();
558 closed_stream_sent_bytes_ = stream_->stream_bytes_written();
559 stream_ = nullptr;
561 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress
562 // read.
563 if (request_body_stream_)
564 request_body_stream_->Reset();
567 void QuicHttpStream::CrashIfInvalid() const {
568 #ifdef TEMP_INSTRUMENTATION_468529
569 Liveness liveness = liveness_;
571 if (liveness == ALIVE)
572 return;
574 // Copy relevant variables onto the stack to guarantee they will be available
575 // in minidumps, and then crash.
576 base::debug::StackTrace stack_trace = stack_trace_;
578 base::debug::Alias(&liveness);
579 base::debug::Alias(&stack_trace);
581 CHECK_EQ(ALIVE, liveness);
582 #endif
585 } // namespace net