Use EXPECT_EQ when possible.
[chromium-blink-merge.git] / net / socket / client_socket_pool_manager.cc
blobbc7d111dcd45399f46904584817cdd677f2e865a
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"
7 #include <string>
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"
22 namespace net {
24 namespace {
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
43 // arbitrarily.
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,
79 bool force_tunnel,
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.
118 DCHECK(!using_ssl);
119 connection_group = "ftp/" + connection_group;
121 if (using_ssl) {
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 != kDefaultSSLVersionMax) {
133 switch (ssl_config_for_origin.version_max) {
134 case SSL_PROTOCOL_VERSION_TLS1_2:
135 prefix = "ssl(max:3.3)/";
136 break;
137 case SSL_PROTOCOL_VERSION_TLS1_1:
138 prefix = "ssl(max:3.2)/";
139 break;
140 case SSL_PROTOCOL_VERSION_TLS1:
141 prefix = "ssl(max:3.1)/";
142 break;
143 case SSL_PROTOCOL_VERSION_SSL3:
144 prefix = "sslv3/";
145 break;
146 default:
147 CHECK(false);
148 break;
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(
160 *proxy_host_port,
161 disable_resolver_cache,
162 ignore_limits,
163 resolution_callback,
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,
169 &user_agent);
170 scoped_refptr<SSLSocketParams> ssl_params;
171 if (proxy_info.is_https()) {
172 // Combine connect and write for SSL sockets in TCP FastOpen
173 // field trial.
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,
181 ignore_limits,
182 resolution_callback,
183 combine_connect_and_write);
184 // Set ssl_params, and unset proxy_tcp_params
185 ssl_params = new SSLSocketParams(proxy_tcp_params,
186 NULL,
187 NULL,
188 *proxy_host_port.get(),
189 ssl_config_for_proxy,
190 PRIVACY_MODE_DISABLED,
191 load_flags,
192 force_spdy_over_ssl,
193 want_spdy_over_npn);
194 proxy_tcp_params = NULL;
197 http_proxy_params =
198 new HttpProxySocketParams(proxy_tcp_params,
199 ssl_params,
200 request_url,
201 user_agent,
202 origin_host_port,
203 session->http_auth_cache(),
204 session->http_auth_handler_factory(),
205 session->spdy_session_pool(),
206 force_tunnel || using_ssl,
207 session->params().proxy_delegate);
208 } else {
209 DCHECK(proxy_info.is_socks());
210 char socks_version;
211 if (proxy_server.scheme() == ProxyServer::SCHEME_SOCKS5)
212 socks_version = '5';
213 else
214 socks_version = '4';
215 connection_group = base::StringPrintf(
216 "socks%c/%s", socks_version, connection_group.c_str());
218 socks_params = new SOCKSSocketParams(proxy_tcp_params,
219 socks_version == '5',
220 origin_host_port);
224 // Change group name if privacy mode is enabled.
225 if (privacy_mode == PRIVACY_MODE_ENABLED)
226 connection_group = "pm/" + connection_group;
228 // Deal with SSL - which layers on top of any given proxy.
229 if (using_ssl) {
230 scoped_refptr<TransportSocketParams> ssl_tcp_params;
231 if (proxy_info.is_direct()) {
232 // Setup TCP params if non-proxied SSL connection.
233 // Combine connect and write for SSL sockets in TCP FastOpen field trial.
234 TransportSocketParams::CombineConnectAndWritePolicy
235 combine_connect_and_write =
236 session->params().enable_tcp_fast_open_for_ssl ?
237 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED :
238 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT;
239 ssl_tcp_params = new TransportSocketParams(origin_host_port,
240 disable_resolver_cache,
241 ignore_limits,
242 resolution_callback,
243 combine_connect_and_write);
245 scoped_refptr<SSLSocketParams> ssl_params =
246 new SSLSocketParams(ssl_tcp_params,
247 socks_params,
248 http_proxy_params,
249 origin_host_port,
250 ssl_config_for_origin,
251 privacy_mode,
252 load_flags,
253 force_spdy_over_ssl,
254 want_spdy_over_npn);
255 SSLClientSocketPool* ssl_pool = NULL;
256 if (proxy_info.is_direct()) {
257 ssl_pool = session->GetSSLSocketPool(socket_pool_type);
258 } else {
259 ssl_pool = session->GetSocketPoolForSSLWithProxy(socket_pool_type,
260 *proxy_host_port);
263 if (num_preconnect_streams) {
264 RequestSocketsForPool(ssl_pool, connection_group, ssl_params,
265 num_preconnect_streams, net_log);
266 return OK;
269 return socket_handle->Init(connection_group, ssl_params,
270 request_priority, callback, ssl_pool,
271 net_log);
274 // Finally, get the connection started.
276 if (proxy_info.is_http() || proxy_info.is_https()) {
277 HttpProxyClientSocketPool* pool =
278 session->GetSocketPoolForHTTPProxy(socket_pool_type, *proxy_host_port);
279 if (num_preconnect_streams) {
280 RequestSocketsForPool(pool, connection_group, http_proxy_params,
281 num_preconnect_streams, net_log);
282 return OK;
285 return socket_handle->Init(connection_group, http_proxy_params,
286 request_priority, callback,
287 pool, net_log);
290 if (proxy_info.is_socks()) {
291 SOCKSClientSocketPool* pool =
292 session->GetSocketPoolForSOCKSProxy(socket_pool_type, *proxy_host_port);
293 if (num_preconnect_streams) {
294 RequestSocketsForPool(pool, connection_group, socks_params,
295 num_preconnect_streams, net_log);
296 return OK;
299 return socket_handle->Init(connection_group, socks_params,
300 request_priority, callback, pool,
301 net_log);
304 DCHECK(proxy_info.is_direct());
305 scoped_refptr<TransportSocketParams> tcp_params =
306 new TransportSocketParams(
307 origin_host_port,
308 disable_resolver_cache,
309 ignore_limits,
310 resolution_callback,
311 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT);
312 TransportClientSocketPool* pool =
313 session->GetTransportSocketPool(socket_pool_type);
314 if (num_preconnect_streams) {
315 RequestSocketsForPool(pool, connection_group, tcp_params,
316 num_preconnect_streams, net_log);
317 return OK;
320 return socket_handle->Init(connection_group, tcp_params,
321 request_priority, callback,
322 pool, net_log);
325 } // namespace
327 ClientSocketPoolManager::ClientSocketPoolManager() {}
328 ClientSocketPoolManager::~ClientSocketPoolManager() {}
330 // static
331 int ClientSocketPoolManager::max_sockets_per_pool(
332 HttpNetworkSession::SocketPoolType pool_type) {
333 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
334 return g_max_sockets_per_pool[pool_type];
337 // static
338 void ClientSocketPoolManager::set_max_sockets_per_pool(
339 HttpNetworkSession::SocketPoolType pool_type,
340 int socket_count) {
341 DCHECK_LT(0, socket_count);
342 DCHECK_GT(1000, socket_count); // Sanity check.
343 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
344 g_max_sockets_per_pool[pool_type] = socket_count;
345 DCHECK_GE(g_max_sockets_per_pool[pool_type],
346 g_max_sockets_per_group[pool_type]);
349 // static
350 int ClientSocketPoolManager::max_sockets_per_group(
351 HttpNetworkSession::SocketPoolType pool_type) {
352 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
353 return g_max_sockets_per_group[pool_type];
356 // static
357 void ClientSocketPoolManager::set_max_sockets_per_group(
358 HttpNetworkSession::SocketPoolType pool_type,
359 int socket_count) {
360 DCHECK_LT(0, socket_count);
361 // The following is a sanity check... but we should NEVER be near this value.
362 DCHECK_GT(100, socket_count);
363 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
364 g_max_sockets_per_group[pool_type] = socket_count;
366 DCHECK_GE(g_max_sockets_per_pool[pool_type],
367 g_max_sockets_per_group[pool_type]);
368 DCHECK_GE(g_max_sockets_per_proxy_server[pool_type],
369 g_max_sockets_per_group[pool_type]);
372 // static
373 int ClientSocketPoolManager::max_sockets_per_proxy_server(
374 HttpNetworkSession::SocketPoolType pool_type) {
375 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
376 return g_max_sockets_per_proxy_server[pool_type];
379 // static
380 void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
381 HttpNetworkSession::SocketPoolType pool_type,
382 int socket_count) {
383 DCHECK_LT(0, socket_count);
384 DCHECK_GT(100, socket_count); // Sanity check.
385 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
386 // Assert this case early on. The max number of sockets per group cannot
387 // exceed the max number of sockets per proxy server.
388 DCHECK_LE(g_max_sockets_per_group[pool_type], socket_count);
389 g_max_sockets_per_proxy_server[pool_type] = socket_count;
392 int InitSocketHandleForHttpRequest(
393 const GURL& request_url,
394 const HttpRequestHeaders& request_extra_headers,
395 int request_load_flags,
396 RequestPriority request_priority,
397 HttpNetworkSession* session,
398 const ProxyInfo& proxy_info,
399 bool force_spdy_over_ssl,
400 bool want_spdy_over_npn,
401 const SSLConfig& ssl_config_for_origin,
402 const SSLConfig& ssl_config_for_proxy,
403 PrivacyMode privacy_mode,
404 const BoundNetLog& net_log,
405 ClientSocketHandle* socket_handle,
406 const OnHostResolutionCallback& resolution_callback,
407 const CompletionCallback& callback) {
408 DCHECK(socket_handle);
409 return InitSocketPoolHelper(
410 request_url, request_extra_headers, request_load_flags, request_priority,
411 session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn,
412 ssl_config_for_origin, ssl_config_for_proxy, false, privacy_mode, net_log,
413 0, socket_handle, HttpNetworkSession::NORMAL_SOCKET_POOL,
414 resolution_callback, callback);
417 int InitSocketHandleForWebSocketRequest(
418 const GURL& request_url,
419 const HttpRequestHeaders& request_extra_headers,
420 int request_load_flags,
421 RequestPriority request_priority,
422 HttpNetworkSession* session,
423 const ProxyInfo& proxy_info,
424 bool force_spdy_over_ssl,
425 bool want_spdy_over_npn,
426 const SSLConfig& ssl_config_for_origin,
427 const SSLConfig& ssl_config_for_proxy,
428 PrivacyMode privacy_mode,
429 const BoundNetLog& net_log,
430 ClientSocketHandle* socket_handle,
431 const OnHostResolutionCallback& resolution_callback,
432 const CompletionCallback& callback) {
433 DCHECK(socket_handle);
434 return InitSocketPoolHelper(
435 request_url, request_extra_headers, request_load_flags, request_priority,
436 session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn,
437 ssl_config_for_origin, ssl_config_for_proxy, true, privacy_mode, net_log,
438 0, socket_handle, HttpNetworkSession::WEBSOCKET_SOCKET_POOL,
439 resolution_callback, callback);
442 int InitSocketHandleForRawConnect(
443 const HostPortPair& host_port_pair,
444 HttpNetworkSession* session,
445 const ProxyInfo& proxy_info,
446 const SSLConfig& ssl_config_for_origin,
447 const SSLConfig& ssl_config_for_proxy,
448 PrivacyMode privacy_mode,
449 const BoundNetLog& net_log,
450 ClientSocketHandle* socket_handle,
451 const CompletionCallback& callback) {
452 DCHECK(socket_handle);
453 // Synthesize an HttpRequestInfo.
454 GURL request_url = GURL("http://" + host_port_pair.ToString());
455 HttpRequestHeaders request_extra_headers;
456 int request_load_flags = 0;
457 RequestPriority request_priority = MEDIUM;
459 return InitSocketPoolHelper(
460 request_url, request_extra_headers, request_load_flags, request_priority,
461 session, proxy_info, false, false, ssl_config_for_origin,
462 ssl_config_for_proxy, true, privacy_mode, net_log, 0, socket_handle,
463 HttpNetworkSession::NORMAL_SOCKET_POOL, OnHostResolutionCallback(),
464 callback);
467 int InitSocketHandleForTlsConnect(
468 const HostPortPair& host_port_pair,
469 HttpNetworkSession* session,
470 const ProxyInfo& proxy_info,
471 const SSLConfig& ssl_config_for_origin,
472 const SSLConfig& ssl_config_for_proxy,
473 PrivacyMode privacy_mode,
474 const BoundNetLog& net_log,
475 ClientSocketHandle* socket_handle,
476 const CompletionCallback& callback) {
477 DCHECK(socket_handle);
478 // Synthesize an HttpRequestInfo.
479 GURL request_url = GURL("https://" + host_port_pair.ToString());
480 HttpRequestHeaders request_extra_headers;
481 int request_load_flags = 0;
482 RequestPriority request_priority = MEDIUM;
484 return InitSocketPoolHelper(
485 request_url, request_extra_headers, request_load_flags, request_priority,
486 session, proxy_info, false, false, ssl_config_for_origin,
487 ssl_config_for_proxy, true, privacy_mode, net_log, 0, socket_handle,
488 HttpNetworkSession::NORMAL_SOCKET_POOL, OnHostResolutionCallback(),
489 callback);
492 int PreconnectSocketsForHttpRequest(
493 const GURL& request_url,
494 const HttpRequestHeaders& request_extra_headers,
495 int request_load_flags,
496 RequestPriority request_priority,
497 HttpNetworkSession* session,
498 const ProxyInfo& proxy_info,
499 bool force_spdy_over_ssl,
500 bool want_spdy_over_npn,
501 const SSLConfig& ssl_config_for_origin,
502 const SSLConfig& ssl_config_for_proxy,
503 PrivacyMode privacy_mode,
504 const BoundNetLog& net_log,
505 int num_preconnect_streams) {
506 return InitSocketPoolHelper(
507 request_url, request_extra_headers, request_load_flags, request_priority,
508 session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn,
509 ssl_config_for_origin, ssl_config_for_proxy, false, privacy_mode, net_log,
510 num_preconnect_streams, NULL, HttpNetworkSession::NORMAL_SOCKET_POOL,
511 OnHostResolutionCallback(), CompletionCallback());
514 } // namespace net