blob: 4676fcf5ed39e6e94d689ed8b2d99bbae45cc71f [file] [log] [blame]
[email protected]cf4cae32014-05-27 00:39:101// 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#ifndef NET_HTTP_HTTP_NETWORK_SESSION_H_
6#define NET_HTTP_HTTP_NETWORK_SESSION_H_
7
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9#include <stdint.h>
10
avifceb32f62016-10-07 16:30:5211#include <map>
12#include <memory>
[email protected]cf4cae32014-05-27 00:39:1013#include <set>
14#include <string>
rch74da0e1a2016-01-14 02:49:3215#include <unordered_set>
[email protected]cf4cae32014-05-27 00:39:1016#include <vector>
17
maksim.sisov0adf8592016-07-15 06:25:5618#include "base/bind.h"
hajimehoshi8156e7c2016-09-29 06:17:5219#include "base/memory/memory_coordinator_client.h"
maksim.sisov0adf8592016-07-15 06:25:5620#include "base/memory/memory_pressure_monitor.h"
[email protected]cf4cae32014-05-27 00:39:1021#include "base/memory/ref_counted.h"
22#include "base/memory/weak_ptr.h"
23#include "base/threading/non_thread_safe.h"
24#include "net/base/host_port_pair.h"
25#include "net/base/net_export.h"
26#include "net/dns/host_resolver.h"
27#include "net/http/http_auth_cache.h"
28#include "net/http/http_stream_factory.h"
rch675757b2016-07-29 16:40:1129#include "net/quic/chromium/quic_stream_factory.h"
[email protected]cf4cae32014-05-27 00:39:1030#include "net/socket/next_proto.h"
bnc3171a2432016-12-28 18:40:2631#include "net/spdy/spdy_protocol.h"
[email protected]cf4cae32014-05-27 00:39:1032#include "net/spdy/spdy_session_pool.h"
33#include "net/ssl/ssl_client_auth_cache.h"
34
35namespace base {
36class Value;
xunjieli9f8c5fb52016-12-07 22:59:3337namespace trace_event {
38class ProcessMemoryDump;
39}
[email protected]cf4cae32014-05-27 00:39:1040}
41
42namespace net {
43
estark6f9b3d82016-01-12 21:37:0544class CTPolicyEnforcer;
[email protected]cf4cae32014-05-27 00:39:1045class CertVerifier;
[email protected]6b8a3c742014-07-25 00:25:3546class ChannelIDService;
[email protected]cf4cae32014-05-27 00:39:1047class ClientSocketFactory;
48class ClientSocketPoolManager;
49class CTVerifier;
50class HostResolver;
[email protected]cf4cae32014-05-27 00:39:1051class HttpAuthHandlerFactory;
52class HttpNetworkSessionPeer;
53class HttpProxyClientSocketPool;
54class HttpResponseBodyDrainer;
55class HttpServerProperties;
56class NetLog;
rdsmith1d343be52016-10-21 20:37:5057class NetworkThrottleManager;
bengr39e406102014-09-10 23:04:4658class ProxyDelegate;
[email protected]cf4cae32014-05-27 00:39:1059class ProxyService;
60class QuicClock;
61class QuicCryptoClientStreamFactory;
tbansalba8f4112015-09-03 21:57:1962class SocketPerformanceWatcherFactory;
[email protected]cf4cae32014-05-27 00:39:1063class SOCKSClientSocketPool;
64class SSLClientSocketPool;
65class SSLConfigService;
66class TransportClientSocketPool;
67class TransportSecurityState;
68
bnc3171a2432016-12-28 18:40:2669// Specifies the maximum HPACK dynamic table size the server is allowed to set.
70const uint32_t kSpdyMaxHeaderTableSize = 64 * 1024;
71
72// Specifies the maximum concurrent streams server could send (via push).
73const uint32_t kSpdyMaxConcurrentPushedStreams = 1000;
74
[email protected]cf4cae32014-05-27 00:39:1075// This class holds session objects used by HttpNetworkTransaction objects.
76class NET_EXPORT HttpNetworkSession
hajimehoshi8156e7c2016-09-29 06:17:5277 : NON_EXPORTED_BASE(public base::NonThreadSafe),
78 public base::MemoryCoordinatorClient {
[email protected]cf4cae32014-05-27 00:39:1079 public:
80 struct NET_EXPORT Params {
81 Params();
vmpstracd23b72016-02-26 21:08:5582 Params(const Params& other);
[email protected]cf4cae32014-05-27 00:39:1083 ~Params();
84
85 ClientSocketFactory* client_socket_factory;
86 HostResolver* host_resolver;
87 CertVerifier* cert_verifier;
[email protected]6b8a3c742014-07-25 00:25:3588 ChannelIDService* channel_id_service;
[email protected]cf4cae32014-05-27 00:39:1089 TransportSecurityState* transport_security_state;
90 CTVerifier* cert_transparency_verifier;
rsleevid6de8302016-06-21 01:33:2091 CTPolicyEnforcer* ct_policy_enforcer;
[email protected]cf4cae32014-05-27 00:39:1092 ProxyService* proxy_service;
[email protected]cf4cae32014-05-27 00:39:1093 SSLConfigService* ssl_config_service;
94 HttpAuthHandlerFactory* http_auth_handler_factory;
bnc525e175a2016-06-20 12:36:4095 HttpServerProperties* http_server_properties;
[email protected]cf4cae32014-05-27 00:39:1096 NetLog* net_log;
97 HostMappingRules* host_mapping_rules;
tbansalba8f4112015-09-03 21:57:1998 SocketPerformanceWatcherFactory* socket_performance_watcher_factory;
[email protected]cf4cae32014-05-27 00:39:1099 bool ignore_certificate_errors;
Avi Drissman13fc8932015-12-20 04:40:46100 uint16_t testing_fixed_http_port;
101 uint16_t testing_fixed_https_port;
jrif9b4bec2014-09-15 15:46:54102 bool enable_tcp_fast_open_for_ssl;
[email protected]cf4cae32014-05-27 00:39:10103
rchd502a302015-10-16 03:57:21104 // Use SPDY ping frames to test for connection health after idle.
[email protected]cf4cae32014-05-27 00:39:10105 bool enable_spdy_ping_based_connection_checking;
bnc3f0118e2016-02-02 15:42:22106 bool enable_http2;
bnc8f0f3b62015-04-08 04:37:23107 size_t spdy_session_max_recv_window_size;
bnc3171a2432016-12-28 18:40:26108 // HTTP/2 connection settings.
109 // Unknown settings will still be sent to the server.
110 SettingsMap http2_settings;
rchd502a302015-10-16 03:57:21111 // Source of time for SPDY connections.
[email protected]cf4cae32014-05-27 00:39:10112 SpdySessionPool::TimeFunc time_func;
bnca86815342016-06-27 12:27:48113 // Whether to enable HTTP/2 Alt-Svc entries with hostname different than
114 // that of the origin.
115 bool enable_http2_alternative_service_with_different_host;
116 // Whether to enable QUIC Alt-Svc entries with hostname different than that
117 // of the origin.
118 bool enable_quic_alternative_service_with_different_host;
[email protected]cf4cae32014-05-27 00:39:10119
rchd502a302015-10-16 03:57:21120 // Enables QUIC support.
[email protected]cf4cae32014-05-27 00:39:10121 bool enable_quic;
zhongyi75527dd2016-01-21 22:26:43122 // Disable QUIC if a connection times out with open streams.
123 bool disable_quic_on_timeout_with_open_streams;
rchd502a302015-10-16 03:57:21124 // Disables QUIC's 0-RTT behavior.
jri2b966f22014-09-02 22:25:36125 bool quic_always_require_handshake_confirmation;
rchd502a302015-10-16 03:57:21126 // Disables QUIC connection pooling.
jri584002d12014-09-09 00:51:28127 bool quic_disable_connection_pooling;
rchd502a302015-10-16 03:57:21128 // If not zero, the task to load QUIC server configs from the disk cache
129 // will timeout after this value multiplied by the smoothed RTT for the
130 // server.
rtenneti2912825c2015-01-06 01:19:46131 float quic_load_server_info_timeout_srtt_multiplier;
rchd502a302015-10-16 03:57:21132 // Causes QUIC to race reading the server config from disk with
133 // sending an inchoate CHLO.
rtenneti4f809972015-02-11 19:38:34134 bool quic_enable_connection_racing;
rchd502a302015-10-16 03:57:21135 // Use non-blocking IO for UDP sockets.
qyearsley3257b7de2015-02-28 06:59:03136 bool quic_enable_non_blocking_io;
rchd502a302015-10-16 03:57:21137 // Disables using the disk cache to store QUIC server configs.
rtenneti34dffe752015-02-24 23:27:32138 bool quic_disable_disk_cache;
rchd502a302015-10-16 03:57:21139 // Prefer AES-GCM to ChaCha20 even if no hardware support is present.
rch9976b0c2015-06-10 21:27:23140 bool quic_prefer_aes;
rchd502a302015-10-16 03:57:21141 // Size in bytes of the QUIC DUP socket receive buffer.
rchc7433572015-02-27 18:16:51142 int quic_socket_receive_buffer_size;
rtennetib8e80fb2016-05-16 00:12:09143 // Delay starting a TCP connection when QUIC believes it can speak
144 // 0-RTT to a server.
145 bool quic_delay_tcp_race;
rtenneti6971c172016-01-15 20:12:10146 // Maximum number of server configs that are to be stored in
147 // HttpServerProperties, instead of the disk cache.
148 size_t quic_max_server_configs_stored_in_properties;
rtenneti8a2f4632016-03-21 20:26:57149 // If not empty, QUIC will be used for all connections to the set of
150 // origins in |origins_to_force_quic_on|.
151 std::set<HostPortPair> origins_to_force_quic_on;
rchd502a302015-10-16 03:57:21152 // Source of time for QUIC connections. Will be owned by QuicStreamFactory.
153 QuicClock* quic_clock;
154 // Source of entropy for QUIC connections.
[email protected]cf4cae32014-05-27 00:39:10155 QuicRandom* quic_random;
rchd502a302015-10-16 03:57:21156 // Limit on the size of QUIC packets.
[email protected]cf4cae32014-05-27 00:39:10157 size_t quic_max_packet_length;
rchd502a302015-10-16 03:57:21158 // User agent description to send in the QUIC handshake.
[email protected]0c4017ca2014-06-06 03:30:45159 std::string quic_user_agent_id;
[email protected]cf4cae32014-05-27 00:39:10160 bool enable_user_alternate_protocol_ports;
rchd502a302015-10-16 03:57:21161 // Optional factory to use for creating QuicCryptoClientStreams.
[email protected]cf4cae32014-05-27 00:39:10162 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
rchd502a302015-10-16 03:57:21163 // Versions of QUIC which may be used.
[email protected]cf4cae32014-05-27 00:39:10164 QuicVersionVector quic_supported_versions;
jri8c44d692015-10-23 23:53:41165 // Set of QUIC tags to send in the handshake's connection options.
[email protected]488a0e252014-06-25 04:37:44166 QuicTagVector quic_connection_options;
jri8c44d692015-10-23 23:53:41167 // If true, all QUIC sessions are closed when any local IP address changes.
168 bool quic_close_sessions_on_ip_change;
zhongyi6ba0f4252016-08-23 05:20:04169 // Specifies QUIC idle connection state lifetime.
rtenneti41c09992015-11-30 18:24:01170 int quic_idle_connection_timeout_seconds;
zhongyidd1439f62016-09-02 02:02:26171 // Specifies the reduced ping timeout subsequent connections should use when
172 // a connection was timed out with open streams.
173 int quic_reduced_ping_timeout_seconds;
zhongyi6ba0f4252016-08-23 05:20:04174 // Specifies the maximum time duration that QUIC packet reader can perform
175 // consecutive packets reading.
176 int quic_packet_reader_yield_after_duration_milliseconds;
rtennetid2e74caa2015-12-09 00:51:57177 // If true, disable preconnections if QUIC can do 0RTT.
178 bool quic_disable_preconnect_if_0rtt;
rch74da0e1a2016-01-14 02:49:32179 // List of hosts for which QUIC is explicitly whitelisted.
180 std::unordered_set<std::string> quic_host_whitelist;
jrid36ada62016-02-06 02:42:08181 // If true, active QUIC sessions may be migrated onto a new network when
182 // the platform indicates that the default network is changing.
jri7e636642016-01-14 06:57:08183 bool quic_migrate_sessions_on_network_change;
jrid36ada62016-02-06 02:42:08184 // If true, active QUIC sessions experiencing poor connectivity may be
185 // migrated onto a new network.
186 bool quic_migrate_sessions_early;
jri217455a12016-07-13 20:15:09187 // If true, allows migration of QUIC connections to a server-specified
188 // alternate server address.
189 bool quic_allow_server_migration;
xunjieli888c29922016-03-18 21:05:09190 // If true, bidirectional streams over QUIC will be disabled.
191 bool quic_disable_bidirectional_streams;
ckrasicda193a82016-07-09 00:39:36192 // If true, enable force HOL blocking. For measurement purposes.
193 bool quic_force_hol_blocking;
rtennetid073dd22016-08-04 01:58:33194 // If true, race cert verification with host resolution.
195 bool quic_race_cert_verification;
rchbedd57452016-08-30 19:11:48196 // If true, configure QUIC sockets to not fragment packets.
197 bool quic_do_not_fragment;
xunjieli888c29922016-03-18 21:05:09198
bengr39e406102014-09-10 23:04:46199 ProxyDelegate* proxy_delegate;
nharperb7441ef2016-01-25 23:54:14200 // Enable support for Token Binding.
201 bool enable_token_binding;
mmenkea7da6da2016-09-01 21:56:52202
203 // Enable HTTP/0.9 for HTTP/HTTPS on ports other than the default one for
204 // each protocol.
205 bool http_09_on_non_default_ports_enabled;
[email protected]cf4cae32014-05-27 00:39:10206 };
207
208 enum SocketPoolType {
209 NORMAL_SOCKET_POOL,
210 WEBSOCKET_SOCKET_POOL,
211 NUM_SOCKET_POOL_TYPES
212 };
213
214 explicit HttpNetworkSession(const Params& params);
hajimehoshi8156e7c2016-09-29 06:17:52215 ~HttpNetworkSession() override;
[email protected]cf4cae32014-05-27 00:39:10216
217 HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
218 SSLClientAuthCache* ssl_client_auth_cache() {
219 return &ssl_client_auth_cache_;
220 }
221
avifceb32f62016-10-07 16:30:52222 void AddResponseDrainer(std::unique_ptr<HttpResponseBodyDrainer> drainer);
[email protected]cf4cae32014-05-27 00:39:10223
avifceb32f62016-10-07 16:30:52224 // Removes the drainer from the session. Does not dispose of it.
[email protected]cf4cae32014-05-27 00:39:10225 void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
226
227 TransportClientSocketPool* GetTransportSocketPool(SocketPoolType pool_type);
228 SSLClientSocketPool* GetSSLSocketPool(SocketPoolType pool_type);
229 SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
230 SocketPoolType pool_type,
231 const HostPortPair& socks_proxy);
232 HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
233 SocketPoolType pool_type,
234 const HostPortPair& http_proxy);
235 SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
236 SocketPoolType pool_type,
237 const HostPortPair& proxy_server);
238
239 CertVerifier* cert_verifier() { return cert_verifier_; }
240 ProxyService* proxy_service() { return proxy_service_; }
241 SSLConfigService* ssl_config_service() { return ssl_config_service_.get(); }
242 SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
243 QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
244 HttpAuthHandlerFactory* http_auth_handler_factory() {
245 return http_auth_handler_factory_;
246 }
bnc525e175a2016-06-20 12:36:40247 HttpServerProperties* http_server_properties() {
[email protected]cf4cae32014-05-27 00:39:10248 return http_server_properties_;
249 }
250 HttpStreamFactory* http_stream_factory() {
251 return http_stream_factory_.get();
252 }
253 HttpStreamFactory* http_stream_factory_for_websocket() {
254 return http_stream_factory_for_websocket_.get();
255 }
rdsmith1d343be52016-10-21 20:37:50256 NetworkThrottleManager* throttler() {
257 return network_stream_throttler_.get();
258 }
[email protected]cf4cae32014-05-27 00:39:10259 NetLog* net_log() {
260 return net_log_;
261 }
[email protected]cf4cae32014-05-27 00:39:10262
payal.pandey62a400292015-05-28 09:29:54263 // Creates a Value summary of the state of the socket pools.
danakj1fd259a02016-04-16 03:17:09264 std::unique_ptr<base::Value> SocketPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10265
payal.pandey91cb2312015-05-27 07:41:51266 // Creates a Value summary of the state of the SPDY sessions.
danakj1fd259a02016-04-16 03:17:09267 std::unique_ptr<base::Value> SpdySessionPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10268
269 // Creates a Value summary of the state of the QUIC sessions and
payal.pandeya18956a2015-05-27 05:57:55270 // configuration.
danakj1fd259a02016-04-16 03:17:09271 std::unique_ptr<base::Value> QuicInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10272
273 void CloseAllConnections();
274 void CloseIdleConnections();
275
276 // Returns the original Params used to construct this session.
277 const Params& params() const { return params_; }
278
bnc3472afd2016-11-17 15:27:21279 bool IsProtocolEnabled(NextProto protocol) const;
[email protected]cf4cae32014-05-27 00:39:10280
zhongyiaf257542016-12-19 03:36:01281 void SetServerPushDelegate(std::unique_ptr<ServerPushDelegate> push_delegate);
282
bnc1f295372015-10-21 23:24:22283 // Populates |*alpn_protos| with protocols to be used with ALPN.
284 void GetAlpnProtos(NextProtoVector* alpn_protos) const;
285
nharper8cdb0fb2016-04-22 21:34:59286 // Populates |server_config| and |proxy_config| based on this session and
287 // |request|.
288 void GetSSLConfig(const HttpRequestInfo& request,
289 SSLConfig* server_config,
290 SSLConfig* proxy_config) const;
291
xunjieli9f8c5fb52016-12-07 22:59:33292 // Dumps memory allocation stats. |parent_dump_absolute_name| is the name
293 // used by the parent MemoryAllocatorDump in the memory dump hierarchy.
294 void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
295 const std::string& parent_absolute_name) const;
296
[email protected]cf4cae32014-05-27 00:39:10297 private:
[email protected]cf4cae32014-05-27 00:39:10298 friend class HttpNetworkSessionPeer;
299
[email protected]cf4cae32014-05-27 00:39:10300 ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
301
maksim.sisov0adf8592016-07-15 06:25:56302 // Flush sockets on low memory notifications callback.
303 void OnMemoryPressure(
304 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
305
hajimehoshi8156e7c2016-09-29 06:17:52306 // base::MemoryCoordinatorClient implementation:
307 void OnMemoryStateChange(base::MemoryState state) override;
308
[email protected]cf4cae32014-05-27 00:39:10309 NetLog* const net_log_;
bnc525e175a2016-06-20 12:36:40310 HttpServerProperties* const http_server_properties_;
[email protected]cf4cae32014-05-27 00:39:10311 CertVerifier* const cert_verifier_;
312 HttpAuthHandlerFactory* const http_auth_handler_factory_;
313
314 // Not const since it's modified by HttpNetworkSessionPeer for testing.
315 ProxyService* proxy_service_;
316 const scoped_refptr<SSLConfigService> ssl_config_service_;
317
318 HttpAuthCache http_auth_cache_;
319 SSLClientAuthCache ssl_client_auth_cache_;
danakj1fd259a02016-04-16 03:17:09320 std::unique_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
321 std::unique_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
zhongyiaf257542016-12-19 03:36:01322 std::unique_ptr<ServerPushDelegate> push_delegate_;
[email protected]cf4cae32014-05-27 00:39:10323 QuicStreamFactory quic_stream_factory_;
324 SpdySessionPool spdy_session_pool_;
danakj1fd259a02016-04-16 03:17:09325 std::unique_ptr<HttpStreamFactory> http_stream_factory_;
326 std::unique_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
avifceb32f62016-10-07 16:30:52327 std::map<HttpResponseBodyDrainer*, std::unique_ptr<HttpResponseBodyDrainer>>
328 response_drainers_;
rdsmith1d343be52016-10-21 20:37:50329 std::unique_ptr<NetworkThrottleManager> network_stream_throttler_;
[email protected]cf4cae32014-05-27 00:39:10330
bnc0d23cf42014-12-11 14:09:46331 NextProtoVector next_protos_;
[email protected]cf4cae32014-05-27 00:39:10332
333 Params params_;
maksim.sisov0adf8592016-07-15 06:25:56334
335 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
[email protected]cf4cae32014-05-27 00:39:10336};
337
338} // namespace net
339
340#endif // NET_HTTP_HTTP_NETWORK_SESSION_H_