Home | History | Annotate | Download | only in http
      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 <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "net/base/host_port_pair.h"
     16 #include "net/base/net_export.h"
     17 #include "net/dns/host_resolver.h"
     18 #include "net/http/http_auth_cache.h"
     19 #include "net/http/http_stream_factory.h"
     20 #include "net/quic/quic_stream_factory.h"
     21 #include "net/spdy/spdy_session_pool.h"
     22 #include "net/ssl/ssl_client_auth_cache.h"
     23 
     24 namespace base {
     25 class Value;
     26 }
     27 
     28 namespace net {
     29 
     30 class CertVerifier;
     31 class ClientSocketFactory;
     32 class ClientSocketPoolManager;
     33 class HostResolver;
     34 class HttpAuthHandlerFactory;
     35 class HttpNetworkSessionPeer;
     36 class HttpProxyClientSocketPool;
     37 class HttpResponseBodyDrainer;
     38 class HttpServerProperties;
     39 class NetLog;
     40 class NetworkDelegate;
     41 class ServerBoundCertService;
     42 class ProxyService;
     43 class QuicClock;
     44 class QuicCryptoClientStreamFactory;
     45 class SOCKSClientSocketPool;
     46 class SSLClientSocketPool;
     47 class SSLConfigService;
     48 class TransportClientSocketPool;
     49 class TransportSecurityState;
     50 
     51 // This class holds session objects used by HttpNetworkTransaction objects.
     52 class NET_EXPORT HttpNetworkSession
     53     : public base::RefCounted<HttpNetworkSession>,
     54       NON_EXPORTED_BASE(public base::NonThreadSafe) {
     55  public:
     56   struct NET_EXPORT Params {
     57     Params();
     58     ~Params();
     59 
     60     ClientSocketFactory* client_socket_factory;
     61     HostResolver* host_resolver;
     62     CertVerifier* cert_verifier;
     63     ServerBoundCertService* server_bound_cert_service;
     64     TransportSecurityState* transport_security_state;
     65     CTVerifier* cert_transparency_verifier;
     66     ProxyService* proxy_service;
     67     std::string ssl_session_cache_shard;
     68     SSLConfigService* ssl_config_service;
     69     HttpAuthHandlerFactory* http_auth_handler_factory;
     70     NetworkDelegate* network_delegate;
     71     base::WeakPtr<HttpServerProperties> http_server_properties;
     72     NetLog* net_log;
     73     HostMappingRules* host_mapping_rules;
     74     bool force_http_pipelining;
     75     bool ignore_certificate_errors;
     76     bool http_pipelining_enabled;
     77     uint16 testing_fixed_http_port;
     78     uint16 testing_fixed_https_port;
     79     bool force_spdy_single_domain;
     80     bool enable_spdy_ip_pooling;
     81     bool enable_spdy_compression;
     82     bool enable_spdy_ping_based_connection_checking;
     83     NextProto spdy_default_protocol;
     84     size_t spdy_stream_initial_recv_window_size;
     85     size_t spdy_initial_max_concurrent_streams;
     86     size_t spdy_max_concurrent_streams_limit;
     87     SpdySessionPool::TimeFunc time_func;
     88     std::string trusted_spdy_proxy;
     89     bool enable_quic;
     90     bool enable_quic_https;
     91     HostPortPair origin_to_force_quic_on;
     92     QuicClock* quic_clock;  // Will be owned by QuicStreamFactory.
     93     QuicRandom* quic_random;
     94     size_t quic_max_packet_length;
     95     bool enable_user_alternate_protocol_ports;
     96     QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory;
     97   };
     98 
     99   enum SocketPoolType {
    100     NORMAL_SOCKET_POOL,
    101     WEBSOCKET_SOCKET_POOL,
    102     NUM_SOCKET_POOL_TYPES
    103   };
    104 
    105   explicit HttpNetworkSession(const Params& params);
    106 
    107   HttpAuthCache* http_auth_cache() { return &http_auth_cache_; }
    108   SSLClientAuthCache* ssl_client_auth_cache() {
    109     return &ssl_client_auth_cache_;
    110   }
    111 
    112   void AddResponseDrainer(HttpResponseBodyDrainer* drainer);
    113 
    114   void RemoveResponseDrainer(HttpResponseBodyDrainer* drainer);
    115 
    116   TransportClientSocketPool* GetTransportSocketPool(SocketPoolType pool_type);
    117   SSLClientSocketPool* GetSSLSocketPool(SocketPoolType pool_type);
    118   SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
    119       SocketPoolType pool_type,
    120       const HostPortPair& socks_proxy);
    121   HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
    122       SocketPoolType pool_type,
    123       const HostPortPair& http_proxy);
    124   SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
    125       SocketPoolType pool_type,
    126       const HostPortPair& proxy_server);
    127 
    128   CertVerifier* cert_verifier() { return cert_verifier_; }
    129   ProxyService* proxy_service() { return proxy_service_; }
    130   SSLConfigService* ssl_config_service() { return ssl_config_service_.get(); }
    131   SpdySessionPool* spdy_session_pool() { return &spdy_session_pool_; }
    132   QuicStreamFactory* quic_stream_factory() { return &quic_stream_factory_; }
    133   HttpAuthHandlerFactory* http_auth_handler_factory() {
    134     return http_auth_handler_factory_;
    135   }
    136   NetworkDelegate* network_delegate() {
    137     return network_delegate_;
    138   }
    139   base::WeakPtr<HttpServerProperties> http_server_properties() {
    140     return http_server_properties_;
    141   }
    142   HttpStreamFactory* http_stream_factory() {
    143     return http_stream_factory_.get();
    144   }
    145   HttpStreamFactory* http_stream_factory_for_websocket() {
    146     return http_stream_factory_for_websocket_.get();
    147   }
    148   NetLog* net_log() {
    149     return net_log_;
    150   }
    151 
    152   // Creates a Value summary of the state of the socket pools. The caller is
    153   // responsible for deleting the returned value.
    154   base::Value* SocketPoolInfoToValue() const;
    155 
    156   // Creates a Value summary of the state of the SPDY sessions. The caller is
    157   // responsible for deleting the returned value.
    158   base::Value* SpdySessionPoolInfoToValue() const;
    159 
    160   // Creates a Value summary of the state of the QUIC sessions and
    161   // configuration. The caller is responsible for deleting the returned value.
    162   base::Value* QuicInfoToValue() const;
    163 
    164   void CloseAllConnections();
    165   void CloseIdleConnections();
    166 
    167   bool force_http_pipelining() const { return force_http_pipelining_; }
    168 
    169   // Returns the original Params used to construct this session.
    170   const Params& params() const { return params_; }
    171 
    172   void set_http_pipelining_enabled(bool enable) {
    173     params_.http_pipelining_enabled = enable;
    174   }
    175 
    176  private:
    177   friend class base::RefCounted<HttpNetworkSession>;
    178   friend class HttpNetworkSessionPeer;
    179 
    180   ~HttpNetworkSession();
    181 
    182   ClientSocketPoolManager* GetSocketPoolManager(SocketPoolType pool_type);
    183 
    184   NetLog* const net_log_;
    185   NetworkDelegate* const network_delegate_;
    186   const base::WeakPtr<HttpServerProperties> http_server_properties_;
    187   CertVerifier* const cert_verifier_;
    188   HttpAuthHandlerFactory* const http_auth_handler_factory_;
    189   bool force_http_pipelining_;
    190 
    191   // Not const since it's modified by HttpNetworkSessionPeer for testing.
    192   ProxyService* proxy_service_;
    193   const scoped_refptr<SSLConfigService> ssl_config_service_;
    194 
    195   HttpAuthCache http_auth_cache_;
    196   SSLClientAuthCache ssl_client_auth_cache_;
    197   scoped_ptr<ClientSocketPoolManager> normal_socket_pool_manager_;
    198   scoped_ptr<ClientSocketPoolManager> websocket_socket_pool_manager_;
    199   QuicStreamFactory quic_stream_factory_;
    200   SpdySessionPool spdy_session_pool_;
    201   scoped_ptr<HttpStreamFactory> http_stream_factory_;
    202   scoped_ptr<HttpStreamFactory> http_stream_factory_for_websocket_;
    203   std::set<HttpResponseBodyDrainer*> response_drainers_;
    204 
    205   Params params_;
    206 };
    207 
    208 }  // namespace net
    209 
    210 #endif  // NET_HTTP_HTTP_NETWORK_SESSION_H_
    211