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 socket_performance_watcher_factory(NULL
),
83 ignore_certificate_errors(false),
84 testing_fixed_http_port(0),
85 testing_fixed_https_port(0),
86 enable_tcp_fast_open_for_ssl(false),
87 enable_spdy_compression(true),
88 enable_spdy_ping_based_connection_checking(true),
89 spdy_default_protocol(kProtoUnknown
),
90 spdy_session_max_recv_window_size(kSpdySessionMaxRecvWindowSize
),
91 spdy_stream_max_recv_window_size(kSpdyStreamMaxRecvWindowSize
),
92 spdy_initial_max_concurrent_streams(0),
93 time_func(&base::TimeTicks::Now
),
94 use_alternative_services(false),
95 alternative_service_probability_threshold(1),
97 enable_insecure_quic(false),
98 enable_quic_for_proxies(false),
99 enable_quic_port_selection(true),
100 quic_always_require_handshake_confirmation(false),
101 quic_disable_connection_pooling(false),
102 quic_load_server_info_timeout_srtt_multiplier(0.25f
),
103 quic_enable_connection_racing(false),
104 quic_enable_non_blocking_io(false),
105 quic_disable_disk_cache(false),
106 quic_prefer_aes(false),
107 quic_max_number_of_lossy_connections(4),
108 quic_packet_loss_threshold(0.5f
),
109 quic_socket_receive_buffer_size(kQuicSocketReceiveBufferSize
),
112 quic_max_packet_length(kDefaultMaxPacketSize
),
113 enable_user_alternate_protocol_ports(false),
114 quic_crypto_client_stream_factory(NULL
),
115 quic_max_recent_disabled_reasons(kQuicMaxRecentDisabledReasons
),
116 quic_threshold_public_resets_post_handshake(0),
117 quic_threshold_timeouts_streams_open(0),
118 proxy_delegate(NULL
) {
119 quic_supported_versions
.push_back(QUIC_VERSION_25
);
122 HttpNetworkSession::Params::~Params() {}
124 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
125 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
126 : net_log_(params
.net_log
),
127 network_delegate_(params
.network_delegate
),
128 http_server_properties_(params
.http_server_properties
),
129 cert_verifier_(params
.cert_verifier
),
130 http_auth_handler_factory_(params
.http_auth_handler_factory
),
131 proxy_service_(params
.proxy_service
),
132 ssl_config_service_(params
.ssl_config_service
),
133 normal_socket_pool_manager_(
134 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
135 websocket_socket_pool_manager_(
136 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
137 quic_stream_factory_(
138 params
.host_resolver
,
139 params
.client_socket_factory
140 ? params
.client_socket_factory
141 : ClientSocketFactory::GetDefaultFactory(),
142 params
.http_server_properties
,
143 params
.cert_verifier
,
144 params
.cert_policy_enforcer
,
145 params
.channel_id_service
,
146 params
.transport_security_state
,
147 params
.quic_crypto_client_stream_factory
,
148 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
149 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
150 params
.quic_max_packet_length
,
151 params
.quic_user_agent_id
,
152 params
.quic_supported_versions
,
153 params
.enable_quic_port_selection
,
154 params
.quic_always_require_handshake_confirmation
,
155 params
.quic_disable_connection_pooling
,
156 params
.quic_load_server_info_timeout_srtt_multiplier
,
157 params
.quic_enable_connection_racing
,
158 params
.quic_enable_non_blocking_io
,
159 params
.quic_disable_disk_cache
,
160 params
.quic_prefer_aes
,
161 params
.quic_max_number_of_lossy_connections
,
162 params
.quic_packet_loss_threshold
,
163 params
.quic_max_recent_disabled_reasons
,
164 params
.quic_threshold_public_resets_post_handshake
,
165 params
.quic_threshold_timeouts_streams_open
,
166 params
.quic_socket_receive_buffer_size
,
167 params
.quic_connection_options
),
168 spdy_session_pool_(params
.host_resolver
,
169 params
.ssl_config_service
,
170 params
.http_server_properties
,
171 params
.transport_security_state
,
172 params
.enable_spdy_compression
,
173 params
.enable_spdy_ping_based_connection_checking
,
174 params
.spdy_default_protocol
,
175 params
.spdy_session_max_recv_window_size
,
176 params
.spdy_stream_max_recv_window_size
,
177 params
.spdy_initial_max_concurrent_streams
,
179 params
.trusted_spdy_proxy
),
180 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
181 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
183 DCHECK(proxy_service_
);
184 DCHECK(ssl_config_service_
.get());
185 CHECK(http_server_properties_
);
187 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
188 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
189 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
192 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
193 // AlternateProtocol.
194 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
195 it
!= params_
.next_protos
.end(); ++it
) {
196 NextProto proto
= *it
;
198 // Add the protocol to the TLS next protocol list, except for QUIC
199 // since it uses UDP.
200 if (proto
!= kProtoQUIC1SPDY3
) {
201 next_protos_
.push_back(proto
);
204 // Enable the corresponding alternate protocol, except for HTTP
205 // which has not corresponding alternative.
206 if (proto
!= kProtoHTTP11
) {
207 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
208 if (!IsAlternateProtocolValid(alternate
)) {
209 NOTREACHED() << "Invalid next proto: " << proto
;
212 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
217 http_server_properties_
->SetAlternativeServiceProbabilityThreshold(
218 params
.alternative_service_probability_threshold
);
221 HttpNetworkSession::~HttpNetworkSession() {
222 STLDeleteElements(&response_drainers_
);
223 spdy_session_pool_
.CloseAllSessions();
226 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
227 DCHECK(!ContainsKey(response_drainers_
, drainer
));
228 response_drainers_
.insert(drainer
);
231 void HttpNetworkSession::RemoveResponseDrainer(
232 HttpResponseBodyDrainer
* drainer
) {
233 DCHECK(ContainsKey(response_drainers_
, drainer
));
234 response_drainers_
.erase(drainer
);
237 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
238 SocketPoolType pool_type
) {
239 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
242 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
243 SocketPoolType pool_type
) {
244 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
247 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
248 SocketPoolType pool_type
,
249 const HostPortPair
& socks_proxy
) {
250 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
254 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
255 SocketPoolType pool_type
,
256 const HostPortPair
& http_proxy
) {
257 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
260 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
261 SocketPoolType pool_type
,
262 const HostPortPair
& proxy_server
) {
263 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
267 scoped_ptr
<base::Value
> HttpNetworkSession::SocketPoolInfoToValue() const {
268 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
269 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
272 scoped_ptr
<base::Value
> HttpNetworkSession::SpdySessionPoolInfoToValue() const {
273 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
276 scoped_ptr
<base::Value
> HttpNetworkSession::QuicInfoToValue() const {
277 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
278 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
279 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
280 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
281 dict
->SetBoolean("enable_quic_port_selection",
282 params_
.enable_quic_port_selection
);
283 scoped_ptr
<base::ListValue
> connection_options(new base::ListValue
);
284 for (QuicTagVector::const_iterator it
=
285 params_
.quic_connection_options
.begin();
286 it
!= params_
.quic_connection_options
.end(); ++it
) {
287 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
289 dict
->Set("connection_options", connection_options
.Pass());
290 dict
->SetString("origin_to_force_quic_on",
291 params_
.origin_to_force_quic_on
.ToString());
292 dict
->SetDouble("alternative_service_probability_threshold",
293 params_
.alternative_service_probability_threshold
);
294 dict
->SetString("disabled_reason",
295 quic_stream_factory_
.QuicDisabledReasonString());
299 void HttpNetworkSession::CloseAllConnections() {
300 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
301 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
302 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
303 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
306 void HttpNetworkSession::CloseIdleConnections() {
307 normal_socket_pool_manager_
->CloseIdleSockets();
308 websocket_socket_pool_manager_
->CloseIdleSockets();
309 spdy_session_pool_
.CloseCurrentIdleSessions();
312 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
313 DCHECK(IsAlternateProtocolValid(protocol
));
314 return enabled_protocols_
[
315 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
318 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
319 if (HttpStreamFactory::spdy_enabled()) {
320 *next_protos
= next_protos_
;
322 next_protos
->clear();
326 bool HttpNetworkSession::HasSpdyExclusion(
327 HostPortPair host_port_pair
) const {
328 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
329 params_
.forced_spdy_exclusions
.end();
332 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
333 SocketPoolType pool_type
) {
335 case NORMAL_SOCKET_POOL
:
336 return normal_socket_pool_manager_
.get();
337 case WEBSOCKET_SOCKET_POOL
:
338 return websocket_socket_pool_manager_
.get();