blob: 2cb0f5f5a624f65ae83c1f0ad785df776248a3b4 [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"
Yixin Wang10f477ed2017-11-21 04:20:2019#include "base/containers/flat_set.h"
hajimehoshi8156e7c2016-09-29 06:17:5220#include "base/memory/memory_coordinator_client.h"
maksim.sisov0adf8592016-07-15 06:25:5621#include "base/memory/memory_pressure_monitor.h"
[email protected]cf4cae32014-05-27 00:39:1022#include "base/memory/ref_counted.h"
23#include "base/memory/weak_ptr.h"
gab47aa7da2017-06-02 16:09:4324#include "base/threading/thread_checker.h"
mmenke0d700dd82017-05-26 20:36:1125#include "net/base/host_mapping_rules.h"
[email protected]cf4cae32014-05-27 00:39:1026#include "net/base/host_port_pair.h"
27#include "net/base/net_export.h"
28#include "net/dns/host_resolver.h"
29#include "net/http/http_auth_cache.h"
30#include "net/http/http_stream_factory.h"
rch675757b2016-07-29 16:40:1131#include "net/quic/chromium/quic_stream_factory.h"
[email protected]cf4cae32014-05-27 00:39:1032#include "net/socket/next_proto.h"
bnc8f8f7d302017-04-24 18:08:0633#include "net/spdy/chromium/spdy_session_pool.h"
34#include "net/spdy/core/spdy_protocol.h"
[email protected]cf4cae32014-05-27 00:39:1035#include "net/ssl/ssl_client_auth_cache.h"
36
37namespace base {
38class Value;
xunjieli9f8c5fb52016-12-07 22:59:3339namespace trace_event {
40class ProcessMemoryDump;
41}
[email protected]cf4cae32014-05-27 00:39:1042}
43
44namespace net {
45
estark6f9b3d82016-01-12 21:37:0546class CTPolicyEnforcer;
[email protected]cf4cae32014-05-27 00:39:1047class CertVerifier;
[email protected]6b8a3c742014-07-25 00:25:3548class ChannelIDService;
[email protected]cf4cae32014-05-27 00:39:1049class ClientSocketFactory;
50class ClientSocketPoolManager;
51class CTVerifier;
52class HostResolver;
[email protected]cf4cae32014-05-27 00:39:1053class HttpAuthHandlerFactory;
54class HttpNetworkSessionPeer;
55class HttpProxyClientSocketPool;
56class HttpResponseBodyDrainer;
57class HttpServerProperties;
58class NetLog;
tbansal16196a1e2017-06-09 01:50:0959class NetworkQualityProvider;
rdsmith1d343be52016-10-21 20:37:5060class NetworkThrottleManager;
bengr39e406102014-09-10 23:04:4661class ProxyDelegate;
[email protected]cf4cae32014-05-27 00:39:1062class ProxyService;
63class QuicClock;
64class QuicCryptoClientStreamFactory;
tbansalba8f4112015-09-03 21:57:1965class SocketPerformanceWatcherFactory;
[email protected]cf4cae32014-05-27 00:39:1066class SOCKSClientSocketPool;
67class SSLClientSocketPool;
68class SSLConfigService;
69class TransportClientSocketPool;
70class TransportSecurityState;
71
bnc3171a2432016-12-28 18:40:2672// Specifies the maximum HPACK dynamic table size the server is allowed to set.
73const uint32_t kSpdyMaxHeaderTableSize = 64 * 1024;
74
75// Specifies the maximum concurrent streams server could send (via push).
76const uint32_t kSpdyMaxConcurrentPushedStreams = 1000;
77
[email protected]cf4cae32014-05-27 00:39:1078// This class holds session objects used by HttpNetworkTransaction objects.
gab47aa7da2017-06-02 16:09:4379class NET_EXPORT HttpNetworkSession : public base::MemoryCoordinatorClient {
[email protected]cf4cae32014-05-27 00:39:1080 public:
mmenke6ddfbea2017-05-31 21:48:4181 // Self-contained structure with all the simple configuration options
82 // supported by the HttpNetworkSession.
[email protected]cf4cae32014-05-27 00:39:1083 struct NET_EXPORT Params {
84 Params();
vmpstracd23b72016-02-26 21:08:5585 Params(const Params& other);
[email protected]cf4cae32014-05-27 00:39:1086 ~Params();
87
Matt Menke53c59762017-09-14 16:38:1288 enum class TcpFastOpenMode {
89 DISABLED,
90 // If true, TCP fast open will be used for all HTTPS connections.
91 ENABLED_FOR_SSL_ONLY,
92 // TCP fast open will be used for all HTTP/HTTPS connections.
93 // TODO(mmenke): With 0-RTT session resumption, does this option make
94 // sense?
95 ENABLED_FOR_ALL,
96 };
97
zhongyid7dd2db12017-04-14 17:01:2598 bool enable_server_push_cancellation;
mmenke0d700dd82017-05-26 20:36:1199 HostMappingRules host_mapping_rules;
[email protected]cf4cae32014-05-27 00:39:10100 bool ignore_certificate_errors;
Avi Drissman13fc8932015-12-20 04:40:46101 uint16_t testing_fixed_http_port;
102 uint16_t testing_fixed_https_port;
Matt Menke53c59762017-09-14 16:38:12103 TcpFastOpenMode tcp_fast_open_mode;
rch1546ccd2017-04-20 02:14:23104 bool enable_user_alternate_protocol_ports;
[email protected]cf4cae32014-05-27 00:39:10105
rchd502a302015-10-16 03:57:21106 // Use SPDY ping frames to test for connection health after idle.
[email protected]cf4cae32014-05-27 00:39:10107 bool enable_spdy_ping_based_connection_checking;
bnc3f0118e2016-02-02 15:42:22108 bool enable_http2;
bnc8f0f3b62015-04-08 04:37:23109 size_t spdy_session_max_recv_window_size;
bnc3171a2432016-12-28 18:40:26110 // HTTP/2 connection settings.
111 // Unknown settings will still be sent to the server.
112 SettingsMap http2_settings;
rchd502a302015-10-16 03:57:21113 // Source of time for SPDY connections.
[email protected]cf4cae32014-05-27 00:39:10114 SpdySessionPool::TimeFunc time_func;
bnca86731e2017-04-17 12:31:28115 // Whether to enable HTTP/2 Alt-Svc entries.
116 bool enable_http2_alternative_service;
[email protected]cf4cae32014-05-27 00:39:10117
rchd502a302015-10-16 03:57:21118 // Enables QUIC support.
[email protected]cf4cae32014-05-27 00:39:10119 bool enable_quic;
rch1546ccd2017-04-20 02:14:23120
121 // QUIC runtime configuration options.
122
123 // Versions of QUIC which may be used.
Michael Warres74ee3ce2017-10-09 15:26:37124 QuicTransportVersionVector quic_supported_versions;
rch1546ccd2017-04-20 02:14:23125 // User agent description to send in the QUIC handshake.
126 std::string quic_user_agent_id;
127 // Limit on the size of QUIC packets.
128 size_t quic_max_packet_length;
rch1546ccd2017-04-20 02:14:23129 // Maximum number of server configs that are to be stored in
130 // HttpServerProperties, instead of the disk cache.
131 size_t quic_max_server_configs_stored_in_properties;
132 // QUIC will be used for all connections in this set.
133 std::set<HostPortPair> origins_to_force_quic_on;
134 // Set of QUIC tags to send in the handshake's connection options.
135 QuicTagVector quic_connection_options;
Yixin Wang46a425f2017-08-10 23:02:20136 // Set of QUIC tags to send in the handshake's connection options that only
137 // affect the client.
138 QuicTagVector quic_client_connection_options;
rch1546ccd2017-04-20 02:14:23139
140 // Active QUIC experiments
141
rch9ecde09b2017-04-08 00:18:23142 // Marks a QUIC server broken when a connection blackholes after the
143 // handshake is confirmed.
144 bool mark_quic_broken_when_network_blackholes;
rch2f2991c2017-04-13 19:28:17145 // Retry requests which fail with QUIC_PROTOCOL_ERROR, and mark QUIC
146 // broken if the retry succeeds.
147 bool retry_without_alt_svc_on_quic_errors;
Yixin Wanga9afead2017-10-19 20:23:10148 // If true, alt-svc headers advertising QUIC in IETF format will be
149 // supported.
150 bool support_ietf_format_quic_altsvc;
zhongyi6ba0f4252016-08-23 05:20:04151 // Specifies QUIC idle connection state lifetime.
rtenneti41c09992015-11-30 18:24:01152 int quic_idle_connection_timeout_seconds;
zhongyidd1439f62016-09-02 02:02:26153 // Specifies the reduced ping timeout subsequent connections should use when
154 // a connection was timed out with open streams.
155 int quic_reduced_ping_timeout_seconds;
Yixin Wang469da562017-11-15 21:34:58156 // Maximum time the session can be alive before crypto handshake is
157 // finished.
158 int quic_max_time_before_crypto_handshake_seconds;
159 // Maximum idle time before the crypto handshake has completed.
160 int quic_max_idle_time_before_crypto_handshake_seconds;
Zhongyi Shi1a605d22017-09-29 20:09:48161 // If true, QUIC will attempt to explicitly use default network for sockets.
162 bool quic_connect_using_default_network;
jrid36ada62016-02-06 02:42:08163 // If true, active QUIC sessions may be migrated onto a new network when
164 // the platform indicates that the default network is changing.
jri7e636642016-01-14 06:57:08165 bool quic_migrate_sessions_on_network_change;
Zhongyi Shi64795622017-11-20 02:21:49166 // If true, connection migration v2 will be used to migrate existing
167 // sessions to network when the platform indicates that the default network
168 // is changing.
169 bool quic_migrate_sessions_on_network_change_v2;
jrid36ada62016-02-06 02:42:08170 // If true, active QUIC sessions experiencing poor connectivity may be
171 // migrated onto a new network.
172 bool quic_migrate_sessions_early;
jri217455a12016-07-13 20:15:09173 // If true, allows migration of QUIC connections to a server-specified
174 // alternate server address.
175 bool quic_allow_server_migration;
Ryan Hamiltonc84473f2017-11-23 03:18:34176 // If true, allows QUIC to use alternative services with a different
177 // hostname from the origin.
178 bool quic_allow_remote_alt_svc;
xunjieli888c29922016-03-18 21:05:09179 // If true, bidirectional streams over QUIC will be disabled.
180 bool quic_disable_bidirectional_streams;
ckrasicda193a82016-07-09 00:39:36181 // If true, enable force HOL blocking. For measurement purposes.
182 bool quic_force_hol_blocking;
rtennetid073dd22016-08-04 01:58:33183 // If true, race cert verification with host resolution.
184 bool quic_race_cert_verification;
rchd6163f32017-01-30 23:50:38185 // If true, estimate the initial RTT for QUIC connections based on network.
186 bool quic_estimate_initial_rtt;
Yixin Wang10f477ed2017-11-21 04:20:20187 // If non-empty, QUIC will only be spoken to hosts in this list.
188 base::flat_set<std::string> quic_host_whitelist;
xunjieli888c29922016-03-18 21:05:09189
nharperb7441ef2016-01-25 23:54:14190 // Enable support for Token Binding.
191 bool enable_token_binding;
mmenkea7da6da2016-09-01 21:56:52192
193 // Enable HTTP/0.9 for HTTP/HTTPS on ports other than the default one for
194 // each protocol.
195 bool http_09_on_non_default_ports_enabled;
[email protected]cf4cae32014-05-27 00:39:10196 };
197
mmenke6ddfbea2017-05-31 21:48:41198 // Structure with pointers to the dependencies of the HttpNetworkSession.
199 // These objects must all outlive the HttpNetworkSession.
200 struct NET_EXPORT Context {
201 Context();
202 Context(const Context& other);
203 ~Context();
204
205 ClientSocketFactory* client_socket_factory;
206 HostResolver* host_resolver;
207 CertVerifier* cert_verifier;
208 ChannelIDService* channel_id_service;
209 TransportSecurityState* transport_security_state;
210 CTVerifier* cert_transparency_verifier;
211 CTPolicyEnforcer* ct_policy_enforcer;
212 ProxyService* proxy_service;
213 SSLConfigService* ssl_config_service;
214 HttpAuthHandlerFactory* http_auth_handler_factory;
215 HttpServerProperties* http_server_properties;
216 NetLog* net_log;
217 SocketPerformanceWatcherFactory* socket_performance_watcher_factory;
tbansal16196a1e2017-06-09 01:50:09218 NetworkQualityProvider* network_quality_provider;
mmenke6ddfbea2017-05-31 21:48:41219
220 // Source of time for QUIC connections.
221 QuicClock* quic_clock;
222 // Source of entropy for QUIC connections.
223 QuicRandom* quic_random;
224 // Optional factory to use for creating QuicCryptoClientStreams.
225 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
226
227 ProxyDelegate* proxy_delegate;
228 };
229
[email protected]cf4cae32014-05-27 00:39:10230 enum SocketPoolType {
231 NORMAL_SOCKET_POOL,
232 WEBSOCKET_SOCKET_POOL,
233 NUM_SOCKET_POOL_TYPES
234 };
235
mmenke6ddfbea2017-05-31 21:48:41236 HttpNetworkSession(const Params& params, const Context& context);
hajimehoshi8156e7c2016-09-29 06:17:52237 ~HttpNetworkSession() override;
[email protected]cf4cae32014-05-27 00:39:10238
239 HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
240 SSLClientAuthCache* ssl_client_auth_cache() {
241 return &ssl_client_auth_cache_;
242 }
243
avifceb32f62016-10-07 16:30:52244 void AddResponseDrainer(std::unique_ptr<HttpResponseBodyDrainer> drainer);
[email protected]cf4cae32014-05-27 00:39:10245
avifceb32f62016-10-07 16:30:52246 // Removes the drainer from the session. Does not dispose of it.
[email protected]cf4cae32014-05-27 00:39:10247 void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
248
249 TransportClientSocketPool* GetTransportSocketPool(SocketPoolType pool_type);
250 SSLClientSocketPool* GetSSLSocketPool(SocketPoolType pool_type);
251 SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
252 SocketPoolType pool_type,
253 const HostPortPair& socks_proxy);
254 HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
255 SocketPoolType pool_type,
256 const HostPortPair& http_proxy);
257 SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
258 SocketPoolType pool_type,
259 const HostPortPair& proxy_server);
260
261 CertVerifier* cert_verifier() { return cert_verifier_; }
262 ProxyService* proxy_service() { return proxy_service_; }
263 SSLConfigService* ssl_config_service() { return ssl_config_service_.get(); }
264 SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
265 QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
266 HttpAuthHandlerFactory* http_auth_handler_factory() {
267 return http_auth_handler_factory_;
268 }
bnc525e175a2016-06-20 12:36:40269 HttpServerProperties* http_server_properties() {
[email protected]cf4cae32014-05-27 00:39:10270 return http_server_properties_;
271 }
272 HttpStreamFactory* http_stream_factory() {
273 return http_stream_factory_.get();
274 }
275 HttpStreamFactory* http_stream_factory_for_websocket() {
276 return http_stream_factory_for_websocket_.get();
277 }
rdsmith1d343be52016-10-21 20:37:50278 NetworkThrottleManager* throttler() {
279 return network_stream_throttler_.get();
280 }
[email protected]cf4cae32014-05-27 00:39:10281 NetLog* net_log() {
282 return net_log_;
283 }
[email protected]cf4cae32014-05-27 00:39:10284
payal.pandey62a400292015-05-28 09:29:54285 // Creates a Value summary of the state of the socket pools.
danakj1fd259a02016-04-16 03:17:09286 std::unique_ptr<base::Value> SocketPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10287
payal.pandey91cb2312015-05-27 07:41:51288 // Creates a Value summary of the state of the SPDY sessions.
danakj1fd259a02016-04-16 03:17:09289 std::unique_ptr<base::Value> SpdySessionPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10290
291 // Creates a Value summary of the state of the QUIC sessions and
payal.pandeya18956a2015-05-27 05:57:55292 // configuration.
danakj1fd259a02016-04-16 03:17:09293 std::unique_ptr<base::Value> QuicInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10294
295 void CloseAllConnections();
296 void CloseIdleConnections();
297
298 // Returns the original Params used to construct this session.
299 const Params& params() const { return params_; }
mmenke6ddfbea2017-05-31 21:48:41300 // Returns the original Context used to construct this session.
301 const Context& context() const { return context_; }
[email protected]cf4cae32014-05-27 00:39:10302
bnc3472afd2016-11-17 15:27:21303 bool IsProtocolEnabled(NextProto protocol) const;
[email protected]cf4cae32014-05-27 00:39:10304
zhongyiaf257542016-12-19 03:36:01305 void SetServerPushDelegate(std::unique_ptr<ServerPushDelegate> push_delegate);
306
bnc1f295372015-10-21 23:24:22307 // Populates |*alpn_protos| with protocols to be used with ALPN.
308 void GetAlpnProtos(NextProtoVector* alpn_protos) const;
309
nharper8cdb0fb2016-04-22 21:34:59310 // Populates |server_config| and |proxy_config| based on this session and
311 // |request|.
312 void GetSSLConfig(const HttpRequestInfo& request,
313 SSLConfig* server_config,
314 SSLConfig* proxy_config) const;
315
xunjieli9f8c5fb52016-12-07 22:59:33316 // Dumps memory allocation stats. |parent_dump_absolute_name| is the name
317 // used by the parent MemoryAllocatorDump in the memory dump hierarchy.
318 void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
319 const std::string& parent_absolute_name) const;
320
pmarko6ab8be242017-01-11 11:02:55321 // Evaluates if QUIC is enabled for new streams.
322 bool IsQuicEnabled() const;
323
324 // Disable QUIC for new streams.
325 void DisableQuic();
326
[email protected]cf4cae32014-05-27 00:39:10327 private:
[email protected]cf4cae32014-05-27 00:39:10328 friend class HttpNetworkSessionPeer;
329
[email protected]cf4cae32014-05-27 00:39:10330 ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
331
maksim.sisov0adf8592016-07-15 06:25:56332 // Flush sockets on low memory notifications callback.
333 void OnMemoryPressure(
334 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
335
hajimehoshi8156e7c2016-09-29 06:17:52336 // base::MemoryCoordinatorClient implementation:
bashi56b23f302017-02-09 01:24:57337 void OnPurgeMemory() override;
hajimehoshi8156e7c2016-09-29 06:17:52338
[email protected]cf4cae32014-05-27 00:39:10339 NetLog* const net_log_;
bnc525e175a2016-06-20 12:36:40340 HttpServerProperties* const http_server_properties_;
[email protected]cf4cae32014-05-27 00:39:10341 CertVerifier* const cert_verifier_;
342 HttpAuthHandlerFactory* const http_auth_handler_factory_;
343
344 // Not const since it's modified by HttpNetworkSessionPeer for testing.
345 ProxyService* proxy_service_;
346 const scoped_refptr<SSLConfigService> ssl_config_service_;
347
348 HttpAuthCache http_auth_cache_;
349 SSLClientAuthCache ssl_client_auth_cache_;
danakj1fd259a02016-04-16 03:17:09350 std::unique_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
351 std::unique_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
zhongyiaf257542016-12-19 03:36:01352 std::unique_ptr<ServerPushDelegate> push_delegate_;
[email protected]cf4cae32014-05-27 00:39:10353 QuicStreamFactory quic_stream_factory_;
354 SpdySessionPool spdy_session_pool_;
danakj1fd259a02016-04-16 03:17:09355 std::unique_ptr<HttpStreamFactory> http_stream_factory_;
356 std::unique_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
avifceb32f62016-10-07 16:30:52357 std::map<HttpResponseBodyDrainer*, std::unique_ptr<HttpResponseBodyDrainer>>
358 response_drainers_;
rdsmith1d343be52016-10-21 20:37:50359 std::unique_ptr<NetworkThrottleManager> network_stream_throttler_;
[email protected]cf4cae32014-05-27 00:39:10360
bnc0d23cf42014-12-11 14:09:46361 NextProtoVector next_protos_;
[email protected]cf4cae32014-05-27 00:39:10362
363 Params params_;
mmenke6ddfbea2017-05-31 21:48:41364 Context context_;
maksim.sisov0adf8592016-07-15 06:25:56365
366 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
gab47aa7da2017-06-02 16:09:43367
368 THREAD_CHECKER(thread_checker_);
[email protected]cf4cae32014-05-27 00:39:10369};
370
371} // namespace net
372
373#endif // NET_HTTP_HTTP_NETWORK_SESSION_H_