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::BidirectionalStream( |
| 25 const BidirectionalStreamRequestInfo& request_info, |
| 26 RequestPriority priority, |
| 27 HttpNetworkSession* session, |
| 28 Delegate* delegate) |
| 29 : BidirectionalStream(request_info, |
| 30 priority, |
| 31 session, |
| 32 delegate, |
| 33 make_scoped_ptr(new base::Timer(false, false))) {} |
| 34 |
| 35 BidirectionalStream::BidirectionalStream( |
| 36 const BidirectionalStreamRequestInfo& request_info, |
| 37 RequestPriority priority, |
| 38 HttpNetworkSession* session, |
| 39 Delegate* delegate, |
| 40 scoped_ptr<base::Timer> timer) |
| 41 : request_info_(request_info), |
| 42 priority_(priority), |
| 43 net_log_(BoundNetLog::Make(session->net_log(), |
| 44 NetLog::SOURCE_BIDIRECTIONAL_STREAM)), |
| 45 delegate_(delegate), |
| 46 timer_(timer.release()) { |
| 47 DCHECK(delegate_); |
| 48 SSLConfig server_ssl_config; |
| 49 session->ssl_config_service()->GetSSLConfig(&server_ssl_config); |
| 50 session->GetAlpnProtos(&server_ssl_config.alpn_protos); |
| 51 session->GetNpnProtos(&server_ssl_config.npn_protos); |
| 52 |
| 53 if (!request_info.url.SchemeIsCryptographic()) { |
| 54 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 55 FROM_HERE, base::Bind(&Delegate::OnFailed, base::Unretained(delegate_), |
| 56 ERR_DISALLOWED_URL_SCHEME)); |
| 57 return; |
| 58 } |
| 59 |
| 60 HttpRequestInfo http_request_info; |
| 61 http_request_info.url = request_info.url; |
| 62 http_request_info.method = request_info.method; |
| 63 http_request_info.extra_headers = request_info.extra_headers; |
| 64 stream_request_.reset( |
| 65 session->http_stream_factory()->RequestBidirectionalStreamJob( |
| 66 http_request_info, priority_, server_ssl_config, server_ssl_config, |
| 67 this, net_log_)); |
| 68 // Check that this call cannot fail to set a non-NULL |stream_request_|. |
| 69 DCHECK(stream_request_); |
| 70 // Check that HttpStreamFactory does not invoke OnBidirectionalStreamJobReady |
| 71 // synchronously. |
| 72 DCHECK(!stream_job_); |
| 73 } |
| 74 |
| 75 BidirectionalStream::~BidirectionalStream() { |
| 76 Cancel(); |
| 77 } |
| 78 |
| 79 int BidirectionalStream::ReadData(IOBuffer* buf, int buf_len) { |
| 80 DCHECK(stream_job_); |
| 81 |
| 82 return stream_job_->ReadData(buf, buf_len); |
| 83 } |
| 84 |
| 85 void BidirectionalStream::SendData(IOBuffer* data, |
| 86 int length, |
| 87 bool end_stream) { |
| 88 DCHECK(stream_job_); |
| 89 |
| 90 stream_job_->SendData(data, length, end_stream); |
| 91 } |
| 92 |
| 93 void BidirectionalStream::Cancel() { |
| 94 if (stream_request_) |
| 95 stream_request_.reset(); |
| 96 if (stream_job_) |
| 97 stream_job_->Cancel(); |
| 98 } |
| 99 |
| 100 NextProto BidirectionalStream::GetProtocol() const { |
| 101 if (!stream_job_) |
| 102 return kProtoUnknown; |
| 103 |
| 104 return stream_job_->GetProtocol(); |
| 105 } |
| 106 |
| 107 int64_t BidirectionalStream::GetTotalReceivedBytes() const { |
| 108 if (!stream_job_) |
| 109 return 0; |
| 110 |
| 111 return stream_job_->GetTotalReceivedBytes(); |
| 112 } |
| 113 |
| 114 int64_t BidirectionalStream::GetTotalSentBytes() const { |
| 115 DCHECK(stream_job_); |
| 116 |
| 117 return stream_job_->GetTotalSentBytes(); |
| 118 } |
| 119 |
| 120 void BidirectionalStream::OnHeadersSent() { |
| 121 delegate_->OnHeadersSent(); |
| 122 } |
| 123 |
| 124 void BidirectionalStream::OnHeadersReceived( |
| 125 const SpdyHeaderBlock& response_headers) { |
| 126 delegate_->OnHeadersReceived(response_headers); |
| 127 } |
| 128 |
| 129 void BidirectionalStream::OnDataRead(int bytes_read) { |
| 130 delegate_->OnDataRead(bytes_read); |
| 131 } |
| 132 |
| 133 void BidirectionalStream::OnDataSent() { |
| 134 delegate_->OnDataSent(); |
| 135 } |
| 136 |
| 137 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { |
| 138 delegate_->OnTrailersReceived(trailers); |
| 139 } |
| 140 |
| 141 void BidirectionalStream::OnFailed(int status) { |
| 142 delegate_->OnFailed(status); |
| 143 } |
| 144 |
| 145 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, |
| 146 const ProxyInfo& used_proxy_info, |
| 147 HttpStream* stream) { |
| 148 NOTREACHED(); |
| 149 } |
| 150 |
| 151 void BidirectionalStream::OnBidirectionalStreamJobReady( |
| 152 const SSLConfig& used_ssl_config, |
| 153 const ProxyInfo& used_proxy_info, |
| 154 BidirectionalStreamJob* stream) { |
| 155 DCHECK(!stream_job_); |
| 156 |
| 157 stream_job_.reset(stream); |
| 158 stream_request_.reset(); |
| 159 stream_job_->Start(request_info_, priority_, net_log_, this, timer_.Pass()); |
| 160 } |
| 161 |
| 162 void BidirectionalStream::OnWebSocketHandshakeStreamReady( |
| 163 const SSLConfig& used_ssl_config, |
| 164 const ProxyInfo& used_proxy_info, |
| 165 WebSocketHandshakeStreamBase* stream) { |
| 166 NOTREACHED(); |
| 167 } |
| 168 |
| 169 void BidirectionalStream::OnStreamFailed(int result, |
| 170 const SSLConfig& used_ssl_config, |
| 171 SSLFailureState ssl_failure_state) { |
| 172 DCHECK_LT(result, 0); |
| 173 DCHECK_NE(result, ERR_IO_PENDING); |
| 174 DCHECK(stream_request_); |
| 175 |
| 176 delegate_->OnFailed(static_cast<Error>(result)); |
| 177 } |
| 178 |
| 179 void BidirectionalStream::OnCertificateError(int result, |
| 180 const SSLConfig& used_ssl_config, |
| 181 const SSLInfo& ssl_info) { |
| 182 DCHECK_LT(result, 0); |
| 183 DCHECK_NE(result, ERR_IO_PENDING); |
| 184 DCHECK(stream_request_); |
| 185 |
| 186 delegate_->OnFailed(static_cast<Error>(result)); |
| 187 } |
| 188 |
| 189 void BidirectionalStream::OnNeedsProxyAuth( |
| 190 const HttpResponseInfo& proxy_response, |
| 191 const SSLConfig& used_ssl_config, |
| 192 const ProxyInfo& used_proxy_info, |
| 193 HttpAuthController* auth_controller) { |
| 194 DCHECK(stream_request_); |
| 195 |
| 196 delegate_->OnFailed(ERR_PROXY_AUTH_REQUESTED); |
| 197 } |
| 198 |
| 199 void BidirectionalStream::OnNeedsClientAuth(const SSLConfig& used_ssl_config, |
| 200 SSLCertRequestInfo* cert_info) { |
| 201 DCHECK(stream_request_); |
| 202 |
| 203 delegate_->OnFailed(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 204 } |
| 205 |
| 206 void BidirectionalStream::OnHttpsProxyTunnelResponse( |
| 207 const HttpResponseInfo& response_info, |
| 208 const SSLConfig& used_ssl_config, |
| 209 const ProxyInfo& used_proxy_info, |
| 210 HttpStream* stream) { |
| 211 DCHECK(stream_request_); |
| 212 |
| 213 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 214 } |
| 215 |
| 216 void BidirectionalStream::OnQuicBroken() {} |
| 217 |
| 218 } // namespace net |
OLD | NEW |