Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
blob052777b0e1b16873118151cda1448751cfa7e9f5
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 user_buffer_len_(0),
42 weak_factory_(this) {
43 DCHECK(session_);
44 session_->AddObserver(this);
47 QuicHttpStream::~QuicHttpStream() {
48 Close(false);
49 if (session_)
50 session_->RemoveObserver(this);
53 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
54 RequestPriority priority,
55 const BoundNetLog& stream_net_log,
56 const CompletionCallback& callback) {
57 DCHECK(!stream_);
58 if (!session_)
59 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
60 ERR_QUIC_HANDSHAKE_FAILED;
62 stream_net_log.AddEvent(
63 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
64 session_->net_log().source().ToEventParametersCallback());
66 if (request_info->url.SchemeIsCryptographic()) {
67 SSLInfo ssl_info;
68 bool secure_session =
69 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
70 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
71 secure_session);
72 if (!secure_session)
73 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
76 stream_net_log_ = stream_net_log;
77 request_info_ = request_info;
78 request_time_ = base::Time::Now();
79 priority_ = priority;
81 int rv = stream_request_.StartRequest(
82 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
83 weak_factory_.GetWeakPtr()));
84 if (rv == ERR_IO_PENDING) {
85 callback_ = callback;
86 } else if (rv == OK) {
87 stream_->SetDelegate(this);
88 } else if (!was_handshake_confirmed_) {
89 rv = ERR_QUIC_HANDSHAKE_FAILED;
92 return rv;
95 void QuicHttpStream::OnStreamReady(int rv) {
96 DCHECK(rv == OK || !stream_);
97 if (rv == OK) {
98 stream_->SetDelegate(this);
99 } else if (!was_handshake_confirmed_) {
100 rv = ERR_QUIC_HANDSHAKE_FAILED;
103 ResetAndReturn(&callback_).Run(rv);
106 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
107 HttpResponseInfo* response,
108 const CompletionCallback& callback) {
109 CHECK(!request_body_stream_);
110 CHECK(!response_info_);
111 CHECK(!callback.is_null());
112 CHECK(response);
114 // TODO(rch): remove this once we figure out why channel ID is not being
115 // sent when it should be.
116 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
117 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
118 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
119 SSLInfo ssl_info;
120 bool secure_session =
121 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
122 DCHECK(secure_session);
123 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
124 ssl_info.channel_id_sent);
126 if (!stream_) {
127 return ERR_CONNECTION_CLOSED;
130 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
131 stream_->set_priority(priority);
132 // Store the serialized request headers.
133 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
134 GetSpdyVersion(),
135 /*direct=*/true, &request_headers_);
137 // Store the request body.
138 request_body_stream_ = request_info_->upload_data_stream;
139 if (request_body_stream_) {
140 // TODO(rch): Can we be more precise about when to allocate
141 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
142 // was being called even if we didn't yet allocate raw_request_body_buf_.
143 // && (request_body_stream_->size() ||
144 // request_body_stream_->is_chunked()))
145 // Use 10 packets as the body buffer size to give enough space to
146 // help ensure we don't often send out partial packets.
147 raw_request_body_buf_ =
148 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
149 // The request body buffer is empty at first.
150 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
153 // Store the response info.
154 response_info_ = response;
156 next_state_ = STATE_SEND_HEADERS;
157 int rv = DoLoop(OK);
158 if (rv == ERR_IO_PENDING)
159 callback_ = callback;
161 return rv > 0 ? OK : rv;
164 UploadProgress QuicHttpStream::GetUploadProgress() const {
165 if (!request_body_stream_)
166 return UploadProgress();
168 return UploadProgress(request_body_stream_->position(),
169 request_body_stream_->size());
172 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
173 CHECK(!callback.is_null());
175 if (stream_ == nullptr)
176 return response_status_;
178 // Check if we already have the response headers. If so, return synchronously.
179 if (response_headers_received_)
180 return OK;
182 // Still waiting for the response, return IO_PENDING.
183 CHECK(callback_.is_null());
184 callback_ = callback;
185 return ERR_IO_PENDING;
188 int QuicHttpStream::ReadResponseBody(
189 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
190 CHECK(buf);
191 CHECK(buf_len);
192 CHECK(!callback.is_null());
194 // If we have data buffered, complete the IO immediately.
195 if (!response_body_.empty()) {
196 int bytes_read = 0;
197 while (!response_body_.empty() && buf_len > 0) {
198 scoped_refptr<IOBufferWithSize> data = response_body_.front();
199 const int bytes_to_copy = std::min(buf_len, data->size());
200 memcpy(&(buf->data()[bytes_read]), data->data(), bytes_to_copy);
201 buf_len -= bytes_to_copy;
202 if (bytes_to_copy == data->size()) {
203 response_body_.pop_front();
204 } else {
205 const int bytes_remaining = data->size() - bytes_to_copy;
206 IOBufferWithSize* new_buffer = new IOBufferWithSize(bytes_remaining);
207 memcpy(new_buffer->data(), &(data->data()[bytes_to_copy]),
208 bytes_remaining);
209 response_body_.pop_front();
210 response_body_.push_front(make_scoped_refptr(new_buffer));
212 bytes_read += bytes_to_copy;
214 return bytes_read;
217 if (!stream_) {
218 // If the stream is already closed, there is no body to read.
219 return response_status_;
222 CHECK(callback_.is_null());
223 CHECK(!user_buffer_.get());
224 CHECK_EQ(0, user_buffer_len_);
226 callback_ = callback;
227 user_buffer_ = buf;
228 user_buffer_len_ = buf_len;
229 return ERR_IO_PENDING;
232 void QuicHttpStream::Close(bool not_reusable) {
233 // Note: the not_reusable flag has no meaning for SPDY streams.
234 if (stream_) {
235 closed_stream_received_bytes_ = stream_->stream_bytes_read();
236 stream_->SetDelegate(nullptr);
237 stream_->Reset(QUIC_STREAM_CANCELLED);
238 stream_ = nullptr;
239 response_status_ = was_handshake_confirmed_ ?
240 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
244 HttpStream* QuicHttpStream::RenewStreamForAuth() {
245 return nullptr;
248 bool QuicHttpStream::IsResponseBodyComplete() const {
249 return next_state_ == STATE_OPEN && !stream_;
252 bool QuicHttpStream::CanFindEndOfResponse() const {
253 return true;
256 bool QuicHttpStream::IsConnectionReused() const {
257 // TODO(rch): do something smarter here.
258 return stream_ && stream_->id() > 1;
261 void QuicHttpStream::SetConnectionReused() {
262 // QUIC doesn't need an indicator here.
265 bool QuicHttpStream::IsConnectionReusable() const {
266 // QUIC streams aren't considered reusable.
267 return false;
270 int64 QuicHttpStream::GetTotalReceivedBytes() const {
271 if (stream_) {
272 return stream_->stream_bytes_read();
275 return closed_stream_received_bytes_;
278 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
279 // TODO(mmenke): Figure out what to do here.
280 return true;
283 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
284 DCHECK(stream_);
285 session_->GetSSLInfo(ssl_info);
288 void QuicHttpStream::GetSSLCertRequestInfo(
289 SSLCertRequestInfo* cert_request_info) {
290 DCHECK(stream_);
291 NOTIMPLEMENTED();
294 bool QuicHttpStream::IsSpdyHttpStream() const {
295 return false;
298 void QuicHttpStream::Drain(HttpNetworkSession* session) {
299 Close(false);
300 delete this;
303 void QuicHttpStream::SetPriority(RequestPriority priority) {
304 priority_ = priority;
307 void QuicHttpStream::OnHeadersAvailable(StringPiece headers) {
308 int rv = ParseResponseHeaders(headers);
309 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
310 DoCallback(rv);
314 int QuicHttpStream::OnDataReceived(const char* data, int length) {
315 DCHECK_NE(0, length);
317 if (callback_.is_null()) {
318 BufferResponseBody(data, length);
319 return OK;
322 if (length <= user_buffer_len_) {
323 memcpy(user_buffer_->data(), data, length);
324 } else {
325 memcpy(user_buffer_->data(), data, user_buffer_len_);
326 int delta = length - user_buffer_len_;
327 BufferResponseBody(data + user_buffer_len_, delta);
328 length = user_buffer_len_;
331 user_buffer_ = nullptr;
332 user_buffer_len_ = 0;
333 DoCallback(length);
334 return OK;
337 void QuicHttpStream::OnClose(QuicErrorCode error) {
338 if (error != QUIC_NO_ERROR) {
339 response_status_ = was_handshake_confirmed_ ?
340 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
341 } else if (!response_headers_received_) {
342 response_status_ = ERR_ABORTED;
345 closed_stream_received_bytes_ = stream_->stream_bytes_read();
346 stream_ = nullptr;
347 if (!callback_.is_null())
348 DoCallback(response_status_);
351 void QuicHttpStream::OnError(int error) {
352 stream_ = nullptr;
353 response_status_ = was_handshake_confirmed_ ?
354 error : ERR_QUIC_HANDSHAKE_FAILED;
355 if (!callback_.is_null())
356 DoCallback(response_status_);
359 bool QuicHttpStream::HasSendHeadersComplete() {
360 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
363 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
364 was_handshake_confirmed_ = true;
367 void QuicHttpStream::OnSessionClosed(int error) {
368 Close(false);
369 session_error_ = error;
370 session_.reset();
373 void QuicHttpStream::OnIOComplete(int rv) {
374 rv = DoLoop(rv);
376 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
377 DoCallback(rv);
381 void QuicHttpStream::DoCallback(int rv) {
382 CHECK_NE(rv, ERR_IO_PENDING);
383 CHECK(!callback_.is_null());
385 // The client callback can do anything, including destroying this class,
386 // so any pending callback must be issued after everything else is done.
387 base::ResetAndReturn(&callback_).Run(rv);
390 int QuicHttpStream::DoLoop(int rv) {
391 do {
392 State state = next_state_;
393 next_state_ = STATE_NONE;
394 switch (state) {
395 case STATE_SEND_HEADERS:
396 CHECK_EQ(OK, rv);
397 rv = DoSendHeaders();
398 break;
399 case STATE_SEND_HEADERS_COMPLETE:
400 rv = DoSendHeadersComplete(rv);
401 break;
402 case STATE_READ_REQUEST_BODY:
403 CHECK_EQ(OK, rv);
404 rv = DoReadRequestBody();
405 break;
406 case STATE_READ_REQUEST_BODY_COMPLETE:
407 rv = DoReadRequestBodyComplete(rv);
408 break;
409 case STATE_SEND_BODY:
410 CHECK_EQ(OK, rv);
411 rv = DoSendBody();
412 break;
413 case STATE_SEND_BODY_COMPLETE:
414 rv = DoSendBodyComplete(rv);
415 break;
416 case STATE_OPEN:
417 CHECK_EQ(OK, rv);
418 break;
419 default:
420 NOTREACHED() << "next_state_: " << next_state_;
421 break;
423 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
424 rv != ERR_IO_PENDING);
426 return rv;
429 int QuicHttpStream::DoSendHeaders() {
430 if (!stream_)
431 return ERR_UNEXPECTED;
433 // Log the actual request with the URL Request's net log.
434 stream_net_log_.AddEvent(
435 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
436 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
437 priority_));
438 // Also log to the QuicSession's net log.
439 stream_->net_log().AddEvent(
440 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
441 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
442 priority_));
444 bool has_upload_data = request_body_stream_ != nullptr;
446 next_state_ = STATE_SEND_HEADERS_COMPLETE;
447 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
448 request_headers_.clear();
449 return rv;
452 int QuicHttpStream::DoSendHeadersComplete(int rv) {
453 if (rv < 0)
454 return rv;
456 next_state_ = request_body_stream_ ?
457 STATE_READ_REQUEST_BODY : STATE_OPEN;
459 return OK;
462 int QuicHttpStream::DoReadRequestBody() {
463 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
464 return request_body_stream_->Read(
465 raw_request_body_buf_.get(),
466 raw_request_body_buf_->size(),
467 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
470 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
471 // |rv| is the result of read from the request body from the last call to
472 // DoSendBody().
473 if (rv < 0)
474 return rv;
476 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
477 if (rv == 0) { // Reached the end.
478 DCHECK(request_body_stream_->IsEOF());
481 next_state_ = STATE_SEND_BODY;
482 return OK;
485 int QuicHttpStream::DoSendBody() {
486 if (!stream_)
487 return ERR_UNEXPECTED;
489 CHECK(request_body_stream_);
490 CHECK(request_body_buf_.get());
491 const bool eof = request_body_stream_->IsEOF();
492 int len = request_body_buf_->BytesRemaining();
493 if (len > 0 || eof) {
494 next_state_ = STATE_SEND_BODY_COMPLETE;
495 base::StringPiece data(request_body_buf_->data(), len);
496 return stream_->WriteStreamData(
497 data, eof,
498 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
501 next_state_ = STATE_OPEN;
502 return OK;
505 int QuicHttpStream::DoSendBodyComplete(int rv) {
506 if (rv < 0)
507 return rv;
509 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
511 if (!request_body_stream_->IsEOF()) {
512 next_state_ = STATE_READ_REQUEST_BODY;
513 return OK;
516 next_state_ = STATE_OPEN;
517 return OK;
520 int QuicHttpStream::ParseResponseHeaders(StringPiece headers_data) {
521 SpdyFramer framer(GetSpdyVersion());
522 SpdyHeaderBlock headers;
523 size_t len = framer.ParseHeaderBlockInBuffer(headers_data.data(),
524 headers_data.length(), &headers);
525 if (len == 0 || len != headers_data.length()) {
526 DLOG(WARNING) << "Invalid headers";
527 return ERR_QUIC_PROTOCOL_ERROR;
530 // The URLRequest logs these headers, so only log to the QuicSession's
531 // net log.
532 stream_->net_log().AddEvent(
533 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
534 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
536 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
537 DLOG(WARNING) << "Invalid headers";
538 return ERR_QUIC_PROTOCOL_ERROR;
540 // Put the peer's IP address and port into the response.
541 IPEndPoint address = session_->peer_address();
542 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
543 response_info_->connection_info =
544 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
545 response_info_->vary_data
546 .Init(*request_info_, *response_info_->headers.get());
547 response_info_->was_npn_negotiated = true;
548 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
549 response_info_->response_time = base::Time::Now();
550 response_info_->request_time = request_time_;
551 response_headers_received_ = true;
553 return OK;
556 void QuicHttpStream::BufferResponseBody(const char* data, int length) {
557 if (length == 0)
558 return;
559 IOBufferWithSize* io_buffer = new IOBufferWithSize(length);
560 memcpy(io_buffer->data(), data, length);
561 response_body_.push_back(make_scoped_refptr(io_buffer));
564 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
565 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
568 } // namespace net