1 // Copyright (c) 2012 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.
5 #include "jingle/glue/proxy_resolving_client_socket.h"
7 #include "base/basictypes.h"
9 #include "base/bind_helpers.h"
10 #include "base/compiler_specific.h"
11 #include "base/logging.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/load_flags.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_auth_controller.h"
17 #include "net/http/http_network_session.h"
18 #include "net/http/proxy_client_socket.h"
19 #include "net/socket/client_socket_handle.h"
20 #include "net/socket/client_socket_pool_manager.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
24 namespace jingle_glue
{
26 ProxyResolvingClientSocket::ProxyResolvingClientSocket(
27 net::ClientSocketFactory
* socket_factory
,
28 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
29 const net::SSLConfig
& ssl_config
,
30 const net::HostPortPair
& dest_host_port_pair
)
31 : proxy_resolve_callback_(
32 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
33 base::Unretained(this))),
35 base::Bind(&ProxyResolvingClientSocket::ProcessConnectDone
,
36 base::Unretained(this))),
37 ssl_config_(ssl_config
),
39 dest_host_port_pair_(dest_host_port_pair
),
40 // Assume that we intend to do TLS on this socket; all
41 // current use cases do.
42 proxy_url_("https://" + dest_host_port_pair_
.ToString()),
43 tried_direct_connect_fallback_(false),
45 net::BoundNetLog::Make(
46 request_context_getter
->GetURLRequestContext()->net_log(),
47 net::NetLog::SOURCE_SOCKET
)),
49 DCHECK(request_context_getter
.get());
50 net::URLRequestContext
* request_context
=
51 request_context_getter
->GetURLRequestContext();
52 DCHECK(request_context
);
53 DCHECK(!dest_host_port_pair_
.host().empty());
54 DCHECK_GT(dest_host_port_pair_
.port(), 0);
55 DCHECK(proxy_url_
.is_valid());
57 net::HttpNetworkSession::Params session_params
;
58 session_params
.client_socket_factory
= socket_factory
;
59 session_params
.host_resolver
= request_context
->host_resolver();
60 session_params
.cert_verifier
= request_context
->cert_verifier();
61 session_params
.transport_security_state
=
62 request_context
->transport_security_state();
63 // TODO(rkn): This is NULL because ChannelIDService is not thread safe.
64 session_params
.channel_id_service
= NULL
;
65 session_params
.proxy_service
= request_context
->proxy_service();
66 session_params
.ssl_config_service
= request_context
->ssl_config_service();
67 session_params
.http_auth_handler_factory
=
68 request_context
->http_auth_handler_factory();
69 session_params
.network_delegate
= request_context
->network_delegate();
70 session_params
.http_server_properties
=
71 request_context
->http_server_properties();
72 session_params
.net_log
= request_context
->net_log();
74 const net::HttpNetworkSession::Params
* reference_params
=
75 request_context
->GetNetworkSessionParams();
76 if (reference_params
) {
77 // TODO(mmenke): Just copying specific parameters seems highly regression
78 // prone. Should have a better way to do this.
79 session_params
.host_mapping_rules
= reference_params
->host_mapping_rules
;
80 session_params
.ignore_certificate_errors
=
81 reference_params
->ignore_certificate_errors
;
82 session_params
.testing_fixed_http_port
=
83 reference_params
->testing_fixed_http_port
;
84 session_params
.testing_fixed_https_port
=
85 reference_params
->testing_fixed_https_port
;
86 session_params
.next_protos
= reference_params
->next_protos
;
87 session_params
.trusted_spdy_proxy
= reference_params
->trusted_spdy_proxy
;
88 session_params
.forced_spdy_exclusions
=
89 reference_params
->forced_spdy_exclusions
;
90 session_params
.use_alternate_protocols
=
91 reference_params
->use_alternate_protocols
;
94 network_session_
= new net::HttpNetworkSession(session_params
);
97 ProxyResolvingClientSocket::~ProxyResolvingClientSocket() {
101 int ProxyResolvingClientSocket::Read(net::IOBuffer
* buf
, int buf_len
,
102 const net::CompletionCallback
& callback
) {
103 if (transport_
.get() && transport_
->socket())
104 return transport_
->socket()->Read(buf
, buf_len
, callback
);
106 return net::ERR_SOCKET_NOT_CONNECTED
;
109 int ProxyResolvingClientSocket::Write(
112 const net::CompletionCallback
& callback
) {
113 if (transport_
.get() && transport_
->socket())
114 return transport_
->socket()->Write(buf
, buf_len
, callback
);
116 return net::ERR_SOCKET_NOT_CONNECTED
;
119 int ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size
) {
120 if (transport_
.get() && transport_
->socket())
121 return transport_
->socket()->SetReceiveBufferSize(size
);
123 return net::ERR_SOCKET_NOT_CONNECTED
;
126 int ProxyResolvingClientSocket::SetSendBufferSize(int32 size
) {
127 if (transport_
.get() && transport_
->socket())
128 return transport_
->socket()->SetSendBufferSize(size
);
130 return net::ERR_SOCKET_NOT_CONNECTED
;
133 int ProxyResolvingClientSocket::Connect(
134 const net::CompletionCallback
& callback
) {
135 DCHECK(user_connect_callback_
.is_null());
137 tried_direct_connect_fallback_
= false;
139 // First we try and resolve the proxy.
140 int status
= network_session_
->proxy_service()->ResolveProxy(
144 proxy_resolve_callback_
,
148 if (status
!= net::ERR_IO_PENDING
) {
149 // We defer execution of ProcessProxyResolveDone instead of calling it
150 // directly here for simplicity. From the caller's point of view,
151 // the connect always happens asynchronously.
152 base::MessageLoop
* message_loop
= base::MessageLoop::current();
154 message_loop
->PostTask(
156 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
157 weak_factory_
.GetWeakPtr(), status
));
159 user_connect_callback_
= callback
;
160 return net::ERR_IO_PENDING
;
163 void ProxyResolvingClientSocket::RunUserConnectCallback(int status
) {
164 DCHECK_LE(status
, net::OK
);
165 net::CompletionCallback user_connect_callback
= user_connect_callback_
;
166 user_connect_callback_
.Reset();
167 user_connect_callback
.Run(status
);
170 // Always runs asynchronously.
171 void ProxyResolvingClientSocket::ProcessProxyResolveDone(int status
) {
174 DCHECK_NE(status
, net::ERR_IO_PENDING
);
175 if (status
== net::OK
) {
176 // Remove unsupported proxies from the list.
177 proxy_info_
.RemoveProxiesWithoutScheme(
178 net::ProxyServer::SCHEME_DIRECT
|
179 net::ProxyServer::SCHEME_HTTP
| net::ProxyServer::SCHEME_HTTPS
|
180 net::ProxyServer::SCHEME_SOCKS4
| net::ProxyServer::SCHEME_SOCKS5
);
182 if (proxy_info_
.is_empty()) {
183 // No proxies/direct to choose from. This happens when we don't support
184 // any of the proxies in the returned list.
185 status
= net::ERR_NO_SUPPORTED_PROXIES
;
189 // Since we are faking the URL, it is possible that no proxies match our URL.
190 // Try falling back to a direct connection if we have not tried that before.
191 if (status
!= net::OK
) {
192 if (!tried_direct_connect_fallback_
) {
193 tried_direct_connect_fallback_
= true;
194 proxy_info_
.UseDirect();
196 CloseTransportSocket();
197 RunUserConnectCallback(status
);
202 transport_
.reset(new net::ClientSocketHandle
);
203 // Now that we have resolved the proxy, we need to connect.
204 status
= net::InitSocketHandleForRawConnect(
205 dest_host_port_pair_
, network_session_
.get(), proxy_info_
, ssl_config_
,
206 ssl_config_
, net::PRIVACY_MODE_DISABLED
, bound_net_log_
, transport_
.get(),
208 if (status
!= net::ERR_IO_PENDING
) {
209 // Since this method is always called asynchronously. it is OK to call
210 // ProcessConnectDone synchronously.
211 ProcessConnectDone(status
);
215 void ProxyResolvingClientSocket::ProcessConnectDone(int status
) {
216 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455884 is fixed.
217 tracked_objects::ScopedTracker
tracking_profile(
218 FROM_HERE_WITH_EXPLICIT_FUNCTION(
219 "455884 ProxyResolvingClientSocket::OnConnectDone"));
220 if (status
!= net::OK
) {
221 // If the connection fails, try another proxy.
222 status
= ReconsiderProxyAfterError(status
);
223 // ReconsiderProxyAfterError either returns an error (in which case it is
224 // not reconsidering a proxy) or returns ERR_IO_PENDING if it is considering
226 DCHECK_NE(status
, net::OK
);
227 if (status
== net::ERR_IO_PENDING
)
228 // Proxy reconsideration pending. Return.
230 CloseTransportSocket();
232 ReportSuccessfulProxyConnection();
234 RunUserConnectCallback(status
);
237 // TODO(sanjeevr): This has largely been copied from
238 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be
239 // refactored into some common place.
240 // This method reconsiders the proxy on certain errors. If it does reconsider
241 // a proxy it always returns ERR_IO_PENDING and posts a call to
242 // ProcessProxyResolveDone with the result of the reconsideration.
243 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error
) {
244 DCHECK(!pac_request_
);
245 DCHECK_NE(error
, net::OK
);
246 DCHECK_NE(error
, net::ERR_IO_PENDING
);
247 // A failure to resolve the hostname or any error related to establishing a
248 // TCP connection could be grounds for trying a new proxy configuration.
250 // Why do this when a hostname cannot be resolved? Some URLs only make sense
251 // to proxy servers. The hostname in those URLs might fail to resolve if we
252 // are still using a non-proxy config. We need to check if a proxy config
253 // now exists that corresponds to a proxy server that could load the URL.
256 case net::ERR_PROXY_CONNECTION_FAILED
:
257 case net::ERR_NAME_NOT_RESOLVED
:
258 case net::ERR_INTERNET_DISCONNECTED
:
259 case net::ERR_ADDRESS_UNREACHABLE
:
260 case net::ERR_CONNECTION_CLOSED
:
261 case net::ERR_CONNECTION_RESET
:
262 case net::ERR_CONNECTION_REFUSED
:
263 case net::ERR_CONNECTION_ABORTED
:
264 case net::ERR_TIMED_OUT
:
265 case net::ERR_TUNNEL_CONNECTION_FAILED
:
266 case net::ERR_SOCKS_CONNECTION_FAILED
:
268 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE
:
269 // Remap the SOCKS-specific "host unreachable" error to a more
270 // generic error code (this way consumers like the link doctor
271 // know to substitute their error page).
273 // Note that if the host resolving was done by the SOCSK5 proxy, we can't
274 // differentiate between a proxy-side "host not found" versus a proxy-side
275 // "address unreachable" error, and will report both of these failures as
276 // ERR_ADDRESS_UNREACHABLE.
277 return net::ERR_ADDRESS_UNREACHABLE
;
278 case net::ERR_PROXY_AUTH_REQUESTED
: {
279 net::ProxyClientSocket
* proxy_socket
=
280 static_cast<net::ProxyClientSocket
*>(transport_
->socket());
282 if (proxy_socket
->GetAuthController()->HaveAuth())
283 return proxy_socket
->RestartWithAuth(connect_callback_
);
291 if (proxy_info_
.is_https() && ssl_config_
.send_client_cert
) {
292 network_session_
->ssl_client_auth_cache()->Remove(
293 proxy_info_
.proxy_server().host_port_pair());
296 int rv
= network_session_
->proxy_service()->ReconsiderProxyAfterError(
297 proxy_url_
, net::LOAD_NORMAL
, error
, &proxy_info_
,
298 proxy_resolve_callback_
, &pac_request_
, NULL
, bound_net_log_
);
299 if (rv
== net::OK
|| rv
== net::ERR_IO_PENDING
) {
300 CloseTransportSocket();
302 // If ReconsiderProxyAfterError() failed synchronously, it means
303 // there was nothing left to fall-back to, so fail the transaction
304 // with the last connection error we got.
308 // We either have new proxy info or there was an error in falling back.
309 // In both cases we want to post ProcessProxyResolveDone (in the error case
310 // we might still want to fall back a direct connection).
311 if (rv
!= net::ERR_IO_PENDING
) {
312 base::MessageLoop
* message_loop
= base::MessageLoop::current();
314 message_loop
->PostTask(
316 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
317 weak_factory_
.GetWeakPtr(), rv
));
318 // Since we potentially have another try to go (trying the direct connect)
319 // set the return code code to ERR_IO_PENDING.
320 rv
= net::ERR_IO_PENDING
;
325 void ProxyResolvingClientSocket::ReportSuccessfulProxyConnection() {
326 network_session_
->proxy_service()->ReportSuccess(proxy_info_
, NULL
);
329 void ProxyResolvingClientSocket::Disconnect() {
330 CloseTransportSocket();
332 network_session_
->proxy_service()->CancelPacRequest(pac_request_
);
335 user_connect_callback_
.Reset();
338 bool ProxyResolvingClientSocket::IsConnected() const {
339 if (!transport_
.get() || !transport_
->socket())
341 return transport_
->socket()->IsConnected();
344 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const {
345 if (!transport_
.get() || !transport_
->socket())
347 return transport_
->socket()->IsConnectedAndIdle();
350 int ProxyResolvingClientSocket::GetPeerAddress(
351 net::IPEndPoint
* address
) const {
352 if (!transport_
.get() || !transport_
->socket()) {
354 return net::ERR_SOCKET_NOT_CONNECTED
;
357 if (proxy_info_
.is_direct())
358 return transport_
->socket()->GetPeerAddress(address
);
360 net::IPAddressNumber ip_number
;
361 if (!net::ParseIPLiteralToNumber(dest_host_port_pair_
.host(), &ip_number
)) {
362 // Do not expose the proxy IP address to the caller.
363 return net::ERR_NAME_NOT_RESOLVED
;
366 *address
= net::IPEndPoint(ip_number
, dest_host_port_pair_
.port());
370 int ProxyResolvingClientSocket::GetLocalAddress(
371 net::IPEndPoint
* address
) const {
372 if (transport_
.get() && transport_
->socket())
373 return transport_
->socket()->GetLocalAddress(address
);
375 return net::ERR_SOCKET_NOT_CONNECTED
;
378 const net::BoundNetLog
& ProxyResolvingClientSocket::NetLog() const {
379 if (transport_
.get() && transport_
->socket())
380 return transport_
->socket()->NetLog();
382 return bound_net_log_
;
385 void ProxyResolvingClientSocket::SetSubresourceSpeculation() {
386 if (transport_
.get() && transport_
->socket())
387 transport_
->socket()->SetSubresourceSpeculation();
392 void ProxyResolvingClientSocket::SetOmniboxSpeculation() {
393 if (transport_
.get() && transport_
->socket())
394 transport_
->socket()->SetOmniboxSpeculation();
399 bool ProxyResolvingClientSocket::WasEverUsed() const {
400 if (transport_
.get() && transport_
->socket())
401 return transport_
->socket()->WasEverUsed();
406 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const {
407 if (transport_
.get() && transport_
->socket())
408 return transport_
->socket()->UsingTCPFastOpen();
413 bool ProxyResolvingClientSocket::WasNpnNegotiated() const {
417 net::NextProto
ProxyResolvingClientSocket::GetNegotiatedProtocol() const {
418 if (transport_
.get() && transport_
->socket())
419 return transport_
->socket()->GetNegotiatedProtocol();
421 return net::kProtoUnknown
;
424 bool ProxyResolvingClientSocket::GetSSLInfo(net::SSLInfo
* ssl_info
) {
428 void ProxyResolvingClientSocket::CloseTransportSocket() {
429 if (transport_
.get() && transport_
->socket())
430 transport_
->socket()->Disconnect();
434 } // namespace jingle_glue