Cleanup unused param in AddActiveSetupWorkItems
[chromium-blink-merge.git] / net / quic / quic_http_stream.cc
blobb516aba138784abb426a5c5affb54a7f4479433f
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_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(const base::WeakPtr<QuicClientSession>& session)
28 : next_state_(STATE_NONE),
29 session_(session),
30 session_error_(OK),
31 was_handshake_confirmed_(session->IsCryptoHandshakeConfirmed()),
32 stream_(nullptr),
33 request_info_(nullptr),
34 request_body_stream_(nullptr),
35 priority_(MINIMUM_PRIORITY),
36 response_info_(nullptr),
37 response_status_(OK),
38 response_headers_received_(false),
39 closed_stream_received_bytes_(0),
40 user_buffer_len_(0),
41 weak_factory_(this) {
42 DCHECK(session_);
43 session_->AddObserver(this);
46 QuicHttpStream::~QuicHttpStream() {
47 Close(false);
48 if (session_)
49 session_->RemoveObserver(this);
52 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info,
53 RequestPriority priority,
54 const BoundNetLog& stream_net_log,
55 const CompletionCallback& callback) {
56 DCHECK(!stream_);
57 if (!session_)
58 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED :
59 ERR_QUIC_HANDSHAKE_FAILED;
61 stream_net_log.AddEvent(
62 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_QUIC_SESSION,
63 session_->net_log().source().ToEventParametersCallback());
65 if (request_info->url.SchemeIsCryptographic()) {
66 SSLInfo ssl_info;
67 bool secure_session =
68 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
69 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession",
70 secure_session);
71 if (!secure_session)
72 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC;
75 stream_net_log_ = stream_net_log;
76 request_info_ = request_info;
77 request_time_ = base::Time::Now();
78 priority_ = priority;
80 int rv = stream_request_.StartRequest(
81 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady,
82 weak_factory_.GetWeakPtr()));
83 if (rv == ERR_IO_PENDING) {
84 callback_ = callback;
85 } else if (rv == OK) {
86 stream_->SetDelegate(this);
87 } else if (!was_handshake_confirmed_) {
88 rv = ERR_QUIC_HANDSHAKE_FAILED;
91 return rv;
94 void QuicHttpStream::OnStreamReady(int rv) {
95 DCHECK(rv == OK || !stream_);
96 if (rv == OK) {
97 stream_->SetDelegate(this);
98 } else if (!was_handshake_confirmed_) {
99 rv = ERR_QUIC_HANDSHAKE_FAILED;
102 ResetAndReturn(&callback_).Run(rv);
105 int QuicHttpStream::SendRequest(const HttpRequestHeaders& request_headers,
106 HttpResponseInfo* response,
107 const CompletionCallback& callback) {
108 CHECK(!request_body_stream_);
109 CHECK(!response_info_);
110 CHECK(!callback.is_null());
111 CHECK(response);
113 // TODO(rch): remove this once we figure out why channel ID is not being
114 // sent when it should be.
115 HostPortPair origin = HostPortPair::FromURL(request_info_->url);
116 if (origin.Equals(HostPortPair("accounts.google.com", 443)) &&
117 request_headers.HasHeader(HttpRequestHeaders::kCookie)) {
118 SSLInfo ssl_info;
119 bool secure_session =
120 session_->GetSSLInfo(&ssl_info) && ssl_info.cert.get();
121 DCHECK(secure_session);
122 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.CookieSentToAccountsOverChannelId",
123 ssl_info.channel_id_sent);
125 if (!stream_) {
126 return ERR_CONNECTION_CLOSED;
129 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_);
130 stream_->set_priority(priority);
131 // Store the serialized request headers.
132 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers,
133 GetSpdyVersion(),
134 /*direct=*/true, &request_headers_);
136 // Store the request body.
137 request_body_stream_ = request_info_->upload_data_stream;
138 if (request_body_stream_) {
139 // TODO(rch): Can we be more precise about when to allocate
140 // raw_request_body_buf_. Removed the following check. DoReadRequestBody()
141 // was being called even if we didn't yet allocate raw_request_body_buf_.
142 // && (request_body_stream_->size() ||
143 // request_body_stream_->is_chunked()))
144 // Use 10 packets as the body buffer size to give enough space to
145 // help ensure we don't often send out partial packets.
146 raw_request_body_buf_ =
147 new IOBufferWithSize(static_cast<size_t>(10 * kMaxPacketSize));
148 // The request body buffer is empty at first.
149 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), 0);
152 // Store the response info.
153 response_info_ = response;
155 next_state_ = STATE_SEND_HEADERS;
156 int rv = DoLoop(OK);
157 if (rv == ERR_IO_PENDING)
158 callback_ = callback;
160 return rv > 0 ? OK : rv;
163 UploadProgress QuicHttpStream::GetUploadProgress() const {
164 if (!request_body_stream_)
165 return UploadProgress();
167 return UploadProgress(request_body_stream_->position(),
168 request_body_stream_->size());
171 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) {
172 CHECK(!callback.is_null());
174 if (stream_ == nullptr)
175 return response_status_;
177 // Check if we already have the response headers. If so, return synchronously.
178 if (response_headers_received_)
179 return OK;
181 // Still waiting for the response, return IO_PENDING.
182 CHECK(callback_.is_null());
183 callback_ = callback;
184 return ERR_IO_PENDING;
187 int QuicHttpStream::ReadResponseBody(
188 IOBuffer* buf, int buf_len, const CompletionCallback& callback) {
189 CHECK(buf);
190 CHECK(buf_len);
191 CHECK(!callback.is_null());
193 // If we have data buffered, complete the IO immediately.
194 if (!response_body_.empty()) {
195 int bytes_read = 0;
196 while (!response_body_.empty() && buf_len > 0) {
197 scoped_refptr<IOBufferWithSize> data = response_body_.front();
198 const int bytes_to_copy = std::min(buf_len, data->size());
199 memcpy(&(buf->data()[bytes_read]), data->data(), bytes_to_copy);
200 buf_len -= bytes_to_copy;
201 if (bytes_to_copy == data->size()) {
202 response_body_.pop_front();
203 } else {
204 const int bytes_remaining = data->size() - bytes_to_copy;
205 IOBufferWithSize* new_buffer = new IOBufferWithSize(bytes_remaining);
206 memcpy(new_buffer->data(), &(data->data()[bytes_to_copy]),
207 bytes_remaining);
208 response_body_.pop_front();
209 response_body_.push_front(make_scoped_refptr(new_buffer));
211 bytes_read += bytes_to_copy;
213 return bytes_read;
216 if (!stream_) {
217 // If the stream is already closed, there is no body to read.
218 return response_status_;
221 CHECK(callback_.is_null());
222 CHECK(!user_buffer_.get());
223 CHECK_EQ(0, user_buffer_len_);
225 callback_ = callback;
226 user_buffer_ = buf;
227 user_buffer_len_ = buf_len;
228 return ERR_IO_PENDING;
231 void QuicHttpStream::Close(bool not_reusable) {
232 // Note: the not_reusable flag has no meaning for SPDY streams.
233 if (stream_) {
234 closed_stream_received_bytes_ = stream_->stream_bytes_read();
235 stream_->SetDelegate(nullptr);
236 stream_->Reset(QUIC_STREAM_CANCELLED);
237 stream_ = nullptr;
238 response_status_ = was_handshake_confirmed_ ?
239 ERR_CONNECTION_CLOSED : ERR_QUIC_HANDSHAKE_FAILED;
243 HttpStream* QuicHttpStream::RenewStreamForAuth() {
244 return nullptr;
247 bool QuicHttpStream::IsResponseBodyComplete() const {
248 return next_state_ == STATE_OPEN && !stream_;
251 bool QuicHttpStream::CanFindEndOfResponse() const {
252 return true;
255 bool QuicHttpStream::IsConnectionReused() const {
256 // TODO(rch): do something smarter here.
257 return stream_ && stream_->id() > 1;
260 void QuicHttpStream::SetConnectionReused() {
261 // QUIC doesn't need an indicator here.
264 bool QuicHttpStream::IsConnectionReusable() const {
265 // QUIC streams aren't considered reusable.
266 return false;
269 int64 QuicHttpStream::GetTotalReceivedBytes() const {
270 if (stream_) {
271 return stream_->stream_bytes_read();
274 return closed_stream_received_bytes_;
277 bool QuicHttpStream::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
278 // TODO(mmenke): Figure out what to do here.
279 return true;
282 void QuicHttpStream::GetSSLInfo(SSLInfo* ssl_info) {
283 DCHECK(stream_);
284 session_->GetSSLInfo(ssl_info);
287 void QuicHttpStream::GetSSLCertRequestInfo(
288 SSLCertRequestInfo* cert_request_info) {
289 DCHECK(stream_);
290 NOTIMPLEMENTED();
293 bool QuicHttpStream::IsSpdyHttpStream() const {
294 return false;
297 void QuicHttpStream::Drain(HttpNetworkSession* session) {
298 Close(false);
299 delete this;
302 void QuicHttpStream::SetPriority(RequestPriority priority) {
303 priority_ = priority;
306 void QuicHttpStream::OnHeadersAvailable(StringPiece headers) {
307 int rv = ParseResponseHeaders(headers);
308 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
309 DoCallback(rv);
313 int QuicHttpStream::OnDataReceived(const char* data, int length) {
314 DCHECK_NE(0, length);
316 if (callback_.is_null()) {
317 BufferResponseBody(data, length);
318 return OK;
321 if (length <= user_buffer_len_) {
322 memcpy(user_buffer_->data(), data, length);
323 } else {
324 memcpy(user_buffer_->data(), data, user_buffer_len_);
325 int delta = length - user_buffer_len_;
326 BufferResponseBody(data + user_buffer_len_, delta);
327 length = user_buffer_len_;
330 user_buffer_ = nullptr;
331 user_buffer_len_ = 0;
332 DoCallback(length);
333 return OK;
336 void QuicHttpStream::OnClose(QuicErrorCode error) {
337 if (error != QUIC_NO_ERROR) {
338 response_status_ = was_handshake_confirmed_ ?
339 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED;
340 } else if (!response_headers_received_) {
341 response_status_ = ERR_ABORTED;
344 closed_stream_received_bytes_ = stream_->stream_bytes_read();
345 stream_ = nullptr;
346 if (!callback_.is_null())
347 DoCallback(response_status_);
350 void QuicHttpStream::OnError(int error) {
351 stream_ = nullptr;
352 response_status_ = was_handshake_confirmed_ ?
353 error : ERR_QUIC_HANDSHAKE_FAILED;
354 if (!callback_.is_null())
355 DoCallback(response_status_);
358 bool QuicHttpStream::HasSendHeadersComplete() {
359 return next_state_ > STATE_SEND_HEADERS_COMPLETE;
362 void QuicHttpStream::OnCryptoHandshakeConfirmed() {
363 was_handshake_confirmed_ = true;
366 void QuicHttpStream::OnSessionClosed(int error) {
367 Close(false);
368 session_error_ = error;
369 session_.reset();
372 void QuicHttpStream::OnIOComplete(int rv) {
373 rv = DoLoop(rv);
375 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
376 DoCallback(rv);
380 void QuicHttpStream::DoCallback(int rv) {
381 CHECK_NE(rv, ERR_IO_PENDING);
382 CHECK(!callback_.is_null());
384 // The client callback can do anything, including destroying this class,
385 // so any pending callback must be issued after everything else is done.
386 base::ResetAndReturn(&callback_).Run(rv);
389 int QuicHttpStream::DoLoop(int rv) {
390 do {
391 State state = next_state_;
392 next_state_ = STATE_NONE;
393 switch (state) {
394 case STATE_SEND_HEADERS:
395 CHECK_EQ(OK, rv);
396 rv = DoSendHeaders();
397 break;
398 case STATE_SEND_HEADERS_COMPLETE:
399 rv = DoSendHeadersComplete(rv);
400 break;
401 case STATE_READ_REQUEST_BODY:
402 CHECK_EQ(OK, rv);
403 rv = DoReadRequestBody();
404 break;
405 case STATE_READ_REQUEST_BODY_COMPLETE:
406 rv = DoReadRequestBodyComplete(rv);
407 break;
408 case STATE_SEND_BODY:
409 CHECK_EQ(OK, rv);
410 rv = DoSendBody();
411 break;
412 case STATE_SEND_BODY_COMPLETE:
413 rv = DoSendBodyComplete(rv);
414 break;
415 case STATE_OPEN:
416 CHECK_EQ(OK, rv);
417 break;
418 default:
419 NOTREACHED() << "next_state_: " << next_state_;
420 break;
422 } while (next_state_ != STATE_NONE && next_state_ != STATE_OPEN &&
423 rv != ERR_IO_PENDING);
425 return rv;
428 int QuicHttpStream::DoSendHeaders() {
429 if (!stream_)
430 return ERR_UNEXPECTED;
432 // Log the actual request with the URL Request's net log.
433 stream_net_log_.AddEvent(
434 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS,
435 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
436 priority_));
437 // Also log to the QuicSession's net log.
438 stream_->net_log().AddEvent(
439 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS,
440 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_,
441 priority_));
443 bool has_upload_data = request_body_stream_ != nullptr;
445 next_state_ = STATE_SEND_HEADERS_COMPLETE;
446 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, nullptr);
447 request_headers_.clear();
448 return rv;
451 int QuicHttpStream::DoSendHeadersComplete(int rv) {
452 if (rv < 0)
453 return rv;
455 next_state_ = request_body_stream_ ?
456 STATE_READ_REQUEST_BODY : STATE_OPEN;
458 return OK;
461 int QuicHttpStream::DoReadRequestBody() {
462 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE;
463 return request_body_stream_->Read(
464 raw_request_body_buf_.get(),
465 raw_request_body_buf_->size(),
466 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
469 int QuicHttpStream::DoReadRequestBodyComplete(int rv) {
470 // |rv| is the result of read from the request body from the last call to
471 // DoSendBody().
472 if (rv < 0)
473 return rv;
475 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv);
476 if (rv == 0) { // Reached the end.
477 DCHECK(request_body_stream_->IsEOF());
480 next_state_ = STATE_SEND_BODY;
481 return OK;
484 int QuicHttpStream::DoSendBody() {
485 if (!stream_)
486 return ERR_UNEXPECTED;
488 CHECK(request_body_stream_);
489 CHECK(request_body_buf_.get());
490 const bool eof = request_body_stream_->IsEOF();
491 int len = request_body_buf_->BytesRemaining();
492 if (len > 0 || eof) {
493 next_state_ = STATE_SEND_BODY_COMPLETE;
494 base::StringPiece data(request_body_buf_->data(), len);
495 return stream_->WriteStreamData(
496 data, eof,
497 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr()));
500 next_state_ = STATE_OPEN;
501 return OK;
504 int QuicHttpStream::DoSendBodyComplete(int rv) {
505 if (rv < 0)
506 return rv;
508 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining());
510 if (!request_body_stream_->IsEOF()) {
511 next_state_ = STATE_READ_REQUEST_BODY;
512 return OK;
515 next_state_ = STATE_OPEN;
516 return OK;
519 int QuicHttpStream::ParseResponseHeaders(StringPiece headers_data) {
520 SpdyFramer framer(GetSpdyVersion());
521 SpdyHeaderBlock headers;
522 size_t len = framer.ParseHeaderBlockInBuffer(headers_data.data(),
523 headers_data.length(), &headers);
524 if (len == 0 || len != headers_data.length()) {
525 DLOG(WARNING) << "Invalid headers";
526 return ERR_QUIC_PROTOCOL_ERROR;
529 // The URLRequest logs these headers, so only log to the QuicSession's
530 // net log.
531 stream_->net_log().AddEvent(
532 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS,
533 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers));
535 if (!SpdyHeadersToHttpResponse(headers, GetSpdyVersion(), response_info_)) {
536 DLOG(WARNING) << "Invalid headers";
537 return ERR_QUIC_PROTOCOL_ERROR;
539 // Put the peer's IP address and port into the response.
540 IPEndPoint address = session_->peer_address();
541 response_info_->socket_address = HostPortPair::FromIPEndPoint(address);
542 response_info_->connection_info =
543 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3;
544 response_info_->vary_data
545 .Init(*request_info_, *response_info_->headers.get());
546 response_info_->was_npn_negotiated = true;
547 response_info_->npn_negotiated_protocol = "quic/1+spdy/3";
548 response_info_->response_time = base::Time::Now();
549 response_info_->request_time = request_time_;
550 response_headers_received_ = true;
552 return OK;
555 void QuicHttpStream::BufferResponseBody(const char* data, int length) {
556 if (length == 0)
557 return;
558 IOBufferWithSize* io_buffer = new IOBufferWithSize(length);
559 memcpy(io_buffer->data(), data, length);
560 response_body_.push_back(make_scoped_refptr(io_buffer));
563 SpdyMajorVersion QuicHttpStream::GetSpdyVersion() {
564 return SpdyUtils::GetSpdyVersionForQuicVersion(stream_->version());
567 } // namespace net