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/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 COMPILE_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 COMPILE_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 COMPILE_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 const BoundNetLog
& net_log
,
81 int num_preconnect_streams
,
82 ClientSocketHandle
* socket_handle
,
83 const OnHostResolutionCallback
& resolution_callback
,
84 const CompletionCallback
& callback
) {
85 scoped_refptr
<TransportSocketParams
> tcp_params
;
86 scoped_refptr
<HttpProxySocketParams
> http_proxy_params
;
87 scoped_refptr
<SOCKSSocketParams
> socks_params
;
88 scoped_ptr
<HostPortPair
> proxy_host_port
;
90 bool using_ssl
= request_url
.SchemeIs("https") || force_spdy_over_ssl
;
92 HostPortPair origin_host_port
=
93 HostPortPair(request_url
.HostNoBrackets(),
94 request_url
.EffectiveIntPort());
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 SSLConfigService::default_version_max()) {
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 connection_group
= prefix
+ connection_group
;
155 bool ignore_limits
= (request_load_flags
& LOAD_IGNORE_LIMITS
) != 0;
156 if (proxy_info
.is_direct()) {
157 tcp_params
= new TransportSocketParams(origin_host_port
,
159 disable_resolver_cache
,
161 resolution_callback
);
163 ProxyServer proxy_server
= proxy_info
.proxy_server();
164 proxy_host_port
.reset(new HostPortPair(proxy_server
.host_port_pair()));
165 scoped_refptr
<TransportSocketParams
> proxy_tcp_params(
166 new TransportSocketParams(*proxy_host_port
,
168 disable_resolver_cache
,
170 resolution_callback
));
172 if (proxy_info
.is_http() || proxy_info
.is_https()) {
173 std::string user_agent
;
174 request_extra_headers
.GetHeader(HttpRequestHeaders::kUserAgent
,
176 scoped_refptr
<SSLSocketParams
> ssl_params
;
177 if (proxy_info
.is_https()) {
178 // Set ssl_params, and unset proxy_tcp_params
179 ssl_params
= new SSLSocketParams(proxy_tcp_params
,
182 ProxyServer::SCHEME_DIRECT
,
183 *proxy_host_port
.get(),
184 ssl_config_for_proxy
,
188 proxy_tcp_params
= NULL
;
192 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
);
202 DCHECK(proxy_info
.is_socks());
204 if (proxy_server
.scheme() == ProxyServer::SCHEME_SOCKS5
)
208 connection_group
= base::StringPrintf(
209 "socks%c/%s", socks_version
, connection_group
.c_str());
211 socks_params
= new SOCKSSocketParams(proxy_tcp_params
,
212 socks_version
== '5',
218 // Deal with SSL - which layers on top of any given proxy.
220 scoped_refptr
<SSLSocketParams
> ssl_params
=
221 new SSLSocketParams(tcp_params
,
224 proxy_info
.proxy_server().scheme(),
226 ssl_config_for_origin
,
230 SSLClientSocketPool
* ssl_pool
= NULL
;
231 if (proxy_info
.is_direct()) {
232 ssl_pool
= session
->GetSSLSocketPool(
233 HttpNetworkSession::NORMAL_SOCKET_POOL
);
235 ssl_pool
= session
->GetSocketPoolForSSLWithProxy(
236 HttpNetworkSession::NORMAL_SOCKET_POOL
,
240 if (num_preconnect_streams
) {
241 RequestSocketsForPool(ssl_pool
, connection_group
, ssl_params
,
242 num_preconnect_streams
, net_log
);
246 return socket_handle
->Init(connection_group
, ssl_params
,
247 request_priority
, callback
, ssl_pool
,
251 // Finally, get the connection started.
253 if (proxy_info
.is_http() || proxy_info
.is_https()) {
254 HttpProxyClientSocketPool
* pool
=
255 session
->GetSocketPoolForHTTPProxy(
256 HttpNetworkSession::NORMAL_SOCKET_POOL
,
258 if (num_preconnect_streams
) {
259 RequestSocketsForPool(pool
, connection_group
, http_proxy_params
,
260 num_preconnect_streams
, net_log
);
264 return socket_handle
->Init(connection_group
, http_proxy_params
,
265 request_priority
, callback
,
269 if (proxy_info
.is_socks()) {
270 SOCKSClientSocketPool
* pool
=
271 session
->GetSocketPoolForSOCKSProxy(
272 HttpNetworkSession::NORMAL_SOCKET_POOL
,
274 if (num_preconnect_streams
) {
275 RequestSocketsForPool(pool
, connection_group
, socks_params
,
276 num_preconnect_streams
, net_log
);
280 return socket_handle
->Init(connection_group
, socks_params
,
281 request_priority
, callback
, pool
,
285 DCHECK(proxy_info
.is_direct());
287 TransportClientSocketPool
* pool
=
288 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
);
289 if (num_preconnect_streams
) {
290 RequestSocketsForPool(pool
, connection_group
, tcp_params
,
291 num_preconnect_streams
, net_log
);
295 return socket_handle
->Init(connection_group
, tcp_params
,
296 request_priority
, callback
,
302 ClientSocketPoolManager::ClientSocketPoolManager() {}
303 ClientSocketPoolManager::~ClientSocketPoolManager() {}
306 int ClientSocketPoolManager::max_sockets_per_pool(
307 HttpNetworkSession::SocketPoolType pool_type
) {
308 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
309 return g_max_sockets_per_pool
[pool_type
];
313 void ClientSocketPoolManager::set_max_sockets_per_pool(
314 HttpNetworkSession::SocketPoolType pool_type
,
316 DCHECK_LT(0, socket_count
);
317 DCHECK_GT(1000, socket_count
); // Sanity check.
318 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
319 g_max_sockets_per_pool
[pool_type
] = socket_count
;
320 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
321 g_max_sockets_per_group
[pool_type
]);
325 int ClientSocketPoolManager::max_sockets_per_group(
326 HttpNetworkSession::SocketPoolType pool_type
) {
327 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
328 return g_max_sockets_per_group
[pool_type
];
332 void ClientSocketPoolManager::set_max_sockets_per_group(
333 HttpNetworkSession::SocketPoolType pool_type
,
335 DCHECK_LT(0, socket_count
);
336 // The following is a sanity check... but we should NEVER be near this value.
337 DCHECK_GT(100, socket_count
);
338 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
339 g_max_sockets_per_group
[pool_type
] = socket_count
;
341 DCHECK_GE(g_max_sockets_per_pool
[pool_type
],
342 g_max_sockets_per_group
[pool_type
]);
343 DCHECK_GE(g_max_sockets_per_proxy_server
[pool_type
],
344 g_max_sockets_per_group
[pool_type
]);
348 int ClientSocketPoolManager::max_sockets_per_proxy_server(
349 HttpNetworkSession::SocketPoolType pool_type
) {
350 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
351 return g_max_sockets_per_proxy_server
[pool_type
];
355 void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
356 HttpNetworkSession::SocketPoolType pool_type
,
358 DCHECK_LT(0, socket_count
);
359 DCHECK_GT(100, socket_count
); // Sanity check.
360 DCHECK_LT(pool_type
, HttpNetworkSession::NUM_SOCKET_POOL_TYPES
);
361 // Assert this case early on. The max number of sockets per group cannot
362 // exceed the max number of sockets per proxy server.
363 DCHECK_LE(g_max_sockets_per_group
[pool_type
], socket_count
);
364 g_max_sockets_per_proxy_server
[pool_type
] = socket_count
;
367 int InitSocketHandleForHttpRequest(
368 const GURL
& request_url
,
369 const HttpRequestHeaders
& request_extra_headers
,
370 int request_load_flags
,
371 RequestPriority request_priority
,
372 HttpNetworkSession
* session
,
373 const ProxyInfo
& proxy_info
,
374 bool force_spdy_over_ssl
,
375 bool want_spdy_over_npn
,
376 const SSLConfig
& ssl_config_for_origin
,
377 const SSLConfig
& ssl_config_for_proxy
,
378 const BoundNetLog
& net_log
,
379 ClientSocketHandle
* socket_handle
,
380 const OnHostResolutionCallback
& resolution_callback
,
381 const CompletionCallback
& callback
) {
382 DCHECK(socket_handle
);
383 return InitSocketPoolHelper(
384 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
385 session
, proxy_info
, force_spdy_over_ssl
, want_spdy_over_npn
,
386 ssl_config_for_origin
, ssl_config_for_proxy
, false, net_log
, 0,
387 socket_handle
, resolution_callback
, callback
);
390 int InitSocketHandleForRawConnect(
391 const HostPortPair
& host_port_pair
,
392 HttpNetworkSession
* session
,
393 const ProxyInfo
& proxy_info
,
394 const SSLConfig
& ssl_config_for_origin
,
395 const SSLConfig
& ssl_config_for_proxy
,
396 const BoundNetLog
& net_log
,
397 ClientSocketHandle
* socket_handle
,
398 const CompletionCallback
& callback
) {
399 DCHECK(socket_handle
);
400 // Synthesize an HttpRequestInfo.
401 GURL request_url
= GURL("http://" + host_port_pair
.ToString());
402 HttpRequestHeaders request_extra_headers
;
403 int request_load_flags
= 0;
404 RequestPriority request_priority
= MEDIUM
;
406 return InitSocketPoolHelper(
407 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
408 session
, proxy_info
, false, false, ssl_config_for_origin
,
409 ssl_config_for_proxy
, true, net_log
, 0, socket_handle
,
410 OnHostResolutionCallback(), callback
);
413 int PreconnectSocketsForHttpRequest(
414 const GURL
& request_url
,
415 const HttpRequestHeaders
& request_extra_headers
,
416 int request_load_flags
,
417 RequestPriority request_priority
,
418 HttpNetworkSession
* session
,
419 const ProxyInfo
& proxy_info
,
420 bool force_spdy_over_ssl
,
421 bool want_spdy_over_npn
,
422 const SSLConfig
& ssl_config_for_origin
,
423 const SSLConfig
& ssl_config_for_proxy
,
424 const BoundNetLog
& net_log
,
425 int num_preconnect_streams
) {
426 return InitSocketPoolHelper(
427 request_url
, request_extra_headers
, request_load_flags
, request_priority
,
428 session
, proxy_info
, force_spdy_over_ssl
, want_spdy_over_npn
,
429 ssl_config_for_origin
, ssl_config_for_proxy
, false, net_log
,
430 num_preconnect_streams
, NULL
, OnHostResolutionCallback(),
431 CompletionCallback());