| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/http/bidirectional_stream.h" | 5 #include "net/http/bidirectional_stream.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "net/http/http_stream.h" | 21 #include "net/http/http_stream.h" |
| 22 #include "net/spdy/spdy_http_utils.h" | 22 #include "net/spdy/spdy_http_utils.h" |
| 23 #include "net/ssl/ssl_cert_request_info.h" | 23 #include "net/ssl/ssl_cert_request_info.h" |
| 24 #include "net/ssl/ssl_config.h" | 24 #include "net/ssl/ssl_config.h" |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 namespace net { | 27 namespace net { |
| 28 | 28 |
| 29 BidirectionalStream::Delegate::Delegate() {} | 29 BidirectionalStream::Delegate::Delegate() {} |
| 30 | 30 |
| 31 void BidirectionalStream::Delegate::OnStreamReady() {} |
| 32 |
| 31 BidirectionalStream::Delegate::~Delegate() {} | 33 BidirectionalStream::Delegate::~Delegate() {} |
| 32 | 34 |
| 33 BidirectionalStream::BidirectionalStream( | 35 BidirectionalStream::BidirectionalStream( |
| 34 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 36 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 35 HttpNetworkSession* session, | 37 HttpNetworkSession* session, |
| 38 bool disable_auto_flush, |
| 36 Delegate* delegate) | 39 Delegate* delegate) |
| 37 : BidirectionalStream(std::move(request_info), | 40 : BidirectionalStream(std::move(request_info), |
| 38 session, | 41 session, |
| 42 disable_auto_flush, |
| 39 delegate, | 43 delegate, |
| 40 base::WrapUnique(new base::Timer(false, false))) {} | 44 base::WrapUnique(new base::Timer(false, false))) {} |
| 41 | 45 |
| 42 BidirectionalStream::BidirectionalStream( | 46 BidirectionalStream::BidirectionalStream( |
| 43 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 47 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 44 HttpNetworkSession* session, | 48 HttpNetworkSession* session, |
| 49 bool disable_auto_flush, |
| 45 Delegate* delegate, | 50 Delegate* delegate, |
| 46 std::unique_ptr<base::Timer> timer) | 51 std::unique_ptr<base::Timer> timer) |
| 47 : request_info_(std::move(request_info)), | 52 : request_info_(std::move(request_info)), |
| 48 net_log_(BoundNetLog::Make(session->net_log(), | 53 net_log_(BoundNetLog::Make(session->net_log(), |
| 49 NetLog::SOURCE_BIDIRECTIONAL_STREAM)), | 54 NetLog::SOURCE_BIDIRECTIONAL_STREAM)), |
| 50 session_(session), | 55 session_(session), |
| 56 disable_auto_flush_(disable_auto_flush), |
| 51 delegate_(delegate), | 57 delegate_(delegate), |
| 52 timer_(std::move(timer)), | 58 timer_(std::move(timer)) { |
| 53 write_buffer_len_(0) { | |
| 54 DCHECK(delegate_); | 59 DCHECK(delegate_); |
| 55 DCHECK(request_info_); | 60 DCHECK(request_info_); |
| 56 | 61 |
| 57 SSLConfig server_ssl_config; | 62 SSLConfig server_ssl_config; |
| 58 session->ssl_config_service()->GetSSLConfig(&server_ssl_config); | 63 session->ssl_config_service()->GetSSLConfig(&server_ssl_config); |
| 59 session->GetAlpnProtos(&server_ssl_config.alpn_protos); | 64 session->GetAlpnProtos(&server_ssl_config.alpn_protos); |
| 60 session->GetNpnProtos(&server_ssl_config.npn_protos); | 65 session->GetNpnProtos(&server_ssl_config.npn_protos); |
| 61 | 66 |
| 62 if (!request_info_->url.SchemeIs(url::kHttpsScheme)) { | 67 if (!request_info_->url.SchemeIs(url::kHttpsScheme)) { |
| 63 base::ThreadTaskRunnerHandle::Get()->PostTask( | 68 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 read_buffer_ = buf; | 102 read_buffer_ = buf; |
| 98 // Bytes will be logged in OnDataRead(). | 103 // Bytes will be logged in OnDataRead(). |
| 99 } | 104 } |
| 100 return rv; | 105 return rv; |
| 101 } | 106 } |
| 102 | 107 |
| 103 void BidirectionalStream::SendData(IOBuffer* data, | 108 void BidirectionalStream::SendData(IOBuffer* data, |
| 104 int length, | 109 int length, |
| 105 bool end_stream) { | 110 bool end_stream) { |
| 106 DCHECK(stream_impl_); | 111 DCHECK(stream_impl_); |
| 112 DCHECK(write_buffer_list_.empty()); |
| 113 DCHECK(write_buffer_len_list_.empty()); |
| 107 | 114 |
| 108 stream_impl_->SendData(data, length, end_stream); | 115 stream_impl_->SendData(data, length, end_stream); |
| 109 write_buffer_ = data; | 116 write_buffer_list_.push_back(data); |
| 110 write_buffer_len_ = length; | 117 write_buffer_len_list_.push_back(length); |
| 118 } |
| 119 |
| 120 void BidirectionalStream::SendvData(const std::vector<IOBuffer*>& buffers, |
| 121 const std::vector<int>& lengths, |
| 122 bool end_stream) { |
| 123 DCHECK(stream_impl_); |
| 124 DCHECK_EQ(buffers.size(), lengths.size()); |
| 125 DCHECK(write_buffer_list_.empty()); |
| 126 DCHECK(write_buffer_len_list_.empty()); |
| 127 |
| 128 stream_impl_->SendvData(buffers, lengths, end_stream); |
| 129 for (size_t i = 0; i < buffers.size(); ++i) { |
| 130 write_buffer_list_.push_back(buffers[i]); |
| 131 write_buffer_len_list_.push_back(lengths[i]); |
| 132 } |
| 111 } | 133 } |
| 112 | 134 |
| 113 void BidirectionalStream::Cancel() { | 135 void BidirectionalStream::Cancel() { |
| 114 stream_request_.reset(); | 136 stream_request_.reset(); |
| 115 if (stream_impl_) { | 137 if (stream_impl_) { |
| 116 stream_impl_->Cancel(); | 138 stream_impl_->Cancel(); |
| 117 stream_impl_.reset(); | 139 stream_impl_.reset(); |
| 118 } | 140 } |
| 119 } | 141 } |
| 120 | 142 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 132 return stream_impl_->GetTotalReceivedBytes(); | 154 return stream_impl_->GetTotalReceivedBytes(); |
| 133 } | 155 } |
| 134 | 156 |
| 135 int64_t BidirectionalStream::GetTotalSentBytes() const { | 157 int64_t BidirectionalStream::GetTotalSentBytes() const { |
| 136 if (!stream_impl_) | 158 if (!stream_impl_) |
| 137 return 0; | 159 return 0; |
| 138 | 160 |
| 139 return stream_impl_->GetTotalSentBytes(); | 161 return stream_impl_->GetTotalSentBytes(); |
| 140 } | 162 } |
| 141 | 163 |
| 142 void BidirectionalStream::OnHeadersSent() { | 164 void BidirectionalStream::OnStreamReady() { |
| 143 delegate_->OnHeadersSent(); | 165 delegate_->OnStreamReady(); |
| 144 } | 166 } |
| 145 | 167 |
| 146 void BidirectionalStream::OnHeadersReceived( | 168 void BidirectionalStream::OnHeadersReceived( |
| 147 const SpdyHeaderBlock& response_headers) { | 169 const SpdyHeaderBlock& response_headers) { |
| 148 HttpResponseInfo response_info; | 170 HttpResponseInfo response_info; |
| 149 if (!SpdyHeadersToHttpResponse(response_headers, HTTP2, &response_info)) { | 171 if (!SpdyHeadersToHttpResponse(response_headers, HTTP2, &response_info)) { |
| 150 DLOG(WARNING) << "Invalid headers"; | 172 DLOG(WARNING) << "Invalid headers"; |
| 151 delegate_->OnFailed(ERR_FAILED); | 173 delegate_->OnFailed(ERR_FAILED); |
| 152 return; | 174 return; |
| 153 } | 175 } |
| 154 | 176 |
| 155 session_->http_stream_factory()->ProcessAlternativeServices( | 177 session_->http_stream_factory()->ProcessAlternativeServices( |
| 156 session_, response_info.headers.get(), | 178 session_, response_info.headers.get(), |
| 157 url::SchemeHostPort(request_info_->url)); | 179 url::SchemeHostPort(request_info_->url)); |
| 158 delegate_->OnHeadersReceived(response_headers); | 180 delegate_->OnHeadersReceived(response_headers); |
| 159 } | 181 } |
| 160 | 182 |
| 161 void BidirectionalStream::OnDataRead(int bytes_read) { | 183 void BidirectionalStream::OnDataRead(int bytes_read) { |
| 162 DCHECK(read_buffer_); | 184 DCHECK(read_buffer_); |
| 163 | 185 |
| 164 net_log_.AddByteTransferEvent( | 186 net_log_.AddByteTransferEvent( |
| 165 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, bytes_read, | 187 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, bytes_read, |
| 166 read_buffer_->data()); | 188 read_buffer_->data()); |
| 167 read_buffer_ = nullptr; | 189 read_buffer_ = nullptr; |
| 168 delegate_->OnDataRead(bytes_read); | 190 delegate_->OnDataRead(bytes_read); |
| 169 } | 191 } |
| 170 | 192 |
| 171 void BidirectionalStream::OnDataSent() { | 193 void BidirectionalStream::OnDataSent() { |
| 172 DCHECK(write_buffer_); | 194 DCHECK(!write_buffer_list_.empty()); |
| 195 DCHECK_EQ(write_buffer_list_.size(), write_buffer_len_list_.size()); |
| 173 | 196 |
| 174 net_log_.AddByteTransferEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_SENT, | 197 for (size_t i = 0; i < write_buffer_list_.size(); ++i) { |
| 175 write_buffer_len_, write_buffer_->data()); | 198 net_log_.AddByteTransferEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_SENT, |
| 176 write_buffer_ = nullptr; | 199 write_buffer_len_list_[i], |
| 177 write_buffer_len_ = 0; | 200 write_buffer_list_[i]->data()); |
| 201 } |
| 202 write_buffer_list_.clear(); |
| 203 write_buffer_len_list_.clear(); |
| 178 delegate_->OnDataSent(); | 204 delegate_->OnDataSent(); |
| 179 } | 205 } |
| 180 | 206 |
| 181 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { | 207 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { |
| 182 delegate_->OnTrailersReceived(trailers); | 208 delegate_->OnTrailersReceived(trailers); |
| 183 } | 209 } |
| 184 | 210 |
| 185 void BidirectionalStream::OnFailed(int status) { | 211 void BidirectionalStream::OnFailed(int status) { |
| 186 delegate_->OnFailed(status); | 212 delegate_->OnFailed(status); |
| 187 } | 213 } |
| 188 | 214 |
| 189 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, | 215 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, |
| 190 const ProxyInfo& used_proxy_info, | 216 const ProxyInfo& used_proxy_info, |
| 191 HttpStream* stream) { | 217 HttpStream* stream) { |
| 192 NOTREACHED(); | 218 NOTREACHED(); |
| 193 } | 219 } |
| 194 | 220 |
| 195 void BidirectionalStream::OnBidirectionalStreamImplReady( | 221 void BidirectionalStream::OnBidirectionalStreamImplReady( |
| 196 const SSLConfig& used_ssl_config, | 222 const SSLConfig& used_ssl_config, |
| 197 const ProxyInfo& used_proxy_info, | 223 const ProxyInfo& used_proxy_info, |
| 198 BidirectionalStreamImpl* stream) { | 224 BidirectionalStreamImpl* stream) { |
| 199 DCHECK(!stream_impl_); | 225 DCHECK(!stream_impl_); |
| 200 | 226 |
| 201 stream_request_.reset(); | 227 stream_request_.reset(); |
| 202 stream_impl_.reset(stream); | 228 stream_impl_.reset(stream); |
| 203 stream_impl_->Start(request_info_.get(), net_log_, this, std::move(timer_)); | 229 stream_impl_->Start(request_info_.get(), net_log_, disable_auto_flush_, this, |
| 230 std::move(timer_)); |
| 204 } | 231 } |
| 205 | 232 |
| 206 void BidirectionalStream::OnWebSocketHandshakeStreamReady( | 233 void BidirectionalStream::OnWebSocketHandshakeStreamReady( |
| 207 const SSLConfig& used_ssl_config, | 234 const SSLConfig& used_ssl_config, |
| 208 const ProxyInfo& used_proxy_info, | 235 const ProxyInfo& used_proxy_info, |
| 209 WebSocketHandshakeStreamBase* stream) { | 236 WebSocketHandshakeStreamBase* stream) { |
| 210 NOTREACHED(); | 237 NOTREACHED(); |
| 211 } | 238 } |
| 212 | 239 |
| 213 void BidirectionalStream::OnStreamFailed(int result, | 240 void BidirectionalStream::OnStreamFailed(int result, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 const ProxyInfo& used_proxy_info, | 280 const ProxyInfo& used_proxy_info, |
| 254 HttpStream* stream) { | 281 HttpStream* stream) { |
| 255 DCHECK(stream_request_); | 282 DCHECK(stream_request_); |
| 256 | 283 |
| 257 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); | 284 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 258 } | 285 } |
| 259 | 286 |
| 260 void BidirectionalStream::OnQuicBroken() {} | 287 void BidirectionalStream::OnQuicBroken() {} |
| 261 | 288 |
| 262 } // namespace net | 289 } // namespace net |
| OLD | NEW |