blob: 359d488a064576898583fc6014a42a654638a041 [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
Biljith Jayan45a41722017-08-16 18:43:1477namespace {
David Benjamin83ddfb32018-03-30 01:07:5278
Biljith Jayan45a41722017-08-16 18:43:1479// Max number of |retry_attempts| (excluding the initial request) after which
80// we give up and show an error page.
81const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5282
83// Max number of calls to RestartWith* allowed for a single connection. A single
84// HttpNetworkTransaction should not signal very many restartable errors, but it
85// may occur due to a bug (e.g. https://crbug.com/823387 or
86// https://crbug.com/488043) or simply if the server or proxy requests
87// authentication repeatedly. Although these calls are often associated with a
88// user prompt, in other scenarios (remembered preferences, extensions,
89// multi-leg authentication), they may be triggered automatically. To avoid
90// looping forever, bound the number of restarts.
91const size_t kMaxRestarts = 32;
92
Biljith Jayan45a41722017-08-16 18:43:1493} // namespace
94
initial.commit586acc5fe2008-07-26 22:42:5295namespace net {
96
Victor Costan9c7302b2018-08-27 16:39:4497const int HttpNetworkTransaction::kDrainBodyBufferSize;
98
[email protected]262eec82013-03-19 21:01:3699HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
100 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22101 : pending_auth_target_(HttpAuth::AUTH_NONE),
Bence Béky61f756c2018-04-25 14:17:53102 io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete,
103 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52104 session_(session),
105 request_(NULL),
[email protected]262eec82013-03-19 21:01:36106 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57107 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27108 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08109 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20110 request_headers_(),
Lily Chenfec60d92019-01-24 01:16:42111#if BUILDFLAG(ENABLE_REPORTING)
112 network_error_logging_report_generated_(false),
113 request_reporting_upload_depth_(0),
114#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:52115 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19116 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22117 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54118 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44119 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29120 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26121 enable_alternative_services_(true),
zhongyi48704c182015-12-07 07:52:02122 websocket_handshake_stream_base_create_helper_(NULL),
Biljith Jayan45a41722017-08-16 18:43:14123 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52124 retry_attempts_(0),
David Benjamin5cb91132018-04-06 05:54:49125 num_restarts_(0),
Lily Chenfec60d92019-01-24 01:16:42126 ssl_version_interference_error_(OK) {
127}
[email protected]3ce7df0f2010-03-03 00:30:50128
[email protected]0b0bf032010-09-21 18:08:50129HttpNetworkTransaction::~HttpNetworkTransaction() {
Lily Chenfec60d92019-01-24 01:16:42130#if BUILDFLAG(ENABLE_REPORTING)
Lily Chend3930e72019-03-01 19:31:11131 // If no error or success report has been generated yet at this point, then
132 // this network transaction was prematurely cancelled.
133 GenerateNetworkErrorLoggingReport(ERR_ABORTED);
Lily Chenfec60d92019-01-24 01:16:42134#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]0b0bf032010-09-21 18:08:50135 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50136 // TODO(mbelshe): The stream_ should be able to compute whether or not the
137 // stream should be kept alive. No reason to compute here
138 // and pass it in.
mmenkebd84c392015-09-02 14:12:34139 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50140 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34141 } else if (stream_->IsResponseBodyComplete()) {
142 // If the response body is complete, we can just reuse the socket.
143 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50144 } else {
mmenkebd84c392015-09-02 14:12:34145 // Otherwise, we try to drain the response body.
146 HttpStream* stream = stream_.release();
147 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50148 }
149 }
[email protected]02cad5d2013-10-02 08:14:03150 if (request_ && request_->upload_data_stream)
151 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50152}
153
[email protected]684970b2009-08-14 04:54:46154int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
Bence Béky046f8c82018-06-12 02:26:04155 CompletionOnceCallback callback,
tfarina42834112016-09-22 13:38:20156 const NetLogWithSource& net_log) {
Ramin Halavatib5e433e2018-02-07 07:41:10157 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53158 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04159 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15160 url_ = request_->url;
Douglas Creageref5eecdc2018-11-09 20:50:36161#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:42162 // Store values for later use in NEL report generation.
Douglas Creageref5eecdc2018-11-09 20:50:36163 request_method_ = request_->method;
164 request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer,
165 &request_referrer_);
166 request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent,
167 &request_user_agent_);
168 request_reporting_upload_depth_ = request_->reporting_upload_depth;
Lily Chenfec60d92019-01-24 01:16:42169 start_timeticks_ = base::TimeTicks::Now();
170#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]96d570e42008-08-05 22:43:04171
nharper8cdb0fb2016-04-22 21:34:59172 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
173 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
174
Ryan Sleevi24fe2682018-08-16 21:33:46175 if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) {
176 server_ssl_config_.disable_cert_verification_network_fetches = true;
177 proxy_ssl_config_.disable_cert_verification_network_fetches = true;
[email protected]99ffa5a2011-10-06 04:20:19178 }
[email protected]6fbac162011-06-20 00:29:04179
Steven Valdez03e872d2018-03-02 15:39:15180 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27181 can_send_early_data_ = true;
182 }
183
jkarlinfb1d5172015-01-12 14:10:29184 if (request_->load_flags & LOAD_PREFETCH)
185 response_.unused_since_prefetch = true;
186
Helen Lib495c3802018-03-30 13:46:09187 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04188 int rv = DoLoop(OK);
189 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04190 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42191
192 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
193 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
194 // other net::Error can be returned.
195 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]96d570e42008-08-05 22:43:04196 return rv;
197}
198
199int HttpNetworkTransaction::RestartIgnoringLastError(
Bence Béky046f8c82018-06-12 02:26:04200 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38201 DCHECK(!stream_.get());
202 DCHECK(!stream_request_.get());
203 DCHECK_EQ(STATE_NONE, next_state_);
204
David Benjamin83ddfb32018-03-30 01:07:52205 if (!CheckMaxRestarts())
206 return ERR_TOO_MANY_RETRIES;
207
[email protected]82918cc2010-08-25 17:24:50208 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38209
[email protected]ccb40e52008-09-17 20:54:40210 int rv = DoLoop(OK);
211 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04212 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42213
214 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
215 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
216 // other net::Error can be returned.
217 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]aaead502008-10-15 00:20:11218 return rv;
[email protected]96d570e42008-08-05 22:43:04219}
220
[email protected]0b45559b2009-06-12 21:45:11221int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08222 scoped_refptr<X509Certificate> client_cert,
223 scoped_refptr<SSLPrivateKey> client_private_key,
Bence Béky046f8c82018-06-12 02:26:04224 CompletionOnceCallback callback) {
[email protected]8e6441ca2010-08-19 05:56:38225 // In HandleCertificateRequest(), we always tear down existing stream
226 // requests to force a new connection. So we shouldn't have one here.
227 DCHECK(!stream_request_.get());
228 DCHECK(!stream_.get());
229 DCHECK_EQ(STATE_NONE, next_state_);
230
David Benjamin83ddfb32018-03-30 01:07:52231 if (!CheckMaxRestarts())
232 return ERR_TOO_MANY_RETRIES;
233
[email protected]102957f2011-09-02 17:10:14234 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
235 &proxy_ssl_config_ : &server_ssl_config_;
236 ssl_config->send_client_cert = true;
237 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54238 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45239 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08240 response_.cert_request_info->host_and_port, std::move(client_cert),
241 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11242 // Reset the other member variables.
243 // Note: this is necessary only with SSL renegotiation.
244 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50245 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11246 int rv = DoLoop(OK);
247 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04248 callback_ = std::move(callback);
Lily Chenfec60d92019-01-24 01:16:42249
250 // This always returns ERR_IO_PENDING because DoCreateStream() does, but
251 // GenerateNetworkErrorLoggingReportIfError() should be called here if any
252 // other net::Error can be returned.
253 DCHECK_EQ(rv, ERR_IO_PENDING);
[email protected]0b45559b2009-06-12 21:45:11254 return rv;
255}
256
Bence Béky046f8c82018-06-12 02:26:04257int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials,
258 CompletionOnceCallback callback) {
David Benjamin83ddfb32018-03-30 01:07:52259 if (!CheckMaxRestarts())
260 return ERR_TOO_MANY_RETRIES;
261
[email protected]0757e7702009-03-27 04:00:22262 HttpAuth::Target target = pending_auth_target_;
263 if (target == HttpAuth::AUTH_NONE) {
264 NOTREACHED();
265 return ERR_UNEXPECTED;
266 }
[email protected]0757e7702009-03-27 04:00:22267 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42268
[email protected]f3cf9802011-10-28 18:44:58269 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13270
[email protected]49639fa2011-12-20 23:22:41271 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38272
273 int rv = OK;
274 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
275 // In this case, we've gathered credentials for use with proxy
276 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50277 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38278 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59279 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54280 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39281 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54282 } else {
[email protected]8e6441ca2010-08-19 05:56:38283 // In this case, we've gathered credentials for the server or the proxy
284 // but it is not during the tunneling phase.
285 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54286 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38287 rv = DoLoop(OK);
Lily Chenfec60d92019-01-24 01:16:42288 // Note: If an error is encountered while draining the old response body, no
289 // Network Error Logging report will be generated, because the error was
290 // with the old request, which will already have had a NEL report generated
291 // for it due to the auth challenge (so we don't report a second error for
292 // that request).
[email protected]a7ea8832010-07-12 17:54:54293 }
[email protected]c3b35c22008-09-27 03:19:42294
[email protected]c3b35c22008-09-27 03:19:42295 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04296 callback_ = std::move(callback);
[email protected]c3b35c22008-09-27 03:19:42297 return rv;
[email protected]96d570e42008-08-05 22:43:04298}
299
[email protected]f9ee6b52008-11-08 06:46:23300void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
301 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38302 DCHECK(!stream_request_.get());
303
Bence Béky3238f2e12017-09-22 22:44:49304 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
305 if (target == HttpAuth::AUTH_SERVER &&
306 auth_controllers_[target]->NeedsHTTP11()) {
307 session_->http_server_properties()->SetHTTP11Required(
308 HostPortPair::FromURL(request_->url));
309 }
310
[email protected]2d2697f92009-02-18 21:00:32311 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57312 // Even if the server says the connection is keep-alive, we have to be
313 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34314 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57315 // If the response body hasn't been completely read, we need to drain
316 // it first.
[email protected]351ab642010-08-05 16:55:31317 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32318 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
Victor Costan9c7302b2018-08-27 16:39:44319 read_buf_ = base::MakeRefCounted<IOBuffer>(
320 kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32321 read_buf_len_ = kDrainBodyBufferSize;
322 return;
323 }
[email protected]0877e3d2009-10-17 22:29:57324 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09325 }
326
[email protected]2d2697f92009-02-18 21:00:32327 // We don't need to drain the response body, so we act as if we had drained
328 // the response body.
329 DidDrainBodyForAuthRestart(keep_alive);
330}
331
332void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38333 DCHECK(!stream_request_.get());
334
335 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19336 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22337 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58338 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34339 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38340 // We should call connection_->set_idle_time(), but this doesn't occur
341 // often enough to be worth the trouble.
342 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39343 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38344 }
[email protected]697ef4c2010-10-14 16:38:58345
346 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46347 // Close the stream and mark it as not_reusable. Even in the
348 // keep_alive case, we've determined that the stream_ is not
349 // reusable if new_stream is NULL.
350 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58351 next_state_ = STATE_CREATE_STREAM;
352 } else {
sclittlefb249892015-09-10 21:33:22353 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19354 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22355 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58356 next_state_ = STATE_INIT_STREAM;
357 }
358 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32359 }
[email protected]f9ee6b52008-11-08 06:46:23360
361 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58362 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23363}
364
[email protected]8e6441ca2010-08-19 05:56:38365bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
366 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
367 HaveAuth(pending_auth_target_);
368}
369
Bence Béky046f8c82018-06-12 02:26:04370int HttpNetworkTransaction::Read(IOBuffer* buf,
371 int buf_len,
372 CompletionOnceCallback callback) {
[email protected]96d570e42008-08-05 22:43:04373 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35374 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04375
[email protected]ad8e04a2010-11-01 04:16:27376 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38377 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04378 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29379 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04380 // bytes when establishing a tunnel because they might be controlled by
381 // an active network attacker. We don't worry about this for HTTP
382 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29383 // We reach this case when the user cancels a 407 proxy auth prompt. We
384 // also don't worry about this for an HTTPS Proxy, because the
385 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04386 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56387 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
388 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58389 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54390 LOG(WARNING) << "Blocked proxy response with status "
391 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15392 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04393 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44394 }
395
[email protected]e60e47a2010-07-14 03:37:18396 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15397 next_state_ = STATE_READ_BODY;
398
[email protected]96d570e42008-08-05 22:43:04399 read_buf_ = buf;
400 read_buf_len_ = buf_len;
401
[email protected]96d570e42008-08-05 22:43:04402 int rv = DoLoop(OK);
403 if (rv == ERR_IO_PENDING)
Bence Béky046f8c82018-06-12 02:26:04404 callback_ = std::move(callback);
[email protected]96d570e42008-08-05 22:43:04405 return rv;
406}
407
[email protected]8cd06c02014-01-25 07:50:14408void HttpNetworkTransaction::StopCaching() {}
409
[email protected]79e1fd62013-06-20 06:50:04410bool HttpNetworkTransaction::GetFullRequestHeaders(
411 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32412 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04413 *headers = request_headers_;
414 return true;
415}
416
sclittle4de1bab92015-09-22 21:28:24417int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
418 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19419 if (stream_)
420 total_received_bytes += stream_->GetTotalReceivedBytes();
421 return total_received_bytes;
422}
423
sclittlefb249892015-09-10 21:33:22424int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
425 int64_t total_sent_bytes = total_sent_bytes_;
426 if (stream_)
427 total_sent_bytes += stream_->GetTotalSentBytes();
428 return total_sent_bytes;
429}
430
[email protected]8cd06c02014-01-25 07:50:14431void HttpNetworkTransaction::DoneReading() {}
432
[email protected]96d570e42008-08-05 22:43:04433const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55434 return &response_;
[email protected]96d570e42008-08-05 22:43:04435}
436
437LoadState HttpNetworkTransaction::GetLoadState() const {
438 // TODO(wtc): Define a new LoadState value for the
439 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
440 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48441 case STATE_CREATE_STREAM:
442 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50443 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38444 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15445 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
446 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57447 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04448 return LOAD_STATE_SENDING_REQUEST;
449 case STATE_READ_HEADERS_COMPLETE:
450 return LOAD_STATE_WAITING_FOR_RESPONSE;
451 case STATE_READ_BODY_COMPLETE:
452 return LOAD_STATE_READING_RESPONSE;
453 default:
454 return LOAD_STATE_IDLE;
455 }
456}
457
[email protected]8cd06c02014-01-25 07:50:14458void HttpNetworkTransaction::SetQuicServerInfo(
459 QuicServerInfo* quic_server_info) {}
460
[email protected]5033ab82013-03-22 20:17:46461bool HttpNetworkTransaction::GetLoadTimingInfo(
462 LoadTimingInfo* load_timing_info) const {
463 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
464 return false;
465
466 load_timing_info->proxy_resolve_start =
467 proxy_info_.proxy_resolve_start_time();
468 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
469 load_timing_info->send_start = send_start_time_;
470 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46471 return true;
472}
473
ttuttled9dbc652015-09-29 20:00:59474bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36475 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59476 return false;
477
478 *endpoint = remote_endpoint_;
479 return true;
480}
481
zhongyi48704c182015-12-07 07:52:02482void HttpNetworkTransaction::PopulateNetErrorDetails(
483 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12484 *details = net_error_details_;
485 if (stream_)
486 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02487}
488
[email protected]5033ab82013-03-22 20:17:46489void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
490 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50491
[email protected]bf828982013-08-14 18:01:47492 if (stream_request_)
493 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03494 if (stream_)
495 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50496
rdsmith1d343be52016-10-21 20:37:50497 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46498}
499
[email protected]831e4a32013-11-14 02:14:44500void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
501 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
502 websocket_handshake_stream_base_create_helper_ = create_helper;
503}
504
[email protected]1826a402014-01-08 15:40:48505void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
506 const BeforeNetworkStartCallback& callback) {
507 before_network_start_callback_ = callback;
508}
509
ryansturm49a8cb12016-06-15 16:51:09510void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
511 const BeforeHeadersSentCallback& callback) {
512 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27513}
514
Andrey Kosyakov83a6eee2017-08-14 19:20:04515void HttpNetworkTransaction::SetRequestHeadersCallback(
516 RequestHeadersCallback callback) {
517 DCHECK(!stream_);
518 request_headers_callback_ = std::move(callback);
519}
520
Andrey Kosyakov2e893e62017-08-31 17:00:52521void HttpNetworkTransaction::SetResponseHeadersCallback(
522 ResponseHeadersCallback callback) {
523 DCHECK(!stream_);
524 response_headers_callback_ = std::move(callback);
525}
526
[email protected]1826a402014-01-08 15:40:48527int HttpNetworkTransaction::ResumeNetworkStart() {
528 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
529 return DoLoop(OK);
530}
531
[email protected]102e27c2011-02-23 01:01:31532void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
533 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00534 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50535 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38536 DCHECK(stream_request_.get());
537
sclittlefb249892015-09-10 21:33:22538 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19539 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22540 total_sent_bytes_ += stream_->GetTotalSentBytes();
541 }
bnc5029f4632017-06-08 16:19:00542 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04543 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14544 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31545 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52546 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21547 response_.alpn_negotiated_protocol =
548 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38549 response_.was_fetched_via_spdy = stream_request_->using_spdy();
550 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15551 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47552 response_.proxy_server = proxy_info_.proxy_server();
553 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
554 response_.proxy_server = ProxyServer::Direct();
555 else
556 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38557 OnIOComplete(OK);
558}
559
xunjieli5749218c2016-03-22 16:43:06560void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08561 const SSLConfig& used_ssl_config,
562 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00563 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08564 NOTREACHED();
565}
566
[email protected]a9cf2b92013-10-30 12:08:49567void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50568 const SSLConfig& used_ssl_config,
569 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00570 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
571 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50572}
573
Ryan Hamilton75f197262017-08-17 14:00:07574void HttpNetworkTransaction::OnStreamFailed(
575 int result,
576 const NetErrorDetails& net_error_details,
577 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50578 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38579 DCHECK_NE(OK, result);
580 DCHECK(stream_request_.get());
581 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14582 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07583 net_error_details_ = net_error_details;
[email protected]8e6441ca2010-08-19 05:56:38584
585 OnIOComplete(result);
586}
587
[email protected]102e27c2011-02-23 01:01:31588void HttpNetworkTransaction::OnCertificateError(
589 int result,
590 const SSLConfig& used_ssl_config,
591 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50592 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38593 DCHECK_NE(OK, result);
594 DCHECK(stream_request_.get());
595 DCHECK(!stream_.get());
596
597 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14598 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38599
600 // TODO(mbelshe): For now, we're going to pass the error through, and that
601 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50602 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
603 // good and the user chooses to ignore the error. This is not ideal, but not
604 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38605
606 OnIOComplete(result);
607}
608
609void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50610 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31611 const SSLConfig& used_ssl_config,
612 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50613 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38614 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50615 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38616
617 establishing_tunnel_ = true;
618 response_.headers = proxy_response.headers;
619 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20620
621 if (response_.headers.get() && !ContentEncodingsValid()) {
622 DoCallback(ERR_CONTENT_DECODING_FAILED);
623 return;
624 }
625
[email protected]8e6441ca2010-08-19 05:56:38626 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14627 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31628 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38629
630 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
631 pending_auth_target_ = HttpAuth::AUTH_PROXY;
632
633 DoCallback(OK);
634}
635
636void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31637 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50638 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50639 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38640
[email protected]102957f2011-09-02 17:10:14641 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38642 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58643 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38644}
645
Eric Roman74103c72019-02-21 00:23:12646void HttpNetworkTransaction::OnHttpsProxyTunnelResponseRedirect(
[email protected]511f6f52010-12-17 03:58:29647 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31648 const SSLConfig& used_ssl_config,
649 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01650 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29651 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
652
ttuttle1f2d7e92015-04-28 16:17:47653 CopyConnectionAttemptsFromStreamRequest();
654
[email protected]511f6f52010-12-17 03:58:29655 headers_valid_ = true;
656 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14657 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31658 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22659 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19660 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22661 total_sent_bytes_ += stream_->GetTotalSentBytes();
662 }
Bence Béky6d05ebd2017-05-16 00:09:01663 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04664 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29665 stream_request_.reset(); // we're done with the stream request
Eric Roman74103c72019-02-21 00:23:12666 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT);
[email protected]511f6f52010-12-17 03:58:29667}
668
zhongyi48704c182015-12-07 07:52:02669void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12670 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02671}
672
ttuttle1f2d7e92015-04-28 16:17:47673void HttpNetworkTransaction::GetConnectionAttempts(
674 ConnectionAttempts* out) const {
675 *out = connection_attempts_;
676}
677
Adam Ricecb76ac62015-02-20 05:33:25678bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37679 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52680}
681
Adam Rice425cf122015-01-19 06:18:24682bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12683 return (proxy_info_.is_http() || proxy_info_.is_https() ||
684 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24685 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
686}
687
initial.commit586acc5fe2008-07-26 22:42:52688void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50689 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41690 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52691
Douglas Creageref5eecdc2018-11-09 20:50:36692#if BUILDFLAG(ENABLE_REPORTING)
693 // Just before invoking the caller's completion callback, generate a NEL
Lily Chenfec60d92019-01-24 01:16:42694 // report about this network request if the result was an error.
695 GenerateNetworkErrorLoggingReportIfError(rv);
696#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creageref5eecdc2018-11-09 20:50:36697
[email protected]96d570e42008-08-05 22:43:04698 // Since Run may result in Read being called, clear user_callback_ up front.
Bence Békybc1de292018-02-01 15:48:03699 base::ResetAndReturn(&callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52700}
701
702void HttpNetworkTransaction::OnIOComplete(int result) {
703 int rv = DoLoop(result);
704 if (rv != ERR_IO_PENDING)
705 DoCallback(rv);
706}
707
708int HttpNetworkTransaction::DoLoop(int result) {
709 DCHECK(next_state_ != STATE_NONE);
710
711 int rv = result;
712 do {
713 State state = next_state_;
714 next_state_ = STATE_NONE;
715 switch (state) {
[email protected]1826a402014-01-08 15:40:48716 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
717 DCHECK_EQ(OK, rv);
718 rv = DoNotifyBeforeCreateStream();
719 break;
[email protected]82918cc2010-08-25 17:24:50720 case STATE_CREATE_STREAM:
721 DCHECK_EQ(OK, rv);
722 rv = DoCreateStream();
723 break;
724 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46725 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
726 // solved.
727 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50728 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46729 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50730 break;
[email protected]351ab642010-08-05 16:55:31731 case STATE_INIT_STREAM:
732 DCHECK_EQ(OK, rv);
733 rv = DoInitStream();
734 break;
735 case STATE_INIT_STREAM_COMPLETE:
736 rv = DoInitStreamComplete(rv);
737 break;
[email protected]044de0642010-06-17 10:42:15738 case STATE_GENERATE_PROXY_AUTH_TOKEN:
739 DCHECK_EQ(OK, rv);
740 rv = DoGenerateProxyAuthToken();
741 break;
742 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
743 rv = DoGenerateProxyAuthTokenComplete(rv);
744 break;
745 case STATE_GENERATE_SERVER_AUTH_TOKEN:
746 DCHECK_EQ(OK, rv);
747 rv = DoGenerateServerAuthToken();
748 break;
749 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
750 rv = DoGenerateServerAuthTokenComplete(rv);
751 break;
[email protected]daddea62012-09-19 05:51:13752 case STATE_INIT_REQUEST_BODY:
753 DCHECK_EQ(OK, rv);
754 rv = DoInitRequestBody();
755 break;
756 case STATE_INIT_REQUEST_BODY_COMPLETE:
757 rv = DoInitRequestBodyComplete(rv);
758 break;
[email protected]4875ba12011-03-30 22:31:51759 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55760 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00761 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51762 rv = DoBuildRequest();
763 break;
764 case STATE_BUILD_REQUEST_COMPLETE:
765 rv = DoBuildRequestComplete(rv);
766 break;
767 case STATE_SEND_REQUEST:
768 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57769 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52770 break;
[email protected]0877e3d2009-10-17 22:29:57771 case STATE_SEND_REQUEST_COMPLETE:
772 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43773 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00774 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52775 break;
776 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55777 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00778 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52779 rv = DoReadHeaders();
780 break;
781 case STATE_READ_HEADERS_COMPLETE:
782 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43783 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00784 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52785 break;
786 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55787 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00788 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52789 rv = DoReadBody();
790 break;
791 case STATE_READ_BODY_COMPLETE:
792 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43793 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00794 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52795 break;
[email protected]2d2697f92009-02-18 21:00:32796 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55797 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53798 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00799 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32800 rv = DoDrainBodyForAuthRestart();
801 break;
802 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
803 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43804 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00805 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32806 break;
initial.commit586acc5fe2008-07-26 22:42:52807 default:
808 NOTREACHED() << "bad state";
809 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04810 break;
initial.commit586acc5fe2008-07-26 22:42:52811 }
812 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
813
814 return rv;
815}
816
[email protected]1826a402014-01-08 15:40:48817int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
818 next_state_ = STATE_CREATE_STREAM;
819 bool defer = false;
820 if (!before_network_start_callback_.is_null())
821 before_network_start_callback_.Run(&defer);
822 if (!defer)
823 return OK;
824 return ERR_IO_PENDING;
825}
826
[email protected]82918cc2010-08-25 17:24:50827int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55828 response_.network_accessed = true;
829
[email protected]82918cc2010-08-25 17:24:50830 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17831 // IP based pooling is only enabled on a retry after 421 Misdirected Request
832 // is received. Alternative Services are also disabled in this case (though
833 // they can also be disabled when retrying after a QUIC error).
834 if (!enable_ip_based_pooling_)
835 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44836 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27837 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06838 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
839 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
840 websocket_handshake_stream_base_create_helper_,
841 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44842 } else {
xunjieli96f2a402017-06-05 17:24:27843 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29844 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27845 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44846 }
[email protected]26816882010-10-14 18:03:09847 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38848 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31849}
850
[email protected]82918cc2010-08-25 17:24:50851int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
Eric Roman74103c72019-02-21 00:23:12852 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT, then
853 // DoCreateStreamComplete is being called from
854 // OnHttpsProxyTunnelResponseRedirect, which resets the stream request first.
855 // Therefore, we have to grab the connection attempts in *that* function
856 // instead of here in that case.
857 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT)
ttuttle1f2d7e92015-04-28 16:17:47858 CopyConnectionAttemptsFromStreamRequest();
859
[email protected]394816e92010-08-03 07:38:59860 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50861 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38862 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33863 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
864 result = HandleCertificateRequest(result);
Eric Roman74103c72019-02-21 00:23:12865 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT) {
866 return DoCreateStreamCompletedTunnelResponseRedirect();
bncfacdd852015-01-09 19:22:54867 } else if (result == ERR_HTTP_1_1_REQUIRED ||
868 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
869 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59870 }
871
David Benjamin5cb91132018-04-06 05:54:49872 // Perform a TLS 1.3 version interference probe on various connection
873 // errors. The retry will never produce a successful connection but may map
874 // errors to ERR_SSL_VERSION_INTERFERENCE, which signals a probable
875 // version-interfering middlebox.
876 if (IsSecureRequest() && !HasExceededMaxRetries() &&
877 server_ssl_config_.version_max == SSL_PROTOCOL_VERSION_TLS1_3 &&
878 !server_ssl_config_.version_interference_probe) {
879 if (result == ERR_CONNECTION_CLOSED || result == ERR_SSL_PROTOCOL_ERROR ||
880 result == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
881 result == ERR_CONNECTION_RESET ||
882 result == ERR_SSL_BAD_RECORD_MAC_ALERT) {
883 // Report the error code for each time a version interference probe is
884 // triggered.
885 base::UmaHistogramSparse("Net.SSLVersionInterferenceProbeTrigger",
886 std::abs(result));
887 net_log_.AddEventWithNetErrorCode(
888 NetLogEventType::SSL_VERSION_INTERFERENCE_PROBE, result);
889
890 retry_attempts_++;
891 server_ssl_config_.version_interference_probe = true;
892 server_ssl_config_.version_max = SSL_PROTOCOL_VERSION_TLS1_2;
893 ssl_version_interference_error_ = result;
894 ResetConnectionAndRequestForResend();
895 return OK;
896 }
897 }
898
899 if (result == ERR_SSL_VERSION_INTERFERENCE) {
900 // Record the error code version interference was detected at.
901 DCHECK(server_ssl_config_.version_interference_probe);
902 DCHECK_NE(OK, ssl_version_interference_error_);
903 base::UmaHistogramSparse("Net.SSLVersionInterferenceError",
904 std::abs(ssl_version_interference_error_));
905 }
906
David Benjamin76a40ad2018-02-24 22:22:08907 // Handle possible client certificate errors that may have occurred if the
908 // stream used SSL for one or more of the layers.
909 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30910
[email protected]8e6441ca2010-08-19 05:56:38911 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09912 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38913 return result;
[email protected]394816e92010-08-03 07:38:59914}
915
[email protected]82918cc2010-08-25 17:24:50916int HttpNetworkTransaction::DoInitStream() {
917 DCHECK(stream_.get());
918 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59919
920 stream_->GetRemoteEndpoint(&remote_endpoint_);
921
Steven Valdezb4ff0412018-01-18 22:39:27922 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
923 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50924}
925
926int HttpNetworkTransaction::DoInitStreamComplete(int result) {
927 if (result == OK) {
928 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50929 } else {
[email protected]82918cc2010-08-25 17:24:50930 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26931 result = HandleIOError(result);
932
933 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22934 if (stream_) {
935 total_received_bytes_ += stream_->GetTotalReceivedBytes();
936 total_sent_bytes_ += stream_->GetTotalSentBytes();
937 }
zhongyica364fbb2015-12-12 03:39:12938 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50939 }
940
941 return result;
942}
943
[email protected]044de0642010-06-17 10:42:15944int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
945 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
946 if (!ShouldApplyProxyAuth())
947 return OK;
[email protected]394816e92010-08-03 07:38:59948 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
949 if (!auth_controllers_[target].get())
Eric Orthbe2efac2019-03-06 01:11:11950 auth_controllers_[target] = new HttpAuthController(
951 target, AuthURL(target), session_->http_auth_cache(),
952 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]394816e92010-08-03 07:38:59953 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41954 io_callback_,
[email protected]394816e92010-08-03 07:38:59955 net_log_);
[email protected]044de0642010-06-17 10:42:15956}
957
958int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
959 DCHECK_NE(ERR_IO_PENDING, rv);
960 if (rv == OK)
961 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
962 return rv;
963}
964
965int HttpNetworkTransaction::DoGenerateServerAuthToken() {
966 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59967 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54968 if (!auth_controllers_[target].get()) {
Eric Orthbe2efac2019-03-06 01:11:11969 auth_controllers_[target] = new HttpAuthController(
970 target, AuthURL(target), session_->http_auth_cache(),
971 session_->http_auth_handler_factory(), session_->host_resolver());
[email protected]2217aa22013-10-11 03:03:54972 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
973 auth_controllers_[target]->DisableEmbeddedIdentity();
974 }
[email protected]044de0642010-06-17 10:42:15975 if (!ShouldApplyServerAuth())
976 return OK;
[email protected]394816e92010-08-03 07:38:59977 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41978 io_callback_,
[email protected]394816e92010-08-03 07:38:59979 net_log_);
[email protected]044de0642010-06-17 10:42:15980}
981
982int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
983 DCHECK_NE(ERR_IO_PENDING, rv);
984 if (rv == OK)
nharperd6e65822016-03-30 23:05:48985 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14986 return rv;
987}
988
989int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24990 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14991 request_headers_.SetHeader(HttpRequestHeaders::kHost,
992 GetHostAndOptionalPort(request_->url));
993
994 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24995 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14996 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
997 "keep-alive");
998 } else {
999 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1000 }
1001
[email protected]2979a492011-04-06 00:29:141002 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111003 if (request_->upload_data_stream) {
1004 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141005 request_headers_.SetHeader(
1006 HttpRequestHeaders::kTransferEncoding, "chunked");
1007 } else {
1008 request_headers_.SetHeader(
1009 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:091010 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141011 }
csharrisonf473dd192015-08-18 13:54:131012 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141013 // An empty POST/PUT request still needs a content length. As for HEAD,
1014 // IE and Safari also add a content length header. Presumably it is to
1015 // support sending a HEAD request to an URL that only expects to be sent a
1016 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131017 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1018 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141019 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1020 }
1021
[email protected]2979a492011-04-06 00:29:141022 // Honor load flags that impact proxy caches.
1023 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1024 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1025 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1026 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1027 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1028 }
1029
1030 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1031 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1032 &request_headers_);
1033 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1034 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1035 &request_headers_);
1036
[email protected]c10450102011-06-27 09:06:161037 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201038
ryansturm49a8cb12016-06-15 16:51:091039 if (!before_headers_sent_callback_.is_null())
1040 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201041
[email protected]173f8e22013-04-10 04:18:201042 response_.did_use_http_auth =
1043 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1044 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141045 return OK;
1046}
1047
[email protected]daddea62012-09-19 05:51:131048int HttpNetworkTransaction::DoInitRequestBody() {
1049 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131050 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111051 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331052 rv = request_->upload_data_stream->Init(
1053 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1054 base::Unretained(this)),
1055 net_log_);
[email protected]daddea62012-09-19 05:51:131056 return rv;
1057}
[email protected]4875ba12011-03-30 22:31:511058
[email protected]daddea62012-09-19 05:51:131059int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1060 if (result == OK)
1061 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131062 return result;
1063}
1064
1065int HttpNetworkTransaction::DoBuildRequest() {
1066 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511067 headers_valid_ = false;
1068
1069 // This is constructed lazily (instead of within our Start method), so that
1070 // we have proxy info available.
1071 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241072 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141073 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511074 }
1075
[email protected]4875ba12011-03-30 22:31:511076 return OK;
1077}
1078
1079int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241080 if (result == OK)
1081 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511082 return result;
1083}
1084
[email protected]0877e3d2009-10-17 22:29:571085int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251086 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571087 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1088
[email protected]bf3eb002012-11-15 05:50:111089 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521090}
1091
[email protected]0877e3d2009-10-17 22:29:571092int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251093 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361094
1095 if (result == ERR_HTTP_1_1_REQUIRED ||
1096 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1097 return HandleHttp11Required(result);
1098 }
1099
initial.commit586acc5fe2008-07-26 22:42:521100 if (result < 0)
1101 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571102 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521103 return OK;
1104}
1105
1106int HttpNetworkTransaction::DoReadHeaders() {
1107 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411108 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521109}
1110
1111int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111112 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1113 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381114 if (IsCertificateError(result)) {
1115 // We don't handle a certificate error during SSL renegotiation, so we
1116 // have to return an error that's not in the certificate error range
1117 // (-2xx).
1118 LOG(ERROR) << "Got a server certificate with error " << result
1119 << " during SSL renegotiation";
1120 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1121 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1122 // TODO(wtc): Need a test case for this code path!
1123 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251124 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381125 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501126 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381127 result = HandleCertificateRequest(result);
1128 if (result == OK)
1129 return result;
[email protected]2181ea002009-06-09 01:37:271130 }
1131
bncfacdd852015-01-09 19:22:541132 if (result == ERR_HTTP_1_1_REQUIRED ||
1133 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1134 return HandleHttp11Required(result);
1135 }
1136
[email protected]c871864d72014-03-13 19:56:191137 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1138 // response headers were received, we do the best we can to make sense of it
1139 // and send it back up the stack.
1140 //
1141 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1142 // bizarre for SPDY. Assuming this logic is useful at all.
1143 // TODO(davidben): Bubble the error code up so we do not cache?
1144 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1145 result = OK;
1146
1147 if (result < 0)
1148 return HandleIOError(result);
1149
[email protected]90499482013-06-01 00:39:501150 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521151
eustasc7d27da2017-04-06 10:33:201152 if (response_.headers.get() && !ContentEncodingsValid())
1153 return ERR_CONTENT_DECODING_FAILED;
1154
[email protected]d58ceea82014-06-04 10:55:541155 // On a 408 response from the server ("Request Timeout") on a stale socket,
1156 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041157 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181158 if (response_.headers.get() &&
1159 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541160 stream_->IsConnectionReused()) {
Lily Chenfec60d92019-01-24 01:16:421161#if BUILDFLAG(ENABLE_REPORTING)
1162 GenerateNetworkErrorLoggingReport(OK);
1163#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]d58ceea82014-06-04 10:55:541164 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001165 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541166 response_.headers->response_code());
1167 // This will close the socket - it would be weird to try and reuse it, even
1168 // if the server doesn't actually close it.
1169 ResetConnectionAndRequestForResend();
1170 return OK;
1171 }
1172
[email protected]93f8b562012-03-27 01:00:161173 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211174 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161175 const int response_code = response_.headers->response_code();
1176 UMA_HISTOGRAM_ENUMERATION(
1177 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1178 }
1179
[email protected]3abacd62012-06-10 20:20:321180 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001181 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321182 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521183 if (response_headers_callback_)
1184 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391185
bncbe0f6af2015-10-15 17:49:561186 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571187 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1188 // indicates a buggy server. See:
1189 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1190 if (request_->method == "PUT")
1191 return ERR_METHOD_NOT_SUPPORTED;
1192 }
[email protected]4ddaf2502008-10-23 18:26:191193
Steven Valdez58097ec32018-07-16 18:29:041194 if (can_send_early_data_ && response_.headers.get() &&
1195 response_.headers->response_code() == HTTP_TOO_EARLY) {
1196 return HandleIOError(ERR_EARLY_DATA_REJECTED);
1197 }
1198
[email protected]0877e3d2009-10-17 22:29:571199 // Check for an intermediate 100 Continue response. An origin server is
1200 // allowed to send this response even if we didn't ask for it, so we just
1201 // need to skip over it.
1202 // We treat any other 1xx in this same way (although in practice getting
1203 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441204 // Unless this is a WebSocket request, in which case we pass it on up.
1205 if (response_.headers->response_code() / 100 == 1 &&
1206 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451207 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571208 next_state_ = STATE_READ_HEADERS;
1209 return OK;
1210 }
1211
davidbence688ae2017-05-04 15:12:591212 if (response_.headers->response_code() == 421 &&
1213 (enable_ip_based_pooling_ || enable_alternative_services_)) {
Lily Chenfec60d92019-01-24 01:16:421214#if BUILDFLAG(ENABLE_REPORTING)
1215 GenerateNetworkErrorLoggingReport(OK);
1216#endif // BUILDFLAG(ENABLE_REPORTING)
davidbence688ae2017-05-04 15:12:591217 // Retry the request with both IP based pooling and Alternative Services
1218 // disabled.
1219 enable_ip_based_pooling_ = false;
1220 enable_alternative_services_ = false;
1221 net_log_.AddEvent(
1222 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1223 ResetConnectionAndRequestForResend();
1224 return OK;
bnc8016c1f2017-03-31 02:11:291225 }
1226
bncb26024382016-06-29 02:39:451227 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491228 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081229 if (response_.ssl_info.is_valid() &&
1230 !IsCertStatusError(response_.ssl_info.cert_status)) {
1231 session_->http_stream_factory()->ProcessAlternativeServices(
1232 session_, response_.headers.get(),
1233 url::SchemeHostPort(request_->url));
1234 }
1235 }
asanka5ffd5d72016-03-23 16:20:491236
[email protected]e772db3f2010-07-12 18:11:131237 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571238 if (rv != OK)
1239 return rv;
1240
Douglas Creager3cb042052018-11-06 23:08:521241#if BUILDFLAG(ENABLE_REPORTING)
Lily Chenfec60d92019-01-24 01:16:421242 // Note: Unless there is a pre-existing NEL policy for this origin, any NEL
1243 // reports generated before the NEL header is processed here will just be
1244 // dropped by the NetworkErrorLoggingService.
Douglas Creager134b52e2018-11-09 18:00:141245 ProcessReportToHeader();
Douglas Creager3cb042052018-11-06 23:08:521246 ProcessNetworkErrorLoggingHeader();
Lily Chenfec60d92019-01-24 01:16:421247
1248 // Generate NEL report here if we have to report an HTTP error (4xx or 5xx
Lily Chend3930e72019-03-01 19:31:111249 // code), or if the response body will not be read, or on a redirect.
1250 // Note: This will report a success for a redirect even if an error is
1251 // encountered later while draining the body.
Lily Chenfec60d92019-01-24 01:16:421252 int response_code = response_.headers->response_code();
1253 if ((response_code >= 400 && response_code < 600) ||
1254 response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT ||
1255 response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" ||
Lily Chend3930e72019-03-01 19:31:111256 response_.headers->GetContentLength() == 0 ||
1257 response_.headers->IsRedirect(nullptr /* location */)) {
Lily Chenfec60d92019-01-24 01:16:421258 GenerateNetworkErrorLoggingReport(OK);
1259 }
1260#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521261
[email protected]0877e3d2009-10-17 22:29:571262 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261263
1264 // We have reached the end of Start state machine, set the RequestInfo to
1265 // null.
1266 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1267 // only until the final response headers are received. Clearing it will ensure
1268 // that HttpRequestInfo is only used up until final response headers are
1269 // received. Clearing is allowed so that the transaction can be disassociated
1270 // from its creating consumer in cases where it is shared for writing to the
1271 // cache. It is also safe to set it to null at this point since
1272 // upload_data_stream is also not used in the Read state machine.
1273 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1274 request_ = nullptr;
1275
[email protected]0877e3d2009-10-17 22:29:571276 return OK;
initial.commit586acc5fe2008-07-26 22:42:521277}
1278
1279int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501280 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131281 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381282 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521283
1284 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501285 return stream_->ReadResponseBody(
1286 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521287}
1288
1289int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1290 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381291 bool done = false;
1292 if (result <= 0) {
1293 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521294 done = true;
[email protected]8e6441ca2010-08-19 05:56:381295 }
[email protected]9492e4a2010-02-24 00:58:461296
mmenkebd84c392015-09-02 14:12:341297 // Clean up connection if we are done.
1298 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381299 // Note: Just because IsResponseBodyComplete is true, we're not
1300 // necessarily "done". We're only "done" when it is the last
1301 // read on this HttpNetworkTransaction, which will be signified
1302 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341303 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381304 // the stream. No need to compute it here just to pass back
1305 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341306 bool keep_alive =
1307 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521308
[email protected]8e6441ca2010-08-19 05:56:381309 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011310 // Note: we don't reset the stream here. We've closed it, but we still
1311 // need it around so that callers can call methods such as
1312 // GetUploadProgress() and have them be meaningful.
1313 // TODO(mbelshe): This means we closed the stream here, and we close it
1314 // again in ~HttpNetworkTransaction. Clean that up.
1315
[email protected]8e6441ca2010-08-19 05:56:381316 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171317
1318 // This transaction was successful. If it had been retried because of an
1319 // error with an alternative service, mark that alternative service broken.
1320 if (!enable_alternative_services_ &&
1321 retried_alternative_service_.protocol != kProtoUnknown) {
1322 session_->http_server_properties()->MarkAlternativeServiceBroken(
1323 retried_alternative_service_);
1324 }
Lily Chenfec60d92019-01-24 01:16:421325
1326#if BUILDFLAG(ENABLE_REPORTING)
1327 GenerateNetworkErrorLoggingReport(result);
1328#endif // BUILDFLAG(ENABLE_REPORTING)
initial.commit586acc5fe2008-07-26 22:42:521329 }
1330
1331 // Clear these to avoid leaving around old state.
1332 read_buf_ = NULL;
1333 read_buf_len_ = 0;
1334
1335 return result;
1336}
1337
[email protected]2d2697f92009-02-18 21:00:321338int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1339 // This method differs from DoReadBody only in the next_state_. So we just
1340 // call DoReadBody and override the next_state_. Perhaps there is a more
1341 // elegant way for these two methods to share code.
1342 int rv = DoReadBody();
1343 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1344 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1345 return rv;
1346}
1347
[email protected]0877e3d2009-10-17 22:29:571348// TODO(wtc): This method and the DoReadBodyComplete method are almost
1349// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321350int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231351 // keep_alive defaults to true because the very reason we're draining the
1352 // response body is to reuse the connection for auth restart.
1353 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321354 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571355 // Error or closed connection while reading the socket.
Lily Chenfec60d92019-01-24 01:16:421356 // Note: No Network Error Logging report is generated here because a report
1357 // will have already been generated for the original request due to the auth
1358 // challenge, so a second report is not generated for the same request here.
[email protected]2d2697f92009-02-18 21:00:321359 done = true;
[email protected]68873ba2009-06-04 21:49:231360 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311361 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571362 done = true;
[email protected]2d2697f92009-02-18 21:00:321363 }
1364
1365 if (done) {
1366 DidDrainBodyForAuthRestart(keep_alive);
1367 } else {
1368 // Keep draining.
1369 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1370 }
1371
1372 return OK;
1373}
1374
Douglas Creager3cb042052018-11-06 23:08:521375#if BUILDFLAG(ENABLE_REPORTING)
Douglas Creager134b52e2018-11-09 18:00:141376void HttpNetworkTransaction::ProcessReportToHeader() {
1377 std::string value;
1378 if (!response_.headers->GetNormalizedHeader("Report-To", &value))
1379 return;
1380
1381 ReportingService* service = session_->reporting_service();
1382 if (!service) {
1383 ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService();
1384 return;
1385 }
1386
1387 // Only accept Report-To headers on HTTPS connections that have no
1388 // certificate errors.
1389 if (!response_.ssl_info.is_valid()) {
1390 ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo();
1391 return;
1392 }
1393 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1394 ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError();
1395 return;
1396 }
1397
1398 service->ProcessHeader(url_.GetOrigin(), value);
1399}
1400
Douglas Creager3cb042052018-11-06 23:08:521401void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() {
1402 std::string value;
1403 if (!response_.headers->GetNormalizedHeader(
1404 NetworkErrorLoggingService::kHeaderName, &value)) {
1405 return;
1406 }
1407
1408 NetworkErrorLoggingService* service =
1409 session_->network_error_logging_service();
1410 if (!service) {
1411 NetworkErrorLoggingService::
1412 RecordHeaderDiscardedForNoNetworkErrorLoggingService();
1413 return;
1414 }
1415
Lily Chen90ae93cc2019-02-14 01:15:391416 // Don't accept NEL headers received via a proxy, because the IP address of
1417 // the destination server is not known.
1418 if (response_.was_fetched_via_proxy)
1419 return;
1420
Douglas Creager3cb042052018-11-06 23:08:521421 // Only accept NEL headers on HTTPS connections that have no certificate
1422 // errors.
1423 if (!response_.ssl_info.is_valid()) {
1424 NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo();
1425 return;
1426 }
1427 if (IsCertStatusError(response_.ssl_info.cert_status)) {
1428 NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError();
1429 return;
1430 }
1431
1432 if (remote_endpoint_.address().empty()) {
1433 NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint();
1434 return;
1435 }
1436
1437 service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(),
1438 value);
1439}
Douglas Creageref5eecdc2018-11-09 20:50:361440
Lily Chenfec60d92019-01-24 01:16:421441void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) {
1442 if (rv < 0 && rv != ERR_IO_PENDING)
1443 GenerateNetworkErrorLoggingReport(rv);
1444}
1445
Douglas Creageref5eecdc2018-11-09 20:50:361446void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) {
Lily Chenfec60d92019-01-24 01:16:421447 // |rv| should be a valid net::Error
1448 DCHECK_NE(rv, ERR_IO_PENDING);
1449 DCHECK_LE(rv, 0);
1450
1451 if (network_error_logging_report_generated_)
1452 return;
1453 network_error_logging_report_generated_ = true;
1454
Douglas Creageref5eecdc2018-11-09 20:50:361455 NetworkErrorLoggingService* service =
1456 session_->network_error_logging_service();
1457 if (!service) {
1458 NetworkErrorLoggingService::
1459 RecordRequestDiscardedForNoNetworkErrorLoggingService();
1460 return;
1461 }
1462
Lily Chen90ae93cc2019-02-14 01:15:391463 // Don't report on proxy auth challenges.
1464 if (response_.headers && response_.headers->response_code() ==
1465 HTTP_PROXY_AUTHENTICATION_REQUIRED) {
1466 return;
1467 }
1468
1469 // Don't generate NEL reports if we are behind a proxy, to avoid leaking
1470 // internal network details.
1471 if (response_.was_fetched_via_proxy)
1472 return;
1473
Lily Chen00196ab62018-12-04 19:52:291474 // Ignore errors from non-HTTPS origins.
1475 if (!url_.SchemeIsCryptographic()) {
1476 NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin();
1477 return;
1478 }
Douglas Creageref5eecdc2018-11-09 20:50:361479
1480 NetworkErrorLoggingService::RequestDetails details;
1481
1482 details.uri = url_;
1483 if (!request_referrer_.empty())
1484 details.referrer = GURL(request_referrer_);
1485 details.user_agent = request_user_agent_;
Lily Chenfec60d92019-01-24 01:16:421486 if (!remote_endpoint_.address().empty()) {
Douglas Creageref5eecdc2018-11-09 20:50:361487 details.server_ip = remote_endpoint_.address();
Lily Chenfec60d92019-01-24 01:16:421488 } else {
1489 details.server_ip = IPAddress();
1490 }
Douglas Creageref5eecdc2018-11-09 20:50:361491 // HttpResponseHeaders::response_code() returns 0 if response code couldn't
1492 // be parsed, which is also how NEL represents the same.
Lily Chenfec60d92019-01-24 01:16:421493 if (response_.headers) {
Douglas Creageref5eecdc2018-11-09 20:50:361494 details.status_code = response_.headers->response_code();
Lily Chenfec60d92019-01-24 01:16:421495 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361496 details.status_code = 0;
Lily Chenfec60d92019-01-24 01:16:421497 }
Douglas Creageref5eecdc2018-11-09 20:50:361498 // If we got response headers, assume that the connection used HTTP/1.1
1499 // unless ALPN negotiation tells us otherwise (handled below).
Lily Chenfec60d92019-01-24 01:16:421500 if (response_.was_alpn_negotiated) {
Douglas Creageref5eecdc2018-11-09 20:50:361501 details.protocol = response_.alpn_negotiated_protocol;
Lily Chenfec60d92019-01-24 01:16:421502 } else {
Douglas Creageref5eecdc2018-11-09 20:50:361503 details.protocol = "http/1.1";
Douglas Creageref5eecdc2018-11-09 20:50:361504 }
Lily Chenfec60d92019-01-24 01:16:421505 details.method = request_method_;
1506 details.elapsed_time = base::TimeTicks::Now() - start_timeticks_;
Douglas Creageref5eecdc2018-11-09 20:50:361507 details.type = static_cast<Error>(rv);
1508 details.reporting_upload_depth = request_reporting_upload_depth_;
1509
1510 service->OnRequest(std::move(details));
1511}
Lily Chenfec60d92019-01-24 01:16:421512#endif // BUILDFLAG(ENABLE_REPORTING)
Douglas Creager3cb042052018-11-06 23:08:521513
[email protected]5e363962009-06-19 19:57:011514int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381515 // There are two paths through which the server can request a certificate
1516 // from us. The first is during the initial handshake, the second is
1517 // during SSL renegotiation.
1518 //
1519 // In both cases, we want to close the connection before proceeding.
1520 // We do this for two reasons:
1521 // First, we don't want to keep the connection to the server hung for a
1522 // long time while the user selects a certificate.
1523 // Second, even if we did keep the connection open, NSS has a bug where
1524 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581525 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381526
1527 if (stream_.get()) {
1528 // Since we already have a stream, we're being called as part of SSL
1529 // renegotiation.
1530 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191531 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221532 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381533 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121534 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381535 }
1536
[email protected]26816882010-10-14 18:03:091537 // The server is asking for a client certificate during the initial
1538 // handshake.
1539 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011540
[email protected]ec229bc92010-11-22 09:51:451541 // If the user selected one of the certificates in client_certs or declined
1542 // to provide one for this server before, use the past decision
1543 // automatically.
1544 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541545 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451546 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541547 response_.cert_request_info->host_and_port, &client_cert,
1548 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451549 if (!found_cached_cert)
1550 return error;
1551
1552 // Check that the certificate selected is still a certificate the server
1553 // is likely to accept, based on the criteria supplied in the
1554 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501555 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261556 const std::vector<std::string>& cert_authorities =
1557 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451558
[email protected]f1958c382013-02-07 00:15:261559 bool cert_still_valid = cert_authorities.empty() ||
1560 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451561 if (!cert_still_valid)
1562 return error;
[email protected]5e363962009-06-19 19:57:011563 }
[email protected]ec229bc92010-11-22 09:51:451564
David Benjamin76a40ad2018-02-24 22:22:081565 if (!response_.cert_request_info->is_proxy) {
1566 server_ssl_client_cert_was_cached_ = true;
1567 }
1568
[email protected]ec229bc92010-11-22 09:51:451569 // TODO(davidben): Add a unit test which covers this path; we need to be
1570 // able to send a legitimate certificate and also bypass/clear the
1571 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141572 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1573 &proxy_ssl_config_ : &server_ssl_config_;
1574 ssl_config->send_client_cert = true;
1575 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541576 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451577 next_state_ = STATE_CREATE_STREAM;
1578 // Reset the other member variables.
1579 // Note: this is necessary only with SSL renegotiation.
1580 ResetStateForRestart();
1581 return OK;
[email protected]0b45559b2009-06-12 21:45:111582}
1583
bncfacdd852015-01-09 19:22:541584int HttpNetworkTransaction::HandleHttp11Required(int error) {
1585 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1586 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1587
1588 if (error == ERR_HTTP_1_1_REQUIRED) {
1589 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1590 } else {
1591 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1592 }
1593 ResetConnectionAndRequestForResend();
1594 return OK;
1595}
1596
David Benjamin76a40ad2018-02-24 22:22:081597int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
1598 // TODO(davidben): This does handle client certificate errors from the
1599 // proxy. https://crbug.com/814911.
[email protected]384cc73a2013-12-08 22:41:031600 if (server_ssl_config_.send_client_cert &&
1601 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1602 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411603 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031604
David Benjamin76a40ad2018-02-24 22:22:081605 // The private key handle may have gone stale due to, e.g., the user
1606 // unplugging their smartcard. Operating systems do not provide reliable
1607 // notifications for this, so if the signature failed and the private key
1608 // came from SSLClientAuthCache, retry to ask the user for a new one.
1609 if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1610 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1611 server_ssl_client_cert_was_cached_ = false;
1612 server_ssl_config_.send_client_cert = false;
1613 server_ssl_config_.client_cert = nullptr;
1614 server_ssl_config_.client_private_key = nullptr;
1615 retry_attempts_++;
1616 net_log_.AddEventWithNetErrorCode(
1617 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1618 ResetConnectionAndRequestForResend();
1619 return OK;
1620 }
1621 }
initial.commit586acc5fe2008-07-26 22:42:521622 return error;
1623}
1624
[email protected]bd0b6772011-01-11 19:59:301625// This method determines whether it is safe to resend the request after an
1626// IO error. It can only be called in response to request header or body
1627// write errors or response header read errors. It should not be used in
1628// other cases, such as a Connect error.
1629int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031630 // Because the peer may request renegotiation with client authentication at
1631 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081632 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301633
Lily Chenfec60d92019-01-24 01:16:421634#if BUILDFLAG(ENABLE_REPORTING)
1635 GenerateNetworkErrorLoggingReportIfError(error);
1636#endif // BUILDFLAG(ENABLE_REPORTING)
1637
[email protected]bd0b6772011-01-11 19:59:301638 switch (error) {
1639 // If we try to reuse a connection that the server is in the process of
1640 // closing, we may end up successfully writing out our request (or a
1641 // portion of our request) only to find a connection error when we try to
1642 // read from (or finish writing to) the socket.
1643 case ERR_CONNECTION_RESET:
1644 case ERR_CONNECTION_CLOSED:
1645 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511646 // There can be a race between the socket pool checking checking whether a
1647 // socket is still connected, receiving the FIN, and sending/reading data
1648 // on a reused socket. If we receive the FIN between the connectedness
1649 // check and writing/reading from the socket, we may first learn the socket
1650 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1651 // likely happen when trying to retrieve its IP address.
1652 // See http://crbug.com/105824 for more details.
1653 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491654 // If a socket is closed on its initial request, HttpStreamParser returns
1655 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1656 // preconnected but failed to be used before the server timed it out.
1657 case ERR_EMPTY_RESPONSE:
1658 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381659 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001660 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301661 ResetConnectionAndRequestForResend();
1662 error = OK;
1663 }
1664 break;
Steven Valdez58097ec32018-07-16 18:29:041665 case ERR_EARLY_DATA_REJECTED:
1666 case ERR_WRONG_VERSION_ON_EARLY_DATA:
1667 net_log_.AddEventWithNetErrorCode(
1668 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1669 // Disable early data on the SSLConfig on a reset.
1670 can_send_early_data_ = false;
1671 ResetConnectionAndRequestForResend();
1672 error = OK;
1673 break;
[email protected]8753a122011-10-16 08:05:081674 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001675 case ERR_SPDY_SERVER_REFUSED_STREAM:
Bence Béky1a5d8562018-01-05 17:29:281676 case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE:
Yoav Weiss9693572f2018-01-04 09:37:341677 case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
Bence Béky49db0e22018-05-11 00:54:051678 case ERR_SPDY_PUSHED_RESPONSE_DOES_NOT_MATCH:
[email protected]4d283b32013-10-17 12:57:271679 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141680 if (HasExceededMaxRetries())
1681 break;
[email protected]b6754252012-06-13 23:14:381682 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001683 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141684 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001685 ResetConnectionAndRequestForResend();
1686 error = OK;
1687 break;
rch2f2991c2017-04-13 19:28:171688 case ERR_QUIC_PROTOCOL_ERROR:
1689 if (GetResponseHeaders() != nullptr ||
1690 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1691 // If the response headers have already been recieved and passed up
1692 // then the request can not be retried. Also, if there was no
1693 // alternative service used for this request, then there is no
1694 // alternative service to be disabled.
1695 break;
1696 }
Biljith Jayan45a41722017-08-16 18:43:141697 if (HasExceededMaxRetries())
1698 break;
rch2f2991c2017-04-13 19:28:171699 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1700 retried_alternative_service_)) {
1701 // If the alternative service was marked as broken while the request
1702 // was in flight, retry the request which will not use the broken
1703 // alternative service.
1704 net_log_.AddEventWithNetErrorCode(
1705 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141706 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171707 ResetConnectionAndRequestForResend();
1708 error = OK;
1709 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1710 // Disable alternative services for this request and retry it. If the
1711 // retry succeeds, then the alternative service will be marked as
1712 // broken then.
1713 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361714 net_log_.AddEventWithNetErrorCode(
1715 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141716 retry_attempts_++;
rch514a44a82017-04-13 04:46:361717 ResetConnectionAndRequestForResend();
1718 error = OK;
1719 }
1720 break;
[email protected]bd0b6772011-01-11 19:59:301721 }
1722 return error;
1723}
1724
[email protected]c3b35c22008-09-27 03:19:421725void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581726 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221727 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191728 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221729 total_sent_bytes_ += stream_->GetTotalSentBytes();
1730 }
zhongyica364fbb2015-12-12 03:39:121731 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581732}
1733
1734void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251735 send_start_time_ = base::TimeTicks();
1736 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251737
[email protected]0757e7702009-03-27 04:00:221738 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421739 read_buf_ = NULL;
1740 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571741 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201742 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141743 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381744 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591745 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121746 net_error_details_.quic_broken = false;
Ryan Hamilton8d9ee76e2018-05-29 23:52:521747 net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR;
Lily Chenfec60d92019-01-24 01:16:421748#if BUILDFLAG(ENABLE_REPORTING)
1749 network_error_logging_report_generated_ = false;
1750 start_timeticks_ = base::TimeTicks::Now();
1751#endif // BUILDFLAG(ENABLE_REPORTING)
zhongyica364fbb2015-12-12 03:39:121752}
1753
1754void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1755 if (stream_)
1756 stream_->PopulateNetErrorDetails(&net_error_details_);
1757 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571758}
1759
[email protected]0877e3d2009-10-17 22:29:571760HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501761 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421762}
1763
[email protected]a34f61ee2014-03-18 20:59:491764bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381765 bool connection_is_proven = stream_->IsConnectionReused();
1766 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161767
[email protected]2a5c76b2008-09-25 22:15:161768 // NOTE: we resend a request only if we reused a keep-alive connection.
1769 // This automatically prevents an infinite resend loop because we'll run
1770 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381771 if (connection_is_proven && !has_received_headers)
1772 return true;
1773 return false;
[email protected]1c773ea12009-04-28 19:58:421774}
1775
Biljith Jayan45a41722017-08-16 18:43:141776bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1777 return (retry_attempts_ >= kMaxRetryAttempts);
1778}
1779
David Benjamin83ddfb32018-03-30 01:07:521780bool HttpNetworkTransaction::CheckMaxRestarts() {
1781 num_restarts_++;
1782 return num_restarts_ < kMaxRestarts;
1783}
1784
[email protected]1c773ea12009-04-28 19:58:421785void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381786 if (stream_.get()) {
1787 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121788 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161789 }
1790
[email protected]0877e3d2009-10-17 22:29:571791 // We need to clear request_headers_ because it contains the real request
1792 // headers, but we may need to resend the CONNECT request first to recreate
1793 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201794 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501795 next_state_ = STATE_CREATE_STREAM; // Resend the request.
Lily Chenfec60d92019-01-24 01:16:421796
1797#if BUILDFLAG(ENABLE_REPORTING)
1798 // Reset for new request.
1799 network_error_logging_report_generated_ = false;
1800 start_timeticks_ = base::TimeTicks::Now();
1801#endif // BUILDFLAG(ENABLE_REPORTING)
[email protected]86ec30d2008-09-29 21:53:541802}
1803
[email protected]1c773ea12009-04-28 19:58:421804bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241805 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421806}
license.botbf09a502008-08-24 00:55:551807
[email protected]1c773ea12009-04-28 19:58:421808bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041809 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421810}
1811
[email protected]e772db3f2010-07-12 18:11:131812int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271813 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501814 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421815
[email protected]0877e3d2009-10-17 22:29:571816 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581817 if (status != HTTP_UNAUTHORIZED &&
1818 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421819 return OK;
[email protected]9094b602012-02-27 21:44:581820 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111821 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161822 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1823 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421824
[email protected]9094b602012-02-27 21:44:581825 // This case can trigger when an HTTPS server responds with a "Proxy
1826 // authentication required" status code through a non-authenticating
1827 // proxy.
[email protected]7a67a8152010-11-05 18:31:101828 if (!auth_controllers_[target].get())
1829 return ERR_UNEXPECTED_PROXY_AUTH;
1830
[email protected]a7ea8832010-07-12 17:54:541831 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491832 headers, response_.ssl_info,
1833 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311834 net_log_);
[email protected]228404f2010-06-24 04:31:411835 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491836 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411837
1838 scoped_refptr<AuthChallengeInfo> auth_info =
1839 auth_controllers_[target]->auth_info();
1840 if (auth_info.get())
1841 response_.auth_challenge = auth_info;
1842
[email protected]228404f2010-06-24 04:31:411843 return rv;
[email protected]f9ee6b52008-11-08 06:46:231844}
1845
[email protected]8e6441ca2010-08-19 05:56:381846bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1847 return auth_controllers_[target].get() &&
1848 auth_controllers_[target]->HaveAuth();
1849}
1850
[email protected]228404f2010-06-24 04:31:411851GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1852 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461853 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411854 if (!proxy_info_.proxy_server().is_valid() ||
1855 proxy_info_.proxy_server().is_direct()) {
1856 return GURL(); // There is no proxy server.
1857 }
[email protected]2df19bb2010-08-25 20:13:461858 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1859 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351860 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461861 }
[email protected]228404f2010-06-24 04:31:411862 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291863 if (ForWebSocketHandshake()) {
Adam Rice9bd428b0a2019-02-15 06:31:361864 return net::ChangeWebSocketSchemeToHttpScheme(request_->url);
[email protected]e69c1cd2014-07-29 07:42:291865 }
[email protected]228404f2010-06-24 04:31:411866 return request_->url;
1867 default:
1868 return GURL();
1869 }
[email protected]c3b35c22008-09-27 03:19:421870}
1871
[email protected]831e4a32013-11-14 02:14:441872bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141873 return websocket_handshake_stream_base_create_helper_ &&
1874 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441875}
1876
ttuttle1f2d7e92015-04-28 16:17:471877void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1878 DCHECK(stream_request_);
1879
1880 // Since the transaction can restart with auth credentials, it may create a
1881 // stream more than once. Accumulate all of the connection attempts across
1882 // those streams by appending them to the vector:
1883 for (const auto& attempt : stream_request_->connection_attempts())
1884 connection_attempts_.push_back(attempt);
1885}
1886
eustasc7d27da2017-04-06 10:33:201887bool HttpNetworkTransaction::ContentEncodingsValid() const {
1888 HttpResponseHeaders* headers = GetResponseHeaders();
1889 DCHECK(headers);
1890
1891 std::string accept_encoding;
1892 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1893 &accept_encoding);
1894 std::set<std::string> allowed_encodings;
1895 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1896 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1897 return false;
1898 }
1899
1900 std::string content_encoding;
1901 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1902 std::set<std::string> used_encodings;
1903 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1904 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1905 return false;
1906 }
1907
1908 // When "Accept-Encoding" is not specified, it is parsed as "*".
1909 // If "*" encoding is advertised, then any encoding should be "accepted".
1910 // This does not mean, that it will be successfully decoded.
1911 if (allowed_encodings.find("*") != allowed_encodings.end())
1912 return true;
1913
sky50576f32017-05-01 19:28:031914 bool result = true;
eustasc7d27da2017-04-06 10:33:201915 for (auto const& encoding : used_encodings) {
1916 SourceStream::SourceType source_type =
1917 FilterSourceStream::ParseEncodingType(encoding);
1918 // We don't reject encodings we are not aware. They just will not decode.
1919 if (source_type == SourceStream::TYPE_UNKNOWN)
1920 continue;
1921 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031922 FilterSourceStream::ReportContentDecodingFailed(
1923 SourceStream::TYPE_REJECTED);
1924 result = false;
1925 break;
eustasc7d27da2017-04-06 10:33:201926 }
1927 }
sky50576f32017-05-01 19:28:031928
1929 // Temporary workaround for http://crbug.com/714514
1930 if (headers->IsRedirect(nullptr)) {
1931 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1932 !result);
1933 return true;
1934 }
1935
1936 return result;
eustasc7d27da2017-04-06 10:33:201937}
1938
Eric Roman74103c72019-02-21 00:23:121939static HttpNetworkTransaction::TunnelRedirectHistogramValue
1940GetTunnelRedirectHistogramValue(bool is_main_frame, bool was_auto_detected) {
1941 if (!is_main_frame && !was_auto_detected)
1942 return HttpNetworkTransaction::kSubresourceByExplicitProxy;
1943 if (is_main_frame && !was_auto_detected)
1944 return HttpNetworkTransaction::kMainFrameByExplicitProxy;
1945 if (!is_main_frame && was_auto_detected)
1946 return HttpNetworkTransaction::kSubresourceByAutoDetectedProxy;
1947 return HttpNetworkTransaction::kMainFrameByAutoDetectedProxy;
1948}
1949
1950// TODO(https://crbug.com/928551): Support for redirect on CONNECT is
1951// deprecated, and support will be removed.
1952//
1953// The code in this method handles the temporary histogramming and
1954// compatibility-mode policy during the phase-out.
1955int HttpNetworkTransaction::DoCreateStreamCompletedTunnelResponseRedirect() {
1956 bool is_main_frame = (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) ==
1957 LOAD_MAIN_FRAME_DEPRECATED;
1958 bool was_auto_detected = proxy_info_.did_use_auto_detected_pac_script();
1959
1960 UMA_HISTOGRAM_ENUMERATION(
1961 "Net.Proxy.RedirectDuringConnect",
1962 GetTunnelRedirectHistogramValue(is_main_frame, was_auto_detected));
1963
1964 // For legacy compatibility, the proxy is allowed to redirect CONNECT
1965 // if:
1966 // (a) the request was for a top-level frame
1967 // (b) the proxy server was explicitly configured (i.e. not
1968 // auto-detected).
1969 if (is_main_frame && !was_auto_detected) {
1970 // Return OK and let the caller read the proxy's error page
1971 next_state_ = STATE_NONE;
1972 return OK;
1973 }
1974
1975 // Otherwise let the request fail.
1976 stream_.reset();
1977 return ERR_HTTPS_PROXY_TUNNEL_RESPONSE_REDIRECT;
1978}
1979
[email protected]c3b35c22008-09-27 03:19:421980} // namespace net