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/http/http_network_session.h"
9 #include "base/compiler_specific.h"
10 #include "base/debug/stack_trace.h"
11 #include "base/logging.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_util.h"
15 #include "base/values.h"
16 #include "net/http/http_auth_handler_factory.h"
17 #include "net/http/http_response_body_drainer.h"
18 #include "net/http/http_stream_factory_impl.h"
19 #include "net/http/url_security_manager.h"
20 #include "net/proxy/proxy_service.h"
21 #include "net/quic/crypto/quic_random.h"
22 #include "net/quic/quic_clock.h"
23 #include "net/quic/quic_crypto_client_stream_factory.h"
24 #include "net/quic/quic_protocol.h"
25 #include "net/quic/quic_stream_factory.h"
26 #include "net/quic/quic_utils.h"
27 #include "net/socket/client_socket_factory.h"
28 #include "net/socket/client_socket_pool_manager_impl.h"
29 #include "net/socket/next_proto.h"
30 #include "net/socket/ssl_client_socket.h"
31 #include "net/spdy/spdy_session_pool.h"
37 ClientSocketPoolManager
* CreateSocketPoolManager(
38 HttpNetworkSession::SocketPoolType pool_type
,
39 const HttpNetworkSession::Params
& params
) {
40 // TODO(yutak): Differentiate WebSocket pool manager and allow more
41 // simultaneous connections for WebSockets.
42 return new ClientSocketPoolManagerImpl(
44 params
.client_socket_factory
? params
.client_socket_factory
45 : ClientSocketFactory::GetDefaultFactory(),
46 params
.host_resolver
, params
.cert_verifier
, params
.channel_id_service
,
47 params
.transport_security_state
, params
.cert_transparency_verifier
,
48 params
.cert_policy_enforcer
, params
.ssl_session_cache_shard
,
49 params
.ssl_config_service
, pool_type
);
52 } // unnamed namespace
54 // The maximum receive window sizes for HTTP/2 sessions and streams.
55 const int32 kSpdySessionMaxRecvWindowSize
= 15 * 1024 * 1024; // 15 MB
56 const int32 kSpdyStreamMaxRecvWindowSize
= 6 * 1024 * 1024; // 6 MB
57 // QUIC's socket receive buffer size.
58 // We should adaptively set this buffer size, but for now, we'll use a size
59 // that seems large enough to receive data at line rate for most connections,
60 // and does not consume "too much" memory.
61 const int32 kQuicSocketReceiveBufferSize
= 1024 * 1024; // 1MB
63 HttpNetworkSession::Params::Params()
64 : client_socket_factory(NULL
),
67 cert_policy_enforcer(NULL
),
68 channel_id_service(NULL
),
69 transport_security_state(NULL
),
70 cert_transparency_verifier(NULL
),
72 ssl_config_service(NULL
),
73 http_auth_handler_factory(NULL
),
74 network_delegate(NULL
),
76 host_mapping_rules(NULL
),
77 ignore_certificate_errors(false),
78 use_stale_while_revalidate(false),
79 testing_fixed_http_port(0),
80 testing_fixed_https_port(0),
81 enable_tcp_fast_open_for_ssl(false),
82 enable_spdy_compression(true),
83 enable_spdy_ping_based_connection_checking(true),
84 spdy_default_protocol(kProtoUnknown
),
85 spdy_session_max_recv_window_size(kSpdySessionMaxRecvWindowSize
),
86 spdy_stream_max_recv_window_size(kSpdyStreamMaxRecvWindowSize
),
87 spdy_initial_max_concurrent_streams(0),
88 time_func(&base::TimeTicks::Now
),
89 use_alternate_protocols(false),
90 alternative_service_probability_threshold(1),
92 disable_insecure_quic(false),
93 enable_quic_for_proxies(false),
94 enable_quic_port_selection(true),
95 quic_always_require_handshake_confirmation(false),
96 quic_disable_connection_pooling(false),
97 quic_load_server_info_timeout_srtt_multiplier(0.25f
),
98 quic_enable_connection_racing(false),
99 quic_enable_non_blocking_io(false),
100 quic_disable_disk_cache(false),
101 quic_max_number_of_lossy_connections(0),
102 quic_packet_loss_threshold(1.0f
),
103 quic_socket_receive_buffer_size(kQuicSocketReceiveBufferSize
),
106 quic_max_packet_length(kDefaultMaxPacketSize
),
107 enable_user_alternate_protocol_ports(false),
108 quic_crypto_client_stream_factory(NULL
),
109 proxy_delegate(NULL
) {
110 quic_supported_versions
.push_back(QUIC_VERSION_25
);
113 HttpNetworkSession::Params::~Params() {}
115 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
116 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
117 : net_log_(params
.net_log
),
118 network_delegate_(params
.network_delegate
),
119 http_server_properties_(params
.http_server_properties
),
120 cert_verifier_(params
.cert_verifier
),
121 http_auth_handler_factory_(params
.http_auth_handler_factory
),
122 proxy_service_(params
.proxy_service
),
123 ssl_config_service_(params
.ssl_config_service
),
124 normal_socket_pool_manager_(
125 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
126 websocket_socket_pool_manager_(
127 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
128 quic_stream_factory_(
129 params
.host_resolver
,
130 params
.client_socket_factory
131 ? params
.client_socket_factory
132 : ClientSocketFactory::GetDefaultFactory(),
133 params
.http_server_properties
,
134 params
.cert_verifier
,
135 params
.channel_id_service
,
136 params
.transport_security_state
,
137 params
.quic_crypto_client_stream_factory
,
138 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
139 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
140 params
.quic_max_packet_length
,
141 params
.quic_user_agent_id
,
142 params
.quic_supported_versions
,
143 params
.enable_quic_port_selection
,
144 params
.quic_always_require_handshake_confirmation
,
145 params
.quic_disable_connection_pooling
,
146 params
.quic_load_server_info_timeout_srtt_multiplier
,
147 params
.quic_enable_connection_racing
,
148 params
.quic_enable_non_blocking_io
,
149 params
.quic_disable_disk_cache
,
150 params
.quic_max_number_of_lossy_connections
,
151 params
.quic_packet_loss_threshold
,
152 params
.quic_socket_receive_buffer_size
,
153 params
.quic_connection_options
),
154 spdy_session_pool_(params
.host_resolver
,
155 params
.ssl_config_service
,
156 params
.http_server_properties
,
157 params
.transport_security_state
,
158 params
.enable_spdy_compression
,
159 params
.enable_spdy_ping_based_connection_checking
,
160 params
.spdy_default_protocol
,
161 params
.spdy_session_max_recv_window_size
,
162 params
.spdy_stream_max_recv_window_size
,
163 params
.spdy_initial_max_concurrent_streams
,
165 params
.trusted_spdy_proxy
),
166 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
167 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
169 DCHECK(proxy_service_
);
170 DCHECK(ssl_config_service_
.get());
171 CHECK(http_server_properties_
);
173 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
174 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
175 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
178 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
179 // AlternateProtocol.
180 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
181 it
!= params_
.next_protos
.end(); ++it
) {
182 NextProto proto
= *it
;
184 // Add the protocol to the TLS next protocol list, except for QUIC
185 // since it uses UDP.
186 if (proto
!= kProtoQUIC1SPDY3
) {
187 next_protos_
.push_back(proto
);
190 // Enable the corresponding alternate protocol, except for HTTP
191 // which has not corresponding alternative.
192 if (proto
!= kProtoHTTP11
) {
193 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
194 if (!IsAlternateProtocolValid(alternate
)) {
195 NOTREACHED() << "Invalid next proto: " << proto
;
198 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
203 http_server_properties_
->SetAlternativeServiceProbabilityThreshold(
204 params
.alternative_service_probability_threshold
);
207 HttpNetworkSession::~HttpNetworkSession() {
208 STLDeleteElements(&response_drainers_
);
209 spdy_session_pool_
.CloseAllSessions();
212 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
213 DCHECK(!ContainsKey(response_drainers_
, drainer
));
214 response_drainers_
.insert(drainer
);
217 void HttpNetworkSession::RemoveResponseDrainer(
218 HttpResponseBodyDrainer
* drainer
) {
219 DCHECK(ContainsKey(response_drainers_
, drainer
));
220 response_drainers_
.erase(drainer
);
223 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
224 SocketPoolType pool_type
) {
225 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
228 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
229 SocketPoolType pool_type
) {
230 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
233 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
234 SocketPoolType pool_type
,
235 const HostPortPair
& socks_proxy
) {
236 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
240 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
241 SocketPoolType pool_type
,
242 const HostPortPair
& http_proxy
) {
243 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
246 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
247 SocketPoolType pool_type
,
248 const HostPortPair
& proxy_server
) {
249 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
253 scoped_ptr
<base::Value
> HttpNetworkSession::SocketPoolInfoToValue() const {
254 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
255 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
258 scoped_ptr
<base::Value
> HttpNetworkSession::SpdySessionPoolInfoToValue() const {
259 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
262 scoped_ptr
<base::Value
> HttpNetworkSession::QuicInfoToValue() const {
263 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
264 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
265 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
266 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
267 dict
->SetBoolean("enable_quic_port_selection",
268 params_
.enable_quic_port_selection
);
269 scoped_ptr
<base::ListValue
> connection_options(new base::ListValue
);
270 for (QuicTagVector::const_iterator it
=
271 params_
.quic_connection_options
.begin();
272 it
!= params_
.quic_connection_options
.end(); ++it
) {
273 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
275 dict
->Set("connection_options", connection_options
.Pass());
276 dict
->SetString("origin_to_force_quic_on",
277 params_
.origin_to_force_quic_on
.ToString());
278 dict
->SetDouble("alternative_service_probability_threshold",
279 params_
.alternative_service_probability_threshold
);
283 void HttpNetworkSession::CloseAllConnections() {
284 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
285 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
286 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
287 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
290 void HttpNetworkSession::CloseIdleConnections() {
291 normal_socket_pool_manager_
->CloseIdleSockets();
292 websocket_socket_pool_manager_
->CloseIdleSockets();
293 spdy_session_pool_
.CloseCurrentIdleSessions();
296 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
297 DCHECK(IsAlternateProtocolValid(protocol
));
298 return enabled_protocols_
[
299 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
302 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
303 if (HttpStreamFactory::spdy_enabled()) {
304 *next_protos
= next_protos_
;
306 next_protos
->clear();
310 bool HttpNetworkSession::HasSpdyExclusion(
311 HostPortPair host_port_pair
) const {
312 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
313 params_
.forced_spdy_exclusions
.end();
316 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
317 SocketPoolType pool_type
) {
319 case NORMAL_SOCKET_POOL
:
320 return normal_socket_pool_manager_
.get();
321 case WEBSOCKET_SOCKET_POOL
:
322 return websocket_socket_pool_manager_
.get();