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/spdy_session_pool.h"
37 ClientSocketPoolManager
* CreateSocketPoolManager(
38 HttpNetworkSession::SocketPoolType pool_type
,
39 const HttpNetworkSession::Params
& params
) {
40 // TODO(yutak): Differentiate WebSocket pool manager and allow more
41 // simultaneous connections for WebSockets.
42 return new ClientSocketPoolManagerImpl(
44 params
.client_socket_factory
? params
.client_socket_factory
45 : ClientSocketFactory::GetDefaultFactory(),
46 params
.host_resolver
, params
.cert_verifier
, params
.channel_id_service
,
47 params
.transport_security_state
, params
.cert_transparency_verifier
,
48 params
.cert_policy_enforcer
, params
.ssl_session_cache_shard
,
49 params
.ssl_config_service
, pool_type
);
52 } // unnamed namespace
54 // The maximum receive window sizes for HTTP/2 sessions and streams.
55 const int32 kSpdySessionMaxRecvWindowSize
= 10 * 1024 * 1024; // 10 MB
56 const int32 kSpdyStreamMaxRecvWindowSize
= 10 * 1024 * 1024; // 10 MB
58 HttpNetworkSession::Params::Params()
59 : client_socket_factory(NULL
),
62 cert_policy_enforcer(NULL
),
63 channel_id_service(NULL
),
64 transport_security_state(NULL
),
65 cert_transparency_verifier(NULL
),
67 ssl_config_service(NULL
),
68 http_auth_handler_factory(NULL
),
69 network_delegate(NULL
),
71 host_mapping_rules(NULL
),
72 ignore_certificate_errors(false),
73 use_stale_while_revalidate(false),
74 testing_fixed_http_port(0),
75 testing_fixed_https_port(0),
76 enable_tcp_fast_open_for_ssl(false),
77 enable_spdy_compression(true),
78 enable_spdy_ping_based_connection_checking(true),
79 spdy_default_protocol(kProtoUnknown
),
80 spdy_session_max_recv_window_size(kSpdySessionMaxRecvWindowSize
),
81 spdy_stream_max_recv_window_size(kSpdyStreamMaxRecvWindowSize
),
82 spdy_initial_max_concurrent_streams(0),
83 spdy_max_concurrent_streams_limit(0),
84 time_func(&base::TimeTicks::Now
),
85 use_alternate_protocols(false),
86 alternate_protocol_probability_threshold(1),
88 enable_quic_for_proxies(false),
89 enable_quic_port_selection(true),
90 quic_always_require_handshake_confirmation(false),
91 quic_disable_connection_pooling(false),
92 quic_load_server_info_timeout_srtt_multiplier(0.25f
),
93 quic_enable_connection_racing(false),
94 quic_enable_non_blocking_io(false),
95 quic_disable_disk_cache(false),
96 quic_max_number_of_lossy_connections(0),
97 quic_packet_loss_threshold(1.0f
),
98 quic_socket_receive_buffer_size(kDefaultSocketReceiveBuffer
),
101 quic_max_packet_length(kDefaultMaxPacketSize
),
102 enable_user_alternate_protocol_ports(false),
103 quic_crypto_client_stream_factory(NULL
),
104 proxy_delegate(NULL
) {
105 quic_supported_versions
.push_back(QUIC_VERSION_25
);
108 HttpNetworkSession::Params::~Params() {}
110 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
111 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
112 : net_log_(params
.net_log
),
113 network_delegate_(params
.network_delegate
),
114 http_server_properties_(params
.http_server_properties
),
115 cert_verifier_(params
.cert_verifier
),
116 http_auth_handler_factory_(params
.http_auth_handler_factory
),
117 proxy_service_(params
.proxy_service
),
118 ssl_config_service_(params
.ssl_config_service
),
119 normal_socket_pool_manager_(
120 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
121 websocket_socket_pool_manager_(
122 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
123 quic_stream_factory_(
124 params
.host_resolver
,
125 params
.client_socket_factory
126 ? params
.client_socket_factory
127 : ClientSocketFactory::GetDefaultFactory(),
128 params
.http_server_properties
,
129 params
.cert_verifier
,
130 params
.channel_id_service
,
131 params
.transport_security_state
,
132 params
.quic_crypto_client_stream_factory
,
133 params
.quic_random
? params
.quic_random
: QuicRandom::GetInstance(),
134 params
.quic_clock
? params
.quic_clock
: new QuicClock(),
135 params
.quic_max_packet_length
,
136 params
.quic_user_agent_id
,
137 params
.quic_supported_versions
,
138 params
.enable_quic_port_selection
,
139 params
.quic_always_require_handshake_confirmation
,
140 params
.quic_disable_connection_pooling
,
141 params
.quic_load_server_info_timeout_srtt_multiplier
,
142 params
.quic_enable_connection_racing
,
143 params
.quic_enable_non_blocking_io
,
144 params
.quic_disable_disk_cache
,
145 params
.quic_max_number_of_lossy_connections
,
146 params
.quic_packet_loss_threshold
,
147 params
.quic_socket_receive_buffer_size
,
148 params
.quic_connection_options
),
149 spdy_session_pool_(params
.host_resolver
,
150 params
.ssl_config_service
,
151 params
.http_server_properties
,
152 params
.transport_security_state
,
153 params
.enable_spdy_compression
,
154 params
.enable_spdy_ping_based_connection_checking
,
155 params
.spdy_default_protocol
,
156 params
.spdy_session_max_recv_window_size
,
157 params
.spdy_stream_max_recv_window_size
,
158 params
.spdy_initial_max_concurrent_streams
,
159 params
.spdy_max_concurrent_streams_limit
,
161 params
.trusted_spdy_proxy
),
162 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
163 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
165 DCHECK(proxy_service_
);
166 DCHECK(ssl_config_service_
.get());
167 CHECK(http_server_properties_
);
169 for (int i
= ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
;
170 i
<= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION
; ++i
) {
171 enabled_protocols_
[i
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] = false;
174 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
175 // AlternateProtocol.
176 for (std::vector
<NextProto
>::const_iterator it
= params_
.next_protos
.begin();
177 it
!= params_
.next_protos
.end(); ++it
) {
178 NextProto proto
= *it
;
180 // Add the protocol to the TLS next protocol list, except for QUIC
181 // since it uses UDP.
182 if (proto
!= kProtoQUIC1SPDY3
) {
183 next_protos_
.push_back(proto
);
186 // Enable the corresponding alternate protocol, except for HTTP
187 // which has not corresponding alternative.
188 if (proto
!= kProtoHTTP11
) {
189 AlternateProtocol alternate
= AlternateProtocolFromNextProto(proto
);
190 if (!IsAlternateProtocolValid(alternate
)) {
191 NOTREACHED() << "Invalid next proto: " << proto
;
194 enabled_protocols_
[alternate
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
] =
199 http_server_properties_
->SetAlternateProtocolProbabilityThreshold(
200 params
.alternate_protocol_probability_threshold
);
203 HttpNetworkSession::~HttpNetworkSession() {
204 STLDeleteElements(&response_drainers_
);
205 spdy_session_pool_
.CloseAllSessions();
208 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
209 DCHECK(!ContainsKey(response_drainers_
, drainer
));
210 response_drainers_
.insert(drainer
);
213 void HttpNetworkSession::RemoveResponseDrainer(
214 HttpResponseBodyDrainer
* drainer
) {
215 DCHECK(ContainsKey(response_drainers_
, drainer
));
216 response_drainers_
.erase(drainer
);
219 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
220 SocketPoolType pool_type
) {
221 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
224 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
225 SocketPoolType pool_type
) {
226 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
229 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
230 SocketPoolType pool_type
,
231 const HostPortPair
& socks_proxy
) {
232 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
236 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
237 SocketPoolType pool_type
,
238 const HostPortPair
& http_proxy
) {
239 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
242 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
243 SocketPoolType pool_type
,
244 const HostPortPair
& proxy_server
) {
245 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
249 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
250 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
251 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
254 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
255 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
258 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
259 base::DictionaryValue
* dict
= new base::DictionaryValue();
260 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
261 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
262 dict
->SetBoolean("quic_enabled_for_proxies", params_
.enable_quic_for_proxies
);
263 dict
->SetBoolean("enable_quic_port_selection",
264 params_
.enable_quic_port_selection
);
265 base::ListValue
* connection_options
= new base::ListValue
;
266 for (QuicTagVector::const_iterator it
=
267 params_
.quic_connection_options
.begin();
268 it
!= params_
.quic_connection_options
.end(); ++it
) {
269 connection_options
->AppendString("'" + QuicUtils::TagToString(*it
) + "'");
271 dict
->Set("connection_options", connection_options
);
272 dict
->SetString("origin_to_force_quic_on",
273 params_
.origin_to_force_quic_on
.ToString());
274 dict
->SetDouble("alternate_protocol_probability_threshold",
275 params_
.alternate_protocol_probability_threshold
);
279 void HttpNetworkSession::CloseAllConnections() {
280 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
281 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
282 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
283 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
286 void HttpNetworkSession::CloseIdleConnections() {
287 normal_socket_pool_manager_
->CloseIdleSockets();
288 websocket_socket_pool_manager_
->CloseIdleSockets();
289 spdy_session_pool_
.CloseCurrentIdleSessions();
292 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol
) const {
293 DCHECK(IsAlternateProtocolValid(protocol
));
294 return enabled_protocols_
[
295 protocol
- ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION
];
298 void HttpNetworkSession::GetNextProtos(NextProtoVector
* next_protos
) const {
299 if (HttpStreamFactory::spdy_enabled()) {
300 *next_protos
= next_protos_
;
302 next_protos
->clear();
306 bool HttpNetworkSession::HasSpdyExclusion(
307 HostPortPair host_port_pair
) const {
308 return params_
.forced_spdy_exclusions
.find(host_port_pair
) !=
309 params_
.forced_spdy_exclusions
.end();
312 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
313 SocketPoolType pool_type
) {
315 case NORMAL_SOCKET_POOL
:
316 return normal_socket_pool_manager_
.get();
317 case WEBSOCKET_SOCKET_POOL
:
318 return websocket_socket_pool_manager_
.get();