blob: 7c069f076c00019f337794c0aee7bf186aa0544c [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]2fbaecf22010-07-22 22:20:359#include <vector>
10
nharperb7441ef2016-01-25 23:54:1411#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4112#include "base/bind.h"
13#include "base/bind_helpers.h"
Bence Békybc1de292018-02-01 15:48:0314#include "base/callback_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
[email protected]7286e3fc2011-07-19 22:13:2420#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4721#include "base/strings/string_number_conversions.h"
22#include "base/strings/string_util.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
Lily Houghton582d4622018-01-22 22:43:4032#include "net/base/proxy_server.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4434#include "net/base/url_util.h"
Bence Béky230ac612017-08-30 19:17:0835#include "net/cert/cert_status_flags.h"
eustasc7d27da2017-04-06 10:33:2036#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0037#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4238#include "net/http/http_auth.h"
39#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2440#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3641#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5242#include "net/http/http_chunked_decoder.h"
43#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5444#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1345#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4746#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5247#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2148#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5749#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5350#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5851#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3952#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3153#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5254#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2355#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3656#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0057#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4458#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2159#include "net/socket/next_proto.h"
[email protected]a796bcec2010-03-22 17:17:2660#include "net/socket/socks_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2861#include "net/socket/transport_client_socket_pool.h"
bnc8f8f7d302017-04-24 18:08:0662#include "net/spdy/chromium/spdy_http_stream.h"
63#include "net/spdy/chromium/spdy_session.h"
64#include "net/spdy/chromium/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5765#include "net/ssl/ssl_cert_request_info.h"
66#include "net/ssl/ssl_connection_status_flags.h"
Bence Béky230ac612017-08-30 19:17:0867#include "net/ssl/ssl_info.h"
svaldez7872fd02015-11-19 21:10:5468#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1469#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5470#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2971#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5272
Biljith Jayan45a41722017-08-16 18:43:1473namespace {
74// Max number of |retry_attempts| (excluding the initial request) after which
75// we give up and show an error page.
76const size_t kMaxRetryAttempts = 2;
77} // namespace
78
initial.commit586acc5fe2008-07-26 22:42:5279namespace net {
80
[email protected]262eec82013-03-19 21:01:3681HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
82 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2283 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3284 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
85 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5286 session_(session),
87 request_(NULL),
[email protected]262eec82013-03-19 21:01:3688 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5789 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:2790 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:0891 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:2092 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5293 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:1994 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:2295 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:5496 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:4497 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:2998 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:2699 enable_alternative_services_(true),
zhongyi48704c182015-12-07 07:52:02100 websocket_handshake_stream_base_create_helper_(NULL),
Biljith Jayan45a41722017-08-16 18:43:14101 net_error_details_(),
102 retry_attempts_(0) {}
[email protected]3ce7df0f2010-03-03 00:30:50103
[email protected]0b0bf032010-09-21 18:08:50104HttpNetworkTransaction::~HttpNetworkTransaction() {
105 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50106 // TODO(mbelshe): The stream_ should be able to compute whether or not the
107 // stream should be kept alive. No reason to compute here
108 // and pass it in.
mmenkebd84c392015-09-02 14:12:34109 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50110 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34111 } else if (stream_->IsResponseBodyComplete()) {
112 // If the response body is complete, we can just reuse the socket.
113 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50114 } else {
mmenkebd84c392015-09-02 14:12:34115 // Otherwise, we try to drain the response body.
116 HttpStream* stream = stream_.release();
117 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50118 }
119 }
[email protected]02cad5d2013-10-02 08:14:03120 if (request_ && request_->upload_data_stream)
121 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50122}
123
[email protected]684970b2009-08-14 04:54:46124int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41125 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20126 const NetLogWithSource& net_log) {
Ramin Halavatib5e433e2018-02-07 07:41:10127 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53128 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04129 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15130 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04131
nharper8cdb0fb2016-04-22 21:34:59132 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
133 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
134
[email protected]99ffa5a2011-10-06 04:20:19135 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14136 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19137 proxy_ssl_config_.rev_checking_enabled = false;
138 }
[email protected]6fbac162011-06-20 00:29:04139
Steven Valdez03e872d2018-03-02 15:39:15140 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27141 can_send_early_data_ = true;
142 }
143
jkarlinfb1d5172015-01-12 14:10:29144 if (request_->load_flags & LOAD_PREFETCH)
145 response_.unused_since_prefetch = true;
146
rdsmith1d343be52016-10-21 20:37:50147 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04148 int rv = DoLoop(OK);
149 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41150 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04151 return rv;
152}
153
154int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41155 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38156 DCHECK(!stream_.get());
157 DCHECK(!stream_request_.get());
158 DCHECK_EQ(STATE_NONE, next_state_);
159
[email protected]82918cc2010-08-25 17:24:50160 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38161
[email protected]ccb40e52008-09-17 20:54:40162 int rv = DoLoop(OK);
163 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41164 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11165 return rv;
[email protected]96d570e42008-08-05 22:43:04166}
167
[email protected]0b45559b2009-06-12 21:45:11168int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08169 scoped_refptr<X509Certificate> client_cert,
170 scoped_refptr<SSLPrivateKey> client_private_key,
svaldez7872fd02015-11-19 21:10:54171 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38172 // In HandleCertificateRequest(), we always tear down existing stream
173 // requests to force a new connection. So we shouldn't have one here.
174 DCHECK(!stream_request_.get());
175 DCHECK(!stream_.get());
176 DCHECK_EQ(STATE_NONE, next_state_);
177
[email protected]102957f2011-09-02 17:10:14178 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
179 &proxy_ssl_config_ : &server_ssl_config_;
180 ssl_config->send_client_cert = true;
181 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54182 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45183 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08184 response_.cert_request_info->host_and_port, std::move(client_cert),
185 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11186 // Reset the other member variables.
187 // Note: this is necessary only with SSL renegotiation.
188 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50189 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11190 int rv = DoLoop(OK);
191 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41192 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11193 return rv;
194}
195
[email protected]49639fa2011-12-20 23:22:41196int HttpNetworkTransaction::RestartWithAuth(
197 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22198 HttpAuth::Target target = pending_auth_target_;
199 if (target == HttpAuth::AUTH_NONE) {
200 NOTREACHED();
201 return ERR_UNEXPECTED;
202 }
[email protected]0757e7702009-03-27 04:00:22203 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42204
[email protected]f3cf9802011-10-28 18:44:58205 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13206
[email protected]49639fa2011-12-20 23:22:41207 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38208
209 int rv = OK;
210 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
211 // In this case, we've gathered credentials for use with proxy
212 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50213 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38214 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59215 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54216 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39217 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54218 } else {
[email protected]8e6441ca2010-08-19 05:56:38219 // In this case, we've gathered credentials for the server or the proxy
220 // but it is not during the tunneling phase.
221 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54222 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38223 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54224 }
[email protected]c3b35c22008-09-27 03:19:42225
[email protected]c3b35c22008-09-27 03:19:42226 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41227 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42228 return rv;
[email protected]96d570e42008-08-05 22:43:04229}
230
[email protected]f9ee6b52008-11-08 06:46:23231void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
232 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38233 DCHECK(!stream_request_.get());
234
Bence Béky3238f2e12017-09-22 22:44:49235 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
236 if (target == HttpAuth::AUTH_SERVER &&
237 auth_controllers_[target]->NeedsHTTP11()) {
238 session_->http_server_properties()->SetHTTP11Required(
239 HostPortPair::FromURL(request_->url));
240 }
241
[email protected]2d2697f92009-02-18 21:00:32242 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57243 // Even if the server says the connection is keep-alive, we have to be
244 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34245 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57246 // If the response body hasn't been completely read, we need to drain
247 // it first.
[email protected]351ab642010-08-05 16:55:31248 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32249 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57250 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32251 read_buf_len_ = kDrainBodyBufferSize;
252 return;
253 }
[email protected]0877e3d2009-10-17 22:29:57254 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09255 }
256
[email protected]2d2697f92009-02-18 21:00:32257 // We don't need to drain the response body, so we act as if we had drained
258 // the response body.
259 DidDrainBodyForAuthRestart(keep_alive);
260}
261
262void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38263 DCHECK(!stream_request_.get());
264
265 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19266 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22267 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58268 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34269 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38270 // We should call connection_->set_idle_time(), but this doesn't occur
271 // often enough to be worth the trouble.
272 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39273 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38274 }
[email protected]697ef4c2010-10-14 16:38:58275
276 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46277 // Close the stream and mark it as not_reusable. Even in the
278 // keep_alive case, we've determined that the stream_ is not
279 // reusable if new_stream is NULL.
280 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58281 next_state_ = STATE_CREATE_STREAM;
282 } else {
sclittlefb249892015-09-10 21:33:22283 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19284 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22285 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58286 next_state_ = STATE_INIT_STREAM;
287 }
288 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32289 }
[email protected]f9ee6b52008-11-08 06:46:23290
291 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58292 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23293}
294
[email protected]8e6441ca2010-08-19 05:56:38295bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
296 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
297 HaveAuth(pending_auth_target_);
298}
299
[email protected]9dea9e1f2009-01-29 00:30:47300int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41301 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04302 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35303 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04304
[email protected]ad8e04a2010-11-01 04:16:27305 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38306 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04307 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29308 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04309 // bytes when establishing a tunnel because they might be controlled by
310 // an active network attacker. We don't worry about this for HTTP
311 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29312 // We reach this case when the user cancels a 407 proxy auth prompt. We
313 // also don't worry about this for an HTTPS Proxy, because the
314 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04315 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56316 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
317 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58318 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54319 LOG(WARNING) << "Blocked proxy response with status "
320 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15321 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04322 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44323 }
324
[email protected]e60e47a2010-07-14 03:37:18325 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15326 next_state_ = STATE_READ_BODY;
327
[email protected]96d570e42008-08-05 22:43:04328 read_buf_ = buf;
329 read_buf_len_ = buf_len;
330
[email protected]96d570e42008-08-05 22:43:04331 int rv = DoLoop(OK);
332 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41333 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04334 return rv;
335}
336
[email protected]8cd06c02014-01-25 07:50:14337void HttpNetworkTransaction::StopCaching() {}
338
[email protected]79e1fd62013-06-20 06:50:04339bool HttpNetworkTransaction::GetFullRequestHeaders(
340 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32341 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04342 *headers = request_headers_;
343 return true;
344}
345
sclittle4de1bab92015-09-22 21:28:24346int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
347 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19348 if (stream_)
349 total_received_bytes += stream_->GetTotalReceivedBytes();
350 return total_received_bytes;
351}
352
sclittlefb249892015-09-10 21:33:22353int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
354 int64_t total_sent_bytes = total_sent_bytes_;
355 if (stream_)
356 total_sent_bytes += stream_->GetTotalSentBytes();
357 return total_sent_bytes;
358}
359
[email protected]8cd06c02014-01-25 07:50:14360void HttpNetworkTransaction::DoneReading() {}
361
[email protected]96d570e42008-08-05 22:43:04362const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55363 return &response_;
[email protected]96d570e42008-08-05 22:43:04364}
365
366LoadState HttpNetworkTransaction::GetLoadState() const {
367 // TODO(wtc): Define a new LoadState value for the
368 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
369 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50370 case STATE_THROTTLE_COMPLETE:
371 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48372 case STATE_CREATE_STREAM:
373 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50374 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38375 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15376 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
377 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57378 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04379 return LOAD_STATE_SENDING_REQUEST;
380 case STATE_READ_HEADERS_COMPLETE:
381 return LOAD_STATE_WAITING_FOR_RESPONSE;
382 case STATE_READ_BODY_COMPLETE:
383 return LOAD_STATE_READING_RESPONSE;
384 default:
385 return LOAD_STATE_IDLE;
386 }
387}
388
[email protected]8cd06c02014-01-25 07:50:14389void HttpNetworkTransaction::SetQuicServerInfo(
390 QuicServerInfo* quic_server_info) {}
391
[email protected]5033ab82013-03-22 20:17:46392bool HttpNetworkTransaction::GetLoadTimingInfo(
393 LoadTimingInfo* load_timing_info) const {
394 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
395 return false;
396
397 load_timing_info->proxy_resolve_start =
398 proxy_info_.proxy_resolve_start_time();
399 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
400 load_timing_info->send_start = send_start_time_;
401 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46402 return true;
403}
404
ttuttled9dbc652015-09-29 20:00:59405bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36406 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59407 return false;
408
409 *endpoint = remote_endpoint_;
410 return true;
411}
412
zhongyi48704c182015-12-07 07:52:02413void HttpNetworkTransaction::PopulateNetErrorDetails(
414 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12415 *details = net_error_details_;
416 if (stream_)
417 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02418}
419
[email protected]5033ab82013-03-22 20:17:46420void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
421 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50422
rdsmith29dbad12017-02-17 02:22:18423 // TODO(rdsmith): Note that if any code indirectly executed by
424 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
425 // ever implements a throttling mechanism where changing a request's
426 // priority may cause a this or another request to synchronously succeed
427 // or fail, that callback could synchronously delete |*this|, causing
428 // a crash on return to this code.
429 //
430 // |throttle_->SetPriority()| has exactly the above attributes, which
431 // is why it's the last call in this function.
432
[email protected]bf828982013-08-14 18:01:47433 if (stream_request_)
434 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03435 if (stream_)
436 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50437
438 if (throttle_)
439 throttle_->SetPriority(priority);
440 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46441}
442
[email protected]831e4a32013-11-14 02:14:44443void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
444 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
445 websocket_handshake_stream_base_create_helper_ = create_helper;
446}
447
[email protected]1826a402014-01-08 15:40:48448void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
449 const BeforeNetworkStartCallback& callback) {
450 before_network_start_callback_ = callback;
451}
452
ryansturm49a8cb12016-06-15 16:51:09453void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
454 const BeforeHeadersSentCallback& callback) {
455 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27456}
457
Andrey Kosyakov83a6eee2017-08-14 19:20:04458void HttpNetworkTransaction::SetRequestHeadersCallback(
459 RequestHeadersCallback callback) {
460 DCHECK(!stream_);
461 request_headers_callback_ = std::move(callback);
462}
463
Andrey Kosyakov2e893e62017-08-31 17:00:52464void HttpNetworkTransaction::SetResponseHeadersCallback(
465 ResponseHeadersCallback callback) {
466 DCHECK(!stream_);
467 response_headers_callback_ = std::move(callback);
468}
469
[email protected]1826a402014-01-08 15:40:48470int HttpNetworkTransaction::ResumeNetworkStart() {
471 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
472 return DoLoop(OK);
473}
474
[email protected]102e27c2011-02-23 01:01:31475void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
476 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00477 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50478 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38479 DCHECK(stream_request_.get());
480
sclittlefb249892015-09-10 21:33:22481 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19482 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22483 total_sent_bytes_ += stream_->GetTotalSentBytes();
484 }
bnc5029f4632017-06-08 16:19:00485 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04486 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14487 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31488 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52489 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21490 response_.alpn_negotiated_protocol =
491 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38492 response_.was_fetched_via_spdy = stream_request_->using_spdy();
493 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15494 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47495 response_.proxy_server = proxy_info_.proxy_server();
496 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
497 response_.proxy_server = ProxyServer::Direct();
498 else
499 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38500 OnIOComplete(OK);
501}
502
xunjieli5749218c2016-03-22 16:43:06503void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08504 const SSLConfig& used_ssl_config,
505 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00506 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08507 NOTREACHED();
508}
509
[email protected]a9cf2b92013-10-30 12:08:49510void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50511 const SSLConfig& used_ssl_config,
512 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00513 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
514 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50515}
516
Ryan Hamilton75f197262017-08-17 14:00:07517void HttpNetworkTransaction::OnStreamFailed(
518 int result,
519 const NetErrorDetails& net_error_details,
520 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50521 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38522 DCHECK_NE(OK, result);
523 DCHECK(stream_request_.get());
524 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14525 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07526 net_error_details_ = net_error_details;
[email protected]8e6441ca2010-08-19 05:56:38527
528 OnIOComplete(result);
529}
530
[email protected]102e27c2011-02-23 01:01:31531void HttpNetworkTransaction::OnCertificateError(
532 int result,
533 const SSLConfig& used_ssl_config,
534 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50535 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38536 DCHECK_NE(OK, result);
537 DCHECK(stream_request_.get());
538 DCHECK(!stream_.get());
539
540 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14541 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38542
543 // TODO(mbelshe): For now, we're going to pass the error through, and that
544 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50545 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
546 // good and the user chooses to ignore the error. This is not ideal, but not
547 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38548
549 OnIOComplete(result);
550}
551
552void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50553 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31554 const SSLConfig& used_ssl_config,
555 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50556 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38557 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50558 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38559
560 establishing_tunnel_ = true;
561 response_.headers = proxy_response.headers;
562 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20563
564 if (response_.headers.get() && !ContentEncodingsValid()) {
565 DoCallback(ERR_CONTENT_DECODING_FAILED);
566 return;
567 }
568
[email protected]8e6441ca2010-08-19 05:56:38569 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14570 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31571 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38572
573 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
574 pending_auth_target_ = HttpAuth::AUTH_PROXY;
575
576 DoCallback(OK);
577}
578
579void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31580 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50581 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50582 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38583
[email protected]102957f2011-09-02 17:10:14584 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38585 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58586 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38587}
588
[email protected]511f6f52010-12-17 03:58:29589void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
590 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31591 const SSLConfig& used_ssl_config,
592 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01593 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29594 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
595
ttuttle1f2d7e92015-04-28 16:17:47596 CopyConnectionAttemptsFromStreamRequest();
597
[email protected]511f6f52010-12-17 03:58:29598 headers_valid_ = true;
599 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14600 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31601 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22602 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19603 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22604 total_sent_bytes_ += stream_->GetTotalSentBytes();
605 }
Bence Béky6d05ebd2017-05-16 00:09:01606 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04607 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29608 stream_request_.reset(); // we're done with the stream request
609 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
610}
611
zhongyi48704c182015-12-07 07:52:02612void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12613 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02614}
615
ttuttle1f2d7e92015-04-28 16:17:47616void HttpNetworkTransaction::GetConnectionAttempts(
617 ConnectionAttempts* out) const {
618 *out = connection_attempts_;
619}
620
rdsmithbf8c3c12016-11-18 18:16:24621void HttpNetworkTransaction::OnThrottleUnblocked(
622 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50623 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24624 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50625 // be so in the future.
626 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
627
628 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
629
630 DoLoop(OK);
631}
632
Adam Ricecb76ac62015-02-20 05:33:25633bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37634 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52635}
636
nharperb7441ef2016-01-25 23:54:14637bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
638 if (!IsSecureRequest())
639 return false;
640 SSLInfo ssl_info;
641 stream_->GetSSLInfo(&ssl_info);
642 return ssl_info.token_binding_negotiated &&
643 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
mmenke6ddfbea2017-05-31 21:48:41644 session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14645}
646
647void HttpNetworkTransaction::RecordTokenBindingSupport() const {
648 // This enum is used for an UMA histogram - do not change or re-use values.
649 enum {
650 DISABLED = 0,
651 CLIENT_ONLY = 1,
652 CLIENT_AND_SERVER = 2,
653 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
654 TOKEN_BINDING_SUPPORT_MAX
655 } supported;
656 if (!IsSecureRequest())
657 return;
658 SSLInfo ssl_info;
659 stream_->GetSSLInfo(&ssl_info);
660 if (!session_->params().enable_token_binding) {
661 supported = DISABLED;
mmenke6ddfbea2017-05-31 21:48:41662 } else if (!session_->context().channel_id_service) {
nharperb7441ef2016-01-25 23:54:14663 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
664 } else if (ssl_info.token_binding_negotiated) {
665 supported = CLIENT_AND_SERVER;
666 } else {
667 supported = CLIENT_ONLY;
668 }
669 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
670 TOKEN_BINDING_SUPPORT_MAX);
671}
672
Adam Rice425cf122015-01-19 06:18:24673bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12674 return (proxy_info_.is_http() || proxy_info_.is_https() ||
675 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24676 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
677}
678
initial.commit586acc5fe2008-07-26 22:42:52679void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50680 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41681 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52682
[email protected]96d570e42008-08-05 22:43:04683 // Since Run may result in Read being called, clear user_callback_ up front.
Bence Békybc1de292018-02-01 15:48:03684 base::ResetAndReturn(&callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52685}
686
687void HttpNetworkTransaction::OnIOComplete(int result) {
688 int rv = DoLoop(result);
689 if (rv != ERR_IO_PENDING)
690 DoCallback(rv);
691}
692
693int HttpNetworkTransaction::DoLoop(int result) {
694 DCHECK(next_state_ != STATE_NONE);
695
696 int rv = result;
697 do {
698 State state = next_state_;
699 next_state_ = STATE_NONE;
700 switch (state) {
rdsmith1d343be52016-10-21 20:37:50701 case STATE_THROTTLE:
702 DCHECK_EQ(OK, rv);
703 rv = DoThrottle();
704 break;
705 case STATE_THROTTLE_COMPLETE:
706 DCHECK_EQ(OK, rv);
707 rv = DoThrottleComplete();
708 break;
[email protected]1826a402014-01-08 15:40:48709 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
710 DCHECK_EQ(OK, rv);
711 rv = DoNotifyBeforeCreateStream();
712 break;
[email protected]82918cc2010-08-25 17:24:50713 case STATE_CREATE_STREAM:
714 DCHECK_EQ(OK, rv);
715 rv = DoCreateStream();
716 break;
717 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46718 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
719 // solved.
720 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50721 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46722 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50723 break;
[email protected]351ab642010-08-05 16:55:31724 case STATE_INIT_STREAM:
725 DCHECK_EQ(OK, rv);
726 rv = DoInitStream();
727 break;
728 case STATE_INIT_STREAM_COMPLETE:
729 rv = DoInitStreamComplete(rv);
730 break;
[email protected]044de0642010-06-17 10:42:15731 case STATE_GENERATE_PROXY_AUTH_TOKEN:
732 DCHECK_EQ(OK, rv);
733 rv = DoGenerateProxyAuthToken();
734 break;
735 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
736 rv = DoGenerateProxyAuthTokenComplete(rv);
737 break;
738 case STATE_GENERATE_SERVER_AUTH_TOKEN:
739 DCHECK_EQ(OK, rv);
740 rv = DoGenerateServerAuthToken();
741 break;
742 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
743 rv = DoGenerateServerAuthTokenComplete(rv);
744 break;
nharperd6e65822016-03-30 23:05:48745 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14746 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48747 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14748 break;
nharperd6e65822016-03-30 23:05:48749 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
750 rv = DoGetProvidedTokenBindingKeyComplete(rv);
751 break;
752 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
753 DCHECK_EQ(OK, rv);
754 rv = DoGetReferredTokenBindingKey();
755 break;
756 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
757 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14758 break;
[email protected]daddea62012-09-19 05:51:13759 case STATE_INIT_REQUEST_BODY:
760 DCHECK_EQ(OK, rv);
761 rv = DoInitRequestBody();
762 break;
763 case STATE_INIT_REQUEST_BODY_COMPLETE:
764 rv = DoInitRequestBodyComplete(rv);
765 break;
[email protected]4875ba12011-03-30 22:31:51766 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55767 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00768 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51769 rv = DoBuildRequest();
770 break;
771 case STATE_BUILD_REQUEST_COMPLETE:
772 rv = DoBuildRequestComplete(rv);
773 break;
774 case STATE_SEND_REQUEST:
775 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57776 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52777 break;
[email protected]0877e3d2009-10-17 22:29:57778 case STATE_SEND_REQUEST_COMPLETE:
779 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43780 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00781 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52782 break;
783 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55784 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00785 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52786 rv = DoReadHeaders();
787 break;
788 case STATE_READ_HEADERS_COMPLETE:
789 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43790 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00791 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52792 break;
793 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55794 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00795 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52796 rv = DoReadBody();
797 break;
798 case STATE_READ_BODY_COMPLETE:
799 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43800 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00801 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52802 break;
[email protected]2d2697f92009-02-18 21:00:32803 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55804 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53805 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00806 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32807 rv = DoDrainBodyForAuthRestart();
808 break;
809 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
810 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43811 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00812 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32813 break;
initial.commit586acc5fe2008-07-26 22:42:52814 default:
815 NOTREACHED() << "bad state";
816 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04817 break;
initial.commit586acc5fe2008-07-26 22:42:52818 }
819 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
820
821 return rv;
822}
823
rdsmith1d343be52016-10-21 20:37:50824int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24825 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50826 throttle_ = session_->throttler()->CreateThrottle(
827 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
828 next_state_ = STATE_THROTTLE_COMPLETE;
829
rdsmithbf8c3c12016-11-18 18:16:24830 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50831 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
832 return ERR_IO_PENDING;
833 }
834
835 return OK;
836}
837
838int HttpNetworkTransaction::DoThrottleComplete() {
839 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24840 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50841
842 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
843
844 return OK;
845}
846
[email protected]1826a402014-01-08 15:40:48847int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
848 next_state_ = STATE_CREATE_STREAM;
849 bool defer = false;
850 if (!before_network_start_callback_.is_null())
851 before_network_start_callback_.Run(&defer);
852 if (!defer)
853 return OK;
854 return ERR_IO_PENDING;
855}
856
[email protected]82918cc2010-08-25 17:24:50857int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55858 response_.network_accessed = true;
859
[email protected]82918cc2010-08-25 17:24:50860 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17861 // IP based pooling is only enabled on a retry after 421 Misdirected Request
862 // is received. Alternative Services are also disabled in this case (though
863 // they can also be disabled when retrying after a QUIC error).
864 if (!enable_ip_based_pooling_)
865 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44866 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27867 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06868 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
869 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
870 websocket_handshake_stream_base_create_helper_,
871 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44872 } else {
xunjieli96f2a402017-06-05 17:24:27873 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29874 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27875 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44876 }
[email protected]26816882010-10-14 18:03:09877 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38878 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31879}
880
[email protected]82918cc2010-08-25 17:24:50881int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47882 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
883 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
884 // which resets the stream request first. Therefore, we have to grab the
885 // connection attempts in *that* function instead of here in that case.
886 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
887 CopyConnectionAttemptsFromStreamRequest();
888
[email protected]394816e92010-08-03 07:38:59889 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50890 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38891 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33892 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
893 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29894 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
895 // Return OK and let the caller read the proxy's error page
896 next_state_ = STATE_NONE;
897 return OK;
bncfacdd852015-01-09 19:22:54898 } else if (result == ERR_HTTP_1_1_REQUIRED ||
899 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
900 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59901 }
902
David Benjamin76a40ad2018-02-24 22:22:08903 // Handle possible client certificate errors that may have occurred if the
904 // stream used SSL for one or more of the layers.
905 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30906
[email protected]8e6441ca2010-08-19 05:56:38907 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09908 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38909 return result;
[email protected]394816e92010-08-03 07:38:59910}
911
[email protected]82918cc2010-08-25 17:24:50912int HttpNetworkTransaction::DoInitStream() {
913 DCHECK(stream_.get());
914 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59915
916 stream_->GetRemoteEndpoint(&remote_endpoint_);
917
Steven Valdezb4ff0412018-01-18 22:39:27918 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
919 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50920}
921
922int HttpNetworkTransaction::DoInitStreamComplete(int result) {
923 if (result == OK) {
924 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50925 } else {
[email protected]82918cc2010-08-25 17:24:50926 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26927 result = HandleIOError(result);
928
929 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22930 if (stream_) {
931 total_received_bytes_ += stream_->GetTotalReceivedBytes();
932 total_sent_bytes_ += stream_->GetTotalSentBytes();
933 }
zhongyica364fbb2015-12-12 03:39:12934 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50935 }
936
937 return result;
938}
939
[email protected]044de0642010-06-17 10:42:15940int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
941 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
942 if (!ShouldApplyProxyAuth())
943 return OK;
[email protected]394816e92010-08-03 07:38:59944 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
945 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09946 auth_controllers_[target] =
947 new HttpAuthController(target,
948 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31949 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09950 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59951 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41952 io_callback_,
[email protected]394816e92010-08-03 07:38:59953 net_log_);
[email protected]044de0642010-06-17 10:42:15954}
955
956int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
957 DCHECK_NE(ERR_IO_PENDING, rv);
958 if (rv == OK)
959 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
960 return rv;
961}
962
963int HttpNetworkTransaction::DoGenerateServerAuthToken() {
964 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59965 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54966 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09967 auth_controllers_[target] =
968 new HttpAuthController(target,
969 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31970 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09971 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54972 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
973 auth_controllers_[target]->DisableEmbeddedIdentity();
974 }
[email protected]044de0642010-06-17 10:42:15975 if (!ShouldApplyServerAuth())
976 return OK;
[email protected]394816e92010-08-03 07:38:59977 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41978 io_callback_,
[email protected]394816e92010-08-03 07:38:59979 net_log_);
[email protected]044de0642010-06-17 10:42:15980}
981
982int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
983 DCHECK_NE(ERR_IO_PENDING, rv);
984 if (rv == OK)
nharperd6e65822016-03-30 23:05:48985 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15986 return rv;
987}
988
nharperd6e65822016-03-30 23:05:48989int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
990 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14991 if (!IsTokenBindingEnabled())
992 return OK;
993
mikecirone8b85c432016-09-08 19:11:00994 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:41995 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14996 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48997 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14998 &token_binding_request_);
999}
1000
nharperd6e65822016-03-30 23:05:481001int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:141002 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:481003 if (IsTokenBindingEnabled()) {
1004 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001005 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481006 }
1007
1008 if (rv == OK)
1009 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
1010 return rv;
1011}
1012
1013int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1014 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1015 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141016 return OK;
1017
mikecirone8b85c432016-09-08 19:11:001018 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:411019 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperd6e65822016-03-30 23:05:481020 return channel_id_service->GetOrCreateChannelID(
1021 request_->token_binding_referrer, &referred_token_binding_key_,
1022 io_callback_, &token_binding_request_);
1023}
1024
1025int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1026 DCHECK_NE(ERR_IO_PENDING, rv);
1027 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1028 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001029 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481030 }
1031 if (rv == OK)
1032 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141033 return rv;
1034}
1035
1036int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241037 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141038 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1039 GetHostAndOptionalPort(request_->url));
1040
1041 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241042 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141043 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1044 "keep-alive");
1045 } else {
1046 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1047 }
1048
[email protected]2979a492011-04-06 00:29:141049 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111050 if (request_->upload_data_stream) {
1051 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141052 request_headers_.SetHeader(
1053 HttpRequestHeaders::kTransferEncoding, "chunked");
1054 } else {
1055 request_headers_.SetHeader(
1056 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:091057 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141058 }
csharrisonf473dd192015-08-18 13:54:131059 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141060 // An empty POST/PUT request still needs a content length. As for HEAD,
1061 // IE and Safari also add a content length header. Presumably it is to
1062 // support sending a HEAD request to an URL that only expects to be sent a
1063 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131064 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1065 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141066 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1067 }
1068
nharperb7441ef2016-01-25 23:54:141069 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481070 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141071 std::string token_binding_header;
1072 int rv = BuildTokenBindingHeader(&token_binding_header);
1073 if (rv != OK)
1074 return rv;
1075 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1076 token_binding_header);
1077 }
1078
[email protected]2979a492011-04-06 00:29:141079 // Honor load flags that impact proxy caches.
1080 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1081 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1082 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1083 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1084 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1085 }
1086
1087 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1088 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1089 &request_headers_);
1090 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1091 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1092 &request_headers_);
1093
[email protected]c10450102011-06-27 09:06:161094 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201095
ryansturm49a8cb12016-06-15 16:51:091096 if (!before_headers_sent_callback_.is_null())
1097 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201098
[email protected]173f8e22013-04-10 04:18:201099 response_.did_use_http_auth =
1100 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1101 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141102 return OK;
1103}
1104
1105int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471106 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141107 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351108 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1109 TokenBindingType::PROVIDED,
1110 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141111 if (rv != OK)
1112 return rv;
1113 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481114 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1115 provided_token_binding_key_.get(), signed_ekm,
1116 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141117 if (rv != OK)
1118 return rv;
nharperd6e65822016-03-30 23:05:481119
nharperb7441ef2016-01-25 23:54:141120 std::vector<base::StringPiece> token_bindings;
1121 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481122
1123 std::string referred_token_binding;
1124 if (referred_token_binding_key_) {
1125 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351126 int rv = stream_->GetTokenBindingSignature(
1127 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1128 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481129 if (rv != OK)
1130 return rv;
1131 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1132 referred_token_binding_key_.get(),
1133 referred_signed_ekm, &referred_token_binding);
1134 if (rv != OK)
1135 return rv;
1136 token_bindings.push_back(referred_token_binding);
1137 }
nharperb7441ef2016-01-25 23:54:141138 std::string header;
1139 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1140 if (rv != OK)
1141 return rv;
nharper78e6d2b2016-09-21 05:42:351142 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471143 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1144 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1145 header_creation_time,
1146 base::TimeDelta::FromMilliseconds(1),
1147 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141148 return OK;
[email protected]2979a492011-04-06 00:29:141149}
1150
[email protected]daddea62012-09-19 05:51:131151int HttpNetworkTransaction::DoInitRequestBody() {
1152 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131153 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111154 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331155 rv = request_->upload_data_stream->Init(
1156 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1157 base::Unretained(this)),
1158 net_log_);
[email protected]daddea62012-09-19 05:51:131159 return rv;
1160}
[email protected]4875ba12011-03-30 22:31:511161
[email protected]daddea62012-09-19 05:51:131162int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1163 if (result == OK)
1164 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131165 return result;
1166}
1167
1168int HttpNetworkTransaction::DoBuildRequest() {
1169 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511170 headers_valid_ = false;
1171
1172 // This is constructed lazily (instead of within our Start method), so that
1173 // we have proxy info available.
1174 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241175 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141176 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511177 }
1178
[email protected]4875ba12011-03-30 22:31:511179 return OK;
1180}
1181
1182int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241183 if (result == OK)
1184 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511185 return result;
1186}
1187
[email protected]0877e3d2009-10-17 22:29:571188int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251189 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571190 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1191
[email protected]bf3eb002012-11-15 05:50:111192 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521193}
1194
[email protected]0877e3d2009-10-17 22:29:571195int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251196 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361197
1198 if (result == ERR_HTTP_1_1_REQUIRED ||
1199 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1200 return HandleHttp11Required(result);
1201 }
1202
initial.commit586acc5fe2008-07-26 22:42:521203 if (result < 0)
1204 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571205 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521206 return OK;
1207}
1208
1209int HttpNetworkTransaction::DoReadHeaders() {
1210 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411211 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521212}
1213
1214int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111215 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1216 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381217 if (IsCertificateError(result)) {
1218 // We don't handle a certificate error during SSL renegotiation, so we
1219 // have to return an error that's not in the certificate error range
1220 // (-2xx).
1221 LOG(ERROR) << "Got a server certificate with error " << result
1222 << " during SSL renegotiation";
1223 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1224 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1225 // TODO(wtc): Need a test case for this code path!
1226 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251227 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381228 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501229 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381230 result = HandleCertificateRequest(result);
1231 if (result == OK)
1232 return result;
[email protected]2181ea002009-06-09 01:37:271233 }
1234
bncfacdd852015-01-09 19:22:541235 if (result == ERR_HTTP_1_1_REQUIRED ||
1236 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1237 return HandleHttp11Required(result);
1238 }
1239
[email protected]c871864d72014-03-13 19:56:191240 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1241 // response headers were received, we do the best we can to make sense of it
1242 // and send it back up the stack.
1243 //
1244 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1245 // bizarre for SPDY. Assuming this logic is useful at all.
1246 // TODO(davidben): Bubble the error code up so we do not cache?
1247 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1248 result = OK;
1249
1250 if (result < 0)
1251 return HandleIOError(result);
1252
[email protected]90499482013-06-01 00:39:501253 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521254
eustasc7d27da2017-04-06 10:33:201255 if (response_.headers.get() && !ContentEncodingsValid())
1256 return ERR_CONTENT_DECODING_FAILED;
1257
[email protected]d58ceea82014-06-04 10:55:541258 // On a 408 response from the server ("Request Timeout") on a stale socket,
1259 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041260 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181261 if (response_.headers.get() &&
1262 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541263 stream_->IsConnectionReused()) {
1264 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001265 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541266 response_.headers->response_code());
1267 // This will close the socket - it would be weird to try and reuse it, even
1268 // if the server doesn't actually close it.
1269 ResetConnectionAndRequestForResend();
1270 return OK;
1271 }
1272
[email protected]93f8b562012-03-27 01:00:161273 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211274 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161275 const int response_code = response_.headers->response_code();
1276 UMA_HISTOGRAM_ENUMERATION(
1277 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1278 }
1279
[email protected]3abacd62012-06-10 20:20:321280 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001281 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321282 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521283 if (response_headers_callback_)
1284 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391285
bncbe0f6af2015-10-15 17:49:561286 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571287 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1288 // indicates a buggy server. See:
1289 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1290 if (request_->method == "PUT")
1291 return ERR_METHOD_NOT_SUPPORTED;
1292 }
[email protected]4ddaf2502008-10-23 18:26:191293
[email protected]0877e3d2009-10-17 22:29:571294 // Check for an intermediate 100 Continue response. An origin server is
1295 // allowed to send this response even if we didn't ask for it, so we just
1296 // need to skip over it.
1297 // We treat any other 1xx in this same way (although in practice getting
1298 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441299 // Unless this is a WebSocket request, in which case we pass it on up.
1300 if (response_.headers->response_code() / 100 == 1 &&
1301 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451302 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571303 next_state_ = STATE_READ_HEADERS;
1304 return OK;
1305 }
1306
davidbence688ae2017-05-04 15:12:591307 if (response_.headers->response_code() == 421 &&
1308 (enable_ip_based_pooling_ || enable_alternative_services_)) {
1309 // Retry the request with both IP based pooling and Alternative Services
1310 // disabled.
1311 enable_ip_based_pooling_ = false;
1312 enable_alternative_services_ = false;
1313 net_log_.AddEvent(
1314 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1315 ResetConnectionAndRequestForResend();
1316 return OK;
bnc8016c1f2017-03-31 02:11:291317 }
1318
bncb26024382016-06-29 02:39:451319 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491320 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081321 if (response_.ssl_info.is_valid() &&
1322 !IsCertStatusError(response_.ssl_info.cert_status)) {
1323 session_->http_stream_factory()->ProcessAlternativeServices(
1324 session_, response_.headers.get(),
1325 url::SchemeHostPort(request_->url));
1326 }
1327 }
asanka5ffd5d72016-03-23 16:20:491328
[email protected]e772db3f2010-07-12 18:11:131329 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571330 if (rv != OK)
1331 return rv;
1332
[email protected]0877e3d2009-10-17 22:29:571333 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261334
1335 // We have reached the end of Start state machine, set the RequestInfo to
1336 // null.
1337 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1338 // only until the final response headers are received. Clearing it will ensure
1339 // that HttpRequestInfo is only used up until final response headers are
1340 // received. Clearing is allowed so that the transaction can be disassociated
1341 // from its creating consumer in cases where it is shared for writing to the
1342 // cache. It is also safe to set it to null at this point since
1343 // upload_data_stream is also not used in the Read state machine.
1344 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1345 request_ = nullptr;
1346
[email protected]0877e3d2009-10-17 22:29:571347 return OK;
initial.commit586acc5fe2008-07-26 22:42:521348}
1349
1350int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501351 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131352 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381353 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521354
1355 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501356 return stream_->ReadResponseBody(
1357 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521358}
1359
1360int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1361 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381362 bool done = false;
1363 if (result <= 0) {
1364 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521365 done = true;
[email protected]8e6441ca2010-08-19 05:56:381366 }
[email protected]9492e4a2010-02-24 00:58:461367
mmenkebd84c392015-09-02 14:12:341368 // Clean up connection if we are done.
1369 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381370 // Note: Just because IsResponseBodyComplete is true, we're not
1371 // necessarily "done". We're only "done" when it is the last
1372 // read on this HttpNetworkTransaction, which will be signified
1373 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341374 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381375 // the stream. No need to compute it here just to pass back
1376 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341377 bool keep_alive =
1378 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521379
[email protected]8e6441ca2010-08-19 05:56:381380 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011381 // Note: we don't reset the stream here. We've closed it, but we still
1382 // need it around so that callers can call methods such as
1383 // GetUploadProgress() and have them be meaningful.
1384 // TODO(mbelshe): This means we closed the stream here, and we close it
1385 // again in ~HttpNetworkTransaction. Clean that up.
1386
[email protected]8e6441ca2010-08-19 05:56:381387 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171388
1389 // This transaction was successful. If it had been retried because of an
1390 // error with an alternative service, mark that alternative service broken.
1391 if (!enable_alternative_services_ &&
1392 retried_alternative_service_.protocol != kProtoUnknown) {
1393 session_->http_server_properties()->MarkAlternativeServiceBroken(
1394 retried_alternative_service_);
1395 }
initial.commit586acc5fe2008-07-26 22:42:521396 }
1397
1398 // Clear these to avoid leaving around old state.
1399 read_buf_ = NULL;
1400 read_buf_len_ = 0;
1401
1402 return result;
1403}
1404
[email protected]2d2697f92009-02-18 21:00:321405int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1406 // This method differs from DoReadBody only in the next_state_. So we just
1407 // call DoReadBody and override the next_state_. Perhaps there is a more
1408 // elegant way for these two methods to share code.
1409 int rv = DoReadBody();
1410 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1411 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1412 return rv;
1413}
1414
[email protected]0877e3d2009-10-17 22:29:571415// TODO(wtc): This method and the DoReadBodyComplete method are almost
1416// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321417int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231418 // keep_alive defaults to true because the very reason we're draining the
1419 // response body is to reuse the connection for auth restart.
1420 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321421 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571422 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321423 done = true;
[email protected]68873ba2009-06-04 21:49:231424 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311425 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571426 done = true;
[email protected]2d2697f92009-02-18 21:00:321427 }
1428
1429 if (done) {
1430 DidDrainBodyForAuthRestart(keep_alive);
1431 } else {
1432 // Keep draining.
1433 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1434 }
1435
1436 return OK;
1437}
1438
[email protected]5e363962009-06-19 19:57:011439int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381440 // There are two paths through which the server can request a certificate
1441 // from us. The first is during the initial handshake, the second is
1442 // during SSL renegotiation.
1443 //
1444 // In both cases, we want to close the connection before proceeding.
1445 // We do this for two reasons:
1446 // First, we don't want to keep the connection to the server hung for a
1447 // long time while the user selects a certificate.
1448 // Second, even if we did keep the connection open, NSS has a bug where
1449 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581450 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381451
1452 if (stream_.get()) {
1453 // Since we already have a stream, we're being called as part of SSL
1454 // renegotiation.
1455 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191456 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221457 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381458 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121459 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381460 }
1461
[email protected]26816882010-10-14 18:03:091462 // The server is asking for a client certificate during the initial
1463 // handshake.
1464 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011465
[email protected]ec229bc92010-11-22 09:51:451466 // If the user selected one of the certificates in client_certs or declined
1467 // to provide one for this server before, use the past decision
1468 // automatically.
1469 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541470 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451471 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541472 response_.cert_request_info->host_and_port, &client_cert,
1473 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451474 if (!found_cached_cert)
1475 return error;
1476
1477 // Check that the certificate selected is still a certificate the server
1478 // is likely to accept, based on the criteria supplied in the
1479 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501480 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261481 const std::vector<std::string>& cert_authorities =
1482 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451483
[email protected]f1958c382013-02-07 00:15:261484 bool cert_still_valid = cert_authorities.empty() ||
1485 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451486 if (!cert_still_valid)
1487 return error;
[email protected]5e363962009-06-19 19:57:011488 }
[email protected]ec229bc92010-11-22 09:51:451489
David Benjamin76a40ad2018-02-24 22:22:081490 if (!response_.cert_request_info->is_proxy) {
1491 server_ssl_client_cert_was_cached_ = true;
1492 }
1493
[email protected]ec229bc92010-11-22 09:51:451494 // TODO(davidben): Add a unit test which covers this path; we need to be
1495 // able to send a legitimate certificate and also bypass/clear the
1496 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141497 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1498 &proxy_ssl_config_ : &server_ssl_config_;
1499 ssl_config->send_client_cert = true;
1500 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541501 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451502 next_state_ = STATE_CREATE_STREAM;
1503 // Reset the other member variables.
1504 // Note: this is necessary only with SSL renegotiation.
1505 ResetStateForRestart();
1506 return OK;
[email protected]0b45559b2009-06-12 21:45:111507}
1508
bncfacdd852015-01-09 19:22:541509int HttpNetworkTransaction::HandleHttp11Required(int error) {
1510 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1511 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1512
1513 if (error == ERR_HTTP_1_1_REQUIRED) {
1514 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1515 } else {
1516 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1517 }
1518 ResetConnectionAndRequestForResend();
1519 return OK;
1520}
1521
David Benjamin76a40ad2018-02-24 22:22:081522int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
1523 // TODO(davidben): This does handle client certificate errors from the
1524 // proxy. https://crbug.com/814911.
[email protected]384cc73a2013-12-08 22:41:031525 if (server_ssl_config_.send_client_cert &&
1526 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1527 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411528 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031529
David Benjamin76a40ad2018-02-24 22:22:081530 // The private key handle may have gone stale due to, e.g., the user
1531 // unplugging their smartcard. Operating systems do not provide reliable
1532 // notifications for this, so if the signature failed and the private key
1533 // came from SSLClientAuthCache, retry to ask the user for a new one.
1534 if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1535 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1536 server_ssl_client_cert_was_cached_ = false;
1537 server_ssl_config_.send_client_cert = false;
1538 server_ssl_config_.client_cert = nullptr;
1539 server_ssl_config_.client_private_key = nullptr;
1540 retry_attempts_++;
1541 net_log_.AddEventWithNetErrorCode(
1542 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1543 ResetConnectionAndRequestForResend();
1544 return OK;
1545 }
1546 }
initial.commit586acc5fe2008-07-26 22:42:521547 return error;
1548}
1549
[email protected]bd0b6772011-01-11 19:59:301550// This method determines whether it is safe to resend the request after an
1551// IO error. It can only be called in response to request header or body
1552// write errors or response header read errors. It should not be used in
1553// other cases, such as a Connect error.
1554int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031555 // Because the peer may request renegotiation with client authentication at
1556 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081557 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301558
1559 switch (error) {
1560 // If we try to reuse a connection that the server is in the process of
1561 // closing, we may end up successfully writing out our request (or a
1562 // portion of our request) only to find a connection error when we try to
1563 // read from (or finish writing to) the socket.
1564 case ERR_CONNECTION_RESET:
1565 case ERR_CONNECTION_CLOSED:
1566 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511567 // There can be a race between the socket pool checking checking whether a
1568 // socket is still connected, receiving the FIN, and sending/reading data
1569 // on a reused socket. If we receive the FIN between the connectedness
1570 // check and writing/reading from the socket, we may first learn the socket
1571 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1572 // likely happen when trying to retrieve its IP address.
1573 // See http://crbug.com/105824 for more details.
1574 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491575 // If a socket is closed on its initial request, HttpStreamParser returns
1576 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1577 // preconnected but failed to be used before the server timed it out.
1578 case ERR_EMPTY_RESPONSE:
1579 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381580 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001581 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301582 ResetConnectionAndRequestForResend();
1583 error = OK;
1584 }
1585 break;
[email protected]8753a122011-10-16 08:05:081586 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001587 case ERR_SPDY_SERVER_REFUSED_STREAM:
Bence Béky1a5d8562018-01-05 17:29:281588 case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE:
Yoav Weiss9693572f2018-01-04 09:37:341589 case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
[email protected]4d283b32013-10-17 12:57:271590 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141591 if (HasExceededMaxRetries())
1592 break;
[email protected]b6754252012-06-13 23:14:381593 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001594 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141595 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001596 ResetConnectionAndRequestForResend();
1597 error = OK;
1598 break;
rch2f2991c2017-04-13 19:28:171599 case ERR_QUIC_PROTOCOL_ERROR:
1600 if (GetResponseHeaders() != nullptr ||
1601 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1602 // If the response headers have already been recieved and passed up
1603 // then the request can not be retried. Also, if there was no
1604 // alternative service used for this request, then there is no
1605 // alternative service to be disabled.
1606 break;
1607 }
Biljith Jayan45a41722017-08-16 18:43:141608 if (HasExceededMaxRetries())
1609 break;
rch2f2991c2017-04-13 19:28:171610 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1611 retried_alternative_service_)) {
1612 // If the alternative service was marked as broken while the request
1613 // was in flight, retry the request which will not use the broken
1614 // alternative service.
1615 net_log_.AddEventWithNetErrorCode(
1616 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141617 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171618 ResetConnectionAndRequestForResend();
1619 error = OK;
1620 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1621 // Disable alternative services for this request and retry it. If the
1622 // retry succeeds, then the alternative service will be marked as
1623 // broken then.
1624 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361625 net_log_.AddEventWithNetErrorCode(
1626 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141627 retry_attempts_++;
rch514a44a82017-04-13 04:46:361628 ResetConnectionAndRequestForResend();
1629 error = OK;
1630 }
1631 break;
[email protected]bd0b6772011-01-11 19:59:301632 }
1633 return error;
1634}
1635
[email protected]c3b35c22008-09-27 03:19:421636void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581637 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221638 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191639 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221640 total_sent_bytes_ += stream_->GetTotalSentBytes();
1641 }
zhongyica364fbb2015-12-12 03:39:121642 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581643}
1644
1645void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251646 send_start_time_ = base::TimeTicks();
1647 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251648
[email protected]0757e7702009-03-27 04:00:221649 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421650 read_buf_ = NULL;
1651 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571652 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201653 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141654 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381655 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591656 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121657 net_error_details_.quic_broken = false;
1658 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481659 provided_token_binding_key_.reset();
1660 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121661}
1662
1663void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1664 if (stream_)
1665 stream_->PopulateNetErrorDetails(&net_error_details_);
1666 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571667}
1668
[email protected]0877e3d2009-10-17 22:29:571669HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501670 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421671}
1672
[email protected]a34f61ee2014-03-18 20:59:491673bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381674 bool connection_is_proven = stream_->IsConnectionReused();
1675 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161676
[email protected]2a5c76b2008-09-25 22:15:161677 // NOTE: we resend a request only if we reused a keep-alive connection.
1678 // This automatically prevents an infinite resend loop because we'll run
1679 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381680 if (connection_is_proven && !has_received_headers)
1681 return true;
1682 return false;
[email protected]1c773ea12009-04-28 19:58:421683}
1684
Biljith Jayan45a41722017-08-16 18:43:141685bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1686 return (retry_attempts_ >= kMaxRetryAttempts);
1687}
1688
[email protected]1c773ea12009-04-28 19:58:421689void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381690 if (stream_.get()) {
1691 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121692 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161693 }
1694
[email protected]0877e3d2009-10-17 22:29:571695 // We need to clear request_headers_ because it contains the real request
1696 // headers, but we may need to resend the CONNECT request first to recreate
1697 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201698 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501699 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541700}
1701
[email protected]1c773ea12009-04-28 19:58:421702bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241703 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421704}
license.botbf09a502008-08-24 00:55:551705
[email protected]1c773ea12009-04-28 19:58:421706bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041707 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421708}
1709
[email protected]e772db3f2010-07-12 18:11:131710int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271711 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501712 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421713
[email protected]0877e3d2009-10-17 22:29:571714 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581715 if (status != HTTP_UNAUTHORIZED &&
1716 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421717 return OK;
[email protected]9094b602012-02-27 21:44:581718 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111719 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161720 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1721 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421722
[email protected]9094b602012-02-27 21:44:581723 // This case can trigger when an HTTPS server responds with a "Proxy
1724 // authentication required" status code through a non-authenticating
1725 // proxy.
[email protected]7a67a8152010-11-05 18:31:101726 if (!auth_controllers_[target].get())
1727 return ERR_UNEXPECTED_PROXY_AUTH;
1728
[email protected]a7ea8832010-07-12 17:54:541729 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491730 headers, response_.ssl_info,
1731 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311732 net_log_);
[email protected]228404f2010-06-24 04:31:411733 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491734 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411735
1736 scoped_refptr<AuthChallengeInfo> auth_info =
1737 auth_controllers_[target]->auth_info();
1738 if (auth_info.get())
1739 response_.auth_challenge = auth_info;
1740
[email protected]228404f2010-06-24 04:31:411741 return rv;
[email protected]f9ee6b52008-11-08 06:46:231742}
1743
[email protected]8e6441ca2010-08-19 05:56:381744bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1745 return auth_controllers_[target].get() &&
1746 auth_controllers_[target]->HaveAuth();
1747}
1748
[email protected]228404f2010-06-24 04:31:411749GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1750 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461751 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411752 if (!proxy_info_.proxy_server().is_valid() ||
1753 proxy_info_.proxy_server().is_direct()) {
1754 return GURL(); // There is no proxy server.
1755 }
[email protected]2df19bb2010-08-25 20:13:461756 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1757 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351758 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461759 }
[email protected]228404f2010-06-24 04:31:411760 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291761 if (ForWebSocketHandshake()) {
1762 const GURL& url = request_->url;
1763 url::Replacements<char> ws_to_http;
1764 if (url.SchemeIs("ws")) {
1765 ws_to_http.SetScheme("http", url::Component(0, 4));
1766 } else {
1767 DCHECK(url.SchemeIs("wss"));
1768 ws_to_http.SetScheme("https", url::Component(0, 5));
1769 }
1770 return url.ReplaceComponents(ws_to_http);
1771 }
[email protected]228404f2010-06-24 04:31:411772 return request_->url;
1773 default:
1774 return GURL();
1775 }
[email protected]c3b35c22008-09-27 03:19:421776}
1777
[email protected]831e4a32013-11-14 02:14:441778bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141779 return websocket_handshake_stream_base_create_helper_ &&
1780 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441781}
1782
ttuttle1f2d7e92015-04-28 16:17:471783void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1784 DCHECK(stream_request_);
1785
1786 // Since the transaction can restart with auth credentials, it may create a
1787 // stream more than once. Accumulate all of the connection attempts across
1788 // those streams by appending them to the vector:
1789 for (const auto& attempt : stream_request_->connection_attempts())
1790 connection_attempts_.push_back(attempt);
1791}
1792
eustasc7d27da2017-04-06 10:33:201793bool HttpNetworkTransaction::ContentEncodingsValid() const {
1794 HttpResponseHeaders* headers = GetResponseHeaders();
1795 DCHECK(headers);
1796
1797 std::string accept_encoding;
1798 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1799 &accept_encoding);
1800 std::set<std::string> allowed_encodings;
1801 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1802 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1803 return false;
1804 }
1805
1806 std::string content_encoding;
1807 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1808 std::set<std::string> used_encodings;
1809 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1810 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1811 return false;
1812 }
1813
1814 // When "Accept-Encoding" is not specified, it is parsed as "*".
1815 // If "*" encoding is advertised, then any encoding should be "accepted".
1816 // This does not mean, that it will be successfully decoded.
1817 if (allowed_encodings.find("*") != allowed_encodings.end())
1818 return true;
1819
sky50576f32017-05-01 19:28:031820 bool result = true;
eustasc7d27da2017-04-06 10:33:201821 for (auto const& encoding : used_encodings) {
1822 SourceStream::SourceType source_type =
1823 FilterSourceStream::ParseEncodingType(encoding);
1824 // We don't reject encodings we are not aware. They just will not decode.
1825 if (source_type == SourceStream::TYPE_UNKNOWN)
1826 continue;
1827 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031828 FilterSourceStream::ReportContentDecodingFailed(
1829 SourceStream::TYPE_REJECTED);
1830 result = false;
1831 break;
eustasc7d27da2017-04-06 10:33:201832 }
1833 }
sky50576f32017-05-01 19:28:031834
1835 // Temporary workaround for http://crbug.com/714514
1836 if (headers->IsRedirect(nullptr)) {
1837 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1838 !result);
1839 return true;
1840 }
1841
1842 return result;
eustasc7d27da2017-04-06 10:33:201843}
1844
[email protected]c3b35c22008-09-27 03:19:421845} // namespace net