summaryrefslogtreecommitdiffstats
path: root/net/http/http_network_session.cc
blob: 971786a50533b5cf210e9011964913cb1e38c25d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http/http_network_session.h"

#include <utility>

#include "base/logging.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "net/http/http_auth_handler_factory.h"
#include "net/http/url_security_manager.h"
#include "net/spdy/spdy_session_pool.h"

namespace net {

namespace {

// Total limit of sockets.
int g_max_sockets = 256;

// Default to allow up to 6 connections per host. Experiment and tuning may
// try other values (greater than 0).  Too large may cause many problems, such
// as home routers blocking the connections!?!?  See http://crbug.com/12066.
int g_max_sockets_per_group = 6;

// The max number of sockets to allow per proxy server.  This applies both to
// http and SOCKS proxies.  See http://crbug.com/12066 and
// http://crbug.com/44501 for details about proxy server connection limits.
int g_max_sockets_per_proxy_server = 32;

uint16 g_fixed_http_port = 0;
uint16 g_fixed_https_port = 0;

}  // namespace

HttpNetworkSession::HttpNetworkSession(
    HostResolver* host_resolver,
    ProxyService* proxy_service,
    ClientSocketFactory* client_socket_factory,
    SSLConfigService* ssl_config_service,
    SpdySessionPool* spdy_session_pool,
    HttpAuthHandlerFactory* http_auth_handler_factory,
    HttpNetworkDelegate* network_delegate,
    NetLog* net_log)
    : tcp_pool_histograms_(new ClientSocketPoolHistograms("TCP")),
      tcp_for_http_proxy_pool_histograms_(
          new ClientSocketPoolHistograms("TCPforHTTPProxy")),
      http_proxy_pool_histograms_(new ClientSocketPoolHistograms("HTTPProxy")),
      tcp_for_socks_pool_histograms_(
          new ClientSocketPoolHistograms("TCPforSOCKS")),
      socks_pool_histograms_(new ClientSocketPoolHistograms("SOCK")),
      ssl_pool_histograms_(new ClientSocketPoolHistograms("SSL")),
      tcp_socket_pool_(new TCPClientSocketPool(
          g_max_sockets, g_max_sockets_per_group, tcp_pool_histograms_,
          host_resolver, client_socket_factory, net_log)),
      ssl_socket_pool_(new SSLClientSocketPool(
          g_max_sockets, g_max_sockets_per_group, ssl_pool_histograms_,
          host_resolver, client_socket_factory, tcp_socket_pool_, NULL,
          NULL, net_log)),
      socket_factory_(client_socket_factory),
      host_resolver_(host_resolver),
      proxy_service_(proxy_service),
      ssl_config_service_(ssl_config_service),
      spdy_session_pool_(spdy_session_pool),
      http_auth_handler_factory_(http_auth_handler_factory),
      network_delegate_(network_delegate),
      net_log_(net_log) {
  DCHECK(proxy_service);
  DCHECK(ssl_config_service);
}

HttpNetworkSession::~HttpNetworkSession() {
}

const scoped_refptr<HttpProxyClientSocketPool>&
HttpNetworkSession::GetSocketPoolForHTTPProxy(const HostPortPair& http_proxy) {
  HTTPProxySocketPoolMap::const_iterator it =
      http_proxy_socket_pools_.find(http_proxy);
  if (it != http_proxy_socket_pools_.end())
    return it->second;

  std::pair<HTTPProxySocketPoolMap::iterator, bool> ret =
      http_proxy_socket_pools_.insert(
          std::make_pair(
              http_proxy,
              new HttpProxyClientSocketPool(
                  g_max_sockets_per_proxy_server, g_max_sockets_per_group,
                  http_proxy_pool_histograms_, host_resolver_,
                  new TCPClientSocketPool(
                      g_max_sockets_per_proxy_server, g_max_sockets_per_group,
                      tcp_for_http_proxy_pool_histograms_, host_resolver_,
                      socket_factory_, net_log_),
                  net_log_)));

  return ret.first->second;
}

const scoped_refptr<SOCKSClientSocketPool>&
HttpNetworkSession::GetSocketPoolForSOCKSProxy(
    const HostPortPair& socks_proxy) {
  SOCKSSocketPoolMap::const_iterator it = socks_socket_pools_.find(socks_proxy);
  if (it != socks_socket_pools_.end())
    return it->second;

  std::pair<SOCKSSocketPoolMap::iterator, bool> ret =
      socks_socket_pools_.insert(
          std::make_pair(socks_proxy, new SOCKSClientSocketPool(
              g_max_sockets_per_proxy_server, g_max_sockets_per_group,
              socks_pool_histograms_, host_resolver_,
              new TCPClientSocketPool(g_max_sockets_per_proxy_server,
                  g_max_sockets_per_group, tcp_for_socks_pool_histograms_,
                  host_resolver_, socket_factory_, net_log_),
              net_log_)));

  return ret.first->second;
}

const scoped_refptr<SSLClientSocketPool>&
HttpNetworkSession::GetSocketPoolForSSLWithProxy(
    const HostPortPair& proxy_server) {
  SSLSocketPoolMap::const_iterator it =
      ssl_socket_pools_for_proxies_.find(proxy_server);
  if (it != ssl_socket_pools_for_proxies_.end())
    return it->second;

  SSLClientSocketPool* new_pool = new SSLClientSocketPool(
      g_max_sockets_per_proxy_server, g_max_sockets_per_group,
      ssl_pool_histograms_, host_resolver_, socket_factory_,
      NULL,
      GetSocketPoolForHTTPProxy(proxy_server),
      GetSocketPoolForSOCKSProxy(proxy_server),
      net_log_);

  std::pair<SSLSocketPoolMap::iterator, bool> ret =
      ssl_socket_pools_for_proxies_.insert(std::make_pair(proxy_server,
                                                          new_pool));

  return ret.first->second;
}

// static
void HttpNetworkSession::set_max_sockets_per_group(int socket_count) {
  DCHECK_LT(0, socket_count);
  // The following is a sanity check... but we should NEVER be near this value.
  DCHECK_GT(100, socket_count);
  g_max_sockets_per_group = socket_count;
}

// static
uint16 HttpNetworkSession::fixed_http_port() {
  return g_fixed_http_port;
}

// static
void HttpNetworkSession::set_fixed_http_port(uint16 port) {
  g_fixed_http_port = port;
}

// static
uint16 HttpNetworkSession::fixed_https_port() {
  return g_fixed_https_port;
}

// static
void HttpNetworkSession::set_fixed_https_port(uint16 port) {
  g_fixed_https_port = port;
}

}  //  namespace net