[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d102f54 | 2010-06-30 14:51:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 13 | #include "base/format_macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/field_trial.h" |
| 16 | #include "base/metrics/histogram.h" |
| 17 | #include "base/metrics/stats_counters.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 18 | #include "base/stl_util.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame^] | 19 | #include "base/strings/string_number_conversions.h" |
| 20 | #include "base/strings/string_util.h" |
| 21 | #include "base/strings/stringprintf.h" |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 22 | #include "base/values.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 23 | #include "build/build_config.h" |
[email protected] | 631f132 | 2010-04-30 17:59:11 | [diff] [blame] | 24 | #include "googleurl/src/gurl.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 25 | #include "net/base/auth.h" |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 26 | #include "net/base/host_port_pair.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 27 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 28 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 29 | #include "net/base/load_timing_info.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 30 | #include "net/base/net_errors.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 31 | #include "net/base/net_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 32 | #include "net/base/upload_data_stream.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 33 | #include "net/http/http_auth.h" |
| 34 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 35 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 36 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 37 | #include "net/http/http_chunked_decoder.h" |
| 38 | #include "net/http/http_network_session.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 39 | #include "net/http/http_proxy_client_socket.h" |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 40 | #include "net/http/http_proxy_client_socket_pool.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 41 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 43 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 44 | #include "net/http/http_response_info.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 45 | #include "net/http/http_server_properties.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 46 | #include "net/http/http_status_code.h" |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 47 | #include "net/http/http_stream_base.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 48 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | #include "net/http/http_util.h" |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 50 | #include "net/http/transport_security_state.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 51 | #include "net/http/url_security_manager.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 52 | #include "net/socket/client_socket_factory.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 53 | #include "net/socket/socks_client_socket_pool.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 54 | #include "net/socket/ssl_client_socket.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 55 | #include "net/socket/ssl_client_socket_pool.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 56 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 57 | #include "net/spdy/spdy_http_stream.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 58 | #include "net/spdy/spdy_session.h" |
| 59 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 60 | #include "net/ssl/ssl_cert_request_info.h" |
| 61 | #include "net/ssl/ssl_connection_status_flags.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 62 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 63 | using base::Time; |
| 64 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 65 | namespace net { |
| 66 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 67 | namespace { |
| 68 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 69 | void ProcessAlternateProtocol(HttpStreamFactory* factory, |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 70 | HttpServerProperties* http_server_properties, |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 71 | const HttpResponseHeaders& headers, |
| 72 | const HostPortPair& http_host_port_pair) { |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 73 | std::string alternate_protocol_str; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 74 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 75 | if (!headers.EnumerateHeader(NULL, kAlternateProtocolHeader, |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 76 | &alternate_protocol_str)) { |
| 77 | // Header is not present. |
| 78 | return; |
| 79 | } |
| 80 | |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 81 | factory->ProcessAlternateProtocol(http_server_properties, |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 82 | alternate_protocol_str, |
| 83 | http_host_port_pair); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | a1cb2cd | 2011-02-05 02:02:08 | [diff] [blame] | 86 | // Returns true if |error| is a client certificate authentication error. |
| 87 | bool IsClientCertificateError(int error) { |
| 88 | switch (error) { |
| 89 | case ERR_BAD_SSL_CLIENT_AUTH_CERT: |
| 90 | case ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED: |
| 91 | case ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY: |
| 92 | case ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED: |
| 93 | return true; |
| 94 | default: |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 99 | Value* NetLogSSLVersionFallbackCallback(const GURL* url, |
| 100 | int net_error, |
| 101 | uint16 version_before, |
| 102 | uint16 version_after, |
| 103 | NetLog::LogLevel /* log_level */) { |
| 104 | DictionaryValue* dict = new DictionaryValue(); |
| 105 | dict->SetString("host_and_port", GetHostAndPort(*url)); |
| 106 | dict->SetInteger("net_error", net_error); |
| 107 | dict->SetInteger("version_before", version_before); |
| 108 | dict->SetInteger("version_after", version_after); |
| 109 | return dict; |
| 110 | } |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 111 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 112 | } // namespace |
| 113 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 114 | //----------------------------------------------------------------------------- |
| 115 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 116 | HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority, |
| 117 | HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 118 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 119 | io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete, |
| 120 | base::Unretained(this))), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 121 | session_(session), |
| 122 | request_(NULL), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 123 | priority_(priority), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 124 | headers_valid_(false), |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 125 | logged_response_time_(false), |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 126 | request_headers_(), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 127 | read_buf_len_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 128 | next_state_(STATE_NONE), |
| 129 | establishing_tunnel_(false) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 130 | session->ssl_config_service()->GetSSLConfig(&server_ssl_config_); |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 131 | if (session->http_stream_factory()->has_next_protos()) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 132 | server_ssl_config_.next_protos = |
[email protected] | 5285d97 | 2011-10-18 18:56:34 | [diff] [blame] | 133 | session->http_stream_factory()->next_protos(); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 134 | } |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 135 | proxy_ssl_config_ = server_ssl_config_; |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 138 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
| 139 | if (stream_.get()) { |
| 140 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 141 | // TODO(mbelshe): The stream_ should be able to compute whether or not the |
| 142 | // stream should be kept alive. No reason to compute here |
| 143 | // and pass it in. |
| 144 | bool try_to_keep_alive = |
| 145 | next_state_ == STATE_NONE && |
| 146 | stream_->CanFindEndOfResponse() && |
| 147 | (!headers || headers->IsKeepAlive()); |
| 148 | if (!try_to_keep_alive) { |
| 149 | stream_->Close(true /* not reusable */); |
| 150 | } else { |
| 151 | if (stream_->IsResponseBodyComplete()) { |
| 152 | // If the response body is complete, we can just reuse the socket. |
| 153 | stream_->Close(false /* reusable */); |
[email protected] | 3a8d685 | 2011-03-11 23:43:44 | [diff] [blame] | 154 | } else if (stream_->IsSpdyHttpStream()) { |
| 155 | // Doesn't really matter for SpdyHttpStream. Just close it. |
| 156 | stream_->Close(true /* not reusable */); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 157 | } else { |
| 158 | // Otherwise, we try to drain the response body. |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 159 | HttpStreamBase* stream = stream_.release(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 160 | stream->Drain(session_.get()); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 164 | } |
| 165 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 166 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 167 | const CompletionCallback& callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 168 | const BoundNetLog& net_log) { |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 169 | SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count"); |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 170 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 171 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 172 | request_ = request_info; |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 173 | start_time_ = base::Time::Now(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 174 | |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 175 | if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 176 | server_ssl_config_.rev_checking_enabled = false; |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 177 | proxy_ssl_config_.rev_checking_enabled = false; |
| 178 | } |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 179 | |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 180 | // Channel ID is enabled unless --disable-tls-channel-id flag is set, |
| 181 | // or if privacy mode is enabled. |
| 182 | bool channel_id_enabled = server_ssl_config_.channel_id_enabled && |
| 183 | (request_->privacy_mode == kPrivacyModeDisabled); |
| 184 | server_ssl_config_.channel_id_enabled = channel_id_enabled; |
| 185 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 186 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 187 | int rv = DoLoop(OK); |
| 188 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 189 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 190 | return rv; |
| 191 | } |
| 192 | |
| 193 | int HttpNetworkTransaction::RestartIgnoringLastError( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 194 | const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 195 | DCHECK(!stream_.get()); |
| 196 | DCHECK(!stream_request_.get()); |
| 197 | DCHECK_EQ(STATE_NONE, next_state_); |
| 198 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 199 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 200 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 201 | int rv = DoLoop(OK); |
| 202 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 203 | callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 204 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 205 | } |
| 206 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 207 | int HttpNetworkTransaction::RestartWithCertificate( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 208 | X509Certificate* client_cert, const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 209 | // In HandleCertificateRequest(), we always tear down existing stream |
| 210 | // requests to force a new connection. So we shouldn't have one here. |
| 211 | DCHECK(!stream_request_.get()); |
| 212 | DCHECK(!stream_.get()); |
| 213 | DCHECK_EQ(STATE_NONE, next_state_); |
| 214 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 215 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 216 | &proxy_ssl_config_ : &server_ssl_config_; |
| 217 | ssl_config->send_client_cert = true; |
| 218 | ssl_config->client_cert = client_cert; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 219 | session_->ssl_client_auth_cache()->Add( |
| 220 | response_.cert_request_info->host_and_port, client_cert); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 221 | // Reset the other member variables. |
| 222 | // Note: this is necessary only with SSL renegotiation. |
| 223 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 224 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 225 | int rv = DoLoop(OK); |
| 226 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 227 | callback_ = callback; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 228 | return rv; |
| 229 | } |
| 230 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 231 | int HttpNetworkTransaction::RestartWithAuth( |
| 232 | const AuthCredentials& credentials, const CompletionCallback& callback) { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 233 | HttpAuth::Target target = pending_auth_target_; |
| 234 | if (target == HttpAuth::AUTH_NONE) { |
| 235 | NOTREACHED(); |
| 236 | return ERR_UNEXPECTED; |
| 237 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 238 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 239 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 240 | auth_controllers_[target]->ResetAuth(credentials); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 241 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 242 | DCHECK(callback_.is_null()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 243 | |
| 244 | int rv = OK; |
| 245 | if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) { |
| 246 | // In this case, we've gathered credentials for use with proxy |
| 247 | // authentication of a tunnel. |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 248 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 249 | DCHECK(stream_request_ != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 250 | auth_controllers_[target] = NULL; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 251 | ResetStateForRestart(); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 252 | rv = stream_request_->RestartTunnelWithProxyAuth(credentials); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 253 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 254 | // In this case, we've gathered credentials for the server or the proxy |
| 255 | // but it is not during the tunneling phase. |
| 256 | DCHECK(stream_request_ == NULL); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 257 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 258 | rv = DoLoop(OK); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 259 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 260 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 261 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 262 | callback_ = callback; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 263 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 266 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 267 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 268 | DCHECK(!stream_request_.get()); |
| 269 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 270 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 271 | // Even if the server says the connection is keep-alive, we have to be |
| 272 | // able to find the end of each response in order to reuse the connection. |
| 273 | if (GetResponseHeaders()->IsKeepAlive() && |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 274 | stream_->CanFindEndOfResponse()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 275 | // If the response body hasn't been completely read, we need to drain |
| 276 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 277 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 278 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 279 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 280 | read_buf_len_ = kDrainBodyBufferSize; |
| 281 | return; |
| 282 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 283 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 284 | } |
| 285 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 286 | // We don't need to drain the response body, so we act as if we had drained |
| 287 | // the response body. |
| 288 | DidDrainBodyForAuthRestart(keep_alive); |
| 289 | } |
| 290 | |
| 291 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 292 | DCHECK(!stream_request_.get()); |
| 293 | |
| 294 | if (stream_.get()) { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 295 | HttpStream* new_stream = NULL; |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 296 | if (keep_alive && stream_->IsConnectionReusable()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 297 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 298 | // often enough to be worth the trouble. |
| 299 | stream_->SetConnectionReused(); |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 300 | new_stream = |
| 301 | static_cast<HttpStream*>(stream_.get())->RenewStreamForAuth(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 302 | } |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 303 | |
| 304 | if (!new_stream) { |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 305 | // Close the stream and mark it as not_reusable. Even in the |
| 306 | // keep_alive case, we've determined that the stream_ is not |
| 307 | // reusable if new_stream is NULL. |
| 308 | stream_->Close(true); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 309 | next_state_ = STATE_CREATE_STREAM; |
| 310 | } else { |
| 311 | next_state_ = STATE_INIT_STREAM; |
| 312 | } |
| 313 | stream_.reset(new_stream); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 314 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 315 | |
| 316 | // Reset the other member variables. |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 317 | ResetStateForAuthRestart(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 320 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 321 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 322 | HaveAuth(pending_auth_target_); |
| 323 | } |
| 324 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 325 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 326 | const CompletionCallback& callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 327 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 328 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 329 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 330 | State next_state = STATE_NONE; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 331 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 332 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 333 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 334 | // We're trying to read the body of the response but we're still trying |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 335 | // to establish an SSL tunnel through an HTTP proxy. We can't read these |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 336 | // bytes when establishing a tunnel because they might be controlled by |
| 337 | // an active network attacker. We don't worry about this for HTTP |
| 338 | // because an active network attacker can already control HTTP sessions. |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 339 | // We reach this case when the user cancels a 407 proxy auth prompt. We |
| 340 | // also don't worry about this for an HTTPS Proxy, because the |
| 341 | // communication with the proxy is secure. |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 342 | // See http://crbug.com/8473. |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 343 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https()); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 344 | DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 345 | LOG(WARNING) << "Blocked proxy response with status " |
| 346 | << headers->response_code() << " to CONNECT request for " |
| 347 | << GetHostAndPort(request_->url) << "."; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 348 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 349 | } |
| 350 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 351 | // Are we using SPDY or HTTP? |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 352 | next_state = STATE_READ_BODY; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 353 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 354 | read_buf_ = buf; |
| 355 | read_buf_len_ = buf_len; |
| 356 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 357 | next_state_ = next_state; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 358 | int rv = DoLoop(OK); |
| 359 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 360 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 361 | return rv; |
| 362 | } |
| 363 | |
| 364 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 365 | return ((headers_valid_ && response_.headers.get()) || |
| 366 | response_.ssl_info.cert.get() || response_.cert_request_info.get()) |
| 367 | ? &response_ |
| 368 | : NULL; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 372 | // TODO(wtc): Define a new LoadState value for the |
| 373 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 374 | switch (next_state_) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 375 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 376 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 377 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 378 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 379 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 380 | return LOAD_STATE_SENDING_REQUEST; |
| 381 | case STATE_READ_HEADERS_COMPLETE: |
| 382 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 383 | case STATE_READ_BODY_COMPLETE: |
| 384 | return LOAD_STATE_READING_RESPONSE; |
| 385 | default: |
| 386 | return LOAD_STATE_IDLE; |
| 387 | } |
| 388 | } |
| 389 | |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 390 | UploadProgress HttpNetworkTransaction::GetUploadProgress() const { |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 391 | if (!stream_.get()) |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 392 | return UploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 393 | |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 394 | // TODO(bashi): This cast is temporary. Remove later. |
| 395 | return static_cast<HttpStream*>(stream_.get())->GetUploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 398 | bool HttpNetworkTransaction::GetLoadTimingInfo( |
| 399 | LoadTimingInfo* load_timing_info) const { |
| 400 | if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info)) |
| 401 | return false; |
| 402 | |
| 403 | load_timing_info->proxy_resolve_start = |
| 404 | proxy_info_.proxy_resolve_start_time(); |
| 405 | load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time(); |
| 406 | load_timing_info->send_start = send_start_time_; |
| 407 | load_timing_info->send_end = send_end_time_; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 408 | return true; |
| 409 | } |
| 410 | |
| 411 | void HttpNetworkTransaction::SetPriority(RequestPriority priority) { |
| 412 | priority_ = priority; |
| 413 | // TODO(akalin): Plumb this through to |stream_request_| and |
| 414 | // |stream_|. |
| 415 | } |
| 416 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 417 | void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config, |
| 418 | const ProxyInfo& used_proxy_info, |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 419 | HttpStreamBase* stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 420 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 421 | DCHECK(stream_request_.get()); |
| 422 | |
| 423 | stream_.reset(stream); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 424 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 425 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 426 | response_.was_npn_negotiated = stream_request_->was_npn_negotiated(); |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 427 | response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString( |
| 428 | stream_request_->protocol_negotiated()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 429 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
| 430 | response_.was_fetched_via_proxy = !proxy_info_.is_direct(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 431 | |
| 432 | OnIOComplete(OK); |
| 433 | } |
| 434 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 435 | void HttpNetworkTransaction::OnStreamFailed(int result, |
| 436 | const SSLConfig& used_ssl_config) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 437 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 438 | DCHECK_NE(OK, result); |
| 439 | DCHECK(stream_request_.get()); |
| 440 | DCHECK(!stream_.get()); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 441 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 442 | |
| 443 | OnIOComplete(result); |
| 444 | } |
| 445 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 446 | void HttpNetworkTransaction::OnCertificateError( |
| 447 | int result, |
| 448 | const SSLConfig& used_ssl_config, |
| 449 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 450 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 451 | DCHECK_NE(OK, result); |
| 452 | DCHECK(stream_request_.get()); |
| 453 | DCHECK(!stream_.get()); |
| 454 | |
| 455 | response_.ssl_info = ssl_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 456 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 457 | |
| 458 | // TODO(mbelshe): For now, we're going to pass the error through, and that |
| 459 | // will close the stream_request in all cases. This means that we're always |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 460 | // going to restart an entire STATE_CREATE_STREAM, even if the connection is |
| 461 | // good and the user chooses to ignore the error. This is not ideal, but not |
| 462 | // the end of the world either. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 463 | |
| 464 | OnIOComplete(result); |
| 465 | } |
| 466 | |
| 467 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 468 | const HttpResponseInfo& proxy_response, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 469 | const SSLConfig& used_ssl_config, |
| 470 | const ProxyInfo& used_proxy_info, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 471 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 472 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 473 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 474 | |
| 475 | establishing_tunnel_ = true; |
| 476 | response_.headers = proxy_response.headers; |
| 477 | response_.auth_challenge = proxy_response.auth_challenge; |
| 478 | headers_valid_ = true; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 479 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 480 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 481 | |
| 482 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 483 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 484 | |
| 485 | DoCallback(OK); |
| 486 | } |
| 487 | |
| 488 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 489 | const SSLConfig& used_ssl_config, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 490 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 491 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 492 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 493 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 494 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 495 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 496 | } |
| 497 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 498 | void HttpNetworkTransaction::OnHttpsProxyTunnelResponse( |
| 499 | const HttpResponseInfo& response_info, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 500 | const SSLConfig& used_ssl_config, |
| 501 | const ProxyInfo& used_proxy_info, |
[email protected] | 92b9a3e | 2012-11-09 08:52:52 | [diff] [blame] | 502 | HttpStreamBase* stream) { |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 503 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
| 504 | |
| 505 | headers_valid_ = true; |
| 506 | response_ = response_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 507 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 508 | proxy_info_ = used_proxy_info; |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 509 | stream_.reset(stream); |
| 510 | stream_request_.reset(); // we're done with the stream request |
| 511 | OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 512 | } |
| 513 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 514 | bool HttpNetworkTransaction::is_https_request() const { |
| 515 | return request_->url.SchemeIs("https"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 516 | } |
| 517 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 518 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 519 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 520 | DCHECK(!callback_.is_null()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 521 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 522 | // Since Run may result in Read being called, clear user_callback_ up front. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 523 | CompletionCallback c = callback_; |
| 524 | callback_.Reset(); |
| 525 | c.Run(rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 529 | int rv = DoLoop(result); |
| 530 | if (rv != ERR_IO_PENDING) |
| 531 | DoCallback(rv); |
| 532 | } |
| 533 | |
| 534 | int HttpNetworkTransaction::DoLoop(int result) { |
| 535 | DCHECK(next_state_ != STATE_NONE); |
| 536 | |
| 537 | int rv = result; |
| 538 | do { |
| 539 | State state = next_state_; |
| 540 | next_state_ = STATE_NONE; |
| 541 | switch (state) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 542 | case STATE_CREATE_STREAM: |
| 543 | DCHECK_EQ(OK, rv); |
| 544 | rv = DoCreateStream(); |
| 545 | break; |
| 546 | case STATE_CREATE_STREAM_COMPLETE: |
| 547 | rv = DoCreateStreamComplete(rv); |
| 548 | break; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 549 | case STATE_INIT_STREAM: |
| 550 | DCHECK_EQ(OK, rv); |
| 551 | rv = DoInitStream(); |
| 552 | break; |
| 553 | case STATE_INIT_STREAM_COMPLETE: |
| 554 | rv = DoInitStreamComplete(rv); |
| 555 | break; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 556 | case STATE_GENERATE_PROXY_AUTH_TOKEN: |
| 557 | DCHECK_EQ(OK, rv); |
| 558 | rv = DoGenerateProxyAuthToken(); |
| 559 | break; |
| 560 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 561 | rv = DoGenerateProxyAuthTokenComplete(rv); |
| 562 | break; |
| 563 | case STATE_GENERATE_SERVER_AUTH_TOKEN: |
| 564 | DCHECK_EQ(OK, rv); |
| 565 | rv = DoGenerateServerAuthToken(); |
| 566 | break; |
| 567 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
| 568 | rv = DoGenerateServerAuthTokenComplete(rv); |
| 569 | break; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 570 | case STATE_INIT_REQUEST_BODY: |
| 571 | DCHECK_EQ(OK, rv); |
| 572 | rv = DoInitRequestBody(); |
| 573 | break; |
| 574 | case STATE_INIT_REQUEST_BODY_COMPLETE: |
| 575 | rv = DoInitRequestBodyComplete(rv); |
| 576 | break; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 577 | case STATE_BUILD_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 578 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 579 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 580 | rv = DoBuildRequest(); |
| 581 | break; |
| 582 | case STATE_BUILD_REQUEST_COMPLETE: |
| 583 | rv = DoBuildRequestComplete(rv); |
| 584 | break; |
| 585 | case STATE_SEND_REQUEST: |
| 586 | DCHECK_EQ(OK, rv); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 587 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 588 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 589 | case STATE_SEND_REQUEST_COMPLETE: |
| 590 | rv = DoSendRequestComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 591 | net_log_.EndEventWithNetErrorCode( |
| 592 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 593 | break; |
| 594 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 595 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 596 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 597 | rv = DoReadHeaders(); |
| 598 | break; |
| 599 | case STATE_READ_HEADERS_COMPLETE: |
| 600 | rv = DoReadHeadersComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 601 | net_log_.EndEventWithNetErrorCode( |
| 602 | NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 603 | break; |
| 604 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 605 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 606 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 607 | rv = DoReadBody(); |
| 608 | break; |
| 609 | case STATE_READ_BODY_COMPLETE: |
| 610 | rv = DoReadBodyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 611 | net_log_.EndEventWithNetErrorCode( |
| 612 | NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 613 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 614 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 615 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 616 | net_log_.BeginEvent( |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 617 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 618 | rv = DoDrainBodyForAuthRestart(); |
| 619 | break; |
| 620 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 621 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 622 | net_log_.EndEventWithNetErrorCode( |
| 623 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 624 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 625 | default: |
| 626 | NOTREACHED() << "bad state"; |
| 627 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 628 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 629 | } |
| 630 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 631 | |
| 632 | return rv; |
| 633 | } |
| 634 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 635 | int HttpNetworkTransaction::DoCreateStream() { |
| 636 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 637 | |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 638 | stream_request_.reset( |
| 639 | session_->http_stream_factory()->RequestStream( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 640 | *request_, |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 641 | priority_, |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 642 | server_ssl_config_, |
| 643 | proxy_ssl_config_, |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 644 | this, |
| 645 | net_log_)); |
| 646 | DCHECK(stream_request_.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 647 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 648 | } |
| 649 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 650 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 651 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 652 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 653 | DCHECK(stream_.get()); |
[email protected] | adb0024 | 2010-10-29 03:04:33 | [diff] [blame] | 654 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 655 | result = HandleCertificateRequest(result); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 656 | } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { |
| 657 | // Return OK and let the caller read the proxy's error page |
| 658 | next_state_ = STATE_NONE; |
| 659 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 660 | } |
| 661 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 662 | // Handle possible handshake errors that may have occurred if the stream |
| 663 | // used SSL for one or more of the layers. |
| 664 | result = HandleSSLHandshakeError(result); |
| 665 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 666 | // At this point we are done with the stream_request_. |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 667 | stream_request_.reset(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 668 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 669 | } |
| 670 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 671 | int HttpNetworkTransaction::DoInitStream() { |
| 672 | DCHECK(stream_.get()); |
| 673 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 674 | return stream_->InitializeStream(request_, priority_, net_log_, io_callback_); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 678 | if (result == OK) { |
| 679 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 680 | } else { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 681 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 682 | result = HandleIOError(result); |
| 683 | |
| 684 | // The stream initialization failed, so this stream will never be useful. |
| 685 | stream_.reset(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | return result; |
| 689 | } |
| 690 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 691 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 692 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 693 | if (!ShouldApplyProxyAuth()) |
| 694 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 695 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 696 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 697 | auth_controllers_[target] = |
| 698 | new HttpAuthController(target, |
| 699 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 700 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 701 | session_->http_auth_handler_factory()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 702 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 703 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 704 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) { |
| 708 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 709 | if (rv == OK) |
| 710 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN; |
| 711 | return rv; |
| 712 | } |
| 713 | |
| 714 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 715 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 716 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
| 717 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 718 | auth_controllers_[target] = |
| 719 | new HttpAuthController(target, |
| 720 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 721 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 722 | session_->http_auth_handler_factory()); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 723 | if (!ShouldApplyServerAuth()) |
| 724 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 725 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 726 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 727 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 731 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 732 | if (rv == OK) |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 733 | next_state_ = STATE_INIT_REQUEST_BODY; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 734 | return rv; |
| 735 | } |
| 736 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 737 | void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy) { |
| 738 | request_headers_.SetHeader(HttpRequestHeaders::kHost, |
| 739 | GetHostAndOptionalPort(request_->url)); |
| 740 | |
| 741 | // For compat with HTTP/1.0 servers and proxies: |
| 742 | if (using_proxy) { |
| 743 | request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection, |
| 744 | "keep-alive"); |
| 745 | } else { |
| 746 | request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 747 | } |
| 748 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 749 | // Add a content length header? |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 750 | if (request_->upload_data_stream) { |
| 751 | if (request_->upload_data_stream->is_chunked()) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 752 | request_headers_.SetHeader( |
| 753 | HttpRequestHeaders::kTransferEncoding, "chunked"); |
| 754 | } else { |
| 755 | request_headers_.SetHeader( |
| 756 | HttpRequestHeaders::kContentLength, |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 757 | base::Uint64ToString(request_->upload_data_stream->size())); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 758 | } |
| 759 | } else if (request_->method == "POST" || request_->method == "PUT" || |
| 760 | request_->method == "HEAD") { |
| 761 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 762 | // IE and Safari also add a content length header. Presumably it is to |
| 763 | // support sending a HEAD request to an URL that only expects to be sent a |
| 764 | // POST or some other method that normally would have a message body. |
| 765 | request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 766 | } |
| 767 | |
| 768 | // Honor load flags that impact proxy caches. |
| 769 | if (request_->load_flags & LOAD_BYPASS_CACHE) { |
| 770 | request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 771 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 772 | } else if (request_->load_flags & LOAD_VALIDATE_CACHE) { |
| 773 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 774 | } |
| 775 | |
| 776 | if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY)) |
| 777 | auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader( |
| 778 | &request_headers_); |
| 779 | if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER)) |
| 780 | auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader( |
| 781 | &request_headers_); |
| 782 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 783 | request_headers_.MergeFrom(request_->extra_headers); |
[email protected] | 173f8e2 | 2013-04-10 04:18:20 | [diff] [blame] | 784 | response_.did_use_http_auth = |
| 785 | request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) || |
| 786 | request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 787 | } |
| 788 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 789 | int HttpNetworkTransaction::DoInitRequestBody() { |
| 790 | next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 791 | int rv = OK; |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 792 | if (request_->upload_data_stream) |
| 793 | rv = request_->upload_data_stream->Init(io_callback_); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 794 | return rv; |
| 795 | } |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 796 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 797 | int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) { |
| 798 | if (result == OK) |
| 799 | next_state_ = STATE_BUILD_REQUEST; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 800 | return result; |
| 801 | } |
| 802 | |
| 803 | int HttpNetworkTransaction::DoBuildRequest() { |
| 804 | next_state_ = STATE_BUILD_REQUEST_COMPLETE; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 805 | headers_valid_ = false; |
| 806 | |
| 807 | // This is constructed lazily (instead of within our Start method), so that |
| 808 | // we have proxy info available. |
| 809 | if (request_headers_.IsEmpty()) { |
| 810 | bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) && |
| 811 | !is_https_request(); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 812 | BuildRequestHeaders(using_proxy); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 813 | } |
| 814 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 815 | return OK; |
| 816 | } |
| 817 | |
| 818 | int HttpNetworkTransaction::DoBuildRequestComplete(int result) { |
[email protected] | 9f489d7 | 2011-04-04 23:29:24 | [diff] [blame] | 819 | if (result == OK) |
| 820 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 821 | return result; |
| 822 | } |
| 823 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 824 | int HttpNetworkTransaction::DoSendRequest() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 825 | send_start_time_ = base::TimeTicks::Now(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 826 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 827 | |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 828 | return stream_->SendRequest(request_headers_, &response_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 829 | } |
| 830 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 831 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 832 | send_end_time_ = base::TimeTicks::Now(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 833 | if (result < 0) |
| 834 | return HandleIOError(result); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 835 | response_.network_accessed = true; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 836 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 837 | return OK; |
| 838 | } |
| 839 | |
| 840 | int HttpNetworkTransaction::DoReadHeaders() { |
| 841 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 842 | return stream_->ReadResponseHeaders(io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 843 | } |
| 844 | |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 845 | int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 846 | if (!response_.headers.get() && !stream_->IsConnectionReused()) { |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 847 | // The connection was closed before any data was sent. Likely an error |
| 848 | // rather than empty HTTP/0.9 response. |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 849 | return ERR_EMPTY_RESPONSE; |
| 850 | } |
| 851 | |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 852 | return OK; |
| 853 | } |
| 854 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 855 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 856 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 857 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 858 | if (IsCertificateError(result)) { |
| 859 | // We don't handle a certificate error during SSL renegotiation, so we |
| 860 | // have to return an error that's not in the certificate error range |
| 861 | // (-2xx). |
| 862 | LOG(ERROR) << "Got a server certificate with error " << result |
| 863 | << " during SSL renegotiation"; |
| 864 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 865 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 866 | // TODO(wtc): Need a test case for this code path! |
| 867 | DCHECK(stream_.get()); |
| 868 | DCHECK(is_https_request()); |
| 869 | response_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 870 | stream_->GetSSLCertRequestInfo(response_.cert_request_info.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 871 | result = HandleCertificateRequest(result); |
| 872 | if (result == OK) |
| 873 | return result; |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 874 | } |
| 875 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 876 | if (result < 0 && result != ERR_CONNECTION_CLOSED) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 877 | return HandleIOError(result); |
| 878 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 879 | if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) { |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 880 | ResetConnectionAndRequestForResend(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 881 | return OK; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 882 | } |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 883 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 884 | // After we call RestartWithAuth a new response_time will be recorded, and |
| 885 | // we need to be cautious about incorrectly logging the duration across the |
| 886 | // authentication activity. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 887 | if (result == OK) |
| 888 | LogTransactionConnectedMetrics(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 889 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 890 | if (result == ERR_CONNECTION_CLOSED) { |
[email protected] | 02c92c49 | 2010-03-08 21:28:14 | [diff] [blame] | 891 | // For now, if we get at least some data, we do the best we can to make |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 892 | // sense of it and send it back up the stack. |
[email protected] | 0e75a73 | 2008-10-16 20:36:09 | [diff] [blame] | 893 | int rv = HandleConnectionClosedBeforeEndOfHeaders(); |
[email protected] | aecfbf2 | 2008-10-16 02:02:47 | [diff] [blame] | 894 | if (rv != OK) |
| 895 | return rv; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 896 | } |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 897 | DCHECK(response_.headers.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 898 | |
[email protected] | 14b7e9a | 2012-10-16 19:51:29 | [diff] [blame] | 899 | // Server-induced fallback is supported only if this is a PAC configured |
| 900 | // proxy. See: http://crbug.com/143712 |
[email protected] | 2694ceea | 2013-05-15 04:32:00 | [diff] [blame] | 901 | if (response_.was_fetched_via_proxy && proxy_info_.did_use_pac_script() && |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 902 | response_.headers.get() != NULL) { |
[email protected] | 2694ceea | 2013-05-15 04:32:00 | [diff] [blame] | 903 | bool should_fallback = |
| 904 | response_.headers->HasHeaderValue("connection", "proxy-bypass"); |
| 905 | // Additionally, fallback if a 500 is returned via the data reduction proxy. |
| 906 | // This is conservative, as the 500 might have been generated by the origin, |
| 907 | // and not the proxy. |
| 908 | #if defined(SPDY_PROXY_AUTH_ORIGIN) |
| 909 | if (!should_fallback) { |
| 910 | should_fallback = |
| 911 | response_.headers->response_code() == HTTP_INTERNAL_SERVER_ERROR && |
| 912 | proxy_info_.proxy_server().host_port_pair().Equals( |
| 913 | HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN))); |
| 914 | } |
| 915 | #endif |
| 916 | if (should_fallback) { |
[email protected] | 14b7e9a | 2012-10-16 19:51:29 | [diff] [blame] | 917 | ProxyService* proxy_service = session_->proxy_service(); |
| 918 | if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) { |
| 919 | // Only retry in the case of GETs. We don't want to resubmit a POST |
| 920 | // if the proxy took some action. |
| 921 | if (request_->method == "GET") { |
| 922 | ResetConnectionAndRequestForResend(); |
| 923 | return OK; |
| 924 | } |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
[email protected] | 93f8b56 | 2012-03-27 01:00:16 | [diff] [blame] | 929 | // Like Net.HttpResponseCode, but only for MAIN_FRAME loads. |
| 930 | if (request_->load_flags & LOAD_MAIN_FRAME) { |
| 931 | const int response_code = response_.headers->response_code(); |
| 932 | UMA_HISTOGRAM_ENUMERATION( |
| 933 | "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10); |
| 934 | } |
| 935 | |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 936 | net_log_.AddEvent( |
| 937 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 938 | base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers)); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 939 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 940 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 941 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 942 | // indicates a buggy server. See: |
| 943 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 944 | if (request_->method == "PUT") |
| 945 | return ERR_METHOD_NOT_SUPPORTED; |
| 946 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 947 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 948 | // Check for an intermediate 100 Continue response. An origin server is |
| 949 | // allowed to send this response even if we didn't ask for it, so we just |
| 950 | // need to skip over it. |
| 951 | // We treat any other 1xx in this same way (although in practice getting |
| 952 | // a 1xx that isn't a 100 is rare). |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 953 | if (response_.headers->response_code() / 100 == 1) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 954 | response_.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 955 | next_state_ = STATE_READ_HEADERS; |
| 956 | return OK; |
| 957 | } |
| 958 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 959 | HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(), |
| 960 | request_->url.EffectiveIntPort()); |
| 961 | ProcessAlternateProtocol(session_->http_stream_factory(), |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 962 | session_->http_server_properties(), |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 963 | *response_.headers.get(), |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 964 | endpoint); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 965 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 966 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 967 | if (rv != OK) |
| 968 | return rv; |
| 969 | |
[email protected] | 8536ef5 | 2010-09-30 16:18:21 | [diff] [blame] | 970 | if (is_https_request()) |
| 971 | stream_->GetSSLInfo(&response_.ssl_info); |
| 972 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 973 | headers_valid_ = true; |
| 974 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | int HttpNetworkTransaction::DoReadBody() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 978 | DCHECK(read_buf_.get()); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 979 | DCHECK_GT(read_buf_len_, 0); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 980 | DCHECK(stream_ != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 981 | |
| 982 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 983 | return stream_->ReadResponseBody( |
| 984 | read_buf_.get(), read_buf_len_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 988 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 989 | bool done = false; |
| 990 | if (result <= 0) { |
| 991 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 992 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 993 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 994 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 995 | bool keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 996 | if (stream_->IsResponseBodyComplete()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 997 | // Note: Just because IsResponseBodyComplete is true, we're not |
| 998 | // necessarily "done". We're only "done" when it is the last |
| 999 | // read on this HttpNetworkTransaction, which will be signified |
| 1000 | // by a zero-length read. |
| 1001 | // TODO(mbelshe): The keepalive property is really a property of |
| 1002 | // the stream. No need to compute it here just to pass back |
| 1003 | // to the stream's Close function. |
[email protected] | 5c6908e | 2012-08-06 18:53:47 | [diff] [blame] | 1004 | // TODO(rtenneti): CanFindEndOfResponse should return false if there are no |
| 1005 | // ResponseHeaders. |
| 1006 | if (stream_->CanFindEndOfResponse()) { |
| 1007 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 1008 | if (headers) |
| 1009 | keep_alive = headers->IsKeepAlive(); |
| 1010 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1011 | } |
| 1012 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1013 | // Clean up connection if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1014 | if (done) { |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1015 | LogTransactionMetrics(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1016 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 1017 | // Note: we don't reset the stream here. We've closed it, but we still |
| 1018 | // need it around so that callers can call methods such as |
| 1019 | // GetUploadProgress() and have them be meaningful. |
| 1020 | // TODO(mbelshe): This means we closed the stream here, and we close it |
| 1021 | // again in ~HttpNetworkTransaction. Clean that up. |
| 1022 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1023 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | // Clear these to avoid leaving around old state. |
| 1027 | read_buf_ = NULL; |
| 1028 | read_buf_len_ = 0; |
| 1029 | |
| 1030 | return result; |
| 1031 | } |
| 1032 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1033 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 1034 | // This method differs from DoReadBody only in the next_state_. So we just |
| 1035 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 1036 | // elegant way for these two methods to share code. |
| 1037 | int rv = DoReadBody(); |
| 1038 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 1039 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 1040 | return rv; |
| 1041 | } |
| 1042 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1043 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 1044 | // 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] | 1045 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1046 | // keep_alive defaults to true because the very reason we're draining the |
| 1047 | // response body is to reuse the connection for auth restart. |
| 1048 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1049 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1050 | // Error or closed connection while reading the socket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1051 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1052 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1053 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1054 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | if (done) { |
| 1058 | DidDrainBodyForAuthRestart(keep_alive); |
| 1059 | } else { |
| 1060 | // Keep draining. |
| 1061 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 1062 | } |
| 1063 | |
| 1064 | return OK; |
| 1065 | } |
| 1066 | |
[email protected] | 8e3d2d3 | 2010-06-13 18:46:23 | [diff] [blame] | 1067 | void HttpNetworkTransaction::LogTransactionConnectedMetrics() { |
| 1068 | if (logged_response_time_) |
| 1069 | return; |
| 1070 | |
| 1071 | logged_response_time_ = true; |
| 1072 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1073 | base::TimeDelta total_duration = response_.response_time - start_time_; |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1074 | |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1075 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1076 | "Net.Transaction_Connected", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1077 | total_duration, |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1078 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1079 | 100); |
[email protected] | 1fa4759 | 2009-07-27 22:45:00 | [diff] [blame] | 1080 | |
[email protected] | c38ec5a | 2010-08-25 23:32:38 | [diff] [blame] | 1081 | bool reused_socket = stream_->IsConnectionReused(); |
| 1082 | if (!reused_socket) { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1083 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1084 | "Net.Transaction_Connected_New_b", |
[email protected] | b01998a | 2009-04-21 01:01:11 | [diff] [blame] | 1085 | total_duration, |
| 1086 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1087 | 100); |
[email protected] | 0310d43 | 2009-08-25 07:49:52 | [diff] [blame] | 1088 | } |
| 1089 | |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 1090 | // Currently, non-HIGHEST priority requests are frame or sub-frame resource |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1091 | // types. This will change when we also prioritize certain subresources like |
| 1092 | // css, js, etc. |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 1093 | if (priority_ != HIGHEST) { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1094 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1095 | "Net.Priority_High_Latency_b", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1096 | total_duration, |
| 1097 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1098 | 100); |
| 1099 | } else { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1100 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1101 | "Net.Priority_Low_Latency_b", |
[email protected] | 510e854f | 2009-04-20 18:39:08 | [diff] [blame] | 1102 | total_duration, |
| 1103 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10), |
| 1104 | 100); |
| 1105 | } |
[email protected] | 9a0a55f | 2009-04-13 23:23:03 | [diff] [blame] | 1106 | } |
| 1107 | |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1108 | void HttpNetworkTransaction::LogTransactionMetrics() const { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1109 | base::TimeDelta duration = base::Time::Now() - |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1110 | response_.request_time; |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1111 | if (60 < duration.InMinutes()) |
| 1112 | return; |
[email protected] | 0b48db4 | 2009-03-23 02:45:11 | [diff] [blame] | 1113 | |
[email protected] | 21b316a | 2009-03-23 18:25:06 | [diff] [blame] | 1114 | base::TimeDelta total_duration = base::Time::Now() - start_time_; |
| 1115 | |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1116 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration, |
| 1117 | base::TimeDelta::FromMilliseconds(1), |
| 1118 | base::TimeDelta::FromMinutes(10), |
| 1119 | 100); |
| 1120 | UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total", |
| 1121 | total_duration, |
| 1122 | base::TimeDelta::FromMilliseconds(1), |
| 1123 | base::TimeDelta::FromMinutes(10), 100); |
[email protected] | 5c68d69 | 2011-08-24 04:59:41 | [diff] [blame] | 1124 | |
[email protected] | c38ec5a | 2010-08-25 23:32:38 | [diff] [blame] | 1125 | if (!stream_->IsConnectionReused()) { |
[email protected] | d6b5539 | 2011-08-05 04:04:35 | [diff] [blame] | 1126 | UMA_HISTOGRAM_CUSTOM_TIMES( |
| 1127 | "Net.Transaction_Latency_Total_New_Connection", |
[email protected] | 808f640 | 2009-03-30 20:02:07 | [diff] [blame] | 1128 | total_duration, base::TimeDelta::FromMilliseconds(1), |
| 1129 | base::TimeDelta::FromMinutes(10), 100); |
| 1130 | } |
[email protected] | 5630017 | 2008-11-06 18:42:55 | [diff] [blame] | 1131 | } |
| 1132 | |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1133 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1134 | // There are two paths through which the server can request a certificate |
| 1135 | // from us. The first is during the initial handshake, the second is |
| 1136 | // during SSL renegotiation. |
| 1137 | // |
| 1138 | // In both cases, we want to close the connection before proceeding. |
| 1139 | // We do this for two reasons: |
| 1140 | // First, we don't want to keep the connection to the server hung for a |
| 1141 | // long time while the user selects a certificate. |
| 1142 | // Second, even if we did keep the connection open, NSS has a bug where |
| 1143 | // restarting the handshake for ClientAuth is currently broken. |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1144 | DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1145 | |
| 1146 | if (stream_.get()) { |
| 1147 | // Since we already have a stream, we're being called as part of SSL |
| 1148 | // renegotiation. |
| 1149 | DCHECK(!stream_request_.get()); |
| 1150 | stream_->Close(true); |
| 1151 | stream_.reset(); |
| 1152 | } |
| 1153 | |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 1154 | // The server is asking for a client certificate during the initial |
| 1155 | // handshake. |
| 1156 | stream_request_.reset(); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1157 | |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1158 | // If the user selected one of the certificates in client_certs or declined |
| 1159 | // to provide one for this server before, use the past decision |
| 1160 | // automatically. |
| 1161 | scoped_refptr<X509Certificate> client_cert; |
| 1162 | bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup( |
| 1163 | response_.cert_request_info->host_and_port, &client_cert); |
| 1164 | if (!found_cached_cert) |
| 1165 | return error; |
| 1166 | |
| 1167 | // Check that the certificate selected is still a certificate the server |
| 1168 | // is likely to accept, based on the criteria supplied in the |
| 1169 | // CertificateRequest message. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1170 | if (client_cert.get()) { |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1171 | const std::vector<std::string>& cert_authorities = |
| 1172 | response_.cert_request_info->cert_authorities; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1173 | |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1174 | bool cert_still_valid = cert_authorities.empty() || |
| 1175 | client_cert->IsIssuedByEncoded(cert_authorities); |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1176 | if (!cert_still_valid) |
| 1177 | return error; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1178 | } |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1179 | |
| 1180 | // TODO(davidben): Add a unit test which covers this path; we need to be |
| 1181 | // able to send a legitimate certificate and also bypass/clear the |
| 1182 | // SSL session cache. |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 1183 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 1184 | &proxy_ssl_config_ : &server_ssl_config_; |
| 1185 | ssl_config->send_client_cert = true; |
| 1186 | ssl_config->client_cert = client_cert; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1187 | next_state_ = STATE_CREATE_STREAM; |
| 1188 | // Reset the other member variables. |
| 1189 | // Note: this is necessary only with SSL renegotiation. |
| 1190 | ResetStateForRestart(); |
| 1191 | return OK; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1192 | } |
| 1193 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1194 | // TODO(rch): This does not correctly handle errors when an SSL proxy is |
| 1195 | // being used, as all of the errors are handled as if they were generated |
| 1196 | // by the endpoint host, request_->url, rather than considering if they were |
[email protected] | 1c53a1f | 2011-01-13 00:36:38 | [diff] [blame] | 1197 | // generated by the SSL proxy. http://crbug.com/69329 |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1198 | int HttpNetworkTransaction::HandleSSLHandshakeError(int error) { |
| 1199 | DCHECK(request_); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 1200 | if (server_ssl_config_.send_client_cert && |
[email protected] | a1cb2cd | 2011-02-05 02:02:08 | [diff] [blame] | 1201 | (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) { |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1202 | session_->ssl_client_auth_cache()->Remove( |
| 1203 | GetHostAndPort(request_->url)); |
| 1204 | } |
| 1205 | |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1206 | uint16 version_max = server_ssl_config_.version_max; |
| 1207 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1208 | switch (error) { |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1209 | case ERR_SSL_PROTOCOL_ERROR: |
| 1210 | case ERR_SSL_VERSION_OR_CIPHER_MISMATCH: |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1211 | if (version_max >= SSL_PROTOCOL_VERSION_TLS1 && |
| 1212 | version_max > server_ssl_config_.version_min) { |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 1213 | // This could be a TLS-intolerant server or a server that chose a |
| 1214 | // cipher suite defined only for higher protocol versions (such as |
| 1215 | // an SSL 3.0 server that chose a TLS-only cipher suite). Fall |
| 1216 | // back to the next lower version and retry. |
| 1217 | // NOTE: if the SSLClientSocket class doesn't support TLS 1.1, |
| 1218 | // specifying TLS 1.1 in version_max will result in a TLS 1.0 |
| 1219 | // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply |
| 1220 | // repeat the TLS 1.0 handshake. To avoid this problem, the default |
| 1221 | // version_max should match the maximum protocol version supported |
| 1222 | // by the SSLClientSocket class. |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1223 | version_max--; |
| 1224 | |
| 1225 | // Fallback to the lower SSL version. |
| 1226 | // While SSL 3.0 fallback should be eliminated because of security |
| 1227 | // reasons, there is a high risk of breaking the servers if this is |
| 1228 | // done in general. |
| 1229 | // For now SSL 3.0 fallback is disabled for Google servers first, |
| 1230 | // and will be expanded to other servers after enough experiences |
| 1231 | // have been gained showing that this experiment works well with |
| 1232 | // today's Internet. |
| 1233 | if (version_max > SSL_PROTOCOL_VERSION_SSL3 || |
| 1234 | (server_ssl_config_.unrestricted_ssl3_fallback_enabled || |
| 1235 | !TransportSecurityState::IsGooglePinnedProperty( |
| 1236 | request_->url.host(), true /* include SNI */))) { |
| 1237 | net_log_.AddEvent( |
| 1238 | NetLog::TYPE_SSL_VERSION_FALLBACK, |
| 1239 | base::Bind(&NetLogSSLVersionFallbackCallback, |
| 1240 | &request_->url, error, server_ssl_config_.version_max, |
| 1241 | version_max)); |
| 1242 | server_ssl_config_.version_max = version_max; |
| 1243 | server_ssl_config_.version_fallback = true; |
| 1244 | ResetConnectionAndRequestForResend(); |
| 1245 | error = OK; |
| 1246 | } |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1247 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1248 | break; |
| 1249 | } |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1250 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1251 | return error; |
| 1252 | } |
| 1253 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1254 | // This method determines whether it is safe to resend the request after an |
| 1255 | // IO error. It can only be called in response to request header or body |
| 1256 | // write errors or response header read errors. It should not be used in |
| 1257 | // other cases, such as a Connect error. |
| 1258 | int HttpNetworkTransaction::HandleIOError(int error) { |
| 1259 | // SSL errors may happen at any time during the stream and indicate issues |
| 1260 | // with the underlying connection. Because the peer may request |
| 1261 | // renegotiation at any time, check and handle any possible SSL handshake |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 1262 | // related errors. In addition to renegotiation, TLS False Start may cause |
| 1263 | // SSL handshake errors (specifically servers with buggy DEFLATE support) |
| 1264 | // to be delayed until the first Read on the underlying connection. |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1265 | error = HandleSSLHandshakeError(error); |
| 1266 | |
| 1267 | switch (error) { |
| 1268 | // If we try to reuse a connection that the server is in the process of |
| 1269 | // closing, we may end up successfully writing out our request (or a |
| 1270 | // portion of our request) only to find a connection error when we try to |
| 1271 | // read from (or finish writing to) the socket. |
| 1272 | case ERR_CONNECTION_RESET: |
| 1273 | case ERR_CONNECTION_CLOSED: |
| 1274 | case ERR_CONNECTION_ABORTED: |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1275 | // There can be a race between the socket pool checking checking whether a |
| 1276 | // socket is still connected, receiving the FIN, and sending/reading data |
| 1277 | // on a reused socket. If we receive the FIN between the connectedness |
| 1278 | // check and writing/reading from the socket, we may first learn the socket |
| 1279 | // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most |
| 1280 | // likely happen when trying to retrieve its IP address. |
| 1281 | // See http://crbug.com/105824 for more details. |
| 1282 | case ERR_SOCKET_NOT_CONNECTED: |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1283 | if (ShouldResendRequest(error)) { |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1284 | net_log_.AddEventWithNetErrorCode( |
| 1285 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1286 | ResetConnectionAndRequestForResend(); |
| 1287 | error = OK; |
| 1288 | } |
| 1289 | break; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 1290 | case ERR_PIPELINE_EVICTION: |
[email protected] | 5477d89 | 2012-03-01 21:31:31 | [diff] [blame] | 1291 | if (!session_->force_http_pipelining()) { |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1292 | net_log_.AddEventWithNetErrorCode( |
| 1293 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | 5477d89 | 2012-03-01 21:31:31 | [diff] [blame] | 1294 | ResetConnectionAndRequestForResend(); |
| 1295 | error = OK; |
| 1296 | } |
| 1297 | break; |
[email protected] | 8753a12 | 2011-10-16 08:05:08 | [diff] [blame] | 1298 | case ERR_SPDY_PING_FAILED: |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1299 | case ERR_SPDY_SERVER_REFUSED_STREAM: |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1300 | net_log_.AddEventWithNetErrorCode( |
| 1301 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1302 | ResetConnectionAndRequestForResend(); |
| 1303 | error = OK; |
| 1304 | break; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1305 | } |
| 1306 | return error; |
| 1307 | } |
| 1308 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1309 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1310 | ResetStateForAuthRestart(); |
| 1311 | stream_.reset(); |
| 1312 | } |
| 1313 | |
| 1314 | void HttpNetworkTransaction::ResetStateForAuthRestart() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1315 | send_start_time_ = base::TimeTicks(); |
| 1316 | send_end_time_ = base::TimeTicks(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1317 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1318 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1319 | read_buf_ = NULL; |
| 1320 | read_buf_len_ = 0; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1321 | headers_valid_ = false; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1322 | request_headers_.Clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1323 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1324 | establishing_tunnel_ = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1328 | return response_.headers.get(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1329 | } |
| 1330 | |
[email protected] | a19f1c60 | 2009-08-24 21:35:28 | [diff] [blame] | 1331 | bool HttpNetworkTransaction::ShouldResendRequest(int error) const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1332 | bool connection_is_proven = stream_->IsConnectionReused(); |
| 1333 | bool has_received_headers = GetResponseHeaders() != NULL; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1334 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1335 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1336 | // This automatically prevents an infinite resend loop because we'll run |
| 1337 | // out of the cached keep-alive connections eventually. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1338 | if (connection_is_proven && !has_received_headers) |
| 1339 | return true; |
| 1340 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1344 | if (stream_.get()) { |
| 1345 | stream_->Close(true); |
| 1346 | stream_.reset(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1347 | } |
| 1348 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1349 | // We need to clear request_headers_ because it contains the real request |
| 1350 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1351 | // the SSL tunnel. |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1352 | request_headers_.Clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1353 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1354 | } |
| 1355 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1356 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1357 | return !is_https_request() && |
| 1358 | (proxy_info_.is_https() || proxy_info_.is_http()); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1359 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1360 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1361 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1362 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1363 | } |
| 1364 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1365 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1366 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1367 | DCHECK(headers.get()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1368 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1369 | int status = headers->response_code(); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1370 | if (status != HTTP_UNAUTHORIZED && |
| 1371 | status != HTTP_PROXY_AUTHENTICATION_REQUIRED) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1372 | return OK; |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1373 | HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1374 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1375 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1376 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1377 | |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1378 | // This case can trigger when an HTTPS server responds with a "Proxy |
| 1379 | // authentication required" status code through a non-authenticating |
| 1380 | // proxy. |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1381 | if (!auth_controllers_[target].get()) |
| 1382 | return ERR_UNEXPECTED_PROXY_AUTH; |
| 1383 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1384 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1385 | headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
| 1386 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1387 | if (auth_controllers_[target]->HaveAuthHandler()) |
| 1388 | pending_auth_target_ = target; |
| 1389 | |
| 1390 | scoped_refptr<AuthChallengeInfo> auth_info = |
| 1391 | auth_controllers_[target]->auth_info(); |
| 1392 | if (auth_info.get()) |
| 1393 | response_.auth_challenge = auth_info; |
| 1394 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1395 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1396 | } |
| 1397 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1398 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1399 | return auth_controllers_[target].get() && |
| 1400 | auth_controllers_[target]->HaveAuth(); |
| 1401 | } |
| 1402 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1403 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1404 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1405 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1406 | if (!proxy_info_.proxy_server().is_valid() || |
| 1407 | proxy_info_.proxy_server().is_direct()) { |
| 1408 | return GURL(); // There is no proxy server. |
| 1409 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1410 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1411 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1412 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1413 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1414 | case HttpAuth::AUTH_SERVER: |
| 1415 | return request_->url; |
| 1416 | default: |
| 1417 | return GURL(); |
| 1418 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1419 | } |
| 1420 | |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1421 | #define STATE_CASE(s) \ |
| 1422 | case s: \ |
| 1423 | description = base::StringPrintf("%s (0x%08X)", #s, s); \ |
| 1424 | break |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1425 | |
| 1426 | std::string HttpNetworkTransaction::DescribeState(State state) { |
| 1427 | std::string description; |
| 1428 | switch (state) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1429 | STATE_CASE(STATE_CREATE_STREAM); |
| 1430 | STATE_CASE(STATE_CREATE_STREAM_COMPLETE); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1431 | STATE_CASE(STATE_INIT_REQUEST_BODY); |
| 1432 | STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1433 | STATE_CASE(STATE_BUILD_REQUEST); |
| 1434 | STATE_CASE(STATE_BUILD_REQUEST_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1435 | STATE_CASE(STATE_SEND_REQUEST); |
| 1436 | STATE_CASE(STATE_SEND_REQUEST_COMPLETE); |
| 1437 | STATE_CASE(STATE_READ_HEADERS); |
| 1438 | STATE_CASE(STATE_READ_HEADERS_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1439 | STATE_CASE(STATE_READ_BODY); |
| 1440 | STATE_CASE(STATE_READ_BODY_COMPLETE); |
| 1441 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART); |
| 1442 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1443 | STATE_CASE(STATE_NONE); |
| 1444 | default: |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1445 | description = base::StringPrintf("Unknown state 0x%08X (%u)", state, |
| 1446 | state); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1447 | break; |
| 1448 | } |
| 1449 | return description; |
| 1450 | } |
| 1451 | |
| 1452 | #undef STATE_CASE |
| 1453 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1454 | } // namespace net |