Move android_app_version_* into an inner variables dict.
[chromium-blink-merge.git] / net / http / http_proxy_client_socket_pool.cc
bloba861ca479a881ad150d2ce5170f3e2e240fb7372
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 TransportClientSocketPool* transport_pool,
444 SSLClientSocketPool* ssl_pool,
445 NetLog* net_log)
446 : transport_pool_(transport_pool),
447 ssl_pool_(ssl_pool),
448 base_(this,
449 max_sockets,
450 max_sockets_per_group,
451 ClientSocketPool::unused_idle_socket_timeout(),
452 ClientSocketPool::used_idle_socket_timeout(),
453 new HttpProxyConnectJobFactory(transport_pool, ssl_pool, net_log)) {
454 // We should always have a |transport_pool_| except in unit tests.
455 if (transport_pool_)
456 base_.AddLowerLayeredPool(transport_pool_);
457 if (ssl_pool_)
458 base_.AddLowerLayeredPool(ssl_pool_);
461 HttpProxyClientSocketPool::~HttpProxyClientSocketPool() {
464 int HttpProxyClientSocketPool::RequestSocket(
465 const std::string& group_name, const void* socket_params,
466 RequestPriority priority, ClientSocketHandle* handle,
467 const CompletionCallback& callback, const BoundNetLog& net_log) {
468 const scoped_refptr<HttpProxySocketParams>* casted_socket_params =
469 static_cast<const scoped_refptr<HttpProxySocketParams>*>(socket_params);
471 return base_.RequestSocket(group_name, *casted_socket_params, priority,
472 handle, callback, net_log);
475 void HttpProxyClientSocketPool::RequestSockets(
476 const std::string& group_name,
477 const void* params,
478 int num_sockets,
479 const BoundNetLog& net_log) {
480 const scoped_refptr<HttpProxySocketParams>* casted_params =
481 static_cast<const scoped_refptr<HttpProxySocketParams>*>(params);
483 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
486 void HttpProxyClientSocketPool::CancelRequest(
487 const std::string& group_name,
488 ClientSocketHandle* handle) {
489 base_.CancelRequest(group_name, handle);
492 void HttpProxyClientSocketPool::ReleaseSocket(const std::string& group_name,
493 scoped_ptr<StreamSocket> socket,
494 int id) {
495 base_.ReleaseSocket(group_name, socket.Pass(), id);
498 void HttpProxyClientSocketPool::FlushWithError(int error) {
499 base_.FlushWithError(error);
502 void HttpProxyClientSocketPool::CloseIdleSockets() {
503 base_.CloseIdleSockets();
506 int HttpProxyClientSocketPool::IdleSocketCount() const {
507 return base_.idle_socket_count();
510 int HttpProxyClientSocketPool::IdleSocketCountInGroup(
511 const std::string& group_name) const {
512 return base_.IdleSocketCountInGroup(group_name);
515 LoadState HttpProxyClientSocketPool::GetLoadState(
516 const std::string& group_name, const ClientSocketHandle* handle) const {
517 return base_.GetLoadState(group_name, handle);
520 base::DictionaryValue* HttpProxyClientSocketPool::GetInfoAsValue(
521 const std::string& name,
522 const std::string& type,
523 bool include_nested_pools) const {
524 base::DictionaryValue* dict = base_.GetInfoAsValue(name, type);
525 if (include_nested_pools) {
526 base::ListValue* list = new base::ListValue();
527 if (transport_pool_) {
528 list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool",
529 "transport_socket_pool",
530 true));
532 if (ssl_pool_) {
533 list->Append(ssl_pool_->GetInfoAsValue("ssl_socket_pool",
534 "ssl_socket_pool",
535 true));
537 dict->Set("nested_pools", list);
539 return dict;
542 base::TimeDelta HttpProxyClientSocketPool::ConnectionTimeout() const {
543 return base_.ConnectionTimeout();
546 bool HttpProxyClientSocketPool::IsStalled() const {
547 return base_.IsStalled();
550 void HttpProxyClientSocketPool::AddHigherLayeredPool(
551 HigherLayeredPool* higher_pool) {
552 base_.AddHigherLayeredPool(higher_pool);
555 void HttpProxyClientSocketPool::RemoveHigherLayeredPool(
556 HigherLayeredPool* higher_pool) {
557 base_.RemoveHigherLayeredPool(higher_pool);
560 bool HttpProxyClientSocketPool::CloseOneIdleConnection() {
561 if (base_.CloseOneIdleSocket())
562 return true;
563 return base_.CloseOneIdleConnectionInHigherLayeredPool();
566 } // namespace net