Delete unused downloads page asset.
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
blobab46a6d2a10d4b49016cb3e11f7ed3c8eccabe47
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 user_buffer_len_(0),
45 weak_factory_(this) {
46 DCHECK(session_);
47 session_->AddObserver(this);
50 QuicHttpStream::~QuicHttpStream() {
51 #ifdef TEMP_INSTRUMENTATION_468529
52 liveness_ = DEAD;
53 stack_trace_ = base::debug::StackTrace();
55 // Probably not necessary, but just in case compiler tries to optimize out the
56 // writes to liveness_ and stack_trace_.
57 base::debug::Alias(&liveness_);
58 base::debug::Alias(&stack_trace_);
59 #endif
61 Close(false);
62 if (session_)
63 session_->RemoveObserver(this);
66 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
67 RequestPriority priority,
68 const BoundNetLog& stream_net_log,
69 const CompletionCallback& callback) {
70 DCHECK(!stream_);
71 if (!session_)
72 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
73 ERR_QUIC_HANDSHAKE_FAILED;
75 stream_net_log.AddEvent(
76 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
77 session_->net_log().source().ToEventParametersCallback());
79 if (request_info->url.SchemeIsCryptographic()) {
80 SSLInfo ssl_info;
81 bool secure_session =
82 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
83 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
84 secure_session);
85 if (!secure_session)
86 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
89 stream_net_log_ = stream_net_log;
90 request_info_ = request_info;
91 request_time_ = base::Time::Now();
92 priority_ = priority;
94 int rv = stream_request_.StartRequest(
95 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
96 weak_factory_.GetWeakPtr()));
97 if (rv == ERR_IO_PENDING) {
98 callback_ = callback;
99 } else if (rv == OK) {
100 stream_->SetDelegate(this);
101 } else if (!was_handshake_confirmed_) {
102 rv = ERR_QUIC_HANDSHAKE_FAILED;
105 return rv;
108 void QuicHttpStream::OnStreamReady(int rv) {
109 DCHECK(rv == OK || !stream_);
110 if (rv == OK) {
111 stream_->SetDelegate(this);
112 } else if (!was_handshake_confirmed_) {
113 rv = ERR_QUIC_HANDSHAKE_FAILED;
116 ResetAndReturn(&callback_).Run(rv);
119 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
120 HttpResponseInfo* response,
121 const CompletionCallback& callback) {
122 CHECK(!request_body_stream_);
123 CHECK(!response_info_);
124 CHECK(!callback.is_null());
125 CHECK(response);
127 // TODO(rch): remove this once we figure out why channel ID is not being
128 // sent when it should be.
129 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
130 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
131 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
132 SSLInfo ssl_info;
133 bool secure_session =
134 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
135 DCHECK(secure_session);
136 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
137 ssl_info.channel_id_sent);
139 if (!stream_) {
140 return ERR_CONNECTION_CLOSED;
143 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
144 stream_->set_priority(priority);
145 // Store the serialized request headers.
146 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
147 GetSpdyVersion(),
148 /*direct=*/true, &request_headers_);
150 // Store the request body.
151 request_body_stream_ = request_info_->upload_data_stream;
152 if (request_body_stream_) {
153 // TODO(rch): Can we be more precise about when to allocate
154 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
155 // was being called even if we didn't yet allocate raw_request_body_buf_.
156 // && (request_body_stream_->size() ||
157 // request_body_stream_->is_chunked()))
158 // Use 10 packets as the body buffer size to give enough space to
159 // help ensure we don't often send out partial packets.
160 raw_request_body_buf_ =
161 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
162 // The request body buffer is empty at first.
163 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
166 // Store the response info.
167 response_info_ = response;
169 next_state_ = STATE_SEND_HEADERS;
170 int rv = DoLoop(OK);
171 if (rv == ERR_IO_PENDING)
172 callback_ = callback;
174 return rv > 0 ? OK : rv;
177 UploadProgress QuicHttpStream::GetUploadProgress() const {
178 if (!request_body_stream_)
179 return UploadProgress();
181 return UploadProgress(request_body_stream_->position(),
182 request_body_stream_->size());
185 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
186 CHECK(!callback.is_null());
188 if (stream_ == nullptr)
189 return response_status_;
191 // Check if we already have the response headers. If so, return synchronously.
192 if (response_headers_received_)
193 return OK;
195 // Still waiting for the response, return IO_PENDING.
196 CHECK(callback_.is_null());
197 callback_ = callback;
198 return ERR_IO_PENDING;
201 int QuicHttpStream::ReadResponseBody(
202 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
203 if (!stream_) {
204 // If the stream is already closed, there is no body to read.
205 return response_status_;
208 int rv = ReadAvailableData(buf, buf_len);
209 if (rv != ERR_IO_PENDING)
210 return rv;
212 CHECK(callback_.is_null());
213 CHECK(!user_buffer_.get());
214 CHECK_EQ(0, user_buffer_len_);
216 callback_ = callback;
217 user_buffer_ = buf;
218 user_buffer_len_ = buf_len;
219 return ERR_IO_PENDING;
222 void QuicHttpStream::Close(bool not_reusable) {
223 // Note: the not_reusable flag has no meaning for SPDY streams.
224 if (stream_) {
225 stream_->SetDelegate(nullptr);
226 stream_->Reset(QUIC_STREAM_CANCELLED);
227 ResetStream();
228 response_status_ = was_handshake_confirmed_ ?
229 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
233 HttpStream* QuicHttpStream::RenewStreamForAuth() {
234 return nullptr;
237 bool QuicHttpStream::IsResponseBodyComplete() const {
238 return next_state_ == STATE_OPEN && !stream_;
241 bool QuicHttpStream::CanFindEndOfResponse() const {
242 return true;
245 bool QuicHttpStream::IsConnectionReused() const {
246 // TODO(rch): do something smarter here.
247 return stream_ && stream_->id() > 1;
250 void QuicHttpStream::SetConnectionReused() {
251 // QUIC doesn't need an indicator here.
254 bool QuicHttpStream::IsConnectionReusable() const {
255 // QUIC streams aren't considered reusable.
256 return false;
259 int64 QuicHttpStream::GetTotalReceivedBytes() const {
260 if (stream_) {
261 return stream_->stream_bytes_read();
264 return closed_stream_received_bytes_;
267 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
268 // TODO(mmenke): Figure out what to do here.
269 return true;
272 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
273 DCHECK(stream_);
274 session_->GetSSLInfo(ssl_info);
277 void QuicHttpStream::GetSSLCertRequestInfo(
278 SSLCertRequestInfo* cert_request_info) {
279 DCHECK(stream_);
280 NOTIMPLEMENTED();
283 bool QuicHttpStream::IsSpdyHttpStream() const {
284 return false;
287 void QuicHttpStream::Drain(HttpNetworkSession* session) {
288 Close(false);
289 delete this;
292 void QuicHttpStream::SetPriority(RequestPriority priority) {
293 priority_ = priority;
296 void QuicHttpStream::OnHeadersAvailable(const SpdyHeaderBlock& headers) {
297 int rv = ProcessResponseHeaders(headers);
298 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
299 DoCallback(rv);
303 void QuicHttpStream::OnDataAvailable() {
304 if (callback_.is_null()) {
305 // Data is available, but can't be delivered
306 return;
309 CHECK(user_buffer_.get());
310 CHECK_NE(0, user_buffer_len_);
311 int rv = ReadAvailableData(user_buffer_.get(), user_buffer_len_);
312 if (rv == ERR_IO_PENDING) {
313 // This was a spurrious notification. Wait for the next one.
314 return;
317 CHECK(!callback_.is_null());
318 user_buffer_ = nullptr;
319 user_buffer_len_ = 0;
320 DoCallback(rv);
323 void QuicHttpStream::OnClose(QuicErrorCode error) {
324 if (error != QUIC_NO_ERROR) {
325 response_status_ = was_handshake_confirmed_ ?
326 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
327 } else if (!response_headers_received_) {
328 response_status_ = ERR_ABORTED;
331 ResetStream();
332 if (!callback_.is_null())
333 DoCallback(response_status_);
336 void QuicHttpStream::OnError(int error) {
337 ResetStream();
338 response_status_ = was_handshake_confirmed_ ?
339 error : ERR_QUIC_HANDSHAKE_FAILED;
340 if (!callback_.is_null())
341 DoCallback(response_status_);
344 bool QuicHttpStream::HasSendHeadersComplete() {
345 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
348 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
349 was_handshake_confirmed_ = true;
352 void QuicHttpStream::OnSessionClosed(int error) {
353 Close(false);
354 session_error_ = error;
355 session_.reset();
358 void QuicHttpStream::OnIOComplete(int rv) {
359 rv = DoLoop(rv);
361 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
362 DoCallback(rv);
366 void QuicHttpStream::DoCallback(int rv) {
367 CHECK_NE(rv, ERR_IO_PENDING);
368 CHECK(!callback_.is_null());
370 // The client callback can do anything, including destroying this class,
371 // so any pending callback must be issued after everything else is done.
372 base::ResetAndReturn(&callback_).Run(rv);
375 int QuicHttpStream::DoLoop(int rv) {
376 do {
377 CrashIfInvalid();
379 State state = next_state_;
380 next_state_ = STATE_NONE;
381 switch (state) {
382 case STATE_SEND_HEADERS:
383 CHECK_EQ(OK, rv);
384 rv = DoSendHeaders();
385 break;
386 case STATE_SEND_HEADERS_COMPLETE:
387 rv = DoSendHeadersComplete(rv);
388 break;
389 case STATE_READ_REQUEST_BODY:
390 CHECK_EQ(OK, rv);
391 rv = DoReadRequestBody();
392 break;
393 case STATE_READ_REQUEST_BODY_COMPLETE:
394 rv = DoReadRequestBodyComplete(rv);
395 break;
396 case STATE_SEND_BODY:
397 CHECK_EQ(OK, rv);
398 rv = DoSendBody();
399 break;
400 case STATE_SEND_BODY_COMPLETE:
401 rv = DoSendBodyComplete(rv);
402 break;
403 case STATE_OPEN:
404 CHECK_EQ(OK, rv);
405 break;
406 default:
407 NOTREACHED() << "next_state_: " << next_state_;
408 break;
410 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
411 rv != ERR_IO_PENDING);
413 return rv;
416 int QuicHttpStream::DoSendHeaders() {
417 if (!stream_)
418 return ERR_UNEXPECTED;
420 // Log the actual request with the URL Request's net log.
421 stream_net_log_.AddEvent(
422 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
423 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
424 priority_));
425 // Also log to the QuicSession's net log.
426 stream_->net_log().AddEvent(
427 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
428 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
429 priority_));
431 bool has_upload_data = request_body_stream_ != nullptr;
433 next_state_ = STATE_SEND_HEADERS_COMPLETE;
434 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
435 request_headers_.clear();
436 return rv;
439 int QuicHttpStream::DoSendHeadersComplete(int rv) {
440 if (rv < 0)
441 return rv;
443 next_state_ = request_body_stream_ ?
444 STATE_READ_REQUEST_BODY : STATE_OPEN;
446 return OK;
449 int QuicHttpStream::DoReadRequestBody() {
450 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
451 return request_body_stream_->Read(
452 raw_request_body_buf_.get(),
453 raw_request_body_buf_->size(),
454 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
457 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
458 // |rv| is the result of read from the request body from the last call to
459 // DoSendBody().
460 if (rv < 0)
461 return rv;
463 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
464 if (rv == 0) { // Reached the end.
465 DCHECK(request_body_stream_->IsEOF());
468 next_state_ = STATE_SEND_BODY;
469 return OK;
472 int QuicHttpStream::DoSendBody() {
473 if (!stream_)
474 return ERR_UNEXPECTED;
476 CHECK(request_body_stream_);
477 CHECK(request_body_buf_.get());
478 const bool eof = request_body_stream_->IsEOF();
479 int len = request_body_buf_->BytesRemaining();
480 if (len > 0 || eof) {
481 next_state_ = STATE_SEND_BODY_COMPLETE;
482 base::StringPiece data(request_body_buf_->data(), len);
483 return stream_->WriteStreamData(
484 data, eof,
485 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
488 next_state_ = STATE_OPEN;
489 return OK;
492 int QuicHttpStream::DoSendBodyComplete(int rv) {
493 if (rv < 0)
494 return rv;
496 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
498 if (!request_body_stream_->IsEOF()) {
499 next_state_ = STATE_READ_REQUEST_BODY;
500 return OK;
503 next_state_ = STATE_OPEN;
504 return OK;
507 int QuicHttpStream::ProcessResponseHeaders(const SpdyHeaderBlock& headers) {
508 // The URLRequest logs these headers, so only log to the QuicSession's
509 // net log.
510 stream_->net_log().AddEvent(
511 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
512 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
514 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
515 DLOG(WARNING) << "Invalid headers";
516 return ERR_QUIC_PROTOCOL_ERROR;
518 // Put the peer's IP address and port into the response.
519 IPEndPoint address = session_->peer_address();
520 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
521 response_info_->connection_info =
522 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
523 response_info_->vary_data
524 .Init(*request_info_, *response_info_->headers.get());
525 response_info_->was_npn_negotiated = true;
526 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
527 response_info_->response_time = base::Time::Now();
528 response_info_->request_time = request_time_;
529 response_headers_received_ = true;
531 return OK;
534 int QuicHttpStream::ReadAvailableData(IOBuffer* buf, int buf_len) {
535 int rv = stream_->Read(buf, buf_len);
536 if (stream_->IsDoneReading()) {
537 stream_->SetDelegate(nullptr);
538 stream_->OnFinRead();
539 ResetStream();
541 return rv;
544 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
545 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
548 void QuicHttpStream::ResetStream() {
549 if (!stream_)
550 return;
551 closed_stream_received_bytes_ = stream_->stream_bytes_read();
552 stream_ = nullptr;
555 void QuicHttpStream::CrashIfInvalid() const {
556 #ifdef TEMP_INSTRUMENTATION_468529
557 Liveness liveness = liveness_;
559 if (liveness == ALIVE)
560 return;
562 // Copy relevant variables onto the stack to guarantee they will be available
563 // in minidumps, and then crash.
564 base::debug::StackTrace stack_trace = stack_trace_;
566 base::debug::Alias(&liveness);
567 base::debug::Alias(&stack_trace);
569 CHECK_EQ(ALIVE, liveness);
570 #endif
573 } // namespace net