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 RequestPriority priority
,
100 const scoped_refptr
<HttpProxySocketParams
>& params
,
101 const base::TimeDelta
& timeout_duration
,
102 TransportClientSocketPool
* transport_pool
,
103 SSLClientSocketPool
* ssl_pool
,
104 HostResolver
* host_resolver
,
107 virtual ~HttpProxyConnectJob();
109 // ConnectJob methods.
110 virtual LoadState
GetLoadState() const OVERRIDE
;
112 virtual void GetAdditionalErrorState(ClientSocketHandle
* handle
) OVERRIDE
;
117 STATE_TCP_CONNECT_COMPLETE
,
119 STATE_SSL_CONNECT_COMPLETE
,
120 STATE_HTTP_PROXY_CONNECT
,
121 STATE_HTTP_PROXY_CONNECT_COMPLETE
,
122 STATE_SPDY_PROXY_CREATE_STREAM
,
123 STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE
,
124 STATE_SPDY_PROXY_CONNECT_COMPLETE
,
128 void OnIOComplete(int result
);
130 // Runs the state transition loop.
131 int DoLoop(int result
);
133 // Connecting to HTTP Proxy
134 int DoTransportConnect();
135 int DoTransportConnectComplete(int result
);
136 // Connecting to HTTPS Proxy
138 int DoSSLConnectComplete(int result
);
140 int DoHttpProxyConnect();
141 int DoHttpProxyConnectComplete(int result
);
143 int DoSpdyProxyCreateStream();
144 int DoSpdyProxyCreateStreamComplete(int result
);
146 // Begins the tcp connection and the optional Http proxy tunnel. If the
147 // request is not immediately servicable (likely), the request will return
148 // ERR_IO_PENDING. An OK return from this function or the callback means
149 // that the connection is established; ERR_PROXY_AUTH_REQUESTED means
150 // that the tunnel needs authentication credentials, the socket will be
151 // returned in this case, and must be release back to the pool; or
152 // a standard net error code will be returned.
153 virtual int ConnectInternal() OVERRIDE
;
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 base::WeakPtrFactory
<HttpProxyConnectJob
> weak_ptr_factory_
;
174 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob
);
177 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool
178 : public ClientSocketPool
,
179 public HigherLayeredPool
{
181 typedef HttpProxySocketParams SocketParams
;
183 HttpProxyClientSocketPool(
185 int max_sockets_per_group
,
186 ClientSocketPoolHistograms
* histograms
,
187 HostResolver
* host_resolver
,
188 TransportClientSocketPool
* transport_pool
,
189 SSLClientSocketPool
* ssl_pool
,
192 virtual ~HttpProxyClientSocketPool();
194 // ClientSocketPool implementation.
195 virtual int RequestSocket(const std::string
& group_name
,
196 const void* connect_params
,
197 RequestPriority priority
,
198 ClientSocketHandle
* handle
,
199 const CompletionCallback
& callback
,
200 const BoundNetLog
& net_log
) OVERRIDE
;
202 virtual void RequestSockets(const std::string
& group_name
,
205 const BoundNetLog
& net_log
) OVERRIDE
;
207 virtual void CancelRequest(const std::string
& group_name
,
208 ClientSocketHandle
* handle
) OVERRIDE
;
210 virtual void ReleaseSocket(const std::string
& group_name
,
211 scoped_ptr
<StreamSocket
> socket
,
214 virtual void FlushWithError(int error
) OVERRIDE
;
216 virtual void CloseIdleSockets() OVERRIDE
;
218 virtual int IdleSocketCount() const OVERRIDE
;
220 virtual int IdleSocketCountInGroup(
221 const std::string
& group_name
) const OVERRIDE
;
223 virtual LoadState
GetLoadState(
224 const std::string
& group_name
,
225 const ClientSocketHandle
* handle
) const OVERRIDE
;
227 virtual base::DictionaryValue
* GetInfoAsValue(
228 const std::string
& name
,
229 const std::string
& type
,
230 bool include_nested_pools
) const OVERRIDE
;
232 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
234 virtual ClientSocketPoolHistograms
* histograms() const OVERRIDE
;
236 // LowerLayeredPool implementation.
237 virtual bool IsStalled() const OVERRIDE
;
239 virtual void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) OVERRIDE
;
241 virtual void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) OVERRIDE
;
243 // HigherLayeredPool implementation.
244 virtual bool CloseOneIdleConnection() OVERRIDE
;
247 typedef ClientSocketPoolBase
<HttpProxySocketParams
> PoolBase
;
249 class HttpProxyConnectJobFactory
: public PoolBase::ConnectJobFactory
{
251 HttpProxyConnectJobFactory(
252 TransportClientSocketPool
* transport_pool
,
253 SSLClientSocketPool
* ssl_pool
,
254 HostResolver
* host_resolver
,
257 // ClientSocketPoolBase::ConnectJobFactory methods.
258 virtual scoped_ptr
<ConnectJob
> NewConnectJob(
259 const std::string
& group_name
,
260 const PoolBase::Request
& request
,
261 ConnectJob::Delegate
* delegate
) const OVERRIDE
;
263 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
;
266 TransportClientSocketPool
* const transport_pool_
;
267 SSLClientSocketPool
* const ssl_pool_
;
268 HostResolver
* const host_resolver_
;
270 base::TimeDelta timeout_
;
272 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJobFactory
);
275 TransportClientSocketPool
* const transport_pool_
;
276 SSLClientSocketPool
* const ssl_pool_
;
279 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocketPool
);
284 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_