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_alternative_services(false),
94 alternative_service_probability_threshold(1),
96 enable_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
.cert_policy_enforcer
,
144 params
.channel_id_service
,
145 params
.transport_security_state
,
146 params
.quic_crypto_client_stream_factory
,
147 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
148 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
149 params
.quic_max_packet_length
,
150 params
.quic_user_agent_id
,
151 params
.quic_supported_versions
,
152 params
.enable_quic_port_selection
,
153 params
.quic_always_require_handshake_confirmation
,
154 params
.quic_disable_connection_pooling
,
155 params
.quic_load_server_info_timeout_srtt_multiplier
,
156 params
.quic_enable_connection_racing
,
157 params
.quic_enable_non_blocking_io
,
158 params
.quic_disable_disk_cache
,
159 params
.quic_prefer_aes
,
160 params
.quic_max_number_of_lossy_connections
,
161 params
.quic_packet_loss_threshold
,
162 params
.quic_max_recent_disabled_reasons
,
163 params
.quic_threshold_public_resets_post_handshake
,
164 params
.quic_threshold_timeouts_streams_open
,
165 params
.quic_socket_receive_buffer_size
,
166 params
.quic_connection_options
),
167 spdy_session_pool_(params
.host_resolver
,
168 params
.ssl_config_service
,
169 params
.http_server_properties
,
170 params
.transport_security_state
,
171 params
.enable_spdy_compression
,
172 params
.enable_spdy_ping_based_connection_checking
,
173 params
.spdy_default_protocol
,
174 params
.spdy_session_max_recv_window_size
,
175 params
.spdy_stream_max_recv_window_size
,
176 params
.spdy_initial_max_concurrent_streams
,
178 params
.trusted_spdy_proxy
),
179 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
180 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
182 DCHECK(proxy_service_
);
183 DCHECK(ssl_config_service_
.get());
184 CHECK(http_server_properties_
);
186 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
187 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
188 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
191 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
192 // AlternateProtocol.
193 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
194 it
!= params_
.next_protos
.end(); ++it
) {
195 NextProto proto
= *it
;
197 // Add the protocol to the TLS next protocol list, except for QUIC
198 // since it uses UDP.
199 if (proto
!= kProtoQUIC1SPDY3
) {
200 next_protos_
.push_back(proto
);
203 // Enable the corresponding alternate protocol, except for HTTP
204 // which has not corresponding alternative.
205 if (proto
!= kProtoHTTP11
) {
206 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
207 if (!IsAlternateProtocolValid(alternate
)) {
208 NOTREACHED() << "Invalid next proto: " << proto
;
211 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
216 http_server_properties_
->SetAlternativeServiceProbabilityThreshold(
217 params
.alternative_service_probability_threshold
);
220 HttpNetworkSession::~HttpNetworkSession() {
221 STLDeleteElements(&response_drainers_
);
222 spdy_session_pool_
.CloseAllSessions();
225 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
226 DCHECK(!ContainsKey(response_drainers_
, drainer
));
227 response_drainers_
.insert(drainer
);
230 void HttpNetworkSession::RemoveResponseDrainer(
231 HttpResponseBodyDrainer
* drainer
) {
232 DCHECK(ContainsKey(response_drainers_
, drainer
));
233 response_drainers_
.erase(drainer
);
236 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
237 SocketPoolType pool_type
) {
238 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
241 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
242 SocketPoolType pool_type
) {
243 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
246 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
247 SocketPoolType pool_type
,
248 const HostPortPair
& socks_proxy
) {
249 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
253 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
254 SocketPoolType pool_type
,
255 const HostPortPair
& http_proxy
) {
256 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
259 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
260 SocketPoolType pool_type
,
261 const HostPortPair
& proxy_server
) {
262 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
266 scoped_ptr
<base::Value
> HttpNetworkSession::SocketPoolInfoToValue() const {
267 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
268 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
271 scoped_ptr
<base::Value
> HttpNetworkSession::SpdySessionPoolInfoToValue() const {
272 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
275 scoped_ptr
<base::Value
> HttpNetworkSession::QuicInfoToValue() const {
276 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
277 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
278 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
279 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
280 dict
->SetBoolean("enable_quic_port_selection",
281 params_
.enable_quic_port_selection
);
282 scoped_ptr
<base::ListValue
> connection_options(new base::ListValue
);
283 for (QuicTagVector::const_iterator it
=
284 params_
.quic_connection_options
.begin();
285 it
!= params_
.quic_connection_options
.end(); ++it
) {
286 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
288 dict
->Set("connection_options", connection_options
.Pass());
289 dict
->SetString("origin_to_force_quic_on",
290 params_
.origin_to_force_quic_on
.ToString());
291 dict
->SetDouble("alternative_service_probability_threshold",
292 params_
.alternative_service_probability_threshold
);
293 dict
->SetString("disabled_reason",
294 quic_stream_factory_
.QuicDisabledReasonString());
298 void HttpNetworkSession::CloseAllConnections() {
299 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
300 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
301 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
302 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
305 void HttpNetworkSession::CloseIdleConnections() {
306 normal_socket_pool_manager_
->CloseIdleSockets();
307 websocket_socket_pool_manager_
->CloseIdleSockets();
308 spdy_session_pool_
.CloseCurrentIdleSessions();
311 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
312 DCHECK(IsAlternateProtocolValid(protocol
));
313 return enabled_protocols_
[
314 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
317 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
318 if (HttpStreamFactory::spdy_enabled()) {
319 *next_protos
= next_protos_
;
321 next_protos
->clear();
325 bool HttpNetworkSession::HasSpdyExclusion(
326 HostPortPair host_port_pair
) const {
327 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
328 params_
.forced_spdy_exclusions
.end();
331 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
332 SocketPoolType pool_type
) {
334 case NORMAL_SOCKET_POOL
:
335 return normal_socket_pool_manager_
.get();
336 case WEBSOCKET_SOCKET_POOL
:
337 return websocket_socket_pool_manager_
.get();