blob: 44104427ace559a4411c85cb8ce0371af413e428 [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
Steven Valdez03e872d2018-03-02 15:39:15195 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27196 can_send_early_data_ = true;
197 }
198
Dominic Farolino4ecaf0a2019-08-16 06:40:09199 if (request_->load_flags & LOAD_PREFETCH) {
jkarlinfb1d5172015-01-12 14:10:29200 response_.unused_since_prefetch = true;
Dominic Farolino4ecaf0a2019-08-16 06:40:09201 }
202
203 if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
204 DCHECK(response_.unused_since_prefetch);
205 response_.restricted_prefetch = true;
206 }
jkarlinfb1d5172015-01-12 14:10:29207
Helen Lib495c3802018-03-30 13:46:09208 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04209 int rv = DoLoop(OK);
210 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04211 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42212
213 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
214 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
215 // other net::Error can be returned.
216 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04217 return rv;
218}
219
220int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04221 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38222 DCHECK(!stream_.get());
223 DCHECK(!stream_request_.get());
224 DCHECK_EQ(STATE_NONE, next_state_);
225
David Benjamin83ddfb32018-03-30 01:07:52226 if (!CheckMaxRestarts())
227 return ERR_TOO_MANY_RETRIES;
228
[email protected]82918cc2010-08-25 17:24:50229 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38230
[email protected]ccb40e52008-09-17 20:54:40231 int rv = DoLoop(OK);
232 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04233 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42234
235 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
236 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
237 // other net::Error can be returned.
238 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11239 return rv;
[email protected]96d570e42008-08-05 22:43:04240}
241
[email protected]0b45559b2009-06-12 21:45:11242int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08243 scoped_refptr<X509Certificate> client_cert,
244 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04245 CompletionOnceCallback callback) {
David Benjaminbac8dff2019-08-07 01:30:41246 // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
247 // existing streams and stream requests to force a new connection.
[email protected]8e6441ca2010-08-19 05:56:38248 DCHECK(!stream_request_.get());
249 DCHECK(!stream_.get());
250 DCHECK_EQ(STATE_NONE, next_state_);
251
David Benjamin83ddfb32018-03-30 01:07:52252 if (!CheckMaxRestarts())
253 return ERR_TOO_MANY_RETRIES;
254
David Benjaminbac8dff2019-08-07 01:30:41255 // Add the credentials to the client auth cache. The next stream request will
256 // then pick them up.
257 session_->ssl_client_context()->SetClientCertificate(
mattm436ccfe2017-06-19 20:24:08258 response_.cert_request_info->host_and_port, std::move(client_cert),
259 std::move(client_private_key));
David Benjaminbac8dff2019-08-07 01:30:41260
261 if (!response_.cert_request_info->is_proxy)
262 configured_client_cert_for_server_ = true;
263
[email protected]0b45559b2009-06-12 21:45:11264 // Reset the other member variables.
265 // Note: this is necessary only with SSL renegotiation.
266 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50267 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11268 int rv = DoLoop(OK);
269 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04270 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42271
272 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
273 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
274 // other net::Error can be returned.
275 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11276 return rv;
277}
278
Bence Béky046f8c82018-06-12 02:26:04279int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
280 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52281 if (!CheckMaxRestarts())
282 return ERR_TOO_MANY_RETRIES;
283
[email protected]0757e7702009-03-27 04:00:22284 HttpAuth::Target target = pending_auth_target_;
285 if (target == HttpAuth::AUTH_NONE) {
286 NOTREACHED();
287 return ERR_UNEXPECTED;
288 }
[email protected]0757e7702009-03-27 04:00:22289 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42290
[email protected]f3cf9802011-10-28 18:44:58291 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13292
[email protected]49639fa2011-12-20 23:22:41293 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38294
295 int rv = OK;
296 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
297 // In this case, we've gathered credentials for use with proxy
298 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50299 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35300 DCHECK(stream_request_ != nullptr);
301 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54302 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39303 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54304 } else {
[email protected]8e6441ca2010-08-19 05:56:38305 // In this case, we've gathered credentials for the server or the proxy
306 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35307 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54308 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38309 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42310 // Note: If an error is encountered while draining the old response body, no
311 // Network Error Logging report will be generated, because the error was
312 // with the old request, which will already have had a NEL report generated
313 // for it due to the auth challenge (so we don't report a second error for
314 // that request).
[email protected]a7ea8832010-07-12 17:54:54315 }
[email protected]c3b35c22008-09-27 03:19:42316
[email protected]c3b35c22008-09-27 03:19:42317 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04318 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42319 return rv;
[email protected]96d570e42008-08-05 22:43:04320}
321
[email protected]f9ee6b52008-11-08 06:46:23322void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
323 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38324 DCHECK(!stream_request_.get());
325
Bence Béky3238f2e12017-09-22 22:44:49326 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
327 if (target == HttpAuth::AUTH_SERVER &&
328 auth_controllers_[target]->NeedsHTTP11()) {
329 session_->http_server_properties()->SetHTTP11Required(
Matt Menked9b24f02019-09-26 17:07:17330 url::SchemeHostPort(request_->url), network_isolation_key_);
Bence Béky3238f2e12017-09-22 22:44:49331 }
332
[email protected]2d2697f92009-02-18 21:00:32333 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57334 // Even if the server says the connection is keep-alive, we have to be
335 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34336 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57337 // If the response body hasn't been completely read, we need to drain
338 // it first.
[email protected]351ab642010-08-05 16:55:31339 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32340 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44341 read_buf_ = base::MakeRefCounted<IOBuffer>(
342 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32343 read_buf_len_ = kDrainBodyBufferSize;
344 return;
345 }
[email protected]0877e3d2009-10-17 22:29:57346 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09347 }
348
[email protected]2d2697f92009-02-18 21:00:32349 // We don't need to drain the response body, so we act as if we had drained
350 // the response body.
351 DidDrainBodyForAuthRestart(keep_alive);
352}
353
354void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38355 DCHECK(!stream_request_.get());
356
357 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19358 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22359 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35360 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34361 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38362 // We should call connection_->set_idle_time(), but this doesn't occur
363 // often enough to be worth the trouble.
364 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39365 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38366 }
[email protected]697ef4c2010-10-14 16:38:58367
368 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46369 // Close the stream and mark it as not_reusable. Even in the
370 // keep_alive case, we've determined that the stream_ is not
371 // reusable if new_stream is NULL.
372 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58373 next_state_ = STATE_CREATE_STREAM;
374 } else {
sclittlefb249892015-09-10 21:33:22375 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19376 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22377 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58378 next_state_ = STATE_INIT_STREAM;
379 }
380 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32381 }
[email protected]f9ee6b52008-11-08 06:46:23382
383 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58384 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23385}
386
[email protected]8e6441ca2010-08-19 05:56:38387bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
388 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
389 HaveAuth(pending_auth_target_);
390}
391
Bence Béky046f8c82018-06-12 02:26:04392int HttpNetworkTransaction::Read(IOBuffer* buf,
393 int buf_len,
394 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04395 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35396 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04397
[email protected]ad8e04a2010-11-01 04:16:27398 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38399 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04400 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29401 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04402 // bytes when establishing a tunnel because they might be controlled by
403 // an active network attacker. We don't worry about this for HTTP
404 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29405 // We reach this case when the user cancels a 407 proxy auth prompt. We
406 // also don't worry about this for an HTTPS Proxy, because the
407 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04408 // See http://crbug.com/8473.
Eric Romandbf7441f2020-07-28 07:05:02409 DCHECK(proxy_info_.is_http_like());
[email protected]9094b602012-02-27 21:44:58410 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]8a1f3312010-05-25 19:25:04411 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44412 }
413
[email protected]e60e47a2010-07-14 03:37:18414 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15415 next_state_ = STATE_READ_BODY;
416
[email protected]96d570e42008-08-05 22:43:04417 read_buf_ = buf;
418 read_buf_len_ = buf_len;
419
[email protected]96d570e42008-08-05 22:43:04420 int rv = DoLoop(OK);
421 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04422 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04423 return rv;
424}
425
[email protected]8cd06c02014-01-25 07:50:14426void HttpNetworkTransaction::StopCaching() {}
427
sclittle4de1bab92015-09-22 21:28:24428int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
429 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19430 if (stream_)
431 total_received_bytes += stream_->GetTotalReceivedBytes();
432 return total_received_bytes;
433}
434
sclittlefb249892015-09-10 21:33:22435int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
436 int64_t total_sent_bytes = total_sent_bytes_;
437 if (stream_)
438 total_sent_bytes += stream_->GetTotalSentBytes();
439 return total_sent_bytes;
440}
441
[email protected]8cd06c02014-01-25 07:50:14442void HttpNetworkTransaction::DoneReading() {}
443
[email protected]96d570e42008-08-05 22:43:04444const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55445 return &response_;
[email protected]96d570e42008-08-05 22:43:04446}
447
448LoadState HttpNetworkTransaction::GetLoadState() const {
449 // TODO(wtc): Define a new LoadState value for the
450 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
451 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48452 case STATE_CREATE_STREAM:
453 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50454 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38455 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15456 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
457 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57458 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04459 return LOAD_STATE_SENDING_REQUEST;
460 case STATE_READ_HEADERS_COMPLETE:
461 return LOAD_STATE_WAITING_FOR_RESPONSE;
462 case STATE_READ_BODY_COMPLETE:
463 return LOAD_STATE_READING_RESPONSE;
464 default:
465 return LOAD_STATE_IDLE;
466 }
467}
468
[email protected]8cd06c02014-01-25 07:50:14469void HttpNetworkTransaction::SetQuicServerInfo(
470 QuicServerInfo* quic_server_info) {}
471
[email protected]5033ab82013-03-22 20:17:46472bool HttpNetworkTransaction::GetLoadTimingInfo(
473 LoadTimingInfo* load_timing_info) const {
474 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
475 return false;
476
477 load_timing_info->proxy_resolve_start =
478 proxy_info_.proxy_resolve_start_time();
479 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
480 load_timing_info->send_start = send_start_time_;
481 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46482 return true;
483}
484
ttuttled9dbc652015-09-29 20:00:59485bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36486 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59487 return false;
488
489 *endpoint = remote_endpoint_;
490 return true;
491}
492
zhongyi48704c182015-12-07 07:52:02493void HttpNetworkTransaction::PopulateNetErrorDetails(
494 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12495 *details = net_error_details_;
496 if (stream_)
497 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02498}
499
[email protected]5033ab82013-03-22 20:17:46500void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
501 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50502
[email protected]bf828982013-08-14 18:01:47503 if (stream_request_)
504 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03505 if (stream_)
506 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50507
rdsmith1d343be52016-10-21 20:37:50508 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46509}
510
[email protected]831e4a32013-11-14 02:14:44511void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
512 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
513 websocket_handshake_stream_base_create_helper_ = create_helper;
514}
515
[email protected]1826a402014-01-08 15:40:48516void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
Will Cassella61125f92020-07-25 09:11:37517 BeforeNetworkStartCallback callback) {
518 before_network_start_callback_ = std::move(callback);
[email protected]1826a402014-01-08 15:40:48519}
520
Titouan Rigoudy78af7da2020-07-07 14:30:12521void HttpNetworkTransaction::SetConnectedCallback(
522 const ConnectedCallback& callback) {
523 connected_callback_ = callback;
524}
525
Andrey Kosyakov83a6eee2017-08-14 19:20:04526void HttpNetworkTransaction::SetRequestHeadersCallback(
527 RequestHeadersCallback callback) {
528 DCHECK(!stream_);
529 request_headers_callback_ = std::move(callback);
530}
531
Andrey Kosyakov2e893e62017-08-31 17:00:52532void HttpNetworkTransaction::SetResponseHeadersCallback(
533 ResponseHeadersCallback callback) {
534 DCHECK(!stream_);
535 response_headers_callback_ = std::move(callback);
536}
537
[email protected]1826a402014-01-08 15:40:48538int HttpNetworkTransaction::ResumeNetworkStart() {
539 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
540 return DoLoop(OK);
541}
542
[email protected]102e27c2011-02-23 01:01:31543void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
544 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00545 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50546 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38547 DCHECK(stream_request_.get());
548
sclittlefb249892015-09-10 21:33:22549 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19550 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22551 total_sent_bytes_ += stream_->GetTotalSentBytes();
552 }
bnc5029f4632017-06-08 16:19:00553 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04554 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14555 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31556 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52557 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21558 response_.alpn_negotiated_protocol =
559 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38560 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22561 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38562 OnIOComplete(OK);
563}
564
xunjieli5749218c2016-03-22 16:43:06565void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08566 const SSLConfig& used_ssl_config,
567 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00568 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08569 NOTREACHED();
570}
571
[email protected]a9cf2b92013-10-30 12:08:49572void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50573 const SSLConfig& used_ssl_config,
574 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00575 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
576 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50577}
578
Ryan Hamilton75f197262017-08-17 14:00:07579void HttpNetworkTransaction::OnStreamFailed(
580 int result,
581 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22582 const SSLConfig& used_ssl_config,
dalyk6d7a8c52019-12-18 21:43:01583 const ProxyInfo& used_proxy_info,
584 ResolveErrorInfo resolve_error_info) {
[email protected]82918cc2010-08-25 17:24:50585 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38586 DCHECK_NE(OK, result);
587 DCHECK(stream_request_.get());
588 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14589 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07590 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22591 proxy_info_ = used_proxy_info;
592 SetProxyInfoInReponse(used_proxy_info, &response_);
dalyk6d7a8c52019-12-18 21:43:01593 response_.resolve_error_info = resolve_error_info;
[email protected]8e6441ca2010-08-19 05:56:38594
595 OnIOComplete(result);
596}
597
[email protected]102e27c2011-02-23 01:01:31598void HttpNetworkTransaction::OnCertificateError(
599 int result,
600 const SSLConfig& used_ssl_config,
601 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50602 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38603 DCHECK_NE(OK, result);
604 DCHECK(stream_request_.get());
605 DCHECK(!stream_.get());
606
607 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14608 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38609
610 // TODO(mbelshe): For now, we're going to pass the error through, and that
611 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50612 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
613 // good and the user chooses to ignore the error. This is not ideal, but not
614 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38615
616 OnIOComplete(result);
617}
618
619void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50620 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31621 const SSLConfig& used_ssl_config,
622 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50623 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38624 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50625 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38626
627 establishing_tunnel_ = true;
628 response_.headers = proxy_response.headers;
629 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50630 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20631
632 if (response_.headers.get() && !ContentEncodingsValid()) {
633 DoCallback(ERR_CONTENT_DECODING_FAILED);
634 return;
635 }
636
[email protected]8e6441ca2010-08-19 05:56:38637 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14638 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31639 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38640
641 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
642 pending_auth_target_ = HttpAuth::AUTH_PROXY;
643
644 DoCallback(OK);
645}
646
647void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31648 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50649 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50650 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38651
[email protected]102957f2011-09-02 17:10:14652 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38653 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58654 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38655}
656
zhongyi48704c182015-12-07 07:52:02657void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12658 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02659}
660
ttuttle1f2d7e92015-04-28 16:17:47661void HttpNetworkTransaction::GetConnectionAttempts(
662 ConnectionAttempts* out) const {
663 *out = connection_attempts_;
664}
665
Adam Ricecb76ac62015-02-20 05:33:25666bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37667 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52668}
669
Adam Rice425cf122015-01-19 06:18:24670bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
Eric Romandbf7441f2020-07-28 07:05:02671 return proxy_info_.is_http_like() &&
Adam Rice425cf122015-01-19 06:18:24672 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
673}
674
initial.commit586acc5fe2008-07-26 22:42:52675void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50676 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41677 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52678
Douglas Creageref5eecdc2018-11-09 20:50:36679#if BUILDFLAG(ENABLE_REPORTING)
680 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42681 // report about this network request if the result was an error.
682 GenerateNetworkErrorLoggingReportIfError(rv);
683#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36684
[email protected]96d570e42008-08-05 22:43:04685 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27686 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52687}
688
689void HttpNetworkTransaction::OnIOComplete(int result) {
690 int rv = DoLoop(result);
691 if (rv != ERR_IO_PENDING)
692 DoCallback(rv);
693}
694
695int HttpNetworkTransaction::DoLoop(int result) {
696 DCHECK(next_state_ != STATE_NONE);
697
698 int rv = result;
699 do {
700 State state = next_state_;
701 next_state_ = STATE_NONE;
702 switch (state) {
[email protected]1826a402014-01-08 15:40:48703 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
704 DCHECK_EQ(OK, rv);
705 rv = DoNotifyBeforeCreateStream();
706 break;
[email protected]82918cc2010-08-25 17:24:50707 case STATE_CREATE_STREAM:
708 DCHECK_EQ(OK, rv);
709 rv = DoCreateStream();
710 break;
711 case STATE_CREATE_STREAM_COMPLETE:
712 rv = DoCreateStreamComplete(rv);
[email protected]82918cc2010-08-25 17:24:50713 break;
[email protected]351ab642010-08-05 16:55:31714 case STATE_INIT_STREAM:
715 DCHECK_EQ(OK, rv);
716 rv = DoInitStream();
717 break;
718 case STATE_INIT_STREAM_COMPLETE:
719 rv = DoInitStreamComplete(rv);
720 break;
[email protected]044de0642010-06-17 10:42:15721 case STATE_GENERATE_PROXY_AUTH_TOKEN:
722 DCHECK_EQ(OK, rv);
723 rv = DoGenerateProxyAuthToken();
724 break;
725 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
726 rv = DoGenerateProxyAuthTokenComplete(rv);
727 break;
728 case STATE_GENERATE_SERVER_AUTH_TOKEN:
729 DCHECK_EQ(OK, rv);
730 rv = DoGenerateServerAuthToken();
731 break;
732 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
733 rv = DoGenerateServerAuthTokenComplete(rv);
734 break;
[email protected]daddea62012-09-19 05:51:13735 case STATE_INIT_REQUEST_BODY:
736 DCHECK_EQ(OK, rv);
737 rv = DoInitRequestBody();
738 break;
739 case STATE_INIT_REQUEST_BODY_COMPLETE:
740 rv = DoInitRequestBodyComplete(rv);
741 break;
[email protected]4875ba12011-03-30 22:31:51742 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55743 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00744 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51745 rv = DoBuildRequest();
746 break;
747 case STATE_BUILD_REQUEST_COMPLETE:
748 rv = DoBuildRequestComplete(rv);
749 break;
750 case STATE_SEND_REQUEST:
751 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57752 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52753 break;
[email protected]0877e3d2009-10-17 22:29:57754 case STATE_SEND_REQUEST_COMPLETE:
755 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43756 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00757 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52758 break;
759 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55760 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00761 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52762 rv = DoReadHeaders();
763 break;
764 case STATE_READ_HEADERS_COMPLETE:
765 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43766 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00767 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52768 break;
769 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55770 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00771 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52772 rv = DoReadBody();
773 break;
774 case STATE_READ_BODY_COMPLETE:
775 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43776 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00777 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52778 break;
[email protected]2d2697f92009-02-18 21:00:32779 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55780 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53781 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00782 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32783 rv = DoDrainBodyForAuthRestart();
784 break;
785 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
786 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32789 break;
initial.commit586acc5fe2008-07-26 22:42:52790 default:
791 NOTREACHED() << "bad state";
792 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 }
795 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
796
797 return rv;
798}
799
[email protected]1826a402014-01-08 15:40:48800int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
801 next_state_ = STATE_CREATE_STREAM;
802 bool defer = false;
803 if (!before_network_start_callback_.is_null())
Will Cassella61125f92020-07-25 09:11:37804 std::move(before_network_start_callback_).Run(&defer);
[email protected]1826a402014-01-08 15:40:48805 if (!defer)
806 return OK;
807 return ERR_IO_PENDING;
808}
809
[email protected]82918cc2010-08-25 17:24:50810int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55811 response_.network_accessed = true;
812
[email protected]82918cc2010-08-25 17:24:50813 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17814 // IP based pooling is only enabled on a retry after 421 Misdirected Request
815 // is received. Alternative Services are also disabled in this case (though
816 // they can also be disabled when retrying after a QUIC error).
817 if (!enable_ip_based_pooling_)
818 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44819 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27820 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06821 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
822 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
823 websocket_handshake_stream_base_create_helper_,
824 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44825 } else {
xunjieli96f2a402017-06-05 17:24:27826 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29827 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27828 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44829 }
[email protected]26816882010-10-14 18:03:09830 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38831 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31832}
833
[email protected]82918cc2010-08-25 17:24:50834int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59835 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59836 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50837 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38838 DCHECK(stream_.get());
bncfacdd852015-01-09 19:22:54839 } else if (result == ERR_HTTP_1_1_REQUIRED ||
840 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
841 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59842 }
843
David Benjamin76a40ad2018-02-24 22:22:08844 // Handle possible client certificate errors that may have occurred if the
845 // stream used SSL for one or more of the layers.
846 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30847
[email protected]8e6441ca2010-08-19 05:56:38848 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09849 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38850 return result;
[email protected]394816e92010-08-03 07:38:59851}
852
[email protected]82918cc2010-08-25 17:24:50853int HttpNetworkTransaction::DoInitStream() {
854 DCHECK(stream_.get());
855 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59856
857 stream_->GetRemoteEndpoint(&remote_endpoint_);
858
Steven Valdezb4ff0412018-01-18 22:39:27859 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
860 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50861}
862
863int HttpNetworkTransaction::DoInitStreamComplete(int result) {
Titouan Rigoudy78af7da2020-07-07 14:30:12864 if (result != OK) {
[email protected]82918cc2010-08-25 17:24:50865 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26866 result = HandleIOError(result);
867
868 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22869 if (stream_) {
870 total_received_bytes_ += stream_->GetTotalReceivedBytes();
871 total_sent_bytes_ += stream_->GetTotalSentBytes();
872 }
zhongyica364fbb2015-12-12 03:39:12873 CacheNetErrorDetailsAndResetStream();
Titouan Rigoudy78af7da2020-07-07 14:30:12874
875 return result;
[email protected]82918cc2010-08-25 17:24:50876 }
877
Titouan Rigoudy78af7da2020-07-07 14:30:12878 // Fire off notification that we have successfully connected.
879 if (!connected_callback_.is_null()) {
Titouan Rigoudyba507a882020-07-31 12:15:15880 TransportType type = TransportType::kDirect;
881 if (!proxy_info_.is_direct()) {
882 type = TransportType::kProxied;
883 }
884 result = connected_callback_.Run(TransportInfo(type, remote_endpoint_));
Titouan Rigoudy78af7da2020-07-07 14:30:12885 DCHECK_NE(result, ERR_IO_PENDING);
886 }
887
888 if (result == OK) {
889 // Only transition if we succeeded. Otherwise stop at STATE_NONE.
890 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
891 }
[email protected]82918cc2010-08-25 17:24:50892 return result;
893}
894
[email protected]044de0642010-06-17 10:42:15895int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
896 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
897 if (!ShouldApplyProxyAuth())
898 return OK;
[email protected]394816e92010-08-03 07:38:59899 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
900 if (!auth_controllers_[target].get())
Matt Menkebe090422019-10-18 20:25:26901 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
902 target, AuthURL(target), request_->network_isolation_key,
903 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27904 session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59905 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41906 io_callback_,
[email protected]394816e92010-08-03 07:38:59907 net_log_);
[email protected]044de0642010-06-17 10:42:15908}
909
910int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
911 DCHECK_NE(ERR_IO_PENDING, rv);
912 if (rv == OK)
913 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
914 return rv;
915}
916
917int HttpNetworkTransaction::DoGenerateServerAuthToken() {
918 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59919 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54920 if (!auth_controllers_[target].get()) {
Matt Menkebe090422019-10-18 20:25:26921 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
922 target, AuthURL(target), request_->network_isolation_key,
923 session_->http_auth_cache(), session_->http_auth_handler_factory(),
Rohit Agarwal2653f472019-11-12 19:39:27924 session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54925 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
926 auth_controllers_[target]->DisableEmbeddedIdentity();
927 }
[email protected]044de0642010-06-17 10:42:15928 if (!ShouldApplyServerAuth())
929 return OK;
[email protected]394816e92010-08-03 07:38:59930 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41931 io_callback_,
[email protected]394816e92010-08-03 07:38:59932 net_log_);
[email protected]044de0642010-06-17 10:42:15933}
934
935int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
936 DCHECK_NE(ERR_IO_PENDING, rv);
937 if (rv == OK)
nharperd6e65822016-03-30 23:05:48938 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14939 return rv;
940}
941
942int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24943 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14944 request_headers_.SetHeader(HttpRequestHeaders::kHost,
945 GetHostAndOptionalPort(request_->url));
946
947 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24948 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14949 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
950 "keep-alive");
951 } else {
952 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
953 }
954
[email protected]2979a492011-04-06 00:29:14955 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11956 if (request_->upload_data_stream) {
957 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14958 request_headers_.SetHeader(
959 HttpRequestHeaders::kTransferEncoding, "chunked");
960 } else {
961 request_headers_.SetHeader(
962 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09963 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14964 }
csharrisonf473dd192015-08-18 13:54:13965 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14966 // An empty POST/PUT request still needs a content length. As for HEAD,
967 // IE and Safari also add a content length header. Presumably it is to
968 // support sending a HEAD request to an URL that only expects to be sent a
969 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13970 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
971 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14972 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
973 }
974
[email protected]2979a492011-04-06 00:29:14975 // Honor load flags that impact proxy caches.
976 if (request_->load_flags & LOAD_BYPASS_CACHE) {
977 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
978 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
979 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
980 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
981 }
982
983 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
984 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
985 &request_headers_);
986 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
987 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
988 &request_headers_);
989
[email protected]c10450102011-06-27 09:06:16990 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20991
[email protected]173f8e22013-04-10 04:18:20992 response_.did_use_http_auth =
993 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
994 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14995 return OK;
996}
997
[email protected]daddea62012-09-19 05:51:13998int HttpNetworkTransaction::DoInitRequestBody() {
999 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131000 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111001 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331002 rv = request_->upload_data_stream->Init(
1003 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1004 base::Unretained(this)),
1005 net_log_);
[email protected]daddea62012-09-19 05:51:131006 return rv;
1007}
[email protected]4875ba12011-03-30 22:31:511008
[email protected]daddea62012-09-19 05:51:131009int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1010 if (result == OK)
1011 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131012 return result;
1013}
1014
1015int HttpNetworkTransaction::DoBuildRequest() {
1016 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511017 headers_valid_ = false;
1018
1019 // This is constructed lazily (instead of within our Start method), so that
1020 // we have proxy info available.
1021 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241022 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141023 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511024 }
1025
[email protected]4875ba12011-03-30 22:31:511026 return OK;
1027}
1028
1029int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241030 if (result == OK)
1031 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511032 return result;
1033}
1034
[email protected]0877e3d2009-10-17 22:29:571035int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251036 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571037 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1038
[email protected]bf3eb002012-11-15 05:50:111039 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521040}
1041
[email protected]0877e3d2009-10-17 22:29:571042int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251043 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361044
1045 if (result == ERR_HTTP_1_1_REQUIRED ||
1046 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1047 return HandleHttp11Required(result);
1048 }
1049
initial.commit586acc5fe2008-07-26 22:42:521050 if (result < 0)
1051 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571052 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521053 return OK;
1054}
1055
1056int HttpNetworkTransaction::DoReadHeaders() {
1057 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411058 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521059}
1060
1061int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111062 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1063 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381064 if (IsCertificateError(result)) {
1065 // We don't handle a certificate error during SSL renegotiation, so we
1066 // have to return an error that's not in the certificate error range
1067 // (-2xx).
David Benjaminbac8dff2019-08-07 01:30:411068 //
1069 // TODO(davidben): Remove this error. This is impossible now that server
1070 // certificates are forbidden from changing in renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381071 LOG(ERROR) << "Got a server certificate with error " << result
1072 << " during SSL renegotiation";
1073 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1074 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381075 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251076 DCHECK(IsSecureRequest());
David Benjamin1c4b6d012019-07-08 17:12:571077 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]90499482013-06-01 00:39:501078 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
David Benjaminbac8dff2019-08-07 01:30:411079 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1080 total_sent_bytes_ += stream_->GetTotalSentBytes();
1081 stream_->Close(true);
1082 CacheNetErrorDetailsAndResetStream();
[email protected]2181ea002009-06-09 01:37:271083 }
1084
bncfacdd852015-01-09 19:22:541085 if (result == ERR_HTTP_1_1_REQUIRED ||
1086 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1087 return HandleHttp11Required(result);
1088 }
1089
[email protected]c871864d72014-03-13 19:56:191090 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1091 // response headers were received, we do the best we can to make sense of it
1092 // and send it back up the stack.
1093 //
1094 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1095 // bizarre for SPDY. Assuming this logic is useful at all.
1096 // TODO(davidben): Bubble the error code up so we do not cache?
1097 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1098 result = OK;
1099
1100 if (result < 0)
1101 return HandleIOError(result);
1102
[email protected]90499482013-06-01 00:39:501103 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521104
eustasc7d27da2017-04-06 10:33:201105 if (response_.headers.get() && !ContentEncodingsValid())
1106 return ERR_CONTENT_DECODING_FAILED;
1107
[email protected]d58ceea82014-06-04 10:55:541108 // On a 408 response from the server ("Request Timeout") on a stale socket,
David Schinazif832cb82019-11-08 22:25:271109 // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
1110 // multiplex requests and have no need for 408.
[email protected]0aff0d82014-06-14 08:49:041111 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181112 if (response_.headers.get() &&
1113 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
David Schinazif832cb82019-11-08 22:25:271114 HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
1115 HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
[email protected]d58ceea82014-06-04 10:55:541116 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421117#if BUILDFLAG(ENABLE_REPORTING)
1118 GenerateNetworkErrorLoggingReport(OK);
1119#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541120 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001121 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541122 response_.headers->response_code());
1123 // This will close the socket - it would be weird to try and reuse it, even
1124 // if the server doesn't actually close it.
1125 ResetConnectionAndRequestForResend();
1126 return OK;
1127 }
1128
Eric Roman06bd9742019-07-13 15:19:131129 NetLogResponseHeaders(net_log_,
1130 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1131 response_.headers.get());
Andrey Kosyakov2e893e62017-08-31 17:00:521132 if (response_headers_callback_)
1133 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391134
bncbe0f6af2015-10-15 17:49:561135 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571136 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1137 // indicates a buggy server. See:
1138 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1139 if (request_->method == "PUT")
1140 return ERR_METHOD_NOT_SUPPORTED;
1141 }
[email protected]4ddaf2502008-10-23 18:26:191142
Steven Valdez58097ec32018-07-16 18:29:041143 if (can_send_early_data_ && response_.headers.get() &&
1144 response_.headers->response_code() == HTTP_TOO_EARLY) {
1145 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1146 }
1147
[email protected]0877e3d2009-10-17 22:29:571148 // Check for an intermediate 100 Continue response. An origin server is
1149 // allowed to send this response even if we didn't ask for it, so we just
1150 // need to skip over it.
1151 // We treat any other 1xx in this same way (although in practice getting
1152 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441153 // Unless this is a WebSocket request, in which case we pass it on up.
1154 if (response_.headers->response_code() / 100 == 1 &&
1155 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451156 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571157 next_state_ = STATE_READ_HEADERS;
1158 return OK;
1159 }
1160
davidbence688ae2017-05-04 15:12:591161 if (response_.headers->response_code() == 421 &&
1162 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421163#if BUILDFLAG(ENABLE_REPORTING)
1164 GenerateNetworkErrorLoggingReport(OK);
1165#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591166 // Retry the request with both IP based pooling and Alternative Services
1167 // disabled.
1168 enable_ip_based_pooling_ = false;
1169 enable_alternative_services_ = false;
1170 net_log_.AddEvent(
1171 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1172 ResetConnectionAndRequestForResend();
1173 return OK;
bnc8016c1f2017-03-31 02:11:291174 }
1175
bncb26024382016-06-29 02:39:451176 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491177 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081178 if (response_.ssl_info.is_valid() &&
1179 !IsCertStatusError(response_.ssl_info.cert_status)) {
1180 session_->http_stream_factory()->ProcessAlternativeServices(
Matt Menkeb32ba5122019-09-10 19:17:051181 session_, network_isolation_key_, response_.headers.get(),
Bence Béky230ac612017-08-30 19:17:081182 url::SchemeHostPort(request_->url));
1183 }
1184 }
asanka5ffd5d72016-03-23 16:20:491185
[email protected]e772db3f2010-07-12 18:11:131186 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571187 if (rv != OK)
1188 return rv;
1189
Douglas Creager3cb042052018-11-06 23:08:521190#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421191 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1192 // reports generated before the NEL header is processed here will just be
1193 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141194 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521195 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421196
1197 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111198 // code), or if the response body will not be read, or on a redirect.
1199 // Note: This will report a success for a redirect even if an error is
1200 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421201 int response_code = response_.headers->response_code();
1202 if ((response_code >= 400 && response_code < 600) ||
1203 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1204 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111205 response_.headers->GetContentLength() == 0 ||
1206 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421207 GenerateNetworkErrorLoggingReport(OK);
1208 }
1209#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521210
[email protected]0877e3d2009-10-17 22:29:571211 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261212
1213 // We have reached the end of Start state machine, set the RequestInfo to
1214 // null.
1215 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1216 // only until the final response headers are received. Clearing it will ensure
1217 // that HttpRequestInfo is only used up until final response headers are
1218 // received. Clearing is allowed so that the transaction can be disassociated
1219 // from its creating consumer in cases where it is shared for writing to the
1220 // cache. It is also safe to set it to null at this point since
1221 // upload_data_stream is also not used in the Read state machine.
1222 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1223 request_ = nullptr;
1224
[email protected]0877e3d2009-10-17 22:29:571225 return OK;
initial.commit586acc5fe2008-07-26 22:42:521226}
1227
1228int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501229 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131230 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351231 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521232
1233 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501234 return stream_->ReadResponseBody(
1235 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521236}
1237
1238int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1239 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381240 bool done = false;
1241 if (result <= 0) {
1242 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521243 done = true;
[email protected]8e6441ca2010-08-19 05:56:381244 }
[email protected]9492e4a2010-02-24 00:58:461245
mmenkebd84c392015-09-02 14:12:341246 // Clean up connection if we are done.
1247 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381248 // Note: Just because IsResponseBodyComplete is true, we're not
1249 // necessarily "done". We're only "done" when it is the last
1250 // read on this HttpNetworkTransaction, which will be signified
1251 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341252 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381253 // the stream. No need to compute it here just to pass back
1254 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341255 bool keep_alive =
1256 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521257
[email protected]8e6441ca2010-08-19 05:56:381258 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011259 // Note: we don't reset the stream here. We've closed it, but we still
1260 // need it around so that callers can call methods such as
1261 // GetUploadProgress() and have them be meaningful.
1262 // TODO(mbelshe): This means we closed the stream here, and we close it
1263 // again in ~HttpNetworkTransaction. Clean that up.
1264
[email protected]8e6441ca2010-08-19 05:56:381265 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171266
1267 // This transaction was successful. If it had been retried because of an
1268 // error with an alternative service, mark that alternative service broken.
1269 if (!enable_alternative_services_ &&
1270 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411271 HistogramBrokenAlternateProtocolLocation(
1272 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171273 session_->http_server_properties()->MarkAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051274 retried_alternative_service_, network_isolation_key_);
rch2f2991c2017-04-13 19:28:171275 }
Lily Chenfec60d92019-01-24 01:16:421276
1277#if BUILDFLAG(ENABLE_REPORTING)
1278 GenerateNetworkErrorLoggingReport(result);
1279#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521280 }
1281
1282 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351283 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521284 read_buf_len_ = 0;
1285
1286 return result;
1287}
1288
[email protected]2d2697f92009-02-18 21:00:321289int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1290 // This method differs from DoReadBody only in the next_state_. So we just
1291 // call DoReadBody and override the next_state_. Perhaps there is a more
1292 // elegant way for these two methods to share code.
1293 int rv = DoReadBody();
1294 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1295 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1296 return rv;
1297}
1298
[email protected]0877e3d2009-10-17 22:29:571299// TODO(wtc): This method and the DoReadBodyComplete method are almost
1300// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321301int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231302 // keep_alive defaults to true because the very reason we're draining the
1303 // response body is to reuse the connection for auth restart.
1304 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321305 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571306 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421307 // Note: No Network Error Logging report is generated here because a report
1308 // will have already been generated for the original request due to the auth
1309 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321310 done = true;
[email protected]68873ba2009-06-04 21:49:231311 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311312 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571313 done = true;
[email protected]2d2697f92009-02-18 21:00:321314 }
1315
1316 if (done) {
1317 DidDrainBodyForAuthRestart(keep_alive);
1318 } else {
1319 // Keep draining.
1320 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1321 }
1322
1323 return OK;
1324}
1325
Douglas Creager3cb042052018-11-06 23:08:521326#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141327void HttpNetworkTransaction::ProcessReportToHeader() {
1328 std::string value;
1329 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1330 return;
1331
1332 ReportingService* service = session_->reporting_service();
Lily Chen9364a4c2020-06-25 16:07:521333 if (!service)
Douglas Creager134b52e2018-11-09 18:00:141334 return;
Douglas Creager134b52e2018-11-09 18:00:141335
1336 // Only accept Report-To headers on HTTPS connections that have no
1337 // certificate errors.
Lily Chen9364a4c2020-06-25 16:07:521338 if (!response_.ssl_info.is_valid())
Douglas Creager134b52e2018-11-09 18:00:141339 return;
Lily Chen9364a4c2020-06-25 16:07:521340 if (IsCertStatusError(response_.ssl_info.cert_status))
Douglas Creager134b52e2018-11-09 18:00:141341 return;
Douglas Creager134b52e2018-11-09 18:00:141342
1343 service->ProcessHeader(url_.GetOrigin(), value);
1344}
1345
Douglas Creager3cb042052018-11-06 23:08:521346void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1347 std::string value;
1348 if (!response_.headers->GetNormalizedHeader(
1349 NetworkErrorLoggingService::kHeaderName, &value)) {
1350 return;
1351 }
1352
1353 NetworkErrorLoggingService* service =
1354 session_->network_error_logging_service();
Lily Chena9e9d6cb2020-06-25 17:34:491355 if (!service)
Douglas Creager3cb042052018-11-06 23:08:521356 return;
Douglas Creager3cb042052018-11-06 23:08:521357
Lily Chen90ae93cc2019-02-14 01:15:391358 // Don't accept NEL headers received via a proxy, because the IP address of
1359 // the destination server is not known.
1360 if (response_.was_fetched_via_proxy)
1361 return;
1362
Douglas Creager3cb042052018-11-06 23:08:521363 // Only accept NEL headers on HTTPS connections that have no certificate
1364 // errors.
Lily Chena9e9d6cb2020-06-25 17:34:491365 if (!response_.ssl_info.is_valid() ||
1366 IsCertStatusError(response_.ssl_info.cert_status)) {
Douglas Creager3cb042052018-11-06 23:08:521367 return;
1368 }
1369
Lily Chena9e9d6cb2020-06-25 17:34:491370 if (remote_endpoint_.address().empty())
Douglas Creager3cb042052018-11-06 23:08:521371 return;
Douglas Creager3cb042052018-11-06 23:08:521372
1373 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1374 value);
1375}
Douglas Creageref5eecdc2018-11-09 20:50:361376
Lily Chenfec60d92019-01-24 01:16:421377void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1378 if (rv < 0 && rv != ERR_IO_PENDING)
1379 GenerateNetworkErrorLoggingReport(rv);
1380}
1381
Douglas Creageref5eecdc2018-11-09 20:50:361382void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421383 // |rv| should be a valid net::Error
1384 DCHECK_NE(rv, ERR_IO_PENDING);
1385 DCHECK_LE(rv, 0);
1386
1387 if (network_error_logging_report_generated_)
1388 return;
1389 network_error_logging_report_generated_ = true;
1390
Douglas Creageref5eecdc2018-11-09 20:50:361391 NetworkErrorLoggingService* service =
1392 session_->network_error_logging_service();
Lily Chend613caa2019-11-25 20:46:331393 if (!service)
Douglas Creageref5eecdc2018-11-09 20:50:361394 return;
Douglas Creageref5eecdc2018-11-09 20:50:361395
Lily Chen90ae93cc2019-02-14 01:15:391396 // Don't report on proxy auth challenges.
1397 if (response_.headers && response_.headers->response_code() ==
1398 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1399 return;
1400 }
1401
1402 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1403 // internal network details.
1404 if (response_.was_fetched_via_proxy)
1405 return;
1406
Lily Chen00196ab62018-12-04 19:52:291407 // Ignore errors from non-HTTPS origins.
Lily Chend613caa2019-11-25 20:46:331408 if (!url_.SchemeIsCryptographic())
Lily Chen00196ab62018-12-04 19:52:291409 return;
Douglas Creageref5eecdc2018-11-09 20:50:361410
1411 NetworkErrorLoggingService::RequestDetails details;
1412
1413 details.uri = url_;
1414 if (!request_referrer_.empty())
1415 details.referrer = GURL(request_referrer_);
1416 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421417 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361418 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421419 } else {
1420 details.server_ip = IPAddress();
1421 }
Douglas Creageref5eecdc2018-11-09 20:50:361422 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1423 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421424 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361425 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421426 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361427 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421428 }
Douglas Creageref5eecdc2018-11-09 20:50:361429 // If we got response headers, assume that the connection used HTTP/1.1
1430 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421431 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361432 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421433 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361434 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361435 }
Lily Chenfec60d92019-01-24 01:16:421436 details.method = request_method_;
1437 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361438 details.type = static_cast<Error>(rv);
1439 details.reporting_upload_depth = request_reporting_upload_depth_;
1440
1441 service->OnRequest(std::move(details));
1442}
Lily Chenfec60d92019-01-24 01:16:421443#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521444
bncfacdd852015-01-09 19:22:541445int HttpNetworkTransaction::HandleHttp11Required(int error) {
1446 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1447 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1448
Matt Menke0e3276862019-08-23 00:21:341449 // HttpServerProperties should have been updated, so when the request is sent
1450 // again, it will automatically use HTTP/1.1.
bncfacdd852015-01-09 19:22:541451 ResetConnectionAndRequestForResend();
1452 return OK;
1453}
1454
David Benjamin76a40ad2018-02-24 22:22:081455int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjaminbac8dff2019-08-07 01:30:411456 // Client certificate errors may come from either the origin server or the
1457 // proxy.
1458 //
1459 // Origin errors are handled here, while most proxy errors are handled in the
1460 // HttpStreamFactory and below. However, if the request is not tunneled (i.e.
1461 // the origin is HTTP, so there is no HTTPS connection) and the proxy does not
1462 // report a bad client certificate until after the TLS handshake completes.
1463 // The latter occurs in TLS 1.3 or TLS 1.2 with False Start (disabled for
1464 // proxies). The error will then surface out of Read() rather than Connect()
1465 // and ultimately surfaced out of DoReadHeadersComplete().
David Benjamin3b94b0f2019-04-25 23:07:521466 //
1467 // See https://crbug.com/828965.
David Benjaminbac8dff2019-08-07 01:30:411468 bool is_server = !UsingHttpProxyWithoutTunnel();
1469 HostPortPair host_port_pair =
1470 is_server ? HostPortPair::FromURL(request_->url)
1471 : proxy_info_.proxy_server().host_port_pair();
David Benjamin3b94b0f2019-04-25 23:07:521472
David Benjaminbac8dff2019-08-07 01:30:411473 if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
Eric Romandbf7441f2020-07-28 07:05:021474 DCHECK((is_server && IsSecureRequest()) ||
1475 proxy_info_.is_secure_http_like());
David Benjaminbac8dff2019-08-07 01:30:411476 if (session_->ssl_client_context()->ClearClientCertificate(
1477 host_port_pair)) {
1478 // The private key handle may have gone stale due to, e.g., the user
1479 // unplugging their smartcard. Operating systems do not provide reliable
1480 // notifications for this, so if the signature failed and the user was
1481 // not already prompted for certificate on this request, retry to ask
1482 // the user for a new one.
1483 //
1484 // TODO(davidben): There is no corresponding feature for proxy client
1485 // certificates. Ideally this would live at a lower level, common to both,
1486 // but |configured_client_cert_for_server_| is not accessible below the
1487 // socket pools.
1488 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1489 !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
1490 retry_attempts_++;
1491 net_log_.AddEventWithNetErrorCode(
1492 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1493 ResetConnectionAndRequestForResend();
1494 return OK;
1495 }
David Benjamin76a40ad2018-02-24 22:22:081496 }
1497 }
initial.commit586acc5fe2008-07-26 22:42:521498 return error;
1499}
1500
[email protected]bd0b6772011-01-11 19:59:301501// This method determines whether it is safe to resend the request after an
1502// IO error. It can only be called in response to request header or body
1503// write errors or response header read errors. It should not be used in
1504// other cases, such as a Connect error.
1505int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031506 // Because the peer may request renegotiation with client authentication at
1507 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081508 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301509
Lily Chenfec60d92019-01-24 01:16:421510#if BUILDFLAG(ENABLE_REPORTING)
1511 GenerateNetworkErrorLoggingReportIfError(error);
1512#endif // BUILDFLAG(ENABLE_REPORTING)
1513
[email protected]bd0b6772011-01-11 19:59:301514 switch (error) {
1515 // If we try to reuse a connection that the server is in the process of
1516 // closing, we may end up successfully writing out our request (or a
1517 // portion of our request) only to find a connection error when we try to
1518 // read from (or finish writing to) the socket.
1519 case ERR_CONNECTION_RESET:
1520 case ERR_CONNECTION_CLOSED:
1521 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511522 // There can be a race between the socket pool checking checking whether a
1523 // socket is still connected, receiving the FIN, and sending/reading data
1524 // on a reused socket. If we receive the FIN between the connectedness
1525 // check and writing/reading from the socket, we may first learn the socket
1526 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1527 // likely happen when trying to retrieve its IP address.
1528 // See http://crbug.com/105824 for more details.
1529 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491530 // If a socket is closed on its initial request, HttpStreamParser returns
1531 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1532 // preconnected but failed to be used before the server timed it out.
1533 case ERR_EMPTY_RESPONSE:
1534 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381535 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001536 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301537 ResetConnectionAndRequestForResend();
1538 error = OK;
1539 }
1540 break;
Steven Valdez58097ec32018-07-16 18:29:041541 case ERR_EARLY_DATA_REJECTED:
1542 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1543 net_log_.AddEventWithNetErrorCode(
1544 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1545 // Disable early data on the SSLConfig on a reset.
1546 can_send_early_data_ = false;
1547 ResetConnectionAndRequestForResend();
1548 error = OK;
1549 break;
Bence Békyd0d69502019-06-25 19:47:181550 case ERR_HTTP2_PING_FAILED:
1551 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1552 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1553 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1554 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271555 case ERR_QUIC_HANDSHAKE_FAILED:
Bence Béky2ee18922020-09-25 12:11:321556 case ERR_QUIC_GOAWAY_REQUEST_CAN_BE_RETRIED:
Biljith Jayan45a41722017-08-16 18:43:141557 if (HasExceededMaxRetries())
1558 break;
[email protected]b6754252012-06-13 23:14:381559 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001560 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141561 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001562 ResetConnectionAndRequestForResend();
1563 error = OK;
1564 break;
rch2f2991c2017-04-13 19:28:171565 case ERR_QUIC_PROTOCOL_ERROR:
1566 if (GetResponseHeaders() != nullptr ||
1567 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1568 // If the response headers have already been recieved and passed up
1569 // then the request can not be retried. Also, if there was no
1570 // alternative service used for this request, then there is no
1571 // alternative service to be disabled.
1572 break;
1573 }
Biljith Jayan45a41722017-08-16 18:43:141574 if (HasExceededMaxRetries())
1575 break;
rch2f2991c2017-04-13 19:28:171576 if (session_->http_server_properties()->IsAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051577 retried_alternative_service_, network_isolation_key_)) {
rch2f2991c2017-04-13 19:28:171578 // If the alternative service was marked as broken while the request
1579 // was in flight, retry the request which will not use the broken
1580 // alternative service.
1581 net_log_.AddEventWithNetErrorCode(
1582 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141583 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171584 ResetConnectionAndRequestForResend();
1585 error = OK;
Victor Vasilieva1e66d72019-12-05 17:55:381586 } else if (session_->context()
1587 .quic_context->params()
1588 ->retry_without_alt_svc_on_quic_errors) {
rch2f2991c2017-04-13 19:28:171589 // Disable alternative services for this request and retry it. If the
1590 // retry succeeds, then the alternative service will be marked as
1591 // broken then.
1592 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361593 net_log_.AddEventWithNetErrorCode(
1594 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141595 retry_attempts_++;
rch514a44a82017-04-13 04:46:361596 ResetConnectionAndRequestForResend();
1597 error = OK;
1598 }
1599 break;
[email protected]bd0b6772011-01-11 19:59:301600 }
1601 return error;
1602}
1603
[email protected]c3b35c22008-09-27 03:19:421604void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581605 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221606 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191607 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221608 total_sent_bytes_ += stream_->GetTotalSentBytes();
1609 }
zhongyica364fbb2015-12-12 03:39:121610 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581611}
1612
1613void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251614 send_start_time_ = base::TimeTicks();
1615 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251616
[email protected]0757e7702009-03-27 04:00:221617 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351618 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421619 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571620 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201621 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141622 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381623 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591624 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121625 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521626 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421627#if BUILDFLAG(ENABLE_REPORTING)
1628 network_error_logging_report_generated_ = false;
1629 start_timeticks_ = base::TimeTicks::Now();
1630#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121631}
1632
1633void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1634 if (stream_)
1635 stream_->PopulateNetErrorDetails(&net_error_details_);
1636 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571637}
1638
[email protected]0877e3d2009-10-17 22:29:571639HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501640 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421641}
1642
[email protected]a34f61ee2014-03-18 20:59:491643bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381644 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351645 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161646
[email protected]2a5c76b2008-09-25 22:15:161647 // NOTE: we resend a request only if we reused a keep-alive connection.
1648 // This automatically prevents an infinite resend loop because we'll run
1649 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381650 if (connection_is_proven && !has_received_headers)
1651 return true;
1652 return false;
[email protected]1c773ea12009-04-28 19:58:421653}
1654
Biljith Jayan45a41722017-08-16 18:43:141655bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1656 return (retry_attempts_ >= kMaxRetryAttempts);
1657}
1658
David Benjamin83ddfb32018-03-30 01:07:521659bool HttpNetworkTransaction::CheckMaxRestarts() {
1660 num_restarts_++;
1661 return num_restarts_ < kMaxRestarts;
1662}
1663
[email protected]1c773ea12009-04-28 19:58:421664void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381665 if (stream_.get()) {
1666 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121667 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161668 }
1669
[email protected]0877e3d2009-10-17 22:29:571670 // We need to clear request_headers_ because it contains the real request
1671 // headers, but we may need to resend the CONNECT request first to recreate
1672 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201673 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501674 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421675
1676#if BUILDFLAG(ENABLE_REPORTING)
1677 // Reset for new request.
1678 network_error_logging_report_generated_ = false;
1679 start_timeticks_ = base::TimeTicks::Now();
1680#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221681
1682 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541683}
1684
[email protected]1c773ea12009-04-28 19:58:421685bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241686 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421687}
license.botbf09a502008-08-24 00:55:551688
[email protected]1c773ea12009-04-28 19:58:421689bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
Matt Menke25eaa432020-08-25 00:10:001690 return request_->privacy_mode == PRIVACY_MODE_DISABLED;
[email protected]1c773ea12009-04-28 19:58:421691}
1692
[email protected]e772db3f2010-07-12 18:11:131693int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271694 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501695 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421696
[email protected]0877e3d2009-10-17 22:29:571697 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581698 if (status != HTTP_UNAUTHORIZED &&
1699 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421700 return OK;
[email protected]9094b602012-02-27 21:44:581701 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111702 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161703 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1704 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421705
[email protected]9094b602012-02-27 21:44:581706 // This case can trigger when an HTTPS server responds with a "Proxy
1707 // authentication required" status code through a non-authenticating
1708 // proxy.
[email protected]7a67a8152010-11-05 18:31:101709 if (!auth_controllers_[target].get())
1710 return ERR_UNEXPECTED_PROXY_AUTH;
1711
[email protected]a7ea8832010-07-12 17:54:541712 int rv = auth_controllers_[target]->HandleAuthChallenge(
Matt Menke25eaa432020-08-25 00:10:001713 headers, response_.ssl_info, !ShouldApplyServerAuth(), false, net_log_);
[email protected]228404f2010-06-24 04:31:411714 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491715 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411716
Emily Starkf2c9bbd2019-04-09 17:08:581717 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411718
[email protected]228404f2010-06-24 04:31:411719 return rv;
[email protected]f9ee6b52008-11-08 06:46:231720}
1721
[email protected]8e6441ca2010-08-19 05:56:381722bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1723 return auth_controllers_[target].get() &&
1724 auth_controllers_[target]->HaveAuth();
1725}
1726
[email protected]228404f2010-06-24 04:31:411727GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1728 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461729 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411730 if (!proxy_info_.proxy_server().is_valid() ||
1731 proxy_info_.proxy_server().is_direct()) {
1732 return GURL(); // There is no proxy server.
1733 }
Eric Romandbf7441f2020-07-28 07:05:021734 // TODO(https://crbug.com/1103768): Mapping proxy addresses to
1735 // URLs is a lossy conversion, shouldn't do this.
1736 const char* scheme =
1737 proxy_info_.is_secure_http_like() ? "https://" : "http://";
[email protected]2df19bb2010-08-25 20:13:461738 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351739 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461740 }
[email protected]228404f2010-06-24 04:31:411741 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291742 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361743 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291744 }
[email protected]228404f2010-06-24 04:31:411745 return request_->url;
1746 default:
1747 return GURL();
1748 }
[email protected]c3b35c22008-09-27 03:19:421749}
1750
[email protected]831e4a32013-11-14 02:14:441751bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141752 return websocket_handshake_stream_base_create_helper_ &&
1753 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441754}
1755
ttuttle1f2d7e92015-04-28 16:17:471756void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1757 DCHECK(stream_request_);
1758
1759 // Since the transaction can restart with auth credentials, it may create a
1760 // stream more than once. Accumulate all of the connection attempts across
1761 // those streams by appending them to the vector:
1762 for (const auto& attempt : stream_request_->connection_attempts())
1763 connection_attempts_.push_back(attempt);
1764}
1765
eustasc7d27da2017-04-06 10:33:201766bool HttpNetworkTransaction::ContentEncodingsValid() const {
1767 HttpResponseHeaders* headers = GetResponseHeaders();
1768 DCHECK(headers);
1769
1770 std::string accept_encoding;
1771 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1772 &accept_encoding);
1773 std::set<std::string> allowed_encodings;
Paul Jensene85e8152019-07-01 15:38:001774 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
eustasc7d27da2017-04-06 10:33:201775 return false;
eustasc7d27da2017-04-06 10:33:201776
1777 std::string content_encoding;
1778 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1779 std::set<std::string> used_encodings;
Paul Jensene85e8152019-07-01 15:38:001780 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
eustasc7d27da2017-04-06 10:33:201781 return false;
eustasc7d27da2017-04-06 10:33:201782
1783 // When "Accept-Encoding" is not specified, it is parsed as "*".
1784 // If "*" encoding is advertised, then any encoding should be "accepted".
1785 // This does not mean, that it will be successfully decoded.
1786 if (allowed_encodings.find("*") != allowed_encodings.end())
1787 return true;
1788
sky50576f32017-05-01 19:28:031789 bool result = true;
eustasc7d27da2017-04-06 10:33:201790 for (auto const& encoding : used_encodings) {
1791 SourceStream::SourceType source_type =
1792 FilterSourceStream::ParseEncodingType(encoding);
1793 // We don't reject encodings we are not aware. They just will not decode.
1794 if (source_type == SourceStream::TYPE_UNKNOWN)
1795 continue;
1796 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031797 result = false;
1798 break;
eustasc7d27da2017-04-06 10:33:201799 }
1800 }
sky50576f32017-05-01 19:28:031801
1802 // Temporary workaround for http://crbug.com/714514
1803 if (headers->IsRedirect(nullptr)) {
1804 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1805 !result);
1806 return true;
1807 }
1808
1809 return result;
eustasc7d27da2017-04-06 10:33:201810}
1811
[email protected]c3b35c22008-09-27 03:19:421812} // namespace net