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/spdy/hpack_huffman_aggregator.h"
30 #include "net/spdy/spdy_session_pool.h"
34 net::ClientSocketPoolManager
* CreateSocketPoolManager(
35 net::HttpNetworkSession::SocketPoolType pool_type
,
36 const net::HttpNetworkSession::Params
& params
) {
37 // TODO(yutak): Differentiate WebSocket pool manager and allow more
38 // simultaneous connections for WebSockets.
39 return new net::ClientSocketPoolManagerImpl(
41 params
.client_socket_factory
42 ? params
.client_socket_factory
43 : net::ClientSocketFactory::GetDefaultFactory(),
46 params
.channel_id_service
,
47 params
.transport_security_state
,
48 params
.cert_transparency_verifier
,
49 params
.ssl_session_cache_shard
,
51 params
.ssl_config_service
,
52 params
.enable_ssl_connect_job_waiting
,
53 params
.proxy_delegate
,
57 } // unnamed namespace
61 HttpNetworkSession::Params::Params()
62 : client_socket_factory(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_port_selection(true),
94 quic_always_require_handshake_confirmation(false),
95 quic_disable_connection_pooling(false),
96 quic_load_server_info_timeout_ms(0),
99 quic_max_packet_length(kDefaultMaxPacketSize
),
100 enable_user_alternate_protocol_ports(false),
101 quic_crypto_client_stream_factory(NULL
),
102 proxy_delegate(NULL
) {
103 quic_supported_versions
.push_back(QUIC_VERSION_23
);
106 HttpNetworkSession::Params::~Params() {}
108 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
109 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
110 : net_log_(params
.net_log
),
111 network_delegate_(params
.network_delegate
),
112 http_server_properties_(params
.http_server_properties
),
113 cert_verifier_(params
.cert_verifier
),
114 http_auth_handler_factory_(params
.http_auth_handler_factory
),
115 proxy_service_(params
.proxy_service
),
116 ssl_config_service_(params
.ssl_config_service
),
117 normal_socket_pool_manager_(
118 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
119 websocket_socket_pool_manager_(
120 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
121 quic_stream_factory_(
122 params
.host_resolver
,
123 params
.client_socket_factory
124 ? params
.client_socket_factory
125 : net::ClientSocketFactory::GetDefaultFactory(),
126 params
.http_server_properties
,
127 params
.cert_verifier
,
128 params
.channel_id_service
,
129 params
.transport_security_state
,
130 params
.quic_crypto_client_stream_factory
,
131 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
132 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
133 params
.quic_max_packet_length
,
134 params
.quic_user_agent_id
,
135 params
.quic_supported_versions
,
136 params
.enable_quic_port_selection
,
137 params
.quic_always_require_handshake_confirmation
,
138 params
.quic_disable_connection_pooling
,
139 params
.quic_load_server_info_timeout_ms
,
140 params
.quic_connection_options
),
141 spdy_session_pool_(params
.host_resolver
,
142 params
.ssl_config_service
,
143 params
.http_server_properties
,
144 params
.transport_security_state
,
145 params
.force_spdy_single_domain
,
146 params
.enable_spdy_compression
,
147 params
.enable_spdy_ping_based_connection_checking
,
148 params
.spdy_default_protocol
,
149 params
.spdy_stream_initial_recv_window_size
,
150 params
.spdy_initial_max_concurrent_streams
,
151 params
.spdy_max_concurrent_streams_limit
,
153 params
.trusted_spdy_proxy
),
154 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
155 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
157 DCHECK(proxy_service_
);
158 DCHECK(ssl_config_service_
.get());
159 CHECK(http_server_properties_
);
161 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
162 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
163 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
166 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
167 // AlternateProtocol.
168 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
169 it
!= params_
.next_protos
.end(); ++it
) {
170 NextProto proto
= *it
;
172 // Add the protocol to the TLS next protocol list, except for QUIC
173 // since it uses UDP.
174 if (proto
!= kProtoQUIC1SPDY3
) {
175 next_protos_
.push_back(SSLClientSocket::NextProtoToString(proto
));
178 // Enable the corresponding alternate protocol, except for HTTP
179 // which has not corresponding alternative.
180 if (proto
!= kProtoHTTP11
) {
181 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
182 if (!IsAlternateProtocolValid(alternate
)) {
183 NOTREACHED() << "Invalid next proto: " << proto
;
186 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
191 if (HpackHuffmanAggregator::UseAggregator()) {
192 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
195 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
196 params
.alternate_protocol_probability_threshold
);
199 HttpNetworkSession::~HttpNetworkSession() {
200 STLDeleteElements(&response_drainers_
);
201 spdy_session_pool_
.CloseAllSessions();
204 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
205 DCHECK(!ContainsKey(response_drainers_
, drainer
));
206 response_drainers_
.insert(drainer
);
209 void HttpNetworkSession::RemoveResponseDrainer(
210 HttpResponseBodyDrainer
* drainer
) {
211 DCHECK(ContainsKey(response_drainers_
, drainer
));
212 response_drainers_
.erase(drainer
);
215 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
216 SocketPoolType pool_type
) {
217 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
220 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
221 SocketPoolType pool_type
) {
222 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
225 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
226 SocketPoolType pool_type
,
227 const HostPortPair
& socks_proxy
) {
228 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
232 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
233 SocketPoolType pool_type
,
234 const HostPortPair
& http_proxy
) {
235 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
238 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
239 SocketPoolType pool_type
,
240 const HostPortPair
& proxy_server
) {
241 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
245 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
246 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
247 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
250 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
251 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
254 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
255 base::DictionaryValue
* dict
= new base::DictionaryValue();
256 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
257 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
258 dict
->SetBoolean("enable_quic_port_selection",
259 params_
.enable_quic_port_selection
);
260 base::ListValue
* connection_options
= new base::ListValue
;
261 for (QuicTagVector::const_iterator it
=
262 params_
.quic_connection_options
.begin();
263 it
!= params_
.quic_connection_options
.end(); ++it
) {
264 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
266 dict
->Set("connection_options", connection_options
);
267 dict
->SetString("origin_to_force_quic_on",
268 params_
.origin_to_force_quic_on
.ToString());
269 dict
->SetDouble("alternate_protocol_probability_threshold",
270 params_
.alternate_protocol_probability_threshold
);
274 void HttpNetworkSession::CloseAllConnections() {
275 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
276 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
277 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
278 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
281 void HttpNetworkSession::CloseIdleConnections() {
282 normal_socket_pool_manager_
->CloseIdleSockets();
283 websocket_socket_pool_manager_
->CloseIdleSockets();
284 spdy_session_pool_
.CloseCurrentIdleSessions();
287 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
288 DCHECK(IsAlternateProtocolValid(protocol
));
289 return enabled_protocols_
[
290 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
293 void HttpNetworkSession::GetNextProtos(
294 std::vector
<std::string
>* 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();