[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> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11 | #include "base/base64url.h" |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/bind_helpers.h" |
Bence Béky | bc1de29 | 2018-02-01 15:48:03 | [diff] [blame] | 14 | #include "base/callback_helpers.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 16 | #include "base/format_macros.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 17 | #include "base/metrics/field_trial.h" |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 18 | #include "base/metrics/histogram_functions.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 20 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 21 | #include "base/stl_util.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
| 23 | #include "base/strings/string_util.h" |
[email protected] | 3d498f7 | 2013-10-28 21:17:40 | [diff] [blame] | 24 | #include "base/time/time.h" |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 25 | #include "base/values.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 26 | #include "build/build_config.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 27 | #include "net/base/auth.h" |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 28 | #include "net/base/host_port_pair.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 29 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 31 | #include "net/base/load_timing_info.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 32 | #include "net/base/net_errors.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 33 | #include "net/base/proxy_server.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/base/upload_data_stream.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 35 | #include "net/base/url_util.h" |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 36 | #include "net/cert/cert_status_flags.h" |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 37 | #include "net/filter/filter_source_stream.h" |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 38 | #include "net/http/bidirectional_stream_impl.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | #include "net/http/http_auth.h" |
| 40 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 41 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 42 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_chunked_decoder.h" |
| 44 | #include "net/http/http_network_session.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 45 | #include "net/http/http_proxy_client_socket.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 46 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 48 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 49 | #include "net/http/http_response_info.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 50 | #include "net/http/http_server_properties.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 51 | #include "net/http/http_status_code.h" |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 52 | #include "net/http/http_stream.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 53 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "net/http/http_util.h" |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 55 | #include "net/http/transport_security_state.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 56 | #include "net/http/url_security_manager.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 57 | #include "net/log/net_log_event_type.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 58 | #include "net/socket/client_socket_factory.h" |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 59 | #include "net/socket/next_proto.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 60 | #include "net/socket/transport_client_socket_pool.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 61 | #include "net/spdy/spdy_http_stream.h" |
| 62 | #include "net/spdy/spdy_session.h" |
| 63 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 64 | #include "net/ssl/ssl_cert_request_info.h" |
| 65 | #include "net/ssl/ssl_connection_status_flags.h" |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 66 | #include "net/ssl/ssl_info.h" |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 67 | #include "net/ssl/ssl_private_key.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 68 | #include "url/gurl.h" |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 69 | #include "url/url_canon.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 71 | #if BUILDFLAG(ENABLE_REPORTING) |
| 72 | #include "net/network_error_logging/network_error_logging_service.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 73 | #include "net/reporting/reporting_header_parser.h" |
| 74 | #include "net/reporting/reporting_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 75 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 76 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 77 | namespace net { |
| 78 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 79 | namespace { |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 80 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 81 | // Max number of |retry_attempts| (excluding the initial request) after which |
| 82 | // we give up and show an error page. |
| 83 | const size_t kMaxRetryAttempts = 2; |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 84 | |
| 85 | // Max number of calls to RestartWith* allowed for a single connection. A single |
| 86 | // HttpNetworkTransaction should not signal very many restartable errors, but it |
| 87 | // may occur due to a bug (e.g. https://crbug.com/823387 or |
| 88 | // https://crbug.com/488043) or simply if the server or proxy requests |
| 89 | // authentication repeatedly. Although these calls are often associated with a |
| 90 | // user prompt, in other scenarios (remembered preferences, extensions, |
| 91 | // multi-leg authentication), they may be triggered automatically. To avoid |
| 92 | // looping forever, bound the number of restarts. |
| 93 | const size_t kMaxRestarts = 32; |
| 94 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 95 | void SetProxyInfoInReponse(const ProxyInfo& proxy_info, |
| 96 | HttpResponseInfo* response_info) { |
| 97 | response_info->was_fetched_via_proxy = !proxy_info.is_direct(); |
| 98 | if (response_info->was_fetched_via_proxy && !proxy_info.is_empty()) |
| 99 | response_info->proxy_server = proxy_info.proxy_server(); |
| 100 | else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct()) |
| 101 | response_info->proxy_server = ProxyServer::Direct(); |
| 102 | else |
| 103 | response_info->proxy_server = ProxyServer(); |
| 104 | } |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 105 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 106 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 107 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 108 | const int HttpNetworkTransaction::kDrainBodyBufferSize; |
| 109 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 110 | HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority, |
| 111 | HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 112 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
Bence Béky | 61f756c | 2018-04-25 14:17:53 | [diff] [blame] | 113 | io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete, |
| 114 | base::Unretained(this))), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | session_(session), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 116 | request_(nullptr), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 117 | priority_(priority), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 118 | headers_valid_(false), |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 119 | can_send_early_data_(false), |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 120 | server_ssl_client_cert_was_cached_(false), |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 121 | request_headers_(), |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 122 | #if BUILDFLAG(ENABLE_REPORTING) |
| 123 | network_error_logging_report_generated_(false), |
| 124 | request_reporting_upload_depth_(0), |
| 125 | #endif // BUILDFLAG(ENABLE_REPORTING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | read_buf_len_(0), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 127 | total_received_bytes_(0), |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 128 | total_sent_bytes_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 129 | next_state_(STATE_NONE), |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 130 | establishing_tunnel_(false), |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 131 | enable_ip_based_pooling_(true), |
bnc | accd496 | 2017-04-06 21:00:26 | [diff] [blame] | 132 | enable_alternative_services_(true), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 133 | websocket_handshake_stream_base_create_helper_(nullptr), |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 134 | net_error_details_(), |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 135 | retry_attempts_(0), |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame^] | 136 | num_restarts_(0) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 137 | } |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 138 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 139 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 140 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 141 | // If no error or success report has been generated yet at this point, then |
| 142 | // this network transaction was prematurely cancelled. |
| 143 | GenerateNetworkErrorLoggingReport(ERR_ABORTED); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 144 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 145 | if (stream_.get()) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 146 | // TODO(mbelshe): The stream_ should be able to compute whether or not the |
| 147 | // stream should be kept alive. No reason to compute here |
| 148 | // and pass it in. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 149 | if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 150 | stream_->Close(true /* not reusable */); |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 151 | } else if (stream_->IsResponseBodyComplete()) { |
| 152 | // If the response body is complete, we can just reuse the socket. |
| 153 | stream_->Close(false /* reusable */); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 154 | } else { |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 155 | // Otherwise, we try to drain the response body. |
| 156 | HttpStream* stream = stream_.release(); |
| 157 | stream->Drain(session_); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 158 | } |
| 159 | } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 160 | if (request_ && request_->upload_data_stream) |
| 161 | request_->upload_data_stream->Reset(); // Invalidate pending callbacks. |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 164 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 165 | CompletionOnceCallback callback, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 166 | const NetLogWithSource& net_log) { |
Batalov Vladislav | a4e97a50 | 2019-04-11 15:35:23 | [diff] [blame] | 167 | if (request_info->load_flags & LOAD_ONLY_FROM_CACHE) |
| 168 | return ERR_CACHE_MISS; |
| 169 | |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 170 | DCHECK(request_info->traffic_annotation.is_valid()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 171 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 172 | request_ = request_info; |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 173 | url_ = request_->url; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 174 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 175 | // Store values for later use in NEL report generation. |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 176 | request_method_ = request_->method; |
| 177 | request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer, |
| 178 | &request_referrer_); |
| 179 | request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent, |
| 180 | &request_user_agent_); |
| 181 | request_reporting_upload_depth_ = request_->reporting_upload_depth; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 182 | start_timeticks_ = base::TimeTicks::Now(); |
| 183 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 184 | |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 185 | // Now that we have an HttpRequestInfo object, update server_ssl_config_. |
| 186 | session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_); |
| 187 | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 188 | if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) { |
| 189 | server_ssl_config_.disable_cert_verification_network_fetches = true; |
| 190 | proxy_ssl_config_.disable_cert_verification_network_fetches = true; |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 191 | } |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 192 | |
Steven Valdez | 03e872d | 2018-03-02 15:39:15 | [diff] [blame] | 193 | if (HttpUtil::IsMethodSafe(request_info->method)) { |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 194 | can_send_early_data_ = true; |
| 195 | } |
| 196 | |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 197 | if (request_->load_flags & LOAD_PREFETCH) |
| 198 | response_.unused_since_prefetch = true; |
| 199 | |
Helen Li | b495c380 | 2018-03-30 13:46:09 | [diff] [blame] | 200 | next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 201 | int rv = DoLoop(OK); |
| 202 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 203 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 204 | |
| 205 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 206 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 207 | // other net::Error can be returned. |
| 208 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 209 | return rv; |
| 210 | } |
| 211 | |
| 212 | int HttpNetworkTransaction::RestartIgnoringLastError( |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 213 | CompletionOnceCallback callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 214 | DCHECK(!stream_.get()); |
| 215 | DCHECK(!stream_request_.get()); |
| 216 | DCHECK_EQ(STATE_NONE, next_state_); |
| 217 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 218 | if (!CheckMaxRestarts()) |
| 219 | return ERR_TOO_MANY_RETRIES; |
| 220 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 221 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 222 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 223 | int rv = DoLoop(OK); |
| 224 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 225 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 226 | |
| 227 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 228 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 229 | // other net::Error can be returned. |
| 230 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 231 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 234 | int HttpNetworkTransaction::RestartWithCertificate( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 235 | scoped_refptr<X509Certificate> client_cert, |
| 236 | scoped_refptr<SSLPrivateKey> client_private_key, |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 237 | CompletionOnceCallback callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 238 | // In HandleCertificateRequest(), we always tear down existing stream |
| 239 | // requests to force a new connection. So we shouldn't have one here. |
| 240 | DCHECK(!stream_request_.get()); |
| 241 | DCHECK(!stream_.get()); |
| 242 | DCHECK_EQ(STATE_NONE, next_state_); |
| 243 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 244 | if (!CheckMaxRestarts()) |
| 245 | return ERR_TOO_MANY_RETRIES; |
| 246 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 247 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 248 | &proxy_ssl_config_ : &server_ssl_config_; |
| 249 | ssl_config->send_client_cert = true; |
| 250 | ssl_config->client_cert = client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 251 | ssl_config->client_private_key = client_private_key; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 252 | session_->ssl_client_auth_cache()->Add( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 253 | response_.cert_request_info->host_and_port, std::move(client_cert), |
| 254 | std::move(client_private_key)); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 255 | // Reset the other member variables. |
| 256 | // Note: this is necessary only with SSL renegotiation. |
| 257 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 258 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 259 | int rv = DoLoop(OK); |
| 260 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 261 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 262 | |
| 263 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 264 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 265 | // other net::Error can be returned. |
| 266 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 267 | return rv; |
| 268 | } |
| 269 | |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 270 | int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials, |
| 271 | CompletionOnceCallback callback) { |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 272 | if (!CheckMaxRestarts()) |
| 273 | return ERR_TOO_MANY_RETRIES; |
| 274 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 275 | HttpAuth::Target target = pending_auth_target_; |
| 276 | if (target == HttpAuth::AUTH_NONE) { |
| 277 | NOTREACHED(); |
| 278 | return ERR_UNEXPECTED; |
| 279 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 280 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 281 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 282 | auth_controllers_[target]->ResetAuth(credentials); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 283 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 284 | DCHECK(callback_.is_null()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 285 | |
| 286 | int rv = OK; |
| 287 | if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) { |
| 288 | // In this case, we've gathered credentials for use with proxy |
| 289 | // authentication of a tunnel. |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 290 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 291 | DCHECK(stream_request_ != nullptr); |
| 292 | auth_controllers_[target] = nullptr; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 293 | ResetStateForRestart(); |
asanka | 681f02d | 2017-02-22 17:06:39 | [diff] [blame] | 294 | rv = stream_request_->RestartTunnelWithProxyAuth(); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 295 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 296 | // In this case, we've gathered credentials for the server or the proxy |
| 297 | // but it is not during the tunneling phase. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 298 | DCHECK(stream_request_ == nullptr); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 299 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 300 | rv = DoLoop(OK); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 301 | // Note: If an error is encountered while draining the old response body, no |
| 302 | // Network Error Logging report will be generated, because the error was |
| 303 | // with the old request, which will already have had a NEL report generated |
| 304 | // for it due to the auth challenge (so we don't report a second error for |
| 305 | // that request). |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 306 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 307 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 308 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 309 | callback_ = std::move(callback); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 310 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 313 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 314 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 315 | DCHECK(!stream_request_.get()); |
| 316 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 317 | // Authorization schemes incompatible with HTTP/2 are unsupported for proxies. |
| 318 | if (target == HttpAuth::AUTH_SERVER && |
| 319 | auth_controllers_[target]->NeedsHTTP11()) { |
| 320 | session_->http_server_properties()->SetHTTP11Required( |
| 321 | HostPortPair::FromURL(request_->url)); |
| 322 | } |
| 323 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 324 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 325 | // Even if the server says the connection is keep-alive, we have to be |
| 326 | // able to find the end of each response in order to reuse the connection. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 327 | if (stream_->CanReuseConnection()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 328 | // If the response body hasn't been completely read, we need to drain |
| 329 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 330 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 331 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 332 | read_buf_ = base::MakeRefCounted<IOBuffer>( |
| 333 | kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 334 | read_buf_len_ = kDrainBodyBufferSize; |
| 335 | return; |
| 336 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 337 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 340 | // We don't need to drain the response body, so we act as if we had drained |
| 341 | // the response body. |
| 342 | DidDrainBodyForAuthRestart(keep_alive); |
| 343 | } |
| 344 | |
| 345 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 346 | DCHECK(!stream_request_.get()); |
| 347 | |
| 348 | if (stream_.get()) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 349 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 350 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 351 | HttpStream* new_stream = nullptr; |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 352 | if (keep_alive && stream_->CanReuseConnection()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 353 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 354 | // often enough to be worth the trouble. |
| 355 | stream_->SetConnectionReused(); |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 356 | new_stream = stream_->RenewStreamForAuth(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 357 | } |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 358 | |
| 359 | if (!new_stream) { |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 360 | // Close the stream and mark it as not_reusable. Even in the |
| 361 | // keep_alive case, we've determined that the stream_ is not |
| 362 | // reusable if new_stream is NULL. |
| 363 | stream_->Close(true); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 364 | next_state_ = STATE_CREATE_STREAM; |
| 365 | } else { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 366 | // Renewed streams shouldn't carry over sent or received bytes. |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 367 | DCHECK_EQ(0, new_stream->GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 368 | DCHECK_EQ(0, new_stream->GetTotalSentBytes()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 369 | next_state_ = STATE_INIT_STREAM; |
| 370 | } |
| 371 | stream_.reset(new_stream); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 372 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 373 | |
| 374 | // Reset the other member variables. |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 375 | ResetStateForAuthRestart(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 378 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 379 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 380 | HaveAuth(pending_auth_target_); |
| 381 | } |
| 382 | |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 383 | int HttpNetworkTransaction::Read(IOBuffer* buf, |
| 384 | int buf_len, |
| 385 | CompletionOnceCallback callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 386 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 387 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 388 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 389 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 390 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 391 | // 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] | 392 | // 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] | 393 | // bytes when establishing a tunnel because they might be controlled by |
| 394 | // an active network attacker. We don't worry about this for HTTP |
| 395 | // because an active network attacker can already control HTTP sessions. |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 396 | // We reach this case when the user cancels a 407 proxy auth prompt. We |
| 397 | // also don't worry about this for an HTTPS Proxy, because the |
| 398 | // communication with the proxy is secure. |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 399 | // See http://crbug.com/8473. |
Yixin Wang | 46a273ec30 | 2018-01-23 17:59:56 | [diff] [blame] | 400 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https() || |
| 401 | proxy_info_.is_quic()); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 402 | DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 403 | LOG(WARNING) << "Blocked proxy response with status " |
| 404 | << headers->response_code() << " to CONNECT request for " |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 405 | << GetHostAndPort(url_) << "."; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 406 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 409 | // Are we using SPDY or HTTP? |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 410 | next_state_ = STATE_READ_BODY; |
| 411 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 412 | read_buf_ = buf; |
| 413 | read_buf_len_ = buf_len; |
| 414 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 415 | int rv = DoLoop(OK); |
| 416 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 417 | callback_ = std::move(callback); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 418 | return rv; |
| 419 | } |
| 420 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 421 | void HttpNetworkTransaction::StopCaching() {} |
| 422 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 423 | bool HttpNetworkTransaction::GetFullRequestHeaders( |
| 424 | HttpRequestHeaders* headers) const { |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 425 | // TODO(juliatuttle): Make sure we've populated request_headers_. |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 426 | *headers = request_headers_; |
| 427 | return true; |
| 428 | } |
| 429 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 430 | int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const { |
| 431 | int64_t total_received_bytes = total_received_bytes_; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 432 | if (stream_) |
| 433 | total_received_bytes += stream_->GetTotalReceivedBytes(); |
| 434 | return total_received_bytes; |
| 435 | } |
| 436 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 437 | int64_t HttpNetworkTransaction::GetTotalSentBytes() const { |
| 438 | int64_t total_sent_bytes = total_sent_bytes_; |
| 439 | if (stream_) |
| 440 | total_sent_bytes += stream_->GetTotalSentBytes(); |
| 441 | return total_sent_bytes; |
| 442 | } |
| 443 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 444 | void HttpNetworkTransaction::DoneReading() {} |
| 445 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 446 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 447 | return &response_; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 451 | // TODO(wtc): Define a new LoadState value for the |
| 452 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 453 | switch (next_state_) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 454 | case STATE_CREATE_STREAM: |
| 455 | return LOAD_STATE_WAITING_FOR_DELEGATE; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 456 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 457 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 458 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 459 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 460 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 461 | return LOAD_STATE_SENDING_REQUEST; |
| 462 | case STATE_READ_HEADERS_COMPLETE: |
| 463 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 464 | case STATE_READ_BODY_COMPLETE: |
| 465 | return LOAD_STATE_READING_RESPONSE; |
| 466 | default: |
| 467 | return LOAD_STATE_IDLE; |
| 468 | } |
| 469 | } |
| 470 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 471 | void HttpNetworkTransaction::SetQuicServerInfo( |
| 472 | QuicServerInfo* quic_server_info) {} |
| 473 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 474 | bool HttpNetworkTransaction::GetLoadTimingInfo( |
| 475 | LoadTimingInfo* load_timing_info) const { |
| 476 | if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info)) |
| 477 | return false; |
| 478 | |
| 479 | load_timing_info->proxy_resolve_start = |
| 480 | proxy_info_.proxy_resolve_start_time(); |
| 481 | load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time(); |
| 482 | load_timing_info->send_start = send_start_time_; |
| 483 | load_timing_info->send_end = send_end_time_; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 484 | return true; |
| 485 | } |
| 486 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 487 | bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const { |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 488 | if (remote_endpoint_.address().empty()) |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 489 | return false; |
| 490 | |
| 491 | *endpoint = remote_endpoint_; |
| 492 | return true; |
| 493 | } |
| 494 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 495 | void HttpNetworkTransaction::PopulateNetErrorDetails( |
| 496 | NetErrorDetails* details) const { |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 497 | *details = net_error_details_; |
| 498 | if (stream_) |
| 499 | stream_->PopulateNetErrorDetails(details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 500 | } |
| 501 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 502 | void HttpNetworkTransaction::SetPriority(RequestPriority priority) { |
| 503 | priority_ = priority; |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 504 | |
[email protected] | bf82898 | 2013-08-14 18:01:47 | [diff] [blame] | 505 | if (stream_request_) |
| 506 | stream_request_->SetPriority(priority); |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 507 | if (stream_) |
| 508 | stream_->SetPriority(priority); |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 509 | |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 510 | // The above call may have resulted in deleting |*this|. |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 511 | } |
| 512 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 513 | void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 514 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 515 | websocket_handshake_stream_base_create_helper_ = create_helper; |
| 516 | } |
| 517 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 518 | void HttpNetworkTransaction::SetBeforeNetworkStartCallback( |
| 519 | const BeforeNetworkStartCallback& callback) { |
| 520 | before_network_start_callback_ = callback; |
| 521 | } |
| 522 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 523 | void HttpNetworkTransaction::SetBeforeHeadersSentCallback( |
| 524 | const BeforeHeadersSentCallback& callback) { |
| 525 | before_headers_sent_callback_ = callback; |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 526 | } |
| 527 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 528 | void HttpNetworkTransaction::SetRequestHeadersCallback( |
| 529 | RequestHeadersCallback callback) { |
| 530 | DCHECK(!stream_); |
| 531 | request_headers_callback_ = std::move(callback); |
| 532 | } |
| 533 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 534 | void HttpNetworkTransaction::SetResponseHeadersCallback( |
| 535 | ResponseHeadersCallback callback) { |
| 536 | DCHECK(!stream_); |
| 537 | response_headers_callback_ = std::move(callback); |
| 538 | } |
| 539 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 540 | int HttpNetworkTransaction::ResumeNetworkStart() { |
| 541 | DCHECK_EQ(next_state_, STATE_CREATE_STREAM); |
| 542 | return DoLoop(OK); |
| 543 | } |
| 544 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 545 | void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config, |
| 546 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 547 | std::unique_ptr<HttpStream> stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 548 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 549 | DCHECK(stream_request_.get()); |
| 550 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 551 | if (stream_) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 552 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 553 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 554 | } |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 555 | stream_ = std::move(stream); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 556 | stream_->SetRequestHeadersCallback(request_headers_callback_); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 557 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 558 | proxy_info_ = used_proxy_info; |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 559 | response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 560 | response_.alpn_negotiated_protocol = |
| 561 | NextProtoToString(stream_request_->negotiated_protocol()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 562 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 563 | SetProxyInfoInReponse(used_proxy_info, &response_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 564 | OnIOComplete(OK); |
| 565 | } |
| 566 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 567 | void HttpNetworkTransaction::OnBidirectionalStreamImplReady( |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 568 | const SSLConfig& used_ssl_config, |
| 569 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 570 | std::unique_ptr<BidirectionalStreamImpl> stream) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 571 | NOTREACHED(); |
| 572 | } |
| 573 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 574 | void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady( |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 575 | const SSLConfig& used_ssl_config, |
| 576 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 577 | std::unique_ptr<WebSocketHandshakeStreamBase> stream) { |
| 578 | OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream)); |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 579 | } |
| 580 | |
Ryan Hamilton | 75f19726 | 2017-08-17 14:00:07 | [diff] [blame] | 581 | void HttpNetworkTransaction::OnStreamFailed( |
| 582 | int result, |
| 583 | const NetErrorDetails& net_error_details, |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 584 | const SSLConfig& used_ssl_config, |
| 585 | const ProxyInfo& used_proxy_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 586 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 587 | DCHECK_NE(OK, result); |
| 588 | DCHECK(stream_request_.get()); |
| 589 | DCHECK(!stream_.get()); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 590 | server_ssl_config_ = used_ssl_config; |
Ryan Hamilton | 75f19726 | 2017-08-17 14:00:07 | [diff] [blame] | 591 | net_error_details_ = net_error_details; |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 592 | proxy_info_ = used_proxy_info; |
| 593 | SetProxyInfoInReponse(used_proxy_info, &response_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 594 | |
| 595 | OnIOComplete(result); |
| 596 | } |
| 597 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 598 | void HttpNetworkTransaction::OnCertificateError( |
| 599 | int result, |
| 600 | const SSLConfig& used_ssl_config, |
| 601 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 602 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 603 | DCHECK_NE(OK, result); |
| 604 | DCHECK(stream_request_.get()); |
| 605 | DCHECK(!stream_.get()); |
| 606 | |
| 607 | response_.ssl_info = ssl_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 608 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 609 | |
| 610 | // TODO(mbelshe): For now, we're going to pass the error through, and that |
| 611 | // 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] | 612 | // going to restart an entire STATE_CREATE_STREAM, even if the connection is |
| 613 | // good and the user chooses to ignore the error. This is not ideal, but not |
| 614 | // the end of the world either. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 615 | |
| 616 | OnIOComplete(result); |
| 617 | } |
| 618 | |
| 619 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 620 | const HttpResponseInfo& proxy_response, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 621 | const SSLConfig& used_ssl_config, |
| 622 | const ProxyInfo& used_proxy_info, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 623 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 624 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 625 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 626 | |
| 627 | establishing_tunnel_ = true; |
| 628 | response_.headers = proxy_response.headers; |
| 629 | response_.auth_challenge = proxy_response.auth_challenge; |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 630 | response_.did_use_http_auth = proxy_response.did_use_http_auth; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 631 | |
| 632 | if (response_.headers.get() && !ContentEncodingsValid()) { |
| 633 | DoCallback(ERR_CONTENT_DECODING_FAILED); |
| 634 | return; |
| 635 | } |
| 636 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 637 | headers_valid_ = true; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 638 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 639 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 640 | |
| 641 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 642 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 643 | |
| 644 | DoCallback(OK); |
| 645 | } |
| 646 | |
| 647 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 648 | const SSLConfig& used_ssl_config, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 649 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 650 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 651 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 652 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 653 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 654 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 655 | } |
| 656 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 657 | void HttpNetworkTransaction::OnQuicBroken() { |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 658 | net_error_details_.quic_broken = true; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 659 | } |
| 660 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 661 | void HttpNetworkTransaction::GetConnectionAttempts( |
| 662 | ConnectionAttempts* out) const { |
| 663 | *out = connection_attempts_; |
| 664 | } |
| 665 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 666 | bool HttpNetworkTransaction::IsSecureRequest() const { |
lgarron | a774b92 | 2015-05-14 22:56:37 | [diff] [blame] | 667 | return request_->url.SchemeIsCryptographic(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 668 | } |
| 669 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 670 | bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const { |
tbansal | 7cec381 | 2015-02-05 21:25:12 | [diff] [blame] | 671 | return (proxy_info_.is_http() || proxy_info_.is_https() || |
| 672 | proxy_info_.is_quic()) && |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 673 | !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS()); |
| 674 | } |
| 675 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 676 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 677 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 678 | DCHECK(!callback_.is_null()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 679 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 680 | #if BUILDFLAG(ENABLE_REPORTING) |
| 681 | // Just before invoking the caller's completion callback, generate a NEL |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 682 | // report about this network request if the result was an error. |
| 683 | GenerateNetworkErrorLoggingReportIfError(rv); |
| 684 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 685 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 686 | // Since Run may result in Read being called, clear user_callback_ up front. |
Bence Béky | bc1de29 | 2018-02-01 15:48:03 | [diff] [blame] | 687 | base::ResetAndReturn(&callback_).Run(rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 691 | int rv = DoLoop(result); |
| 692 | if (rv != ERR_IO_PENDING) |
| 693 | DoCallback(rv); |
| 694 | } |
| 695 | |
| 696 | int HttpNetworkTransaction::DoLoop(int result) { |
| 697 | DCHECK(next_state_ != STATE_NONE); |
| 698 | |
| 699 | int rv = result; |
| 700 | do { |
| 701 | State state = next_state_; |
| 702 | next_state_ = STATE_NONE; |
| 703 | switch (state) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 704 | case STATE_NOTIFY_BEFORE_CREATE_STREAM: |
| 705 | DCHECK_EQ(OK, rv); |
| 706 | rv = DoNotifyBeforeCreateStream(); |
| 707 | break; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 708 | case STATE_CREATE_STREAM: |
| 709 | DCHECK_EQ(OK, rv); |
| 710 | rv = DoCreateStream(); |
| 711 | break; |
| 712 | case STATE_CREATE_STREAM_COMPLETE: |
zhongyi | 3aedde9 | 2016-10-14 02:29:46 | [diff] [blame] | 713 | // TODO(zhongyi): remove liveness checks when crbug.com/652868 is |
| 714 | // solved. |
| 715 | net_log_.CrashIfInvalid(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 716 | rv = DoCreateStreamComplete(rv); |
zhongyi | 3aedde9 | 2016-10-14 02:29:46 | [diff] [blame] | 717 | net_log_.CrashIfInvalid(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 718 | break; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 719 | case STATE_INIT_STREAM: |
| 720 | DCHECK_EQ(OK, rv); |
| 721 | rv = DoInitStream(); |
| 722 | break; |
| 723 | case STATE_INIT_STREAM_COMPLETE: |
| 724 | rv = DoInitStreamComplete(rv); |
| 725 | break; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 726 | case STATE_GENERATE_PROXY_AUTH_TOKEN: |
| 727 | DCHECK_EQ(OK, rv); |
| 728 | rv = DoGenerateProxyAuthToken(); |
| 729 | break; |
| 730 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 731 | rv = DoGenerateProxyAuthTokenComplete(rv); |
| 732 | break; |
| 733 | case STATE_GENERATE_SERVER_AUTH_TOKEN: |
| 734 | DCHECK_EQ(OK, rv); |
| 735 | rv = DoGenerateServerAuthToken(); |
| 736 | break; |
| 737 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
| 738 | rv = DoGenerateServerAuthTokenComplete(rv); |
| 739 | break; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 740 | case STATE_INIT_REQUEST_BODY: |
| 741 | DCHECK_EQ(OK, rv); |
| 742 | rv = DoInitRequestBody(); |
| 743 | break; |
| 744 | case STATE_INIT_REQUEST_BODY_COMPLETE: |
| 745 | rv = DoInitRequestBodyComplete(rv); |
| 746 | break; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 747 | case STATE_BUILD_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 748 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 749 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 750 | rv = DoBuildRequest(); |
| 751 | break; |
| 752 | case STATE_BUILD_REQUEST_COMPLETE: |
| 753 | rv = DoBuildRequestComplete(rv); |
| 754 | break; |
| 755 | case STATE_SEND_REQUEST: |
| 756 | DCHECK_EQ(OK, rv); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 757 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 758 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 759 | case STATE_SEND_REQUEST_COMPLETE: |
| 760 | rv = DoSendRequestComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 761 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 762 | NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 763 | break; |
| 764 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 765 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 766 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 767 | rv = DoReadHeaders(); |
| 768 | break; |
| 769 | case STATE_READ_HEADERS_COMPLETE: |
| 770 | rv = DoReadHeadersComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 771 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 772 | NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 773 | break; |
| 774 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 775 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 776 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 777 | rv = DoReadBody(); |
| 778 | break; |
| 779 | case STATE_READ_BODY_COMPLETE: |
| 780 | rv = DoReadBodyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 781 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 782 | NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 783 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 784 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 785 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 786 | net_log_.BeginEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 787 | NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 788 | rv = DoDrainBodyForAuthRestart(); |
| 789 | break; |
| 790 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 791 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 792 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 793 | NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 794 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 795 | default: |
| 796 | NOTREACHED() << "bad state"; |
| 797 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 798 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 799 | } |
| 800 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 801 | |
| 802 | return rv; |
| 803 | } |
| 804 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 805 | int HttpNetworkTransaction::DoNotifyBeforeCreateStream() { |
| 806 | next_state_ = STATE_CREATE_STREAM; |
| 807 | bool defer = false; |
| 808 | if (!before_network_start_callback_.is_null()) |
| 809 | before_network_start_callback_.Run(&defer); |
| 810 | if (!defer) |
| 811 | return OK; |
| 812 | return ERR_IO_PENDING; |
| 813 | } |
| 814 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 815 | int HttpNetworkTransaction::DoCreateStream() { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 816 | response_.network_accessed = true; |
| 817 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 818 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 819 | // IP based pooling is only enabled on a retry after 421 Misdirected Request |
| 820 | // is received. Alternative Services are also disabled in this case (though |
| 821 | // they can also be disabled when retrying after a QUIC error). |
| 822 | if (!enable_ip_based_pooling_) |
| 823 | DCHECK(!enable_alternative_services_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 824 | if (ForWebSocketHandshake()) { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 825 | stream_request_ = |
Bence Béky | 8cae04e | 2018-01-15 18:37:06 | [diff] [blame] | 826 | session_->http_stream_factory()->RequestWebSocketHandshakeStream( |
| 827 | *request_, priority_, server_ssl_config_, proxy_ssl_config_, this, |
| 828 | websocket_handshake_stream_base_create_helper_, |
| 829 | enable_ip_based_pooling_, enable_alternative_services_, net_log_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 830 | } else { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 831 | stream_request_ = session_->http_stream_factory()->RequestStream( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 832 | *request_, priority_, server_ssl_config_, proxy_ssl_config_, this, |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 833 | enable_ip_based_pooling_, enable_alternative_services_, net_log_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 834 | } |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 835 | DCHECK(stream_request_.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 836 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 837 | } |
| 838 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 839 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 840 | CopyConnectionAttemptsFromStreamRequest(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 841 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 842 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 843 | DCHECK(stream_.get()); |
[email protected] | adb0024 | 2010-10-29 03:04:33 | [diff] [blame] | 844 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 845 | result = HandleCertificateRequest(result); |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 846 | } else if (result == ERR_HTTP_1_1_REQUIRED || |
| 847 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 848 | return HandleHttp11Required(result); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 849 | } |
| 850 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 851 | // Handle possible client certificate errors that may have occurred if the |
| 852 | // stream used SSL for one or more of the layers. |
| 853 | result = HandleSSLClientAuthError(result); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 854 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 855 | // At this point we are done with the stream_request_. |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 856 | stream_request_.reset(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 857 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 858 | } |
| 859 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 860 | int HttpNetworkTransaction::DoInitStream() { |
| 861 | DCHECK(stream_.get()); |
| 862 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 863 | |
| 864 | stream_->GetRemoteEndpoint(&remote_endpoint_); |
| 865 | |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 866 | return stream_->InitializeStream(request_, can_send_early_data_, priority_, |
| 867 | net_log_, io_callback_); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 871 | if (result == OK) { |
| 872 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 873 | } else { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 874 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 875 | result = HandleIOError(result); |
| 876 | |
| 877 | // The stream initialization failed, so this stream will never be useful. |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 878 | if (stream_) { |
| 879 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
| 880 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 881 | } |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 882 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | return result; |
| 886 | } |
| 887 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 888 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 889 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 890 | if (!ShouldApplyProxyAuth()) |
| 891 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 892 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 893 | if (!auth_controllers_[target].get()) |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame] | 894 | auth_controllers_[target] = new HttpAuthController( |
| 895 | target, AuthURL(target), session_->http_auth_cache(), |
| 896 | session_->http_auth_handler_factory(), session_->host_resolver()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 897 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 898 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 899 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) { |
| 903 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 904 | if (rv == OK) |
| 905 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN; |
| 906 | return rv; |
| 907 | } |
| 908 | |
| 909 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 910 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 911 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 912 | if (!auth_controllers_[target].get()) { |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame] | 913 | auth_controllers_[target] = new HttpAuthController( |
| 914 | target, AuthURL(target), session_->http_auth_cache(), |
| 915 | session_->http_auth_handler_factory(), session_->host_resolver()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 916 | if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY) |
| 917 | auth_controllers_[target]->DisableEmbeddedIdentity(); |
| 918 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 919 | if (!ShouldApplyServerAuth()) |
| 920 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 921 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 922 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 923 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 927 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 928 | if (rv == OK) |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 929 | next_state_ = STATE_INIT_REQUEST_BODY; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 930 | return rv; |
| 931 | } |
| 932 | |
| 933 | int HttpNetworkTransaction::BuildRequestHeaders( |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 934 | bool using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 935 | request_headers_.SetHeader(HttpRequestHeaders::kHost, |
| 936 | GetHostAndOptionalPort(request_->url)); |
| 937 | |
| 938 | // For compat with HTTP/1.0 servers and proxies: |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 939 | if (using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 940 | request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection, |
| 941 | "keep-alive"); |
| 942 | } else { |
| 943 | request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 944 | } |
| 945 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 946 | // Add a content length header? |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 947 | if (request_->upload_data_stream) { |
| 948 | if (request_->upload_data_stream->is_chunked()) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 949 | request_headers_.SetHeader( |
| 950 | HttpRequestHeaders::kTransferEncoding, "chunked"); |
| 951 | } else { |
| 952 | request_headers_.SetHeader( |
| 953 | HttpRequestHeaders::kContentLength, |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 954 | base::NumberToString(request_->upload_data_stream->size())); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 955 | } |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 956 | } else if (request_->method == "POST" || request_->method == "PUT") { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 957 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 958 | // IE and Safari also add a content length header. Presumably it is to |
| 959 | // support sending a HEAD request to an URL that only expects to be sent a |
| 960 | // POST or some other method that normally would have a message body. |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 961 | // Firefox (40.0) does not send the header, and RFC 7230 & 7231 |
| 962 | // specify that it should not be sent due to undefined behavior. |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 963 | request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 964 | } |
| 965 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 966 | // Honor load flags that impact proxy caches. |
| 967 | if (request_->load_flags & LOAD_BYPASS_CACHE) { |
| 968 | request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 969 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 970 | } else if (request_->load_flags & LOAD_VALIDATE_CACHE) { |
| 971 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 972 | } |
| 973 | |
| 974 | if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY)) |
| 975 | auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader( |
| 976 | &request_headers_); |
| 977 | if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER)) |
| 978 | auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader( |
| 979 | &request_headers_); |
| 980 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 981 | request_headers_.MergeFrom(request_->extra_headers); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 982 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 983 | if (!before_headers_sent_callback_.is_null()) |
| 984 | before_headers_sent_callback_.Run(proxy_info_, &request_headers_); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 985 | |
[email protected] | 173f8e2 | 2013-04-10 04:18:20 | [diff] [blame] | 986 | response_.did_use_http_auth = |
| 987 | request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) || |
| 988 | request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 989 | return OK; |
| 990 | } |
| 991 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 992 | int HttpNetworkTransaction::DoInitRequestBody() { |
| 993 | next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 994 | int rv = OK; |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 995 | if (request_->upload_data_stream) |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 996 | rv = request_->upload_data_stream->Init( |
| 997 | base::BindOnce(&HttpNetworkTransaction::OnIOComplete, |
| 998 | base::Unretained(this)), |
| 999 | net_log_); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1000 | return rv; |
| 1001 | } |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1002 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1003 | int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) { |
| 1004 | if (result == OK) |
| 1005 | next_state_ = STATE_BUILD_REQUEST; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1006 | return result; |
| 1007 | } |
| 1008 | |
| 1009 | int HttpNetworkTransaction::DoBuildRequest() { |
| 1010 | next_state_ = STATE_BUILD_REQUEST_COMPLETE; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1011 | headers_valid_ = false; |
| 1012 | |
| 1013 | // This is constructed lazily (instead of within our Start method), so that |
| 1014 | // we have proxy info available. |
| 1015 | if (request_headers_.IsEmpty()) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1016 | bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel(); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 1017 | return BuildRequestHeaders(using_http_proxy_without_tunnel); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1018 | } |
| 1019 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1020 | return OK; |
| 1021 | } |
| 1022 | |
| 1023 | int HttpNetworkTransaction::DoBuildRequestComplete(int result) { |
[email protected] | 9f489d7 | 2011-04-04 23:29:24 | [diff] [blame] | 1024 | if (result == OK) |
| 1025 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1026 | return result; |
| 1027 | } |
| 1028 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1029 | int HttpNetworkTransaction::DoSendRequest() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1030 | send_start_time_ = base::TimeTicks::Now(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1031 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 1032 | |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 1033 | return stream_->SendRequest(request_headers_, &response_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1034 | } |
| 1035 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1036 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1037 | send_end_time_ = base::TimeTicks::Now(); |
bnc | aa17133 | 2016-07-20 14:47:36 | [diff] [blame] | 1038 | |
| 1039 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 1040 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 1041 | return HandleHttp11Required(result); |
| 1042 | } |
| 1043 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1044 | if (result < 0) |
| 1045 | return HandleIOError(result); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1046 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1047 | return OK; |
| 1048 | } |
| 1049 | |
| 1050 | int HttpNetworkTransaction::DoReadHeaders() { |
| 1051 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1052 | return stream_->ReadResponseHeaders(io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1056 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 1057 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1058 | if (IsCertificateError(result)) { |
| 1059 | // We don't handle a certificate error during SSL renegotiation, so we |
| 1060 | // have to return an error that's not in the certificate error range |
| 1061 | // (-2xx). |
| 1062 | LOG(ERROR) << "Got a server certificate with error " << result |
| 1063 | << " during SSL renegotiation"; |
| 1064 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 1065 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 1066 | // TODO(wtc): Need a test case for this code path! |
| 1067 | DCHECK(stream_.get()); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 1068 | DCHECK(IsSecureRequest()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1069 | response_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1070 | stream_->GetSSLCertRequestInfo(response_.cert_request_info.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1071 | result = HandleCertificateRequest(result); |
| 1072 | if (result == OK) |
| 1073 | return result; |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 1074 | } |
| 1075 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1076 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 1077 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 1078 | return HandleHttp11Required(result); |
| 1079 | } |
| 1080 | |
[email protected] | c871864d7 | 2014-03-13 19:56:19 | [diff] [blame] | 1081 | // ERR_CONNECTION_CLOSED is treated differently at this point; if partial |
| 1082 | // response headers were received, we do the best we can to make sense of it |
| 1083 | // and send it back up the stack. |
| 1084 | // |
| 1085 | // TODO(davidben): Consider moving this to HttpBasicStream, It's a little |
| 1086 | // bizarre for SPDY. Assuming this logic is useful at all. |
| 1087 | // TODO(davidben): Bubble the error code up so we do not cache? |
| 1088 | if (result == ERR_CONNECTION_CLOSED && response_.headers.get()) |
| 1089 | result = OK; |
| 1090 | |
| 1091 | if (result < 0) |
| 1092 | return HandleIOError(result); |
| 1093 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1094 | DCHECK(response_.headers.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1095 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1096 | if (response_.headers.get() && !ContentEncodingsValid()) |
| 1097 | return ERR_CONTENT_DECODING_FAILED; |
| 1098 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1099 | // On a 408 response from the server ("Request Timeout") on a stale socket, |
| 1100 | // retry the request. |
[email protected] | 0aff0d8 | 2014-06-14 08:49:04 | [diff] [blame] | 1101 | // Headers can be NULL because of http://crbug.com/384554. |
tyoshino | e8b3e22 | 2017-04-21 03:07:18 | [diff] [blame] | 1102 | if (response_.headers.get() && |
| 1103 | response_.headers->response_code() == HTTP_REQUEST_TIMEOUT && |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1104 | stream_->IsConnectionReused()) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1105 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1106 | GenerateNetworkErrorLoggingReport(OK); |
| 1107 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1108 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1109 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1110 | response_.headers->response_code()); |
| 1111 | // This will close the socket - it would be weird to try and reuse it, even |
| 1112 | // if the server doesn't actually close it. |
| 1113 | ResetConnectionAndRequestForResend(); |
| 1114 | return OK; |
| 1115 | } |
| 1116 | |
[email protected] | 93f8b56 | 2012-03-27 01:00:16 | [diff] [blame] | 1117 | // Like Net.HttpResponseCode, but only for MAIN_FRAME loads. |
rdsmith | 4de1fcf | 2016-08-30 05:35:21 | [diff] [blame] | 1118 | if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) { |
[email protected] | 93f8b56 | 2012-03-27 01:00:16 | [diff] [blame] | 1119 | const int response_code = response_.headers->response_code(); |
| 1120 | UMA_HISTOGRAM_ENUMERATION( |
| 1121 | "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10); |
| 1122 | } |
| 1123 | |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 1124 | net_log_.AddEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1125 | NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 1126 | base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers)); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 1127 | if (response_headers_callback_) |
| 1128 | response_headers_callback_.Run(response_.headers); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1129 | |
bnc | be0f6af | 2015-10-15 17:49:56 | [diff] [blame] | 1130 | if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1131 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 1132 | // indicates a buggy server. See: |
| 1133 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 1134 | if (request_->method == "PUT") |
| 1135 | return ERR_METHOD_NOT_SUPPORTED; |
| 1136 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1137 | |
Steven Valdez | 58097ec3 | 2018-07-16 18:29:04 | [diff] [blame] | 1138 | if (can_send_early_data_ && response_.headers.get() && |
| 1139 | response_.headers->response_code() == HTTP_TOO_EARLY) { |
| 1140 | return HandleIOError(ERR_EARLY_DATA_REJECTED); |
| 1141 | } |
| 1142 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1143 | // Check for an intermediate 100 Continue response. An origin server is |
| 1144 | // allowed to send this response even if we didn't ask for it, so we just |
| 1145 | // need to skip over it. |
| 1146 | // We treat any other 1xx in this same way (although in practice getting |
| 1147 | // a 1xx that isn't a 100 is rare). |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1148 | // Unless this is a WebSocket request, in which case we pass it on up. |
| 1149 | if (response_.headers->response_code() / 100 == 1 && |
| 1150 | !ForWebSocketHandshake()) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1151 | response_.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1152 | next_state_ = STATE_READ_HEADERS; |
| 1153 | return OK; |
| 1154 | } |
| 1155 | |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 1156 | if (response_.headers->response_code() == 421 && |
| 1157 | (enable_ip_based_pooling_ || enable_alternative_services_)) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1158 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1159 | GenerateNetworkErrorLoggingReport(OK); |
| 1160 | #endif // BUILDFLAG(ENABLE_REPORTING) |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 1161 | // Retry the request with both IP based pooling and Alternative Services |
| 1162 | // disabled. |
| 1163 | enable_ip_based_pooling_ = false; |
| 1164 | enable_alternative_services_ = false; |
| 1165 | net_log_.AddEvent( |
| 1166 | NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST); |
| 1167 | ResetConnectionAndRequestForResend(); |
| 1168 | return OK; |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 1169 | } |
| 1170 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 1171 | if (IsSecureRequest()) { |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1172 | stream_->GetSSLInfo(&response_.ssl_info); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 1173 | if (response_.ssl_info.is_valid() && |
| 1174 | !IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1175 | session_->http_stream_factory()->ProcessAlternativeServices( |
| 1176 | session_, response_.headers.get(), |
| 1177 | url::SchemeHostPort(request_->url)); |
| 1178 | } |
| 1179 | } |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1180 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1181 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1182 | if (rv != OK) |
| 1183 | return rv; |
| 1184 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1185 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1186 | // Note: Unless there is a pre-existing NEL policy for this origin, any NEL |
| 1187 | // reports generated before the NEL header is processed here will just be |
| 1188 | // dropped by the NetworkErrorLoggingService. |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 1189 | ProcessReportToHeader(); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1190 | ProcessNetworkErrorLoggingHeader(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1191 | |
| 1192 | // Generate NEL report here if we have to report an HTTP error (4xx or 5xx |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 1193 | // code), or if the response body will not be read, or on a redirect. |
| 1194 | // Note: This will report a success for a redirect even if an error is |
| 1195 | // encountered later while draining the body. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1196 | int response_code = response_.headers->response_code(); |
| 1197 | if ((response_code >= 400 && response_code < 600) || |
| 1198 | response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT || |
| 1199 | response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" || |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 1200 | response_.headers->GetContentLength() == 0 || |
| 1201 | response_.headers->IsRedirect(nullptr /* location */)) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1202 | GenerateNetworkErrorLoggingReport(OK); |
| 1203 | } |
| 1204 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1205 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1206 | headers_valid_ = true; |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 1207 | |
| 1208 | // We have reached the end of Start state machine, set the RequestInfo to |
| 1209 | // null. |
| 1210 | // RequestInfo is a member of the HttpTransaction's consumer and is useful |
| 1211 | // only until the final response headers are received. Clearing it will ensure |
| 1212 | // that HttpRequestInfo is only used up until final response headers are |
| 1213 | // received. Clearing is allowed so that the transaction can be disassociated |
| 1214 | // from its creating consumer in cases where it is shared for writing to the |
| 1215 | // cache. It is also safe to set it to null at this point since |
| 1216 | // upload_data_stream is also not used in the Read state machine. |
| 1217 | if (pending_auth_target_ == HttpAuth::AUTH_NONE) |
| 1218 | request_ = nullptr; |
| 1219 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1220 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | int HttpNetworkTransaction::DoReadBody() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1224 | DCHECK(read_buf_.get()); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 1225 | DCHECK_GT(read_buf_len_, 0); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1226 | DCHECK(stream_ != nullptr); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1227 | |
| 1228 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1229 | return stream_->ReadResponseBody( |
| 1230 | read_buf_.get(), read_buf_len_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 1234 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1235 | bool done = false; |
| 1236 | if (result <= 0) { |
| 1237 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1238 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1239 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1240 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1241 | // Clean up connection if we are done. |
| 1242 | if (done) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1243 | // Note: Just because IsResponseBodyComplete is true, we're not |
| 1244 | // necessarily "done". We're only "done" when it is the last |
| 1245 | // read on this HttpNetworkTransaction, which will be signified |
| 1246 | // by a zero-length read. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1247 | // TODO(mbelshe): The keep-alive property is really a property of |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1248 | // the stream. No need to compute it here just to pass back |
| 1249 | // to the stream's Close function. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1250 | bool keep_alive = |
| 1251 | stream_->IsResponseBodyComplete() && stream_->CanReuseConnection(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1252 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1253 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 1254 | // Note: we don't reset the stream here. We've closed it, but we still |
| 1255 | // need it around so that callers can call methods such as |
| 1256 | // GetUploadProgress() and have them be meaningful. |
| 1257 | // TODO(mbelshe): This means we closed the stream here, and we close it |
| 1258 | // again in ~HttpNetworkTransaction. Clean that up. |
| 1259 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1260 | // The next Read call will return 0 (EOF). |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1261 | |
| 1262 | // This transaction was successful. If it had been retried because of an |
| 1263 | // error with an alternative service, mark that alternative service broken. |
| 1264 | if (!enable_alternative_services_ && |
| 1265 | retried_alternative_service_.protocol != kProtoUnknown) { |
Ryan Hamilton | 9f532a1 | 2019-04-22 22:26:41 | [diff] [blame] | 1266 | HistogramBrokenAlternateProtocolLocation( |
| 1267 | BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION); |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1268 | session_->http_server_properties()->MarkAlternativeServiceBroken( |
| 1269 | retried_alternative_service_); |
| 1270 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1271 | |
| 1272 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1273 | GenerateNetworkErrorLoggingReport(result); |
| 1274 | #endif // BUILDFLAG(ENABLE_REPORTING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | // Clear these to avoid leaving around old state. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1278 | read_buf_ = nullptr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1279 | read_buf_len_ = 0; |
| 1280 | |
| 1281 | return result; |
| 1282 | } |
| 1283 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1284 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 1285 | // This method differs from DoReadBody only in the next_state_. So we just |
| 1286 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 1287 | // elegant way for these two methods to share code. |
| 1288 | int rv = DoReadBody(); |
| 1289 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 1290 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 1291 | return rv; |
| 1292 | } |
| 1293 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1294 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 1295 | // 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] | 1296 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1297 | // keep_alive defaults to true because the very reason we're draining the |
| 1298 | // response body is to reuse the connection for auth restart. |
| 1299 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1300 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1301 | // Error or closed connection while reading the socket. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1302 | // Note: No Network Error Logging report is generated here because a report |
| 1303 | // will have already been generated for the original request due to the auth |
| 1304 | // challenge, so a second report is not generated for the same request here. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1305 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1306 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1307 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1308 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | if (done) { |
| 1312 | DidDrainBodyForAuthRestart(keep_alive); |
| 1313 | } else { |
| 1314 | // Keep draining. |
| 1315 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 1316 | } |
| 1317 | |
| 1318 | return OK; |
| 1319 | } |
| 1320 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1321 | #if BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 1322 | void HttpNetworkTransaction::ProcessReportToHeader() { |
| 1323 | std::string value; |
| 1324 | if (!response_.headers->GetNormalizedHeader("Report-To", &value)) |
| 1325 | return; |
| 1326 | |
| 1327 | ReportingService* service = session_->reporting_service(); |
| 1328 | if (!service) { |
| 1329 | ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService(); |
| 1330 | return; |
| 1331 | } |
| 1332 | |
| 1333 | // Only accept Report-To headers on HTTPS connections that have no |
| 1334 | // certificate errors. |
| 1335 | if (!response_.ssl_info.is_valid()) { |
| 1336 | ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo(); |
| 1337 | return; |
| 1338 | } |
| 1339 | if (IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1340 | ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError(); |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | service->ProcessHeader(url_.GetOrigin(), value); |
| 1345 | } |
| 1346 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1347 | void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() { |
| 1348 | std::string value; |
| 1349 | if (!response_.headers->GetNormalizedHeader( |
| 1350 | NetworkErrorLoggingService::kHeaderName, &value)) { |
| 1351 | return; |
| 1352 | } |
| 1353 | |
| 1354 | NetworkErrorLoggingService* service = |
| 1355 | session_->network_error_logging_service(); |
| 1356 | if (!service) { |
| 1357 | NetworkErrorLoggingService:: |
| 1358 | RecordHeaderDiscardedForNoNetworkErrorLoggingService(); |
| 1359 | return; |
| 1360 | } |
| 1361 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 1362 | // Don't accept NEL headers received via a proxy, because the IP address of |
| 1363 | // the destination server is not known. |
| 1364 | if (response_.was_fetched_via_proxy) |
| 1365 | return; |
| 1366 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1367 | // Only accept NEL headers on HTTPS connections that have no certificate |
| 1368 | // errors. |
| 1369 | if (!response_.ssl_info.is_valid()) { |
| 1370 | NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo(); |
| 1371 | return; |
| 1372 | } |
| 1373 | if (IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1374 | NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError(); |
| 1375 | return; |
| 1376 | } |
| 1377 | |
| 1378 | if (remote_endpoint_.address().empty()) { |
| 1379 | NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint(); |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(), |
| 1384 | value); |
| 1385 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1386 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1387 | void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) { |
| 1388 | if (rv < 0 && rv != ERR_IO_PENDING) |
| 1389 | GenerateNetworkErrorLoggingReport(rv); |
| 1390 | } |
| 1391 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1392 | void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1393 | // |rv| should be a valid net::Error |
| 1394 | DCHECK_NE(rv, ERR_IO_PENDING); |
| 1395 | DCHECK_LE(rv, 0); |
| 1396 | |
| 1397 | if (network_error_logging_report_generated_) |
| 1398 | return; |
| 1399 | network_error_logging_report_generated_ = true; |
| 1400 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1401 | NetworkErrorLoggingService* service = |
| 1402 | session_->network_error_logging_service(); |
| 1403 | if (!service) { |
| 1404 | NetworkErrorLoggingService:: |
| 1405 | RecordRequestDiscardedForNoNetworkErrorLoggingService(); |
| 1406 | return; |
| 1407 | } |
| 1408 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 1409 | // Don't report on proxy auth challenges. |
| 1410 | if (response_.headers && response_.headers->response_code() == |
| 1411 | HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 1412 | return; |
| 1413 | } |
| 1414 | |
| 1415 | // Don't generate NEL reports if we are behind a proxy, to avoid leaking |
| 1416 | // internal network details. |
| 1417 | if (response_.was_fetched_via_proxy) |
| 1418 | return; |
| 1419 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 1420 | // Ignore errors from non-HTTPS origins. |
| 1421 | if (!url_.SchemeIsCryptographic()) { |
| 1422 | NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin(); |
| 1423 | return; |
| 1424 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1425 | |
| 1426 | NetworkErrorLoggingService::RequestDetails details; |
| 1427 | |
| 1428 | details.uri = url_; |
| 1429 | if (!request_referrer_.empty()) |
| 1430 | details.referrer = GURL(request_referrer_); |
| 1431 | details.user_agent = request_user_agent_; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1432 | if (!remote_endpoint_.address().empty()) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1433 | details.server_ip = remote_endpoint_.address(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1434 | } else { |
| 1435 | details.server_ip = IPAddress(); |
| 1436 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1437 | // HttpResponseHeaders::response_code() returns 0 if response code couldn't |
| 1438 | // be parsed, which is also how NEL represents the same. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1439 | if (response_.headers) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1440 | details.status_code = response_.headers->response_code(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1441 | } else { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1442 | details.status_code = 0; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1443 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1444 | // If we got response headers, assume that the connection used HTTP/1.1 |
| 1445 | // unless ALPN negotiation tells us otherwise (handled below). |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1446 | if (response_.was_alpn_negotiated) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1447 | details.protocol = response_.alpn_negotiated_protocol; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1448 | } else { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1449 | details.protocol = "http/1.1"; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1450 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1451 | details.method = request_method_; |
| 1452 | details.elapsed_time = base::TimeTicks::Now() - start_timeticks_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1453 | details.type = static_cast<Error>(rv); |
| 1454 | details.reporting_upload_depth = request_reporting_upload_depth_; |
| 1455 | |
| 1456 | service->OnRequest(std::move(details)); |
| 1457 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1458 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1459 | |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1460 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1461 | // There are two paths through which the server can request a certificate |
| 1462 | // from us. The first is during the initial handshake, the second is |
| 1463 | // during SSL renegotiation. |
| 1464 | // |
| 1465 | // In both cases, we want to close the connection before proceeding. |
| 1466 | // We do this for two reasons: |
| 1467 | // First, we don't want to keep the connection to the server hung for a |
| 1468 | // long time while the user selects a certificate. |
| 1469 | // Second, even if we did keep the connection open, NSS has a bug where |
| 1470 | // restarting the handshake for ClientAuth is currently broken. |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1471 | DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1472 | |
| 1473 | if (stream_.get()) { |
| 1474 | // Since we already have a stream, we're being called as part of SSL |
| 1475 | // renegotiation. |
| 1476 | DCHECK(!stream_request_.get()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1477 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1478 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1479 | stream_->Close(true); |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1480 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1481 | } |
| 1482 | |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 1483 | // The server is asking for a client certificate during the initial |
| 1484 | // handshake. |
| 1485 | stream_request_.reset(); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1486 | |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1487 | // If the user selected one of the certificates in client_certs or declined |
| 1488 | // to provide one for this server before, use the past decision |
| 1489 | // automatically. |
| 1490 | scoped_refptr<X509Certificate> client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1491 | scoped_refptr<SSLPrivateKey> client_private_key; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1492 | bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup( |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1493 | response_.cert_request_info->host_and_port, &client_cert, |
| 1494 | &client_private_key); |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1495 | if (!found_cached_cert) |
| 1496 | return error; |
| 1497 | |
| 1498 | // Check that the certificate selected is still a certificate the server |
| 1499 | // is likely to accept, based on the criteria supplied in the |
| 1500 | // CertificateRequest message. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1501 | if (client_cert.get()) { |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1502 | const std::vector<std::string>& cert_authorities = |
| 1503 | response_.cert_request_info->cert_authorities; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1504 | |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1505 | bool cert_still_valid = cert_authorities.empty() || |
| 1506 | client_cert->IsIssuedByEncoded(cert_authorities); |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1507 | if (!cert_still_valid) |
| 1508 | return error; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1509 | } |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1510 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1511 | if (!response_.cert_request_info->is_proxy) { |
| 1512 | server_ssl_client_cert_was_cached_ = true; |
| 1513 | } |
| 1514 | |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1515 | // TODO(davidben): Add a unit test which covers this path; we need to be |
| 1516 | // able to send a legitimate certificate and also bypass/clear the |
| 1517 | // SSL session cache. |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 1518 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 1519 | &proxy_ssl_config_ : &server_ssl_config_; |
| 1520 | ssl_config->send_client_cert = true; |
| 1521 | ssl_config->client_cert = client_cert; |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 1522 | ssl_config->client_private_key = client_private_key; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1523 | next_state_ = STATE_CREATE_STREAM; |
| 1524 | // Reset the other member variables. |
| 1525 | // Note: this is necessary only with SSL renegotiation. |
| 1526 | ResetStateForRestart(); |
| 1527 | return OK; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1528 | } |
| 1529 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1530 | int HttpNetworkTransaction::HandleHttp11Required(int error) { |
| 1531 | DCHECK(error == ERR_HTTP_1_1_REQUIRED || |
| 1532 | error == ERR_PROXY_HTTP_1_1_REQUIRED); |
| 1533 | |
| 1534 | if (error == ERR_HTTP_1_1_REQUIRED) { |
| 1535 | HttpServerProperties::ForceHTTP11(&server_ssl_config_); |
| 1536 | } else { |
| 1537 | HttpServerProperties::ForceHTTP11(&proxy_ssl_config_); |
| 1538 | } |
| 1539 | ResetConnectionAndRequestForResend(); |
| 1540 | return OK; |
| 1541 | } |
| 1542 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1543 | int HttpNetworkTransaction::HandleSSLClientAuthError(int error) { |
David Benjamin | 98ba2be | 2019-04-09 14:25:25 | [diff] [blame] | 1544 | // Client certificate errors from the proxy are handled in the |
| 1545 | // HttpStreamFactory and below. See discussion in https://crbug.com/828965. |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1546 | if (server_ssl_config_.send_client_cert && |
| 1547 | (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) { |
| 1548 | session_->ssl_client_auth_cache()->Remove( |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 1549 | HostPortPair::FromURL(request_->url)); |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1550 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1551 | // The private key handle may have gone stale due to, e.g., the user |
| 1552 | // unplugging their smartcard. Operating systems do not provide reliable |
| 1553 | // notifications for this, so if the signature failed and the private key |
| 1554 | // came from SSLClientAuthCache, retry to ask the user for a new one. |
| 1555 | if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED && |
| 1556 | server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) { |
| 1557 | server_ssl_client_cert_was_cached_ = false; |
| 1558 | server_ssl_config_.send_client_cert = false; |
| 1559 | server_ssl_config_.client_cert = nullptr; |
| 1560 | server_ssl_config_.client_private_key = nullptr; |
| 1561 | retry_attempts_++; |
| 1562 | net_log_.AddEventWithNetErrorCode( |
| 1563 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
| 1564 | ResetConnectionAndRequestForResend(); |
| 1565 | return OK; |
| 1566 | } |
| 1567 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1568 | return error; |
| 1569 | } |
| 1570 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1571 | // This method determines whether it is safe to resend the request after an |
| 1572 | // IO error. It can only be called in response to request header or body |
| 1573 | // write errors or response header read errors. It should not be used in |
| 1574 | // other cases, such as a Connect error. |
| 1575 | int HttpNetworkTransaction::HandleIOError(int error) { |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1576 | // Because the peer may request renegotiation with client authentication at |
| 1577 | // any time, check and handle client authentication errors. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1578 | error = HandleSSLClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1579 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1580 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1581 | GenerateNetworkErrorLoggingReportIfError(error); |
| 1582 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 1583 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1584 | switch (error) { |
| 1585 | // If we try to reuse a connection that the server is in the process of |
| 1586 | // closing, we may end up successfully writing out our request (or a |
| 1587 | // portion of our request) only to find a connection error when we try to |
| 1588 | // read from (or finish writing to) the socket. |
| 1589 | case ERR_CONNECTION_RESET: |
| 1590 | case ERR_CONNECTION_CLOSED: |
| 1591 | case ERR_CONNECTION_ABORTED: |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1592 | // There can be a race between the socket pool checking checking whether a |
| 1593 | // socket is still connected, receiving the FIN, and sending/reading data |
| 1594 | // on a reused socket. If we receive the FIN between the connectedness |
| 1595 | // check and writing/reading from the socket, we may first learn the socket |
| 1596 | // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most |
| 1597 | // likely happen when trying to retrieve its IP address. |
| 1598 | // See http://crbug.com/105824 for more details. |
| 1599 | case ERR_SOCKET_NOT_CONNECTED: |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1600 | // If a socket is closed on its initial request, HttpStreamParser returns |
| 1601 | // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was |
| 1602 | // preconnected but failed to be used before the server timed it out. |
| 1603 | case ERR_EMPTY_RESPONSE: |
| 1604 | if (ShouldResendRequest()) { |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1605 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1606 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1607 | ResetConnectionAndRequestForResend(); |
| 1608 | error = OK; |
| 1609 | } |
| 1610 | break; |
Steven Valdez | 58097ec3 | 2018-07-16 18:29:04 | [diff] [blame] | 1611 | case ERR_EARLY_DATA_REJECTED: |
| 1612 | case ERR_WRONG_VERSION_ON_EARLY_DATA: |
| 1613 | net_log_.AddEventWithNetErrorCode( |
| 1614 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
| 1615 | // Disable early data on the SSLConfig on a reset. |
| 1616 | can_send_early_data_ = false; |
| 1617 | ResetConnectionAndRequestForResend(); |
| 1618 | error = OK; |
| 1619 | break; |
[email protected] | 8753a12 | 2011-10-16 08:05:08 | [diff] [blame] | 1620 | case ERR_SPDY_PING_FAILED: |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1621 | case ERR_SPDY_SERVER_REFUSED_STREAM: |
Bence Béky | 1a5d856 | 2018-01-05 17:29:28 | [diff] [blame] | 1622 | case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE: |
Yoav Weiss | 9693572f | 2018-01-04 09:37:34 | [diff] [blame] | 1623 | case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER: |
Bence Béky | 49db0e2 | 2018-05-11 00:54:05 | [diff] [blame] | 1624 | case ERR_SPDY_PUSHED_RESPONSE_DOES_NOT_MATCH: |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 1625 | case ERR_QUIC_HANDSHAKE_FAILED: |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1626 | if (HasExceededMaxRetries()) |
| 1627 | break; |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1628 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1629 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1630 | retry_attempts_++; |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1631 | ResetConnectionAndRequestForResend(); |
| 1632 | error = OK; |
| 1633 | break; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1634 | case ERR_QUIC_PROTOCOL_ERROR: |
| 1635 | if (GetResponseHeaders() != nullptr || |
| 1636 | !stream_->GetAlternativeService(&retried_alternative_service_)) { |
| 1637 | // If the response headers have already been recieved and passed up |
| 1638 | // then the request can not be retried. Also, if there was no |
| 1639 | // alternative service used for this request, then there is no |
| 1640 | // alternative service to be disabled. |
| 1641 | break; |
| 1642 | } |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1643 | if (HasExceededMaxRetries()) |
| 1644 | break; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1645 | if (session_->http_server_properties()->IsAlternativeServiceBroken( |
| 1646 | retried_alternative_service_)) { |
| 1647 | // If the alternative service was marked as broken while the request |
| 1648 | // was in flight, retry the request which will not use the broken |
| 1649 | // alternative service. |
| 1650 | net_log_.AddEventWithNetErrorCode( |
| 1651 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1652 | retry_attempts_++; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1653 | ResetConnectionAndRequestForResend(); |
| 1654 | error = OK; |
| 1655 | } else if (session_->params().retry_without_alt_svc_on_quic_errors) { |
| 1656 | // Disable alternative services for this request and retry it. If the |
| 1657 | // retry succeeds, then the alternative service will be marked as |
| 1658 | // broken then. |
| 1659 | enable_alternative_services_ = false; |
rch | 514a44a8 | 2017-04-13 04:46:36 | [diff] [blame] | 1660 | net_log_.AddEventWithNetErrorCode( |
| 1661 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1662 | retry_attempts_++; |
rch | 514a44a8 | 2017-04-13 04:46:36 | [diff] [blame] | 1663 | ResetConnectionAndRequestForResend(); |
| 1664 | error = OK; |
| 1665 | } |
| 1666 | break; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1667 | } |
| 1668 | return error; |
| 1669 | } |
| 1670 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1671 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1672 | ResetStateForAuthRestart(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1673 | if (stream_) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1674 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1675 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 1676 | } |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1677 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | void HttpNetworkTransaction::ResetStateForAuthRestart() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1681 | send_start_time_ = base::TimeTicks(); |
| 1682 | send_end_time_ = base::TimeTicks(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1683 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1684 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1685 | read_buf_ = nullptr; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1686 | read_buf_len_ = 0; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1687 | headers_valid_ = false; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1688 | request_headers_.Clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1689 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1690 | establishing_tunnel_ = false; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 1691 | remote_endpoint_ = IPEndPoint(); |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1692 | net_error_details_.quic_broken = false; |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 1693 | net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1694 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1695 | network_error_logging_report_generated_ = false; |
| 1696 | start_timeticks_ = base::TimeTicks::Now(); |
| 1697 | #endif // BUILDFLAG(ENABLE_REPORTING) |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() { |
| 1701 | if (stream_) |
| 1702 | stream_->PopulateNetErrorDetails(&net_error_details_); |
| 1703 | stream_.reset(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1704 | } |
| 1705 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1706 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1707 | return response_.headers.get(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1708 | } |
| 1709 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1710 | bool HttpNetworkTransaction::ShouldResendRequest() const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1711 | bool connection_is_proven = stream_->IsConnectionReused(); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1712 | bool has_received_headers = GetResponseHeaders() != nullptr; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1713 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1714 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1715 | // This automatically prevents an infinite resend loop because we'll run |
| 1716 | // out of the cached keep-alive connections eventually. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1717 | if (connection_is_proven && !has_received_headers) |
| 1718 | return true; |
| 1719 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1720 | } |
| 1721 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1722 | bool HttpNetworkTransaction::HasExceededMaxRetries() const { |
| 1723 | return (retry_attempts_ >= kMaxRetryAttempts); |
| 1724 | } |
| 1725 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 1726 | bool HttpNetworkTransaction::CheckMaxRestarts() { |
| 1727 | num_restarts_++; |
| 1728 | return num_restarts_ < kMaxRestarts; |
| 1729 | } |
| 1730 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1731 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1732 | if (stream_.get()) { |
| 1733 | stream_->Close(true); |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1734 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1735 | } |
| 1736 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1737 | // We need to clear request_headers_ because it contains the real request |
| 1738 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1739 | // the SSL tunnel. |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1740 | request_headers_.Clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1741 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1742 | |
| 1743 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1744 | // Reset for new request. |
| 1745 | network_error_logging_report_generated_ = false; |
| 1746 | start_timeticks_ = base::TimeTicks::Now(); |
| 1747 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 1748 | |
| 1749 | ResetStateForRestart(); |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1750 | } |
| 1751 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1752 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1753 | return UsingHttpProxyWithoutTunnel(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1754 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1755 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1756 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1757 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1758 | } |
| 1759 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1760 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1761 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1762 | DCHECK(headers.get()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1763 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1764 | int status = headers->response_code(); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1765 | if (status != HTTP_UNAUTHORIZED && |
| 1766 | status != HTTP_PROXY_AUTHENTICATION_REQUIRED) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1767 | return OK; |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1768 | HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1769 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1770 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1771 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1772 | |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1773 | // This case can trigger when an HTTPS server responds with a "Proxy |
| 1774 | // authentication required" status code through a non-authenticating |
| 1775 | // proxy. |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1776 | if (!auth_controllers_[target].get()) |
| 1777 | return ERR_UNEXPECTED_PROXY_AUTH; |
| 1778 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1779 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1780 | headers, response_.ssl_info, |
| 1781 | (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1782 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1783 | if (auth_controllers_[target]->HaveAuthHandler()) |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1784 | pending_auth_target_ = target; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1785 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 1786 | auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1787 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1788 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1789 | } |
| 1790 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1791 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1792 | return auth_controllers_[target].get() && |
| 1793 | auth_controllers_[target]->HaveAuth(); |
| 1794 | } |
| 1795 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1796 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1797 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1798 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1799 | if (!proxy_info_.proxy_server().is_valid() || |
| 1800 | proxy_info_.proxy_server().is_direct()) { |
| 1801 | return GURL(); // There is no proxy server. |
| 1802 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1803 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1804 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1805 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1806 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1807 | case HttpAuth::AUTH_SERVER: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1808 | if (ForWebSocketHandshake()) { |
Adam Rice | 9bd428b0a | 2019-02-15 06:31:36 | [diff] [blame] | 1809 | return net::ChangeWebSocketSchemeToHttpScheme(request_->url); |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1810 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1811 | return request_->url; |
| 1812 | default: |
| 1813 | return GURL(); |
| 1814 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1815 | } |
| 1816 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1817 | bool HttpNetworkTransaction::ForWebSocketHandshake() const { |
[email protected] | 23d3e02 | 2013-11-23 20:38:14 | [diff] [blame] | 1818 | return websocket_handshake_stream_base_create_helper_ && |
| 1819 | request_->url.SchemeIsWSOrWSS(); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1820 | } |
| 1821 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 1822 | void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() { |
| 1823 | DCHECK(stream_request_); |
| 1824 | |
| 1825 | // Since the transaction can restart with auth credentials, it may create a |
| 1826 | // stream more than once. Accumulate all of the connection attempts across |
| 1827 | // those streams by appending them to the vector: |
| 1828 | for (const auto& attempt : stream_request_->connection_attempts()) |
| 1829 | connection_attempts_.push_back(attempt); |
| 1830 | } |
| 1831 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1832 | bool HttpNetworkTransaction::ContentEncodingsValid() const { |
| 1833 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 1834 | DCHECK(headers); |
| 1835 | |
| 1836 | std::string accept_encoding; |
| 1837 | request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 1838 | &accept_encoding); |
| 1839 | std::set<std::string> allowed_encodings; |
| 1840 | if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) { |
| 1841 | FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID); |
| 1842 | return false; |
| 1843 | } |
| 1844 | |
| 1845 | std::string content_encoding; |
| 1846 | headers->GetNormalizedHeader("Content-Encoding", &content_encoding); |
| 1847 | std::set<std::string> used_encodings; |
| 1848 | if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) { |
| 1849 | FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID); |
| 1850 | return false; |
| 1851 | } |
| 1852 | |
| 1853 | // When "Accept-Encoding" is not specified, it is parsed as "*". |
| 1854 | // If "*" encoding is advertised, then any encoding should be "accepted". |
| 1855 | // This does not mean, that it will be successfully decoded. |
| 1856 | if (allowed_encodings.find("*") != allowed_encodings.end()) |
| 1857 | return true; |
| 1858 | |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1859 | bool result = true; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1860 | for (auto const& encoding : used_encodings) { |
| 1861 | SourceStream::SourceType source_type = |
| 1862 | FilterSourceStream::ParseEncodingType(encoding); |
| 1863 | // We don't reject encodings we are not aware. They just will not decode. |
| 1864 | if (source_type == SourceStream::TYPE_UNKNOWN) |
| 1865 | continue; |
| 1866 | if (allowed_encodings.find(encoding) == allowed_encodings.end()) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1867 | FilterSourceStream::ReportContentDecodingFailed( |
| 1868 | SourceStream::TYPE_REJECTED); |
| 1869 | result = false; |
| 1870 | break; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1871 | } |
| 1872 | } |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1873 | |
| 1874 | // Temporary workaround for http://crbug.com/714514 |
| 1875 | if (headers->IsRedirect(nullptr)) { |
| 1876 | UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding", |
| 1877 | !result); |
| 1878 | return true; |
| 1879 | } |
| 1880 | |
| 1881 | return result; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1882 | } |
| 1883 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1884 | } // namespace net |