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