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