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/stl_util.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15 #include "net/http/http_auth_handler_factory.h"
16 #include "net/http/http_response_body_drainer.h"
17 #include "net/http/http_stream_factory_impl.h"
18 #include "net/http/url_security_manager.h"
19 #include "net/proxy/proxy_service.h"
20 #include "net/quic/crypto/quic_random.h"
21 #include "net/quic/quic_clock.h"
22 #include "net/quic/quic_crypto_client_stream_factory.h"
23 #include "net/quic/quic_protocol.h"
24 #include "net/quic/quic_stream_factory.h"
25 #include "net/quic/quic_utils.h"
26 #include "net/socket/client_socket_factory.h"
27 #include "net/socket/client_socket_pool_manager_impl.h"
28 #include "net/socket/next_proto.h"
29 #include "net/socket/ssl_client_socket.h"
30 #include "net/spdy/hpack_huffman_aggregator.h"
31 #include "net/spdy/spdy_session_pool.h"
35 net::ClientSocketPoolManager
* CreateSocketPoolManager(
36 net::HttpNetworkSession::SocketPoolType pool_type
,
37 const net::HttpNetworkSession::Params
& params
) {
38 // TODO(yutak): Differentiate WebSocket pool manager and allow more
39 // simultaneous connections for WebSockets.
40 return new net::ClientSocketPoolManagerImpl(
41 params
.net_log
, params
.client_socket_factory
42 ? params
.client_socket_factory
43 : net::ClientSocketFactory::GetDefaultFactory(),
44 params
.host_resolver
, params
.cert_verifier
, params
.channel_id_service
,
45 params
.transport_security_state
, params
.cert_transparency_verifier
,
46 params
.cert_policy_enforcer
, params
.ssl_session_cache_shard
,
47 params
.ssl_config_service
, params
.enable_ssl_connect_job_waiting
,
51 } // unnamed namespace
55 HttpNetworkSession::Params::Params()
56 : client_socket_factory(NULL
),
59 cert_policy_enforcer(NULL
),
60 channel_id_service(NULL
),
61 transport_security_state(NULL
),
62 cert_transparency_verifier(NULL
),
64 ssl_config_service(NULL
),
65 http_auth_handler_factory(NULL
),
66 network_delegate(NULL
),
68 host_mapping_rules(NULL
),
69 enable_ssl_connect_job_waiting(false),
70 ignore_certificate_errors(false),
71 use_stale_while_revalidate(false),
72 testing_fixed_http_port(0),
73 testing_fixed_https_port(0),
74 enable_tcp_fast_open_for_ssl(false),
75 force_spdy_single_domain(false),
76 enable_spdy_compression(true),
77 enable_spdy_ping_based_connection_checking(true),
78 spdy_default_protocol(kProtoUnknown
),
79 spdy_stream_initial_recv_window_size(0),
80 spdy_initial_max_concurrent_streams(0),
81 spdy_max_concurrent_streams_limit(0),
82 time_func(&base::TimeTicks::Now
),
83 force_spdy_over_ssl(true),
84 force_spdy_always(false),
85 use_alternate_protocols(false),
86 alternate_protocol_probability_threshold(1),
88 enable_quic_port_selection(true),
89 quic_always_require_handshake_confirmation(false),
90 quic_disable_connection_pooling(false),
91 quic_load_server_info_timeout_ms(0),
92 quic_disable_loading_server_info_for_new_servers(false),
93 quic_load_server_info_timeout_srtt_multiplier(0.0f
),
94 quic_enable_truncated_connection_ids(false),
97 quic_max_packet_length(kDefaultMaxPacketSize
),
98 enable_user_alternate_protocol_ports(false),
99 quic_crypto_client_stream_factory(NULL
),
100 proxy_delegate(NULL
) {
101 quic_supported_versions
.push_back(QUIC_VERSION_23
);
104 HttpNetworkSession::Params::~Params() {}
106 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
107 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
108 : net_log_(params
.net_log
),
109 network_delegate_(params
.network_delegate
),
110 http_server_properties_(params
.http_server_properties
),
111 cert_verifier_(params
.cert_verifier
),
112 http_auth_handler_factory_(params
.http_auth_handler_factory
),
113 proxy_service_(params
.proxy_service
),
114 ssl_config_service_(params
.ssl_config_service
),
115 normal_socket_pool_manager_(
116 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
117 websocket_socket_pool_manager_(
118 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
119 quic_stream_factory_(
120 params
.host_resolver
,
121 params
.client_socket_factory
122 ? params
.client_socket_factory
123 : net::ClientSocketFactory::GetDefaultFactory(),
124 params
.http_server_properties
,
125 params
.cert_verifier
,
126 params
.channel_id_service
,
127 params
.transport_security_state
,
128 params
.quic_crypto_client_stream_factory
,
129 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
130 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
131 params
.quic_max_packet_length
,
132 params
.quic_user_agent_id
,
133 params
.quic_supported_versions
,
134 params
.enable_quic_port_selection
,
135 params
.quic_always_require_handshake_confirmation
,
136 params
.quic_disable_connection_pooling
,
137 params
.quic_load_server_info_timeout_ms
,
138 params
.quic_disable_loading_server_info_for_new_servers
,
139 params
.quic_load_server_info_timeout_srtt_multiplier
,
140 params
.quic_enable_truncated_connection_ids
,
141 params
.quic_connection_options
),
142 spdy_session_pool_(params
.host_resolver
,
143 params
.ssl_config_service
,
144 params
.http_server_properties
,
145 params
.transport_security_state
,
146 params
.force_spdy_single_domain
,
147 params
.enable_spdy_compression
,
148 params
.enable_spdy_ping_based_connection_checking
,
149 params
.spdy_default_protocol
,
150 params
.spdy_stream_initial_recv_window_size
,
151 params
.spdy_initial_max_concurrent_streams
,
152 params
.spdy_max_concurrent_streams_limit
,
154 params
.trusted_spdy_proxy
),
155 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
156 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
158 DCHECK(proxy_service_
);
159 DCHECK(ssl_config_service_
.get());
160 CHECK(http_server_properties_
);
162 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
163 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
164 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
167 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
168 // AlternateProtocol.
169 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
170 it
!= params_
.next_protos
.end(); ++it
) {
171 NextProto proto
= *it
;
173 // Add the protocol to the TLS next protocol list, except for QUIC
174 // since it uses UDP.
175 if (proto
!= kProtoQUIC1SPDY3
) {
176 next_protos_
.push_back(proto
);
179 // Enable the corresponding alternate protocol, except for HTTP
180 // which has not corresponding alternative.
181 if (proto
!= kProtoHTTP11
) {
182 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
183 if (!IsAlternateProtocolValid(alternate
)) {
184 NOTREACHED() << "Invalid next proto: " << proto
;
187 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
192 if (HpackHuffmanAggregator::UseAggregator()) {
193 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
196 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
197 params
.alternate_protocol_probability_threshold
);
200 HttpNetworkSession::~HttpNetworkSession() {
201 STLDeleteElements(&response_drainers_
);
202 spdy_session_pool_
.CloseAllSessions();
205 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
206 DCHECK(!ContainsKey(response_drainers_
, drainer
));
207 response_drainers_
.insert(drainer
);
210 void HttpNetworkSession::RemoveResponseDrainer(
211 HttpResponseBodyDrainer
* drainer
) {
212 DCHECK(ContainsKey(response_drainers_
, drainer
));
213 response_drainers_
.erase(drainer
);
216 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
217 SocketPoolType pool_type
) {
218 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
221 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
222 SocketPoolType pool_type
) {
223 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
226 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
227 SocketPoolType pool_type
,
228 const HostPortPair
& socks_proxy
) {
229 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
233 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
234 SocketPoolType pool_type
,
235 const HostPortPair
& http_proxy
) {
236 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
239 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
240 SocketPoolType pool_type
,
241 const HostPortPair
& proxy_server
) {
242 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
246 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
247 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
248 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
251 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
252 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
255 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
256 base::DictionaryValue
* dict
= new base::DictionaryValue();
257 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
258 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
259 dict
->SetBoolean("enable_quic_port_selection",
260 params_
.enable_quic_port_selection
);
261 base::ListValue
* connection_options
= new base::ListValue
;
262 for (QuicTagVector::const_iterator it
=
263 params_
.quic_connection_options
.begin();
264 it
!= params_
.quic_connection_options
.end(); ++it
) {
265 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
267 dict
->Set("connection_options", connection_options
);
268 dict
->SetString("origin_to_force_quic_on",
269 params_
.origin_to_force_quic_on
.ToString());
270 dict
->SetDouble("alternate_protocol_probability_threshold",
271 params_
.alternate_protocol_probability_threshold
);
275 void HttpNetworkSession::CloseAllConnections() {
276 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
277 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
278 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
279 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
282 void HttpNetworkSession::CloseIdleConnections() {
283 normal_socket_pool_manager_
->CloseIdleSockets();
284 websocket_socket_pool_manager_
->CloseIdleSockets();
285 spdy_session_pool_
.CloseCurrentIdleSessions();
288 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
289 DCHECK(IsAlternateProtocolValid(protocol
));
290 return enabled_protocols_
[
291 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
294 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
295 if (HttpStreamFactory::spdy_enabled()) {
296 *next_protos
= next_protos_
;
298 next_protos
->clear();
302 bool HttpNetworkSession::HasSpdyExclusion(
303 HostPortPair host_port_pair
) const {
304 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
305 params_
.forced_spdy_exclusions
.end();
308 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
309 SocketPoolType pool_type
) {
311 case NORMAL_SOCKET_POOL
:
312 return normal_socket_pool_manager_
.get();
313 case WEBSOCKET_SOCKET_POOL
:
314 return websocket_socket_pool_manager_
.get();