Roll src/third_party/WebKit d0c7791:6b6978f (svn 192455:192456)
[chromium-blink-merge.git] / net / http / http_proxy_client_socket_pool.cc
blob9c8a531778530c6420f9dcd80be1a612baa1ac0f
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 #include "net/http/http_proxy_client_socket_pool.h"
7 #include <algorithm>
9 #include "base/compiler_specific.h"
10 #include "base/time/time.h"
11 #include "base/values.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/net_errors.h"
14 #include "net/base/proxy_delegate.h"
15 #include "net/http/http_network_session.h"
16 #include "net/http/http_proxy_client_socket.h"
17 #include "net/socket/client_socket_factory.h"
18 #include "net/socket/client_socket_handle.h"
19 #include "net/socket/client_socket_pool_base.h"
20 #include "net/socket/ssl_client_socket.h"
21 #include "net/socket/ssl_client_socket_pool.h"
22 #include "net/socket/transport_client_socket_pool.h"
23 #include "net/spdy/spdy_proxy_client_socket.h"
24 #include "net/spdy/spdy_session.h"
25 #include "net/spdy/spdy_session_pool.h"
26 #include "net/spdy/spdy_stream.h"
27 #include "net/ssl/ssl_cert_request_info.h"
28 #include "url/gurl.h"
30 namespace net {
32 HttpProxySocketParams::HttpProxySocketParams(
33 const scoped_refptr<TransportSocketParams>& transport_params,
34 const scoped_refptr<SSLSocketParams>& ssl_params,
35 const GURL& request_url,
36 const std::string& user_agent,
37 const HostPortPair& endpoint,
38 HttpAuthCache* http_auth_cache,
39 HttpAuthHandlerFactory* http_auth_handler_factory,
40 SpdySessionPool* spdy_session_pool,
41 bool tunnel,
42 ProxyDelegate* proxy_delegate)
43 : transport_params_(transport_params),
44 ssl_params_(ssl_params),
45 spdy_session_pool_(spdy_session_pool),
46 request_url_(request_url),
47 user_agent_(user_agent),
48 endpoint_(endpoint),
49 http_auth_cache_(tunnel ? http_auth_cache : NULL),
50 http_auth_handler_factory_(tunnel ? http_auth_handler_factory : NULL),
51 tunnel_(tunnel),
52 proxy_delegate_(proxy_delegate) {
53 DCHECK((transport_params.get() == NULL && ssl_params.get() != NULL) ||
54 (transport_params.get() != NULL && ssl_params.get() == NULL));
55 if (transport_params_.get()) {
56 ignore_limits_ = transport_params->ignore_limits();
57 } else {
58 ignore_limits_ = ssl_params->ignore_limits();
62 const HostResolver::RequestInfo& HttpProxySocketParams::destination() const {
63 if (transport_params_.get() == NULL) {
64 return ssl_params_->GetDirectConnectionParams()->destination();
65 } else {
66 return transport_params_->destination();
70 HttpProxySocketParams::~HttpProxySocketParams() {}
72 // HttpProxyConnectJobs will time out after this many seconds. Note this is on
73 // top of the timeout for the transport socket.
74 // TODO(kundaji): Proxy connect timeout should be independent of platform and be
75 // based on proxy. Bug http://crbug.com/407446.
76 #if defined(OS_ANDROID) || defined(OS_IOS)
77 static const int kHttpProxyConnectJobTimeoutInSeconds = 10;
78 #else
79 static const int kHttpProxyConnectJobTimeoutInSeconds = 30;
80 #endif
82 HttpProxyConnectJob::HttpProxyConnectJob(
83 const std::string& group_name,
84 RequestPriority priority,
85 const scoped_refptr<HttpProxySocketParams>& params,
86 const base::TimeDelta& timeout_duration,
87 TransportClientSocketPool* transport_pool,
88 SSLClientSocketPool* ssl_pool,
89 Delegate* delegate,
90 NetLog* net_log)
91 : ConnectJob(group_name, timeout_duration, priority, delegate,
92 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
93 params_(params),
94 transport_pool_(transport_pool),
95 ssl_pool_(ssl_pool),
96 using_spdy_(false),
97 protocol_negotiated_(kProtoUnknown),
98 weak_ptr_factory_(this) {
99 callback_= base::Bind(&HttpProxyConnectJob::OnIOComplete,
100 weak_ptr_factory_.GetWeakPtr());
103 HttpProxyConnectJob::~HttpProxyConnectJob() {}
105 LoadState HttpProxyConnectJob::GetLoadState() const {
106 switch (next_state_) {
107 case STATE_TCP_CONNECT:
108 case STATE_TCP_CONNECT_COMPLETE:
109 case STATE_SSL_CONNECT:
110 case STATE_SSL_CONNECT_COMPLETE:
111 return transport_socket_handle_->GetLoadState();
112 case STATE_HTTP_PROXY_CONNECT:
113 case STATE_HTTP_PROXY_CONNECT_COMPLETE:
114 case STATE_SPDY_PROXY_CREATE_STREAM:
115 case STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE:
116 return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL;
117 default:
118 NOTREACHED();
119 return LOAD_STATE_IDLE;
123 void HttpProxyConnectJob::GetAdditionalErrorState(ClientSocketHandle * handle) {
124 if (error_response_info_.cert_request_info.get()) {
125 handle->set_ssl_error_response_info(error_response_info_);
126 handle->set_is_ssl_error(true);
130 void HttpProxyConnectJob::OnIOComplete(int result) {
131 int rv = DoLoop(result);
132 if (rv != ERR_IO_PENDING) {
133 NotifyProxyDelegateOfCompletion(rv);
134 NotifyDelegateOfCompletion(rv); // Deletes |this|
138 int HttpProxyConnectJob::DoLoop(int result) {
139 DCHECK_NE(next_state_, STATE_NONE);
141 int rv = result;
142 do {
143 State state = next_state_;
144 next_state_ = STATE_NONE;
145 switch (state) {
146 case STATE_TCP_CONNECT:
147 DCHECK_EQ(OK, rv);
148 rv = DoTransportConnect();
149 break;
150 case STATE_TCP_CONNECT_COMPLETE:
151 rv = DoTransportConnectComplete(rv);
152 break;
153 case STATE_SSL_CONNECT:
154 DCHECK_EQ(OK, rv);
155 rv = DoSSLConnect();
156 break;
157 case STATE_SSL_CONNECT_COMPLETE:
158 rv = DoSSLConnectComplete(rv);
159 break;
160 case STATE_HTTP_PROXY_CONNECT:
161 DCHECK_EQ(OK, rv);
162 rv = DoHttpProxyConnect();
163 break;
164 case STATE_HTTP_PROXY_CONNECT_COMPLETE:
165 rv = DoHttpProxyConnectComplete(rv);
166 break;
167 case STATE_SPDY_PROXY_CREATE_STREAM:
168 DCHECK_EQ(OK, rv);
169 rv = DoSpdyProxyCreateStream();
170 break;
171 case STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE:
172 rv = DoSpdyProxyCreateStreamComplete(rv);
173 break;
174 default:
175 NOTREACHED() << "bad state";
176 rv = ERR_FAILED;
177 break;
179 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
181 return rv;
184 int HttpProxyConnectJob::DoTransportConnect() {
185 next_state_ = STATE_TCP_CONNECT_COMPLETE;
186 transport_socket_handle_.reset(new ClientSocketHandle());
187 return transport_socket_handle_->Init(group_name(),
188 params_->transport_params(),
189 priority(),
190 callback_,
191 transport_pool_,
192 net_log());
195 int HttpProxyConnectJob::DoTransportConnectComplete(int result) {
196 if (result != OK)
197 return ERR_PROXY_CONNECTION_FAILED;
199 // Reset the timer to just the length of time allowed for HttpProxy handshake
200 // so that a fast TCP connection plus a slow HttpProxy failure doesn't take
201 // longer to timeout than it should.
202 ResetTimer(base::TimeDelta::FromSeconds(
203 kHttpProxyConnectJobTimeoutInSeconds));
205 next_state_ = STATE_HTTP_PROXY_CONNECT;
206 return result;
209 int HttpProxyConnectJob::DoSSLConnect() {
210 if (params_->tunnel()) {
211 SpdySessionKey key(params_->destination().host_port_pair(),
212 ProxyServer::Direct(),
213 PRIVACY_MODE_DISABLED);
214 if (params_->spdy_session_pool()->FindAvailableSession(key, net_log())) {
215 using_spdy_ = true;
216 next_state_ = STATE_SPDY_PROXY_CREATE_STREAM;
217 return OK;
220 next_state_ = STATE_SSL_CONNECT_COMPLETE;
221 transport_socket_handle_.reset(new ClientSocketHandle());
222 return transport_socket_handle_->Init(
223 group_name(), params_->ssl_params(), priority(), callback_,
224 ssl_pool_, net_log());
227 int HttpProxyConnectJob::DoSSLConnectComplete(int result) {
228 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
229 error_response_info_ = transport_socket_handle_->ssl_error_response_info();
230 DCHECK(error_response_info_.cert_request_info.get());
231 error_response_info_.cert_request_info->is_proxy = true;
232 return result;
234 if (IsCertificateError(result)) {
235 if (params_->ssl_params()->load_flags() & LOAD_IGNORE_ALL_CERT_ERRORS) {
236 result = OK;
237 } else {
238 // TODO(rch): allow the user to deal with proxy cert errors in the
239 // same way as server cert errors.
240 transport_socket_handle_->socket()->Disconnect();
241 return ERR_PROXY_CERTIFICATE_INVALID;
244 // A SPDY session to the proxy completed prior to resolving the proxy
245 // hostname. Surface this error, and allow the delegate to retry.
246 // See crbug.com/334413.
247 if (result == ERR_SPDY_SESSION_ALREADY_EXISTS) {
248 DCHECK(!transport_socket_handle_->socket());
249 return ERR_SPDY_SESSION_ALREADY_EXISTS;
251 if (result < 0) {
252 if (transport_socket_handle_->socket())
253 transport_socket_handle_->socket()->Disconnect();
254 return ERR_PROXY_CONNECTION_FAILED;
257 SSLClientSocket* ssl =
258 static_cast<SSLClientSocket*>(transport_socket_handle_->socket());
259 using_spdy_ = ssl->was_spdy_negotiated();
260 protocol_negotiated_ = ssl->GetNegotiatedProtocol();
262 // Reset the timer to just the length of time allowed for HttpProxy handshake
263 // so that a fast SSL connection plus a slow HttpProxy failure doesn't take
264 // longer to timeout than it should.
265 ResetTimer(base::TimeDelta::FromSeconds(
266 kHttpProxyConnectJobTimeoutInSeconds));
267 // TODO(rch): If we ever decide to implement a "trusted" SPDY proxy
268 // (one that we speak SPDY over SSL to, but to which we send HTTPS
269 // request directly instead of through CONNECT tunnels, then we
270 // need to add a predicate to this if statement so we fall through
271 // to the else case. (HttpProxyClientSocket currently acts as
272 // a "trusted" SPDY proxy).
273 if (using_spdy_ && params_->tunnel()) {
274 next_state_ = STATE_SPDY_PROXY_CREATE_STREAM;
275 } else {
276 next_state_ = STATE_HTTP_PROXY_CONNECT;
278 return result;
281 int HttpProxyConnectJob::DoHttpProxyConnect() {
282 next_state_ = STATE_HTTP_PROXY_CONNECT_COMPLETE;
283 const HostResolver::RequestInfo& tcp_destination = params_->destination();
284 const HostPortPair& proxy_server = tcp_destination.host_port_pair();
286 // Add a HttpProxy connection on top of the tcp socket.
287 transport_socket_.reset(
288 new HttpProxyClientSocket(transport_socket_handle_.release(),
289 params_->request_url(),
290 params_->user_agent(),
291 params_->endpoint(),
292 proxy_server,
293 params_->http_auth_cache(),
294 params_->http_auth_handler_factory(),
295 params_->tunnel(),
296 using_spdy_,
297 protocol_negotiated_,
298 params_->proxy_delegate(),
299 params_->ssl_params().get() != NULL));
300 return transport_socket_->Connect(callback_);
303 int HttpProxyConnectJob::DoHttpProxyConnectComplete(int result) {
304 if (result == OK || result == ERR_PROXY_AUTH_REQUESTED ||
305 result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
306 SetSocket(transport_socket_.Pass());
309 if (result == ERR_HTTP_1_1_REQUIRED)
310 return ERR_PROXY_HTTP_1_1_REQUIRED;
312 return result;
315 int HttpProxyConnectJob::DoSpdyProxyCreateStream() {
316 DCHECK(using_spdy_);
317 DCHECK(params_->tunnel());
318 SpdySessionKey key(params_->destination().host_port_pair(),
319 ProxyServer::Direct(),
320 PRIVACY_MODE_DISABLED);
321 SpdySessionPool* spdy_pool = params_->spdy_session_pool();
322 base::WeakPtr<SpdySession> spdy_session =
323 spdy_pool->FindAvailableSession(key, net_log());
324 // It's possible that a session to the proxy has recently been created
325 if (spdy_session) {
326 if (transport_socket_handle_.get()) {
327 if (transport_socket_handle_->socket())
328 transport_socket_handle_->socket()->Disconnect();
329 transport_socket_handle_->Reset();
331 } else {
332 // Create a session direct to the proxy itself
333 spdy_session =
334 spdy_pool->CreateAvailableSessionFromSocket(
335 key, transport_socket_handle_.Pass(),
336 net_log(), OK, /*using_ssl_*/ true);
337 DCHECK(spdy_session);
340 next_state_ = STATE_SPDY_PROXY_CREATE_STREAM_COMPLETE;
341 return spdy_stream_request_.StartRequest(SPDY_BIDIRECTIONAL_STREAM,
342 spdy_session,
343 params_->request_url(),
344 priority(),
345 spdy_session->net_log(),
346 callback_);
349 int HttpProxyConnectJob::DoSpdyProxyCreateStreamComplete(int result) {
350 if (result < 0)
351 return result;
353 next_state_ = STATE_HTTP_PROXY_CONNECT_COMPLETE;
354 base::WeakPtr<SpdyStream> stream = spdy_stream_request_.ReleaseStream();
355 DCHECK(stream.get());
356 // |transport_socket_| will set itself as |stream|'s delegate.
357 transport_socket_.reset(
358 new SpdyProxyClientSocket(stream,
359 params_->user_agent(),
360 params_->endpoint(),
361 params_->request_url(),
362 params_->destination().host_port_pair(),
363 net_log(),
364 params_->http_auth_cache(),
365 params_->http_auth_handler_factory()));
366 return transport_socket_->Connect(callback_);
369 void HttpProxyConnectJob::NotifyProxyDelegateOfCompletion(int result) {
370 if (!params_->proxy_delegate())
371 return;
373 const HostPortPair& proxy_server = params_->destination().host_port_pair();
374 params_->proxy_delegate()->OnTunnelConnectCompleted(params_->endpoint(),
375 proxy_server,
376 result);
379 int HttpProxyConnectJob::ConnectInternal() {
380 if (params_->transport_params().get()) {
381 next_state_ = STATE_TCP_CONNECT;
382 } else {
383 next_state_ = STATE_SSL_CONNECT;
386 int rv = DoLoop(OK);
387 if (rv != ERR_IO_PENDING) {
388 NotifyProxyDelegateOfCompletion(rv);
391 return rv;
394 HttpProxyClientSocketPool::
395 HttpProxyConnectJobFactory::HttpProxyConnectJobFactory(
396 TransportClientSocketPool* transport_pool,
397 SSLClientSocketPool* ssl_pool,
398 NetLog* net_log)
399 : transport_pool_(transport_pool),
400 ssl_pool_(ssl_pool),
401 net_log_(net_log) {
402 base::TimeDelta max_pool_timeout = base::TimeDelta();
404 // TODO(kundaji): Proxy connect timeout should be independent of platform and be
405 // based on proxy. Bug http://crbug.com/407446.
406 #if (defined(OS_ANDROID) || defined(OS_IOS))
407 #else
408 if (transport_pool_)
409 max_pool_timeout = transport_pool_->ConnectionTimeout();
410 if (ssl_pool_)
411 max_pool_timeout = std::max(max_pool_timeout,
412 ssl_pool_->ConnectionTimeout());
413 #endif
414 timeout_ = max_pool_timeout +
415 base::TimeDelta::FromSeconds(kHttpProxyConnectJobTimeoutInSeconds);
419 scoped_ptr<ConnectJob>
420 HttpProxyClientSocketPool::HttpProxyConnectJobFactory::NewConnectJob(
421 const std::string& group_name,
422 const PoolBase::Request& request,
423 ConnectJob::Delegate* delegate) const {
424 return scoped_ptr<ConnectJob>(new HttpProxyConnectJob(group_name,
425 request.priority(),
426 request.params(),
427 ConnectionTimeout(),
428 transport_pool_,
429 ssl_pool_,
430 delegate,
431 net_log_));
434 base::TimeDelta
435 HttpProxyClientSocketPool::HttpProxyConnectJobFactory::ConnectionTimeout(
436 ) const {
437 return timeout_;
440 HttpProxyClientSocketPool::HttpProxyClientSocketPool(
441 int max_sockets,
442 int max_sockets_per_group,
443 ClientSocketPoolHistograms* histograms,
444 TransportClientSocketPool* transport_pool,
445 SSLClientSocketPool* ssl_pool,
446 NetLog* net_log)
447 : transport_pool_(transport_pool),
448 ssl_pool_(ssl_pool),
449 base_(this, max_sockets, max_sockets_per_group, histograms,
450 ClientSocketPool::unused_idle_socket_timeout(),
451 ClientSocketPool::used_idle_socket_timeout(),
452 new HttpProxyConnectJobFactory(transport_pool,
453 ssl_pool,
454 net_log)) {
455 // We should always have a |transport_pool_| except in unit tests.
456 if (transport_pool_)
457 base_.AddLowerLayeredPool(transport_pool_);
458 if (ssl_pool_)
459 base_.AddLowerLayeredPool(ssl_pool_);
462 HttpProxyClientSocketPool::~HttpProxyClientSocketPool() {
465 int HttpProxyClientSocketPool::RequestSocket(
466 const std::string& group_name, const void* socket_params,
467 RequestPriority priority, ClientSocketHandle* handle,
468 const CompletionCallback& callback, const BoundNetLog& net_log) {
469 const scoped_refptr<HttpProxySocketParams>* casted_socket_params =
470 static_cast<const scoped_refptr<HttpProxySocketParams>*>(socket_params);
472 return base_.RequestSocket(group_name, *casted_socket_params, priority,
473 handle, callback, net_log);
476 void HttpProxyClientSocketPool::RequestSockets(
477 const std::string& group_name,
478 const void* params,
479 int num_sockets,
480 const BoundNetLog& net_log) {
481 const scoped_refptr<HttpProxySocketParams>* casted_params =
482 static_cast<const scoped_refptr<HttpProxySocketParams>*>(params);
484 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
487 void HttpProxyClientSocketPool::CancelRequest(
488 const std::string& group_name,
489 ClientSocketHandle* handle) {
490 base_.CancelRequest(group_name, handle);
493 void HttpProxyClientSocketPool::ReleaseSocket(const std::string& group_name,
494 scoped_ptr<StreamSocket> socket,
495 int id) {
496 base_.ReleaseSocket(group_name, socket.Pass(), id);
499 void HttpProxyClientSocketPool::FlushWithError(int error) {
500 base_.FlushWithError(error);
503 void HttpProxyClientSocketPool::CloseIdleSockets() {
504 base_.CloseIdleSockets();
507 int HttpProxyClientSocketPool::IdleSocketCount() const {
508 return base_.idle_socket_count();
511 int HttpProxyClientSocketPool::IdleSocketCountInGroup(
512 const std::string& group_name) const {
513 return base_.IdleSocketCountInGroup(group_name);
516 LoadState HttpProxyClientSocketPool::GetLoadState(
517 const std::string& group_name, const ClientSocketHandle* handle) const {
518 return base_.GetLoadState(group_name, handle);
521 base::DictionaryValue* HttpProxyClientSocketPool::GetInfoAsValue(
522 const std::string& name,
523 const std::string& type,
524 bool include_nested_pools) const {
525 base::DictionaryValue* dict = base_.GetInfoAsValue(name, type);
526 if (include_nested_pools) {
527 base::ListValue* list = new base::ListValue();
528 if (transport_pool_) {
529 list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool",
530 "transport_socket_pool",
531 true));
533 if (ssl_pool_) {
534 list->Append(ssl_pool_->GetInfoAsValue("ssl_socket_pool",
535 "ssl_socket_pool",
536 true));
538 dict->Set("nested_pools", list);
540 return dict;
543 base::TimeDelta HttpProxyClientSocketPool::ConnectionTimeout() const {
544 return base_.ConnectionTimeout();
547 ClientSocketPoolHistograms* HttpProxyClientSocketPool::histograms() const {
548 return base_.histograms();
551 bool HttpProxyClientSocketPool::IsStalled() const {
552 return base_.IsStalled();
555 void HttpProxyClientSocketPool::AddHigherLayeredPool(
556 HigherLayeredPool* higher_pool) {
557 base_.AddHigherLayeredPool(higher_pool);
560 void HttpProxyClientSocketPool::RemoveHigherLayeredPool(
561 HigherLayeredPool* higher_pool) {
562 base_.RemoveHigherLayeredPool(higher_pool);
565 bool HttpProxyClientSocketPool::CloseOneIdleConnection() {
566 if (base_.CloseOneIdleSocket())
567 return true;
568 return base_.CloseOneIdleConnectionInHigherLayeredPool();
571 } // namespace net