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