| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 request_.method = "GET"; | 55 request_.method = "GET"; |
| 56 if (!user_agent.empty()) | 56 if (!user_agent.empty()) |
| 57 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, | 57 request_.extra_headers.SetHeader(HttpRequestHeaders::kUserAgent, |
| 58 user_agent); | 58 user_agent); |
| 59 } | 59 } |
| 60 | 60 |
| 61 HttpProxyClientSocket::~HttpProxyClientSocket() { | 61 HttpProxyClientSocket::~HttpProxyClientSocket() { |
| 62 Disconnect(); | 62 Disconnect(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 int HttpProxyClientSocket::RestartWithAuth(CompletionCallback* callback) { |
| 66 DCHECK_EQ(STATE_NONE, next_state_); |
| 67 DCHECK(!user_callback_); |
| 68 |
| 69 int rv = PrepareForAuthRestart(); |
| 70 if (rv != OK) |
| 71 return rv; |
| 72 |
| 73 rv = DoLoop(OK); |
| 74 if (rv == ERR_IO_PENDING) |
| 75 user_callback_ = callback; |
| 76 return rv; |
| 77 } |
| 78 |
| 79 const HttpResponseInfo* HttpProxyClientSocket::GetConnectResponseInfo() const { |
| 80 return response_.headers ? &response_ : NULL; |
| 81 } |
| 82 |
| 65 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { | 83 HttpStream* HttpProxyClientSocket::CreateConnectResponseStream() { |
| 66 return new HttpBasicStream(transport_.release(), | 84 return new HttpBasicStream(transport_.release(), |
| 67 http_stream_parser_.release(), false); | 85 http_stream_parser_.release(), false); |
| 68 } | 86 } |
| 69 | 87 |
| 70 | 88 |
| 71 int HttpProxyClientSocket::Connect(CompletionCallback* callback) { | 89 int HttpProxyClientSocket::Connect(CompletionCallback* callback) { |
| 72 DCHECK(transport_.get()); | 90 DCHECK(transport_.get()); |
| 73 DCHECK(transport_->socket()); | 91 DCHECK(transport_->socket()); |
| 74 DCHECK(!user_callback_); | 92 DCHECK(!user_callback_); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 85 | 103 |
| 86 DCHECK_EQ(STATE_NONE, next_state_); | 104 DCHECK_EQ(STATE_NONE, next_state_); |
| 87 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 105 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 88 | 106 |
| 89 int rv = DoLoop(OK); | 107 int rv = DoLoop(OK); |
| 90 if (rv == ERR_IO_PENDING) | 108 if (rv == ERR_IO_PENDING) |
| 91 user_callback_ = callback; | 109 user_callback_ = callback; |
| 92 return rv; | 110 return rv; |
| 93 } | 111 } |
| 94 | 112 |
| 95 int HttpProxyClientSocket::RestartWithAuth(CompletionCallback* callback) { | |
| 96 DCHECK_EQ(STATE_NONE, next_state_); | |
| 97 DCHECK(!user_callback_); | |
| 98 | |
| 99 int rv = PrepareForAuthRestart(); | |
| 100 if (rv != OK) | |
| 101 return rv; | |
| 102 | |
| 103 rv = DoLoop(OK); | |
| 104 if (rv == ERR_IO_PENDING) | |
| 105 user_callback_ = callback; | |
| 106 return rv; | |
| 107 } | |
| 108 | |
| 109 int HttpProxyClientSocket::PrepareForAuthRestart() { | |
| 110 if (!response_.headers.get()) | |
| 111 return ERR_CONNECTION_RESET; | |
| 112 | |
| 113 bool keep_alive = false; | |
| 114 if (response_.headers->IsKeepAlive() && | |
| 115 http_stream_parser_->CanFindEndOfResponse()) { | |
| 116 if (!http_stream_parser_->IsResponseBodyComplete()) { | |
| 117 next_state_ = STATE_DRAIN_BODY; | |
| 118 drain_buf_ = new IOBuffer(kDrainBodyBufferSize); | |
| 119 return OK; | |
| 120 } | |
| 121 keep_alive = true; | |
| 122 } | |
| 123 | |
| 124 // We don't need to drain the response body, so we act as if we had drained | |
| 125 // the response body. | |
| 126 return DidDrainBodyForAuthRestart(keep_alive); | |
| 127 } | |
| 128 | |
| 129 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { | |
| 130 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { | |
| 131 next_state_ = STATE_GENERATE_AUTH_TOKEN; | |
| 132 transport_->set_is_reused(true); | |
| 133 } else { | |
| 134 // This assumes that the underlying transport socket is a TCP socket, | |
| 135 // since only TCP sockets are restartable. | |
| 136 next_state_ = STATE_TCP_RESTART; | |
| 137 transport_->socket()->Disconnect(); | |
| 138 } | |
| 139 | |
| 140 // Reset the other member variables. | |
| 141 drain_buf_ = NULL; | |
| 142 parser_buf_ = NULL; | |
| 143 http_stream_parser_.reset(); | |
| 144 request_line_.clear(); | |
| 145 request_headers_.Clear(); | |
| 146 response_ = HttpResponseInfo(); | |
| 147 return OK; | |
| 148 } | |
| 149 | |
| 150 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { | |
| 151 LOG(WARNING) << "Blocked proxy response with status " << response_code | |
| 152 << " to CONNECT request for " | |
| 153 << GetHostAndPort(request_.url) << "."; | |
| 154 } | |
| 155 | |
| 156 void HttpProxyClientSocket::Disconnect() { | 113 void HttpProxyClientSocket::Disconnect() { |
| 157 if (transport_.get()) | 114 if (transport_.get()) |
| 158 transport_->socket()->Disconnect(); | 115 transport_->socket()->Disconnect(); |
| 159 | 116 |
| 160 // Reset other states to make sure they aren't mistakenly used later. | 117 // Reset other states to make sure they aren't mistakenly used later. |
| 161 // These are the states initialized by Connect(). | 118 // These are the states initialized by Connect(). |
| 162 next_state_ = STATE_NONE; | 119 next_state_ = STATE_NONE; |
| 163 user_callback_ = NULL; | 120 user_callback_ = NULL; |
| 164 } | 121 } |
| 165 | 122 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 } | 198 } |
| 242 | 199 |
| 243 bool HttpProxyClientSocket::SetSendBufferSize(int32 size) { | 200 bool HttpProxyClientSocket::SetSendBufferSize(int32 size) { |
| 244 return transport_->socket()->SetSendBufferSize(size); | 201 return transport_->socket()->SetSendBufferSize(size); |
| 245 } | 202 } |
| 246 | 203 |
| 247 int HttpProxyClientSocket::GetPeerAddress(AddressList* address) const { | 204 int HttpProxyClientSocket::GetPeerAddress(AddressList* address) const { |
| 248 return transport_->socket()->GetPeerAddress(address); | 205 return transport_->socket()->GetPeerAddress(address); |
| 249 } | 206 } |
| 250 | 207 |
| 208 int HttpProxyClientSocket::PrepareForAuthRestart() { |
| 209 if (!response_.headers.get()) |
| 210 return ERR_CONNECTION_RESET; |
| 211 |
| 212 bool keep_alive = false; |
| 213 if (response_.headers->IsKeepAlive() && |
| 214 http_stream_parser_->CanFindEndOfResponse()) { |
| 215 if (!http_stream_parser_->IsResponseBodyComplete()) { |
| 216 next_state_ = STATE_DRAIN_BODY; |
| 217 drain_buf_ = new IOBuffer(kDrainBodyBufferSize); |
| 218 return OK; |
| 219 } |
| 220 keep_alive = true; |
| 221 } |
| 222 |
| 223 // We don't need to drain the response body, so we act as if we had drained |
| 224 // the response body. |
| 225 return DidDrainBodyForAuthRestart(keep_alive); |
| 226 } |
| 227 |
| 228 int HttpProxyClientSocket::DidDrainBodyForAuthRestart(bool keep_alive) { |
| 229 if (keep_alive && transport_->socket()->IsConnectedAndIdle()) { |
| 230 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 231 transport_->set_is_reused(true); |
| 232 } else { |
| 233 // This assumes that the underlying transport socket is a TCP socket, |
| 234 // since only TCP sockets are restartable. |
| 235 next_state_ = STATE_TCP_RESTART; |
| 236 transport_->socket()->Disconnect(); |
| 237 } |
| 238 |
| 239 // Reset the other member variables. |
| 240 drain_buf_ = NULL; |
| 241 parser_buf_ = NULL; |
| 242 http_stream_parser_.reset(); |
| 243 request_line_.clear(); |
| 244 request_headers_.Clear(); |
| 245 response_ = HttpResponseInfo(); |
| 246 return OK; |
| 247 } |
| 248 |
| 249 int HttpProxyClientSocket::HandleAuthChallenge() { |
| 250 DCHECK(response_.headers); |
| 251 |
| 252 int rv = auth_->HandleAuthChallenge(response_.headers, false, true, net_log_); |
| 253 response_.auth_challenge = auth_->auth_info(); |
| 254 if (rv == OK) |
| 255 return ERR_PROXY_AUTH_REQUESTED; |
| 256 |
| 257 return rv; |
| 258 } |
| 259 |
| 260 void HttpProxyClientSocket::LogBlockedTunnelResponse(int response_code) const { |
| 261 LOG(WARNING) << "Blocked proxy response with status " << response_code |
| 262 << " to CONNECT request for " |
| 263 << GetHostAndPort(request_.url) << "."; |
| 264 } |
| 265 |
| 251 void HttpProxyClientSocket::DoCallback(int result) { | 266 void HttpProxyClientSocket::DoCallback(int result) { |
| 252 DCHECK_NE(ERR_IO_PENDING, result); | 267 DCHECK_NE(ERR_IO_PENDING, result); |
| 253 DCHECK(user_callback_); | 268 DCHECK(user_callback_); |
| 254 | 269 |
| 255 // Since Run() may result in Read being called, | 270 // Since Run() may result in Read being called, |
| 256 // clear user_callback_ up front. | 271 // clear user_callback_ up front. |
| 257 CompletionCallback* c = user_callback_; | 272 CompletionCallback* c = user_callback_; |
| 258 user_callback_ = NULL; | 273 user_callback_ = NULL; |
| 259 c->Run(result); | 274 c->Run(result); |
| 260 } | 275 } |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 } | 473 } |
| 459 | 474 |
| 460 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { | 475 int HttpProxyClientSocket::DoTCPRestartComplete(int result) { |
| 461 if (result != OK) | 476 if (result != OK) |
| 462 return result; | 477 return result; |
| 463 | 478 |
| 464 next_state_ = STATE_GENERATE_AUTH_TOKEN; | 479 next_state_ = STATE_GENERATE_AUTH_TOKEN; |
| 465 return result; | 480 return result; |
| 466 } | 481 } |
| 467 | 482 |
| 468 int HttpProxyClientSocket::HandleAuthChallenge() { | |
| 469 DCHECK(response_.headers); | |
| 470 | |
| 471 int rv = auth_->HandleAuthChallenge(response_.headers, false, true, net_log_); | |
| 472 response_.auth_challenge = auth_->auth_info(); | |
| 473 if (rv == OK) | |
| 474 return ERR_PROXY_AUTH_REQUESTED; | |
| 475 | |
| 476 return rv; | |
| 477 } | |
| 478 | |
| 479 } // namespace net | 483 } // namespace net |
| OLD | NEW |