blob: 9aea7f000c9f77437072f4df68728b3e550c82c5 [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"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
Douglas Creager3cb042052018-11-06 23:08:5271#if BUILDFLAG(ENABLE_REPORTING)
72#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1473#include "net/reporting/reporting_header_parser.h"
74#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4275#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5276
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2277namespace net {
78
Biljith Jayan45a41722017-08-16 18:43:1479namespace {
David Benjamin83ddfb32018-03-30 01:07:5280
Biljith Jayan45a41722017-08-16 18:43:1481// Max number of |retry_attempts| (excluding the initial request) after which
82// we give up and show an error page.
83const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5284
85// Max number of calls to RestartWith* allowed for a single connection. A single
86// HttpNetworkTransaction should not signal very many restartable errors, but it
87// may occur due to a bug (e.g. https://crbug.com/823387 or
88// https://crbug.com/488043) or simply if the server or proxy requests
89// authentication repeatedly. Although these calls are often associated with a
90// user prompt, in other scenarios (remembered preferences, extensions,
91// multi-leg authentication), they may be triggered automatically. To avoid
92// looping forever, bound the number of restarts.
93const size_t kMaxRestarts = 32;
94
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2295void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
96 HttpResponseInfo* response_info) {
97 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
98 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
99 response_info->proxy_server = proxy_info.proxy_server();
100 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
101 response_info->proxy_server = ProxyServer::Direct();
102 else
103 response_info->proxy_server = ProxyServer();
104}
Biljith Jayan45a41722017-08-16 18:43:14105
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22106} // namespace
initial.commit586acc5fe2008-07-26 22:42:52107
Victor Costan9c7302b2018-08-27 16:39:44108const int HttpNetworkTransaction::kDrainBodyBufferSize;
109
[email protected]262eec82013-03-19 21:01:36110HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
111 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22112 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53113 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
114 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52115 session_(session),
Raul Tambre94493c652019-03-11 17:18:35116 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36117 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57118 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27119 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08120 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20121 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42122#if BUILDFLAG(ENABLE_REPORTING)
123 network_error_logging_report_generated_(false),
124 request_reporting_upload_depth_(0),
125#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52126 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19127 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22128 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54129 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44130 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29131 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26132 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35133 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14134 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52135 retry_attempts_(0),
David Benjamind61bd532019-04-23 21:11:37136 num_restarts_(0) {
Lily Chenfec60d92019-01-24 01:16:42137}
[email protected]3ce7df0f2010-03-03 00:30:50138
[email protected]0b0bf032010-09-21 18:08:50139HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42140#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11141 // If no error or success report has been generated yet at this point, then
142 // this network transaction was prematurely cancelled.
143 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42144#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50145 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50146 // TODO(mbelshe): The stream_ should be able to compute whether or not the
147 // stream should be kept alive. No reason to compute here
148 // and pass it in.
mmenkebd84c392015-09-02 14:12:34149 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50150 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34151 } else if (stream_->IsResponseBodyComplete()) {
152 // If the response body is complete, we can just reuse the socket.
153 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50154 } else {
mmenkebd84c392015-09-02 14:12:34155 // Otherwise, we try to drain the response body.
156 HttpStream* stream = stream_.release();
157 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50158 }
159 }
[email protected]02cad5d2013-10-02 08:14:03160 if (request_ && request_->upload_data_stream)
161 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50162}
163
[email protected]684970b2009-08-14 04:54:46164int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04165 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20166 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23167 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
168 return ERR_CACHE_MISS;
169
Ramin Halavatib5e433e2018-02-07 07:41:10170 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53171 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04172 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15173 url_ = request_->url;
Douglas Creageref5eecdc2018-11-09 20:50:36174#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42175 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36176 request_method_ = request_->method;
177 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
178 &request_referrer_);
179 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
180 &request_user_agent_);
181 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42182 start_timeticks_ = base::TimeTicks::Now();
183#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04184
nharper8cdb0fb2016-04-22 21:34:59185 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
186 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
187
Ryan Sleevi24fe2682018-08-16 21:33:46188 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
189 server_ssl_config_.disable_cert_verification_network_fetches = true;
190 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19191 }
[email protected]6fbac162011-06-20 00:29:04192
Steven Valdez03e872d2018-03-02 15:39:15193 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27194 can_send_early_data_ = true;
195 }
196
jkarlinfb1d5172015-01-12 14:10:29197 if (request_->load_flags & LOAD_PREFETCH)
198 response_.unused_since_prefetch = true;
199
Helen Lib495c3802018-03-30 13:46:09200 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04201 int rv = DoLoop(OK);
202 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04203 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42204
205 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
206 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
207 // other net::Error can be returned.
208 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04209 return rv;
210}
211
212int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04213 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38214 DCHECK(!stream_.get());
215 DCHECK(!stream_request_.get());
216 DCHECK_EQ(STATE_NONE, next_state_);
217
David Benjamin83ddfb32018-03-30 01:07:52218 if (!CheckMaxRestarts())
219 return ERR_TOO_MANY_RETRIES;
220
[email protected]82918cc2010-08-25 17:24:50221 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38222
[email protected]ccb40e52008-09-17 20:54:40223 int rv = DoLoop(OK);
224 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04225 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42226
227 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
228 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
229 // other net::Error can be returned.
230 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11231 return rv;
[email protected]96d570e42008-08-05 22:43:04232}
233
[email protected]0b45559b2009-06-12 21:45:11234int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08235 scoped_refptr<X509Certificate> client_cert,
236 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04237 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38238 // In HandleCertificateRequest(), we always tear down existing stream
239 // requests to force a new connection. So we shouldn't have one here.
240 DCHECK(!stream_request_.get());
241 DCHECK(!stream_.get());
242 DCHECK_EQ(STATE_NONE, next_state_);
243
David Benjamin83ddfb32018-03-30 01:07:52244 if (!CheckMaxRestarts())
245 return ERR_TOO_MANY_RETRIES;
246
[email protected]102957f2011-09-02 17:10:14247 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
248 &proxy_ssl_config_ : &server_ssl_config_;
249 ssl_config->send_client_cert = true;
250 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54251 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45252 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08253 response_.cert_request_info->host_and_port, std::move(client_cert),
254 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11255 // Reset the other member variables.
256 // Note: this is necessary only with SSL renegotiation.
257 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50258 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11259 int rv = DoLoop(OK);
260 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04261 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42262
263 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
264 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
265 // other net::Error can be returned.
266 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11267 return rv;
268}
269
Bence Béky046f8c82018-06-12 02:26:04270int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
271 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52272 if (!CheckMaxRestarts())
273 return ERR_TOO_MANY_RETRIES;
274
[email protected]0757e7702009-03-27 04:00:22275 HttpAuth::Target target = pending_auth_target_;
276 if (target == HttpAuth::AUTH_NONE) {
277 NOTREACHED();
278 return ERR_UNEXPECTED;
279 }
[email protected]0757e7702009-03-27 04:00:22280 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42281
[email protected]f3cf9802011-10-28 18:44:58282 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13283
[email protected]49639fa2011-12-20 23:22:41284 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38285
286 int rv = OK;
287 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
288 // In this case, we've gathered credentials for use with proxy
289 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50290 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35291 DCHECK(stream_request_ != nullptr);
292 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54293 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39294 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54295 } else {
[email protected]8e6441ca2010-08-19 05:56:38296 // In this case, we've gathered credentials for the server or the proxy
297 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35298 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54299 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38300 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42301 // Note: If an error is encountered while draining the old response body, no
302 // Network Error Logging report will be generated, because the error was
303 // with the old request, which will already have had a NEL report generated
304 // for it due to the auth challenge (so we don't report a second error for
305 // that request).
[email protected]a7ea8832010-07-12 17:54:54306 }
[email protected]c3b35c22008-09-27 03:19:42307
[email protected]c3b35c22008-09-27 03:19:42308 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04309 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42310 return rv;
[email protected]96d570e42008-08-05 22:43:04311}
312
[email protected]f9ee6b52008-11-08 06:46:23313void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
314 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38315 DCHECK(!stream_request_.get());
316
Bence Béky3238f2e12017-09-22 22:44:49317 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
318 if (target == HttpAuth::AUTH_SERVER &&
319 auth_controllers_[target]->NeedsHTTP11()) {
320 session_->http_server_properties()->SetHTTP11Required(
321 HostPortPair::FromURL(request_->url));
322 }
323
[email protected]2d2697f92009-02-18 21:00:32324 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57325 // Even if the server says the connection is keep-alive, we have to be
326 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34327 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57328 // If the response body hasn't been completely read, we need to drain
329 // it first.
[email protected]351ab642010-08-05 16:55:31330 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32331 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44332 read_buf_ = base::MakeRefCounted<IOBuffer>(
333 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32334 read_buf_len_ = kDrainBodyBufferSize;
335 return;
336 }
[email protected]0877e3d2009-10-17 22:29:57337 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09338 }
339
[email protected]2d2697f92009-02-18 21:00:32340 // We don't need to drain the response body, so we act as if we had drained
341 // the response body.
342 DidDrainBodyForAuthRestart(keep_alive);
343}
344
345void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38346 DCHECK(!stream_request_.get());
347
348 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19349 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22350 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35351 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34352 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38353 // We should call connection_->set_idle_time(), but this doesn't occur
354 // often enough to be worth the trouble.
355 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39356 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38357 }
[email protected]697ef4c2010-10-14 16:38:58358
359 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46360 // Close the stream and mark it as not_reusable. Even in the
361 // keep_alive case, we've determined that the stream_ is not
362 // reusable if new_stream is NULL.
363 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58364 next_state_ = STATE_CREATE_STREAM;
365 } else {
sclittlefb249892015-09-10 21:33:22366 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19367 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22368 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58369 next_state_ = STATE_INIT_STREAM;
370 }
371 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32372 }
[email protected]f9ee6b52008-11-08 06:46:23373
374 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58375 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23376}
377
[email protected]8e6441ca2010-08-19 05:56:38378bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
379 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
380 HaveAuth(pending_auth_target_);
381}
382
Bence Béky046f8c82018-06-12 02:26:04383int HttpNetworkTransaction::Read(IOBuffer* buf,
384 int buf_len,
385 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04386 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35387 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04388
[email protected]ad8e04a2010-11-01 04:16:27389 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38390 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04391 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29392 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04393 // bytes when establishing a tunnel because they might be controlled by
394 // an active network attacker. We don't worry about this for HTTP
395 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29396 // We reach this case when the user cancels a 407 proxy auth prompt. We
397 // also don't worry about this for an HTTPS Proxy, because the
398 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04399 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56400 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
401 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58402 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]8a1f3312010-05-25 19:25:04403 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44404 }
405
[email protected]e60e47a2010-07-14 03:37:18406 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15407 next_state_ = STATE_READ_BODY;
408
[email protected]96d570e42008-08-05 22:43:04409 read_buf_ = buf;
410 read_buf_len_ = buf_len;
411
[email protected]96d570e42008-08-05 22:43:04412 int rv = DoLoop(OK);
413 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04414 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04415 return rv;
416}
417
[email protected]8cd06c02014-01-25 07:50:14418void HttpNetworkTransaction::StopCaching() {}
419
[email protected]79e1fd62013-06-20 06:50:04420bool HttpNetworkTransaction::GetFullRequestHeaders(
421 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32422 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04423 *headers = request_headers_;
424 return true;
425}
426
sclittle4de1bab92015-09-22 21:28:24427int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
428 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19429 if (stream_)
430 total_received_bytes += stream_->GetTotalReceivedBytes();
431 return total_received_bytes;
432}
433
sclittlefb249892015-09-10 21:33:22434int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
435 int64_t total_sent_bytes = total_sent_bytes_;
436 if (stream_)
437 total_sent_bytes += stream_->GetTotalSentBytes();
438 return total_sent_bytes;
439}
440
[email protected]8cd06c02014-01-25 07:50:14441void HttpNetworkTransaction::DoneReading() {}
442
[email protected]96d570e42008-08-05 22:43:04443const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55444 return &response_;
[email protected]96d570e42008-08-05 22:43:04445}
446
447LoadState HttpNetworkTransaction::GetLoadState() const {
448 // TODO(wtc): Define a new LoadState value for the
449 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
450 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48451 case STATE_CREATE_STREAM:
452 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50453 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38454 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15455 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
456 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57457 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04458 return LOAD_STATE_SENDING_REQUEST;
459 case STATE_READ_HEADERS_COMPLETE:
460 return LOAD_STATE_WAITING_FOR_RESPONSE;
461 case STATE_READ_BODY_COMPLETE:
462 return LOAD_STATE_READING_RESPONSE;
463 default:
464 return LOAD_STATE_IDLE;
465 }
466}
467
[email protected]8cd06c02014-01-25 07:50:14468void HttpNetworkTransaction::SetQuicServerInfo(
469 QuicServerInfo* quic_server_info) {}
470
[email protected]5033ab82013-03-22 20:17:46471bool HttpNetworkTransaction::GetLoadTimingInfo(
472 LoadTimingInfo* load_timing_info) const {
473 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
474 return false;
475
476 load_timing_info->proxy_resolve_start =
477 proxy_info_.proxy_resolve_start_time();
478 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
479 load_timing_info->send_start = send_start_time_;
480 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46481 return true;
482}
483
ttuttled9dbc652015-09-29 20:00:59484bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36485 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59486 return false;
487
488 *endpoint = remote_endpoint_;
489 return true;
490}
491
zhongyi48704c182015-12-07 07:52:02492void HttpNetworkTransaction::PopulateNetErrorDetails(
493 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12494 *details = net_error_details_;
495 if (stream_)
496 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02497}
498
[email protected]5033ab82013-03-22 20:17:46499void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
500 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50501
[email protected]bf828982013-08-14 18:01:47502 if (stream_request_)
503 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03504 if (stream_)
505 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50506
rdsmith1d343be52016-10-21 20:37:50507 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46508}
509
[email protected]831e4a32013-11-14 02:14:44510void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
511 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
512 websocket_handshake_stream_base_create_helper_ = create_helper;
513}
514
[email protected]1826a402014-01-08 15:40:48515void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
516 const BeforeNetworkStartCallback& callback) {
517 before_network_start_callback_ = callback;
518}
519
ryansturm49a8cb12016-06-15 16:51:09520void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
521 const BeforeHeadersSentCallback& callback) {
522 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27523}
524
Andrey Kosyakov83a6eee2017-08-14 19:20:04525void HttpNetworkTransaction::SetRequestHeadersCallback(
526 RequestHeadersCallback callback) {
527 DCHECK(!stream_);
528 request_headers_callback_ = std::move(callback);
529}
530
Andrey Kosyakov2e893e62017-08-31 17:00:52531void HttpNetworkTransaction::SetResponseHeadersCallback(
532 ResponseHeadersCallback callback) {
533 DCHECK(!stream_);
534 response_headers_callback_ = std::move(callback);
535}
536
[email protected]1826a402014-01-08 15:40:48537int HttpNetworkTransaction::ResumeNetworkStart() {
538 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
539 return DoLoop(OK);
540}
541
[email protected]102e27c2011-02-23 01:01:31542void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
543 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00544 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50545 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38546 DCHECK(stream_request_.get());
547
sclittlefb249892015-09-10 21:33:22548 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19549 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22550 total_sent_bytes_ += stream_->GetTotalSentBytes();
551 }
bnc5029f4632017-06-08 16:19:00552 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04553 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14554 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31555 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52556 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21557 response_.alpn_negotiated_protocol =
558 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38559 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22560 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38561 OnIOComplete(OK);
562}
563
xunjieli5749218c2016-03-22 16:43:06564void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08565 const SSLConfig& used_ssl_config,
566 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00567 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08568 NOTREACHED();
569}
570
[email protected]a9cf2b92013-10-30 12:08:49571void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50572 const SSLConfig& used_ssl_config,
573 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00574 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
575 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50576}
577
Ryan Hamilton75f197262017-08-17 14:00:07578void HttpNetworkTransaction::OnStreamFailed(
579 int result,
580 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22581 const SSLConfig& used_ssl_config,
582 const ProxyInfo& used_proxy_info) {
[email protected]82918cc2010-08-25 17:24:50583 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38584 DCHECK_NE(OK, result);
585 DCHECK(stream_request_.get());
586 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14587 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07588 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22589 proxy_info_ = used_proxy_info;
590 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38591
592 OnIOComplete(result);
593}
594
[email protected]102e27c2011-02-23 01:01:31595void HttpNetworkTransaction::OnCertificateError(
596 int result,
597 const SSLConfig& used_ssl_config,
598 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50599 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38600 DCHECK_NE(OK, result);
601 DCHECK(stream_request_.get());
602 DCHECK(!stream_.get());
603
604 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14605 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38606
607 // TODO(mbelshe): For now, we're going to pass the error through, and that
608 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50609 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
610 // good and the user chooses to ignore the error. This is not ideal, but not
611 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38612
613 OnIOComplete(result);
614}
615
616void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50617 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31618 const SSLConfig& used_ssl_config,
619 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50620 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38621 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50622 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38623
624 establishing_tunnel_ = true;
625 response_.headers = proxy_response.headers;
626 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50627 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20628
629 if (response_.headers.get() && !ContentEncodingsValid()) {
630 DoCallback(ERR_CONTENT_DECODING_FAILED);
631 return;
632 }
633
[email protected]8e6441ca2010-08-19 05:56:38634 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14635 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31636 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38637
638 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
639 pending_auth_target_ = HttpAuth::AUTH_PROXY;
640
641 DoCallback(OK);
642}
643
644void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31645 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50646 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50647 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38648
[email protected]102957f2011-09-02 17:10:14649 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38650 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58651 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38652}
653
zhongyi48704c182015-12-07 07:52:02654void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12655 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02656}
657
ttuttle1f2d7e92015-04-28 16:17:47658void HttpNetworkTransaction::GetConnectionAttempts(
659 ConnectionAttempts* out) const {
660 *out = connection_attempts_;
661}
662
Adam Ricecb76ac62015-02-20 05:33:25663bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37664 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52665}
666
Adam Rice425cf122015-01-19 06:18:24667bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12668 return (proxy_info_.is_http() || proxy_info_.is_https() ||
669 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24670 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
671}
672
initial.commit586acc5fe2008-07-26 22:42:52673void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50674 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41675 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52676
Douglas Creageref5eecdc2018-11-09 20:50:36677#if BUILDFLAG(ENABLE_REPORTING)
678 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42679 // report about this network request if the result was an error.
680 GenerateNetworkErrorLoggingReportIfError(rv);
681#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36682
[email protected]96d570e42008-08-05 22:43:04683 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27684 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52685}
686
687void HttpNetworkTransaction::OnIOComplete(int result) {
688 int rv = DoLoop(result);
689 if (rv != ERR_IO_PENDING)
690 DoCallback(rv);
691}
692
693int HttpNetworkTransaction::DoLoop(int result) {
694 DCHECK(next_state_ != STATE_NONE);
695
696 int rv = result;
697 do {
698 State state = next_state_;
699 next_state_ = STATE_NONE;
700 switch (state) {
[email protected]1826a402014-01-08 15:40:48701 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
702 DCHECK_EQ(OK, rv);
703 rv = DoNotifyBeforeCreateStream();
704 break;
[email protected]82918cc2010-08-25 17:24:50705 case STATE_CREATE_STREAM:
706 DCHECK_EQ(OK, rv);
707 rv = DoCreateStream();
708 break;
709 case STATE_CREATE_STREAM_COMPLETE:
[email protected]82918cc2010-08-25 17:24:50710 rv = DoCreateStreamComplete(rv);
[email protected]82918cc2010-08-25 17:24:50711 break;
[email protected]351ab642010-08-05 16:55:31712 case STATE_INIT_STREAM:
713 DCHECK_EQ(OK, rv);
714 rv = DoInitStream();
715 break;
716 case STATE_INIT_STREAM_COMPLETE:
717 rv = DoInitStreamComplete(rv);
718 break;
[email protected]044de0642010-06-17 10:42:15719 case STATE_GENERATE_PROXY_AUTH_TOKEN:
720 DCHECK_EQ(OK, rv);
721 rv = DoGenerateProxyAuthToken();
722 break;
723 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
724 rv = DoGenerateProxyAuthTokenComplete(rv);
725 break;
726 case STATE_GENERATE_SERVER_AUTH_TOKEN:
727 DCHECK_EQ(OK, rv);
728 rv = DoGenerateServerAuthToken();
729 break;
730 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
731 rv = DoGenerateServerAuthTokenComplete(rv);
732 break;
[email protected]daddea62012-09-19 05:51:13733 case STATE_INIT_REQUEST_BODY:
734 DCHECK_EQ(OK, rv);
735 rv = DoInitRequestBody();
736 break;
737 case STATE_INIT_REQUEST_BODY_COMPLETE:
738 rv = DoInitRequestBodyComplete(rv);
739 break;
[email protected]4875ba12011-03-30 22:31:51740 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55741 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00742 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51743 rv = DoBuildRequest();
744 break;
745 case STATE_BUILD_REQUEST_COMPLETE:
746 rv = DoBuildRequestComplete(rv);
747 break;
748 case STATE_SEND_REQUEST:
749 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57750 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52751 break;
[email protected]0877e3d2009-10-17 22:29:57752 case STATE_SEND_REQUEST_COMPLETE:
753 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43754 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00755 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52756 break;
757 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55758 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00759 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52760 rv = DoReadHeaders();
761 break;
762 case STATE_READ_HEADERS_COMPLETE:
763 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43764 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00765 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52766 break;
767 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55768 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00769 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52770 rv = DoReadBody();
771 break;
772 case STATE_READ_BODY_COMPLETE:
773 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43774 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00775 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52776 break;
[email protected]2d2697f92009-02-18 21:00:32777 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55778 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53779 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00780 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32781 rv = DoDrainBodyForAuthRestart();
782 break;
783 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
784 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43785 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00786 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32787 break;
initial.commit586acc5fe2008-07-26 22:42:52788 default:
789 NOTREACHED() << "bad state";
790 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04791 break;
initial.commit586acc5fe2008-07-26 22:42:52792 }
793 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
794
795 return rv;
796}
797
[email protected]1826a402014-01-08 15:40:48798int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
799 next_state_ = STATE_CREATE_STREAM;
800 bool defer = false;
801 if (!before_network_start_callback_.is_null())
802 before_network_start_callback_.Run(&defer);
803 if (!defer)
804 return OK;
805 return ERR_IO_PENDING;
806}
807
[email protected]82918cc2010-08-25 17:24:50808int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55809 response_.network_accessed = true;
810
[email protected]82918cc2010-08-25 17:24:50811 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17812 // IP based pooling is only enabled on a retry after 421 Misdirected Request
813 // is received. Alternative Services are also disabled in this case (though
814 // they can also be disabled when retrying after a QUIC error).
815 if (!enable_ip_based_pooling_)
816 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44817 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27818 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06819 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
820 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
821 websocket_handshake_stream_base_create_helper_,
822 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44823 } else {
xunjieli96f2a402017-06-05 17:24:27824 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29825 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27826 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44827 }
[email protected]26816882010-10-14 18:03:09828 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38829 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31830}
831
[email protected]82918cc2010-08-25 17:24:50832int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59833 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59834 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50835 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38836 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33837 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
838 result = HandleCertificateRequest(result);
bncfacdd852015-01-09 19:22:54839 } else if (result == ERR_HTTP_1_1_REQUIRED ||
840 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
841 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59842 }
843
David Benjamin76a40ad2018-02-24 22:22:08844 // Handle possible client certificate errors that may have occurred if the
845 // stream used SSL for one or more of the layers.
846 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30847
[email protected]8e6441ca2010-08-19 05:56:38848 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09849 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38850 return result;
[email protected]394816e92010-08-03 07:38:59851}
852
[email protected]82918cc2010-08-25 17:24:50853int HttpNetworkTransaction::DoInitStream() {
854 DCHECK(stream_.get());
855 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59856
857 stream_->GetRemoteEndpoint(&remote_endpoint_);
858
Steven Valdezb4ff0412018-01-18 22:39:27859 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
860 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50861}
862
863int HttpNetworkTransaction::DoInitStreamComplete(int result) {
864 if (result == OK) {
865 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50866 } else {
[email protected]82918cc2010-08-25 17:24:50867 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26868 result = HandleIOError(result);
869
870 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22871 if (stream_) {
872 total_received_bytes_ += stream_->GetTotalReceivedBytes();
873 total_sent_bytes_ += stream_->GetTotalSentBytes();
874 }
zhongyica364fbb2015-12-12 03:39:12875 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50876 }
877
878 return result;
879}
880
[email protected]044de0642010-06-17 10:42:15881int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
882 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
883 if (!ShouldApplyProxyAuth())
884 return OK;
[email protected]394816e92010-08-03 07:38:59885 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
886 if (!auth_controllers_[target].get())
Eric Orthbe2efac2019-03-06 01:11:11887 auth_controllers_[target] = new HttpAuthController(
888 target, AuthURL(target), session_->http_auth_cache(),
889 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59890 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41891 io_callback_,
[email protected]394816e92010-08-03 07:38:59892 net_log_);
[email protected]044de0642010-06-17 10:42:15893}
894
895int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
896 DCHECK_NE(ERR_IO_PENDING, rv);
897 if (rv == OK)
898 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
899 return rv;
900}
901
902int HttpNetworkTransaction::DoGenerateServerAuthToken() {
903 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59904 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54905 if (!auth_controllers_[target].get()) {
Eric Orthbe2efac2019-03-06 01:11:11906 auth_controllers_[target] = new HttpAuthController(
907 target, AuthURL(target), session_->http_auth_cache(),
908 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54909 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
910 auth_controllers_[target]->DisableEmbeddedIdentity();
911 }
[email protected]044de0642010-06-17 10:42:15912 if (!ShouldApplyServerAuth())
913 return OK;
[email protected]394816e92010-08-03 07:38:59914 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41915 io_callback_,
[email protected]394816e92010-08-03 07:38:59916 net_log_);
[email protected]044de0642010-06-17 10:42:15917}
918
919int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
920 DCHECK_NE(ERR_IO_PENDING, rv);
921 if (rv == OK)
nharperd6e65822016-03-30 23:05:48922 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14923 return rv;
924}
925
926int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24927 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14928 request_headers_.SetHeader(HttpRequestHeaders::kHost,
929 GetHostAndOptionalPort(request_->url));
930
931 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24932 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14933 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
934 "keep-alive");
935 } else {
936 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
937 }
938
[email protected]2979a492011-04-06 00:29:14939 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11940 if (request_->upload_data_stream) {
941 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14942 request_headers_.SetHeader(
943 HttpRequestHeaders::kTransferEncoding, "chunked");
944 } else {
945 request_headers_.SetHeader(
946 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09947 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14948 }
csharrisonf473dd192015-08-18 13:54:13949 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14950 // An empty POST/PUT request still needs a content length. As for HEAD,
951 // IE and Safari also add a content length header. Presumably it is to
952 // support sending a HEAD request to an URL that only expects to be sent a
953 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13954 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
955 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14956 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
957 }
958
[email protected]2979a492011-04-06 00:29:14959 // Honor load flags that impact proxy caches.
960 if (request_->load_flags & LOAD_BYPASS_CACHE) {
961 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
962 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
963 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
964 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
965 }
966
967 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
968 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
969 &request_headers_);
970 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
971 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
972 &request_headers_);
973
[email protected]c10450102011-06-27 09:06:16974 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20975
ryansturm49a8cb12016-06-15 16:51:09976 if (!before_headers_sent_callback_.is_null())
977 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:20978
[email protected]173f8e22013-04-10 04:18:20979 response_.did_use_http_auth =
980 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
981 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14982 return OK;
983}
984
[email protected]daddea62012-09-19 05:51:13985int HttpNetworkTransaction::DoInitRequestBody() {
986 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13987 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11988 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:33989 rv = request_->upload_data_stream->Init(
990 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
991 base::Unretained(this)),
992 net_log_);
[email protected]daddea62012-09-19 05:51:13993 return rv;
994}
[email protected]4875ba12011-03-30 22:31:51995
[email protected]daddea62012-09-19 05:51:13996int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
997 if (result == OK)
998 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13999 return result;
1000}
1001
1002int HttpNetworkTransaction::DoBuildRequest() {
1003 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511004 headers_valid_ = false;
1005
1006 // This is constructed lazily (instead of within our Start method), so that
1007 // we have proxy info available.
1008 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241009 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141010 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511011 }
1012
[email protected]4875ba12011-03-30 22:31:511013 return OK;
1014}
1015
1016int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241017 if (result == OK)
1018 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511019 return result;
1020}
1021
[email protected]0877e3d2009-10-17 22:29:571022int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251023 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571024 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1025
[email protected]bf3eb002012-11-15 05:50:111026 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521027}
1028
[email protected]0877e3d2009-10-17 22:29:571029int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251030 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361031
1032 if (result == ERR_HTTP_1_1_REQUIRED ||
1033 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1034 return HandleHttp11Required(result);
1035 }
1036
initial.commit586acc5fe2008-07-26 22:42:521037 if (result < 0)
1038 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571039 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521040 return OK;
1041}
1042
1043int HttpNetworkTransaction::DoReadHeaders() {
1044 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411045 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521046}
1047
1048int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111049 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1050 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381051 if (IsCertificateError(result)) {
1052 // We don't handle a certificate error during SSL renegotiation, so we
1053 // have to return an error that's not in the certificate error range
1054 // (-2xx).
1055 LOG(ERROR) << "Got a server certificate with error " << result
1056 << " during SSL renegotiation";
1057 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1058 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381059 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251060 DCHECK(IsSecureRequest());
David Benjamin1c4b6d012019-07-08 17:12:571061 response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>();
[email protected]90499482013-06-01 00:39:501062 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381063 result = HandleCertificateRequest(result);
1064 if (result == OK)
1065 return result;
[email protected]2181ea002009-06-09 01:37:271066 }
1067
bncfacdd852015-01-09 19:22:541068 if (result == ERR_HTTP_1_1_REQUIRED ||
1069 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1070 return HandleHttp11Required(result);
1071 }
1072
[email protected]c871864d72014-03-13 19:56:191073 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1074 // response headers were received, we do the best we can to make sense of it
1075 // and send it back up the stack.
1076 //
1077 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1078 // bizarre for SPDY. Assuming this logic is useful at all.
1079 // TODO(davidben): Bubble the error code up so we do not cache?
1080 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1081 result = OK;
1082
1083 if (result < 0)
1084 return HandleIOError(result);
1085
[email protected]90499482013-06-01 00:39:501086 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521087
eustasc7d27da2017-04-06 10:33:201088 if (response_.headers.get() && !ContentEncodingsValid())
1089 return ERR_CONTENT_DECODING_FAILED;
1090
[email protected]d58ceea82014-06-04 10:55:541091 // On a 408 response from the server ("Request Timeout") on a stale socket,
1092 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041093 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181094 if (response_.headers.get() &&
1095 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541096 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421097#if BUILDFLAG(ENABLE_REPORTING)
1098 GenerateNetworkErrorLoggingReport(OK);
1099#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541100 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001101 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541102 response_.headers->response_code());
1103 // This will close the socket - it would be weird to try and reuse it, even
1104 // if the server doesn't actually close it.
1105 ResetConnectionAndRequestForResend();
1106 return OK;
1107 }
1108
Eric Roman06bd9742019-07-13 15:19:131109 NetLogResponseHeaders(net_log_,
1110 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1111 response_.headers.get());
Andrey Kosyakov2e893e62017-08-31 17:00:521112 if (response_headers_callback_)
1113 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391114
bncbe0f6af2015-10-15 17:49:561115 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571116 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1117 // indicates a buggy server. See:
1118 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1119 if (request_->method == "PUT")
1120 return ERR_METHOD_NOT_SUPPORTED;
1121 }
[email protected]4ddaf2502008-10-23 18:26:191122
Steven Valdez58097ec32018-07-16 18:29:041123 if (can_send_early_data_ && response_.headers.get() &&
1124 response_.headers->response_code() == HTTP_TOO_EARLY) {
1125 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1126 }
1127
[email protected]0877e3d2009-10-17 22:29:571128 // Check for an intermediate 100 Continue response. An origin server is
1129 // allowed to send this response even if we didn't ask for it, so we just
1130 // need to skip over it.
1131 // We treat any other 1xx in this same way (although in practice getting
1132 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441133 // Unless this is a WebSocket request, in which case we pass it on up.
1134 if (response_.headers->response_code() / 100 == 1 &&
1135 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451136 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571137 next_state_ = STATE_READ_HEADERS;
1138 return OK;
1139 }
1140
davidbence688ae2017-05-04 15:12:591141 if (response_.headers->response_code() == 421 &&
1142 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421143#if BUILDFLAG(ENABLE_REPORTING)
1144 GenerateNetworkErrorLoggingReport(OK);
1145#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591146 // Retry the request with both IP based pooling and Alternative Services
1147 // disabled.
1148 enable_ip_based_pooling_ = false;
1149 enable_alternative_services_ = false;
1150 net_log_.AddEvent(
1151 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1152 ResetConnectionAndRequestForResend();
1153 return OK;
bnc8016c1f2017-03-31 02:11:291154 }
1155
bncb26024382016-06-29 02:39:451156 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491157 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081158 if (response_.ssl_info.is_valid() &&
1159 !IsCertStatusError(response_.ssl_info.cert_status)) {
1160 session_->http_stream_factory()->ProcessAlternativeServices(
1161 session_, response_.headers.get(),
1162 url::SchemeHostPort(request_->url));
1163 }
1164 }
asanka5ffd5d72016-03-23 16:20:491165
[email protected]e772db3f2010-07-12 18:11:131166 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571167 if (rv != OK)
1168 return rv;
1169
Douglas Creager3cb042052018-11-06 23:08:521170#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421171 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1172 // reports generated before the NEL header is processed here will just be
1173 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141174 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521175 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421176
1177 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111178 // code), or if the response body will not be read, or on a redirect.
1179 // Note: This will report a success for a redirect even if an error is
1180 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421181 int response_code = response_.headers->response_code();
1182 if ((response_code >= 400 && response_code < 600) ||
1183 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1184 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111185 response_.headers->GetContentLength() == 0 ||
1186 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421187 GenerateNetworkErrorLoggingReport(OK);
1188 }
1189#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521190
[email protected]0877e3d2009-10-17 22:29:571191 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261192
1193 // We have reached the end of Start state machine, set the RequestInfo to
1194 // null.
1195 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1196 // only until the final response headers are received. Clearing it will ensure
1197 // that HttpRequestInfo is only used up until final response headers are
1198 // received. Clearing is allowed so that the transaction can be disassociated
1199 // from its creating consumer in cases where it is shared for writing to the
1200 // cache. It is also safe to set it to null at this point since
1201 // upload_data_stream is also not used in the Read state machine.
1202 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1203 request_ = nullptr;
1204
[email protected]0877e3d2009-10-17 22:29:571205 return OK;
initial.commit586acc5fe2008-07-26 22:42:521206}
1207
1208int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501209 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131210 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351211 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521212
1213 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501214 return stream_->ReadResponseBody(
1215 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521216}
1217
1218int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1219 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381220 bool done = false;
1221 if (result <= 0) {
1222 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521223 done = true;
[email protected]8e6441ca2010-08-19 05:56:381224 }
[email protected]9492e4a2010-02-24 00:58:461225
mmenkebd84c392015-09-02 14:12:341226 // Clean up connection if we are done.
1227 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381228 // Note: Just because IsResponseBodyComplete is true, we're not
1229 // necessarily "done". We're only "done" when it is the last
1230 // read on this HttpNetworkTransaction, which will be signified
1231 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341232 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381233 // the stream. No need to compute it here just to pass back
1234 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341235 bool keep_alive =
1236 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521237
[email protected]8e6441ca2010-08-19 05:56:381238 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011239 // Note: we don't reset the stream here. We've closed it, but we still
1240 // need it around so that callers can call methods such as
1241 // GetUploadProgress() and have them be meaningful.
1242 // TODO(mbelshe): This means we closed the stream here, and we close it
1243 // again in ~HttpNetworkTransaction. Clean that up.
1244
[email protected]8e6441ca2010-08-19 05:56:381245 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171246
1247 // This transaction was successful. If it had been retried because of an
1248 // error with an alternative service, mark that alternative service broken.
1249 if (!enable_alternative_services_ &&
1250 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411251 HistogramBrokenAlternateProtocolLocation(
1252 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171253 session_->http_server_properties()->MarkAlternativeServiceBroken(
1254 retried_alternative_service_);
1255 }
Lily Chenfec60d92019-01-24 01:16:421256
1257#if BUILDFLAG(ENABLE_REPORTING)
1258 GenerateNetworkErrorLoggingReport(result);
1259#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521260 }
1261
1262 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351263 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521264 read_buf_len_ = 0;
1265
1266 return result;
1267}
1268
[email protected]2d2697f92009-02-18 21:00:321269int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1270 // This method differs from DoReadBody only in the next_state_. So we just
1271 // call DoReadBody and override the next_state_. Perhaps there is a more
1272 // elegant way for these two methods to share code.
1273 int rv = DoReadBody();
1274 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1275 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1276 return rv;
1277}
1278
[email protected]0877e3d2009-10-17 22:29:571279// TODO(wtc): This method and the DoReadBodyComplete method are almost
1280// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321281int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231282 // keep_alive defaults to true because the very reason we're draining the
1283 // response body is to reuse the connection for auth restart.
1284 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321285 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571286 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421287 // Note: No Network Error Logging report is generated here because a report
1288 // will have already been generated for the original request due to the auth
1289 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321290 done = true;
[email protected]68873ba2009-06-04 21:49:231291 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311292 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571293 done = true;
[email protected]2d2697f92009-02-18 21:00:321294 }
1295
1296 if (done) {
1297 DidDrainBodyForAuthRestart(keep_alive);
1298 } else {
1299 // Keep draining.
1300 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1301 }
1302
1303 return OK;
1304}
1305
Douglas Creager3cb042052018-11-06 23:08:521306#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141307void HttpNetworkTransaction::ProcessReportToHeader() {
1308 std::string value;
1309 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1310 return;
1311
1312 ReportingService* service = session_->reporting_service();
1313 if (!service) {
1314 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1315 return;
1316 }
1317
1318 // Only accept Report-To headers on HTTPS connections that have no
1319 // certificate errors.
1320 if (!response_.ssl_info.is_valid()) {
1321 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1322 return;
1323 }
1324 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1325 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1326 return;
1327 }
1328
1329 service->ProcessHeader(url_.GetOrigin(), value);
1330}
1331
Douglas Creager3cb042052018-11-06 23:08:521332void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1333 std::string value;
1334 if (!response_.headers->GetNormalizedHeader(
1335 NetworkErrorLoggingService::kHeaderName, &value)) {
1336 return;
1337 }
1338
1339 NetworkErrorLoggingService* service =
1340 session_->network_error_logging_service();
1341 if (!service) {
1342 NetworkErrorLoggingService::
1343 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1344 return;
1345 }
1346
Lily Chen90ae93cc2019-02-14 01:15:391347 // Don't accept NEL headers received via a proxy, because the IP address of
1348 // the destination server is not known.
1349 if (response_.was_fetched_via_proxy)
1350 return;
1351
Douglas Creager3cb042052018-11-06 23:08:521352 // Only accept NEL headers on HTTPS connections that have no certificate
1353 // errors.
1354 if (!response_.ssl_info.is_valid()) {
1355 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1356 return;
1357 }
1358 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1359 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1360 return;
1361 }
1362
1363 if (remote_endpoint_.address().empty()) {
1364 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1365 return;
1366 }
1367
1368 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1369 value);
1370}
Douglas Creageref5eecdc2018-11-09 20:50:361371
Lily Chenfec60d92019-01-24 01:16:421372void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1373 if (rv < 0 && rv != ERR_IO_PENDING)
1374 GenerateNetworkErrorLoggingReport(rv);
1375}
1376
Douglas Creageref5eecdc2018-11-09 20:50:361377void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421378 // |rv| should be a valid net::Error
1379 DCHECK_NE(rv, ERR_IO_PENDING);
1380 DCHECK_LE(rv, 0);
1381
1382 if (network_error_logging_report_generated_)
1383 return;
1384 network_error_logging_report_generated_ = true;
1385
Douglas Creageref5eecdc2018-11-09 20:50:361386 NetworkErrorLoggingService* service =
1387 session_->network_error_logging_service();
1388 if (!service) {
1389 NetworkErrorLoggingService::
1390 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1391 return;
1392 }
1393
Lily Chen90ae93cc2019-02-14 01:15:391394 // Don't report on proxy auth challenges.
1395 if (response_.headers && response_.headers->response_code() ==
1396 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1397 return;
1398 }
1399
1400 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1401 // internal network details.
1402 if (response_.was_fetched_via_proxy)
1403 return;
1404
Lily Chen00196ab62018-12-04 19:52:291405 // Ignore errors from non-HTTPS origins.
1406 if (!url_.SchemeIsCryptographic()) {
1407 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1408 return;
1409 }
Douglas Creageref5eecdc2018-11-09 20:50:361410
1411 NetworkErrorLoggingService::RequestDetails details;
1412
1413 details.uri = url_;
1414 if (!request_referrer_.empty())
1415 details.referrer = GURL(request_referrer_);
1416 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421417 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361418 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421419 } else {
1420 details.server_ip = IPAddress();
1421 }
Douglas Creageref5eecdc2018-11-09 20:50:361422 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1423 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421424 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361425 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421426 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361427 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421428 }
Douglas Creageref5eecdc2018-11-09 20:50:361429 // If we got response headers, assume that the connection used HTTP/1.1
1430 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421431 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361432 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421433 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361434 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361435 }
Lily Chenfec60d92019-01-24 01:16:421436 details.method = request_method_;
1437 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361438 details.type = static_cast<Error>(rv);
1439 details.reporting_upload_depth = request_reporting_upload_depth_;
1440
1441 service->OnRequest(std::move(details));
1442}
Lily Chenfec60d92019-01-24 01:16:421443#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521444
[email protected]5e363962009-06-19 19:57:011445int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381446 // There are two paths through which the server can request a certificate
1447 // from us. The first is during the initial handshake, the second is
1448 // during SSL renegotiation.
1449 //
1450 // In both cases, we want to close the connection before proceeding.
1451 // We do this for two reasons:
1452 // First, we don't want to keep the connection to the server hung for a
1453 // long time while the user selects a certificate.
1454 // Second, even if we did keep the connection open, NSS has a bug where
1455 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581456 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381457
1458 if (stream_.get()) {
1459 // Since we already have a stream, we're being called as part of SSL
1460 // renegotiation.
1461 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191462 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221463 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381464 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121465 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381466 }
1467
[email protected]26816882010-10-14 18:03:091468 // The server is asking for a client certificate during the initial
1469 // handshake.
1470 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011471
[email protected]ec229bc92010-11-22 09:51:451472 // If the user selected one of the certificates in client_certs or declined
1473 // to provide one for this server before, use the past decision
1474 // automatically.
1475 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541476 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451477 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541478 response_.cert_request_info->host_and_port, &client_cert,
1479 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451480 if (!found_cached_cert)
1481 return error;
1482
1483 // Check that the certificate selected is still a certificate the server
1484 // is likely to accept, based on the criteria supplied in the
1485 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501486 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261487 const std::vector<std::string>& cert_authorities =
1488 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451489
[email protected]f1958c382013-02-07 00:15:261490 bool cert_still_valid = cert_authorities.empty() ||
1491 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451492 if (!cert_still_valid)
1493 return error;
[email protected]5e363962009-06-19 19:57:011494 }
[email protected]ec229bc92010-11-22 09:51:451495
David Benjamin76a40ad2018-02-24 22:22:081496 if (!response_.cert_request_info->is_proxy) {
1497 server_ssl_client_cert_was_cached_ = true;
1498 }
1499
[email protected]ec229bc92010-11-22 09:51:451500 // TODO(davidben): Add a unit test which covers this path; we need to be
1501 // able to send a legitimate certificate and also bypass/clear the
1502 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141503 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1504 &proxy_ssl_config_ : &server_ssl_config_;
1505 ssl_config->send_client_cert = true;
1506 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541507 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451508 next_state_ = STATE_CREATE_STREAM;
1509 // Reset the other member variables.
1510 // Note: this is necessary only with SSL renegotiation.
1511 ResetStateForRestart();
1512 return OK;
[email protected]0b45559b2009-06-12 21:45:111513}
1514
bncfacdd852015-01-09 19:22:541515int HttpNetworkTransaction::HandleHttp11Required(int error) {
1516 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1517 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1518
1519 if (error == ERR_HTTP_1_1_REQUIRED) {
1520 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1521 } else {
1522 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1523 }
1524 ResetConnectionAndRequestForResend();
1525 return OK;
1526}
1527
David Benjamin76a40ad2018-02-24 22:22:081528int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjamin3b94b0f2019-04-25 23:07:521529 // Most client auth errors here come from the origin server, but they may come
1530 // from the proxy if the request is not tunneled (i.e. the origin is HTTP, so
1531 // there is no HTTPS connection) and the proxy does not report a bad client
1532 // certificate until after the TLS handshake completes. The latter occurs in
1533 // TLS 1.3 or TLS 1.2 with False Start (disabled for proxies). The error will
1534 // then surface out of Read() rather than Connect() and ultimately surfaced
1535 // out of DoReadHeadersComplete().
1536 //
1537 // See https://crbug.com/828965.
1538 bool is_server;
1539 SSLConfig* ssl_config;
1540 HostPortPair host_port_pair;
1541 if (UsingHttpProxyWithoutTunnel()) {
1542 is_server = false;
1543 ssl_config = &proxy_ssl_config_;
1544 host_port_pair = proxy_info_.proxy_server().host_port_pair();
1545 } else {
1546 is_server = true;
1547 ssl_config = &server_ssl_config_;
1548 host_port_pair = HostPortPair::FromURL(request_->url);
1549 }
1550
David Benjamin98ba2be2019-04-09 14:25:251551 // Client certificate errors from the proxy are handled in the
David Benjamin3b94b0f2019-04-25 23:07:521552 // HttpStreamFactory and below.
1553 if (ssl_config->send_client_cert &&
[email protected]384cc73a2013-12-08 22:41:031554 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
David Benjamin3b94b0f2019-04-25 23:07:521555 DCHECK((is_server && IsSecureRequest()) || proxy_info_.is_https());
1556 session_->ssl_client_auth_cache()->Remove(host_port_pair);
[email protected]384cc73a2013-12-08 22:41:031557
David Benjamin76a40ad2018-02-24 22:22:081558 // The private key handle may have gone stale due to, e.g., the user
1559 // unplugging their smartcard. Operating systems do not provide reliable
1560 // notifications for this, so if the signature failed and the private key
1561 // came from SSLClientAuthCache, retry to ask the user for a new one.
David Benjamin3b94b0f2019-04-25 23:07:521562 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
David Benjamin76a40ad2018-02-24 22:22:081563 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1564 server_ssl_client_cert_was_cached_ = false;
1565 server_ssl_config_.send_client_cert = false;
1566 server_ssl_config_.client_cert = nullptr;
1567 server_ssl_config_.client_private_key = nullptr;
1568 retry_attempts_++;
1569 net_log_.AddEventWithNetErrorCode(
1570 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1571 ResetConnectionAndRequestForResend();
1572 return OK;
1573 }
1574 }
initial.commit586acc5fe2008-07-26 22:42:521575 return error;
1576}
1577
[email protected]bd0b6772011-01-11 19:59:301578// This method determines whether it is safe to resend the request after an
1579// IO error. It can only be called in response to request header or body
1580// write errors or response header read errors. It should not be used in
1581// other cases, such as a Connect error.
1582int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031583 // Because the peer may request renegotiation with client authentication at
1584 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081585 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301586
Lily Chenfec60d92019-01-24 01:16:421587#if BUILDFLAG(ENABLE_REPORTING)
1588 GenerateNetworkErrorLoggingReportIfError(error);
1589#endif // BUILDFLAG(ENABLE_REPORTING)
1590
[email protected]bd0b6772011-01-11 19:59:301591 switch (error) {
1592 // If we try to reuse a connection that the server is in the process of
1593 // closing, we may end up successfully writing out our request (or a
1594 // portion of our request) only to find a connection error when we try to
1595 // read from (or finish writing to) the socket.
1596 case ERR_CONNECTION_RESET:
1597 case ERR_CONNECTION_CLOSED:
1598 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511599 // There can be a race between the socket pool checking checking whether a
1600 // socket is still connected, receiving the FIN, and sending/reading data
1601 // on a reused socket. If we receive the FIN between the connectedness
1602 // check and writing/reading from the socket, we may first learn the socket
1603 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1604 // likely happen when trying to retrieve its IP address.
1605 // See http://crbug.com/105824 for more details.
1606 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491607 // If a socket is closed on its initial request, HttpStreamParser returns
1608 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1609 // preconnected but failed to be used before the server timed it out.
1610 case ERR_EMPTY_RESPONSE:
1611 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381612 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001613 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301614 ResetConnectionAndRequestForResend();
1615 error = OK;
1616 }
1617 break;
Steven Valdez58097ec32018-07-16 18:29:041618 case ERR_EARLY_DATA_REJECTED:
1619 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1620 net_log_.AddEventWithNetErrorCode(
1621 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1622 // Disable early data on the SSLConfig on a reset.
1623 can_send_early_data_ = false;
1624 ResetConnectionAndRequestForResend();
1625 error = OK;
1626 break;
Bence Békyd0d69502019-06-25 19:47:181627 case ERR_HTTP2_PING_FAILED:
1628 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1629 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1630 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1631 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271632 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141633 if (HasExceededMaxRetries())
1634 break;
[email protected]b6754252012-06-13 23:14:381635 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001636 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141637 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001638 ResetConnectionAndRequestForResend();
1639 error = OK;
1640 break;
rch2f2991c2017-04-13 19:28:171641 case ERR_QUIC_PROTOCOL_ERROR:
1642 if (GetResponseHeaders() != nullptr ||
1643 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1644 // If the response headers have already been recieved and passed up
1645 // then the request can not be retried. Also, if there was no
1646 // alternative service used for this request, then there is no
1647 // alternative service to be disabled.
1648 break;
1649 }
Biljith Jayan45a41722017-08-16 18:43:141650 if (HasExceededMaxRetries())
1651 break;
rch2f2991c2017-04-13 19:28:171652 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1653 retried_alternative_service_)) {
1654 // If the alternative service was marked as broken while the request
1655 // was in flight, retry the request which will not use the broken
1656 // alternative service.
1657 net_log_.AddEventWithNetErrorCode(
1658 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141659 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171660 ResetConnectionAndRequestForResend();
1661 error = OK;
Nick Harper72ade192019-07-17 03:30:421662 } else if (session_->params()
1663 .quic_params.retry_without_alt_svc_on_quic_errors) {
rch2f2991c2017-04-13 19:28:171664 // Disable alternative services for this request and retry it. If the
1665 // retry succeeds, then the alternative service will be marked as
1666 // broken then.
1667 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361668 net_log_.AddEventWithNetErrorCode(
1669 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141670 retry_attempts_++;
rch514a44a82017-04-13 04:46:361671 ResetConnectionAndRequestForResend();
1672 error = OK;
1673 }
1674 break;
[email protected]bd0b6772011-01-11 19:59:301675 }
1676 return error;
1677}
1678
[email protected]c3b35c22008-09-27 03:19:421679void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581680 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221681 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191682 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221683 total_sent_bytes_ += stream_->GetTotalSentBytes();
1684 }
zhongyica364fbb2015-12-12 03:39:121685 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581686}
1687
1688void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251689 send_start_time_ = base::TimeTicks();
1690 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251691
[email protected]0757e7702009-03-27 04:00:221692 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351693 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421694 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571695 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201696 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141697 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381698 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591699 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121700 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521701 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421702#if BUILDFLAG(ENABLE_REPORTING)
1703 network_error_logging_report_generated_ = false;
1704 start_timeticks_ = base::TimeTicks::Now();
1705#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121706}
1707
1708void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1709 if (stream_)
1710 stream_->PopulateNetErrorDetails(&net_error_details_);
1711 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571712}
1713
[email protected]0877e3d2009-10-17 22:29:571714HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501715 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421716}
1717
[email protected]a34f61ee2014-03-18 20:59:491718bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381719 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351720 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161721
[email protected]2a5c76b2008-09-25 22:15:161722 // NOTE: we resend a request only if we reused a keep-alive connection.
1723 // This automatically prevents an infinite resend loop because we'll run
1724 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381725 if (connection_is_proven && !has_received_headers)
1726 return true;
1727 return false;
[email protected]1c773ea12009-04-28 19:58:421728}
1729
Biljith Jayan45a41722017-08-16 18:43:141730bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1731 return (retry_attempts_ >= kMaxRetryAttempts);
1732}
1733
David Benjamin83ddfb32018-03-30 01:07:521734bool HttpNetworkTransaction::CheckMaxRestarts() {
1735 num_restarts_++;
1736 return num_restarts_ < kMaxRestarts;
1737}
1738
[email protected]1c773ea12009-04-28 19:58:421739void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381740 if (stream_.get()) {
1741 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121742 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161743 }
1744
[email protected]0877e3d2009-10-17 22:29:571745 // We need to clear request_headers_ because it contains the real request
1746 // headers, but we may need to resend the CONNECT request first to recreate
1747 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201748 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501749 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421750
1751#if BUILDFLAG(ENABLE_REPORTING)
1752 // Reset for new request.
1753 network_error_logging_report_generated_ = false;
1754 start_timeticks_ = base::TimeTicks::Now();
1755#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221756
1757 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541758}
1759
[email protected]1c773ea12009-04-28 19:58:421760bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241761 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421762}
license.botbf09a502008-08-24 00:55:551763
[email protected]1c773ea12009-04-28 19:58:421764bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041765 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421766}
1767
[email protected]e772db3f2010-07-12 18:11:131768int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271769 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501770 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421771
[email protected]0877e3d2009-10-17 22:29:571772 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581773 if (status != HTTP_UNAUTHORIZED &&
1774 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421775 return OK;
[email protected]9094b602012-02-27 21:44:581776 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111777 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161778 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1779 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421780
[email protected]9094b602012-02-27 21:44:581781 // This case can trigger when an HTTPS server responds with a "Proxy
1782 // authentication required" status code through a non-authenticating
1783 // proxy.
[email protected]7a67a8152010-11-05 18:31:101784 if (!auth_controllers_[target].get())
1785 return ERR_UNEXPECTED_PROXY_AUTH;
1786
[email protected]a7ea8832010-07-12 17:54:541787 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491788 headers, response_.ssl_info,
1789 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311790 net_log_);
[email protected]228404f2010-06-24 04:31:411791 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491792 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411793
Emily Starkf2c9bbd2019-04-09 17:08:581794 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411795
[email protected]228404f2010-06-24 04:31:411796 return rv;
[email protected]f9ee6b52008-11-08 06:46:231797}
1798
[email protected]8e6441ca2010-08-19 05:56:381799bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1800 return auth_controllers_[target].get() &&
1801 auth_controllers_[target]->HaveAuth();
1802}
1803
[email protected]228404f2010-06-24 04:31:411804GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1805 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461806 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411807 if (!proxy_info_.proxy_server().is_valid() ||
1808 proxy_info_.proxy_server().is_direct()) {
1809 return GURL(); // There is no proxy server.
1810 }
[email protected]2df19bb2010-08-25 20:13:461811 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1812 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351813 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461814 }
[email protected]228404f2010-06-24 04:31:411815 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291816 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361817 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291818 }
[email protected]228404f2010-06-24 04:31:411819 return request_->url;
1820 default:
1821 return GURL();
1822 }
[email protected]c3b35c22008-09-27 03:19:421823}
1824
[email protected]831e4a32013-11-14 02:14:441825bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141826 return websocket_handshake_stream_base_create_helper_ &&
1827 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441828}
1829
ttuttle1f2d7e92015-04-28 16:17:471830void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1831 DCHECK(stream_request_);
1832
1833 // Since the transaction can restart with auth credentials, it may create a
1834 // stream more than once. Accumulate all of the connection attempts across
1835 // those streams by appending them to the vector:
1836 for (const auto& attempt : stream_request_->connection_attempts())
1837 connection_attempts_.push_back(attempt);
1838}
1839
eustasc7d27da2017-04-06 10:33:201840bool HttpNetworkTransaction::ContentEncodingsValid() const {
1841 HttpResponseHeaders* headers = GetResponseHeaders();
1842 DCHECK(headers);
1843
1844 std::string accept_encoding;
1845 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1846 &accept_encoding);
1847 std::set<std::string> allowed_encodings;
Paul Jensene85e8152019-07-01 15:38:001848 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings))
eustasc7d27da2017-04-06 10:33:201849 return false;
eustasc7d27da2017-04-06 10:33:201850
1851 std::string content_encoding;
1852 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1853 std::set<std::string> used_encodings;
Paul Jensene85e8152019-07-01 15:38:001854 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings))
eustasc7d27da2017-04-06 10:33:201855 return false;
eustasc7d27da2017-04-06 10:33:201856
1857 // When "Accept-Encoding" is not specified, it is parsed as "*".
1858 // If "*" encoding is advertised, then any encoding should be "accepted".
1859 // This does not mean, that it will be successfully decoded.
1860 if (allowed_encodings.find("*") != allowed_encodings.end())
1861 return true;
1862
sky50576f32017-05-01 19:28:031863 bool result = true;
eustasc7d27da2017-04-06 10:33:201864 for (auto const& encoding : used_encodings) {
1865 SourceStream::SourceType source_type =
1866 FilterSourceStream::ParseEncodingType(encoding);
1867 // We don't reject encodings we are not aware. They just will not decode.
1868 if (source_type == SourceStream::TYPE_UNKNOWN)
1869 continue;
1870 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031871 result = false;
1872 break;
eustasc7d27da2017-04-06 10:33:201873 }
1874 }
sky50576f32017-05-01 19:28:031875
1876 // Temporary workaround for http://crbug.com/714514
1877 if (headers->IsRedirect(nullptr)) {
1878 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1879 !result);
1880 return true;
1881 }
1882
1883 return result;
eustasc7d27da2017-04-06 10:33:201884}
1885
[email protected]c3b35c22008-09-27 03:19:421886} // namespace net