[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d102f54 | 2010-06-30 14:51:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 7 | #include <set> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 11 | #include "base/base64url.h" |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 12 | #include "base/bind.h" |
| 13 | #include "base/bind_helpers.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 15 | #include "base/format_macros.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 16 | #include "base/metrics/field_trial.h" |
David Benjamin | 5cb9113 | 2018-04-06 05:54:49 | [diff] [blame] | 17 | #include "base/metrics/histogram_functions.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 18 | #include "base/metrics/histogram_macros.h" |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 19 | #include "base/metrics/sparse_histogram.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 20 | #include "base/stl_util.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
| 22 | #include "base/strings/string_util.h" |
[email protected] | 3d498f7 | 2013-10-28 21:17:40 | [diff] [blame] | 23 | #include "base/time/time.h" |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 24 | #include "base/values.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 26 | #include "net/base/auth.h" |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 27 | #include "net/base/host_port_pair.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 28 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 29 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 30 | #include "net/base/load_timing_info.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 31 | #include "net/base/net_errors.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 32 | #include "net/base/proxy_server.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/base/upload_data_stream.h" |
tfarina | 7a4a7fd | 2016-01-20 14:23:44 | [diff] [blame] | 34 | #include "net/base/url_util.h" |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 35 | #include "net/cert/cert_status_flags.h" |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 36 | #include "net/filter/filter_source_stream.h" |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 37 | #include "net/http/bidirectional_stream_impl.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 38 | #include "net/http/http_auth.h" |
| 39 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 40 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 41 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | #include "net/http/http_chunked_decoder.h" |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 43 | #include "net/http/http_log_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 44 | #include "net/http/http_network_session.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 45 | #include "net/http/http_proxy_client_socket.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 46 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 48 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 49 | #include "net/http/http_response_info.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 50 | #include "net/http/http_server_properties.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 51 | #include "net/http/http_status_code.h" |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 52 | #include "net/http/http_stream.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 53 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "net/http/http_util.h" |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 55 | #include "net/http/transport_security_state.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 56 | #include "net/http/url_security_manager.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 57 | #include "net/log/net_log_event_type.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 58 | #include "net/socket/client_socket_factory.h" |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 59 | #include "net/socket/next_proto.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 60 | #include "net/socket/transport_client_socket_pool.h" |
Bence Béky | 94658bf | 2018-05-11 19:22:58 | [diff] [blame] | 61 | #include "net/spdy/spdy_http_stream.h" |
| 62 | #include "net/spdy/spdy_session.h" |
| 63 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 64 | #include "net/ssl/ssl_cert_request_info.h" |
| 65 | #include "net/ssl/ssl_connection_status_flags.h" |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 66 | #include "net/ssl/ssl_info.h" |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 67 | #include "net/ssl/ssl_private_key.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 68 | #include "url/gurl.h" |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 69 | #include "url/url_canon.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 71 | #if BUILDFLAG(ENABLE_REPORTING) |
| 72 | #include "net/network_error_logging/network_error_logging_service.h" |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 73 | #include "net/reporting/reporting_header_parser.h" |
| 74 | #include "net/reporting/reporting_service.h" |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 75 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 76 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 77 | namespace net { |
| 78 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 79 | namespace { |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 80 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 81 | // Max number of |retry_attempts| (excluding the initial request) after which |
| 82 | // we give up and show an error page. |
| 83 | const size_t kMaxRetryAttempts = 2; |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 84 | |
| 85 | // Max number of calls to RestartWith* allowed for a single connection. A single |
| 86 | // HttpNetworkTransaction should not signal very many restartable errors, but it |
| 87 | // may occur due to a bug (e.g. https://crbug.com/823387 or |
| 88 | // https://crbug.com/488043) or simply if the server or proxy requests |
| 89 | // authentication repeatedly. Although these calls are often associated with a |
| 90 | // user prompt, in other scenarios (remembered preferences, extensions, |
| 91 | // multi-leg authentication), they may be triggered automatically. To avoid |
| 92 | // looping forever, bound the number of restarts. |
| 93 | const size_t kMaxRestarts = 32; |
| 94 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 95 | void SetProxyInfoInReponse(const ProxyInfo& proxy_info, |
| 96 | HttpResponseInfo* response_info) { |
| 97 | response_info->was_fetched_via_proxy = !proxy_info.is_direct(); |
| 98 | if (response_info->was_fetched_via_proxy && !proxy_info.is_empty()) |
| 99 | response_info->proxy_server = proxy_info.proxy_server(); |
| 100 | else if (!response_info->was_fetched_via_proxy && proxy_info.is_direct()) |
| 101 | response_info->proxy_server = ProxyServer::Direct(); |
| 102 | else |
| 103 | response_info->proxy_server = ProxyServer(); |
| 104 | } |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 105 | |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 106 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 107 | |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 108 | const int HttpNetworkTransaction::kDrainBodyBufferSize; |
| 109 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 110 | HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority, |
| 111 | HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 112 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
Bence Béky | 61f756c | 2018-04-25 14:17:53 | [diff] [blame] | 113 | io_callback_(base::BindRepeating(&HttpNetworkTransaction::OnIOComplete, |
| 114 | base::Unretained(this))), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 115 | session_(session), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 116 | request_(nullptr), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 117 | priority_(priority), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 118 | headers_valid_(false), |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 119 | can_send_early_data_(false), |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 120 | configured_client_cert_for_server_(false), |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 121 | request_headers_(), |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 122 | #if BUILDFLAG(ENABLE_REPORTING) |
| 123 | network_error_logging_report_generated_(false), |
| 124 | request_reporting_upload_depth_(0), |
| 125 | #endif // BUILDFLAG(ENABLE_REPORTING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 126 | read_buf_len_(0), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 127 | total_received_bytes_(0), |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 128 | total_sent_bytes_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 129 | next_state_(STATE_NONE), |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 130 | establishing_tunnel_(false), |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 131 | enable_ip_based_pooling_(true), |
bnc | accd496 | 2017-04-06 21:00:26 | [diff] [blame] | 132 | enable_alternative_services_(true), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 133 | websocket_handshake_stream_base_create_helper_(nullptr), |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 134 | net_error_details_(), |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 135 | retry_attempts_(0), |
David Benjamin | d61bd53 | 2019-04-23 21:11:37 | [diff] [blame] | 136 | num_restarts_(0) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 137 | } |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 138 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 139 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 140 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 141 | // If no error or success report has been generated yet at this point, then |
| 142 | // this network transaction was prematurely cancelled. |
| 143 | GenerateNetworkErrorLoggingReport(ERR_ABORTED); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 144 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 145 | if (stream_.get()) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 146 | // TODO(mbelshe): The stream_ should be able to compute whether or not the |
| 147 | // stream should be kept alive. No reason to compute here |
| 148 | // and pass it in. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 149 | if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 150 | stream_->Close(true /* not reusable */); |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 151 | } else if (stream_->IsResponseBodyComplete()) { |
| 152 | // If the response body is complete, we can just reuse the socket. |
| 153 | stream_->Close(false /* reusable */); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 154 | } else { |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 155 | // Otherwise, we try to drain the response body. |
| 156 | HttpStream* stream = stream_.release(); |
| 157 | stream->Drain(session_); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 158 | } |
| 159 | } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 160 | if (request_ && request_->upload_data_stream) |
| 161 | request_->upload_data_stream->Reset(); // Invalidate pending callbacks. |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 162 | } |
| 163 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 164 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 165 | CompletionOnceCallback callback, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 166 | const NetLogWithSource& net_log) { |
Batalov Vladislav | a4e97a50 | 2019-04-11 15:35:23 | [diff] [blame] | 167 | if (request_info->load_flags & LOAD_ONLY_FROM_CACHE) |
| 168 | return ERR_CACHE_MISS; |
| 169 | |
Ramin Halavati | b5e433e | 2018-02-07 07:41:10 | [diff] [blame] | 170 | DCHECK(request_info->traffic_annotation.is_valid()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 171 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 172 | request_ = request_info; |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 173 | url_ = request_->url; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 174 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 175 | // Store values for later use in NEL report generation. |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 176 | request_method_ = request_->method; |
| 177 | request_->extra_headers.GetHeader(HttpRequestHeaders::kReferer, |
| 178 | &request_referrer_); |
| 179 | request_->extra_headers.GetHeader(HttpRequestHeaders::kUserAgent, |
| 180 | &request_user_agent_); |
| 181 | request_reporting_upload_depth_ = request_->reporting_upload_depth; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 182 | start_timeticks_ = base::TimeTicks::Now(); |
| 183 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 184 | |
David Benjamin | 8119ce9e4 | 2019-08-06 00:05:06 | [diff] [blame] | 185 | session_->GetSSLConfig(&server_ssl_config_, &proxy_ssl_config_); |
nharper | 8cdb0fb | 2016-04-22 21:34:59 | [diff] [blame] | 186 | |
Ryan Sleevi | 24fe268 | 2018-08-16 21:33:46 | [diff] [blame] | 187 | if (request_->load_flags & LOAD_DISABLE_CERT_NETWORK_FETCHES) { |
| 188 | server_ssl_config_.disable_cert_verification_network_fetches = true; |
| 189 | proxy_ssl_config_.disable_cert_verification_network_fetches = true; |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 190 | } |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 191 | |
Steven Valdez | 03e872d | 2018-03-02 15:39:15 | [diff] [blame] | 192 | if (HttpUtil::IsMethodSafe(request_info->method)) { |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 193 | can_send_early_data_ = true; |
| 194 | } |
| 195 | |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 196 | if (request_->load_flags & LOAD_PREFETCH) |
| 197 | response_.unused_since_prefetch = true; |
| 198 | |
Helen Li | b495c380 | 2018-03-30 13:46:09 | [diff] [blame] | 199 | next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 200 | int rv = DoLoop(OK); |
| 201 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 202 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 203 | |
| 204 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 205 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 206 | // other net::Error can be returned. |
| 207 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 208 | return rv; |
| 209 | } |
| 210 | |
| 211 | int HttpNetworkTransaction::RestartIgnoringLastError( |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 212 | CompletionOnceCallback callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 213 | DCHECK(!stream_.get()); |
| 214 | DCHECK(!stream_request_.get()); |
| 215 | DCHECK_EQ(STATE_NONE, next_state_); |
| 216 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 217 | if (!CheckMaxRestarts()) |
| 218 | return ERR_TOO_MANY_RETRIES; |
| 219 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 220 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 221 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 222 | int rv = DoLoop(OK); |
| 223 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 224 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 225 | |
| 226 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 227 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 228 | // other net::Error can be returned. |
| 229 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 230 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 233 | int HttpNetworkTransaction::RestartWithCertificate( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 234 | scoped_refptr<X509Certificate> client_cert, |
| 235 | scoped_refptr<SSLPrivateKey> client_private_key, |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 236 | CompletionOnceCallback callback) { |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 237 | // When we receive ERR_SSL_CLIENT_AUTH_CERT_NEEDED, we always tear down |
| 238 | // existing streams and stream requests to force a new connection. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 239 | DCHECK(!stream_request_.get()); |
| 240 | DCHECK(!stream_.get()); |
| 241 | DCHECK_EQ(STATE_NONE, next_state_); |
| 242 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 243 | if (!CheckMaxRestarts()) |
| 244 | return ERR_TOO_MANY_RETRIES; |
| 245 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 246 | // Add the credentials to the client auth cache. The next stream request will |
| 247 | // then pick them up. |
| 248 | session_->ssl_client_context()->SetClientCertificate( |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 249 | response_.cert_request_info->host_and_port, std::move(client_cert), |
| 250 | std::move(client_private_key)); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 251 | |
| 252 | if (!response_.cert_request_info->is_proxy) |
| 253 | configured_client_cert_for_server_ = true; |
| 254 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 255 | // Reset the other member variables. |
| 256 | // Note: this is necessary only with SSL renegotiation. |
| 257 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 258 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 259 | int rv = DoLoop(OK); |
| 260 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 261 | callback_ = std::move(callback); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 262 | |
| 263 | // This always returns ERR_IO_PENDING because DoCreateStream() does, but |
| 264 | // GenerateNetworkErrorLoggingReportIfError() should be called here if any |
| 265 | // other net::Error can be returned. |
| 266 | DCHECK_EQ(rv, ERR_IO_PENDING); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 267 | return rv; |
| 268 | } |
| 269 | |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 270 | int HttpNetworkTransaction::RestartWithAuth(const AuthCredentials& credentials, |
| 271 | CompletionOnceCallback callback) { |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 272 | if (!CheckMaxRestarts()) |
| 273 | return ERR_TOO_MANY_RETRIES; |
| 274 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 275 | HttpAuth::Target target = pending_auth_target_; |
| 276 | if (target == HttpAuth::AUTH_NONE) { |
| 277 | NOTREACHED(); |
| 278 | return ERR_UNEXPECTED; |
| 279 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 280 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 281 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 282 | auth_controllers_[target]->ResetAuth(credentials); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 283 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 284 | DCHECK(callback_.is_null()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 285 | |
| 286 | int rv = OK; |
| 287 | if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) { |
| 288 | // In this case, we've gathered credentials for use with proxy |
| 289 | // authentication of a tunnel. |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 290 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 291 | DCHECK(stream_request_ != nullptr); |
| 292 | auth_controllers_[target] = nullptr; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 293 | ResetStateForRestart(); |
asanka | 681f02d | 2017-02-22 17:06:39 | [diff] [blame] | 294 | rv = stream_request_->RestartTunnelWithProxyAuth(); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 295 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 296 | // In this case, we've gathered credentials for the server or the proxy |
| 297 | // but it is not during the tunneling phase. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 298 | DCHECK(stream_request_ == nullptr); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 299 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 300 | rv = DoLoop(OK); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 301 | // Note: If an error is encountered while draining the old response body, no |
| 302 | // Network Error Logging report will be generated, because the error was |
| 303 | // with the old request, which will already have had a NEL report generated |
| 304 | // for it due to the auth challenge (so we don't report a second error for |
| 305 | // that request). |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 306 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 307 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 308 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 309 | callback_ = std::move(callback); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 310 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 313 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 314 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 315 | DCHECK(!stream_request_.get()); |
| 316 | |
Bence Béky | 3238f2e1 | 2017-09-22 22:44:49 | [diff] [blame] | 317 | // Authorization schemes incompatible with HTTP/2 are unsupported for proxies. |
| 318 | if (target == HttpAuth::AUTH_SERVER && |
| 319 | auth_controllers_[target]->NeedsHTTP11()) { |
| 320 | session_->http_server_properties()->SetHTTP11Required( |
| 321 | HostPortPair::FromURL(request_->url)); |
| 322 | } |
| 323 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 324 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 325 | // Even if the server says the connection is keep-alive, we have to be |
| 326 | // able to find the end of each response in order to reuse the connection. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 327 | if (stream_->CanReuseConnection()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 328 | // If the response body hasn't been completely read, we need to drain |
| 329 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 330 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 331 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
Victor Costan | 9c7302b | 2018-08-27 16:39:44 | [diff] [blame] | 332 | read_buf_ = base::MakeRefCounted<IOBuffer>( |
| 333 | kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 334 | read_buf_len_ = kDrainBodyBufferSize; |
| 335 | return; |
| 336 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 337 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 340 | // We don't need to drain the response body, so we act as if we had drained |
| 341 | // the response body. |
| 342 | DidDrainBodyForAuthRestart(keep_alive); |
| 343 | } |
| 344 | |
| 345 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 346 | DCHECK(!stream_request_.get()); |
| 347 | |
| 348 | if (stream_.get()) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 349 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 350 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 351 | HttpStream* new_stream = nullptr; |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 352 | if (keep_alive && stream_->CanReuseConnection()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 353 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 354 | // often enough to be worth the trouble. |
| 355 | stream_->SetConnectionReused(); |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 356 | new_stream = stream_->RenewStreamForAuth(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 357 | } |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 358 | |
| 359 | if (!new_stream) { |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 360 | // Close the stream and mark it as not_reusable. Even in the |
| 361 | // keep_alive case, we've determined that the stream_ is not |
| 362 | // reusable if new_stream is NULL. |
| 363 | stream_->Close(true); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 364 | next_state_ = STATE_CREATE_STREAM; |
| 365 | } else { |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 366 | // Renewed streams shouldn't carry over sent or received bytes. |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 367 | DCHECK_EQ(0, new_stream->GetTotalReceivedBytes()); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 368 | DCHECK_EQ(0, new_stream->GetTotalSentBytes()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 369 | next_state_ = STATE_INIT_STREAM; |
| 370 | } |
| 371 | stream_.reset(new_stream); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 372 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 373 | |
| 374 | // Reset the other member variables. |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 375 | ResetStateForAuthRestart(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 376 | } |
| 377 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 378 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 379 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 380 | HaveAuth(pending_auth_target_); |
| 381 | } |
| 382 | |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 383 | int HttpNetworkTransaction::Read(IOBuffer* buf, |
| 384 | int buf_len, |
| 385 | CompletionOnceCallback callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 386 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 387 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 388 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 389 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 390 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 391 | // We're trying to read the body of the response but we're still trying |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 392 | // to establish an SSL tunnel through an HTTP proxy. We can't read these |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 393 | // bytes when establishing a tunnel because they might be controlled by |
| 394 | // an active network attacker. We don't worry about this for HTTP |
| 395 | // because an active network attacker can already control HTTP sessions. |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 396 | // We reach this case when the user cancels a 407 proxy auth prompt. We |
| 397 | // also don't worry about this for an HTTPS Proxy, because the |
| 398 | // communication with the proxy is secure. |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 399 | // See http://crbug.com/8473. |
Yixin Wang | 46a273ec30 | 2018-01-23 17:59:56 | [diff] [blame] | 400 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https() || |
| 401 | proxy_info_.is_quic()); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 402 | DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED); |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 403 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 404 | } |
| 405 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 406 | // Are we using SPDY or HTTP? |
shivanisha | 0b44085 | 2016-10-18 15:48:15 | [diff] [blame] | 407 | next_state_ = STATE_READ_BODY; |
| 408 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 409 | read_buf_ = buf; |
| 410 | read_buf_len_ = buf_len; |
| 411 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 412 | int rv = DoLoop(OK); |
| 413 | if (rv == ERR_IO_PENDING) |
Bence Béky | 046f8c8 | 2018-06-12 02:26:04 | [diff] [blame] | 414 | callback_ = std::move(callback); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 415 | return rv; |
| 416 | } |
| 417 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 418 | void HttpNetworkTransaction::StopCaching() {} |
| 419 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 420 | bool HttpNetworkTransaction::GetFullRequestHeaders( |
| 421 | HttpRequestHeaders* headers) const { |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 422 | // TODO(juliatuttle): Make sure we've populated request_headers_. |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 423 | *headers = request_headers_; |
| 424 | return true; |
| 425 | } |
| 426 | |
sclittle | 4de1bab9 | 2015-09-22 21:28:24 | [diff] [blame] | 427 | int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const { |
| 428 | int64_t total_received_bytes = total_received_bytes_; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 429 | if (stream_) |
| 430 | total_received_bytes += stream_->GetTotalReceivedBytes(); |
| 431 | return total_received_bytes; |
| 432 | } |
| 433 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 434 | int64_t HttpNetworkTransaction::GetTotalSentBytes() const { |
| 435 | int64_t total_sent_bytes = total_sent_bytes_; |
| 436 | if (stream_) |
| 437 | total_sent_bytes += stream_->GetTotalSentBytes(); |
| 438 | return total_sent_bytes; |
| 439 | } |
| 440 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 441 | void HttpNetworkTransaction::DoneReading() {} |
| 442 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 443 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 444 | return &response_; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 448 | // TODO(wtc): Define a new LoadState value for the |
| 449 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 450 | switch (next_state_) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 451 | case STATE_CREATE_STREAM: |
| 452 | return LOAD_STATE_WAITING_FOR_DELEGATE; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 453 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 454 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 455 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 456 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 457 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 458 | return LOAD_STATE_SENDING_REQUEST; |
| 459 | case STATE_READ_HEADERS_COMPLETE: |
| 460 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 461 | case STATE_READ_BODY_COMPLETE: |
| 462 | return LOAD_STATE_READING_RESPONSE; |
| 463 | default: |
| 464 | return LOAD_STATE_IDLE; |
| 465 | } |
| 466 | } |
| 467 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 468 | void HttpNetworkTransaction::SetQuicServerInfo( |
| 469 | QuicServerInfo* quic_server_info) {} |
| 470 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 471 | bool HttpNetworkTransaction::GetLoadTimingInfo( |
| 472 | LoadTimingInfo* load_timing_info) const { |
| 473 | if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info)) |
| 474 | return false; |
| 475 | |
| 476 | load_timing_info->proxy_resolve_start = |
| 477 | proxy_info_.proxy_resolve_start_time(); |
| 478 | load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time(); |
| 479 | load_timing_info->send_start = send_start_time_; |
| 480 | load_timing_info->send_end = send_end_time_; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 481 | return true; |
| 482 | } |
| 483 | |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 484 | bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const { |
thestig | a74ad2b | 2016-07-11 20:52:36 | [diff] [blame] | 485 | if (remote_endpoint_.address().empty()) |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 486 | return false; |
| 487 | |
| 488 | *endpoint = remote_endpoint_; |
| 489 | return true; |
| 490 | } |
| 491 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 492 | void HttpNetworkTransaction::PopulateNetErrorDetails( |
| 493 | NetErrorDetails* details) const { |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 494 | *details = net_error_details_; |
| 495 | if (stream_) |
| 496 | stream_->PopulateNetErrorDetails(details); |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 499 | void HttpNetworkTransaction::SetPriority(RequestPriority priority) { |
| 500 | priority_ = priority; |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 501 | |
[email protected] | bf82898 | 2013-08-14 18:01:47 | [diff] [blame] | 502 | if (stream_request_) |
| 503 | stream_request_->SetPriority(priority); |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 504 | if (stream_) |
| 505 | stream_->SetPriority(priority); |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 506 | |
rdsmith | 1d343be5 | 2016-10-21 20:37:50 | [diff] [blame] | 507 | // The above call may have resulted in deleting |*this|. |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 510 | void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 511 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 512 | websocket_handshake_stream_base_create_helper_ = create_helper; |
| 513 | } |
| 514 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 515 | void HttpNetworkTransaction::SetBeforeNetworkStartCallback( |
| 516 | const BeforeNetworkStartCallback& callback) { |
| 517 | before_network_start_callback_ = callback; |
| 518 | } |
| 519 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 520 | void HttpNetworkTransaction::SetBeforeHeadersSentCallback( |
| 521 | const BeforeHeadersSentCallback& callback) { |
| 522 | before_headers_sent_callback_ = callback; |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 523 | } |
| 524 | |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 525 | void HttpNetworkTransaction::SetRequestHeadersCallback( |
| 526 | RequestHeadersCallback callback) { |
| 527 | DCHECK(!stream_); |
| 528 | request_headers_callback_ = std::move(callback); |
| 529 | } |
| 530 | |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 531 | void HttpNetworkTransaction::SetResponseHeadersCallback( |
| 532 | ResponseHeadersCallback callback) { |
| 533 | DCHECK(!stream_); |
| 534 | response_headers_callback_ = std::move(callback); |
| 535 | } |
| 536 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 537 | int HttpNetworkTransaction::ResumeNetworkStart() { |
| 538 | DCHECK_EQ(next_state_, STATE_CREATE_STREAM); |
| 539 | return DoLoop(OK); |
| 540 | } |
| 541 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 542 | void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config, |
| 543 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 544 | std::unique_ptr<HttpStream> stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 545 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 546 | DCHECK(stream_request_.get()); |
| 547 | |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 548 | if (stream_) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 549 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 550 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 551 | } |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 552 | stream_ = std::move(stream); |
Andrey Kosyakov | 83a6eee | 2017-08-14 19:20:04 | [diff] [blame] | 553 | stream_->SetRequestHeadersCallback(request_headers_callback_); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 554 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 555 | proxy_info_ = used_proxy_info; |
bnc | 94c9284 | 2016-09-21 15:22:52 | [diff] [blame] | 556 | response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated(); |
bnc | 3472afd | 2016-11-17 15:27:21 | [diff] [blame] | 557 | response_.alpn_negotiated_protocol = |
| 558 | NextProtoToString(stream_request_->negotiated_protocol()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 559 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 560 | SetProxyInfoInReponse(used_proxy_info, &response_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 561 | OnIOComplete(OK); |
| 562 | } |
| 563 | |
xunjieli | 5749218c | 2016-03-22 16:43:06 | [diff] [blame] | 564 | void HttpNetworkTransaction::OnBidirectionalStreamImplReady( |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 565 | const SSLConfig& used_ssl_config, |
| 566 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 567 | std::unique_ptr<BidirectionalStreamImpl> stream) { |
xunjieli | 11834f0 | 2015-12-22 04:27:08 | [diff] [blame] | 568 | NOTREACHED(); |
| 569 | } |
| 570 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 571 | void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady( |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 572 | const SSLConfig& used_ssl_config, |
| 573 | const ProxyInfo& used_proxy_info, |
bnc | 5029f463 | 2017-06-08 16:19:00 | [diff] [blame] | 574 | std::unique_ptr<WebSocketHandshakeStreamBase> stream) { |
| 575 | OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream)); |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 576 | } |
| 577 | |
Ryan Hamilton | 75f19726 | 2017-08-17 14:00:07 | [diff] [blame] | 578 | void HttpNetworkTransaction::OnStreamFailed( |
| 579 | int result, |
| 580 | const NetErrorDetails& net_error_details, |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 581 | const SSLConfig& used_ssl_config, |
| 582 | const ProxyInfo& used_proxy_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 583 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 584 | DCHECK_NE(OK, result); |
| 585 | DCHECK(stream_request_.get()); |
| 586 | DCHECK(!stream_.get()); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 587 | server_ssl_config_ = used_ssl_config; |
Ryan Hamilton | 75f19726 | 2017-08-17 14:00:07 | [diff] [blame] | 588 | net_error_details_ = net_error_details; |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 589 | proxy_info_ = used_proxy_info; |
| 590 | SetProxyInfoInReponse(used_proxy_info, &response_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 591 | |
| 592 | OnIOComplete(result); |
| 593 | } |
| 594 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 595 | void HttpNetworkTransaction::OnCertificateError( |
| 596 | int result, |
| 597 | const SSLConfig& used_ssl_config, |
| 598 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 599 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 600 | DCHECK_NE(OK, result); |
| 601 | DCHECK(stream_request_.get()); |
| 602 | DCHECK(!stream_.get()); |
| 603 | |
| 604 | response_.ssl_info = ssl_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 605 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 606 | |
| 607 | // TODO(mbelshe): For now, we're going to pass the error through, and that |
| 608 | // will close the stream_request in all cases. This means that we're always |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 609 | // going to restart an entire STATE_CREATE_STREAM, even if the connection is |
| 610 | // good and the user chooses to ignore the error. This is not ideal, but not |
| 611 | // the end of the world either. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 612 | |
| 613 | OnIOComplete(result); |
| 614 | } |
| 615 | |
| 616 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 617 | const HttpResponseInfo& proxy_response, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 618 | const SSLConfig& used_ssl_config, |
| 619 | const ProxyInfo& used_proxy_info, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 620 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 621 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 622 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 623 | |
| 624 | establishing_tunnel_ = true; |
| 625 | response_.headers = proxy_response.headers; |
| 626 | response_.auth_challenge = proxy_response.auth_challenge; |
Wojciech Dzierżanowski | 0950c37 | 2019-04-02 19:29:50 | [diff] [blame] | 627 | response_.did_use_http_auth = proxy_response.did_use_http_auth; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 628 | |
| 629 | if (response_.headers.get() && !ContentEncodingsValid()) { |
| 630 | DoCallback(ERR_CONTENT_DECODING_FAILED); |
| 631 | return; |
| 632 | } |
| 633 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 634 | headers_valid_ = true; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 635 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 636 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 637 | |
| 638 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 639 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 640 | |
| 641 | DoCallback(OK); |
| 642 | } |
| 643 | |
| 644 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 645 | const SSLConfig& used_ssl_config, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 646 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 647 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 648 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 649 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 650 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 651 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 652 | } |
| 653 | |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 654 | void HttpNetworkTransaction::OnQuicBroken() { |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 655 | net_error_details_.quic_broken = true; |
zhongyi | 48704c18 | 2015-12-07 07:52:02 | [diff] [blame] | 656 | } |
| 657 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 658 | void HttpNetworkTransaction::GetConnectionAttempts( |
| 659 | ConnectionAttempts* out) const { |
| 660 | *out = connection_attempts_; |
| 661 | } |
| 662 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 663 | bool HttpNetworkTransaction::IsSecureRequest() const { |
lgarron | a774b92 | 2015-05-14 22:56:37 | [diff] [blame] | 664 | return request_->url.SchemeIsCryptographic(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 665 | } |
| 666 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 667 | bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const { |
tbansal | 7cec381 | 2015-02-05 21:25:12 | [diff] [blame] | 668 | return (proxy_info_.is_http() || proxy_info_.is_https() || |
| 669 | proxy_info_.is_quic()) && |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 670 | !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS()); |
| 671 | } |
| 672 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 673 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 674 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 675 | DCHECK(!callback_.is_null()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 676 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 677 | #if BUILDFLAG(ENABLE_REPORTING) |
| 678 | // Just before invoking the caller's completion callback, generate a NEL |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 679 | // report about this network request if the result was an error. |
| 680 | GenerateNetworkErrorLoggingReportIfError(rv); |
| 681 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 682 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 683 | // Since Run may result in Read being called, clear user_callback_ up front. |
Daniel Cheng | cac5f4c6 | 2019-04-25 22:39:27 | [diff] [blame] | 684 | std::move(callback_).Run(rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 688 | int rv = DoLoop(result); |
| 689 | if (rv != ERR_IO_PENDING) |
| 690 | DoCallback(rv); |
| 691 | } |
| 692 | |
| 693 | int HttpNetworkTransaction::DoLoop(int result) { |
| 694 | DCHECK(next_state_ != STATE_NONE); |
| 695 | |
| 696 | int rv = result; |
| 697 | do { |
| 698 | State state = next_state_; |
| 699 | next_state_ = STATE_NONE; |
| 700 | switch (state) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 701 | case STATE_NOTIFY_BEFORE_CREATE_STREAM: |
| 702 | DCHECK_EQ(OK, rv); |
| 703 | rv = DoNotifyBeforeCreateStream(); |
| 704 | break; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 705 | case STATE_CREATE_STREAM: |
| 706 | DCHECK_EQ(OK, rv); |
| 707 | rv = DoCreateStream(); |
| 708 | break; |
| 709 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 710 | rv = DoCreateStreamComplete(rv); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 711 | break; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 712 | case STATE_INIT_STREAM: |
| 713 | DCHECK_EQ(OK, rv); |
| 714 | rv = DoInitStream(); |
| 715 | break; |
| 716 | case STATE_INIT_STREAM_COMPLETE: |
| 717 | rv = DoInitStreamComplete(rv); |
| 718 | break; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 719 | case STATE_GENERATE_PROXY_AUTH_TOKEN: |
| 720 | DCHECK_EQ(OK, rv); |
| 721 | rv = DoGenerateProxyAuthToken(); |
| 722 | break; |
| 723 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 724 | rv = DoGenerateProxyAuthTokenComplete(rv); |
| 725 | break; |
| 726 | case STATE_GENERATE_SERVER_AUTH_TOKEN: |
| 727 | DCHECK_EQ(OK, rv); |
| 728 | rv = DoGenerateServerAuthToken(); |
| 729 | break; |
| 730 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
| 731 | rv = DoGenerateServerAuthTokenComplete(rv); |
| 732 | break; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 733 | case STATE_INIT_REQUEST_BODY: |
| 734 | DCHECK_EQ(OK, rv); |
| 735 | rv = DoInitRequestBody(); |
| 736 | break; |
| 737 | case STATE_INIT_REQUEST_BODY_COMPLETE: |
| 738 | rv = DoInitRequestBodyComplete(rv); |
| 739 | break; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 740 | case STATE_BUILD_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 741 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 742 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 743 | rv = DoBuildRequest(); |
| 744 | break; |
| 745 | case STATE_BUILD_REQUEST_COMPLETE: |
| 746 | rv = DoBuildRequestComplete(rv); |
| 747 | break; |
| 748 | case STATE_SEND_REQUEST: |
| 749 | DCHECK_EQ(OK, rv); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 750 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 751 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 752 | case STATE_SEND_REQUEST_COMPLETE: |
| 753 | rv = DoSendRequestComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 754 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 755 | NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 756 | break; |
| 757 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 758 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 759 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 760 | rv = DoReadHeaders(); |
| 761 | break; |
| 762 | case STATE_READ_HEADERS_COMPLETE: |
| 763 | rv = DoReadHeadersComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 764 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 765 | NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 766 | break; |
| 767 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 768 | DCHECK_EQ(OK, rv); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 769 | net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 770 | rv = DoReadBody(); |
| 771 | break; |
| 772 | case STATE_READ_BODY_COMPLETE: |
| 773 | rv = DoReadBodyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 774 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 775 | NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 776 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 777 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 778 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 779 | net_log_.BeginEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 780 | NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 781 | rv = DoDrainBodyForAuthRestart(); |
| 782 | break; |
| 783 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 784 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 785 | net_log_.EndEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 786 | NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 787 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 788 | default: |
| 789 | NOTREACHED() << "bad state"; |
| 790 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 791 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 792 | } |
| 793 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 794 | |
| 795 | return rv; |
| 796 | } |
| 797 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 798 | int HttpNetworkTransaction::DoNotifyBeforeCreateStream() { |
| 799 | next_state_ = STATE_CREATE_STREAM; |
| 800 | bool defer = false; |
| 801 | if (!before_network_start_callback_.is_null()) |
| 802 | before_network_start_callback_.Run(&defer); |
| 803 | if (!defer) |
| 804 | return OK; |
| 805 | return ERR_IO_PENDING; |
| 806 | } |
| 807 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 808 | int HttpNetworkTransaction::DoCreateStream() { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 809 | response_.network_accessed = true; |
| 810 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 811 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 812 | // IP based pooling is only enabled on a retry after 421 Misdirected Request |
| 813 | // is received. Alternative Services are also disabled in this case (though |
| 814 | // they can also be disabled when retrying after a QUIC error). |
| 815 | if (!enable_ip_based_pooling_) |
| 816 | DCHECK(!enable_alternative_services_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 817 | if (ForWebSocketHandshake()) { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 818 | stream_request_ = |
Bence Béky | 8cae04e | 2018-01-15 18:37:06 | [diff] [blame] | 819 | session_->http_stream_factory()->RequestWebSocketHandshakeStream( |
| 820 | *request_, priority_, server_ssl_config_, proxy_ssl_config_, this, |
| 821 | websocket_handshake_stream_base_create_helper_, |
| 822 | enable_ip_based_pooling_, enable_alternative_services_, net_log_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 823 | } else { |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 824 | stream_request_ = session_->http_stream_factory()->RequestStream( |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 825 | *request_, priority_, server_ssl_config_, proxy_ssl_config_, this, |
xunjieli | 96f2a40 | 2017-06-05 17:24:27 | [diff] [blame] | 826 | enable_ip_based_pooling_, enable_alternative_services_, net_log_); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 827 | } |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 828 | DCHECK(stream_request_.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 829 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 830 | } |
| 831 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 832 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
Eric Roman | 96c5b29 | 2019-04-23 18:04:59 | [diff] [blame] | 833 | CopyConnectionAttemptsFromStreamRequest(); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 834 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 835 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 836 | DCHECK(stream_.get()); |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 837 | } else if (result == ERR_HTTP_1_1_REQUIRED || |
| 838 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 839 | return HandleHttp11Required(result); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 840 | } |
| 841 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 842 | // Handle possible client certificate errors that may have occurred if the |
| 843 | // stream used SSL for one or more of the layers. |
| 844 | result = HandleSSLClientAuthError(result); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 845 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 846 | // At this point we are done with the stream_request_. |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 847 | stream_request_.reset(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 848 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 849 | } |
| 850 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 851 | int HttpNetworkTransaction::DoInitStream() { |
| 852 | DCHECK(stream_.get()); |
| 853 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 854 | |
| 855 | stream_->GetRemoteEndpoint(&remote_endpoint_); |
| 856 | |
Steven Valdez | b4ff041 | 2018-01-18 22:39:27 | [diff] [blame] | 857 | return stream_->InitializeStream(request_, can_send_early_data_, priority_, |
| 858 | net_log_, io_callback_); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 862 | if (result == OK) { |
| 863 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 864 | } else { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 865 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 866 | result = HandleIOError(result); |
| 867 | |
| 868 | // The stream initialization failed, so this stream will never be useful. |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 869 | if (stream_) { |
| 870 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
| 871 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 872 | } |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 873 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | return result; |
| 877 | } |
| 878 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 879 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 880 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 881 | if (!ShouldApplyProxyAuth()) |
| 882 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 883 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 884 | if (!auth_controllers_[target].get()) |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame] | 885 | auth_controllers_[target] = new HttpAuthController( |
| 886 | target, AuthURL(target), session_->http_auth_cache(), |
| 887 | session_->http_auth_handler_factory(), session_->host_resolver()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 888 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 889 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 890 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) { |
| 894 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 895 | if (rv == OK) |
| 896 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN; |
| 897 | return rv; |
| 898 | } |
| 899 | |
| 900 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 901 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 902 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 903 | if (!auth_controllers_[target].get()) { |
Eric Orth | be2efac | 2019-03-06 01:11:11 | [diff] [blame] | 904 | auth_controllers_[target] = new HttpAuthController( |
| 905 | target, AuthURL(target), session_->http_auth_cache(), |
| 906 | session_->http_auth_handler_factory(), session_->host_resolver()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 907 | if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY) |
| 908 | auth_controllers_[target]->DisableEmbeddedIdentity(); |
| 909 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 910 | if (!ShouldApplyServerAuth()) |
| 911 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 912 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 913 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 914 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 918 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 919 | if (rv == OK) |
nharper | d6e6582 | 2016-03-30 23:05:48 | [diff] [blame] | 920 | next_state_ = STATE_INIT_REQUEST_BODY; |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 921 | return rv; |
| 922 | } |
| 923 | |
| 924 | int HttpNetworkTransaction::BuildRequestHeaders( |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 925 | bool using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 926 | request_headers_.SetHeader(HttpRequestHeaders::kHost, |
| 927 | GetHostAndOptionalPort(request_->url)); |
| 928 | |
| 929 | // For compat with HTTP/1.0 servers and proxies: |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 930 | if (using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 931 | request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection, |
| 932 | "keep-alive"); |
| 933 | } else { |
| 934 | request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 935 | } |
| 936 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 937 | // Add a content length header? |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 938 | if (request_->upload_data_stream) { |
| 939 | if (request_->upload_data_stream->is_chunked()) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 940 | request_headers_.SetHeader( |
| 941 | HttpRequestHeaders::kTransferEncoding, "chunked"); |
| 942 | } else { |
| 943 | request_headers_.SetHeader( |
| 944 | HttpRequestHeaders::kContentLength, |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 945 | base::NumberToString(request_->upload_data_stream->size())); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 946 | } |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 947 | } else if (request_->method == "POST" || request_->method == "PUT") { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 948 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 949 | // IE and Safari also add a content length header. Presumably it is to |
| 950 | // support sending a HEAD request to an URL that only expects to be sent a |
| 951 | // POST or some other method that normally would have a message body. |
csharrison | f473dd19 | 2015-08-18 13:54:13 | [diff] [blame] | 952 | // Firefox (40.0) does not send the header, and RFC 7230 & 7231 |
| 953 | // specify that it should not be sent due to undefined behavior. |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 954 | request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 955 | } |
| 956 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 957 | // Honor load flags that impact proxy caches. |
| 958 | if (request_->load_flags & LOAD_BYPASS_CACHE) { |
| 959 | request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 960 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 961 | } else if (request_->load_flags & LOAD_VALIDATE_CACHE) { |
| 962 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 963 | } |
| 964 | |
| 965 | if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY)) |
| 966 | auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader( |
| 967 | &request_headers_); |
| 968 | if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER)) |
| 969 | auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader( |
| 970 | &request_headers_); |
| 971 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 972 | request_headers_.MergeFrom(request_->extra_headers); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 973 | |
ryansturm | 49a8cb1 | 2016-06-15 16:51:09 | [diff] [blame] | 974 | if (!before_headers_sent_callback_.is_null()) |
| 975 | before_headers_sent_callback_.Run(proxy_info_, &request_headers_); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 976 | |
[email protected] | 173f8e2 | 2013-04-10 04:18:20 | [diff] [blame] | 977 | response_.did_use_http_auth = |
| 978 | request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) || |
| 979 | request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 980 | return OK; |
| 981 | } |
| 982 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 983 | int HttpNetworkTransaction::DoInitRequestBody() { |
| 984 | next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 985 | int rv = OK; |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 986 | if (request_->upload_data_stream) |
Matt Menke | cc1d3a90 | 2018-02-05 18:27:33 | [diff] [blame] | 987 | rv = request_->upload_data_stream->Init( |
| 988 | base::BindOnce(&HttpNetworkTransaction::OnIOComplete, |
| 989 | base::Unretained(this)), |
| 990 | net_log_); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 991 | return rv; |
| 992 | } |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 993 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 994 | int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) { |
| 995 | if (result == OK) |
| 996 | next_state_ = STATE_BUILD_REQUEST; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 997 | return result; |
| 998 | } |
| 999 | |
| 1000 | int HttpNetworkTransaction::DoBuildRequest() { |
| 1001 | next_state_ = STATE_BUILD_REQUEST_COMPLETE; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1002 | headers_valid_ = false; |
| 1003 | |
| 1004 | // This is constructed lazily (instead of within our Start method), so that |
| 1005 | // we have proxy info available. |
| 1006 | if (request_headers_.IsEmpty()) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1007 | bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel(); |
nharper | b7441ef | 2016-01-25 23:54:14 | [diff] [blame] | 1008 | return BuildRequestHeaders(using_http_proxy_without_tunnel); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1009 | } |
| 1010 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1011 | return OK; |
| 1012 | } |
| 1013 | |
| 1014 | int HttpNetworkTransaction::DoBuildRequestComplete(int result) { |
[email protected] | 9f489d7 | 2011-04-04 23:29:24 | [diff] [blame] | 1015 | if (result == OK) |
| 1016 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1017 | return result; |
| 1018 | } |
| 1019 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1020 | int HttpNetworkTransaction::DoSendRequest() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1021 | send_start_time_ = base::TimeTicks::Now(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1022 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 1023 | |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 1024 | return stream_->SendRequest(request_headers_, &response_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1025 | } |
| 1026 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1027 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1028 | send_end_time_ = base::TimeTicks::Now(); |
bnc | aa17133 | 2016-07-20 14:47:36 | [diff] [blame] | 1029 | |
| 1030 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 1031 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 1032 | return HandleHttp11Required(result); |
| 1033 | } |
| 1034 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1035 | if (result < 0) |
| 1036 | return HandleIOError(result); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1037 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1038 | return OK; |
| 1039 | } |
| 1040 | |
| 1041 | int HttpNetworkTransaction::DoReadHeaders() { |
| 1042 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 1043 | return stream_->ReadResponseHeaders(io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1047 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 1048 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1049 | if (IsCertificateError(result)) { |
| 1050 | // We don't handle a certificate error during SSL renegotiation, so we |
| 1051 | // have to return an error that's not in the certificate error range |
| 1052 | // (-2xx). |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1053 | // |
| 1054 | // TODO(davidben): Remove this error. This is impossible now that server |
| 1055 | // certificates are forbidden from changing in renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1056 | LOG(ERROR) << "Got a server certificate with error " << result |
| 1057 | << " during SSL renegotiation"; |
| 1058 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 1059 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1060 | DCHECK(stream_.get()); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 1061 | DCHECK(IsSecureRequest()); |
David Benjamin | 1c4b6d01 | 2019-07-08 17:12:57 | [diff] [blame] | 1062 | response_.cert_request_info = base::MakeRefCounted<SSLCertRequestInfo>(); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1063 | stream_->GetSSLCertRequestInfo(response_.cert_request_info.get()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1064 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
| 1065 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 1066 | stream_->Close(true); |
| 1067 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 1068 | } |
| 1069 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1070 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 1071 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 1072 | return HandleHttp11Required(result); |
| 1073 | } |
| 1074 | |
[email protected] | c871864d7 | 2014-03-13 19:56:19 | [diff] [blame] | 1075 | // ERR_CONNECTION_CLOSED is treated differently at this point; if partial |
| 1076 | // response headers were received, we do the best we can to make sense of it |
| 1077 | // and send it back up the stack. |
| 1078 | // |
| 1079 | // TODO(davidben): Consider moving this to HttpBasicStream, It's a little |
| 1080 | // bizarre for SPDY. Assuming this logic is useful at all. |
| 1081 | // TODO(davidben): Bubble the error code up so we do not cache? |
| 1082 | if (result == ERR_CONNECTION_CLOSED && response_.headers.get()) |
| 1083 | result = OK; |
| 1084 | |
| 1085 | if (result < 0) |
| 1086 | return HandleIOError(result); |
| 1087 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1088 | DCHECK(response_.headers.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1089 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1090 | if (response_.headers.get() && !ContentEncodingsValid()) |
| 1091 | return ERR_CONTENT_DECODING_FAILED; |
| 1092 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1093 | // On a 408 response from the server ("Request Timeout") on a stale socket, |
| 1094 | // retry the request. |
[email protected] | 0aff0d8 | 2014-06-14 08:49:04 | [diff] [blame] | 1095 | // Headers can be NULL because of http://crbug.com/384554. |
tyoshino | e8b3e22 | 2017-04-21 03:07:18 | [diff] [blame] | 1096 | if (response_.headers.get() && |
| 1097 | response_.headers->response_code() == HTTP_REQUEST_TIMEOUT && |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1098 | stream_->IsConnectionReused()) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1099 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1100 | GenerateNetworkErrorLoggingReport(OK); |
| 1101 | #endif // BUILDFLAG(ENABLE_REPORTING) |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1102 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1103 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1104 | response_.headers->response_code()); |
| 1105 | // This will close the socket - it would be weird to try and reuse it, even |
| 1106 | // if the server doesn't actually close it. |
| 1107 | ResetConnectionAndRequestForResend(); |
| 1108 | return OK; |
| 1109 | } |
| 1110 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 1111 | NetLogResponseHeaders(net_log_, |
| 1112 | NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 1113 | response_.headers.get()); |
Andrey Kosyakov | 2e893e6 | 2017-08-31 17:00:52 | [diff] [blame] | 1114 | if (response_headers_callback_) |
| 1115 | response_headers_callback_.Run(response_.headers); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1116 | |
bnc | be0f6af | 2015-10-15 17:49:56 | [diff] [blame] | 1117 | if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1118 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 1119 | // indicates a buggy server. See: |
| 1120 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 1121 | if (request_->method == "PUT") |
| 1122 | return ERR_METHOD_NOT_SUPPORTED; |
| 1123 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1124 | |
Steven Valdez | 58097ec3 | 2018-07-16 18:29:04 | [diff] [blame] | 1125 | if (can_send_early_data_ && response_.headers.get() && |
| 1126 | response_.headers->response_code() == HTTP_TOO_EARLY) { |
| 1127 | return HandleIOError(ERR_EARLY_DATA_REJECTED); |
| 1128 | } |
| 1129 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1130 | // Check for an intermediate 100 Continue response. An origin server is |
| 1131 | // allowed to send this response even if we didn't ask for it, so we just |
| 1132 | // need to skip over it. |
| 1133 | // We treat any other 1xx in this same way (although in practice getting |
| 1134 | // a 1xx that isn't a 100 is rare). |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1135 | // Unless this is a WebSocket request, in which case we pass it on up. |
| 1136 | if (response_.headers->response_code() / 100 == 1 && |
| 1137 | !ForWebSocketHandshake()) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1138 | response_.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1139 | next_state_ = STATE_READ_HEADERS; |
| 1140 | return OK; |
| 1141 | } |
| 1142 | |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 1143 | if (response_.headers->response_code() == 421 && |
| 1144 | (enable_ip_based_pooling_ || enable_alternative_services_)) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1145 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1146 | GenerateNetworkErrorLoggingReport(OK); |
| 1147 | #endif // BUILDFLAG(ENABLE_REPORTING) |
davidben | ce688ae | 2017-05-04 15:12:59 | [diff] [blame] | 1148 | // Retry the request with both IP based pooling and Alternative Services |
| 1149 | // disabled. |
| 1150 | enable_ip_based_pooling_ = false; |
| 1151 | enable_alternative_services_ = false; |
| 1152 | net_log_.AddEvent( |
| 1153 | NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST); |
| 1154 | ResetConnectionAndRequestForResend(); |
| 1155 | return OK; |
bnc | 8016c1f | 2017-03-31 02:11:29 | [diff] [blame] | 1156 | } |
| 1157 | |
bnc | b2602438 | 2016-06-29 02:39:45 | [diff] [blame] | 1158 | if (IsSecureRequest()) { |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1159 | stream_->GetSSLInfo(&response_.ssl_info); |
Bence Béky | 230ac61 | 2017-08-30 19:17:08 | [diff] [blame] | 1160 | if (response_.ssl_info.is_valid() && |
| 1161 | !IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1162 | session_->http_stream_factory()->ProcessAlternativeServices( |
| 1163 | session_, response_.headers.get(), |
| 1164 | url::SchemeHostPort(request_->url)); |
| 1165 | } |
| 1166 | } |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1167 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1168 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1169 | if (rv != OK) |
| 1170 | return rv; |
| 1171 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1172 | #if BUILDFLAG(ENABLE_REPORTING) |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1173 | // Note: Unless there is a pre-existing NEL policy for this origin, any NEL |
| 1174 | // reports generated before the NEL header is processed here will just be |
| 1175 | // dropped by the NetworkErrorLoggingService. |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 1176 | ProcessReportToHeader(); |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1177 | ProcessNetworkErrorLoggingHeader(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1178 | |
| 1179 | // Generate NEL report here if we have to report an HTTP error (4xx or 5xx |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 1180 | // code), or if the response body will not be read, or on a redirect. |
| 1181 | // Note: This will report a success for a redirect even if an error is |
| 1182 | // encountered later while draining the body. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1183 | int response_code = response_.headers->response_code(); |
| 1184 | if ((response_code >= 400 && response_code < 600) || |
| 1185 | response_code == HTTP_NO_CONTENT || response_code == HTTP_RESET_CONTENT || |
| 1186 | response_code == HTTP_NOT_MODIFIED || request_->method == "HEAD" || |
Lily Chen | d3930e7 | 2019-03-01 19:31:11 | [diff] [blame] | 1187 | response_.headers->GetContentLength() == 0 || |
| 1188 | response_.headers->IsRedirect(nullptr /* location */)) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1189 | GenerateNetworkErrorLoggingReport(OK); |
| 1190 | } |
| 1191 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1192 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1193 | headers_valid_ = true; |
Shivani Sharma | fdcaefd | 2017-11-02 00:12:26 | [diff] [blame] | 1194 | |
| 1195 | // We have reached the end of Start state machine, set the RequestInfo to |
| 1196 | // null. |
| 1197 | // RequestInfo is a member of the HttpTransaction's consumer and is useful |
| 1198 | // only until the final response headers are received. Clearing it will ensure |
| 1199 | // that HttpRequestInfo is only used up until final response headers are |
| 1200 | // received. Clearing is allowed so that the transaction can be disassociated |
| 1201 | // from its creating consumer in cases where it is shared for writing to the |
| 1202 | // cache. It is also safe to set it to null at this point since |
| 1203 | // upload_data_stream is also not used in the Read state machine. |
| 1204 | if (pending_auth_target_ == HttpAuth::AUTH_NONE) |
| 1205 | request_ = nullptr; |
| 1206 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1207 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | int HttpNetworkTransaction::DoReadBody() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1211 | DCHECK(read_buf_.get()); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 1212 | DCHECK_GT(read_buf_len_, 0); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1213 | DCHECK(stream_ != nullptr); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1214 | |
| 1215 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1216 | return stream_->ReadResponseBody( |
| 1217 | read_buf_.get(), read_buf_len_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 1221 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1222 | bool done = false; |
| 1223 | if (result <= 0) { |
| 1224 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1225 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1226 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1227 | |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1228 | // Clean up connection if we are done. |
| 1229 | if (done) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1230 | // Note: Just because IsResponseBodyComplete is true, we're not |
| 1231 | // necessarily "done". We're only "done" when it is the last |
| 1232 | // read on this HttpNetworkTransaction, which will be signified |
| 1233 | // by a zero-length read. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1234 | // TODO(mbelshe): The keep-alive property is really a property of |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1235 | // the stream. No need to compute it here just to pass back |
| 1236 | // to the stream's Close function. |
mmenke | bd84c39 | 2015-09-02 14:12:34 | [diff] [blame] | 1237 | bool keep_alive = |
| 1238 | stream_->IsResponseBodyComplete() && stream_->CanReuseConnection(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1239 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1240 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 1241 | // Note: we don't reset the stream here. We've closed it, but we still |
| 1242 | // need it around so that callers can call methods such as |
| 1243 | // GetUploadProgress() and have them be meaningful. |
| 1244 | // TODO(mbelshe): This means we closed the stream here, and we close it |
| 1245 | // again in ~HttpNetworkTransaction. Clean that up. |
| 1246 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1247 | // The next Read call will return 0 (EOF). |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1248 | |
| 1249 | // This transaction was successful. If it had been retried because of an |
| 1250 | // error with an alternative service, mark that alternative service broken. |
| 1251 | if (!enable_alternative_services_ && |
| 1252 | retried_alternative_service_.protocol != kProtoUnknown) { |
Ryan Hamilton | 9f532a1 | 2019-04-22 22:26:41 | [diff] [blame] | 1253 | HistogramBrokenAlternateProtocolLocation( |
| 1254 | BROKEN_ALTERNATE_PROTOCOL_LOCATION_HTTP_NETWORK_TRANSACTION); |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1255 | session_->http_server_properties()->MarkAlternativeServiceBroken( |
| 1256 | retried_alternative_service_); |
| 1257 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1258 | |
| 1259 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1260 | GenerateNetworkErrorLoggingReport(result); |
| 1261 | #endif // BUILDFLAG(ENABLE_REPORTING) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | // Clear these to avoid leaving around old state. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1265 | read_buf_ = nullptr; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1266 | read_buf_len_ = 0; |
| 1267 | |
| 1268 | return result; |
| 1269 | } |
| 1270 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1271 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 1272 | // This method differs from DoReadBody only in the next_state_. So we just |
| 1273 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 1274 | // elegant way for these two methods to share code. |
| 1275 | int rv = DoReadBody(); |
| 1276 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 1277 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 1278 | return rv; |
| 1279 | } |
| 1280 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1281 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 1282 | // the same. Figure out a good way for these two methods to share code. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1283 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1284 | // keep_alive defaults to true because the very reason we're draining the |
| 1285 | // response body is to reuse the connection for auth restart. |
| 1286 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1287 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1288 | // Error or closed connection while reading the socket. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1289 | // Note: No Network Error Logging report is generated here because a report |
| 1290 | // will have already been generated for the original request due to the auth |
| 1291 | // challenge, so a second report is not generated for the same request here. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1292 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1293 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1294 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1295 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | if (done) { |
| 1299 | DidDrainBodyForAuthRestart(keep_alive); |
| 1300 | } else { |
| 1301 | // Keep draining. |
| 1302 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 1303 | } |
| 1304 | |
| 1305 | return OK; |
| 1306 | } |
| 1307 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1308 | #if BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 134b52e | 2018-11-09 18:00:14 | [diff] [blame] | 1309 | void HttpNetworkTransaction::ProcessReportToHeader() { |
| 1310 | std::string value; |
| 1311 | if (!response_.headers->GetNormalizedHeader("Report-To", &value)) |
| 1312 | return; |
| 1313 | |
| 1314 | ReportingService* service = session_->reporting_service(); |
| 1315 | if (!service) { |
| 1316 | ReportingHeaderParser::RecordHeaderDiscardedForNoReportingService(); |
| 1317 | return; |
| 1318 | } |
| 1319 | |
| 1320 | // Only accept Report-To headers on HTTPS connections that have no |
| 1321 | // certificate errors. |
| 1322 | if (!response_.ssl_info.is_valid()) { |
| 1323 | ReportingHeaderParser::RecordHeaderDiscardedForInvalidSSLInfo(); |
| 1324 | return; |
| 1325 | } |
| 1326 | if (IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1327 | ReportingHeaderParser::RecordHeaderDiscardedForCertStatusError(); |
| 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | service->ProcessHeader(url_.GetOrigin(), value); |
| 1332 | } |
| 1333 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1334 | void HttpNetworkTransaction::ProcessNetworkErrorLoggingHeader() { |
| 1335 | std::string value; |
| 1336 | if (!response_.headers->GetNormalizedHeader( |
| 1337 | NetworkErrorLoggingService::kHeaderName, &value)) { |
| 1338 | return; |
| 1339 | } |
| 1340 | |
| 1341 | NetworkErrorLoggingService* service = |
| 1342 | session_->network_error_logging_service(); |
| 1343 | if (!service) { |
| 1344 | NetworkErrorLoggingService:: |
| 1345 | RecordHeaderDiscardedForNoNetworkErrorLoggingService(); |
| 1346 | return; |
| 1347 | } |
| 1348 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 1349 | // Don't accept NEL headers received via a proxy, because the IP address of |
| 1350 | // the destination server is not known. |
| 1351 | if (response_.was_fetched_via_proxy) |
| 1352 | return; |
| 1353 | |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1354 | // Only accept NEL headers on HTTPS connections that have no certificate |
| 1355 | // errors. |
| 1356 | if (!response_.ssl_info.is_valid()) { |
| 1357 | NetworkErrorLoggingService::RecordHeaderDiscardedForInvalidSSLInfo(); |
| 1358 | return; |
| 1359 | } |
| 1360 | if (IsCertStatusError(response_.ssl_info.cert_status)) { |
| 1361 | NetworkErrorLoggingService::RecordHeaderDiscardedForCertStatusError(); |
| 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | if (remote_endpoint_.address().empty()) { |
| 1366 | NetworkErrorLoggingService::RecordHeaderDiscardedForMissingRemoteEndpoint(); |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | service->OnHeader(url::Origin::Create(url_), remote_endpoint_.address(), |
| 1371 | value); |
| 1372 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1373 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1374 | void HttpNetworkTransaction::GenerateNetworkErrorLoggingReportIfError(int rv) { |
| 1375 | if (rv < 0 && rv != ERR_IO_PENDING) |
| 1376 | GenerateNetworkErrorLoggingReport(rv); |
| 1377 | } |
| 1378 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1379 | void HttpNetworkTransaction::GenerateNetworkErrorLoggingReport(int rv) { |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1380 | // |rv| should be a valid net::Error |
| 1381 | DCHECK_NE(rv, ERR_IO_PENDING); |
| 1382 | DCHECK_LE(rv, 0); |
| 1383 | |
| 1384 | if (network_error_logging_report_generated_) |
| 1385 | return; |
| 1386 | network_error_logging_report_generated_ = true; |
| 1387 | |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1388 | NetworkErrorLoggingService* service = |
| 1389 | session_->network_error_logging_service(); |
| 1390 | if (!service) { |
| 1391 | NetworkErrorLoggingService:: |
| 1392 | RecordRequestDiscardedForNoNetworkErrorLoggingService(); |
| 1393 | return; |
| 1394 | } |
| 1395 | |
Lily Chen | 90ae93cc | 2019-02-14 01:15:39 | [diff] [blame] | 1396 | // Don't report on proxy auth challenges. |
| 1397 | if (response_.headers && response_.headers->response_code() == |
| 1398 | HTTP_PROXY_AUTHENTICATION_REQUIRED) { |
| 1399 | return; |
| 1400 | } |
| 1401 | |
| 1402 | // Don't generate NEL reports if we are behind a proxy, to avoid leaking |
| 1403 | // internal network details. |
| 1404 | if (response_.was_fetched_via_proxy) |
| 1405 | return; |
| 1406 | |
Lily Chen | 00196ab6 | 2018-12-04 19:52:29 | [diff] [blame] | 1407 | // Ignore errors from non-HTTPS origins. |
| 1408 | if (!url_.SchemeIsCryptographic()) { |
| 1409 | NetworkErrorLoggingService::RecordRequestDiscardedForInsecureOrigin(); |
| 1410 | return; |
| 1411 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1412 | |
| 1413 | NetworkErrorLoggingService::RequestDetails details; |
| 1414 | |
| 1415 | details.uri = url_; |
| 1416 | if (!request_referrer_.empty()) |
| 1417 | details.referrer = GURL(request_referrer_); |
| 1418 | details.user_agent = request_user_agent_; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1419 | if (!remote_endpoint_.address().empty()) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1420 | details.server_ip = remote_endpoint_.address(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1421 | } else { |
| 1422 | details.server_ip = IPAddress(); |
| 1423 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1424 | // HttpResponseHeaders::response_code() returns 0 if response code couldn't |
| 1425 | // be parsed, which is also how NEL represents the same. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1426 | if (response_.headers) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1427 | details.status_code = response_.headers->response_code(); |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1428 | } else { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1429 | details.status_code = 0; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1430 | } |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1431 | // If we got response headers, assume that the connection used HTTP/1.1 |
| 1432 | // unless ALPN negotiation tells us otherwise (handled below). |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1433 | if (response_.was_alpn_negotiated) { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1434 | details.protocol = response_.alpn_negotiated_protocol; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1435 | } else { |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1436 | details.protocol = "http/1.1"; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1437 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1438 | details.method = request_method_; |
| 1439 | details.elapsed_time = base::TimeTicks::Now() - start_timeticks_; |
Douglas Creager | ef5eecdc | 2018-11-09 20:50:36 | [diff] [blame] | 1440 | details.type = static_cast<Error>(rv); |
| 1441 | details.reporting_upload_depth = request_reporting_upload_depth_; |
| 1442 | |
| 1443 | service->OnRequest(std::move(details)); |
| 1444 | } |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1445 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Douglas Creager | 3cb04205 | 2018-11-06 23:08:52 | [diff] [blame] | 1446 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1447 | int HttpNetworkTransaction::HandleHttp11Required(int error) { |
| 1448 | DCHECK(error == ERR_HTTP_1_1_REQUIRED || |
| 1449 | error == ERR_PROXY_HTTP_1_1_REQUIRED); |
| 1450 | |
| 1451 | if (error == ERR_HTTP_1_1_REQUIRED) { |
| 1452 | HttpServerProperties::ForceHTTP11(&server_ssl_config_); |
| 1453 | } else { |
| 1454 | HttpServerProperties::ForceHTTP11(&proxy_ssl_config_); |
| 1455 | } |
| 1456 | ResetConnectionAndRequestForResend(); |
| 1457 | return OK; |
| 1458 | } |
| 1459 | |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1460 | int HttpNetworkTransaction::HandleSSLClientAuthError(int error) { |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1461 | // Client certificate errors may come from either the origin server or the |
| 1462 | // proxy. |
| 1463 | // |
| 1464 | // Origin errors are handled here, while most proxy errors are handled in the |
| 1465 | // HttpStreamFactory and below. However, if the request is not tunneled (i.e. |
| 1466 | // the origin is HTTP, so there is no HTTPS connection) and the proxy does not |
| 1467 | // report a bad client certificate until after the TLS handshake completes. |
| 1468 | // The latter occurs in TLS 1.3 or TLS 1.2 with False Start (disabled for |
| 1469 | // proxies). The error will then surface out of Read() rather than Connect() |
| 1470 | // and ultimately surfaced out of DoReadHeadersComplete(). |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 1471 | // |
| 1472 | // See https://crbug.com/828965. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1473 | bool is_server = !UsingHttpProxyWithoutTunnel(); |
| 1474 | HostPortPair host_port_pair = |
| 1475 | is_server ? HostPortPair::FromURL(request_->url) |
| 1476 | : proxy_info_.proxy_server().host_port_pair(); |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 1477 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1478 | if (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error)) { |
David Benjamin | 3b94b0f | 2019-04-25 23:07:52 | [diff] [blame] | 1479 | DCHECK((is_server && IsSecureRequest()) || proxy_info_.is_https()); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame^] | 1480 | if (session_->ssl_client_context()->ClearClientCertificate( |
| 1481 | host_port_pair)) { |
| 1482 | // The private key handle may have gone stale due to, e.g., the user |
| 1483 | // unplugging their smartcard. Operating systems do not provide reliable |
| 1484 | // notifications for this, so if the signature failed and the user was |
| 1485 | // not already prompted for certificate on this request, retry to ask |
| 1486 | // the user for a new one. |
| 1487 | // |
| 1488 | // TODO(davidben): There is no corresponding feature for proxy client |
| 1489 | // certificates. Ideally this would live at a lower level, common to both, |
| 1490 | // but |configured_client_cert_for_server_| is not accessible below the |
| 1491 | // socket pools. |
| 1492 | if (is_server && error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED && |
| 1493 | !configured_client_cert_for_server_ && !HasExceededMaxRetries()) { |
| 1494 | retry_attempts_++; |
| 1495 | net_log_.AddEventWithNetErrorCode( |
| 1496 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
| 1497 | ResetConnectionAndRequestForResend(); |
| 1498 | return OK; |
| 1499 | } |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1500 | } |
| 1501 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1502 | return error; |
| 1503 | } |
| 1504 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1505 | // This method determines whether it is safe to resend the request after an |
| 1506 | // IO error. It can only be called in response to request header or body |
| 1507 | // write errors or response header read errors. It should not be used in |
| 1508 | // other cases, such as a Connect error. |
| 1509 | int HttpNetworkTransaction::HandleIOError(int error) { |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1510 | // Because the peer may request renegotiation with client authentication at |
| 1511 | // any time, check and handle client authentication errors. |
David Benjamin | 76a40ad | 2018-02-24 22:22:08 | [diff] [blame] | 1512 | error = HandleSSLClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1513 | |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1514 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1515 | GenerateNetworkErrorLoggingReportIfError(error); |
| 1516 | #endif // BUILDFLAG(ENABLE_REPORTING) |
| 1517 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1518 | switch (error) { |
| 1519 | // If we try to reuse a connection that the server is in the process of |
| 1520 | // closing, we may end up successfully writing out our request (or a |
| 1521 | // portion of our request) only to find a connection error when we try to |
| 1522 | // read from (or finish writing to) the socket. |
| 1523 | case ERR_CONNECTION_RESET: |
| 1524 | case ERR_CONNECTION_CLOSED: |
| 1525 | case ERR_CONNECTION_ABORTED: |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1526 | // There can be a race between the socket pool checking checking whether a |
| 1527 | // socket is still connected, receiving the FIN, and sending/reading data |
| 1528 | // on a reused socket. If we receive the FIN between the connectedness |
| 1529 | // check and writing/reading from the socket, we may first learn the socket |
| 1530 | // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most |
| 1531 | // likely happen when trying to retrieve its IP address. |
| 1532 | // See http://crbug.com/105824 for more details. |
| 1533 | case ERR_SOCKET_NOT_CONNECTED: |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1534 | // If a socket is closed on its initial request, HttpStreamParser returns |
| 1535 | // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was |
| 1536 | // preconnected but failed to be used before the server timed it out. |
| 1537 | case ERR_EMPTY_RESPONSE: |
| 1538 | if (ShouldResendRequest()) { |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1539 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1540 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1541 | ResetConnectionAndRequestForResend(); |
| 1542 | error = OK; |
| 1543 | } |
| 1544 | break; |
Steven Valdez | 58097ec3 | 2018-07-16 18:29:04 | [diff] [blame] | 1545 | case ERR_EARLY_DATA_REJECTED: |
| 1546 | case ERR_WRONG_VERSION_ON_EARLY_DATA: |
| 1547 | net_log_.AddEventWithNetErrorCode( |
| 1548 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
| 1549 | // Disable early data on the SSLConfig on a reset. |
| 1550 | can_send_early_data_ = false; |
| 1551 | ResetConnectionAndRequestForResend(); |
| 1552 | error = OK; |
| 1553 | break; |
Bence Béky | d0d6950 | 2019-06-25 19:47:18 | [diff] [blame] | 1554 | case ERR_HTTP2_PING_FAILED: |
| 1555 | case ERR_HTTP2_SERVER_REFUSED_STREAM: |
| 1556 | case ERR_HTTP2_PUSHED_STREAM_NOT_AVAILABLE: |
| 1557 | case ERR_HTTP2_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER: |
| 1558 | case ERR_HTTP2_PUSHED_RESPONSE_DOES_NOT_MATCH: |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 1559 | case ERR_QUIC_HANDSHAKE_FAILED: |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1560 | if (HasExceededMaxRetries()) |
| 1561 | break; |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1562 | net_log_.AddEventWithNetErrorCode( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1563 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1564 | retry_attempts_++; |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1565 | ResetConnectionAndRequestForResend(); |
| 1566 | error = OK; |
| 1567 | break; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1568 | case ERR_QUIC_PROTOCOL_ERROR: |
| 1569 | if (GetResponseHeaders() != nullptr || |
| 1570 | !stream_->GetAlternativeService(&retried_alternative_service_)) { |
| 1571 | // If the response headers have already been recieved and passed up |
| 1572 | // then the request can not be retried. Also, if there was no |
| 1573 | // alternative service used for this request, then there is no |
| 1574 | // alternative service to be disabled. |
| 1575 | break; |
| 1576 | } |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1577 | if (HasExceededMaxRetries()) |
| 1578 | break; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1579 | if (session_->http_server_properties()->IsAlternativeServiceBroken( |
| 1580 | retried_alternative_service_)) { |
| 1581 | // If the alternative service was marked as broken while the request |
| 1582 | // was in flight, retry the request which will not use the broken |
| 1583 | // alternative service. |
| 1584 | net_log_.AddEventWithNetErrorCode( |
| 1585 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1586 | retry_attempts_++; |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1587 | ResetConnectionAndRequestForResend(); |
| 1588 | error = OK; |
Nick Harper | 72ade19 | 2019-07-17 03:30:42 | [diff] [blame] | 1589 | } else if (session_->params() |
| 1590 | .quic_params.retry_without_alt_svc_on_quic_errors) { |
rch | 2f2991c | 2017-04-13 19:28:17 | [diff] [blame] | 1591 | // Disable alternative services for this request and retry it. If the |
| 1592 | // retry succeeds, then the alternative service will be marked as |
| 1593 | // broken then. |
| 1594 | enable_alternative_services_ = false; |
rch | 514a44a8 | 2017-04-13 04:46:36 | [diff] [blame] | 1595 | net_log_.AddEventWithNetErrorCode( |
| 1596 | NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1597 | retry_attempts_++; |
rch | 514a44a8 | 2017-04-13 04:46:36 | [diff] [blame] | 1598 | ResetConnectionAndRequestForResend(); |
| 1599 | error = OK; |
| 1600 | } |
| 1601 | break; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1602 | } |
| 1603 | return error; |
| 1604 | } |
| 1605 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1606 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1607 | ResetStateForAuthRestart(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1608 | if (stream_) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1609 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
sclittle | fb24989 | 2015-09-10 21:33:22 | [diff] [blame] | 1610 | total_sent_bytes_ += stream_->GetTotalSentBytes(); |
| 1611 | } |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1612 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | void HttpNetworkTransaction::ResetStateForAuthRestart() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1616 | send_start_time_ = base::TimeTicks(); |
| 1617 | send_end_time_ = base::TimeTicks(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1618 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1619 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1620 | read_buf_ = nullptr; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1621 | read_buf_len_ = 0; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1622 | headers_valid_ = false; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1623 | request_headers_.Clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1624 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1625 | establishing_tunnel_ = false; |
ttuttle | d9dbc65 | 2015-09-29 20:00:59 | [diff] [blame] | 1626 | remote_endpoint_ = IPEndPoint(); |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1627 | net_error_details_.quic_broken = false; |
Ryan Hamilton | 8d9ee76e | 2018-05-29 23:52:52 | [diff] [blame] | 1628 | net_error_details_.quic_connection_error = quic::QUIC_NO_ERROR; |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1629 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1630 | network_error_logging_report_generated_ = false; |
| 1631 | start_timeticks_ = base::TimeTicks::Now(); |
| 1632 | #endif // BUILDFLAG(ENABLE_REPORTING) |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() { |
| 1636 | if (stream_) |
| 1637 | stream_->PopulateNetErrorDetails(&net_error_details_); |
| 1638 | stream_.reset(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1639 | } |
| 1640 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1641 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1642 | return response_.headers.get(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1643 | } |
| 1644 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1645 | bool HttpNetworkTransaction::ShouldResendRequest() const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1646 | bool connection_is_proven = stream_->IsConnectionReused(); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1647 | bool has_received_headers = GetResponseHeaders() != nullptr; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1648 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1649 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1650 | // This automatically prevents an infinite resend loop because we'll run |
| 1651 | // out of the cached keep-alive connections eventually. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1652 | if (connection_is_proven && !has_received_headers) |
| 1653 | return true; |
| 1654 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1655 | } |
| 1656 | |
Biljith Jayan | 45a4172 | 2017-08-16 18:43:14 | [diff] [blame] | 1657 | bool HttpNetworkTransaction::HasExceededMaxRetries() const { |
| 1658 | return (retry_attempts_ >= kMaxRetryAttempts); |
| 1659 | } |
| 1660 | |
David Benjamin | 83ddfb3 | 2018-03-30 01:07:52 | [diff] [blame] | 1661 | bool HttpNetworkTransaction::CheckMaxRestarts() { |
| 1662 | num_restarts_++; |
| 1663 | return num_restarts_ < kMaxRestarts; |
| 1664 | } |
| 1665 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1666 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1667 | if (stream_.get()) { |
| 1668 | stream_->Close(true); |
zhongyi | ca364fbb | 2015-12-12 03:39:12 | [diff] [blame] | 1669 | CacheNetErrorDetailsAndResetStream(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1670 | } |
| 1671 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1672 | // We need to clear request_headers_ because it contains the real request |
| 1673 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1674 | // the SSL tunnel. |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1675 | request_headers_.Clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1676 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
Lily Chen | fec60d9 | 2019-01-24 01:16:42 | [diff] [blame] | 1677 | |
| 1678 | #if BUILDFLAG(ENABLE_REPORTING) |
| 1679 | // Reset for new request. |
| 1680 | network_error_logging_report_generated_ = false; |
| 1681 | start_timeticks_ = base::TimeTicks::Now(); |
| 1682 | #endif // BUILDFLAG(ENABLE_REPORTING) |
Wojciech Dzierżanowski | abdeeaf | 2019-04-01 20:16:22 | [diff] [blame] | 1683 | |
| 1684 | ResetStateForRestart(); |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1685 | } |
| 1686 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1687 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1688 | return UsingHttpProxyWithoutTunnel(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1689 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1690 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1691 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1692 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1693 | } |
| 1694 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1695 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1696 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1697 | DCHECK(headers.get()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1698 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1699 | int status = headers->response_code(); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1700 | if (status != HTTP_UNAUTHORIZED && |
| 1701 | status != HTTP_PROXY_AUTHENTICATION_REQUIRED) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1702 | return OK; |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1703 | HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1704 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1705 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1706 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1707 | |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1708 | // This case can trigger when an HTTPS server responds with a "Proxy |
| 1709 | // authentication required" status code through a non-authenticating |
| 1710 | // proxy. |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1711 | if (!auth_controllers_[target].get()) |
| 1712 | return ERR_UNEXPECTED_PROXY_AUTH; |
| 1713 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1714 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1715 | headers, response_.ssl_info, |
| 1716 | (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1717 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1718 | if (auth_controllers_[target]->HaveAuthHandler()) |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 1719 | pending_auth_target_ = target; |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1720 | |
Emily Stark | f2c9bbd | 2019-04-09 17:08:58 | [diff] [blame] | 1721 | auth_controllers_[target]->TakeAuthInfo(&response_.auth_challenge); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1722 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1723 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1724 | } |
| 1725 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1726 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1727 | return auth_controllers_[target].get() && |
| 1728 | auth_controllers_[target]->HaveAuth(); |
| 1729 | } |
| 1730 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1731 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1732 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1733 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1734 | if (!proxy_info_.proxy_server().is_valid() || |
| 1735 | proxy_info_.proxy_server().is_direct()) { |
| 1736 | return GURL(); // There is no proxy server. |
| 1737 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1738 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1739 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1740 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1741 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1742 | case HttpAuth::AUTH_SERVER: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1743 | if (ForWebSocketHandshake()) { |
Adam Rice | 9bd428b0a | 2019-02-15 06:31:36 | [diff] [blame] | 1744 | return net::ChangeWebSocketSchemeToHttpScheme(request_->url); |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1745 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1746 | return request_->url; |
| 1747 | default: |
| 1748 | return GURL(); |
| 1749 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1750 | } |
| 1751 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1752 | bool HttpNetworkTransaction::ForWebSocketHandshake() const { |
[email protected] | 23d3e02 | 2013-11-23 20:38:14 | [diff] [blame] | 1753 | return websocket_handshake_stream_base_create_helper_ && |
| 1754 | request_->url.SchemeIsWSOrWSS(); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1755 | } |
| 1756 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 1757 | void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() { |
| 1758 | DCHECK(stream_request_); |
| 1759 | |
| 1760 | // Since the transaction can restart with auth credentials, it may create a |
| 1761 | // stream more than once. Accumulate all of the connection attempts across |
| 1762 | // those streams by appending them to the vector: |
| 1763 | for (const auto& attempt : stream_request_->connection_attempts()) |
| 1764 | connection_attempts_.push_back(attempt); |
| 1765 | } |
| 1766 | |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1767 | bool HttpNetworkTransaction::ContentEncodingsValid() const { |
| 1768 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 1769 | DCHECK(headers); |
| 1770 | |
| 1771 | std::string accept_encoding; |
| 1772 | request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding, |
| 1773 | &accept_encoding); |
| 1774 | std::set<std::string> allowed_encodings; |
Paul Jensen | e85e815 | 2019-07-01 15:38:00 | [diff] [blame] | 1775 | if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1776 | return false; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1777 | |
| 1778 | std::string content_encoding; |
| 1779 | headers->GetNormalizedHeader("Content-Encoding", &content_encoding); |
| 1780 | std::set<std::string> used_encodings; |
Paul Jensen | e85e815 | 2019-07-01 15:38:00 | [diff] [blame] | 1781 | if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1782 | return false; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1783 | |
| 1784 | // When "Accept-Encoding" is not specified, it is parsed as "*". |
| 1785 | // If "*" encoding is advertised, then any encoding should be "accepted". |
| 1786 | // This does not mean, that it will be successfully decoded. |
| 1787 | if (allowed_encodings.find("*") != allowed_encodings.end()) |
| 1788 | return true; |
| 1789 | |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1790 | bool result = true; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1791 | for (auto const& encoding : used_encodings) { |
| 1792 | SourceStream::SourceType source_type = |
| 1793 | FilterSourceStream::ParseEncodingType(encoding); |
| 1794 | // We don't reject encodings we are not aware. They just will not decode. |
| 1795 | if (source_type == SourceStream::TYPE_UNKNOWN) |
| 1796 | continue; |
| 1797 | if (allowed_encodings.find(encoding) == allowed_encodings.end()) { |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1798 | result = false; |
| 1799 | break; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1800 | } |
| 1801 | } |
sky | 50576f3 | 2017-05-01 19:28:03 | [diff] [blame] | 1802 | |
| 1803 | // Temporary workaround for http://crbug.com/714514 |
| 1804 | if (headers->IsRedirect(nullptr)) { |
| 1805 | UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding", |
| 1806 | !result); |
| 1807 | return true; |
| 1808 | } |
| 1809 | |
| 1810 | return result; |
eustas | c7d27da | 2017-04-06 10:33:20 | [diff] [blame] | 1811 | } |
| 1812 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1813 | } // namespace net |