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_PROXY_CLIENT_SOCKET_POOL_H_
6 #define NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_
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"
30 class HttpAuthHandlerFactory
;
31 class SSLClientSocketPool
;
32 class SSLSocketParams
;
33 class SpdySessionPool
;
35 class TransportClientSocketPool
;
36 class TransportSocketParams
;
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|
42 class NET_EXPORT_PRIVATE HttpProxySocketParams
43 : public base::RefCounted
<HttpProxySocketParams
> {
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
,
56 const scoped_refptr
<TransportSocketParams
>& transport_params() const {
57 return transport_params_
;
59 const scoped_refptr
<SSLSocketParams
>& ssl_params() const {
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_
;
69 SpdySessionPool
* spdy_session_pool() {
70 return spdy_session_pool_
;
72 const HostResolver::RequestInfo
& destination() const;
73 bool tunnel() const { return tunnel_
; }
74 bool ignore_limits() const { return ignore_limits_
; }
77 friend class base::RefCounted
<HttpProxySocketParams
>;
78 ~HttpProxySocketParams();
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_
;
91 DISALLOW_COPY_AND_ASSIGN(HttpProxySocketParams
);
94 // HttpProxyConnectJob optionally establishes a tunnel through the proxy
95 // server after connecting the underlying transport socket.
96 class HttpProxyConnectJob
: public ConnectJob
{
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
,
106 virtual ~HttpProxyConnectJob();
108 // ConnectJob methods.
109 virtual LoadState
GetLoadState() const OVERRIDE
;
111 virtual void GetAdditionalErrorState(ClientSocketHandle
* handle
) OVERRIDE
;
116 STATE_TCP_CONNECT_COMPLETE
,
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
,
127 void OnIOComplete(int result
);
129 // Runs the state transition loop.
130 int DoLoop(int result
);
132 // Connecting to HTTP Proxy
133 int DoTransportConnect();
134 int DoTransportConnectComplete(int result
);
135 // Connecting to HTTPS Proxy
137 int DoSSLConnectComplete(int result
);
139 int DoHttpProxyConnect();
140 int DoHttpProxyConnectComplete(int result
);
142 int DoSpdyProxyCreateStream();
143 int DoSpdyProxyCreateStreamComplete(int result
);
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
;
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_
;
161 CompletionCallback callback_
;
162 scoped_ptr
<ClientSocketHandle
> transport_socket_handle_
;
163 scoped_ptr
<ProxyClientSocket
> transport_socket_
;
165 // Protocol negotiated with the server.
166 NextProto protocol_negotiated_
;
168 HttpResponseInfo error_response_info_
;
170 SpdyStreamRequest spdy_stream_request_
;
172 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob
);
175 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool
176 : public ClientSocketPool
,
179 HttpProxyClientSocketPool(
181 int max_sockets_per_group
,
182 ClientSocketPoolHistograms
* histograms
,
183 HostResolver
* host_resolver
,
184 TransportClientSocketPool
* transport_pool
,
185 SSLClientSocketPool
* ssl_pool
,
188 virtual ~HttpProxyClientSocketPool();
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
;
198 virtual void RequestSockets(const std::string
& group_name
,
201 const BoundNetLog
& net_log
) OVERRIDE
;
203 virtual void CancelRequest(const std::string
& group_name
,
204 ClientSocketHandle
* handle
) OVERRIDE
;
206 virtual void ReleaseSocket(const std::string
& group_name
,
207 StreamSocket
* socket
,
210 virtual void FlushWithError(int error
) OVERRIDE
;
212 virtual bool IsStalled() const OVERRIDE
;
214 virtual void CloseIdleSockets() OVERRIDE
;
216 virtual int IdleSocketCount() const OVERRIDE
;
218 virtual int IdleSocketCountInGroup(
219 const std::string
& group_name
) const OVERRIDE
;
221 virtual LoadState
GetLoadState(
222 const std::string
& group_name
,
223 const ClientSocketHandle
* handle
) const OVERRIDE
;
225 virtual void AddLayeredPool(LayeredPool
* layered_pool
) OVERRIDE
;
227 virtual void RemoveLayeredPool(LayeredPool
* layered_pool
) OVERRIDE
;
229 virtual base::DictionaryValue
* GetInfoAsValue(
230 const std::string
& name
,
231 const std::string
& type
,
232 bool include_nested_pools
) const OVERRIDE
;
234 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
236 virtual ClientSocketPoolHistograms
* histograms() const OVERRIDE
;
238 // LayeredPool implementation.
239 virtual bool CloseOneIdleConnection() OVERRIDE
;
242 typedef ClientSocketPoolBase
<HttpProxySocketParams
> PoolBase
;
244 class HttpProxyConnectJobFactory
: public PoolBase::ConnectJobFactory
{
246 HttpProxyConnectJobFactory(
247 TransportClientSocketPool
* transport_pool
,
248 SSLClientSocketPool
* ssl_pool
,
249 HostResolver
* host_resolver
,
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
;
258 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
261 TransportClientSocketPool
* const transport_pool_
;
262 SSLClientSocketPool
* const ssl_pool_
;
263 HostResolver
* const host_resolver_
;
265 base::TimeDelta timeout_
;
267 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJobFactory
);
270 TransportClientSocketPool
* const transport_pool_
;
271 SSLClientSocketPool
* const ssl_pool_
;
274 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocketPool
);
277 REGISTER_SOCKET_PARAMS_FOR_POOL(HttpProxyClientSocketPool
,
278 HttpProxySocketParams
);
282 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_