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