blob: 118a1ff5386f74d78b7f21520b457f93c685d903 [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]2fbaecf22010-07-22 22:20:359#include <vector>
10
nharperb7441ef2016-01-25 23:54:1411#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4112#include "base/bind.h"
13#include "base/bind_helpers.h"
Bence Békybc1de292018-02-01 15:48:0314#include "base/callback_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
David Benjamin5cb91132018-04-06 05:54:4918#include "base/metrics/histogram_functions.h"
asvitkinec3c93722015-06-17 14:48:3719#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4220#include "base/metrics/sparse_histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
[email protected]3d498f72013-10-28 21:17:4024#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5325#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3026#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3527#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5528#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1929#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2531#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2632#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4033#include "net/base/proxy_server.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.h"
Bence Béky230ac612017-08-30 19:17:0836#include "net/cert/cert_status_flags.h"
eustasc7d27da2017-04-06 10:33:2037#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0038#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4239#include "net/http/http_auth.h"
40#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2441#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3642#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_chunked_decoder.h"
44#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5445#include "net/http/http_proxy_client_socket.h"
[email protected]270c6412010-03-29 22:02:4746#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5247#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2148#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5749#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5350#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5851#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3952#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3153#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5254#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2355#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3656#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0057#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4458#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2159#include "net/socket/next_proto.h"
[email protected]ab739042011-04-07 15:22:2860#include "net/socket/transport_client_socket_pool.h"
Bence Béky94658bf2018-05-11 19:22:5861#include "net/spdy/spdy_http_stream.h"
62#include "net/spdy/spdy_session.h"
63#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_cert_request_info.h"
65#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0866#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5467#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5468#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
Douglas Creager3cb042052018-11-06 23:08:5271#if BUILDFLAG(ENABLE_REPORTING)
72#include "net/network_error_logging/network_error_logging_service.h"
Douglas Creager134b52e2018-11-09 18:00:1473#include "net/reporting/reporting_header_parser.h"
74#include "net/reporting/reporting_service.h"
Lily Chenfec60d92019-01-24 01:16:4275#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:5276
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2277namespace net {
78
Biljith Jayan45a41722017-08-16 18:43:1479namespace {
David Benjamin83ddfb32018-03-30 01:07:5280
Biljith Jayan45a41722017-08-16 18:43:1481// Max number of |retry_attempts| (excluding the initial request) after which
82// we give up and show an error page.
83const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5284
85// Max number of calls to RestartWith* allowed for a single connection. A single
86// HttpNetworkTransaction should not signal very many restartable errors, but it
87// may occur due to a bug (e.g. https://crbug.com/823387 or
88// https://crbug.com/488043) or simply if the server or proxy requests
89// authentication repeatedly. Although these calls are often associated with a
90// user prompt, in other scenarios (remembered preferences, extensions,
91// multi-leg authentication), they may be triggered automatically. To avoid
92// looping forever, bound the number of restarts.
93const size_t kMaxRestarts = 32;
94
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:2295void SetProxyInfoInReponse(const ProxyInfo& proxy_info,
96 HttpResponseInfo* response_info) {
97 response_info->was_fetched_via_proxy = !proxy_info.is_direct();
98 if (response_info->was_fetched_via_proxy && !proxy_info.is_empty())
99 response_info->proxy_server = proxy_info.proxy_server();
100 else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct())
101 response_info->proxy_server = ProxyServer::Direct();
102 else
103 response_info->proxy_server = ProxyServer();
104}
Biljith Jayan45a41722017-08-16 18:43:14105
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22106} // namespace
initial.commit586acc5fe2008-07-26 22:42:52107
Victor Costan9c7302b2018-08-27 16:39:44108const int HttpNetworkTransaction::kDrainBodyBufferSize;
109
[email protected]262eec82013-03-19 21:01:36110HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
111 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22112 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53113 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
114 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52115 session_(session),
Raul Tambre94493c652019-03-11 17:18:35116 request_(nullptr),
[email protected]262eec82013-03-19 21:01:36117 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57118 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27119 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08120 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20121 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42122#if BUILDFLAG(ENABLE_REPORTING)
123 network_error_logging_report_generated_(false),
124 request_reporting_upload_depth_(0),
125#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52126 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19127 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22128 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54129 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44130 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29131 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26132 enable_alternative_services_(true),
Raul Tambre94493c652019-03-11 17:18:35133 websocket_handshake_stream_base_create_helper_(nullptr),
Biljith Jayan45a41722017-08-16 18:43:14134 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52135 retry_attempts_(0),
David Benjamin5cb91132018-04-06 05:54:49136 num_restarts_(0),
Lily Chenfec60d92019-01-24 01:16:42137 ssl_version_interference_error_(OK) {
138}
[email protected]3ce7df0f2010-03-03 00:30:50139
[email protected]0b0bf032010-09-21 18:08:50140HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42141#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11142 // If no error or success report has been generated yet at this point, then
143 // this network transaction was prematurely cancelled.
144 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42145#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50146 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50147 // TODO(mbelshe): The stream_ should be able to compute whether or not the
148 // stream should be kept alive. No reason to compute here
149 // and pass it in.
mmenkebd84c392015-09-02 14:12:34150 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50151 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34152 } else if (stream_->IsResponseBodyComplete()) {
153 // If the response body is complete, we can just reuse the socket.
154 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50155 } else {
mmenkebd84c392015-09-02 14:12:34156 // Otherwise, we try to drain the response body.
157 HttpStream* stream = stream_.release();
158 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50159 }
160 }
[email protected]02cad5d2013-10-02 08:14:03161 if (request_ && request_->upload_data_stream)
162 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50163}
164
[email protected]684970b2009-08-14 04:54:46165int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04166 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20167 const NetLogWithSource& net_log) {
Ramin Halavatib5e433e2018-02-07 07:41:10168 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53169 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04170 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15171 url_ = request_->url;
Douglas Creageref5eecdc2018-11-09 20:50:36172#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42173 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36174 request_method_ = request_->method;
175 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
176 &request_referrer_);
177 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
178 &request_user_agent_);
179 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42180 start_timeticks_ = base::TimeTicks::Now();
181#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04182
nharper8cdb0fb2016-04-22 21:34:59183 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
184 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
185
Ryan Sleevi24fe2682018-08-16 21:33:46186 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
187 server_ssl_config_.disable_cert_verification_network_fetches = true;
188 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19189 }
[email protected]6fbac162011-06-20 00:29:04190
Steven Valdez03e872d2018-03-02 15:39:15191 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27192 can_send_early_data_ = true;
193 }
194
jkarlinfb1d5172015-01-12 14:10:29195 if (request_->load_flags & LOAD_PREFETCH)
196 response_.unused_since_prefetch = true;
197
Helen Lib495c3802018-03-30 13:46:09198 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04199 int rv = DoLoop(OK);
200 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04201 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42202
203 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
204 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
205 // other net::Error can be returned.
206 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04207 return rv;
208}
209
210int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04211 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38212 DCHECK(!stream_.get());
213 DCHECK(!stream_request_.get());
214 DCHECK_EQ(STATE_NONE, next_state_);
215
David Benjamin83ddfb32018-03-30 01:07:52216 if (!CheckMaxRestarts())
217 return ERR_TOO_MANY_RETRIES;
218
[email protected]82918cc2010-08-25 17:24:50219 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38220
[email protected]ccb40e52008-09-17 20:54:40221 int rv = DoLoop(OK);
222 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04223 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42224
225 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
226 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
227 // other net::Error can be returned.
228 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11229 return rv;
[email protected]96d570e42008-08-05 22:43:04230}
231
[email protected]0b45559b2009-06-12 21:45:11232int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08233 scoped_refptr<X509Certificate> client_cert,
234 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04235 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38236 // In HandleCertificateRequest(), we always tear down existing stream
237 // requests to force a new connection. So we shouldn't have one here.
238 DCHECK(!stream_request_.get());
239 DCHECK(!stream_.get());
240 DCHECK_EQ(STATE_NONE, next_state_);
241
David Benjamin83ddfb32018-03-30 01:07:52242 if (!CheckMaxRestarts())
243 return ERR_TOO_MANY_RETRIES;
244
[email protected]102957f2011-09-02 17:10:14245 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
246 &proxy_ssl_config_ : &server_ssl_config_;
247 ssl_config->send_client_cert = true;
248 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54249 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45250 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08251 response_.cert_request_info->host_and_port, std::move(client_cert),
252 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11253 // Reset the other member variables.
254 // Note: this is necessary only with SSL renegotiation.
255 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50256 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11257 int rv = DoLoop(OK);
258 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04259 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42260
261 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
262 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
263 // other net::Error can be returned.
264 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11265 return rv;
266}
267
Bence Béky046f8c82018-06-12 02:26:04268int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
269 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52270 if (!CheckMaxRestarts())
271 return ERR_TOO_MANY_RETRIES;
272
[email protected]0757e7702009-03-27 04:00:22273 HttpAuth::Target target = pending_auth_target_;
274 if (target == HttpAuth::AUTH_NONE) {
275 NOTREACHED();
276 return ERR_UNEXPECTED;
277 }
[email protected]0757e7702009-03-27 04:00:22278 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42279
[email protected]f3cf9802011-10-28 18:44:58280 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13281
[email protected]49639fa2011-12-20 23:22:41282 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38283
284 int rv = OK;
285 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
286 // In this case, we've gathered credentials for use with proxy
287 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50288 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
Raul Tambre94493c652019-03-11 17:18:35289 DCHECK(stream_request_ != nullptr);
290 auth_controllers_[target] = nullptr;
[email protected]a7ea8832010-07-12 17:54:54291 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39292 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54293 } else {
[email protected]8e6441ca2010-08-19 05:56:38294 // In this case, we've gathered credentials for the server or the proxy
295 // but it is not during the tunneling phase.
Raul Tambre94493c652019-03-11 17:18:35296 DCHECK(stream_request_ == nullptr);
[email protected]a7ea8832010-07-12 17:54:54297 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38298 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42299 // Note: If an error is encountered while draining the old response body, no
300 // Network Error Logging report will be generated, because the error was
301 // with the old request, which will already have had a NEL report generated
302 // for it due to the auth challenge (so we don't report a second error for
303 // that request).
[email protected]a7ea8832010-07-12 17:54:54304 }
[email protected]c3b35c22008-09-27 03:19:42305
[email protected]c3b35c22008-09-27 03:19:42306 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04307 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42308 return rv;
[email protected]96d570e42008-08-05 22:43:04309}
310
[email protected]f9ee6b52008-11-08 06:46:23311void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
312 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38313 DCHECK(!stream_request_.get());
314
Bence Béky3238f2e12017-09-22 22:44:49315 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
316 if (target == HttpAuth::AUTH_SERVER &&
317 auth_controllers_[target]->NeedsHTTP11()) {
318 session_->http_server_properties()->SetHTTP11Required(
319 HostPortPair::FromURL(request_->url));
320 }
321
[email protected]2d2697f92009-02-18 21:00:32322 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57323 // Even if the server says the connection is keep-alive, we have to be
324 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34325 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57326 // If the response body hasn't been completely read, we need to drain
327 // it first.
[email protected]351ab642010-08-05 16:55:31328 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32329 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44330 read_buf_ = base::MakeRefCounted<IOBuffer>(
331 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32332 read_buf_len_ = kDrainBodyBufferSize;
333 return;
334 }
[email protected]0877e3d2009-10-17 22:29:57335 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09336 }
337
[email protected]2d2697f92009-02-18 21:00:32338 // We don't need to drain the response body, so we act as if we had drained
339 // the response body.
340 DidDrainBodyForAuthRestart(keep_alive);
341}
342
343void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38344 DCHECK(!stream_request_.get());
345
346 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19347 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22348 total_sent_bytes_ += stream_->GetTotalSentBytes();
Raul Tambre94493c652019-03-11 17:18:35349 HttpStream* new_stream = nullptr;
mmenkebd84c392015-09-02 14:12:34350 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38351 // We should call connection_->set_idle_time(), but this doesn't occur
352 // often enough to be worth the trouble.
353 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39354 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38355 }
[email protected]697ef4c2010-10-14 16:38:58356
357 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46358 // Close the stream and mark it as not_reusable. Even in the
359 // keep_alive case, we've determined that the stream_ is not
360 // reusable if new_stream is NULL.
361 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58362 next_state_ = STATE_CREATE_STREAM;
363 } else {
sclittlefb249892015-09-10 21:33:22364 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19365 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22366 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58367 next_state_ = STATE_INIT_STREAM;
368 }
369 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32370 }
[email protected]f9ee6b52008-11-08 06:46:23371
372 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58373 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23374}
375
[email protected]8e6441ca2010-08-19 05:56:38376bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
377 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
378 HaveAuth(pending_auth_target_);
379}
380
Bence Béky046f8c82018-06-12 02:26:04381int HttpNetworkTransaction::Read(IOBuffer* buf,
382 int buf_len,
383 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04384 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35385 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04386
[email protected]ad8e04a2010-11-01 04:16:27387 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38388 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04389 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29390 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04391 // bytes when establishing a tunnel because they might be controlled by
392 // an active network attacker. We don't worry about this for HTTP
393 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29394 // We reach this case when the user cancels a 407 proxy auth prompt. We
395 // also don't worry about this for an HTTPS Proxy, because the
396 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04397 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56398 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
399 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58400 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54401 LOG(WARNING) << "Blocked proxy response with status "
402 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15403 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04404 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44405 }
406
[email protected]e60e47a2010-07-14 03:37:18407 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15408 next_state_ = STATE_READ_BODY;
409
[email protected]96d570e42008-08-05 22:43:04410 read_buf_ = buf;
411 read_buf_len_ = buf_len;
412
[email protected]96d570e42008-08-05 22:43:04413 int rv = DoLoop(OK);
414 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04415 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04416 return rv;
417}
418
[email protected]8cd06c02014-01-25 07:50:14419void HttpNetworkTransaction::StopCaching() {}
420
[email protected]79e1fd62013-06-20 06:50:04421bool HttpNetworkTransaction::GetFullRequestHeaders(
422 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32423 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04424 *headers = request_headers_;
425 return true;
426}
427
sclittle4de1bab92015-09-22 21:28:24428int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
429 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19430 if (stream_)
431 total_received_bytes += stream_->GetTotalReceivedBytes();
432 return total_received_bytes;
433}
434
sclittlefb249892015-09-10 21:33:22435int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
436 int64_t total_sent_bytes = total_sent_bytes_;
437 if (stream_)
438 total_sent_bytes += stream_->GetTotalSentBytes();
439 return total_sent_bytes;
440}
441
[email protected]8cd06c02014-01-25 07:50:14442void HttpNetworkTransaction::DoneReading() {}
443
[email protected]96d570e42008-08-05 22:43:04444const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55445 return &response_;
[email protected]96d570e42008-08-05 22:43:04446}
447
448LoadState HttpNetworkTransaction::GetLoadState() const {
449 // TODO(wtc): Define a new LoadState value for the
450 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
451 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48452 case STATE_CREATE_STREAM:
453 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50454 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38455 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15456 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
457 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57458 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04459 return LOAD_STATE_SENDING_REQUEST;
460 case STATE_READ_HEADERS_COMPLETE:
461 return LOAD_STATE_WAITING_FOR_RESPONSE;
462 case STATE_READ_BODY_COMPLETE:
463 return LOAD_STATE_READING_RESPONSE;
464 default:
465 return LOAD_STATE_IDLE;
466 }
467}
468
[email protected]8cd06c02014-01-25 07:50:14469void HttpNetworkTransaction::SetQuicServerInfo(
470 QuicServerInfo* quic_server_info) {}
471
[email protected]5033ab82013-03-22 20:17:46472bool HttpNetworkTransaction::GetLoadTimingInfo(
473 LoadTimingInfo* load_timing_info) const {
474 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
475 return false;
476
477 load_timing_info->proxy_resolve_start =
478 proxy_info_.proxy_resolve_start_time();
479 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
480 load_timing_info->send_start = send_start_time_;
481 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46482 return true;
483}
484
ttuttled9dbc652015-09-29 20:00:59485bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36486 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59487 return false;
488
489 *endpoint = remote_endpoint_;
490 return true;
491}
492
zhongyi48704c182015-12-07 07:52:02493void HttpNetworkTransaction::PopulateNetErrorDetails(
494 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12495 *details = net_error_details_;
496 if (stream_)
497 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02498}
499
[email protected]5033ab82013-03-22 20:17:46500void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
501 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50502
[email protected]bf828982013-08-14 18:01:47503 if (stream_request_)
504 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03505 if (stream_)
506 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50507
rdsmith1d343be52016-10-21 20:37:50508 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46509}
510
[email protected]831e4a32013-11-14 02:14:44511void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
512 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
513 websocket_handshake_stream_base_create_helper_ = create_helper;
514}
515
[email protected]1826a402014-01-08 15:40:48516void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
517 const BeforeNetworkStartCallback& callback) {
518 before_network_start_callback_ = callback;
519}
520
ryansturm49a8cb12016-06-15 16:51:09521void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
522 const BeforeHeadersSentCallback& callback) {
523 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27524}
525
Andrey Kosyakov83a6eee2017-08-14 19:20:04526void HttpNetworkTransaction::SetRequestHeadersCallback(
527 RequestHeadersCallback callback) {
528 DCHECK(!stream_);
529 request_headers_callback_ = std::move(callback);
530}
531
Andrey Kosyakov2e893e62017-08-31 17:00:52532void HttpNetworkTransaction::SetResponseHeadersCallback(
533 ResponseHeadersCallback callback) {
534 DCHECK(!stream_);
535 response_headers_callback_ = std::move(callback);
536}
537
[email protected]1826a402014-01-08 15:40:48538int HttpNetworkTransaction::ResumeNetworkStart() {
539 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
540 return DoLoop(OK);
541}
542
[email protected]102e27c2011-02-23 01:01:31543void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
544 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00545 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50546 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38547 DCHECK(stream_request_.get());
548
sclittlefb249892015-09-10 21:33:22549 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19550 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22551 total_sent_bytes_ += stream_->GetTotalSentBytes();
552 }
bnc5029f4632017-06-08 16:19:00553 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04554 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14555 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31556 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52557 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21558 response_.alpn_negotiated_protocol =
559 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38560 response_.was_fetched_via_spdy = stream_request_->using_spdy();
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22561 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38562 OnIOComplete(OK);
563}
564
xunjieli5749218c2016-03-22 16:43:06565void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08566 const SSLConfig& used_ssl_config,
567 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00568 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08569 NOTREACHED();
570}
571
[email protected]a9cf2b92013-10-30 12:08:49572void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50573 const SSLConfig& used_ssl_config,
574 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00575 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
576 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50577}
578
Ryan Hamilton75f197262017-08-17 14:00:07579void HttpNetworkTransaction::OnStreamFailed(
580 int result,
581 const NetErrorDetails& net_error_details,
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22582 const SSLConfig& used_ssl_config,
583 const ProxyInfo& used_proxy_info) {
[email protected]82918cc2010-08-25 17:24:50584 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38585 DCHECK_NE(OK, result);
586 DCHECK(stream_request_.get());
587 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14588 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07589 net_error_details_ = net_error_details;
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:22590 proxy_info_ = used_proxy_info;
591 SetProxyInfoInReponse(used_proxy_info, &response_);
[email protected]8e6441ca2010-08-19 05:56:38592
593 OnIOComplete(result);
594}
595
[email protected]102e27c2011-02-23 01:01:31596void HttpNetworkTransaction::OnCertificateError(
597 int result,
598 const SSLConfig& used_ssl_config,
599 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50600 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38601 DCHECK_NE(OK, result);
602 DCHECK(stream_request_.get());
603 DCHECK(!stream_.get());
604
605 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14606 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38607
608 // TODO(mbelshe): For now, we're going to pass the error through, and that
609 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50610 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
611 // good and the user chooses to ignore the error. This is not ideal, but not
612 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38613
614 OnIOComplete(result);
615}
616
617void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50618 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31619 const SSLConfig& used_ssl_config,
620 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50621 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38622 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50623 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38624
625 establishing_tunnel_ = true;
626 response_.headers = proxy_response.headers;
627 response_.auth_challenge = proxy_response.auth_challenge;
Wojciech Dzierżanowski0950c372019-04-02 19:29:50628 response_.did_use_http_auth = proxy_response.did_use_http_auth;
eustasc7d27da2017-04-06 10:33:20629
630 if (response_.headers.get() && !ContentEncodingsValid()) {
631 DoCallback(ERR_CONTENT_DECODING_FAILED);
632 return;
633 }
634
[email protected]8e6441ca2010-08-19 05:56:38635 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14636 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31637 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38638
639 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
640 pending_auth_target_ = HttpAuth::AUTH_PROXY;
641
642 DoCallback(OK);
643}
644
645void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31646 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50647 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50648 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38649
[email protected]102957f2011-09-02 17:10:14650 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38651 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58652 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38653}
654
Eric Roman74103c72019-02-21 00:23:12655void HttpNetworkTransaction::OnHttpsProxyTunnelResponseRedirect(
[email protected]511f6f52010-12-17 03:58:29656 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31657 const SSLConfig& used_ssl_config,
658 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01659 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29660 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
661
ttuttle1f2d7e92015-04-28 16:17:47662 CopyConnectionAttemptsFromStreamRequest();
663
[email protected]511f6f52010-12-17 03:58:29664 headers_valid_ = true;
665 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14666 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31667 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22668 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19669 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22670 total_sent_bytes_ += stream_->GetTotalSentBytes();
671 }
Bence Béky6d05ebd2017-05-16 00:09:01672 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04673 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29674 stream_request_.reset(); // we're done with the stream request
Eric Roman74103c72019-02-21 00:23:12675 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT);
[email protected]511f6f52010-12-17 03:58:29676}
677
zhongyi48704c182015-12-07 07:52:02678void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12679 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02680}
681
ttuttle1f2d7e92015-04-28 16:17:47682void HttpNetworkTransaction::GetConnectionAttempts(
683 ConnectionAttempts* out) const {
684 *out = connection_attempts_;
685}
686
Adam Ricecb76ac62015-02-20 05:33:25687bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37688 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52689}
690
Adam Rice425cf122015-01-19 06:18:24691bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12692 return (proxy_info_.is_http() || proxy_info_.is_https() ||
693 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24694 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
695}
696
initial.commit586acc5fe2008-07-26 22:42:52697void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50698 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41699 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52700
Douglas Creageref5eecdc2018-11-09 20:50:36701#if BUILDFLAG(ENABLE_REPORTING)
702 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42703 // report about this network request if the result was an error.
704 GenerateNetworkErrorLoggingReportIfError(rv);
705#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36706
[email protected]96d570e42008-08-05 22:43:04707 // Since Run may result in Read being called, clear user_callback_ up front.
Bence Békybc1de292018-02-01 15:48:03708 base::ResetAndReturn(&callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52709}
710
711void HttpNetworkTransaction::OnIOComplete(int result) {
712 int rv = DoLoop(result);
713 if (rv != ERR_IO_PENDING)
714 DoCallback(rv);
715}
716
717int HttpNetworkTransaction::DoLoop(int result) {
718 DCHECK(next_state_ != STATE_NONE);
719
720 int rv = result;
721 do {
722 State state = next_state_;
723 next_state_ = STATE_NONE;
724 switch (state) {
[email protected]1826a402014-01-08 15:40:48725 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
726 DCHECK_EQ(OK, rv);
727 rv = DoNotifyBeforeCreateStream();
728 break;
[email protected]82918cc2010-08-25 17:24:50729 case STATE_CREATE_STREAM:
730 DCHECK_EQ(OK, rv);
731 rv = DoCreateStream();
732 break;
733 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46734 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
735 // solved.
736 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50737 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46738 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50739 break;
[email protected]351ab642010-08-05 16:55:31740 case STATE_INIT_STREAM:
741 DCHECK_EQ(OK, rv);
742 rv = DoInitStream();
743 break;
744 case STATE_INIT_STREAM_COMPLETE:
745 rv = DoInitStreamComplete(rv);
746 break;
[email protected]044de0642010-06-17 10:42:15747 case STATE_GENERATE_PROXY_AUTH_TOKEN:
748 DCHECK_EQ(OK, rv);
749 rv = DoGenerateProxyAuthToken();
750 break;
751 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
752 rv = DoGenerateProxyAuthTokenComplete(rv);
753 break;
754 case STATE_GENERATE_SERVER_AUTH_TOKEN:
755 DCHECK_EQ(OK, rv);
756 rv = DoGenerateServerAuthToken();
757 break;
758 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
759 rv = DoGenerateServerAuthTokenComplete(rv);
760 break;
[email protected]daddea62012-09-19 05:51:13761 case STATE_INIT_REQUEST_BODY:
762 DCHECK_EQ(OK, rv);
763 rv = DoInitRequestBody();
764 break;
765 case STATE_INIT_REQUEST_BODY_COMPLETE:
766 rv = DoInitRequestBodyComplete(rv);
767 break;
[email protected]4875ba12011-03-30 22:31:51768 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55769 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00770 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51771 rv = DoBuildRequest();
772 break;
773 case STATE_BUILD_REQUEST_COMPLETE:
774 rv = DoBuildRequestComplete(rv);
775 break;
776 case STATE_SEND_REQUEST:
777 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57778 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52779 break;
[email protected]0877e3d2009-10-17 22:29:57780 case STATE_SEND_REQUEST_COMPLETE:
781 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43782 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00783 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52784 break;
785 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55786 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00787 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52788 rv = DoReadHeaders();
789 break;
790 case STATE_READ_HEADERS_COMPLETE:
791 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43792 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00793 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52794 break;
795 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55796 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00797 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52798 rv = DoReadBody();
799 break;
800 case STATE_READ_BODY_COMPLETE:
801 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43802 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00803 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52804 break;
[email protected]2d2697f92009-02-18 21:00:32805 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55806 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53807 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00808 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32809 rv = DoDrainBodyForAuthRestart();
810 break;
811 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
812 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43813 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00814 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32815 break;
initial.commit586acc5fe2008-07-26 22:42:52816 default:
817 NOTREACHED() << "bad state";
818 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04819 break;
initial.commit586acc5fe2008-07-26 22:42:52820 }
821 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
822
823 return rv;
824}
825
[email protected]1826a402014-01-08 15:40:48826int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
827 next_state_ = STATE_CREATE_STREAM;
828 bool defer = false;
829 if (!before_network_start_callback_.is_null())
830 before_network_start_callback_.Run(&defer);
831 if (!defer)
832 return OK;
833 return ERR_IO_PENDING;
834}
835
[email protected]82918cc2010-08-25 17:24:50836int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55837 response_.network_accessed = true;
838
[email protected]82918cc2010-08-25 17:24:50839 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17840 // IP based pooling is only enabled on a retry after 421 Misdirected Request
841 // is received. Alternative Services are also disabled in this case (though
842 // they can also be disabled when retrying after a QUIC error).
843 if (!enable_ip_based_pooling_)
844 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44845 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27846 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06847 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
848 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
849 websocket_handshake_stream_base_create_helper_,
850 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44851 } else {
xunjieli96f2a402017-06-05 17:24:27852 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29853 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27854 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44855 }
[email protected]26816882010-10-14 18:03:09856 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38857 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31858}
859
[email protected]82918cc2010-08-25 17:24:50860int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman74103c72019-02-21 00:23:12861 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT, then
862 // DoCreateStreamComplete is being called from
863 // OnHttpsProxyTunnelResponseRedirect, which resets the stream request first.
864 // Therefore, we have to grab the connection attempts in *that* function
865 // instead of here in that case.
866 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT)
ttuttle1f2d7e92015-04-28 16:17:47867 CopyConnectionAttemptsFromStreamRequest();
868
[email protected]394816e92010-08-03 07:38:59869 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50870 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38871 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33872 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
873 result = HandleCertificateRequest(result);
Eric Roman74103c72019-02-21 00:23:12874 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT) {
875 return DoCreateStreamCompletedTunnelResponseRedirect();
bncfacdd852015-01-09 19:22:54876 } else if (result == ERR_HTTP_1_1_REQUIRED ||
877 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
878 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59879 }
880
David Benjamin5cb91132018-04-06 05:54:49881 // Perform a TLS 1.3 version interference probe on various connection
882 // errors. The retry will never produce a successful connection but may map
883 // errors to ERR_SSL_VERSION_INTERFERENCE, which signals a probable
884 // version-interfering middlebox.
885 if (IsSecureRequest() && !HasExceededMaxRetries() &&
886 server_ssl_config_.version_max == SSL_PROTOCOL_VERSION_TLS1_3 &&
887 !server_ssl_config_.version_interference_probe) {
888 if (result == ERR_CONNECTION_CLOSED || result == ERR_SSL_PROTOCOL_ERROR ||
889 result == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
890 result == ERR_CONNECTION_RESET ||
891 result == ERR_SSL_BAD_RECORD_MAC_ALERT) {
892 // Report the error code for each time a version interference probe is
893 // triggered.
894 base::UmaHistogramSparse("Net.SSLVersionInterferenceProbeTrigger",
895 std::abs(result));
896 net_log_.AddEventWithNetErrorCode(
897 NetLogEventType::SSL_VERSION_INTERFERENCE_PROBE, result);
898
899 retry_attempts_++;
900 server_ssl_config_.version_interference_probe = true;
901 server_ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_2;
902 ssl_version_interference_error_ = result;
903 ResetConnectionAndRequestForResend();
904 return OK;
905 }
906 }
907
908 if (result == ERR_SSL_VERSION_INTERFERENCE) {
909 // Record the error code version interference was detected at.
910 DCHECK(server_ssl_config_.version_interference_probe);
911 DCHECK_NE(OK, ssl_version_interference_error_);
912 base::UmaHistogramSparse("Net.SSLVersionInterferenceError",
913 std::abs(ssl_version_interference_error_));
914 }
915
David Benjamin76a40ad2018-02-24 22:22:08916 // Handle possible client certificate errors that may have occurred if the
917 // stream used SSL for one or more of the layers.
918 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30919
[email protected]8e6441ca2010-08-19 05:56:38920 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09921 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38922 return result;
[email protected]394816e92010-08-03 07:38:59923}
924
[email protected]82918cc2010-08-25 17:24:50925int HttpNetworkTransaction::DoInitStream() {
926 DCHECK(stream_.get());
927 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59928
929 stream_->GetRemoteEndpoint(&remote_endpoint_);
930
Steven Valdezb4ff0412018-01-18 22:39:27931 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
932 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50933}
934
935int HttpNetworkTransaction::DoInitStreamComplete(int result) {
936 if (result == OK) {
937 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50938 } else {
[email protected]82918cc2010-08-25 17:24:50939 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26940 result = HandleIOError(result);
941
942 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22943 if (stream_) {
944 total_received_bytes_ += stream_->GetTotalReceivedBytes();
945 total_sent_bytes_ += stream_->GetTotalSentBytes();
946 }
zhongyica364fbb2015-12-12 03:39:12947 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50948 }
949
950 return result;
951}
952
[email protected]044de0642010-06-17 10:42:15953int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
954 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
955 if (!ShouldApplyProxyAuth())
956 return OK;
[email protected]394816e92010-08-03 07:38:59957 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
958 if (!auth_controllers_[target].get())
Eric Orthbe2efac2019-03-06 01:11:11959 auth_controllers_[target] = new HttpAuthController(
960 target, AuthURL(target), session_->http_auth_cache(),
961 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59962 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41963 io_callback_,
[email protected]394816e92010-08-03 07:38:59964 net_log_);
[email protected]044de0642010-06-17 10:42:15965}
966
967int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
968 DCHECK_NE(ERR_IO_PENDING, rv);
969 if (rv == OK)
970 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
971 return rv;
972}
973
974int HttpNetworkTransaction::DoGenerateServerAuthToken() {
975 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59976 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54977 if (!auth_controllers_[target].get()) {
Eric Orthbe2efac2019-03-06 01:11:11978 auth_controllers_[target] = new HttpAuthController(
979 target, AuthURL(target), session_->http_auth_cache(),
980 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54981 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
982 auth_controllers_[target]->DisableEmbeddedIdentity();
983 }
[email protected]044de0642010-06-17 10:42:15984 if (!ShouldApplyServerAuth())
985 return OK;
[email protected]394816e92010-08-03 07:38:59986 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41987 io_callback_,
[email protected]394816e92010-08-03 07:38:59988 net_log_);
[email protected]044de0642010-06-17 10:42:15989}
990
991int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
992 DCHECK_NE(ERR_IO_PENDING, rv);
993 if (rv == OK)
nharperd6e65822016-03-30 23:05:48994 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14995 return rv;
996}
997
998int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24999 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141000 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1001 GetHostAndOptionalPort(request_->url));
1002
1003 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241004 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141005 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1006 "keep-alive");
1007 } else {
1008 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1009 }
1010
[email protected]2979a492011-04-06 00:29:141011 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111012 if (request_->upload_data_stream) {
1013 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141014 request_headers_.SetHeader(
1015 HttpRequestHeaders::kTransferEncoding, "chunked");
1016 } else {
1017 request_headers_.SetHeader(
1018 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:091019 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141020 }
csharrisonf473dd192015-08-18 13:54:131021 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141022 // An empty POST/PUT request still needs a content length. As for HEAD,
1023 // IE and Safari also add a content length header. Presumably it is to
1024 // support sending a HEAD request to an URL that only expects to be sent a
1025 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131026 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1027 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141028 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1029 }
1030
[email protected]2979a492011-04-06 00:29:141031 // Honor load flags that impact proxy caches.
1032 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1033 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1034 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1035 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1036 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1037 }
1038
1039 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1040 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1041 &request_headers_);
1042 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1043 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1044 &request_headers_);
1045
[email protected]c10450102011-06-27 09:06:161046 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201047
ryansturm49a8cb12016-06-15 16:51:091048 if (!before_headers_sent_callback_.is_null())
1049 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201050
[email protected]173f8e22013-04-10 04:18:201051 response_.did_use_http_auth =
1052 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1053 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141054 return OK;
1055}
1056
[email protected]daddea62012-09-19 05:51:131057int HttpNetworkTransaction::DoInitRequestBody() {
1058 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131059 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111060 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331061 rv = request_->upload_data_stream->Init(
1062 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1063 base::Unretained(this)),
1064 net_log_);
[email protected]daddea62012-09-19 05:51:131065 return rv;
1066}
[email protected]4875ba12011-03-30 22:31:511067
[email protected]daddea62012-09-19 05:51:131068int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1069 if (result == OK)
1070 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131071 return result;
1072}
1073
1074int HttpNetworkTransaction::DoBuildRequest() {
1075 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511076 headers_valid_ = false;
1077
1078 // This is constructed lazily (instead of within our Start method), so that
1079 // we have proxy info available.
1080 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241081 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141082 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511083 }
1084
[email protected]4875ba12011-03-30 22:31:511085 return OK;
1086}
1087
1088int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241089 if (result == OK)
1090 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511091 return result;
1092}
1093
[email protected]0877e3d2009-10-17 22:29:571094int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251095 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571096 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1097
[email protected]bf3eb002012-11-15 05:50:111098 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521099}
1100
[email protected]0877e3d2009-10-17 22:29:571101int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251102 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361103
1104 if (result == ERR_HTTP_1_1_REQUIRED ||
1105 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1106 return HandleHttp11Required(result);
1107 }
1108
initial.commit586acc5fe2008-07-26 22:42:521109 if (result < 0)
1110 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571111 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521112 return OK;
1113}
1114
1115int HttpNetworkTransaction::DoReadHeaders() {
1116 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411117 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521118}
1119
1120int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111121 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1122 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381123 if (IsCertificateError(result)) {
1124 // We don't handle a certificate error during SSL renegotiation, so we
1125 // have to return an error that's not in the certificate error range
1126 // (-2xx).
1127 LOG(ERROR) << "Got a server certificate with error " << result
1128 << " during SSL renegotiation";
1129 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1130 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1131 // TODO(wtc): Need a test case for this code path!
1132 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251133 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381134 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501135 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381136 result = HandleCertificateRequest(result);
1137 if (result == OK)
1138 return result;
[email protected]2181ea002009-06-09 01:37:271139 }
1140
bncfacdd852015-01-09 19:22:541141 if (result == ERR_HTTP_1_1_REQUIRED ||
1142 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1143 return HandleHttp11Required(result);
1144 }
1145
[email protected]c871864d72014-03-13 19:56:191146 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1147 // response headers were received, we do the best we can to make sense of it
1148 // and send it back up the stack.
1149 //
1150 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1151 // bizarre for SPDY. Assuming this logic is useful at all.
1152 // TODO(davidben): Bubble the error code up so we do not cache?
1153 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1154 result = OK;
1155
1156 if (result < 0)
1157 return HandleIOError(result);
1158
[email protected]90499482013-06-01 00:39:501159 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521160
eustasc7d27da2017-04-06 10:33:201161 if (response_.headers.get() && !ContentEncodingsValid())
1162 return ERR_CONTENT_DECODING_FAILED;
1163
[email protected]d58ceea82014-06-04 10:55:541164 // On a 408 response from the server ("Request Timeout") on a stale socket,
1165 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041166 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181167 if (response_.headers.get() &&
1168 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541169 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421170#if BUILDFLAG(ENABLE_REPORTING)
1171 GenerateNetworkErrorLoggingReport(OK);
1172#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541173 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001174 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541175 response_.headers->response_code());
1176 // This will close the socket - it would be weird to try and reuse it, even
1177 // if the server doesn't actually close it.
1178 ResetConnectionAndRequestForResend();
1179 return OK;
1180 }
1181
[email protected]93f8b562012-03-27 01:00:161182 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211183 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161184 const int response_code = response_.headers->response_code();
1185 UMA_HISTOGRAM_ENUMERATION(
1186 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1187 }
1188
[email protected]3abacd62012-06-10 20:20:321189 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001190 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321191 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521192 if (response_headers_callback_)
1193 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391194
bncbe0f6af2015-10-15 17:49:561195 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571196 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1197 // indicates a buggy server. See:
1198 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1199 if (request_->method == "PUT")
1200 return ERR_METHOD_NOT_SUPPORTED;
1201 }
[email protected]4ddaf2502008-10-23 18:26:191202
Steven Valdez58097ec32018-07-16 18:29:041203 if (can_send_early_data_ && response_.headers.get() &&
1204 response_.headers->response_code() == HTTP_TOO_EARLY) {
1205 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1206 }
1207
[email protected]0877e3d2009-10-17 22:29:571208 // Check for an intermediate 100 Continue response. An origin server is
1209 // allowed to send this response even if we didn't ask for it, so we just
1210 // need to skip over it.
1211 // We treat any other 1xx in this same way (although in practice getting
1212 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441213 // Unless this is a WebSocket request, in which case we pass it on up.
1214 if (response_.headers->response_code() / 100 == 1 &&
1215 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451216 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571217 next_state_ = STATE_READ_HEADERS;
1218 return OK;
1219 }
1220
davidbence688ae2017-05-04 15:12:591221 if (response_.headers->response_code() == 421 &&
1222 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421223#if BUILDFLAG(ENABLE_REPORTING)
1224 GenerateNetworkErrorLoggingReport(OK);
1225#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591226 // Retry the request with both IP based pooling and Alternative Services
1227 // disabled.
1228 enable_ip_based_pooling_ = false;
1229 enable_alternative_services_ = false;
1230 net_log_.AddEvent(
1231 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1232 ResetConnectionAndRequestForResend();
1233 return OK;
bnc8016c1f2017-03-31 02:11:291234 }
1235
bncb26024382016-06-29 02:39:451236 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491237 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081238 if (response_.ssl_info.is_valid() &&
1239 !IsCertStatusError(response_.ssl_info.cert_status)) {
1240 session_->http_stream_factory()->ProcessAlternativeServices(
1241 session_, response_.headers.get(),
1242 url::SchemeHostPort(request_->url));
1243 }
1244 }
asanka5ffd5d72016-03-23 16:20:491245
[email protected]e772db3f2010-07-12 18:11:131246 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571247 if (rv != OK)
1248 return rv;
1249
Douglas Creager3cb042052018-11-06 23:08:521250#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421251 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1252 // reports generated before the NEL header is processed here will just be
1253 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141254 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521255 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421256
1257 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111258 // code), or if the response body will not be read, or on a redirect.
1259 // Note: This will report a success for a redirect even if an error is
1260 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421261 int response_code = response_.headers->response_code();
1262 if ((response_code >= 400 && response_code < 600) ||
1263 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1264 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111265 response_.headers->GetContentLength() == 0 ||
1266 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421267 GenerateNetworkErrorLoggingReport(OK);
1268 }
1269#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521270
[email protected]0877e3d2009-10-17 22:29:571271 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261272
1273 // We have reached the end of Start state machine, set the RequestInfo to
1274 // null.
1275 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1276 // only until the final response headers are received. Clearing it will ensure
1277 // that HttpRequestInfo is only used up until final response headers are
1278 // received. Clearing is allowed so that the transaction can be disassociated
1279 // from its creating consumer in cases where it is shared for writing to the
1280 // cache. It is also safe to set it to null at this point since
1281 // upload_data_stream is also not used in the Read state machine.
1282 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1283 request_ = nullptr;
1284
[email protected]0877e3d2009-10-17 22:29:571285 return OK;
initial.commit586acc5fe2008-07-26 22:42:521286}
1287
1288int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501289 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131290 DCHECK_GT(read_buf_len_, 0);
Raul Tambre94493c652019-03-11 17:18:351291 DCHECK(stream_ != nullptr);
initial.commit586acc5fe2008-07-26 22:42:521292
1293 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501294 return stream_->ReadResponseBody(
1295 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521296}
1297
1298int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1299 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381300 bool done = false;
1301 if (result <= 0) {
1302 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521303 done = true;
[email protected]8e6441ca2010-08-19 05:56:381304 }
[email protected]9492e4a2010-02-24 00:58:461305
mmenkebd84c392015-09-02 14:12:341306 // Clean up connection if we are done.
1307 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381308 // Note: Just because IsResponseBodyComplete is true, we're not
1309 // necessarily "done". We're only "done" when it is the last
1310 // read on this HttpNetworkTransaction, which will be signified
1311 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341312 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381313 // the stream. No need to compute it here just to pass back
1314 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341315 bool keep_alive =
1316 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521317
[email protected]8e6441ca2010-08-19 05:56:381318 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011319 // Note: we don't reset the stream here. We've closed it, but we still
1320 // need it around so that callers can call methods such as
1321 // GetUploadProgress() and have them be meaningful.
1322 // TODO(mbelshe): This means we closed the stream here, and we close it
1323 // again in ~HttpNetworkTransaction. Clean that up.
1324
[email protected]8e6441ca2010-08-19 05:56:381325 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171326
1327 // This transaction was successful. If it had been retried because of an
1328 // error with an alternative service, mark that alternative service broken.
1329 if (!enable_alternative_services_ &&
1330 retried_alternative_service_.protocol != kProtoUnknown) {
1331 session_->http_server_properties()->MarkAlternativeServiceBroken(
1332 retried_alternative_service_);
1333 }
Lily Chenfec60d92019-01-24 01:16:421334
1335#if BUILDFLAG(ENABLE_REPORTING)
1336 GenerateNetworkErrorLoggingReport(result);
1337#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521338 }
1339
1340 // Clear these to avoid leaving around old state.
Raul Tambre94493c652019-03-11 17:18:351341 read_buf_ = nullptr;
initial.commit586acc5fe2008-07-26 22:42:521342 read_buf_len_ = 0;
1343
1344 return result;
1345}
1346
[email protected]2d2697f92009-02-18 21:00:321347int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1348 // This method differs from DoReadBody only in the next_state_. So we just
1349 // call DoReadBody and override the next_state_. Perhaps there is a more
1350 // elegant way for these two methods to share code.
1351 int rv = DoReadBody();
1352 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1353 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1354 return rv;
1355}
1356
[email protected]0877e3d2009-10-17 22:29:571357// TODO(wtc): This method and the DoReadBodyComplete method are almost
1358// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321359int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231360 // keep_alive defaults to true because the very reason we're draining the
1361 // response body is to reuse the connection for auth restart.
1362 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321363 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571364 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421365 // Note: No Network Error Logging report is generated here because a report
1366 // will have already been generated for the original request due to the auth
1367 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321368 done = true;
[email protected]68873ba2009-06-04 21:49:231369 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311370 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571371 done = true;
[email protected]2d2697f92009-02-18 21:00:321372 }
1373
1374 if (done) {
1375 DidDrainBodyForAuthRestart(keep_alive);
1376 } else {
1377 // Keep draining.
1378 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1379 }
1380
1381 return OK;
1382}
1383
Douglas Creager3cb042052018-11-06 23:08:521384#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141385void HttpNetworkTransaction::ProcessReportToHeader() {
1386 std::string value;
1387 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1388 return;
1389
1390 ReportingService* service = session_->reporting_service();
1391 if (!service) {
1392 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1393 return;
1394 }
1395
1396 // Only accept Report-To headers on HTTPS connections that have no
1397 // certificate errors.
1398 if (!response_.ssl_info.is_valid()) {
1399 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1400 return;
1401 }
1402 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1403 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1404 return;
1405 }
1406
1407 service->ProcessHeader(url_.GetOrigin(), value);
1408}
1409
Douglas Creager3cb042052018-11-06 23:08:521410void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1411 std::string value;
1412 if (!response_.headers->GetNormalizedHeader(
1413 NetworkErrorLoggingService::kHeaderName, &value)) {
1414 return;
1415 }
1416
1417 NetworkErrorLoggingService* service =
1418 session_->network_error_logging_service();
1419 if (!service) {
1420 NetworkErrorLoggingService::
1421 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1422 return;
1423 }
1424
Lily Chen90ae93cc2019-02-14 01:15:391425 // Don't accept NEL headers received via a proxy, because the IP address of
1426 // the destination server is not known.
1427 if (response_.was_fetched_via_proxy)
1428 return;
1429
Douglas Creager3cb042052018-11-06 23:08:521430 // Only accept NEL headers on HTTPS connections that have no certificate
1431 // errors.
1432 if (!response_.ssl_info.is_valid()) {
1433 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1434 return;
1435 }
1436 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1437 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1438 return;
1439 }
1440
1441 if (remote_endpoint_.address().empty()) {
1442 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1443 return;
1444 }
1445
1446 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1447 value);
1448}
Douglas Creageref5eecdc2018-11-09 20:50:361449
Lily Chenfec60d92019-01-24 01:16:421450void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1451 if (rv < 0 && rv != ERR_IO_PENDING)
1452 GenerateNetworkErrorLoggingReport(rv);
1453}
1454
Douglas Creageref5eecdc2018-11-09 20:50:361455void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421456 // |rv| should be a valid net::Error
1457 DCHECK_NE(rv, ERR_IO_PENDING);
1458 DCHECK_LE(rv, 0);
1459
1460 if (network_error_logging_report_generated_)
1461 return;
1462 network_error_logging_report_generated_ = true;
1463
Douglas Creageref5eecdc2018-11-09 20:50:361464 NetworkErrorLoggingService* service =
1465 session_->network_error_logging_service();
1466 if (!service) {
1467 NetworkErrorLoggingService::
1468 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1469 return;
1470 }
1471
Lily Chen90ae93cc2019-02-14 01:15:391472 // Don't report on proxy auth challenges.
1473 if (response_.headers && response_.headers->response_code() ==
1474 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1475 return;
1476 }
1477
1478 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1479 // internal network details.
1480 if (response_.was_fetched_via_proxy)
1481 return;
1482
Lily Chen00196ab62018-12-04 19:52:291483 // Ignore errors from non-HTTPS origins.
1484 if (!url_.SchemeIsCryptographic()) {
1485 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1486 return;
1487 }
Douglas Creageref5eecdc2018-11-09 20:50:361488
1489 NetworkErrorLoggingService::RequestDetails details;
1490
1491 details.uri = url_;
1492 if (!request_referrer_.empty())
1493 details.referrer = GURL(request_referrer_);
1494 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421495 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361496 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421497 } else {
1498 details.server_ip = IPAddress();
1499 }
Douglas Creageref5eecdc2018-11-09 20:50:361500 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1501 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421502 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361503 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421504 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361505 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421506 }
Douglas Creageref5eecdc2018-11-09 20:50:361507 // If we got response headers, assume that the connection used HTTP/1.1
1508 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421509 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361510 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421511 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361512 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361513 }
Lily Chenfec60d92019-01-24 01:16:421514 details.method = request_method_;
1515 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361516 details.type = static_cast<Error>(rv);
1517 details.reporting_upload_depth = request_reporting_upload_depth_;
1518
1519 service->OnRequest(std::move(details));
1520}
Lily Chenfec60d92019-01-24 01:16:421521#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521522
[email protected]5e363962009-06-19 19:57:011523int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381524 // There are two paths through which the server can request a certificate
1525 // from us. The first is during the initial handshake, the second is
1526 // during SSL renegotiation.
1527 //
1528 // In both cases, we want to close the connection before proceeding.
1529 // We do this for two reasons:
1530 // First, we don't want to keep the connection to the server hung for a
1531 // long time while the user selects a certificate.
1532 // Second, even if we did keep the connection open, NSS has a bug where
1533 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581534 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381535
1536 if (stream_.get()) {
1537 // Since we already have a stream, we're being called as part of SSL
1538 // renegotiation.
1539 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191540 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221541 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381542 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121543 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381544 }
1545
[email protected]26816882010-10-14 18:03:091546 // The server is asking for a client certificate during the initial
1547 // handshake.
1548 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011549
[email protected]ec229bc92010-11-22 09:51:451550 // If the user selected one of the certificates in client_certs or declined
1551 // to provide one for this server before, use the past decision
1552 // automatically.
1553 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541554 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451555 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541556 response_.cert_request_info->host_and_port, &client_cert,
1557 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451558 if (!found_cached_cert)
1559 return error;
1560
1561 // Check that the certificate selected is still a certificate the server
1562 // is likely to accept, based on the criteria supplied in the
1563 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501564 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261565 const std::vector<std::string>& cert_authorities =
1566 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451567
[email protected]f1958c382013-02-07 00:15:261568 bool cert_still_valid = cert_authorities.empty() ||
1569 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451570 if (!cert_still_valid)
1571 return error;
[email protected]5e363962009-06-19 19:57:011572 }
[email protected]ec229bc92010-11-22 09:51:451573
David Benjamin76a40ad2018-02-24 22:22:081574 if (!response_.cert_request_info->is_proxy) {
1575 server_ssl_client_cert_was_cached_ = true;
1576 }
1577
[email protected]ec229bc92010-11-22 09:51:451578 // TODO(davidben): Add a unit test which covers this path; we need to be
1579 // able to send a legitimate certificate and also bypass/clear the
1580 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141581 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1582 &proxy_ssl_config_ : &server_ssl_config_;
1583 ssl_config->send_client_cert = true;
1584 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541585 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451586 next_state_ = STATE_CREATE_STREAM;
1587 // Reset the other member variables.
1588 // Note: this is necessary only with SSL renegotiation.
1589 ResetStateForRestart();
1590 return OK;
[email protected]0b45559b2009-06-12 21:45:111591}
1592
bncfacdd852015-01-09 19:22:541593int HttpNetworkTransaction::HandleHttp11Required(int error) {
1594 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1595 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1596
1597 if (error == ERR_HTTP_1_1_REQUIRED) {
1598 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1599 } else {
1600 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1601 }
1602 ResetConnectionAndRequestForResend();
1603 return OK;
1604}
1605
David Benjamin76a40ad2018-02-24 22:22:081606int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
David Benjamin98ba2be2019-04-09 14:25:251607 // Client certificate errors from the proxy are handled in the
1608 // HttpStreamFactory and below. See discussion in https://crbug.com/828965.
[email protected]384cc73a2013-12-08 22:41:031609 if (server_ssl_config_.send_client_cert &&
1610 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1611 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411612 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031613
David Benjamin76a40ad2018-02-24 22:22:081614 // The private key handle may have gone stale due to, e.g., the user
1615 // unplugging their smartcard. Operating systems do not provide reliable
1616 // notifications for this, so if the signature failed and the private key
1617 // came from SSLClientAuthCache, retry to ask the user for a new one.
1618 if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1619 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1620 server_ssl_client_cert_was_cached_ = false;
1621 server_ssl_config_.send_client_cert = false;
1622 server_ssl_config_.client_cert = nullptr;
1623 server_ssl_config_.client_private_key = nullptr;
1624 retry_attempts_++;
1625 net_log_.AddEventWithNetErrorCode(
1626 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1627 ResetConnectionAndRequestForResend();
1628 return OK;
1629 }
1630 }
initial.commit586acc5fe2008-07-26 22:42:521631 return error;
1632}
1633
[email protected]bd0b6772011-01-11 19:59:301634// This method determines whether it is safe to resend the request after an
1635// IO error. It can only be called in response to request header or body
1636// write errors or response header read errors. It should not be used in
1637// other cases, such as a Connect error.
1638int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031639 // Because the peer may request renegotiation with client authentication at
1640 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081641 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301642
Lily Chenfec60d92019-01-24 01:16:421643#if BUILDFLAG(ENABLE_REPORTING)
1644 GenerateNetworkErrorLoggingReportIfError(error);
1645#endif // BUILDFLAG(ENABLE_REPORTING)
1646
[email protected]bd0b6772011-01-11 19:59:301647 switch (error) {
1648 // If we try to reuse a connection that the server is in the process of
1649 // closing, we may end up successfully writing out our request (or a
1650 // portion of our request) only to find a connection error when we try to
1651 // read from (or finish writing to) the socket.
1652 case ERR_CONNECTION_RESET:
1653 case ERR_CONNECTION_CLOSED:
1654 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511655 // There can be a race between the socket pool checking checking whether a
1656 // socket is still connected, receiving the FIN, and sending/reading data
1657 // on a reused socket. If we receive the FIN between the connectedness
1658 // check and writing/reading from the socket, we may first learn the socket
1659 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1660 // likely happen when trying to retrieve its IP address.
1661 // See http://crbug.com/105824 for more details.
1662 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491663 // If a socket is closed on its initial request, HttpStreamParser returns
1664 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1665 // preconnected but failed to be used before the server timed it out.
1666 case ERR_EMPTY_RESPONSE:
1667 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381668 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001669 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301670 ResetConnectionAndRequestForResend();
1671 error = OK;
1672 }
1673 break;
Steven Valdez58097ec32018-07-16 18:29:041674 case ERR_EARLY_DATA_REJECTED:
1675 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1676 net_log_.AddEventWithNetErrorCode(
1677 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1678 // Disable early data on the SSLConfig on a reset.
1679 can_send_early_data_ = false;
1680 ResetConnectionAndRequestForResend();
1681 error = OK;
1682 break;
[email protected]8753a122011-10-16 08:05:081683 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001684 case ERR_SPDY_SERVER_REFUSED_STREAM:
Bence Béky1a5d8562018-01-05 17:29:281685 case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE:
Yoav Weiss9693572f2018-01-04 09:37:341686 case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
Bence Béky49db0e22018-05-11 00:54:051687 case ERR_SPDY_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271688 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141689 if (HasExceededMaxRetries())
1690 break;
[email protected]b6754252012-06-13 23:14:381691 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001692 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141693 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001694 ResetConnectionAndRequestForResend();
1695 error = OK;
1696 break;
rch2f2991c2017-04-13 19:28:171697 case ERR_QUIC_PROTOCOL_ERROR:
1698 if (GetResponseHeaders() != nullptr ||
1699 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1700 // If the response headers have already been recieved and passed up
1701 // then the request can not be retried. Also, if there was no
1702 // alternative service used for this request, then there is no
1703 // alternative service to be disabled.
1704 break;
1705 }
Biljith Jayan45a41722017-08-16 18:43:141706 if (HasExceededMaxRetries())
1707 break;
rch2f2991c2017-04-13 19:28:171708 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1709 retried_alternative_service_)) {
1710 // If the alternative service was marked as broken while the request
1711 // was in flight, retry the request which will not use the broken
1712 // alternative service.
1713 net_log_.AddEventWithNetErrorCode(
1714 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141715 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171716 ResetConnectionAndRequestForResend();
1717 error = OK;
1718 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1719 // Disable alternative services for this request and retry it. If the
1720 // retry succeeds, then the alternative service will be marked as
1721 // broken then.
1722 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361723 net_log_.AddEventWithNetErrorCode(
1724 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141725 retry_attempts_++;
rch514a44a82017-04-13 04:46:361726 ResetConnectionAndRequestForResend();
1727 error = OK;
1728 }
1729 break;
[email protected]bd0b6772011-01-11 19:59:301730 }
1731 return error;
1732}
1733
[email protected]c3b35c22008-09-27 03:19:421734void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581735 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221736 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191737 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221738 total_sent_bytes_ += stream_->GetTotalSentBytes();
1739 }
zhongyica364fbb2015-12-12 03:39:121740 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581741}
1742
1743void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251744 send_start_time_ = base::TimeTicks();
1745 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251746
[email protected]0757e7702009-03-27 04:00:221747 pending_auth_target_ = HttpAuth::AUTH_NONE;
Raul Tambre94493c652019-03-11 17:18:351748 read_buf_ = nullptr;
[email protected]c3b35c22008-09-27 03:19:421749 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571750 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201751 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141752 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381753 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591754 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121755 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521756 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421757#if BUILDFLAG(ENABLE_REPORTING)
1758 network_error_logging_report_generated_ = false;
1759 start_timeticks_ = base::TimeTicks::Now();
1760#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121761}
1762
1763void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1764 if (stream_)
1765 stream_->PopulateNetErrorDetails(&net_error_details_);
1766 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571767}
1768
[email protected]0877e3d2009-10-17 22:29:571769HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501770 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421771}
1772
[email protected]a34f61ee2014-03-18 20:59:491773bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381774 bool connection_is_proven = stream_->IsConnectionReused();
Raul Tambre94493c652019-03-11 17:18:351775 bool has_received_headers = GetResponseHeaders() != nullptr;
[email protected]58cebf8f2010-07-31 19:20:161776
[email protected]2a5c76b2008-09-25 22:15:161777 // NOTE: we resend a request only if we reused a keep-alive connection.
1778 // This automatically prevents an infinite resend loop because we'll run
1779 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381780 if (connection_is_proven && !has_received_headers)
1781 return true;
1782 return false;
[email protected]1c773ea12009-04-28 19:58:421783}
1784
Biljith Jayan45a41722017-08-16 18:43:141785bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1786 return (retry_attempts_ >= kMaxRetryAttempts);
1787}
1788
David Benjamin83ddfb32018-03-30 01:07:521789bool HttpNetworkTransaction::CheckMaxRestarts() {
1790 num_restarts_++;
1791 return num_restarts_ < kMaxRestarts;
1792}
1793
[email protected]1c773ea12009-04-28 19:58:421794void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381795 if (stream_.get()) {
1796 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121797 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161798 }
1799
[email protected]0877e3d2009-10-17 22:29:571800 // We need to clear request_headers_ because it contains the real request
1801 // headers, but we may need to resend the CONNECT request first to recreate
1802 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201803 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501804 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421805
1806#if BUILDFLAG(ENABLE_REPORTING)
1807 // Reset for new request.
1808 network_error_logging_report_generated_ = false;
1809 start_timeticks_ = base::TimeTicks::Now();
1810#endif // BUILDFLAG(ENABLE_REPORTING)
Wojciech Dzierżanowskiabdeeaf2019-04-01 20:16:221811
1812 ResetStateForRestart();
[email protected]86ec30d2008-09-29 21:53:541813}
1814
[email protected]1c773ea12009-04-28 19:58:421815bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241816 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421817}
license.botbf09a502008-08-24 00:55:551818
[email protected]1c773ea12009-04-28 19:58:421819bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041820 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421821}
1822
[email protected]e772db3f2010-07-12 18:11:131823int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271824 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501825 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421826
[email protected]0877e3d2009-10-17 22:29:571827 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581828 if (status != HTTP_UNAUTHORIZED &&
1829 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421830 return OK;
[email protected]9094b602012-02-27 21:44:581831 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111832 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161833 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1834 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421835
[email protected]9094b602012-02-27 21:44:581836 // This case can trigger when an HTTPS server responds with a "Proxy
1837 // authentication required" status code through a non-authenticating
1838 // proxy.
[email protected]7a67a8152010-11-05 18:31:101839 if (!auth_controllers_[target].get())
1840 return ERR_UNEXPECTED_PROXY_AUTH;
1841
[email protected]a7ea8832010-07-12 17:54:541842 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491843 headers, response_.ssl_info,
1844 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311845 net_log_);
[email protected]228404f2010-06-24 04:31:411846 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491847 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411848
Emily Starkf2c9bbd2019-04-09 17:08:581849 auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge);
[email protected]228404f2010-06-24 04:31:411850
[email protected]228404f2010-06-24 04:31:411851 return rv;
[email protected]f9ee6b52008-11-08 06:46:231852}
1853
[email protected]8e6441ca2010-08-19 05:56:381854bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1855 return auth_controllers_[target].get() &&
1856 auth_controllers_[target]->HaveAuth();
1857}
1858
[email protected]228404f2010-06-24 04:31:411859GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1860 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461861 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411862 if (!proxy_info_.proxy_server().is_valid() ||
1863 proxy_info_.proxy_server().is_direct()) {
1864 return GURL(); // There is no proxy server.
1865 }
[email protected]2df19bb2010-08-25 20:13:461866 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1867 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351868 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461869 }
[email protected]228404f2010-06-24 04:31:411870 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291871 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361872 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291873 }
[email protected]228404f2010-06-24 04:31:411874 return request_->url;
1875 default:
1876 return GURL();
1877 }
[email protected]c3b35c22008-09-27 03:19:421878}
1879
[email protected]831e4a32013-11-14 02:14:441880bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141881 return websocket_handshake_stream_base_create_helper_ &&
1882 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441883}
1884
ttuttle1f2d7e92015-04-28 16:17:471885void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1886 DCHECK(stream_request_);
1887
1888 // Since the transaction can restart with auth credentials, it may create a
1889 // stream more than once. Accumulate all of the connection attempts across
1890 // those streams by appending them to the vector:
1891 for (const auto& attempt : stream_request_->connection_attempts())
1892 connection_attempts_.push_back(attempt);
1893}
1894
eustasc7d27da2017-04-06 10:33:201895bool HttpNetworkTransaction::ContentEncodingsValid() const {
1896 HttpResponseHeaders* headers = GetResponseHeaders();
1897 DCHECK(headers);
1898
1899 std::string accept_encoding;
1900 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1901 &accept_encoding);
1902 std::set<std::string> allowed_encodings;
1903 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1904 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1905 return false;
1906 }
1907
1908 std::string content_encoding;
1909 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1910 std::set<std::string> used_encodings;
1911 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1912 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1913 return false;
1914 }
1915
1916 // When "Accept-Encoding" is not specified, it is parsed as "*".
1917 // If "*" encoding is advertised, then any encoding should be "accepted".
1918 // This does not mean, that it will be successfully decoded.
1919 if (allowed_encodings.find("*") != allowed_encodings.end())
1920 return true;
1921
sky50576f32017-05-01 19:28:031922 bool result = true;
eustasc7d27da2017-04-06 10:33:201923 for (auto const& encoding : used_encodings) {
1924 SourceStream::SourceType source_type =
1925 FilterSourceStream::ParseEncodingType(encoding);
1926 // We don't reject encodings we are not aware. They just will not decode.
1927 if (source_type == SourceStream::TYPE_UNKNOWN)
1928 continue;
1929 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031930 FilterSourceStream::ReportContentDecodingFailed(
1931 SourceStream::TYPE_REJECTED);
1932 result = false;
1933 break;
eustasc7d27da2017-04-06 10:33:201934 }
1935 }
sky50576f32017-05-01 19:28:031936
1937 // Temporary workaround for http://crbug.com/714514
1938 if (headers->IsRedirect(nullptr)) {
1939 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1940 !result);
1941 return true;
1942 }
1943
1944 return result;
eustasc7d27da2017-04-06 10:33:201945}
1946
Eric Roman74103c72019-02-21 00:23:121947static HttpNetworkTransaction::TunnelRedirectHistogramValue
1948GetTunnelRedirectHistogramValue(bool is_main_frame, bool was_auto_detected) {
1949 if (!is_main_frame && !was_auto_detected)
1950 return HttpNetworkTransaction::kSubresourceByExplicitProxy;
1951 if (is_main_frame && !was_auto_detected)
1952 return HttpNetworkTransaction::kMainFrameByExplicitProxy;
1953 if (!is_main_frame && was_auto_detected)
1954 return HttpNetworkTransaction::kSubresourceByAutoDetectedProxy;
1955 return HttpNetworkTransaction::kMainFrameByAutoDetectedProxy;
1956}
1957
1958// TODO(https://crbug.com/928551): Support for redirect on CONNECT is
1959// deprecated, and support will be removed.
1960//
1961// The code in this method handles the temporary histogramming and
1962// compatibility-mode policy during the phase-out.
1963int HttpNetworkTransaction::DoCreateStreamCompletedTunnelResponseRedirect() {
1964 bool is_main_frame = (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) ==
1965 LOAD_MAIN_FRAME_DEPRECATED;
1966 bool was_auto_detected = proxy_info_.did_use_auto_detected_pac_script();
1967
1968 UMA_HISTOGRAM_ENUMERATION(
1969 "Net.Proxy.RedirectDuringConnect",
1970 GetTunnelRedirectHistogramValue(is_main_frame, was_auto_detected));
1971
1972 // For legacy compatibility, the proxy is allowed to redirect CONNECT
1973 // if:
1974 // (a) the request was for a top-level frame
1975 // (b) the proxy server was explicitly configured (i.e. not
1976 // auto-detected).
1977 if (is_main_frame && !was_auto_detected) {
1978 // Return OK and let the caller read the proxy's error page
1979 next_state_ = STATE_NONE;
1980 return OK;
1981 }
1982
1983 // Otherwise let the request fail.
1984 stream_.reset();
1985 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT;
1986}
1987
[email protected]c3b35c22008-09-27 03:19:421988} // namespace net