blob: d8c1a38e247fe80ffe5cd17656a62f7541e035b3 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]2fbaecf22010-07-22 22:20:359#include <vector>
10
nharperb7441ef2016-01-25 23:54:1411#include "base/base64url.h"
[email protected]68bf9152008-09-25 19:47:3012#include "base/compiler_specific.h"
Rodney Ding329e4bb2021-03-19 22:21:5313#include "base/feature_list.h"
[email protected]270c6412010-03-29 22:02:4714#include "base/format_macros.h"
Avi Drissman41c4a412023-01-11 22:45:3715#include "base/functional/bind.h"
16#include "base/functional/callback_helpers.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
David Benjamin5cb91132018-04-06 05:54:4918#include "base/metrics/histogram_functions.h"
asvitkinec3c93722015-06-17 14:48:3719#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4220#include "base/metrics/sparse_histogram.h"
[email protected]125ef482013-06-11 18:32:4721#include "base/strings/string_number_conversions.h"
22#include "base/strings/string_util.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
Rodney Ding329e4bb2021-03-19 22:21:5327#include "net/base/features.h"
[email protected]2d6728692011-03-12 01:39:5528#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1929#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2531#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2632#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4033#include "net/base/proxy_server.h"
Titouan Rigoudyba507a882020-07-31 12:15:1534#include "net/base/transport_info.h"
initial.commit586acc5fe2008-07-26 22:42:5235#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4436#include "net/base/url_util.h"
Bence Béky230ac612017-08-30 19:17:0837#include "net/cert/cert_status_flags.h"
eustasc7d27da2017-04-06 10:33:2038#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0039#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4240#include "net/http/http_auth.h"
David Benjamin7c6df0b762021-12-23 21:02:5441#include "net/http/http_auth_controller.h"
[email protected]c3b35c22008-09-27 03:19:4242#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2443#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3644#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_chunked_decoder.h"
Eric Roman06bd9742019-07-13 15:19:1346#include "net/http/http_log_util.h"
initial.commit586acc5fe2008-07-26 22:42:5247#include "net/http/http_network_session.h"
[email protected]270c6412010-03-29 22:02:4748#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5249#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2150#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5751#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5352#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5853#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3954#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3155#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5256#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2357#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3658#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0059#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4460#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2161#include "net/socket/next_proto.h"
[email protected]ab739042011-04-07 15:22:2862#include "net/socket/transport_client_socket_pool.h"
Bence Béky94658bf2018-05-11 19:22:5863#include "net/spdy/spdy_http_stream.h"
64#include "net/spdy/spdy_session.h"
65#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5766#include "net/ssl/ssl_cert_request_info.h"
67#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0868#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5469#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5470#include "url/gurl.h"
Matt Menkee4399022021-10-21 18:04:4271#include "url/origin.h"
Matt Menkef2ee07c2019-08-29 02:10:3672#include "url/scheme_host_port.h"
[email protected]e69c1cd2014-07-29 07:42:2973#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5274
Douglas Creager3cb042052018-11-06 23:08:5275#if BUILDFLAG(ENABLE_REPORTING)
76#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1477#include "net/reporting/reporting_header_parser.h"
78#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4279#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5280
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2281namespace net {
82
Biljith Jayan45a41722017-08-16 18:43:1483namespace {
David Benjamin83ddfb32018-03-30 01:07:5284
Biljith Jayan45a41722017-08-16 18:43:1485// Max number of |retry_attempts| (excluding the initial request) after which
86// we give up and show an error page.
87const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5288
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.
97const size_t kMaxRestarts = 32;
98
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2299void 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 Jayan45a41722017-08-16 18:43:14109
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22110} // namespace
initial.commit586acc5fe2008-07-26 22:42:52111
Victor Costan9c7302b2018-08-27 16:39:44112const int HttpNetworkTransaction::kDrainBodyBufferSize;
113
[email protected]262eec82013-03-19 21:01:36114HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
115 HttpNetworkSession* session)
Tsuyoshi Horoca07e3a2022-06-07 07:37:56116 : io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
Bence Béky61f756c2018-04-25 14:17:53117 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52118 session_(session),
Tsuyoshi Horoca07e3a2022-06-07 07:37:56119 priority_(priority) {}
[email protected]3ce7df0f2010-03-03 00:30:50120
[email protected]0b0bf032010-09-21 18:08:50121HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42122#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11123 // If no error or success report has been generated yet at this point, then
124 // this network transaction was prematurely cancelled.
125 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42126#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50127 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50128 // TODO(mbelshe): The stream_ should be able to compute whether or not the
129 // stream should be kept alive. No reason to compute here
130 // and pass it in.
Matt Menke2451c8b2020-12-08 22:42:51131 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE ||
132 close_connection_on_destruction_) {
[email protected]0b0bf032010-09-21 18:08:50133 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34134 } else if (stream_->IsResponseBodyComplete()) {
135 // If the response body is complete, we can just reuse the socket.
136 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50137 } else {
mmenkebd84c392015-09-02 14:12:34138 // Otherwise, we try to drain the response body.
139 HttpStream* stream = stream_.release();
140 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50141 }
142 }
[email protected]02cad5d2013-10-02 08:14:03143 if (request_ && request_->upload_data_stream)
144 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50145}
146
[email protected]684970b2009-08-14 04:54:46147int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04148 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20149 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23150 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
151 return ERR_CACHE_MISS;
152
Ramin Halavatib5e433e2018-02-07 07:41:10153 DCHECK(request_info->traffic_annotation.is_valid());
Brianna Goldstein314ddf722022-09-24 02:00:52154 DCHECK(request_info->IsConsistent());
[email protected]9e743cd2010-03-16 07:03:53155 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04156 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15157 url_ = request_->url;
Brianna Goldsteinc8ae1392022-10-10 20:09:47158 network_anonymization_key_ = request_->network_anonymization_key;
Douglas Creageref5eecdc2018-11-09 20:50:36159#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42160 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36161 request_method_ = request_->method;
162 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
163 &request_referrer_);
164 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
165 &request_user_agent_);
166 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42167 start_timeticks_ = base::TimeTicks::Now();
168#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04169
Ryan Sleevi24fe2682018-08-16 21:33:46170 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
171 server_ssl_config_.disable_cert_verification_network_fetches = true;
172 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19173 }
[email protected]6fbac162011-06-20 00:29:04174
Yu Su2d738bb2020-10-30 02:23:55175 if (request_->idempotency == IDEMPOTENT ||
176 (request_->idempotency == DEFAULT_IDEMPOTENCY &&
177 HttpUtil::IsMethodSafe(request_info->method))) {
Steven Valdezb4ff0412018-01-18 22:39:27178 can_send_early_data_ = true;
179 }
180
Dominic Farolino4ecaf0a2019-08-16 06:40:09181 if (request_->load_flags & LOAD_PREFETCH) {
jkarlinfb1d5172015-01-12 14:10:29182 response_.unused_since_prefetch = true;
Dominic Farolino4ecaf0a2019-08-16 06:40:09183 }
184
185 if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
186 DCHECK(response_.unused_since_prefetch);
187 response_.restricted_prefetch = true;
188 }
jkarlinfb1d5172015-01-12 14:10:29189
Helen Lib495c3802018-03-30 13:46:09190 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04191 int rv = DoLoop(OK);
192 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04193 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42194
195 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
196 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
197 // other net::Error can be returned.
198 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04199 return rv;
200}
201
202int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04203 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38204 DCHECK(!stream_.get());
205 DCHECK(!stream_request_.get());
206 DCHECK_EQ(STATE_NONE, next_state_);
207
David Benjamin83ddfb32018-03-30 01:07:52208 if (!CheckMaxRestarts())
209 return ERR_TOO_MANY_RETRIES;
210
[email protected]82918cc2010-08-25 17:24:50211 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38212
[email protected]ccb40e52008-09-17 20:54:40213 int rv = DoLoop(OK);
214 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04215 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42216
217 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
218 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
219 // other net::Error can be returned.
220 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11221 return rv;
[email protected]96d570e42008-08-05 22:43:04222}
223
[email protected]0b45559b2009-06-12 21:45:11224int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08225 scoped_refptr<X509Certificate> client_cert,
226 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04227 CompletionOnceCallback callback) {
David Benjaminbac8dff2019-08-07 01:30:41228 // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
229 // existing streams and stream requests to force a new connection.
[email protected]8e6441ca2010-08-19 05:56:38230 DCHECK(!stream_request_.get());
231 DCHECK(!stream_.get());
232 DCHECK_EQ(STATE_NONE, next_state_);
233
David Benjamin83ddfb32018-03-30 01:07:52234 if (!CheckMaxRestarts())
235 return ERR_TOO_MANY_RETRIES;
236
David Benjaminbac8dff2019-08-07 01:30:41237 // Add the credentials to the client auth cache. The next stream request will
238 // then pick them up.
239 session_->ssl_client_context()->SetClientCertificate(
mattm436ccfe2017-06-19 20:24:08240 response_.cert_request_info->host_and_port, std::move(client_cert),
241 std::move(client_private_key));
David Benjaminbac8dff2019-08-07 01:30:41242
243 if (!response_.cert_request_info->is_proxy)
244 configured_client_cert_for_server_ = true;
245
[email protected]0b45559b2009-06-12 21:45:11246 // Reset the other member variables.
247 // Note: this is necessary only with SSL renegotiation.
248 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50249 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11250 int rv = DoLoop(OK);
251 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04252 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42253
254 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
255 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
256 // other net::Error can be returned.
257 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11258 return rv;
259}
260
Bence Béky046f8c82018-06-12 02:26:04261int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
262 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52263 if (!CheckMaxRestarts())
264 return ERR_TOO_MANY_RETRIES;
265
[email protected]0757e7702009-03-27 04:00:22266 HttpAuth::Target target = pending_auth_target_;
267 if (target == HttpAuth::AUTH_NONE) {
268 NOTREACHED();
269 return ERR_UNEXPECTED;
270 }
[email protected]0757e7702009-03-27 04:00:22271 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42272
[email protected]f3cf9802011-10-28 18:44:58273 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13274
[email protected]49639fa2011-12-20 23:22:41275 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38276
277 int rv = OK;
278 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
279 // In this case, we've gathered credentials for use with proxy
280 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50281 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35282 DCHECK(stream_request_ != nullptr);
283 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54284 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39285 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54286 } else {
[email protected]8e6441ca2010-08-19 05:56:38287 // In this case, we've gathered credentials for the server or the proxy
288 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35289 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54290 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38291 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42292 // Note: If an error is encountered while draining the old response body, no
293 // Network Error Logging report will be generated, because the error was
294 // with the old request, which will already have had a NEL report generated
295 // for it due to the auth challenge (so we don't report a second error for
296 // that request).
[email protected]a7ea8832010-07-12 17:54:54297 }
[email protected]c3b35c22008-09-27 03:19:42298
[email protected]c3b35c22008-09-27 03:19:42299 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04300 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42301 return rv;
[email protected]96d570e42008-08-05 22:43:04302}
303
[email protected]f9ee6b52008-11-08 06:46:23304void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
305 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38306 DCHECK(!stream_request_.get());
307
Bence Béky3238f2e12017-09-22 22:44:49308 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
309 if (target == HttpAuth::AUTH_SERVER &&
310 auth_controllers_[target]->NeedsHTTP11()) {
311 session_->http_server_properties()->SetHTTP11Required(
Brianna Goldsteinc8ae1392022-10-10 20:09:47312 url::SchemeHostPort(request_->url), network_anonymization_key_);
Bence Béky3238f2e12017-09-22 22:44:49313 }
314
[email protected]2d2697f92009-02-18 21:00:32315 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57316 // Even if the server says the connection is keep-alive, we have to be
317 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34318 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57319 // If the response body hasn't been completely read, we need to drain
320 // it first.
[email protected]351ab642010-08-05 16:55:31321 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32322 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44323 read_buf_ = base::MakeRefCounted<IOBuffer>(
324 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32325 read_buf_len_ = kDrainBodyBufferSize;
326 return;
327 }
[email protected]0877e3d2009-10-17 22:29:57328 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09329 }
330
[email protected]2d2697f92009-02-18 21:00:32331 // We don't need to drain the response body, so we act as if we had drained
332 // the response body.
333 DidDrainBodyForAuthRestart(keep_alive);
334}
335
336void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38337 DCHECK(!stream_request_.get());
338
339 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19340 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22341 total_sent_bytes_ += stream_->GetTotalSentBytes();
Tsuyoshi Horo34e23cc2022-07-11 02:04:05342 std::unique_ptr<HttpStream> new_stream;
mmenkebd84c392015-09-02 14:12:34343 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38344 // We should call connection_->set_idle_time(), but this doesn't occur
345 // often enough to be worth the trouble.
346 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39347 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38348 }
[email protected]697ef4c2010-10-14 16:38:58349
350 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46351 // Close the stream and mark it as not_reusable. Even in the
352 // keep_alive case, we've determined that the stream_ is not
353 // reusable if new_stream is NULL.
354 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58355 next_state_ = STATE_CREATE_STREAM;
356 } else {
sclittlefb249892015-09-10 21:33:22357 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19358 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22359 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
Ali Beyada0b1a1c2022-04-08 20:08:14360 next_state_ = STATE_CONNECTED_CALLBACK;
[email protected]697ef4c2010-10-14 16:38:58361 }
Tsuyoshi Horo34e23cc2022-07-11 02:04:05362 stream_ = std::move(new_stream);
[email protected]2d2697f92009-02-18 21:00:32363 }
[email protected]f9ee6b52008-11-08 06:46:23364
365 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58366 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23367}
368
[email protected]8e6441ca2010-08-19 05:56:38369bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
370 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
371 HaveAuth(pending_auth_target_);
372}
373
Bence Béky046f8c82018-06-12 02:26:04374int HttpNetworkTransaction::Read(IOBuffer* buf,
375 int buf_len,
376 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04377 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35378 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04379
[email protected]ad8e04a2010-11-01 04:16:27380 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38381 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04382 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29383 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04384 // bytes when establishing a tunnel because they might be controlled by
385 // an active network attacker. We don't worry about this for HTTP
386 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29387 // We reach this case when the user cancels a 407 proxy auth prompt. We
388 // also don't worry about this for an HTTPS Proxy, because the
389 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04390 // See http://crbug.com/8473.
Eric Romandbf7441f2020-07-28 07:05:02391 DCHECK(proxy_info_.is_http_like());
[email protected]9094b602012-02-27 21:44:58392 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]8a1f3312010-05-25 19:25:04393 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44394 }
395
[email protected]e60e47a2010-07-14 03:37:18396 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15397 next_state_ = STATE_READ_BODY;
398
[email protected]96d570e42008-08-05 22:43:04399 read_buf_ = buf;
400 read_buf_len_ = buf_len;
401
[email protected]96d570e42008-08-05 22:43:04402 int rv = DoLoop(OK);
403 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04404 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04405 return rv;
406}
407
[email protected]8cd06c02014-01-25 07:50:14408void HttpNetworkTransaction::StopCaching() {}
409
sclittle4de1bab92015-09-22 21:28:24410int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
411 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19412 if (stream_)
413 total_received_bytes += stream_->GetTotalReceivedBytes();
414 return total_received_bytes;
415}
416
sclittlefb249892015-09-10 21:33:22417int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
418 int64_t total_sent_bytes = total_sent_bytes_;
419 if (stream_)
420 total_sent_bytes += stream_->GetTotalSentBytes();
421 return total_sent_bytes;
422}
423
[email protected]8cd06c02014-01-25 07:50:14424void HttpNetworkTransaction::DoneReading() {}
425
[email protected]96d570e42008-08-05 22:43:04426const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55427 return &response_;
[email protected]96d570e42008-08-05 22:43:04428}
429
430LoadState HttpNetworkTransaction::GetLoadState() const {
431 // TODO(wtc): Define a new LoadState value for the
432 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
433 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48434 case STATE_CREATE_STREAM:
435 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50436 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38437 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15438 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
439 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57440 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04441 return LOAD_STATE_SENDING_REQUEST;
442 case STATE_READ_HEADERS_COMPLETE:
443 return LOAD_STATE_WAITING_FOR_RESPONSE;
444 case STATE_READ_BODY_COMPLETE:
445 return LOAD_STATE_READING_RESPONSE;
446 default:
447 return LOAD_STATE_IDLE;
448 }
449}
450
[email protected]8cd06c02014-01-25 07:50:14451void HttpNetworkTransaction::SetQuicServerInfo(
452 QuicServerInfo* quic_server_info) {}
453
[email protected]5033ab82013-03-22 20:17:46454bool HttpNetworkTransaction::GetLoadTimingInfo(
455 LoadTimingInfo* load_timing_info) const {
456 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
457 return false;
458
459 load_timing_info->proxy_resolve_start =
460 proxy_info_.proxy_resolve_start_time();
461 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
462 load_timing_info->send_start = send_start_time_;
463 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46464 return true;
465}
466
ttuttled9dbc652015-09-29 20:00:59467bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36468 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59469 return false;
470
471 *endpoint = remote_endpoint_;
472 return true;
473}
474
zhongyi48704c182015-12-07 07:52:02475void HttpNetworkTransaction::PopulateNetErrorDetails(
476 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12477 *details = net_error_details_;
478 if (stream_)
479 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02480}
481
[email protected]5033ab82013-03-22 20:17:46482void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
483 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50484
[email protected]bf828982013-08-14 18:01:47485 if (stream_request_)
486 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03487 if (stream_)
488 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50489
rdsmith1d343be52016-10-21 20:37:50490 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46491}
492
[email protected]831e4a32013-11-14 02:14:44493void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
494 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
495 websocket_handshake_stream_base_create_helper_ = create_helper;
496}
497
[email protected]1826a402014-01-08 15:40:48498void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
Will Cassella61125f92020-07-25 09:11:37499 BeforeNetworkStartCallback callback) {
500 before_network_start_callback_ = std::move(callback);
[email protected]1826a402014-01-08 15:40:48501}
502
Titouan Rigoudy78af7da2020-07-07 14:30:12503void HttpNetworkTransaction::SetConnectedCallback(
504 const ConnectedCallback& callback) {
505 connected_callback_ = callback;
506}
507
Andrey Kosyakov83a6eee2017-08-14 19:20:04508void HttpNetworkTransaction::SetRequestHeadersCallback(
509 RequestHeadersCallback callback) {
510 DCHECK(!stream_);
511 request_headers_callback_ = std::move(callback);
512}
513
Kenichi Ishibashi74155532021-03-13 01:38:06514void HttpNetworkTransaction::SetEarlyResponseHeadersCallback(
515 ResponseHeadersCallback callback) {
516 DCHECK(!stream_);
517 early_response_headers_callback_ = std::move(callback);
518}
519
Andrey Kosyakov2e893e62017-08-31 17:00:52520void HttpNetworkTransaction::SetResponseHeadersCallback(
521 ResponseHeadersCallback callback) {
522 DCHECK(!stream_);
523 response_headers_callback_ = std::move(callback);
524}
525
[email protected]1826a402014-01-08 15:40:48526int HttpNetworkTransaction::ResumeNetworkStart() {
527 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
528 return DoLoop(OK);
529}
530
Aaron Tagliaboschib35cc402021-03-10 22:23:32531void HttpNetworkTransaction::ResumeAfterConnected(int result) {
532 DCHECK_EQ(next_state_, STATE_CONNECTED_CALLBACK_COMPLETE);
533 OnIOComplete(result);
534}
535
Matt Menke2451c8b2020-12-08 22:42:51536void HttpNetworkTransaction::CloseConnectionOnDestruction() {
537 close_connection_on_destruction_ = true;
538}
539
[email protected]102e27c2011-02-23 01:01:31540void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
541 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00542 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50543 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38544 DCHECK(stream_request_.get());
545
sclittlefb249892015-09-10 21:33:22546 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19547 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22548 total_sent_bytes_ += stream_->GetTotalSentBytes();
549 }
bnc5029f4632017-06-08 16:19:00550 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04551 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14552 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31553 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52554 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21555 response_.alpn_negotiated_protocol =
556 NextProtoToString(stream_request_->negotiated_protocol());
Yoichiro Hibarac69381062022-09-16 11:37:19557 response_.alternate_protocol_usage =
558 stream_request_->alternate_protocol_usage();
[email protected]8e6441ca2010-08-19 05:56:38559 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Cammie Smith Barnesbf91e2a2020-12-23 20:49:04560 response_.dns_aliases = stream_->GetDnsAliases();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22561 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38562 OnIOComplete(OK);
563}
564
xunjieli5749218c2016-03-22 16:43:06565void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08566 const SSLConfig& used_ssl_config,
567 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00568 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08569 NOTREACHED();
570}
571
[email protected]a9cf2b92013-10-30 12:08:49572void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50573 const SSLConfig& used_ssl_config,
574 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00575 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
576 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50577}
578
Ryan Hamilton75f197262017-08-17 14:00:07579void HttpNetworkTransaction::OnStreamFailed(
580 int result,
581 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22582 const SSLConfig& used_ssl_config,
dalyk6d7a8c52019-12-18 21:43:01583 const ProxyInfo& used_proxy_info,
584 ResolveErrorInfo resolve_error_info) {
[email protected]82918cc2010-08-25 17:24:50585 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38586 DCHECK_NE(OK, result);
587 DCHECK(stream_request_.get());
588 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14589 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07590 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22591 proxy_info_ = used_proxy_info;
592 SetProxyInfoInReponse(used_proxy_info, &response_);
dalyk6d7a8c52019-12-18 21:43:01593 response_.resolve_error_info = resolve_error_info;
[email protected]8e6441ca2010-08-19 05:56:38594
595 OnIOComplete(result);
596}
597
[email protected]102e27c2011-02-23 01:01:31598void HttpNetworkTransaction::OnCertificateError(
599 int result,
600 const SSLConfig& used_ssl_config,
601 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50602 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38603 DCHECK_NE(OK, result);
604 DCHECK(stream_request_.get());
605 DCHECK(!stream_.get());
606
607 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14608 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38609
610 // TODO(mbelshe): For now, we're going to pass the error through, and that
611 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50612 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
613 // good and the user chooses to ignore the error. This is not ideal, but not
614 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38615
616 OnIOComplete(result);
617}
618
619void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50620 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31621 const SSLConfig& used_ssl_config,
622 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50623 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38624 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50625 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38626
627 establishing_tunnel_ = true;
628 response_.headers = proxy_response.headers;
629 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50630 response_.did_use_http_auth = proxy_response.did_use_http_auth;
Wojciech Dzierżanowski758b4fb2021-04-19 23:26:59631 SetProxyInfoInReponse(used_proxy_info, &response_);
eustasc7d27da2017-04-06 10:33:20632
Matt Menke48f5f2d02021-02-11 18:54:26633 if (!ContentEncodingsValid()) {
eustasc7d27da2017-04-06 10:33:20634 DoCallback(ERR_CONTENT_DECODING_FAILED);
635 return;
636 }
637
[email protected]8e6441ca2010-08-19 05:56:38638 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14639 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31640 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38641
642 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
643 pending_auth_target_ = HttpAuth::AUTH_PROXY;
644
645 DoCallback(OK);
646}
647
648void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31649 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50650 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50651 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38652
[email protected]102957f2011-09-02 17:10:14653 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38654 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58655 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38656}
657
zhongyi48704c182015-12-07 07:52:02658void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12659 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02660}
661
David Benjamin3c19a2ed2022-04-09 21:58:27662ConnectionAttempts HttpNetworkTransaction::GetConnectionAttempts() const {
663 return connection_attempts_;
ttuttle1f2d7e92015-04-28 16:17:47664}
665
Adam Ricecb76ac62015-02-20 05:33:25666bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37667 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52668}
669
Adam Rice425cf122015-01-19 06:18:24670bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
Eric Romandbf7441f2020-07-28 07:05:02671 return proxy_info_.is_http_like() &&
Adam Rice425cf122015-01-19 06:18:24672 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
673}
674
initial.commit586acc5fe2008-07-26 22:42:52675void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50676 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41677 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52678
Douglas Creageref5eecdc2018-11-09 20:50:36679#if BUILDFLAG(ENABLE_REPORTING)
680 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42681 // report about this network request if the result was an error.
682 GenerateNetworkErrorLoggingReportIfError(rv);
683#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36684
[email protected]96d570e42008-08-05 22:43:04685 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27686 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52687}
688
689void HttpNetworkTransaction::OnIOComplete(int result) {
690 int rv = DoLoop(result);
691 if (rv != ERR_IO_PENDING)
692 DoCallback(rv);
693}
694
695int HttpNetworkTransaction::DoLoop(int result) {
696 DCHECK(next_state_ != STATE_NONE);
697
698 int rv = result;
699 do {
700 State state = next_state_;
701 next_state_ = STATE_NONE;
702 switch (state) {
[email protected]1826a402014-01-08 15:40:48703 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
704 DCHECK_EQ(OK, rv);
705 rv = DoNotifyBeforeCreateStream();
706 break;
[email protected]82918cc2010-08-25 17:24:50707 case STATE_CREATE_STREAM:
708 DCHECK_EQ(OK, rv);
709 rv = DoCreateStream();
710 break;
711 case STATE_CREATE_STREAM_COMPLETE:
712 rv = DoCreateStreamComplete(rv);
713 break;
[email protected]351ab642010-08-05 16:55:31714 case STATE_INIT_STREAM:
715 DCHECK_EQ(OK, rv);
716 rv = DoInitStream();
717 break;
718 case STATE_INIT_STREAM_COMPLETE:
719 rv = DoInitStreamComplete(rv);
720 break;
Ali Beyada0b1a1c2022-04-08 20:08:14721 case STATE_CONNECTED_CALLBACK:
722 rv = DoConnectedCallback();
723 break;
Aaron Tagliaboschib35cc402021-03-10 22:23:32724 case STATE_CONNECTED_CALLBACK_COMPLETE:
725 rv = DoConnectedCallbackComplete(rv);
726 break;
[email protected]044de0642010-06-17 10:42:15727 case STATE_GENERATE_PROXY_AUTH_TOKEN:
728 DCHECK_EQ(OK, rv);
729 rv = DoGenerateProxyAuthToken();
730 break;
731 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
732 rv = DoGenerateProxyAuthTokenComplete(rv);
733 break;
734 case STATE_GENERATE_SERVER_AUTH_TOKEN:
735 DCHECK_EQ(OK, rv);
736 rv = DoGenerateServerAuthToken();
737 break;
738 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
739 rv = DoGenerateServerAuthTokenComplete(rv);
740 break;
[email protected]daddea62012-09-19 05:51:13741 case STATE_INIT_REQUEST_BODY:
742 DCHECK_EQ(OK, rv);
743 rv = DoInitRequestBody();
744 break;
745 case STATE_INIT_REQUEST_BODY_COMPLETE:
746 rv = DoInitRequestBodyComplete(rv);
747 break;
[email protected]4875ba12011-03-30 22:31:51748 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55749 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00750 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51751 rv = DoBuildRequest();
752 break;
753 case STATE_BUILD_REQUEST_COMPLETE:
754 rv = DoBuildRequestComplete(rv);
755 break;
756 case STATE_SEND_REQUEST:
757 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57758 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52759 break;
[email protected]0877e3d2009-10-17 22:29:57760 case STATE_SEND_REQUEST_COMPLETE:
761 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43762 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00763 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52764 break;
765 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55766 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00767 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52768 rv = DoReadHeaders();
769 break;
770 case STATE_READ_HEADERS_COMPLETE:
771 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43772 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00773 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52774 break;
775 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55776 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00777 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52778 rv = DoReadBody();
779 break;
780 case STATE_READ_BODY_COMPLETE:
781 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43782 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00783 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52784 break;
[email protected]2d2697f92009-02-18 21:00:32785 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55786 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53787 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32789 rv = DoDrainBodyForAuthRestart();
790 break;
791 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
792 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43793 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00794 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32795 break;
initial.commit586acc5fe2008-07-26 22:42:52796 default:
797 NOTREACHED() << "bad state";
798 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04799 break;
initial.commit586acc5fe2008-07-26 22:42:52800 }
801 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
802
803 return rv;
804}
805
[email protected]1826a402014-01-08 15:40:48806int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
807 next_state_ = STATE_CREATE_STREAM;
808 bool defer = false;
809 if (!before_network_start_callback_.is_null())
Will Cassella61125f92020-07-25 09:11:37810 std::move(before_network_start_callback_).Run(&defer);
[email protected]1826a402014-01-08 15:40:48811 if (!defer)
812 return OK;
813 return ERR_IO_PENDING;
814}
815
[email protected]82918cc2010-08-25 17:24:50816int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55817 response_.network_accessed = true;
818
[email protected]82918cc2010-08-25 17:24:50819 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17820 // IP based pooling is only enabled on a retry after 421 Misdirected Request
821 // is received. Alternative Services are also disabled in this case (though
822 // they can also be disabled when retrying after a QUIC error).
823 if (!enable_ip_based_pooling_)
824 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44825 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27826 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06827 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
828 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
829 websocket_handshake_stream_base_create_helper_,
830 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44831 } else {
xunjieli96f2a402017-06-05 17:24:27832 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29833 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27834 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44835 }
[email protected]26816882010-10-14 18:03:09836 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38837 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31838}
839
[email protected]82918cc2010-08-25 17:24:50840int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59841 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59842 if (result == OK) {
Ali Beyada0b1a1c2022-04-08 20:08:14843 next_state_ = STATE_CONNECTED_CALLBACK;
[email protected]8e6441ca2010-08-19 05:56:38844 DCHECK(stream_.get());
bncfacdd852015-01-09 19:22:54845 } else if (result == ERR_HTTP_1_1_REQUIRED ||
846 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
847 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59848 }
849
David Benjamin76a40ad2018-02-24 22:22:08850 // Handle possible client certificate errors that may have occurred if the
851 // stream used SSL for one or more of the layers.
852 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30853
[email protected]8e6441ca2010-08-19 05:56:38854 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09855 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38856 return result;
[email protected]394816e92010-08-03 07:38:59857}
858
[email protected]82918cc2010-08-25 17:24:50859int HttpNetworkTransaction::DoInitStream() {
860 DCHECK(stream_.get());
861 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59862
Ali Beyada0b1a1c2022-04-08 20:08:14863 return stream_->InitializeStream(can_send_early_data_, priority_, net_log_,
864 io_callback_);
[email protected]82918cc2010-08-25 17:24:50865}
866
867int HttpNetworkTransaction::DoInitStreamComplete(int result) {
Titouan Rigoudy78af7da2020-07-07 14:30:12868 if (result != OK) {
[email protected]82918cc2010-08-25 17:24:50869 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26870 result = HandleIOError(result);
871
872 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22873 if (stream_) {
874 total_received_bytes_ += stream_->GetTotalReceivedBytes();
875 total_sent_bytes_ += stream_->GetTotalSentBytes();
876 }
zhongyica364fbb2015-12-12 03:39:12877 CacheNetErrorDetailsAndResetStream();
Titouan Rigoudy78af7da2020-07-07 14:30:12878
879 return result;
[email protected]82918cc2010-08-25 17:24:50880 }
881
Ali Beyada0b1a1c2022-04-08 20:08:14882 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
883 return result;
884}
885
886int HttpNetworkTransaction::DoConnectedCallback() {
887 // Register the HttpRequestInfo object on the stream here so that it's
888 // available when invoking the `connected_callback_`, as
889 // HttpStream::GetAcceptChViaAlps() needs the HttpRequestInfo to retrieve
890 // the ACCEPT_CH frame payload.
891 stream_->RegisterRequest(request_);
Aaron Tagliaboschib35cc402021-03-10 22:23:32892 next_state_ = STATE_CONNECTED_CALLBACK_COMPLETE;
893
Titouan Rigoudy5cf08f12022-05-20 15:49:22894 int result = stream_->GetRemoteEndpoint(&remote_endpoint_);
895 if (result != OK) {
896 // `GetRemoteEndpoint()` fails when the underlying socket is not connected
897 // anymore, even though the peer's address is known. This can happen when
898 // we picked a socket from socket pools while it was still connected, but
899 // the remote side closes it before we get a chance to send our request.
900 // See if we should retry the request based on the error code we got.
901 return HandleIOError(result);
902 }
903
Ali Beyada0b1a1c2022-04-08 20:08:14904 if (connected_callback_.is_null()) {
905 return OK;
Titouan Rigoudy78af7da2020-07-07 14:30:12906 }
907
Ali Beyada0b1a1c2022-04-08 20:08:14908 // Fire off notification that we have successfully connected.
909 TransportType type = TransportType::kDirect;
910 if (!proxy_info_.is_direct()) {
911 type = TransportType::kProxied;
912 }
913 return connected_callback_.Run(
914 TransportInfo(type, remote_endpoint_,
915 std::string{stream_->GetAcceptChViaAlps()}),
916 base::BindOnce(&HttpNetworkTransaction::ResumeAfterConnected,
917 base::Unretained(this)));
Aaron Tagliaboschib35cc402021-03-10 22:23:32918}
919
920int HttpNetworkTransaction::DoConnectedCallbackComplete(int result) {
Titouan Rigoudyc6766812022-04-01 13:29:56921 if (result != OK) {
922 if (stream_) {
923 stream_->Close(/*not_reusable=*/false);
924 }
925
926 // Stop the state machine here if the call failed.
927 return result;
Titouan Rigoudy78af7da2020-07-07 14:30:12928 }
Titouan Rigoudyc6766812022-04-01 13:29:56929
Ali Beyada0b1a1c2022-04-08 20:08:14930 next_state_ = STATE_INIT_STREAM;
Titouan Rigoudyc6766812022-04-01 13:29:56931 return OK;
[email protected]82918cc2010-08-25 17:24:50932}
933
[email protected]044de0642010-06-17 10:42:15934int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
935 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
936 if (!ShouldApplyProxyAuth())
937 return OK;
[email protected]394816e92010-08-03 07:38:59938 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
939 if (!auth_controllers_[target].get())
Matt Menkebe090422019-10-18 20:25:26940 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
Brianna Goldsteina56efdb2022-10-06 19:55:54941 target, AuthURL(target), request_->network_anonymization_key,
Matt Menkebe090422019-10-18 20:25:26942 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27943 session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59944 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41945 io_callback_,
[email protected]394816e92010-08-03 07:38:59946 net_log_);
[email protected]044de0642010-06-17 10:42:15947}
948
949int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
950 DCHECK_NE(ERR_IO_PENDING, rv);
951 if (rv == OK)
952 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
953 return rv;
954}
955
956int HttpNetworkTransaction::DoGenerateServerAuthToken() {
957 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59958 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54959 if (!auth_controllers_[target].get()) {
Matt Menkebe090422019-10-18 20:25:26960 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
Brianna Goldsteina56efdb2022-10-06 19:55:54961 target, AuthURL(target), request_->network_anonymization_key,
Matt Menkebe090422019-10-18 20:25:26962 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27963 session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54964 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
965 auth_controllers_[target]->DisableEmbeddedIdentity();
966 }
[email protected]044de0642010-06-17 10:42:15967 if (!ShouldApplyServerAuth())
968 return OK;
[email protected]394816e92010-08-03 07:38:59969 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41970 io_callback_,
[email protected]394816e92010-08-03 07:38:59971 net_log_);
[email protected]044de0642010-06-17 10:42:15972}
973
974int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
975 DCHECK_NE(ERR_IO_PENDING, rv);
976 if (rv == OK)
nharperd6e65822016-03-30 23:05:48977 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14978 return rv;
979}
980
981int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24982 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14983 request_headers_.SetHeader(HttpRequestHeaders::kHost,
984 GetHostAndOptionalPort(request_->url));
985
986 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24987 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14988 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
989 "keep-alive");
990 } else {
991 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
992 }
993
[email protected]2979a492011-04-06 00:29:14994 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11995 if (request_->upload_data_stream) {
996 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14997 request_headers_.SetHeader(
998 HttpRequestHeaders::kTransferEncoding, "chunked");
999 } else {
1000 request_headers_.SetHeader(
1001 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:091002 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141003 }
csharrisonf473dd192015-08-18 13:54:131004 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141005 // An empty POST/PUT request still needs a content length. As for HEAD,
1006 // IE and Safari also add a content length header. Presumably it is to
1007 // support sending a HEAD request to an URL that only expects to be sent a
1008 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131009 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1010 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141011 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1012 }
1013
1014 // Honor load flags that impact proxy caches.
1015 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1016 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1017 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1018 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1019 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1020 }
1021
1022 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1023 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1024 &request_headers_);
1025 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1026 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1027 &request_headers_);
1028
[email protected]c10450102011-06-27 09:06:161029 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201030
[email protected]173f8e22013-04-10 04:18:201031 response_.did_use_http_auth =
1032 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1033 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141034 return OK;
1035}
1036
[email protected]daddea62012-09-19 05:51:131037int HttpNetworkTransaction::DoInitRequestBody() {
1038 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131039 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111040 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331041 rv = request_->upload_data_stream->Init(
1042 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1043 base::Unretained(this)),
1044 net_log_);
[email protected]daddea62012-09-19 05:51:131045 return rv;
1046}
[email protected]4875ba12011-03-30 22:31:511047
[email protected]daddea62012-09-19 05:51:131048int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1049 if (result == OK)
1050 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131051 return result;
1052}
1053
1054int HttpNetworkTransaction::DoBuildRequest() {
1055 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511056 headers_valid_ = false;
1057
1058 // This is constructed lazily (instead of within our Start method), so that
1059 // we have proxy info available.
1060 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241061 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141062 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511063 }
1064
[email protected]4875ba12011-03-30 22:31:511065 return OK;
1066}
1067
1068int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241069 if (result == OK)
1070 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511071 return result;
1072}
1073
[email protected]0877e3d2009-10-17 22:29:571074int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251075 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571076 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1077
Yu Suba2a1d72020-12-04 02:14:411078 stream_->SetRequestIdempotency(request_->idempotency);
[email protected]bf3eb002012-11-15 05:50:111079 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521080}
1081
[email protected]0877e3d2009-10-17 22:29:571082int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251083 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361084
1085 if (result == ERR_HTTP_1_1_REQUIRED ||
1086 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1087 return HandleHttp11Required(result);
1088 }
1089
initial.commit586acc5fe2008-07-26 22:42:521090 if (result < 0)
1091 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571092 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521093 return OK;
1094}
1095
1096int HttpNetworkTransaction::DoReadHeaders() {
1097 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411098 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521099}
1100
1101int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
David Benjamin38c55d32022-01-10 18:59:101102 // We can get a ERR_SSL_CLIENT_AUTH_CERT_NEEDED here due to SSL renegotiation.
1103 // Server certificate errors are impossible. Rather than reverify the new
1104 // server certificate, BoringSSL forbids server certificates from changing.
1105 DCHECK(!IsCertificateError(result));
1106 if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381107 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251108 DCHECK(IsSecureRequest());
David Benjamin1c4b6d012019-07-08 17:12:571109 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]90499482013-06-01 00:39:501110 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
David Benjaminbac8dff2019-08-07 01:30:411111 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1112 total_sent_bytes_ += stream_->GetTotalSentBytes();
1113 stream_->Close(true);
1114 CacheNetErrorDetailsAndResetStream();
[email protected]2181ea002009-06-09 01:37:271115 }
1116
bncfacdd852015-01-09 19:22:541117 if (result == ERR_HTTP_1_1_REQUIRED ||
1118 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1119 return HandleHttp11Required(result);
1120 }
1121
[email protected]c871864d72014-03-13 19:56:191122 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1123 // response headers were received, we do the best we can to make sense of it
1124 // and send it back up the stack.
1125 //
1126 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1127 // bizarre for SPDY. Assuming this logic is useful at all.
1128 // TODO(davidben): Bubble the error code up so we do not cache?
1129 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1130 result = OK;
1131
1132 if (result < 0)
1133 return HandleIOError(result);
1134
[email protected]90499482013-06-01 00:39:501135 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521136
Kenichi Ishibashi74155532021-03-13 01:38:061137 // Check for a 103 Early Hints response.
1138 if (response_.headers->response_code() == HTTP_EARLY_HINTS) {
1139 NetLogResponseHeaders(
1140 net_log_,
1141 NetLogEventType::HTTP_TRANSACTION_READ_EARLY_HINTS_RESPONSE_HEADERS,
1142 response_.headers.get());
1143
1144 // Early Hints does not make sense for a WebSocket handshake.
1145 if (ForWebSocketHandshake())
1146 return ERR_FAILED;
1147
1148 // TODO(crbug.com/671310): Validate headers? It seems that
1149 // "Content-Encoding" etc should not appear.
1150
1151 if (early_response_headers_callback_)
1152 early_response_headers_callback_.Run(std::move(response_.headers));
1153
1154 response_.headers =
1155 base::MakeRefCounted<HttpResponseHeaders>(std::string());
1156 next_state_ = STATE_READ_HEADERS;
1157 return OK;
1158 }
1159
Matt Menke48f5f2d02021-02-11 18:54:261160 if (!ContentEncodingsValid())
eustasc7d27da2017-04-06 10:33:201161 return ERR_CONTENT_DECODING_FAILED;
1162
[email protected]d58ceea82014-06-04 10:55:541163 // On a 408 response from the server ("Request Timeout") on a stale socket,
David Schinazif832cb82019-11-08 22:25:271164 // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
1165 // multiplex requests and have no need for 408.
Matt Menke48f5f2d02021-02-11 18:54:261166 if (response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
David Schinazif832cb82019-11-08 22:25:271167 HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
1168 HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
[email protected]d58ceea82014-06-04 10:55:541169 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421170#if BUILDFLAG(ENABLE_REPORTING)
1171 GenerateNetworkErrorLoggingReport(OK);
1172#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541173 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001174 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541175 response_.headers->response_code());
1176 // This will close the socket - it would be weird to try and reuse it, even
1177 // if the server doesn't actually close it.
1178 ResetConnectionAndRequestForResend();
1179 return OK;
1180 }
1181
Eric Roman06bd9742019-07-13 15:19:131182 NetLogResponseHeaders(net_log_,
1183 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1184 response_.headers.get());
Andrey Kosyakov2e893e62017-08-31 17:00:521185 if (response_headers_callback_)
1186 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391187
bncbe0f6af2015-10-15 17:49:561188 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571189 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1190 // indicates a buggy server. See:
1191 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1192 if (request_->method == "PUT")
1193 return ERR_METHOD_NOT_SUPPORTED;
1194 }
[email protected]4ddaf2502008-10-23 18:26:191195
Matt Menke48f5f2d02021-02-11 18:54:261196 if (can_send_early_data_ &&
Steven Valdez58097ec32018-07-16 18:29:041197 response_.headers->response_code() == HTTP_TOO_EARLY) {
1198 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1199 }
1200
[email protected]0877e3d2009-10-17 22:29:571201 // Check for an intermediate 100 Continue response. An origin server is
1202 // allowed to send this response even if we didn't ask for it, so we just
1203 // need to skip over it.
1204 // We treat any other 1xx in this same way (although in practice getting
1205 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441206 // Unless this is a WebSocket request, in which case we pass it on up.
1207 if (response_.headers->response_code() / 100 == 1 &&
1208 !ForWebSocketHandshake()) {
Tsuyoshi Horo2c0a5042022-07-06 05:53:071209 response_.headers =
1210 base::MakeRefCounted<HttpResponseHeaders>(std::string());
[email protected]0877e3d2009-10-17 22:29:571211 next_state_ = STATE_READ_HEADERS;
1212 return OK;
1213 }
1214
Yutaka Hiranoc267fb242022-07-05 01:27:171215 const bool has_body_with_null_source =
1216 request_->upload_data_stream &&
1217 request_->upload_data_stream->has_null_source();
davidbence688ae2017-05-04 15:12:591218 if (response_.headers->response_code() == 421 &&
Yutaka Hiranoc267fb242022-07-05 01:27:171219 (enable_ip_based_pooling_ || enable_alternative_services_) &&
1220 !has_body_with_null_source) {
Lily Chenfec60d92019-01-24 01:16:421221#if BUILDFLAG(ENABLE_REPORTING)
1222 GenerateNetworkErrorLoggingReport(OK);
1223#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591224 // Retry the request with both IP based pooling and Alternative Services
1225 // disabled.
1226 enable_ip_based_pooling_ = false;
1227 enable_alternative_services_ = false;
1228 net_log_.AddEvent(
1229 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1230 ResetConnectionAndRequestForResend();
1231 return OK;
bnc8016c1f2017-03-31 02:11:291232 }
1233
bncb26024382016-06-29 02:39:451234 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491235 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081236 if (response_.ssl_info.is_valid() &&
1237 !IsCertStatusError(response_.ssl_info.cert_status)) {
1238 session_->http_stream_factory()->ProcessAlternativeServices(
Brianna Goldsteinc8ae1392022-10-10 20:09:471239 session_, network_anonymization_key_, response_.headers.get(),
Bence Béky230ac612017-08-30 19:17:081240 url::SchemeHostPort(request_->url));
1241 }
1242 }
asanka5ffd5d72016-03-23 16:20:491243
[email protected]e772db3f2010-07-12 18:11:131244 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571245 if (rv != OK)
1246 return rv;
1247
Douglas Creager3cb042052018-11-06 23:08:521248#if BUILDFLAG(ENABLE_REPORTING)
Ian Clelland4563d4d2021-06-02 20:25:501249 // Note: This just handles the legacy Report-To header, which is still
1250 // required for NEL. The newer Reporting-Endpoints header is processed in
1251 // network::PopulateParsedHeaders().
1252 ProcessReportToHeader();
1253
Lily Chenfec60d92019-01-24 01:16:421254 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1255 // reports generated before the NEL header is processed here will just be
1256 // dropped by the NetworkErrorLoggingService.
Douglas Creager3cb042052018-11-06 23:08:521257 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421258
1259 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111260 // code), or if the response body will not be read, or on a redirect.
1261 // Note: This will report a success for a redirect even if an error is
1262 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421263 int response_code = response_.headers->response_code();
1264 if ((response_code >= 400 && response_code < 600) ||
1265 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1266 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111267 response_.headers->GetContentLength() == 0 ||
1268 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421269 GenerateNetworkErrorLoggingReport(OK);
1270 }
1271#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521272
[email protected]0877e3d2009-10-17 22:29:571273 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261274
1275 // We have reached the end of Start state machine, set the RequestInfo to
1276 // null.
1277 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1278 // only until the final response headers are received. Clearing it will ensure
1279 // that HttpRequestInfo is only used up until final response headers are
1280 // received. Clearing is allowed so that the transaction can be disassociated
1281 // from its creating consumer in cases where it is shared for writing to the
1282 // cache. It is also safe to set it to null at this point since
1283 // upload_data_stream is also not used in the Read state machine.
1284 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1285 request_ = nullptr;
1286
[email protected]0877e3d2009-10-17 22:29:571287 return OK;
initial.commit586acc5fe2008-07-26 22:42:521288}
1289
1290int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501291 DCHECK(read_buf_.get());
Bence Béky707a52242022-06-13 20:46:471292 // TODO(https://crbug.com/1335423): Change to DCHECK_GT() or remove after bug
1293 // is fixed.
1294 CHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351295 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521296
1297 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501298 return stream_->ReadResponseBody(
1299 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521300}
1301
1302int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1303 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381304 bool done = false;
1305 if (result <= 0) {
1306 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521307 done = true;
[email protected]8e6441ca2010-08-19 05:56:381308 }
[email protected]9492e4a2010-02-24 00:58:461309
mmenkebd84c392015-09-02 14:12:341310 // Clean up connection if we are done.
1311 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381312 // Note: Just because IsResponseBodyComplete is true, we're not
1313 // necessarily "done". We're only "done" when it is the last
1314 // read on this HttpNetworkTransaction, which will be signified
1315 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341316 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381317 // the stream. No need to compute it here just to pass back
1318 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341319 bool keep_alive =
1320 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521321
[email protected]8e6441ca2010-08-19 05:56:381322 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011323 // Note: we don't reset the stream here. We've closed it, but we still
1324 // need it around so that callers can call methods such as
1325 // GetUploadProgress() and have them be meaningful.
1326 // TODO(mbelshe): This means we closed the stream here, and we close it
1327 // again in ~HttpNetworkTransaction. Clean that up.
1328
[email protected]8e6441ca2010-08-19 05:56:381329 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171330
1331 // This transaction was successful. If it had been retried because of an
1332 // error with an alternative service, mark that alternative service broken.
1333 if (!enable_alternative_services_ &&
1334 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411335 HistogramBrokenAlternateProtocolLocation(
1336 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171337 session_->http_server_properties()->MarkAlternativeServiceBroken(
Brianna Goldsteinc8ae1392022-10-10 20:09:471338 retried_alternative_service_, network_anonymization_key_);
rch2f2991c2017-04-13 19:28:171339 }
Lily Chenfec60d92019-01-24 01:16:421340
1341#if BUILDFLAG(ENABLE_REPORTING)
1342 GenerateNetworkErrorLoggingReport(result);
1343#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521344 }
1345
1346 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351347 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521348 read_buf_len_ = 0;
1349
1350 return result;
1351}
1352
[email protected]2d2697f92009-02-18 21:00:321353int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1354 // This method differs from DoReadBody only in the next_state_. So we just
1355 // call DoReadBody and override the next_state_. Perhaps there is a more
1356 // elegant way for these two methods to share code.
1357 int rv = DoReadBody();
1358 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1359 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1360 return rv;
1361}
1362
[email protected]0877e3d2009-10-17 22:29:571363// TODO(wtc): This method and the DoReadBodyComplete method are almost
1364// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321365int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231366 // keep_alive defaults to true because the very reason we're draining the
1367 // response body is to reuse the connection for auth restart.
1368 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321369 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571370 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421371 // Note: No Network Error Logging report is generated here because a report
1372 // will have already been generated for the original request due to the auth
1373 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321374 done = true;
[email protected]68873ba2009-06-04 21:49:231375 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311376 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571377 done = true;
[email protected]2d2697f92009-02-18 21:00:321378 }
1379
1380 if (done) {
1381 DidDrainBodyForAuthRestart(keep_alive);
1382 } else {
1383 // Keep draining.
1384 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1385 }
1386
1387 return OK;
1388}
1389
Douglas Creager3cb042052018-11-06 23:08:521390#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141391void HttpNetworkTransaction::ProcessReportToHeader() {
1392 std::string value;
1393 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1394 return;
1395
Matt Menkef437a87e2020-10-22 23:22:241396 ReportingService* reporting_service = session_->reporting_service();
1397 if (!reporting_service)
Douglas Creager134b52e2018-11-09 18:00:141398 return;
Douglas Creager134b52e2018-11-09 18:00:141399
1400 // Only accept Report-To headers on HTTPS connections that have no
1401 // certificate errors.
Lily Chen9364a4c2020-06-25 16:07:521402 if (!response_.ssl_info.is_valid())
Douglas Creager134b52e2018-11-09 18:00:141403 return;
Lily Chen9364a4c2020-06-25 16:07:521404 if (IsCertStatusError(response_.ssl_info.cert_status))
Douglas Creager134b52e2018-11-09 18:00:141405 return;
Douglas Creager134b52e2018-11-09 18:00:141406
Brianna Goldsteinc8ae1392022-10-10 20:09:471407 reporting_service->ProcessReportToHeader(url::Origin::Create(url_),
1408 network_anonymization_key_, value);
Douglas Creager134b52e2018-11-09 18:00:141409}
1410
Douglas Creager3cb042052018-11-06 23:08:521411void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1412 std::string value;
1413 if (!response_.headers->GetNormalizedHeader(
1414 NetworkErrorLoggingService::kHeaderName, &value)) {
1415 return;
1416 }
1417
Matt Menkef437a87e2020-10-22 23:22:241418 NetworkErrorLoggingService* network_error_logging_service =
Douglas Creager3cb042052018-11-06 23:08:521419 session_->network_error_logging_service();
Matt Menkef437a87e2020-10-22 23:22:241420 if (!network_error_logging_service)
Douglas Creager3cb042052018-11-06 23:08:521421 return;
Douglas Creager3cb042052018-11-06 23:08:521422
Lily Chen90ae93cc2019-02-14 01:15:391423 // Don't accept NEL headers received via a proxy, because the IP address of
1424 // the destination server is not known.
1425 if (response_.was_fetched_via_proxy)
1426 return;
1427
Douglas Creager3cb042052018-11-06 23:08:521428 // Only accept NEL headers on HTTPS connections that have no certificate
1429 // errors.
Lily Chena9e9d6cb2020-06-25 17:34:491430 if (!response_.ssl_info.is_valid() ||
1431 IsCertStatusError(response_.ssl_info.cert_status)) {
Douglas Creager3cb042052018-11-06 23:08:521432 return;
1433 }
1434
Lily Chena9e9d6cb2020-06-25 17:34:491435 if (remote_endpoint_.address().empty())
Douglas Creager3cb042052018-11-06 23:08:521436 return;
Douglas Creager3cb042052018-11-06 23:08:521437
Brianna Goldsteinc8ae1392022-10-10 20:09:471438 network_error_logging_service->OnHeader(network_anonymization_key_,
1439 url::Origin::Create(url_),
1440 remote_endpoint_.address(), value);
Douglas Creager3cb042052018-11-06 23:08:521441}
Douglas Creageref5eecdc2018-11-09 20:50:361442
Lily Chenfec60d92019-01-24 01:16:421443void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1444 if (rv < 0 && rv != ERR_IO_PENDING)
1445 GenerateNetworkErrorLoggingReport(rv);
1446}
1447
Douglas Creageref5eecdc2018-11-09 20:50:361448void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421449 // |rv| should be a valid net::Error
1450 DCHECK_NE(rv, ERR_IO_PENDING);
1451 DCHECK_LE(rv, 0);
1452
1453 if (network_error_logging_report_generated_)
1454 return;
1455 network_error_logging_report_generated_ = true;
1456
Douglas Creageref5eecdc2018-11-09 20:50:361457 NetworkErrorLoggingService* service =
1458 session_->network_error_logging_service();
Lily Chend613caa2019-11-25 20:46:331459 if (!service)
Douglas Creageref5eecdc2018-11-09 20:50:361460 return;
Douglas Creageref5eecdc2018-11-09 20:50:361461
Lily Chen90ae93cc2019-02-14 01:15:391462 // Don't report on proxy auth challenges.
1463 if (response_.headers && response_.headers->response_code() ==
1464 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1465 return;
1466 }
1467
1468 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1469 // internal network details.
1470 if (response_.was_fetched_via_proxy)
1471 return;
1472
Lily Chen00196ab62018-12-04 19:52:291473 // Ignore errors from non-HTTPS origins.
Lily Chend613caa2019-11-25 20:46:331474 if (!url_.SchemeIsCryptographic())
Lily Chen00196ab62018-12-04 19:52:291475 return;
Douglas Creageref5eecdc2018-11-09 20:50:361476
1477 NetworkErrorLoggingService::RequestDetails details;
1478
Brianna Goldsteinc8ae1392022-10-10 20:09:471479 details.network_anonymization_key = network_anonymization_key_;
Douglas Creageref5eecdc2018-11-09 20:50:361480 details.uri = url_;
1481 if (!request_referrer_.empty())
1482 details.referrer = GURL(request_referrer_);
1483 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421484 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361485 details.server_ip = remote_endpoint_.address();
Tsuyoshi Horoc6a35422022-04-25 22:44:331486 } else if (!connection_attempts_.empty()) {
1487 // When we failed to connect to the server, `remote_endpoint_` is not set.
1488 // In such case, we use the last endpoint address of `connection_attempts_`
1489 // for the NEL report. This address information is important for the
1490 // downgrade step to protect against port scan attack.
1491 // https://www.w3.org/TR/network-error-logging/#generate-a-network-error-report
1492 details.server_ip = connection_attempts_.back().endpoint.address();
Lily Chenfec60d92019-01-24 01:16:421493 } else {
1494 details.server_ip = IPAddress();
1495 }
Douglas Creageref5eecdc2018-11-09 20:50:361496 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1497 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421498 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361499 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421500 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361501 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421502 }
Douglas Creageref5eecdc2018-11-09 20:50:361503 // If we got response headers, assume that the connection used HTTP/1.1
1504 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421505 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361506 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421507 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361508 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361509 }
Lily Chenfec60d92019-01-24 01:16:421510 details.method = request_method_;
1511 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361512 details.type = static_cast<Error>(rv);
1513 details.reporting_upload_depth = request_reporting_upload_depth_;
1514
1515 service->OnRequest(std::move(details));
1516}
Lily Chenfec60d92019-01-24 01:16:421517#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521518
bncfacdd852015-01-09 19:22:541519int HttpNetworkTransaction::HandleHttp11Required(int error) {
1520 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1521 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1522
Matt Menke0e3276862019-08-23 00:21:341523 // HttpServerProperties should have been updated, so when the request is sent
1524 // again, it will automatically use HTTP/1.1.
bncfacdd852015-01-09 19:22:541525 ResetConnectionAndRequestForResend();
1526 return OK;
1527}
1528
David Benjamin76a40ad2018-02-24 22:22:081529int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjaminbac8dff2019-08-07 01:30:411530 // Client certificate errors may come from either the origin server or the
1531 // proxy.
1532 //
1533 // Origin errors are handled here, while most proxy errors are handled in the
David Benjamin587def42021-03-30 15:38:441534 // HttpStreamFactory and below, while handshaking with the proxy. However, in
1535 // TLS 1.2 with False Start, or TLS 1.3, client certificate errors are
1536 // reported immediately after the handshake. The error will then surface out
1537 // of the first Read() rather than Connect().
1538 //
1539 // If the request is tunneled (i.e. the origin is HTTPS), this first Read()
1540 // occurs while establishing the tunnel and HttpStreamFactory handles the
1541 // proxy error. However, if the request is not tunneled (i.e. the origin is
1542 // HTTP), this first Read() happens late and is ultimately surfaced out of
1543 // DoReadHeadersComplete(). This method will then be responsible for both
1544 // origin and proxy errors.
David Benjamin3b94b0f2019-04-25 23:07:521545 //
1546 // See https://crbug.com/828965.
David Benjaminbac8dff2019-08-07 01:30:411547 bool is_server = !UsingHttpProxyWithoutTunnel();
1548 HostPortPair host_port_pair =
1549 is_server ? HostPortPair::FromURL(request_->url)
1550 : proxy_info_.proxy_server().host_port_pair();
David Benjamin3b94b0f2019-04-25 23:07:521551
David Benjaminbac8dff2019-08-07 01:30:411552 if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
Eric Romandbf7441f2020-07-28 07:05:021553 DCHECK((is_server && IsSecureRequest()) ||
1554 proxy_info_.is_secure_http_like());
David Benjaminbac8dff2019-08-07 01:30:411555 if (session_->ssl_client_context()->ClearClientCertificate(
1556 host_port_pair)) {
1557 // The private key handle may have gone stale due to, e.g., the user
1558 // unplugging their smartcard. Operating systems do not provide reliable
1559 // notifications for this, so if the signature failed and the user was
1560 // not already prompted for certificate on this request, retry to ask
1561 // the user for a new one.
1562 //
1563 // TODO(davidben): There is no corresponding feature for proxy client
1564 // certificates. Ideally this would live at a lower level, common to both,
1565 // but |configured_client_cert_for_server_| is not accessible below the
1566 // socket pools.
1567 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1568 !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
1569 retry_attempts_++;
1570 net_log_.AddEventWithNetErrorCode(
1571 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1572 ResetConnectionAndRequestForResend();
1573 return OK;
1574 }
David Benjamin76a40ad2018-02-24 22:22:081575 }
1576 }
initial.commit586acc5fe2008-07-26 22:42:521577 return error;
1578}
1579
[email protected]bd0b6772011-01-11 19:59:301580// This method determines whether it is safe to resend the request after an
Titouan Rigoudy5cf08f12022-05-20 15:49:221581// IO error. It should only be called in response to errors received before
1582// final set of response headers have been successfully parsed, that the
1583// transaction may need to be retried on.
1584// It should not be used in other cases, such as a Connect error.
[email protected]bd0b6772011-01-11 19:59:301585int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031586 // Because the peer may request renegotiation with client authentication at
1587 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081588 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301589
Lily Chenfec60d92019-01-24 01:16:421590#if BUILDFLAG(ENABLE_REPORTING)
1591 GenerateNetworkErrorLoggingReportIfError(error);
1592#endif // BUILDFLAG(ENABLE_REPORTING)
1593
[email protected]bd0b6772011-01-11 19:59:301594 switch (error) {
1595 // If we try to reuse a connection that the server is in the process of
1596 // closing, we may end up successfully writing out our request (or a
1597 // portion of our request) only to find a connection error when we try to
1598 // read from (or finish writing to) the socket.
1599 case ERR_CONNECTION_RESET:
1600 case ERR_CONNECTION_CLOSED:
1601 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511602 // There can be a race between the socket pool checking checking whether a
1603 // socket is still connected, receiving the FIN, and sending/reading data
1604 // on a reused socket. If we receive the FIN between the connectedness
1605 // check and writing/reading from the socket, we may first learn the socket
1606 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1607 // likely happen when trying to retrieve its IP address.
1608 // See http://crbug.com/105824 for more details.
1609 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491610 // If a socket is closed on its initial request, HttpStreamParser returns
1611 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1612 // preconnected but failed to be used before the server timed it out.
1613 case ERR_EMPTY_RESPONSE:
1614 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381615 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001616 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301617 ResetConnectionAndRequestForResend();
1618 error = OK;
1619 }
1620 break;
Steven Valdez58097ec32018-07-16 18:29:041621 case ERR_EARLY_DATA_REJECTED:
1622 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1623 net_log_.AddEventWithNetErrorCode(
1624 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1625 // Disable early data on the SSLConfig on a reset.
1626 can_send_early_data_ = false;
1627 ResetConnectionAndRequestForResend();
1628 error = OK;
1629 break;
Bence Békyd0d69502019-06-25 19:47:181630 case ERR_HTTP2_PING_FAILED:
1631 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1632 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1633 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1634 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271635 case ERR_QUIC_HANDSHAKE_FAILED:
Bence Béky2ee18922020-09-25 12:11:321636 case ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED:
Biljith Jayan45a41722017-08-16 18:43:141637 if (HasExceededMaxRetries())
1638 break;
[email protected]b6754252012-06-13 23:14:381639 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001640 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141641 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001642 ResetConnectionAndRequestForResend();
1643 error = OK;
1644 break;
rch2f2991c2017-04-13 19:28:171645 case ERR_QUIC_PROTOCOL_ERROR:
1646 if (GetResponseHeaders() != nullptr ||
1647 !stream_->GetAlternativeService(&retried_alternative_service_)) {
momoka0122yc80b0f62022-08-19 02:51:011648 // If the response headers have already been received and passed up
rch2f2991c2017-04-13 19:28:171649 // then the request can not be retried. Also, if there was no
1650 // alternative service used for this request, then there is no
1651 // alternative service to be disabled.
1652 break;
1653 }
Biljith Jayan45a41722017-08-16 18:43:141654 if (HasExceededMaxRetries())
1655 break;
rch2f2991c2017-04-13 19:28:171656 if (session_->http_server_properties()->IsAlternativeServiceBroken(
Brianna Goldsteinc8ae1392022-10-10 20:09:471657 retried_alternative_service_, network_anonymization_key_)) {
rch2f2991c2017-04-13 19:28:171658 // If the alternative service was marked as broken while the request
1659 // was in flight, retry the request which will not use the broken
1660 // alternative service.
1661 net_log_.AddEventWithNetErrorCode(
1662 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141663 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171664 ResetConnectionAndRequestForResend();
1665 error = OK;
Victor Vasilieva1e66d72019-12-05 17:55:381666 } else if (session_->context()
1667 .quic_context->params()
1668 ->retry_without_alt_svc_on_quic_errors) {
rch2f2991c2017-04-13 19:28:171669 // Disable alternative services for this request and retry it. If the
1670 // retry succeeds, then the alternative service will be marked as
1671 // broken then.
1672 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361673 net_log_.AddEventWithNetErrorCode(
1674 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141675 retry_attempts_++;
rch514a44a82017-04-13 04:46:361676 ResetConnectionAndRequestForResend();
1677 error = OK;
1678 }
1679 break;
[email protected]bd0b6772011-01-11 19:59:301680 }
1681 return error;
1682}
1683
[email protected]c3b35c22008-09-27 03:19:421684void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581685 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221686 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191687 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221688 total_sent_bytes_ += stream_->GetTotalSentBytes();
1689 }
zhongyica364fbb2015-12-12 03:39:121690 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581691}
1692
1693void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251694 send_start_time_ = base::TimeTicks();
1695 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251696
[email protected]0757e7702009-03-27 04:00:221697 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351698 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421699 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571700 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201701 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141702 response_ = HttpResponseInfo();
Wojciech Dzierżanowski758b4fb2021-04-19 23:26:591703 SetProxyInfoInReponse(proxy_info_, &response_);
[email protected]8e6441ca2010-08-19 05:56:381704 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591705 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121706 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521707 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421708#if BUILDFLAG(ENABLE_REPORTING)
1709 network_error_logging_report_generated_ = false;
1710 start_timeticks_ = base::TimeTicks::Now();
1711#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121712}
1713
1714void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1715 if (stream_)
1716 stream_->PopulateNetErrorDetails(&net_error_details_);
1717 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571718}
1719
1720HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501721 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421722}
1723
[email protected]a34f61ee2014-03-18 20:59:491724bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381725 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351726 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161727
[email protected]2a5c76b2008-09-25 22:15:161728 // NOTE: we resend a request only if we reused a keep-alive connection.
1729 // This automatically prevents an infinite resend loop because we'll run
1730 // out of the cached keep-alive connections eventually.
Titouan Rigoudy5cf08f12022-05-20 15:49:221731 return connection_is_proven && !has_received_headers;
[email protected]1c773ea12009-04-28 19:58:421732}
1733
Biljith Jayan45a41722017-08-16 18:43:141734bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1735 return (retry_attempts_ >= kMaxRetryAttempts);
1736}
1737
David Benjamin83ddfb32018-03-30 01:07:521738bool HttpNetworkTransaction::CheckMaxRestarts() {
1739 num_restarts_++;
1740 return num_restarts_ < kMaxRestarts;
1741}
1742
[email protected]1c773ea12009-04-28 19:58:421743void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381744 if (stream_.get()) {
1745 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121746 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161747 }
1748
[email protected]0877e3d2009-10-17 22:29:571749 // We need to clear request_headers_ because it contains the real request
1750 // headers, but we may need to resend the CONNECT request first to recreate
1751 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201752 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501753 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421754
1755#if BUILDFLAG(ENABLE_REPORTING)
1756 // Reset for new request.
1757 network_error_logging_report_generated_ = false;
1758 start_timeticks_ = base::TimeTicks::Now();
1759#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221760
1761 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541762}
1763
[email protected]1c773ea12009-04-28 19:58:421764bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241765 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421766}
license.botbf09a502008-08-24 00:55:551767
[email protected]1c773ea12009-04-28 19:58:421768bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
Matt Menke25eaa432020-08-25 00:10:001769 return request_->privacy_mode == PRIVACY_MODE_DISABLED;
[email protected]1c773ea12009-04-28 19:58:421770}
1771
[email protected]e772db3f2010-07-12 18:11:131772int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271773 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501774 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421775
[email protected]0877e3d2009-10-17 22:29:571776 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581777 if (status != HTTP_UNAUTHORIZED &&
1778 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421779 return OK;
[email protected]9094b602012-02-27 21:44:581780 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111781 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161782 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1783 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421784
[email protected]9094b602012-02-27 21:44:581785 // This case can trigger when an HTTPS server responds with a "Proxy
1786 // authentication required" status code through a non-authenticating
1787 // proxy.
[email protected]7a67a8152010-11-05 18:31:101788 if (!auth_controllers_[target].get())
1789 return ERR_UNEXPECTED_PROXY_AUTH;
1790
[email protected]a7ea8832010-07-12 17:54:541791 int rv = auth_controllers_[target]->HandleAuthChallenge(
Matt Menke25eaa432020-08-25 00:10:001792 headers, response_.ssl_info, !ShouldApplyServerAuth(), false, net_log_);
[email protected]228404f2010-06-24 04:31:411793 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491794 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411795
Emily Starkf2c9bbd2019-04-09 17:08:581796 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411797
[email protected]228404f2010-06-24 04:31:411798 return rv;
[email protected]f9ee6b52008-11-08 06:46:231799}
1800
[email protected]8e6441ca2010-08-19 05:56:381801bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1802 return auth_controllers_[target].get() &&
1803 auth_controllers_[target]->HaveAuth();
1804}
1805
[email protected]228404f2010-06-24 04:31:411806GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1807 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461808 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411809 if (!proxy_info_.proxy_server().is_valid() ||
1810 proxy_info_.proxy_server().is_direct()) {
1811 return GURL(); // There is no proxy server.
1812 }
Eric Romandbf7441f2020-07-28 07:05:021813 // TODO(https://crbug.com/1103768): Mapping proxy addresses to
1814 // URLs is a lossy conversion, shouldn't do this.
1815 const char* scheme =
1816 proxy_info_.is_secure_http_like() ? "https://" : "http://";
[email protected]2df19bb2010-08-25 20:13:461817 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351818 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461819 }
[email protected]228404f2010-06-24 04:31:411820 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291821 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361822 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291823 }
[email protected]228404f2010-06-24 04:31:411824 return request_->url;
1825 default:
1826 return GURL();
1827 }
[email protected]c3b35c22008-09-27 03:19:421828}
1829
[email protected]831e4a32013-11-14 02:14:441830bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141831 return websocket_handshake_stream_base_create_helper_ &&
1832 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441833}
1834
ttuttle1f2d7e92015-04-28 16:17:471835void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1836 DCHECK(stream_request_);
1837
1838 // Since the transaction can restart with auth credentials, it may create a
1839 // stream more than once. Accumulate all of the connection attempts across
1840 // those streams by appending them to the vector:
1841 for (const auto& attempt : stream_request_->connection_attempts())
1842 connection_attempts_.push_back(attempt);
1843}
1844
eustasc7d27da2017-04-06 10:33:201845bool HttpNetworkTransaction::ContentEncodingsValid() const {
1846 HttpResponseHeaders* headers = GetResponseHeaders();
1847 DCHECK(headers);
1848
1849 std::string accept_encoding;
1850 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1851 &accept_encoding);
1852 std::set<std::string> allowed_encodings;
Paul Jensene85e8152019-07-01 15:38:001853 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
eustasc7d27da2017-04-06 10:33:201854 return false;
eustasc7d27da2017-04-06 10:33:201855
1856 std::string content_encoding;
1857 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1858 std::set<std::string> used_encodings;
Paul Jensene85e8152019-07-01 15:38:001859 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
eustasc7d27da2017-04-06 10:33:201860 return false;
eustasc7d27da2017-04-06 10:33:201861
1862 // When "Accept-Encoding" is not specified, it is parsed as "*".
1863 // If "*" encoding is advertised, then any encoding should be "accepted".
1864 // This does not mean, that it will be successfully decoded.
1865 if (allowed_encodings.find("*") != allowed_encodings.end())
1866 return true;
1867
sky50576f32017-05-01 19:28:031868 bool result = true;
eustasc7d27da2017-04-06 10:33:201869 for (auto const& encoding : used_encodings) {
1870 SourceStream::SourceType source_type =
1871 FilterSourceStream::ParseEncodingType(encoding);
1872 // We don't reject encodings we are not aware. They just will not decode.
1873 if (source_type == SourceStream::TYPE_UNKNOWN)
1874 continue;
1875 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031876 result = false;
1877 break;
eustasc7d27da2017-04-06 10:33:201878 }
1879 }
sky50576f32017-05-01 19:28:031880
1881 // Temporary workaround for http://crbug.com/714514
1882 if (headers->IsRedirect(nullptr)) {
1883 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1884 !result);
1885 return true;
1886 }
1887
1888 return result;
eustasc7d27da2017-04-06 10:33:201889}
1890
[email protected]c3b35c22008-09-27 03:19:421891} // namespace net