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