Chromium Code Reviews| 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 #include <string> | |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/location.h" | 11 #include "base/location.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 13 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "base/values.h" | |
| 16 #include "net/base/load_flags.h" | 18 #include "net/base/load_flags.h" |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "net/http/bidirectional_stream_request_info.h" | 20 #include "net/http/bidirectional_stream_request_info.h" |
| 21 #include "net/http/http_log_util.h" | |
| 19 #include "net/http/http_network_session.h" | 22 #include "net/http/http_network_session.h" |
| 20 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
| 21 #include "net/http/http_stream.h" | 24 #include "net/http/http_stream.h" |
| 22 #include "net/spdy/spdy_http_utils.h" | 25 #include "net/spdy/spdy_http_utils.h" |
| 23 #include "net/ssl/ssl_cert_request_info.h" | 26 #include "net/ssl/ssl_cert_request_info.h" |
| 24 #include "net/ssl/ssl_config.h" | 27 #include "net/ssl/ssl_config.h" |
| 25 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 26 | 29 |
| 27 namespace net { | 30 namespace net { |
| 28 | 31 |
| 32 namespace { | |
| 33 | |
| 34 std::unique_ptr<base::Value> NetLogHeadersCallback( | |
| 35 const SpdyHeaderBlock* headers, | |
|
mef
2016/04/28 20:43:18
#include "net/spdy/spdy_header_block.h"
xunjieli
2016/04/28 21:06:15
Done.
| |
| 36 NetLogCaptureMode capture_mode) { | |
|
mef
2016/04/28 20:43:18
#include "net/log/net_log_capture_mode.h"
xunjieli
2016/04/28 21:06:15
Done.
| |
| 37 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 38 dict->Set("headers", SpdyHeaderBlockToListValue(*headers, capture_mode)); | |
| 39 return std::move(dict); | |
| 40 } | |
| 41 | |
| 42 std::unique_ptr<base::Value> NetLogCallback(const GURL* url, | |
| 43 const std::string* method, | |
| 44 const HttpRequestHeaders* headers, | |
| 45 NetLogCaptureMode capture_mode) { | |
| 46 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 47 dict->SetString("url", url->possibly_invalid_spec()); | |
| 48 dict->SetString("method", *method); | |
| 49 std::string empty; | |
| 50 std::unique_ptr<base::Value> headers_param( | |
| 51 headers->NetLogCallback(&empty, capture_mode)); | |
| 52 dict->Set("headers", std::move(headers_param)); | |
| 53 return std::move(dict); | |
| 54 } | |
| 55 | |
| 56 } // namespace | |
| 57 | |
| 29 BidirectionalStream::Delegate::Delegate() {} | 58 BidirectionalStream::Delegate::Delegate() {} |
| 30 | 59 |
| 31 void BidirectionalStream::Delegate::OnStreamReady() {} | 60 void BidirectionalStream::Delegate::OnStreamReady() {} |
| 32 | 61 |
| 33 BidirectionalStream::Delegate::~Delegate() {} | 62 BidirectionalStream::Delegate::~Delegate() {} |
| 34 | 63 |
| 35 BidirectionalStream::BidirectionalStream( | 64 BidirectionalStream::BidirectionalStream( |
| 36 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, | 65 std::unique_ptr<BidirectionalStreamRequestInfo> request_info, |
| 37 HttpNetworkSession* session, | 66 HttpNetworkSession* session, |
| 38 bool disable_auto_flush, | 67 bool disable_auto_flush, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 http_request_info.extra_headers = request_info_->extra_headers; | 107 http_request_info.extra_headers = request_info_->extra_headers; |
| 79 stream_request_.reset( | 108 stream_request_.reset( |
| 80 session->http_stream_factory()->RequestBidirectionalStreamImpl( | 109 session->http_stream_factory()->RequestBidirectionalStreamImpl( |
| 81 http_request_info, request_info_->priority, server_ssl_config, | 110 http_request_info, request_info_->priority, server_ssl_config, |
| 82 server_ssl_config, this, net_log_)); | 111 server_ssl_config, this, net_log_)); |
| 83 // Check that this call cannot fail to set a non-NULL |stream_request_|. | 112 // Check that this call cannot fail to set a non-NULL |stream_request_|. |
| 84 DCHECK(stream_request_); | 113 DCHECK(stream_request_); |
| 85 // Check that HttpStreamFactory does not invoke OnBidirectionalStreamImplReady | 114 // Check that HttpStreamFactory does not invoke OnBidirectionalStreamImplReady |
| 86 // synchronously. | 115 // synchronously. |
| 87 DCHECK(!stream_impl_); | 116 DCHECK(!stream_impl_); |
| 117 if (net_log_.IsCapturing()) { | |
| 118 net_log_.BeginEvent( | |
| 119 NetLog::TYPE_BIDIRECTIONAL_STREAM_ALIVE, | |
| 120 base::Bind(&NetLogCallback, &request_info_->url, &request_info_->method, | |
| 121 base::Unretained(&request_info_->extra_headers))); | |
| 122 } | |
| 88 } | 123 } |
| 89 | 124 |
| 90 BidirectionalStream::~BidirectionalStream() { | 125 BidirectionalStream::~BidirectionalStream() { |
| 91 Cancel(); | 126 Cancel(); |
| 127 net_log_.EndEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_ALIVE); | |
|
mef
2016/04/28 20:43:18
Should this also be guarded by net_log_.IsCapturin
xunjieli
2016/04/28 21:06:15
Done. good catch! thanks
| |
| 92 } | 128 } |
| 93 | 129 |
| 94 int BidirectionalStream::ReadData(IOBuffer* buf, int buf_len) { | 130 int BidirectionalStream::ReadData(IOBuffer* buf, int buf_len) { |
| 95 DCHECK(stream_impl_); | 131 DCHECK(stream_impl_); |
| 96 | 132 |
| 97 int rv = stream_impl_->ReadData(buf, buf_len); | 133 int rv = stream_impl_->ReadData(buf, buf_len); |
| 98 if (rv > 0) { | 134 if (rv > 0) { |
| 99 net_log_.AddByteTransferEvent( | 135 net_log_.AddByteTransferEvent( |
| 100 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, rv, buf->data()); | 136 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, rv, buf->data()); |
| 101 } else if (rv == ERR_IO_PENDING) { | 137 } else if (rv == ERR_IO_PENDING) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 } | 202 } |
| 167 | 203 |
| 168 void BidirectionalStream::OnHeadersReceived( | 204 void BidirectionalStream::OnHeadersReceived( |
| 169 const SpdyHeaderBlock& response_headers) { | 205 const SpdyHeaderBlock& response_headers) { |
| 170 HttpResponseInfo response_info; | 206 HttpResponseInfo response_info; |
| 171 if (!SpdyHeadersToHttpResponse(response_headers, HTTP2, &response_info)) { | 207 if (!SpdyHeadersToHttpResponse(response_headers, HTTP2, &response_info)) { |
| 172 DLOG(WARNING) << "Invalid headers"; | 208 DLOG(WARNING) << "Invalid headers"; |
| 173 delegate_->OnFailed(ERR_FAILED); | 209 delegate_->OnFailed(ERR_FAILED); |
| 174 return; | 210 return; |
| 175 } | 211 } |
| 176 | 212 if (net_log_.IsCapturing()) { |
| 213 net_log_.AddEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_RECV_HEADERS, | |
| 214 base::Bind(&NetLogHeadersCallback, &response_headers)); | |
| 215 } | |
| 177 session_->http_stream_factory()->ProcessAlternativeServices( | 216 session_->http_stream_factory()->ProcessAlternativeServices( |
| 178 session_, response_info.headers.get(), | 217 session_, response_info.headers.get(), |
| 179 url::SchemeHostPort(request_info_->url)); | 218 url::SchemeHostPort(request_info_->url)); |
| 180 delegate_->OnHeadersReceived(response_headers); | 219 delegate_->OnHeadersReceived(response_headers); |
| 181 } | 220 } |
| 182 | 221 |
| 183 void BidirectionalStream::OnDataRead(int bytes_read) { | 222 void BidirectionalStream::OnDataRead(int bytes_read) { |
| 184 DCHECK(read_buffer_); | 223 DCHECK(read_buffer_); |
| 185 | 224 |
| 186 net_log_.AddByteTransferEvent( | 225 if (net_log_.IsCapturing()) { |
| 187 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, bytes_read, | 226 net_log_.AddByteTransferEvent( |
| 188 read_buffer_->data()); | 227 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_RECEIVED, bytes_read, |
| 228 read_buffer_->data()); | |
| 229 } | |
| 189 read_buffer_ = nullptr; | 230 read_buffer_ = nullptr; |
| 190 delegate_->OnDataRead(bytes_read); | 231 delegate_->OnDataRead(bytes_read); |
| 191 } | 232 } |
| 192 | 233 |
| 193 void BidirectionalStream::OnDataSent() { | 234 void BidirectionalStream::OnDataSent() { |
| 194 DCHECK(!write_buffer_list_.empty()); | 235 DCHECK(!write_buffer_list_.empty()); |
| 195 DCHECK_EQ(write_buffer_list_.size(), write_buffer_len_list_.size()); | 236 DCHECK_EQ(write_buffer_list_.size(), write_buffer_len_list_.size()); |
| 196 | 237 |
| 197 for (size_t i = 0; i < write_buffer_list_.size(); ++i) { | 238 if (net_log_.IsCapturing()) { |
| 198 net_log_.AddByteTransferEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_SENT, | 239 for (size_t i = 0; i < write_buffer_list_.size(); ++i) { |
| 199 write_buffer_len_list_[i], | 240 net_log_.AddByteTransferEvent( |
| 200 write_buffer_list_[i]->data()); | 241 NetLog::TYPE_BIDIRECTIONAL_STREAM_BYTES_SENT, |
| 242 write_buffer_len_list_[i], write_buffer_list_[i]->data()); | |
| 243 } | |
| 201 } | 244 } |
| 202 write_buffer_list_.clear(); | 245 write_buffer_list_.clear(); |
| 203 write_buffer_len_list_.clear(); | 246 write_buffer_len_list_.clear(); |
| 204 delegate_->OnDataSent(); | 247 delegate_->OnDataSent(); |
| 205 } | 248 } |
| 206 | 249 |
| 207 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { | 250 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { |
| 251 if (net_log_.IsCapturing()) { | |
| 252 net_log_.AddEvent(NetLog::TYPE_BIDIRECTIONAL_STREAM_RECV_TRAILERS, | |
| 253 base::Bind(&NetLogHeadersCallback, &trailers)); | |
| 254 } | |
| 208 delegate_->OnTrailersReceived(trailers); | 255 delegate_->OnTrailersReceived(trailers); |
| 209 } | 256 } |
| 210 | 257 |
| 211 void BidirectionalStream::OnFailed(int status) { | 258 void BidirectionalStream::OnFailed(int status) { |
| 212 delegate_->OnFailed(status); | 259 delegate_->OnFailed(status); |
| 213 } | 260 } |
| 214 | 261 |
| 215 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, | 262 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, |
| 216 const ProxyInfo& used_proxy_info, | 263 const ProxyInfo& used_proxy_info, |
| 217 HttpStream* stream) { | 264 HttpStream* stream) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 const ProxyInfo& used_proxy_info, | 327 const ProxyInfo& used_proxy_info, |
| 281 HttpStream* stream) { | 328 HttpStream* stream) { |
| 282 DCHECK(stream_request_); | 329 DCHECK(stream_request_); |
| 283 | 330 |
| 284 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); | 331 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 285 } | 332 } |
| 286 | 333 |
| 287 void BidirectionalStream::OnQuicBroken() {} | 334 void BidirectionalStream::OnQuicBroken() {} |
| 288 | 335 |
| 289 } // namespace net | 336 } // namespace net |
| OLD | NEW |