Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
blobcce1512c788cef89141727a5b12dfe154f69ebae
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.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_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 static const size_t kHeaderBufInitialSize = 4096;
29 QuicHttpStream::QuicHttpStream(const base::WeakPtr<QuicClientSession>& session)
30 : next_state_(STATE_NONE),
31 session_(session),
32 session_error_(OK),
33 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()),
34 stream_(nullptr),
35 request_info_(nullptr),
36 request_body_stream_(nullptr),
37 priority_(MINIMUM_PRIORITY),
38 response_info_(nullptr),
39 response_status_(OK),
40 response_headers_received_(false),
41 read_buf_(new GrowableIOBuffer()),
42 closed_stream_received_bytes_(0),
43 user_buffer_len_(0),
44 weak_factory_(this) {
45 DCHECK(session_);
46 session_->AddObserver(this);
49 QuicHttpStream::~QuicHttpStream() {
50 Close(false);
51 if (session_)
52 session_->RemoveObserver(this);
55 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
56 RequestPriority priority,
57 const BoundNetLog& stream_net_log,
58 const CompletionCallback& callback) {
59 DCHECK(!stream_);
60 if (!session_)
61 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
62 ERR_QUIC_HANDSHAKE_FAILED;
64 stream_net_log.AddEvent(
65 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
66 session_->net_log().source().ToEventParametersCallback());
68 if (request_info->url.SchemeIsCryptographic()) {
69 SSLInfo ssl_info;
70 bool secure_session =
71 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
72 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
73 secure_session);
74 if (!secure_session)
75 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
78 stream_net_log_ = stream_net_log;
79 request_info_ = request_info;
80 request_time_ = base::Time::Now();
81 priority_ = priority;
83 int rv = stream_request_.StartRequest(
84 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
85 weak_factory_.GetWeakPtr()));
86 if (rv == ERR_IO_PENDING) {
87 callback_ = callback;
88 } else if (rv == OK) {
89 stream_->SetDelegate(this);
90 } else if (!was_handshake_confirmed_) {
91 rv = ERR_QUIC_HANDSHAKE_FAILED;
94 return rv;
97 void QuicHttpStream::OnStreamReady(int rv) {
98 DCHECK(rv == OK || !stream_);
99 if (rv == OK) {
100 stream_->SetDelegate(this);
101 } else if (!was_handshake_confirmed_) {
102 rv = ERR_QUIC_HANDSHAKE_FAILED;
105 ResetAndReturn(&callback_).Run(rv);
108 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
109 HttpResponseInfo* response,
110 const CompletionCallback& callback) {
111 CHECK(!request_body_stream_);
112 CHECK(!response_info_);
113 CHECK(!callback.is_null());
114 CHECK(response);
116 // TODO(rch): remove this once we figure out why channel ID is not being
117 // sent when it should be.
118 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
119 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
120 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
121 SSLInfo ssl_info;
122 bool secure_session =
123 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
124 DCHECK(secure_session);
125 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
126 ssl_info.channel_id_sent);
128 if (!stream_) {
129 return ERR_CONNECTION_CLOSED;
132 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
133 stream_->set_priority(priority);
134 // Store the serialized request headers.
135 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
136 GetSpdyVersion(),
137 /*direct=*/true, &request_headers_);
139 // Store the request body.
140 request_body_stream_ = request_info_->upload_data_stream;
141 if (request_body_stream_) {
142 // TODO(rch): Can we be more precise about when to allocate
143 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
144 // was being called even if we didn't yet allocate raw_request_body_buf_.
145 // && (request_body_stream_->size() ||
146 // request_body_stream_->is_chunked()))
147 // Use 10 packets as the body buffer size to give enough space to
148 // help ensure we don't often send out partial packets.
149 raw_request_body_buf_ =
150 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
151 // The request body buffer is empty at first.
152 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
155 // Store the response info.
156 response_info_ = response;
158 next_state_ = STATE_SEND_HEADERS;
159 int rv = DoLoop(OK);
160 if (rv == ERR_IO_PENDING)
161 callback_ = callback;
163 return rv > 0 ? OK : rv;
166 UploadProgress QuicHttpStream::GetUploadProgress() const {
167 if (!request_body_stream_)
168 return UploadProgress();
170 return UploadProgress(request_body_stream_->position(),
171 request_body_stream_->size());
174 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
175 CHECK(!callback.is_null());
177 if (stream_ == nullptr)
178 return response_status_;
180 // Check if we already have the response headers. If so, return synchronously.
181 if (response_headers_received_)
182 return OK;
184 // Still waiting for the response, return IO_PENDING.
185 CHECK(callback_.is_null());
186 callback_ = callback;
187 return ERR_IO_PENDING;
190 int QuicHttpStream::ReadResponseBody(
191 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
192 CHECK(buf);
193 CHECK(buf_len);
194 CHECK(!callback.is_null());
196 // If we have data buffered, complete the IO immediately.
197 if (!response_body_.empty()) {
198 int bytes_read = 0;
199 while (!response_body_.empty() && buf_len > 0) {
200 scoped_refptr<IOBufferWithSize> data = response_body_.front();
201 const int bytes_to_copy = std::min(buf_len, data->size());
202 memcpy(&(buf->data()[bytes_read]), data->data(), bytes_to_copy);
203 buf_len -= bytes_to_copy;
204 if (bytes_to_copy == data->size()) {
205 response_body_.pop_front();
206 } else {
207 const int bytes_remaining = data->size() - bytes_to_copy;
208 IOBufferWithSize* new_buffer = new IOBufferWithSize(bytes_remaining);
209 memcpy(new_buffer->data(), &(data->data()[bytes_to_copy]),
210 bytes_remaining);
211 response_body_.pop_front();
212 response_body_.push_front(make_scoped_refptr(new_buffer));
214 bytes_read += bytes_to_copy;
216 return bytes_read;
219 if (!stream_) {
220 // If the stream is already closed, there is no body to read.
221 return response_status_;
224 CHECK(callback_.is_null());
225 CHECK(!user_buffer_.get());
226 CHECK_EQ(0, user_buffer_len_);
228 callback_ = callback;
229 user_buffer_ = buf;
230 user_buffer_len_ = buf_len;
231 return ERR_IO_PENDING;
234 void QuicHttpStream::Close(bool not_reusable) {
235 // Note: the not_reusable flag has no meaning for SPDY streams.
236 if (stream_) {
237 closed_stream_received_bytes_ = stream_->stream_bytes_read();
238 stream_->SetDelegate(nullptr);
239 stream_->Reset(QUIC_STREAM_CANCELLED);
240 stream_ = nullptr;
241 response_status_ = was_handshake_confirmed_ ?
242 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
246 HttpStream* QuicHttpStream::RenewStreamForAuth() {
247 return nullptr;
250 bool QuicHttpStream::IsResponseBodyComplete() const {
251 return next_state_ == STATE_OPEN && !stream_;
254 bool QuicHttpStream::CanFindEndOfResponse() const {
255 return true;
258 bool QuicHttpStream::IsConnectionReused() const {
259 // TODO(rch): do something smarter here.
260 return stream_ && stream_->id() > 1;
263 void QuicHttpStream::SetConnectionReused() {
264 // QUIC doesn't need an indicator here.
267 bool QuicHttpStream::IsConnectionReusable() const {
268 // QUIC streams aren't considered reusable.
269 return false;
272 int64 QuicHttpStream::GetTotalReceivedBytes() const {
273 if (stream_) {
274 return stream_->stream_bytes_read();
277 return closed_stream_received_bytes_;
280 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
281 // TODO(mmenke): Figure out what to do here.
282 return true;
285 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
286 DCHECK(stream_);
287 session_->GetSSLInfo(ssl_info);
290 void QuicHttpStream::GetSSLCertRequestInfo(
291 SSLCertRequestInfo* cert_request_info) {
292 DCHECK(stream_);
293 NOTIMPLEMENTED();
296 bool QuicHttpStream::IsSpdyHttpStream() const {
297 return false;
300 void QuicHttpStream::Drain(HttpNetworkSession* session) {
301 Close(false);
302 delete this;
305 void QuicHttpStream::SetPriority(RequestPriority priority) {
306 priority_ = priority;
309 int QuicHttpStream::OnDataReceived(const char* data, int length) {
310 DCHECK_NE(0, length);
311 // Are we still reading the response headers.
312 if (!response_headers_received_) {
313 // Grow the read buffer if necessary.
314 if (read_buf_->RemainingCapacity() < length) {
315 size_t additional_capacity = length - read_buf_->RemainingCapacity();
316 if (additional_capacity < kHeaderBufInitialSize)
317 additional_capacity = kHeaderBufInitialSize;
318 read_buf_->SetCapacity(read_buf_->capacity() + additional_capacity);
320 memcpy(read_buf_->data(), data, length);
321 read_buf_->set_offset(read_buf_->offset() + length);
322 int rv = ParseResponseHeaders();
323 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
324 DoCallback(rv);
326 return OK;
329 if (callback_.is_null()) {
330 BufferResponseBody(data, length);
331 return OK;
334 if (length <= user_buffer_len_) {
335 memcpy(user_buffer_->data(), data, length);
336 } else {
337 memcpy(user_buffer_->data(), data, user_buffer_len_);
338 int delta = length - user_buffer_len_;
339 BufferResponseBody(data + user_buffer_len_, delta);
340 length = user_buffer_len_;
343 user_buffer_ = nullptr;
344 user_buffer_len_ = 0;
345 DoCallback(length);
346 return OK;
349 void QuicHttpStream::OnClose(QuicErrorCode error) {
350 if (error != QUIC_NO_ERROR) {
351 response_status_ = was_handshake_confirmed_ ?
352 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
353 } else if (!response_headers_received_) {
354 response_status_ = ERR_ABORTED;
357 closed_stream_received_bytes_ = stream_->stream_bytes_read();
358 stream_ = nullptr;
359 if (!callback_.is_null())
360 DoCallback(response_status_);
363 void QuicHttpStream::OnError(int error) {
364 stream_ = nullptr;
365 response_status_ = was_handshake_confirmed_ ?
366 error : ERR_QUIC_HANDSHAKE_FAILED;
367 if (!callback_.is_null())
368 DoCallback(response_status_);
371 bool QuicHttpStream::HasSendHeadersComplete() {
372 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
375 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
376 was_handshake_confirmed_ = true;
379 void QuicHttpStream::OnSessionClosed(int error) {
380 Close(false);
381 session_error_ = error;
382 session_.reset();
385 void QuicHttpStream::OnIOComplete(int rv) {
386 rv = DoLoop(rv);
388 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
389 DoCallback(rv);
393 void QuicHttpStream::DoCallback(int rv) {
394 CHECK_NE(rv, ERR_IO_PENDING);
395 CHECK(!callback_.is_null());
397 // The client callback can do anything, including destroying this class,
398 // so any pending callback must be issued after everything else is done.
399 base::ResetAndReturn(&callback_).Run(rv);
402 int QuicHttpStream::DoLoop(int rv) {
403 do {
404 State state = next_state_;
405 next_state_ = STATE_NONE;
406 switch (state) {
407 case STATE_SEND_HEADERS:
408 CHECK_EQ(OK, rv);
409 rv = DoSendHeaders();
410 break;
411 case STATE_SEND_HEADERS_COMPLETE:
412 rv = DoSendHeadersComplete(rv);
413 break;
414 case STATE_READ_REQUEST_BODY:
415 CHECK_EQ(OK, rv);
416 rv = DoReadRequestBody();
417 break;
418 case STATE_READ_REQUEST_BODY_COMPLETE:
419 rv = DoReadRequestBodyComplete(rv);
420 break;
421 case STATE_SEND_BODY:
422 CHECK_EQ(OK, rv);
423 rv = DoSendBody();
424 break;
425 case STATE_SEND_BODY_COMPLETE:
426 rv = DoSendBodyComplete(rv);
427 break;
428 case STATE_OPEN:
429 CHECK_EQ(OK, rv);
430 break;
431 default:
432 NOTREACHED() << "next_state_: " << next_state_;
433 break;
435 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
436 rv != ERR_IO_PENDING);
438 return rv;
441 int QuicHttpStream::DoSendHeaders() {
442 if (!stream_)
443 return ERR_UNEXPECTED;
445 // Log the actual request with the URL Request's net log.
446 stream_net_log_.AddEvent(
447 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
448 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
449 priority_));
450 // Also log to the QuicSession's net log.
451 stream_->net_log().AddEvent(
452 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
453 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
454 priority_));
456 bool has_upload_data = request_body_stream_ != nullptr;
458 next_state_ = STATE_SEND_HEADERS_COMPLETE;
459 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
460 request_headers_.clear();
461 return rv;
464 int QuicHttpStream::DoSendHeadersComplete(int rv) {
465 if (rv < 0)
466 return rv;
468 next_state_ = request_body_stream_ ?
469 STATE_READ_REQUEST_BODY : STATE_OPEN;
471 return OK;
474 int QuicHttpStream::DoReadRequestBody() {
475 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
476 return request_body_stream_->Read(
477 raw_request_body_buf_.get(),
478 raw_request_body_buf_->size(),
479 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
482 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
483 // |rv| is the result of read from the request body from the last call to
484 // DoSendBody().
485 if (rv < 0)
486 return rv;
488 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
489 if (rv == 0) { // Reached the end.
490 DCHECK(request_body_stream_->IsEOF());
493 next_state_ = STATE_SEND_BODY;
494 return OK;
497 int QuicHttpStream::DoSendBody() {
498 if (!stream_)
499 return ERR_UNEXPECTED;
501 CHECK(request_body_stream_);
502 CHECK(request_body_buf_.get());
503 const bool eof = request_body_stream_->IsEOF();
504 int len = request_body_buf_->BytesRemaining();
505 if (len > 0 || eof) {
506 next_state_ = STATE_SEND_BODY_COMPLETE;
507 base::StringPiece data(request_body_buf_->data(), len);
508 return stream_->WriteStreamData(
509 data, eof,
510 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
513 next_state_ = STATE_OPEN;
514 return OK;
517 int QuicHttpStream::DoSendBodyComplete(int rv) {
518 if (rv < 0)
519 return rv;
521 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
523 if (!request_body_stream_->IsEOF()) {
524 next_state_ = STATE_READ_REQUEST_BODY;
525 return OK;
528 next_state_ = STATE_OPEN;
529 return OK;
532 int QuicHttpStream::ParseResponseHeaders() {
533 size_t read_buf_len = static_cast<size_t>(read_buf_->offset());
534 SpdyFramer framer(GetSpdyVersion());
535 SpdyHeaderBlock headers;
536 char* data = read_buf_->StartOfBuffer();
537 size_t len = framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(),
538 &headers);
540 if (len == 0) {
541 return ERR_IO_PENDING;
544 // Save the remaining received data.
545 size_t delta = read_buf_len - len;
546 if (delta > 0) {
547 BufferResponseBody(data + len, delta);
550 // The URLRequest logs these headers, so only log to the QuicSession's
551 // net log.
552 stream_->net_log().AddEvent(
553 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
554 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
556 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
557 DLOG(WARNING) << "Invalid headers";
558 return ERR_QUIC_PROTOCOL_ERROR;
560 // Put the peer's IP address and port into the response.
561 IPEndPoint address = session_->peer_address();
562 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
563 response_info_->connection_info =
564 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
565 response_info_->vary_data
566 .Init(*request_info_, *response_info_->headers.get());
567 response_info_->was_npn_negotiated = true;
568 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
569 response_info_->response_time = base::Time::Now();
570 response_info_->request_time = request_time_;
571 response_headers_received_ = true;
573 return OK;
576 void QuicHttpStream::BufferResponseBody(const char* data, int length) {
577 if (length == 0)
578 return;
579 IOBufferWithSize* io_buffer = new IOBufferWithSize(length);
580 memcpy(io_buffer->data(), data, length);
581 response_body_.push_back(make_scoped_refptr(io_buffer));
584 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
585 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
588 } // namespace net