OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/http/http_network_session.h" | |
6 | |
7 #include <utility> | |
8 | |
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/hpack_huffman_aggregator.h" | |
32 #include "net/spdy/spdy_session_pool.h" | |
33 | |
34 namespace { | |
35 | |
36 net::ClientSocketPoolManager* CreateSocketPoolManager( | |
37 net::HttpNetworkSession::SocketPoolType pool_type, | |
38 const net::HttpNetworkSession::Params& params) { | |
39 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed | |
40 tracked_objects::ScopedTracker tracking_profile( | |
41 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
42 "454983 CreateSocketPoolManager")); | |
43 // TODO(yutak): Differentiate WebSocket pool manager and allow more | |
44 // simultaneous connections for WebSockets. | |
45 return new net::ClientSocketPoolManagerImpl( | |
46 params.net_log, params.client_socket_factory | |
47 ? params.client_socket_factory | |
48 : net::ClientSocketFactory::GetDefaultFactory(), | |
49 params.host_resolver, params.cert_verifier, params.channel_id_service, | |
50 params.transport_security_state, params.cert_transparency_verifier, | |
51 params.cert_policy_enforcer, params.ssl_session_cache_shard, | |
52 params.ssl_config_service, params.enable_ssl_connect_job_waiting, | |
53 pool_type); | |
54 } | |
55 | |
56 } // unnamed namespace | |
57 | |
58 namespace net { | |
59 | |
60 HttpNetworkSession::Params::Params() | |
61 : client_socket_factory(NULL), | |
62 host_resolver(NULL), | |
63 cert_verifier(NULL), | |
64 cert_policy_enforcer(NULL), | |
65 channel_id_service(NULL), | |
66 transport_security_state(NULL), | |
67 cert_transparency_verifier(NULL), | |
68 proxy_service(NULL), | |
69 ssl_config_service(NULL), | |
70 http_auth_handler_factory(NULL), | |
71 network_delegate(NULL), | |
72 net_log(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), | |
92 enable_quic(false), | |
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), | |
97 quic_load_server_info_timeout_srtt_multiplier(0.0f), | |
98 quic_enable_truncated_connection_ids(false), | |
99 quic_enable_connection_racing(false), | |
100 quic_clock(NULL), | |
101 quic_random(NULL), | |
102 quic_max_packet_length(kDefaultMaxPacketSize), | |
103 enable_user_alternate_protocol_ports(false), | |
104 quic_crypto_client_stream_factory(NULL), | |
105 proxy_delegate(NULL) { | |
106 quic_supported_versions.push_back(QUIC_VERSION_24); | |
107 } | |
108 | |
109 HttpNetworkSession::Params::~Params() {} | |
110 | |
111 // TODO(mbelshe): Move the socket factories into HttpStreamFactory. | |
112 HttpNetworkSession::HttpNetworkSession(const Params& params) | |
113 : net_log_(params.net_log), | |
114 network_delegate_(params.network_delegate), | |
115 http_server_properties_(params.http_server_properties), | |
116 cert_verifier_(params.cert_verifier), | |
117 http_auth_handler_factory_(params.http_auth_handler_factory), | |
118 proxy_service_(params.proxy_service), | |
119 ssl_config_service_(params.ssl_config_service), | |
120 normal_socket_pool_manager_( | |
121 CreateSocketPoolManager(NORMAL_SOCKET_POOL, params)), | |
122 websocket_socket_pool_manager_( | |
123 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL, params)), | |
124 quic_stream_factory_( | |
125 params.host_resolver, | |
126 params.client_socket_factory | |
127 ? params.client_socket_factory | |
128 : net::ClientSocketFactory::GetDefaultFactory(), | |
129 params.http_server_properties, | |
130 params.cert_verifier, | |
131 params.channel_id_service, | |
132 params.transport_security_state, | |
133 params.quic_crypto_client_stream_factory, | |
134 params.quic_random ? params.quic_random : QuicRandom::GetInstance(), | |
135 params.quic_clock ? params.quic_clock : new QuicClock(), | |
136 params.quic_max_packet_length, | |
137 params.quic_user_agent_id, | |
138 params.quic_supported_versions, | |
139 params.enable_quic_port_selection, | |
140 params.quic_always_require_handshake_confirmation, | |
141 params.quic_disable_connection_pooling, | |
142 params.quic_load_server_info_timeout_ms, | |
143 params.quic_load_server_info_timeout_srtt_multiplier, | |
144 params.quic_enable_truncated_connection_ids, | |
145 params.quic_enable_connection_racing, | |
146 params.quic_connection_options), | |
147 spdy_session_pool_(params.host_resolver, | |
148 params.ssl_config_service, | |
149 params.http_server_properties, | |
150 params.transport_security_state, | |
151 params.force_spdy_single_domain, | |
152 params.enable_spdy_compression, | |
153 params.enable_spdy_ping_based_connection_checking, | |
154 params.spdy_default_protocol, | |
155 params.spdy_stream_initial_recv_window_size, | |
156 params.spdy_initial_max_concurrent_streams, | |
157 params.spdy_max_concurrent_streams_limit, | |
158 params.time_func, | |
159 params.trusted_spdy_proxy), | |
160 http_stream_factory_(new HttpStreamFactoryImpl(this, false)), | |
161 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)), | |
162 params_(params) { | |
163 DCHECK(proxy_service_); | |
164 DCHECK(ssl_config_service_.get()); | |
165 CHECK(http_server_properties_); | |
166 // TODO(michaeln): Remove ScopedTracker below once crbug.com/454983 is fixed | |
167 tracked_objects::ScopedTracker tracking_profile( | |
168 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
169 "454983 HttpNetworkSession::HttpNetworkSession")); | |
170 | |
171 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION; | |
172 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) { | |
173 enabled_protocols_[i - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = false; | |
174 } | |
175 | |
176 // TODO(rtenneti): bug 116575 - consider combining the NextProto and | |
177 // AlternateProtocol. | |
178 for (std::vector<NextProto>::const_iterator it = params_.next_protos.begin(); | |
179 it != params_.next_protos.end(); ++it) { | |
180 NextProto proto = *it; | |
181 | |
182 // Add the protocol to the TLS next protocol list, except for QUIC | |
183 // since it uses UDP. | |
184 if (proto != kProtoQUIC1SPDY3) { | |
185 next_protos_.push_back(proto); | |
186 } | |
187 | |
188 // Enable the corresponding alternate protocol, except for HTTP | |
189 // which has not corresponding alternative. | |
190 if (proto != kProtoHTTP11) { | |
191 AlternateProtocol alternate = AlternateProtocolFromNextProto(proto); | |
192 if (!IsAlternateProtocolValid(alternate)) { | |
193 NOTREACHED() << "Invalid next proto: " << proto; | |
194 continue; | |
195 } | |
196 enabled_protocols_[alternate - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = | |
197 true; | |
198 } | |
199 } | |
200 | |
201 if (HpackHuffmanAggregator::UseAggregator()) { | |
202 huffman_aggregator_.reset(new HpackHuffmanAggregator()); | |
203 } | |
204 | |
205 http_server_properties_->SetAlternateProtocolProbabilityThreshold( | |
206 params.alternate_protocol_probability_threshold); | |
207 } | |
208 | |
209 HttpNetworkSession::~HttpNetworkSession() { | |
210 STLDeleteElements(&response_drainers_); | |
211 spdy_session_pool_.CloseAllSessions(); | |
212 } | |
213 | |
214 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) { | |
215 DCHECK(!ContainsKey(response_drainers_, drainer)); | |
216 response_drainers_.insert(drainer); | |
217 } | |
218 | |
219 void HttpNetworkSession::RemoveResponseDrainer( | |
220 HttpResponseBodyDrainer* drainer) { | |
221 DCHECK(ContainsKey(response_drainers_, drainer)); | |
222 response_drainers_.erase(drainer); | |
223 } | |
224 | |
225 TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool( | |
226 SocketPoolType pool_type) { | |
227 return GetSocketPoolManager(pool_type)->GetTransportSocketPool(); | |
228 } | |
229 | |
230 SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool( | |
231 SocketPoolType pool_type) { | |
232 return GetSocketPoolManager(pool_type)->GetSSLSocketPool(); | |
233 } | |
234 | |
235 SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy( | |
236 SocketPoolType pool_type, | |
237 const HostPortPair& socks_proxy) { | |
238 return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy( | |
239 socks_proxy); | |
240 } | |
241 | |
242 HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy( | |
243 SocketPoolType pool_type, | |
244 const HostPortPair& http_proxy) { | |
245 return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy); | |
246 } | |
247 | |
248 SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy( | |
249 SocketPoolType pool_type, | |
250 const HostPortPair& proxy_server) { | |
251 return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy( | |
252 proxy_server); | |
253 } | |
254 | |
255 base::Value* HttpNetworkSession::SocketPoolInfoToValue() const { | |
256 // TODO(yutak): Should merge values from normal pools and WebSocket pools. | |
257 return normal_socket_pool_manager_->SocketPoolInfoToValue(); | |
258 } | |
259 | |
260 base::Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const { | |
261 return spdy_session_pool_.SpdySessionPoolInfoToValue(); | |
262 } | |
263 | |
264 base::Value* HttpNetworkSession::QuicInfoToValue() const { | |
265 base::DictionaryValue* dict = new base::DictionaryValue(); | |
266 dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue()); | |
267 dict->SetBoolean("quic_enabled", params_.enable_quic); | |
268 dict->SetBoolean("enable_quic_port_selection", | |
269 params_.enable_quic_port_selection); | |
270 base::ListValue* connection_options = new base::ListValue; | |
271 for (QuicTagVector::const_iterator it = | |
272 params_.quic_connection_options.begin(); | |
273 it != params_.quic_connection_options.end(); ++it) { | |
274 connection_options->AppendString("'" + QuicUtils::TagToString(*it) + "'"); | |
275 } | |
276 dict->Set("connection_options", connection_options); | |
277 dict->SetString("origin_to_force_quic_on", | |
278 params_.origin_to_force_quic_on.ToString()); | |
279 dict->SetDouble("alternate_protocol_probability_threshold", | |
280 params_.alternate_protocol_probability_threshold); | |
281 return dict; | |
282 } | |
283 | |
284 void HttpNetworkSession::CloseAllConnections() { | |
285 normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED); | |
286 websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED); | |
287 spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED); | |
288 quic_stream_factory_.CloseAllSessions(ERR_ABORTED); | |
289 } | |
290 | |
291 void HttpNetworkSession::CloseIdleConnections() { | |
292 normal_socket_pool_manager_->CloseIdleSockets(); | |
293 websocket_socket_pool_manager_->CloseIdleSockets(); | |
294 spdy_session_pool_.CloseCurrentIdleSessions(); | |
295 } | |
296 | |
297 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol) const { | |
298 DCHECK(IsAlternateProtocolValid(protocol)); | |
299 return enabled_protocols_[ | |
300 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION]; | |
301 } | |
302 | |
303 void HttpNetworkSession::GetNextProtos(NextProtoVector* next_protos) const { | |
304 if (HttpStreamFactory::spdy_enabled()) { | |
305 *next_protos = next_protos_; | |
306 } else { | |
307 next_protos->clear(); | |
308 } | |
309 } | |
310 | |
311 bool HttpNetworkSession::HasSpdyExclusion( | |
312 HostPortPair host_port_pair) const { | |
313 return params_.forced_spdy_exclusions.find(host_port_pair) != | |
314 params_.forced_spdy_exclusions.end(); | |
315 } | |
316 | |
317 ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager( | |
318 SocketPoolType pool_type) { | |
319 switch (pool_type) { | |
320 case NORMAL_SOCKET_POOL: | |
321 return normal_socket_pool_manager_.get(); | |
322 case WEBSOCKET_SOCKET_POOL: | |
323 return websocket_socket_pool_manager_.get(); | |
324 default: | |
325 NOTREACHED(); | |
326 break; | |
327 } | |
328 return NULL; | |
329 } | |
330 | |
331 } // namespace net | |
OLD | NEW |