| 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" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 8 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 9 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 10 #include "net/base/auth.h" | 12 #include "net/base/auth.h" |
| 11 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
| 12 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
| 14 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 15 #include "net/http/http_basic_stream.h" | 17 #include "net/http/http_basic_stream.h" |
| 16 #include "net/http/http_net_log_params.h" | 18 #include "net/http/http_net_log_params.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 27 ClientSocketHandle* transport_socket, | 29 ClientSocketHandle* transport_socket, |
| 28 const GURL& request_url, | 30 const GURL& request_url, |
| 29 const std::string& user_agent, | 31 const std::string& user_agent, |
| 30 const HostPortPair& endpoint, | 32 const HostPortPair& endpoint, |
| 31 const HostPortPair& proxy_server, | 33 const HostPortPair& proxy_server, |
| 32 HttpAuthCache* http_auth_cache, | 34 HttpAuthCache* http_auth_cache, |
| 33 HttpAuthHandlerFactory* http_auth_handler_factory, | 35 HttpAuthHandlerFactory* http_auth_handler_factory, |
| 34 bool tunnel, | 36 bool tunnel, |
| 35 bool using_spdy, | 37 bool using_spdy, |
| 36 bool is_https_proxy) | 38 bool is_https_proxy) |
| 37 : ALLOW_THIS_IN_INITIALIZER_LIST( | 39 : ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( |
| 38 io_callback_(this, &HttpProxyClientSocket::OnIOComplete)), | 40 base::Bind(&HttpProxyClientSocket::OnIOComplete, |
| 41 base::Unretained(this)))), |
| 39 next_state_(STATE_NONE), | 42 next_state_(STATE_NONE), |
| 40 transport_(transport_socket), | 43 transport_(transport_socket), |
| 41 endpoint_(endpoint), | 44 endpoint_(endpoint), |
| 42 auth_(tunnel ? | 45 auth_(tunnel ? |
| 43 new HttpAuthController(HttpAuth::AUTH_PROXY, | 46 new HttpAuthController(HttpAuth::AUTH_PROXY, |
| 44 GURL((is_https_proxy ? "https://" : "http://") | 47 GURL((is_https_proxy ? "https://" : "http://") |
| 45 + proxy_server.ToString()), | 48 + proxy_server.ToString()), |
| 46 http_auth_cache, | 49 http_auth_cache, |
| 47 http_auth_handler_factory) | 50 http_auth_handler_factory) |
| 48 : NULL), | 51 : NULL), |
| 49 tunnel_(tunnel), | 52 tunnel_(tunnel), |
| 50 using_spdy_(using_spdy), | 53 using_spdy_(using_spdy), |
| 51 is_https_proxy_(is_https_proxy), | 54 is_https_proxy_(is_https_proxy), |
| 52 net_log_(transport_socket->socket()->NetLog()) { | 55 net_log_(transport_socket->socket()->NetLog()) { |
| 53 // Synthesize the bits of a request that we actually use. | 56 // Synthesize the bits of a request that we actually use. |
| 54 request_.url = request_url; | 57 request_.url = request_url; |
| 55 request_.method = "GET"; | 58 request_.method = "GET"; |
| 56 if (!user_agent.empty()) | 59 if (!user_agent.empty()) |
| 57 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 60 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 58 user_agent); | 61 user_agent); |
| 59 } | 62 } |
| 60 | 63 |
| 61 HttpProxyClientSocket::~HttpProxyClientSocket() { | 64 HttpProxyClientSocket::~HttpProxyClientSocket() { |
| 62 Disconnect(); | 65 Disconnect(); |
| 63 } | 66 } |
| 64 | 67 |
| 65 int HttpProxyClientSocket::RestartWithAuth(OldCompletionCallback* callback) { | 68 int HttpProxyClientSocket::RestartWithAuth(const CompletionCallback& callback) { |
| 66 DCHECK_EQ(STATE_NONE, next_state_); | 69 DCHECK_EQ(STATE_NONE, next_state_); |
| 67 DCHECK(user_callback_.is_null()); | 70 DCHECK(user_callback_.is_null()); |
| 68 | 71 |
| 69 int rv = PrepareForAuthRestart(); | 72 int rv = PrepareForAuthRestart(); |
| 70 if (rv != OK) | 73 if (rv != OK) |
| 71 return rv; | 74 return rv; |
| 72 | 75 |
| 73 rv = DoLoop(OK); | 76 rv = DoLoop(OK); |
| 74 if (rv == ERR_IO_PENDING) | 77 if (rv == ERR_IO_PENDING) { |
| 75 if (callback) { | 78 if (!callback.is_null()) |
| 76 user_callback_ = base::Bind(&OldCompletionCallback::Run<int>, | 79 user_callback_ = callback; |
| 77 base::Unretained(callback)); | 80 } |
| 78 } | 81 |
| 79 return rv; | 82 return rv; |
| 80 } | 83 } |
| 81 | 84 |
| 82 const HttpResponseInfo* HttpProxyClientSocket::GetConnectResponseInfo() const { | 85 const HttpResponseInfo* HttpProxyClientSocket::GetConnectResponseInfo() const { |
| 83 return response_.headers ? &response_ : NULL; | 86 return response_.headers ? &response_ : NULL; |
| 84 } | 87 } |
| 85 | 88 |
| 86 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { | 89 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { |
| 87 return new HttpBasicStream(transport_.release(), | 90 return new HttpBasicStream(transport_.release(), |
| 88 http_stream_parser_.release(), false); | 91 http_stream_parser_.release(), false); |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 rv = ERR_UNEXPECTED; | 366 rv = ERR_UNEXPECTED; |
| 364 break; | 367 break; |
| 365 } | 368 } |
| 366 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && | 369 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE && |
| 367 next_state_ != STATE_DONE); | 370 next_state_ != STATE_DONE); |
| 368 return rv; | 371 return rv; |
| 369 } | 372 } |
| 370 | 373 |
| 371 int HttpProxyClientSocket::DoGenerateAuthToken() { | 374 int HttpProxyClientSocket::DoGenerateAuthToken() { |
| 372 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; | 375 next_state_ = STATE_GENERATE_AUTH_TOKEN_COMPLETE; |
| 373 return auth_->MaybeGenerateAuthToken(&request_, &io_callback_, net_log_); | 376 return auth_->MaybeGenerateAuthToken(&request_, io_callback_, net_log_); |
| 374 } | 377 } |
| 375 | 378 |
| 376 int HttpProxyClientSocket::DoGenerateAuthTokenComplete(int result) { | 379 int HttpProxyClientSocket::DoGenerateAuthTokenComplete(int result) { |
| 377 DCHECK_NE(ERR_IO_PENDING, result); | 380 DCHECK_NE(ERR_IO_PENDING, result); |
| 378 if (result == OK) | 381 if (result == OK) |
| 379 next_state_ = STATE_SEND_REQUEST; | 382 next_state_ = STATE_SEND_REQUEST; |
| 380 return result; | 383 return result; |
| 381 } | 384 } |
| 382 | 385 |
| 383 int HttpProxyClientSocket::DoSendRequest() { | 386 int HttpProxyClientSocket::DoSendRequest() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 397 NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, | 400 NetLog::TYPE_HTTP_TRANSACTION_SEND_TUNNEL_HEADERS, |
| 398 make_scoped_refptr(new NetLogHttpRequestParameter( | 401 make_scoped_refptr(new NetLogHttpRequestParameter( |
| 399 request_line_, request_headers_))); | 402 request_line_, request_headers_))); |
| 400 } | 403 } |
| 401 } | 404 } |
| 402 | 405 |
| 403 parser_buf_ = new GrowableIOBuffer(); | 406 parser_buf_ = new GrowableIOBuffer(); |
| 404 http_stream_parser_.reset( | 407 http_stream_parser_.reset( |
| 405 new HttpStreamParser(transport_.get(), &request_, parser_buf_, net_log_)); | 408 new HttpStreamParser(transport_.get(), &request_, parser_buf_, net_log_)); |
| 406 return http_stream_parser_->SendRequest(request_line_, request_headers_, NULL, | 409 return http_stream_parser_->SendRequest(request_line_, request_headers_, NULL, |
| 407 &response_, &io_callback_); | 410 &response_, io_callback_); |
| 408 } | 411 } |
| 409 | 412 |
| 410 int HttpProxyClientSocket::DoSendRequestComplete(int result) { | 413 int HttpProxyClientSocket::DoSendRequestComplete(int result) { |
| 411 if (result < 0) | 414 if (result < 0) |
| 412 return result; | 415 return result; |
| 413 | 416 |
| 414 next_state_ = STATE_READ_HEADERS; | 417 next_state_ = STATE_READ_HEADERS; |
| 415 return OK; | 418 return OK; |
| 416 } | 419 } |
| 417 | 420 |
| 418 int HttpProxyClientSocket::DoReadHeaders() { | 421 int HttpProxyClientSocket::DoReadHeaders() { |
| 419 next_state_ = STATE_READ_HEADERS_COMPLETE; | 422 next_state_ = STATE_READ_HEADERS_COMPLETE; |
| 420 return http_stream_parser_->ReadResponseHeaders(&io_callback_); | 423 return http_stream_parser_->ReadResponseHeaders(io_callback_); |
| 421 } | 424 } |
| 422 | 425 |
| 423 int HttpProxyClientSocket::DoReadHeadersComplete(int result) { | 426 int HttpProxyClientSocket::DoReadHeadersComplete(int result) { |
| 424 if (result < 0) | 427 if (result < 0) |
| 425 return result; | 428 return result; |
| 426 | 429 |
| 427 // Require the "HTTP/1.x" status line for SSL CONNECT. | 430 // Require the "HTTP/1.x" status line for SSL CONNECT. |
| 428 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) | 431 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) |
| 429 return ERR_TUNNEL_CONNECTION_FAILED; | 432 return ERR_TUNNEL_CONNECTION_FAILED; |
| 430 | 433 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 LogBlockedTunnelResponse(response_.headers->response_code()); | 471 LogBlockedTunnelResponse(response_.headers->response_code()); |
| 469 return ERR_TUNNEL_CONNECTION_FAILED; | 472 return ERR_TUNNEL_CONNECTION_FAILED; |
| 470 } | 473 } |
| 471 } | 474 } |
| 472 | 475 |
| 473 int HttpProxyClientSocket::DoDrainBody() { | 476 int HttpProxyClientSocket::DoDrainBody() { |
| 474 DCHECK(drain_buf_); | 477 DCHECK(drain_buf_); |
| 475 DCHECK(transport_->is_initialized()); | 478 DCHECK(transport_->is_initialized()); |
| 476 next_state_ = STATE_DRAIN_BODY_COMPLETE; | 479 next_state_ = STATE_DRAIN_BODY_COMPLETE; |
| 477 return http_stream_parser_->ReadResponseBody(drain_buf_, kDrainBodyBufferSize, | 480 return http_stream_parser_->ReadResponseBody(drain_buf_, kDrainBodyBufferSize, |
| 478 &io_callback_); | 481 io_callback_); |
| 479 } | 482 } |
| 480 | 483 |
| 481 int HttpProxyClientSocket::DoDrainBodyComplete(int result) { | 484 int HttpProxyClientSocket::DoDrainBodyComplete(int result) { |
| 482 if (result < 0) | 485 if (result < 0) |
| 483 return result; | 486 return result; |
| 484 | 487 |
| 485 if (http_stream_parser_->IsResponseBodyComplete()) | 488 if (http_stream_parser_->IsResponseBodyComplete()) |
| 486 return DidDrainBodyForAuthRestart(true); | 489 return DidDrainBodyForAuthRestart(true); |
| 487 | 490 |
| 488 // Keep draining. | 491 // Keep draining. |
| 489 next_state_ = STATE_DRAIN_BODY; | 492 next_state_ = STATE_DRAIN_BODY; |
| 490 return OK; | 493 return OK; |
| 491 } | 494 } |
| 492 | 495 |
| 493 int HttpProxyClientSocket::DoTCPRestart() { | 496 int HttpProxyClientSocket::DoTCPRestart() { |
| 494 next_state_ = STATE_TCP_RESTART_COMPLETE; | 497 next_state_ = STATE_TCP_RESTART_COMPLETE; |
| 495 return transport_->socket()->Connect( | 498 return transport_->socket()->Connect( |
| 496 base::Bind(&HttpProxyClientSocket::OnIOComplete, base::Unretained(this))); | 499 base::Bind(&HttpProxyClientSocket::OnIOComplete, base::Unretained(this))); |
| 497 } | 500 } |
| 498 | 501 |
| 499 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { | 502 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { |
| 500 if (result != OK) | 503 if (result != OK) |
| 501 return result; | 504 return result; |
| 502 | 505 |
| 503 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 506 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 504 return result; | 507 return result; |
| 505 } | 508 } |
| 506 | 509 |
| 507 } // namespace net | 510 } // namespace net |
| OLD | NEW |