| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "jingle/notifier/base/proxy_resolving_client_socket.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" |
| 10 #include "googleurl/src/gurl.h" |
| 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_network_session.h" |
| 14 #include "net/socket/client_socket_handle.h" |
| 15 #include "net/socket/client_socket_pool_manager.h" |
| 16 #include "net/url_request/url_request_context.h" |
| 17 #include "net/url_request/url_request_context_getter.h" |
| 18 |
| 19 namespace notifier { |
| 20 |
| 21 ProxyResolvingClientSocket::ProxyResolvingClientSocket( |
| 22 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
| 23 const net::SSLConfig& ssl_config, |
| 24 const net::HostPortPair& dest_host_port_pair, |
| 25 net::NetLog* net_log) |
| 26 : proxy_resolve_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 27 &ProxyResolvingClientSocket::ProcessProxyResolveDone), |
| 28 connect_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 29 &ProxyResolvingClientSocket::ProcessConnectDone), |
| 30 ssl_config_(ssl_config), |
| 31 pac_request_(NULL), |
| 32 dest_host_port_pair_(dest_host_port_pair), |
| 33 tried_direct_connect_fallback_(false), |
| 34 bound_net_log_( |
| 35 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), |
| 36 scoped_runnable_method_factory_( |
| 37 ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 38 user_connect_callback_(NULL) { |
| 39 DCHECK(request_context_getter); |
| 40 net::URLRequestContext* request_context = |
| 41 request_context_getter->GetURLRequestContext(); |
| 42 DCHECK(request_context); |
| 43 net::HttpNetworkSession::Params session_params; |
| 44 session_params.host_resolver = request_context->host_resolver(); |
| 45 session_params.cert_verifier = request_context->cert_verifier(); |
| 46 session_params.dnsrr_resolver = request_context->dnsrr_resolver(); |
| 47 session_params.proxy_service = request_context->proxy_service(); |
| 48 session_params.ssl_config_service = request_context->ssl_config_service(); |
| 49 session_params.http_auth_handler_factory = |
| 50 request_context->http_auth_handler_factory(); |
| 51 network_session_ = new net::HttpNetworkSession(session_params); |
| 52 } |
| 53 |
| 54 ProxyResolvingClientSocket::~ProxyResolvingClientSocket() { |
| 55 Disconnect(); |
| 56 } |
| 57 |
| 58 int ProxyResolvingClientSocket::Read(net::IOBuffer* buf, int buf_len, |
| 59 net::CompletionCallback* callback) { |
| 60 if (transport_.get() && transport_->socket()) |
| 61 return transport_->socket()->Read(buf, buf_len, callback); |
| 62 NOTREACHED(); |
| 63 return net::ERR_SOCKET_NOT_CONNECTED; |
| 64 } |
| 65 |
| 66 int ProxyResolvingClientSocket::Write(net::IOBuffer* buf, int buf_len, |
| 67 net::CompletionCallback* callback) { |
| 68 if (transport_.get() && transport_->socket()) |
| 69 return transport_->socket()->Write(buf, buf_len, callback); |
| 70 NOTREACHED(); |
| 71 return net::ERR_SOCKET_NOT_CONNECTED; |
| 72 } |
| 73 |
| 74 bool ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size) { |
| 75 if (transport_.get() && transport_->socket()) |
| 76 return transport_->socket()->SetReceiveBufferSize(size); |
| 77 NOTREACHED(); |
| 78 return false; |
| 79 } |
| 80 |
| 81 bool ProxyResolvingClientSocket::SetSendBufferSize(int32 size) { |
| 82 if (transport_.get() && transport_->socket()) |
| 83 return transport_->socket()->SetSendBufferSize(size); |
| 84 NOTREACHED(); |
| 85 return false; |
| 86 } |
| 87 |
| 88 int ProxyResolvingClientSocket::Connect(net::CompletionCallback* callback) { |
| 89 DCHECK(!user_connect_callback_); |
| 90 |
| 91 tried_direct_connect_fallback_ = false; |
| 92 |
| 93 // First we try and resolve the proxy. |
| 94 GURL url("http://" + dest_host_port_pair_.ToString()); |
| 95 int status = network_session_->proxy_service()->ResolveProxy( |
| 96 url, |
| 97 &proxy_info_, |
| 98 &proxy_resolve_callback_, |
| 99 &pac_request_, |
| 100 bound_net_log_); |
| 101 if (status != net::ERR_IO_PENDING) { |
| 102 // We defer execution of ProcessProxyResolveDone instead of calling it |
| 103 // directly here for simplicity. From the caller's point of view, |
| 104 // the connect always happens asynchronously. |
| 105 MessageLoop* message_loop = MessageLoop::current(); |
| 106 CHECK(message_loop); |
| 107 message_loop->PostTask( |
| 108 FROM_HERE, |
| 109 scoped_runnable_method_factory_.NewRunnableMethod( |
| 110 &ProxyResolvingClientSocket::ProcessProxyResolveDone, status)); |
| 111 } |
| 112 user_connect_callback_ = callback; |
| 113 return net::ERR_IO_PENDING; |
| 114 } |
| 115 |
| 116 void ProxyResolvingClientSocket::RunUserConnectCallback(int status) { |
| 117 DCHECK_LE(status, net::OK); |
| 118 net::CompletionCallback* user_connect_callback = user_connect_callback_; |
| 119 user_connect_callback_ = NULL; |
| 120 user_connect_callback->Run(status); |
| 121 } |
| 122 |
| 123 // Always runs asynchronously. |
| 124 void ProxyResolvingClientSocket::ProcessProxyResolveDone(int status) { |
| 125 pac_request_ = NULL; |
| 126 |
| 127 DCHECK_NE(status, net::ERR_IO_PENDING); |
| 128 if (status == net::OK) { |
| 129 // Remove unsupported proxies from the list. |
| 130 proxy_info_.RemoveProxiesWithoutScheme( |
| 131 net::ProxyServer::SCHEME_DIRECT | |
| 132 net::ProxyServer::SCHEME_HTTP | net::ProxyServer::SCHEME_HTTPS | |
| 133 net::ProxyServer::SCHEME_SOCKS4 | net::ProxyServer::SCHEME_SOCKS5); |
| 134 |
| 135 if (proxy_info_.is_empty()) { |
| 136 // No proxies/direct to choose from. This happens when we don't support |
| 137 // any of the proxies in the returned list. |
| 138 status = net::ERR_NO_SUPPORTED_PROXIES; |
| 139 } |
| 140 } |
| 141 |
| 142 // Since we are faking the URL, it is possible that no proxies match our URL. |
| 143 // Try falling back to a direct connection if we have not tried that before. |
| 144 if (status != net::OK) { |
| 145 if (!tried_direct_connect_fallback_) { |
| 146 tried_direct_connect_fallback_ = true; |
| 147 proxy_info_.UseDirect(); |
| 148 } else { |
| 149 CloseTransportSocket(); |
| 150 RunUserConnectCallback(status); |
| 151 return; |
| 152 } |
| 153 } |
| 154 |
| 155 transport_.reset(new net::ClientSocketHandle); |
| 156 // Now that we have resolved the proxy, we need to connect. |
| 157 status = net::ClientSocketPoolManager::InitSocketHandleForRawConnect( |
| 158 dest_host_port_pair_, |
| 159 network_session_.get(), |
| 160 proxy_info_, |
| 161 ssl_config_, |
| 162 ssl_config_, |
| 163 bound_net_log_, |
| 164 transport_.get(), |
| 165 &connect_callback_); |
| 166 if (status != net::ERR_IO_PENDING) { |
| 167 // Since this method is always called asynchronously. it is OK to call |
| 168 // ProcessConnectDone synchronously. |
| 169 ProcessConnectDone(status); |
| 170 } |
| 171 } |
| 172 |
| 173 void ProxyResolvingClientSocket::ProcessConnectDone(int status) { |
| 174 if (status != net::OK) { |
| 175 // If the connection fails, try another proxy. |
| 176 status = ReconsiderProxyAfterError(status); |
| 177 // ReconsiderProxyAfterError either returns an error (in which case it is |
| 178 // not reconsidering a proxy) or returns ERR_IO_PENDING if it is considering |
| 179 // another proxy. |
| 180 DCHECK_NE(status, net::OK); |
| 181 if (status == net::ERR_IO_PENDING) |
| 182 // Proxy reconsideration pending. Return. |
| 183 return; |
| 184 CloseTransportSocket(); |
| 185 } |
| 186 RunUserConnectCallback(status); |
| 187 } |
| 188 |
| 189 // TODO(sanjeevr): This has largely been copied from |
| 190 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be |
| 191 // refactored into some common place. |
| 192 // This method reconsiders the proxy on certain errors. If it does reconsider |
| 193 // a proxy it always returns ERR_IO_PENDING and posts a call to |
| 194 // ProcessProxyResolveDone with the result of the reconsideration. |
| 195 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error) { |
| 196 DCHECK(!pac_request_); |
| 197 DCHECK_NE(error, net::OK); |
| 198 DCHECK_NE(error, net::ERR_IO_PENDING); |
| 199 // A failure to resolve the hostname or any error related to establishing a |
| 200 // TCP connection could be grounds for trying a new proxy configuration. |
| 201 // |
| 202 // Why do this when a hostname cannot be resolved? Some URLs only make sense |
| 203 // to proxy servers. The hostname in those URLs might fail to resolve if we |
| 204 // are still using a non-proxy config. We need to check if a proxy config |
| 205 // now exists that corresponds to a proxy server that could load the URL. |
| 206 // |
| 207 switch (error) { |
| 208 case net::ERR_PROXY_CONNECTION_FAILED: |
| 209 case net::ERR_NAME_NOT_RESOLVED: |
| 210 case net::ERR_INTERNET_DISCONNECTED: |
| 211 case net::ERR_ADDRESS_UNREACHABLE: |
| 212 case net::ERR_CONNECTION_CLOSED: |
| 213 case net::ERR_CONNECTION_RESET: |
| 214 case net::ERR_CONNECTION_REFUSED: |
| 215 case net::ERR_CONNECTION_ABORTED: |
| 216 case net::ERR_TIMED_OUT: |
| 217 case net::ERR_TUNNEL_CONNECTION_FAILED: |
| 218 case net::ERR_SOCKS_CONNECTION_FAILED: |
| 219 break; |
| 220 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE: |
| 221 // Remap the SOCKS-specific "host unreachable" error to a more |
| 222 // generic error code (this way consumers like the link doctor |
| 223 // know to substitute their error page). |
| 224 // |
| 225 // Note that if the host resolving was done by the SOCSK5 proxy, we can't |
| 226 // differentiate between a proxy-side "host not found" versus a proxy-side |
| 227 // "address unreachable" error, and will report both of these failures as |
| 228 // ERR_ADDRESS_UNREACHABLE. |
| 229 return net::ERR_ADDRESS_UNREACHABLE; |
| 230 default: |
| 231 return error; |
| 232 } |
| 233 |
| 234 if (proxy_info_.is_https() && ssl_config_.send_client_cert) { |
| 235 network_session_->ssl_client_auth_cache()->Remove( |
| 236 proxy_info_.proxy_server().host_port_pair().ToString()); |
| 237 } |
| 238 |
| 239 GURL url("http://" + dest_host_port_pair_.ToString()); |
| 240 int rv = network_session_->proxy_service()->ReconsiderProxyAfterError( |
| 241 url, &proxy_info_, &proxy_resolve_callback_, &pac_request_, |
| 242 bound_net_log_); |
| 243 if (rv == net::OK || rv == net::ERR_IO_PENDING) { |
| 244 CloseTransportSocket(); |
| 245 } else { |
| 246 // If ReconsiderProxyAfterError() failed synchronously, it means |
| 247 // there was nothing left to fall-back to, so fail the transaction |
| 248 // with the last connection error we got. |
| 249 rv = error; |
| 250 } |
| 251 |
| 252 // We either have new proxy info or there was an error in falling back. |
| 253 // In both cases we want to post ProcessProxyResolveDone (in the error case |
| 254 // we might still want to fall back a direct connection). |
| 255 if (rv != net::ERR_IO_PENDING) { |
| 256 MessageLoop* message_loop = MessageLoop::current(); |
| 257 CHECK(message_loop); |
| 258 message_loop->PostTask( |
| 259 FROM_HERE, |
| 260 scoped_runnable_method_factory_.NewRunnableMethod( |
| 261 &ProxyResolvingClientSocket::ProcessProxyResolveDone, rv)); |
| 262 // Since we potentially have another try to go (trying the direct connect) |
| 263 // set the return code code to ERR_IO_PENDING. |
| 264 rv = net::ERR_IO_PENDING; |
| 265 } |
| 266 return rv; |
| 267 } |
| 268 |
| 269 void ProxyResolvingClientSocket::Disconnect() { |
| 270 CloseTransportSocket(); |
| 271 if (pac_request_) |
| 272 network_session_->proxy_service()->CancelPacRequest(pac_request_); |
| 273 user_connect_callback_ = NULL; |
| 274 } |
| 275 |
| 276 bool ProxyResolvingClientSocket::IsConnected() const { |
| 277 if (!transport_.get() || !transport_->socket()) |
| 278 return false; |
| 279 return transport_->socket()->IsConnected(); |
| 280 } |
| 281 |
| 282 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const { |
| 283 if (!transport_.get() || !transport_->socket()) |
| 284 return false; |
| 285 return transport_->socket()->IsConnectedAndIdle(); |
| 286 } |
| 287 |
| 288 int ProxyResolvingClientSocket::GetPeerAddress( |
| 289 net::AddressList* address) const { |
| 290 if (transport_.get() && transport_->socket()) |
| 291 return transport_->socket()->GetPeerAddress(address); |
| 292 NOTREACHED(); |
| 293 return net::ERR_SOCKET_NOT_CONNECTED; |
| 294 } |
| 295 |
| 296 const net::BoundNetLog& ProxyResolvingClientSocket::NetLog() const { |
| 297 if (transport_.get() && transport_->socket()) |
| 298 return transport_->socket()->NetLog(); |
| 299 NOTREACHED(); |
| 300 return bound_net_log_; |
| 301 } |
| 302 |
| 303 void ProxyResolvingClientSocket::SetSubresourceSpeculation() { |
| 304 if (transport_.get() && transport_->socket()) |
| 305 transport_->socket()->SetSubresourceSpeculation(); |
| 306 else |
| 307 NOTREACHED(); |
| 308 } |
| 309 |
| 310 void ProxyResolvingClientSocket::SetOmniboxSpeculation() { |
| 311 if (transport_.get() && transport_->socket()) |
| 312 transport_->socket()->SetOmniboxSpeculation(); |
| 313 else |
| 314 NOTREACHED(); |
| 315 } |
| 316 |
| 317 bool ProxyResolvingClientSocket::WasEverUsed() const { |
| 318 if (transport_.get() && transport_->socket()) |
| 319 return transport_->socket()->WasEverUsed(); |
| 320 NOTREACHED(); |
| 321 return false; |
| 322 } |
| 323 |
| 324 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const { |
| 325 if (transport_.get() && transport_->socket()) |
| 326 return transport_->socket()->UsingTCPFastOpen(); |
| 327 NOTREACHED(); |
| 328 return false; |
| 329 } |
| 330 |
| 331 void ProxyResolvingClientSocket::CloseTransportSocket() { |
| 332 if (transport_.get() && transport_->socket()) |
| 333 transport_->socket()->Disconnect(); |
| 334 transport_.reset(); |
| 335 } |
| 336 |
| 337 } // namespace notifier |
| OLD | NEW |