Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/bidirectional_stream.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "net/base/load_flags.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/http/http_network_session.h" | |
| 17 #include "net/http/http_stream.h" | |
| 18 #include "net/log/net_log.h" | |
| 19 #include "net/ssl/ssl_cert_request_info.h" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace net { | |
| 23 | |
| 24 BidirectionalStream::Delegate::Delegate() {} | |
| 25 | |
| 26 BidirectionalStream::Delegate::~Delegate() {} | |
| 27 | |
| 28 BidirectionalStream::BidirectionalStream( | |
| 29 const BidirectionalStreamRequestInfo* request_info, | |
| 30 HttpNetworkSession* session, | |
| 31 Delegate* delegate) | |
| 32 : BidirectionalStream(request_info, | |
| 33 session, | |
| 34 delegate, | |
| 35 make_scoped_ptr(new base::Timer(false, false))) {} | |
| 36 | |
| 37 BidirectionalStream::BidirectionalStream( | |
| 38 const BidirectionalStreamRequestInfo* request_info, | |
| 39 HttpNetworkSession* session, | |
| 40 Delegate* delegate, | |
| 41 scoped_ptr<base::Timer> timer) | |
| 42 : request_info_(request_info), | |
|
mmenke
2015/12/16 23:04:36
Should either take this as a scoped_ptr and take o
xunjieli
2015/12/17 18:18:54
I was following SpdyHttpStream's example. It doesn
mmenke
2015/12/17 18:58:20
SpdyHttpStream is a little different - it's owned
xunjieli
2015/12/17 21:18:21
Done. I see. That makes sense!
| |
| 43 net_log_(BoundNetLog::Make(session->net_log(), | |
| 44 NetLog::SOURCE_BIDIRECTIONAL_STREAM)), | |
| 45 delegate_(delegate), | |
| 46 timer_(timer.release()) { | |
|
mmenke
2015/12/16 23:04:36
std::move
xunjieli
2015/12/17 18:18:54
Done.
| |
| 47 DCHECK(delegate_); | |
| 48 DCHECK(request_info_); | |
| 49 | |
| 50 SSLConfig server_ssl_config; | |
| 51 session->ssl_config_service()->GetSSLConfig(&server_ssl_config); | |
| 52 session->GetAlpnProtos(&server_ssl_config.alpn_protos); | |
| 53 session->GetNpnProtos(&server_ssl_config.npn_protos); | |
| 54 | |
| 55 if (!request_info->url.SchemeIsCryptographic()) { | |
|
mmenke
2015/12/16 23:04:36
I suggest just SchemeIs(url::kHttpsScheme). wss i
xunjieli
2015/12/17 18:18:54
Done.
| |
| 56 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 57 FROM_HERE, | |
| 58 base::Bind(&BidirectionalStream::Delegate::OnFailed, | |
| 59 base::Unretained(delegate_), ERR_DISALLOWED_URL_SCHEME)); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 HttpRequestInfo http_request_info; | |
| 64 http_request_info.url = request_info->url; | |
| 65 http_request_info.method = request_info->method; | |
| 66 http_request_info.extra_headers = request_info->extra_headers; | |
| 67 stream_request_.reset( | |
| 68 session->http_stream_factory()->RequestBidirectionalStreamJob( | |
| 69 http_request_info, request_info_->priority, server_ssl_config, | |
| 70 server_ssl_config, this, net_log_)); | |
| 71 // Check that this call cannot fail to set a non-NULL |stream_request_|. | |
| 72 DCHECK(stream_request_); | |
| 73 // Check that HttpStreamFactory does not invoke OnBidirectionalStreamJobReady | |
| 74 // synchronously. | |
| 75 DCHECK(!stream_job_); | |
| 76 } | |
| 77 | |
| 78 BidirectionalStream::~BidirectionalStream() { | |
| 79 Cancel(); | |
| 80 } | |
| 81 | |
| 82 int BidirectionalStream::ReadData(IOBuffer* buf, int buf_len) { | |
| 83 DCHECK(stream_job_); | |
| 84 | |
| 85 return stream_job_->ReadData(buf, buf_len); | |
| 86 } | |
| 87 | |
| 88 void BidirectionalStream::SendData(IOBuffer* data, | |
| 89 int length, | |
| 90 bool end_stream) { | |
| 91 DCHECK(stream_job_); | |
| 92 | |
| 93 stream_job_->SendData(data, length, end_stream); | |
| 94 } | |
| 95 | |
| 96 void BidirectionalStream::Cancel() { | |
| 97 if (stream_request_) | |
|
mmenke
2015/12/16 23:04:36
If not needed - reset() has the smarts to do nothi
xunjieli
2015/12/17 18:18:54
Done.
| |
| 98 stream_request_.reset(); | |
| 99 if (stream_job_) | |
| 100 stream_job_->Cancel(); | |
|
mmenke
2015/12/16 23:04:36
I assume we don't just reset it here so the caller
xunjieli
2015/12/17 18:18:54
Done. You are right. I think we should reset the j
| |
| 101 } | |
| 102 | |
| 103 NextProto BidirectionalStream::GetProtocol() const { | |
| 104 if (!stream_job_) | |
| 105 return kProtoUnknown; | |
| 106 | |
| 107 return stream_job_->GetProtocol(); | |
| 108 } | |
| 109 | |
| 110 int64_t BidirectionalStream::GetTotalReceivedBytes() const { | |
| 111 if (!stream_job_) | |
| 112 return 0; | |
| 113 | |
| 114 return stream_job_->GetTotalReceivedBytes(); | |
| 115 } | |
| 116 | |
| 117 int64_t BidirectionalStream::GetTotalSentBytes() const { | |
| 118 DCHECK(stream_job_); | |
| 119 | |
| 120 return stream_job_->GetTotalSentBytes(); | |
| 121 } | |
| 122 | |
| 123 void BidirectionalStream::OnHeadersSent() { | |
| 124 delegate_->OnHeadersSent(); | |
| 125 } | |
| 126 | |
| 127 void BidirectionalStream::OnHeadersReceived( | |
| 128 const SpdyHeaderBlock& response_headers) { | |
| 129 delegate_->OnHeadersReceived(response_headers); | |
| 130 } | |
| 131 | |
| 132 void BidirectionalStream::OnDataRead(int bytes_read) { | |
| 133 delegate_->OnDataRead(bytes_read); | |
| 134 } | |
| 135 | |
| 136 void BidirectionalStream::OnDataSent() { | |
| 137 delegate_->OnDataSent(); | |
| 138 } | |
| 139 | |
| 140 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { | |
| 141 delegate_->OnTrailersReceived(trailers); | |
| 142 } | |
| 143 | |
| 144 void BidirectionalStream::OnFailed(int status) { | |
| 145 delegate_->OnFailed(status); | |
| 146 } | |
| 147 | |
| 148 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, | |
| 149 const ProxyInfo& used_proxy_info, | |
| 150 HttpStream* stream) { | |
| 151 NOTREACHED(); | |
| 152 } | |
| 153 | |
| 154 void BidirectionalStream::OnBidirectionalStreamJobReady( | |
| 155 const SSLConfig& used_ssl_config, | |
| 156 const ProxyInfo& used_proxy_info, | |
| 157 BidirectionalStreamJob* stream) { | |
| 158 DCHECK(!stream_job_); | |
| 159 | |
| 160 stream_job_.reset(stream); | |
| 161 stream_request_.reset(); | |
|
mmenke
2015/12/16 23:04:36
nit: May want to move this up a line, before sett
xunjieli
2015/12/17 18:18:54
Done.
| |
| 162 stream_job_->Start(request_info_, net_log_, this, timer_.Pass()); | |
| 163 } | |
| 164 | |
| 165 void BidirectionalStream::OnWebSocketHandshakeStreamReady( | |
| 166 const SSLConfig& used_ssl_config, | |
| 167 const ProxyInfo& used_proxy_info, | |
| 168 WebSocketHandshakeStreamBase* stream) { | |
| 169 NOTREACHED(); | |
| 170 } | |
| 171 | |
| 172 void BidirectionalStream::OnStreamFailed(int result, | |
| 173 const SSLConfig& used_ssl_config, | |
| 174 SSLFailureState ssl_failure_state) { | |
| 175 DCHECK_LT(result, 0); | |
| 176 DCHECK_NE(result, ERR_IO_PENDING); | |
| 177 DCHECK(stream_request_); | |
| 178 | |
| 179 delegate_->OnFailed(static_cast<Error>(result)); | |
| 180 } | |
| 181 | |
| 182 void BidirectionalStream::OnCertificateError(int result, | |
| 183 const SSLConfig& used_ssl_config, | |
| 184 const SSLInfo& ssl_info) { | |
| 185 DCHECK_LT(result, 0); | |
| 186 DCHECK_NE(result, ERR_IO_PENDING); | |
| 187 DCHECK(stream_request_); | |
| 188 | |
| 189 delegate_->OnFailed(static_cast<Error>(result)); | |
| 190 } | |
| 191 | |
| 192 void BidirectionalStream::OnNeedsProxyAuth( | |
| 193 const HttpResponseInfo& proxy_response, | |
| 194 const SSLConfig& used_ssl_config, | |
| 195 const ProxyInfo& used_proxy_info, | |
| 196 HttpAuthController* auth_controller) { | |
| 197 DCHECK(stream_request_); | |
| 198 | |
| 199 delegate_->OnFailed(ERR_PROXY_AUTH_REQUESTED); | |
| 200 } | |
| 201 | |
| 202 void BidirectionalStream::OnNeedsClientAuth(const SSLConfig& used_ssl_config, | |
| 203 SSLCertRequestInfo* cert_info) { | |
| 204 DCHECK(stream_request_); | |
| 205 | |
| 206 delegate_->OnFailed(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); | |
| 207 } | |
| 208 | |
| 209 void BidirectionalStream::OnHttpsProxyTunnelResponse( | |
| 210 const HttpResponseInfo& response_info, | |
| 211 const SSLConfig& used_ssl_config, | |
| 212 const ProxyInfo& used_proxy_info, | |
| 213 HttpStream* stream) { | |
| 214 DCHECK(stream_request_); | |
| 215 | |
| 216 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); | |
| 217 } | |
| 218 | |
| 219 void BidirectionalStream::OnQuicBroken() {} | |
| 220 | |
| 221 } // namespace net | |
| OLD | NEW |