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 // Number of recent connections to consider for certain thresholds
64 // that trigger disabling QUIC. E.g. disable QUIC if PUBLIC_RESET was
65 // received post handshake for at least 2 of 20 recent connections.
66 const int32 kQuicMaxRecentDisabledReasons
= 20;
68 HttpNetworkSession::Params::Params()
69 : client_socket_factory(NULL
),
72 cert_policy_enforcer(NULL
),
73 channel_id_service(NULL
),
74 transport_security_state(NULL
),
75 cert_transparency_verifier(NULL
),
77 ssl_config_service(NULL
),
78 http_auth_handler_factory(NULL
),
79 network_delegate(NULL
),
81 host_mapping_rules(NULL
),
82 ignore_certificate_errors(false),
83 testing_fixed_http_port(0),
84 testing_fixed_https_port(0),
85 enable_tcp_fast_open_for_ssl(false),
86 enable_spdy_compression(true),
87 enable_spdy_ping_based_connection_checking(true),
88 spdy_default_protocol(kProtoUnknown
),
89 spdy_session_max_recv_window_size(kSpdySessionMaxRecvWindowSize
),
90 spdy_stream_max_recv_window_size(kSpdyStreamMaxRecvWindowSize
),
91 spdy_initial_max_concurrent_streams(0),
92 time_func(&base::TimeTicks::Now
),
93 use_alternate_protocols(false),
94 alternative_service_probability_threshold(1),
96 disable_insecure_quic(false),
97 enable_quic_for_proxies(false),
98 enable_quic_port_selection(true),
99 quic_always_require_handshake_confirmation(false),
100 quic_disable_connection_pooling(false),
101 quic_load_server_info_timeout_srtt_multiplier(0.25f
),
102 quic_enable_connection_racing(false),
103 quic_enable_non_blocking_io(false),
104 quic_disable_disk_cache(false),
105 quic_prefer_aes(false),
106 quic_max_number_of_lossy_connections(0),
107 quic_packet_loss_threshold(1.0f
),
108 quic_socket_receive_buffer_size(kQuicSocketReceiveBufferSize
),
111 quic_max_packet_length(kDefaultMaxPacketSize
),
112 enable_user_alternate_protocol_ports(false),
113 quic_crypto_client_stream_factory(NULL
),
114 quic_max_recent_disabled_reasons(kQuicMaxRecentDisabledReasons
),
115 quic_threshold_public_resets_post_handshake(0),
116 quic_threshold_timeouts_streams_open(0),
117 proxy_delegate(NULL
) {
118 quic_supported_versions
.push_back(QUIC_VERSION_25
);
121 HttpNetworkSession::Params::~Params() {}
123 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
124 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
125 : net_log_(params
.net_log
),
126 network_delegate_(params
.network_delegate
),
127 http_server_properties_(params
.http_server_properties
),
128 cert_verifier_(params
.cert_verifier
),
129 http_auth_handler_factory_(params
.http_auth_handler_factory
),
130 proxy_service_(params
.proxy_service
),
131 ssl_config_service_(params
.ssl_config_service
),
132 normal_socket_pool_manager_(
133 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
134 websocket_socket_pool_manager_(
135 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
136 quic_stream_factory_(
137 params
.host_resolver
,
138 params
.client_socket_factory
139 ? params
.client_socket_factory
140 : ClientSocketFactory::GetDefaultFactory(),
141 params
.http_server_properties
,
142 params
.cert_verifier
,
143 params
.channel_id_service
,
144 params
.transport_security_state
,
145 params
.quic_crypto_client_stream_factory
,
146 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
147 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
148 params
.quic_max_packet_length
,
149 params
.quic_user_agent_id
,
150 params
.quic_supported_versions
,
151 params
.enable_quic_port_selection
,
152 params
.quic_always_require_handshake_confirmation
,
153 params
.quic_disable_connection_pooling
,
154 params
.quic_load_server_info_timeout_srtt_multiplier
,
155 params
.quic_enable_connection_racing
,
156 params
.quic_enable_non_blocking_io
,
157 params
.quic_disable_disk_cache
,
158 params
.quic_prefer_aes
,
159 params
.quic_max_number_of_lossy_connections
,
160 params
.quic_packet_loss_threshold
,
161 params
.quic_max_recent_disabled_reasons
,
162 params
.quic_threshold_public_resets_post_handshake
,
163 params
.quic_threshold_timeouts_streams_open
,
164 params
.quic_socket_receive_buffer_size
,
165 params
.quic_connection_options
),
166 spdy_session_pool_(params
.host_resolver
,
167 params
.ssl_config_service
,
168 params
.http_server_properties
,
169 params
.transport_security_state
,
170 params
.enable_spdy_compression
,
171 params
.enable_spdy_ping_based_connection_checking
,
172 params
.spdy_default_protocol
,
173 params
.spdy_session_max_recv_window_size
,
174 params
.spdy_stream_max_recv_window_size
,
175 params
.spdy_initial_max_concurrent_streams
,
177 params
.trusted_spdy_proxy
),
178 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
179 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
181 DCHECK(proxy_service_
);
182 DCHECK(ssl_config_service_
.get());
183 CHECK(http_server_properties_
);
185 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
186 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
187 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
190 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
191 // AlternateProtocol.
192 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
193 it
!= params_
.next_protos
.end(); ++it
) {
194 NextProto proto
= *it
;
196 // Add the protocol to the TLS next protocol list, except for QUIC
197 // since it uses UDP.
198 if (proto
!= kProtoQUIC1SPDY3
) {
199 next_protos_
.push_back(proto
);
202 // Enable the corresponding alternate protocol, except for HTTP
203 // which has not corresponding alternative.
204 if (proto
!= kProtoHTTP11
) {
205 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
206 if (!IsAlternateProtocolValid(alternate
)) {
207 NOTREACHED() << "Invalid next proto: " << proto
;
210 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
215 http_server_properties_
->SetAlternativeServiceProbabilityThreshold(
216 params
.alternative_service_probability_threshold
);
219 HttpNetworkSession::~HttpNetworkSession() {
220 STLDeleteElements(&response_drainers_
);
221 spdy_session_pool_
.CloseAllSessions();
224 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
225 DCHECK(!ContainsKey(response_drainers_
, drainer
));
226 response_drainers_
.insert(drainer
);
229 void HttpNetworkSession::RemoveResponseDrainer(
230 HttpResponseBodyDrainer
* drainer
) {
231 DCHECK(ContainsKey(response_drainers_
, drainer
));
232 response_drainers_
.erase(drainer
);
235 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
236 SocketPoolType pool_type
) {
237 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
240 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
241 SocketPoolType pool_type
) {
242 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
245 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
246 SocketPoolType pool_type
,
247 const HostPortPair
& socks_proxy
) {
248 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
252 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
253 SocketPoolType pool_type
,
254 const HostPortPair
& http_proxy
) {
255 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
258 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
259 SocketPoolType pool_type
,
260 const HostPortPair
& proxy_server
) {
261 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
265 scoped_ptr
<base::Value
> HttpNetworkSession::SocketPoolInfoToValue() const {
266 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
267 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
270 scoped_ptr
<base::Value
> HttpNetworkSession::SpdySessionPoolInfoToValue() const {
271 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
274 scoped_ptr
<base::Value
> HttpNetworkSession::QuicInfoToValue() const {
275 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
276 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
277 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
278 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
279 dict
->SetBoolean("enable_quic_port_selection",
280 params_
.enable_quic_port_selection
);
281 scoped_ptr
<base::ListValue
> connection_options(new base::ListValue
);
282 for (QuicTagVector::const_iterator it
=
283 params_
.quic_connection_options
.begin();
284 it
!= params_
.quic_connection_options
.end(); ++it
) {
285 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
287 dict
->Set("connection_options", connection_options
.Pass());
288 dict
->SetString("origin_to_force_quic_on",
289 params_
.origin_to_force_quic_on
.ToString());
290 dict
->SetDouble("alternative_service_probability_threshold",
291 params_
.alternative_service_probability_threshold
);
292 dict
->SetString("disabled_reason",
293 quic_stream_factory_
.QuicDisabledReasonString());
297 void HttpNetworkSession::CloseAllConnections() {
298 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
299 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
300 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
301 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
304 void HttpNetworkSession::CloseIdleConnections() {
305 normal_socket_pool_manager_
->CloseIdleSockets();
306 websocket_socket_pool_manager_
->CloseIdleSockets();
307 spdy_session_pool_
.CloseCurrentIdleSessions();
310 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
311 DCHECK(IsAlternateProtocolValid(protocol
));
312 return enabled_protocols_
[
313 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
316 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
317 if (HttpStreamFactory::spdy_enabled()) {
318 *next_protos
= next_protos_
;
320 next_protos
->clear();
324 bool HttpNetworkSession::HasSpdyExclusion(
325 HostPortPair host_port_pair
) const {
326 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
327 params_
.forced_spdy_exclusions
.end();
330 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
331 SocketPoolType pool_type
) {
333 case NORMAL_SOCKET_POOL
:
334 return normal_socket_pool_manager_
.get();
335 case WEBSOCKET_SOCKET_POOL
:
336 return websocket_socket_pool_manager_
.get();