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(ClientSocketPoolManager::SocketGroupType group_type
,
70 const HostPortPair
& endpoint
,
71 const HttpRequestHeaders
& request_extra_headers
,
72 int request_load_flags
,
73 RequestPriority request_priority
,
74 HttpNetworkSession
* session
,
75 const ProxyInfo
& proxy_info
,
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
= group_type
== ClientSocketPoolManager::SSL_GROUP
;
92 HostPortPair origin_host_port
= endpoint
;
94 if (!using_ssl
&& session
->params().testing_fixed_http_port
!= 0) {
95 origin_host_port
.set_port(session
->params().testing_fixed_http_port
);
96 } else if (using_ssl
&& session
->params().testing_fixed_https_port
!= 0) {
97 origin_host_port
.set_port(session
->params().testing_fixed_https_port
);
100 bool disable_resolver_cache
=
101 request_load_flags
& LOAD_BYPASS_CACHE
||
102 request_load_flags
& LOAD_VALIDATE_CACHE
||
103 request_load_flags
& LOAD_DISABLE_CACHE
;
105 int load_flags
= request_load_flags
;
106 if (session
->params().ignore_certificate_errors
)
107 load_flags
|= LOAD_IGNORE_ALL_CERT_ERRORS
;
109 // Build the string used to uniquely identify connections of this type.
110 // Determine the host and port to connect to.
111 std::string connection_group
= origin_host_port
.ToString();
112 DCHECK(!connection_group
.empty());
113 if (group_type
== ClientSocketPoolManager::FTP_GROUP
) {
114 // Combining FTP with forced SPDY over SSL would be a "path to madness".
115 // Make sure we never do that.
117 connection_group
= "ftp/" + connection_group
;
120 // All connections in a group should use the same SSLConfig settings.
121 // Encode version_max in the connection group's name, unless it's the
122 // default version_max. (We want the common case to use the shortest
123 // encoding). A version_max of TLS 1.1 is encoded as "ssl(max:3.2)/"
124 // rather than "tlsv1.1/" because the actual protocol version, which
125 // is selected by the server, may not be TLS 1.1. Do not encode
126 // version_min in the connection group's name because version_min
127 // should be the same for all connections, whereas version_max may
128 // change for version fallbacks.
129 std::string prefix
= "ssl/";
130 if (ssl_config_for_origin
.version_max
!=
131 SSLClientSocket::GetMaxSupportedSSLVersion()) {
132 switch (ssl_config_for_origin
.version_max
) {
133 case SSL_PROTOCOL_VERSION_TLS1_2
:
134 prefix
= "ssl(max:3.3)/";
136 case SSL_PROTOCOL_VERSION_TLS1_1
:
137 prefix
= "ssl(max:3.2)/";
139 case SSL_PROTOCOL_VERSION_TLS1
:
140 prefix
= "ssl(max:3.1)/";
147 // Place sockets with and without deprecated ciphers into separate
148 // connection groups.
149 if (ssl_config_for_origin
.enable_deprecated_cipher_suites
)
150 prefix
+= "deprecatedciphers/";
151 connection_group
= prefix
+ connection_group
;
154 bool ignore_limits
= (request_load_flags
& LOAD_IGNORE_LIMITS
) != 0;
155 if (!proxy_info
.is_direct()) {
156 ProxyServer proxy_server
= proxy_info
.proxy_server();
157 proxy_host_port
.reset(new HostPortPair(proxy_server
.host_port_pair()));
158 scoped_refptr
<TransportSocketParams
> proxy_tcp_params(
159 new TransportSocketParams(
161 disable_resolver_cache
,
164 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
166 if (proxy_info
.is_http() || proxy_info
.is_https()) {
167 std::string user_agent
;
168 request_extra_headers
.GetHeader(HttpRequestHeaders::kUserAgent
,
170 scoped_refptr
<SSLSocketParams
> ssl_params
;
171 if (proxy_info
.is_https()) {
172 // Combine connect and write for SSL sockets in TCP FastOpen
174 TransportSocketParams::CombineConnectAndWritePolicy
175 combine_connect_and_write
=
176 session
->params().enable_tcp_fast_open_for_ssl
?
177 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
:
178 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
;
179 proxy_tcp_params
= new TransportSocketParams(*proxy_host_port
,
180 disable_resolver_cache
,
183 combine_connect_and_write
);
184 // Set ssl_params, and unset proxy_tcp_params
186 new SSLSocketParams(proxy_tcp_params
, NULL
, NULL
,
187 *proxy_host_port
.get(), ssl_config_for_proxy
,
188 PRIVACY_MODE_DISABLED
, load_flags
, expect_spdy
);
189 proxy_tcp_params
= NULL
;
193 new HttpProxySocketParams(proxy_tcp_params
,
197 session
->http_auth_cache(),
198 session
->http_auth_handler_factory(),
199 session
->spdy_session_pool(),
200 force_tunnel
|| using_ssl
,
201 session
->params().proxy_delegate
);
203 DCHECK(proxy_info
.is_socks());
205 if (proxy_server
.scheme() == ProxyServer::SCHEME_SOCKS5
)
209 connection_group
= base::StringPrintf(
210 "socks%c/%s", socks_version
, connection_group
.c_str());
212 socks_params
= new SOCKSSocketParams(proxy_tcp_params
,
213 socks_version
== '5',
218 // Change group name if privacy mode is enabled.
219 if (privacy_mode
== PRIVACY_MODE_ENABLED
)
220 connection_group
= "pm/" + connection_group
;
222 // Deal with SSL - which layers on top of any given proxy.
224 scoped_refptr
<TransportSocketParams
> ssl_tcp_params
;
225 if (proxy_info
.is_direct()) {
226 // Setup TCP params if non-proxied SSL connection.
227 // Combine connect and write for SSL sockets in TCP FastOpen field trial.
228 TransportSocketParams::CombineConnectAndWritePolicy
229 combine_connect_and_write
=
230 session
->params().enable_tcp_fast_open_for_ssl
?
231 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
:
232 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
;
233 ssl_tcp_params
= new TransportSocketParams(origin_host_port
,
234 disable_resolver_cache
,
237 combine_connect_and_write
);
239 scoped_refptr
<SSLSocketParams
> ssl_params
= new SSLSocketParams(
240 ssl_tcp_params
, socks_params
, http_proxy_params
, origin_host_port
,
241 ssl_config_for_origin
, privacy_mode
, load_flags
, expect_spdy
);
242 SSLClientSocketPool
* ssl_pool
= NULL
;
243 if (proxy_info
.is_direct()) {
244 ssl_pool
= session
->GetSSLSocketPool(socket_pool_type
);
246 ssl_pool
= session
->GetSocketPoolForSSLWithProxy(socket_pool_type
,
250 if (num_preconnect_streams
) {
251 RequestSocketsForPool(ssl_pool
, connection_group
, ssl_params
,
252 num_preconnect_streams
, net_log
);
256 return socket_handle
->Init(connection_group
, ssl_params
,
257 request_priority
, callback
, ssl_pool
,
261 // Finally, get the connection started.
263 if (proxy_info
.is_http() || proxy_info
.is_https()) {
264 HttpProxyClientSocketPool
* pool
=
265 session
->GetSocketPoolForHTTPProxy(socket_pool_type
, *proxy_host_port
);
266 if (num_preconnect_streams
) {
267 RequestSocketsForPool(pool
, connection_group
, http_proxy_params
,
268 num_preconnect_streams
, net_log
);
272 return socket_handle
->Init(connection_group
, http_proxy_params
,
273 request_priority
, callback
,
277 if (proxy_info
.is_socks()) {
278 SOCKSClientSocketPool
* pool
=
279 session
->GetSocketPoolForSOCKSProxy(socket_pool_type
, *proxy_host_port
);
280 if (num_preconnect_streams
) {
281 RequestSocketsForPool(pool
, connection_group
, socks_params
,
282 num_preconnect_streams
, net_log
);
286 return socket_handle
->Init(connection_group
, socks_params
,
287 request_priority
, callback
, pool
,
291 DCHECK(proxy_info
.is_direct());
292 scoped_refptr
<TransportSocketParams
> tcp_params
=
293 new TransportSocketParams(
295 disable_resolver_cache
,
298 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
);
299 TransportClientSocketPool
* pool
=
300 session
->GetTransportSocketPool(socket_pool_type
);
301 if (num_preconnect_streams
) {
302 RequestSocketsForPool(pool
, connection_group
, tcp_params
,
303 num_preconnect_streams
, net_log
);
307 return socket_handle
->Init(connection_group
, tcp_params
,
308 request_priority
, callback
,
314 ClientSocketPoolManager::ClientSocketPoolManager() {}
315 ClientSocketPoolManager::~ClientSocketPoolManager() {}
318 int ClientSocketPoolManager::max_sockets_per_pool(
319 HttpNetworkSession::SocketPoolType pool_type
) {
320 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
321 return g_max_sockets_per_pool
[pool_type
];
325 void ClientSocketPoolManager::set_max_sockets_per_pool(
326 HttpNetworkSession::SocketPoolType pool_type
,
328 DCHECK_LT(0, socket_count
);
329 DCHECK_GT(1000, socket_count
); // Sanity check.
330 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
331 g_max_sockets_per_pool
[pool_type
] = socket_count
;
332 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
333 g_max_sockets_per_group
[pool_type
]);
337 int ClientSocketPoolManager::max_sockets_per_group(
338 HttpNetworkSession::SocketPoolType pool_type
) {
339 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
340 return g_max_sockets_per_group
[pool_type
];
344 void ClientSocketPoolManager::set_max_sockets_per_group(
345 HttpNetworkSession::SocketPoolType pool_type
,
347 DCHECK_LT(0, socket_count
);
348 // The following is a sanity check... but we should NEVER be near this value.
349 DCHECK_GT(100, socket_count
);
350 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
351 g_max_sockets_per_group
[pool_type
] = socket_count
;
353 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
354 g_max_sockets_per_group
[pool_type
]);
355 DCHECK_GE(g_max_sockets_per_proxy_server
[pool_type
],
356 g_max_sockets_per_group
[pool_type
]);
360 int ClientSocketPoolManager::max_sockets_per_proxy_server(
361 HttpNetworkSession::SocketPoolType pool_type
) {
362 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
363 return g_max_sockets_per_proxy_server
[pool_type
];
367 void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
368 HttpNetworkSession::SocketPoolType pool_type
,
370 DCHECK_LT(0, socket_count
);
371 DCHECK_GT(100, socket_count
); // Sanity check.
372 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
373 // Assert this case early on. The max number of sockets per group cannot
374 // exceed the max number of sockets per proxy server.
375 DCHECK_LE(g_max_sockets_per_group
[pool_type
], socket_count
);
376 g_max_sockets_per_proxy_server
[pool_type
] = socket_count
;
379 int InitSocketHandleForHttpRequest(
380 ClientSocketPoolManager::SocketGroupType group_type
,
381 const HostPortPair
& endpoint
,
382 const HttpRequestHeaders
& request_extra_headers
,
383 int request_load_flags
,
384 RequestPriority request_priority
,
385 HttpNetworkSession
* session
,
386 const ProxyInfo
& proxy_info
,
388 const SSLConfig
& ssl_config_for_origin
,
389 const SSLConfig
& ssl_config_for_proxy
,
390 PrivacyMode privacy_mode
,
391 const BoundNetLog
& net_log
,
392 ClientSocketHandle
* socket_handle
,
393 const OnHostResolutionCallback
& resolution_callback
,
394 const CompletionCallback
& callback
) {
395 DCHECK(socket_handle
);
396 return InitSocketPoolHelper(
397 group_type
, endpoint
, request_extra_headers
, request_load_flags
,
398 request_priority
, session
, proxy_info
, expect_spdy
, ssl_config_for_origin
,
399 ssl_config_for_proxy
, /*force_tunnel=*/false, privacy_mode
, net_log
, 0,
400 socket_handle
, HttpNetworkSession::NORMAL_SOCKET_POOL
,
401 resolution_callback
, callback
);
404 int InitSocketHandleForWebSocketRequest(
405 ClientSocketPoolManager::SocketGroupType group_type
,
406 const HostPortPair
& endpoint
,
407 const HttpRequestHeaders
& request_extra_headers
,
408 int request_load_flags
,
409 RequestPriority request_priority
,
410 HttpNetworkSession
* session
,
411 const ProxyInfo
& proxy_info
,
413 const SSLConfig
& ssl_config_for_origin
,
414 const SSLConfig
& ssl_config_for_proxy
,
415 PrivacyMode privacy_mode
,
416 const BoundNetLog
& net_log
,
417 ClientSocketHandle
* socket_handle
,
418 const OnHostResolutionCallback
& resolution_callback
,
419 const CompletionCallback
& callback
) {
420 DCHECK(socket_handle
);
421 return InitSocketPoolHelper(
422 group_type
, endpoint
, request_extra_headers
, request_load_flags
,
423 request_priority
, session
, proxy_info
, expect_spdy
, ssl_config_for_origin
,
424 ssl_config_for_proxy
, /*force_tunnel=*/true, privacy_mode
, net_log
, 0,
425 socket_handle
, HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
426 resolution_callback
, callback
);
429 int InitSocketHandleForRawConnect(
430 const HostPortPair
& host_port_pair
,
431 HttpNetworkSession
* session
,
432 const ProxyInfo
& proxy_info
,
433 const SSLConfig
& ssl_config_for_origin
,
434 const SSLConfig
& ssl_config_for_proxy
,
435 PrivacyMode privacy_mode
,
436 const BoundNetLog
& net_log
,
437 ClientSocketHandle
* socket_handle
,
438 const CompletionCallback
& callback
) {
439 DCHECK(socket_handle
);
440 HttpRequestHeaders request_extra_headers
;
441 int request_load_flags
= 0;
442 RequestPriority request_priority
= MEDIUM
;
443 return InitSocketPoolHelper(
444 ClientSocketPoolManager::NORMAL_GROUP
, host_port_pair
,
445 request_extra_headers
, request_load_flags
, request_priority
, session
,
446 proxy_info
, false, ssl_config_for_origin
, ssl_config_for_proxy
,
447 /*force_tunnel=*/true, privacy_mode
, net_log
, 0, socket_handle
,
448 HttpNetworkSession::NORMAL_SOCKET_POOL
, OnHostResolutionCallback(),
452 int InitSocketHandleForTlsConnect(const HostPortPair
& endpoint
,
453 HttpNetworkSession
* session
,
454 const ProxyInfo
& proxy_info
,
455 const SSLConfig
& ssl_config_for_origin
,
456 const SSLConfig
& ssl_config_for_proxy
,
457 PrivacyMode privacy_mode
,
458 const BoundNetLog
& net_log
,
459 ClientSocketHandle
* socket_handle
,
460 const CompletionCallback
& callback
) {
461 DCHECK(socket_handle
);
462 HttpRequestHeaders request_extra_headers
;
463 int request_load_flags
= 0;
464 RequestPriority request_priority
= MEDIUM
;
465 return InitSocketPoolHelper(
466 ClientSocketPoolManager::SSL_GROUP
, endpoint
, request_extra_headers
,
467 request_load_flags
, request_priority
, session
, proxy_info
,
468 /*expect_spdy=*/false, ssl_config_for_origin
, ssl_config_for_proxy
,
469 /*force_tunnel=*/true, privacy_mode
, net_log
, 0, socket_handle
,
470 HttpNetworkSession::NORMAL_SOCKET_POOL
, OnHostResolutionCallback(),
474 int PreconnectSocketsForHttpRequest(
475 ClientSocketPoolManager::SocketGroupType group_type
,
476 const HostPortPair
& endpoint
,
477 const HttpRequestHeaders
& request_extra_headers
,
478 int request_load_flags
,
479 RequestPriority request_priority
,
480 HttpNetworkSession
* session
,
481 const ProxyInfo
& proxy_info
,
483 const SSLConfig
& ssl_config_for_origin
,
484 const SSLConfig
& ssl_config_for_proxy
,
485 PrivacyMode privacy_mode
,
486 const BoundNetLog
& net_log
,
487 int num_preconnect_streams
) {
488 return InitSocketPoolHelper(
489 group_type
, endpoint
, request_extra_headers
, request_load_flags
,
490 request_priority
, session
, proxy_info
, expect_spdy
, ssl_config_for_origin
,
491 ssl_config_for_proxy
, /*force_tunnel=*/false, privacy_mode
, net_log
,
492 num_preconnect_streams
, NULL
, HttpNetworkSession::NORMAL_SOCKET_POOL
,
493 OnHostResolutionCallback(), CompletionCallback());