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. 4 5 #ifndef NET_HTTP_HTTP_NETWORK_SESSION_H_ 6 #define NET_HTTP_HTTP_NETWORK_SESSION_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include <map> 12 #include <memory> 13 #include <set> 14 #include <string> 15 #include <unordered_set> 16 #include <vector> 17 18 #include "base/bind.h" 19 #include "base/containers/flat_set.h" 20 #include "base/memory/memory_pressure_monitor.h" 21 #include "base/memory/ref_counted.h" 22 #include "base/memory/weak_ptr.h" 23 #include "base/optional.h" 24 #include "base/threading/thread_checker.h" 25 #include "build/buildflag.h" 26 #include "net/base/host_mapping_rules.h" 27 #include "net/base/host_port_pair.h" 28 #include "net/base/net_export.h" 29 #include "net/http/http_auth_cache.h" 30 #include "net/http/http_stream_factory.h" 31 #include "net/net_buildflags.h" 32 #include "net/quic/quic_stream_factory.h" 33 #include "net/socket/connect_job.h" 34 #include "net/socket/next_proto.h" 35 #include "net/socket/websocket_endpoint_lock_manager.h" 36 #include "net/spdy/spdy_session_pool.h" 37 #include "net/ssl/ssl_client_session_cache.h" 38 #include "net/third_party/quiche/src/spdy/core/spdy_protocol.h" 39 40 namespace base { 41 class Value; 42 namespace trace_event { 43 class ProcessMemoryDump; 44 } 45 } 46 47 namespace net { 48 49 class CTPolicyEnforcer; 50 class CertVerifier; 51 class ClientSocketFactory; 52 class ClientSocketPool; 53 class ClientSocketPoolManager; 54 class CTVerifier; 55 class HostResolver; 56 class HttpAuthHandlerFactory; 57 class HttpNetworkSessionPeer; 58 class HttpResponseBodyDrainer; 59 class HttpServerProperties; 60 class HttpUserAgentSettings; 61 class NetLog; 62 #if BUILDFLAG(ENABLE_REPORTING) 63 class NetworkErrorLoggingService; 64 #endif 65 class NetworkQualityEstimator; 66 class ProxyDelegate; 67 class ProxyResolutionService; 68 class ProxyServer; 69 class QuicCryptoClientStreamFactory; 70 #if BUILDFLAG(ENABLE_REPORTING) 71 class ReportingService; 72 #endif 73 class SocketPerformanceWatcherFactory; 74 class SSLConfigService; 75 class TransportSecurityState; 76 77 // Specifies the maximum HPACK dynamic table size the server is allowed to set. 78 const uint32_t kSpdyMaxHeaderTableSize = 64 * 1024; 79 80 // The maximum size of header list that the server is allowed to send. 81 const uint32_t kSpdyMaxHeaderListSize = 256 * 1024; 82 83 // Specifies the maximum concurrent streams server could send (via push). 84 const uint32_t kSpdyMaxConcurrentPushedStreams = 1000; 85 86 // This class holds session objects used by HttpNetworkTransaction objects. 87 class NET_EXPORT HttpNetworkSession { 88 public: 89 // Self-contained structure with all the simple configuration options 90 // supported by the HttpNetworkSession. 91 struct NET_EXPORT Params { 92 Params(); 93 Params(const Params& other); 94 ~Params(); 95 96 bool enable_server_push_cancellation; 97 HostMappingRules host_mapping_rules; 98 bool ignore_certificate_errors; 99 uint16_t testing_fixed_http_port; 100 uint16_t testing_fixed_https_port; 101 bool enable_user_alternate_protocol_ports; 102 103 // Use SPDY ping frames to test for connection health after idle. 104 bool enable_spdy_ping_based_connection_checking; 105 bool enable_http2; 106 size_t spdy_session_max_recv_window_size; 107 // Maximum number of capped frames that can be queued at any time. 108 int spdy_session_max_queued_capped_frames; 109 // HTTP/2 connection settings. 110 // Unknown settings will still be sent to the server. 111 // Might contain unknown setting identifiers from a predefined set that 112 // servers are supposed to ignore, see 113 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00. 114 // The same setting will be sent on every connection to prevent the retry 115 // logic from hiding broken servers. 116 spdy::SettingsMap http2_settings; 117 // If set, an HTTP/2 frame with a reserved frame type will be sent after 118 // every HEADERS and SETTINGS frame. See 119 // https://tools.ietf.org/html/draft-bishop-httpbis-grease-00. 120 // The same frame will be sent out on all connections to prevent the retry 121 // logic from hiding broken servers. 122 base::Optional<SpdySessionPool::GreasedHttp2Frame> greased_http2_frame; 123 // Source of time for SPDY connections. 124 SpdySessionPool::TimeFunc time_func; 125 // Whether to enable HTTP/2 Alt-Svc entries. 126 bool enable_http2_alternative_service; 127 // Whether to enable Websocket over HTTP/2. 128 bool enable_websocket_over_http2; 129 130 // Enables 0-RTT support. 131 bool enable_early_data; 132 133 // Enables QUIC support. 134 bool enable_quic; 135 136 // If true, HTTPS URLs can be sent to QUIC proxies. 137 bool enable_quic_proxies_for_https_urls; 138 139 // If non-empty, QUIC will only be spoken to hosts in this list. 140 base::flat_set<std::string> quic_host_allowlist; 141 142 // If true, idle sockets won't be closed when memory pressure happens. 143 bool disable_idle_sockets_close_on_memory_pressure; 144 145 bool key_auth_cache_server_entries_by_network_isolation_key; 146 }; 147 148 // Structure with pointers to the dependencies of the HttpNetworkSession. 149 // These objects must all outlive the HttpNetworkSession. 150 struct NET_EXPORT Context { 151 Context(); 152 Context(const Context& other); 153 ~Context(); 154 155 ClientSocketFactory* client_socket_factory; 156 HostResolver* host_resolver; 157 CertVerifier* cert_verifier; 158 TransportSecurityState* transport_security_state; 159 CTVerifier* cert_transparency_verifier; 160 CTPolicyEnforcer* ct_policy_enforcer; 161 ProxyResolutionService* proxy_resolution_service; 162 ProxyDelegate* proxy_delegate; 163 const HttpUserAgentSettings* http_user_agent_settings; 164 SSLConfigService* ssl_config_service; 165 HttpAuthHandlerFactory* http_auth_handler_factory; 166 HttpServerProperties* http_server_properties; 167 NetLog* net_log; 168 SocketPerformanceWatcherFactory* socket_performance_watcher_factory; 169 NetworkQualityEstimator* network_quality_estimator; 170 QuicContext* quic_context; 171 #if BUILDFLAG(ENABLE_REPORTING) 172 ReportingService* reporting_service; 173 NetworkErrorLoggingService* network_error_logging_service; 174 #endif 175 176 // Optional factory to use for creating QuicCryptoClientStreams. 177 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory; 178 }; 179 180 enum SocketPoolType { 181 NORMAL_SOCKET_POOL, 182 WEBSOCKET_SOCKET_POOL, 183 NUM_SOCKET_POOL_TYPES 184 }; 185 186 HttpNetworkSession(const Params& params, const Context& context); 187 ~HttpNetworkSession(); 188 http_auth_cache()189 HttpAuthCache* http_auth_cache() { return &http_auth_cache_; } ssl_client_context()190 SSLClientContext* ssl_client_context() { return &ssl_client_context_; } 191 192 void AddResponseDrainer(std::unique_ptr<HttpResponseBodyDrainer> drainer); 193 194 // Removes the drainer from the session. Does not dispose of it. 195 void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer); 196 197 // Returns the socket pool of the given type for use with the specified 198 // ProxyServer. Use ProxyServer::Direct() to get the pool for use with direct 199 // connections. 200 ClientSocketPool* GetSocketPool(SocketPoolType pool_type, 201 const ProxyServer& proxy_server); 202 cert_verifier()203 CertVerifier* cert_verifier() { return cert_verifier_; } proxy_resolution_service()204 ProxyResolutionService* proxy_resolution_service() { 205 return proxy_resolution_service_; 206 } ssl_config_service()207 SSLConfigService* ssl_config_service() { return ssl_config_service_; } websocket_endpoint_lock_manager()208 WebSocketEndpointLockManager* websocket_endpoint_lock_manager() { 209 return &websocket_endpoint_lock_manager_; 210 } spdy_session_pool()211 SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; } quic_stream_factory()212 QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; } http_auth_handler_factory()213 HttpAuthHandlerFactory* http_auth_handler_factory() { 214 return http_auth_handler_factory_; 215 } http_server_properties()216 HttpServerProperties* http_server_properties() { 217 return http_server_properties_; 218 } http_stream_factory()219 HttpStreamFactory* http_stream_factory() { 220 return http_stream_factory_.get(); 221 } net_log()222 NetLog* net_log() { 223 return net_log_; 224 } host_resolver()225 HostResolver* host_resolver() { return host_resolver_; } 226 #if BUILDFLAG(ENABLE_REPORTING) reporting_service()227 ReportingService* reporting_service() const { return reporting_service_; } network_error_logging_service()228 NetworkErrorLoggingService* network_error_logging_service() const { 229 return network_error_logging_service_; 230 } 231 #endif 232 233 // Creates a Value summary of the state of the socket pools. 234 std::unique_ptr<base::Value> SocketPoolInfoToValue() const; 235 236 // Creates a Value summary of the state of the SPDY sessions. 237 std::unique_ptr<base::Value> SpdySessionPoolInfoToValue() const; 238 239 // Creates a Value summary of the state of the QUIC sessions and 240 // configuration. 241 std::unique_ptr<base::Value> QuicInfoToValue() const; 242 243 void CloseAllConnections(int net_error, const char* net_log_reason_utf8); 244 void CloseIdleConnections(const char* net_log_reason_utf8); 245 246 // Returns the original Params used to construct this session. params()247 const Params& params() const { return params_; } 248 // Returns the original Context used to construct this session. context()249 const Context& context() const { return context_; } 250 251 void SetServerPushDelegate(std::unique_ptr<ServerPushDelegate> push_delegate); 252 253 // Populates |*alpn_protos| with protocols to be used with ALPN. 254 void GetAlpnProtos(NextProtoVector* alpn_protos) const; 255 256 // Populates |server_config| and |proxy_config| based on this session. 257 void GetSSLConfig(SSLConfig* server_config, SSLConfig* proxy_config) const; 258 259 // Dumps memory allocation stats. |parent_dump_absolute_name| is the name 260 // used by the parent MemoryAllocatorDump in the memory dump hierarchy. 261 void DumpMemoryStats(base::trace_event::ProcessMemoryDump* pmd, 262 const std::string& parent_absolute_name) const; 263 264 // Evaluates if QUIC is enabled for new streams. 265 bool IsQuicEnabled() const; 266 267 // Disable QUIC for new streams. 268 void DisableQuic(); 269 270 // Clear the SSL session cache. 271 void ClearSSLSessionCache(); 272 273 // Returns a CommonConnectJobParams that references the NetworkSession's 274 // components. If |for_websockets| is true, the Params' 275 // |websocket_endpoint_lock_manager| field will be populated. Otherwise, it 276 // will be nullptr. 277 CommonConnectJobParams CreateCommonConnectJobParams( 278 bool for_websockets = false); 279 280 private: 281 friend class HttpNetworkSessionPeer; 282 283 ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type); 284 285 // Flush sockets on low memory notifications callback. 286 void OnMemoryPressure( 287 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); 288 289 NetLog* const net_log_; 290 HttpServerProperties* const http_server_properties_; 291 CertVerifier* const cert_verifier_; 292 HttpAuthHandlerFactory* const http_auth_handler_factory_; 293 HostResolver* const host_resolver_; 294 295 #if BUILDFLAG(ENABLE_REPORTING) 296 ReportingService* const reporting_service_; 297 NetworkErrorLoggingService* const network_error_logging_service_; 298 #endif 299 ProxyResolutionService* const proxy_resolution_service_; 300 SSLConfigService* const ssl_config_service_; 301 302 HttpAuthCache http_auth_cache_; 303 SSLClientSessionCache ssl_client_session_cache_; 304 SSLClientContext ssl_client_context_; 305 WebSocketEndpointLockManager websocket_endpoint_lock_manager_; 306 std::unique_ptr<ClientSocketPoolManager> normal_socket_pool_manager_; 307 std::unique_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_; 308 std::unique_ptr<ServerPushDelegate> push_delegate_; 309 QuicStreamFactory quic_stream_factory_; 310 SpdySessionPool spdy_session_pool_; 311 std::unique_ptr<HttpStreamFactory> http_stream_factory_; 312 std::map<HttpResponseBodyDrainer*, std::unique_ptr<HttpResponseBodyDrainer>> 313 response_drainers_; 314 NextProtoVector next_protos_; 315 316 Params params_; 317 Context context_; 318 319 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; 320 321 THREAD_CHECKER(thread_checker_); 322 }; 323 324 } // namespace net 325 326 #endif // NET_HTTP_HTTP_NETWORK_SESSION_H_ 327