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
,
56 } // unnamed namespace
60 HttpNetworkSession::Params::Params()
61 : client_socket_factory(NULL
),
64 channel_id_service(NULL
),
65 transport_security_state(NULL
),
66 cert_transparency_verifier(NULL
),
68 ssl_config_service(NULL
),
69 http_auth_handler_factory(NULL
),
70 network_delegate(NULL
),
72 host_mapping_rules(NULL
),
73 enable_ssl_connect_job_waiting(false),
74 ignore_certificate_errors(false),
75 testing_fixed_http_port(0),
76 testing_fixed_https_port(0),
77 force_spdy_single_domain(false),
78 enable_spdy_compression(true),
79 enable_spdy_ping_based_connection_checking(true),
80 spdy_default_protocol(kProtoUnknown
),
81 spdy_stream_initial_recv_window_size(0),
82 spdy_initial_max_concurrent_streams(0),
83 spdy_max_concurrent_streams_limit(0),
84 time_func(&base::TimeTicks::Now
),
85 force_spdy_over_ssl(true),
86 force_spdy_always(false),
87 use_alternate_protocols(false),
88 alternate_protocol_probability_threshold(1),
89 enable_websocket_over_spdy(false),
91 enable_quic_port_selection(true),
92 enable_quic_time_based_loss_detection(false),
95 quic_max_packet_length(kDefaultMaxPacketSize
),
96 enable_user_alternate_protocol_ports(false),
97 quic_crypto_client_stream_factory(NULL
) {
98 quic_supported_versions
.push_back(QUIC_VERSION_21
);
101 HttpNetworkSession::Params::~Params() {}
103 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
104 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
105 : net_log_(params
.net_log
),
106 network_delegate_(params
.network_delegate
),
107 http_server_properties_(params
.http_server_properties
),
108 cert_verifier_(params
.cert_verifier
),
109 http_auth_handler_factory_(params
.http_auth_handler_factory
),
110 proxy_service_(params
.proxy_service
),
111 ssl_config_service_(params
.ssl_config_service
),
112 normal_socket_pool_manager_(
113 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
114 websocket_socket_pool_manager_(
115 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
116 quic_stream_factory_(params
.host_resolver
,
117 params
.client_socket_factory
?
118 params
.client_socket_factory
:
119 net::ClientSocketFactory::GetDefaultFactory(),
120 params
.http_server_properties
,
121 params
.cert_verifier
,
122 params
.channel_id_service
,
123 params
.transport_security_state
,
124 params
.quic_crypto_client_stream_factory
,
125 params
.quic_random
? params
.quic_random
:
126 QuicRandom::GetInstance(),
127 params
.quic_clock
? params
. quic_clock
:
129 params
.quic_max_packet_length
,
130 params
.quic_user_agent_id
,
131 params
.quic_supported_versions
,
132 params
.enable_quic_port_selection
,
133 params
.enable_quic_time_based_loss_detection
,
134 params
.quic_connection_options
),
135 spdy_session_pool_(params
.host_resolver
,
136 params
.ssl_config_service
,
137 params
.http_server_properties
,
138 params
.transport_security_state
,
139 params
.force_spdy_single_domain
,
140 params
.enable_spdy_compression
,
141 params
.enable_spdy_ping_based_connection_checking
,
142 params
.spdy_default_protocol
,
143 params
.spdy_stream_initial_recv_window_size
,
144 params
.spdy_initial_max_concurrent_streams
,
145 params
.spdy_max_concurrent_streams_limit
,
147 params
.trusted_spdy_proxy
),
148 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
149 http_stream_factory_for_websocket_(
150 new HttpStreamFactoryImpl(this, true)),
152 DCHECK(proxy_service_
);
153 DCHECK(ssl_config_service_
.get());
154 CHECK(http_server_properties_
);
156 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
157 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
158 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
161 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
162 // AlternateProtocol.
163 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
164 it
!= params_
.next_protos
.end(); ++it
) {
165 NextProto proto
= *it
;
167 // Add the protocol to the TLS next protocol list, except for QUIC
168 // since it uses UDP.
169 if (proto
!= kProtoQUIC1SPDY3
) {
170 next_protos_
.push_back(SSLClientSocket::NextProtoToString(proto
));
173 // Enable the corresponding alternate protocol, except for HTTP
174 // which has not corresponding alternative.
175 if (proto
!= kProtoHTTP11
) {
176 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
177 if (!IsAlternateProtocolValid(alternate
)) {
178 NOTREACHED() << "Invalid next proto: " << proto
;
181 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
186 if (HpackHuffmanAggregator::UseAggregator()) {
187 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
190 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
191 params
.alternate_protocol_probability_threshold
);
194 HttpNetworkSession::~HttpNetworkSession() {
195 STLDeleteElements(&response_drainers_
);
196 spdy_session_pool_
.CloseAllSessions();
199 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
200 DCHECK(!ContainsKey(response_drainers_
, drainer
));
201 response_drainers_
.insert(drainer
);
204 void HttpNetworkSession::RemoveResponseDrainer(
205 HttpResponseBodyDrainer
* drainer
) {
206 DCHECK(ContainsKey(response_drainers_
, drainer
));
207 response_drainers_
.erase(drainer
);
210 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
211 SocketPoolType pool_type
) {
212 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
215 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
216 SocketPoolType pool_type
) {
217 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
220 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
221 SocketPoolType pool_type
,
222 const HostPortPair
& socks_proxy
) {
223 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
227 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
228 SocketPoolType pool_type
,
229 const HostPortPair
& http_proxy
) {
230 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
233 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
234 SocketPoolType pool_type
,
235 const HostPortPair
& proxy_server
) {
236 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
240 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
241 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
242 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
245 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
246 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
249 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
250 base::DictionaryValue
* dict
= new base::DictionaryValue();
251 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
252 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
253 dict
->SetBoolean("enable_quic_port_selection",
254 params_
.enable_quic_port_selection
);
255 base::ListValue
* connection_options
= new base::ListValue
;
256 for (QuicTagVector::const_iterator it
=
257 params_
.quic_connection_options
.begin();
258 it
!= params_
.quic_connection_options
.end(); ++it
) {
259 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
261 dict
->Set("connection_options", connection_options
);
262 dict
->SetBoolean("enable_quic_time_based_loss_detection",
263 params_
.enable_quic_time_based_loss_detection
);
264 dict
->SetString("origin_to_force_quic_on",
265 params_
.origin_to_force_quic_on
.ToString());
269 void HttpNetworkSession::CloseAllConnections() {
270 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
271 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
272 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
273 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
276 void HttpNetworkSession::CloseIdleConnections() {
277 normal_socket_pool_manager_
->CloseIdleSockets();
278 websocket_socket_pool_manager_
->CloseIdleSockets();
279 spdy_session_pool_
.CloseCurrentIdleSessions();
282 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
283 DCHECK(IsAlternateProtocolValid(protocol
));
284 return enabled_protocols_
[
285 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
288 void HttpNetworkSession::GetNextProtos(
289 std::vector
<std::string
>* next_protos
) const {
290 if (HttpStreamFactory::spdy_enabled()) {
291 *next_protos
= next_protos_
;
293 next_protos
->clear();
297 bool HttpNetworkSession::HasSpdyExclusion(
298 HostPortPair host_port_pair
) const {
299 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
300 params_
.forced_spdy_exclusions
.end();
303 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
304 SocketPoolType pool_type
) {
306 case NORMAL_SOCKET_POOL
:
307 return normal_socket_pool_manager_
.get();
308 case WEBSOCKET_SOCKET_POOL
:
309 return websocket_socket_pool_manager_
.get();