Chromium Code Reviews
|
| 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) { | |
|
akalin
2011/04/15 17:41:05
indentation
sanjeevr
2011/04/15 18:04:33
Done.
| |
| 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 = GURL("http://" + dest_host_port_pair_.ToString()); | |
|
akalin
2011/04/15 17:41:05
GURL url("http://" + ...);
sanjeevr
2011/04/15 18:04:33
Oops, Done.
| |
| 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(status != net::OK); | |
|
akalin
2011/04/15 17:41:05
DCHECK_NE
sanjeevr
2011/04/15 18:04:33
Done.
| |
| 181 if (status == net::ERR_IO_PENDING) { | |
| 182 // Proxy reconsideration pending. Return. | |
| 183 return; | |
| 184 } else { | |
|
akalin
2011/04/15 17:41:05
no need for 'else' since you're returning
sanjeevr
2011/04/15 18:04:33
Done.
| |
| 185 CloseTransportSocket(); | |
| 186 } | |
| 187 } | |
| 188 RunUserConnectCallback(status); | |
| 189 } | |
| 190 | |
| 191 // TODO(sanjeevr): This has largely been copied from | |
| 192 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be | |
| 193 // refactored into some common place. | |
| 194 // This method reconsiders the proxy on certain errors. If it does reconsider | |
| 195 // a proxy it always returns ERR_IO_PENDING and posts a call to | |
| 196 // ProcessProxyResolveDone with the result of the reconsideration. | |
| 197 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error) { | |
| 198 DCHECK(!pac_request_); | |
| 199 DCHECK((error != net::OK) && (error != net::ERR_IO_PENDING)); | |
|
akalin
2011/04/15 17:41:05
split into two DCHECK_NEs
sanjeevr
2011/04/15 18:04:33
Done.
| |
| 200 // A failure to resolve the hostname or any error related to establishing a | |
| 201 // TCP connection could be grounds for trying a new proxy configuration. | |
| 202 // | |
| 203 // Why do this when a hostname cannot be resolved? Some URLs only make sense | |
| 204 // to proxy servers. The hostname in those URLs might fail to resolve if we | |
| 205 // are still using a non-proxy config. We need to check if a proxy config | |
| 206 // now exists that corresponds to a proxy server that could load the URL. | |
| 207 // | |
| 208 switch (error) { | |
| 209 case net::ERR_PROXY_CONNECTION_FAILED: | |
| 210 case net::ERR_NAME_NOT_RESOLVED: | |
| 211 case net::ERR_INTERNET_DISCONNECTED: | |
| 212 case net::ERR_ADDRESS_UNREACHABLE: | |
| 213 case net::ERR_CONNECTION_CLOSED: | |
| 214 case net::ERR_CONNECTION_RESET: | |
| 215 case net::ERR_CONNECTION_REFUSED: | |
| 216 case net::ERR_CONNECTION_ABORTED: | |
| 217 case net::ERR_TIMED_OUT: | |
| 218 case net::ERR_TUNNEL_CONNECTION_FAILED: | |
| 219 case net::ERR_SOCKS_CONNECTION_FAILED: | |
| 220 break; | |
| 221 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE: | |
| 222 // Remap the SOCKS-specific "host unreachable" error to a more | |
| 223 // generic error code (this way consumers like the link doctor | |
| 224 // know to substitute their error page). | |
| 225 // | |
| 226 // Note that if the host resolving was done by the SOCSK5 proxy, we can't | |
| 227 // differentiate between a proxy-side "host not found" versus a proxy-side | |
| 228 // "address unreachable" error, and will report both of these failures as | |
| 229 // ERR_ADDRESS_UNREACHABLE. | |
| 230 return net::ERR_ADDRESS_UNREACHABLE; | |
| 231 default: | |
| 232 return error; | |
| 233 } | |
| 234 | |
| 235 if (proxy_info_.is_https() && ssl_config_.send_client_cert) { | |
| 236 network_session_->ssl_client_auth_cache()->Remove( | |
| 237 proxy_info_.proxy_server().host_port_pair().ToString()); | |
| 238 } | |
| 239 | |
| 240 GURL url = GURL("http://" + dest_host_port_pair_.ToString()); | |
|
akalin
2011/04/15 17:41:05
GURL url("http://" + ...
sanjeevr
2011/04/15 18:04:33
Done.
| |
| 241 int rv = network_session_->proxy_service()->ReconsiderProxyAfterError( | |
| 242 url, &proxy_info_, &proxy_resolve_callback_, &pac_request_, | |
| 243 bound_net_log_); | |
| 244 if (rv == net::OK || rv == net::ERR_IO_PENDING) { | |
| 245 CloseTransportSocket(); | |
| 246 } else { | |
| 247 // If ReconsiderProxyAfterError() failed synchronously, it means | |
| 248 // there was nothing left to fall-back to, so fail the transaction | |
| 249 // with the last connection error we got. | |
| 250 rv = error; | |
| 251 } | |
| 252 | |
| 253 // We either have new proxy info or there was an error in falling back. | |
| 254 // In both cases we want to post ProcessProxyResolveDone (in the error case | |
| 255 // we might still want to fall back a direct connection). | |
| 256 if (rv != net::ERR_IO_PENDING) { | |
| 257 MessageLoop* message_loop = MessageLoop::current(); | |
| 258 CHECK(message_loop); | |
| 259 message_loop->PostTask( | |
| 260 FROM_HERE, | |
| 261 scoped_runnable_method_factory_.NewRunnableMethod( | |
| 262 &ProxyResolvingClientSocket::ProcessProxyResolveDone, rv)); | |
| 263 // Since we potentially have another try to go (trying the direct connect) | |
| 264 // set the return code code to ERR_IO_PENDING. | |
| 265 rv = net::ERR_IO_PENDING; | |
| 266 } | |
| 267 return rv; | |
| 268 } | |
| 269 | |
| 270 void ProxyResolvingClientSocket::Disconnect() { | |
| 271 CloseTransportSocket(); | |
| 272 if (pac_request_) | |
| 273 network_session_->proxy_service()->CancelPacRequest(pac_request_); | |
| 274 user_connect_callback_ = NULL; | |
| 275 } | |
| 276 | |
| 277 bool ProxyResolvingClientSocket::IsConnected() const { | |
| 278 if (!transport_.get() || !transport_->socket()) | |
| 279 return false; | |
| 280 return transport_->socket()->IsConnected(); | |
| 281 } | |
| 282 | |
| 283 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const { | |
| 284 if (!transport_.get() || !transport_->socket()) | |
| 285 return false; | |
| 286 return transport_->socket()->IsConnectedAndIdle(); | |
| 287 } | |
| 288 | |
| 289 int ProxyResolvingClientSocket::GetPeerAddress( | |
| 290 net::AddressList* address) const { | |
| 291 if (transport_.get() && transport_->socket()) | |
| 292 return transport_->socket()->GetPeerAddress(address); | |
| 293 NOTREACHED(); | |
| 294 return net::ERR_SOCKET_NOT_CONNECTED; | |
| 295 } | |
| 296 | |
| 297 const net::BoundNetLog& ProxyResolvingClientSocket::NetLog() const { | |
| 298 if (transport_.get() && transport_->socket()) | |
| 299 return transport_->socket()->NetLog(); | |
| 300 NOTREACHED(); | |
| 301 return bound_net_log_; | |
| 302 } | |
| 303 | |
| 304 void ProxyResolvingClientSocket::SetSubresourceSpeculation() { | |
| 305 if (transport_.get() && transport_->socket()) | |
| 306 transport_->socket()->SetSubresourceSpeculation(); | |
| 307 else | |
| 308 NOTREACHED(); | |
| 309 } | |
| 310 | |
| 311 void ProxyResolvingClientSocket::SetOmniboxSpeculation() { | |
| 312 if (transport_.get() && transport_->socket()) | |
| 313 transport_->socket()->SetOmniboxSpeculation(); | |
| 314 else | |
| 315 NOTREACHED(); | |
| 316 } | |
| 317 | |
| 318 bool ProxyResolvingClientSocket::WasEverUsed() const { | |
| 319 if (transport_.get() && transport_->socket()) | |
| 320 return transport_->socket()->WasEverUsed(); | |
| 321 NOTREACHED(); | |
| 322 return false; | |
| 323 } | |
| 324 | |
| 325 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const { | |
| 326 if (transport_.get() && transport_->socket()) | |
| 327 return transport_->socket()->UsingTCPFastOpen(); | |
| 328 NOTREACHED(); | |
| 329 return false; | |
| 330 } | |
| 331 | |
| 332 void ProxyResolvingClientSocket::CloseTransportSocket() { | |
| 333 if (transport_.get() && transport_->socket()) | |
| 334 transport_->socket()->Disconnect(); | |
| 335 transport_.reset(); | |
| 336 } | |
| 337 | |
| 338 } // namespace notifier | |
| OLD | NEW |