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