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/chromium/quic_http_stream.h" | 5 #include "net/quic/chromium/quic_http_stream.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 response_status_(ERR_UNEXPECTED), | 57 response_status_(ERR_UNEXPECTED), |
58 response_headers_received_(false), | 58 response_headers_received_(false), |
59 trailing_headers_received_(false), | 59 trailing_headers_received_(false), |
60 headers_bytes_received_(0), | 60 headers_bytes_received_(0), |
61 headers_bytes_sent_(0), | 61 headers_bytes_sent_(0), |
62 closed_stream_received_bytes_(0), | 62 closed_stream_received_bytes_(0), |
63 closed_stream_sent_bytes_(0), | 63 closed_stream_sent_bytes_(0), |
64 closed_is_first_stream_(false), | 64 closed_is_first_stream_(false), |
65 user_buffer_len_(0), | 65 user_buffer_len_(0), |
66 session_error_(ERR_UNEXPECTED), | 66 session_error_(ERR_UNEXPECTED), |
67 quic_connection_error_(QUIC_NO_ERROR), | |
68 quic_stream_error_(QUIC_STREAM_NO_ERROR), | |
69 found_promise_(false), | 67 found_promise_(false), |
70 push_handle_(nullptr), | 68 push_handle_(nullptr), |
71 in_loop_(false), | 69 in_loop_(false), |
72 weak_factory_(this) {} | 70 weak_factory_(this) {} |
73 | 71 |
74 QuicHttpStream::~QuicHttpStream() { | 72 QuicHttpStream::~QuicHttpStream() { |
75 CHECK(!in_loop_); | 73 CHECK(!in_loop_); |
76 Close(false); | 74 Close(false); |
77 } | 75 } |
78 | 76 |
(...skipping 21 matching lines...) Expand all Loading... |
100 return true; | 98 return true; |
101 } | 99 } |
102 // Now compare the client request for matching. | 100 // Now compare the client request for matching. |
103 return vary_data.MatchesRequest(client_request_info, | 101 return vary_data.MatchesRequest(client_request_info, |
104 *promise_response_info.headers.get()); | 102 *promise_response_info.headers.get()); |
105 } | 103 } |
106 | 104 |
107 void QuicHttpStream::OnRendezvousResult(QuicSpdyStream* stream) { | 105 void QuicHttpStream::OnRendezvousResult(QuicSpdyStream* stream) { |
108 push_handle_ = nullptr; | 106 push_handle_ = nullptr; |
109 if (stream) { | 107 if (stream) { |
110 stream_ = | 108 stream_ = static_cast<QuicChromiumClientStream*>(stream)->CreateHandle(); |
111 static_cast<QuicChromiumClientStream*>(stream)->CreateHandle(this); | |
112 } | 109 } |
113 | 110 |
114 // callback_ should only be non-null in the case of asynchronous | 111 // callback_ should only be non-null in the case of asynchronous |
115 // rendezvous; i.e. |Try()| returned QUIC_PENDING. | 112 // rendezvous; i.e. |Try()| returned QUIC_PENDING. |
116 if (callback_.is_null()) | 113 if (callback_.is_null()) |
117 return; | 114 return; |
118 | 115 |
119 DCHECK_EQ(STATE_HANDLE_PROMISE_COMPLETE, next_state_); | 116 DCHECK_EQ(STATE_HANDLE_PROMISE_COMPLETE, next_state_); |
120 if (!stream) { | 117 if (!stream) { |
121 // rendezvous has failed so proceed as with a non-push request. | 118 // rendezvous has failed so proceed as with a non-push request. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 base::Bind(&NetLogQuicPushStreamCallback, promised->id(), | 183 base::Bind(&NetLogQuicPushStreamCallback, promised->id(), |
187 &request_info_->url)); | 184 &request_info_->url)); |
188 return OK; | 185 return OK; |
189 } | 186 } |
190 | 187 |
191 next_state_ = STATE_REQUEST_STREAM; | 188 next_state_ = STATE_REQUEST_STREAM; |
192 int rv = DoLoop(OK); | 189 int rv = DoLoop(OK); |
193 if (rv == ERR_IO_PENDING) | 190 if (rv == ERR_IO_PENDING) |
194 callback_ = callback; | 191 callback_ = callback; |
195 | 192 |
196 return rv; | 193 return MapStreamError(rv); |
197 } | 194 } |
198 | 195 |
199 int QuicHttpStream::DoHandlePromise() { | 196 int QuicHttpStream::DoHandlePromise() { |
200 QuicAsyncStatus push_status = quic_session()->GetPushPromiseIndex()->Try( | 197 QuicAsyncStatus push_status = quic_session()->GetPushPromiseIndex()->Try( |
201 request_headers_, this, &this->push_handle_); | 198 request_headers_, this, &this->push_handle_); |
202 | 199 |
203 switch (push_status) { | 200 switch (push_status) { |
204 case QUIC_FAILURE: | 201 case QUIC_FAILURE: |
205 // Push rendezvous failed. | 202 // Push rendezvous failed. |
206 next_state_ = STATE_REQUEST_STREAM; | 203 next_state_ = STATE_REQUEST_STREAM; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 next_state_ = STATE_HANDLE_PROMISE; | 295 next_state_ = STATE_HANDLE_PROMISE; |
299 } else { | 296 } else { |
300 found_promise_ = false; | 297 found_promise_ = false; |
301 next_state_ = STATE_REQUEST_STREAM; | 298 next_state_ = STATE_REQUEST_STREAM; |
302 } | 299 } |
303 rv = DoLoop(OK); | 300 rv = DoLoop(OK); |
304 | 301 |
305 if (rv == ERR_IO_PENDING) | 302 if (rv == ERR_IO_PENDING) |
306 callback_ = callback; | 303 callback_ = callback; |
307 | 304 |
308 return rv > 0 ? OK : rv; | 305 return rv > 0 ? OK : MapStreamError(rv); |
309 } | 306 } |
310 | 307 |
311 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) { | 308 int QuicHttpStream::ReadResponseHeaders(const CompletionCallback& callback) { |
312 CHECK(callback_.is_null()); | 309 CHECK(callback_.is_null()); |
313 CHECK(!callback.is_null()); | 310 CHECK(!callback.is_null()); |
314 | 311 |
315 if (stream_ == nullptr) | |
316 return GetResponseStatus(); | |
317 | |
318 int rv = stream_->ReadInitialHeaders( | 312 int rv = stream_->ReadInitialHeaders( |
319 &response_header_block_, | 313 &response_header_block_, |
320 base::Bind(&QuicHttpStream::OnReadResponseHeadersComplete, | 314 base::Bind(&QuicHttpStream::OnReadResponseHeadersComplete, |
321 weak_factory_.GetWeakPtr())); | 315 weak_factory_.GetWeakPtr())); |
322 | 316 |
323 if (rv == ERR_IO_PENDING) { | 317 if (rv == ERR_IO_PENDING) { |
324 // Still waiting for the response, return IO_PENDING. | 318 // Still waiting for the response, return IO_PENDING. |
325 CHECK(callback_.is_null()); | 319 CHECK(callback_.is_null()); |
326 callback_ = callback; | 320 callback_ = callback; |
327 return ERR_IO_PENDING; | 321 return ERR_IO_PENDING; |
328 } | 322 } |
329 | 323 |
330 if (rv < 0) | 324 if (rv < 0) |
331 return rv; | 325 return MapStreamError(rv); |
332 | 326 |
333 // Check if we already have the response headers. If so, return synchronously. | 327 // Check if we already have the response headers. If so, return synchronously. |
334 if (response_headers_received_) | 328 if (response_headers_received_) |
335 return OK; | 329 return OK; |
336 | 330 |
337 headers_bytes_received_ += rv; | 331 headers_bytes_received_ += rv; |
338 return ProcessResponseHeaders(response_header_block_); | 332 return ProcessResponseHeaders(response_header_block_); |
339 } | 333 } |
340 | 334 |
341 int QuicHttpStream::ReadResponseBody(IOBuffer* buf, | 335 int QuicHttpStream::ReadResponseBody(IOBuffer* buf, |
342 int buf_len, | 336 int buf_len, |
343 const CompletionCallback& callback) { | 337 const CompletionCallback& callback) { |
344 CHECK(callback_.is_null()); | 338 CHECK(callback_.is_null()); |
345 CHECK(!callback.is_null()); | 339 CHECK(!callback.is_null()); |
346 CHECK(!user_buffer_.get()); | 340 CHECK(!user_buffer_.get()); |
347 CHECK_EQ(0, user_buffer_len_); | 341 CHECK_EQ(0, user_buffer_len_); |
348 | 342 |
349 // Invalidate HttpRequestInfo pointer. This is to allow the stream to be | 343 // Invalidate HttpRequestInfo pointer. This is to allow the stream to be |
350 // shared across multiple transactions which might require this | 344 // shared across multiple transactions which might require this |
351 // stream to outlive the request_info_'s owner. | 345 // stream to outlive the request_info_'s owner. |
352 // Only allowed when Read state machine starts. It is safe to reset it at | 346 // Only allowed when Read state machine starts. It is safe to reset it at |
353 // this point since request_info_->upload_data_stream is also not needed | 347 // this point since request_info_->upload_data_stream is also not needed |
354 // anymore. | 348 // anymore. |
355 request_info_ = nullptr; | 349 request_info_ = nullptr; |
356 | 350 |
357 // If the stream is already closed, there is no body to read. | 351 // If the stream is already closed, there is no body to read. |
358 if (!stream_) | 352 if (stream_->IsDoneReading()) |
359 return GetResponseStatus(); | 353 return GetResponseStatus(); |
360 | 354 |
361 int rv = stream_->ReadBody(buf, buf_len, | 355 int rv = stream_->ReadBody(buf, buf_len, |
362 base::Bind(&QuicHttpStream::OnReadBodyComplete, | 356 base::Bind(&QuicHttpStream::OnReadBodyComplete, |
363 weak_factory_.GetWeakPtr())); | 357 weak_factory_.GetWeakPtr())); |
364 if (rv == ERR_IO_PENDING) { | 358 if (rv == ERR_IO_PENDING) { |
365 callback_ = callback; | 359 callback_ = callback; |
366 user_buffer_ = buf; | 360 user_buffer_ = buf; |
367 user_buffer_len_ = buf_len; | 361 user_buffer_len_ = buf_len; |
368 return ERR_IO_PENDING; | 362 return ERR_IO_PENDING; |
369 } | 363 } |
370 | 364 |
371 if (rv < 0) | 365 if (rv < 0) |
372 return rv; | 366 return MapStreamError(rv); |
373 | 367 |
374 return HandleReadComplete(rv); | 368 return HandleReadComplete(rv); |
375 } | 369 } |
376 | 370 |
377 void QuicHttpStream::Close(bool /*not_reusable*/) { | 371 void QuicHttpStream::Close(bool /*not_reusable*/) { |
378 session_error_ = ERR_ABORTED; | 372 session_error_ = ERR_ABORTED; |
379 SaveResponseStatus(); | 373 SaveResponseStatus(); |
380 // Note: the not_reusable flag has no meaning for QUIC streams. | 374 // Note: the not_reusable flag has no meaning for QUIC streams. |
381 if (stream_) { | 375 if (stream_) |
382 stream_->ClearDelegate(); | |
383 stream_->Reset(QUIC_STREAM_CANCELLED); | 376 stream_->Reset(QUIC_STREAM_CANCELLED); |
384 } | |
385 ResetStream(); | 377 ResetStream(); |
386 } | 378 } |
387 | 379 |
388 bool QuicHttpStream::IsResponseBodyComplete() const { | 380 bool QuicHttpStream::IsResponseBodyComplete() const { |
389 return next_state_ == STATE_OPEN && !stream_; | 381 return next_state_ == STATE_OPEN && stream_->IsDoneReading(); |
390 } | 382 } |
391 | 383 |
392 bool QuicHttpStream::IsConnectionReused() const { | 384 bool QuicHttpStream::IsConnectionReused() const { |
393 // TODO(rch): do something smarter here. | 385 // TODO(rch): do something smarter here. |
394 return stream_ && stream_->id() > 1; | 386 return stream_ && stream_->id() > 1; |
395 } | 387 } |
396 | 388 |
397 int64_t QuicHttpStream::GetTotalReceivedBytes() const { | 389 int64_t QuicHttpStream::GetTotalReceivedBytes() const { |
398 // TODO(sclittle): Currently, this only includes headers and response body | 390 // TODO(sclittle): Currently, this only includes headers and response body |
399 // bytes. Change this to include QUIC overhead as well. | 391 // bytes. Change this to include QUIC overhead as well. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 alternative_service->protocol = kProtoQUIC; | 430 alternative_service->protocol = kProtoQUIC; |
439 alternative_service->host = quic_session()->server_id().host(); | 431 alternative_service->host = quic_session()->server_id().host(); |
440 alternative_service->port = quic_session()->server_id().port(); | 432 alternative_service->port = quic_session()->server_id().port(); |
441 return true; | 433 return true; |
442 } | 434 } |
443 | 435 |
444 void QuicHttpStream::PopulateNetErrorDetails(NetErrorDetails* details) { | 436 void QuicHttpStream::PopulateNetErrorDetails(NetErrorDetails* details) { |
445 details->connection_info = | 437 details->connection_info = |
446 ConnectionInfoFromQuicVersion(quic_session()->GetQuicVersion()); | 438 ConnectionInfoFromQuicVersion(quic_session()->GetQuicVersion()); |
447 quic_session()->PopulateNetErrorDetails(details); | 439 quic_session()->PopulateNetErrorDetails(details); |
448 if (quic_session()->IsCryptoHandshakeConfirmed()) | 440 if (quic_session()->IsCryptoHandshakeConfirmed() && stream_) |
449 details->quic_connection_error = quic_connection_error_; | 441 details->quic_connection_error = stream_->connection_error(); |
450 } | 442 } |
451 | 443 |
452 void QuicHttpStream::SetPriority(RequestPriority priority) { | 444 void QuicHttpStream::SetPriority(RequestPriority priority) { |
453 priority_ = priority; | 445 priority_ = priority; |
454 } | 446 } |
455 | 447 |
456 void QuicHttpStream::OnReadResponseHeadersComplete(int rv) { | 448 void QuicHttpStream::OnReadResponseHeadersComplete(int rv) { |
457 DCHECK(callback_); | 449 DCHECK(callback_); |
458 DCHECK(!response_headers_received_); | 450 DCHECK(!response_headers_received_); |
459 if (rv > 0) { | 451 if (rv > 0) { |
460 headers_bytes_received_ += rv; | 452 headers_bytes_received_ += rv; |
461 rv = ProcessResponseHeaders(response_header_block_); | 453 rv = ProcessResponseHeaders(response_header_block_); |
462 } | 454 } |
463 if (rv != ERR_IO_PENDING && !callback_.is_null()) { | 455 if (rv != ERR_IO_PENDING && !callback_.is_null()) { |
464 DoCallback(rv); | 456 DoCallback(rv); |
465 } | 457 } |
466 } | 458 } |
467 | 459 |
468 void QuicHttpStream::ReadTrailingHeaders() { | 460 void QuicHttpStream::ReadTrailingHeaders() { |
469 if (!stream_) | |
470 return; | |
471 | |
472 int rv = stream_->ReadTrailingHeaders( | 461 int rv = stream_->ReadTrailingHeaders( |
473 &trailing_header_block_, | 462 &trailing_header_block_, |
474 base::Bind(&QuicHttpStream::OnReadTrailingHeadersComplete, | 463 base::Bind(&QuicHttpStream::OnReadTrailingHeadersComplete, |
475 weak_factory_.GetWeakPtr())); | 464 weak_factory_.GetWeakPtr())); |
476 | 465 |
477 if (rv != ERR_IO_PENDING) | 466 if (rv != ERR_IO_PENDING) |
478 OnReadTrailingHeadersComplete(rv); | 467 OnReadTrailingHeadersComplete(rv); |
479 } | 468 } |
480 | 469 |
481 void QuicHttpStream::OnReadTrailingHeadersComplete(int rv) { | 470 void QuicHttpStream::OnReadTrailingHeadersComplete(int rv) { |
482 DCHECK(response_headers_received_); | 471 DCHECK(response_headers_received_); |
483 if (rv > 0) | 472 if (rv > 0) |
484 headers_bytes_received_ += rv; | 473 headers_bytes_received_ += rv; |
485 | 474 |
486 // QuicHttpStream ignores trailers. | 475 // QuicHttpStream ignores trailers. |
487 if (stream_->IsDoneReading()) { | 476 if (stream_->IsDoneReading()) { |
488 // Close the read side. If the write side has been closed, this will | 477 // Close the read side. If the write side has been closed, this will |
489 // invoke QuicHttpStream::OnClose to reset the stream. | 478 // invoke QuicHttpStream::OnClose to reset the stream. |
490 stream_->OnFinRead(); | 479 stream_->OnFinRead(); |
491 SetResponseStatus(OK); | 480 SetResponseStatus(OK); |
492 } | 481 } |
493 } | 482 } |
494 | 483 |
495 void QuicHttpStream::OnClose() { | |
496 quic_connection_error_ = stream_->connection_error(); | |
497 quic_stream_error_ = stream_->stream_error(); | |
498 SaveResponseStatus(); | |
499 | |
500 ResetStream(); | |
501 // If already in DoLoop(), |callback_| will be handled when DoLoop() exits. | |
502 if (in_loop_) | |
503 return; | |
504 | |
505 if (!callback_.is_null()) { | |
506 DoCallback(GetResponseStatus()); | |
507 } | |
508 } | |
509 | |
510 void QuicHttpStream::OnError(int error) { | |
511 ResetStream(); | |
512 session_error_ = error; | |
513 SaveResponseStatus(); | |
514 if (in_loop_) { | |
515 // If already in DoLoop(), |callback_| will be handled when DoLoop() exits. | |
516 return; | |
517 } | |
518 if (!callback_.is_null()) | |
519 DoCallback(GetResponseStatus()); | |
520 } | |
521 | |
522 void QuicHttpStream::OnIOComplete(int rv) { | 484 void QuicHttpStream::OnIOComplete(int rv) { |
523 rv = DoLoop(rv); | 485 rv = DoLoop(rv); |
524 | 486 |
525 if (rv != ERR_IO_PENDING && !callback_.is_null()) { | 487 if (rv != ERR_IO_PENDING && !callback_.is_null()) { |
526 DoCallback(rv); | 488 DoCallback(rv); |
527 } | 489 } |
528 } | 490 } |
529 | 491 |
530 void QuicHttpStream::DoCallback(int rv) { | 492 void QuicHttpStream::DoCallback(int rv) { |
531 CHECK_NE(rv, ERR_IO_PENDING); | 493 CHECK_NE(rv, ERR_IO_PENDING); |
532 CHECK(!callback_.is_null()); | 494 CHECK(!callback_.is_null()); |
533 CHECK(!in_loop_); | 495 CHECK(!in_loop_); |
534 | 496 |
535 // The client callback can do anything, including destroying this class, | 497 // The client callback can do anything, including destroying this class, |
536 // so any pending callback must be issued after everything else is done. | 498 // so any pending callback must be issued after everything else is done. |
537 base::ResetAndReturn(&callback_).Run(rv); | 499 base::ResetAndReturn(&callback_).Run(MapStreamError(rv)); |
538 } | 500 } |
539 | 501 |
540 int QuicHttpStream::DoLoop(int rv) { | 502 int QuicHttpStream::DoLoop(int rv) { |
541 CHECK(!in_loop_); | 503 CHECK(!in_loop_); |
542 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); | 504 base::AutoReset<bool> auto_reset_in_loop(&in_loop_, true); |
543 do { | 505 do { |
544 State state = next_state_; | 506 State state = next_state_; |
545 next_state_ = STATE_NONE; | 507 next_state_ = STATE_NONE; |
546 switch (state) { | 508 switch (state) { |
547 case STATE_HANDLE_PROMISE: | 509 case STATE_HANDLE_PROMISE: |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); | 566 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); |
605 } | 567 } |
606 | 568 |
607 int QuicHttpStream::DoRequestStreamComplete(int rv) { | 569 int QuicHttpStream::DoRequestStreamComplete(int rv) { |
608 DCHECK(rv == OK || !stream_); | 570 DCHECK(rv == OK || !stream_); |
609 if (rv != OK) { | 571 if (rv != OK) { |
610 session_error_ = rv; | 572 session_error_ = rv; |
611 return GetResponseStatus(); | 573 return GetResponseStatus(); |
612 } | 574 } |
613 | 575 |
614 stream_ = quic_session()->ReleaseStream(this); | 576 stream_ = quic_session()->ReleaseStream(); |
615 if (request_info_->load_flags & LOAD_DISABLE_CONNECTION_MIGRATION) { | 577 if (request_info_->load_flags & LOAD_DISABLE_CONNECTION_MIGRATION) { |
616 stream_->DisableConnectionMigration(); | 578 stream_->DisableConnectionMigration(); |
617 } | 579 } |
618 | 580 |
619 if (response_info_) { | 581 if (response_info_) { |
620 // This happens in the case of a asynchronous push rendezvous | 582 // This happens in the case of a asynchronous push rendezvous |
621 // that ultimately fails (e.g. vary failure). |response_info_| | 583 // that ultimately fails (e.g. vary failure). |response_info_| |
622 // non-null implies that |DoRequestStream()| was called via | 584 // non-null implies that |DoRequestStream()| was called via |
623 // |SendRequest()|. | 585 // |SendRequest()|. |
624 next_state_ = STATE_SET_REQUEST_PRIORITY; | 586 next_state_ = STATE_SET_REQUEST_PRIORITY; |
625 } | 587 } |
626 | 588 |
627 return OK; | 589 return OK; |
628 } | 590 } |
629 | 591 |
630 int QuicHttpStream::DoSetRequestPriority() { | 592 int QuicHttpStream::DoSetRequestPriority() { |
631 // Set priority according to request | 593 // Set priority according to request |
632 DCHECK(stream_); | 594 DCHECK(stream_); |
633 DCHECK(response_info_); | 595 DCHECK(response_info_); |
634 SpdyPriority priority = ConvertRequestPriorityToQuicPriority(priority_); | 596 SpdyPriority priority = ConvertRequestPriorityToQuicPriority(priority_); |
635 stream_->SetPriority(priority); | 597 stream_->SetPriority(priority); |
636 next_state_ = STATE_SEND_HEADERS; | 598 next_state_ = STATE_SEND_HEADERS; |
637 return OK; | 599 return OK; |
638 } | 600 } |
639 | 601 |
640 int QuicHttpStream::DoSendHeaders() { | 602 int QuicHttpStream::DoSendHeaders() { |
641 if (!stream_) | |
642 return GetResponseStatus(); | |
643 | |
644 // Log the actual request with the URL Request's net log. | 603 // Log the actual request with the URL Request's net log. |
645 stream_net_log_.AddEvent( | 604 stream_net_log_.AddEvent( |
646 NetLogEventType::HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS, | 605 NetLogEventType::HTTP_TRANSACTION_QUIC_SEND_REQUEST_HEADERS, |
647 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_, | 606 base::Bind(&QuicRequestNetLogCallback, stream_->id(), &request_headers_, |
648 priority_)); | 607 priority_)); |
649 bool has_upload_data = request_body_stream_ != nullptr; | 608 bool has_upload_data = request_body_stream_ != nullptr; |
650 | 609 |
651 next_state_ = STATE_SEND_HEADERS_COMPLETE; | 610 next_state_ = STATE_SEND_HEADERS_COMPLETE; |
652 size_t frame_len = stream_->WriteHeaders(std::move(request_headers_), | 611 size_t frame_len = stream_->WriteHeaders(std::move(request_headers_), |
653 !has_upload_data, nullptr); | 612 !has_upload_data, nullptr); |
654 headers_bytes_sent_ += frame_len; | 613 headers_bytes_sent_ += frame_len; |
655 | 614 |
656 request_headers_ = SpdyHeaderBlock(); | 615 request_headers_ = SpdyHeaderBlock(); |
657 return static_cast<int>(frame_len); | 616 return static_cast<int>(frame_len); |
658 } | 617 } |
659 | 618 |
660 int QuicHttpStream::DoSendHeadersComplete(int rv) { | 619 int QuicHttpStream::DoSendHeadersComplete(int rv) { |
661 if (rv < 0) | 620 if (rv < 0) |
662 return rv; | 621 return rv; |
663 | 622 |
664 // If the stream is already closed, don't read the request body. | |
665 if (!stream_) | |
666 return GetResponseStatus(); | |
667 | |
668 next_state_ = request_body_stream_ ? STATE_READ_REQUEST_BODY : STATE_OPEN; | 623 next_state_ = request_body_stream_ ? STATE_READ_REQUEST_BODY : STATE_OPEN; |
669 | 624 |
670 return OK; | 625 return OK; |
671 } | 626 } |
672 | 627 |
673 int QuicHttpStream::DoReadRequestBody() { | 628 int QuicHttpStream::DoReadRequestBody() { |
674 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE; | 629 next_state_ = STATE_READ_REQUEST_BODY_COMPLETE; |
675 return request_body_stream_->Read( | 630 return request_body_stream_->Read( |
676 raw_request_body_buf_.get(), raw_request_body_buf_->size(), | 631 raw_request_body_buf_.get(), raw_request_body_buf_->size(), |
677 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); | 632 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); |
678 } | 633 } |
679 | 634 |
680 int QuicHttpStream::DoReadRequestBodyComplete(int rv) { | 635 int QuicHttpStream::DoReadRequestBodyComplete(int rv) { |
681 // If the stream is already closed, don't continue. | |
682 if (!stream_) | |
683 return GetResponseStatus(); | |
684 | |
685 // |rv| is the result of read from the request body from the last call to | 636 // |rv| is the result of read from the request body from the last call to |
686 // DoSendBody(). | 637 // DoSendBody(). |
687 if (rv < 0) { | 638 if (rv < 0) { |
688 stream_->ClearDelegate(); | |
689 stream_->Reset(QUIC_ERROR_PROCESSING_STREAM); | 639 stream_->Reset(QUIC_ERROR_PROCESSING_STREAM); |
690 ResetStream(); | 640 ResetStream(); |
691 return rv; | 641 return rv; |
692 } | 642 } |
693 | 643 |
694 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv); | 644 request_body_buf_ = new DrainableIOBuffer(raw_request_body_buf_.get(), rv); |
695 if (rv == 0) { // Reached the end. | 645 if (rv == 0) { // Reached the end. |
696 DCHECK(request_body_stream_->IsEOF()); | 646 DCHECK(request_body_stream_->IsEOF()); |
697 } | 647 } |
698 | 648 |
699 next_state_ = STATE_SEND_BODY; | 649 next_state_ = STATE_SEND_BODY; |
700 return OK; | 650 return OK; |
701 } | 651 } |
702 | 652 |
703 int QuicHttpStream::DoSendBody() { | 653 int QuicHttpStream::DoSendBody() { |
704 if (!stream_) | |
705 return GetResponseStatus(); | |
706 | |
707 CHECK(request_body_stream_); | 654 CHECK(request_body_stream_); |
708 CHECK(request_body_buf_.get()); | 655 CHECK(request_body_buf_.get()); |
709 const bool eof = request_body_stream_->IsEOF(); | 656 const bool eof = request_body_stream_->IsEOF(); |
710 int len = request_body_buf_->BytesRemaining(); | 657 int len = request_body_buf_->BytesRemaining(); |
711 if (len > 0 || eof) { | 658 if (len > 0 || eof) { |
712 next_state_ = STATE_SEND_BODY_COMPLETE; | 659 next_state_ = STATE_SEND_BODY_COMPLETE; |
713 QuicStringPiece data(request_body_buf_->data(), len); | 660 QuicStringPiece data(request_body_buf_->data(), len); |
714 return stream_->WriteStreamData( | 661 return stream_->WriteStreamData( |
715 data, eof, | 662 data, eof, |
716 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); | 663 base::Bind(&QuicHttpStream::OnIOComplete, weak_factory_.GetWeakPtr())); |
717 } | 664 } |
718 | 665 |
719 next_state_ = STATE_OPEN; | 666 next_state_ = STATE_OPEN; |
720 return OK; | 667 return OK; |
721 } | 668 } |
722 | 669 |
723 int QuicHttpStream::DoSendBodyComplete(int rv) { | 670 int QuicHttpStream::DoSendBodyComplete(int rv) { |
724 if (rv < 0) | 671 if (rv < 0) |
725 return rv; | 672 return rv; |
726 | 673 |
727 // If the stream is already closed, don't continue. | |
728 if (!stream_) | |
729 return GetResponseStatus(); | |
730 | |
731 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining()); | 674 request_body_buf_->DidConsume(request_body_buf_->BytesRemaining()); |
732 | 675 |
733 if (!request_body_stream_->IsEOF()) { | 676 if (!request_body_stream_->IsEOF()) { |
734 next_state_ = STATE_READ_REQUEST_BODY; | 677 next_state_ = STATE_READ_REQUEST_BODY; |
735 return OK; | 678 return OK; |
736 } | 679 } |
737 | 680 |
738 next_state_ = STATE_OPEN; | 681 next_state_ = STATE_OPEN; |
739 return OK; | 682 return OK; |
740 } | 683 } |
(...skipping 22 matching lines...) Expand all Loading... |
763 response_headers_received_ = true; | 706 response_headers_received_ = true; |
764 | 707 |
765 // Populate |connect_timing_| when response headers are received. This should | 708 // Populate |connect_timing_| when response headers are received. This should |
766 // take care of 0-RTT where request is sent before handshake is confirmed. | 709 // take care of 0-RTT where request is sent before handshake is confirmed. |
767 connect_timing_ = quic_session()->GetConnectTiming(); | 710 connect_timing_ = quic_session()->GetConnectTiming(); |
768 | 711 |
769 base::ThreadTaskRunnerHandle::Get()->PostTask( | 712 base::ThreadTaskRunnerHandle::Get()->PostTask( |
770 FROM_HERE, base::Bind(&QuicHttpStream::ReadTrailingHeaders, | 713 FROM_HERE, base::Bind(&QuicHttpStream::ReadTrailingHeaders, |
771 weak_factory_.GetWeakPtr())); | 714 weak_factory_.GetWeakPtr())); |
772 | 715 |
| 716 if (stream_->IsDoneReading()) { |
| 717 session_error_ = OK; |
| 718 SaveResponseStatus(); |
| 719 stream_->OnFinRead(); |
| 720 } |
| 721 |
773 return OK; | 722 return OK; |
774 } | 723 } |
775 | 724 |
776 void QuicHttpStream::OnReadBodyComplete(int rv) { | 725 void QuicHttpStream::OnReadBodyComplete(int rv) { |
777 CHECK(callback_); | 726 CHECK(callback_); |
778 user_buffer_ = nullptr; | 727 user_buffer_ = nullptr; |
779 user_buffer_len_ = 0; | 728 user_buffer_len_ = 0; |
780 rv = HandleReadComplete(rv); | 729 rv = HandleReadComplete(rv); |
781 DoCallback(rv); | 730 DoCallback(rv); |
782 } | 731 } |
783 | 732 |
784 int QuicHttpStream::HandleReadComplete(int rv) { | 733 int QuicHttpStream::HandleReadComplete(int rv) { |
785 if (stream_->IsDoneReading()) { | 734 if (stream_->IsDoneReading()) { |
786 stream_->ClearDelegate(); | |
787 stream_->OnFinRead(); | 735 stream_->OnFinRead(); |
788 SetResponseStatus(OK); | 736 SetResponseStatus(OK); |
789 ResetStream(); | 737 ResetStream(); |
790 } | 738 } |
791 return rv; | 739 return rv; |
792 } | 740 } |
793 | 741 |
794 void QuicHttpStream::ResetStream() { | 742 void QuicHttpStream::ResetStream() { |
795 if (push_handle_) { | 743 if (push_handle_) { |
796 push_handle_->Cancel(); | 744 push_handle_->Cancel(); |
797 push_handle_ = nullptr; | 745 push_handle_ = nullptr; |
798 } | 746 } |
799 | 747 |
800 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress | 748 // If |request_body_stream_| is non-NULL, Reset it, to abort any in progress |
801 // read. | 749 // read. |
802 if (request_body_stream_) | 750 if (request_body_stream_) |
803 request_body_stream_->Reset(); | 751 request_body_stream_->Reset(); |
804 | 752 |
805 if (!stream_) | 753 if (!stream_) |
806 return; | 754 return; |
| 755 |
807 DCHECK_LE(stream_->NumBytesConsumed(), stream_->stream_bytes_read()); | 756 DCHECK_LE(stream_->NumBytesConsumed(), stream_->stream_bytes_read()); |
808 // Only count the uniquely received bytes. | 757 // Only count the uniquely received bytes. |
809 closed_stream_received_bytes_ = stream_->NumBytesConsumed(); | 758 closed_stream_received_bytes_ = stream_->NumBytesConsumed(); |
810 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); | 759 closed_stream_sent_bytes_ = stream_->stream_bytes_written(); |
811 closed_is_first_stream_ = stream_->IsFirstStream(); | 760 closed_is_first_stream_ = stream_->IsFirstStream(); |
812 stream_->ClearDelegate(); | 761 } |
813 stream_ = nullptr; | 762 |
| 763 int QuicHttpStream::MapStreamError(int rv) { |
| 764 if (rv == ERR_QUIC_PROTOCOL_ERROR && |
| 765 !quic_session()->IsCryptoHandshakeConfirmed()) { |
| 766 return ERR_QUIC_HANDSHAKE_FAILED; |
| 767 } |
| 768 return rv; |
814 } | 769 } |
815 | 770 |
816 int QuicHttpStream::GetResponseStatus() { | 771 int QuicHttpStream::GetResponseStatus() { |
817 SaveResponseStatus(); | 772 SaveResponseStatus(); |
818 return response_status_; | 773 return response_status_; |
819 } | 774 } |
820 | 775 |
821 void QuicHttpStream::SaveResponseStatus() { | 776 void QuicHttpStream::SaveResponseStatus() { |
822 if (!has_response_status_) | 777 if (!has_response_status_) |
823 SetResponseStatus(ComputeResponseStatus()); | 778 SetResponseStatus(ComputeResponseStatus()); |
(...skipping 16 matching lines...) Expand all Loading... |
840 if (session_error_ != ERR_UNEXPECTED) | 795 if (session_error_ != ERR_UNEXPECTED) |
841 return session_error_; | 796 return session_error_; |
842 | 797 |
843 // If |response_info_| is null then the request has not been sent, so | 798 // If |response_info_| is null then the request has not been sent, so |
844 // return ERR_CONNECTION_CLOSED to permit HttpNetworkTransaction to | 799 // return ERR_CONNECTION_CLOSED to permit HttpNetworkTransaction to |
845 // retry the request. | 800 // retry the request. |
846 if (!response_info_) | 801 if (!response_info_) |
847 return ERR_CONNECTION_CLOSED; | 802 return ERR_CONNECTION_CLOSED; |
848 | 803 |
849 // Explicit stream error are always fatal. | 804 // Explicit stream error are always fatal. |
850 if (quic_stream_error_ != QUIC_STREAM_NO_ERROR && | 805 if (stream_->stream_error() != QUIC_STREAM_NO_ERROR && |
851 quic_stream_error_ != QUIC_STREAM_CONNECTION_ERROR) { | 806 stream_->stream_error() != QUIC_STREAM_CONNECTION_ERROR) { |
852 return ERR_QUIC_PROTOCOL_ERROR; | 807 return ERR_QUIC_PROTOCOL_ERROR; |
853 } | 808 } |
854 | 809 |
855 DCHECK_NE(QUIC_HANDSHAKE_TIMEOUT, quic_connection_error_); | |
856 | |
857 return ERR_QUIC_PROTOCOL_ERROR; | 810 return ERR_QUIC_PROTOCOL_ERROR; |
858 } | 811 } |
859 | 812 |
860 } // namespace net | 813 } // namespace net |
OLD | NEW |