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_load_server_info_timeout_srtt_multiplier(0.0f
),
93 quic_enable_truncated_connection_ids(false),
96 quic_max_packet_length(kDefaultMaxPacketSize
),
97 enable_user_alternate_protocol_ports(false),
98 quic_crypto_client_stream_factory(NULL
),
99 proxy_delegate(NULL
) {
100 quic_supported_versions
.push_back(QUIC_VERSION_24
);
103 HttpNetworkSession::Params::~Params() {}
105 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
106 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
107 : net_log_(params
.net_log
),
108 network_delegate_(params
.network_delegate
),
109 http_server_properties_(params
.http_server_properties
),
110 cert_verifier_(params
.cert_verifier
),
111 http_auth_handler_factory_(params
.http_auth_handler_factory
),
112 proxy_service_(params
.proxy_service
),
113 ssl_config_service_(params
.ssl_config_service
),
114 normal_socket_pool_manager_(
115 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
116 websocket_socket_pool_manager_(
117 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
118 quic_stream_factory_(
119 params
.host_resolver
,
120 params
.client_socket_factory
121 ? params
.client_socket_factory
122 : net::ClientSocketFactory::GetDefaultFactory(),
123 params
.http_server_properties
,
124 params
.cert_verifier
,
125 params
.channel_id_service
,
126 params
.transport_security_state
,
127 params
.quic_crypto_client_stream_factory
,
128 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
129 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
130 params
.quic_max_packet_length
,
131 params
.quic_user_agent_id
,
132 params
.quic_supported_versions
,
133 params
.enable_quic_port_selection
,
134 params
.quic_always_require_handshake_confirmation
,
135 params
.quic_disable_connection_pooling
,
136 params
.quic_load_server_info_timeout_ms
,
137 params
.quic_load_server_info_timeout_srtt_multiplier
,
138 params
.quic_enable_truncated_connection_ids
,
139 params
.quic_connection_options
),
140 spdy_session_pool_(params
.host_resolver
,
141 params
.ssl_config_service
,
142 params
.http_server_properties
,
143 params
.transport_security_state
,
144 params
.force_spdy_single_domain
,
145 params
.enable_spdy_compression
,
146 params
.enable_spdy_ping_based_connection_checking
,
147 params
.spdy_default_protocol
,
148 params
.spdy_stream_initial_recv_window_size
,
149 params
.spdy_initial_max_concurrent_streams
,
150 params
.spdy_max_concurrent_streams_limit
,
152 params
.trusted_spdy_proxy
),
153 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
154 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
156 DCHECK(proxy_service_
);
157 DCHECK(ssl_config_service_
.get());
158 CHECK(http_server_properties_
);
160 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
161 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
162 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
165 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
166 // AlternateProtocol.
167 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
168 it
!= params_
.next_protos
.end(); ++it
) {
169 NextProto proto
= *it
;
171 // Add the protocol to the TLS next protocol list, except for QUIC
172 // since it uses UDP.
173 if (proto
!= kProtoQUIC1SPDY3
) {
174 next_protos_
.push_back(proto
);
177 // Enable the corresponding alternate protocol, except for HTTP
178 // which has not corresponding alternative.
179 if (proto
!= kProtoHTTP11
) {
180 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
181 if (!IsAlternateProtocolValid(alternate
)) {
182 NOTREACHED() << "Invalid next proto: " << proto
;
185 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
190 if (HpackHuffmanAggregator::UseAggregator()) {
191 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
194 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
195 params
.alternate_protocol_probability_threshold
);
198 HttpNetworkSession::~HttpNetworkSession() {
199 STLDeleteElements(&response_drainers_
);
200 spdy_session_pool_
.CloseAllSessions();
203 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
204 DCHECK(!ContainsKey(response_drainers_
, drainer
));
205 response_drainers_
.insert(drainer
);
208 void HttpNetworkSession::RemoveResponseDrainer(
209 HttpResponseBodyDrainer
* drainer
) {
210 DCHECK(ContainsKey(response_drainers_
, drainer
));
211 response_drainers_
.erase(drainer
);
214 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
215 SocketPoolType pool_type
) {
216 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
219 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
220 SocketPoolType pool_type
) {
221 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
224 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
225 SocketPoolType pool_type
,
226 const HostPortPair
& socks_proxy
) {
227 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
231 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
232 SocketPoolType pool_type
,
233 const HostPortPair
& http_proxy
) {
234 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
237 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
238 SocketPoolType pool_type
,
239 const HostPortPair
& proxy_server
) {
240 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
244 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
245 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
246 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
249 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
250 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
253 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
254 base::DictionaryValue
* dict
= new base::DictionaryValue();
255 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
256 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
257 dict
->SetBoolean("enable_quic_port_selection",
258 params_
.enable_quic_port_selection
);
259 base::ListValue
* connection_options
= new base::ListValue
;
260 for (QuicTagVector::const_iterator it
=
261 params_
.quic_connection_options
.begin();
262 it
!= params_
.quic_connection_options
.end(); ++it
) {
263 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
265 dict
->Set("connection_options", connection_options
);
266 dict
->SetString("origin_to_force_quic_on",
267 params_
.origin_to_force_quic_on
.ToString());
268 dict
->SetDouble("alternate_protocol_probability_threshold",
269 params_
.alternate_protocol_probability_threshold
);
273 void HttpNetworkSession::CloseAllConnections() {
274 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
275 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
276 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
277 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
280 void HttpNetworkSession::CloseIdleConnections() {
281 normal_socket_pool_manager_
->CloseIdleSockets();
282 websocket_socket_pool_manager_
->CloseIdleSockets();
283 spdy_session_pool_
.CloseCurrentIdleSessions();
286 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
287 DCHECK(IsAlternateProtocolValid(protocol
));
288 return enabled_protocols_
[
289 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
292 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
293 if (HttpStreamFactory::spdy_enabled()) {
294 *next_protos
= next_protos_
;
296 next_protos
->clear();
300 bool HttpNetworkSession::HasSpdyExclusion(
301 HostPortPair host_port_pair
) const {
302 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
303 params_
.forced_spdy_exclusions
.end();
306 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
307 SocketPoolType pool_type
) {
309 case NORMAL_SOCKET_POOL
:
310 return normal_socket_pool_manager_
.get();
311 case WEBSOCKET_SOCKET_POOL
:
312 return websocket_socket_pool_manager_
.get();