blob: 07e461ac960d5567fee9d0021b4caaa9fc4099cc [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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]49639fa2011-12-20 23:22:4112#include "base/bind.h"
13#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3014#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4715#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3816#include "base/metrics/field_trial.h"
David Benjamin5cb91132018-04-06 05:54:4917#include "base/metrics/histogram_functions.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.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"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4032#include "net/base/proxy_server.h"
Titouan Rigoudyba507a882020-07-31 12:15:1533#include "net/base/transport_info.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.h"
Bence Béky230ac612017-08-30 19:17:0836#include "net/cert/cert_status_flags.h"
eustasc7d27da2017-04-06 10:33:2037#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0038#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4239#include "net/http/http_auth.h"
40#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2441#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3642#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_chunked_decoder.h"
Eric Roman06bd9742019-07-13 15:19:1344#include "net/http/http_log_util.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5446#include "net/http/http_proxy_client_socket.h"
[email protected]270c6412010-03-29 22:02:4747#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5248#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2149#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5750#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5351#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5852#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3953#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3154#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5255#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2356#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3657#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0058#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4459#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2160#include "net/socket/next_proto.h"
[email protected]ab739042011-04-07 15:22:2861#include "net/socket/transport_client_socket_pool.h"
Bence Béky94658bf2018-05-11 19:22:5862#include "net/spdy/spdy_http_stream.h"
63#include "net/spdy/spdy_session.h"
64#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5765#include "net/ssl/ssl_cert_request_info.h"
66#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0867#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5468#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5469#include "url/gurl.h"
Matt Menkef2ee07c2019-08-29 02:10:3670#include "url/scheme_host_port.h"
[email protected]e69c1cd2014-07-29 07:42:2971#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5272
Douglas Creager3cb042052018-11-06 23:08:5273#if BUILDFLAG(ENABLE_REPORTING)
74#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1475#include "net/reporting/reporting_header_parser.h"
76#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4277#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5278
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2279namespace net {
80
Biljith Jayan45a41722017-08-16 18:43:1481namespace {
David Benjamin83ddfb32018-03-30 01:07:5282
Biljith Jayan45a41722017-08-16 18:43:1483// Max number of |retry_attempts| (excluding the initial request) after which
84// we give up and show an error page.
85const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5286
87// Max number of calls to RestartWith* allowed for a single connection. A single
88// HttpNetworkTransaction should not signal very many restartable errors, but it
89// may occur due to a bug (e.g. https://crbug.com/823387 or
90// https://crbug.com/488043) or simply if the server or proxy requests
91// authentication repeatedly. Although these calls are often associated with a
92// user prompt, in other scenarios (remembered preferences, extensions,
93// multi-leg authentication), they may be triggered automatically. To avoid
94// looping forever, bound the number of restarts.
95const size_t kMaxRestarts = 32;
96
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2297void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
98 HttpResponseInfo* response_info) {
99 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
100 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
101 response_info->proxy_server = proxy_info.proxy_server();
102 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
103 response_info->proxy_server = ProxyServer::Direct();
104 else
105 response_info->proxy_server = ProxyServer();
106}
Biljith Jayan45a41722017-08-16 18:43:14107
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22108} // namespace
initial.commit586acc5fe2008-07-26 22:42:52109
Victor Costan9c7302b2018-08-27 16:39:44110const int HttpNetworkTransaction::kDrainBodyBufferSize;
111
[email protected]262eec82013-03-19 21:01:36112HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
113 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22114 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53115 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
116 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52117 session_(session),
Raul Tambre94493c652019-03-11 17:18:35118 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36119 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57120 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27121 can_send_early_data_(false),
David Benjaminbac8dff2019-08-07 01:30:41122 configured_client_cert_for_server_(false),
[email protected]b94f92d2010-10-27 16:45:20123 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42124#if BUILDFLAG(ENABLE_REPORTING)
125 network_error_logging_report_generated_(false),
126 request_reporting_upload_depth_(0),
127#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52128 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19129 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22130 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54131 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44132 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29133 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26134 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35135 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14136 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52137 retry_attempts_(0),
David Benjamind61bd532019-04-23 21:11:37138 num_restarts_(0) {
Lily Chenfec60d92019-01-24 01:16:42139}
[email protected]3ce7df0f2010-03-03 00:30:50140
[email protected]0b0bf032010-09-21 18:08:50141HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42142#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11143 // If no error or success report has been generated yet at this point, then
144 // this network transaction was prematurely cancelled.
145 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42146#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50147 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50148 // TODO(mbelshe): The stream_ should be able to compute whether or not the
149 // stream should be kept alive. No reason to compute here
150 // and pass it in.
mmenkebd84c392015-09-02 14:12:34151 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50152 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34153 } else if (stream_->IsResponseBodyComplete()) {
154 // If the response body is complete, we can just reuse the socket.
155 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50156 } else {
mmenkebd84c392015-09-02 14:12:34157 // Otherwise, we try to drain the response body.
158 HttpStream* stream = stream_.release();
159 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50160 }
161 }
[email protected]02cad5d2013-10-02 08:14:03162 if (request_ && request_->upload_data_stream)
163 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50164}
165
[email protected]684970b2009-08-14 04:54:46166int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04167 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20168 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23169 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
170 return ERR_CACHE_MISS;
171
Ramin Halavatib5e433e2018-02-07 07:41:10172 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53173 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04174 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15175 url_ = request_->url;
Matt Menkeb32ba5122019-09-10 19:17:05176 network_isolation_key_ = request_->network_isolation_key;
Douglas Creageref5eecdc2018-11-09 20:50:36177#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42178 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36179 request_method_ = request_->method;
180 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
181 &request_referrer_);
182 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
183 &request_user_agent_);
184 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42185 start_timeticks_ = base::TimeTicks::Now();
186#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04187
David Benjamin8119ce9e42019-08-06 00:05:06188 session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_);
nharper8cdb0fb2016-04-22 21:34:59189
Ryan Sleevi24fe2682018-08-16 21:33:46190 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
191 server_ssl_config_.disable_cert_verification_network_fetches = true;
192 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19193 }
[email protected]6fbac162011-06-20 00:29:04194
Yu Su2d738bb2020-10-30 02:23:55195 if (request_->idempotency == IDEMPOTENT ||
196 (request_->idempotency == DEFAULT_IDEMPOTENCY &&
197 HttpUtil::IsMethodSafe(request_info->method))) {
Steven Valdezb4ff0412018-01-18 22:39:27198 can_send_early_data_ = true;
199 }
200
Dominic Farolino4ecaf0a2019-08-16 06:40:09201 if (request_->load_flags & LOAD_PREFETCH) {
jkarlinfb1d5172015-01-12 14:10:29202 response_.unused_since_prefetch = true;
Dominic Farolino4ecaf0a2019-08-16 06:40:09203 }
204
205 if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
206 DCHECK(response_.unused_since_prefetch);
207 response_.restricted_prefetch = true;
208 }
jkarlinfb1d5172015-01-12 14:10:29209
Helen Lib495c3802018-03-30 13:46:09210 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04211 int rv = DoLoop(OK);
212 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04213 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42214
215 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
216 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
217 // other net::Error can be returned.
218 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04219 return rv;
220}
221
222int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04223 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38224 DCHECK(!stream_.get());
225 DCHECK(!stream_request_.get());
226 DCHECK_EQ(STATE_NONE, next_state_);
227
David Benjamin83ddfb32018-03-30 01:07:52228 if (!CheckMaxRestarts())
229 return ERR_TOO_MANY_RETRIES;
230
[email protected]82918cc2010-08-25 17:24:50231 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38232
[email protected]ccb40e52008-09-17 20:54:40233 int rv = DoLoop(OK);
234 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04235 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42236
237 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
238 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
239 // other net::Error can be returned.
240 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11241 return rv;
[email protected]96d570e42008-08-05 22:43:04242}
243
[email protected]0b45559b2009-06-12 21:45:11244int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08245 scoped_refptr<X509Certificate> client_cert,
246 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04247 CompletionOnceCallback callback) {
David Benjaminbac8dff2019-08-07 01:30:41248 // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
249 // existing streams and stream requests to force a new connection.
[email protected]8e6441ca2010-08-19 05:56:38250 DCHECK(!stream_request_.get());
251 DCHECK(!stream_.get());
252 DCHECK_EQ(STATE_NONE, next_state_);
253
David Benjamin83ddfb32018-03-30 01:07:52254 if (!CheckMaxRestarts())
255 return ERR_TOO_MANY_RETRIES;
256
David Benjaminbac8dff2019-08-07 01:30:41257 // Add the credentials to the client auth cache. The next stream request will
258 // then pick them up.
259 session_->ssl_client_context()->SetClientCertificate(
mattm436ccfe2017-06-19 20:24:08260 response_.cert_request_info->host_and_port, std::move(client_cert),
261 std::move(client_private_key));
David Benjaminbac8dff2019-08-07 01:30:41262
263 if (!response_.cert_request_info->is_proxy)
264 configured_client_cert_for_server_ = true;
265
[email protected]0b45559b2009-06-12 21:45:11266 // Reset the other member variables.
267 // Note: this is necessary only with SSL renegotiation.
268 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50269 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11270 int rv = DoLoop(OK);
271 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04272 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42273
274 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
275 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
276 // other net::Error can be returned.
277 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11278 return rv;
279}
280
Bence Béky046f8c82018-06-12 02:26:04281int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
282 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52283 if (!CheckMaxRestarts())
284 return ERR_TOO_MANY_RETRIES;
285
[email protected]0757e7702009-03-27 04:00:22286 HttpAuth::Target target = pending_auth_target_;
287 if (target == HttpAuth::AUTH_NONE) {
288 NOTREACHED();
289 return ERR_UNEXPECTED;
290 }
[email protected]0757e7702009-03-27 04:00:22291 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42292
[email protected]f3cf9802011-10-28 18:44:58293 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13294
[email protected]49639fa2011-12-20 23:22:41295 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38296
297 int rv = OK;
298 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
299 // In this case, we've gathered credentials for use with proxy
300 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50301 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35302 DCHECK(stream_request_ != nullptr);
303 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54304 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39305 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54306 } else {
[email protected]8e6441ca2010-08-19 05:56:38307 // In this case, we've gathered credentials for the server or the proxy
308 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35309 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54310 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38311 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42312 // Note: If an error is encountered while draining the old response body, no
313 // Network Error Logging report will be generated, because the error was
314 // with the old request, which will already have had a NEL report generated
315 // for it due to the auth challenge (so we don't report a second error for
316 // that request).
[email protected]a7ea8832010-07-12 17:54:54317 }
[email protected]c3b35c22008-09-27 03:19:42318
[email protected]c3b35c22008-09-27 03:19:42319 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04320 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42321 return rv;
[email protected]96d570e42008-08-05 22:43:04322}
323
[email protected]f9ee6b52008-11-08 06:46:23324void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
325 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38326 DCHECK(!stream_request_.get());
327
Bence Béky3238f2e12017-09-22 22:44:49328 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
329 if (target == HttpAuth::AUTH_SERVER &&
330 auth_controllers_[target]->NeedsHTTP11()) {
331 session_->http_server_properties()->SetHTTP11Required(
Matt Menked9b24f02019-09-26 17:07:17332 url::SchemeHostPort(request_->url), network_isolation_key_);
Bence Béky3238f2e12017-09-22 22:44:49333 }
334
[email protected]2d2697f92009-02-18 21:00:32335 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57336 // Even if the server says the connection is keep-alive, we have to be
337 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34338 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57339 // If the response body hasn't been completely read, we need to drain
340 // it first.
[email protected]351ab642010-08-05 16:55:31341 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32342 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44343 read_buf_ = base::MakeRefCounted<IOBuffer>(
344 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32345 read_buf_len_ = kDrainBodyBufferSize;
346 return;
347 }
[email protected]0877e3d2009-10-17 22:29:57348 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09349 }
350
[email protected]2d2697f92009-02-18 21:00:32351 // We don't need to drain the response body, so we act as if we had drained
352 // the response body.
353 DidDrainBodyForAuthRestart(keep_alive);
354}
355
356void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38357 DCHECK(!stream_request_.get());
358
359 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19360 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22361 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35362 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34363 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38364 // We should call connection_->set_idle_time(), but this doesn't occur
365 // often enough to be worth the trouble.
366 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39367 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38368 }
[email protected]697ef4c2010-10-14 16:38:58369
370 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46371 // Close the stream and mark it as not_reusable. Even in the
372 // keep_alive case, we've determined that the stream_ is not
373 // reusable if new_stream is NULL.
374 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58375 next_state_ = STATE_CREATE_STREAM;
376 } else {
sclittlefb249892015-09-10 21:33:22377 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19378 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22379 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58380 next_state_ = STATE_INIT_STREAM;
381 }
382 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32383 }
[email protected]f9ee6b52008-11-08 06:46:23384
385 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58386 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23387}
388
[email protected]8e6441ca2010-08-19 05:56:38389bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
390 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
391 HaveAuth(pending_auth_target_);
392}
393
Bence Béky046f8c82018-06-12 02:26:04394int HttpNetworkTransaction::Read(IOBuffer* buf,
395 int buf_len,
396 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04397 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35398 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04399
[email protected]ad8e04a2010-11-01 04:16:27400 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38401 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04402 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29403 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04404 // bytes when establishing a tunnel because they might be controlled by
405 // an active network attacker. We don't worry about this for HTTP
406 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29407 // We reach this case when the user cancels a 407 proxy auth prompt. We
408 // also don't worry about this for an HTTPS Proxy, because the
409 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04410 // See http://crbug.com/8473.
Eric Romandbf7441f2020-07-28 07:05:02411 DCHECK(proxy_info_.is_http_like());
[email protected]9094b602012-02-27 21:44:58412 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]8a1f3312010-05-25 19:25:04413 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44414 }
415
[email protected]e60e47a2010-07-14 03:37:18416 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15417 next_state_ = STATE_READ_BODY;
418
[email protected]96d570e42008-08-05 22:43:04419 read_buf_ = buf;
420 read_buf_len_ = buf_len;
421
[email protected]96d570e42008-08-05 22:43:04422 int rv = DoLoop(OK);
423 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04424 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04425 return rv;
426}
427
[email protected]8cd06c02014-01-25 07:50:14428void HttpNetworkTransaction::StopCaching() {}
429
sclittle4de1bab92015-09-22 21:28:24430int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
431 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19432 if (stream_)
433 total_received_bytes += stream_->GetTotalReceivedBytes();
434 return total_received_bytes;
435}
436
sclittlefb249892015-09-10 21:33:22437int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
438 int64_t total_sent_bytes = total_sent_bytes_;
439 if (stream_)
440 total_sent_bytes += stream_->GetTotalSentBytes();
441 return total_sent_bytes;
442}
443
[email protected]8cd06c02014-01-25 07:50:14444void HttpNetworkTransaction::DoneReading() {}
445
[email protected]96d570e42008-08-05 22:43:04446const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55447 return &response_;
[email protected]96d570e42008-08-05 22:43:04448}
449
450LoadState HttpNetworkTransaction::GetLoadState() const {
451 // TODO(wtc): Define a new LoadState value for the
452 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
453 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48454 case STATE_CREATE_STREAM:
455 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50456 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38457 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15458 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
459 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57460 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04461 return LOAD_STATE_SENDING_REQUEST;
462 case STATE_READ_HEADERS_COMPLETE:
463 return LOAD_STATE_WAITING_FOR_RESPONSE;
464 case STATE_READ_BODY_COMPLETE:
465 return LOAD_STATE_READING_RESPONSE;
466 default:
467 return LOAD_STATE_IDLE;
468 }
469}
470
[email protected]8cd06c02014-01-25 07:50:14471void HttpNetworkTransaction::SetQuicServerInfo(
472 QuicServerInfo* quic_server_info) {}
473
[email protected]5033ab82013-03-22 20:17:46474bool HttpNetworkTransaction::GetLoadTimingInfo(
475 LoadTimingInfo* load_timing_info) const {
476 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
477 return false;
478
479 load_timing_info->proxy_resolve_start =
480 proxy_info_.proxy_resolve_start_time();
481 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
482 load_timing_info->send_start = send_start_time_;
483 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46484 return true;
485}
486
ttuttled9dbc652015-09-29 20:00:59487bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36488 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59489 return false;
490
491 *endpoint = remote_endpoint_;
492 return true;
493}
494
zhongyi48704c182015-12-07 07:52:02495void HttpNetworkTransaction::PopulateNetErrorDetails(
496 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12497 *details = net_error_details_;
498 if (stream_)
499 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02500}
501
[email protected]5033ab82013-03-22 20:17:46502void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
503 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50504
[email protected]bf828982013-08-14 18:01:47505 if (stream_request_)
506 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03507 if (stream_)
508 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50509
rdsmith1d343be52016-10-21 20:37:50510 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46511}
512
[email protected]831e4a32013-11-14 02:14:44513void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
514 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
515 websocket_handshake_stream_base_create_helper_ = create_helper;
516}
517
[email protected]1826a402014-01-08 15:40:48518void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
Will Cassella61125f92020-07-25 09:11:37519 BeforeNetworkStartCallback callback) {
520 before_network_start_callback_ = std::move(callback);
[email protected]1826a402014-01-08 15:40:48521}
522
Titouan Rigoudy78af7da2020-07-07 14:30:12523void HttpNetworkTransaction::SetConnectedCallback(
524 const ConnectedCallback& callback) {
525 connected_callback_ = callback;
526}
527
Andrey Kosyakov83a6eee2017-08-14 19:20:04528void HttpNetworkTransaction::SetRequestHeadersCallback(
529 RequestHeadersCallback callback) {
530 DCHECK(!stream_);
531 request_headers_callback_ = std::move(callback);
532}
533
Andrey Kosyakov2e893e62017-08-31 17:00:52534void HttpNetworkTransaction::SetResponseHeadersCallback(
535 ResponseHeadersCallback callback) {
536 DCHECK(!stream_);
537 response_headers_callback_ = std::move(callback);
538}
539
[email protected]1826a402014-01-08 15:40:48540int HttpNetworkTransaction::ResumeNetworkStart() {
541 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
542 return DoLoop(OK);
543}
544
[email protected]102e27c2011-02-23 01:01:31545void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
546 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00547 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50548 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38549 DCHECK(stream_request_.get());
550
sclittlefb249892015-09-10 21:33:22551 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19552 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22553 total_sent_bytes_ += stream_->GetTotalSentBytes();
554 }
bnc5029f4632017-06-08 16:19:00555 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04556 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14557 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31558 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52559 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21560 response_.alpn_negotiated_protocol =
561 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38562 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22563 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38564 OnIOComplete(OK);
565}
566
xunjieli5749218c2016-03-22 16:43:06567void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08568 const SSLConfig& used_ssl_config,
569 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00570 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08571 NOTREACHED();
572}
573
[email protected]a9cf2b92013-10-30 12:08:49574void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50575 const SSLConfig& used_ssl_config,
576 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00577 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
578 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50579}
580
Ryan Hamilton75f197262017-08-17 14:00:07581void HttpNetworkTransaction::OnStreamFailed(
582 int result,
583 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22584 const SSLConfig& used_ssl_config,
dalyk6d7a8c52019-12-18 21:43:01585 const ProxyInfo& used_proxy_info,
586 ResolveErrorInfo resolve_error_info) {
[email protected]82918cc2010-08-25 17:24:50587 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38588 DCHECK_NE(OK, result);
589 DCHECK(stream_request_.get());
590 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14591 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07592 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22593 proxy_info_ = used_proxy_info;
594 SetProxyInfoInReponse(used_proxy_info, &response_);
dalyk6d7a8c52019-12-18 21:43:01595 response_.resolve_error_info = resolve_error_info;
[email protected]8e6441ca2010-08-19 05:56:38596
597 OnIOComplete(result);
598}
599
[email protected]102e27c2011-02-23 01:01:31600void HttpNetworkTransaction::OnCertificateError(
601 int result,
602 const SSLConfig& used_ssl_config,
603 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50604 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38605 DCHECK_NE(OK, result);
606 DCHECK(stream_request_.get());
607 DCHECK(!stream_.get());
608
609 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14610 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38611
612 // TODO(mbelshe): For now, we're going to pass the error through, and that
613 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50614 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
615 // good and the user chooses to ignore the error. This is not ideal, but not
616 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38617
618 OnIOComplete(result);
619}
620
621void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50622 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31623 const SSLConfig& used_ssl_config,
624 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50625 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38626 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50627 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38628
629 establishing_tunnel_ = true;
630 response_.headers = proxy_response.headers;
631 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50632 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20633
634 if (response_.headers.get() && !ContentEncodingsValid()) {
635 DoCallback(ERR_CONTENT_DECODING_FAILED);
636 return;
637 }
638
[email protected]8e6441ca2010-08-19 05:56:38639 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14640 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31641 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38642
643 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
644 pending_auth_target_ = HttpAuth::AUTH_PROXY;
645
646 DoCallback(OK);
647}
648
649void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31650 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50651 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50652 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38653
[email protected]102957f2011-09-02 17:10:14654 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38655 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58656 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38657}
658
zhongyi48704c182015-12-07 07:52:02659void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12660 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02661}
662
ttuttle1f2d7e92015-04-28 16:17:47663void HttpNetworkTransaction::GetConnectionAttempts(
664 ConnectionAttempts* out) const {
665 *out = connection_attempts_;
666}
667
Adam Ricecb76ac62015-02-20 05:33:25668bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37669 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52670}
671
Adam Rice425cf122015-01-19 06:18:24672bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
Eric Romandbf7441f2020-07-28 07:05:02673 return proxy_info_.is_http_like() &&
Adam Rice425cf122015-01-19 06:18:24674 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
675}
676
initial.commit586acc5fe2008-07-26 22:42:52677void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50678 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41679 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52680
Douglas Creageref5eecdc2018-11-09 20:50:36681#if BUILDFLAG(ENABLE_REPORTING)
682 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42683 // report about this network request if the result was an error.
684 GenerateNetworkErrorLoggingReportIfError(rv);
685#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36686
[email protected]96d570e42008-08-05 22:43:04687 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27688 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52689}
690
691void HttpNetworkTransaction::OnIOComplete(int result) {
692 int rv = DoLoop(result);
693 if (rv != ERR_IO_PENDING)
694 DoCallback(rv);
695}
696
697int HttpNetworkTransaction::DoLoop(int result) {
698 DCHECK(next_state_ != STATE_NONE);
699
700 int rv = result;
701 do {
702 State state = next_state_;
703 next_state_ = STATE_NONE;
704 switch (state) {
[email protected]1826a402014-01-08 15:40:48705 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
706 DCHECK_EQ(OK, rv);
707 rv = DoNotifyBeforeCreateStream();
708 break;
[email protected]82918cc2010-08-25 17:24:50709 case STATE_CREATE_STREAM:
710 DCHECK_EQ(OK, rv);
711 rv = DoCreateStream();
712 break;
713 case STATE_CREATE_STREAM_COMPLETE:
714 rv = DoCreateStreamComplete(rv);
[email protected]82918cc2010-08-25 17:24:50715 break;
[email protected]351ab642010-08-05 16:55:31716 case STATE_INIT_STREAM:
717 DCHECK_EQ(OK, rv);
718 rv = DoInitStream();
719 break;
720 case STATE_INIT_STREAM_COMPLETE:
721 rv = DoInitStreamComplete(rv);
722 break;
[email protected]044de0642010-06-17 10:42:15723 case STATE_GENERATE_PROXY_AUTH_TOKEN:
724 DCHECK_EQ(OK, rv);
725 rv = DoGenerateProxyAuthToken();
726 break;
727 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
728 rv = DoGenerateProxyAuthTokenComplete(rv);
729 break;
730 case STATE_GENERATE_SERVER_AUTH_TOKEN:
731 DCHECK_EQ(OK, rv);
732 rv = DoGenerateServerAuthToken();
733 break;
734 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
735 rv = DoGenerateServerAuthTokenComplete(rv);
736 break;
[email protected]daddea62012-09-19 05:51:13737 case STATE_INIT_REQUEST_BODY:
738 DCHECK_EQ(OK, rv);
739 rv = DoInitRequestBody();
740 break;
741 case STATE_INIT_REQUEST_BODY_COMPLETE:
742 rv = DoInitRequestBodyComplete(rv);
743 break;
[email protected]4875ba12011-03-30 22:31:51744 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55745 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00746 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51747 rv = DoBuildRequest();
748 break;
749 case STATE_BUILD_REQUEST_COMPLETE:
750 rv = DoBuildRequestComplete(rv);
751 break;
752 case STATE_SEND_REQUEST:
753 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57754 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52755 break;
[email protected]0877e3d2009-10-17 22:29:57756 case STATE_SEND_REQUEST_COMPLETE:
757 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43758 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00759 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52760 break;
761 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55762 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00763 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52764 rv = DoReadHeaders();
765 break;
766 case STATE_READ_HEADERS_COMPLETE:
767 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43768 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00769 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52770 break;
771 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55772 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00773 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52774 rv = DoReadBody();
775 break;
776 case STATE_READ_BODY_COMPLETE:
777 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43778 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00779 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52780 break;
[email protected]2d2697f92009-02-18 21:00:32781 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55782 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53783 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00784 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32785 rv = DoDrainBodyForAuthRestart();
786 break;
787 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
788 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43789 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00790 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32791 break;
initial.commit586acc5fe2008-07-26 22:42:52792 default:
793 NOTREACHED() << "bad state";
794 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04795 break;
initial.commit586acc5fe2008-07-26 22:42:52796 }
797 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
798
799 return rv;
800}
801
[email protected]1826a402014-01-08 15:40:48802int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
803 next_state_ = STATE_CREATE_STREAM;
804 bool defer = false;
805 if (!before_network_start_callback_.is_null())
Will Cassella61125f92020-07-25 09:11:37806 std::move(before_network_start_callback_).Run(&defer);
[email protected]1826a402014-01-08 15:40:48807 if (!defer)
808 return OK;
809 return ERR_IO_PENDING;
810}
811
[email protected]82918cc2010-08-25 17:24:50812int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55813 response_.network_accessed = true;
814
[email protected]82918cc2010-08-25 17:24:50815 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17816 // IP based pooling is only enabled on a retry after 421 Misdirected Request
817 // is received. Alternative Services are also disabled in this case (though
818 // they can also be disabled when retrying after a QUIC error).
819 if (!enable_ip_based_pooling_)
820 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44821 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27822 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06823 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
824 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
825 websocket_handshake_stream_base_create_helper_,
826 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44827 } else {
xunjieli96f2a402017-06-05 17:24:27828 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29829 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27830 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44831 }
[email protected]26816882010-10-14 18:03:09832 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38833 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31834}
835
[email protected]82918cc2010-08-25 17:24:50836int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59837 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59838 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50839 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38840 DCHECK(stream_.get());
bncfacdd852015-01-09 19:22:54841 } else if (result == ERR_HTTP_1_1_REQUIRED ||
842 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
843 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59844 }
845
David Benjamin76a40ad2018-02-24 22:22:08846 // Handle possible client certificate errors that may have occurred if the
847 // stream used SSL for one or more of the layers.
848 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30849
[email protected]8e6441ca2010-08-19 05:56:38850 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09851 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38852 return result;
[email protected]394816e92010-08-03 07:38:59853}
854
[email protected]82918cc2010-08-25 17:24:50855int HttpNetworkTransaction::DoInitStream() {
856 DCHECK(stream_.get());
857 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59858
859 stream_->GetRemoteEndpoint(&remote_endpoint_);
860
Steven Valdezb4ff0412018-01-18 22:39:27861 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
862 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50863}
864
865int HttpNetworkTransaction::DoInitStreamComplete(int result) {
Titouan Rigoudy78af7da2020-07-07 14:30:12866 if (result != OK) {
[email protected]82918cc2010-08-25 17:24:50867 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26868 result = HandleIOError(result);
869
870 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22871 if (stream_) {
872 total_received_bytes_ += stream_->GetTotalReceivedBytes();
873 total_sent_bytes_ += stream_->GetTotalSentBytes();
874 }
zhongyica364fbb2015-12-12 03:39:12875 CacheNetErrorDetailsAndResetStream();
Titouan Rigoudy78af7da2020-07-07 14:30:12876
877 return result;
[email protected]82918cc2010-08-25 17:24:50878 }
879
Titouan Rigoudy78af7da2020-07-07 14:30:12880 // Fire off notification that we have successfully connected.
881 if (!connected_callback_.is_null()) {
Titouan Rigoudyba507a882020-07-31 12:15:15882 TransportType type = TransportType::kDirect;
883 if (!proxy_info_.is_direct()) {
884 type = TransportType::kProxied;
885 }
886 result = connected_callback_.Run(TransportInfo(type, remote_endpoint_));
Titouan Rigoudy78af7da2020-07-07 14:30:12887 DCHECK_NE(result, ERR_IO_PENDING);
888 }
889
890 if (result == OK) {
891 // Only transition if we succeeded. Otherwise stop at STATE_NONE.
892 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
893 }
[email protected]82918cc2010-08-25 17:24:50894 return result;
895}
896
[email protected]044de0642010-06-17 10:42:15897int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
898 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
899 if (!ShouldApplyProxyAuth())
900 return OK;
[email protected]394816e92010-08-03 07:38:59901 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
902 if (!auth_controllers_[target].get())
Matt Menkebe090422019-10-18 20:25:26903 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
904 target, AuthURL(target), request_->network_isolation_key,
905 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27906 session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59907 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41908 io_callback_,
[email protected]394816e92010-08-03 07:38:59909 net_log_);
[email protected]044de0642010-06-17 10:42:15910}
911
912int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
913 DCHECK_NE(ERR_IO_PENDING, rv);
914 if (rv == OK)
915 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
916 return rv;
917}
918
919int HttpNetworkTransaction::DoGenerateServerAuthToken() {
920 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59921 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54922 if (!auth_controllers_[target].get()) {
Matt Menkebe090422019-10-18 20:25:26923 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
924 target, AuthURL(target), request_->network_isolation_key,
925 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27926 session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54927 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
928 auth_controllers_[target]->DisableEmbeddedIdentity();
929 }
[email protected]044de0642010-06-17 10:42:15930 if (!ShouldApplyServerAuth())
931 return OK;
[email protected]394816e92010-08-03 07:38:59932 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41933 io_callback_,
[email protected]394816e92010-08-03 07:38:59934 net_log_);
[email protected]044de0642010-06-17 10:42:15935}
936
937int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
938 DCHECK_NE(ERR_IO_PENDING, rv);
939 if (rv == OK)
nharperd6e65822016-03-30 23:05:48940 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14941 return rv;
942}
943
944int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24945 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14946 request_headers_.SetHeader(HttpRequestHeaders::kHost,
947 GetHostAndOptionalPort(request_->url));
948
949 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24950 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14951 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
952 "keep-alive");
953 } else {
954 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
955 }
956
[email protected]2979a492011-04-06 00:29:14957 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11958 if (request_->upload_data_stream) {
959 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14960 request_headers_.SetHeader(
961 HttpRequestHeaders::kTransferEncoding, "chunked");
962 } else {
963 request_headers_.SetHeader(
964 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09965 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14966 }
csharrisonf473dd192015-08-18 13:54:13967 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14968 // An empty POST/PUT request still needs a content length. As for HEAD,
969 // IE and Safari also add a content length header. Presumably it is to
970 // support sending a HEAD request to an URL that only expects to be sent a
971 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13972 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
973 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14974 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
975 }
976
[email protected]2979a492011-04-06 00:29:14977 // Honor load flags that impact proxy caches.
978 if (request_->load_flags & LOAD_BYPASS_CACHE) {
979 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
980 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
981 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
982 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
983 }
984
985 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
986 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
987 &request_headers_);
988 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
989 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
990 &request_headers_);
991
[email protected]c10450102011-06-27 09:06:16992 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20993
[email protected]173f8e22013-04-10 04:18:20994 response_.did_use_http_auth =
995 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
996 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14997 return OK;
998}
999
[email protected]daddea62012-09-19 05:51:131000int HttpNetworkTransaction::DoInitRequestBody() {
1001 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131002 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111003 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331004 rv = request_->upload_data_stream->Init(
1005 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1006 base::Unretained(this)),
1007 net_log_);
[email protected]daddea62012-09-19 05:51:131008 return rv;
1009}
[email protected]4875ba12011-03-30 22:31:511010
[email protected]daddea62012-09-19 05:51:131011int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1012 if (result == OK)
1013 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131014 return result;
1015}
1016
1017int HttpNetworkTransaction::DoBuildRequest() {
1018 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511019 headers_valid_ = false;
1020
1021 // This is constructed lazily (instead of within our Start method), so that
1022 // we have proxy info available.
1023 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241024 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141025 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511026 }
1027
[email protected]4875ba12011-03-30 22:31:511028 return OK;
1029}
1030
1031int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241032 if (result == OK)
1033 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511034 return result;
1035}
1036
[email protected]0877e3d2009-10-17 22:29:571037int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251038 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571039 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1040
[email protected]bf3eb002012-11-15 05:50:111041 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521042}
1043
[email protected]0877e3d2009-10-17 22:29:571044int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251045 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361046
1047 if (result == ERR_HTTP_1_1_REQUIRED ||
1048 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1049 return HandleHttp11Required(result);
1050 }
1051
initial.commit586acc5fe2008-07-26 22:42:521052 if (result < 0)
1053 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571054 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521055 return OK;
1056}
1057
1058int HttpNetworkTransaction::DoReadHeaders() {
1059 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411060 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521061}
1062
1063int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111064 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1065 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381066 if (IsCertificateError(result)) {
1067 // We don't handle a certificate error during SSL renegotiation, so we
1068 // have to return an error that's not in the certificate error range
1069 // (-2xx).
David Benjaminbac8dff2019-08-07 01:30:411070 //
1071 // TODO(davidben): Remove this error. This is impossible now that server
1072 // certificates are forbidden from changing in renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381073 LOG(ERROR) << "Got a server certificate with error " << result
1074 << " during SSL renegotiation";
1075 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1076 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381077 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251078 DCHECK(IsSecureRequest());
David Benjamin1c4b6d012019-07-08 17:12:571079 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]90499482013-06-01 00:39:501080 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
David Benjaminbac8dff2019-08-07 01:30:411081 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1082 total_sent_bytes_ += stream_->GetTotalSentBytes();
1083 stream_->Close(true);
1084 CacheNetErrorDetailsAndResetStream();
[email protected]2181ea002009-06-09 01:37:271085 }
1086
bncfacdd852015-01-09 19:22:541087 if (result == ERR_HTTP_1_1_REQUIRED ||
1088 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1089 return HandleHttp11Required(result);
1090 }
1091
[email protected]c871864d72014-03-13 19:56:191092 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1093 // response headers were received, we do the best we can to make sense of it
1094 // and send it back up the stack.
1095 //
1096 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1097 // bizarre for SPDY. Assuming this logic is useful at all.
1098 // TODO(davidben): Bubble the error code up so we do not cache?
1099 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1100 result = OK;
1101
1102 if (result < 0)
1103 return HandleIOError(result);
1104
[email protected]90499482013-06-01 00:39:501105 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521106
eustasc7d27da2017-04-06 10:33:201107 if (response_.headers.get() && !ContentEncodingsValid())
1108 return ERR_CONTENT_DECODING_FAILED;
1109
[email protected]d58ceea82014-06-04 10:55:541110 // On a 408 response from the server ("Request Timeout") on a stale socket,
David Schinazif832cb82019-11-08 22:25:271111 // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
1112 // multiplex requests and have no need for 408.
[email protected]0aff0d82014-06-14 08:49:041113 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181114 if (response_.headers.get() &&
1115 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
David Schinazif832cb82019-11-08 22:25:271116 HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
1117 HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
[email protected]d58ceea82014-06-04 10:55:541118 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421119#if BUILDFLAG(ENABLE_REPORTING)
1120 GenerateNetworkErrorLoggingReport(OK);
1121#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541122 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001123 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541124 response_.headers->response_code());
1125 // This will close the socket - it would be weird to try and reuse it, even
1126 // if the server doesn't actually close it.
1127 ResetConnectionAndRequestForResend();
1128 return OK;
1129 }
1130
Eric Roman06bd9742019-07-13 15:19:131131 NetLogResponseHeaders(net_log_,
1132 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1133 response_.headers.get());
Andrey Kosyakov2e893e62017-08-31 17:00:521134 if (response_headers_callback_)
1135 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391136
bncbe0f6af2015-10-15 17:49:561137 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571138 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1139 // indicates a buggy server. See:
1140 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1141 if (request_->method == "PUT")
1142 return ERR_METHOD_NOT_SUPPORTED;
1143 }
[email protected]4ddaf2502008-10-23 18:26:191144
Steven Valdez58097ec32018-07-16 18:29:041145 if (can_send_early_data_ && response_.headers.get() &&
1146 response_.headers->response_code() == HTTP_TOO_EARLY) {
1147 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1148 }
1149
[email protected]0877e3d2009-10-17 22:29:571150 // Check for an intermediate 100 Continue response. An origin server is
1151 // allowed to send this response even if we didn't ask for it, so we just
1152 // need to skip over it.
1153 // We treat any other 1xx in this same way (although in practice getting
1154 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441155 // Unless this is a WebSocket request, in which case we pass it on up.
1156 if (response_.headers->response_code() / 100 == 1 &&
1157 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451158 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571159 next_state_ = STATE_READ_HEADERS;
1160 return OK;
1161 }
1162
davidbence688ae2017-05-04 15:12:591163 if (response_.headers->response_code() == 421 &&
1164 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421165#if BUILDFLAG(ENABLE_REPORTING)
1166 GenerateNetworkErrorLoggingReport(OK);
1167#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591168 // Retry the request with both IP based pooling and Alternative Services
1169 // disabled.
1170 enable_ip_based_pooling_ = false;
1171 enable_alternative_services_ = false;
1172 net_log_.AddEvent(
1173 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1174 ResetConnectionAndRequestForResend();
1175 return OK;
bnc8016c1f2017-03-31 02:11:291176 }
1177
bncb26024382016-06-29 02:39:451178 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491179 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081180 if (response_.ssl_info.is_valid() &&
1181 !IsCertStatusError(response_.ssl_info.cert_status)) {
1182 session_->http_stream_factory()->ProcessAlternativeServices(
Matt Menkeb32ba5122019-09-10 19:17:051183 session_, network_isolation_key_, response_.headers.get(),
Bence Béky230ac612017-08-30 19:17:081184 url::SchemeHostPort(request_->url));
1185 }
1186 }
asanka5ffd5d72016-03-23 16:20:491187
[email protected]e772db3f2010-07-12 18:11:131188 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571189 if (rv != OK)
1190 return rv;
1191
Douglas Creager3cb042052018-11-06 23:08:521192#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421193 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1194 // reports generated before the NEL header is processed here will just be
1195 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141196 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521197 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421198
1199 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111200 // code), or if the response body will not be read, or on a redirect.
1201 // Note: This will report a success for a redirect even if an error is
1202 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421203 int response_code = response_.headers->response_code();
1204 if ((response_code >= 400 && response_code < 600) ||
1205 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1206 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111207 response_.headers->GetContentLength() == 0 ||
1208 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421209 GenerateNetworkErrorLoggingReport(OK);
1210 }
1211#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521212
[email protected]0877e3d2009-10-17 22:29:571213 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261214
1215 // We have reached the end of Start state machine, set the RequestInfo to
1216 // null.
1217 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1218 // only until the final response headers are received. Clearing it will ensure
1219 // that HttpRequestInfo is only used up until final response headers are
1220 // received. Clearing is allowed so that the transaction can be disassociated
1221 // from its creating consumer in cases where it is shared for writing to the
1222 // cache. It is also safe to set it to null at this point since
1223 // upload_data_stream is also not used in the Read state machine.
1224 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1225 request_ = nullptr;
1226
[email protected]0877e3d2009-10-17 22:29:571227 return OK;
initial.commit586acc5fe2008-07-26 22:42:521228}
1229
1230int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501231 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131232 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351233 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521234
1235 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501236 return stream_->ReadResponseBody(
1237 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521238}
1239
1240int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1241 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381242 bool done = false;
1243 if (result <= 0) {
1244 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521245 done = true;
[email protected]8e6441ca2010-08-19 05:56:381246 }
[email protected]9492e4a2010-02-24 00:58:461247
mmenkebd84c392015-09-02 14:12:341248 // Clean up connection if we are done.
1249 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381250 // Note: Just because IsResponseBodyComplete is true, we're not
1251 // necessarily "done". We're only "done" when it is the last
1252 // read on this HttpNetworkTransaction, which will be signified
1253 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341254 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381255 // the stream. No need to compute it here just to pass back
1256 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341257 bool keep_alive =
1258 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521259
[email protected]8e6441ca2010-08-19 05:56:381260 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011261 // Note: we don't reset the stream here. We've closed it, but we still
1262 // need it around so that callers can call methods such as
1263 // GetUploadProgress() and have them be meaningful.
1264 // TODO(mbelshe): This means we closed the stream here, and we close it
1265 // again in ~HttpNetworkTransaction. Clean that up.
1266
[email protected]8e6441ca2010-08-19 05:56:381267 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171268
1269 // This transaction was successful. If it had been retried because of an
1270 // error with an alternative service, mark that alternative service broken.
1271 if (!enable_alternative_services_ &&
1272 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411273 HistogramBrokenAlternateProtocolLocation(
1274 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171275 session_->http_server_properties()->MarkAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051276 retried_alternative_service_, network_isolation_key_);
rch2f2991c2017-04-13 19:28:171277 }
Lily Chenfec60d92019-01-24 01:16:421278
1279#if BUILDFLAG(ENABLE_REPORTING)
1280 GenerateNetworkErrorLoggingReport(result);
1281#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521282 }
1283
1284 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351285 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521286 read_buf_len_ = 0;
1287
1288 return result;
1289}
1290
[email protected]2d2697f92009-02-18 21:00:321291int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1292 // This method differs from DoReadBody only in the next_state_. So we just
1293 // call DoReadBody and override the next_state_. Perhaps there is a more
1294 // elegant way for these two methods to share code.
1295 int rv = DoReadBody();
1296 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1297 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1298 return rv;
1299}
1300
[email protected]0877e3d2009-10-17 22:29:571301// TODO(wtc): This method and the DoReadBodyComplete method are almost
1302// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321303int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231304 // keep_alive defaults to true because the very reason we're draining the
1305 // response body is to reuse the connection for auth restart.
1306 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321307 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571308 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421309 // Note: No Network Error Logging report is generated here because a report
1310 // will have already been generated for the original request due to the auth
1311 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321312 done = true;
[email protected]68873ba2009-06-04 21:49:231313 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311314 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571315 done = true;
[email protected]2d2697f92009-02-18 21:00:321316 }
1317
1318 if (done) {
1319 DidDrainBodyForAuthRestart(keep_alive);
1320 } else {
1321 // Keep draining.
1322 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1323 }
1324
1325 return OK;
1326}
1327
Douglas Creager3cb042052018-11-06 23:08:521328#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141329void HttpNetworkTransaction::ProcessReportToHeader() {
1330 std::string value;
1331 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1332 return;
1333
Matt Menkef437a87e2020-10-22 23:22:241334 ReportingService* reporting_service = session_->reporting_service();
1335 if (!reporting_service)
Douglas Creager134b52e2018-11-09 18:00:141336 return;
Douglas Creager134b52e2018-11-09 18:00:141337
1338 // Only accept Report-To headers on HTTPS connections that have no
1339 // certificate errors.
Lily Chen9364a4c2020-06-25 16:07:521340 if (!response_.ssl_info.is_valid())
Douglas Creager134b52e2018-11-09 18:00:141341 return;
Lily Chen9364a4c2020-06-25 16:07:521342 if (IsCertStatusError(response_.ssl_info.cert_status))
Douglas Creager134b52e2018-11-09 18:00:141343 return;
Douglas Creager134b52e2018-11-09 18:00:141344
Matt Menkef437a87e2020-10-22 23:22:241345 reporting_service->ProcessHeader(url_.GetOrigin(), network_isolation_key_,
1346 value);
Douglas Creager134b52e2018-11-09 18:00:141347}
1348
Douglas Creager3cb042052018-11-06 23:08:521349void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1350 std::string value;
1351 if (!response_.headers->GetNormalizedHeader(
1352 NetworkErrorLoggingService::kHeaderName, &value)) {
1353 return;
1354 }
1355
Matt Menkef437a87e2020-10-22 23:22:241356 NetworkErrorLoggingService* network_error_logging_service =
Douglas Creager3cb042052018-11-06 23:08:521357 session_->network_error_logging_service();
Matt Menkef437a87e2020-10-22 23:22:241358 if (!network_error_logging_service)
Douglas Creager3cb042052018-11-06 23:08:521359 return;
Douglas Creager3cb042052018-11-06 23:08:521360
Lily Chen90ae93cc2019-02-14 01:15:391361 // Don't accept NEL headers received via a proxy, because the IP address of
1362 // the destination server is not known.
1363 if (response_.was_fetched_via_proxy)
1364 return;
1365
Douglas Creager3cb042052018-11-06 23:08:521366 // Only accept NEL headers on HTTPS connections that have no certificate
1367 // errors.
Lily Chena9e9d6cb2020-06-25 17:34:491368 if (!response_.ssl_info.is_valid() ||
1369 IsCertStatusError(response_.ssl_info.cert_status)) {
Douglas Creager3cb042052018-11-06 23:08:521370 return;
1371 }
1372
Lily Chena9e9d6cb2020-06-25 17:34:491373 if (remote_endpoint_.address().empty())
Douglas Creager3cb042052018-11-06 23:08:521374 return;
Douglas Creager3cb042052018-11-06 23:08:521375
Matt Menkef437a87e2020-10-22 23:22:241376 network_error_logging_service->OnHeader(network_isolation_key_,
1377 url::Origin::Create(url_),
1378 remote_endpoint_.address(), value);
Douglas Creager3cb042052018-11-06 23:08:521379}
Douglas Creageref5eecdc2018-11-09 20:50:361380
Lily Chenfec60d92019-01-24 01:16:421381void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1382 if (rv < 0 && rv != ERR_IO_PENDING)
1383 GenerateNetworkErrorLoggingReport(rv);
1384}
1385
Douglas Creageref5eecdc2018-11-09 20:50:361386void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421387 // |rv| should be a valid net::Error
1388 DCHECK_NE(rv, ERR_IO_PENDING);
1389 DCHECK_LE(rv, 0);
1390
1391 if (network_error_logging_report_generated_)
1392 return;
1393 network_error_logging_report_generated_ = true;
1394
Douglas Creageref5eecdc2018-11-09 20:50:361395 NetworkErrorLoggingService* service =
1396 session_->network_error_logging_service();
Lily Chend613caa2019-11-25 20:46:331397 if (!service)
Douglas Creageref5eecdc2018-11-09 20:50:361398 return;
Douglas Creageref5eecdc2018-11-09 20:50:361399
Lily Chen90ae93cc2019-02-14 01:15:391400 // Don't report on proxy auth challenges.
1401 if (response_.headers && response_.headers->response_code() ==
1402 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1403 return;
1404 }
1405
1406 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1407 // internal network details.
1408 if (response_.was_fetched_via_proxy)
1409 return;
1410
Lily Chen00196ab62018-12-04 19:52:291411 // Ignore errors from non-HTTPS origins.
Lily Chend613caa2019-11-25 20:46:331412 if (!url_.SchemeIsCryptographic())
Lily Chen00196ab62018-12-04 19:52:291413 return;
Douglas Creageref5eecdc2018-11-09 20:50:361414
1415 NetworkErrorLoggingService::RequestDetails details;
1416
Matt Menkef437a87e2020-10-22 23:22:241417 details.network_isolation_key = network_isolation_key_;
Douglas Creageref5eecdc2018-11-09 20:50:361418 details.uri = url_;
1419 if (!request_referrer_.empty())
1420 details.referrer = GURL(request_referrer_);
1421 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421422 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361423 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421424 } else {
1425 details.server_ip = IPAddress();
1426 }
Douglas Creageref5eecdc2018-11-09 20:50:361427 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1428 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421429 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361430 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421431 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361432 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421433 }
Douglas Creageref5eecdc2018-11-09 20:50:361434 // If we got response headers, assume that the connection used HTTP/1.1
1435 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421436 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361437 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421438 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361439 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361440 }
Lily Chenfec60d92019-01-24 01:16:421441 details.method = request_method_;
1442 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361443 details.type = static_cast<Error>(rv);
1444 details.reporting_upload_depth = request_reporting_upload_depth_;
1445
1446 service->OnRequest(std::move(details));
1447}
Lily Chenfec60d92019-01-24 01:16:421448#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521449
bncfacdd852015-01-09 19:22:541450int HttpNetworkTransaction::HandleHttp11Required(int error) {
1451 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1452 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1453
Matt Menke0e3276862019-08-23 00:21:341454 // HttpServerProperties should have been updated, so when the request is sent
1455 // again, it will automatically use HTTP/1.1.
bncfacdd852015-01-09 19:22:541456 ResetConnectionAndRequestForResend();
1457 return OK;
1458}
1459
David Benjamin76a40ad2018-02-24 22:22:081460int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjaminbac8dff2019-08-07 01:30:411461 // Client certificate errors may come from either the origin server or the
1462 // proxy.
1463 //
1464 // Origin errors are handled here, while most proxy errors are handled in the
1465 // HttpStreamFactory and below. However, if the request is not tunneled (i.e.
1466 // the origin is HTTP, so there is no HTTPS connection) and the proxy does not
1467 // report a bad client certificate until after the TLS handshake completes.
1468 // The latter occurs in TLS 1.3 or TLS 1.2 with False Start (disabled for
1469 // proxies). The error will then surface out of Read() rather than Connect()
1470 // and ultimately surfaced out of DoReadHeadersComplete().
David Benjamin3b94b0f2019-04-25 23:07:521471 //
1472 // See https://crbug.com/828965.
David Benjaminbac8dff2019-08-07 01:30:411473 bool is_server = !UsingHttpProxyWithoutTunnel();
1474 HostPortPair host_port_pair =
1475 is_server ? HostPortPair::FromURL(request_->url)
1476 : proxy_info_.proxy_server().host_port_pair();
David Benjamin3b94b0f2019-04-25 23:07:521477
David Benjaminbac8dff2019-08-07 01:30:411478 if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
Eric Romandbf7441f2020-07-28 07:05:021479 DCHECK((is_server && IsSecureRequest()) ||
1480 proxy_info_.is_secure_http_like());
David Benjaminbac8dff2019-08-07 01:30:411481 if (session_->ssl_client_context()->ClearClientCertificate(
1482 host_port_pair)) {
1483 // The private key handle may have gone stale due to, e.g., the user
1484 // unplugging their smartcard. Operating systems do not provide reliable
1485 // notifications for this, so if the signature failed and the user was
1486 // not already prompted for certificate on this request, retry to ask
1487 // the user for a new one.
1488 //
1489 // TODO(davidben): There is no corresponding feature for proxy client
1490 // certificates. Ideally this would live at a lower level, common to both,
1491 // but |configured_client_cert_for_server_| is not accessible below the
1492 // socket pools.
1493 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1494 !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
1495 retry_attempts_++;
1496 net_log_.AddEventWithNetErrorCode(
1497 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1498 ResetConnectionAndRequestForResend();
1499 return OK;
1500 }
David Benjamin76a40ad2018-02-24 22:22:081501 }
1502 }
initial.commit586acc5fe2008-07-26 22:42:521503 return error;
1504}
1505
[email protected]bd0b6772011-01-11 19:59:301506// This method determines whether it is safe to resend the request after an
1507// IO error. It can only be called in response to request header or body
1508// write errors or response header read errors. It should not be used in
1509// other cases, such as a Connect error.
1510int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031511 // Because the peer may request renegotiation with client authentication at
1512 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081513 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301514
Lily Chenfec60d92019-01-24 01:16:421515#if BUILDFLAG(ENABLE_REPORTING)
1516 GenerateNetworkErrorLoggingReportIfError(error);
1517#endif // BUILDFLAG(ENABLE_REPORTING)
1518
[email protected]bd0b6772011-01-11 19:59:301519 switch (error) {
1520 // If we try to reuse a connection that the server is in the process of
1521 // closing, we may end up successfully writing out our request (or a
1522 // portion of our request) only to find a connection error when we try to
1523 // read from (or finish writing to) the socket.
1524 case ERR_CONNECTION_RESET:
1525 case ERR_CONNECTION_CLOSED:
1526 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511527 // There can be a race between the socket pool checking checking whether a
1528 // socket is still connected, receiving the FIN, and sending/reading data
1529 // on a reused socket. If we receive the FIN between the connectedness
1530 // check and writing/reading from the socket, we may first learn the socket
1531 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1532 // likely happen when trying to retrieve its IP address.
1533 // See http://crbug.com/105824 for more details.
1534 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491535 // If a socket is closed on its initial request, HttpStreamParser returns
1536 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1537 // preconnected but failed to be used before the server timed it out.
1538 case ERR_EMPTY_RESPONSE:
1539 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381540 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001541 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301542 ResetConnectionAndRequestForResend();
1543 error = OK;
1544 }
1545 break;
Steven Valdez58097ec32018-07-16 18:29:041546 case ERR_EARLY_DATA_REJECTED:
1547 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1548 net_log_.AddEventWithNetErrorCode(
1549 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1550 // Disable early data on the SSLConfig on a reset.
1551 can_send_early_data_ = false;
1552 ResetConnectionAndRequestForResend();
1553 error = OK;
1554 break;
Bence Békyd0d69502019-06-25 19:47:181555 case ERR_HTTP2_PING_FAILED:
1556 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1557 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1558 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1559 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271560 case ERR_QUIC_HANDSHAKE_FAILED:
Bence Béky2ee18922020-09-25 12:11:321561 case ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED:
Biljith Jayan45a41722017-08-16 18:43:141562 if (HasExceededMaxRetries())
1563 break;
[email protected]b6754252012-06-13 23:14:381564 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001565 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141566 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001567 ResetConnectionAndRequestForResend();
1568 error = OK;
1569 break;
rch2f2991c2017-04-13 19:28:171570 case ERR_QUIC_PROTOCOL_ERROR:
1571 if (GetResponseHeaders() != nullptr ||
1572 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1573 // If the response headers have already been recieved and passed up
1574 // then the request can not be retried. Also, if there was no
1575 // alternative service used for this request, then there is no
1576 // alternative service to be disabled.
1577 break;
1578 }
Biljith Jayan45a41722017-08-16 18:43:141579 if (HasExceededMaxRetries())
1580 break;
rch2f2991c2017-04-13 19:28:171581 if (session_->http_server_properties()->IsAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051582 retried_alternative_service_, network_isolation_key_)) {
rch2f2991c2017-04-13 19:28:171583 // If the alternative service was marked as broken while the request
1584 // was in flight, retry the request which will not use the broken
1585 // alternative service.
1586 net_log_.AddEventWithNetErrorCode(
1587 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141588 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171589 ResetConnectionAndRequestForResend();
1590 error = OK;
Victor Vasilieva1e66d72019-12-05 17:55:381591 } else if (session_->context()
1592 .quic_context->params()
1593 ->retry_without_alt_svc_on_quic_errors) {
rch2f2991c2017-04-13 19:28:171594 // Disable alternative services for this request and retry it. If the
1595 // retry succeeds, then the alternative service will be marked as
1596 // broken then.
1597 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361598 net_log_.AddEventWithNetErrorCode(
1599 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141600 retry_attempts_++;
rch514a44a82017-04-13 04:46:361601 ResetConnectionAndRequestForResend();
1602 error = OK;
1603 }
1604 break;
[email protected]bd0b6772011-01-11 19:59:301605 }
1606 return error;
1607}
1608
[email protected]c3b35c22008-09-27 03:19:421609void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581610 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221611 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191612 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221613 total_sent_bytes_ += stream_->GetTotalSentBytes();
1614 }
zhongyica364fbb2015-12-12 03:39:121615 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581616}
1617
1618void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251619 send_start_time_ = base::TimeTicks();
1620 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251621
[email protected]0757e7702009-03-27 04:00:221622 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351623 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421624 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571625 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201626 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141627 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381628 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591629 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121630 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521631 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421632#if BUILDFLAG(ENABLE_REPORTING)
1633 network_error_logging_report_generated_ = false;
1634 start_timeticks_ = base::TimeTicks::Now();
1635#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121636}
1637
1638void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1639 if (stream_)
1640 stream_->PopulateNetErrorDetails(&net_error_details_);
1641 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571642}
1643
[email protected]0877e3d2009-10-17 22:29:571644HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501645 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421646}
1647
[email protected]a34f61ee2014-03-18 20:59:491648bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381649 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351650 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161651
[email protected]2a5c76b2008-09-25 22:15:161652 // NOTE: we resend a request only if we reused a keep-alive connection.
1653 // This automatically prevents an infinite resend loop because we'll run
1654 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381655 if (connection_is_proven && !has_received_headers)
1656 return true;
1657 return false;
[email protected]1c773ea12009-04-28 19:58:421658}
1659
Biljith Jayan45a41722017-08-16 18:43:141660bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1661 return (retry_attempts_ >= kMaxRetryAttempts);
1662}
1663
David Benjamin83ddfb32018-03-30 01:07:521664bool HttpNetworkTransaction::CheckMaxRestarts() {
1665 num_restarts_++;
1666 return num_restarts_ < kMaxRestarts;
1667}
1668
[email protected]1c773ea12009-04-28 19:58:421669void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381670 if (stream_.get()) {
1671 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121672 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161673 }
1674
[email protected]0877e3d2009-10-17 22:29:571675 // We need to clear request_headers_ because it contains the real request
1676 // headers, but we may need to resend the CONNECT request first to recreate
1677 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201678 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501679 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421680
1681#if BUILDFLAG(ENABLE_REPORTING)
1682 // Reset for new request.
1683 network_error_logging_report_generated_ = false;
1684 start_timeticks_ = base::TimeTicks::Now();
1685#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221686
1687 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541688}
1689
[email protected]1c773ea12009-04-28 19:58:421690bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241691 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421692}
license.botbf09a502008-08-24 00:55:551693
[email protected]1c773ea12009-04-28 19:58:421694bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
Matt Menke25eaa432020-08-25 00:10:001695 return request_->privacy_mode == PRIVACY_MODE_DISABLED;
[email protected]1c773ea12009-04-28 19:58:421696}
1697
[email protected]e772db3f2010-07-12 18:11:131698int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271699 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501700 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421701
[email protected]0877e3d2009-10-17 22:29:571702 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581703 if (status != HTTP_UNAUTHORIZED &&
1704 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421705 return OK;
[email protected]9094b602012-02-27 21:44:581706 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111707 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161708 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1709 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421710
[email protected]9094b602012-02-27 21:44:581711 // This case can trigger when an HTTPS server responds with a "Proxy
1712 // authentication required" status code through a non-authenticating
1713 // proxy.
[email protected]7a67a8152010-11-05 18:31:101714 if (!auth_controllers_[target].get())
1715 return ERR_UNEXPECTED_PROXY_AUTH;
1716
[email protected]a7ea8832010-07-12 17:54:541717 int rv = auth_controllers_[target]->HandleAuthChallenge(
Matt Menke25eaa432020-08-25 00:10:001718 headers, response_.ssl_info, !ShouldApplyServerAuth(), false, net_log_);
[email protected]228404f2010-06-24 04:31:411719 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491720 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411721
Emily Starkf2c9bbd2019-04-09 17:08:581722 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411723
[email protected]228404f2010-06-24 04:31:411724 return rv;
[email protected]f9ee6b52008-11-08 06:46:231725}
1726
[email protected]8e6441ca2010-08-19 05:56:381727bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1728 return auth_controllers_[target].get() &&
1729 auth_controllers_[target]->HaveAuth();
1730}
1731
[email protected]228404f2010-06-24 04:31:411732GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1733 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461734 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411735 if (!proxy_info_.proxy_server().is_valid() ||
1736 proxy_info_.proxy_server().is_direct()) {
1737 return GURL(); // There is no proxy server.
1738 }
Eric Romandbf7441f2020-07-28 07:05:021739 // TODO(https://crbug.com/1103768): Mapping proxy addresses to
1740 // URLs is a lossy conversion, shouldn't do this.
1741 const char* scheme =
1742 proxy_info_.is_secure_http_like() ? "https://" : "http://";
[email protected]2df19bb2010-08-25 20:13:461743 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351744 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461745 }
[email protected]228404f2010-06-24 04:31:411746 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291747 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361748 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291749 }
[email protected]228404f2010-06-24 04:31:411750 return request_->url;
1751 default:
1752 return GURL();
1753 }
[email protected]c3b35c22008-09-27 03:19:421754}
1755
[email protected]831e4a32013-11-14 02:14:441756bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141757 return websocket_handshake_stream_base_create_helper_ &&
1758 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441759}
1760
ttuttle1f2d7e92015-04-28 16:17:471761void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1762 DCHECK(stream_request_);
1763
1764 // Since the transaction can restart with auth credentials, it may create a
1765 // stream more than once. Accumulate all of the connection attempts across
1766 // those streams by appending them to the vector:
1767 for (const auto& attempt : stream_request_->connection_attempts())
1768 connection_attempts_.push_back(attempt);
1769}
1770
eustasc7d27da2017-04-06 10:33:201771bool HttpNetworkTransaction::ContentEncodingsValid() const {
1772 HttpResponseHeaders* headers = GetResponseHeaders();
1773 DCHECK(headers);
1774
1775 std::string accept_encoding;
1776 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1777 &accept_encoding);
1778 std::set<std::string> allowed_encodings;
Paul Jensene85e8152019-07-01 15:38:001779 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
eustasc7d27da2017-04-06 10:33:201780 return false;
eustasc7d27da2017-04-06 10:33:201781
1782 std::string content_encoding;
1783 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1784 std::set<std::string> used_encodings;
Paul Jensene85e8152019-07-01 15:38:001785 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
eustasc7d27da2017-04-06 10:33:201786 return false;
eustasc7d27da2017-04-06 10:33:201787
1788 // When "Accept-Encoding" is not specified, it is parsed as "*".
1789 // If "*" encoding is advertised, then any encoding should be "accepted".
1790 // This does not mean, that it will be successfully decoded.
1791 if (allowed_encodings.find("*") != allowed_encodings.end())
1792 return true;
1793
sky50576f32017-05-01 19:28:031794 bool result = true;
eustasc7d27da2017-04-06 10:33:201795 for (auto const& encoding : used_encodings) {
1796 SourceStream::SourceType source_type =
1797 FilterSourceStream::ParseEncodingType(encoding);
1798 // We don't reject encodings we are not aware. They just will not decode.
1799 if (source_type == SourceStream::TYPE_UNKNOWN)
1800 continue;
1801 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031802 result = false;
1803 break;
eustasc7d27da2017-04-06 10:33:201804 }
1805 }
sky50576f32017-05-01 19:28:031806
1807 // Temporary workaround for http://crbug.com/714514
1808 if (headers->IsRedirect(nullptr)) {
1809 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1810 !result);
1811 return true;
1812 }
1813
1814 return result;
eustasc7d27da2017-04-06 10:33:201815}
1816
[email protected]c3b35c22008-09-27 03:19:421817} // namespace net