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