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_PROXY_CLIENT_SOCKET_POOL_H_
      6 #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/time/time.h"
     15 #include "net/base/host_port_pair.h"
     16 #include "net/base/net_export.h"
     17 #include "net/http/http_auth.h"
     18 #include "net/http/http_response_info.h"
     19 #include "net/http/proxy_client_socket.h"
     20 #include "net/socket/client_socket_pool.h"
     21 #include "net/socket/client_socket_pool_base.h"
     22 #include "net/socket/client_socket_pool_histograms.h"
     23 #include "net/socket/ssl_client_socket.h"
     24 #include "net/spdy/spdy_session.h"
     25 
     26 namespace net {
     27 
     28 class HostResolver;
     29 class HttpAuthCache;
     30 class HttpAuthHandlerFactory;
     31 class ProxyDelegate;
     32 class SSLClientSocketPool;
     33 class SSLSocketParams;
     34 class SpdySessionPool;
     35 class SpdyStream;
     36 class TransportClientSocketPool;
     37 class TransportSocketParams;
     38 
     39 // HttpProxySocketParams only needs the socket params for one of the proxy
     40 // types.  The other param must be NULL.  When using an HTTP Proxy,
     41 // |transport_params| must be set.  When using an HTTPS Proxy, |ssl_params|
     42 // must be set.
     43 class NET_EXPORT_PRIVATE HttpProxySocketParams
     44     : public base::RefCounted<HttpProxySocketParams> {
     45  public:
     46   HttpProxySocketParams(
     47       const scoped_refptr<TransportSocketParams>& transport_params,
     48       const scoped_refptr<SSLSocketParams>& ssl_params,
     49       const GURL& request_url,
     50       const std::string& user_agent,
     51       const HostPortPair& endpoint,
     52       HttpAuthCache* http_auth_cache,
     53       HttpAuthHandlerFactory* http_auth_handler_factory,
     54       SpdySessionPool* spdy_session_pool,
     55       bool tunnel,
     56       ProxyDelegate* proxy_delegate);
     57 
     58   const scoped_refptr<TransportSocketParams>& transport_params() const {
     59     return transport_params_;
     60   }
     61   const scoped_refptr<SSLSocketParams>& ssl_params() const {
     62     return ssl_params_;
     63   }
     64   const GURL& request_url() const { return request_url_; }
     65   const std::string& user_agent() const { return user_agent_; }
     66   const HostPortPair& endpoint() const { return endpoint_; }
     67   HttpAuthCache* http_auth_cache() const { return http_auth_cache_; }
     68   HttpAuthHandlerFactory* http_auth_handler_factory() const {
     69     return http_auth_handler_factory_;
     70   }
     71   SpdySessionPool* spdy_session_pool() {
     72     return spdy_session_pool_;
     73   }
     74   const HostResolver::RequestInfo& destination() const;
     75   bool tunnel() const { return tunnel_; }
     76   bool ignore_limits() const { return ignore_limits_; }
     77 
     78   ProxyDelegate* proxy_delegate() const {
     79     return proxy_delegate_;
     80   }
     81 
     82  private:
     83   friend class base::RefCounted<HttpProxySocketParams>;
     84   ~HttpProxySocketParams();
     85 
     86   const scoped_refptr<TransportSocketParams> transport_params_;
     87   const scoped_refptr<SSLSocketParams> ssl_params_;
     88   SpdySessionPool* spdy_session_pool_;
     89   const GURL request_url_;
     90   const std::string user_agent_;
     91   const HostPortPair endpoint_;
     92   HttpAuthCache* const http_auth_cache_;
     93   HttpAuthHandlerFactory* const http_auth_handler_factory_;
     94   const bool tunnel_;
     95   bool ignore_limits_;
     96   ProxyDelegate* proxy_delegate_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(HttpProxySocketParams);
     99 };
    100 
    101 // HttpProxyConnectJob optionally establishes a tunnel through the proxy
    102 // server after connecting the underlying transport socket.
    103 class HttpProxyConnectJob : public ConnectJob {
    104  public:
    105   HttpProxyConnectJob(const std::string& group_name,
    106                       RequestPriority priority,
    107                       const scoped_refptr<HttpProxySocketParams>& params,
    108                       const base::TimeDelta& timeout_duration,
    109                       TransportClientSocketPool* transport_pool,
    110                       SSLClientSocketPool* ssl_pool,
    111                       HostResolver* host_resolver,
    112                       Delegate* delegate,
    113                       NetLog* net_log);
    114   virtual ~HttpProxyConnectJob();
    115 
    116   // ConnectJob methods.
    117   virtual LoadState GetLoadState() const OVERRIDE;
    118 
    119   virtual void GetAdditionalErrorState(ClientSocketHandle* handle) OVERRIDE;
    120 
    121  private:
    122   enum State {
    123     STATE_TCP_CONNECT,
    124     STATE_TCP_CONNECT_COMPLETE,
    125     STATE_SSL_CONNECT,
    126     STATE_SSL_CONNECT_COMPLETE,
    127     STATE_HTTP_PROXY_CONNECT,
    128     STATE_HTTP_PROXY_CONNECT_COMPLETE,
    129     STATE_SPDY_PROXY_CREATE_STREAM,
    130     STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE,
    131     STATE_SPDY_PROXY_CONNECT_COMPLETE,
    132     STATE_NONE,
    133   };
    134 
    135   void OnIOComplete(int result);
    136 
    137   // Runs the state transition loop.
    138   int DoLoop(int result);
    139 
    140   // Connecting to HTTP Proxy
    141   int DoTransportConnect();
    142   int DoTransportConnectComplete(int result);
    143   // Connecting to HTTPS Proxy
    144   int DoSSLConnect();
    145   int DoSSLConnectComplete(int result);
    146 
    147   int DoHttpProxyConnect();
    148   int DoHttpProxyConnectComplete(int result);
    149 
    150   int DoSpdyProxyCreateStream();
    151   int DoSpdyProxyCreateStreamComplete(int result);
    152 
    153   // Begins the tcp connection and the optional Http proxy tunnel.  If the
    154   // request is not immediately servicable (likely), the request will return
    155   // ERR_IO_PENDING. An OK return from this function or the callback means
    156   // that the connection is established; ERR_PROXY_AUTH_REQUESTED means
    157   // that the tunnel needs authentication credentials, the socket will be
    158   // returned in this case, and must be release back to the pool; or
    159   // a standard net error code will be returned.
    160   virtual int ConnectInternal() OVERRIDE;
    161 
    162   scoped_refptr<HttpProxySocketParams> params_;
    163   TransportClientSocketPool* const transport_pool_;
    164   SSLClientSocketPool* const ssl_pool_;
    165   HostResolver* const resolver_;
    166 
    167   State next_state_;
    168   CompletionCallback callback_;
    169   scoped_ptr<ClientSocketHandle> transport_socket_handle_;
    170   scoped_ptr<ProxyClientSocket> transport_socket_;
    171   bool using_spdy_;
    172   // Protocol negotiated with the server.
    173   NextProto protocol_negotiated_;
    174 
    175   HttpResponseInfo error_response_info_;
    176 
    177   SpdyStreamRequest spdy_stream_request_;
    178 
    179   base::WeakPtrFactory<HttpProxyConnectJob> weak_ptr_factory_;
    180 
    181   DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob);
    182 };
    183 
    184 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool
    185     : public ClientSocketPool,
    186       public HigherLayeredPool {
    187  public:
    188   typedef HttpProxySocketParams SocketParams;
    189 
    190   HttpProxyClientSocketPool(
    191       int max_sockets,
    192       int max_sockets_per_group,
    193       ClientSocketPoolHistograms* histograms,
    194       HostResolver* host_resolver,
    195       TransportClientSocketPool* transport_pool,
    196       SSLClientSocketPool* ssl_pool,
    197       const ProxyDelegate* proxy_delegate,
    198       NetLog* net_log);
    199 
    200   virtual ~HttpProxyClientSocketPool();
    201 
    202   // ClientSocketPool implementation.
    203   virtual int RequestSocket(const std::string& group_name,
    204                             const void* connect_params,
    205                             RequestPriority priority,
    206                             ClientSocketHandle* handle,
    207                             const CompletionCallback& callback,
    208                             const BoundNetLog& net_log) OVERRIDE;
    209 
    210   virtual void RequestSockets(const std::string& group_name,
    211                               const void* params,
    212                               int num_sockets,
    213                               const BoundNetLog& net_log) OVERRIDE;
    214 
    215   virtual void CancelRequest(const std::string& group_name,
    216                              ClientSocketHandle* handle) OVERRIDE;
    217 
    218   virtual void ReleaseSocket(const std::string& group_name,
    219                              scoped_ptr<StreamSocket> socket,
    220                              int id) OVERRIDE;
    221 
    222   virtual void FlushWithError(int error) OVERRIDE;
    223 
    224   virtual void CloseIdleSockets() OVERRIDE;
    225 
    226   virtual int IdleSocketCount() const OVERRIDE;
    227 
    228   virtual int IdleSocketCountInGroup(
    229       const std::string& group_name) const OVERRIDE;
    230 
    231   virtual LoadState GetLoadState(
    232       const std::string& group_name,
    233       const ClientSocketHandle* handle) const OVERRIDE;
    234 
    235   virtual base::DictionaryValue* GetInfoAsValue(
    236       const std::string& name,
    237       const std::string& type,
    238       bool include_nested_pools) const OVERRIDE;
    239 
    240   virtual base::TimeDelta ConnectionTimeout() const OVERRIDE;
    241 
    242   virtual ClientSocketPoolHistograms* histograms() const OVERRIDE;
    243 
    244   // LowerLayeredPool implementation.
    245   virtual bool IsStalled() const OVERRIDE;
    246 
    247   virtual void AddHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE;
    248 
    249   virtual void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) OVERRIDE;
    250 
    251   // HigherLayeredPool implementation.
    252   virtual bool CloseOneIdleConnection() OVERRIDE;
    253 
    254  private:
    255   typedef ClientSocketPoolBase<HttpProxySocketParams> PoolBase;
    256 
    257   class HttpProxyConnectJobFactory : public PoolBase::ConnectJobFactory {
    258    public:
    259     HttpProxyConnectJobFactory(
    260         TransportClientSocketPool* transport_pool,
    261         SSLClientSocketPool* ssl_pool,
    262         HostResolver* host_resolver,
    263         const ProxyDelegate* proxy_delegate,
    264         NetLog* net_log);
    265 
    266     // ClientSocketPoolBase::ConnectJobFactory methods.
    267     virtual scoped_ptr<ConnectJob> NewConnectJob(
    268         const std::string& group_name,
    269         const PoolBase::Request& request,
    270         ConnectJob::Delegate* delegate) const OVERRIDE;
    271 
    272     virtual base::TimeDelta ConnectionTimeout() const OVERRIDE;
    273 
    274    private:
    275     TransportClientSocketPool* const transport_pool_;
    276     SSLClientSocketPool* const ssl_pool_;
    277     HostResolver* const host_resolver_;
    278     const ProxyDelegate* proxy_delegate_;
    279     NetLog* net_log_;
    280     base::TimeDelta timeout_;
    281 
    282     DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJobFactory);
    283   };
    284 
    285   TransportClientSocketPool* const transport_pool_;
    286   SSLClientSocketPool* const ssl_pool_;
    287   PoolBase base_;
    288 
    289   DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocketPool);
    290 };
    291 
    292 }  // namespace net
    293 
    294 #endif  // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_
    295