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/spdy/spdy_stream.h" | 5 #include "net/spdy/spdy_stream.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 if (*i >= 'A' && *i <= 'Z') { | 65 if (*i >= 'A' && *i <= 'Z') { |
66 return true; | 66 return true; |
67 } | 67 } |
68 } | 68 } |
69 return false; | 69 return false; |
70 } | 70 } |
71 | 71 |
72 } // namespace | 72 } // namespace |
73 | 73 |
74 SpdyStream::SpdyStream(SpdySession* session, | 74 SpdyStream::SpdyStream(SpdySession* session, |
75 SpdyStreamId stream_id, | |
76 bool pushed, | 75 bool pushed, |
77 const BoundNetLog& net_log) | 76 const BoundNetLog& net_log) |
78 : continue_buffering_data_(true), | 77 : continue_buffering_data_(true), |
79 stream_id_(stream_id), | 78 stream_id_(0), |
80 priority_(HIGHEST), | 79 priority_(HIGHEST), |
81 slot_(0), | 80 slot_(0), |
82 stalled_by_flow_control_(false), | 81 stalled_by_flow_control_(false), |
83 send_window_size_(kSpdyStreamInitialWindowSize), | 82 send_window_size_(kSpdyStreamInitialWindowSize), |
84 recv_window_size_(kSpdyStreamInitialWindowSize), | 83 recv_window_size_(kSpdyStreamInitialWindowSize), |
85 unacked_recv_window_bytes_(0), | 84 unacked_recv_window_bytes_(0), |
86 pushed_(pushed), | 85 pushed_(pushed), |
87 response_received_(false), | 86 response_received_(false), |
88 session_(session), | 87 session_(session), |
89 delegate_(NULL), | 88 delegate_(NULL), |
90 request_time_(base::Time::Now()), | 89 request_time_(base::Time::Now()), |
91 response_(new SpdyHeaderBlock), | 90 response_(new SpdyHeaderBlock), |
92 io_state_(STATE_NONE), | 91 io_state_(STATE_NONE), |
93 response_status_(OK), | 92 response_status_(OK), |
94 cancelled_(false), | 93 cancelled_(false), |
95 has_upload_data_(false), | 94 has_upload_data_(false), |
96 net_log_(net_log), | 95 net_log_(net_log), |
97 send_bytes_(0), | 96 send_bytes_(0), |
98 recv_bytes_(0), | 97 recv_bytes_(0), |
99 domain_bound_cert_type_(CLIENT_CERT_INVALID_TYPE) { | 98 domain_bound_cert_type_(CLIENT_CERT_INVALID_TYPE) { |
100 } | 99 } |
101 | 100 |
| 101 RequestPriority SpdyStream::GetPriority() const { |
| 102 return priority(); |
| 103 } |
| 104 |
| 105 SpdyFrame* SpdyStream::ProduceNextFrame() { |
| 106 if (io_state_ == STATE_SEND_DOMAIN_BOUND_CERT_COMPLETE) { |
| 107 CHECK(request_.get()); |
| 108 std::string origin = GetUrl().GetOrigin().spec(); |
| 109 origin.erase(origin.length() - 1); // trim trailing slash |
| 110 SpdyCredentialControlFrame* frame = session_->CreateCredentialFrame( |
| 111 origin, domain_bound_cert_type_, domain_bound_private_key_, |
| 112 domain_bound_cert_, priority_); |
| 113 return frame; |
| 114 } else if (io_state_ == STATE_SEND_HEADERS_COMPLETE) { |
| 115 SpdyControlFlags flags = |
| 116 has_upload_data_ ? CONTROL_FLAG_NONE : CONTROL_FLAG_FIN; |
| 117 |
| 118 CHECK(request_.get()); |
| 119 SpdySynStreamControlFrame* frame = session_->CreateSynStream( |
| 120 stream_id_, priority_, slot_, flags, request_); |
| 121 set_stream_id(frame->stream_id()); |
| 122 send_time_ = base::TimeTicks::Now(); |
| 123 return frame; |
| 124 } else { |
| 125 CHECK(!cancelled()); |
| 126 // We must need to write stream data. |
| 127 // Until the headers have been completely sent, we can not be sure |
| 128 // that our stream_id is correct. |
| 129 DCHECK_GT(io_state_, STATE_SEND_HEADERS_COMPLETE); |
| 130 DCHECK_GT(stream_id_, 0u); |
| 131 DCHECK(!pending_data_frames_.empty()); |
| 132 SpdyFrame* frame = pending_data_frames_.front(); |
| 133 pending_data_frames_.pop_front(); |
| 134 return frame; |
| 135 } |
| 136 } |
| 137 |
| 138 SpdyStream* SpdyStream::GetSpdyStream() const { |
| 139 return const_cast<SpdyStream*>(this); |
| 140 } |
| 141 |
102 SpdyStream::~SpdyStream() { | 142 SpdyStream::~SpdyStream() { |
103 UpdateHistograms(); | 143 UpdateHistograms(); |
| 144 while (!pending_data_frames_.empty()) { |
| 145 SpdyFrame* frame = pending_data_frames_.back(); |
| 146 pending_data_frames_.pop_back(); |
| 147 delete frame; |
| 148 } |
104 } | 149 } |
105 | 150 |
106 void SpdyStream::SetDelegate(Delegate* delegate) { | 151 void SpdyStream::SetDelegate(Delegate* delegate) { |
107 CHECK(delegate); | 152 CHECK(delegate); |
108 delegate_ = delegate; | 153 delegate_ = delegate; |
109 | 154 |
110 if (pushed_) { | 155 if (pushed_) { |
111 CHECK(response_received()); | 156 CHECK(response_received()); |
112 MessageLoop::current()->PostTask( | 157 MessageLoop::current()->PostTask( |
113 FROM_HERE, base::Bind(&SpdyStream::PushedStreamReplayData, this)); | 158 FROM_HERE, base::Bind(&SpdyStream::PushedStreamReplayData, this)); |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 void SpdyStream::Cancel() { | 533 void SpdyStream::Cancel() { |
489 if (cancelled()) | 534 if (cancelled()) |
490 return; | 535 return; |
491 | 536 |
492 cancelled_ = true; | 537 cancelled_ = true; |
493 if (session_->IsStreamActive(stream_id_)) | 538 if (session_->IsStreamActive(stream_id_)) |
494 session_->ResetStream(stream_id_, CANCEL, ""); | 539 session_->ResetStream(stream_id_, CANCEL, ""); |
495 } | 540 } |
496 | 541 |
497 void SpdyStream::Close() { | 542 void SpdyStream::Close() { |
498 session_->CloseStream(stream_id_, net::OK); | 543 if (stream_id_ != 0) |
| 544 session_->CloseStream(stream_id_, net::OK); |
| 545 else |
| 546 session_->CloseCreatedStream(this, OK); |
499 } | 547 } |
500 | 548 |
501 int SpdyStream::SendRequest(bool has_upload_data) { | 549 int SpdyStream::SendRequest(bool has_upload_data) { |
502 if (delegate_) | 550 if (delegate_) |
503 delegate_->set_chunk_callback(this); | 551 delegate_->set_chunk_callback(this); |
504 | 552 |
505 // Pushed streams do not send any data, and should always be in STATE_OPEN or | 553 // Pushed streams do not send any data, and should always be in STATE_OPEN or |
506 // STATE_DONE. However, we still want to return IO_PENDING to mimic non-push | 554 // STATE_DONE. However, we still want to return IO_PENDING to mimic non-push |
507 // behavior. | 555 // behavior. |
508 has_upload_data_ = has_upload_data; | 556 has_upload_data_ = has_upload_data; |
509 if (pushed_) { | 557 if (pushed_) { |
510 send_time_ = base::TimeTicks::Now(); | 558 send_time_ = base::TimeTicks::Now(); |
511 DCHECK(!has_upload_data_); | 559 DCHECK(!has_upload_data_); |
512 DCHECK(response_received()); | 560 DCHECK(response_received()); |
513 return ERR_IO_PENDING; | 561 return ERR_IO_PENDING; |
514 } | 562 } |
515 CHECK_EQ(STATE_NONE, io_state_); | 563 CHECK_EQ(STATE_NONE, io_state_); |
516 io_state_ = STATE_GET_DOMAIN_BOUND_CERT; | 564 io_state_ = STATE_GET_DOMAIN_BOUND_CERT; |
517 return DoLoop(OK); | 565 return DoLoop(OK); |
518 } | 566 } |
519 | 567 |
520 int SpdyStream::WriteStreamData(IOBuffer* data, int length, | 568 int SpdyStream::WriteStreamData(IOBuffer* data, int length, |
521 SpdyDataFlags flags) { | 569 SpdyDataFlags flags) { |
522 // Until the headers have been completely sent, we can not be sure | 570 // Until the headers have been completely sent, we can not be sure |
523 // that our stream_id is correct. | 571 // that our stream_id is correct. |
524 DCHECK_GT(io_state_, STATE_SEND_HEADERS_COMPLETE); | 572 DCHECK_GT(io_state_, STATE_SEND_HEADERS_COMPLETE); |
525 return session_->WriteStreamData(stream_id_, data, length, flags); | 573 DCHECK_GT(stream_id_, 0u); |
| 574 |
| 575 pending_data_frames_.push_back( |
| 576 session_->CreateDataFrame(stream_id_, data, length, flags)); |
| 577 |
| 578 session_->SetStreamHasWriteAvailable(this); |
| 579 return ERR_IO_PENDING; |
526 } | 580 } |
527 | 581 |
528 bool SpdyStream::GetSSLInfo(SSLInfo* ssl_info, | 582 bool SpdyStream::GetSSLInfo(SSLInfo* ssl_info, |
529 bool* was_npn_negotiated, | 583 bool* was_npn_negotiated, |
530 NextProto* protocol_negotiated) { | 584 NextProto* protocol_negotiated) { |
531 return session_->GetSSLInfo( | 585 return session_->GetSSLInfo( |
532 ssl_info, was_npn_negotiated, protocol_negotiated); | 586 ssl_info, was_npn_negotiated, protocol_negotiated); |
533 } | 587 } |
534 | 588 |
535 bool SpdyStream::GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) { | 589 bool SpdyStream::GetSSLCertRequestInfo(SSLCertRequestInfo* cert_request_info) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
661 io_state_ = STATE_SEND_DOMAIN_BOUND_CERT; | 715 io_state_ = STATE_SEND_DOMAIN_BOUND_CERT; |
662 slot_ = session_->credential_state()->SetHasCredential(GetUrl()); | 716 slot_ = session_->credential_state()->SetHasCredential(GetUrl()); |
663 return OK; | 717 return OK; |
664 } | 718 } |
665 | 719 |
666 int SpdyStream::DoSendDomainBoundCert() { | 720 int SpdyStream::DoSendDomainBoundCert() { |
667 io_state_ = STATE_SEND_DOMAIN_BOUND_CERT_COMPLETE; | 721 io_state_ = STATE_SEND_DOMAIN_BOUND_CERT_COMPLETE; |
668 CHECK(request_.get()); | 722 CHECK(request_.get()); |
669 std::string origin = GetUrl().GetOrigin().spec(); | 723 std::string origin = GetUrl().GetOrigin().spec(); |
670 origin.erase(origin.length() - 1); // trim trailing slash | 724 origin.erase(origin.length() - 1); // trim trailing slash |
671 int rv = session_->WriteCredentialFrame( | 725 SpdyCredentialControlFrame* frame = session_->CreateCredentialFrame( |
672 origin, domain_bound_cert_type_, domain_bound_private_key_, | 726 origin, domain_bound_cert_type_, domain_bound_private_key_, |
673 domain_bound_cert_, priority_); | 727 domain_bound_cert_, priority_); |
674 if (rv != ERR_IO_PENDING) | 728 frame->length(); |
675 return rv; | 729 session_->SetStreamHasWriteAvailable(this); |
676 return OK; | 730 return ERR_IO_PENDING; |
677 } | 731 } |
678 | 732 |
679 int SpdyStream::DoSendDomainBoundCertComplete(int result) { | 733 int SpdyStream::DoSendDomainBoundCertComplete(int result) { |
680 if (result < 0) | 734 if (result < 0) |
681 return result; | 735 return result; |
682 | 736 |
683 io_state_ = STATE_SEND_HEADERS; | 737 io_state_ = STATE_SEND_HEADERS; |
684 return OK; | 738 return OK; |
685 } | 739 } |
686 | 740 |
687 int SpdyStream::DoSendHeaders() { | 741 int SpdyStream::DoSendHeaders() { |
688 CHECK(!cancelled_); | 742 CHECK(!cancelled_); |
689 | 743 |
690 SpdyControlFlags flags = CONTROL_FLAG_NONE; | 744 session_->SetStreamHasWriteAvailable(this); |
691 if (!has_upload_data_) | |
692 flags = CONTROL_FLAG_FIN; | |
693 | |
694 CHECK(request_.get()); | |
695 int result = session_->WriteSynStream( | |
696 stream_id_, priority_, slot_, flags, | |
697 request_); | |
698 if (result != ERR_IO_PENDING) | |
699 return result; | |
700 | |
701 send_time_ = base::TimeTicks::Now(); | |
702 io_state_ = STATE_SEND_HEADERS_COMPLETE; | 745 io_state_ = STATE_SEND_HEADERS_COMPLETE; |
703 return ERR_IO_PENDING; | 746 return ERR_IO_PENDING; |
704 } | 747 } |
705 | 748 |
706 int SpdyStream::DoSendHeadersComplete(int result) { | 749 int SpdyStream::DoSendHeadersComplete(int result) { |
707 if (result < 0) | 750 if (result < 0) |
708 return result; | 751 return result; |
709 | 752 |
710 CHECK_GT(result, 0); | 753 CHECK_GT(result, 0); |
711 | 754 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
771 UMA_HISTOGRAM_TIMES("Net.SpdyStreamDownloadTime", | 814 UMA_HISTOGRAM_TIMES("Net.SpdyStreamDownloadTime", |
772 recv_last_byte_time_ - recv_first_byte_time_); | 815 recv_last_byte_time_ - recv_first_byte_time_); |
773 UMA_HISTOGRAM_TIMES("Net.SpdyStreamTime", | 816 UMA_HISTOGRAM_TIMES("Net.SpdyStreamTime", |
774 recv_last_byte_time_ - send_time_); | 817 recv_last_byte_time_ - send_time_); |
775 | 818 |
776 UMA_HISTOGRAM_COUNTS("Net.SpdySendBytes", send_bytes_); | 819 UMA_HISTOGRAM_COUNTS("Net.SpdySendBytes", send_bytes_); |
777 UMA_HISTOGRAM_COUNTS("Net.SpdyRecvBytes", recv_bytes_); | 820 UMA_HISTOGRAM_COUNTS("Net.SpdyRecvBytes", recv_bytes_); |
778 } | 821 } |
779 | 822 |
780 } // namespace net | 823 } // namespace net |
OLD | NEW |