1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/socket/client_socket_pool_manager.h"
#include <string>
#include "base/logging.h"
#include "base/stringprintf.h"
#include "net/base/load_flags.h"
#include "net/http/http_network_session.h"
#include "net/http/http_proxy_client_socket_pool.h"
#include "net/http/http_request_info.h"
#include "net/http/http_stream_factory.h"
#include "net/proxy/proxy_info.h"
#include "net/socket/client_socket_handle.h"
#include "net/socket/socks_client_socket_pool.h"
#include "net/socket/ssl_client_socket_pool.h"
#include "net/socket/transport_client_socket_pool.h"
namespace net {
namespace {
// Limit of sockets of each socket pool.
int g_max_sockets_per_pool = 256;
// Default to allow up to 6 connections per host. Experiment and tuning may
// try other values (greater than 0). Too large may cause many problems, such
// as home routers blocking the connections!?!? See http://crbug.com/12066.
int g_max_sockets_per_group = 6;
// The max number of sockets to allow per proxy server. This applies both to
// http and SOCKS proxies. See http://crbug.com/12066 and
// http://crbug.com/44501 for details about proxy server connection limits.
int g_max_sockets_per_proxy_server = kDefaultMaxSocketsPerProxyServer;
// The meat of the implementation for the InitSocketHandleForHttpRequest,
// InitSocketHandleForRawConnect and PreconnectSocketsForHttpRequest methods.
int InitSocketPoolHelper(const GURL& request_url,
const HttpRequestHeaders& request_extra_headers,
int request_load_flags,
RequestPriority request_priority,
HttpNetworkSession* session,
const ProxyInfo& proxy_info,
bool force_spdy_over_ssl,
bool want_spdy_over_npn,
const SSLConfig& ssl_config_for_origin,
const SSLConfig& ssl_config_for_proxy,
bool force_tunnel,
const BoundNetLog& net_log,
int num_preconnect_streams,
ClientSocketHandle* socket_handle,
const CompletionCallback& callback) {
scoped_refptr<TransportSocketParams> tcp_params;
scoped_refptr<HttpProxySocketParams> http_proxy_params;
scoped_refptr<SOCKSSocketParams> socks_params;
scoped_ptr<HostPortPair> proxy_host_port;
bool using_ssl = request_url.SchemeIs("https") || force_spdy_over_ssl;
HostPortPair origin_host_port =
HostPortPair(request_url.HostNoBrackets(),
request_url.EffectiveIntPort());
if (!using_ssl && HttpStreamFactory::testing_fixed_http_port() != 0) {
origin_host_port.set_port(HttpStreamFactory::testing_fixed_http_port());
} else if (using_ssl && HttpStreamFactory::testing_fixed_https_port() != 0) {
origin_host_port.set_port(HttpStreamFactory::testing_fixed_https_port());
}
bool disable_resolver_cache =
request_load_flags & LOAD_BYPASS_CACHE ||
request_load_flags & LOAD_VALIDATE_CACHE ||
request_load_flags & LOAD_DISABLE_CACHE;
int load_flags = request_load_flags;
if (HttpStreamFactory::ignore_certificate_errors())
load_flags |= LOAD_IGNORE_ALL_CERT_ERRORS;
// Build the string used to uniquely identify connections of this type.
// Determine the host and port to connect to.
std::string connection_group = origin_host_port.ToString();
DCHECK(!connection_group.empty());
if (using_ssl) {
std::string prefix;
if (ssl_config_for_origin.tls1_enabled) {
prefix = "ssl/";
} else {
prefix = "sslv3/";
}
connection_group = prefix + connection_group;
}
bool ignore_limits = (request_load_flags & LOAD_IGNORE_LIMITS) != 0;
if (proxy_info.is_direct()) {
tcp_params = new TransportSocketParams(origin_host_port,
request_priority,
disable_resolver_cache,
ignore_limits);
} else {
ProxyServer proxy_server = proxy_info.proxy_server();
proxy_host_port.reset(new HostPortPair(proxy_server.host_port_pair()));
scoped_refptr<TransportSocketParams> proxy_tcp_params(
new TransportSocketParams(*proxy_host_port,
request_priority,
disable_resolver_cache,
ignore_limits));
if (proxy_info.is_http() || proxy_info.is_https()) {
std::string user_agent;
request_extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
&user_agent);
scoped_refptr<SSLSocketParams> ssl_params;
if (proxy_info.is_https()) {
// Set ssl_params, and unset proxy_tcp_params
ssl_params = new SSLSocketParams(proxy_tcp_params,
NULL,
NULL,
ProxyServer::SCHEME_DIRECT,
*proxy_host_port.get(),
ssl_config_for_proxy,
load_flags,
force_spdy_over_ssl,
want_spdy_over_npn);
proxy_tcp_params = NULL;
}
http_proxy_params =
new HttpProxySocketParams(proxy_tcp_params,
ssl_params,
request_url,
user_agent,
origin_host_port,
session->http_auth_cache(),
session->http_auth_handler_factory(),
session->spdy_session_pool(),
force_tunnel || using_ssl);
} else {
DCHECK(proxy_info.is_socks());
char socks_version;
if (proxy_server.scheme() == ProxyServer::SCHEME_SOCKS5)
socks_version = '5';
else
socks_version = '4';
connection_group = base::StringPrintf(
"socks%c/%s", socks_version, connection_group.c_str());
socks_params = new SOCKSSocketParams(proxy_tcp_params,
socks_version == '5',
origin_host_port,
request_priority);
}
}
// Deal with SSL - which layers on top of any given proxy.
if (using_ssl) {
scoped_refptr<SSLSocketParams> ssl_params =
new SSLSocketParams(tcp_params,
socks_params,
http_proxy_params,
proxy_info.proxy_server().scheme(),
origin_host_port,
ssl_config_for_origin,
load_flags,
force_spdy_over_ssl,
want_spdy_over_npn);
SSLClientSocketPool* ssl_pool = NULL;
if (proxy_info.is_direct())
ssl_pool = session->GetSSLSocketPool();
else
ssl_pool = session->GetSocketPoolForSSLWithProxy(*proxy_host_port);
if (num_preconnect_streams) {
RequestSocketsForPool(ssl_pool, connection_group, ssl_params,
num_preconnect_streams, net_log);
return OK;
}
return socket_handle->Init(connection_group, ssl_params,
request_priority, callback, ssl_pool,
net_log);
}
// Finally, get the connection started.
if (proxy_info.is_http() || proxy_info.is_https()) {
HttpProxyClientSocketPool* pool =
session->GetSocketPoolForHTTPProxy(*proxy_host_port);
if (num_preconnect_streams) {
RequestSocketsForPool(pool, connection_group, http_proxy_params,
num_preconnect_streams, net_log);
return OK;
}
return socket_handle->Init(connection_group, http_proxy_params,
request_priority, callback,
pool, net_log);
}
if (proxy_info.is_socks()) {
SOCKSClientSocketPool* pool =
session->GetSocketPoolForSOCKSProxy(*proxy_host_port);
if (num_preconnect_streams) {
RequestSocketsForPool(pool, connection_group, socks_params,
num_preconnect_streams, net_log);
return OK;
}
return socket_handle->Init(connection_group, socks_params,
request_priority, callback, pool,
net_log);
}
DCHECK(proxy_info.is_direct());
TransportClientSocketPool* pool = session->GetTransportSocketPool();
if (num_preconnect_streams) {
RequestSocketsForPool(pool, connection_group, tcp_params,
num_preconnect_streams, net_log);
return OK;
}
return socket_handle->Init(connection_group, tcp_params,
request_priority, callback,
pool, net_log);
}
} // namespace
ClientSocketPoolManager::ClientSocketPoolManager() {}
ClientSocketPoolManager::~ClientSocketPoolManager() {}
// static
int ClientSocketPoolManager::max_sockets_per_pool() {
return g_max_sockets_per_pool;
}
// static
void ClientSocketPoolManager::set_max_sockets_per_pool(int socket_count) {
DCHECK_LT(0, socket_count);
DCHECK_GT(1000, socket_count); // Sanity check.
g_max_sockets_per_pool = socket_count;
DCHECK_GE(g_max_sockets_per_pool, g_max_sockets_per_group);
}
// static
int ClientSocketPoolManager::max_sockets_per_group() {
return g_max_sockets_per_group;
}
// static
void ClientSocketPoolManager::set_max_sockets_per_group(int socket_count) {
DCHECK_LT(0, socket_count);
// The following is a sanity check... but we should NEVER be near this value.
DCHECK_GT(100, socket_count);
g_max_sockets_per_group = socket_count;
DCHECK_GE(g_max_sockets_per_pool, g_max_sockets_per_group);
DCHECK_GE(g_max_sockets_per_proxy_server, g_max_sockets_per_group);
}
// static
int ClientSocketPoolManager::max_sockets_per_proxy_server() {
return g_max_sockets_per_proxy_server;
}
// static
void ClientSocketPoolManager::set_max_sockets_per_proxy_server(
int socket_count) {
DCHECK_LT(0, socket_count);
DCHECK_GT(100, socket_count); // Sanity check.
// Assert this case early on. The max number of sockets per group cannot
// exceed the max number of sockets per proxy server.
DCHECK_LE(g_max_sockets_per_group, socket_count);
g_max_sockets_per_proxy_server = socket_count;
}
int InitSocketHandleForHttpRequest(
const GURL& request_url,
const HttpRequestHeaders& request_extra_headers,
int request_load_flags,
RequestPriority request_priority,
HttpNetworkSession* session,
const ProxyInfo& proxy_info,
bool force_spdy_over_ssl,
bool want_spdy_over_npn,
const SSLConfig& ssl_config_for_origin,
const SSLConfig& ssl_config_for_proxy,
const BoundNetLog& net_log,
ClientSocketHandle* socket_handle,
const CompletionCallback& callback) {
DCHECK(socket_handle);
return InitSocketPoolHelper(
request_url, request_extra_headers, request_load_flags, request_priority,
session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn,
ssl_config_for_origin, ssl_config_for_proxy, false, net_log, 0,
socket_handle, callback);
}
int InitSocketHandleForRawConnect(
const HostPortPair& host_port_pair,
HttpNetworkSession* session,
const ProxyInfo& proxy_info,
const SSLConfig& ssl_config_for_origin,
const SSLConfig& ssl_config_for_proxy,
const BoundNetLog& net_log,
ClientSocketHandle* socket_handle,
const CompletionCallback& callback) {
DCHECK(socket_handle);
// Synthesize an HttpRequestInfo.
GURL request_url = GURL("http://" + host_port_pair.ToString());
HttpRequestHeaders request_extra_headers;
int request_load_flags = 0;
RequestPriority request_priority = MEDIUM;
return InitSocketPoolHelper(
request_url, request_extra_headers, request_load_flags, request_priority,
session, proxy_info, false, false, ssl_config_for_origin,
ssl_config_for_proxy, true, net_log, 0, socket_handle, callback);
}
int PreconnectSocketsForHttpRequest(
const GURL& request_url,
const HttpRequestHeaders& request_extra_headers,
int request_load_flags,
RequestPriority request_priority,
HttpNetworkSession* session,
const ProxyInfo& proxy_info,
bool force_spdy_over_ssl,
bool want_spdy_over_npn,
const SSLConfig& ssl_config_for_origin,
const SSLConfig& ssl_config_for_proxy,
const BoundNetLog& net_log,
int num_preconnect_streams) {
return InitSocketPoolHelper(
request_url, request_extra_headers, request_load_flags, request_priority,
session, proxy_info, force_spdy_over_ssl, want_spdy_over_npn,
ssl_config_for_origin, ssl_config_for_proxy, false, net_log,
num_preconnect_streams, NULL, CompletionCallback());
}
} // namespace net
|