| 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/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "net/base/auth.h" | 10 #include "net/base/auth.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 HttpProxyClientSocket::~HttpProxyClientSocket() { | 62 HttpProxyClientSocket::~HttpProxyClientSocket() { |
| 63 Disconnect(); | 63 Disconnect(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 int HttpProxyClientSocket::RestartWithAuth(OldCompletionCallback* callback) { | 66 int HttpProxyClientSocket::RestartWithAuth(OldCompletionCallback* callback) { |
| 67 DCHECK_EQ(STATE_NONE, next_state_); | 67 DCHECK_EQ(STATE_NONE, next_state_); |
| 68 DCHECK(!user_callback_); | 68 DCHECK(!user_callback_); |
| 69 | 69 |
| 70 int rv = PrepareForAuthRestart(); | 70 int rv = PrepareForAuthRestart(); |
| 71 if (rv != OK || next_state_ == STATE_NONE) | 71 if (rv != OK) |
| 72 return rv; | 72 return rv; |
| 73 | 73 |
| 74 rv = DoLoop(OK); | 74 rv = DoLoop(OK); |
| 75 if (rv == ERR_IO_PENDING) | 75 if (rv == ERR_IO_PENDING) |
| 76 user_callback_ = callback; | 76 user_callback_ = callback; |
| 77 return rv; | 77 return rv; |
| 78 } | 78 } |
| 79 | 79 |
| 80 const | |
| 81 scoped_refptr<HttpAuthController>& HttpProxyClientSocket::auth_controller() { | |
| 82 return auth_; | |
| 83 } | |
| 84 | |
| 85 const HttpResponseInfo* HttpProxyClientSocket::GetConnectResponseInfo() const { | 80 const HttpResponseInfo* HttpProxyClientSocket::GetConnectResponseInfo() const { |
| 86 return response_.headers ? &response_ : NULL; | 81 return response_.headers ? &response_ : NULL; |
| 87 } | 82 } |
| 88 | 83 |
| 89 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { | 84 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { |
| 90 return new HttpBasicStream(transport_.release(), | 85 return new HttpBasicStream(transport_.release(), |
| 91 http_stream_parser_.release(), false); | 86 http_stream_parser_.release(), false); |
| 92 } | 87 } |
| 93 | 88 |
| 94 | 89 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 // We don't need to drain the response body, so we act as if we had drained | 244 // We don't need to drain the response body, so we act as if we had drained |
| 250 // the response body. | 245 // the response body. |
| 251 return DidDrainBodyForAuthRestart(keep_alive); | 246 return DidDrainBodyForAuthRestart(keep_alive); |
| 252 } | 247 } |
| 253 | 248 |
| 254 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { | 249 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { |
| 255 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { | 250 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { |
| 256 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 251 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 257 transport_->set_is_reused(true); | 252 transport_->set_is_reused(true); |
| 258 } else { | 253 } else { |
| 259 next_state_ = STATE_NONE; | 254 // This assumes that the underlying transport socket is a TCP socket, |
| 255 // since only TCP sockets are restartable. |
| 256 next_state_ = STATE_TCP_RESTART; |
| 257 transport_->socket()->Disconnect(); |
| 260 } | 258 } |
| 261 | 259 |
| 262 // Reset the other member variables. | 260 // Reset the other member variables. |
| 263 drain_buf_ = NULL; | 261 drain_buf_ = NULL; |
| 264 parser_buf_ = NULL; | 262 parser_buf_ = NULL; |
| 265 http_stream_parser_.reset(); | 263 http_stream_parser_.reset(); |
| 266 request_line_.clear(); | 264 request_line_.clear(); |
| 267 request_headers_.Clear(); | 265 request_headers_.Clear(); |
| 268 response_ = HttpResponseInfo(); | 266 response_ = HttpResponseInfo(); |
| 269 return OK; | 267 return OK; |
| 270 } | 268 } |
| 271 | 269 |
| 270 int HttpProxyClientSocket::HandleAuthChallenge() { |
| 271 DCHECK(response_.headers); |
| 272 |
| 273 int rv = auth_->HandleAuthChallenge(response_.headers, false, true, net_log_); |
| 274 response_.auth_challenge = auth_->auth_info(); |
| 275 if (rv == OK) |
| 276 return ERR_PROXY_AUTH_REQUESTED; |
| 277 |
| 278 return rv; |
| 279 } |
| 280 |
| 272 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { | 281 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { |
| 273 LOG(WARNING) << "Blocked proxy response with status " << response_code | 282 LOG(WARNING) << "Blocked proxy response with status " << response_code |
| 274 << " to CONNECT request for " | 283 << " to CONNECT request for " |
| 275 << GetHostAndPort(request_.url) << "."; | 284 << GetHostAndPort(request_.url) << "."; |
| 276 } | 285 } |
| 277 | 286 |
| 278 void HttpProxyClientSocket::DoCallback(int result) { | 287 void HttpProxyClientSocket::DoCallback(int result) { |
| 279 DCHECK_NE(ERR_IO_PENDING, result); | 288 DCHECK_NE(ERR_IO_PENDING, result); |
| 280 DCHECK(user_callback_); | 289 DCHECK(user_callback_); |
| 281 | 290 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 net_log_.EndEventWithNetErrorCode( | 340 net_log_.EndEventWithNetErrorCode( |
| 332 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); | 341 NetLog::TYPE_HTTP_TRANSACTION_TUNNEL_READ_HEADERS, rv); |
| 333 break; | 342 break; |
| 334 case STATE_DRAIN_BODY: | 343 case STATE_DRAIN_BODY: |
| 335 DCHECK_EQ(OK, rv); | 344 DCHECK_EQ(OK, rv); |
| 336 rv = DoDrainBody(); | 345 rv = DoDrainBody(); |
| 337 break; | 346 break; |
| 338 case STATE_DRAIN_BODY_COMPLETE: | 347 case STATE_DRAIN_BODY_COMPLETE: |
| 339 rv = DoDrainBodyComplete(rv); | 348 rv = DoDrainBodyComplete(rv); |
| 340 break; | 349 break; |
| 350 case STATE_TCP_RESTART: |
| 351 DCHECK_EQ(OK, rv); |
| 352 rv = DoTCPRestart(); |
| 353 break; |
| 354 case STATE_TCP_RESTART_COMPLETE: |
| 355 rv = DoTCPRestartComplete(rv); |
| 356 break; |
| 341 case STATE_DONE: | 357 case STATE_DONE: |
| 342 break; | 358 break; |
| 343 default: | 359 default: |
| 344 NOTREACHED() << "bad state"; | 360 NOTREACHED() << "bad state"; |
| 345 rv = ERR_UNEXPECTED; | 361 rv = ERR_UNEXPECTED; |
| 346 break; | 362 break; |
| 347 } | 363 } |
| 348 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && | 364 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && |
| 349 next_state_ != STATE_DONE); | 365 next_state_ != STATE_DONE); |
| 350 return rv; | 366 return rv; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 // need to be very suspicious about the response because an active network | 445 // need to be very suspicious about the response because an active network |
| 430 // attacker can force us into this state by masquerading as the proxy. | 446 // attacker can force us into this state by masquerading as the proxy. |
| 431 // The only safe thing to do here is to fail the connection because our | 447 // The only safe thing to do here is to fail the connection because our |
| 432 // client is expecting an SSL protected response. | 448 // client is expecting an SSL protected response. |
| 433 // See http://crbug.com/7338. | 449 // See http://crbug.com/7338. |
| 434 case 407: // Proxy Authentication Required | 450 case 407: // Proxy Authentication Required |
| 435 // We need this status code to allow proxy authentication. Our | 451 // We need this status code to allow proxy authentication. Our |
| 436 // authentication code is smart enough to avoid being tricked by an | 452 // authentication code is smart enough to avoid being tricked by an |
| 437 // active network attacker. | 453 // active network attacker. |
| 438 // The next state is intentionally not set as it should be STATE_NONE; | 454 // The next state is intentionally not set as it should be STATE_NONE; |
| 439 return HandleAuthChallenge(auth_, &response_, net_log_); | 455 return HandleAuthChallenge(); |
| 440 | 456 |
| 441 default: | 457 default: |
| 442 if (is_https_proxy_) | 458 if (is_https_proxy_) |
| 443 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE; | 459 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE; |
| 444 // For all other status codes, we conservatively fail the CONNECT | 460 // For all other status codes, we conservatively fail the CONNECT |
| 445 // request. | 461 // request. |
| 446 // We lose something by doing this. We have seen proxy 403, 404, and | 462 // We lose something by doing this. We have seen proxy 403, 404, and |
| 447 // 501 response bodies that contain a useful error message. For | 463 // 501 response bodies that contain a useful error message. For |
| 448 // example, Squid uses a 404 response to report the DNS error: "The | 464 // example, Squid uses a 404 response to report the DNS error: "The |
| 449 // domain name does not exist." | 465 // domain name does not exist." |
| (...skipping 15 matching lines...) Expand all Loading... |
| 465 return result; | 481 return result; |
| 466 | 482 |
| 467 if (http_stream_parser_->IsResponseBodyComplete()) | 483 if (http_stream_parser_->IsResponseBodyComplete()) |
| 468 return DidDrainBodyForAuthRestart(true); | 484 return DidDrainBodyForAuthRestart(true); |
| 469 | 485 |
| 470 // Keep draining. | 486 // Keep draining. |
| 471 next_state_ = STATE_DRAIN_BODY; | 487 next_state_ = STATE_DRAIN_BODY; |
| 472 return OK; | 488 return OK; |
| 473 } | 489 } |
| 474 | 490 |
| 491 int HttpProxyClientSocket::DoTCPRestart() { |
| 492 next_state_ = STATE_TCP_RESTART_COMPLETE; |
| 493 return transport_->socket()->Connect(&io_callback_); |
| 494 } |
| 495 |
| 496 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { |
| 497 if (result != OK) |
| 498 return result; |
| 499 |
| 500 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 501 return result; |
| 502 } |
| 503 |
| 475 } // namespace net | 504 } // namespace net |
| OLD | NEW |