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"
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/spdy_session_pool.h"
31 net::ClientSocketPoolManager
* CreateSocketPoolManager(
32 net::HttpNetworkSession::SocketPoolType pool_type
,
33 const net::HttpNetworkSession::Params
& params
) {
34 // TODO(yutak): Differentiate WebSocket pool manager and allow more
35 // simultaneous connections for WebSockets.
36 return new net::ClientSocketPoolManagerImpl(
38 params
.client_socket_factory
?
39 params
.client_socket_factory
:
40 net::ClientSocketFactory::GetDefaultFactory(),
43 params
.server_bound_cert_service
,
44 params
.transport_security_state
,
45 params
.ssl_session_cache_shard
,
47 params
.ssl_config_service
,
51 } // unnamed namespace
55 HttpNetworkSession::Params::Params()
56 : client_socket_factory(NULL
),
59 server_bound_cert_service(NULL
),
60 transport_security_state(NULL
),
62 ssl_config_service(NULL
),
63 http_auth_handler_factory(NULL
),
64 network_delegate(NULL
),
66 host_mapping_rules(NULL
),
67 force_http_pipelining(false),
68 ignore_certificate_errors(false),
69 http_pipelining_enabled(false),
70 testing_fixed_http_port(0),
71 testing_fixed_https_port(0),
72 force_spdy_single_domain(false),
73 enable_spdy_ip_pooling(true),
74 enable_spdy_credential_frames(false),
75 enable_spdy_compression(true),
76 enable_spdy_ping_based_connection_checking(true),
77 spdy_default_protocol(kProtoUnknown
),
78 spdy_stream_initial_recv_window_size(0),
79 spdy_initial_max_concurrent_streams(0),
80 spdy_max_concurrent_streams_limit(0),
81 time_func(&base::TimeTicks::Now
),
83 enable_quic_https(false),
86 enable_user_alternate_protocol_ports(false),
87 quic_crypto_client_stream_factory(NULL
) {
90 HttpNetworkSession::Params::~Params() {}
92 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
93 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
94 : net_log_(params
.net_log
),
95 network_delegate_(params
.network_delegate
),
96 http_server_properties_(params
.http_server_properties
),
97 cert_verifier_(params
.cert_verifier
),
98 http_auth_handler_factory_(params
.http_auth_handler_factory
),
99 force_http_pipelining_(params
.force_http_pipelining
),
100 proxy_service_(params
.proxy_service
),
101 ssl_config_service_(params
.ssl_config_service
),
102 normal_socket_pool_manager_(
103 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
104 websocket_socket_pool_manager_(
105 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
106 quic_stream_factory_(params
.host_resolver
,
107 params
.client_socket_factory
?
108 params
.client_socket_factory
:
109 net::ClientSocketFactory::GetDefaultFactory(),
110 params
.quic_crypto_client_stream_factory
,
111 params
.quic_random
? params
.quic_random
:
112 QuicRandom::GetInstance(),
113 params
.quic_clock
? params
. quic_clock
:
115 spdy_session_pool_(params
.host_resolver
,
116 params
.ssl_config_service
,
117 params
.http_server_properties
,
118 params
.force_spdy_single_domain
,
119 params
.enable_spdy_ip_pooling
,
120 params
.enable_spdy_credential_frames
,
121 params
.enable_spdy_compression
,
122 params
.enable_spdy_ping_based_connection_checking
,
123 params
.spdy_default_protocol
,
124 params
.spdy_stream_initial_recv_window_size
,
125 params
.spdy_initial_max_concurrent_streams
,
126 params
.spdy_max_concurrent_streams_limit
,
128 params
.trusted_spdy_proxy
),
129 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
130 websocket_stream_factory_(new HttpStreamFactoryImpl(this, true)),
132 DCHECK(proxy_service_
);
133 DCHECK(ssl_config_service_
.get());
134 CHECK(http_server_properties_
);
137 HttpNetworkSession::~HttpNetworkSession() {
138 STLDeleteElements(&response_drainers_
);
139 spdy_session_pool_
.CloseAllSessions();
142 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
143 DCHECK(!ContainsKey(response_drainers_
, drainer
));
144 response_drainers_
.insert(drainer
);
147 void HttpNetworkSession::RemoveResponseDrainer(
148 HttpResponseBodyDrainer
* drainer
) {
149 DCHECK(ContainsKey(response_drainers_
, drainer
));
150 response_drainers_
.erase(drainer
);
153 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
154 SocketPoolType pool_type
) {
155 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
158 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
159 SocketPoolType pool_type
) {
160 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
163 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
164 SocketPoolType pool_type
,
165 const HostPortPair
& socks_proxy
) {
166 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
170 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
171 SocketPoolType pool_type
,
172 const HostPortPair
& http_proxy
) {
173 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
176 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
177 SocketPoolType pool_type
,
178 const HostPortPair
& proxy_server
) {
179 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
183 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
184 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
185 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
188 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
189 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
192 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
193 base::DictionaryValue
* dict
= new base::DictionaryValue();
194 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
195 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
196 dict
->SetBoolean("quic_enabled_https", params_
.enable_quic_https
);
197 dict
->SetString("origin_to_force_quic_on",
198 params_
.origin_to_force_quic_on
.ToString());
202 void HttpNetworkSession::CloseAllConnections() {
203 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
204 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
205 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
206 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
209 void HttpNetworkSession::CloseIdleConnections() {
210 normal_socket_pool_manager_
->CloseIdleSockets();
211 websocket_socket_pool_manager_
->CloseIdleSockets();
212 spdy_session_pool_
.CloseCurrentIdleSessions();
215 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
216 SocketPoolType pool_type
) {
218 case NORMAL_SOCKET_POOL
:
219 return normal_socket_pool_manager_
.get();
220 case WEBSOCKET_SOCKET_POOL
:
221 return websocket_socket_pool_manager_
.get();