license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 7 | #include "base/scoped_ptr.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 9 | #include "base/string_util.h" |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 10 | #include "base/trace_event.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 11 | #include "build/build_config.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include "net/base/client_socket_factory.h" |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 13 | #include "net/base/connection_type_histograms.h" |
[email protected] | 8947da6 | 2008-10-24 19:11:10 | [diff] [blame] | 14 | #include "net/base/dns_resolution_observer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | #include "net/base/host_resolver.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 16 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 17 | #include "net/base/load_flags.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 18 | #include "net/base/net_util.h" |
[email protected] | 4628a2a | 2008-08-14 20:33:25 | [diff] [blame] | 19 | #include "net/base/ssl_client_socket.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 20 | #include "net/base/upload_data_stream.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 21 | #include "net/http/http_auth.h" |
| 22 | #include "net/http/http_auth_handler.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 23 | #include "net/http/http_chunked_decoder.h" |
| 24 | #include "net/http/http_network_session.h" |
| 25 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 26 | #include "net/http/http_response_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | #include "net/http/http_util.h" |
| 28 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 29 | using base::Time; |
| 30 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | namespace net { |
| 32 | |
| 33 | //----------------------------------------------------------------------------- |
| 34 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | HttpNetworkTransaction::HttpNetworkTransaction(HttpNetworkSession* session, |
| 36 | ClientSocketFactory* csf) |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 37 | : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 38 | io_callback_(this, &HttpNetworkTransaction::OnIOComplete)), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | user_callback_(NULL), |
| 40 | session_(session), |
| 41 | request_(NULL), |
| 42 | pac_request_(NULL), |
| 43 | socket_factory_(csf), |
[email protected] | e1e0626 | 2008-08-06 23:57:07 | [diff] [blame] | 44 | connection_(session->connection_pool()), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 45 | reused_socket_(false), |
| 46 | using_ssl_(false), |
| 47 | using_proxy_(false), |
| 48 | using_tunnel_(false), |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 49 | establishing_tunnel_(false), |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 50 | request_headers_bytes_sent_(0), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | header_buf_capacity_(0), |
| 52 | header_buf_len_(0), |
| 53 | header_buf_body_offset_(-1), |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 54 | header_buf_http_offset_(-1), |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 55 | response_body_length_(-1), // -1 means unspecified. |
| 56 | response_body_read_(0), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 57 | read_buf_len_(0), |
| 58 | next_state_(STATE_NONE) { |
[email protected] | 2cd713f | 2008-10-21 17:54:28 | [diff] [blame] | 59 | #if defined(OS_WIN) |
| 60 | // TODO(port): Port the SSLConfigService class to Linux and Mac OS X. |
| 61 | session->ssl_config_service()->GetSSLConfig(&ssl_config_); |
| 62 | #endif |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 65 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
| 66 | CompletionCallback* callback) { |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 67 | UpdateConnectionTypeHistograms(CONNECTION_ANY); |
| 68 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 69 | request_ = request_info; |
| 70 | |
| 71 | next_state_ = STATE_RESOLVE_PROXY; |
| 72 | int rv = DoLoop(OK); |
| 73 | if (rv == ERR_IO_PENDING) |
| 74 | user_callback_ = callback; |
| 75 | return rv; |
| 76 | } |
| 77 | |
| 78 | int HttpNetworkTransaction::RestartIgnoringLastError( |
| 79 | CompletionCallback* callback) { |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 80 | // TODO(wtc): If the connection is no longer alive, call |
| 81 | // connection_.socket()->ReconnectIgnoringLastError(). |
| 82 | next_state_ = STATE_WRITE_HEADERS; |
| 83 | int rv = DoLoop(OK); |
| 84 | if (rv == ERR_IO_PENDING) |
| 85 | user_callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 86 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | int HttpNetworkTransaction::RestartWithAuth( |
| 90 | const std::wstring& username, |
| 91 | const std::wstring& password, |
| 92 | CompletionCallback* callback) { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 93 | |
| 94 | DCHECK(NeedAuth(HttpAuth::AUTH_PROXY) || |
| 95 | NeedAuth(HttpAuth::AUTH_SERVER)); |
| 96 | |
| 97 | // Figure out whether this username password is for proxy or server. |
| 98 | // Proxy gets set first, then server. |
| 99 | HttpAuth::Target target = NeedAuth(HttpAuth::AUTH_PROXY) ? |
| 100 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
| 101 | |
| 102 | // Update the username/password. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 103 | auth_identity_[target].source = HttpAuth::IDENT_SRC_EXTERNAL; |
| 104 | auth_identity_[target].invalid = false; |
| 105 | auth_identity_[target].username = username; |
| 106 | auth_identity_[target].password = password; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 107 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 108 | PrepareForAuthRestart(target); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 109 | |
| 110 | DCHECK(user_callback_ == NULL); |
| 111 | int rv = DoLoop(OK); |
| 112 | if (rv == ERR_IO_PENDING) |
| 113 | user_callback_ = callback; |
| 114 | |
| 115 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 118 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 119 | DCHECK(HaveAuth(target)); |
| 120 | DCHECK(auth_identity_[target].source != HttpAuth::IDENT_SRC_PATH_LOOKUP); |
| 121 | |
| 122 | // Add the auth entry to the cache before restarting. We don't know whether |
| 123 | // the identity is valid yet, but if it is valid we want other transactions |
| 124 | // to know about it. If an entry for (origin, handler->realm()) already |
| 125 | // exists, we update it. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 126 | // |
| 127 | // If auth_identity_[target].source is HttpAuth::IDENT_SRC_NONE, |
| 128 | // auth_identity_[target] contains no identity because identity is not |
| 129 | // required yet. |
| 130 | if (auth_identity_[target].source != HttpAuth::IDENT_SRC_NONE) { |
| 131 | session_->auth_cache()->Add(AuthOrigin(target), auth_handler_[target], |
| 132 | auth_identity_[target].username, auth_identity_[target].password, |
| 133 | AuthPath(target)); |
| 134 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 135 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 136 | bool keep_alive = false; |
| 137 | if (response_.headers->IsKeepAlive()) { |
| 138 | // If there is a response body of known length, we need to drain it first. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 139 | if (response_body_length_ > 0 || chunked_decoder_.get()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 140 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 141 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket |
| 142 | read_buf_len_ = kDrainBodyBufferSize; |
| 143 | return; |
| 144 | } |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 145 | if (response_body_length_ == 0) // No response body to drain. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 146 | keep_alive = true; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 147 | // response_body_length_ is -1 and we're not using chunked encoding. We |
| 148 | // don't know the length of the response body, so we can't reuse this |
| 149 | // connection even though the server says it's keep-alive. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 04f40b3 | 2009-03-04 22:18:11 | [diff] [blame] | 152 | // If the auth scheme is connection-based but the proxy/server mistakenly |
| 153 | // marks the connection as not keep-alive, the auth is going to fail, so log |
| 154 | // an error message. |
| 155 | if (!keep_alive && auth_handler_[target]->is_connection_based() && |
| 156 | auth_identity_[target].source != HttpAuth::IDENT_SRC_NONE) { |
| 157 | std::string auth_target(target == HttpAuth::AUTH_PROXY ? |
| 158 | "proxy" : "server"); |
| 159 | LOG(ERROR) << "Can't perform " << auth_handler_[target]->scheme() |
| 160 | << " auth to the " << auth_target << " " |
| 161 | << AuthOrigin(target).spec() |
| 162 | << " over a non-keep-alive connection"; |
| 163 | |
| 164 | HttpVersion http_version = response_.headers->GetHttpVersion(); |
| 165 | LOG(ERROR) << " HTTP version is " << http_version.major_value() << "." |
| 166 | << http_version.minor_value(); |
| 167 | |
| 168 | std::string connection_val; |
| 169 | void* iter = NULL; |
| 170 | while (response_.headers->EnumerateHeader(&iter, "connection", |
| 171 | &connection_val)) { |
| 172 | LOG(ERROR) << " Has header Connection: " << connection_val; |
| 173 | } |
| 174 | |
| 175 | iter = NULL; |
| 176 | while (response_.headers->EnumerateHeader(&iter, "proxy-connection", |
| 177 | &connection_val)) { |
| 178 | LOG(ERROR) << " Has header Proxy-Connection: " << connection_val; |
| 179 | } |
| 180 | } |
| 181 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 182 | // We don't need to drain the response body, so we act as if we had drained |
| 183 | // the response body. |
| 184 | DidDrainBodyForAuthRestart(keep_alive); |
| 185 | } |
| 186 | |
| 187 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
| 188 | if (keep_alive) { |
| 189 | next_state_ = STATE_WRITE_HEADERS; |
| 190 | reused_socket_ = true; |
| 191 | } else { |
| 192 | next_state_ = STATE_INIT_CONNECTION; |
| 193 | connection_.set_socket(NULL); |
| 194 | connection_.Reset(); |
| 195 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 196 | |
| 197 | // Reset the other member variables. |
| 198 | ResetStateForRestart(); |
| 199 | } |
| 200 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 201 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 202 | CompletionCallback* callback) { |
| 203 | DCHECK(response_.headers); |
| 204 | DCHECK(buf); |
| 205 | DCHECK(buf_len > 0); |
| 206 | |
| 207 | if (!connection_.is_initialized()) |
| 208 | return 0; // connection_ has been reset. Treat like EOF. |
| 209 | |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 210 | if (establishing_tunnel_) { |
| 211 | // We're trying to read the body of the response but we're still trying to |
| 212 | // establish an SSL tunnel through the proxy. We can't read these bytes |
| 213 | // when establishing a tunnel because they might be controlled by an active |
| 214 | // network attacker. We don't worry about this for HTTP because an active |
| 215 | // network attacker can already control HTTP sessions. |
| 216 | // We reach this case when the user cancels a 407 proxy auth prompt. |
| 217 | // See http://crbug.com/8473 |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 218 | DCHECK(response_.headers->response_code() == 407); |
| 219 | LogBlockedTunnelResponse(*response_.headers); |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 220 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 221 | } |
| 222 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 223 | read_buf_ = buf; |
| 224 | read_buf_len_ = buf_len; |
| 225 | |
| 226 | next_state_ = STATE_READ_BODY; |
| 227 | int rv = DoLoop(OK); |
| 228 | if (rv == ERR_IO_PENDING) |
| 229 | user_callback_ = callback; |
| 230 | return rv; |
| 231 | } |
| 232 | |
| 233 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
[email protected] | 4628a2a | 2008-08-14 20:33:25 | [diff] [blame] | 234 | return (response_.headers || response_.ssl_info.cert) ? &response_ : NULL; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 238 | // TODO(wtc): Define a new LoadState value for the |
| 239 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 240 | switch (next_state_) { |
| 241 | case STATE_RESOLVE_PROXY_COMPLETE: |
| 242 | return LOAD_STATE_RESOLVING_PROXY_FOR_URL; |
| 243 | case STATE_RESOLVE_HOST_COMPLETE: |
| 244 | return LOAD_STATE_RESOLVING_HOST; |
| 245 | case STATE_CONNECT_COMPLETE: |
| 246 | return LOAD_STATE_CONNECTING; |
| 247 | case STATE_WRITE_HEADERS_COMPLETE: |
| 248 | case STATE_WRITE_BODY_COMPLETE: |
| 249 | return LOAD_STATE_SENDING_REQUEST; |
| 250 | case STATE_READ_HEADERS_COMPLETE: |
| 251 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 252 | case STATE_READ_BODY_COMPLETE: |
| 253 | return LOAD_STATE_READING_RESPONSE; |
| 254 | default: |
| 255 | return LOAD_STATE_IDLE; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | uint64 HttpNetworkTransaction::GetUploadProgress() const { |
| 260 | if (!request_body_stream_.get()) |
| 261 | return 0; |
| 262 | |
| 263 | return request_body_stream_->position(); |
| 264 | } |
| 265 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 266 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
| 267 | // If we still have an open socket, then make sure to close it so we don't |
| 268 | // try to reuse it later on. |
| 269 | if (connection_.is_initialized()) |
| 270 | connection_.set_socket(NULL); |
| 271 | |
| 272 | if (pac_request_) |
| 273 | session_->proxy_service()->CancelPacRequest(pac_request_); |
| 274 | } |
| 275 | |
| 276 | void HttpNetworkTransaction::BuildRequestHeaders() { |
[email protected] | 3130a853 | 2008-09-15 18:37:11 | [diff] [blame] | 277 | // For proxy use the full url. Otherwise just the absolute path. |
| 278 | // This strips out any reference/username/password. |
| 279 | std::string path = using_proxy_ ? |
| 280 | HttpUtil::SpecForRequest(request_->url) : |
| 281 | HttpUtil::PathForRequest(request_->url); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 282 | |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 283 | request_headers_ = request_->method + " " + path + |
| 284 | " HTTP/1.1\r\nHost: " + request_->url.host(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 285 | if (request_->url.IntPort() != -1) |
| 286 | request_headers_ += ":" + request_->url.port(); |
| 287 | request_headers_ += "\r\n"; |
| 288 | |
| 289 | // For compat with HTTP/1.0 servers and proxies: |
| 290 | if (using_proxy_) |
| 291 | request_headers_ += "Proxy-"; |
| 292 | request_headers_ += "Connection: keep-alive\r\n"; |
| 293 | |
| 294 | if (!request_->user_agent.empty()) |
| 295 | request_headers_ += "User-Agent: " + request_->user_agent + "\r\n"; |
| 296 | |
| 297 | // Our consumer should have made sure that this is a safe referrer. See for |
| 298 | // instance WebCore::FrameLoader::HideReferrer. |
| 299 | if (request_->referrer.is_valid()) |
| 300 | request_headers_ += "Referer: " + request_->referrer.spec() + "\r\n"; |
| 301 | |
| 302 | // Add a content length header? |
| 303 | if (request_->upload_data) { |
| 304 | request_body_stream_.reset(new UploadDataStream(request_->upload_data)); |
| 305 | request_headers_ += |
| 306 | "Content-Length: " + Uint64ToString(request_body_stream_->size()) + |
| 307 | "\r\n"; |
| 308 | } else if (request_->method == "POST" || request_->method == "PUT" || |
| 309 | request_->method == "HEAD") { |
| 310 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 311 | // IE and Safari also add a content length header. Presumably it is to |
| 312 | // support sending a HEAD request to an URL that only expects to be sent a |
| 313 | // POST or some other method that normally would have a message body. |
| 314 | request_headers_ += "Content-Length: 0\r\n"; |
| 315 | } |
| 316 | |
| 317 | // Honor load flags that impact proxy caches. |
| 318 | if (request_->load_flags & LOAD_BYPASS_CACHE) { |
| 319 | request_headers_ += "Pragma: no-cache\r\nCache-Control: no-cache\r\n"; |
| 320 | } else if (request_->load_flags & LOAD_VALIDATE_CACHE) { |
| 321 | request_headers_ += "Cache-Control: max-age=0\r\n"; |
| 322 | } |
| 323 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 324 | ApplyAuth(); |
| 325 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 326 | // TODO(darin): Need to prune out duplicate headers. |
| 327 | |
| 328 | request_headers_ += request_->extra_headers; |
| 329 | request_headers_ += "\r\n"; |
| 330 | } |
| 331 | |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 332 | // The HTTP CONNECT method for establishing a tunnel connection is documented |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 333 | // in draft-luotonen-web-proxy-tunneling-01.txt and RFC 2817, Sections 5.2 and |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 334 | // 5.3. |
| 335 | void HttpNetworkTransaction::BuildTunnelRequest() { |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 336 | // RFC 2616 Section 9 says the Host request-header field MUST accompany all |
| 337 | // HTTP/1.1 requests. |
[email protected] | 6d748ec | 2008-10-08 22:11:39 | [diff] [blame] | 338 | request_headers_ = StringPrintf("CONNECT %s:%d HTTP/1.1\r\n", |
| 339 | request_->url.host().c_str(), request_->url.EffectiveIntPort()); |
| 340 | request_headers_ += "Host: " + request_->url.host(); |
| 341 | if (request_->url.has_port()) |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 342 | request_headers_ += ":" + request_->url.port(); |
| 343 | request_headers_ += "\r\n"; |
| 344 | |
| 345 | if (!request_->user_agent.empty()) |
| 346 | request_headers_ += "User-Agent: " + request_->user_agent + "\r\n"; |
| 347 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 348 | ApplyAuth(); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 349 | |
| 350 | request_headers_ += "\r\n"; |
| 351 | } |
| 352 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 353 | void HttpNetworkTransaction::DoCallback(int rv) { |
| 354 | DCHECK(rv != ERR_IO_PENDING); |
| 355 | DCHECK(user_callback_); |
| 356 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 357 | // Since Run may result in Read being called, clear user_callback_ up front. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 358 | CompletionCallback* c = user_callback_; |
| 359 | user_callback_ = NULL; |
| 360 | c->Run(rv); |
| 361 | } |
| 362 | |
| 363 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 364 | int rv = DoLoop(result); |
| 365 | if (rv != ERR_IO_PENDING) |
| 366 | DoCallback(rv); |
| 367 | } |
| 368 | |
| 369 | int HttpNetworkTransaction::DoLoop(int result) { |
| 370 | DCHECK(next_state_ != STATE_NONE); |
| 371 | |
| 372 | int rv = result; |
| 373 | do { |
| 374 | State state = next_state_; |
| 375 | next_state_ = STATE_NONE; |
| 376 | switch (state) { |
| 377 | case STATE_RESOLVE_PROXY: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 378 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 379 | TRACE_EVENT_BEGIN("http.resolve_proxy", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 380 | rv = DoResolveProxy(); |
| 381 | break; |
| 382 | case STATE_RESOLVE_PROXY_COMPLETE: |
| 383 | rv = DoResolveProxyComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 384 | TRACE_EVENT_END("http.resolve_proxy", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 385 | break; |
| 386 | case STATE_INIT_CONNECTION: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 387 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 388 | TRACE_EVENT_BEGIN("http.init_conn", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 389 | rv = DoInitConnection(); |
| 390 | break; |
| 391 | case STATE_INIT_CONNECTION_COMPLETE: |
| 392 | rv = DoInitConnectionComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 393 | TRACE_EVENT_END("http.init_conn", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 394 | break; |
| 395 | case STATE_RESOLVE_HOST: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 396 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 397 | TRACE_EVENT_BEGIN("http.resolve_host", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 398 | rv = DoResolveHost(); |
| 399 | break; |
| 400 | case STATE_RESOLVE_HOST_COMPLETE: |
| 401 | rv = DoResolveHostComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 402 | TRACE_EVENT_END("http.resolve_host", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 403 | break; |
| 404 | case STATE_CONNECT: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 405 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 406 | TRACE_EVENT_BEGIN("http.connect", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 407 | rv = DoConnect(); |
| 408 | break; |
| 409 | case STATE_CONNECT_COMPLETE: |
| 410 | rv = DoConnectComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 411 | TRACE_EVENT_END("http.connect", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 412 | break; |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 413 | case STATE_SSL_CONNECT_OVER_TUNNEL: |
| 414 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 415 | TRACE_EVENT_BEGIN("http.ssl_tunnel", request_, request_->url.spec()); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 416 | rv = DoSSLConnectOverTunnel(); |
| 417 | break; |
| 418 | case STATE_SSL_CONNECT_OVER_TUNNEL_COMPLETE: |
| 419 | rv = DoSSLConnectOverTunnelComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 420 | TRACE_EVENT_END("http.ssl_tunnel", request_, request_->url.spec()); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 421 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 422 | case STATE_WRITE_HEADERS: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 423 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 424 | TRACE_EVENT_BEGIN("http.write_headers", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 425 | rv = DoWriteHeaders(); |
| 426 | break; |
| 427 | case STATE_WRITE_HEADERS_COMPLETE: |
| 428 | rv = DoWriteHeadersComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 429 | TRACE_EVENT_END("http.write_headers", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 430 | break; |
| 431 | case STATE_WRITE_BODY: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 432 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 433 | TRACE_EVENT_BEGIN("http.write_body", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 434 | rv = DoWriteBody(); |
| 435 | break; |
| 436 | case STATE_WRITE_BODY_COMPLETE: |
| 437 | rv = DoWriteBodyComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 438 | TRACE_EVENT_END("http.write_body", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 439 | break; |
| 440 | case STATE_READ_HEADERS: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 441 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 442 | TRACE_EVENT_BEGIN("http.read_headers", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 443 | rv = DoReadHeaders(); |
| 444 | break; |
| 445 | case STATE_READ_HEADERS_COMPLETE: |
| 446 | rv = DoReadHeadersComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 447 | TRACE_EVENT_END("http.read_headers", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 448 | break; |
| 449 | case STATE_READ_BODY: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 450 | DCHECK(rv == OK); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 451 | TRACE_EVENT_BEGIN("http.read_body", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 452 | rv = DoReadBody(); |
| 453 | break; |
| 454 | case STATE_READ_BODY_COMPLETE: |
| 455 | rv = DoReadBodyComplete(rv); |
[email protected] | 113ab13 | 2008-09-18 20:42:55 | [diff] [blame] | 456 | TRACE_EVENT_END("http.read_body", request_, request_->url.spec()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 457 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 458 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
| 459 | DCHECK(rv == OK); |
| 460 | TRACE_EVENT_BEGIN("http.drain_body_for_auth_restart", |
| 461 | request_, request_->url.spec()); |
| 462 | rv = DoDrainBodyForAuthRestart(); |
| 463 | break; |
| 464 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 465 | rv = DoDrainBodyForAuthRestartComplete(rv); |
| 466 | TRACE_EVENT_END("http.drain_body_for_auth_restart", |
| 467 | request_, request_->url.spec()); |
| 468 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 469 | default: |
| 470 | NOTREACHED() << "bad state"; |
| 471 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 472 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 473 | } |
| 474 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 475 | |
| 476 | return rv; |
| 477 | } |
| 478 | |
| 479 | int HttpNetworkTransaction::DoResolveProxy() { |
| 480 | DCHECK(!pac_request_); |
| 481 | |
| 482 | next_state_ = STATE_RESOLVE_PROXY_COMPLETE; |
| 483 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 484 | if (request_->load_flags & LOAD_BYPASS_PROXY) { |
| 485 | proxy_info_.UseDirect(); |
| 486 | return OK; |
| 487 | } |
| 488 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 489 | return session_->proxy_service()->ResolveProxy( |
| 490 | request_->url, &proxy_info_, &io_callback_, &pac_request_); |
| 491 | } |
| 492 | |
| 493 | int HttpNetworkTransaction::DoResolveProxyComplete(int result) { |
| 494 | next_state_ = STATE_INIT_CONNECTION; |
| 495 | |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 496 | // Since we only support HTTP proxies or DIRECT connections, remove |
| 497 | // any other type of proxy from the list (i.e. SOCKS). |
| 498 | // Supporting SOCKS is issue http://crbug.com/469. |
| 499 | proxy_info_.RemoveProxiesWithoutScheme( |
| 500 | ProxyServer::SCHEME_DIRECT | ProxyServer::SCHEME_HTTP); |
| 501 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 502 | pac_request_ = NULL; |
| 503 | |
| 504 | if (result != OK) { |
| 505 | DLOG(ERROR) << "Failed to resolve proxy: " << result; |
| 506 | proxy_info_.UseDirect(); |
| 507 | } |
| 508 | return OK; |
| 509 | } |
| 510 | |
| 511 | int HttpNetworkTransaction::DoInitConnection() { |
| 512 | DCHECK(!connection_.is_initialized()); |
| 513 | |
| 514 | next_state_ = STATE_INIT_CONNECTION_COMPLETE; |
| 515 | |
| 516 | using_ssl_ = request_->url.SchemeIs("https"); |
| 517 | using_proxy_ = !proxy_info_.is_direct() && !using_ssl_; |
| 518 | using_tunnel_ = !proxy_info_.is_direct() && using_ssl_; |
| 519 | |
| 520 | // Build the string used to uniquely identify connections of this type. |
| 521 | std::string connection_group; |
| 522 | if (using_proxy_ || using_tunnel_) |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 523 | connection_group = "proxy/" + proxy_info_.proxy_server().ToURI() + "/"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 524 | if (!using_proxy_) |
| 525 | connection_group.append(request_->url.GetOrigin().spec()); |
| 526 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 527 | DCHECK(!connection_group.empty()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 528 | return connection_.Init(connection_group, &io_callback_); |
| 529 | } |
| 530 | |
| 531 | int HttpNetworkTransaction::DoInitConnectionComplete(int result) { |
| 532 | if (result < 0) |
| 533 | return result; |
| 534 | |
| 535 | DCHECK(connection_.is_initialized()); |
| 536 | |
| 537 | // Set the reused_socket_ flag to indicate that we are using a keep-alive |
| 538 | // connection. This flag is used to handle errors that occur while we are |
| 539 | // trying to reuse a keep-alive connection. |
[email protected] | 049d4ee | 2008-10-23 21:42:07 | [diff] [blame] | 540 | reused_socket_ = (connection_.socket() != NULL); |
| 541 | if (reused_socket_) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 542 | next_state_ = STATE_WRITE_HEADERS; |
| 543 | } else { |
| 544 | next_state_ = STATE_RESOLVE_HOST; |
| 545 | } |
| 546 | return OK; |
| 547 | } |
| 548 | |
| 549 | int HttpNetworkTransaction::DoResolveHost() { |
| 550 | next_state_ = STATE_RESOLVE_HOST_COMPLETE; |
| 551 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 552 | std::string host; |
| 553 | int port; |
| 554 | |
| 555 | // Determine the host and port to connect to. |
| 556 | if (using_proxy_ || using_tunnel_) { |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 557 | ProxyServer proxy_server = proxy_info_.proxy_server(); |
| 558 | host = proxy_server.host(); |
| 559 | port = proxy_server.port(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 560 | } else { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 561 | // Direct connection |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 562 | host = request_->url.host(); |
[email protected] | 6d748ec | 2008-10-08 22:11:39 | [diff] [blame] | 563 | port = request_->url.EffectiveIntPort(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 564 | } |
| 565 | |
[email protected] | 8947da6 | 2008-10-24 19:11:10 | [diff] [blame] | 566 | DidStartDnsResolution(host, this); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 567 | return resolver_.Resolve(host, port, &addresses_, &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | int HttpNetworkTransaction::DoResolveHostComplete(int result) { |
[email protected] | 8947da6 | 2008-10-24 19:11:10 | [diff] [blame] | 571 | bool ok = (result == OK); |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 572 | DidFinishDnsResolutionWithStatus(ok, request_->referrer, this); |
[email protected] | 8947da6 | 2008-10-24 19:11:10 | [diff] [blame] | 573 | if (ok) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 574 | next_state_ = STATE_CONNECT; |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 575 | } else { |
| 576 | result = ReconsiderProxyAfterError(result); |
| 577 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 578 | return result; |
| 579 | } |
| 580 | |
| 581 | int HttpNetworkTransaction::DoConnect() { |
| 582 | next_state_ = STATE_CONNECT_COMPLETE; |
| 583 | |
| 584 | DCHECK(!connection_.socket()); |
| 585 | |
| 586 | ClientSocket* s = socket_factory_->CreateTCPClientSocket(addresses_); |
| 587 | |
| 588 | // If we are using a direct SSL connection, then go ahead and create the SSL |
| 589 | // wrapper socket now. Otherwise, we need to first issue a CONNECT request. |
| 590 | if (using_ssl_ && !using_tunnel_) |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 591 | s = socket_factory_->CreateSSLClientSocket(s, request_->url.host(), |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 592 | ssl_config_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 593 | |
| 594 | connection_.set_socket(s); |
| 595 | return connection_.socket()->Connect(&io_callback_); |
| 596 | } |
| 597 | |
| 598 | int HttpNetworkTransaction::DoConnectComplete(int result) { |
[email protected] | 771d0c2b | 2008-09-30 00:26:17 | [diff] [blame] | 599 | if (IsCertificateError(result)) |
| 600 | result = HandleCertificateError(result); |
| 601 | |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 602 | if (result == OK) { |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 603 | next_state_ = STATE_WRITE_HEADERS; |
[email protected] | 27161fb | 2008-11-03 23:39:05 | [diff] [blame] | 604 | if (using_tunnel_) |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 605 | establishing_tunnel_ = true; |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 606 | } else { |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 607 | result = HandleSSLHandshakeError(result); |
| 608 | if (result != OK) |
| 609 | result = ReconsiderProxyAfterError(result); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 610 | } |
| 611 | return result; |
| 612 | } |
| 613 | |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 614 | int HttpNetworkTransaction::DoSSLConnectOverTunnel() { |
| 615 | next_state_ = STATE_SSL_CONNECT_OVER_TUNNEL_COMPLETE; |
| 616 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 617 | // Add a SSL socket on top of our existing transport socket. |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 618 | ClientSocket* s = connection_.release_socket(); |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 619 | s = socket_factory_->CreateSSLClientSocket(s, request_->url.host(), |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 620 | ssl_config_); |
[email protected] | c7af8b2 | 2008-08-25 20:41:46 | [diff] [blame] | 621 | connection_.set_socket(s); |
| 622 | return connection_.socket()->Connect(&io_callback_); |
| 623 | } |
| 624 | |
| 625 | int HttpNetworkTransaction::DoSSLConnectOverTunnelComplete(int result) { |
[email protected] | 771d0c2b | 2008-09-30 00:26:17 | [diff] [blame] | 626 | if (IsCertificateError(result)) |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 627 | result = HandleCertificateError(result); |
[email protected] | 771d0c2b | 2008-09-30 00:26:17 | [diff] [blame] | 628 | |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 629 | if (result == OK) { |
[email protected] | 771d0c2b | 2008-09-30 00:26:17 | [diff] [blame] | 630 | next_state_ = STATE_WRITE_HEADERS; |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 631 | } else { |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 632 | result = HandleSSLHandshakeError(result); |
| 633 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 634 | return result; |
| 635 | } |
| 636 | |
| 637 | int HttpNetworkTransaction::DoWriteHeaders() { |
| 638 | next_state_ = STATE_WRITE_HEADERS_COMPLETE; |
| 639 | |
| 640 | // This is constructed lazily (instead of within our Start method), so that |
| 641 | // we have proxy info available. |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 642 | if (request_headers_.empty()) { |
| 643 | if (establishing_tunnel_) { |
| 644 | BuildTunnelRequest(); |
| 645 | } else { |
| 646 | BuildRequestHeaders(); |
| 647 | } |
| 648 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 649 | |
| 650 | // Record our best estimate of the 'request time' as the time when we send |
| 651 | // out the first bytes of the request headers. |
[email protected] | 87a1a95 | 2009-01-13 18:06:03 | [diff] [blame] | 652 | if (request_headers_bytes_sent_ == 0) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 653 | response_.request_time = Time::Now(); |
[email protected] | 87a1a95 | 2009-01-13 18:06:03 | [diff] [blame] | 654 | response_.was_cached = false; |
| 655 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 656 | |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 657 | const char* buf = request_headers_.data() + request_headers_bytes_sent_; |
| 658 | int buf_len = static_cast<int>(request_headers_.size() - |
| 659 | request_headers_bytes_sent_); |
| 660 | DCHECK(buf_len > 0); |
| 661 | |
| 662 | return connection_.socket()->Write(buf, buf_len, &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | int HttpNetworkTransaction::DoWriteHeadersComplete(int result) { |
| 666 | if (result < 0) |
| 667 | return HandleIOError(result); |
| 668 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 669 | request_headers_bytes_sent_ += result; |
| 670 | if (request_headers_bytes_sent_ < request_headers_.size()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 671 | next_state_ = STATE_WRITE_HEADERS; |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 672 | } else if (!establishing_tunnel_ && request_->upload_data) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 673 | next_state_ = STATE_WRITE_BODY; |
| 674 | } else { |
| 675 | next_state_ = STATE_READ_HEADERS; |
| 676 | } |
| 677 | return OK; |
| 678 | } |
| 679 | |
| 680 | int HttpNetworkTransaction::DoWriteBody() { |
| 681 | next_state_ = STATE_WRITE_BODY_COMPLETE; |
| 682 | |
| 683 | DCHECK(request_->upload_data); |
| 684 | DCHECK(request_body_stream_.get()); |
| 685 | |
| 686 | const char* buf = request_body_stream_->buf(); |
| 687 | int buf_len = static_cast<int>(request_body_stream_->buf_len()); |
| 688 | |
| 689 | return connection_.socket()->Write(buf, buf_len, &io_callback_); |
| 690 | } |
| 691 | |
| 692 | int HttpNetworkTransaction::DoWriteBodyComplete(int result) { |
| 693 | if (result < 0) |
| 694 | return HandleIOError(result); |
| 695 | |
| 696 | request_body_stream_->DidConsume(result); |
| 697 | |
| 698 | if (request_body_stream_->position() < request_body_stream_->size()) { |
| 699 | next_state_ = STATE_WRITE_BODY; |
| 700 | } else { |
| 701 | next_state_ = STATE_READ_HEADERS; |
| 702 | } |
| 703 | return OK; |
| 704 | } |
| 705 | |
| 706 | int HttpNetworkTransaction::DoReadHeaders() { |
| 707 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
| 708 | |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 709 | // Grow the read buffer if necessary. |
| 710 | if (header_buf_len_ == header_buf_capacity_) { |
| 711 | header_buf_capacity_ += kHeaderBufInitialSize; |
| 712 | header_buf_.reset(static_cast<char*>( |
| 713 | realloc(header_buf_.release(), header_buf_capacity_))); |
| 714 | } |
| 715 | |
| 716 | char* buf = header_buf_.get() + header_buf_len_; |
| 717 | int buf_len = header_buf_capacity_ - header_buf_len_; |
| 718 | |
| 719 | return connection_.socket()->Read(buf, buf_len, &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 720 | } |
| 721 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 722 | int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() { |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 723 | if (establishing_tunnel_) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 724 | // The connection was closed before the tunnel could be established. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 725 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 726 | } |
| 727 | |
| 728 | if (has_found_status_line_start()) { |
| 729 | // Assume EOF is end-of-headers. |
| 730 | header_buf_body_offset_ = header_buf_len_; |
| 731 | return OK; |
| 732 | } |
| 733 | |
| 734 | // No status line was matched yet. Could have been a HTTP/0.9 response, or |
| 735 | // a partial HTTP/1.x response. |
| 736 | |
| 737 | if (header_buf_len_ == 0) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 738 | // The connection was closed before any data was sent. Likely an error |
| 739 | // rather than empty HTTP/0.9 response. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 740 | return ERR_EMPTY_RESPONSE; |
| 741 | } |
| 742 | |
| 743 | // Assume everything else is a HTTP/0.9 response (including responses |
| 744 | // of 'h', 'ht', 'htt'). |
| 745 | header_buf_body_offset_ = 0; |
| 746 | return OK; |
| 747 | } |
| 748 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 749 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
| 750 | if (result < 0) |
| 751 | return HandleIOError(result); |
| 752 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 753 | if (result == 0 && ShouldResendRequest()) |
| 754 | return result; |
| 755 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 756 | // Record our best estimate of the 'response time' as the time when we read |
| 757 | // the first bytes of the response headers. |
| 758 | if (header_buf_len_ == 0) |
| 759 | response_.response_time = Time::Now(); |
| 760 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 761 | // The socket was closed before we found end-of-headers. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 762 | if (result == 0) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 763 | int rv = HandleConnectionClosedBeforeEndOfHeaders(); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 764 | if (rv != OK) |
| 765 | return rv; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 766 | } else { |
| 767 | header_buf_len_ += result; |
| 768 | DCHECK(header_buf_len_ <= header_buf_capacity_); |
| 769 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 770 | // Look for the start of the status line, if it hasn't been found yet. |
| 771 | if (!has_found_status_line_start()) { |
| 772 | header_buf_http_offset_ = HttpUtil::LocateStartOfStatusLine( |
| 773 | header_buf_.get(), header_buf_len_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 774 | } |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 775 | |
| 776 | if (has_found_status_line_start()) { |
| 777 | int eoh = HttpUtil::LocateEndOfHeaders( |
| 778 | header_buf_.get(), header_buf_len_, header_buf_http_offset_); |
| 779 | if (eoh == -1) { |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 780 | // Prevent growing the headers buffer indefinitely. |
| 781 | if (header_buf_len_ >= kMaxHeaderBufSize) |
| 782 | return ERR_RESPONSE_HEADERS_TOO_BIG; |
| 783 | |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 784 | // Haven't found the end of headers yet, keep reading. |
| 785 | next_state_ = STATE_READ_HEADERS; |
| 786 | return OK; |
| 787 | } |
| 788 | header_buf_body_offset_ = eoh; |
| 789 | } else if (header_buf_len_ < 8) { |
| 790 | // Not enough data to decide whether this is HTTP/0.9 yet. |
| 791 | // 8 bytes = (4 bytes of junk) + "http".length() |
| 792 | next_state_ = STATE_READ_HEADERS; |
| 793 | return OK; |
| 794 | } else { |
| 795 | // Enough data was read -- there is no status line. |
| 796 | header_buf_body_offset_ = 0; |
| 797 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 798 | } |
[email protected] | 65f1140 | 2008-10-31 17:39:44 | [diff] [blame] | 799 | |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 800 | // And, we are done with the Start or the SSL tunnel CONNECT sequence. |
[email protected] | 27161fb | 2008-11-03 23:39:05 | [diff] [blame] | 801 | return DidReadResponseHeaders(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | int HttpNetworkTransaction::DoReadBody() { |
| 805 | DCHECK(read_buf_); |
| 806 | DCHECK(read_buf_len_ > 0); |
| 807 | DCHECK(connection_.is_initialized()); |
| 808 | |
| 809 | next_state_ = STATE_READ_BODY_COMPLETE; |
| 810 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 811 | // We may have already consumed the indicated content length. |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 812 | if (response_body_length_ != -1 && |
| 813 | response_body_read_ >= response_body_length_) |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 814 | return 0; |
| 815 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 816 | // We may have some data remaining in the header buffer. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 817 | if (header_buf_.get() && header_buf_body_offset_ < header_buf_len_) { |
| 818 | int n = std::min(read_buf_len_, header_buf_len_ - header_buf_body_offset_); |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 819 | memcpy(read_buf_->data(), header_buf_.get() + header_buf_body_offset_, n); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 820 | header_buf_body_offset_ += n; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 821 | if (header_buf_body_offset_ == header_buf_len_) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 822 | header_buf_.reset(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 823 | header_buf_capacity_ = 0; |
| 824 | header_buf_len_ = 0; |
| 825 | header_buf_body_offset_ = -1; |
| 826 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 827 | return n; |
| 828 | } |
| 829 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 830 | return connection_.socket()->Read(read_buf_->data(), read_buf_len_, |
| 831 | &io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 835 | // We are done with the Read call. |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 836 | DCHECK(!establishing_tunnel_) << |
| 837 | "We should never read a response body of a tunnel."; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 838 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 839 | bool unfiltered_eof = (result == 0); |
| 840 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 841 | // Filter incoming data if appropriate. FilterBuf may return an error. |
| 842 | if (result > 0 && chunked_decoder_.get()) { |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 843 | result = chunked_decoder_->FilterBuf(read_buf_->data(), result); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 844 | if (result == 0 && !chunked_decoder_->reached_eof()) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 845 | // Don't signal completion of the Read call yet or else it'll look like |
| 846 | // we received end-of-file. Wait for more data. |
| 847 | next_state_ = STATE_READ_BODY; |
| 848 | return OK; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | bool done = false, keep_alive = false; |
| 853 | if (result < 0) { |
| 854 | // Error while reading the socket. |
| 855 | done = true; |
| 856 | } else { |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 857 | response_body_read_ += result; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 858 | if (unfiltered_eof || |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 859 | (response_body_length_ != -1 && |
| 860 | response_body_read_ >= response_body_length_) || |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 861 | (chunked_decoder_.get() && chunked_decoder_->reached_eof())) { |
| 862 | done = true; |
| 863 | keep_alive = response_.headers->IsKeepAlive(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 864 | // We can't reuse the connection if we read more than the advertised |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 865 | // content length. |
[email protected] | f4e426b | 2008-11-05 00:24:49 | [diff] [blame] | 866 | if (unfiltered_eof || |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 867 | (response_body_length_ != -1 && |
| 868 | response_body_read_ > response_body_length_)) |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 869 | keep_alive = false; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 870 | } |
| 871 | } |
| 872 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 873 | // Clean up connection_ if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 874 | if (done) { |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 875 | LogTransactionMetrics(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 876 | if (!keep_alive) |
| 877 | connection_.set_socket(NULL); |
| 878 | connection_.Reset(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 879 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | // Clear these to avoid leaving around old state. |
| 883 | read_buf_ = NULL; |
| 884 | read_buf_len_ = 0; |
| 885 | |
| 886 | return result; |
| 887 | } |
| 888 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 889 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 890 | // This method differs from DoReadBody only in the next_state_. So we just |
| 891 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 892 | // elegant way for these two methods to share code. |
| 893 | int rv = DoReadBody(); |
| 894 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 895 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 896 | return rv; |
| 897 | } |
| 898 | |
| 899 | // TODO(wtc): The first two thirds of this method and the DoReadBodyComplete |
| 900 | // method are almost the same. Figure out a good way for these two methods |
| 901 | // to share code. |
| 902 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
| 903 | bool unfiltered_eof = (result == 0); |
| 904 | |
| 905 | // Filter incoming data if appropriate. FilterBuf may return an error. |
| 906 | if (result > 0 && chunked_decoder_.get()) { |
| 907 | result = chunked_decoder_->FilterBuf(read_buf_->data(), result); |
| 908 | if (result == 0 && !chunked_decoder_->reached_eof()) { |
| 909 | // Don't signal completion of the Read call yet or else it'll look like |
| 910 | // we received end-of-file. Wait for more data. |
| 911 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 912 | return OK; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | bool done = false, keep_alive = false; |
| 917 | if (result < 0) { |
| 918 | // Error while reading the socket. |
| 919 | done = true; |
| 920 | } else { |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 921 | response_body_read_ += result; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 922 | if (unfiltered_eof || |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 923 | (response_body_length_ != -1 && |
| 924 | response_body_read_ >= response_body_length_) || |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 925 | (chunked_decoder_.get() && chunked_decoder_->reached_eof())) { |
| 926 | done = true; |
| 927 | keep_alive = response_.headers->IsKeepAlive(); |
| 928 | // We can't reuse the connection if we read more than the advertised |
| 929 | // content length. |
| 930 | if (unfiltered_eof || |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 931 | (response_body_length_ != -1 && |
| 932 | response_body_read_ > response_body_length_)) |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 933 | keep_alive = false; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if (done) { |
| 938 | DidDrainBodyForAuthRestart(keep_alive); |
| 939 | } else { |
| 940 | // Keep draining. |
| 941 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 942 | } |
| 943 | |
| 944 | return OK; |
| 945 | } |
| 946 | |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 947 | void HttpNetworkTransaction::LogTransactionMetrics() const { |
| 948 | base::TimeDelta duration = base::Time::Now() - response_.request_time; |
| 949 | if (60 < duration.InMinutes()) |
| 950 | return; |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 951 | UMA_HISTOGRAM_LONG_TIMES("Net.Transaction_Latency", duration); |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 952 | if (!duration.InMilliseconds()) |
| 953 | return; |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 954 | UMA_HISTOGRAM_COUNTS("Net.Transaction_Bandwidth", |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 955 | static_cast<int> (response_body_read_ / duration.InMilliseconds())); |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 956 | } |
| 957 | |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 958 | void HttpNetworkTransaction::LogBlockedTunnelResponse( |
| 959 | const HttpResponseHeaders& headers) const { |
| 960 | LOG(WARNING) << "Blocked proxy response with status " |
| 961 | << headers.response_code() << " to CONNECT request for " |
| 962 | << request_->url.host() << ":" |
| 963 | << request_->url.EffectiveIntPort() << "."; |
| 964 | } |
| 965 | |
[email protected] | 27161fb | 2008-11-03 23:39:05 | [diff] [blame] | 966 | int HttpNetworkTransaction::DidReadResponseHeaders() { |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 967 | scoped_refptr<HttpResponseHeaders> headers; |
| 968 | if (has_found_status_line_start()) { |
| 969 | headers = new HttpResponseHeaders( |
| 970 | HttpUtil::AssembleRawHeaders( |
| 971 | header_buf_.get(), header_buf_body_offset_)); |
| 972 | } else { |
| 973 | // Fabricate a status line to to preserve the HTTP/0.9 version. |
| 974 | // (otherwise HttpResponseHeaders will default it to HTTP/1.0). |
| 975 | headers = new HttpResponseHeaders(std::string("HTTP/0.9 200 OK")); |
| 976 | } |
| 977 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 978 | if (headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
| 979 | // Require the "HTTP/1.x" status line for SSL CONNECT. |
| 980 | if (establishing_tunnel_) |
| 981 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | 231d5a3 | 2008-09-13 00:45:27 | [diff] [blame] | 982 | |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 983 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 984 | // indicates a buggy server. See: |
| 985 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 986 | if (request_->method == "PUT") |
| 987 | return ERR_METHOD_NOT_SUPPORTED; |
| 988 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 989 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 990 | if (establishing_tunnel_) { |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 991 | switch (headers->response_code()) { |
| 992 | case 200: // OK |
| 993 | if (header_buf_body_offset_ != header_buf_len_) { |
| 994 | // The proxy sent extraneous data after the headers. |
| 995 | return ERR_TUNNEL_CONNECTION_FAILED; |
| 996 | } |
| 997 | next_state_ = STATE_SSL_CONNECT_OVER_TUNNEL; |
| 998 | // Reset for the real request and response headers. |
| 999 | request_headers_.clear(); |
| 1000 | request_headers_bytes_sent_ = 0; |
| 1001 | header_buf_len_ = 0; |
| 1002 | header_buf_body_offset_ = 0; |
| 1003 | establishing_tunnel_ = false; |
| 1004 | return OK; |
| 1005 | |
| 1006 | // We aren't able to CONNECT to the remote host through the proxy. We |
| 1007 | // need to be very suspicious about the response because an active network |
| 1008 | // attacker can force us into this state by masquerading as the proxy. |
| 1009 | // The only safe thing to do here is to fail the connection because our |
| 1010 | // client is expecting an SSL protected response. |
| 1011 | // See http://crbug.com/7338. |
| 1012 | case 407: // Proxy Authentication Required |
| 1013 | // We need this status code to allow proxy authentication. Our |
| 1014 | // authentication code is smart enough to avoid being tricked by an |
| 1015 | // active network attacker. |
| 1016 | break; |
| 1017 | default: |
| 1018 | // For all other status codes, we conservatively fail the CONNECT |
| 1019 | // request. |
| 1020 | // We lose something by doing this. We have seen proxy 403, 404, and |
| 1021 | // 501 response bodies that contain a useful error message. For |
| 1022 | // example, Squid uses a 404 response to report the DNS error: "The |
| 1023 | // domain name does not exist." |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 1024 | LogBlockedTunnelResponse(*headers); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 1025 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 1026 | } |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 1027 | } |
| 1028 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1029 | // Check for an intermediate 100 Continue response. An origin server is |
| 1030 | // allowed to send this response even if we didn't ask for it, so we just |
| 1031 | // need to skip over it. |
| 1032 | if (headers->response_code() == 100) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1033 | header_buf_len_ -= header_buf_body_offset_; |
| 1034 | // If we've already received some bytes after the 100 Continue response, |
| 1035 | // move them to the beginning of header_buf_. |
| 1036 | if (header_buf_len_) { |
| 1037 | memmove(header_buf_.get(), header_buf_.get() + header_buf_body_offset_, |
| 1038 | header_buf_len_); |
| 1039 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1040 | header_buf_body_offset_ = -1; |
| 1041 | next_state_ = STATE_READ_HEADERS; |
| 1042 | return OK; |
| 1043 | } |
| 1044 | |
| 1045 | response_.headers = headers; |
| 1046 | response_.vary_data.Init(*request_, *response_.headers); |
| 1047 | |
| 1048 | // Figure how to determine EOF: |
| 1049 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1050 | // For certain responses, we know the content length is always 0. From |
| 1051 | // RFC 2616 Section 4.3 Message Body: |
| 1052 | // |
| 1053 | // For response messages, whether or not a message-body is included with |
| 1054 | // a message is dependent on both the request method and the response |
| 1055 | // status code (section 6.1.1). All responses to the HEAD request method |
| 1056 | // MUST NOT include a message-body, even though the presence of entity- |
| 1057 | // header fields might lead one to believe they do. All 1xx |
| 1058 | // (informational), 204 (no content), and 304 (not modified) responses |
| 1059 | // MUST NOT include a message-body. All other responses do include a |
| 1060 | // message-body, although it MAY be of zero length. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1061 | switch (response_.headers->response_code()) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1062 | case 204: // No Content |
| 1063 | case 205: // Reset Content |
| 1064 | case 304: // Not Modified |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1065 | response_body_length_ = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1066 | break; |
| 1067 | } |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1068 | if (request_->method == "HEAD") |
| 1069 | response_body_length_ = 0; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1070 | |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1071 | if (response_body_length_ == -1) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1072 | // Ignore spurious chunked responses from HTTP/1.0 servers and proxies. |
| 1073 | // Otherwise "Transfer-Encoding: chunked" trumps "Content-Length: N" |
[email protected] | f9d44aa | 2008-09-23 23:57:17 | [diff] [blame] | 1074 | if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1) && |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1075 | response_.headers->HasHeaderValue("Transfer-Encoding", "chunked")) { |
| 1076 | chunked_decoder_.reset(new HttpChunkedDecoder()); |
| 1077 | } else { |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1078 | response_body_length_ = response_.headers->GetContentLength(); |
| 1079 | // If response_body_length_ is still -1, then we have to wait for the |
| 1080 | // server to close the connection. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1084 | int rv = HandleAuthChallenge(); |
| 1085 | if (rv == WILL_RESTART_TRANSACTION) |
| 1086 | return OK; |
| 1087 | if (rv != OK) |
| 1088 | return rv; |
| 1089 | |
[email protected] | 6b9833e | 2008-09-10 20:32:25 | [diff] [blame] | 1090 | if (using_ssl_ && !establishing_tunnel_) { |
[email protected] | 4628a2a | 2008-08-14 20:33:25 | [diff] [blame] | 1091 | SSLClientSocket* ssl_socket = |
| 1092 | reinterpret_cast<SSLClientSocket*>(connection_.socket()); |
| 1093 | ssl_socket->GetSSLInfo(&response_.ssl_info); |
| 1094 | } |
| 1095 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1096 | return OK; |
| 1097 | } |
| 1098 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 1099 | int HttpNetworkTransaction::HandleCertificateError(int error) { |
| 1100 | DCHECK(using_ssl_); |
| 1101 | |
| 1102 | const int kCertFlags = LOAD_IGNORE_CERT_COMMON_NAME_INVALID | |
| 1103 | LOAD_IGNORE_CERT_DATE_INVALID | |
| 1104 | LOAD_IGNORE_CERT_AUTHORITY_INVALID | |
| 1105 | LOAD_IGNORE_CERT_WRONG_USAGE; |
| 1106 | if (request_->load_flags & kCertFlags) { |
| 1107 | switch (error) { |
| 1108 | case ERR_CERT_COMMON_NAME_INVALID: |
| 1109 | if (request_->load_flags & LOAD_IGNORE_CERT_COMMON_NAME_INVALID) |
| 1110 | error = OK; |
| 1111 | break; |
| 1112 | case ERR_CERT_DATE_INVALID: |
| 1113 | if (request_->load_flags & LOAD_IGNORE_CERT_DATE_INVALID) |
| 1114 | error = OK; |
| 1115 | break; |
| 1116 | case ERR_CERT_AUTHORITY_INVALID: |
| 1117 | if (request_->load_flags & LOAD_IGNORE_CERT_AUTHORITY_INVALID) |
| 1118 | error = OK; |
| 1119 | break; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | if (error != OK) { |
| 1124 | SSLClientSocket* ssl_socket = |
| 1125 | reinterpret_cast<SSLClientSocket*>(connection_.socket()); |
| 1126 | ssl_socket->GetSSLInfo(&response_.ssl_info); |
| 1127 | } |
| 1128 | return error; |
| 1129 | } |
| 1130 | |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1131 | int HttpNetworkTransaction::HandleSSLHandshakeError(int error) { |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1132 | switch (error) { |
| 1133 | case ERR_SSL_PROTOCOL_ERROR: |
| 1134 | case ERR_SSL_VERSION_OR_CIPHER_MISMATCH: |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1135 | if (ssl_config_.tls1_enabled) { |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1136 | // This could be a TLS-intolerant server or an SSL 3.0 server that |
| 1137 | // chose a TLS-only cipher suite. Turn off TLS 1.0 and retry. |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1138 | ssl_config_.tls1_enabled = false; |
[email protected] | 5a179bcc | 2008-10-13 18:10:59 | [diff] [blame] | 1139 | connection_.set_socket(NULL); |
| 1140 | connection_.Reset(); |
| 1141 | next_state_ = STATE_INIT_CONNECTION; |
| 1142 | error = OK; |
| 1143 | } |
| 1144 | break; |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1145 | } |
[email protected] | c5949a3 | 2008-10-08 17:28:23 | [diff] [blame] | 1146 | return error; |
| 1147 | } |
| 1148 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 1149 | // This method determines whether it is safe to resend the request after an |
| 1150 | // IO error. It can only be called in response to request header or body |
| 1151 | // write errors or response header read errors. It should not be used in |
| 1152 | // other cases, such as a Connect error. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1153 | int HttpNetworkTransaction::HandleIOError(int error) { |
| 1154 | switch (error) { |
| 1155 | // If we try to reuse a connection that the server is in the process of |
| 1156 | // closing, we may end up successfully writing out our request (or a |
| 1157 | // portion of our request) only to find a connection error when we try to |
| 1158 | // read from (or finish writing to) the socket. |
| 1159 | case ERR_CONNECTION_RESET: |
| 1160 | case ERR_CONNECTION_CLOSED: |
| 1161 | case ERR_CONNECTION_ABORTED: |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1162 | if (ShouldResendRequest()) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1163 | error = OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1164 | break; |
| 1165 | } |
| 1166 | return error; |
| 1167 | } |
| 1168 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1169 | void HttpNetworkTransaction::ResetStateForRestart() { |
| 1170 | header_buf_.reset(); |
| 1171 | header_buf_capacity_ = 0; |
| 1172 | header_buf_len_ = 0; |
| 1173 | header_buf_body_offset_ = -1; |
| 1174 | header_buf_http_offset_ = -1; |
[email protected] | ef0faf2e7 | 2009-03-05 23:27:23 | [diff] [blame] | 1175 | response_body_length_ = -1; |
| 1176 | response_body_read_ = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1177 | read_buf_ = NULL; |
| 1178 | read_buf_len_ = 0; |
| 1179 | request_headers_.clear(); |
| 1180 | request_headers_bytes_sent_ = 0; |
| 1181 | chunked_decoder_.reset(); |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1182 | // Reset the scoped_refptr |
| 1183 | response_.headers = NULL; |
| 1184 | response_.auth_challenge = NULL; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1185 | } |
| 1186 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1187 | bool HttpNetworkTransaction::ShouldResendRequest() { |
| 1188 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1189 | // This automatically prevents an infinite resend loop because we'll run |
| 1190 | // out of the cached keep-alive connections eventually. |
| 1191 | if (establishing_tunnel_ || |
| 1192 | !reused_socket_ || // We didn't reuse a keep-alive connection. |
| 1193 | header_buf_len_) { // We have received some response headers. |
| 1194 | return false; |
| 1195 | } |
| 1196 | connection_.set_socket(NULL); |
| 1197 | connection_.Reset(); |
[email protected] | 372d34a | 2008-11-05 21:30:51 | [diff] [blame] | 1198 | // There are two reasons we need to clear request_headers_. 1) It contains |
| 1199 | // the real request headers, but we may need to resend the CONNECT request |
| 1200 | // first to recreate the SSL tunnel. 2) An empty request_headers_ causes |
| 1201 | // BuildRequestHeaders to be called, which rewinds request_body_stream_ to |
| 1202 | // the beginning of request_->upload_data. |
| 1203 | request_headers_.clear(); |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1204 | request_headers_bytes_sent_ = 0; |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1205 | next_state_ = STATE_INIT_CONNECTION; // Resend the request. |
| 1206 | return true; |
| 1207 | } |
| 1208 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1209 | int HttpNetworkTransaction::ReconsiderProxyAfterError(int error) { |
| 1210 | DCHECK(!pac_request_); |
| 1211 | |
| 1212 | // A failure to resolve the hostname or any error related to establishing a |
| 1213 | // TCP connection could be grounds for trying a new proxy configuration. |
[email protected] | 7be5131 | 2008-09-29 23:21:30 | [diff] [blame] | 1214 | // |
| 1215 | // Why do this when a hostname cannot be resolved? Some URLs only make sense |
| 1216 | // to proxy servers. The hostname in those URLs might fail to resolve if we |
| 1217 | // are still using a non-proxy config. We need to check if a proxy config |
| 1218 | // now exists that corresponds to a proxy server that could load the URL. |
| 1219 | // |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1220 | switch (error) { |
| 1221 | case ERR_NAME_NOT_RESOLVED: |
| 1222 | case ERR_INTERNET_DISCONNECTED: |
| 1223 | case ERR_ADDRESS_UNREACHABLE: |
| 1224 | case ERR_CONNECTION_CLOSED: |
| 1225 | case ERR_CONNECTION_RESET: |
| 1226 | case ERR_CONNECTION_REFUSED: |
| 1227 | case ERR_CONNECTION_ABORTED: |
| 1228 | case ERR_TIMED_OUT: |
| 1229 | case ERR_TUNNEL_CONNECTION_FAILED: |
| 1230 | break; |
| 1231 | default: |
| 1232 | return error; |
| 1233 | } |
| 1234 | |
[email protected] | 677c9057 | 2008-12-10 09:03:15 | [diff] [blame] | 1235 | if (request_->load_flags & LOAD_BYPASS_PROXY) { |
| 1236 | return error; |
| 1237 | } |
| 1238 | |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1239 | int rv = session_->proxy_service()->ReconsiderProxyAfterError( |
| 1240 | request_->url, &proxy_info_, &io_callback_, &pac_request_); |
| 1241 | if (rv == OK || rv == ERR_IO_PENDING) { |
| 1242 | connection_.set_socket(NULL); |
| 1243 | connection_.Reset(); |
| 1244 | DCHECK(!request_headers_bytes_sent_); |
| 1245 | next_state_ = STATE_RESOLVE_PROXY_COMPLETE; |
| 1246 | } else { |
| 1247 | rv = error; |
| 1248 | } |
| 1249 | |
| 1250 | return rv; |
| 1251 | } |
| 1252 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1253 | void HttpNetworkTransaction::AddAuthorizationHeader(HttpAuth::Target target) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1254 | // If we have no authentication information, check if we can select |
| 1255 | // a cache entry preemptively (based on the path). |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 1256 | if (!HaveAuth(target) && !SelectPreemptiveAuth(target)) |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1257 | return; |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1258 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1259 | DCHECK(HaveAuth(target)); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 1260 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1261 | // Add a Authorization/Proxy-Authorization header line. |
| 1262 | std::string credentials = auth_handler_[target]->GenerateCredentials( |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1263 | auth_identity_[target].username, |
| 1264 | auth_identity_[target].password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1265 | request_, |
| 1266 | &proxy_info_); |
| 1267 | request_headers_ += HttpAuth::GetAuthorizationHeaderName(target) + |
| 1268 | ": " + credentials + "\r\n"; |
| 1269 | } |
| 1270 | |
| 1271 | void HttpNetworkTransaction::ApplyAuth() { |
| 1272 | // We expect using_proxy_ and using_tunnel_ to be mutually exclusive. |
| 1273 | DCHECK(!using_proxy_ || !using_tunnel_); |
| 1274 | |
| 1275 | // Don't send proxy auth after tunnel has been established. |
| 1276 | bool should_apply_proxy_auth = using_proxy_ || establishing_tunnel_; |
| 1277 | |
| 1278 | // Don't send origin server auth while establishing tunnel. |
| 1279 | bool should_apply_server_auth = !establishing_tunnel_; |
| 1280 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1281 | if (should_apply_proxy_auth) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1282 | AddAuthorizationHeader(HttpAuth::AUTH_PROXY); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1283 | if (should_apply_server_auth) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1284 | AddAuthorizationHeader(HttpAuth::AUTH_SERVER); |
| 1285 | } |
| 1286 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1287 | GURL HttpNetworkTransaction::AuthOrigin(HttpAuth::Target target) const { |
| 1288 | return target == HttpAuth::AUTH_PROXY ? |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 1289 | GURL("http://" + proxy_info_.proxy_server().host_and_port()) : |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1290 | request_->url.GetOrigin(); |
| 1291 | } |
| 1292 | |
| 1293 | std::string HttpNetworkTransaction::AuthPath(HttpAuth::Target target) |
| 1294 | const { |
| 1295 | // Proxy authentication realms apply to all paths. So we will use |
| 1296 | // empty string in place of an absolute path. |
| 1297 | return target == HttpAuth::AUTH_PROXY ? |
| 1298 | std::string() : request_->url.path(); |
| 1299 | } |
| 1300 | |
| 1301 | void HttpNetworkTransaction::InvalidateRejectedAuthFromCache( |
| 1302 | HttpAuth::Target target) { |
| 1303 | DCHECK(HaveAuth(target)); |
| 1304 | |
| 1305 | // TODO(eroman): this short-circuit can be relaxed. If the realm of |
| 1306 | // the preemptively used auth entry matches the realm of the subsequent |
| 1307 | // challenge, then we can invalidate the preemptively used entry. |
| 1308 | // Otherwise as-is we may send the failed credentials one extra time. |
| 1309 | if (auth_identity_[target].source == HttpAuth::IDENT_SRC_PATH_LOOKUP) |
| 1310 | return; |
| 1311 | |
| 1312 | // Clear the cache entry for the identity we just failed on. |
| 1313 | // Note: we require the username/password to match before invalidating |
| 1314 | // since the entry in the cache may be newer than what we used last time. |
| 1315 | session_->auth_cache()->Remove(AuthOrigin(target), |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 1316 | auth_handler_[target]->realm(), |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1317 | auth_identity_[target].username, |
| 1318 | auth_identity_[target].password); |
| 1319 | } |
| 1320 | |
| 1321 | bool HttpNetworkTransaction::SelectPreemptiveAuth(HttpAuth::Target target) { |
| 1322 | DCHECK(!HaveAuth(target)); |
| 1323 | |
| 1324 | // Don't do preemptive authorization if the URL contains a username/password, |
| 1325 | // since we must first be challenged in order to use the URL's identity. |
| 1326 | if (request_->url.has_username()) |
| 1327 | return false; |
| 1328 | |
| 1329 | // SelectPreemptiveAuth() is on the critical path for each request, so it |
| 1330 | // is expected to be fast. LookupByPath() is fast in the common case, since |
| 1331 | // the number of http auth cache entries is expected to be very small. |
| 1332 | // (For most users in fact, it will be 0.) |
| 1333 | |
| 1334 | HttpAuthCache::Entry* entry = session_->auth_cache()->LookupByPath( |
| 1335 | AuthOrigin(target), AuthPath(target)); |
| 1336 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1337 | // We don't support preemptive authentication for connection-based |
| 1338 | // authentication schemes because they can't reuse entry->handler(). |
| 1339 | // Hopefully we can remove this limitation in the future. |
| 1340 | if (entry && !entry->handler()->is_connection_based()) { |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1341 | auth_identity_[target].source = HttpAuth::IDENT_SRC_PATH_LOOKUP; |
| 1342 | auth_identity_[target].invalid = false; |
| 1343 | auth_identity_[target].username = entry->username(); |
| 1344 | auth_identity_[target].password = entry->password(); |
| 1345 | auth_handler_[target] = entry->handler(); |
| 1346 | return true; |
| 1347 | } |
| 1348 | return false; |
| 1349 | } |
| 1350 | |
| 1351 | bool HttpNetworkTransaction::SelectNextAuthIdentityToTry( |
| 1352 | HttpAuth::Target target) { |
| 1353 | DCHECK(auth_handler_[target]); |
| 1354 | DCHECK(auth_identity_[target].invalid); |
| 1355 | |
| 1356 | // Try to use the username/password encoded into the URL first. |
| 1357 | // (By checking source == IDENT_SRC_NONE, we make sure that this |
| 1358 | // is only done once for the transaction.) |
| 1359 | if (target == HttpAuth::AUTH_SERVER && request_->url.has_username() && |
| 1360 | auth_identity_[target].source == HttpAuth::IDENT_SRC_NONE) { |
| 1361 | auth_identity_[target].source = HttpAuth::IDENT_SRC_URL; |
| 1362 | auth_identity_[target].invalid = false; |
[email protected] | 77848d1 | 2008-11-14 00:00:22 | [diff] [blame] | 1363 | // TODO(wtc) It may be necessary to unescape the username and password |
| 1364 | // after extracting them from the URL. We should be careful about |
| 1365 | // embedded nulls in that case. |
| 1366 | auth_identity_[target].username = ASCIIToWide(request_->url.username()); |
| 1367 | auth_identity_[target].password = ASCIIToWide(request_->url.password()); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1368 | // TODO(eroman): If the password is blank, should we also try combining |
| 1369 | // with a password from the cache? |
| 1370 | return true; |
| 1371 | } |
| 1372 | |
| 1373 | // Check the auth cache for a realm entry. |
| 1374 | HttpAuthCache::Entry* entry = session_->auth_cache()->LookupByRealm( |
| 1375 | AuthOrigin(target), auth_handler_[target]->realm()); |
| 1376 | |
| 1377 | if (entry) { |
| 1378 | // Disallow re-using of identity if the scheme of the originating challenge |
| 1379 | // does not match. This protects against the following situation: |
| 1380 | // 1. Browser prompts user to sign into DIGEST realm="Foo". |
| 1381 | // 2. Since the auth-scheme is not BASIC, the user is reasured that it |
| 1382 | // will not be sent over the wire in clear text. So they use their |
| 1383 | // most trusted password. |
| 1384 | // 3. Next, the browser receives a challenge for BASIC realm="Foo". This |
| 1385 | // is the same realm that we have a cached identity for. However if |
| 1386 | // we use that identity, it would get sent over the wire in |
| 1387 | // clear text (which isn't what the user agreed to when entering it). |
| 1388 | if (entry->handler()->scheme() != auth_handler_[target]->scheme()) { |
| 1389 | LOG(WARNING) << "The scheme of realm " << auth_handler_[target]->realm() |
| 1390 | << " has changed from " << entry->handler()->scheme() |
| 1391 | << " to " << auth_handler_[target]->scheme(); |
| 1392 | return false; |
| 1393 | } |
| 1394 | |
| 1395 | auth_identity_[target].source = HttpAuth::IDENT_SRC_REALM_LOOKUP; |
| 1396 | auth_identity_[target].invalid = false; |
| 1397 | auth_identity_[target].username = entry->username(); |
| 1398 | auth_identity_[target].password = entry->password(); |
| 1399 | return true; |
| 1400 | } |
| 1401 | return false; |
| 1402 | } |
| 1403 | |
| 1404 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1405 | DCHECK(response_.headers); |
| 1406 | |
| 1407 | int status = response_.headers->response_code(); |
| 1408 | if (status != 401 && status != 407) |
| 1409 | return OK; |
| 1410 | HttpAuth::Target target = status == 407 ? |
| 1411 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
| 1412 | |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1413 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1414 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1415 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1416 | // The auth we tried just failed, hence it can't be valid. Remove it from |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1417 | // the cache so it won't be used again, unless it's a null identity. |
| 1418 | if (HaveAuth(target) && |
| 1419 | auth_identity_[target].source != HttpAuth::IDENT_SRC_NONE) |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1420 | InvalidateRejectedAuthFromCache(target); |
| 1421 | |
| 1422 | auth_identity_[target].invalid = true; |
| 1423 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1424 | // Find the best authentication challenge that we support. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1425 | HttpAuth::ChooseBestChallenge(response_.headers.get(), |
| 1426 | target, |
| 1427 | &auth_handler_[target]); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1428 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1429 | if (!auth_handler_[target]) { |
| 1430 | if (establishing_tunnel_) { |
[email protected] | 655bdba | 2009-03-13 23:35:38 | [diff] [blame^] | 1431 | // Log an error message to help debug http://crbug.com/8771. |
| 1432 | std::string auth_target(target == HttpAuth::AUTH_PROXY ? |
| 1433 | "proxy" : "server"); |
| 1434 | LOG(ERROR) << "Can't perform auth to the " << auth_target << " " |
| 1435 | << AuthOrigin(target).spec() |
| 1436 | << " when establishing a tunnel"; |
| 1437 | |
| 1438 | std::string challenge; |
| 1439 | void* iter = NULL; |
| 1440 | while (response_.headers->EnumerateHeader(&iter, "Proxy-Authenticate", |
| 1441 | &challenge)) { |
| 1442 | LOG(ERROR) << " Has header Proxy-Authenticate: " << challenge; |
| 1443 | } |
| 1444 | |
| 1445 | iter = NULL; |
| 1446 | while (response_.headers->EnumerateHeader(&iter, "WWW-Authenticate", |
| 1447 | &challenge)) { |
| 1448 | LOG(ERROR) << " Has header WWW-Authenticate: " << challenge; |
| 1449 | } |
| 1450 | |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1451 | // We are establishing a tunnel, we can't show the error page because an |
| 1452 | // active network attacker could control its contents. Instead, we just |
| 1453 | // fail to establish the tunnel. |
[email protected] | 9f9f86c | 2009-03-12 22:32:42 | [diff] [blame] | 1454 | DCHECK(target == HttpAuth::AUTH_PROXY); |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1455 | return ERR_PROXY_AUTH_REQUESTED; |
| 1456 | } |
| 1457 | // We found no supported challenge -- let the transaction continue |
| 1458 | // so we end up displaying the error page. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1459 | return OK; |
[email protected] | c744cf2 | 2009-02-27 07:28:08 | [diff] [blame] | 1460 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1461 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 1462 | bool has_identity_to_try; |
| 1463 | if (auth_handler_[target]->NeedsIdentity()) { |
| 1464 | // Pick a new auth identity to try, by looking to the URL and auth cache. |
| 1465 | // If an identity to try is found, it is saved to auth_identity_[target]. |
| 1466 | has_identity_to_try = SelectNextAuthIdentityToTry(target); |
| 1467 | } else { |
| 1468 | // Proceed with a null identity. |
| 1469 | // |
| 1470 | // TODO(wtc): Add a safeguard against infinite transaction restarts, if |
| 1471 | // the server keeps returning "NTLM". |
| 1472 | auth_identity_[target].source = HttpAuth::IDENT_SRC_NONE; |
| 1473 | auth_identity_[target].invalid = false; |
| 1474 | auth_identity_[target].username.clear(); |
| 1475 | auth_identity_[target].password.clear(); |
| 1476 | has_identity_to_try = true; |
| 1477 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1478 | DCHECK(has_identity_to_try == !auth_identity_[target].invalid); |
| 1479 | |
| 1480 | if (has_identity_to_try) { |
| 1481 | DCHECK(user_callback_); |
| 1482 | PrepareForAuthRestart(target); |
| 1483 | return WILL_RESTART_TRANSACTION; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1484 | } |
| 1485 | |
[email protected] | 74da373 | 2009-01-30 23:45:03 | [diff] [blame] | 1486 | // We have exhausted all identity possibilities, all we can do now is |
| 1487 | // pass the challenge information back to the client. |
| 1488 | PopulateAuthChallenge(target); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1489 | return OK; |
| 1490 | } |
| 1491 | |
| 1492 | void HttpNetworkTransaction::PopulateAuthChallenge(HttpAuth::Target target) { |
| 1493 | // Populates response_.auth_challenge with the authentication challenge info. |
| 1494 | // This info is consumed by URLRequestHttpJob::GetAuthChallengeInfo(). |
| 1495 | |
| 1496 | AuthChallengeInfo* auth_info = new AuthChallengeInfo; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1497 | auth_info->is_proxy = target == HttpAuth::AUTH_PROXY; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1498 | auth_info->scheme = ASCIIToWide(auth_handler_[target]->scheme()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1499 | // TODO(eroman): decode realm according to RFC 2047. |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1500 | auth_info->realm = ASCIIToWide(auth_handler_[target]->realm()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1501 | if (target == HttpAuth::AUTH_PROXY) { |
[email protected] | f6fb2de | 2009-02-19 08:11:42 | [diff] [blame] | 1502 | auth_info->host = ASCIIToWide(proxy_info_.proxy_server().host_and_port()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1503 | } else { |
| 1504 | DCHECK(target == HttpAuth::AUTH_SERVER); |
| 1505 | auth_info->host = ASCIIToWide(request_->url.host()); |
| 1506 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1507 | response_.auth_challenge = auth_info; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | } // namespace net |