Add include.
[chromium-blink-merge.git] / net / http / http_network_session.cc
blob3ec607eca02c77da0588ac324709bb7410e21003
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"
7 #include <utility>
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"
32 namespace {
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(
40 params.net_log,
41 params.client_socket_factory
42 ? params.client_socket_factory
43 : net::ClientSocketFactory::GetDefaultFactory(),
44 params.host_resolver,
45 params.cert_verifier,
46 params.channel_id_service,
47 params.transport_security_state,
48 params.cert_transparency_verifier,
49 params.ssl_session_cache_shard,
50 params.proxy_service,
51 params.ssl_config_service,
52 params.enable_ssl_connect_job_waiting,
53 params.proxy_delegate,
54 pool_type);
57 } // unnamed namespace
59 namespace net {
61 HttpNetworkSession::Params::Params()
62 : client_socket_factory(NULL),
63 host_resolver(NULL),
64 cert_verifier(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_websocket_over_spdy(false),
93 enable_quic(false),
94 enable_quic_port_selection(true),
95 quic_always_require_handshake_confirmation(false),
96 quic_disable_connection_pooling(false),
97 quic_load_server_info_timeout_ms(0),
98 quic_clock(NULL),
99 quic_random(NULL),
100 quic_max_packet_length(kDefaultMaxPacketSize),
101 enable_user_alternate_protocol_ports(false),
102 quic_crypto_client_stream_factory(NULL),
103 proxy_delegate(NULL) {
104 quic_supported_versions.push_back(QUIC_VERSION_23);
107 HttpNetworkSession::Params::~Params() {}
109 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
110 HttpNetworkSession::HttpNetworkSession(const Params& params)
111 : net_log_(params.net_log),
112 network_delegate_(params.network_delegate),
113 http_server_properties_(params.http_server_properties),
114 cert_verifier_(params.cert_verifier),
115 http_auth_handler_factory_(params.http_auth_handler_factory),
116 proxy_service_(params.proxy_service),
117 ssl_config_service_(params.ssl_config_service),
118 normal_socket_pool_manager_(
119 CreateSocketPoolManager(NORMAL_SOCKET_POOL, params)),
120 websocket_socket_pool_manager_(
121 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL, params)),
122 quic_stream_factory_(
123 params.host_resolver,
124 params.client_socket_factory
125 ? params.client_socket_factory
126 : net::ClientSocketFactory::GetDefaultFactory(),
127 params.http_server_properties,
128 params.cert_verifier,
129 params.channel_id_service,
130 params.transport_security_state,
131 params.quic_crypto_client_stream_factory,
132 params.quic_random ? params.quic_random : QuicRandom::GetInstance(),
133 params.quic_clock ? params.quic_clock : new QuicClock(),
134 params.quic_max_packet_length,
135 params.quic_user_agent_id,
136 params.quic_supported_versions,
137 params.enable_quic_port_selection,
138 params.quic_always_require_handshake_confirmation,
139 params.quic_disable_connection_pooling,
140 params.quic_load_server_info_timeout_ms,
141 params.quic_connection_options),
142 spdy_session_pool_(params.host_resolver,
143 params.ssl_config_service,
144 params.http_server_properties,
145 params.transport_security_state,
146 params.force_spdy_single_domain,
147 params.enable_spdy_compression,
148 params.enable_spdy_ping_based_connection_checking,
149 params.spdy_default_protocol,
150 params.spdy_stream_initial_recv_window_size,
151 params.spdy_initial_max_concurrent_streams,
152 params.spdy_max_concurrent_streams_limit,
153 params.time_func,
154 params.trusted_spdy_proxy),
155 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
156 http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
157 params_(params) {
158 DCHECK(proxy_service_);
159 DCHECK(ssl_config_service_.get());
160 CHECK(http_server_properties_);
162 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
163 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
164 enabled_protocols_[i - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = false;
167 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
168 // AlternateProtocol.
169 for (std::vector<NextProto>::const_iterator it = params_.next_protos.begin();
170 it != params_.next_protos.end(); ++it) {
171 NextProto proto = *it;
173 // Add the protocol to the TLS next protocol list, except for QUIC
174 // since it uses UDP.
175 if (proto != kProtoQUIC1SPDY3) {
176 next_protos_.push_back(SSLClientSocket::NextProtoToString(proto));
179 // Enable the corresponding alternate protocol, except for HTTP
180 // which has not corresponding alternative.
181 if (proto != kProtoHTTP11) {
182 AlternateProtocol alternate = AlternateProtocolFromNextProto(proto);
183 if (!IsAlternateProtocolValid(alternate)) {
184 NOTREACHED() << "Invalid next proto: " << proto;
185 continue;
187 enabled_protocols_[alternate - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] =
188 true;
192 if (HpackHuffmanAggregator::UseAggregator()) {
193 huffman_aggregator_.reset(new HpackHuffmanAggregator());
196 http_server_properties_->SetAlternateProtocolProbabilityThreshold(
197 params.alternate_protocol_probability_threshold);
200 HttpNetworkSession::~HttpNetworkSession() {
201 STLDeleteElements(&response_drainers_);
202 spdy_session_pool_.CloseAllSessions();
205 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) {
206 DCHECK(!ContainsKey(response_drainers_, drainer));
207 response_drainers_.insert(drainer);
210 void HttpNetworkSession::RemoveResponseDrainer(
211 HttpResponseBodyDrainer* drainer) {
212 DCHECK(ContainsKey(response_drainers_, drainer));
213 response_drainers_.erase(drainer);
216 TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool(
217 SocketPoolType pool_type) {
218 return GetSocketPoolManager(pool_type)->GetTransportSocketPool();
221 SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool(
222 SocketPoolType pool_type) {
223 return GetSocketPoolManager(pool_type)->GetSSLSocketPool();
226 SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
227 SocketPoolType pool_type,
228 const HostPortPair& socks_proxy) {
229 return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy(
230 socks_proxy);
233 HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy(
234 SocketPoolType pool_type,
235 const HostPortPair& http_proxy) {
236 return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy);
239 SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
240 SocketPoolType pool_type,
241 const HostPortPair& proxy_server) {
242 return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy(
243 proxy_server);
246 base::Value* HttpNetworkSession::SocketPoolInfoToValue() const {
247 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
248 return normal_socket_pool_manager_->SocketPoolInfoToValue();
251 base::Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
252 return spdy_session_pool_.SpdySessionPoolInfoToValue();
255 base::Value* HttpNetworkSession::QuicInfoToValue() const {
256 base::DictionaryValue* dict = new base::DictionaryValue();
257 dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue());
258 dict->SetBoolean("quic_enabled", params_.enable_quic);
259 dict->SetBoolean("enable_quic_port_selection",
260 params_.enable_quic_port_selection);
261 base::ListValue* connection_options = new base::ListValue;
262 for (QuicTagVector::const_iterator it =
263 params_.quic_connection_options.begin();
264 it != params_.quic_connection_options.end(); ++it) {
265 connection_options->AppendString("'" + QuicUtils::TagToString(*it) + "'");
267 dict->Set("connection_options", connection_options);
268 dict->SetString("origin_to_force_quic_on",
269 params_.origin_to_force_quic_on.ToString());
270 dict->SetDouble("alternate_protocol_probability_threshold",
271 params_.alternate_protocol_probability_threshold);
272 return dict;
275 void HttpNetworkSession::CloseAllConnections() {
276 normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
277 websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
278 spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED);
279 quic_stream_factory_.CloseAllSessions(ERR_ABORTED);
282 void HttpNetworkSession::CloseIdleConnections() {
283 normal_socket_pool_manager_->CloseIdleSockets();
284 websocket_socket_pool_manager_->CloseIdleSockets();
285 spdy_session_pool_.CloseCurrentIdleSessions();
288 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol) const {
289 DCHECK(IsAlternateProtocolValid(protocol));
290 return enabled_protocols_[
291 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
294 void HttpNetworkSession::GetNextProtos(
295 std::vector<std::string>* next_protos) const {
296 if (HttpStreamFactory::spdy_enabled()) {
297 *next_protos = next_protos_;
298 } else {
299 next_protos->clear();
303 bool HttpNetworkSession::HasSpdyExclusion(
304 HostPortPair host_port_pair) const {
305 return params_.forced_spdy_exclusions.find(host_port_pair) !=
306 params_.forced_spdy_exclusions.end();
309 ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager(
310 SocketPoolType pool_type) {
311 switch (pool_type) {
312 case NORMAL_SOCKET_POOL:
313 return normal_socket_pool_manager_.get();
314 case WEBSOCKET_SOCKET_POOL:
315 return websocket_socket_pool_manager_.get();
316 default:
317 NOTREACHED();
318 break;
320 return NULL;
323 } // namespace net