OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_proxy_client_socket.h" | 5 #include "net/http/http_proxy_client_socket.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "net/socket/client_socket_handle.h" | 24 #include "net/socket/client_socket_handle.h" |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 HttpProxyClientSocket::HttpProxyClientSocket( | 28 HttpProxyClientSocket::HttpProxyClientSocket( |
29 ClientSocketHandle* transport_socket, | 29 ClientSocketHandle* transport_socket, |
30 const GURL& request_url, | 30 const GURL& request_url, |
31 const std::string& user_agent, | 31 const std::string& user_agent, |
32 const HostPortPair& endpoint, | 32 const HostPortPair& endpoint, |
33 const HostPortPair& proxy_server, | 33 const HostPortPair& proxy_server, |
34 HttpAuthCache* http_auth_cache, | 34 HttpAuthController* http_auth_controller, |
35 HttpAuthHandlerFactory* http_auth_handler_factory, | |
36 bool tunnel, | 35 bool tunnel, |
37 bool using_spdy, | 36 bool using_spdy, |
38 SSLClientSocket::NextProto protocol_negotiated, | 37 SSLClientSocket::NextProto protocol_negotiated, |
39 bool is_https_proxy) | 38 bool is_https_proxy) |
40 : ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | 39 : ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( |
41 base::Bind(&HttpProxyClientSocket::OnIOComplete, | 40 base::Bind(&HttpProxyClientSocket::OnIOComplete, |
42 base::Unretained(this)))), | 41 base::Unretained(this)))), |
43 next_state_(STATE_NONE), | 42 next_state_(STATE_NONE), |
44 transport_(transport_socket), | 43 transport_(transport_socket), |
45 endpoint_(endpoint), | 44 endpoint_(endpoint), |
46 auth_(tunnel ? | 45 auth_(http_auth_controller), |
47 new HttpAuthController(HttpAuth::AUTH_PROXY, | |
48 GURL((is_https_proxy ? "https://" : "http://") | |
49 + proxy_server.ToString()), | |
50 http_auth_cache, | |
51 http_auth_handler_factory) | |
52 : NULL), | |
53 tunnel_(tunnel), | 46 tunnel_(tunnel), |
54 using_spdy_(using_spdy), | 47 using_spdy_(using_spdy), |
55 protocol_negotiated_(protocol_negotiated), | 48 protocol_negotiated_(protocol_negotiated), |
56 is_https_proxy_(is_https_proxy), | 49 is_https_proxy_(is_https_proxy), |
57 net_log_(transport_socket->socket()->NetLog()) { | 50 net_log_(transport_socket->socket()->NetLog()) { |
58 // Synthesize the bits of a request that we actually use. | 51 // Synthesize the bits of a request that we actually use. |
59 request_.url = request_url; | 52 request_.url = request_url; |
60 request_.method = "GET"; | 53 request_.method = "GET"; |
61 if (!user_agent.empty()) | 54 if (!user_agent.empty()) |
62 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 55 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
63 user_agent); | 56 user_agent); |
64 } | 57 } |
65 | 58 |
66 HttpProxyClientSocket::~HttpProxyClientSocket() { | 59 HttpProxyClientSocket::~HttpProxyClientSocket() { |
67 Disconnect(); | 60 Disconnect(); |
68 } | 61 } |
69 | 62 |
| 63 const |
| 64 scoped_refptr<HttpAuthController>& HttpProxyClientSocket::GetAuthController() { |
| 65 return auth_; |
| 66 } |
| 67 |
70 int HttpProxyClientSocket::RestartWithAuth(const CompletionCallback& callback) { | 68 int HttpProxyClientSocket::RestartWithAuth(const CompletionCallback& callback) { |
71 DCHECK_EQ(STATE_NONE, next_state_); | 69 DCHECK_EQ(STATE_NONE, next_state_); |
72 DCHECK(user_callback_.is_null()); | 70 DCHECK(user_callback_.is_null()); |
73 | 71 |
74 int rv = PrepareForAuthRestart(); | 72 int rv = PrepareForAuthRestart(); |
75 if (rv != OK) | 73 if (rv != OK) |
76 return rv; | 74 return rv; |
77 | 75 |
78 rv = DoLoop(OK); | 76 rv = DoLoop(OK); |
79 if (rv == ERR_IO_PENDING) { | 77 if (rv == ERR_IO_PENDING) { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 } | 245 } |
248 keep_alive = true; | 246 keep_alive = true; |
249 } | 247 } |
250 | 248 |
251 // We don't need to drain the response body, so we act as if we had drained | 249 // We don't need to drain the response body, so we act as if we had drained |
252 // the response body. | 250 // the response body. |
253 return DidDrainBodyForAuthRestart(keep_alive); | 251 return DidDrainBodyForAuthRestart(keep_alive); |
254 } | 252 } |
255 | 253 |
256 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { | 254 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { |
| 255 int rv = OK; |
257 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { | 256 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { |
258 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 257 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
259 transport_->set_is_reused(true); | 258 transport_->set_is_reused(true); |
260 } else { | 259 } else { |
261 // This assumes that the underlying transport socket is a TCP socket, | 260 next_state_ = STATE_NONE; |
262 // since only TCP sockets are restartable. | |
263 next_state_ = STATE_TCP_RESTART; | |
264 transport_->socket()->Disconnect(); | 261 transport_->socket()->Disconnect(); |
| 262 rv = ERR_NO_KEEP_ALIVE_ON_AUTH_RESTART; |
265 } | 263 } |
266 | 264 |
267 // Reset the other member variables. | 265 // Reset the other member variables. |
268 drain_buf_ = NULL; | 266 drain_buf_ = NULL; |
269 parser_buf_ = NULL; | 267 parser_buf_ = NULL; |
270 http_stream_parser_.reset(); | 268 http_stream_parser_.reset(); |
271 request_line_.clear(); | 269 request_line_.clear(); |
272 request_headers_.Clear(); | 270 request_headers_.Clear(); |
273 response_ = HttpResponseInfo(); | 271 response_ = HttpResponseInfo(); |
274 return OK; | |
275 } | |
276 | |
277 int HttpProxyClientSocket::HandleAuthChallenge() { | |
278 DCHECK(response_.headers); | |
279 | |
280 int rv = auth_->HandleAuthChallenge(response_.headers, false, true, net_log_); | |
281 response_.auth_challenge = auth_->auth_info(); | |
282 if (rv == OK) | |
283 return ERR_PROXY_AUTH_REQUESTED; | |
284 | |
285 return rv; | 272 return rv; |
286 } | 273 } |
287 | 274 |
288 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { | 275 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { |
289 LOG(WARNING) << "Blocked proxy response with status " << response_code | 276 LOG(WARNING) << "Blocked proxy response with status " << response_code |
290 << " to CONNECT request for " | 277 << " to CONNECT request for " |
291 << GetHostAndPort(request_.url) << "."; | 278 << GetHostAndPort(request_.url) << "."; |
292 } | 279 } |
293 | 280 |
294 void HttpProxyClientSocket::DoCallback(int result) { | 281 void HttpProxyClientSocket::DoCallback(int result) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 net_log_.EndEventWithNetErrorCode( | 334 net_log_.EndEventWithNetErrorCode( |
348 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); | 335 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); |
349 break; | 336 break; |
350 case STATE_DRAIN_BODY: | 337 case STATE_DRAIN_BODY: |
351 DCHECK_EQ(OK, rv); | 338 DCHECK_EQ(OK, rv); |
352 rv = DoDrainBody(); | 339 rv = DoDrainBody(); |
353 break; | 340 break; |
354 case STATE_DRAIN_BODY_COMPLETE: | 341 case STATE_DRAIN_BODY_COMPLETE: |
355 rv = DoDrainBodyComplete(rv); | 342 rv = DoDrainBodyComplete(rv); |
356 break; | 343 break; |
357 case STATE_TCP_RESTART: | |
358 DCHECK_EQ(OK, rv); | |
359 rv = DoTCPRestart(); | |
360 break; | |
361 case STATE_TCP_RESTART_COMPLETE: | |
362 rv = DoTCPRestartComplete(rv); | |
363 break; | |
364 case STATE_DONE: | 344 case STATE_DONE: |
365 break; | 345 break; |
366 default: | 346 default: |
367 NOTREACHED() << "bad state"; | 347 NOTREACHED() << "bad state"; |
368 rv = ERR_UNEXPECTED; | 348 rv = ERR_UNEXPECTED; |
369 break; | 349 break; |
370 } | 350 } |
371 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && | 351 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && |
372 next_state_ != STATE_DONE); | 352 next_state_ != STATE_DONE); |
373 return rv; | 353 return rv; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 // need to be very suspicious about the response because an active network | 432 // need to be very suspicious about the response because an active network |
453 // attacker can force us into this state by masquerading as the proxy. | 433 // attacker can force us into this state by masquerading as the proxy. |
454 // The only safe thing to do here is to fail the connection because our | 434 // The only safe thing to do here is to fail the connection because our |
455 // client is expecting an SSL protected response. | 435 // client is expecting an SSL protected response. |
456 // See http://crbug.com/7338. | 436 // See http://crbug.com/7338. |
457 case 407: // Proxy Authentication Required | 437 case 407: // Proxy Authentication Required |
458 // We need this status code to allow proxy authentication. Our | 438 // We need this status code to allow proxy authentication. Our |
459 // authentication code is smart enough to avoid being tricked by an | 439 // authentication code is smart enough to avoid being tricked by an |
460 // active network attacker. | 440 // active network attacker. |
461 // The next state is intentionally not set as it should be STATE_NONE; | 441 // The next state is intentionally not set as it should be STATE_NONE; |
462 return HandleAuthChallenge(); | 442 return HandleAuthChallenge(auth_, &response_, net_log_); |
463 | 443 |
464 default: | 444 default: |
465 if (is_https_proxy_) | 445 if (is_https_proxy_) |
466 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE; | 446 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE; |
467 // For all other status codes, we conservatively fail the CONNECT | 447 // For all other status codes, we conservatively fail the CONNECT |
468 // request. | 448 // request. |
469 // We lose something by doing this. We have seen proxy 403, 404, and | 449 // We lose something by doing this. We have seen proxy 403, 404, and |
470 // 501 response bodies that contain a useful error message. For | 450 // 501 response bodies that contain a useful error message. For |
471 // example, Squid uses a 404 response to report the DNS error: "The | 451 // example, Squid uses a 404 response to report the DNS error: "The |
472 // domain name does not exist." | 452 // domain name does not exist." |
(...skipping 15 matching lines...) Expand all Loading... |
488 return result; | 468 return result; |
489 | 469 |
490 if (http_stream_parser_->IsResponseBodyComplete()) | 470 if (http_stream_parser_->IsResponseBodyComplete()) |
491 return DidDrainBodyForAuthRestart(true); | 471 return DidDrainBodyForAuthRestart(true); |
492 | 472 |
493 // Keep draining. | 473 // Keep draining. |
494 next_state_ = STATE_DRAIN_BODY; | 474 next_state_ = STATE_DRAIN_BODY; |
495 return OK; | 475 return OK; |
496 } | 476 } |
497 | 477 |
498 int HttpProxyClientSocket::DoTCPRestart() { | |
499 next_state_ = STATE_TCP_RESTART_COMPLETE; | |
500 return transport_->socket()->Connect( | |
501 base::Bind(&HttpProxyClientSocket::OnIOComplete, base::Unretained(this))); | |
502 } | |
503 | |
504 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { | |
505 if (result != OK) | |
506 return result; | |
507 | |
508 next_state_ = STATE_GENERATE_AUTH_TOKEN; | |
509 return result; | |
510 } | |
511 | |
512 } // namespace net | 478 } // namespace net |
OLD | NEW |