blob: f6a0a95b0134a3d8d2cfc9c61ac4eca456e62ad7 [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"
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"
Bence Béky6a070bcd2018-09-06 15:02:4323#include "base/optional.h"
gab47aa7da2017-06-02 16:09:4324#include "base/threading/thread_checker.h"
Douglas Creager3cb042052018-11-06 23:08:5225#include "build/buildflag.h"
mmenke0d700dd82017-05-26 20:36:1126#include "net/base/host_mapping_rules.h"
[email protected]cf4cae32014-05-27 00:39:1027#include "net/base/host_port_pair.h"
28#include "net/base/net_export.h"
[email protected]cf4cae32014-05-27 00:39:1029#include "net/http/http_auth_cache.h"
Kevin DiClementef07119082019-08-12 13:31:3430#include "net/http/http_auth_preferences.h"
[email protected]cf4cae32014-05-27 00:39:1031#include "net/http/http_stream_factory.h"
Douglas Creager3cb042052018-11-06 23:08:5232#include "net/net_buildflags.h"
Ryan Hamiltona3ee93a72018-08-01 22:03:0833#include "net/quic/quic_stream_factory.h"
Matt Menked6fd2a52019-03-20 06:14:3634#include "net/socket/connect_job.h"
[email protected]cf4cae32014-05-27 00:39:1035#include "net/socket/next_proto.h"
Bence Béky33a4e432018-12-03 16:17:2336#include "net/socket/websocket_endpoint_lock_manager.h"
Bence Béky94658bf2018-05-11 19:22:5837#include "net/spdy/spdy_session_pool.h"
Daniel McArdle3a663d62019-01-31 00:48:4738#include "net/ssl/ssl_client_session_cache.h"
Victor Vasiliev27cc7712019-01-24 11:50:1439#include "net/third_party/quiche/src/spdy/core/spdy_protocol.h"
[email protected]cf4cae32014-05-27 00:39:1040
41namespace base {
42class Value;
xunjieli9f8c5fb52016-12-07 22:59:3343namespace trace_event {
44class ProcessMemoryDump;
45}
[email protected]cf4cae32014-05-27 00:39:1046}
47
Bence Béky33a4e432018-12-03 16:17:2348namespace quic {
49class QuicClock;
50} // namespace quic
51
[email protected]cf4cae32014-05-27 00:39:1052namespace net {
53
estark6f9b3d82016-01-12 21:37:0554class CTPolicyEnforcer;
[email protected]cf4cae32014-05-27 00:39:1055class CertVerifier;
[email protected]cf4cae32014-05-27 00:39:1056class ClientSocketFactory;
Matt Menke12d33db92019-03-26 22:45:4457class ClientSocketPool;
[email protected]cf4cae32014-05-27 00:39:1058class ClientSocketPoolManager;
59class CTVerifier;
60class HostResolver;
[email protected]cf4cae32014-05-27 00:39:1061class HttpAuthHandlerFactory;
62class HttpNetworkSessionPeer;
[email protected]cf4cae32014-05-27 00:39:1063class HttpResponseBodyDrainer;
64class HttpServerProperties;
Matt Menked732ea42019-03-08 12:05:0065class HttpUserAgentSettings;
[email protected]cf4cae32014-05-27 00:39:1066class NetLog;
Douglas Creager3cb042052018-11-06 23:08:5267#if BUILDFLAG(ENABLE_REPORTING)
68class NetworkErrorLoggingService;
69#endif
Tarun Bansaled2b20b642018-10-15 19:51:3270class NetworkQualityEstimator;
Wojciech Dzierżanowski1f823562019-01-18 11:26:0071class ProxyDelegate;
Lily Houghton8c2f97d2018-01-22 05:06:5972class ProxyResolutionService;
Matt Menkee8648fa2019-01-17 16:47:0773class ProxyServer;
74class QuicCryptoClientStreamFactory;
Douglas Creager134b52e2018-11-09 18:00:1475#if BUILDFLAG(ENABLE_REPORTING)
76class ReportingService;
77#endif
tbansalba8f4112015-09-03 21:57:1978class SocketPerformanceWatcherFactory;
[email protected]cf4cae32014-05-27 00:39:1079class SSLConfigService;
[email protected]cf4cae32014-05-27 00:39:1080class TransportSecurityState;
81
bnc3171a2432016-12-28 18:40:2682// Specifies the maximum HPACK dynamic table size the server is allowed to set.
83const uint32_t kSpdyMaxHeaderTableSize = 64 * 1024;
84
85// Specifies the maximum concurrent streams server could send (via push).
86const uint32_t kSpdyMaxConcurrentPushedStreams = 1000;
87
[email protected]cf4cae32014-05-27 00:39:1088// This class holds session objects used by HttpNetworkTransaction objects.
Takashi Sakamoto420ec97c2018-09-26 09:27:5989class NET_EXPORT HttpNetworkSession {
[email protected]cf4cae32014-05-27 00:39:1090 public:
mmenke6ddfbea2017-05-31 21:48:4191 // Self-contained structure with all the simple configuration options
92 // supported by the HttpNetworkSession.
[email protected]cf4cae32014-05-27 00:39:1093 struct NET_EXPORT Params {
94 Params();
vmpstracd23b72016-02-26 21:08:5595 Params(const Params& other);
[email protected]cf4cae32014-05-27 00:39:1096 ~Params();
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;
rch1546ccd2017-04-20 02:14:23103 bool enable_user_alternate_protocol_ports;
[email protected]cf4cae32014-05-27 00:39:10104
rchd502a302015-10-16 03:57:21105 // Use SPDY ping frames to test for connection health after idle.
[email protected]cf4cae32014-05-27 00:39:10106 bool enable_spdy_ping_based_connection_checking;
bnc3f0118e2016-02-02 15:42:22107 bool enable_http2;
bnc8f0f3b62015-04-08 04:37:23108 size_t spdy_session_max_recv_window_size;
David Schinazi410676a2019-08-13 22:31:05109 // Maximum number of capped frames that can be queued at any time.
110 int spdy_session_max_queued_capped_frames;
bnc3171a2432016-12-28 18:40:26111 // HTTP/2 connection settings.
112 // Unknown settings will still be sent to the server.
Bence Béky6a070bcd2018-09-06 15:02:43113 // Might contain unknown setting identifiers from a predefined set that
114 // servers are supposed to ignore, see
115 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00.
116 // The same setting will be sent on every connection to prevent the retry
117 // logic from hiding broken servers.
Ryan Hamilton0239aac2018-05-19 00:03:13118 spdy::SettingsMap http2_settings;
Bence Béky6a070bcd2018-09-06 15:02:43119 // If set, an HTTP/2 frame with a reserved frame type will be sent after
120 // every HEADERS and SETTINGS frame. See
121 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00.
122 // The same frame will be sent out on all connections to prevent the retry
123 // logic from hiding broken servers.
124 base::Optional<SpdySessionPool::GreasedHttp2Frame> greased_http2_frame;
rchd502a302015-10-16 03:57:21125 // Source of time for SPDY connections.
[email protected]cf4cae32014-05-27 00:39:10126 SpdySessionPool::TimeFunc time_func;
bnca86731e2017-04-17 12:31:28127 // Whether to enable HTTP/2 Alt-Svc entries.
128 bool enable_http2_alternative_service;
Bence Béky58b423222018-01-24 15:16:54129 // Whether to enable Websocket over HTTP/2.
130 bool enable_websocket_over_http2;
[email protected]cf4cae32014-05-27 00:39:10131
Steven Valdez1c1859172019-04-10 15:33:28132 // Enables 0-RTT support.
133 bool enable_early_data;
134
rchd502a302015-10-16 03:57:21135 // Enables QUIC support.
[email protected]cf4cae32014-05-27 00:39:10136 bool enable_quic;
rch1546ccd2017-04-20 02:14:23137
Ryan Hamilton4cbcbf12018-12-15 05:16:14138 // If true, HTTPS URLs can be sent to QUIC proxies.
139 bool enable_quic_proxies_for_https_urls;
140
Nick Harper72ade192019-07-17 03:30:42141 // QUIC runtime configuration options and active experiments.
142 QuicParams quic_params;
Renjiea0522f062019-04-29 18:52:21143
Yixin Wang10f477ed2017-11-21 04:20:20144 // If non-empty, QUIC will only be spoken to hosts in this list.
Ryan Sleevia9d6aa62019-07-26 13:32:18145 base::flat_set<std::string> quic_host_allowlist;
xunjieli888c29922016-03-18 21:05:09146
yucliu48f235d2018-01-11 00:59:55147 // If true, idle sockets won't be closed when memory pressure happens.
148 bool disable_idle_sockets_close_on_memory_pressure;
Kevin DiClementef07119082019-08-12 13:31:34149
150 // If authentication APIs that support ambient authentication are allowed
151 // to use the default credentials.
152 HttpAuthPreferences::DefaultCredentials allow_default_credentials;
[email protected]cf4cae32014-05-27 00:39:10153 };
154
mmenke6ddfbea2017-05-31 21:48:41155 // Structure with pointers to the dependencies of the HttpNetworkSession.
156 // These objects must all outlive the HttpNetworkSession.
157 struct NET_EXPORT Context {
158 Context();
159 Context(const Context& other);
160 ~Context();
161
162 ClientSocketFactory* client_socket_factory;
163 HostResolver* host_resolver;
164 CertVerifier* cert_verifier;
mmenke6ddfbea2017-05-31 21:48:41165 TransportSecurityState* transport_security_state;
166 CTVerifier* cert_transparency_verifier;
167 CTPolicyEnforcer* ct_policy_enforcer;
Lily Houghton8c2f97d2018-01-22 05:06:59168 ProxyResolutionService* proxy_resolution_service;
Wojciech Dzierżanowski1f823562019-01-18 11:26:00169 ProxyDelegate* proxy_delegate;
Matt Menked732ea42019-03-08 12:05:00170 const HttpUserAgentSettings* http_user_agent_settings;
mmenke6ddfbea2017-05-31 21:48:41171 SSLConfigService* ssl_config_service;
172 HttpAuthHandlerFactory* http_auth_handler_factory;
173 HttpServerProperties* http_server_properties;
174 NetLog* net_log;
175 SocketPerformanceWatcherFactory* socket_performance_watcher_factory;
Tarun Bansaled2b20b642018-10-15 19:51:32176 NetworkQualityEstimator* network_quality_estimator;
Douglas Creager3cb042052018-11-06 23:08:52177#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:14178 ReportingService* reporting_service;
Douglas Creager3cb042052018-11-06 23:08:52179 NetworkErrorLoggingService* network_error_logging_service;
180#endif
mmenke6ddfbea2017-05-31 21:48:41181
182 // Source of time for QUIC connections.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52183 quic::QuicClock* quic_clock;
mmenke6ddfbea2017-05-31 21:48:41184 // Source of entropy for QUIC connections.
Ryan Hamilton8d9ee76e2018-05-29 23:52:52185 quic::QuicRandom* quic_random;
mmenke6ddfbea2017-05-31 21:48:41186 // Optional factory to use for creating QuicCryptoClientStreams.
187 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
mmenke6ddfbea2017-05-31 21:48:41188 };
189
[email protected]cf4cae32014-05-27 00:39:10190 enum SocketPoolType {
191 NORMAL_SOCKET_POOL,
192 WEBSOCKET_SOCKET_POOL,
193 NUM_SOCKET_POOL_TYPES
194 };
195
mmenke6ddfbea2017-05-31 21:48:41196 HttpNetworkSession(const Params& params, const Context& context);
Takashi Sakamoto420ec97c2018-09-26 09:27:59197 ~HttpNetworkSession();
[email protected]cf4cae32014-05-27 00:39:10198
199 HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
David Benjaminbac8dff2019-08-07 01:30:41200 SSLClientContext* ssl_client_context() { return &ssl_client_context_; }
[email protected]cf4cae32014-05-27 00:39:10201
avifceb32f62016-10-07 16:30:52202 void AddResponseDrainer(std::unique_ptr<HttpResponseBodyDrainer> drainer);
[email protected]cf4cae32014-05-27 00:39:10203
avifceb32f62016-10-07 16:30:52204 // Removes the drainer from the session. Does not dispose of it.
[email protected]cf4cae32014-05-27 00:39:10205 void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
206
Matt Menked23ab952019-03-06 00:24:40207 // Returns the socket pool of the given type for use with the specified
208 // ProxyServer. Use ProxyServer::Direct() to get the pool for use with direct
209 // connections.
Matt Menke12d33db92019-03-26 22:45:44210 ClientSocketPool* GetSocketPool(SocketPoolType pool_type,
211 const ProxyServer& proxy_server);
[email protected]cf4cae32014-05-27 00:39:10212
213 CertVerifier* cert_verifier() { return cert_verifier_; }
Lily Houghton8c2f97d2018-01-22 05:06:59214 ProxyResolutionService* proxy_resolution_service() {
215 return proxy_resolution_service_;
216 }
Ryan Sleevib8449e02018-07-15 04:31:07217 SSLConfigService* ssl_config_service() { return ssl_config_service_; }
Bence Békyda280c62018-04-12 15:08:37218 WebSocketEndpointLockManager* websocket_endpoint_lock_manager() {
Bence Béky33a4e432018-12-03 16:17:23219 return &websocket_endpoint_lock_manager_;
Bence Békyda280c62018-04-12 15:08:37220 }
[email protected]cf4cae32014-05-27 00:39:10221 SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
222 QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
223 HttpAuthHandlerFactory* http_auth_handler_factory() {
224 return http_auth_handler_factory_;
225 }
bnc525e175a2016-06-20 12:36:40226 HttpServerProperties* http_server_properties() {
[email protected]cf4cae32014-05-27 00:39:10227 return http_server_properties_;
228 }
229 HttpStreamFactory* http_stream_factory() {
230 return http_stream_factory_.get();
231 }
[email protected]cf4cae32014-05-27 00:39:10232 NetLog* net_log() {
233 return net_log_;
234 }
Eric Orthbe2efac2019-03-06 01:11:11235 HostResolver* host_resolver() { return host_resolver_; }
Douglas Creager3cb042052018-11-06 23:08:52236#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:14237 ReportingService* reporting_service() const { return reporting_service_; }
Douglas Creager3cb042052018-11-06 23:08:52238 NetworkErrorLoggingService* network_error_logging_service() const {
239 return network_error_logging_service_;
240 }
241#endif
[email protected]cf4cae32014-05-27 00:39:10242
payal.pandey62a400292015-05-28 09:29:54243 // Creates a Value summary of the state of the socket pools.
danakj1fd259a02016-04-16 03:17:09244 std::unique_ptr<base::Value> SocketPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10245
payal.pandey91cb2312015-05-27 07:41:51246 // Creates a Value summary of the state of the SPDY sessions.
danakj1fd259a02016-04-16 03:17:09247 std::unique_ptr<base::Value> SpdySessionPoolInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10248
249 // Creates a Value summary of the state of the QUIC sessions and
payal.pandeya18956a2015-05-27 05:57:55250 // configuration.
danakj1fd259a02016-04-16 03:17:09251 std::unique_ptr<base::Value> QuicInfoToValue() const;
[email protected]cf4cae32014-05-27 00:39:10252
253 void CloseAllConnections();
254 void CloseIdleConnections();
255
256 // Returns the original Params used to construct this session.
257 const Params& params() const { return params_; }
mmenke6ddfbea2017-05-31 21:48:41258 // Returns the original Context used to construct this session.
259 const Context& context() const { return context_; }
[email protected]cf4cae32014-05-27 00:39:10260
zhongyiaf257542016-12-19 03:36:01261 void SetServerPushDelegate(std::unique_ptr<ServerPushDelegate> push_delegate);
262
bnc1f295372015-10-21 23:24:22263 // Populates |*alpn_protos| with protocols to be used with ALPN.
264 void GetAlpnProtos(NextProtoVector* alpn_protos) const;
265
David Benjamin8119ce9e42019-08-06 00:05:06266 // Populates |server_config| and |proxy_config| based on this session.
267 void GetSSLConfig(SSLConfig* server_config, SSLConfig* proxy_config) const;
nharper8cdb0fb2016-04-22 21:34:59268
xunjieli9f8c5fb52016-12-07 22:59:33269 // Dumps memory allocation stats. |parent_dump_absolute_name| is the name
270 // used by the parent MemoryAllocatorDump in the memory dump hierarchy.
271 void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd,
272 const std::string& parent_absolute_name) const;
273
pmarko6ab8be242017-01-11 11:02:55274 // Evaluates if QUIC is enabled for new streams.
275 bool IsQuicEnabled() const;
276
277 // Disable QUIC for new streams.
278 void DisableQuic();
279
Daniel McArdle3a663d62019-01-31 00:48:47280 // Clear the SSL session cache.
281 void ClearSSLSessionCache();
Matt Menked6fd2a52019-03-20 06:14:36282
283 // Returns a CommonConnectJobParams that references the NetworkSession's
284 // components. If |for_websockets| is true, the Params'
285 // |websocket_endpoint_lock_manager| field will be populated. Otherwise, it
286 // will be nullptr.
287 CommonConnectJobParams CreateCommonConnectJobParams(
288 bool for_websockets = false);
Daniel McArdle3a663d62019-01-31 00:48:47289
[email protected]cf4cae32014-05-27 00:39:10290 private:
[email protected]cf4cae32014-05-27 00:39:10291 friend class HttpNetworkSessionPeer;
292
[email protected]cf4cae32014-05-27 00:39:10293 ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
294
maksim.sisov0adf8592016-07-15 06:25:56295 // Flush sockets on low memory notifications callback.
296 void OnMemoryPressure(
297 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
298
[email protected]cf4cae32014-05-27 00:39:10299 NetLog* const net_log_;
bnc525e175a2016-06-20 12:36:40300 HttpServerProperties* const http_server_properties_;
[email protected]cf4cae32014-05-27 00:39:10301 CertVerifier* const cert_verifier_;
302 HttpAuthHandlerFactory* const http_auth_handler_factory_;
Eric Orthbe2efac2019-03-06 01:11:11303 HostResolver* const host_resolver_;
[email protected]cf4cae32014-05-27 00:39:10304
Douglas Creager3cb042052018-11-06 23:08:52305#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:14306 ReportingService* const reporting_service_;
Douglas Creager3cb042052018-11-06 23:08:52307 NetworkErrorLoggingService* const network_error_logging_service_;
308#endif
Bence Békybf1c67f22018-04-12 09:26:26309 ProxyResolutionService* const proxy_resolution_service_;
Ryan Sleevib8449e02018-07-15 04:31:07310 SSLConfigService* const ssl_config_service_;
[email protected]cf4cae32014-05-27 00:39:10311
312 HttpAuthCache http_auth_cache_;
Daniel McArdle3a663d62019-01-31 00:48:47313 SSLClientSessionCache ssl_client_session_cache_;
David Benjamin24725be2019-07-24 20:57:18314 SSLClientContext ssl_client_context_;
Bence Béky33a4e432018-12-03 16:17:23315 WebSocketEndpointLockManager websocket_endpoint_lock_manager_;
danakj1fd259a02016-04-16 03:17:09316 std::unique_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
317 std::unique_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
zhongyiaf257542016-12-19 03:36:01318 std::unique_ptr<ServerPushDelegate> push_delegate_;
[email protected]cf4cae32014-05-27 00:39:10319 QuicStreamFactory quic_stream_factory_;
320 SpdySessionPool spdy_session_pool_;
danakj1fd259a02016-04-16 03:17:09321 std::unique_ptr<HttpStreamFactory> http_stream_factory_;
avifceb32f62016-10-07 16:30:52322 std::map<HttpResponseBodyDrainer*, std::unique_ptr<HttpResponseBodyDrainer>>
323 response_drainers_;
bnc0d23cf42014-12-11 14:09:46324 NextProtoVector next_protos_;
[email protected]cf4cae32014-05-27 00:39:10325
326 Params params_;
mmenke6ddfbea2017-05-31 21:48:41327 Context context_;
maksim.sisov0adf8592016-07-15 06:25:56328
329 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_;
gab47aa7da2017-06-02 16:09:43330
331 THREAD_CHECKER(thread_checker_);
[email protected]cf4cae32014-05-27 00:39:10332};
333
334} // namespace net
335
336#endif // NET_HTTP_HTTP_NETWORK_SESSION_H_