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
.cert_transparency_verifier
,
46 params
.ssl_session_cache_shard
,
48 params
.ssl_config_service
,
52 } // unnamed namespace
56 HttpNetworkSession::Params::Params()
57 : client_socket_factory(NULL
),
60 server_bound_cert_service(NULL
),
61 transport_security_state(NULL
),
62 cert_transparency_verifier(NULL
),
64 ssl_config_service(NULL
),
65 http_auth_handler_factory(NULL
),
66 network_delegate(NULL
),
68 host_mapping_rules(NULL
),
69 force_http_pipelining(false),
70 ignore_certificate_errors(false),
71 http_pipelining_enabled(false),
72 testing_fixed_http_port(0),
73 testing_fixed_https_port(0),
74 force_spdy_single_domain(false),
75 enable_spdy_ip_pooling(true),
76 enable_spdy_compression(true),
77 enable_spdy_ping_based_connection_checking(true),
78 spdy_default_protocol(kProtoUnknown
),
79 spdy_stream_initial_recv_window_size(0),
80 spdy_initial_max_concurrent_streams(0),
81 spdy_max_concurrent_streams_limit(0),
82 time_func(&base::TimeTicks::Now
),
84 enable_quic_https(false),
87 quic_max_packet_length(kDefaultMaxPacketSize
),
88 enable_user_alternate_protocol_ports(false),
89 quic_crypto_client_stream_factory(NULL
) {
92 HttpNetworkSession::Params::~Params() {}
94 // TODO(mbelshe): Move the socket factories into HttpStreamFactory.
95 HttpNetworkSession::HttpNetworkSession(const Params
& params
)
96 : net_log_(params
.net_log
),
97 network_delegate_(params
.network_delegate
),
98 http_server_properties_(params
.http_server_properties
),
99 cert_verifier_(params
.cert_verifier
),
100 http_auth_handler_factory_(params
.http_auth_handler_factory
),
101 force_http_pipelining_(params
.force_http_pipelining
),
102 proxy_service_(params
.proxy_service
),
103 ssl_config_service_(params
.ssl_config_service
),
104 normal_socket_pool_manager_(
105 CreateSocketPoolManager(NORMAL_SOCKET_POOL
, params
)),
106 websocket_socket_pool_manager_(
107 CreateSocketPoolManager(WEBSOCKET_SOCKET_POOL
, params
)),
108 quic_stream_factory_(params
.host_resolver
,
109 params
.client_socket_factory
?
110 params
.client_socket_factory
:
111 net::ClientSocketFactory::GetDefaultFactory(),
112 params
.http_server_properties
,
113 params
.quic_crypto_client_stream_factory
,
114 params
.quic_random
? params
.quic_random
:
115 QuicRandom::GetInstance(),
116 params
.quic_clock
? params
. quic_clock
:
118 params
.quic_max_packet_length
),
119 spdy_session_pool_(params
.host_resolver
,
120 params
.ssl_config_service
,
121 params
.http_server_properties
,
122 params
.force_spdy_single_domain
,
123 params
.enable_spdy_ip_pooling
,
124 params
.enable_spdy_compression
,
125 params
.enable_spdy_ping_based_connection_checking
,
126 params
.spdy_default_protocol
,
127 params
.spdy_stream_initial_recv_window_size
,
128 params
.spdy_initial_max_concurrent_streams
,
129 params
.spdy_max_concurrent_streams_limit
,
131 params
.trusted_spdy_proxy
),
132 http_stream_factory_(new HttpStreamFactoryImpl(this, false)),
133 http_stream_factory_for_websocket_(
134 new HttpStreamFactoryImpl(this, true)),
136 DCHECK(proxy_service_
);
137 DCHECK(ssl_config_service_
.get());
138 CHECK(http_server_properties_
);
141 HttpNetworkSession::~HttpNetworkSession() {
142 STLDeleteElements(&response_drainers_
);
143 spdy_session_pool_
.CloseAllSessions();
146 void HttpNetworkSession::AddResponseDrainer(HttpResponseBodyDrainer
* drainer
) {
147 DCHECK(!ContainsKey(response_drainers_
, drainer
));
148 response_drainers_
.insert(drainer
);
151 void HttpNetworkSession::RemoveResponseDrainer(
152 HttpResponseBodyDrainer
* drainer
) {
153 DCHECK(ContainsKey(response_drainers_
, drainer
));
154 response_drainers_
.erase(drainer
);
157 TransportClientSocketPool
* HttpNetworkSession::GetTransportSocketPool(
158 SocketPoolType pool_type
) {
159 return GetSocketPoolManager(pool_type
)->GetTransportSocketPool();
162 SSLClientSocketPool
* HttpNetworkSession::GetSSLSocketPool(
163 SocketPoolType pool_type
) {
164 return GetSocketPoolManager(pool_type
)->GetSSLSocketPool();
167 SOCKSClientSocketPool
* HttpNetworkSession::GetSocketPoolForSOCKSProxy(
168 SocketPoolType pool_type
,
169 const HostPortPair
& socks_proxy
) {
170 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSOCKSProxy(
174 HttpProxyClientSocketPool
* HttpNetworkSession::GetSocketPoolForHTTPProxy(
175 SocketPoolType pool_type
,
176 const HostPortPair
& http_proxy
) {
177 return GetSocketPoolManager(pool_type
)->GetSocketPoolForHTTPProxy(http_proxy
);
180 SSLClientSocketPool
* HttpNetworkSession::GetSocketPoolForSSLWithProxy(
181 SocketPoolType pool_type
,
182 const HostPortPair
& proxy_server
) {
183 return GetSocketPoolManager(pool_type
)->GetSocketPoolForSSLWithProxy(
187 base::Value
* HttpNetworkSession::SocketPoolInfoToValue() const {
188 // TODO(yutak): Should merge values from normal pools and WebSocket pools.
189 return normal_socket_pool_manager_
->SocketPoolInfoToValue();
192 base::Value
* HttpNetworkSession::SpdySessionPoolInfoToValue() const {
193 return spdy_session_pool_
.SpdySessionPoolInfoToValue();
196 base::Value
* HttpNetworkSession::QuicInfoToValue() const {
197 base::DictionaryValue
* dict
= new base::DictionaryValue();
198 dict
->Set("sessions", quic_stream_factory_
.QuicStreamFactoryInfoToValue());
199 dict
->SetBoolean("quic_enabled", params_
.enable_quic
);
200 dict
->SetBoolean("quic_enabled_https", params_
.enable_quic_https
);
201 dict
->SetString("origin_to_force_quic_on",
202 params_
.origin_to_force_quic_on
.ToString());
206 void HttpNetworkSession::CloseAllConnections() {
207 normal_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
208 websocket_socket_pool_manager_
->FlushSocketPoolsWithError(ERR_ABORTED
);
209 spdy_session_pool_
.CloseCurrentSessions(ERR_ABORTED
);
210 quic_stream_factory_
.CloseAllSessions(ERR_ABORTED
);
213 void HttpNetworkSession::CloseIdleConnections() {
214 normal_socket_pool_manager_
->CloseIdleSockets();
215 websocket_socket_pool_manager_
->CloseIdleSockets();
216 spdy_session_pool_
.CloseCurrentIdleSessions();
219 ClientSocketPoolManager
* HttpNetworkSession::GetSocketPoolManager(
220 SocketPoolType pool_type
) {
222 case NORMAL_SOCKET_POOL
:
223 return normal_socket_pool_manager_
.get();
224 case WEBSOCKET_SOCKET_POOL
:
225 return websocket_socket_pool_manager_
.get();