blob: 25c2ca246df52f5fdd5305186847624296a824e0 [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"
Bence Békybc1de292018-02-01 15:48:0314#include "base/callback_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
David Benjamin5cb91132018-04-06 05:54:4918#include "base/metrics/histogram_functions.h"
asvitkinec3c93722015-06-17 14:48:3719#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4220#include "base/metrics/sparse_histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
[email protected]3d498f72013-10-28 21:17:4024#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5325#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3026#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3527#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5528#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1929#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2531#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2632#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4033#include "net/base/proxy_server.h"
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"
44#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5445#include "net/http/http_proxy_client_socket.h"
[email protected]270c6412010-03-29 22:02:4746#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5247#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2148#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5749#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5350#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5851#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3952#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3153#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5254#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2355#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3656#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0057#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4458#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2159#include "net/socket/next_proto.h"
[email protected]ab739042011-04-07 15:22:2860#include "net/socket/transport_client_socket_pool.h"
Bence Béky94658bf2018-05-11 19:22:5861#include "net/spdy/spdy_http_stream.h"
62#include "net/spdy/spdy_session.h"
63#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_cert_request_info.h"
65#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0866#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5467#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5468#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
Douglas Creager3cb042052018-11-06 23:08:5271#if BUILDFLAG(ENABLE_REPORTING)
72#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1473#include "net/reporting/reporting_header_parser.h"
74#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4275#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5276
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2277namespace net {
78
Biljith Jayan45a41722017-08-16 18:43:1479namespace {
David Benjamin83ddfb32018-03-30 01:07:5280
Biljith Jayan45a41722017-08-16 18:43:1481// Max number of |retry_attempts| (excluding the initial request) after which
82// we give up and show an error page.
83const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5284
85// Max number of calls to RestartWith* allowed for a single connection. A single
86// HttpNetworkTransaction should not signal very many restartable errors, but it
87// may occur due to a bug (e.g. https://crbug.com/823387 or
88// https://crbug.com/488043) or simply if the server or proxy requests
89// authentication repeatedly. Although these calls are often associated with a
90// user prompt, in other scenarios (remembered preferences, extensions,
91// multi-leg authentication), they may be triggered automatically. To avoid
92// looping forever, bound the number of restarts.
93const size_t kMaxRestarts = 32;
94
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2295void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
96 HttpResponseInfo* response_info) {
97 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
98 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
99 response_info->proxy_server = proxy_info.proxy_server();
100 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
101 response_info->proxy_server = ProxyServer::Direct();
102 else
103 response_info->proxy_server = ProxyServer();
104}
Biljith Jayan45a41722017-08-16 18:43:14105
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22106} // namespace
initial.commit586acc5fe2008-07-26 22:42:52107
Victor Costan9c7302b2018-08-27 16:39:44108const int HttpNetworkTransaction::kDrainBodyBufferSize;
109
[email protected]262eec82013-03-19 21:01:36110HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
111 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22112 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53113 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
114 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52115 session_(session),
Raul Tambre94493c652019-03-11 17:18:35116 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36117 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57118 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27119 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08120 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20121 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42122#if BUILDFLAG(ENABLE_REPORTING)
123 network_error_logging_report_generated_(false),
124 request_reporting_upload_depth_(0),
125#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52126 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19127 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22128 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54129 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44130 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29131 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26132 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35133 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14134 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52135 retry_attempts_(0),
David Benjamind61bd532019-04-23 21:11:37136 num_restarts_(0) {
Lily Chenfec60d92019-01-24 01:16:42137}
[email protected]3ce7df0f2010-03-03 00:30:50138
[email protected]0b0bf032010-09-21 18:08:50139HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42140#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11141 // If no error or success report has been generated yet at this point, then
142 // this network transaction was prematurely cancelled.
143 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42144#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50145 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50146 // TODO(mbelshe): The stream_ should be able to compute whether or not the
147 // stream should be kept alive. No reason to compute here
148 // and pass it in.
mmenkebd84c392015-09-02 14:12:34149 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50150 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34151 } else if (stream_->IsResponseBodyComplete()) {
152 // If the response body is complete, we can just reuse the socket.
153 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50154 } else {
mmenkebd84c392015-09-02 14:12:34155 // Otherwise, we try to drain the response body.
156 HttpStream* stream = stream_.release();
157 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50158 }
159 }
[email protected]02cad5d2013-10-02 08:14:03160 if (request_ && request_->upload_data_stream)
161 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50162}
163
[email protected]684970b2009-08-14 04:54:46164int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04165 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20166 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23167 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
168 return ERR_CACHE_MISS;
169
Ramin Halavatib5e433e2018-02-07 07:41:10170 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53171 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04172 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15173 url_ = request_->url;
Douglas Creageref5eecdc2018-11-09 20:50:36174#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42175 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36176 request_method_ = request_->method;
177 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
178 &request_referrer_);
179 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
180 &request_user_agent_);
181 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42182 start_timeticks_ = base::TimeTicks::Now();
183#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04184
nharper8cdb0fb2016-04-22 21:34:59185 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
186 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
187
Ryan Sleevi24fe2682018-08-16 21:33:46188 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
189 server_ssl_config_.disable_cert_verification_network_fetches = true;
190 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19191 }
[email protected]6fbac162011-06-20 00:29:04192
Steven Valdez03e872d2018-03-02 15:39:15193 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27194 can_send_early_data_ = true;
195 }
196
jkarlinfb1d5172015-01-12 14:10:29197 if (request_->load_flags & LOAD_PREFETCH)
198 response_.unused_since_prefetch = true;
199
Helen Lib495c3802018-03-30 13:46:09200 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04201 int rv = DoLoop(OK);
202 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04203 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42204
205 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
206 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
207 // other net::Error can be returned.
208 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04209 return rv;
210}
211
212int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04213 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38214 DCHECK(!stream_.get());
215 DCHECK(!stream_request_.get());
216 DCHECK_EQ(STATE_NONE, next_state_);
217
David Benjamin83ddfb32018-03-30 01:07:52218 if (!CheckMaxRestarts())
219 return ERR_TOO_MANY_RETRIES;
220
[email protected]82918cc2010-08-25 17:24:50221 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38222
[email protected]ccb40e52008-09-17 20:54:40223 int rv = DoLoop(OK);
224 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04225 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42226
227 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
228 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
229 // other net::Error can be returned.
230 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11231 return rv;
[email protected]96d570e42008-08-05 22:43:04232}
233
[email protected]0b45559b2009-06-12 21:45:11234int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08235 scoped_refptr<X509Certificate> client_cert,
236 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04237 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38238 // In HandleCertificateRequest(), we always tear down existing stream
239 // requests to force a new connection. So we shouldn't have one here.
240 DCHECK(!stream_request_.get());
241 DCHECK(!stream_.get());
242 DCHECK_EQ(STATE_NONE, next_state_);
243
David Benjamin83ddfb32018-03-30 01:07:52244 if (!CheckMaxRestarts())
245 return ERR_TOO_MANY_RETRIES;
246
[email protected]102957f2011-09-02 17:10:14247 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
248 &proxy_ssl_config_ : &server_ssl_config_;
249 ssl_config->send_client_cert = true;
250 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54251 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45252 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08253 response_.cert_request_info->host_and_port, std::move(client_cert),
254 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11255 // Reset the other member variables.
256 // Note: this is necessary only with SSL renegotiation.
257 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50258 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11259 int rv = DoLoop(OK);
260 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04261 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42262
263 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
264 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
265 // other net::Error can be returned.
266 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11267 return rv;
268}
269
Bence Béky046f8c82018-06-12 02:26:04270int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
271 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52272 if (!CheckMaxRestarts())
273 return ERR_TOO_MANY_RETRIES;
274
[email protected]0757e7702009-03-27 04:00:22275 HttpAuth::Target target = pending_auth_target_;
276 if (target == HttpAuth::AUTH_NONE) {
277 NOTREACHED();
278 return ERR_UNEXPECTED;
279 }
[email protected]0757e7702009-03-27 04:00:22280 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42281
[email protected]f3cf9802011-10-28 18:44:58282 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13283
[email protected]49639fa2011-12-20 23:22:41284 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38285
286 int rv = OK;
287 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
288 // In this case, we've gathered credentials for use with proxy
289 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50290 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35291 DCHECK(stream_request_ != nullptr);
292 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54293 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39294 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54295 } else {
[email protected]8e6441ca2010-08-19 05:56:38296 // In this case, we've gathered credentials for the server or the proxy
297 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35298 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54299 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38300 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42301 // Note: If an error is encountered while draining the old response body, no
302 // Network Error Logging report will be generated, because the error was
303 // with the old request, which will already have had a NEL report generated
304 // for it due to the auth challenge (so we don't report a second error for
305 // that request).
[email protected]a7ea8832010-07-12 17:54:54306 }
[email protected]c3b35c22008-09-27 03:19:42307
[email protected]c3b35c22008-09-27 03:19:42308 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04309 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42310 return rv;
[email protected]96d570e42008-08-05 22:43:04311}
312
[email protected]f9ee6b52008-11-08 06:46:23313void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
314 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38315 DCHECK(!stream_request_.get());
316
Bence Béky3238f2e12017-09-22 22:44:49317 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
318 if (target == HttpAuth::AUTH_SERVER &&
319 auth_controllers_[target]->NeedsHTTP11()) {
320 session_->http_server_properties()->SetHTTP11Required(
321 HostPortPair::FromURL(request_->url));
322 }
323
[email protected]2d2697f92009-02-18 21:00:32324 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57325 // Even if the server says the connection is keep-alive, we have to be
326 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34327 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57328 // If the response body hasn't been completely read, we need to drain
329 // it first.
[email protected]351ab642010-08-05 16:55:31330 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32331 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44332 read_buf_ = base::MakeRefCounted<IOBuffer>(
333 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32334 read_buf_len_ = kDrainBodyBufferSize;
335 return;
336 }
[email protected]0877e3d2009-10-17 22:29:57337 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09338 }
339
[email protected]2d2697f92009-02-18 21:00:32340 // We don't need to drain the response body, so we act as if we had drained
341 // the response body.
342 DidDrainBodyForAuthRestart(keep_alive);
343}
344
345void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38346 DCHECK(!stream_request_.get());
347
348 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19349 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22350 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35351 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34352 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38353 // We should call connection_->set_idle_time(), but this doesn't occur
354 // often enough to be worth the trouble.
355 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39356 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38357 }
[email protected]697ef4c2010-10-14 16:38:58358
359 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46360 // Close the stream and mark it as not_reusable. Even in the
361 // keep_alive case, we've determined that the stream_ is not
362 // reusable if new_stream is NULL.
363 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58364 next_state_ = STATE_CREATE_STREAM;
365 } else {
sclittlefb249892015-09-10 21:33:22366 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19367 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22368 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58369 next_state_ = STATE_INIT_STREAM;
370 }
371 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32372 }
[email protected]f9ee6b52008-11-08 06:46:23373
374 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58375 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23376}
377
[email protected]8e6441ca2010-08-19 05:56:38378bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
379 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
380 HaveAuth(pending_auth_target_);
381}
382
Bence Béky046f8c82018-06-12 02:26:04383int HttpNetworkTransaction::Read(IOBuffer* buf,
384 int buf_len,
385 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04386 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35387 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04388
[email protected]ad8e04a2010-11-01 04:16:27389 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38390 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04391 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29392 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04393 // bytes when establishing a tunnel because they might be controlled by
394 // an active network attacker. We don't worry about this for HTTP
395 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29396 // We reach this case when the user cancels a 407 proxy auth prompt. We
397 // also don't worry about this for an HTTPS Proxy, because the
398 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04399 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56400 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
401 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58402 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54403 LOG(WARNING) << "Blocked proxy response with status "
404 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15405 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04406 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44407 }
408
[email protected]e60e47a2010-07-14 03:37:18409 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15410 next_state_ = STATE_READ_BODY;
411
[email protected]96d570e42008-08-05 22:43:04412 read_buf_ = buf;
413 read_buf_len_ = buf_len;
414
[email protected]96d570e42008-08-05 22:43:04415 int rv = DoLoop(OK);
416 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04417 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04418 return rv;
419}
420
[email protected]8cd06c02014-01-25 07:50:14421void HttpNetworkTransaction::StopCaching() {}
422
[email protected]79e1fd62013-06-20 06:50:04423bool HttpNetworkTransaction::GetFullRequestHeaders(
424 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32425 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04426 *headers = request_headers_;
427 return true;
428}
429
sclittle4de1bab92015-09-22 21:28:24430int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
431 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19432 if (stream_)
433 total_received_bytes += stream_->GetTotalReceivedBytes();
434 return total_received_bytes;
435}
436
sclittlefb249892015-09-10 21:33:22437int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
438 int64_t total_sent_bytes = total_sent_bytes_;
439 if (stream_)
440 total_sent_bytes += stream_->GetTotalSentBytes();
441 return total_sent_bytes;
442}
443
[email protected]8cd06c02014-01-25 07:50:14444void HttpNetworkTransaction::DoneReading() {}
445
[email protected]96d570e42008-08-05 22:43:04446const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55447 return &response_;
[email protected]96d570e42008-08-05 22:43:04448}
449
450LoadState HttpNetworkTransaction::GetLoadState() const {
451 // TODO(wtc): Define a new LoadState value for the
452 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
453 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48454 case STATE_CREATE_STREAM:
455 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50456 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38457 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15458 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
459 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57460 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04461 return LOAD_STATE_SENDING_REQUEST;
462 case STATE_READ_HEADERS_COMPLETE:
463 return LOAD_STATE_WAITING_FOR_RESPONSE;
464 case STATE_READ_BODY_COMPLETE:
465 return LOAD_STATE_READING_RESPONSE;
466 default:
467 return LOAD_STATE_IDLE;
468 }
469}
470
[email protected]8cd06c02014-01-25 07:50:14471void HttpNetworkTransaction::SetQuicServerInfo(
472 QuicServerInfo* quic_server_info) {}
473
[email protected]5033ab82013-03-22 20:17:46474bool HttpNetworkTransaction::GetLoadTimingInfo(
475 LoadTimingInfo* load_timing_info) const {
476 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
477 return false;
478
479 load_timing_info->proxy_resolve_start =
480 proxy_info_.proxy_resolve_start_time();
481 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
482 load_timing_info->send_start = send_start_time_;
483 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46484 return true;
485}
486
ttuttled9dbc652015-09-29 20:00:59487bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36488 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59489 return false;
490
491 *endpoint = remote_endpoint_;
492 return true;
493}
494
zhongyi48704c182015-12-07 07:52:02495void HttpNetworkTransaction::PopulateNetErrorDetails(
496 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12497 *details = net_error_details_;
498 if (stream_)
499 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02500}
501
[email protected]5033ab82013-03-22 20:17:46502void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
503 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50504
[email protected]bf828982013-08-14 18:01:47505 if (stream_request_)
506 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03507 if (stream_)
508 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50509
rdsmith1d343be52016-10-21 20:37:50510 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46511}
512
[email protected]831e4a32013-11-14 02:14:44513void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
514 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
515 websocket_handshake_stream_base_create_helper_ = create_helper;
516}
517
[email protected]1826a402014-01-08 15:40:48518void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
519 const BeforeNetworkStartCallback& callback) {
520 before_network_start_callback_ = callback;
521}
522
ryansturm49a8cb12016-06-15 16:51:09523void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
524 const BeforeHeadersSentCallback& callback) {
525 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27526}
527
Andrey Kosyakov83a6eee2017-08-14 19:20:04528void HttpNetworkTransaction::SetRequestHeadersCallback(
529 RequestHeadersCallback callback) {
530 DCHECK(!stream_);
531 request_headers_callback_ = std::move(callback);
532}
533
Andrey Kosyakov2e893e62017-08-31 17:00:52534void HttpNetworkTransaction::SetResponseHeadersCallback(
535 ResponseHeadersCallback callback) {
536 DCHECK(!stream_);
537 response_headers_callback_ = std::move(callback);
538}
539
[email protected]1826a402014-01-08 15:40:48540int HttpNetworkTransaction::ResumeNetworkStart() {
541 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
542 return DoLoop(OK);
543}
544
[email protected]102e27c2011-02-23 01:01:31545void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
546 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00547 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50548 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38549 DCHECK(stream_request_.get());
550
sclittlefb249892015-09-10 21:33:22551 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19552 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22553 total_sent_bytes_ += stream_->GetTotalSentBytes();
554 }
bnc5029f4632017-06-08 16:19:00555 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04556 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14557 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31558 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52559 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21560 response_.alpn_negotiated_protocol =
561 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38562 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22563 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38564 OnIOComplete(OK);
565}
566
xunjieli5749218c2016-03-22 16:43:06567void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08568 const SSLConfig& used_ssl_config,
569 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00570 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08571 NOTREACHED();
572}
573
[email protected]a9cf2b92013-10-30 12:08:49574void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50575 const SSLConfig& used_ssl_config,
576 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00577 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
578 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50579}
580
Ryan Hamilton75f197262017-08-17 14:00:07581void HttpNetworkTransaction::OnStreamFailed(
582 int result,
583 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22584 const SSLConfig& used_ssl_config,
585 const ProxyInfo& used_proxy_info) {
[email protected]82918cc2010-08-25 17:24:50586 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38587 DCHECK_NE(OK, result);
588 DCHECK(stream_request_.get());
589 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14590 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07591 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22592 proxy_info_ = used_proxy_info;
593 SetProxyInfoInReponse(used_proxy_info, &response_);
[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 {
tbansal7cec3812015-02-05 21:25:12671 return (proxy_info_.is_http() || proxy_info_.is_https() ||
672 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24673 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
674}
675
initial.commit586acc5fe2008-07-26 22:42:52676void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50677 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41678 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52679
Douglas Creageref5eecdc2018-11-09 20:50:36680#if BUILDFLAG(ENABLE_REPORTING)
681 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42682 // report about this network request if the result was an error.
683 GenerateNetworkErrorLoggingReportIfError(rv);
684#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36685
[email protected]96d570e42008-08-05 22:43:04686 // Since Run may result in Read being called, clear user_callback_ up front.
Bence Békybc1de292018-02-01 15:48:03687 base::ResetAndReturn(&callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52688}
689
690void HttpNetworkTransaction::OnIOComplete(int result) {
691 int rv = DoLoop(result);
692 if (rv != ERR_IO_PENDING)
693 DoCallback(rv);
694}
695
696int HttpNetworkTransaction::DoLoop(int result) {
697 DCHECK(next_state_ != STATE_NONE);
698
699 int rv = result;
700 do {
701 State state = next_state_;
702 next_state_ = STATE_NONE;
703 switch (state) {
[email protected]1826a402014-01-08 15:40:48704 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
705 DCHECK_EQ(OK, rv);
706 rv = DoNotifyBeforeCreateStream();
707 break;
[email protected]82918cc2010-08-25 17:24:50708 case STATE_CREATE_STREAM:
709 DCHECK_EQ(OK, rv);
710 rv = DoCreateStream();
711 break;
712 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46713 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
714 // solved.
715 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50716 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46717 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50718 break;
[email protected]351ab642010-08-05 16:55:31719 case STATE_INIT_STREAM:
720 DCHECK_EQ(OK, rv);
721 rv = DoInitStream();
722 break;
723 case STATE_INIT_STREAM_COMPLETE:
724 rv = DoInitStreamComplete(rv);
725 break;
[email protected]044de0642010-06-17 10:42:15726 case STATE_GENERATE_PROXY_AUTH_TOKEN:
727 DCHECK_EQ(OK, rv);
728 rv = DoGenerateProxyAuthToken();
729 break;
730 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
731 rv = DoGenerateProxyAuthTokenComplete(rv);
732 break;
733 case STATE_GENERATE_SERVER_AUTH_TOKEN:
734 DCHECK_EQ(OK, rv);
735 rv = DoGenerateServerAuthToken();
736 break;
737 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
738 rv = DoGenerateServerAuthTokenComplete(rv);
739 break;
[email protected]daddea62012-09-19 05:51:13740 case STATE_INIT_REQUEST_BODY:
741 DCHECK_EQ(OK, rv);
742 rv = DoInitRequestBody();
743 break;
744 case STATE_INIT_REQUEST_BODY_COMPLETE:
745 rv = DoInitRequestBodyComplete(rv);
746 break;
[email protected]4875ba12011-03-30 22:31:51747 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55748 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00749 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51750 rv = DoBuildRequest();
751 break;
752 case STATE_BUILD_REQUEST_COMPLETE:
753 rv = DoBuildRequestComplete(rv);
754 break;
755 case STATE_SEND_REQUEST:
756 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57757 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52758 break;
[email protected]0877e3d2009-10-17 22:29:57759 case STATE_SEND_REQUEST_COMPLETE:
760 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43761 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00762 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52763 break;
764 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55765 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00766 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52767 rv = DoReadHeaders();
768 break;
769 case STATE_READ_HEADERS_COMPLETE:
770 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43771 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00772 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52773 break;
774 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55775 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00776 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52777 rv = DoReadBody();
778 break;
779 case STATE_READ_BODY_COMPLETE:
780 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43781 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00782 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52783 break;
[email protected]2d2697f92009-02-18 21:00:32784 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55785 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53786 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00787 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32788 rv = DoDrainBodyForAuthRestart();
789 break;
790 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
791 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43792 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00793 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32794 break;
initial.commit586acc5fe2008-07-26 22:42:52795 default:
796 NOTREACHED() << "bad state";
797 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04798 break;
initial.commit586acc5fe2008-07-26 22:42:52799 }
800 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
801
802 return rv;
803}
804
[email protected]1826a402014-01-08 15:40:48805int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
806 next_state_ = STATE_CREATE_STREAM;
807 bool defer = false;
808 if (!before_network_start_callback_.is_null())
809 before_network_start_callback_.Run(&defer);
810 if (!defer)
811 return OK;
812 return ERR_IO_PENDING;
813}
814
[email protected]82918cc2010-08-25 17:24:50815int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55816 response_.network_accessed = true;
817
[email protected]82918cc2010-08-25 17:24:50818 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17819 // IP based pooling is only enabled on a retry after 421 Misdirected Request
820 // is received. Alternative Services are also disabled in this case (though
821 // they can also be disabled when retrying after a QUIC error).
822 if (!enable_ip_based_pooling_)
823 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44824 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27825 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06826 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
827 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
828 websocket_handshake_stream_base_create_helper_,
829 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44830 } else {
xunjieli96f2a402017-06-05 17:24:27831 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29832 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27833 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44834 }
[email protected]26816882010-10-14 18:03:09835 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38836 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31837}
838
[email protected]82918cc2010-08-25 17:24:50839int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59840 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59841 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50842 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38843 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33844 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
845 result = HandleCertificateRequest(result);
bncfacdd852015-01-09 19:22:54846 } else if (result == ERR_HTTP_1_1_REQUIRED ||
847 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
848 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59849 }
850
David Benjamin76a40ad2018-02-24 22:22:08851 // Handle possible client certificate errors that may have occurred if the
852 // stream used SSL for one or more of the layers.
853 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30854
[email protected]8e6441ca2010-08-19 05:56:38855 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09856 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38857 return result;
[email protected]394816e92010-08-03 07:38:59858}
859
[email protected]82918cc2010-08-25 17:24:50860int HttpNetworkTransaction::DoInitStream() {
861 DCHECK(stream_.get());
862 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59863
864 stream_->GetRemoteEndpoint(&remote_endpoint_);
865
Steven Valdezb4ff0412018-01-18 22:39:27866 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
867 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50868}
869
870int HttpNetworkTransaction::DoInitStreamComplete(int result) {
871 if (result == OK) {
872 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50873 } else {
[email protected]82918cc2010-08-25 17:24:50874 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26875 result = HandleIOError(result);
876
877 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22878 if (stream_) {
879 total_received_bytes_ += stream_->GetTotalReceivedBytes();
880 total_sent_bytes_ += stream_->GetTotalSentBytes();
881 }
zhongyica364fbb2015-12-12 03:39:12882 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50883 }
884
885 return result;
886}
887
[email protected]044de0642010-06-17 10:42:15888int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
889 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
890 if (!ShouldApplyProxyAuth())
891 return OK;
[email protected]394816e92010-08-03 07:38:59892 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
893 if (!auth_controllers_[target].get())
Eric Orthbe2efac2019-03-06 01:11:11894 auth_controllers_[target] = new HttpAuthController(
895 target, AuthURL(target), session_->http_auth_cache(),
896 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59897 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41898 io_callback_,
[email protected]394816e92010-08-03 07:38:59899 net_log_);
[email protected]044de0642010-06-17 10:42:15900}
901
902int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
903 DCHECK_NE(ERR_IO_PENDING, rv);
904 if (rv == OK)
905 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
906 return rv;
907}
908
909int HttpNetworkTransaction::DoGenerateServerAuthToken() {
910 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59911 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54912 if (!auth_controllers_[target].get()) {
Eric Orthbe2efac2019-03-06 01:11:11913 auth_controllers_[target] = new HttpAuthController(
914 target, AuthURL(target), session_->http_auth_cache(),
915 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54916 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
917 auth_controllers_[target]->DisableEmbeddedIdentity();
918 }
[email protected]044de0642010-06-17 10:42:15919 if (!ShouldApplyServerAuth())
920 return OK;
[email protected]394816e92010-08-03 07:38:59921 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41922 io_callback_,
[email protected]394816e92010-08-03 07:38:59923 net_log_);
[email protected]044de0642010-06-17 10:42:15924}
925
926int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
927 DCHECK_NE(ERR_IO_PENDING, rv);
928 if (rv == OK)
nharperd6e65822016-03-30 23:05:48929 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14930 return rv;
931}
932
933int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24934 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14935 request_headers_.SetHeader(HttpRequestHeaders::kHost,
936 GetHostAndOptionalPort(request_->url));
937
938 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24939 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14940 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
941 "keep-alive");
942 } else {
943 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
944 }
945
[email protected]2979a492011-04-06 00:29:14946 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11947 if (request_->upload_data_stream) {
948 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14949 request_headers_.SetHeader(
950 HttpRequestHeaders::kTransferEncoding, "chunked");
951 } else {
952 request_headers_.SetHeader(
953 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09954 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14955 }
csharrisonf473dd192015-08-18 13:54:13956 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14957 // An empty POST/PUT request still needs a content length. As for HEAD,
958 // IE and Safari also add a content length header. Presumably it is to
959 // support sending a HEAD request to an URL that only expects to be sent a
960 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13961 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
962 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14963 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
964 }
965
[email protected]2979a492011-04-06 00:29:14966 // Honor load flags that impact proxy caches.
967 if (request_->load_flags & LOAD_BYPASS_CACHE) {
968 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
969 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
970 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
971 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
972 }
973
974 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
975 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
976 &request_headers_);
977 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
978 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
979 &request_headers_);
980
[email protected]c10450102011-06-27 09:06:16981 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20982
ryansturm49a8cb12016-06-15 16:51:09983 if (!before_headers_sent_callback_.is_null())
984 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:20985
[email protected]173f8e22013-04-10 04:18:20986 response_.did_use_http_auth =
987 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
988 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14989 return OK;
990}
991
[email protected]daddea62012-09-19 05:51:13992int HttpNetworkTransaction::DoInitRequestBody() {
993 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13994 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11995 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:33996 rv = request_->upload_data_stream->Init(
997 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
998 base::Unretained(this)),
999 net_log_);
[email protected]daddea62012-09-19 05:51:131000 return rv;
1001}
[email protected]4875ba12011-03-30 22:31:511002
[email protected]daddea62012-09-19 05:51:131003int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1004 if (result == OK)
1005 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131006 return result;
1007}
1008
1009int HttpNetworkTransaction::DoBuildRequest() {
1010 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511011 headers_valid_ = false;
1012
1013 // This is constructed lazily (instead of within our Start method), so that
1014 // we have proxy info available.
1015 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241016 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141017 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511018 }
1019
[email protected]4875ba12011-03-30 22:31:511020 return OK;
1021}
1022
1023int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241024 if (result == OK)
1025 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511026 return result;
1027}
1028
[email protected]0877e3d2009-10-17 22:29:571029int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251030 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571031 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1032
[email protected]bf3eb002012-11-15 05:50:111033 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521034}
1035
[email protected]0877e3d2009-10-17 22:29:571036int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251037 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361038
1039 if (result == ERR_HTTP_1_1_REQUIRED ||
1040 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1041 return HandleHttp11Required(result);
1042 }
1043
initial.commit586acc5fe2008-07-26 22:42:521044 if (result < 0)
1045 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571046 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521047 return OK;
1048}
1049
1050int HttpNetworkTransaction::DoReadHeaders() {
1051 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411052 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521053}
1054
1055int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111056 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1057 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381058 if (IsCertificateError(result)) {
1059 // We don't handle a certificate error during SSL renegotiation, so we
1060 // have to return an error that's not in the certificate error range
1061 // (-2xx).
1062 LOG(ERROR) << "Got a server certificate with error " << result
1063 << " during SSL renegotiation";
1064 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1065 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1066 // TODO(wtc): Need a test case for this code path!
1067 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251068 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381069 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501070 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381071 result = HandleCertificateRequest(result);
1072 if (result == OK)
1073 return result;
[email protected]2181ea002009-06-09 01:37:271074 }
1075
bncfacdd852015-01-09 19:22:541076 if (result == ERR_HTTP_1_1_REQUIRED ||
1077 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1078 return HandleHttp11Required(result);
1079 }
1080
[email protected]c871864d72014-03-13 19:56:191081 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1082 // response headers were received, we do the best we can to make sense of it
1083 // and send it back up the stack.
1084 //
1085 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1086 // bizarre for SPDY. Assuming this logic is useful at all.
1087 // TODO(davidben): Bubble the error code up so we do not cache?
1088 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1089 result = OK;
1090
1091 if (result < 0)
1092 return HandleIOError(result);
1093
[email protected]90499482013-06-01 00:39:501094 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521095
eustasc7d27da2017-04-06 10:33:201096 if (response_.headers.get() && !ContentEncodingsValid())
1097 return ERR_CONTENT_DECODING_FAILED;
1098
[email protected]d58ceea82014-06-04 10:55:541099 // On a 408 response from the server ("Request Timeout") on a stale socket,
1100 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041101 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181102 if (response_.headers.get() &&
1103 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541104 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421105#if BUILDFLAG(ENABLE_REPORTING)
1106 GenerateNetworkErrorLoggingReport(OK);
1107#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541108 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001109 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541110 response_.headers->response_code());
1111 // This will close the socket - it would be weird to try and reuse it, even
1112 // if the server doesn't actually close it.
1113 ResetConnectionAndRequestForResend();
1114 return OK;
1115 }
1116
[email protected]93f8b562012-03-27 01:00:161117 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211118 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161119 const int response_code = response_.headers->response_code();
1120 UMA_HISTOGRAM_ENUMERATION(
1121 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1122 }
1123
[email protected]3abacd62012-06-10 20:20:321124 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001125 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321126 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521127 if (response_headers_callback_)
1128 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391129
bncbe0f6af2015-10-15 17:49:561130 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571131 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1132 // indicates a buggy server. See:
1133 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1134 if (request_->method == "PUT")
1135 return ERR_METHOD_NOT_SUPPORTED;
1136 }
[email protected]4ddaf2502008-10-23 18:26:191137
Steven Valdez58097ec32018-07-16 18:29:041138 if (can_send_early_data_ && response_.headers.get() &&
1139 response_.headers->response_code() == HTTP_TOO_EARLY) {
1140 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1141 }
1142
[email protected]0877e3d2009-10-17 22:29:571143 // Check for an intermediate 100 Continue response. An origin server is
1144 // allowed to send this response even if we didn't ask for it, so we just
1145 // need to skip over it.
1146 // We treat any other 1xx in this same way (although in practice getting
1147 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441148 // Unless this is a WebSocket request, in which case we pass it on up.
1149 if (response_.headers->response_code() / 100 == 1 &&
1150 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451151 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571152 next_state_ = STATE_READ_HEADERS;
1153 return OK;
1154 }
1155
davidbence688ae2017-05-04 15:12:591156 if (response_.headers->response_code() == 421 &&
1157 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421158#if BUILDFLAG(ENABLE_REPORTING)
1159 GenerateNetworkErrorLoggingReport(OK);
1160#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591161 // Retry the request with both IP based pooling and Alternative Services
1162 // disabled.
1163 enable_ip_based_pooling_ = false;
1164 enable_alternative_services_ = false;
1165 net_log_.AddEvent(
1166 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1167 ResetConnectionAndRequestForResend();
1168 return OK;
bnc8016c1f2017-03-31 02:11:291169 }
1170
bncb26024382016-06-29 02:39:451171 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491172 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081173 if (response_.ssl_info.is_valid() &&
1174 !IsCertStatusError(response_.ssl_info.cert_status)) {
1175 session_->http_stream_factory()->ProcessAlternativeServices(
1176 session_, response_.headers.get(),
1177 url::SchemeHostPort(request_->url));
1178 }
1179 }
asanka5ffd5d72016-03-23 16:20:491180
[email protected]e772db3f2010-07-12 18:11:131181 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571182 if (rv != OK)
1183 return rv;
1184
Douglas Creager3cb042052018-11-06 23:08:521185#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421186 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1187 // reports generated before the NEL header is processed here will just be
1188 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141189 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521190 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421191
1192 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111193 // code), or if the response body will not be read, or on a redirect.
1194 // Note: This will report a success for a redirect even if an error is
1195 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421196 int response_code = response_.headers->response_code();
1197 if ((response_code >= 400 && response_code < 600) ||
1198 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1199 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111200 response_.headers->GetContentLength() == 0 ||
1201 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421202 GenerateNetworkErrorLoggingReport(OK);
1203 }
1204#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521205
[email protected]0877e3d2009-10-17 22:29:571206 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261207
1208 // We have reached the end of Start state machine, set the RequestInfo to
1209 // null.
1210 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1211 // only until the final response headers are received. Clearing it will ensure
1212 // that HttpRequestInfo is only used up until final response headers are
1213 // received. Clearing is allowed so that the transaction can be disassociated
1214 // from its creating consumer in cases where it is shared for writing to the
1215 // cache. It is also safe to set it to null at this point since
1216 // upload_data_stream is also not used in the Read state machine.
1217 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1218 request_ = nullptr;
1219
[email protected]0877e3d2009-10-17 22:29:571220 return OK;
initial.commit586acc5fe2008-07-26 22:42:521221}
1222
1223int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501224 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131225 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351226 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521227
1228 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501229 return stream_->ReadResponseBody(
1230 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521231}
1232
1233int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1234 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381235 bool done = false;
1236 if (result <= 0) {
1237 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521238 done = true;
[email protected]8e6441ca2010-08-19 05:56:381239 }
[email protected]9492e4a2010-02-24 00:58:461240
mmenkebd84c392015-09-02 14:12:341241 // Clean up connection if we are done.
1242 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381243 // Note: Just because IsResponseBodyComplete is true, we're not
1244 // necessarily "done". We're only "done" when it is the last
1245 // read on this HttpNetworkTransaction, which will be signified
1246 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341247 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381248 // the stream. No need to compute it here just to pass back
1249 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341250 bool keep_alive =
1251 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521252
[email protected]8e6441ca2010-08-19 05:56:381253 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011254 // Note: we don't reset the stream here. We've closed it, but we still
1255 // need it around so that callers can call methods such as
1256 // GetUploadProgress() and have them be meaningful.
1257 // TODO(mbelshe): This means we closed the stream here, and we close it
1258 // again in ~HttpNetworkTransaction. Clean that up.
1259
[email protected]8e6441ca2010-08-19 05:56:381260 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171261
1262 // This transaction was successful. If it had been retried because of an
1263 // error with an alternative service, mark that alternative service broken.
1264 if (!enable_alternative_services_ &&
1265 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411266 HistogramBrokenAlternateProtocolLocation(
1267 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171268 session_->http_server_properties()->MarkAlternativeServiceBroken(
1269 retried_alternative_service_);
1270 }
Lily Chenfec60d92019-01-24 01:16:421271
1272#if BUILDFLAG(ENABLE_REPORTING)
1273 GenerateNetworkErrorLoggingReport(result);
1274#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521275 }
1276
1277 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351278 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521279 read_buf_len_ = 0;
1280
1281 return result;
1282}
1283
[email protected]2d2697f92009-02-18 21:00:321284int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1285 // This method differs from DoReadBody only in the next_state_. So we just
1286 // call DoReadBody and override the next_state_. Perhaps there is a more
1287 // elegant way for these two methods to share code.
1288 int rv = DoReadBody();
1289 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1290 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1291 return rv;
1292}
1293
[email protected]0877e3d2009-10-17 22:29:571294// TODO(wtc): This method and the DoReadBodyComplete method are almost
1295// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321296int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231297 // keep_alive defaults to true because the very reason we're draining the
1298 // response body is to reuse the connection for auth restart.
1299 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321300 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571301 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421302 // Note: No Network Error Logging report is generated here because a report
1303 // will have already been generated for the original request due to the auth
1304 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321305 done = true;
[email protected]68873ba2009-06-04 21:49:231306 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311307 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571308 done = true;
[email protected]2d2697f92009-02-18 21:00:321309 }
1310
1311 if (done) {
1312 DidDrainBodyForAuthRestart(keep_alive);
1313 } else {
1314 // Keep draining.
1315 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1316 }
1317
1318 return OK;
1319}
1320
Douglas Creager3cb042052018-11-06 23:08:521321#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141322void HttpNetworkTransaction::ProcessReportToHeader() {
1323 std::string value;
1324 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1325 return;
1326
1327 ReportingService* service = session_->reporting_service();
1328 if (!service) {
1329 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1330 return;
1331 }
1332
1333 // Only accept Report-To headers on HTTPS connections that have no
1334 // certificate errors.
1335 if (!response_.ssl_info.is_valid()) {
1336 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1337 return;
1338 }
1339 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1340 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1341 return;
1342 }
1343
1344 service->ProcessHeader(url_.GetOrigin(), value);
1345}
1346
Douglas Creager3cb042052018-11-06 23:08:521347void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1348 std::string value;
1349 if (!response_.headers->GetNormalizedHeader(
1350 NetworkErrorLoggingService::kHeaderName, &value)) {
1351 return;
1352 }
1353
1354 NetworkErrorLoggingService* service =
1355 session_->network_error_logging_service();
1356 if (!service) {
1357 NetworkErrorLoggingService::
1358 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1359 return;
1360 }
1361
Lily Chen90ae93cc2019-02-14 01:15:391362 // Don't accept NEL headers received via a proxy, because the IP address of
1363 // the destination server is not known.
1364 if (response_.was_fetched_via_proxy)
1365 return;
1366
Douglas Creager3cb042052018-11-06 23:08:521367 // Only accept NEL headers on HTTPS connections that have no certificate
1368 // errors.
1369 if (!response_.ssl_info.is_valid()) {
1370 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1371 return;
1372 }
1373 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1374 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1375 return;
1376 }
1377
1378 if (remote_endpoint_.address().empty()) {
1379 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1380 return;
1381 }
1382
1383 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1384 value);
1385}
Douglas Creageref5eecdc2018-11-09 20:50:361386
Lily Chenfec60d92019-01-24 01:16:421387void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1388 if (rv < 0 && rv != ERR_IO_PENDING)
1389 GenerateNetworkErrorLoggingReport(rv);
1390}
1391
Douglas Creageref5eecdc2018-11-09 20:50:361392void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421393 // |rv| should be a valid net::Error
1394 DCHECK_NE(rv, ERR_IO_PENDING);
1395 DCHECK_LE(rv, 0);
1396
1397 if (network_error_logging_report_generated_)
1398 return;
1399 network_error_logging_report_generated_ = true;
1400
Douglas Creageref5eecdc2018-11-09 20:50:361401 NetworkErrorLoggingService* service =
1402 session_->network_error_logging_service();
1403 if (!service) {
1404 NetworkErrorLoggingService::
1405 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1406 return;
1407 }
1408
Lily Chen90ae93cc2019-02-14 01:15:391409 // Don't report on proxy auth challenges.
1410 if (response_.headers && response_.headers->response_code() ==
1411 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1412 return;
1413 }
1414
1415 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1416 // internal network details.
1417 if (response_.was_fetched_via_proxy)
1418 return;
1419
Lily Chen00196ab62018-12-04 19:52:291420 // Ignore errors from non-HTTPS origins.
1421 if (!url_.SchemeIsCryptographic()) {
1422 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1423 return;
1424 }
Douglas Creageref5eecdc2018-11-09 20:50:361425
1426 NetworkErrorLoggingService::RequestDetails details;
1427
1428 details.uri = url_;
1429 if (!request_referrer_.empty())
1430 details.referrer = GURL(request_referrer_);
1431 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421432 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361433 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421434 } else {
1435 details.server_ip = IPAddress();
1436 }
Douglas Creageref5eecdc2018-11-09 20:50:361437 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1438 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421439 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361440 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421441 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361442 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421443 }
Douglas Creageref5eecdc2018-11-09 20:50:361444 // If we got response headers, assume that the connection used HTTP/1.1
1445 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421446 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361447 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421448 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361449 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361450 }
Lily Chenfec60d92019-01-24 01:16:421451 details.method = request_method_;
1452 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361453 details.type = static_cast<Error>(rv);
1454 details.reporting_upload_depth = request_reporting_upload_depth_;
1455
1456 service->OnRequest(std::move(details));
1457}
Lily Chenfec60d92019-01-24 01:16:421458#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521459
[email protected]5e363962009-06-19 19:57:011460int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381461 // There are two paths through which the server can request a certificate
1462 // from us. The first is during the initial handshake, the second is
1463 // during SSL renegotiation.
1464 //
1465 // In both cases, we want to close the connection before proceeding.
1466 // We do this for two reasons:
1467 // First, we don't want to keep the connection to the server hung for a
1468 // long time while the user selects a certificate.
1469 // Second, even if we did keep the connection open, NSS has a bug where
1470 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581471 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381472
1473 if (stream_.get()) {
1474 // Since we already have a stream, we're being called as part of SSL
1475 // renegotiation.
1476 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191477 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221478 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381479 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121480 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381481 }
1482
[email protected]26816882010-10-14 18:03:091483 // The server is asking for a client certificate during the initial
1484 // handshake.
1485 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011486
[email protected]ec229bc92010-11-22 09:51:451487 // If the user selected one of the certificates in client_certs or declined
1488 // to provide one for this server before, use the past decision
1489 // automatically.
1490 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541491 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451492 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541493 response_.cert_request_info->host_and_port, &client_cert,
1494 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451495 if (!found_cached_cert)
1496 return error;
1497
1498 // Check that the certificate selected is still a certificate the server
1499 // is likely to accept, based on the criteria supplied in the
1500 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501501 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261502 const std::vector<std::string>& cert_authorities =
1503 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451504
[email protected]f1958c382013-02-07 00:15:261505 bool cert_still_valid = cert_authorities.empty() ||
1506 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451507 if (!cert_still_valid)
1508 return error;
[email protected]5e363962009-06-19 19:57:011509 }
[email protected]ec229bc92010-11-22 09:51:451510
David Benjamin76a40ad2018-02-24 22:22:081511 if (!response_.cert_request_info->is_proxy) {
1512 server_ssl_client_cert_was_cached_ = true;
1513 }
1514
[email protected]ec229bc92010-11-22 09:51:451515 // TODO(davidben): Add a unit test which covers this path; we need to be
1516 // able to send a legitimate certificate and also bypass/clear the
1517 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141518 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1519 &proxy_ssl_config_ : &server_ssl_config_;
1520 ssl_config->send_client_cert = true;
1521 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541522 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451523 next_state_ = STATE_CREATE_STREAM;
1524 // Reset the other member variables.
1525 // Note: this is necessary only with SSL renegotiation.
1526 ResetStateForRestart();
1527 return OK;
[email protected]0b45559b2009-06-12 21:45:111528}
1529
bncfacdd852015-01-09 19:22:541530int HttpNetworkTransaction::HandleHttp11Required(int error) {
1531 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1532 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1533
1534 if (error == ERR_HTTP_1_1_REQUIRED) {
1535 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1536 } else {
1537 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1538 }
1539 ResetConnectionAndRequestForResend();
1540 return OK;
1541}
1542
David Benjamin76a40ad2018-02-24 22:22:081543int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjamin98ba2be2019-04-09 14:25:251544 // Client certificate errors from the proxy are handled in the
1545 // HttpStreamFactory and below. See discussion in https://crbug.com/828965.
[email protected]384cc73a2013-12-08 22:41:031546 if (server_ssl_config_.send_client_cert &&
1547 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1548 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411549 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031550
David Benjamin76a40ad2018-02-24 22:22:081551 // The private key handle may have gone stale due to, e.g., the user
1552 // unplugging their smartcard. Operating systems do not provide reliable
1553 // notifications for this, so if the signature failed and the private key
1554 // came from SSLClientAuthCache, retry to ask the user for a new one.
1555 if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1556 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1557 server_ssl_client_cert_was_cached_ = false;
1558 server_ssl_config_.send_client_cert = false;
1559 server_ssl_config_.client_cert = nullptr;
1560 server_ssl_config_.client_private_key = nullptr;
1561 retry_attempts_++;
1562 net_log_.AddEventWithNetErrorCode(
1563 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1564 ResetConnectionAndRequestForResend();
1565 return OK;
1566 }
1567 }
initial.commit586acc5fe2008-07-26 22:42:521568 return error;
1569}
1570
[email protected]bd0b6772011-01-11 19:59:301571// This method determines whether it is safe to resend the request after an
1572// IO error. It can only be called in response to request header or body
1573// write errors or response header read errors. It should not be used in
1574// other cases, such as a Connect error.
1575int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031576 // Because the peer may request renegotiation with client authentication at
1577 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081578 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301579
Lily Chenfec60d92019-01-24 01:16:421580#if BUILDFLAG(ENABLE_REPORTING)
1581 GenerateNetworkErrorLoggingReportIfError(error);
1582#endif // BUILDFLAG(ENABLE_REPORTING)
1583
[email protected]bd0b6772011-01-11 19:59:301584 switch (error) {
1585 // If we try to reuse a connection that the server is in the process of
1586 // closing, we may end up successfully writing out our request (or a
1587 // portion of our request) only to find a connection error when we try to
1588 // read from (or finish writing to) the socket.
1589 case ERR_CONNECTION_RESET:
1590 case ERR_CONNECTION_CLOSED:
1591 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511592 // There can be a race between the socket pool checking checking whether a
1593 // socket is still connected, receiving the FIN, and sending/reading data
1594 // on a reused socket. If we receive the FIN between the connectedness
1595 // check and writing/reading from the socket, we may first learn the socket
1596 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1597 // likely happen when trying to retrieve its IP address.
1598 // See http://crbug.com/105824 for more details.
1599 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491600 // If a socket is closed on its initial request, HttpStreamParser returns
1601 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1602 // preconnected but failed to be used before the server timed it out.
1603 case ERR_EMPTY_RESPONSE:
1604 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381605 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001606 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301607 ResetConnectionAndRequestForResend();
1608 error = OK;
1609 }
1610 break;
Steven Valdez58097ec32018-07-16 18:29:041611 case ERR_EARLY_DATA_REJECTED:
1612 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1613 net_log_.AddEventWithNetErrorCode(
1614 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1615 // Disable early data on the SSLConfig on a reset.
1616 can_send_early_data_ = false;
1617 ResetConnectionAndRequestForResend();
1618 error = OK;
1619 break;
[email protected]8753a122011-10-16 08:05:081620 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001621 case ERR_SPDY_SERVER_REFUSED_STREAM:
Bence Béky1a5d8562018-01-05 17:29:281622 case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE:
Yoav Weiss9693572f2018-01-04 09:37:341623 case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
Bence Béky49db0e22018-05-11 00:54:051624 case ERR_SPDY_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271625 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141626 if (HasExceededMaxRetries())
1627 break;
[email protected]b6754252012-06-13 23:14:381628 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001629 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141630 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001631 ResetConnectionAndRequestForResend();
1632 error = OK;
1633 break;
rch2f2991c2017-04-13 19:28:171634 case ERR_QUIC_PROTOCOL_ERROR:
1635 if (GetResponseHeaders() != nullptr ||
1636 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1637 // If the response headers have already been recieved and passed up
1638 // then the request can not be retried. Also, if there was no
1639 // alternative service used for this request, then there is no
1640 // alternative service to be disabled.
1641 break;
1642 }
Biljith Jayan45a41722017-08-16 18:43:141643 if (HasExceededMaxRetries())
1644 break;
rch2f2991c2017-04-13 19:28:171645 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1646 retried_alternative_service_)) {
1647 // If the alternative service was marked as broken while the request
1648 // was in flight, retry the request which will not use the broken
1649 // alternative service.
1650 net_log_.AddEventWithNetErrorCode(
1651 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141652 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171653 ResetConnectionAndRequestForResend();
1654 error = OK;
1655 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1656 // Disable alternative services for this request and retry it. If the
1657 // retry succeeds, then the alternative service will be marked as
1658 // broken then.
1659 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361660 net_log_.AddEventWithNetErrorCode(
1661 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141662 retry_attempts_++;
rch514a44a82017-04-13 04:46:361663 ResetConnectionAndRequestForResend();
1664 error = OK;
1665 }
1666 break;
[email protected]bd0b6772011-01-11 19:59:301667 }
1668 return error;
1669}
1670
[email protected]c3b35c22008-09-27 03:19:421671void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581672 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221673 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191674 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221675 total_sent_bytes_ += stream_->GetTotalSentBytes();
1676 }
zhongyica364fbb2015-12-12 03:39:121677 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581678}
1679
1680void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251681 send_start_time_ = base::TimeTicks();
1682 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251683
[email protected]0757e7702009-03-27 04:00:221684 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351685 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421686 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571687 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201688 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141689 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381690 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591691 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121692 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521693 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421694#if BUILDFLAG(ENABLE_REPORTING)
1695 network_error_logging_report_generated_ = false;
1696 start_timeticks_ = base::TimeTicks::Now();
1697#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121698}
1699
1700void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1701 if (stream_)
1702 stream_->PopulateNetErrorDetails(&net_error_details_);
1703 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571704}
1705
[email protected]0877e3d2009-10-17 22:29:571706HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501707 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421708}
1709
[email protected]a34f61ee2014-03-18 20:59:491710bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381711 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351712 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161713
[email protected]2a5c76b2008-09-25 22:15:161714 // NOTE: we resend a request only if we reused a keep-alive connection.
1715 // This automatically prevents an infinite resend loop because we'll run
1716 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381717 if (connection_is_proven && !has_received_headers)
1718 return true;
1719 return false;
[email protected]1c773ea12009-04-28 19:58:421720}
1721
Biljith Jayan45a41722017-08-16 18:43:141722bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1723 return (retry_attempts_ >= kMaxRetryAttempts);
1724}
1725
David Benjamin83ddfb32018-03-30 01:07:521726bool HttpNetworkTransaction::CheckMaxRestarts() {
1727 num_restarts_++;
1728 return num_restarts_ < kMaxRestarts;
1729}
1730
[email protected]1c773ea12009-04-28 19:58:421731void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381732 if (stream_.get()) {
1733 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121734 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161735 }
1736
[email protected]0877e3d2009-10-17 22:29:571737 // We need to clear request_headers_ because it contains the real request
1738 // headers, but we may need to resend the CONNECT request first to recreate
1739 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201740 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501741 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421742
1743#if BUILDFLAG(ENABLE_REPORTING)
1744 // Reset for new request.
1745 network_error_logging_report_generated_ = false;
1746 start_timeticks_ = base::TimeTicks::Now();
1747#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221748
1749 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541750}
1751
[email protected]1c773ea12009-04-28 19:58:421752bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241753 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421754}
license.botbf09a502008-08-24 00:55:551755
[email protected]1c773ea12009-04-28 19:58:421756bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041757 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421758}
1759
[email protected]e772db3f2010-07-12 18:11:131760int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271761 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501762 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421763
[email protected]0877e3d2009-10-17 22:29:571764 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581765 if (status != HTTP_UNAUTHORIZED &&
1766 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421767 return OK;
[email protected]9094b602012-02-27 21:44:581768 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111769 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161770 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1771 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421772
[email protected]9094b602012-02-27 21:44:581773 // This case can trigger when an HTTPS server responds with a "Proxy
1774 // authentication required" status code through a non-authenticating
1775 // proxy.
[email protected]7a67a8152010-11-05 18:31:101776 if (!auth_controllers_[target].get())
1777 return ERR_UNEXPECTED_PROXY_AUTH;
1778
[email protected]a7ea8832010-07-12 17:54:541779 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491780 headers, response_.ssl_info,
1781 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311782 net_log_);
[email protected]228404f2010-06-24 04:31:411783 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491784 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411785
Emily Starkf2c9bbd2019-04-09 17:08:581786 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411787
[email protected]228404f2010-06-24 04:31:411788 return rv;
[email protected]f9ee6b52008-11-08 06:46:231789}
1790
[email protected]8e6441ca2010-08-19 05:56:381791bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1792 return auth_controllers_[target].get() &&
1793 auth_controllers_[target]->HaveAuth();
1794}
1795
[email protected]228404f2010-06-24 04:31:411796GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1797 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461798 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411799 if (!proxy_info_.proxy_server().is_valid() ||
1800 proxy_info_.proxy_server().is_direct()) {
1801 return GURL(); // There is no proxy server.
1802 }
[email protected]2df19bb2010-08-25 20:13:461803 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1804 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351805 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461806 }
[email protected]228404f2010-06-24 04:31:411807 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291808 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361809 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291810 }
[email protected]228404f2010-06-24 04:31:411811 return request_->url;
1812 default:
1813 return GURL();
1814 }
[email protected]c3b35c22008-09-27 03:19:421815}
1816
[email protected]831e4a32013-11-14 02:14:441817bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141818 return websocket_handshake_stream_base_create_helper_ &&
1819 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441820}
1821
ttuttle1f2d7e92015-04-28 16:17:471822void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1823 DCHECK(stream_request_);
1824
1825 // Since the transaction can restart with auth credentials, it may create a
1826 // stream more than once. Accumulate all of the connection attempts across
1827 // those streams by appending them to the vector:
1828 for (const auto& attempt : stream_request_->connection_attempts())
1829 connection_attempts_.push_back(attempt);
1830}
1831
eustasc7d27da2017-04-06 10:33:201832bool HttpNetworkTransaction::ContentEncodingsValid() const {
1833 HttpResponseHeaders* headers = GetResponseHeaders();
1834 DCHECK(headers);
1835
1836 std::string accept_encoding;
1837 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1838 &accept_encoding);
1839 std::set<std::string> allowed_encodings;
1840 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1841 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1842 return false;
1843 }
1844
1845 std::string content_encoding;
1846 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1847 std::set<std::string> used_encodings;
1848 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1849 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1850 return false;
1851 }
1852
1853 // When "Accept-Encoding" is not specified, it is parsed as "*".
1854 // If "*" encoding is advertised, then any encoding should be "accepted".
1855 // This does not mean, that it will be successfully decoded.
1856 if (allowed_encodings.find("*") != allowed_encodings.end())
1857 return true;
1858
sky50576f32017-05-01 19:28:031859 bool result = true;
eustasc7d27da2017-04-06 10:33:201860 for (auto const& encoding : used_encodings) {
1861 SourceStream::SourceType source_type =
1862 FilterSourceStream::ParseEncodingType(encoding);
1863 // We don't reject encodings we are not aware. They just will not decode.
1864 if (source_type == SourceStream::TYPE_UNKNOWN)
1865 continue;
1866 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031867 FilterSourceStream::ReportContentDecodingFailed(
1868 SourceStream::TYPE_REJECTED);
1869 result = false;
1870 break;
eustasc7d27da2017-04-06 10:33:201871 }
1872 }
sky50576f32017-05-01 19:28:031873
1874 // Temporary workaround for http://crbug.com/714514
1875 if (headers->IsRedirect(nullptr)) {
1876 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1877 !result);
1878 return true;
1879 }
1880
1881 return result;
eustasc7d27da2017-04-06 10:33:201882}
1883
[email protected]c3b35c22008-09-27 03:19:421884} // namespace net