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/ssl_client_socket.h"
23 #include "net/spdy/spdy_session.h"
28 class HttpAuthHandlerFactory
;
30 class SSLClientSocketPool
;
31 class SSLSocketParams
;
32 class SpdySessionPool
;
34 class TransportClientSocketPool
;
35 class TransportSocketParams
;
37 // HttpProxySocketParams only needs the socket params for one of the proxy
38 // types. The other param must be NULL. When using an HTTP Proxy,
39 // |transport_params| must be set. When using an HTTPS Proxy, |ssl_params|
41 class NET_EXPORT_PRIVATE HttpProxySocketParams
42 : public base::RefCounted
<HttpProxySocketParams
> {
44 HttpProxySocketParams(
45 const scoped_refptr
<TransportSocketParams
>& transport_params
,
46 const scoped_refptr
<SSLSocketParams
>& ssl_params
,
47 const GURL
& request_url
,
48 const std::string
& user_agent
,
49 const HostPortPair
& endpoint
,
50 HttpAuthCache
* http_auth_cache
,
51 HttpAuthHandlerFactory
* http_auth_handler_factory
,
52 SpdySessionPool
* spdy_session_pool
,
54 ProxyDelegate
* proxy_delegate
);
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_
; }
76 ProxyDelegate
* proxy_delegate() const {
77 return proxy_delegate_
;
81 friend class base::RefCounted
<HttpProxySocketParams
>;
82 ~HttpProxySocketParams();
84 const scoped_refptr
<TransportSocketParams
> transport_params_
;
85 const scoped_refptr
<SSLSocketParams
> ssl_params_
;
86 SpdySessionPool
* spdy_session_pool_
;
87 const GURL request_url_
;
88 const std::string user_agent_
;
89 const HostPortPair endpoint_
;
90 HttpAuthCache
* const http_auth_cache_
;
91 HttpAuthHandlerFactory
* const http_auth_handler_factory_
;
94 ProxyDelegate
* proxy_delegate_
;
96 DISALLOW_COPY_AND_ASSIGN(HttpProxySocketParams
);
99 // HttpProxyConnectJob optionally establishes a tunnel through the proxy
100 // server after connecting the underlying transport socket.
101 class HttpProxyConnectJob
: public ConnectJob
{
103 HttpProxyConnectJob(const std::string
& group_name
,
104 RequestPriority priority
,
105 const scoped_refptr
<HttpProxySocketParams
>& params
,
106 const base::TimeDelta
& timeout_duration
,
107 TransportClientSocketPool
* transport_pool
,
108 SSLClientSocketPool
* ssl_pool
,
111 ~HttpProxyConnectJob() override
;
113 // ConnectJob methods.
114 LoadState
GetLoadState() const override
;
116 void GetAdditionalErrorState(ClientSocketHandle
* handle
) override
;
121 STATE_TCP_CONNECT_COMPLETE
,
123 STATE_SSL_CONNECT_COMPLETE
,
124 STATE_HTTP_PROXY_CONNECT
,
125 STATE_HTTP_PROXY_CONNECT_COMPLETE
,
126 STATE_SPDY_PROXY_CREATE_STREAM
,
127 STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE
,
128 STATE_SPDY_PROXY_CONNECT_COMPLETE
,
132 void OnIOComplete(int result
);
134 // Runs the state transition loop.
135 int DoLoop(int result
);
137 // Connecting to HTTP Proxy
138 int DoTransportConnect();
139 int DoTransportConnectComplete(int result
);
140 // Connecting to HTTPS Proxy
142 int DoSSLConnectComplete(int result
);
144 int DoHttpProxyConnect();
145 int DoHttpProxyConnectComplete(int result
);
147 int DoSpdyProxyCreateStream();
148 int DoSpdyProxyCreateStreamComplete(int result
);
150 void NotifyProxyDelegateOfCompletion(int result
);
152 // Begins the tcp connection and the optional Http proxy tunnel. If the
153 // request is not immediately servicable (likely), the request will return
154 // ERR_IO_PENDING. An OK return from this function or the callback means
155 // that the connection is established; ERR_PROXY_AUTH_REQUESTED means
156 // that the tunnel needs authentication credentials, the socket will be
157 // returned in this case, and must be release back to the pool; or
158 // a standard net error code will be returned.
159 int ConnectInternal() override
;
161 scoped_refptr
<HttpProxySocketParams
> params_
;
162 TransportClientSocketPool
* const transport_pool_
;
163 SSLClientSocketPool
* const ssl_pool_
;
166 CompletionCallback callback_
;
167 scoped_ptr
<ClientSocketHandle
> transport_socket_handle_
;
168 scoped_ptr
<ProxyClientSocket
> transport_socket_
;
170 // Protocol negotiated with the server.
171 NextProto protocol_negotiated_
;
173 HttpResponseInfo error_response_info_
;
175 SpdyStreamRequest spdy_stream_request_
;
177 base::WeakPtrFactory
<HttpProxyConnectJob
> weak_ptr_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob
);
182 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool
183 : public ClientSocketPool
,
184 public HigherLayeredPool
{
186 typedef HttpProxySocketParams SocketParams
;
188 HttpProxyClientSocketPool(int max_sockets
,
189 int max_sockets_per_group
,
190 TransportClientSocketPool
* transport_pool
,
191 SSLClientSocketPool
* ssl_pool
,
194 ~HttpProxyClientSocketPool() override
;
196 // ClientSocketPool implementation.
197 int RequestSocket(const std::string
& group_name
,
198 const void* connect_params
,
199 RequestPriority priority
,
200 ClientSocketHandle
* handle
,
201 const CompletionCallback
& callback
,
202 const BoundNetLog
& net_log
) override
;
204 void RequestSockets(const std::string
& group_name
,
207 const BoundNetLog
& net_log
) override
;
209 void CancelRequest(const std::string
& group_name
,
210 ClientSocketHandle
* handle
) override
;
212 void ReleaseSocket(const std::string
& group_name
,
213 scoped_ptr
<StreamSocket
> socket
,
216 void FlushWithError(int error
) override
;
218 void CloseIdleSockets() override
;
220 int IdleSocketCount() const override
;
222 int IdleSocketCountInGroup(const std::string
& group_name
) const override
;
224 LoadState
GetLoadState(const std::string
& group_name
,
225 const ClientSocketHandle
* handle
) const override
;
227 base::DictionaryValue
* GetInfoAsValue(
228 const std::string
& name
,
229 const std::string
& type
,
230 bool include_nested_pools
) const override
;
232 base::TimeDelta
ConnectionTimeout() const override
;
234 // LowerLayeredPool implementation.
235 bool IsStalled() const override
;
237 void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
239 void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
241 // HigherLayeredPool implementation.
242 bool CloseOneIdleConnection() override
;
245 typedef ClientSocketPoolBase
<HttpProxySocketParams
> PoolBase
;
247 class HttpProxyConnectJobFactory
: public PoolBase::ConnectJobFactory
{
249 HttpProxyConnectJobFactory(TransportClientSocketPool
* transport_pool
,
250 SSLClientSocketPool
* ssl_pool
,
253 // ClientSocketPoolBase::ConnectJobFactory methods.
254 scoped_ptr
<ConnectJob
> NewConnectJob(
255 const std::string
& group_name
,
256 const PoolBase::Request
& request
,
257 ConnectJob::Delegate
* delegate
) const override
;
259 base::TimeDelta
ConnectionTimeout() const override
;
262 TransportClientSocketPool
* const transport_pool_
;
263 SSLClientSocketPool
* const ssl_pool_
;
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
);
279 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_