OLD | NEW |
| (Empty) |
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 HttpAuthCache; | |
29 class HttpAuthHandlerFactory; | |
30 class ProxyDelegate; | |
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 ProxyDelegate* proxy_delegate); | |
56 | |
57 const scoped_refptr<TransportSocketParams>& transport_params() const { | |
58 return transport_params_; | |
59 } | |
60 const scoped_refptr<SSLSocketParams>& ssl_params() const { | |
61 return ssl_params_; | |
62 } | |
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_; | |
69 } | |
70 SpdySessionPool* spdy_session_pool() { | |
71 return spdy_session_pool_; | |
72 } | |
73 const HostResolver::RequestInfo& destination() const; | |
74 bool tunnel() const { return tunnel_; } | |
75 bool ignore_limits() const { return ignore_limits_; } | |
76 | |
77 ProxyDelegate* proxy_delegate() const { | |
78 return proxy_delegate_; | |
79 } | |
80 | |
81 private: | |
82 friend class base::RefCounted<HttpProxySocketParams>; | |
83 ~HttpProxySocketParams(); | |
84 | |
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_; | |
93 const bool tunnel_; | |
94 bool ignore_limits_; | |
95 ProxyDelegate* proxy_delegate_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(HttpProxySocketParams); | |
98 }; | |
99 | |
100 // HttpProxyConnectJob optionally establishes a tunnel through the proxy | |
101 // server after connecting the underlying transport socket. | |
102 class HttpProxyConnectJob : public ConnectJob { | |
103 public: | |
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, | |
110 Delegate* delegate, | |
111 NetLog* net_log); | |
112 ~HttpProxyConnectJob() override; | |
113 | |
114 // ConnectJob methods. | |
115 LoadState GetLoadState() const override; | |
116 | |
117 void GetAdditionalErrorState(ClientSocketHandle* handle) override; | |
118 | |
119 private: | |
120 enum State { | |
121 STATE_TCP_CONNECT, | |
122 STATE_TCP_CONNECT_COMPLETE, | |
123 STATE_SSL_CONNECT, | |
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, | |
130 STATE_NONE, | |
131 }; | |
132 | |
133 void OnIOComplete(int result); | |
134 | |
135 // Runs the state transition loop. | |
136 int DoLoop(int result); | |
137 | |
138 // Connecting to HTTP Proxy | |
139 int DoTransportConnect(); | |
140 int DoTransportConnectComplete(int result); | |
141 // Connecting to HTTPS Proxy | |
142 int DoSSLConnect(); | |
143 int DoSSLConnectComplete(int result); | |
144 | |
145 int DoHttpProxyConnect(); | |
146 int DoHttpProxyConnectComplete(int result); | |
147 | |
148 int DoSpdyProxyCreateStream(); | |
149 int DoSpdyProxyCreateStreamComplete(int result); | |
150 | |
151 void NotifyProxyDelegateOfCompletion(int result); | |
152 | |
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; | |
161 | |
162 scoped_refptr<HttpProxySocketParams> params_; | |
163 TransportClientSocketPool* const transport_pool_; | |
164 SSLClientSocketPool* const ssl_pool_; | |
165 | |
166 State next_state_; | |
167 CompletionCallback callback_; | |
168 scoped_ptr<ClientSocketHandle> transport_socket_handle_; | |
169 scoped_ptr<ProxyClientSocket> transport_socket_; | |
170 bool using_spdy_; | |
171 // Protocol negotiated with the server. | |
172 NextProto protocol_negotiated_; | |
173 | |
174 HttpResponseInfo error_response_info_; | |
175 | |
176 SpdyStreamRequest spdy_stream_request_; | |
177 | |
178 base::WeakPtrFactory<HttpProxyConnectJob> weak_ptr_factory_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJob); | |
181 }; | |
182 | |
183 class NET_EXPORT_PRIVATE HttpProxyClientSocketPool | |
184 : public ClientSocketPool, | |
185 public HigherLayeredPool { | |
186 public: | |
187 typedef HttpProxySocketParams SocketParams; | |
188 | |
189 HttpProxyClientSocketPool(int max_sockets, | |
190 int max_sockets_per_group, | |
191 ClientSocketPoolHistograms* histograms, | |
192 TransportClientSocketPool* transport_pool, | |
193 SSLClientSocketPool* ssl_pool, | |
194 NetLog* net_log); | |
195 | |
196 ~HttpProxyClientSocketPool() override; | |
197 | |
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; | |
205 | |
206 void RequestSockets(const std::string& group_name, | |
207 const void* params, | |
208 int num_sockets, | |
209 const BoundNetLog& net_log) override; | |
210 | |
211 void CancelRequest(const std::string& group_name, | |
212 ClientSocketHandle* handle) override; | |
213 | |
214 void ReleaseSocket(const std::string& group_name, | |
215 scoped_ptr<StreamSocket> socket, | |
216 int id) override; | |
217 | |
218 void FlushWithError(int error) override; | |
219 | |
220 void CloseIdleSockets() override; | |
221 | |
222 int IdleSocketCount() const override; | |
223 | |
224 int IdleSocketCountInGroup(const std::string& group_name) const override; | |
225 | |
226 LoadState GetLoadState(const std::string& group_name, | |
227 const ClientSocketHandle* handle) const override; | |
228 | |
229 base::DictionaryValue* GetInfoAsValue( | |
230 const std::string& name, | |
231 const std::string& type, | |
232 bool include_nested_pools) const override; | |
233 | |
234 base::TimeDelta ConnectionTimeout() const override; | |
235 | |
236 ClientSocketPoolHistograms* histograms() const override; | |
237 | |
238 // LowerLayeredPool implementation. | |
239 bool IsStalled() const override; | |
240 | |
241 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override; | |
242 | |
243 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override; | |
244 | |
245 // HigherLayeredPool implementation. | |
246 bool CloseOneIdleConnection() override; | |
247 | |
248 private: | |
249 typedef ClientSocketPoolBase<HttpProxySocketParams> PoolBase; | |
250 | |
251 class HttpProxyConnectJobFactory : public PoolBase::ConnectJobFactory { | |
252 public: | |
253 HttpProxyConnectJobFactory(TransportClientSocketPool* transport_pool, | |
254 SSLClientSocketPool* ssl_pool, | |
255 NetLog* net_log); | |
256 | |
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; | |
262 | |
263 base::TimeDelta ConnectionTimeout() const override; | |
264 | |
265 private: | |
266 TransportClientSocketPool* const transport_pool_; | |
267 SSLClientSocketPool* const ssl_pool_; | |
268 NetLog* net_log_; | |
269 base::TimeDelta timeout_; | |
270 | |
271 DISALLOW_COPY_AND_ASSIGN(HttpProxyConnectJobFactory); | |
272 }; | |
273 | |
274 TransportClientSocketPool* const transport_pool_; | |
275 SSLClientSocketPool* const ssl_pool_; | |
276 PoolBase base_; | |
277 | |
278 DISALLOW_COPY_AND_ASSIGN(HttpProxyClientSocketPool); | |
279 }; | |
280 | |
281 } // namespace net | |
282 | |
283 #endif // NET_HTTP_HTTP_PROXY_CLIENT_SOCKET_POOL_H_ | |
OLD | NEW |