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