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/hpack_huffman_aggregator.h"
32 #include "net/spdy/spdy_session_pool.h"
36 net::ClientSocketPoolManager
* CreateSocketPoolManager(
37 net::HttpNetworkSession::SocketPoolType pool_type
,
38 const net::HttpNetworkSession::Params
& params
) {
39 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed
40 tracked_objects::ScopedTracker
tracking_profile(
41 FROM_HERE_WITH_EXPLICIT_FUNCTION(
42 "454983 CreateSocketPoolManager"));
43 // TODO(yutak): Differentiate WebSocket pool manager and allow more
44 // simultaneous connections for WebSockets.
45 return new net::ClientSocketPoolManagerImpl(
46 params
.net_log
, params
.client_socket_factory
47 ? params
.client_socket_factory
48 : net::ClientSocketFactory::GetDefaultFactory(),
49 params
.host_resolver
, params
.cert_verifier
, params
.channel_id_service
,
50 params
.transport_security_state
, params
.cert_transparency_verifier
,
51 params
.cert_policy_enforcer
, params
.ssl_session_cache_shard
,
52 params
.ssl_config_service
, params
.enable_ssl_connect_job_waiting
,
56 } // unnamed namespace
60 HttpNetworkSession::Params::Params()
61 : client_socket_factory(NULL
),
64 cert_policy_enforcer(NULL
),
65 channel_id_service(NULL
),
66 transport_security_state(NULL
),
67 cert_transparency_verifier(NULL
),
69 ssl_config_service(NULL
),
70 http_auth_handler_factory(NULL
),
71 network_delegate(NULL
),
73 host_mapping_rules(NULL
),
74 enable_ssl_connect_job_waiting(false),
75 ignore_certificate_errors(false),
76 use_stale_while_revalidate(false),
77 testing_fixed_http_port(0),
78 testing_fixed_https_port(0),
79 enable_tcp_fast_open_for_ssl(false),
80 force_spdy_single_domain(false),
81 enable_spdy_compression(true),
82 enable_spdy_ping_based_connection_checking(true),
83 spdy_default_protocol(kProtoUnknown
),
84 spdy_stream_initial_recv_window_size(0),
85 spdy_initial_max_concurrent_streams(0),
86 spdy_max_concurrent_streams_limit(0),
87 time_func(&base::TimeTicks::Now
),
88 force_spdy_over_ssl(true),
89 force_spdy_always(false),
90 use_alternate_protocols(false),
91 alternate_protocol_probability_threshold(1),
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_ms(0),
98 quic_load_server_info_timeout_srtt_multiplier(0.0f
),
99 quic_enable_truncated_connection_ids(false),
100 quic_enable_connection_racing(false),
101 quic_disable_disk_cache(false),
104 quic_max_packet_length(kDefaultMaxPacketSize
),
105 enable_user_alternate_protocol_ports(false),
106 quic_crypto_client_stream_factory(NULL
),
107 proxy_delegate(NULL
) {
108 quic_supported_versions
.push_back(QUIC_VERSION_24
);
111 HttpNetworkSession::Params::~Params() {}
113 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
114 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
115 : net_log_(params
.net_log
),
116 network_delegate_(params
.network_delegate
),
117 http_server_properties_(params
.http_server_properties
),
118 cert_verifier_(params
.cert_verifier
),
119 http_auth_handler_factory_(params
.http_auth_handler_factory
),
120 proxy_service_(params
.proxy_service
),
121 ssl_config_service_(params
.ssl_config_service
),
122 normal_socket_pool_manager_(
123 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
124 websocket_socket_pool_manager_(
125 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
126 quic_stream_factory_(
127 params
.host_resolver
,
128 params
.client_socket_factory
129 ? params
.client_socket_factory
130 : net::ClientSocketFactory::GetDefaultFactory(),
131 params
.http_server_properties
,
132 params
.cert_verifier
,
133 params
.channel_id_service
,
134 params
.transport_security_state
,
135 params
.quic_crypto_client_stream_factory
,
136 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
137 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
138 params
.quic_max_packet_length
,
139 params
.quic_user_agent_id
,
140 params
.quic_supported_versions
,
141 params
.enable_quic_port_selection
,
142 params
.quic_always_require_handshake_confirmation
,
143 params
.quic_disable_connection_pooling
,
144 params
.quic_load_server_info_timeout_ms
,
145 params
.quic_load_server_info_timeout_srtt_multiplier
,
146 params
.quic_enable_truncated_connection_ids
,
147 params
.quic_enable_connection_racing
,
148 params
.quic_disable_disk_cache
,
149 params
.quic_connection_options
),
150 spdy_session_pool_(params
.host_resolver
,
151 params
.ssl_config_service
,
152 params
.http_server_properties
,
153 params
.transport_security_state
,
154 params
.force_spdy_single_domain
,
155 params
.enable_spdy_compression
,
156 params
.enable_spdy_ping_based_connection_checking
,
157 params
.spdy_default_protocol
,
158 params
.spdy_stream_initial_recv_window_size
,
159 params
.spdy_initial_max_concurrent_streams
,
160 params
.spdy_max_concurrent_streams_limit
,
162 params
.trusted_spdy_proxy
),
163 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
164 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
166 DCHECK(proxy_service_
);
167 DCHECK(ssl_config_service_
.get());
168 CHECK(http_server_properties_
);
169 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed
170 tracked_objects::ScopedTracker
tracking_profile(
171 FROM_HERE_WITH_EXPLICIT_FUNCTION(
172 "454983 HttpNetworkSession::HttpNetworkSession"));
174 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
175 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
176 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
179 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
180 // AlternateProtocol.
181 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
182 it
!= params_
.next_protos
.end(); ++it
) {
183 NextProto proto
= *it
;
185 // Add the protocol to the TLS next protocol list, except for QUIC
186 // since it uses UDP.
187 if (proto
!= kProtoQUIC1SPDY3
) {
188 next_protos_
.push_back(proto
);
191 // Enable the corresponding alternate protocol, except for HTTP
192 // which has not corresponding alternative.
193 if (proto
!= kProtoHTTP11
) {
194 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
195 if (!IsAlternateProtocolValid(alternate
)) {
196 NOTREACHED() << "Invalid next proto: " << proto
;
199 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
204 if (HpackHuffmanAggregator::UseAggregator()) {
205 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
208 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
209 params
.alternate_protocol_probability_threshold
);
212 HttpNetworkSession::~HttpNetworkSession() {
213 STLDeleteElements(&response_drainers_
);
214 spdy_session_pool_
.CloseAllSessions();
217 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
218 DCHECK(!ContainsKey(response_drainers_
, drainer
));
219 response_drainers_
.insert(drainer
);
222 void HttpNetworkSession::RemoveResponseDrainer(
223 HttpResponseBodyDrainer
* drainer
) {
224 DCHECK(ContainsKey(response_drainers_
, drainer
));
225 response_drainers_
.erase(drainer
);
228 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
229 SocketPoolType pool_type
) {
230 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
233 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
234 SocketPoolType pool_type
) {
235 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
238 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
239 SocketPoolType pool_type
,
240 const HostPortPair
& socks_proxy
) {
241 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
245 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
246 SocketPoolType pool_type
,
247 const HostPortPair
& http_proxy
) {
248 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
251 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
252 SocketPoolType pool_type
,
253 const HostPortPair
& proxy_server
) {
254 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
258 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
259 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
260 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
263 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
264 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
267 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
268 base::DictionaryValue
* dict
= new base::DictionaryValue();
269 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
270 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
271 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
272 dict
->SetBoolean("enable_quic_port_selection",
273 params_
.enable_quic_port_selection
);
274 base::ListValue
* connection_options
= new base::ListValue
;
275 for (QuicTagVector::const_iterator it
=
276 params_
.quic_connection_options
.begin();
277 it
!= params_
.quic_connection_options
.end(); ++it
) {
278 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
280 dict
->Set("connection_options", connection_options
);
281 dict
->SetString("origin_to_force_quic_on",
282 params_
.origin_to_force_quic_on
.ToString());
283 dict
->SetDouble("alternate_protocol_probability_threshold",
284 params_
.alternate_protocol_probability_threshold
);
288 void HttpNetworkSession::CloseAllConnections() {
289 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
290 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
291 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
292 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
295 void HttpNetworkSession::CloseIdleConnections() {
296 normal_socket_pool_manager_
->CloseIdleSockets();
297 websocket_socket_pool_manager_
->CloseIdleSockets();
298 spdy_session_pool_
.CloseCurrentIdleSessions();
301 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
302 DCHECK(IsAlternateProtocolValid(protocol
));
303 return enabled_protocols_
[
304 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
307 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
308 if (HttpStreamFactory::spdy_enabled()) {
309 *next_protos
= next_protos_
;
311 next_protos
->clear();
315 bool HttpNetworkSession::HasSpdyExclusion(
316 HostPortPair host_port_pair
) const {
317 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
318 params_
.forced_spdy_exclusions
.end();
321 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
322 SocketPoolType pool_type
) {
324 case NORMAL_SOCKET_POOL
:
325 return normal_socket_pool_manager_
.get();
326 case WEBSOCKET_SOCKET_POOL
:
327 return websocket_socket_pool_manager_
.get();