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