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 #ifndef NET_HTTP_HTTP_NETWORK_SESSION_H_
6 #define NET_HTTP_HTTP_NETWORK_SESSION_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "net/base/host_port_pair.h"
17 #include "net/base/net_export.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/http/http_auth_cache.h"
20 #include "net/http/http_stream_factory.h"
21 #include "net/quic/quic_stream_factory.h"
22 #include "net/socket/next_proto.h"
23 #include "net/spdy/spdy_session_pool.h"
24 #include "net/ssl/ssl_client_auth_cache.h"
33 class ChannelIDService;
34 class ClientSocketFactory;
35 class ClientSocketPoolManager;
38 class HpackHuffmanAggregator;
39 class HttpAuthHandlerFactory;
40 class HttpNetworkSessionPeer;
41 class HttpProxyClientSocketPool;
42 class HttpResponseBodyDrainer;
43 class HttpServerProperties;
45 class NetworkDelegate;
49 class QuicCryptoClientStreamFactory;
50 class QuicServerInfoFactory;
51 class SOCKSClientSocketPool;
52 class SSLClientSocketPool;
53 class SSLConfigService;
54 class TransportClientSocketPool;
55 class TransportSecurityState;
57 // This class holds session objects used by HttpNetworkTransaction objects.
58 class NET_EXPORT HttpNetworkSession
59 : public base::RefCounted<HttpNetworkSession>,
60 NON_EXPORTED_BASE(public base::NonThreadSafe) {
62 struct NET_EXPORT Params {
66 ClientSocketFactory* client_socket_factory;
67 HostResolver* host_resolver;
68 CertVerifier* cert_verifier;
69 ChannelIDService* channel_id_service;
70 TransportSecurityState* transport_security_state;
71 CTVerifier* cert_transparency_verifier;
72 ProxyService* proxy_service;
73 std::string ssl_session_cache_shard;
74 SSLConfigService* ssl_config_service;
75 HttpAuthHandlerFactory* http_auth_handler_factory;
76 NetworkDelegate* network_delegate;
77 base::WeakPtr<HttpServerProperties> http_server_properties;
79 HostMappingRules* host_mapping_rules;
80 bool enable_ssl_connect_job_waiting;
81 bool ignore_certificate_errors;
82 bool use_stale_while_revalidate;
83 uint16 testing_fixed_http_port;
84 uint16 testing_fixed_https_port;
85 bool enable_tcp_fast_open_for_ssl;
87 bool force_spdy_single_domain;
88 bool enable_spdy_compression;
89 bool enable_spdy_ping_based_connection_checking;
90 NextProto spdy_default_protocol;
91 // The protocols supported by NPN (next protocol negotiation) during the
92 // SSL handshake as well as by HTTP Alternate-Protocol.
93 // TODO(mmenke): This is currently empty by default, and alternate
94 // protocols are disabled. We should use some reasonable
96 NextProtoVector next_protos;
97 size_t spdy_stream_initial_recv_window_size;
98 size_t spdy_initial_max_concurrent_streams;
99 size_t spdy_max_concurrent_streams_limit;
100 SpdySessionPool::TimeFunc time_func;
101 std::string trusted_spdy_proxy;
102 // Controls whether or not ssl is used when in SPDY mode.
103 bool force_spdy_over_ssl;
104 // Controls whether or not SPDY is used without NPN.
105 bool force_spdy_always;
106 // URLs to exclude from forced SPDY.
107 std::set<HostPortPair> forced_spdy_exclusions;
108 // Noe: Using this in the case of NPN for HTTP only results in the browser
109 // trying SSL and then falling back to http.
110 bool use_alternate_protocols;
111 double alternate_protocol_probability_threshold;
112 bool enable_websocket_over_spdy;
115 bool enable_quic_port_selection;
116 bool quic_always_require_handshake_confirmation;
117 bool quic_disable_connection_pooling;
118 int quic_load_server_info_timeout_ms;
119 HostPortPair origin_to_force_quic_on;
120 QuicClock* quic_clock; // Will be owned by QuicStreamFactory.
121 QuicRandom* quic_random;
122 size_t quic_max_packet_length;
123 std::string quic_user_agent_id;
124 bool enable_user_alternate_protocol_ports;
125 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
126 QuicVersionVector quic_supported_versions;
127 QuicTagVector quic_connection_options;
128 ProxyDelegate* proxy_delegate;
131 enum SocketPoolType {
133 WEBSOCKET_SOCKET_POOL,
134 NUM_SOCKET_POOL_TYPES
137 explicit HttpNetworkSession(const Params& params);
139 HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
140 SSLClientAuthCache* ssl_client_auth_cache() {
141 return &ssl_client_auth_cache_;
144 void AddResponseDrainer(HttpResponseBodyDrainer* drainer);
146 void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
148 TransportClientSocketPool* GetTransportSocketPool(SocketPoolType pool_type);
149 SSLClientSocketPool* GetSSLSocketPool(SocketPoolType pool_type);
150 SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
151 SocketPoolType pool_type,
152 const HostPortPair& socks_proxy);
153 HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
154 SocketPoolType pool_type,
155 const HostPortPair& http_proxy);
156 SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
157 SocketPoolType pool_type,
158 const HostPortPair& proxy_server);
160 CertVerifier* cert_verifier() { return cert_verifier_; }
161 ProxyService* proxy_service() { return proxy_service_; }
162 SSLConfigService* ssl_config_service() { return ssl_config_service_.get(); }
163 SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
164 QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
165 HttpAuthHandlerFactory* http_auth_handler_factory() {
166 return http_auth_handler_factory_;
168 NetworkDelegate* network_delegate() {
169 return network_delegate_;
171 base::WeakPtr<HttpServerProperties> http_server_properties() {
172 return http_server_properties_;
174 HttpStreamFactory* http_stream_factory() {
175 return http_stream_factory_.get();
177 HttpStreamFactory* http_stream_factory_for_websocket() {
178 return http_stream_factory_for_websocket_.get();
183 HpackHuffmanAggregator* huffman_aggregator() {
184 return huffman_aggregator_.get();
187 // Creates a Value summary of the state of the socket pools. The caller is
188 // responsible for deleting the returned value.
189 base::Value* SocketPoolInfoToValue() const;
191 // Creates a Value summary of the state of the SPDY sessions. The caller is
192 // responsible for deleting the returned value.
193 base::Value* SpdySessionPoolInfoToValue() const;
195 // Creates a Value summary of the state of the QUIC sessions and
196 // configuration. The caller is responsible for deleting the returned value.
197 base::Value* QuicInfoToValue() const;
199 void CloseAllConnections();
200 void CloseIdleConnections();
202 // Returns the original Params used to construct this session.
203 const Params& params() const { return params_; }
205 bool IsProtocolEnabled(AlternateProtocol protocol) const;
207 void GetNextProtos(std::vector<std::string>* next_protos) const;
209 // Convenience function for searching through |params_| for
210 // |forced_spdy_exclusions|.
211 bool HasSpdyExclusion(HostPortPair host_port_pair) const;
214 friend class base::RefCounted<HttpNetworkSession>;
215 friend class HttpNetworkSessionPeer;
217 ~HttpNetworkSession();
219 ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
221 NetLog* const net_log_;
222 NetworkDelegate* const network_delegate_;
223 const base::WeakPtr<HttpServerProperties> http_server_properties_;
224 CertVerifier* const cert_verifier_;
225 HttpAuthHandlerFactory* const http_auth_handler_factory_;
227 // Not const since it's modified by HttpNetworkSessionPeer for testing.
228 ProxyService* proxy_service_;
229 const scoped_refptr<SSLConfigService> ssl_config_service_;
231 HttpAuthCache http_auth_cache_;
232 SSLClientAuthCache ssl_client_auth_cache_;
233 scoped_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
234 scoped_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
235 QuicStreamFactory quic_stream_factory_;
236 SpdySessionPool spdy_session_pool_;
237 scoped_ptr<HttpStreamFactory> http_stream_factory_;
238 scoped_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
239 std::set<HttpResponseBodyDrainer*> response_drainers_;
241 // TODO(jgraettinger): Remove when Huffman collection is complete.
242 scoped_ptr<HpackHuffmanAggregator> huffman_aggregator_;
244 std::vector<std::string> next_protos_;
245 bool enabled_protocols_[NUM_VALID_ALTERNATE_PROTOCOLS];
252 #endif // NET_HTTP_HTTP_NETWORK_SESSION_H_