OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_http_stream.h" | 5 #include "net/quic/quic_http_stream.h" |
6 | 6 |
7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 if (session_) | 50 if (session_) |
51 session_->RemoveObserver(this); | 51 session_->RemoveObserver(this); |
52 } | 52 } |
53 | 53 |
54 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info, | 54 int QuicHttpStream::InitializeStream(const HttpRequestInfo* request_info, |
55 RequestPriority priority, | 55 RequestPriority priority, |
56 const BoundNetLog& stream_net_log, | 56 const BoundNetLog& stream_net_log, |
57 const CompletionCallback& callback) { | 57 const CompletionCallback& callback) { |
58 DCHECK(!stream_); | 58 DCHECK(!stream_); |
59 if (!session_) | 59 if (!session_) |
60 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED : | 60 return was_handshake_confirmed_ ? ERR_CONNECTION_CLOSED |
61 ERR_QUIC_HANDSHAKE_FAILED; | 61 : ERR_QUIC_HANDSHAKE_FAILED; |
62 | 62 |
63 if (request_info->url.SchemeIsSecure()) { | 63 if (request_info->url.SchemeIsSecure()) { |
64 SSLInfo ssl_info; | 64 SSLInfo ssl_info; |
65 bool secure_session = session_->GetSSLInfo(&ssl_info) && ssl_info.cert; | 65 bool secure_session = session_->GetSSLInfo(&ssl_info) && ssl_info.cert; |
66 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession", | 66 UMA_HISTOGRAM_BOOLEAN("Net.QuicSession.SecureResourceSecureSession", |
67 secure_session); | 67 secure_session); |
68 if (!secure_session) | 68 if (!secure_session) |
69 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC; | 69 return ERR_REQUEST_FOR_SECURE_RESOURCE_OVER_INSECURE_QUIC; |
70 } | 70 } |
71 | 71 |
72 stream_net_log_ = stream_net_log; | 72 stream_net_log_ = stream_net_log; |
73 request_info_ = request_info; | 73 request_info_ = request_info; |
74 priority_ = priority; | 74 priority_ = priority; |
75 | 75 |
76 int rv = stream_request_.StartRequest( | 76 int rv = stream_request_.StartRequest( |
77 session_, &stream_, base::Bind(&QuicHttpStream::OnStreamReady, | 77 session_, |
78 weak_factory_.GetWeakPtr())); | 78 &stream_, |
| 79 base::Bind(&QuicHttpStream::OnStreamReady, weak_factory_.GetWeakPtr())); |
79 if (rv == ERR_IO_PENDING) { | 80 if (rv == ERR_IO_PENDING) { |
80 callback_ = callback; | 81 callback_ = callback; |
81 } else if (rv == OK) { | 82 } else if (rv == OK) { |
82 stream_->SetDelegate(this); | 83 stream_->SetDelegate(this); |
83 } else if (!was_handshake_confirmed_) { | 84 } else if (!was_handshake_confirmed_) { |
84 rv = ERR_QUIC_HANDSHAKE_FAILED; | 85 rv = ERR_QUIC_HANDSHAKE_FAILED; |
85 } | 86 } |
86 | 87 |
87 return rv; | 88 return rv; |
88 } | 89 } |
(...skipping 14 matching lines...) Expand all Loading... |
103 const CompletionCallback& callback) { | 104 const CompletionCallback& callback) { |
104 CHECK(stream_); | 105 CHECK(stream_); |
105 CHECK(!request_body_stream_); | 106 CHECK(!request_body_stream_); |
106 CHECK(!response_info_); | 107 CHECK(!response_info_); |
107 CHECK(!callback.is_null()); | 108 CHECK(!callback.is_null()); |
108 CHECK(response); | 109 CHECK(response); |
109 | 110 |
110 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_); | 111 QuicPriority priority = ConvertRequestPriorityToQuicPriority(priority_); |
111 stream_->set_priority(priority); | 112 stream_->set_priority(priority); |
112 // Store the serialized request headers. | 113 // Store the serialized request headers. |
113 CreateSpdyHeadersFromHttpRequest(*request_info_, request_headers, | 114 CreateSpdyHeadersFromHttpRequest(*request_info_, |
114 &request_headers_, SPDY3, /*direct=*/true); | 115 request_headers, |
| 116 &request_headers_, |
| 117 SPDY3, |
| 118 /*direct=*/true); |
115 | 119 |
116 // Store the request body. | 120 // Store the request body. |
117 request_body_stream_ = request_info_->upload_data_stream; | 121 request_body_stream_ = request_info_->upload_data_stream; |
118 if (request_body_stream_) { | 122 if (request_body_stream_) { |
119 // TODO(rch): Can we be more precise about when to allocate | 123 // TODO(rch): Can we be more precise about when to allocate |
120 // raw_request_body_buf_. Removed the following check. DoReadRequestBody() | 124 // raw_request_body_buf_. Removed the following check. DoReadRequestBody() |
121 // was being called even if we didn't yet allocate raw_request_body_buf_. | 125 // was being called even if we didn't yet allocate raw_request_body_buf_. |
122 // && (request_body_stream_->size() || | 126 // && (request_body_stream_->size() || |
123 // request_body_stream_->is_chunked())) | 127 // request_body_stream_->is_chunked())) |
124 // | 128 // |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Still waiting for the response, return IO_PENDING. | 167 // Still waiting for the response, return IO_PENDING. |
164 CHECK(callback_.is_null()); | 168 CHECK(callback_.is_null()); |
165 callback_ = callback; | 169 callback_ = callback; |
166 return ERR_IO_PENDING; | 170 return ERR_IO_PENDING; |
167 } | 171 } |
168 | 172 |
169 const HttpResponseInfo* QuicHttpStream::GetResponseInfo() const { | 173 const HttpResponseInfo* QuicHttpStream::GetResponseInfo() const { |
170 return response_info_; | 174 return response_info_; |
171 } | 175 } |
172 | 176 |
173 int QuicHttpStream::ReadResponseBody( | 177 int QuicHttpStream::ReadResponseBody(IOBuffer* buf, |
174 IOBuffer* buf, int buf_len, const CompletionCallback& callback) { | 178 int buf_len, |
| 179 const CompletionCallback& callback) { |
175 CHECK(buf); | 180 CHECK(buf); |
176 CHECK(buf_len); | 181 CHECK(buf_len); |
177 CHECK(!callback.is_null()); | 182 CHECK(!callback.is_null()); |
178 | 183 |
179 // If we have data buffered, complete the IO immediately. | 184 // If we have data buffered, complete the IO immediately. |
180 if (!response_body_.empty()) { | 185 if (!response_body_.empty()) { |
181 int bytes_read = 0; | 186 int bytes_read = 0; |
182 while (!response_body_.empty() && buf_len > 0) { | 187 while (!response_body_.empty() && buf_len > 0) { |
183 scoped_refptr<IOBufferWithSize> data = response_body_.front(); | 188 scoped_refptr<IOBufferWithSize> data = response_body_.front(); |
184 const int bytes_to_copy = std::min(buf_len, data->size()); | 189 const int bytes_to_copy = std::min(buf_len, data->size()); |
185 memcpy(&(buf->data()[bytes_read]), data->data(), bytes_to_copy); | 190 memcpy(&(buf->data()[bytes_read]), data->data(), bytes_to_copy); |
186 buf_len -= bytes_to_copy; | 191 buf_len -= bytes_to_copy; |
187 if (bytes_to_copy == data->size()) { | 192 if (bytes_to_copy == data->size()) { |
188 response_body_.pop_front(); | 193 response_body_.pop_front(); |
189 } else { | 194 } else { |
190 const int bytes_remaining = data->size() - bytes_to_copy; | 195 const int bytes_remaining = data->size() - bytes_to_copy; |
191 IOBufferWithSize* new_buffer = new IOBufferWithSize(bytes_remaining); | 196 IOBufferWithSize* new_buffer = new IOBufferWithSize(bytes_remaining); |
192 memcpy(new_buffer->data(), &(data->data()[bytes_to_copy]), | 197 memcpy(new_buffer->data(), |
| 198 &(data->data()[bytes_to_copy]), |
193 bytes_remaining); | 199 bytes_remaining); |
194 response_body_.pop_front(); | 200 response_body_.pop_front(); |
195 response_body_.push_front(make_scoped_refptr(new_buffer)); | 201 response_body_.push_front(make_scoped_refptr(new_buffer)); |
196 } | 202 } |
197 bytes_read += bytes_to_copy; | 203 bytes_read += bytes_to_copy; |
198 } | 204 } |
199 return bytes_read; | 205 return bytes_read; |
200 } | 206 } |
201 | 207 |
202 if (!stream_) { | 208 if (!stream_) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 328 } |
323 | 329 |
324 user_buffer_ = NULL; | 330 user_buffer_ = NULL; |
325 user_buffer_len_ = 0; | 331 user_buffer_len_ = 0; |
326 DoCallback(length); | 332 DoCallback(length); |
327 return OK; | 333 return OK; |
328 } | 334 } |
329 | 335 |
330 void QuicHttpStream::OnClose(QuicErrorCode error) { | 336 void QuicHttpStream::OnClose(QuicErrorCode error) { |
331 if (error != QUIC_NO_ERROR) { | 337 if (error != QUIC_NO_ERROR) { |
332 response_status_ = was_handshake_confirmed_ ? | 338 response_status_ = was_handshake_confirmed_ ? ERR_QUIC_PROTOCOL_ERROR |
333 ERR_QUIC_PROTOCOL_ERROR : ERR_QUIC_HANDSHAKE_FAILED; | 339 : ERR_QUIC_HANDSHAKE_FAILED; |
334 } else if (!response_headers_received_) { | 340 } else if (!response_headers_received_) { |
335 response_status_ = ERR_ABORTED; | 341 response_status_ = ERR_ABORTED; |
336 } | 342 } |
337 | 343 |
338 closed_stream_received_bytes_ = stream_->stream_bytes_read(); | 344 closed_stream_received_bytes_ = stream_->stream_bytes_read(); |
339 stream_ = NULL; | 345 stream_ = NULL; |
340 if (!callback_.is_null()) | 346 if (!callback_.is_null()) |
341 DoCallback(response_status_); | 347 DoCallback(response_status_); |
342 } | 348 } |
343 | 349 |
344 void QuicHttpStream::OnError(int error) { | 350 void QuicHttpStream::OnError(int error) { |
345 stream_ = NULL; | 351 stream_ = NULL; |
346 response_status_ = was_handshake_confirmed_ ? | 352 response_status_ = |
347 error : ERR_QUIC_HANDSHAKE_FAILED; | 353 was_handshake_confirmed_ ? error : ERR_QUIC_HANDSHAKE_FAILED; |
348 if (!callback_.is_null()) | 354 if (!callback_.is_null()) |
349 DoCallback(response_status_); | 355 DoCallback(response_status_); |
350 } | 356 } |
351 | 357 |
352 bool QuicHttpStream::HasSendHeadersComplete() { | 358 bool QuicHttpStream::HasSendHeadersComplete() { |
353 return next_state_ > STATE_SEND_HEADERS_COMPLETE; | 359 return next_state_ > STATE_SEND_HEADERS_COMPLETE; |
354 } | 360 } |
355 | 361 |
356 void QuicHttpStream::OnCryptoHandshakeConfirmed() { | 362 void QuicHttpStream::OnCryptoHandshakeConfirmed() { |
357 was_handshake_confirmed_ = true; | 363 was_handshake_confirmed_ = true; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 return rv; | 424 return rv; |
419 } | 425 } |
420 | 426 |
421 int QuicHttpStream::DoSendHeaders() { | 427 int QuicHttpStream::DoSendHeaders() { |
422 if (!stream_) | 428 if (!stream_) |
423 return ERR_UNEXPECTED; | 429 return ERR_UNEXPECTED; |
424 | 430 |
425 // Log the actual request with the URL Request's net log. | 431 // Log the actual request with the URL Request's net log. |
426 stream_net_log_.AddEvent( | 432 stream_net_log_.AddEvent( |
427 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS, | 433 NetLog::TYPE_HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS, |
428 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_, | 434 base::Bind(&QuicRequestNetLogCallback, |
| 435 stream_->id(), |
| 436 &request_headers_, |
429 priority_)); | 437 priority_)); |
430 // Also log to the QuicSession's net log. | 438 // Also log to the QuicSession's net log. |
431 stream_->net_log().AddEvent( | 439 stream_->net_log().AddEvent( |
432 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS, | 440 NetLog::TYPE_QUIC_HTTP_STREAM_SEND_REQUEST_HEADERS, |
433 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_, | 441 base::Bind(&QuicRequestNetLogCallback, |
| 442 stream_->id(), |
| 443 &request_headers_, |
434 priority_)); | 444 priority_)); |
435 | 445 |
436 bool has_upload_data = request_body_stream_ != NULL; | 446 bool has_upload_data = request_body_stream_ != NULL; |
437 | 447 |
438 next_state_ = STATE_SEND_HEADERS_COMPLETE; | 448 next_state_ = STATE_SEND_HEADERS_COMPLETE; |
439 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, NULL); | 449 int rv = stream_->WriteHeaders(request_headers_, !has_upload_data, NULL); |
440 request_headers_.clear(); | 450 request_headers_.clear(); |
441 return rv; | 451 return rv; |
442 } | 452 } |
443 | 453 |
444 int QuicHttpStream::DoSendHeadersComplete(int rv) { | 454 int QuicHttpStream::DoSendHeadersComplete(int rv) { |
445 if (rv < 0) | 455 if (rv < 0) |
446 return rv; | 456 return rv; |
447 | 457 |
448 next_state_ = request_body_stream_ ? | 458 next_state_ = request_body_stream_ ? STATE_READ_REQUEST_BODY : STATE_OPEN; |
449 STATE_READ_REQUEST_BODY : STATE_OPEN; | |
450 | 459 |
451 return OK; | 460 return OK; |
452 } | 461 } |
453 | 462 |
454 int QuicHttpStream::DoReadRequestBody() { | 463 int QuicHttpStream::DoReadRequestBody() { |
455 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE; | 464 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE; |
456 return request_body_stream_->Read( | 465 return request_body_stream_->Read( |
457 raw_request_body_buf_.get(), | 466 raw_request_body_buf_.get(), |
458 raw_request_body_buf_->size(), | 467 raw_request_body_buf_->size(), |
459 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); | 468 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); |
(...skipping 19 matching lines...) Expand all Loading... |
479 return ERR_UNEXPECTED; | 488 return ERR_UNEXPECTED; |
480 | 489 |
481 CHECK(request_body_stream_); | 490 CHECK(request_body_stream_); |
482 CHECK(request_body_buf_.get()); | 491 CHECK(request_body_buf_.get()); |
483 const bool eof = request_body_stream_->IsEOF(); | 492 const bool eof = request_body_stream_->IsEOF(); |
484 int len = request_body_buf_->BytesRemaining(); | 493 int len = request_body_buf_->BytesRemaining(); |
485 if (len > 0 || eof) { | 494 if (len > 0 || eof) { |
486 next_state_ = STATE_SEND_BODY_COMPLETE; | 495 next_state_ = STATE_SEND_BODY_COMPLETE; |
487 base::StringPiece data(request_body_buf_->data(), len); | 496 base::StringPiece data(request_body_buf_->data(), len); |
488 return stream_->WriteStreamData( | 497 return stream_->WriteStreamData( |
489 data, eof, | 498 data, |
| 499 eof, |
490 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); | 500 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); |
491 } | 501 } |
492 | 502 |
493 next_state_ = STATE_OPEN; | 503 next_state_ = STATE_OPEN; |
494 return OK; | 504 return OK; |
495 } | 505 } |
496 | 506 |
497 int QuicHttpStream::DoSendBodyComplete(int rv) { | 507 int QuicHttpStream::DoSendBodyComplete(int rv) { |
498 if (rv < 0) | 508 if (rv < 0) |
499 return rv; | 509 return rv; |
500 | 510 |
501 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining()); | 511 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining()); |
502 | 512 |
503 if (!request_body_stream_->IsEOF()) { | 513 if (!request_body_stream_->IsEOF()) { |
504 next_state_ = STATE_READ_REQUEST_BODY; | 514 next_state_ = STATE_READ_REQUEST_BODY; |
505 return OK; | 515 return OK; |
506 } | 516 } |
507 | 517 |
508 next_state_ = STATE_OPEN; | 518 next_state_ = STATE_OPEN; |
509 return OK; | 519 return OK; |
510 } | 520 } |
511 | 521 |
512 int QuicHttpStream::ParseResponseHeaders() { | 522 int QuicHttpStream::ParseResponseHeaders() { |
513 size_t read_buf_len = static_cast<size_t>(read_buf_->offset()); | 523 size_t read_buf_len = static_cast<size_t>(read_buf_->offset()); |
514 SpdyFramer framer(SPDY3); | 524 SpdyFramer framer(SPDY3); |
515 SpdyHeaderBlock headers; | 525 SpdyHeaderBlock headers; |
516 char* data = read_buf_->StartOfBuffer(); | 526 char* data = read_buf_->StartOfBuffer(); |
517 size_t len = framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(), | 527 size_t len = |
518 &headers); | 528 framer.ParseHeaderBlockInBuffer(data, read_buf_->offset(), &headers); |
519 | 529 |
520 if (len == 0) { | 530 if (len == 0) { |
521 return ERR_IO_PENDING; | 531 return ERR_IO_PENDING; |
522 } | 532 } |
523 | 533 |
524 // Save the remaining received data. | 534 // Save the remaining received data. |
525 size_t delta = read_buf_len - len; | 535 size_t delta = read_buf_len - len; |
526 if (delta > 0) { | 536 if (delta > 0) { |
527 BufferResponseBody(data + len, delta); | 537 BufferResponseBody(data + len, delta); |
528 } | 538 } |
529 | 539 |
530 // The URLRequest logs these headers, so only log to the QuicSession's | 540 // The URLRequest logs these headers, so only log to the QuicSession's |
531 // net log. | 541 // net log. |
532 stream_->net_log().AddEvent( | 542 stream_->net_log().AddEvent( |
533 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS, | 543 NetLog::TYPE_QUIC_HTTP_STREAM_READ_RESPONSE_HEADERS, |
534 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers)); | 544 base::Bind(&SpdyHeaderBlockNetLogCallback, &headers)); |
535 | 545 |
536 if (!SpdyHeadersToHttpResponse(headers, SPDY3, response_info_)) { | 546 if (!SpdyHeadersToHttpResponse(headers, SPDY3, response_info_)) { |
537 DLOG(WARNING) << "Invalid headers"; | 547 DLOG(WARNING) << "Invalid headers"; |
538 return ERR_QUIC_PROTOCOL_ERROR; | 548 return ERR_QUIC_PROTOCOL_ERROR; |
539 } | 549 } |
540 // Put the peer's IP address and port into the response. | 550 // Put the peer's IP address and port into the response. |
541 IPEndPoint address = stream_->GetPeerAddress(); | 551 IPEndPoint address = stream_->GetPeerAddress(); |
542 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); | 552 response_info_->socket_address = HostPortPair::FromIPEndPoint(address); |
543 response_info_->connection_info = | 553 response_info_->connection_info = |
544 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3; | 554 HttpResponseInfo::CONNECTION_INFO_QUIC1_SPDY3; |
545 response_info_->vary_data | 555 response_info_->vary_data.Init(*request_info_, |
546 .Init(*request_info_, *response_info_->headers.get()); | 556 *response_info_->headers.get()); |
547 response_info_->was_npn_negotiated = true; | 557 response_info_->was_npn_negotiated = true; |
548 response_info_->npn_negotiated_protocol = "quic/1+spdy/3"; | 558 response_info_->npn_negotiated_protocol = "quic/1+spdy/3"; |
549 response_headers_received_ = true; | 559 response_headers_received_ = true; |
550 | 560 |
551 return OK; | 561 return OK; |
552 } | 562 } |
553 | 563 |
554 void QuicHttpStream::BufferResponseBody(const char* data, int length) { | 564 void QuicHttpStream::BufferResponseBody(const char* data, int length) { |
555 if (length == 0) | 565 if (length == 0) |
556 return; | 566 return; |
557 IOBufferWithSize* io_buffer = new IOBufferWithSize(length); | 567 IOBufferWithSize* io_buffer = new IOBufferWithSize(length); |
558 memcpy(io_buffer->data(), data, length); | 568 memcpy(io_buffer->data(), data, length); |
559 response_body_.push_back(make_scoped_refptr(io_buffer)); | 569 response_body_.push_back(make_scoped_refptr(io_buffer)); |
560 } | 570 } |
561 | 571 |
562 } // namespace net | 572 } // namespace net |
OLD | NEW |