Extract protobuf database into a new 'leveldb_proto' component
[chromium-blink-merge.git] / net / http / http_network_session.cc
blob5fc60701e143cfd77b8b2fbc494c60b769a58295
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.server_bound_cert_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 server_bound_cert_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 enable_websocket_over_spdy(false),
85 enable_quic(false),
86 enable_quic_https(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(false),
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_18);
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.quic_crypto_client_stream_factory,
121 params.quic_random ? params.quic_random :
122 QuicRandom::GetInstance(),
123 params.quic_clock ? params. quic_clock :
124 new QuicClock(),
125 params.quic_max_packet_length,
126 params.quic_user_agent_id,
127 params.quic_supported_versions,
128 params.enable_quic_port_selection,
129 params.enable_quic_pacing,
130 params.enable_quic_time_based_loss_detection),
131 spdy_session_pool_(params.host_resolver,
132 params.ssl_config_service,
133 params.http_server_properties,
134 params.force_spdy_single_domain,
135 params.enable_spdy_compression,
136 params.enable_spdy_ping_based_connection_checking,
137 params.spdy_default_protocol,
138 params.spdy_stream_initial_recv_window_size,
139 params.spdy_initial_max_concurrent_streams,
140 params.spdy_max_concurrent_streams_limit,
141 params.time_func,
142 params.trusted_spdy_proxy),
143 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
144 http_stream_factory_for_websocket_(
145 new HttpStreamFactoryImpl(this, true)),
146 params_(params) {
147 DCHECK(proxy_service_);
148 DCHECK(ssl_config_service_.get());
149 CHECK(http_server_properties_);
151 for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
152 i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
153 enabled_protocols_[i - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = false;
156 // TODO(rtenneti): bug 116575 - consider combining the NextProto and
157 // AlternateProtocol.
158 for (std::vector<NextProto>::const_iterator it = params_.next_protos.begin();
159 it != params_.next_protos.end(); ++it) {
160 NextProto proto = *it;
162 // Add the protocol to the TLS next protocol list, except for QUIC
163 // since it uses UDP.
164 if (proto != kProtoQUIC1SPDY3) {
165 next_protos_.push_back(SSLClientSocket::NextProtoToString(proto));
168 // Enable the corresponding alternate protocol, except for HTTP
169 // which has not corresponding alternative.
170 if (proto != kProtoHTTP11) {
171 AlternateProtocol alternate = AlternateProtocolFromNextProto(proto);
172 if (!IsAlternateProtocolValid(alternate)) {
173 NOTREACHED() << "Invalid next proto: " << proto;
174 continue;
176 enabled_protocols_[alternate - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] =
177 true;
181 if (HpackHuffmanAggregator::UseAggregator()) {
182 huffman_aggregator_.reset(new HpackHuffmanAggregator());
186 HttpNetworkSession::~HttpNetworkSession() {
187 STLDeleteElements(&response_drainers_);
188 spdy_session_pool_.CloseAllSessions();
191 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) {
192 DCHECK(!ContainsKey(response_drainers_, drainer));
193 response_drainers_.insert(drainer);
196 void HttpNetworkSession::RemoveResponseDrainer(
197 HttpResponseBodyDrainer* drainer) {
198 DCHECK(ContainsKey(response_drainers_, drainer));
199 response_drainers_.erase(drainer);
202 TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool(
203 SocketPoolType pool_type) {
204 return GetSocketPoolManager(pool_type)->GetTransportSocketPool();
207 SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool(
208 SocketPoolType pool_type) {
209 return GetSocketPoolManager(pool_type)->GetSSLSocketPool();
212 SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
213 SocketPoolType pool_type,
214 const HostPortPair& socks_proxy) {
215 return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy(
216 socks_proxy);
219 HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy(
220 SocketPoolType pool_type,
221 const HostPortPair& http_proxy) {
222 return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy);
225 SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
226 SocketPoolType pool_type,
227 const HostPortPair& proxy_server) {
228 return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy(
229 proxy_server);
232 base::Value* HttpNetworkSession::SocketPoolInfoToValue() const {
233 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
234 return normal_socket_pool_manager_->SocketPoolInfoToValue();
237 base::Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
238 return spdy_session_pool_.SpdySessionPoolInfoToValue();
241 base::Value* HttpNetworkSession::QuicInfoToValue() const {
242 base::DictionaryValue* dict = new base::DictionaryValue();
243 dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue());
244 dict->SetBoolean("quic_enabled", params_.enable_quic);
245 dict->SetBoolean("quic_enabled_https", params_.enable_quic_https);
246 dict->SetBoolean("enable_quic_port_selection",
247 params_.enable_quic_port_selection);
248 dict->SetBoolean("enable_quic_pacing",
249 params_.enable_quic_pacing);
250 dict->SetBoolean("enable_quic_time_based_loss_detection",
251 params_.enable_quic_time_based_loss_detection);
252 dict->SetBoolean("enable_quic_persist_server_info",
253 params_.enable_quic_persist_server_info);
254 dict->SetString("origin_to_force_quic_on",
255 params_.origin_to_force_quic_on.ToString());
256 return dict;
259 void HttpNetworkSession::CloseAllConnections() {
260 normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
261 websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
262 spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED);
263 quic_stream_factory_.CloseAllSessions(ERR_ABORTED);
266 void HttpNetworkSession::CloseIdleConnections() {
267 normal_socket_pool_manager_->CloseIdleSockets();
268 websocket_socket_pool_manager_->CloseIdleSockets();
269 spdy_session_pool_.CloseCurrentIdleSessions();
272 bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol) const {
273 DCHECK(IsAlternateProtocolValid(protocol));
274 return enabled_protocols_[
275 protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
278 void HttpNetworkSession::GetNextProtos(
279 std::vector<std::string>* next_protos) const {
280 if (HttpStreamFactory::spdy_enabled()) {
281 *next_protos = next_protos_;
282 } else {
283 next_protos->clear();
287 bool HttpNetworkSession::HasSpdyExclusion(
288 HostPortPair host_port_pair) const {
289 return params_.forced_spdy_exclusions.find(host_port_pair) !=
290 params_.forced_spdy_exclusions.end();
293 ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager(
294 SocketPoolType pool_type) {
295 switch (pool_type) {
296 case NORMAL_SOCKET_POOL:
297 return normal_socket_pool_manager_.get();
298 case WEBSOCKET_SOCKET_POOL:
299 return websocket_socket_pool_manager_.get();
300 default:
301 NOTREACHED();
302 break;
304 return NULL;
307 } // namespace net