QUIC - enable persisting of QUICServerInfo (server config) to disk
[chromium-blink-merge.git] / net / http / http_network_session.cc
blob78e992b4947ee6b38b4a656336801881dac0ece8
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_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"
30 namespace {
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(
38 params.net_log,
39 params.client_socket_factory ?
40 params.client_socket_factory :
41 net::ClientSocketFactory::GetDefaultFactory(),
42 params.host_resolver,
43 params.cert_verifier,
44 params.channel_id_service,
45 params.transport_security_state,
46 params.cert_transparency_verifier,
47 params.ssl_session_cache_shard,
48 params.proxy_service,
49 params.ssl_config_service,
50 pool_type);
53 } // unnamed namespace
55 namespace net {
57 HttpNetworkSession::Params::Params()
58 : client_socket_factory(NULL),
59 host_resolver(NULL),
60 cert_verifier(NULL),
61 channel_id_service(NULL),
62 transport_security_state(NULL),
63 cert_transparency_verifier(NULL),
64 proxy_service(NULL),
65 ssl_config_service(NULL),
66 http_auth_handler_factory(NULL),
67 network_delegate(NULL),
68 net_log(NULL),
69 host_mapping_rules(NULL),
70 ignore_certificate_errors(false),
71 testing_fixed_http_port(0),
72 testing_fixed_https_port(0),
73 force_spdy_single_domain(false),
74 enable_spdy_compression(true),
75 enable_spdy_ping_based_connection_checking(true),
76 spdy_default_protocol(kProtoUnknown),
77 spdy_stream_initial_recv_window_size(0),
78 spdy_initial_max_concurrent_streams(0),
79 spdy_max_concurrent_streams_limit(0),
80 time_func(&base::TimeTicks::Now),
81 force_spdy_over_ssl(true),
82 force_spdy_always(false),
83 use_alternate_protocols(false),
84 alternate_protocol_probability_threshold(1),
85 enable_websocket_over_spdy(false),
86 enable_quic(false),
87 enable_quic_port_selection(true),
88 enable_quic_pacing(false),
89 enable_quic_time_based_loss_detection(false),
90 enable_quic_persist_server_info(true),
91 quic_clock(NULL),
92 quic_random(NULL),
93 quic_max_packet_length(kDefaultMaxPacketSize),
94 enable_user_alternate_protocol_ports(false),
95 quic_crypto_client_stream_factory(NULL) {
96 quic_supported_versions.push_back(QUIC_VERSION_19);
99 HttpNetworkSession::Params::~Params() {}
101 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
102 HttpNetworkSession::HttpNetworkSession(const Params& params)
103 : net_log_(params.net_log),
104 network_delegate_(params.network_delegate),
105 http_server_properties_(params.http_server_properties),
106 cert_verifier_(params.cert_verifier),
107 http_auth_handler_factory_(params.http_auth_handler_factory),
108 proxy_service_(params.proxy_service),
109 ssl_config_service_(params.ssl_config_service),
110 normal_socket_pool_manager_(
111 CreateSocketPoolManager(NORMAL_SOCKET_POOL, params)),
112 websocket_socket_pool_manager_(
113 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL, params)),
114 quic_stream_factory_(params.host_resolver,
115 params.client_socket_factory ?
116 params.client_socket_factory :
117 net::ClientSocketFactory::GetDefaultFactory(),
118 params.http_server_properties,
119 params.cert_verifier,
120 params.channel_id_service,
121 params.quic_crypto_client_stream_factory,
122 params.quic_random ? params.quic_random :
123 QuicRandom::GetInstance(),
124 params.quic_clock ? params. quic_clock :
125 new QuicClock(),
126 params.quic_max_packet_length,
127 params.quic_user_agent_id,
128 params.quic_supported_versions,
129 params.enable_quic_port_selection,
130 params.enable_quic_pacing,
131 params.enable_quic_time_based_loss_detection,
132 params.quic_connection_options),
133 spdy_session_pool_(params.host_resolver,
134 params.ssl_config_service,
135 params.http_server_properties,
136 params.force_spdy_single_domain,
137 params.enable_spdy_compression,
138 params.enable_spdy_ping_based_connection_checking,
139 params.spdy_default_protocol,
140 params.spdy_stream_initial_recv_window_size,
141 params.spdy_initial_max_concurrent_streams,
142 params.spdy_max_concurrent_streams_limit,
143 params.time_func,
144 params.trusted_spdy_proxy),
145 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
146 http_stream_factory_for_websocket_(
147 new HttpStreamFactoryImpl(this, true)),
148 params_(params) {
149 DCHECK(proxy_service_);
150 DCHECK(ssl_config_service_.get());
151 CHECK(http_server_properties_);
153 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
154 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
155 enabled_protocols_[i - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = false;
158 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
159 // AlternateProtocol.
160 for (std::vector<NextProto>::const_iterator it = params_.next_protos.begin();
161 it != params_.next_protos.end(); ++it) {
162 NextProto proto = *it;
164 // Add the protocol to the TLS next protocol list, except for QUIC
165 // since it uses UDP.
166 if (proto != kProtoQUIC1SPDY3) {
167 next_protos_.push_back(SSLClientSocket::NextProtoToString(proto));
170 // Enable the corresponding alternate protocol, except for HTTP
171 // which has not corresponding alternative.
172 if (proto != kProtoHTTP11) {
173 AlternateProtocol alternate = AlternateProtocolFromNextProto(proto);
174 if (!IsAlternateProtocolValid(alternate)) {
175 NOTREACHED() << "Invalid next proto: " << proto;
176 continue;
178 enabled_protocols_[alternate - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] =
179 true;
183 if (HpackHuffmanAggregator::UseAggregator()) {
184 huffman_aggregator_.reset(new HpackHuffmanAggregator());
187 http_server_properties_->SetAlternateProtocolProbabilityThreshold(
188 params.alternate_protocol_probability_threshold);
191 HttpNetworkSession::~HttpNetworkSession() {
192 STLDeleteElements(&response_drainers_);
193 spdy_session_pool_.CloseAllSessions();
196 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) {
197 DCHECK(!ContainsKey(response_drainers_, drainer));
198 response_drainers_.insert(drainer);
201 void HttpNetworkSession::RemoveResponseDrainer(
202 HttpResponseBodyDrainer* drainer) {
203 DCHECK(ContainsKey(response_drainers_, drainer));
204 response_drainers_.erase(drainer);
207 TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool(
208 SocketPoolType pool_type) {
209 return GetSocketPoolManager(pool_type)->GetTransportSocketPool();
212 SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool(
213 SocketPoolType pool_type) {
214 return GetSocketPoolManager(pool_type)->GetSSLSocketPool();
217 SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
218 SocketPoolType pool_type,
219 const HostPortPair& socks_proxy) {
220 return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy(
221 socks_proxy);
224 HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy(
225 SocketPoolType pool_type,
226 const HostPortPair& http_proxy) {
227 return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy);
230 SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
231 SocketPoolType pool_type,
232 const HostPortPair& proxy_server) {
233 return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy(
234 proxy_server);
237 base::Value* HttpNetworkSession::SocketPoolInfoToValue() const {
238 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
239 return normal_socket_pool_manager_->SocketPoolInfoToValue();
242 base::Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
243 return spdy_session_pool_.SpdySessionPoolInfoToValue();
246 base::Value* HttpNetworkSession::QuicInfoToValue() const {
247 base::DictionaryValue* dict = new base::DictionaryValue();
248 dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue());
249 dict->SetBoolean("quic_enabled", params_.enable_quic);
250 dict->SetBoolean("enable_quic_port_selection",
251 params_.enable_quic_port_selection);
252 dict->SetBoolean("enable_quic_pacing",
253 params_.enable_quic_pacing);
254 dict->SetBoolean("enable_quic_time_based_loss_detection",
255 params_.enable_quic_time_based_loss_detection);
256 dict->SetBoolean("enable_quic_persist_server_info",
257 params_.enable_quic_persist_server_info);
258 dict->SetString("origin_to_force_quic_on",
259 params_.origin_to_force_quic_on.ToString());
260 return dict;
263 void HttpNetworkSession::CloseAllConnections() {
264 normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
265 websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
266 spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED);
267 quic_stream_factory_.CloseAllSessions(ERR_ABORTED);
270 void HttpNetworkSession::CloseIdleConnections() {
271 normal_socket_pool_manager_->CloseIdleSockets();
272 websocket_socket_pool_manager_->CloseIdleSockets();
273 spdy_session_pool_.CloseCurrentIdleSessions();
276 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol) const {
277 DCHECK(IsAlternateProtocolValid(protocol));
278 return enabled_protocols_[
279 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
282 void HttpNetworkSession::GetNextProtos(
283 std::vector<std::string>* next_protos) const {
284 if (HttpStreamFactory::spdy_enabled()) {
285 *next_protos = next_protos_;
286 } else {
287 next_protos->clear();
291 bool HttpNetworkSession::HasSpdyExclusion(
292 HostPortPair host_port_pair) const {
293 return params_.forced_spdy_exclusions.find(host_port_pair) !=
294 params_.forced_spdy_exclusions.end();
297 ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager(
298 SocketPoolType pool_type) {
299 switch (pool_type) {
300 case NORMAL_SOCKET_POOL:
301 return normal_socket_pool_manager_.get();
302 case WEBSOCKET_SOCKET_POOL:
303 return websocket_socket_pool_manager_.get();
304 default:
305 NOTREACHED();
306 break;
308 return NULL;
311 } // namespace net