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"
29 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
,
55 ProxyDelegate
* proxy_delegate
);
57 const scoped_refptr
<TransportSocketParams
>& transport_params() const {
58 return transport_params_
;
60 const scoped_refptr
<SSLSocketParams
>& ssl_params() const {
63 const GURL
& request_url() const { return request_url_
; }
64 const std::string
& user_agent() const { return user_agent_
; }
65 const HostPortPair
& endpoint() const { return endpoint_
; }
66 HttpAuthCache
* http_auth_cache() const { return http_auth_cache_
; }
67 HttpAuthHandlerFactory
* http_auth_handler_factory() const {
68 return http_auth_handler_factory_
;
70 SpdySessionPool
* spdy_session_pool() {
71 return spdy_session_pool_
;
73 const HostResolver::RequestInfo
& destination() const;
74 bool tunnel() const { return tunnel_
; }
75 bool ignore_limits() const { return ignore_limits_
; }
77 ProxyDelegate
* proxy_delegate() const {
78 return proxy_delegate_
;
82 friend class base::RefCounted
<HttpProxySocketParams
>;
83 ~HttpProxySocketParams();
85 const scoped_refptr
<TransportSocketParams
> transport_params_
;
86 const scoped_refptr
<SSLSocketParams
> ssl_params_
;
87 SpdySessionPool
* spdy_session_pool_
;
88 const GURL request_url_
;
89 const std::string user_agent_
;
90 const HostPortPair endpoint_
;
91 HttpAuthCache
* const http_auth_cache_
;
92 HttpAuthHandlerFactory
* const http_auth_handler_factory_
;
95 ProxyDelegate
* proxy_delegate_
;
97 DISALLOW_COPY_AND_ASSIGN(HttpProxySocketParams
);
100 // HttpProxyConnectJob optionally establishes a tunnel through the proxy
101 // server after connecting the underlying transport socket.
102 class HttpProxyConnectJob
: public ConnectJob
{
104 HttpProxyConnectJob(const std::string
& group_name
,
105 RequestPriority priority
,
106 const scoped_refptr
<HttpProxySocketParams
>& params
,
107 const base::TimeDelta
& timeout_duration
,
108 TransportClientSocketPool
* transport_pool
,
109 SSLClientSocketPool
* ssl_pool
,
112 ~HttpProxyConnectJob() override
;
114 // ConnectJob methods.
115 LoadState
GetLoadState() const override
;
117 void GetAdditionalErrorState(ClientSocketHandle
* handle
) override
;
122 STATE_TCP_CONNECT_COMPLETE
,
124 STATE_SSL_CONNECT_COMPLETE
,
125 STATE_HTTP_PROXY_CONNECT
,
126 STATE_HTTP_PROXY_CONNECT_COMPLETE
,
127 STATE_SPDY_PROXY_CREATE_STREAM
,
128 STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE
,
129 STATE_SPDY_PROXY_CONNECT_COMPLETE
,
133 void OnIOComplete(int result
);
135 // Runs the state transition loop.
136 int DoLoop(int result
);
138 // Connecting to HTTP Proxy
139 int DoTransportConnect();
140 int DoTransportConnectComplete(int result
);
141 // Connecting to HTTPS Proxy
143 int DoSSLConnectComplete(int result
);
145 int DoHttpProxyConnect();
146 int DoHttpProxyConnectComplete(int result
);
148 int DoSpdyProxyCreateStream();
149 int DoSpdyProxyCreateStreamComplete(int result
);
151 void NotifyProxyDelegateOfCompletion(int result
);
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 int ConnectInternal() override
;
162 scoped_refptr
<HttpProxySocketParams
> params_
;
163 TransportClientSocketPool
* const transport_pool_
;
164 SSLClientSocketPool
* const ssl_pool_
;
167 CompletionCallback callback_
;
168 scoped_ptr
<ClientSocketHandle
> transport_socket_handle_
;
169 scoped_ptr
<ProxyClientSocket
> transport_socket_
;
171 // Protocol negotiated with the server.
172 NextProto protocol_negotiated_
;
174 HttpResponseInfo error_response_info_
;
176 SpdyStreamRequest spdy_stream_request_
;
178 base::WeakPtrFactory
<HttpProxyConnectJob
> weak_ptr_factory_
;
180 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob
);
183 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool
184 : public ClientSocketPool
,
185 public HigherLayeredPool
{
187 typedef HttpProxySocketParams SocketParams
;
189 HttpProxyClientSocketPool(int max_sockets
,
190 int max_sockets_per_group
,
191 ClientSocketPoolHistograms
* histograms
,
192 TransportClientSocketPool
* transport_pool
,
193 SSLClientSocketPool
* ssl_pool
,
196 ~HttpProxyClientSocketPool() override
;
198 // ClientSocketPool implementation.
199 int RequestSocket(const std::string
& group_name
,
200 const void* connect_params
,
201 RequestPriority priority
,
202 ClientSocketHandle
* handle
,
203 const CompletionCallback
& callback
,
204 const BoundNetLog
& net_log
) override
;
206 void RequestSockets(const std::string
& group_name
,
209 const BoundNetLog
& net_log
) override
;
211 void CancelRequest(const std::string
& group_name
,
212 ClientSocketHandle
* handle
) override
;
214 void ReleaseSocket(const std::string
& group_name
,
215 scoped_ptr
<StreamSocket
> socket
,
218 void FlushWithError(int error
) override
;
220 void CloseIdleSockets() override
;
222 int IdleSocketCount() const override
;
224 int IdleSocketCountInGroup(const std::string
& group_name
) const override
;
226 LoadState
GetLoadState(const std::string
& group_name
,
227 const ClientSocketHandle
* handle
) const override
;
229 base::DictionaryValue
* GetInfoAsValue(
230 const std::string
& name
,
231 const std::string
& type
,
232 bool include_nested_pools
) const override
;
234 base::TimeDelta
ConnectionTimeout() const override
;
236 ClientSocketPoolHistograms
* histograms() const override
;
238 // LowerLayeredPool implementation.
239 bool IsStalled() const override
;
241 void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
243 void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
245 // HigherLayeredPool implementation.
246 bool CloseOneIdleConnection() override
;
249 typedef ClientSocketPoolBase
<HttpProxySocketParams
> PoolBase
;
251 class HttpProxyConnectJobFactory
: public PoolBase::ConnectJobFactory
{
253 HttpProxyConnectJobFactory(TransportClientSocketPool
* transport_pool
,
254 SSLClientSocketPool
* ssl_pool
,
257 // ClientSocketPoolBase::ConnectJobFactory methods.
258 scoped_ptr
<ConnectJob
> NewConnectJob(
259 const std::string
& group_name
,
260 const PoolBase::Request
& request
,
261 ConnectJob::Delegate
* delegate
) const override
;
263 base::TimeDelta
ConnectionTimeout() const override
;
266 TransportClientSocketPool
* const transport_pool_
;
267 SSLClientSocketPool
* const ssl_pool_
;
269 base::TimeDelta timeout_
;
271 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJobFactory
);
274 TransportClientSocketPool
* const transport_pool_
;
275 SSLClientSocketPool
* const ssl_pool_
;
278 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocketPool
);
283 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_