blob: e83171a23a1167c77fd61da32de30171e069e34a [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"
43#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5444#include "net/http/http_proxy_client_socket.h"
[email protected]270c6412010-03-29 22:02:4745#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5246#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2147#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5748#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5349#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5850#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3951#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3152#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5253#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2354#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3655#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0056#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4457#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2158#include "net/socket/next_proto.h"
[email protected]ab739042011-04-07 15:22:2859#include "net/socket/transport_client_socket_pool.h"
Bence Béky94658bf2018-05-11 19:22:5860#include "net/spdy/spdy_http_stream.h"
61#include "net/spdy/spdy_session.h"
62#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5763#include "net/ssl/ssl_cert_request_info.h"
64#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0865#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5466#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5467#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2968#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5269
Douglas Creager3cb042052018-11-06 23:08:5270#if BUILDFLAG(ENABLE_REPORTING)
71#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1472#include "net/reporting/reporting_header_parser.h"
73#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4274#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5275
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2276namespace net {
77
Biljith Jayan45a41722017-08-16 18:43:1478namespace {
David Benjamin83ddfb32018-03-30 01:07:5279
Biljith Jayan45a41722017-08-16 18:43:1480// Max number of |retry_attempts| (excluding the initial request) after which
81// we give up and show an error page.
82const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5283
84// Max number of calls to RestartWith* allowed for a single connection. A single
85// HttpNetworkTransaction should not signal very many restartable errors, but it
86// may occur due to a bug (e.g. https://crbug.com/823387 or
87// https://crbug.com/488043) or simply if the server or proxy requests
88// authentication repeatedly. Although these calls are often associated with a
89// user prompt, in other scenarios (remembered preferences, extensions,
90// multi-leg authentication), they may be triggered automatically. To avoid
91// looping forever, bound the number of restarts.
92const size_t kMaxRestarts = 32;
93
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2294void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
95 HttpResponseInfo* response_info) {
96 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
97 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
98 response_info->proxy_server = proxy_info.proxy_server();
99 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
100 response_info->proxy_server = ProxyServer::Direct();
101 else
102 response_info->proxy_server = ProxyServer();
103}
Biljith Jayan45a41722017-08-16 18:43:14104
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22105} // namespace
initial.commit586acc5fe2008-07-26 22:42:52106
Victor Costan9c7302b2018-08-27 16:39:44107const int HttpNetworkTransaction::kDrainBodyBufferSize;
108
[email protected]262eec82013-03-19 21:01:36109HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
110 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22111 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53112 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
113 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52114 session_(session),
Raul Tambre94493c652019-03-11 17:18:35115 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36116 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57117 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27118 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08119 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20120 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42121#if BUILDFLAG(ENABLE_REPORTING)
122 network_error_logging_report_generated_(false),
123 request_reporting_upload_depth_(0),
124#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52125 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19126 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22127 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54128 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44129 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29130 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26131 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35132 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14133 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52134 retry_attempts_(0),
David Benjamind61bd532019-04-23 21:11:37135 num_restarts_(0) {
Lily Chenfec60d92019-01-24 01:16:42136}
[email protected]3ce7df0f2010-03-03 00:30:50137
[email protected]0b0bf032010-09-21 18:08:50138HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42139#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11140 // If no error or success report has been generated yet at this point, then
141 // this network transaction was prematurely cancelled.
142 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42143#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50144 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50145 // TODO(mbelshe): The stream_ should be able to compute whether or not the
146 // stream should be kept alive. No reason to compute here
147 // and pass it in.
mmenkebd84c392015-09-02 14:12:34148 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50149 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34150 } else if (stream_->IsResponseBodyComplete()) {
151 // If the response body is complete, we can just reuse the socket.
152 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50153 } else {
mmenkebd84c392015-09-02 14:12:34154 // Otherwise, we try to drain the response body.
155 HttpStream* stream = stream_.release();
156 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50157 }
158 }
[email protected]02cad5d2013-10-02 08:14:03159 if (request_ && request_->upload_data_stream)
160 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50161}
162
[email protected]684970b2009-08-14 04:54:46163int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04164 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20165 const NetLogWithSource& net_log) {
Batalov Vladislava4e97a502019-04-11 15:35:23166 if (request_info->load_flags & LOAD_ONLY_FROM_CACHE)
167 return ERR_CACHE_MISS;
168
Ramin Halavatib5e433e2018-02-07 07:41:10169 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53170 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04171 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15172 url_ = request_->url;
Douglas Creageref5eecdc2018-11-09 20:50:36173#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42174 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36175 request_method_ = request_->method;
176 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
177 &request_referrer_);
178 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
179 &request_user_agent_);
180 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42181 start_timeticks_ = base::TimeTicks::Now();
182#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04183
nharper8cdb0fb2016-04-22 21:34:59184 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
185 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
186
Ryan Sleevi24fe2682018-08-16 21:33:46187 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
188 server_ssl_config_.disable_cert_verification_network_fetches = true;
189 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19190 }
[email protected]6fbac162011-06-20 00:29:04191
Steven Valdez03e872d2018-03-02 15:39:15192 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27193 can_send_early_data_ = true;
194 }
195
jkarlinfb1d5172015-01-12 14:10:29196 if (request_->load_flags & LOAD_PREFETCH)
197 response_.unused_since_prefetch = true;
198
Helen Lib495c3802018-03-30 13:46:09199 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04200 int rv = DoLoop(OK);
201 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04202 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42203
204 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
205 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
206 // other net::Error can be returned.
207 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04208 return rv;
209}
210
211int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04212 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38213 DCHECK(!stream_.get());
214 DCHECK(!stream_request_.get());
215 DCHECK_EQ(STATE_NONE, next_state_);
216
David Benjamin83ddfb32018-03-30 01:07:52217 if (!CheckMaxRestarts())
218 return ERR_TOO_MANY_RETRIES;
219
[email protected]82918cc2010-08-25 17:24:50220 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38221
[email protected]ccb40e52008-09-17 20:54:40222 int rv = DoLoop(OK);
223 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04224 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42225
226 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
227 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
228 // other net::Error can be returned.
229 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11230 return rv;
[email protected]96d570e42008-08-05 22:43:04231}
232
[email protected]0b45559b2009-06-12 21:45:11233int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08234 scoped_refptr<X509Certificate> client_cert,
235 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04236 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38237 // In HandleCertificateRequest(), we always tear down existing stream
238 // requests to force a new connection. So we shouldn't have one here.
239 DCHECK(!stream_request_.get());
240 DCHECK(!stream_.get());
241 DCHECK_EQ(STATE_NONE, next_state_);
242
David Benjamin83ddfb32018-03-30 01:07:52243 if (!CheckMaxRestarts())
244 return ERR_TOO_MANY_RETRIES;
245
[email protected]102957f2011-09-02 17:10:14246 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
247 &proxy_ssl_config_ : &server_ssl_config_;
248 ssl_config->send_client_cert = true;
249 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54250 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45251 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08252 response_.cert_request_info->host_and_port, std::move(client_cert),
253 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11254 // Reset the other member variables.
255 // Note: this is necessary only with SSL renegotiation.
256 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50257 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11258 int rv = DoLoop(OK);
259 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04260 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42261
262 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
263 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
264 // other net::Error can be returned.
265 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11266 return rv;
267}
268
Bence Béky046f8c82018-06-12 02:26:04269int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
270 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52271 if (!CheckMaxRestarts())
272 return ERR_TOO_MANY_RETRIES;
273
[email protected]0757e7702009-03-27 04:00:22274 HttpAuth::Target target = pending_auth_target_;
275 if (target == HttpAuth::AUTH_NONE) {
276 NOTREACHED();
277 return ERR_UNEXPECTED;
278 }
[email protected]0757e7702009-03-27 04:00:22279 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42280
[email protected]f3cf9802011-10-28 18:44:58281 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13282
[email protected]49639fa2011-12-20 23:22:41283 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38284
285 int rv = OK;
286 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
287 // In this case, we've gathered credentials for use with proxy
288 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50289 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35290 DCHECK(stream_request_ != nullptr);
291 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54292 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39293 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54294 } else {
[email protected]8e6441ca2010-08-19 05:56:38295 // In this case, we've gathered credentials for the server or the proxy
296 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35297 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54298 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38299 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42300 // Note: If an error is encountered while draining the old response body, no
301 // Network Error Logging report will be generated, because the error was
302 // with the old request, which will already have had a NEL report generated
303 // for it due to the auth challenge (so we don't report a second error for
304 // that request).
[email protected]a7ea8832010-07-12 17:54:54305 }
[email protected]c3b35c22008-09-27 03:19:42306
[email protected]c3b35c22008-09-27 03:19:42307 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04308 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42309 return rv;
[email protected]96d570e42008-08-05 22:43:04310}
311
[email protected]f9ee6b52008-11-08 06:46:23312void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
313 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38314 DCHECK(!stream_request_.get());
315
Bence Béky3238f2e12017-09-22 22:44:49316 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
317 if (target == HttpAuth::AUTH_SERVER &&
318 auth_controllers_[target]->NeedsHTTP11()) {
319 session_->http_server_properties()->SetHTTP11Required(
320 HostPortPair::FromURL(request_->url));
321 }
322
[email protected]2d2697f92009-02-18 21:00:32323 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57324 // Even if the server says the connection is keep-alive, we have to be
325 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34326 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57327 // If the response body hasn't been completely read, we need to drain
328 // it first.
[email protected]351ab642010-08-05 16:55:31329 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32330 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44331 read_buf_ = base::MakeRefCounted<IOBuffer>(
332 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32333 read_buf_len_ = kDrainBodyBufferSize;
334 return;
335 }
[email protected]0877e3d2009-10-17 22:29:57336 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09337 }
338
[email protected]2d2697f92009-02-18 21:00:32339 // We don't need to drain the response body, so we act as if we had drained
340 // the response body.
341 DidDrainBodyForAuthRestart(keep_alive);
342}
343
344void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38345 DCHECK(!stream_request_.get());
346
347 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19348 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22349 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35350 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34351 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38352 // We should call connection_->set_idle_time(), but this doesn't occur
353 // often enough to be worth the trouble.
354 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39355 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38356 }
[email protected]697ef4c2010-10-14 16:38:58357
358 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46359 // Close the stream and mark it as not_reusable. Even in the
360 // keep_alive case, we've determined that the stream_ is not
361 // reusable if new_stream is NULL.
362 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58363 next_state_ = STATE_CREATE_STREAM;
364 } else {
sclittlefb249892015-09-10 21:33:22365 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19366 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22367 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58368 next_state_ = STATE_INIT_STREAM;
369 }
370 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32371 }
[email protected]f9ee6b52008-11-08 06:46:23372
373 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58374 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23375}
376
[email protected]8e6441ca2010-08-19 05:56:38377bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
378 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
379 HaveAuth(pending_auth_target_);
380}
381
Bence Béky046f8c82018-06-12 02:26:04382int HttpNetworkTransaction::Read(IOBuffer* buf,
383 int buf_len,
384 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04385 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35386 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04387
[email protected]ad8e04a2010-11-01 04:16:27388 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38389 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04390 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29391 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04392 // bytes when establishing a tunnel because they might be controlled by
393 // an active network attacker. We don't worry about this for HTTP
394 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29395 // We reach this case when the user cancels a 407 proxy auth prompt. We
396 // also don't worry about this for an HTTPS Proxy, because the
397 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04398 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56399 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
400 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58401 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54402 LOG(WARNING) << "Blocked proxy response with status "
403 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15404 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04405 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44406 }
407
[email protected]e60e47a2010-07-14 03:37:18408 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15409 next_state_ = STATE_READ_BODY;
410
[email protected]96d570e42008-08-05 22:43:04411 read_buf_ = buf;
412 read_buf_len_ = buf_len;
413
[email protected]96d570e42008-08-05 22:43:04414 int rv = DoLoop(OK);
415 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04416 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04417 return rv;
418}
419
[email protected]8cd06c02014-01-25 07:50:14420void HttpNetworkTransaction::StopCaching() {}
421
[email protected]79e1fd62013-06-20 06:50:04422bool HttpNetworkTransaction::GetFullRequestHeaders(
423 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32424 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04425 *headers = request_headers_;
426 return true;
427}
428
sclittle4de1bab92015-09-22 21:28:24429int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
430 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19431 if (stream_)
432 total_received_bytes += stream_->GetTotalReceivedBytes();
433 return total_received_bytes;
434}
435
sclittlefb249892015-09-10 21:33:22436int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
437 int64_t total_sent_bytes = total_sent_bytes_;
438 if (stream_)
439 total_sent_bytes += stream_->GetTotalSentBytes();
440 return total_sent_bytes;
441}
442
[email protected]8cd06c02014-01-25 07:50:14443void HttpNetworkTransaction::DoneReading() {}
444
[email protected]96d570e42008-08-05 22:43:04445const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55446 return &response_;
[email protected]96d570e42008-08-05 22:43:04447}
448
449LoadState HttpNetworkTransaction::GetLoadState() const {
450 // TODO(wtc): Define a new LoadState value for the
451 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
452 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48453 case STATE_CREATE_STREAM:
454 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50455 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38456 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15457 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
458 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57459 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04460 return LOAD_STATE_SENDING_REQUEST;
461 case STATE_READ_HEADERS_COMPLETE:
462 return LOAD_STATE_WAITING_FOR_RESPONSE;
463 case STATE_READ_BODY_COMPLETE:
464 return LOAD_STATE_READING_RESPONSE;
465 default:
466 return LOAD_STATE_IDLE;
467 }
468}
469
[email protected]8cd06c02014-01-25 07:50:14470void HttpNetworkTransaction::SetQuicServerInfo(
471 QuicServerInfo* quic_server_info) {}
472
[email protected]5033ab82013-03-22 20:17:46473bool HttpNetworkTransaction::GetLoadTimingInfo(
474 LoadTimingInfo* load_timing_info) const {
475 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
476 return false;
477
478 load_timing_info->proxy_resolve_start =
479 proxy_info_.proxy_resolve_start_time();
480 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
481 load_timing_info->send_start = send_start_time_;
482 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46483 return true;
484}
485
ttuttled9dbc652015-09-29 20:00:59486bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36487 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59488 return false;
489
490 *endpoint = remote_endpoint_;
491 return true;
492}
493
zhongyi48704c182015-12-07 07:52:02494void HttpNetworkTransaction::PopulateNetErrorDetails(
495 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12496 *details = net_error_details_;
497 if (stream_)
498 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02499}
500
[email protected]5033ab82013-03-22 20:17:46501void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
502 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50503
[email protected]bf828982013-08-14 18:01:47504 if (stream_request_)
505 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03506 if (stream_)
507 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50508
rdsmith1d343be52016-10-21 20:37:50509 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46510}
511
[email protected]831e4a32013-11-14 02:14:44512void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
513 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
514 websocket_handshake_stream_base_create_helper_ = create_helper;
515}
516
[email protected]1826a402014-01-08 15:40:48517void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
518 const BeforeNetworkStartCallback& callback) {
519 before_network_start_callback_ = callback;
520}
521
ryansturm49a8cb12016-06-15 16:51:09522void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
523 const BeforeHeadersSentCallback& callback) {
524 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27525}
526
Andrey Kosyakov83a6eee2017-08-14 19:20:04527void HttpNetworkTransaction::SetRequestHeadersCallback(
528 RequestHeadersCallback callback) {
529 DCHECK(!stream_);
530 request_headers_callback_ = std::move(callback);
531}
532
Andrey Kosyakov2e893e62017-08-31 17:00:52533void HttpNetworkTransaction::SetResponseHeadersCallback(
534 ResponseHeadersCallback callback) {
535 DCHECK(!stream_);
536 response_headers_callback_ = std::move(callback);
537}
538
[email protected]1826a402014-01-08 15:40:48539int HttpNetworkTransaction::ResumeNetworkStart() {
540 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
541 return DoLoop(OK);
542}
543
[email protected]102e27c2011-02-23 01:01:31544void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
545 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00546 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50547 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38548 DCHECK(stream_request_.get());
549
sclittlefb249892015-09-10 21:33:22550 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19551 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22552 total_sent_bytes_ += stream_->GetTotalSentBytes();
553 }
bnc5029f4632017-06-08 16:19:00554 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04555 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14556 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31557 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52558 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21559 response_.alpn_negotiated_protocol =
560 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38561 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22562 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38563 OnIOComplete(OK);
564}
565
xunjieli5749218c2016-03-22 16:43:06566void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08567 const SSLConfig& used_ssl_config,
568 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00569 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08570 NOTREACHED();
571}
572
[email protected]a9cf2b92013-10-30 12:08:49573void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50574 const SSLConfig& used_ssl_config,
575 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00576 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
577 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50578}
579
Ryan Hamilton75f197262017-08-17 14:00:07580void HttpNetworkTransaction::OnStreamFailed(
581 int result,
582 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22583 const SSLConfig& used_ssl_config,
584 const ProxyInfo& used_proxy_info) {
[email protected]82918cc2010-08-25 17:24:50585 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38586 DCHECK_NE(OK, result);
587 DCHECK(stream_request_.get());
588 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14589 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07590 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22591 proxy_info_ = used_proxy_info;
592 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38593
594 OnIOComplete(result);
595}
596
[email protected]102e27c2011-02-23 01:01:31597void HttpNetworkTransaction::OnCertificateError(
598 int result,
599 const SSLConfig& used_ssl_config,
600 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50601 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38602 DCHECK_NE(OK, result);
603 DCHECK(stream_request_.get());
604 DCHECK(!stream_.get());
605
606 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14607 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38608
609 // TODO(mbelshe): For now, we're going to pass the error through, and that
610 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50611 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
612 // good and the user chooses to ignore the error. This is not ideal, but not
613 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38614
615 OnIOComplete(result);
616}
617
618void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50619 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31620 const SSLConfig& used_ssl_config,
621 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50622 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38623 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50624 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38625
626 establishing_tunnel_ = true;
627 response_.headers = proxy_response.headers;
628 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50629 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20630
631 if (response_.headers.get() && !ContentEncodingsValid()) {
632 DoCallback(ERR_CONTENT_DECODING_FAILED);
633 return;
634 }
635
[email protected]8e6441ca2010-08-19 05:56:38636 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14637 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31638 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38639
640 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
641 pending_auth_target_ = HttpAuth::AUTH_PROXY;
642
643 DoCallback(OK);
644}
645
646void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31647 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50648 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50649 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38650
[email protected]102957f2011-09-02 17:10:14651 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38652 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58653 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38654}
655
zhongyi48704c182015-12-07 07:52:02656void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12657 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02658}
659
ttuttle1f2d7e92015-04-28 16:17:47660void HttpNetworkTransaction::GetConnectionAttempts(
661 ConnectionAttempts* out) const {
662 *out = connection_attempts_;
663}
664
Adam Ricecb76ac62015-02-20 05:33:25665bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37666 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52667}
668
Adam Rice425cf122015-01-19 06:18:24669bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12670 return (proxy_info_.is_http() || proxy_info_.is_https() ||
671 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24672 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
673}
674
initial.commit586acc5fe2008-07-26 22:42:52675void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50676 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41677 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52678
Douglas Creageref5eecdc2018-11-09 20:50:36679#if BUILDFLAG(ENABLE_REPORTING)
680 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42681 // report about this network request if the result was an error.
682 GenerateNetworkErrorLoggingReportIfError(rv);
683#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36684
[email protected]96d570e42008-08-05 22:43:04685 // Since Run may result in Read being called, clear user_callback_ up front.
Daniel Chengcac5f4c62019-04-25 22:39:27686 std::move(callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52687}
688
689void HttpNetworkTransaction::OnIOComplete(int result) {
690 int rv = DoLoop(result);
691 if (rv != ERR_IO_PENDING)
692 DoCallback(rv);
693}
694
695int HttpNetworkTransaction::DoLoop(int result) {
696 DCHECK(next_state_ != STATE_NONE);
697
698 int rv = result;
699 do {
700 State state = next_state_;
701 next_state_ = STATE_NONE;
702 switch (state) {
[email protected]1826a402014-01-08 15:40:48703 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
704 DCHECK_EQ(OK, rv);
705 rv = DoNotifyBeforeCreateStream();
706 break;
[email protected]82918cc2010-08-25 17:24:50707 case STATE_CREATE_STREAM:
708 DCHECK_EQ(OK, rv);
709 rv = DoCreateStream();
710 break;
711 case STATE_CREATE_STREAM_COMPLETE:
[email protected]82918cc2010-08-25 17:24:50712 rv = DoCreateStreamComplete(rv);
[email protected]82918cc2010-08-25 17:24:50713 break;
[email protected]351ab642010-08-05 16:55:31714 case STATE_INIT_STREAM:
715 DCHECK_EQ(OK, rv);
716 rv = DoInitStream();
717 break;
718 case STATE_INIT_STREAM_COMPLETE:
719 rv = DoInitStreamComplete(rv);
720 break;
[email protected]044de0642010-06-17 10:42:15721 case STATE_GENERATE_PROXY_AUTH_TOKEN:
722 DCHECK_EQ(OK, rv);
723 rv = DoGenerateProxyAuthToken();
724 break;
725 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
726 rv = DoGenerateProxyAuthTokenComplete(rv);
727 break;
728 case STATE_GENERATE_SERVER_AUTH_TOKEN:
729 DCHECK_EQ(OK, rv);
730 rv = DoGenerateServerAuthToken();
731 break;
732 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
733 rv = DoGenerateServerAuthTokenComplete(rv);
734 break;
[email protected]daddea62012-09-19 05:51:13735 case STATE_INIT_REQUEST_BODY:
736 DCHECK_EQ(OK, rv);
737 rv = DoInitRequestBody();
738 break;
739 case STATE_INIT_REQUEST_BODY_COMPLETE:
740 rv = DoInitRequestBodyComplete(rv);
741 break;
[email protected]4875ba12011-03-30 22:31:51742 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55743 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00744 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51745 rv = DoBuildRequest();
746 break;
747 case STATE_BUILD_REQUEST_COMPLETE:
748 rv = DoBuildRequestComplete(rv);
749 break;
750 case STATE_SEND_REQUEST:
751 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57752 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52753 break;
[email protected]0877e3d2009-10-17 22:29:57754 case STATE_SEND_REQUEST_COMPLETE:
755 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43756 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00757 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52758 break;
759 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55760 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00761 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52762 rv = DoReadHeaders();
763 break;
764 case STATE_READ_HEADERS_COMPLETE:
765 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43766 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00767 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52768 break;
769 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55770 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00771 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52772 rv = DoReadBody();
773 break;
774 case STATE_READ_BODY_COMPLETE:
775 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43776 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00777 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52778 break;
[email protected]2d2697f92009-02-18 21:00:32779 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55780 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53781 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00782 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32783 rv = DoDrainBodyForAuthRestart();
784 break;
785 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
786 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32789 break;
initial.commit586acc5fe2008-07-26 22:42:52790 default:
791 NOTREACHED() << "bad state";
792 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 }
795 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
796
797 return rv;
798}
799
[email protected]1826a402014-01-08 15:40:48800int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
801 next_state_ = STATE_CREATE_STREAM;
802 bool defer = false;
803 if (!before_network_start_callback_.is_null())
804 before_network_start_callback_.Run(&defer);
805 if (!defer)
806 return OK;
807 return ERR_IO_PENDING;
808}
809
[email protected]82918cc2010-08-25 17:24:50810int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55811 response_.network_accessed = true;
812
[email protected]82918cc2010-08-25 17:24:50813 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17814 // IP based pooling is only enabled on a retry after 421 Misdirected Request
815 // is received. Alternative Services are also disabled in this case (though
816 // they can also be disabled when retrying after a QUIC error).
817 if (!enable_ip_based_pooling_)
818 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44819 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27820 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06821 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
822 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
823 websocket_handshake_stream_base_create_helper_,
824 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44825 } else {
xunjieli96f2a402017-06-05 17:24:27826 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29827 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27828 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44829 }
[email protected]26816882010-10-14 18:03:09830 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38831 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31832}
833
[email protected]82918cc2010-08-25 17:24:50834int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman96c5b292019-04-23 18:04:59835 CopyConnectionAttemptsFromStreamRequest();
[email protected]394816e92010-08-03 07:38:59836 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50837 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38838 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33839 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
840 result = HandleCertificateRequest(result);
bncfacdd852015-01-09 19:22:54841 } else if (result == ERR_HTTP_1_1_REQUIRED ||
842 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
843 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59844 }
845
David Benjamin76a40ad2018-02-24 22:22:08846 // Handle possible client certificate errors that may have occurred if the
847 // stream used SSL for one or more of the layers.
848 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30849
[email protected]8e6441ca2010-08-19 05:56:38850 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09851 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38852 return result;
[email protected]394816e92010-08-03 07:38:59853}
854
[email protected]82918cc2010-08-25 17:24:50855int HttpNetworkTransaction::DoInitStream() {
856 DCHECK(stream_.get());
857 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59858
859 stream_->GetRemoteEndpoint(&remote_endpoint_);
860
Steven Valdezb4ff0412018-01-18 22:39:27861 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
862 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50863}
864
865int HttpNetworkTransaction::DoInitStreamComplete(int result) {
866 if (result == OK) {
867 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50868 } else {
[email protected]82918cc2010-08-25 17:24:50869 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26870 result = HandleIOError(result);
871
872 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22873 if (stream_) {
874 total_received_bytes_ += stream_->GetTotalReceivedBytes();
875 total_sent_bytes_ += stream_->GetTotalSentBytes();
876 }
zhongyica364fbb2015-12-12 03:39:12877 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50878 }
879
880 return result;
881}
882
[email protected]044de0642010-06-17 10:42:15883int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
884 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
885 if (!ShouldApplyProxyAuth())
886 return OK;
[email protected]394816e92010-08-03 07:38:59887 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
888 if (!auth_controllers_[target].get())
Eric Orthbe2efac2019-03-06 01:11:11889 auth_controllers_[target] = new HttpAuthController(
890 target, AuthURL(target), session_->http_auth_cache(),
891 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59892 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41893 io_callback_,
[email protected]394816e92010-08-03 07:38:59894 net_log_);
[email protected]044de0642010-06-17 10:42:15895}
896
897int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
898 DCHECK_NE(ERR_IO_PENDING, rv);
899 if (rv == OK)
900 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
901 return rv;
902}
903
904int HttpNetworkTransaction::DoGenerateServerAuthToken() {
905 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59906 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54907 if (!auth_controllers_[target].get()) {
Eric Orthbe2efac2019-03-06 01:11:11908 auth_controllers_[target] = new HttpAuthController(
909 target, AuthURL(target), session_->http_auth_cache(),
910 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54911 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
912 auth_controllers_[target]->DisableEmbeddedIdentity();
913 }
[email protected]044de0642010-06-17 10:42:15914 if (!ShouldApplyServerAuth())
915 return OK;
[email protected]394816e92010-08-03 07:38:59916 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41917 io_callback_,
[email protected]394816e92010-08-03 07:38:59918 net_log_);
[email protected]044de0642010-06-17 10:42:15919}
920
921int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
922 DCHECK_NE(ERR_IO_PENDING, rv);
923 if (rv == OK)
nharperd6e65822016-03-30 23:05:48924 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14925 return rv;
926}
927
928int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24929 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14930 request_headers_.SetHeader(HttpRequestHeaders::kHost,
931 GetHostAndOptionalPort(request_->url));
932
933 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24934 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14935 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
936 "keep-alive");
937 } else {
938 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
939 }
940
[email protected]2979a492011-04-06 00:29:14941 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11942 if (request_->upload_data_stream) {
943 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14944 request_headers_.SetHeader(
945 HttpRequestHeaders::kTransferEncoding, "chunked");
946 } else {
947 request_headers_.SetHeader(
948 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:09949 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14950 }
csharrisonf473dd192015-08-18 13:54:13951 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14952 // An empty POST/PUT request still needs a content length. As for HEAD,
953 // IE and Safari also add a content length header. Presumably it is to
954 // support sending a HEAD request to an URL that only expects to be sent a
955 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13956 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
957 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14958 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
959 }
960
[email protected]2979a492011-04-06 00:29:14961 // Honor load flags that impact proxy caches.
962 if (request_->load_flags & LOAD_BYPASS_CACHE) {
963 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
964 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
965 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
966 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
967 }
968
969 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
970 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
971 &request_headers_);
972 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
973 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
974 &request_headers_);
975
[email protected]c10450102011-06-27 09:06:16976 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20977
ryansturm49a8cb12016-06-15 16:51:09978 if (!before_headers_sent_callback_.is_null())
979 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:20980
[email protected]173f8e22013-04-10 04:18:20981 response_.did_use_http_auth =
982 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
983 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:14984 return OK;
985}
986
[email protected]daddea62012-09-19 05:51:13987int HttpNetworkTransaction::DoInitRequestBody() {
988 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13989 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11990 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:33991 rv = request_->upload_data_stream->Init(
992 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
993 base::Unretained(this)),
994 net_log_);
[email protected]daddea62012-09-19 05:51:13995 return rv;
996}
[email protected]4875ba12011-03-30 22:31:51997
[email protected]daddea62012-09-19 05:51:13998int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
999 if (result == OK)
1000 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131001 return result;
1002}
1003
1004int HttpNetworkTransaction::DoBuildRequest() {
1005 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511006 headers_valid_ = false;
1007
1008 // This is constructed lazily (instead of within our Start method), so that
1009 // we have proxy info available.
1010 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241011 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141012 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511013 }
1014
[email protected]4875ba12011-03-30 22:31:511015 return OK;
1016}
1017
1018int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241019 if (result == OK)
1020 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511021 return result;
1022}
1023
[email protected]0877e3d2009-10-17 22:29:571024int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251025 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571026 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1027
[email protected]bf3eb002012-11-15 05:50:111028 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521029}
1030
[email protected]0877e3d2009-10-17 22:29:571031int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251032 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361033
1034 if (result == ERR_HTTP_1_1_REQUIRED ||
1035 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1036 return HandleHttp11Required(result);
1037 }
1038
initial.commit586acc5fe2008-07-26 22:42:521039 if (result < 0)
1040 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571041 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521042 return OK;
1043}
1044
1045int HttpNetworkTransaction::DoReadHeaders() {
1046 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411047 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521048}
1049
1050int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111051 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1052 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381053 if (IsCertificateError(result)) {
1054 // We don't handle a certificate error during SSL renegotiation, so we
1055 // have to return an error that's not in the certificate error range
1056 // (-2xx).
1057 LOG(ERROR) << "Got a server certificate with error " << result
1058 << " during SSL renegotiation";
1059 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1060 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
[email protected]8e6441ca2010-08-19 05:56:381061 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251062 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381063 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501064 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381065 result = HandleCertificateRequest(result);
1066 if (result == OK)
1067 return result;
[email protected]2181ea002009-06-09 01:37:271068 }
1069
bncfacdd852015-01-09 19:22:541070 if (result == ERR_HTTP_1_1_REQUIRED ||
1071 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1072 return HandleHttp11Required(result);
1073 }
1074
[email protected]c871864d72014-03-13 19:56:191075 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1076 // response headers were received, we do the best we can to make sense of it
1077 // and send it back up the stack.
1078 //
1079 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1080 // bizarre for SPDY. Assuming this logic is useful at all.
1081 // TODO(davidben): Bubble the error code up so we do not cache?
1082 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1083 result = OK;
1084
1085 if (result < 0)
1086 return HandleIOError(result);
1087
[email protected]90499482013-06-01 00:39:501088 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521089
eustasc7d27da2017-04-06 10:33:201090 if (response_.headers.get() && !ContentEncodingsValid())
1091 return ERR_CONTENT_DECODING_FAILED;
1092
[email protected]d58ceea82014-06-04 10:55:541093 // On a 408 response from the server ("Request Timeout") on a stale socket,
1094 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041095 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181096 if (response_.headers.get() &&
1097 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541098 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421099#if BUILDFLAG(ENABLE_REPORTING)
1100 GenerateNetworkErrorLoggingReport(OK);
1101#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541102 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001103 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541104 response_.headers->response_code());
1105 // This will close the socket - it would be weird to try and reuse it, even
1106 // if the server doesn't actually close it.
1107 ResetConnectionAndRequestForResend();
1108 return OK;
1109 }
1110
[email protected]3abacd62012-06-10 20:20:321111 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001112 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321113 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521114 if (response_headers_callback_)
1115 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391116
bncbe0f6af2015-10-15 17:49:561117 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571118 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1119 // indicates a buggy server. See:
1120 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1121 if (request_->method == "PUT")
1122 return ERR_METHOD_NOT_SUPPORTED;
1123 }
[email protected]4ddaf2502008-10-23 18:26:191124
Steven Valdez58097ec32018-07-16 18:29:041125 if (can_send_early_data_ && response_.headers.get() &&
1126 response_.headers->response_code() == HTTP_TOO_EARLY) {
1127 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1128 }
1129
[email protected]0877e3d2009-10-17 22:29:571130 // Check for an intermediate 100 Continue response. An origin server is
1131 // allowed to send this response even if we didn't ask for it, so we just
1132 // need to skip over it.
1133 // We treat any other 1xx in this same way (although in practice getting
1134 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441135 // Unless this is a WebSocket request, in which case we pass it on up.
1136 if (response_.headers->response_code() / 100 == 1 &&
1137 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451138 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571139 next_state_ = STATE_READ_HEADERS;
1140 return OK;
1141 }
1142
davidbence688ae2017-05-04 15:12:591143 if (response_.headers->response_code() == 421 &&
1144 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421145#if BUILDFLAG(ENABLE_REPORTING)
1146 GenerateNetworkErrorLoggingReport(OK);
1147#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591148 // Retry the request with both IP based pooling and Alternative Services
1149 // disabled.
1150 enable_ip_based_pooling_ = false;
1151 enable_alternative_services_ = false;
1152 net_log_.AddEvent(
1153 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1154 ResetConnectionAndRequestForResend();
1155 return OK;
bnc8016c1f2017-03-31 02:11:291156 }
1157
bncb26024382016-06-29 02:39:451158 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491159 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081160 if (response_.ssl_info.is_valid() &&
1161 !IsCertStatusError(response_.ssl_info.cert_status)) {
1162 session_->http_stream_factory()->ProcessAlternativeServices(
1163 session_, response_.headers.get(),
1164 url::SchemeHostPort(request_->url));
1165 }
1166 }
asanka5ffd5d72016-03-23 16:20:491167
[email protected]e772db3f2010-07-12 18:11:131168 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571169 if (rv != OK)
1170 return rv;
1171
Douglas Creager3cb042052018-11-06 23:08:521172#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421173 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1174 // reports generated before the NEL header is processed here will just be
1175 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141176 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521177 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421178
1179 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111180 // code), or if the response body will not be read, or on a redirect.
1181 // Note: This will report a success for a redirect even if an error is
1182 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421183 int response_code = response_.headers->response_code();
1184 if ((response_code >= 400 && response_code < 600) ||
1185 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1186 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111187 response_.headers->GetContentLength() == 0 ||
1188 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421189 GenerateNetworkErrorLoggingReport(OK);
1190 }
1191#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521192
[email protected]0877e3d2009-10-17 22:29:571193 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261194
1195 // We have reached the end of Start state machine, set the RequestInfo to
1196 // null.
1197 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1198 // only until the final response headers are received. Clearing it will ensure
1199 // that HttpRequestInfo is only used up until final response headers are
1200 // received. Clearing is allowed so that the transaction can be disassociated
1201 // from its creating consumer in cases where it is shared for writing to the
1202 // cache. It is also safe to set it to null at this point since
1203 // upload_data_stream is also not used in the Read state machine.
1204 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1205 request_ = nullptr;
1206
[email protected]0877e3d2009-10-17 22:29:571207 return OK;
initial.commit586acc5fe2008-07-26 22:42:521208}
1209
1210int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501211 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131212 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351213 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521214
1215 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501216 return stream_->ReadResponseBody(
1217 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521218}
1219
1220int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1221 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381222 bool done = false;
1223 if (result <= 0) {
1224 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521225 done = true;
[email protected]8e6441ca2010-08-19 05:56:381226 }
[email protected]9492e4a2010-02-24 00:58:461227
mmenkebd84c392015-09-02 14:12:341228 // Clean up connection if we are done.
1229 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381230 // Note: Just because IsResponseBodyComplete is true, we're not
1231 // necessarily "done". We're only "done" when it is the last
1232 // read on this HttpNetworkTransaction, which will be signified
1233 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341234 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381235 // the stream. No need to compute it here just to pass back
1236 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341237 bool keep_alive =
1238 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521239
[email protected]8e6441ca2010-08-19 05:56:381240 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011241 // Note: we don't reset the stream here. We've closed it, but we still
1242 // need it around so that callers can call methods such as
1243 // GetUploadProgress() and have them be meaningful.
1244 // TODO(mbelshe): This means we closed the stream here, and we close it
1245 // again in ~HttpNetworkTransaction. Clean that up.
1246
[email protected]8e6441ca2010-08-19 05:56:381247 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171248
1249 // This transaction was successful. If it had been retried because of an
1250 // error with an alternative service, mark that alternative service broken.
1251 if (!enable_alternative_services_ &&
1252 retried_alternative_service_.protocol != kProtoUnknown) {
Ryan Hamilton9f532a12019-04-22 22:26:411253 HistogramBrokenAlternateProtocolLocation(
1254 BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION);
rch2f2991c2017-04-13 19:28:171255 session_->http_server_properties()->MarkAlternativeServiceBroken(
1256 retried_alternative_service_);
1257 }
Lily Chenfec60d92019-01-24 01:16:421258
1259#if BUILDFLAG(ENABLE_REPORTING)
1260 GenerateNetworkErrorLoggingReport(result);
1261#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521262 }
1263
1264 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351265 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521266 read_buf_len_ = 0;
1267
1268 return result;
1269}
1270
[email protected]2d2697f92009-02-18 21:00:321271int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1272 // This method differs from DoReadBody only in the next_state_. So we just
1273 // call DoReadBody and override the next_state_. Perhaps there is a more
1274 // elegant way for these two methods to share code.
1275 int rv = DoReadBody();
1276 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1277 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1278 return rv;
1279}
1280
[email protected]0877e3d2009-10-17 22:29:571281// TODO(wtc): This method and the DoReadBodyComplete method are almost
1282// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321283int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231284 // keep_alive defaults to true because the very reason we're draining the
1285 // response body is to reuse the connection for auth restart.
1286 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321287 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571288 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421289 // Note: No Network Error Logging report is generated here because a report
1290 // will have already been generated for the original request due to the auth
1291 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321292 done = true;
[email protected]68873ba2009-06-04 21:49:231293 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311294 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571295 done = true;
[email protected]2d2697f92009-02-18 21:00:321296 }
1297
1298 if (done) {
1299 DidDrainBodyForAuthRestart(keep_alive);
1300 } else {
1301 // Keep draining.
1302 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1303 }
1304
1305 return OK;
1306}
1307
Douglas Creager3cb042052018-11-06 23:08:521308#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141309void HttpNetworkTransaction::ProcessReportToHeader() {
1310 std::string value;
1311 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1312 return;
1313
1314 ReportingService* service = session_->reporting_service();
1315 if (!service) {
1316 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1317 return;
1318 }
1319
1320 // Only accept Report-To headers on HTTPS connections that have no
1321 // certificate errors.
1322 if (!response_.ssl_info.is_valid()) {
1323 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1324 return;
1325 }
1326 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1327 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1328 return;
1329 }
1330
1331 service->ProcessHeader(url_.GetOrigin(), value);
1332}
1333
Douglas Creager3cb042052018-11-06 23:08:521334void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1335 std::string value;
1336 if (!response_.headers->GetNormalizedHeader(
1337 NetworkErrorLoggingService::kHeaderName, &value)) {
1338 return;
1339 }
1340
1341 NetworkErrorLoggingService* service =
1342 session_->network_error_logging_service();
1343 if (!service) {
1344 NetworkErrorLoggingService::
1345 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1346 return;
1347 }
1348
Lily Chen90ae93cc2019-02-14 01:15:391349 // Don't accept NEL headers received via a proxy, because the IP address of
1350 // the destination server is not known.
1351 if (response_.was_fetched_via_proxy)
1352 return;
1353
Douglas Creager3cb042052018-11-06 23:08:521354 // Only accept NEL headers on HTTPS connections that have no certificate
1355 // errors.
1356 if (!response_.ssl_info.is_valid()) {
1357 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1358 return;
1359 }
1360 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1361 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1362 return;
1363 }
1364
1365 if (remote_endpoint_.address().empty()) {
1366 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1367 return;
1368 }
1369
1370 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1371 value);
1372}
Douglas Creageref5eecdc2018-11-09 20:50:361373
Lily Chenfec60d92019-01-24 01:16:421374void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1375 if (rv < 0 && rv != ERR_IO_PENDING)
1376 GenerateNetworkErrorLoggingReport(rv);
1377}
1378
Douglas Creageref5eecdc2018-11-09 20:50:361379void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421380 // |rv| should be a valid net::Error
1381 DCHECK_NE(rv, ERR_IO_PENDING);
1382 DCHECK_LE(rv, 0);
1383
1384 if (network_error_logging_report_generated_)
1385 return;
1386 network_error_logging_report_generated_ = true;
1387
Douglas Creageref5eecdc2018-11-09 20:50:361388 NetworkErrorLoggingService* service =
1389 session_->network_error_logging_service();
1390 if (!service) {
1391 NetworkErrorLoggingService::
1392 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1393 return;
1394 }
1395
Lily Chen90ae93cc2019-02-14 01:15:391396 // Don't report on proxy auth challenges.
1397 if (response_.headers && response_.headers->response_code() ==
1398 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1399 return;
1400 }
1401
1402 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1403 // internal network details.
1404 if (response_.was_fetched_via_proxy)
1405 return;
1406
Lily Chen00196ab62018-12-04 19:52:291407 // Ignore errors from non-HTTPS origins.
1408 if (!url_.SchemeIsCryptographic()) {
1409 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1410 return;
1411 }
Douglas Creageref5eecdc2018-11-09 20:50:361412
1413 NetworkErrorLoggingService::RequestDetails details;
1414
1415 details.uri = url_;
1416 if (!request_referrer_.empty())
1417 details.referrer = GURL(request_referrer_);
1418 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421419 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361420 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421421 } else {
1422 details.server_ip = IPAddress();
1423 }
Douglas Creageref5eecdc2018-11-09 20:50:361424 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1425 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421426 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361427 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421428 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361429 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421430 }
Douglas Creageref5eecdc2018-11-09 20:50:361431 // If we got response headers, assume that the connection used HTTP/1.1
1432 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421433 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361434 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421435 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361436 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361437 }
Lily Chenfec60d92019-01-24 01:16:421438 details.method = request_method_;
1439 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361440 details.type = static_cast<Error>(rv);
1441 details.reporting_upload_depth = request_reporting_upload_depth_;
1442
1443 service->OnRequest(std::move(details));
1444}
Lily Chenfec60d92019-01-24 01:16:421445#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521446
[email protected]5e363962009-06-19 19:57:011447int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381448 // There are two paths through which the server can request a certificate
1449 // from us. The first is during the initial handshake, the second is
1450 // during SSL renegotiation.
1451 //
1452 // In both cases, we want to close the connection before proceeding.
1453 // We do this for two reasons:
1454 // First, we don't want to keep the connection to the server hung for a
1455 // long time while the user selects a certificate.
1456 // Second, even if we did keep the connection open, NSS has a bug where
1457 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581458 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381459
1460 if (stream_.get()) {
1461 // Since we already have a stream, we're being called as part of SSL
1462 // renegotiation.
1463 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191464 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221465 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381466 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121467 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381468 }
1469
[email protected]26816882010-10-14 18:03:091470 // The server is asking for a client certificate during the initial
1471 // handshake.
1472 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011473
[email protected]ec229bc92010-11-22 09:51:451474 // If the user selected one of the certificates in client_certs or declined
1475 // to provide one for this server before, use the past decision
1476 // automatically.
1477 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541478 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451479 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541480 response_.cert_request_info->host_and_port, &client_cert,
1481 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451482 if (!found_cached_cert)
1483 return error;
1484
1485 // Check that the certificate selected is still a certificate the server
1486 // is likely to accept, based on the criteria supplied in the
1487 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501488 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261489 const std::vector<std::string>& cert_authorities =
1490 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451491
[email protected]f1958c382013-02-07 00:15:261492 bool cert_still_valid = cert_authorities.empty() ||
1493 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451494 if (!cert_still_valid)
1495 return error;
[email protected]5e363962009-06-19 19:57:011496 }
[email protected]ec229bc92010-11-22 09:51:451497
David Benjamin76a40ad2018-02-24 22:22:081498 if (!response_.cert_request_info->is_proxy) {
1499 server_ssl_client_cert_was_cached_ = true;
1500 }
1501
[email protected]ec229bc92010-11-22 09:51:451502 // TODO(davidben): Add a unit test which covers this path; we need to be
1503 // able to send a legitimate certificate and also bypass/clear the
1504 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141505 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1506 &proxy_ssl_config_ : &server_ssl_config_;
1507 ssl_config->send_client_cert = true;
1508 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541509 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451510 next_state_ = STATE_CREATE_STREAM;
1511 // Reset the other member variables.
1512 // Note: this is necessary only with SSL renegotiation.
1513 ResetStateForRestart();
1514 return OK;
[email protected]0b45559b2009-06-12 21:45:111515}
1516
bncfacdd852015-01-09 19:22:541517int HttpNetworkTransaction::HandleHttp11Required(int error) {
1518 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1519 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1520
1521 if (error == ERR_HTTP_1_1_REQUIRED) {
1522 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1523 } else {
1524 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1525 }
1526 ResetConnectionAndRequestForResend();
1527 return OK;
1528}
1529
David Benjamin76a40ad2018-02-24 22:22:081530int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjamin3b94b0f2019-04-25 23:07:521531 // Most client auth errors here come from the origin server, but they may come
1532 // from the proxy if the request is not tunneled (i.e. the origin is HTTP, so
1533 // there is no HTTPS connection) and the proxy does not report a bad client
1534 // certificate until after the TLS handshake completes. The latter occurs in
1535 // TLS 1.3 or TLS 1.2 with False Start (disabled for proxies). The error will
1536 // then surface out of Read() rather than Connect() and ultimately surfaced
1537 // out of DoReadHeadersComplete().
1538 //
1539 // See https://crbug.com/828965.
1540 bool is_server;
1541 SSLConfig* ssl_config;
1542 HostPortPair host_port_pair;
1543 if (UsingHttpProxyWithoutTunnel()) {
1544 is_server = false;
1545 ssl_config = &proxy_ssl_config_;
1546 host_port_pair = proxy_info_.proxy_server().host_port_pair();
1547 } else {
1548 is_server = true;
1549 ssl_config = &server_ssl_config_;
1550 host_port_pair = HostPortPair::FromURL(request_->url);
1551 }
1552
David Benjamin98ba2be2019-04-09 14:25:251553 // Client certificate errors from the proxy are handled in the
David Benjamin3b94b0f2019-04-25 23:07:521554 // HttpStreamFactory and below.
1555 if (ssl_config->send_client_cert &&
[email protected]384cc73a2013-12-08 22:41:031556 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
David Benjamin3b94b0f2019-04-25 23:07:521557 DCHECK((is_server && IsSecureRequest()) || proxy_info_.is_https());
1558 session_->ssl_client_auth_cache()->Remove(host_port_pair);
[email protected]384cc73a2013-12-08 22:41:031559
David Benjamin76a40ad2018-02-24 22:22:081560 // The private key handle may have gone stale due to, e.g., the user
1561 // unplugging their smartcard. Operating systems do not provide reliable
1562 // notifications for this, so if the signature failed and the private key
1563 // came from SSLClientAuthCache, retry to ask the user for a new one.
David Benjamin3b94b0f2019-04-25 23:07:521564 if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
David Benjamin76a40ad2018-02-24 22:22:081565 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1566 server_ssl_client_cert_was_cached_ = false;
1567 server_ssl_config_.send_client_cert = false;
1568 server_ssl_config_.client_cert = nullptr;
1569 server_ssl_config_.client_private_key = nullptr;
1570 retry_attempts_++;
1571 net_log_.AddEventWithNetErrorCode(
1572 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1573 ResetConnectionAndRequestForResend();
1574 return OK;
1575 }
1576 }
initial.commit586acc5fe2008-07-26 22:42:521577 return error;
1578}
1579
[email protected]bd0b6772011-01-11 19:59:301580// This method determines whether it is safe to resend the request after an
1581// IO error. It can only be called in response to request header or body
1582// write errors or response header read errors. It should not be used in
1583// other cases, such as a Connect error.
1584int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031585 // Because the peer may request renegotiation with client authentication at
1586 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081587 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301588
Lily Chenfec60d92019-01-24 01:16:421589#if BUILDFLAG(ENABLE_REPORTING)
1590 GenerateNetworkErrorLoggingReportIfError(error);
1591#endif // BUILDFLAG(ENABLE_REPORTING)
1592
[email protected]bd0b6772011-01-11 19:59:301593 switch (error) {
1594 // If we try to reuse a connection that the server is in the process of
1595 // closing, we may end up successfully writing out our request (or a
1596 // portion of our request) only to find a connection error when we try to
1597 // read from (or finish writing to) the socket.
1598 case ERR_CONNECTION_RESET:
1599 case ERR_CONNECTION_CLOSED:
1600 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511601 // There can be a race between the socket pool checking checking whether a
1602 // socket is still connected, receiving the FIN, and sending/reading data
1603 // on a reused socket. If we receive the FIN between the connectedness
1604 // check and writing/reading from the socket, we may first learn the socket
1605 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1606 // likely happen when trying to retrieve its IP address.
1607 // See http://crbug.com/105824 for more details.
1608 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491609 // If a socket is closed on its initial request, HttpStreamParser returns
1610 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1611 // preconnected but failed to be used before the server timed it out.
1612 case ERR_EMPTY_RESPONSE:
1613 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381614 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001615 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301616 ResetConnectionAndRequestForResend();
1617 error = OK;
1618 }
1619 break;
Steven Valdez58097ec32018-07-16 18:29:041620 case ERR_EARLY_DATA_REJECTED:
1621 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1622 net_log_.AddEventWithNetErrorCode(
1623 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1624 // Disable early data on the SSLConfig on a reset.
1625 can_send_early_data_ = false;
1626 ResetConnectionAndRequestForResend();
1627 error = OK;
1628 break;
Bence Békyd0d69502019-06-25 19:47:181629 case ERR_HTTP2_PING_FAILED:
1630 case ERR_HTTP2_SERVER_REFUSED_STREAM:
1631 case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE:
1632 case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
1633 case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271634 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141635 if (HasExceededMaxRetries())
1636 break;
[email protected]b6754252012-06-13 23:14:381637 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001638 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141639 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001640 ResetConnectionAndRequestForResend();
1641 error = OK;
1642 break;
rch2f2991c2017-04-13 19:28:171643 case ERR_QUIC_PROTOCOL_ERROR:
1644 if (GetResponseHeaders() != nullptr ||
1645 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1646 // If the response headers have already been recieved and passed up
1647 // then the request can not be retried. Also, if there was no
1648 // alternative service used for this request, then there is no
1649 // alternative service to be disabled.
1650 break;
1651 }
Biljith Jayan45a41722017-08-16 18:43:141652 if (HasExceededMaxRetries())
1653 break;
rch2f2991c2017-04-13 19:28:171654 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1655 retried_alternative_service_)) {
1656 // If the alternative service was marked as broken while the request
1657 // was in flight, retry the request which will not use the broken
1658 // alternative service.
1659 net_log_.AddEventWithNetErrorCode(
1660 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141661 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171662 ResetConnectionAndRequestForResend();
1663 error = OK;
1664 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1665 // Disable alternative services for this request and retry it. If the
1666 // retry succeeds, then the alternative service will be marked as
1667 // broken then.
1668 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361669 net_log_.AddEventWithNetErrorCode(
1670 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141671 retry_attempts_++;
rch514a44a82017-04-13 04:46:361672 ResetConnectionAndRequestForResend();
1673 error = OK;
1674 }
1675 break;
[email protected]bd0b6772011-01-11 19:59:301676 }
1677 return error;
1678}
1679
[email protected]c3b35c22008-09-27 03:19:421680void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581681 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221682 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191683 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221684 total_sent_bytes_ += stream_->GetTotalSentBytes();
1685 }
zhongyica364fbb2015-12-12 03:39:121686 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581687}
1688
1689void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251690 send_start_time_ = base::TimeTicks();
1691 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251692
[email protected]0757e7702009-03-27 04:00:221693 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351694 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421695 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571696 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201697 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141698 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381699 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591700 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121701 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521702 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421703#if BUILDFLAG(ENABLE_REPORTING)
1704 network_error_logging_report_generated_ = false;
1705 start_timeticks_ = base::TimeTicks::Now();
1706#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121707}
1708
1709void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1710 if (stream_)
1711 stream_->PopulateNetErrorDetails(&net_error_details_);
1712 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571713}
1714
[email protected]0877e3d2009-10-17 22:29:571715HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501716 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421717}
1718
[email protected]a34f61ee2014-03-18 20:59:491719bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381720 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351721 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161722
[email protected]2a5c76b2008-09-25 22:15:161723 // NOTE: we resend a request only if we reused a keep-alive connection.
1724 // This automatically prevents an infinite resend loop because we'll run
1725 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381726 if (connection_is_proven && !has_received_headers)
1727 return true;
1728 return false;
[email protected]1c773ea12009-04-28 19:58:421729}
1730
Biljith Jayan45a41722017-08-16 18:43:141731bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1732 return (retry_attempts_ >= kMaxRetryAttempts);
1733}
1734
David Benjamin83ddfb32018-03-30 01:07:521735bool HttpNetworkTransaction::CheckMaxRestarts() {
1736 num_restarts_++;
1737 return num_restarts_ < kMaxRestarts;
1738}
1739
[email protected]1c773ea12009-04-28 19:58:421740void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381741 if (stream_.get()) {
1742 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121743 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161744 }
1745
[email protected]0877e3d2009-10-17 22:29:571746 // We need to clear request_headers_ because it contains the real request
1747 // headers, but we may need to resend the CONNECT request first to recreate
1748 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201749 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501750 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421751
1752#if BUILDFLAG(ENABLE_REPORTING)
1753 // Reset for new request.
1754 network_error_logging_report_generated_ = false;
1755 start_timeticks_ = base::TimeTicks::Now();
1756#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221757
1758 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541759}
1760
[email protected]1c773ea12009-04-28 19:58:421761bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241762 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421763}
license.botbf09a502008-08-24 00:55:551764
[email protected]1c773ea12009-04-28 19:58:421765bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041766 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421767}
1768
[email protected]e772db3f2010-07-12 18:11:131769int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271770 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501771 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421772
[email protected]0877e3d2009-10-17 22:29:571773 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581774 if (status != HTTP_UNAUTHORIZED &&
1775 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421776 return OK;
[email protected]9094b602012-02-27 21:44:581777 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111778 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161779 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1780 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421781
[email protected]9094b602012-02-27 21:44:581782 // This case can trigger when an HTTPS server responds with a "Proxy
1783 // authentication required" status code through a non-authenticating
1784 // proxy.
[email protected]7a67a8152010-11-05 18:31:101785 if (!auth_controllers_[target].get())
1786 return ERR_UNEXPECTED_PROXY_AUTH;
1787
[email protected]a7ea8832010-07-12 17:54:541788 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491789 headers, response_.ssl_info,
1790 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311791 net_log_);
[email protected]228404f2010-06-24 04:31:411792 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491793 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411794
Emily Starkf2c9bbd2019-04-09 17:08:581795 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411796
[email protected]228404f2010-06-24 04:31:411797 return rv;
[email protected]f9ee6b52008-11-08 06:46:231798}
1799
[email protected]8e6441ca2010-08-19 05:56:381800bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1801 return auth_controllers_[target].get() &&
1802 auth_controllers_[target]->HaveAuth();
1803}
1804
[email protected]228404f2010-06-24 04:31:411805GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1806 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461807 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411808 if (!proxy_info_.proxy_server().is_valid() ||
1809 proxy_info_.proxy_server().is_direct()) {
1810 return GURL(); // There is no proxy server.
1811 }
[email protected]2df19bb2010-08-25 20:13:461812 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1813 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351814 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461815 }
[email protected]228404f2010-06-24 04:31:411816 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291817 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361818 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291819 }
[email protected]228404f2010-06-24 04:31:411820 return request_->url;
1821 default:
1822 return GURL();
1823 }
[email protected]c3b35c22008-09-27 03:19:421824}
1825
[email protected]831e4a32013-11-14 02:14:441826bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141827 return websocket_handshake_stream_base_create_helper_ &&
1828 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441829}
1830
ttuttle1f2d7e92015-04-28 16:17:471831void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1832 DCHECK(stream_request_);
1833
1834 // Since the transaction can restart with auth credentials, it may create a
1835 // stream more than once. Accumulate all of the connection attempts across
1836 // those streams by appending them to the vector:
1837 for (const auto& attempt : stream_request_->connection_attempts())
1838 connection_attempts_.push_back(attempt);
1839}
1840
eustasc7d27da2017-04-06 10:33:201841bool HttpNetworkTransaction::ContentEncodingsValid() const {
1842 HttpResponseHeaders* headers = GetResponseHeaders();
1843 DCHECK(headers);
1844
1845 std::string accept_encoding;
1846 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1847 &accept_encoding);
1848 std::set<std::string> allowed_encodings;
1849 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1850 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1851 return false;
1852 }
1853
1854 std::string content_encoding;
1855 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1856 std::set<std::string> used_encodings;
1857 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1858 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1859 return false;
1860 }
1861
1862 // When "Accept-Encoding" is not specified, it is parsed as "*".
1863 // If "*" encoding is advertised, then any encoding should be "accepted".
1864 // This does not mean, that it will be successfully decoded.
1865 if (allowed_encodings.find("*") != allowed_encodings.end())
1866 return true;
1867
sky50576f32017-05-01 19:28:031868 bool result = true;
eustasc7d27da2017-04-06 10:33:201869 for (auto const& encoding : used_encodings) {
1870 SourceStream::SourceType source_type =
1871 FilterSourceStream::ParseEncodingType(encoding);
1872 // We don't reject encodings we are not aware. They just will not decode.
1873 if (source_type == SourceStream::TYPE_UNKNOWN)
1874 continue;
1875 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031876 FilterSourceStream::ReportContentDecodingFailed(
1877 SourceStream::TYPE_REJECTED);
1878 result = false;
1879 break;
eustasc7d27da2017-04-06 10:33:201880 }
1881 }
sky50576f32017-05-01 19:28:031882
1883 // Temporary workaround for http://crbug.com/714514
1884 if (headers->IsRedirect(nullptr)) {
1885 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1886 !result);
1887 return true;
1888 }
1889
1890 return result;
eustasc7d27da2017-04-06 10:33:201891}
1892
[email protected]c3b35c22008-09-27 03:19:421893} // namespace net