blob: dff329b442ca78291ce071ac975c99409e6d12e5 [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]2fbaecf22010-07-22 22:20:359#include <vector>
10
nharperb7441ef2016-01-25 23:54:1411#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4112#include "base/bind.h"
13#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3014#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4715#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3816#include "base/metrics/field_trial.h"
David Benjamin5cb91132018-04-06 05:54:4917#include "base/metrics/histogram_functions.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4721#include "base/strings/string_number_conversions.h"
22#include "base/strings/string_util.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4032#include "net/base/proxy_server.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4434#include "net/base/url_util.h"
Bence Béky230ac612017-08-30 19:17:0835#include "net/cert/cert_status_flags.h"
eustasc7d27da2017-04-06 10:33:2036#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0037#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4238#include "net/http/http_auth.h"
39#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2440#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3641#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5242#include "net/http/http_chunked_decoder.h"
Eric Roman06bd9742019-07-13 15:19:1343#include "net/http/http_log_util.h"
initial.commit586acc5fe2008-07-26 22:42:5244#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"
Matt Menkef2ee07c2019-08-29 02:10:3669#include "url/scheme_host_port.h"
[email protected]e69c1cd2014-07-29 07:42:2970#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5271
Douglas Creager3cb042052018-11-06 23:08:5272#if BUILDFLAG(ENABLE_REPORTING)
73#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1474#include "net/reporting/reporting_header_parser.h"
75#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4276#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5277
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2278namespace net {
79
Biljith Jayan45a41722017-08-16 18:43:1480namespace {
David Benjamin83ddfb32018-03-30 01:07:5281
Biljith Jayan45a41722017-08-16 18:43:1482// Max number of |retry_attempts| (excluding the initial request) after which
83// we give up and show an error page.
84const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5285
86// Max number of calls to RestartWith* allowed for a single connection. A single
87// HttpNetworkTransaction should not signal very many restartable errors, but it
88// may occur due to a bug (e.g. https://crbug.com/823387 or
89// https://crbug.com/488043) or simply if the server or proxy requests
90// authentication repeatedly. Although these calls are often associated with a
91// user prompt, in other scenarios (remembered preferences, extensions,
92// multi-leg authentication), they may be triggered automatically. To avoid
93// looping forever, bound the number of restarts.
94const size_t kMaxRestarts = 32;
95
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2296void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
97 HttpResponseInfo* response_info) {
98 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
99 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
100 response_info->proxy_server = proxy_info.proxy_server();
101 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
102 response_info->proxy_server = ProxyServer::Direct();
103 else
104 response_info->proxy_server = ProxyServer();
105}
Biljith Jayan45a41722017-08-16 18:43:14106
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22107} // namespace
initial.commit586acc5fe2008-07-26 22:42:52108
Victor Costan9c7302b2018-08-27 16:39:44109const int HttpNetworkTransaction::kDrainBodyBufferSize;
110
[email protected]262eec82013-03-19 21:01:36111HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
112 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22113 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53114 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
115 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52116 session_(session),
Raul Tambre94493c652019-03-11 17:18:35117 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36118 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57119 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27120 can_send_early_data_(false),
David Benjaminbac8dff2019-08-07 01:30:41121 configured_client_cert_for_server_(false),
[email protected]b94f92d2010-10-27 16:45:20122 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42123#if BUILDFLAG(ENABLE_REPORTING)
124 network_error_logging_report_generated_(false),
125 request_reporting_upload_depth_(0),
126#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52127 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19128 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22129 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54130 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44131 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29132 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26133 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35134 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14135 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52136 retry_attempts_(0),
David Benjamind61bd532019-04-23 21:11:37137 num_restarts_(0) {
Lily Chenfec60d92019-01-24 01:16:42138}
[email protected]3ce7df0f2010-03-03 00:30:50139
[email protected]0b0bf032010-09-21 18:08:50140HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42141#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11142 // If no error or success report has been generated yet at this point, then
143 // this network transaction was prematurely cancelled.
144 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42145#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50146 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50147 // TODO(mbelshe): The stream_ should be able to compute whether or not the
148 // stream should be kept alive. No reason to compute here
149 // and pass it in.
mmenkebd84c392015-09-02 14:12:34150 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50151 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34152 } else if (stream_->IsResponseBodyComplete()) {
153 // If the response body is complete, we can just reuse the socket.
154 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50155 } else {
mmenkebd84c392015-09-02 14:12:34156 // Otherwise, we try to drain the response body.
157 HttpStream* stream = stream_.release();
158 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50159 }
160 }
[email protected]02cad5d2013-10-02 08:14:03161 if (request_ && request_->upload_data_stream)
162 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50163}
164
[email protected]684970b2009-08-14 04:54:46165int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04166 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20167 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23168 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
169 return ERR_CACHE_MISS;
170
Ramin Halavatib5e433e2018-02-07 07:41:10171 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53172 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04173 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15174 url_ = request_->url;
Matt Menkeb32ba5122019-09-10 19:17:05175 network_isolation_key_ = request_->network_isolation_key;
Douglas Creageref5eecdc2018-11-09 20:50:36176#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42177 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36178 request_method_ = request_->method;
179 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
180 &request_referrer_);
181 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
182 &request_user_agent_);
183 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42184 start_timeticks_ = base::TimeTicks::Now();
185#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04186
David Benjamin8119ce9e42019-08-06 00:05:06187 session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_);
nharper8cdb0fb2016-04-22 21:34:59188
Ryan Sleevi24fe2682018-08-16 21:33:46189 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
190 server_ssl_config_.disable_cert_verification_network_fetches = true;
191 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19192 }
[email protected]6fbac162011-06-20 00:29:04193
Steven Valdez03e872d2018-03-02 15:39:15194 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27195 can_send_early_data_ = true;
196 }
197
Dominic Farolino4ecaf0a2019-08-16 06:40:09198 if (request_->load_flags & LOAD_PREFETCH) {
jkarlinfb1d5172015-01-12 14:10:29199 response_.unused_since_prefetch = true;
Dominic Farolino4ecaf0a2019-08-16 06:40:09200 }
201
202 if (request_->load_flags & LOAD_RESTRICTED_PREFETCH) {
203 DCHECK(response_.unused_since_prefetch);
204 response_.restricted_prefetch = true;
205 }
jkarlinfb1d5172015-01-12 14:10:29206
Helen Lib495c3802018-03-30 13:46:09207 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04208 int rv = DoLoop(OK);
209 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04210 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42211
212 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
213 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
214 // other net::Error can be returned.
215 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04216 return rv;
217}
218
219int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04220 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38221 DCHECK(!stream_.get());
222 DCHECK(!stream_request_.get());
223 DCHECK_EQ(STATE_NONE, next_state_);
224
David Benjamin83ddfb32018-03-30 01:07:52225 if (!CheckMaxRestarts())
226 return ERR_TOO_MANY_RETRIES;
227
[email protected]82918cc2010-08-25 17:24:50228 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38229
[email protected]ccb40e52008-09-17 20:54:40230 int rv = DoLoop(OK);
231 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04232 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42233
234 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
235 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
236 // other net::Error can be returned.
237 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11238 return rv;
[email protected]96d570e42008-08-05 22:43:04239}
240
[email protected]0b45559b2009-06-12 21:45:11241int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08242 scoped_refptr<X509Certificate> client_cert,
243 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04244 CompletionOnceCallback callback) {
David Benjaminbac8dff2019-08-07 01:30:41245 // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down
246 // existing streams and stream requests to force a new connection.
[email protected]8e6441ca2010-08-19 05:56:38247 DCHECK(!stream_request_.get());
248 DCHECK(!stream_.get());
249 DCHECK_EQ(STATE_NONE, next_state_);
250
David Benjamin83ddfb32018-03-30 01:07:52251 if (!CheckMaxRestarts())
252 return ERR_TOO_MANY_RETRIES;
253
David Benjaminbac8dff2019-08-07 01:30:41254 // Add the credentials to the client auth cache. The next stream request will
255 // then pick them up.
256 session_->ssl_client_context()->SetClientCertificate(
mattm436ccfe2017-06-19 20:24:08257 response_.cert_request_info->host_and_port, std::move(client_cert),
258 std::move(client_private_key));
David Benjaminbac8dff2019-08-07 01:30:41259
260 if (!response_.cert_request_info->is_proxy)
261 configured_client_cert_for_server_ = true;
262
[email protected]0b45559b2009-06-12 21:45:11263 // Reset the other member variables.
264 // Note: this is necessary only with SSL renegotiation.
265 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50266 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11267 int rv = DoLoop(OK);
268 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04269 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42270
271 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
272 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
273 // other net::Error can be returned.
274 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11275 return rv;
276}
277
Bence Béky046f8c82018-06-12 02:26:04278int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
279 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52280 if (!CheckMaxRestarts())
281 return ERR_TOO_MANY_RETRIES;
282
[email protected]0757e7702009-03-27 04:00:22283 HttpAuth::Target target = pending_auth_target_;
284 if (target == HttpAuth::AUTH_NONE) {
285 NOTREACHED();
286 return ERR_UNEXPECTED;
287 }
[email protected]0757e7702009-03-27 04:00:22288 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42289
[email protected]f3cf9802011-10-28 18:44:58290 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13291
[email protected]49639fa2011-12-20 23:22:41292 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38293
294 int rv = OK;
295 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
296 // In this case, we've gathered credentials for use with proxy
297 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50298 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35299 DCHECK(stream_request_ != nullptr);
300 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54301 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39302 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54303 } else {
[email protected]8e6441ca2010-08-19 05:56:38304 // In this case, we've gathered credentials for the server or the proxy
305 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35306 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54307 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38308 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42309 // Note: If an error is encountered while draining the old response body, no
310 // Network Error Logging report will be generated, because the error was
311 // with the old request, which will already have had a NEL report generated
312 // for it due to the auth challenge (so we don't report a second error for
313 // that request).
[email protected]a7ea8832010-07-12 17:54:54314 }
[email protected]c3b35c22008-09-27 03:19:42315
[email protected]c3b35c22008-09-27 03:19:42316 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04317 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42318 return rv;
[email protected]96d570e42008-08-05 22:43:04319}
320
[email protected]f9ee6b52008-11-08 06:46:23321void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
322 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38323 DCHECK(!stream_request_.get());
324
Bence Béky3238f2e12017-09-22 22:44:49325 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
326 if (target == HttpAuth::AUTH_SERVER &&
327 auth_controllers_[target]->NeedsHTTP11()) {
328 session_->http_server_properties()->SetHTTP11Required(
Matt Menked9b24f02019-09-26 17:07:17329 url::SchemeHostPort(request_->url), network_isolation_key_);
Bence Béky3238f2e12017-09-22 22:44:49330 }
331
[email protected]2d2697f92009-02-18 21:00:32332 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57333 // Even if the server says the connection is keep-alive, we have to be
334 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34335 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57336 // If the response body hasn't been completely read, we need to drain
337 // it first.
[email protected]351ab642010-08-05 16:55:31338 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32339 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44340 read_buf_ = base::MakeRefCounted<IOBuffer>(
341 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32342 read_buf_len_ = kDrainBodyBufferSize;
343 return;
344 }
[email protected]0877e3d2009-10-17 22:29:57345 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09346 }
347
[email protected]2d2697f92009-02-18 21:00:32348 // We don't need to drain the response body, so we act as if we had drained
349 // the response body.
350 DidDrainBodyForAuthRestart(keep_alive);
351}
352
353void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38354 DCHECK(!stream_request_.get());
355
356 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19357 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22358 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35359 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34360 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38361 // We should call connection_->set_idle_time(), but this doesn't occur
362 // often enough to be worth the trouble.
363 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39364 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38365 }
[email protected]697ef4c2010-10-14 16:38:58366
367 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46368 // Close the stream and mark it as not_reusable. Even in the
369 // keep_alive case, we've determined that the stream_ is not
370 // reusable if new_stream is NULL.
371 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58372 next_state_ = STATE_CREATE_STREAM;
373 } else {
sclittlefb249892015-09-10 21:33:22374 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19375 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22376 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58377 next_state_ = STATE_INIT_STREAM;
378 }
379 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32380 }
[email protected]f9ee6b52008-11-08 06:46:23381
382 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58383 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23384}
385
[email protected]8e6441ca2010-08-19 05:56:38386bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
387 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
388 HaveAuth(pending_auth_target_);
389}
390
Bence Béky046f8c82018-06-12 02:26:04391int HttpNetworkTransaction::Read(IOBuffer* buf,
392 int buf_len,
393 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04394 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35395 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04396
[email protected]ad8e04a2010-11-01 04:16:27397 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38398 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04399 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29400 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04401 // bytes when establishing a tunnel because they might be controlled by
402 // an active network attacker. We don't worry about this for HTTP
403 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29404 // We reach this case when the user cancels a 407 proxy auth prompt. We
405 // also don't worry about this for an HTTPS Proxy, because the
406 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04407 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56408 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
409 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58410 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]8a1f3312010-05-25 19:25:04411 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44412 }
413
[email protected]e60e47a2010-07-14 03:37:18414 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15415 next_state_ = STATE_READ_BODY;
416
[email protected]96d570e42008-08-05 22:43:04417 read_buf_ = buf;
418 read_buf_len_ = buf_len;
419
[email protected]96d570e42008-08-05 22:43:04420 int rv = DoLoop(OK);
421 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04422 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04423 return rv;
424}
425
[email protected]8cd06c02014-01-25 07:50:14426void HttpNetworkTransaction::StopCaching() {}
427
sclittle4de1bab92015-09-22 21:28:24428int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
429 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19430 if (stream_)
431 total_received_bytes += stream_->GetTotalReceivedBytes();
432 return total_received_bytes;
433}
434
sclittlefb249892015-09-10 21:33:22435int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
436 int64_t total_sent_bytes = total_sent_bytes_;
437 if (stream_)
438 total_sent_bytes += stream_->GetTotalSentBytes();
439 return total_sent_bytes;
440}
441
[email protected]8cd06c02014-01-25 07:50:14442void HttpNetworkTransaction::DoneReading() {}
443
[email protected]96d570e42008-08-05 22:43:04444const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55445 return &response_;
[email protected]96d570e42008-08-05 22:43:04446}
447
448LoadState HttpNetworkTransaction::GetLoadState() const {
449 // TODO(wtc): Define a new LoadState value for the
450 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
451 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48452 case STATE_CREATE_STREAM:
453 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50454 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38455 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15456 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
457 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57458 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04459 return LOAD_STATE_SENDING_REQUEST;
460 case STATE_READ_HEADERS_COMPLETE:
461 return LOAD_STATE_WAITING_FOR_RESPONSE;
462 case STATE_READ_BODY_COMPLETE:
463 return LOAD_STATE_READING_RESPONSE;
464 default:
465 return LOAD_STATE_IDLE;
466 }
467}
468
[email protected]8cd06c02014-01-25 07:50:14469void HttpNetworkTransaction::SetQuicServerInfo(
470 QuicServerInfo* quic_server_info) {}
471
[email protected]5033ab82013-03-22 20:17:46472bool HttpNetworkTransaction::GetLoadTimingInfo(
473 LoadTimingInfo* load_timing_info) const {
474 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
475 return false;
476
477 load_timing_info->proxy_resolve_start =
478 proxy_info_.proxy_resolve_start_time();
479 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
480 load_timing_info->send_start = send_start_time_;
481 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46482 return true;
483}
484
ttuttled9dbc652015-09-29 20:00:59485bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36486 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59487 return false;
488
489 *endpoint = remote_endpoint_;
490 return true;
491}
492
zhongyi48704c182015-12-07 07:52:02493void HttpNetworkTransaction::PopulateNetErrorDetails(
494 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12495 *details = net_error_details_;
496 if (stream_)
497 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02498}
499
[email protected]5033ab82013-03-22 20:17:46500void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
501 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50502
[email protected]bf828982013-08-14 18:01:47503 if (stream_request_)
504 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03505 if (stream_)
506 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50507
rdsmith1d343be52016-10-21 20:37:50508 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46509}
510
[email protected]831e4a32013-11-14 02:14:44511void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
512 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
513 websocket_handshake_stream_base_create_helper_ = create_helper;
514}
515
[email protected]1826a402014-01-08 15:40:48516void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
517 const BeforeNetworkStartCallback& callback) {
518 before_network_start_callback_ = callback;
519}
520
ryansturm49a8cb12016-06-15 16:51:09521void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
522 const BeforeHeadersSentCallback& callback) {
523 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27524}
525
Andrey Kosyakov83a6eee2017-08-14 19:20:04526void HttpNetworkTransaction::SetRequestHeadersCallback(
527 RequestHeadersCallback callback) {
528 DCHECK(!stream_);
529 request_headers_callback_ = std::move(callback);
530}
531
Andrey Kosyakov2e893e62017-08-31 17:00:52532void HttpNetworkTransaction::SetResponseHeadersCallback(
533 ResponseHeadersCallback callback) {
534 DCHECK(!stream_);
535 response_headers_callback_ = std::move(callback);
536}
537
[email protected]1826a402014-01-08 15:40:48538int HttpNetworkTransaction::ResumeNetworkStart() {
539 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
540 return DoLoop(OK);
541}
542
[email protected]102e27c2011-02-23 01:01:31543void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
544 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00545 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50546 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38547 DCHECK(stream_request_.get());
548
sclittlefb249892015-09-10 21:33:22549 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19550 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22551 total_sent_bytes_ += stream_->GetTotalSentBytes();
552 }
bnc5029f4632017-06-08 16:19:00553 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04554 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14555 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31556 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52557 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21558 response_.alpn_negotiated_protocol =
559 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38560 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22561 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38562 OnIOComplete(OK);
563}
564
xunjieli5749218c2016-03-22 16:43:06565void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08566 const SSLConfig& used_ssl_config,
567 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00568 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08569 NOTREACHED();
570}
571
[email protected]a9cf2b92013-10-30 12:08:49572void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50573 const SSLConfig& used_ssl_config,
574 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00575 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
576 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50577}
578
Ryan Hamilton75f197262017-08-17 14:00:07579void HttpNetworkTransaction::OnStreamFailed(
580 int result,
581 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22582 const SSLConfig& used_ssl_config,
583 const ProxyInfo& used_proxy_info) {
[email protected]82918cc2010-08-25 17:24:50584 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38585 DCHECK_NE(OK, result);
586 DCHECK(stream_request_.get());
587 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14588 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07589 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22590 proxy_info_ = used_proxy_info;
591 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38592
593 OnIOComplete(result);
594}
595
[email protected]102e27c2011-02-23 01:01:31596void HttpNetworkTransaction::OnCertificateError(
597 int result,
598 const SSLConfig& used_ssl_config,
599 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50600 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38601 DCHECK_NE(OK, result);
602 DCHECK(stream_request_.get());
603 DCHECK(!stream_.get());
604
605 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14606 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38607
608 // TODO(mbelshe): For now, we're going to pass the error through, and that
609 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50610 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
611 // good and the user chooses to ignore the error. This is not ideal, but not
612 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38613
614 OnIOComplete(result);
615}
616
617void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50618 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31619 const SSLConfig& used_ssl_config,
620 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50621 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38622 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50623 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38624
625 establishing_tunnel_ = true;
626 response_.headers = proxy_response.headers;
627 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50628 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20629
630 if (response_.headers.get() && !ContentEncodingsValid()) {
631 DoCallback(ERR_CONTENT_DECODING_FAILED);
632 return;
633 }
634
[email protected]8e6441ca2010-08-19 05:56:38635 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14636 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31637 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38638
639 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
640 pending_auth_target_ = HttpAuth::AUTH_PROXY;
641
642 DoCallback(OK);
643}
644
645void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31646 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50647 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50648 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38649
[email protected]102957f2011-09-02 17:10:14650 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38651 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58652 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38653}
654
zhongyi48704c182015-12-07 07:52:02655void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12656 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02657}
658
ttuttle1f2d7e92015-04-28 16:17:47659void HttpNetworkTransaction::GetConnectionAttempts(
660 ConnectionAttempts* out) const {
661 *out = connection_attempts_;
662}
663
Adam Ricecb76ac62015-02-20 05:33:25664bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37665 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52666}
667
Adam Rice425cf122015-01-19 06:18:24668bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12669 return (proxy_info_.is_http() || proxy_info_.is_https() ||
670 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24671 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
672}
673
initial.commit586acc5fe2008-07-26 22:42:52674void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50675 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41676 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52677
Douglas Creageref5eecdc2018-11-09 20:50:36678#if BUILDFLAG(ENABLE_REPORTING)
679 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42680 // report about this network request if the result was an error.
681 GenerateNetworkErrorLoggingReportIfError(rv);
682#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36683
[email protected]96d570e42008-08-05 22:43:04684 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27685 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52686}
687
688void HttpNetworkTransaction::OnIOComplete(int result) {
689 int rv = DoLoop(result);
690 if (rv != ERR_IO_PENDING)
691 DoCallback(rv);
692}
693
694int HttpNetworkTransaction::DoLoop(int result) {
695 DCHECK(next_state_ != STATE_NONE);
696
697 int rv = result;
698 do {
699 State state = next_state_;
700 next_state_ = STATE_NONE;
701 switch (state) {
[email protected]1826a402014-01-08 15:40:48702 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
703 DCHECK_EQ(OK, rv);
704 rv = DoNotifyBeforeCreateStream();
705 break;
[email protected]82918cc2010-08-25 17:24:50706 case STATE_CREATE_STREAM:
707 DCHECK_EQ(OK, rv);
708 rv = DoCreateStream();
709 break;
710 case STATE_CREATE_STREAM_COMPLETE:
[email protected]82918cc2010-08-25 17:24:50711 rv = DoCreateStreamComplete(rv);
[email protected]82918cc2010-08-25 17:24:50712 break;
[email protected]351ab642010-08-05 16:55:31713 case STATE_INIT_STREAM:
714 DCHECK_EQ(OK, rv);
715 rv = DoInitStream();
716 break;
717 case STATE_INIT_STREAM_COMPLETE:
718 rv = DoInitStreamComplete(rv);
719 break;
[email protected]044de0642010-06-17 10:42:15720 case STATE_GENERATE_PROXY_AUTH_TOKEN:
721 DCHECK_EQ(OK, rv);
722 rv = DoGenerateProxyAuthToken();
723 break;
724 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
725 rv = DoGenerateProxyAuthTokenComplete(rv);
726 break;
727 case STATE_GENERATE_SERVER_AUTH_TOKEN:
728 DCHECK_EQ(OK, rv);
729 rv = DoGenerateServerAuthToken();
730 break;
731 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
732 rv = DoGenerateServerAuthTokenComplete(rv);
733 break;
[email protected]daddea62012-09-19 05:51:13734 case STATE_INIT_REQUEST_BODY:
735 DCHECK_EQ(OK, rv);
736 rv = DoInitRequestBody();
737 break;
738 case STATE_INIT_REQUEST_BODY_COMPLETE:
739 rv = DoInitRequestBodyComplete(rv);
740 break;
[email protected]4875ba12011-03-30 22:31:51741 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55742 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00743 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51744 rv = DoBuildRequest();
745 break;
746 case STATE_BUILD_REQUEST_COMPLETE:
747 rv = DoBuildRequestComplete(rv);
748 break;
749 case STATE_SEND_REQUEST:
750 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57751 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52752 break;
[email protected]0877e3d2009-10-17 22:29:57753 case STATE_SEND_REQUEST_COMPLETE:
754 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43755 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00756 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52757 break;
758 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55759 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00760 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52761 rv = DoReadHeaders();
762 break;
763 case STATE_READ_HEADERS_COMPLETE:
764 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43765 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00766 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52767 break;
768 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55769 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00770 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52771 rv = DoReadBody();
772 break;
773 case STATE_READ_BODY_COMPLETE:
774 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43775 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00776 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52777 break;
[email protected]2d2697f92009-02-18 21:00:32778 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55779 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53780 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00781 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32782 rv = DoDrainBodyForAuthRestart();
783 break;
784 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
785 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43786 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00787 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32788 break;
initial.commit586acc5fe2008-07-26 22:42:52789 default:
790 NOTREACHED() << "bad state";
791 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04792 break;
initial.commit586acc5fe2008-07-26 22:42:52793 }
794 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
795
796 return rv;
797}
798
[email protected]1826a402014-01-08 15:40:48799int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
800 next_state_ = STATE_CREATE_STREAM;
801 bool defer = false;
802 if (!before_network_start_callback_.is_null())
803 before_network_start_callback_.Run(&defer);
804 if (!defer)
805 return OK;
806 return ERR_IO_PENDING;
807}
808
[email protected]82918cc2010-08-25 17:24:50809int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55810 response_.network_accessed = true;
811
[email protected]82918cc2010-08-25 17:24:50812 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17813 // IP based pooling is only enabled on a retry after 421 Misdirected Request
814 // is received. Alternative Services are also disabled in this case (though
815 // they can also be disabled when retrying after a QUIC error).
816 if (!enable_ip_based_pooling_)
817 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44818 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27819 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06820 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
821 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
822 websocket_handshake_stream_base_create_helper_,
823 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44824 } else {
xunjieli96f2a402017-06-05 17:24:27825 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29826 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27827 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44828 }
[email protected]26816882010-10-14 18:03:09829 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38830 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31831}
832
[email protected]82918cc2010-08-25 17:24:50833int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59834 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59835 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50836 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38837 DCHECK(stream_.get());
bncfacdd852015-01-09 19:22:54838 } else if (result == ERR_HTTP_1_1_REQUIRED ||
839 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
840 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59841 }
842
David Benjamin76a40ad2018-02-24 22:22:08843 // Handle possible client certificate errors that may have occurred if the
844 // stream used SSL for one or more of the layers.
845 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30846
[email protected]8e6441ca2010-08-19 05:56:38847 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09848 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38849 return result;
[email protected]394816e92010-08-03 07:38:59850}
851
[email protected]82918cc2010-08-25 17:24:50852int HttpNetworkTransaction::DoInitStream() {
853 DCHECK(stream_.get());
854 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59855
856 stream_->GetRemoteEndpoint(&remote_endpoint_);
857
Steven Valdezb4ff0412018-01-18 22:39:27858 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
859 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50860}
861
862int HttpNetworkTransaction::DoInitStreamComplete(int result) {
863 if (result == OK) {
864 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50865 } else {
[email protected]82918cc2010-08-25 17:24:50866 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26867 result = HandleIOError(result);
868
869 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22870 if (stream_) {
871 total_received_bytes_ += stream_->GetTotalReceivedBytes();
872 total_sent_bytes_ += stream_->GetTotalSentBytes();
873 }
zhongyica364fbb2015-12-12 03:39:12874 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50875 }
876
877 return result;
878}
879
[email protected]044de0642010-06-17 10:42:15880int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
881 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
882 if (!ShouldApplyProxyAuth())
883 return OK;
[email protected]394816e92010-08-03 07:38:59884 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
885 if (!auth_controllers_[target].get())
Matt Menkebe090422019-10-18 20:25:26886 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
887 target, AuthURL(target), request_->network_isolation_key,
888 session_->http_auth_cache(), session_->http_auth_handler_factory(),
889 session_->host_resolver(),
Kevin DiClementef07119082019-08-12 13:31:34890 session_->params().allow_default_credentials);
[email protected]394816e92010-08-03 07:38:59891 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41892 io_callback_,
[email protected]394816e92010-08-03 07:38:59893 net_log_);
[email protected]044de0642010-06-17 10:42:15894}
895
896int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
897 DCHECK_NE(ERR_IO_PENDING, rv);
898 if (rv == OK)
899 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
900 return rv;
901}
902
903int HttpNetworkTransaction::DoGenerateServerAuthToken() {
904 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59905 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54906 if (!auth_controllers_[target].get()) {
Matt Menkebe090422019-10-18 20:25:26907 auth_controllers_[target] = base::MakeRefCounted<HttpAuthController>(
908 target, AuthURL(target), request_->network_isolation_key,
909 session_->http_auth_cache(), session_->http_auth_handler_factory(),
910 session_->host_resolver(),
Kevin DiClementef07119082019-08-12 13:31:34911 session_->params().allow_default_credentials);
[email protected]2217aa22013-10-11 03:03:54912 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
913 auth_controllers_[target]->DisableEmbeddedIdentity();
914 }
[email protected]044de0642010-06-17 10:42:15915 if (!ShouldApplyServerAuth())
916 return OK;
[email protected]394816e92010-08-03 07:38:59917 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41918 io_callback_,
[email protected]394816e92010-08-03 07:38:59919 net_log_);
[email protected]044de0642010-06-17 10:42:15920}
921
922int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
923 DCHECK_NE(ERR_IO_PENDING, rv);
924 if (rv == OK)
nharperd6e65822016-03-30 23:05:48925 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14926 return rv;
927}
928
929int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24930 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14931 request_headers_.SetHeader(HttpRequestHeaders::kHost,
932 GetHostAndOptionalPort(request_->url));
933
934 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24935 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14936 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
937 "keep-alive");
938 } else {
939 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
940 }
941
[email protected]2979a492011-04-06 00:29:14942 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11943 if (request_->upload_data_stream) {
944 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14945 request_headers_.SetHeader(
946 HttpRequestHeaders::kTransferEncoding, "chunked");
947 } else {
948 request_headers_.SetHeader(
949 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09950 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14951 }
csharrisonf473dd192015-08-18 13:54:13952 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14953 // An empty POST/PUT request still needs a content length. As for HEAD,
954 // IE and Safari also add a content length header. Presumably it is to
955 // support sending a HEAD request to an URL that only expects to be sent a
956 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13957 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
958 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14959 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
960 }
961
[email protected]2979a492011-04-06 00:29:14962 // Honor load flags that impact proxy caches.
963 if (request_->load_flags & LOAD_BYPASS_CACHE) {
964 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
965 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
966 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
967 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
968 }
969
970 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
971 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
972 &request_headers_);
973 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
974 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
975 &request_headers_);
976
[email protected]c10450102011-06-27 09:06:16977 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20978
ryansturm49a8cb12016-06-15 16:51:09979 if (!before_headers_sent_callback_.is_null())
980 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:20981
[email protected]173f8e22013-04-10 04:18:20982 response_.did_use_http_auth =
983 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
984 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14985 return OK;
986}
987
[email protected]daddea62012-09-19 05:51:13988int HttpNetworkTransaction::DoInitRequestBody() {
989 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13990 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11991 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:33992 rv = request_->upload_data_stream->Init(
993 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
994 base::Unretained(this)),
995 net_log_);
[email protected]daddea62012-09-19 05:51:13996 return rv;
997}
[email protected]4875ba12011-03-30 22:31:51998
[email protected]daddea62012-09-19 05:51:13999int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1000 if (result == OK)
1001 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131002 return result;
1003}
1004
1005int HttpNetworkTransaction::DoBuildRequest() {
1006 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511007 headers_valid_ = false;
1008
1009 // This is constructed lazily (instead of within our Start method), so that
1010 // we have proxy info available.
1011 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241012 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141013 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511014 }
1015
[email protected]4875ba12011-03-30 22:31:511016 return OK;
1017}
1018
1019int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241020 if (result == OK)
1021 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511022 return result;
1023}
1024
[email protected]0877e3d2009-10-17 22:29:571025int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251026 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571027 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1028
[email protected]bf3eb002012-11-15 05:50:111029 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521030}
1031
[email protected]0877e3d2009-10-17 22:29:571032int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251033 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361034
1035 if (result == ERR_HTTP_1_1_REQUIRED ||
1036 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1037 return HandleHttp11Required(result);
1038 }
1039
initial.commit586acc5fe2008-07-26 22:42:521040 if (result < 0)
1041 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571042 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521043 return OK;
1044}
1045
1046int HttpNetworkTransaction::DoReadHeaders() {
1047 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411048 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521049}
1050
1051int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111052 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1053 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381054 if (IsCertificateError(result)) {
1055 // We don't handle a certificate error during SSL renegotiation, so we
1056 // have to return an error that's not in the certificate error range
1057 // (-2xx).
David Benjaminbac8dff2019-08-07 01:30:411058 //
1059 // TODO(davidben): Remove this error. This is impossible now that server
1060 // certificates are forbidden from changing in renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381061 LOG(ERROR) << "Got a server certificate with error " << result
1062 << " during SSL renegotiation";
1063 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1064 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381065 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251066 DCHECK(IsSecureRequest());
David Benjamin1c4b6d012019-07-08 17:12:571067 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]90499482013-06-01 00:39:501068 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
David Benjaminbac8dff2019-08-07 01:30:411069 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1070 total_sent_bytes_ += stream_->GetTotalSentBytes();
1071 stream_->Close(true);
1072 CacheNetErrorDetailsAndResetStream();
[email protected]2181ea002009-06-09 01:37:271073 }
1074
bncfacdd852015-01-09 19:22:541075 if (result == ERR_HTTP_1_1_REQUIRED ||
1076 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1077 return HandleHttp11Required(result);
1078 }
1079
[email protected]c871864d72014-03-13 19:56:191080 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1081 // response headers were received, we do the best we can to make sense of it
1082 // and send it back up the stack.
1083 //
1084 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1085 // bizarre for SPDY. Assuming this logic is useful at all.
1086 // TODO(davidben): Bubble the error code up so we do not cache?
1087 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1088 result = OK;
1089
1090 if (result < 0)
1091 return HandleIOError(result);
1092
[email protected]90499482013-06-01 00:39:501093 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521094
eustasc7d27da2017-04-06 10:33:201095 if (response_.headers.get() && !ContentEncodingsValid())
1096 return ERR_CONTENT_DECODING_FAILED;
1097
[email protected]d58ceea82014-06-04 10:55:541098 // On a 408 response from the server ("Request Timeout") on a stale socket,
David Schinazif832cb82019-11-08 22:25:271099 // retry the request for HTTP/1.1 but not HTTP/2 or QUIC because those
1100 // multiplex requests and have no need for 408.
[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 &&
David Schinazif832cb82019-11-08 22:25:271104 HttpResponseInfo::ConnectionInfoToCoarse(response_.connection_info) ==
1105 HttpResponseInfo::CONNECTION_INFO_COARSE_HTTP1 &&
[email protected]d58ceea82014-06-04 10:55:541106 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421107#if BUILDFLAG(ENABLE_REPORTING)
1108 GenerateNetworkErrorLoggingReport(OK);
1109#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541110 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001111 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541112 response_.headers->response_code());
1113 // This will close the socket - it would be weird to try and reuse it, even
1114 // if the server doesn't actually close it.
1115 ResetConnectionAndRequestForResend();
1116 return OK;
1117 }
1118
Eric Roman06bd9742019-07-13 15:19:131119 NetLogResponseHeaders(net_log_,
1120 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1121 response_.headers.get());
Andrey Kosyakov2e893e62017-08-31 17:00:521122 if (response_headers_callback_)
1123 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391124
bncbe0f6af2015-10-15 17:49:561125 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571126 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1127 // indicates a buggy server. See:
1128 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1129 if (request_->method == "PUT")
1130 return ERR_METHOD_NOT_SUPPORTED;
1131 }
[email protected]4ddaf2502008-10-23 18:26:191132
Steven Valdez58097ec32018-07-16 18:29:041133 if (can_send_early_data_ && response_.headers.get() &&
1134 response_.headers->response_code() == HTTP_TOO_EARLY) {
1135 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1136 }
1137
[email protected]0877e3d2009-10-17 22:29:571138 // Check for an intermediate 100 Continue response. An origin server is
1139 // allowed to send this response even if we didn't ask for it, so we just
1140 // need to skip over it.
1141 // We treat any other 1xx in this same way (although in practice getting
1142 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441143 // Unless this is a WebSocket request, in which case we pass it on up.
1144 if (response_.headers->response_code() / 100 == 1 &&
1145 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451146 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571147 next_state_ = STATE_READ_HEADERS;
1148 return OK;
1149 }
1150
davidbence688ae2017-05-04 15:12:591151 if (response_.headers->response_code() == 421 &&
1152 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421153#if BUILDFLAG(ENABLE_REPORTING)
1154 GenerateNetworkErrorLoggingReport(OK);
1155#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591156 // Retry the request with both IP based pooling and Alternative Services
1157 // disabled.
1158 enable_ip_based_pooling_ = false;
1159 enable_alternative_services_ = false;
1160 net_log_.AddEvent(
1161 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1162 ResetConnectionAndRequestForResend();
1163 return OK;
bnc8016c1f2017-03-31 02:11:291164 }
1165
bncb26024382016-06-29 02:39:451166 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491167 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081168 if (response_.ssl_info.is_valid() &&
1169 !IsCertStatusError(response_.ssl_info.cert_status)) {
1170 session_->http_stream_factory()->ProcessAlternativeServices(
Matt Menkeb32ba5122019-09-10 19:17:051171 session_, network_isolation_key_, response_.headers.get(),
Bence Béky230ac612017-08-30 19:17:081172 url::SchemeHostPort(request_->url));
1173 }
1174 }
asanka5ffd5d72016-03-23 16:20:491175
[email protected]e772db3f2010-07-12 18:11:131176 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571177 if (rv != OK)
1178 return rv;
1179
Douglas Creager3cb042052018-11-06 23:08:521180#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421181 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1182 // reports generated before the NEL header is processed here will just be
1183 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141184 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521185 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421186
1187 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111188 // code), or if the response body will not be read, or on a redirect.
1189 // Note: This will report a success for a redirect even if an error is
1190 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421191 int response_code = response_.headers->response_code();
1192 if ((response_code >= 400 && response_code < 600) ||
1193 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1194 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111195 response_.headers->GetContentLength() == 0 ||
1196 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421197 GenerateNetworkErrorLoggingReport(OK);
1198 }
1199#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521200
[email protected]0877e3d2009-10-17 22:29:571201 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261202
1203 // We have reached the end of Start state machine, set the RequestInfo to
1204 // null.
1205 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1206 // only until the final response headers are received. Clearing it will ensure
1207 // that HttpRequestInfo is only used up until final response headers are
1208 // received. Clearing is allowed so that the transaction can be disassociated
1209 // from its creating consumer in cases where it is shared for writing to the
1210 // cache. It is also safe to set it to null at this point since
1211 // upload_data_stream is also not used in the Read state machine.
1212 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1213 request_ = nullptr;
1214
[email protected]0877e3d2009-10-17 22:29:571215 return OK;
initial.commit586acc5fe2008-07-26 22:42:521216}
1217
1218int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501219 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131220 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351221 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521222
1223 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501224 return stream_->ReadResponseBody(
1225 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521226}
1227
1228int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1229 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381230 bool done = false;
1231 if (result <= 0) {
1232 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521233 done = true;
[email protected]8e6441ca2010-08-19 05:56:381234 }
[email protected]9492e4a2010-02-24 00:58:461235
mmenkebd84c392015-09-02 14:12:341236 // Clean up connection if we are done.
1237 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381238 // Note: Just because IsResponseBodyComplete is true, we're not
1239 // necessarily "done". We're only "done" when it is the last
1240 // read on this HttpNetworkTransaction, which will be signified
1241 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341242 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381243 // the stream. No need to compute it here just to pass back
1244 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341245 bool keep_alive =
1246 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521247
[email protected]8e6441ca2010-08-19 05:56:381248 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011249 // Note: we don't reset the stream here. We've closed it, but we still
1250 // need it around so that callers can call methods such as
1251 // GetUploadProgress() and have them be meaningful.
1252 // TODO(mbelshe): This means we closed the stream here, and we close it
1253 // again in ~HttpNetworkTransaction. Clean that up.
1254
[email protected]8e6441ca2010-08-19 05:56:381255 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171256
1257 // This transaction was successful. If it had been retried because of an
1258 // error with an alternative service, mark that alternative service broken.
1259 if (!enable_alternative_services_ &&
1260 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411261 HistogramBrokenAlternateProtocolLocation(
1262 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171263 session_->http_server_properties()->MarkAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051264 retried_alternative_service_, network_isolation_key_);
rch2f2991c2017-04-13 19:28:171265 }
Lily Chenfec60d92019-01-24 01:16:421266
1267#if BUILDFLAG(ENABLE_REPORTING)
1268 GenerateNetworkErrorLoggingReport(result);
1269#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521270 }
1271
1272 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351273 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521274 read_buf_len_ = 0;
1275
1276 return result;
1277}
1278
[email protected]2d2697f92009-02-18 21:00:321279int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1280 // This method differs from DoReadBody only in the next_state_. So we just
1281 // call DoReadBody and override the next_state_. Perhaps there is a more
1282 // elegant way for these two methods to share code.
1283 int rv = DoReadBody();
1284 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1285 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1286 return rv;
1287}
1288
[email protected]0877e3d2009-10-17 22:29:571289// TODO(wtc): This method and the DoReadBodyComplete method are almost
1290// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321291int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231292 // keep_alive defaults to true because the very reason we're draining the
1293 // response body is to reuse the connection for auth restart.
1294 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321295 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571296 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421297 // Note: No Network Error Logging report is generated here because a report
1298 // will have already been generated for the original request due to the auth
1299 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321300 done = true;
[email protected]68873ba2009-06-04 21:49:231301 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311302 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571303 done = true;
[email protected]2d2697f92009-02-18 21:00:321304 }
1305
1306 if (done) {
1307 DidDrainBodyForAuthRestart(keep_alive);
1308 } else {
1309 // Keep draining.
1310 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1311 }
1312
1313 return OK;
1314}
1315
Douglas Creager3cb042052018-11-06 23:08:521316#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141317void HttpNetworkTransaction::ProcessReportToHeader() {
1318 std::string value;
1319 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1320 return;
1321
1322 ReportingService* service = session_->reporting_service();
1323 if (!service) {
1324 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1325 return;
1326 }
1327
1328 // Only accept Report-To headers on HTTPS connections that have no
1329 // certificate errors.
1330 if (!response_.ssl_info.is_valid()) {
1331 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1332 return;
1333 }
1334 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1335 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1336 return;
1337 }
1338
1339 service->ProcessHeader(url_.GetOrigin(), value);
1340}
1341
Douglas Creager3cb042052018-11-06 23:08:521342void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1343 std::string value;
1344 if (!response_.headers->GetNormalizedHeader(
1345 NetworkErrorLoggingService::kHeaderName, &value)) {
1346 return;
1347 }
1348
1349 NetworkErrorLoggingService* service =
1350 session_->network_error_logging_service();
1351 if (!service) {
1352 NetworkErrorLoggingService::
1353 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1354 return;
1355 }
1356
Lily Chen90ae93cc2019-02-14 01:15:391357 // Don't accept NEL headers received via a proxy, because the IP address of
1358 // the destination server is not known.
1359 if (response_.was_fetched_via_proxy)
1360 return;
1361
Douglas Creager3cb042052018-11-06 23:08:521362 // Only accept NEL headers on HTTPS connections that have no certificate
1363 // errors.
1364 if (!response_.ssl_info.is_valid()) {
1365 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1366 return;
1367 }
1368 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1369 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1370 return;
1371 }
1372
1373 if (remote_endpoint_.address().empty()) {
1374 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1375 return;
1376 }
1377
1378 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1379 value);
1380}
Douglas Creageref5eecdc2018-11-09 20:50:361381
Lily Chenfec60d92019-01-24 01:16:421382void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1383 if (rv < 0 && rv != ERR_IO_PENDING)
1384 GenerateNetworkErrorLoggingReport(rv);
1385}
1386
Douglas Creageref5eecdc2018-11-09 20:50:361387void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421388 // |rv| should be a valid net::Error
1389 DCHECK_NE(rv, ERR_IO_PENDING);
1390 DCHECK_LE(rv, 0);
1391
1392 if (network_error_logging_report_generated_)
1393 return;
1394 network_error_logging_report_generated_ = true;
1395
Douglas Creageref5eecdc2018-11-09 20:50:361396 NetworkErrorLoggingService* service =
1397 session_->network_error_logging_service();
1398 if (!service) {
1399 NetworkErrorLoggingService::
1400 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1401 return;
1402 }
1403
Lily Chen90ae93cc2019-02-14 01:15:391404 // Don't report on proxy auth challenges.
1405 if (response_.headers && response_.headers->response_code() ==
1406 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1407 return;
1408 }
1409
1410 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1411 // internal network details.
1412 if (response_.was_fetched_via_proxy)
1413 return;
1414
Lily Chen00196ab62018-12-04 19:52:291415 // Ignore errors from non-HTTPS origins.
1416 if (!url_.SchemeIsCryptographic()) {
1417 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1418 return;
1419 }
Douglas Creageref5eecdc2018-11-09 20:50:361420
1421 NetworkErrorLoggingService::RequestDetails details;
1422
1423 details.uri = url_;
1424 if (!request_referrer_.empty())
1425 details.referrer = GURL(request_referrer_);
1426 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421427 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361428 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421429 } else {
1430 details.server_ip = IPAddress();
1431 }
Douglas Creageref5eecdc2018-11-09 20:50:361432 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1433 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421434 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361435 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421436 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361437 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421438 }
Douglas Creageref5eecdc2018-11-09 20:50:361439 // If we got response headers, assume that the connection used HTTP/1.1
1440 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421441 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361442 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421443 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361444 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361445 }
Lily Chenfec60d92019-01-24 01:16:421446 details.method = request_method_;
1447 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361448 details.type = static_cast<Error>(rv);
1449 details.reporting_upload_depth = request_reporting_upload_depth_;
1450
1451 service->OnRequest(std::move(details));
1452}
Lily Chenfec60d92019-01-24 01:16:421453#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521454
bncfacdd852015-01-09 19:22:541455int HttpNetworkTransaction::HandleHttp11Required(int error) {
1456 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1457 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1458
Matt Menke0e3276862019-08-23 00:21:341459 // HttpServerProperties should have been updated, so when the request is sent
1460 // again, it will automatically use HTTP/1.1.
bncfacdd852015-01-09 19:22:541461 ResetConnectionAndRequestForResend();
1462 return OK;
1463}
1464
David Benjamin76a40ad2018-02-24 22:22:081465int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjaminbac8dff2019-08-07 01:30:411466 // Client certificate errors may come from either the origin server or the
1467 // proxy.
1468 //
1469 // Origin errors are handled here, while most proxy errors are handled in the
1470 // HttpStreamFactory and below. However, if the request is not tunneled (i.e.
1471 // the origin is HTTP, so there is no HTTPS connection) and the proxy does not
1472 // report a bad client certificate until after the TLS handshake completes.
1473 // The latter occurs in TLS 1.3 or TLS 1.2 with False Start (disabled for
1474 // proxies). The error will then surface out of Read() rather than Connect()
1475 // and ultimately surfaced out of DoReadHeadersComplete().
David Benjamin3b94b0f2019-04-25 23:07:521476 //
1477 // See https://crbug.com/828965.
David Benjaminbac8dff2019-08-07 01:30:411478 bool is_server = !UsingHttpProxyWithoutTunnel();
1479 HostPortPair host_port_pair =
1480 is_server ? HostPortPair::FromURL(request_->url)
1481 : proxy_info_.proxy_server().host_port_pair();
David Benjamin3b94b0f2019-04-25 23:07:521482
David Benjaminbac8dff2019-08-07 01:30:411483 if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) {
David Benjamin3b94b0f2019-04-25 23:07:521484 DCHECK((is_server && IsSecureRequest()) || proxy_info_.is_https());
David Benjaminbac8dff2019-08-07 01:30:411485 if (session_->ssl_client_context()->ClearClientCertificate(
1486 host_port_pair)) {
1487 // The private key handle may have gone stale due to, e.g., the user
1488 // unplugging their smartcard. Operating systems do not provide reliable
1489 // notifications for this, so if the signature failed and the user was
1490 // not already prompted for certificate on this request, retry to ask
1491 // the user for a new one.
1492 //
1493 // TODO(davidben): There is no corresponding feature for proxy client
1494 // certificates. Ideally this would live at a lower level, common to both,
1495 // but |configured_client_cert_for_server_| is not accessible below the
1496 // socket pools.
1497 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1498 !configured_client_cert_for_server_ && !HasExceededMaxRetries()) {
1499 retry_attempts_++;
1500 net_log_.AddEventWithNetErrorCode(
1501 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1502 ResetConnectionAndRequestForResend();
1503 return OK;
1504 }
David Benjamin76a40ad2018-02-24 22:22:081505 }
1506 }
initial.commit586acc5fe2008-07-26 22:42:521507 return error;
1508}
1509
[email protected]bd0b6772011-01-11 19:59:301510// This method determines whether it is safe to resend the request after an
1511// IO error. It can only be called in response to request header or body
1512// write errors or response header read errors. It should not be used in
1513// other cases, such as a Connect error.
1514int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031515 // Because the peer may request renegotiation with client authentication at
1516 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081517 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301518
Lily Chenfec60d92019-01-24 01:16:421519#if BUILDFLAG(ENABLE_REPORTING)
1520 GenerateNetworkErrorLoggingReportIfError(error);
1521#endif // BUILDFLAG(ENABLE_REPORTING)
1522
[email protected]bd0b6772011-01-11 19:59:301523 switch (error) {
1524 // If we try to reuse a connection that the server is in the process of
1525 // closing, we may end up successfully writing out our request (or a
1526 // portion of our request) only to find a connection error when we try to
1527 // read from (or finish writing to) the socket.
1528 case ERR_CONNECTION_RESET:
1529 case ERR_CONNECTION_CLOSED:
1530 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511531 // There can be a race between the socket pool checking checking whether a
1532 // socket is still connected, receiving the FIN, and sending/reading data
1533 // on a reused socket. If we receive the FIN between the connectedness
1534 // check and writing/reading from the socket, we may first learn the socket
1535 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1536 // likely happen when trying to retrieve its IP address.
1537 // See http://crbug.com/105824 for more details.
1538 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491539 // If a socket is closed on its initial request, HttpStreamParser returns
1540 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1541 // preconnected but failed to be used before the server timed it out.
1542 case ERR_EMPTY_RESPONSE:
1543 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381544 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001545 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301546 ResetConnectionAndRequestForResend();
1547 error = OK;
1548 }
1549 break;
Steven Valdez58097ec32018-07-16 18:29:041550 case ERR_EARLY_DATA_REJECTED:
1551 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1552 net_log_.AddEventWithNetErrorCode(
1553 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1554 // Disable early data on the SSLConfig on a reset.
1555 can_send_early_data_ = false;
1556 ResetConnectionAndRequestForResend();
1557 error = OK;
1558 break;
Bence Békyd0d69502019-06-25 19:47:181559 case ERR_HTTP2_PING_FAILED:
1560 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1561 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1562 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1563 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271564 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141565 if (HasExceededMaxRetries())
1566 break;
[email protected]b6754252012-06-13 23:14:381567 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001568 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141569 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001570 ResetConnectionAndRequestForResend();
1571 error = OK;
1572 break;
rch2f2991c2017-04-13 19:28:171573 case ERR_QUIC_PROTOCOL_ERROR:
1574 if (GetResponseHeaders() != nullptr ||
1575 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1576 // If the response headers have already been recieved and passed up
1577 // then the request can not be retried. Also, if there was no
1578 // alternative service used for this request, then there is no
1579 // alternative service to be disabled.
1580 break;
1581 }
Biljith Jayan45a41722017-08-16 18:43:141582 if (HasExceededMaxRetries())
1583 break;
rch2f2991c2017-04-13 19:28:171584 if (session_->http_server_properties()->IsAlternativeServiceBroken(
Matt Menkeb32ba5122019-09-10 19:17:051585 retried_alternative_service_, network_isolation_key_)) {
rch2f2991c2017-04-13 19:28:171586 // If the alternative service was marked as broken while the request
1587 // was in flight, retry the request which will not use the broken
1588 // alternative service.
1589 net_log_.AddEventWithNetErrorCode(
1590 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141591 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171592 ResetConnectionAndRequestForResend();
1593 error = OK;
Nick Harper72ade192019-07-17 03:30:421594 } else if (session_->params()
1595 .quic_params.retry_without_alt_svc_on_quic_errors) {
rch2f2991c2017-04-13 19:28:171596 // Disable alternative services for this request and retry it. If the
1597 // retry succeeds, then the alternative service will be marked as
1598 // broken then.
1599 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361600 net_log_.AddEventWithNetErrorCode(
1601 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141602 retry_attempts_++;
rch514a44a82017-04-13 04:46:361603 ResetConnectionAndRequestForResend();
1604 error = OK;
1605 }
1606 break;
[email protected]bd0b6772011-01-11 19:59:301607 }
1608 return error;
1609}
1610
[email protected]c3b35c22008-09-27 03:19:421611void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581612 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221613 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191614 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221615 total_sent_bytes_ += stream_->GetTotalSentBytes();
1616 }
zhongyica364fbb2015-12-12 03:39:121617 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581618}
1619
1620void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251621 send_start_time_ = base::TimeTicks();
1622 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251623
[email protected]0757e7702009-03-27 04:00:221624 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351625 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421626 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571627 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201628 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141629 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381630 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591631 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121632 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521633 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421634#if BUILDFLAG(ENABLE_REPORTING)
1635 network_error_logging_report_generated_ = false;
1636 start_timeticks_ = base::TimeTicks::Now();
1637#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121638}
1639
1640void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1641 if (stream_)
1642 stream_->PopulateNetErrorDetails(&net_error_details_);
1643 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571644}
1645
[email protected]0877e3d2009-10-17 22:29:571646HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501647 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421648}
1649
[email protected]a34f61ee2014-03-18 20:59:491650bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381651 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351652 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161653
[email protected]2a5c76b2008-09-25 22:15:161654 // NOTE: we resend a request only if we reused a keep-alive connection.
1655 // This automatically prevents an infinite resend loop because we'll run
1656 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381657 if (connection_is_proven && !has_received_headers)
1658 return true;
1659 return false;
[email protected]1c773ea12009-04-28 19:58:421660}
1661
Biljith Jayan45a41722017-08-16 18:43:141662bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1663 return (retry_attempts_ >= kMaxRetryAttempts);
1664}
1665
David Benjamin83ddfb32018-03-30 01:07:521666bool HttpNetworkTransaction::CheckMaxRestarts() {
1667 num_restarts_++;
1668 return num_restarts_ < kMaxRestarts;
1669}
1670
[email protected]1c773ea12009-04-28 19:58:421671void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381672 if (stream_.get()) {
1673 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121674 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161675 }
1676
[email protected]0877e3d2009-10-17 22:29:571677 // We need to clear request_headers_ because it contains the real request
1678 // headers, but we may need to resend the CONNECT request first to recreate
1679 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201680 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501681 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421682
1683#if BUILDFLAG(ENABLE_REPORTING)
1684 // Reset for new request.
1685 network_error_logging_report_generated_ = false;
1686 start_timeticks_ = base::TimeTicks::Now();
1687#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221688
1689 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541690}
1691
[email protected]1c773ea12009-04-28 19:58:421692bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241693 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421694}
license.botbf09a502008-08-24 00:55:551695
[email protected]1c773ea12009-04-28 19:58:421696bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041697 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421698}
1699
[email protected]e772db3f2010-07-12 18:11:131700int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271701 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501702 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421703
[email protected]0877e3d2009-10-17 22:29:571704 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581705 if (status != HTTP_UNAUTHORIZED &&
1706 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421707 return OK;
[email protected]9094b602012-02-27 21:44:581708 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111709 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161710 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1711 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421712
[email protected]9094b602012-02-27 21:44:581713 // This case can trigger when an HTTPS server responds with a "Proxy
1714 // authentication required" status code through a non-authenticating
1715 // proxy.
[email protected]7a67a8152010-11-05 18:31:101716 if (!auth_controllers_[target].get())
1717 return ERR_UNEXPECTED_PROXY_AUTH;
1718
[email protected]a7ea8832010-07-12 17:54:541719 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491720 headers, response_.ssl_info,
1721 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311722 net_log_);
[email protected]228404f2010-06-24 04:31:411723 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491724 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411725
Emily Starkf2c9bbd2019-04-09 17:08:581726 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411727
[email protected]228404f2010-06-24 04:31:411728 return rv;
[email protected]f9ee6b52008-11-08 06:46:231729}
1730
[email protected]8e6441ca2010-08-19 05:56:381731bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1732 return auth_controllers_[target].get() &&
1733 auth_controllers_[target]->HaveAuth();
1734}
1735
[email protected]228404f2010-06-24 04:31:411736GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1737 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461738 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411739 if (!proxy_info_.proxy_server().is_valid() ||
1740 proxy_info_.proxy_server().is_direct()) {
1741 return GURL(); // There is no proxy server.
1742 }
[email protected]2df19bb2010-08-25 20:13:461743 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1744 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351745 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461746 }
[email protected]228404f2010-06-24 04:31:411747 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291748 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361749 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291750 }
[email protected]228404f2010-06-24 04:31:411751 return request_->url;
1752 default:
1753 return GURL();
1754 }
[email protected]c3b35c22008-09-27 03:19:421755}
1756
[email protected]831e4a32013-11-14 02:14:441757bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141758 return websocket_handshake_stream_base_create_helper_ &&
1759 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441760}
1761
ttuttle1f2d7e92015-04-28 16:17:471762void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1763 DCHECK(stream_request_);
1764
1765 // Since the transaction can restart with auth credentials, it may create a
1766 // stream more than once. Accumulate all of the connection attempts across
1767 // those streams by appending them to the vector:
1768 for (const auto& attempt : stream_request_->connection_attempts())
1769 connection_attempts_.push_back(attempt);
1770}
1771
eustasc7d27da2017-04-06 10:33:201772bool HttpNetworkTransaction::ContentEncodingsValid() const {
1773 HttpResponseHeaders* headers = GetResponseHeaders();
1774 DCHECK(headers);
1775
1776 std::string accept_encoding;
1777 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1778 &accept_encoding);
1779 std::set<std::string> allowed_encodings;
Paul Jensene85e8152019-07-01 15:38:001780 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
eustasc7d27da2017-04-06 10:33:201781 return false;
eustasc7d27da2017-04-06 10:33:201782
1783 std::string content_encoding;
1784 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1785 std::set<std::string> used_encodings;
Paul Jensene85e8152019-07-01 15:38:001786 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
eustasc7d27da2017-04-06 10:33:201787 return false;
eustasc7d27da2017-04-06 10:33:201788
1789 // When "Accept-Encoding" is not specified, it is parsed as "*".
1790 // If "*" encoding is advertised, then any encoding should be "accepted".
1791 // This does not mean, that it will be successfully decoded.
1792 if (allowed_encodings.find("*") != allowed_encodings.end())
1793 return true;
1794
sky50576f32017-05-01 19:28:031795 bool result = true;
eustasc7d27da2017-04-06 10:33:201796 for (auto const& encoding : used_encodings) {
1797 SourceStream::SourceType source_type =
1798 FilterSourceStream::ParseEncodingType(encoding);
1799 // We don't reject encodings we are not aware. They just will not decode.
1800 if (source_type == SourceStream::TYPE_UNKNOWN)
1801 continue;
1802 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031803 result = false;
1804 break;
eustasc7d27da2017-04-06 10:33:201805 }
1806 }
sky50576f32017-05-01 19:28:031807
1808 // Temporary workaround for http://crbug.com/714514
1809 if (headers->IsRedirect(nullptr)) {
1810 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1811 !result);
1812 return true;
1813 }
1814
1815 return result;
eustasc7d27da2017-04-06 10:33:201816}
1817
[email protected]c3b35c22008-09-27 03:19:421818} // namespace net