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_stream_factory.h"
24 #include "net/socket/client_socket_factory.h"
25 #include "net/socket/client_socket_pool_manager_impl.h"
26 #include "net/socket/next_proto.h"
27 #include "net/spdy/hpack_huffman_aggregator.h"
28 #include "net/spdy/spdy_session_pool.h"
32 net::ClientSocketPoolManager
* CreateSocketPoolManager(
33 net::HttpNetworkSession::SocketPoolType pool_type
,
34 const net::HttpNetworkSession::Params
& params
) {
35 // TODO(yutak): Differentiate WebSocket pool manager and allow more
36 // simultaneous connections for WebSockets.
37 return new net::ClientSocketPoolManagerImpl(
39 params
.client_socket_factory
?
40 params
.client_socket_factory
:
41 net::ClientSocketFactory::GetDefaultFactory(),
44 params
.server_bound_cert_service
,
45 params
.transport_security_state
,
46 params
.cert_transparency_verifier
,
47 params
.ssl_session_cache_shard
,
49 params
.ssl_config_service
,
53 } // unnamed namespace
57 HttpNetworkSession::Params::Params()
58 : client_socket_factory(NULL
),
61 server_bound_cert_service(NULL
),
62 transport_security_state(NULL
),
63 cert_transparency_verifier(NULL
),
65 ssl_config_service(NULL
),
66 http_auth_handler_factory(NULL
),
67 network_delegate(NULL
),
69 host_mapping_rules(NULL
),
70 force_http_pipelining(false),
71 ignore_certificate_errors(false),
72 http_pipelining_enabled(false),
73 testing_fixed_http_port(0),
74 testing_fixed_https_port(0),
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
),
84 enable_quic_https(false),
85 enable_quic_port_selection(true),
86 enable_quic_pacing(false),
87 enable_quic_time_based_loss_detection(false),
88 enable_quic_persist_server_info(false),
91 quic_max_packet_length(kDefaultMaxPacketSize
),
92 enable_user_alternate_protocol_ports(false),
93 quic_crypto_client_stream_factory(NULL
) {
94 quic_supported_versions
.push_back(QUIC_VERSION_16
);
97 HttpNetworkSession::Params::~Params() {}
99 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
100 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
101 : net_log_(params
.net_log
),
102 network_delegate_(params
.network_delegate
),
103 http_server_properties_(params
.http_server_properties
),
104 cert_verifier_(params
.cert_verifier
),
105 http_auth_handler_factory_(params
.http_auth_handler_factory
),
106 force_http_pipelining_(params
.force_http_pipelining
),
107 proxy_service_(params
.proxy_service
),
108 ssl_config_service_(params
.ssl_config_service
),
109 normal_socket_pool_manager_(
110 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
111 websocket_socket_pool_manager_(
112 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
113 quic_stream_factory_(params
.host_resolver
,
114 params
.client_socket_factory
?
115 params
.client_socket_factory
:
116 net::ClientSocketFactory::GetDefaultFactory(),
117 params
.http_server_properties
,
118 params
.cert_verifier
,
119 params
.quic_crypto_client_stream_factory
,
120 params
.quic_random
? params
.quic_random
:
121 QuicRandom::GetInstance(),
122 params
.quic_clock
? params
. quic_clock
:
124 params
.quic_max_packet_length
,
125 params
.quic_supported_versions
,
126 params
.enable_quic_port_selection
,
127 params
.enable_quic_pacing
,
128 params
.enable_quic_time_based_loss_detection
),
129 spdy_session_pool_(params
.host_resolver
,
130 params
.ssl_config_service
,
131 params
.http_server_properties
,
132 params
.force_spdy_single_domain
,
133 params
.enable_spdy_compression
,
134 params
.enable_spdy_ping_based_connection_checking
,
135 params
.spdy_default_protocol
,
136 params
.spdy_stream_initial_recv_window_size
,
137 params
.spdy_initial_max_concurrent_streams
,
138 params
.spdy_max_concurrent_streams_limit
,
140 params
.trusted_spdy_proxy
),
141 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
142 http_stream_factory_for_websocket_(
143 new HttpStreamFactoryImpl(this, true)),
145 DCHECK(proxy_service_
);
146 DCHECK(ssl_config_service_
.get());
147 CHECK(http_server_properties_
);
149 if (HpackHuffmanAggregator::UseAggregator()) {
150 huffman_aggregator_
.reset(new HpackHuffmanAggregator());
154 HttpNetworkSession::~HttpNetworkSession() {
155 STLDeleteElements(&response_drainers_
);
156 spdy_session_pool_
.CloseAllSessions();
159 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
160 DCHECK(!ContainsKey(response_drainers_
, drainer
));
161 response_drainers_
.insert(drainer
);
164 void HttpNetworkSession::RemoveResponseDrainer(
165 HttpResponseBodyDrainer
* drainer
) {
166 DCHECK(ContainsKey(response_drainers_
, drainer
));
167 response_drainers_
.erase(drainer
);
170 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
171 SocketPoolType pool_type
) {
172 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
175 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
176 SocketPoolType pool_type
) {
177 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
180 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
181 SocketPoolType pool_type
,
182 const HostPortPair
& socks_proxy
) {
183 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
187 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
188 SocketPoolType pool_type
,
189 const HostPortPair
& http_proxy
) {
190 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
193 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
194 SocketPoolType pool_type
,
195 const HostPortPair
& proxy_server
) {
196 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
200 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
201 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
202 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
205 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
206 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
209 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
210 base::DictionaryValue
* dict
= new base::DictionaryValue();
211 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
212 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
213 dict
->SetBoolean("quic_enabled_https", params_
.enable_quic_https
);
214 dict
->SetBoolean("enable_quic_port_selection",
215 params_
.enable_quic_port_selection
);
216 dict
->SetBoolean("enable_quic_pacing",
217 params_
.enable_quic_pacing
);
218 dict
->SetBoolean("enable_quic_time_based_loss_detection",
219 params_
.enable_quic_time_based_loss_detection
);
220 dict
->SetBoolean("enable_quic_persist_server_info",
221 params_
.enable_quic_persist_server_info
);
222 dict
->SetString("origin_to_force_quic_on",
223 params_
.origin_to_force_quic_on
.ToString());
227 void HttpNetworkSession::CloseAllConnections() {
228 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
229 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
230 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
231 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
234 void HttpNetworkSession::CloseIdleConnections() {
235 normal_socket_pool_manager_
->CloseIdleSockets();
236 websocket_socket_pool_manager_
->CloseIdleSockets();
237 spdy_session_pool_
.CloseCurrentIdleSessions();
240 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
241 SocketPoolType pool_type
) {
243 case NORMAL_SOCKET_POOL
:
244 return normal_socket_pool_manager_
.get();
245 case WEBSOCKET_SOCKET_POOL
:
246 return websocket_socket_pool_manager_
.get();