Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / net / socket / client_socket_pool_manager.cc
blob4af9c9b37a21759b81b25c8d744149d898309b2a
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 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
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 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,
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 = 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.
116 DCHECK(!using_ssl);
117 connection_group = "ftp/" + connection_group;
119 if (using_ssl) {
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)/";
135 break;
136 case SSL_PROTOCOL_VERSION_TLS1_1:
137 prefix = "ssl(max:3.2)/";
138 break;
139 case SSL_PROTOCOL_VERSION_TLS1:
140 prefix = "ssl(max:3.1)/";
141 break;
142 default:
143 CHECK(false);
144 break;
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(
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 want_spdy_over_npn);
193 proxy_tcp_params = NULL;
196 http_proxy_params =
197 new HttpProxySocketParams(proxy_tcp_params,
198 ssl_params,
199 user_agent,
200 origin_host_port,
201 session->http_auth_cache(),
202 session->http_auth_handler_factory(),
203 session->spdy_session_pool(),
204 force_tunnel || using_ssl,
205 session->params().proxy_delegate);
206 } else {
207 DCHECK(proxy_info.is_socks());
208 char socks_version;
209 if (proxy_server.scheme() == ProxyServer::SCHEME_SOCKS5)
210 socks_version = '5';
211 else
212 socks_version = '4';
213 connection_group = base::StringPrintf(
214 "socks%c/%s", socks_version, connection_group.c_str());
216 socks_params = new SOCKSSocketParams(proxy_tcp_params,
217 socks_version == '5',
218 origin_host_port);
222 // Change group name if privacy mode is enabled.
223 if (privacy_mode == PRIVACY_MODE_ENABLED)
224 connection_group = "pm/" + connection_group;
226 // Deal with SSL - which layers on top of any given proxy.
227 if (using_ssl) {
228 scoped_refptr<TransportSocketParams> ssl_tcp_params;
229 if (proxy_info.is_direct()) {
230 // Setup TCP params if non-proxied SSL connection.
231 // Combine connect and write for SSL sockets in TCP FastOpen field trial.
232 TransportSocketParams::CombineConnectAndWritePolicy
233 combine_connect_and_write =
234 session->params().enable_tcp_fast_open_for_ssl ?
235 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED :
236 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT;
237 ssl_tcp_params = new TransportSocketParams(origin_host_port,
238 disable_resolver_cache,
239 ignore_limits,
240 resolution_callback,
241 combine_connect_and_write);
243 scoped_refptr<SSLSocketParams> ssl_params =
244 new SSLSocketParams(ssl_tcp_params,
245 socks_params,
246 http_proxy_params,
247 origin_host_port,
248 ssl_config_for_origin,
249 privacy_mode,
250 load_flags,
251 want_spdy_over_npn);
252 SSLClientSocketPool* ssl_pool = NULL;
253 if (proxy_info.is_direct()) {
254 ssl_pool = session->GetSSLSocketPool(socket_pool_type);
255 } else {
256 ssl_pool = session->GetSocketPoolForSSLWithProxy(socket_pool_type,
257 *proxy_host_port);
260 if (num_preconnect_streams) {
261 RequestSocketsForPool(ssl_pool, connection_group, ssl_params,
262 num_preconnect_streams, net_log);
263 return OK;
266 return socket_handle->Init(connection_group, ssl_params,
267 request_priority, callback, ssl_pool,
268 net_log);
271 // Finally, get the connection started.
273 if (proxy_info.is_http() || proxy_info.is_https()) {
274 HttpProxyClientSocketPool* pool =
275 session->GetSocketPoolForHTTPProxy(socket_pool_type, *proxy_host_port);
276 if (num_preconnect_streams) {
277 RequestSocketsForPool(pool, connection_group, http_proxy_params,
278 num_preconnect_streams, net_log);
279 return OK;
282 return socket_handle->Init(connection_group, http_proxy_params,
283 request_priority, callback,
284 pool, net_log);
287 if (proxy_info.is_socks()) {
288 SOCKSClientSocketPool* pool =
289 session->GetSocketPoolForSOCKSProxy(socket_pool_type, *proxy_host_port);
290 if (num_preconnect_streams) {
291 RequestSocketsForPool(pool, connection_group, socks_params,
292 num_preconnect_streams, net_log);
293 return OK;
296 return socket_handle->Init(connection_group, socks_params,
297 request_priority, callback, pool,
298 net_log);
301 DCHECK(proxy_info.is_direct());
302 scoped_refptr<TransportSocketParams> tcp_params =
303 new TransportSocketParams(
304 origin_host_port,
305 disable_resolver_cache,
306 ignore_limits,
307 resolution_callback,
308 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT);
309 TransportClientSocketPool* pool =
310 session->GetTransportSocketPool(socket_pool_type);
311 if (num_preconnect_streams) {
312 RequestSocketsForPool(pool, connection_group, tcp_params,
313 num_preconnect_streams, net_log);
314 return OK;
317 return socket_handle->Init(connection_group, tcp_params,
318 request_priority, callback,
319 pool, net_log);
322 } // namespace
324 ClientSocketPoolManager::ClientSocketPoolManager() {}
325 ClientSocketPoolManager::~ClientSocketPoolManager() {}
327 // static
328 int ClientSocketPoolManager::max_sockets_per_pool(
329 HttpNetworkSession::SocketPoolType pool_type) {
330 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
331 return g_max_sockets_per_pool[pool_type];
334 // static
335 void ClientSocketPoolManager::set_max_sockets_per_pool(
336 HttpNetworkSession::SocketPoolType pool_type,
337 int socket_count) {
338 DCHECK_LT(0, socket_count);
339 DCHECK_GT(1000, socket_count); // Sanity check.
340 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
341 g_max_sockets_per_pool[pool_type] = socket_count;
342 DCHECK_GE(g_max_sockets_per_pool[pool_type],
343 g_max_sockets_per_group[pool_type]);
346 // static
347 int ClientSocketPoolManager::max_sockets_per_group(
348 HttpNetworkSession::SocketPoolType pool_type) {
349 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
350 return g_max_sockets_per_group[pool_type];
353 // static
354 void ClientSocketPoolManager::set_max_sockets_per_group(
355 HttpNetworkSession::SocketPoolType pool_type,
356 int socket_count) {
357 DCHECK_LT(0, socket_count);
358 // The following is a sanity check... but we should NEVER be near this value.
359 DCHECK_GT(100, socket_count);
360 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
361 g_max_sockets_per_group[pool_type] = socket_count;
363 DCHECK_GE(g_max_sockets_per_pool[pool_type],
364 g_max_sockets_per_group[pool_type]);
365 DCHECK_GE(g_max_sockets_per_proxy_server[pool_type],
366 g_max_sockets_per_group[pool_type]);
369 // static
370 int ClientSocketPoolManager::max_sockets_per_proxy_server(
371 HttpNetworkSession::SocketPoolType pool_type) {
372 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
373 return g_max_sockets_per_proxy_server[pool_type];
376 // static
377 void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
378 HttpNetworkSession::SocketPoolType pool_type,
379 int socket_count) {
380 DCHECK_LT(0, socket_count);
381 DCHECK_GT(100, socket_count); // Sanity check.
382 DCHECK_LT(pool_type, HttpNetworkSession::NUM_SOCKET_POOL_TYPES);
383 // Assert this case early on. The max number of sockets per group cannot
384 // exceed the max number of sockets per proxy server.
385 DCHECK_LE(g_max_sockets_per_group[pool_type], socket_count);
386 g_max_sockets_per_proxy_server[pool_type] = socket_count;
389 int InitSocketHandleForHttpRequest(
390 ClientSocketPoolManager::SocketGroupType group_type,
391 const HostPortPair& endpoint,
392 const HttpRequestHeaders& request_extra_headers,
393 int request_load_flags,
394 RequestPriority request_priority,
395 HttpNetworkSession* session,
396 const ProxyInfo& proxy_info,
397 bool want_spdy_over_npn,
398 const SSLConfig& ssl_config_for_origin,
399 const SSLConfig& ssl_config_for_proxy,
400 PrivacyMode privacy_mode,
401 const BoundNetLog& net_log,
402 ClientSocketHandle* socket_handle,
403 const OnHostResolutionCallback& resolution_callback,
404 const CompletionCallback& callback) {
405 DCHECK(socket_handle);
406 return InitSocketPoolHelper(
407 group_type, endpoint, request_extra_headers, request_load_flags,
408 request_priority, session, proxy_info, want_spdy_over_npn,
409 ssl_config_for_origin, ssl_config_for_proxy, /*force_tunnel=*/false,
410 privacy_mode, net_log, 0, socket_handle,
411 HttpNetworkSession::NORMAL_SOCKET_POOL, resolution_callback, callback);
414 int InitSocketHandleForWebSocketRequest(
415 ClientSocketPoolManager::SocketGroupType group_type,
416 const HostPortPair& endpoint,
417 const HttpRequestHeaders& request_extra_headers,
418 int request_load_flags,
419 RequestPriority request_priority,
420 HttpNetworkSession* session,
421 const ProxyInfo& proxy_info,
422 bool want_spdy_over_npn,
423 const SSLConfig& ssl_config_for_origin,
424 const SSLConfig& ssl_config_for_proxy,
425 PrivacyMode privacy_mode,
426 const BoundNetLog& net_log,
427 ClientSocketHandle* socket_handle,
428 const OnHostResolutionCallback& resolution_callback,
429 const CompletionCallback& callback) {
430 DCHECK(socket_handle);
431 return InitSocketPoolHelper(
432 group_type, endpoint, request_extra_headers, request_load_flags,
433 request_priority, session, proxy_info, want_spdy_over_npn,
434 ssl_config_for_origin, ssl_config_for_proxy, /*force_tunnel=*/true,
435 privacy_mode, net_log, 0, socket_handle,
436 HttpNetworkSession::WEBSOCKET_SOCKET_POOL, resolution_callback, callback);
439 int InitSocketHandleForRawConnect(
440 const HostPortPair& host_port_pair,
441 HttpNetworkSession* session,
442 const ProxyInfo& proxy_info,
443 const SSLConfig& ssl_config_for_origin,
444 const SSLConfig& ssl_config_for_proxy,
445 PrivacyMode privacy_mode,
446 const BoundNetLog& net_log,
447 ClientSocketHandle* socket_handle,
448 const CompletionCallback& callback) {
449 DCHECK(socket_handle);
450 HttpRequestHeaders request_extra_headers;
451 int request_load_flags = 0;
452 RequestPriority request_priority = MEDIUM;
453 return InitSocketPoolHelper(
454 ClientSocketPoolManager::NORMAL_GROUP, host_port_pair,
455 request_extra_headers, request_load_flags, request_priority, session,
456 proxy_info, false, ssl_config_for_origin, ssl_config_for_proxy,
457 /*force_tunnel=*/true, privacy_mode, net_log, 0, socket_handle,
458 HttpNetworkSession::NORMAL_SOCKET_POOL, OnHostResolutionCallback(),
459 callback);
462 int InitSocketHandleForTlsConnect(const HostPortPair& endpoint,
463 HttpNetworkSession* session,
464 const ProxyInfo& proxy_info,
465 const SSLConfig& ssl_config_for_origin,
466 const SSLConfig& ssl_config_for_proxy,
467 PrivacyMode privacy_mode,
468 const BoundNetLog& net_log,
469 ClientSocketHandle* socket_handle,
470 const CompletionCallback& callback) {
471 DCHECK(socket_handle);
472 HttpRequestHeaders request_extra_headers;
473 int request_load_flags = 0;
474 RequestPriority request_priority = MEDIUM;
475 return InitSocketPoolHelper(
476 ClientSocketPoolManager::SSL_GROUP, endpoint, request_extra_headers,
477 request_load_flags, request_priority, session, proxy_info,
478 /*want_spdy_over_npn=*/false, ssl_config_for_origin, ssl_config_for_proxy,
479 /*force_tunnel=*/true, privacy_mode, net_log, 0, socket_handle,
480 HttpNetworkSession::NORMAL_SOCKET_POOL, OnHostResolutionCallback(),
481 callback);
484 int PreconnectSocketsForHttpRequest(
485 ClientSocketPoolManager::SocketGroupType group_type,
486 const HostPortPair& endpoint,
487 const HttpRequestHeaders& request_extra_headers,
488 int request_load_flags,
489 RequestPriority request_priority,
490 HttpNetworkSession* session,
491 const ProxyInfo& proxy_info,
492 bool want_spdy_over_npn,
493 const SSLConfig& ssl_config_for_origin,
494 const SSLConfig& ssl_config_for_proxy,
495 PrivacyMode privacy_mode,
496 const BoundNetLog& net_log,
497 int num_preconnect_streams) {
498 return InitSocketPoolHelper(
499 group_type, endpoint, request_extra_headers, request_load_flags,
500 request_priority, session, proxy_info, want_spdy_over_npn,
501 ssl_config_for_origin, ssl_config_for_proxy, /*force_tunnel=*/false,
502 privacy_mode, net_log, num_preconnect_streams, NULL,
503 HttpNetworkSession::NORMAL_SOCKET_POOL, OnHostResolutionCallback(),
504 CompletionCallback());
507 } // namespace net