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 "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
14 #include "net/http/http_network_session.h"
15 #include "net/socket/client_socket_handle.h"
16 #include "net/socket/client_socket_pool_manager.h"
17 #include "net/url_request/url_request_context.h"
18 #include "net/url_request/url_request_context_getter.h"
20 namespace jingle_glue
{
22 ProxyResolvingClientSocket::ProxyResolvingClientSocket(
23 net::ClientSocketFactory
* socket_factory
,
24 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
25 const net::SSLConfig
& ssl_config
,
26 const net::HostPortPair
& dest_host_port_pair
)
27 : proxy_resolve_callback_(
28 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
29 base::Unretained(this))),
31 base::Bind(&ProxyResolvingClientSocket::ProcessConnectDone
,
32 base::Unretained(this))),
33 ssl_config_(ssl_config
),
35 dest_host_port_pair_(dest_host_port_pair
),
36 // Assume that we intend to do TLS on this socket; all
37 // current use cases do.
38 proxy_url_("https://" + dest_host_port_pair_
.ToString()),
39 tried_direct_connect_fallback_(false),
41 net::BoundNetLog::Make(
42 request_context_getter
->GetURLRequestContext()->net_log(),
43 net::NetLog::SOURCE_SOCKET
)),
45 DCHECK(request_context_getter
.get());
46 net::URLRequestContext
* request_context
=
47 request_context_getter
->GetURLRequestContext();
48 DCHECK(request_context
);
49 DCHECK(!dest_host_port_pair_
.host().empty());
50 DCHECK_GT(dest_host_port_pair_
.port(), 0);
51 DCHECK(proxy_url_
.is_valid());
53 net::HttpNetworkSession::Params session_params
;
54 session_params
.client_socket_factory
= socket_factory
;
55 session_params
.host_resolver
= request_context
->host_resolver();
56 session_params
.cert_verifier
= request_context
->cert_verifier();
57 session_params
.transport_security_state
=
58 request_context
->transport_security_state();
59 // TODO(rkn): This is NULL because ServerBoundCertService is not thread safe.
60 session_params
.server_bound_cert_service
= NULL
;
61 session_params
.proxy_service
= request_context
->proxy_service();
62 session_params
.ssl_config_service
= request_context
->ssl_config_service();
63 session_params
.http_auth_handler_factory
=
64 request_context
->http_auth_handler_factory();
65 session_params
.network_delegate
= request_context
->network_delegate();
66 session_params
.http_server_properties
=
67 request_context
->http_server_properties();
68 session_params
.net_log
= request_context
->net_log();
70 const net::HttpNetworkSession::Params
* reference_params
=
71 request_context
->GetNetworkSessionParams();
72 if (reference_params
) {
73 session_params
.host_mapping_rules
= reference_params
->host_mapping_rules
;
74 session_params
.ignore_certificate_errors
=
75 reference_params
->ignore_certificate_errors
;
76 session_params
.http_pipelining_enabled
=
77 reference_params
->http_pipelining_enabled
;
78 session_params
.testing_fixed_http_port
=
79 reference_params
->testing_fixed_http_port
;
80 session_params
.testing_fixed_https_port
=
81 reference_params
->testing_fixed_https_port
;
82 session_params
.trusted_spdy_proxy
= reference_params
->trusted_spdy_proxy
;
85 network_session_
= new net::HttpNetworkSession(session_params
);
88 ProxyResolvingClientSocket::~ProxyResolvingClientSocket() {
92 int ProxyResolvingClientSocket::Read(net::IOBuffer
* buf
, int buf_len
,
93 const net::CompletionCallback
& callback
) {
94 if (transport_
.get() && transport_
->socket())
95 return transport_
->socket()->Read(buf
, buf_len
, callback
);
97 return net::ERR_SOCKET_NOT_CONNECTED
;
100 int ProxyResolvingClientSocket::Write(
103 const net::CompletionCallback
& callback
) {
104 if (transport_
.get() && transport_
->socket())
105 return transport_
->socket()->Write(buf
, buf_len
, callback
);
107 return net::ERR_SOCKET_NOT_CONNECTED
;
110 int ProxyResolvingClientSocket::SetReceiveBufferSize(int32 size
) {
111 if (transport_
.get() && transport_
->socket())
112 return transport_
->socket()->SetReceiveBufferSize(size
);
114 return net::ERR_SOCKET_NOT_CONNECTED
;
117 int ProxyResolvingClientSocket::SetSendBufferSize(int32 size
) {
118 if (transport_
.get() && transport_
->socket())
119 return transport_
->socket()->SetSendBufferSize(size
);
121 return net::ERR_SOCKET_NOT_CONNECTED
;
124 int ProxyResolvingClientSocket::Connect(
125 const net::CompletionCallback
& callback
) {
126 DCHECK(user_connect_callback_
.is_null());
128 tried_direct_connect_fallback_
= false;
130 // First we try and resolve the proxy.
131 int status
= network_session_
->proxy_service()->ResolveProxy(
134 proxy_resolve_callback_
,
137 if (status
!= net::ERR_IO_PENDING
) {
138 // We defer execution of ProcessProxyResolveDone instead of calling it
139 // directly here for simplicity. From the caller's point of view,
140 // the connect always happens asynchronously.
141 base::MessageLoop
* message_loop
= base::MessageLoop::current();
143 message_loop
->PostTask(
145 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
146 weak_factory_
.GetWeakPtr(), status
));
148 user_connect_callback_
= callback
;
149 return net::ERR_IO_PENDING
;
152 void ProxyResolvingClientSocket::RunUserConnectCallback(int status
) {
153 DCHECK_LE(status
, net::OK
);
154 net::CompletionCallback user_connect_callback
= user_connect_callback_
;
155 user_connect_callback_
.Reset();
156 user_connect_callback
.Run(status
);
159 // Always runs asynchronously.
160 void ProxyResolvingClientSocket::ProcessProxyResolveDone(int status
) {
163 DCHECK_NE(status
, net::ERR_IO_PENDING
);
164 if (status
== net::OK
) {
165 // Remove unsupported proxies from the list.
166 proxy_info_
.RemoveProxiesWithoutScheme(
167 net::ProxyServer::SCHEME_DIRECT
|
168 net::ProxyServer::SCHEME_HTTP
| net::ProxyServer::SCHEME_HTTPS
|
169 net::ProxyServer::SCHEME_SOCKS4
| net::ProxyServer::SCHEME_SOCKS5
);
171 if (proxy_info_
.is_empty()) {
172 // No proxies/direct to choose from. This happens when we don't support
173 // any of the proxies in the returned list.
174 status
= net::ERR_NO_SUPPORTED_PROXIES
;
178 // Since we are faking the URL, it is possible that no proxies match our URL.
179 // Try falling back to a direct connection if we have not tried that before.
180 if (status
!= net::OK
) {
181 if (!tried_direct_connect_fallback_
) {
182 tried_direct_connect_fallback_
= true;
183 proxy_info_
.UseDirect();
185 CloseTransportSocket();
186 RunUserConnectCallback(status
);
191 transport_
.reset(new net::ClientSocketHandle
);
192 // Now that we have resolved the proxy, we need to connect.
193 status
= net::InitSocketHandleForRawConnect(
194 dest_host_port_pair_
, network_session_
.get(), proxy_info_
, ssl_config_
,
195 ssl_config_
, net::PRIVACY_MODE_DISABLED
, bound_net_log_
, transport_
.get(),
197 if (status
!= net::ERR_IO_PENDING
) {
198 // Since this method is always called asynchronously. it is OK to call
199 // ProcessConnectDone synchronously.
200 ProcessConnectDone(status
);
204 void ProxyResolvingClientSocket::ProcessConnectDone(int status
) {
205 if (status
!= net::OK
) {
206 // If the connection fails, try another proxy.
207 status
= ReconsiderProxyAfterError(status
);
208 // ReconsiderProxyAfterError either returns an error (in which case it is
209 // not reconsidering a proxy) or returns ERR_IO_PENDING if it is considering
211 DCHECK_NE(status
, net::OK
);
212 if (status
== net::ERR_IO_PENDING
)
213 // Proxy reconsideration pending. Return.
215 CloseTransportSocket();
217 ReportSuccessfulProxyConnection();
219 RunUserConnectCallback(status
);
222 // TODO(sanjeevr): This has largely been copied from
223 // HttpStreamFactoryImpl::Job::ReconsiderProxyAfterError. This should be
224 // refactored into some common place.
225 // This method reconsiders the proxy on certain errors. If it does reconsider
226 // a proxy it always returns ERR_IO_PENDING and posts a call to
227 // ProcessProxyResolveDone with the result of the reconsideration.
228 int ProxyResolvingClientSocket::ReconsiderProxyAfterError(int error
) {
229 DCHECK(!pac_request_
);
230 DCHECK_NE(error
, net::OK
);
231 DCHECK_NE(error
, net::ERR_IO_PENDING
);
232 // A failure to resolve the hostname or any error related to establishing a
233 // TCP connection could be grounds for trying a new proxy configuration.
235 // Why do this when a hostname cannot be resolved? Some URLs only make sense
236 // to proxy servers. The hostname in those URLs might fail to resolve if we
237 // are still using a non-proxy config. We need to check if a proxy config
238 // now exists that corresponds to a proxy server that could load the URL.
241 case net::ERR_PROXY_CONNECTION_FAILED
:
242 case net::ERR_NAME_NOT_RESOLVED
:
243 case net::ERR_INTERNET_DISCONNECTED
:
244 case net::ERR_ADDRESS_UNREACHABLE
:
245 case net::ERR_CONNECTION_CLOSED
:
246 case net::ERR_CONNECTION_RESET
:
247 case net::ERR_CONNECTION_REFUSED
:
248 case net::ERR_CONNECTION_ABORTED
:
249 case net::ERR_TIMED_OUT
:
250 case net::ERR_TUNNEL_CONNECTION_FAILED
:
251 case net::ERR_SOCKS_CONNECTION_FAILED
:
253 case net::ERR_SOCKS_CONNECTION_HOST_UNREACHABLE
:
254 // Remap the SOCKS-specific "host unreachable" error to a more
255 // generic error code (this way consumers like the link doctor
256 // know to substitute their error page).
258 // Note that if the host resolving was done by the SOCSK5 proxy, we can't
259 // differentiate between a proxy-side "host not found" versus a proxy-side
260 // "address unreachable" error, and will report both of these failures as
261 // ERR_ADDRESS_UNREACHABLE.
262 return net::ERR_ADDRESS_UNREACHABLE
;
267 if (proxy_info_
.is_https() && ssl_config_
.send_client_cert
) {
268 network_session_
->ssl_client_auth_cache()->Remove(
269 proxy_info_
.proxy_server().host_port_pair());
272 int rv
= network_session_
->proxy_service()->ReconsiderProxyAfterError(
273 proxy_url_
, &proxy_info_
, proxy_resolve_callback_
, &pac_request_
,
275 if (rv
== net::OK
|| rv
== net::ERR_IO_PENDING
) {
276 CloseTransportSocket();
278 // If ReconsiderProxyAfterError() failed synchronously, it means
279 // there was nothing left to fall-back to, so fail the transaction
280 // with the last connection error we got.
284 // We either have new proxy info or there was an error in falling back.
285 // In both cases we want to post ProcessProxyResolveDone (in the error case
286 // we might still want to fall back a direct connection).
287 if (rv
!= net::ERR_IO_PENDING
) {
288 base::MessageLoop
* message_loop
= base::MessageLoop::current();
290 message_loop
->PostTask(
292 base::Bind(&ProxyResolvingClientSocket::ProcessProxyResolveDone
,
293 weak_factory_
.GetWeakPtr(), rv
));
294 // Since we potentially have another try to go (trying the direct connect)
295 // set the return code code to ERR_IO_PENDING.
296 rv
= net::ERR_IO_PENDING
;
301 void ProxyResolvingClientSocket::ReportSuccessfulProxyConnection() {
302 network_session_
->proxy_service()->ReportSuccess(proxy_info_
);
305 void ProxyResolvingClientSocket::Disconnect() {
306 CloseTransportSocket();
308 network_session_
->proxy_service()->CancelPacRequest(pac_request_
);
311 user_connect_callback_
.Reset();
314 bool ProxyResolvingClientSocket::IsConnected() const {
315 if (!transport_
.get() || !transport_
->socket())
317 return transport_
->socket()->IsConnected();
320 bool ProxyResolvingClientSocket::IsConnectedAndIdle() const {
321 if (!transport_
.get() || !transport_
->socket())
323 return transport_
->socket()->IsConnectedAndIdle();
326 int ProxyResolvingClientSocket::GetPeerAddress(
327 net::IPEndPoint
* address
) const {
328 if (transport_
.get() && transport_
->socket())
329 return transport_
->socket()->GetPeerAddress(address
);
331 return net::ERR_SOCKET_NOT_CONNECTED
;
334 int ProxyResolvingClientSocket::GetLocalAddress(
335 net::IPEndPoint
* address
) const {
336 if (transport_
.get() && transport_
->socket())
337 return transport_
->socket()->GetLocalAddress(address
);
339 return net::ERR_SOCKET_NOT_CONNECTED
;
342 const net::BoundNetLog
& ProxyResolvingClientSocket::NetLog() const {
343 if (transport_
.get() && transport_
->socket())
344 return transport_
->socket()->NetLog();
346 return bound_net_log_
;
349 void ProxyResolvingClientSocket::SetSubresourceSpeculation() {
350 if (transport_
.get() && transport_
->socket())
351 transport_
->socket()->SetSubresourceSpeculation();
356 void ProxyResolvingClientSocket::SetOmniboxSpeculation() {
357 if (transport_
.get() && transport_
->socket())
358 transport_
->socket()->SetOmniboxSpeculation();
363 bool ProxyResolvingClientSocket::WasEverUsed() const {
364 if (transport_
.get() && transport_
->socket())
365 return transport_
->socket()->WasEverUsed();
370 bool ProxyResolvingClientSocket::UsingTCPFastOpen() const {
371 if (transport_
.get() && transport_
->socket())
372 return transport_
->socket()->UsingTCPFastOpen();
377 bool ProxyResolvingClientSocket::WasNpnNegotiated() const {
381 net::NextProto
ProxyResolvingClientSocket::GetNegotiatedProtocol() const {
382 if (transport_
.get() && transport_
->socket())
383 return transport_
->socket()->GetNegotiatedProtocol();
385 return net::kProtoUnknown
;
388 bool ProxyResolvingClientSocket::GetSSLInfo(net::SSLInfo
* ssl_info
) {
392 void ProxyResolvingClientSocket::CloseTransportSocket() {
393 if (transport_
.get() && transport_
->socket())
394 transport_
->socket()->Disconnect();
398 } // namespace jingle_glue