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/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"
31
32namespace {
33
34net::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);
55}
56
57}  // unnamed namespace
58
59namespace net {
60
61HttpNetworkSession::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      testing_fixed_http_port(0),
77      testing_fixed_https_port(0),
78      enable_tcp_fast_open_for_ssl(false),
79      force_spdy_single_domain(false),
80      enable_spdy_compression(true),
81      enable_spdy_ping_based_connection_checking(true),
82      spdy_default_protocol(kProtoUnknown),
83      spdy_stream_initial_recv_window_size(0),
84      spdy_initial_max_concurrent_streams(0),
85      spdy_max_concurrent_streams_limit(0),
86      time_func(&base::TimeTicks::Now),
87      force_spdy_over_ssl(true),
88      force_spdy_always(false),
89      use_alternate_protocols(false),
90      alternate_protocol_probability_threshold(1),
91      enable_websocket_over_spdy(false),
92      enable_quic(false),
93      enable_quic_port_selection(true),
94      enable_quic_time_based_loss_detection(false),
95      quic_always_require_handshake_confirmation(false),
96      quic_disable_connection_pooling(false),
97      quic_clock(NULL),
98      quic_random(NULL),
99      quic_max_packet_length(kDefaultMaxPacketSize),
100      enable_user_alternate_protocol_ports(false),
101      quic_crypto_client_stream_factory(NULL),
102      proxy_delegate(NULL) {
103  quic_supported_versions.push_back(QUIC_VERSION_23);
104}
105
106HttpNetworkSession::Params::~Params() {}
107
108// TODO(mbelshe): Move the socket factories into HttpStreamFactory.
109HttpNetworkSession::HttpNetworkSession(const Params& params)
110    : net_log_(params.net_log),
111      network_delegate_(params.network_delegate),
112      http_server_properties_(params.http_server_properties),
113      cert_verifier_(params.cert_verifier),
114      http_auth_handler_factory_(params.http_auth_handler_factory),
115      proxy_service_(params.proxy_service),
116      ssl_config_service_(params.ssl_config_service),
117      normal_socket_pool_manager_(
118          CreateSocketPoolManager(NORMAL_SOCKET_POOL, params)),
119      websocket_socket_pool_manager_(
120          CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL, params)),
121      quic_stream_factory_(
122          params.host_resolver,
123          params.client_socket_factory
124              ? params.client_socket_factory
125              : net::ClientSocketFactory::GetDefaultFactory(),
126          params.http_server_properties,
127          params.cert_verifier,
128          params.channel_id_service,
129          params.transport_security_state,
130          params.quic_crypto_client_stream_factory,
131          params.quic_random ? params.quic_random : QuicRandom::GetInstance(),
132          params.quic_clock ? params.quic_clock : new QuicClock(),
133          params.quic_max_packet_length,
134          params.quic_user_agent_id,
135          params.quic_supported_versions,
136          params.enable_quic_port_selection,
137          params.enable_quic_time_based_loss_detection,
138          params.quic_always_require_handshake_confirmation,
139          params.quic_disable_connection_pooling,
140          params.quic_connection_options),
141      spdy_session_pool_(params.host_resolver,
142                         params.ssl_config_service,
143                         params.http_server_properties,
144                         params.transport_security_state,
145                         params.force_spdy_single_domain,
146                         params.enable_spdy_compression,
147                         params.enable_spdy_ping_based_connection_checking,
148                         params.spdy_default_protocol,
149                         params.spdy_stream_initial_recv_window_size,
150                         params.spdy_initial_max_concurrent_streams,
151                         params.spdy_max_concurrent_streams_limit,
152                         params.time_func,
153                         params.trusted_spdy_proxy),
154      http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
155      http_stream_factory_for_websocket_(new HttpStreamFactoryImpl(this, true)),
156      params_(params) {
157  DCHECK(proxy_service_);
158  DCHECK(ssl_config_service_.get());
159  CHECK(http_server_properties_);
160
161  for (int i = ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION;
162       i <= ALTERNATE_PROTOCOL_MAXIMUM_VALID_VERSION; ++i) {
163    enabled_protocols_[i - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] = false;
164  }
165
166  // TODO(rtenneti): bug 116575 - consider combining the NextProto and
167  // AlternateProtocol.
168  for (std::vector<NextProto>::const_iterator it = params_.next_protos.begin();
169       it != params_.next_protos.end(); ++it) {
170    NextProto proto = *it;
171
172    // Add the protocol to the TLS next protocol list, except for QUIC
173    // since it uses UDP.
174    if (proto != kProtoQUIC1SPDY3) {
175      next_protos_.push_back(SSLClientSocket::NextProtoToString(proto));
176    }
177
178    // Enable the corresponding alternate protocol, except for HTTP
179    // which has not corresponding alternative.
180    if (proto != kProtoHTTP11) {
181      AlternateProtocol alternate = AlternateProtocolFromNextProto(proto);
182      if (!IsAlternateProtocolValid(alternate)) {
183        NOTREACHED() << "Invalid next proto: " << proto;
184        continue;
185      }
186      enabled_protocols_[alternate - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION] =
187          true;
188    }
189  }
190
191  if (HpackHuffmanAggregator::UseAggregator()) {
192    huffman_aggregator_.reset(new HpackHuffmanAggregator());
193  }
194
195  http_server_properties_->SetAlternateProtocolProbabilityThreshold(
196      params.alternate_protocol_probability_threshold);
197}
198
199HttpNetworkSession::~HttpNetworkSession() {
200  STLDeleteElements(&response_drainers_);
201  spdy_session_pool_.CloseAllSessions();
202}
203
204void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer* drainer) {
205  DCHECK(!ContainsKey(response_drainers_, drainer));
206  response_drainers_.insert(drainer);
207}
208
209void HttpNetworkSession::RemoveResponseDrainer(
210    HttpResponseBodyDrainer* drainer) {
211  DCHECK(ContainsKey(response_drainers_, drainer));
212  response_drainers_.erase(drainer);
213}
214
215TransportClientSocketPool* HttpNetworkSession::GetTransportSocketPool(
216    SocketPoolType pool_type) {
217  return GetSocketPoolManager(pool_type)->GetTransportSocketPool();
218}
219
220SSLClientSocketPool* HttpNetworkSession::GetSSLSocketPool(
221    SocketPoolType pool_type) {
222  return GetSocketPoolManager(pool_type)->GetSSLSocketPool();
223}
224
225SOCKSClientSocketPool* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
226    SocketPoolType pool_type,
227    const HostPortPair& socks_proxy) {
228  return GetSocketPoolManager(pool_type)->GetSocketPoolForSOCKSProxy(
229      socks_proxy);
230}
231
232HttpProxyClientSocketPool* HttpNetworkSession::GetSocketPoolForHTTPProxy(
233    SocketPoolType pool_type,
234    const HostPortPair& http_proxy) {
235  return GetSocketPoolManager(pool_type)->GetSocketPoolForHTTPProxy(http_proxy);
236}
237
238SSLClientSocketPool* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
239    SocketPoolType pool_type,
240    const HostPortPair& proxy_server) {
241  return GetSocketPoolManager(pool_type)->GetSocketPoolForSSLWithProxy(
242      proxy_server);
243}
244
245base::Value* HttpNetworkSession::SocketPoolInfoToValue() const {
246  // TODO(yutak): Should merge values from normal pools and WebSocket pools.
247  return normal_socket_pool_manager_->SocketPoolInfoToValue();
248}
249
250base::Value* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
251  return spdy_session_pool_.SpdySessionPoolInfoToValue();
252}
253
254base::Value* HttpNetworkSession::QuicInfoToValue() const {
255  base::DictionaryValue* dict = new base::DictionaryValue();
256  dict->Set("sessions", quic_stream_factory_.QuicStreamFactoryInfoToValue());
257  dict->SetBoolean("quic_enabled", params_.enable_quic);
258  dict->SetBoolean("enable_quic_port_selection",
259                   params_.enable_quic_port_selection);
260  base::ListValue* connection_options = new base::ListValue;
261  for (QuicTagVector::const_iterator it =
262           params_.quic_connection_options.begin();
263       it != params_.quic_connection_options.end(); ++it) {
264    connection_options->AppendString("'" + QuicUtils::TagToString(*it) + "'");
265  }
266  dict->Set("connection_options", connection_options);
267  dict->SetBoolean("enable_quic_time_based_loss_detection",
268                   params_.enable_quic_time_based_loss_detection);
269  dict->SetString("origin_to_force_quic_on",
270                  params_.origin_to_force_quic_on.ToString());
271  dict->SetDouble("alternate_protocol_probability_threshold",
272                  params_.alternate_protocol_probability_threshold);
273  return dict;
274}
275
276void HttpNetworkSession::CloseAllConnections() {
277  normal_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
278  websocket_socket_pool_manager_->FlushSocketPoolsWithError(ERR_ABORTED);
279  spdy_session_pool_.CloseCurrentSessions(ERR_ABORTED);
280  quic_stream_factory_.CloseAllSessions(ERR_ABORTED);
281}
282
283void HttpNetworkSession::CloseIdleConnections() {
284  normal_socket_pool_manager_->CloseIdleSockets();
285  websocket_socket_pool_manager_->CloseIdleSockets();
286  spdy_session_pool_.CloseCurrentIdleSessions();
287}
288
289bool HttpNetworkSession::IsProtocolEnabled(AlternateProtocol protocol) const {
290  DCHECK(IsAlternateProtocolValid(protocol));
291  return enabled_protocols_[
292      protocol - ALTERNATE_PROTOCOL_MINIMUM_VALID_VERSION];
293}
294
295void HttpNetworkSession::GetNextProtos(
296    std::vector<std::string>* next_protos) const {
297  if (HttpStreamFactory::spdy_enabled()) {
298    *next_protos = next_protos_;
299  } else {
300    next_protos->clear();
301  }
302}
303
304bool HttpNetworkSession::HasSpdyExclusion(
305    HostPortPair host_port_pair) const {
306  return params_.forced_spdy_exclusions.find(host_port_pair) !=
307      params_.forced_spdy_exclusions.end();
308}
309
310ClientSocketPoolManager* HttpNetworkSession::GetSocketPoolManager(
311    SocketPoolType pool_type) {
312  switch (pool_type) {
313    case NORMAL_SOCKET_POOL:
314      return normal_socket_pool_manager_.get();
315    case WEBSOCKET_SOCKET_POOL:
316      return websocket_socket_pool_manager_.get();
317    default:
318      NOTREACHED();
319      break;
320  }
321  return NULL;
322}
323
324}  //  namespace net
325