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 "net/socket/client_socket_pool_manager.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "net/base/load_flags.h"
13 #include "net/http/http_proxy_client_socket_pool.h"
14 #include "net/http/http_request_info.h"
15 #include "net/http/http_stream_factory.h"
16 #include "net/proxy/proxy_info.h"
17 #include "net/socket/client_socket_handle.h"
18 #include "net/socket/socks_client_socket_pool.h"
19 #include "net/socket/ssl_client_socket_pool.h"
20 #include "net/socket/transport_client_socket_pool.h"
26 // Limit of sockets of each socket pool.
27 int g_max_sockets_per_pool
[] = {
28 256, // NORMAL_SOCKET_POOL
29 256 // WEBSOCKET_SOCKET_POOL
32 static_assert(arraysize(g_max_sockets_per_pool
) ==
33 HttpNetworkSession::NUM_SOCKET_POOL_TYPES
,
34 "max sockets per pool length mismatch");
36 // Default to allow up to 6 connections per host. Experiment and tuning may
37 // try other values (greater than 0). Too large may cause many problems, such
38 // as home routers blocking the connections!?!? See http://crbug.com/12066.
40 // WebSocket connections are long-lived, and should be treated differently
41 // than normal other connections. 6 connections per group sounded too small
42 // for such use, thus we use a larger limit which was determined somewhat
44 // TODO(yutak): Look at the usage and determine the right value after
45 // WebSocket protocol stack starts to work.
46 int g_max_sockets_per_group
[] = {
47 6, // NORMAL_SOCKET_POOL
48 30 // WEBSOCKET_SOCKET_POOL
51 static_assert(arraysize(g_max_sockets_per_group
) ==
52 HttpNetworkSession::NUM_SOCKET_POOL_TYPES
,
53 "max sockets per group length mismatch");
55 // The max number of sockets to allow per proxy server. This applies both to
56 // http and SOCKS proxies. See http://crbug.com/12066 and
57 // http://crbug.com/44501 for details about proxy server connection limits.
58 int g_max_sockets_per_proxy_server
[] = {
59 kDefaultMaxSocketsPerProxyServer
, // NORMAL_SOCKET_POOL
60 kDefaultMaxSocketsPerProxyServer
// WEBSOCKET_SOCKET_POOL
63 static_assert(arraysize(g_max_sockets_per_proxy_server
) ==
64 HttpNetworkSession::NUM_SOCKET_POOL_TYPES
,
65 "max sockets per proxy server length mismatch");
67 // The meat of the implementation for the InitSocketHandleForHttpRequest,
68 // InitSocketHandleForRawConnect and PreconnectSocketsForHttpRequest methods.
69 int InitSocketPoolHelper(const GURL
& request_url
,
70 const HttpRequestHeaders
& request_extra_headers
,
71 int request_load_flags
,
72 RequestPriority request_priority
,
73 HttpNetworkSession
* session
,
74 const ProxyInfo
& proxy_info
,
75 bool force_spdy_over_ssl
,
76 bool want_spdy_over_npn
,
77 const SSLConfig
& ssl_config_for_origin
,
78 const SSLConfig
& ssl_config_for_proxy
,
80 PrivacyMode privacy_mode
,
81 const BoundNetLog
& net_log
,
82 int num_preconnect_streams
,
83 ClientSocketHandle
* socket_handle
,
84 HttpNetworkSession::SocketPoolType socket_pool_type
,
85 const OnHostResolutionCallback
& resolution_callback
,
86 const CompletionCallback
& callback
) {
87 scoped_refptr
<HttpProxySocketParams
> http_proxy_params
;
88 scoped_refptr
<SOCKSSocketParams
> socks_params
;
89 scoped_ptr
<HostPortPair
> proxy_host_port
;
91 bool using_ssl
= request_url
.SchemeIs("https") ||
92 request_url
.SchemeIs("wss") || force_spdy_over_ssl
;
94 HostPortPair origin_host_port
= HostPortPair::FromURL(request_url
);
96 if (!using_ssl
&& session
->params().testing_fixed_http_port
!= 0) {
97 origin_host_port
.set_port(session
->params().testing_fixed_http_port
);
98 } else if (using_ssl
&& session
->params().testing_fixed_https_port
!= 0) {
99 origin_host_port
.set_port(session
->params().testing_fixed_https_port
);
102 bool disable_resolver_cache
=
103 request_load_flags
& LOAD_BYPASS_CACHE
||
104 request_load_flags
& LOAD_VALIDATE_CACHE
||
105 request_load_flags
& LOAD_DISABLE_CACHE
;
107 int load_flags
= request_load_flags
;
108 if (session
->params().ignore_certificate_errors
)
109 load_flags
|= LOAD_IGNORE_ALL_CERT_ERRORS
;
111 // Build the string used to uniquely identify connections of this type.
112 // Determine the host and port to connect to.
113 std::string connection_group
= origin_host_port
.ToString();
114 DCHECK(!connection_group
.empty());
115 if (request_url
.SchemeIs("ftp")) {
116 // Combining FTP with forced SPDY over SSL would be a "path to madness".
117 // Make sure we never do that.
119 connection_group
= "ftp/" + connection_group
;
122 // All connections in a group should use the same SSLConfig settings.
123 // Encode version_max in the connection group's name, unless it's the
124 // default version_max. (We want the common case to use the shortest
125 // encoding). A version_max of TLS 1.1 is encoded as "ssl(max:3.2)/"
126 // rather than "tlsv1.1/" because the actual protocol version, which
127 // is selected by the server, may not be TLS 1.1. Do not encode
128 // version_min in the connection group's name because version_min
129 // should be the same for all connections, whereas version_max may
130 // change for version fallbacks.
131 std::string prefix
= "ssl/";
132 if (ssl_config_for_origin
.version_max
!=
133 SSLClientSocket::GetMaxSupportedSSLVersion()) {
134 switch (ssl_config_for_origin
.version_max
) {
135 case SSL_PROTOCOL_VERSION_TLS1_2
:
136 prefix
= "ssl(max:3.3)/";
138 case SSL_PROTOCOL_VERSION_TLS1_1
:
139 prefix
= "ssl(max:3.2)/";
141 case SSL_PROTOCOL_VERSION_TLS1
:
142 prefix
= "ssl(max:3.1)/";
144 case SSL_PROTOCOL_VERSION_SSL3
:
152 // Place sockets with and without deprecated ciphers into separate
153 // connection groups.
154 if (ssl_config_for_origin
.enable_deprecated_cipher_suites
)
155 prefix
+= "deprecatedciphers/";
156 connection_group
= prefix
+ connection_group
;
159 bool ignore_limits
= (request_load_flags
& LOAD_IGNORE_LIMITS
) != 0;
160 if (!proxy_info
.is_direct()) {
161 ProxyServer proxy_server
= proxy_info
.proxy_server();
162 proxy_host_port
.reset(new HostPortPair(proxy_server
.host_port_pair()));
163 scoped_refptr
<TransportSocketParams
> proxy_tcp_params(
164 new TransportSocketParams(
166 disable_resolver_cache
,
169 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
171 if (proxy_info
.is_http() || proxy_info
.is_https()) {
172 std::string user_agent
;
173 request_extra_headers
.GetHeader(HttpRequestHeaders::kUserAgent
,
175 scoped_refptr
<SSLSocketParams
> ssl_params
;
176 if (proxy_info
.is_https()) {
177 // Combine connect and write for SSL sockets in TCP FastOpen
179 TransportSocketParams::CombineConnectAndWritePolicy
180 combine_connect_and_write
=
181 session
->params().enable_tcp_fast_open_for_ssl
?
182 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
:
183 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
;
184 proxy_tcp_params
= new TransportSocketParams(*proxy_host_port
,
185 disable_resolver_cache
,
188 combine_connect_and_write
);
189 // Set ssl_params, and unset proxy_tcp_params
190 ssl_params
= new SSLSocketParams(proxy_tcp_params
,
193 *proxy_host_port
.get(),
194 ssl_config_for_proxy
,
195 PRIVACY_MODE_DISABLED
,
199 proxy_tcp_params
= NULL
;
203 new HttpProxySocketParams(proxy_tcp_params
,
207 session
->http_auth_cache(),
208 session
->http_auth_handler_factory(),
209 session
->spdy_session_pool(),
210 force_tunnel
|| using_ssl
,
211 session
->params().proxy_delegate
);
213 DCHECK(proxy_info
.is_socks());
215 if (proxy_server
.scheme() == ProxyServer::SCHEME_SOCKS5
)
219 connection_group
= base::StringPrintf(
220 "socks%c/%s", socks_version
, connection_group
.c_str());
222 socks_params
= new SOCKSSocketParams(proxy_tcp_params
,
223 socks_version
== '5',
228 // Change group name if privacy mode is enabled.
229 if (privacy_mode
== PRIVACY_MODE_ENABLED
)
230 connection_group
= "pm/" + connection_group
;
232 // Deal with SSL - which layers on top of any given proxy.
234 scoped_refptr
<TransportSocketParams
> ssl_tcp_params
;
235 if (proxy_info
.is_direct()) {
236 // Setup TCP params if non-proxied SSL connection.
237 // Combine connect and write for SSL sockets in TCP FastOpen field trial.
238 TransportSocketParams::CombineConnectAndWritePolicy
239 combine_connect_and_write
=
240 session
->params().enable_tcp_fast_open_for_ssl
?
241 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
:
242 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
;
243 ssl_tcp_params
= new TransportSocketParams(origin_host_port
,
244 disable_resolver_cache
,
247 combine_connect_and_write
);
249 scoped_refptr
<SSLSocketParams
> ssl_params
=
250 new SSLSocketParams(ssl_tcp_params
,
254 ssl_config_for_origin
,
259 SSLClientSocketPool
* ssl_pool
= NULL
;
260 if (proxy_info
.is_direct()) {
261 ssl_pool
= session
->GetSSLSocketPool(socket_pool_type
);
263 ssl_pool
= session
->GetSocketPoolForSSLWithProxy(socket_pool_type
,
267 if (num_preconnect_streams
) {
268 RequestSocketsForPool(ssl_pool
, connection_group
, ssl_params
,
269 num_preconnect_streams
, net_log
);
273 return socket_handle
->Init(connection_group
, ssl_params
,
274 request_priority
, callback
, ssl_pool
,
278 // Finally, get the connection started.
280 if (proxy_info
.is_http() || proxy_info
.is_https()) {
281 HttpProxyClientSocketPool
* pool
=
282 session
->GetSocketPoolForHTTPProxy(socket_pool_type
, *proxy_host_port
);
283 if (num_preconnect_streams
) {
284 RequestSocketsForPool(pool
, connection_group
, http_proxy_params
,
285 num_preconnect_streams
, net_log
);
289 return socket_handle
->Init(connection_group
, http_proxy_params
,
290 request_priority
, callback
,
294 if (proxy_info
.is_socks()) {
295 SOCKSClientSocketPool
* pool
=
296 session
->GetSocketPoolForSOCKSProxy(socket_pool_type
, *proxy_host_port
);
297 if (num_preconnect_streams
) {
298 RequestSocketsForPool(pool
, connection_group
, socks_params
,
299 num_preconnect_streams
, net_log
);
303 return socket_handle
->Init(connection_group
, socks_params
,
304 request_priority
, callback
, pool
,
308 DCHECK(proxy_info
.is_direct());
309 scoped_refptr
<TransportSocketParams
> tcp_params
=
310 new TransportSocketParams(
312 disable_resolver_cache
,
315 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
);
316 TransportClientSocketPool
* pool
=
317 session
->GetTransportSocketPool(socket_pool_type
);
318 if (num_preconnect_streams
) {
319 RequestSocketsForPool(pool
, connection_group
, tcp_params
,
320 num_preconnect_streams
, net_log
);
324 return socket_handle
->Init(connection_group
, tcp_params
,
325 request_priority
, callback
,
331 ClientSocketPoolManager::ClientSocketPoolManager() {}
332 ClientSocketPoolManager::~ClientSocketPoolManager() {}
335 int ClientSocketPoolManager::max_sockets_per_pool(
336 HttpNetworkSession::SocketPoolType pool_type
) {
337 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
338 return g_max_sockets_per_pool
[pool_type
];
342 void ClientSocketPoolManager::set_max_sockets_per_pool(
343 HttpNetworkSession::SocketPoolType pool_type
,
345 DCHECK_LT(0, socket_count
);
346 DCHECK_GT(1000, socket_count
); // Sanity check.
347 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
348 g_max_sockets_per_pool
[pool_type
] = socket_count
;
349 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
350 g_max_sockets_per_group
[pool_type
]);
354 int ClientSocketPoolManager::max_sockets_per_group(
355 HttpNetworkSession::SocketPoolType pool_type
) {
356 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
357 return g_max_sockets_per_group
[pool_type
];
361 void ClientSocketPoolManager::set_max_sockets_per_group(
362 HttpNetworkSession::SocketPoolType pool_type
,
364 DCHECK_LT(0, socket_count
);
365 // The following is a sanity check... but we should NEVER be near this value.
366 DCHECK_GT(100, socket_count
);
367 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
368 g_max_sockets_per_group
[pool_type
] = socket_count
;
370 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
371 g_max_sockets_per_group
[pool_type
]);
372 DCHECK_GE(g_max_sockets_per_proxy_server
[pool_type
],
373 g_max_sockets_per_group
[pool_type
]);
377 int ClientSocketPoolManager::max_sockets_per_proxy_server(
378 HttpNetworkSession::SocketPoolType pool_type
) {
379 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
380 return g_max_sockets_per_proxy_server
[pool_type
];
384 void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
385 HttpNetworkSession::SocketPoolType pool_type
,
387 DCHECK_LT(0, socket_count
);
388 DCHECK_GT(100, socket_count
); // Sanity check.
389 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
390 // Assert this case early on. The max number of sockets per group cannot
391 // exceed the max number of sockets per proxy server.
392 DCHECK_LE(g_max_sockets_per_group
[pool_type
], socket_count
);
393 g_max_sockets_per_proxy_server
[pool_type
] = socket_count
;
396 int InitSocketHandleForHttpRequest(
397 const GURL
& request_url
,
398 const HttpRequestHeaders
& request_extra_headers
,
399 int request_load_flags
,
400 RequestPriority request_priority
,
401 HttpNetworkSession
* session
,
402 const ProxyInfo
& proxy_info
,
403 bool force_spdy_over_ssl
,
404 bool want_spdy_over_npn
,
405 const SSLConfig
& ssl_config_for_origin
,
406 const SSLConfig
& ssl_config_for_proxy
,
407 PrivacyMode privacy_mode
,
408 const BoundNetLog
& net_log
,
409 ClientSocketHandle
* socket_handle
,
410 const OnHostResolutionCallback
& resolution_callback
,
411 const CompletionCallback
& callback
) {
412 DCHECK(socket_handle
);
413 return InitSocketPoolHelper(
414 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
415 session
, proxy_info
, force_spdy_over_ssl
, want_spdy_over_npn
,
416 ssl_config_for_origin
, ssl_config_for_proxy
, false, privacy_mode
, net_log
,
417 0, socket_handle
, HttpNetworkSession::NORMAL_SOCKET_POOL
,
418 resolution_callback
, callback
);
421 int InitSocketHandleForWebSocketRequest(
422 const GURL
& request_url
,
423 const HttpRequestHeaders
& request_extra_headers
,
424 int request_load_flags
,
425 RequestPriority request_priority
,
426 HttpNetworkSession
* session
,
427 const ProxyInfo
& proxy_info
,
428 bool force_spdy_over_ssl
,
429 bool want_spdy_over_npn
,
430 const SSLConfig
& ssl_config_for_origin
,
431 const SSLConfig
& ssl_config_for_proxy
,
432 PrivacyMode privacy_mode
,
433 const BoundNetLog
& net_log
,
434 ClientSocketHandle
* socket_handle
,
435 const OnHostResolutionCallback
& resolution_callback
,
436 const CompletionCallback
& callback
) {
437 DCHECK(socket_handle
);
438 return InitSocketPoolHelper(
439 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
440 session
, proxy_info
, force_spdy_over_ssl
, want_spdy_over_npn
,
441 ssl_config_for_origin
, ssl_config_for_proxy
, true, privacy_mode
, net_log
,
442 0, socket_handle
, HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
443 resolution_callback
, callback
);
446 int InitSocketHandleForRawConnect(
447 const HostPortPair
& host_port_pair
,
448 HttpNetworkSession
* session
,
449 const ProxyInfo
& proxy_info
,
450 const SSLConfig
& ssl_config_for_origin
,
451 const SSLConfig
& ssl_config_for_proxy
,
452 PrivacyMode privacy_mode
,
453 const BoundNetLog
& net_log
,
454 ClientSocketHandle
* socket_handle
,
455 const CompletionCallback
& callback
) {
456 DCHECK(socket_handle
);
457 // Synthesize an HttpRequestInfo.
458 GURL request_url
= GURL("http://" + host_port_pair
.ToString());
459 HttpRequestHeaders request_extra_headers
;
460 int request_load_flags
= 0;
461 RequestPriority request_priority
= MEDIUM
;
463 return InitSocketPoolHelper(
464 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
465 session
, proxy_info
, false, false, ssl_config_for_origin
,
466 ssl_config_for_proxy
, true, privacy_mode
, net_log
, 0, socket_handle
,
467 HttpNetworkSession::NORMAL_SOCKET_POOL
, OnHostResolutionCallback(),
471 int InitSocketHandleForTlsConnect(
472 const HostPortPair
& host_port_pair
,
473 HttpNetworkSession
* session
,
474 const ProxyInfo
& proxy_info
,
475 const SSLConfig
& ssl_config_for_origin
,
476 const SSLConfig
& ssl_config_for_proxy
,
477 PrivacyMode privacy_mode
,
478 const BoundNetLog
& net_log
,
479 ClientSocketHandle
* socket_handle
,
480 const CompletionCallback
& callback
) {
481 DCHECK(socket_handle
);
482 // Synthesize an HttpRequestInfo.
483 GURL request_url
= GURL("https://" + host_port_pair
.ToString());
484 HttpRequestHeaders request_extra_headers
;
485 int request_load_flags
= 0;
486 RequestPriority request_priority
= MEDIUM
;
488 return InitSocketPoolHelper(
489 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
490 session
, proxy_info
, false, false, ssl_config_for_origin
,
491 ssl_config_for_proxy
, true, privacy_mode
, net_log
, 0, socket_handle
,
492 HttpNetworkSession::NORMAL_SOCKET_POOL
, OnHostResolutionCallback(),
496 int PreconnectSocketsForHttpRequest(
497 const GURL
& request_url
,
498 const HttpRequestHeaders
& request_extra_headers
,
499 int request_load_flags
,
500 RequestPriority request_priority
,
501 HttpNetworkSession
* session
,
502 const ProxyInfo
& proxy_info
,
503 bool force_spdy_over_ssl
,
504 bool want_spdy_over_npn
,
505 const SSLConfig
& ssl_config_for_origin
,
506 const SSLConfig
& ssl_config_for_proxy
,
507 PrivacyMode privacy_mode
,
508 const BoundNetLog
& net_log
,
509 int num_preconnect_streams
) {
510 return InitSocketPoolHelper(
511 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
512 session
, proxy_info
, force_spdy_over_ssl
, want_spdy_over_npn
,
513 ssl_config_for_origin
, ssl_config_for_proxy
, false, privacy_mode
, net_log
,
514 num_preconnect_streams
, NULL
, HttpNetworkSession::NORMAL_SOCKET_POOL
,
515 OnHostResolutionCallback(), CompletionCallback());