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