blob: dd3dba87eee0eb4e7a80198429f59b71792811d5 [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 {
David Benjamin83ddfb32018-03-30 01:07:5274
Biljith Jayan45a41722017-08-16 18:43:1475// Max number of |retry_attempts| (excluding the initial request) after which
76// we give up and show an error page.
77const size_t kMaxRetryAttempts = 2;
David Benjamin83ddfb32018-03-30 01:07:5278
79// Max number of calls to RestartWith* allowed for a single connection. A single
80// HttpNetworkTransaction should not signal very many restartable errors, but it
81// may occur due to a bug (e.g. https://crbug.com/823387 or
82// https://crbug.com/488043) or simply if the server or proxy requests
83// authentication repeatedly. Although these calls are often associated with a
84// user prompt, in other scenarios (remembered preferences, extensions,
85// multi-leg authentication), they may be triggered automatically. To avoid
86// looping forever, bound the number of restarts.
87const size_t kMaxRestarts = 32;
88
Biljith Jayan45a41722017-08-16 18:43:1489} // namespace
90
initial.commit586acc5fe2008-07-26 22:42:5291namespace net {
92
[email protected]262eec82013-03-19 21:01:3693HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
94 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2295 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3296 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
97 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5298 session_(session),
99 request_(NULL),
[email protected]262eec82013-03-19 21:01:36100 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57101 headers_valid_(false),
Steven Valdezb4ff0412018-01-18 22:39:27102 can_send_early_data_(false),
David Benjamin76a40ad2018-02-24 22:22:08103 server_ssl_client_cert_was_cached_(false),
[email protected]b94f92d2010-10-27 16:45:20104 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52105 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19106 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22107 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54108 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44109 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29110 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:26111 enable_alternative_services_(true),
zhongyi48704c182015-12-07 07:52:02112 websocket_handshake_stream_base_create_helper_(NULL),
Biljith Jayan45a41722017-08-16 18:43:14113 net_error_details_(),
David Benjamin83ddfb32018-03-30 01:07:52114 retry_attempts_(0),
115 num_restarts_(0) {}
[email protected]3ce7df0f2010-03-03 00:30:50116
[email protected]0b0bf032010-09-21 18:08:50117HttpNetworkTransaction::~HttpNetworkTransaction() {
118 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50119 // TODO(mbelshe): The stream_ should be able to compute whether or not the
120 // stream should be kept alive. No reason to compute here
121 // and pass it in.
mmenkebd84c392015-09-02 14:12:34122 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50123 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34124 } else if (stream_->IsResponseBodyComplete()) {
125 // If the response body is complete, we can just reuse the socket.
126 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50127 } else {
mmenkebd84c392015-09-02 14:12:34128 // Otherwise, we try to drain the response body.
129 HttpStream* stream = stream_.release();
130 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50131 }
132 }
[email protected]02cad5d2013-10-02 08:14:03133 if (request_ && request_->upload_data_stream)
134 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50135}
136
[email protected]684970b2009-08-14 04:54:46137int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41138 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20139 const NetLogWithSource& net_log) {
Ramin Halavatib5e433e2018-02-07 07:41:10140 DCHECK(request_info->traffic_annotation.is_valid());
[email protected]9e743cd2010-03-16 07:03:53141 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04142 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15143 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04144
nharper8cdb0fb2016-04-22 21:34:59145 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
146 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
147
[email protected]99ffa5a2011-10-06 04:20:19148 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14149 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19150 proxy_ssl_config_.rev_checking_enabled = false;
151 }
[email protected]6fbac162011-06-20 00:29:04152
Steven Valdez03e872d2018-03-02 15:39:15153 if (HttpUtil::IsMethodSafe(request_info->method)) {
Steven Valdezb4ff0412018-01-18 22:39:27154 can_send_early_data_ = true;
155 }
156
jkarlinfb1d5172015-01-12 14:10:29157 if (request_->load_flags & LOAD_PREFETCH)
158 response_.unused_since_prefetch = true;
159
rdsmith1d343be52016-10-21 20:37:50160 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04161 int rv = DoLoop(OK);
162 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41163 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04164 return rv;
165}
166
167int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41168 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38169 DCHECK(!stream_.get());
170 DCHECK(!stream_request_.get());
171 DCHECK_EQ(STATE_NONE, next_state_);
172
David Benjamin83ddfb32018-03-30 01:07:52173 if (!CheckMaxRestarts())
174 return ERR_TOO_MANY_RETRIES;
175
[email protected]82918cc2010-08-25 17:24:50176 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38177
[email protected]ccb40e52008-09-17 20:54:40178 int rv = DoLoop(OK);
179 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41180 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11181 return rv;
[email protected]96d570e42008-08-05 22:43:04182}
183
[email protected]0b45559b2009-06-12 21:45:11184int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08185 scoped_refptr<X509Certificate> client_cert,
186 scoped_refptr<SSLPrivateKey> client_private_key,
svaldez7872fd02015-11-19 21:10:54187 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38188 // In HandleCertificateRequest(), we always tear down existing stream
189 // requests to force a new connection. So we shouldn't have one here.
190 DCHECK(!stream_request_.get());
191 DCHECK(!stream_.get());
192 DCHECK_EQ(STATE_NONE, next_state_);
193
David Benjamin83ddfb32018-03-30 01:07:52194 if (!CheckMaxRestarts())
195 return ERR_TOO_MANY_RETRIES;
196
[email protected]102957f2011-09-02 17:10:14197 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
198 &proxy_ssl_config_ : &server_ssl_config_;
199 ssl_config->send_client_cert = true;
200 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54201 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45202 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08203 response_.cert_request_info->host_and_port, std::move(client_cert),
204 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11205 // Reset the other member variables.
206 // Note: this is necessary only with SSL renegotiation.
207 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50208 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11209 int rv = DoLoop(OK);
210 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41211 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11212 return rv;
213}
214
[email protected]49639fa2011-12-20 23:22:41215int HttpNetworkTransaction::RestartWithAuth(
216 const AuthCredentials& credentials, const CompletionCallback& callback) {
David Benjamin83ddfb32018-03-30 01:07:52217 if (!CheckMaxRestarts())
218 return ERR_TOO_MANY_RETRIES;
219
[email protected]0757e7702009-03-27 04:00:22220 HttpAuth::Target target = pending_auth_target_;
221 if (target == HttpAuth::AUTH_NONE) {
222 NOTREACHED();
223 return ERR_UNEXPECTED;
224 }
[email protected]0757e7702009-03-27 04:00:22225 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42226
[email protected]f3cf9802011-10-28 18:44:58227 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13228
[email protected]49639fa2011-12-20 23:22:41229 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38230
231 int rv = OK;
232 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
233 // In this case, we've gathered credentials for use with proxy
234 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50235 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38236 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59237 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54238 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39239 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54240 } else {
[email protected]8e6441ca2010-08-19 05:56:38241 // In this case, we've gathered credentials for the server or the proxy
242 // but it is not during the tunneling phase.
243 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54244 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38245 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54246 }
[email protected]c3b35c22008-09-27 03:19:42247
[email protected]c3b35c22008-09-27 03:19:42248 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41249 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42250 return rv;
[email protected]96d570e42008-08-05 22:43:04251}
252
[email protected]f9ee6b52008-11-08 06:46:23253void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
254 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38255 DCHECK(!stream_request_.get());
256
Bence Béky3238f2e12017-09-22 22:44:49257 // Authorization schemes incompatible with HTTP/2 are unsupported for proxies.
258 if (target == HttpAuth::AUTH_SERVER &&
259 auth_controllers_[target]->NeedsHTTP11()) {
260 session_->http_server_properties()->SetHTTP11Required(
261 HostPortPair::FromURL(request_->url));
262 }
263
[email protected]2d2697f92009-02-18 21:00:32264 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57265 // Even if the server says the connection is keep-alive, we have to be
266 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34267 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57268 // If the response body hasn't been completely read, we need to drain
269 // it first.
[email protected]351ab642010-08-05 16:55:31270 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32271 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57272 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32273 read_buf_len_ = kDrainBodyBufferSize;
274 return;
275 }
[email protected]0877e3d2009-10-17 22:29:57276 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09277 }
278
[email protected]2d2697f92009-02-18 21:00:32279 // We don't need to drain the response body, so we act as if we had drained
280 // the response body.
281 DidDrainBodyForAuthRestart(keep_alive);
282}
283
284void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38285 DCHECK(!stream_request_.get());
286
287 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19288 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22289 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58290 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34291 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38292 // We should call connection_->set_idle_time(), but this doesn't occur
293 // often enough to be worth the trouble.
294 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39295 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38296 }
[email protected]697ef4c2010-10-14 16:38:58297
298 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46299 // Close the stream and mark it as not_reusable. Even in the
300 // keep_alive case, we've determined that the stream_ is not
301 // reusable if new_stream is NULL.
302 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58303 next_state_ = STATE_CREATE_STREAM;
304 } else {
sclittlefb249892015-09-10 21:33:22305 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19306 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22307 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58308 next_state_ = STATE_INIT_STREAM;
309 }
310 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32311 }
[email protected]f9ee6b52008-11-08 06:46:23312
313 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58314 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23315}
316
[email protected]8e6441ca2010-08-19 05:56:38317bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
318 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
319 HaveAuth(pending_auth_target_);
320}
321
[email protected]9dea9e1f2009-01-29 00:30:47322int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41323 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04324 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35325 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04326
[email protected]ad8e04a2010-11-01 04:16:27327 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38328 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04329 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29330 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04331 // bytes when establishing a tunnel because they might be controlled by
332 // an active network attacker. We don't worry about this for HTTP
333 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29334 // We reach this case when the user cancels a 407 proxy auth prompt. We
335 // also don't worry about this for an HTTPS Proxy, because the
336 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04337 // See http://crbug.com/8473.
Yixin Wang46a273ec302018-01-23 17:59:56338 DCHECK(proxy_info_.is_http() || proxy_info_.is_https() ||
339 proxy_info_.is_quic());
[email protected]9094b602012-02-27 21:44:58340 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54341 LOG(WARNING) << "Blocked proxy response with status "
342 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15343 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04344 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44345 }
346
[email protected]e60e47a2010-07-14 03:37:18347 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15348 next_state_ = STATE_READ_BODY;
349
[email protected]96d570e42008-08-05 22:43:04350 read_buf_ = buf;
351 read_buf_len_ = buf_len;
352
[email protected]96d570e42008-08-05 22:43:04353 int rv = DoLoop(OK);
354 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41355 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04356 return rv;
357}
358
[email protected]8cd06c02014-01-25 07:50:14359void HttpNetworkTransaction::StopCaching() {}
360
[email protected]79e1fd62013-06-20 06:50:04361bool HttpNetworkTransaction::GetFullRequestHeaders(
362 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32363 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04364 *headers = request_headers_;
365 return true;
366}
367
sclittle4de1bab92015-09-22 21:28:24368int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
369 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19370 if (stream_)
371 total_received_bytes += stream_->GetTotalReceivedBytes();
372 return total_received_bytes;
373}
374
sclittlefb249892015-09-10 21:33:22375int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
376 int64_t total_sent_bytes = total_sent_bytes_;
377 if (stream_)
378 total_sent_bytes += stream_->GetTotalSentBytes();
379 return total_sent_bytes;
380}
381
[email protected]8cd06c02014-01-25 07:50:14382void HttpNetworkTransaction::DoneReading() {}
383
[email protected]96d570e42008-08-05 22:43:04384const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55385 return &response_;
[email protected]96d570e42008-08-05 22:43:04386}
387
388LoadState HttpNetworkTransaction::GetLoadState() const {
389 // TODO(wtc): Define a new LoadState value for the
390 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
391 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50392 case STATE_THROTTLE_COMPLETE:
393 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48394 case STATE_CREATE_STREAM:
395 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50396 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38397 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15398 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
399 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57400 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04401 return LOAD_STATE_SENDING_REQUEST;
402 case STATE_READ_HEADERS_COMPLETE:
403 return LOAD_STATE_WAITING_FOR_RESPONSE;
404 case STATE_READ_BODY_COMPLETE:
405 return LOAD_STATE_READING_RESPONSE;
406 default:
407 return LOAD_STATE_IDLE;
408 }
409}
410
[email protected]8cd06c02014-01-25 07:50:14411void HttpNetworkTransaction::SetQuicServerInfo(
412 QuicServerInfo* quic_server_info) {}
413
[email protected]5033ab82013-03-22 20:17:46414bool HttpNetworkTransaction::GetLoadTimingInfo(
415 LoadTimingInfo* load_timing_info) const {
416 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
417 return false;
418
419 load_timing_info->proxy_resolve_start =
420 proxy_info_.proxy_resolve_start_time();
421 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
422 load_timing_info->send_start = send_start_time_;
423 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46424 return true;
425}
426
ttuttled9dbc652015-09-29 20:00:59427bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36428 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59429 return false;
430
431 *endpoint = remote_endpoint_;
432 return true;
433}
434
zhongyi48704c182015-12-07 07:52:02435void HttpNetworkTransaction::PopulateNetErrorDetails(
436 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12437 *details = net_error_details_;
438 if (stream_)
439 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02440}
441
[email protected]5033ab82013-03-22 20:17:46442void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
443 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50444
rdsmith29dbad12017-02-17 02:22:18445 // TODO(rdsmith): Note that if any code indirectly executed by
446 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
447 // ever implements a throttling mechanism where changing a request's
448 // priority may cause a this or another request to synchronously succeed
449 // or fail, that callback could synchronously delete |*this|, causing
450 // a crash on return to this code.
451 //
452 // |throttle_->SetPriority()| has exactly the above attributes, which
453 // is why it's the last call in this function.
454
[email protected]bf828982013-08-14 18:01:47455 if (stream_request_)
456 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03457 if (stream_)
458 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50459
460 if (throttle_)
461 throttle_->SetPriority(priority);
462 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46463}
464
[email protected]831e4a32013-11-14 02:14:44465void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
466 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
467 websocket_handshake_stream_base_create_helper_ = create_helper;
468}
469
[email protected]1826a402014-01-08 15:40:48470void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
471 const BeforeNetworkStartCallback& callback) {
472 before_network_start_callback_ = callback;
473}
474
ryansturm49a8cb12016-06-15 16:51:09475void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
476 const BeforeHeadersSentCallback& callback) {
477 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27478}
479
Andrey Kosyakov83a6eee2017-08-14 19:20:04480void HttpNetworkTransaction::SetRequestHeadersCallback(
481 RequestHeadersCallback callback) {
482 DCHECK(!stream_);
483 request_headers_callback_ = std::move(callback);
484}
485
Andrey Kosyakov2e893e62017-08-31 17:00:52486void HttpNetworkTransaction::SetResponseHeadersCallback(
487 ResponseHeadersCallback callback) {
488 DCHECK(!stream_);
489 response_headers_callback_ = std::move(callback);
490}
491
[email protected]1826a402014-01-08 15:40:48492int HttpNetworkTransaction::ResumeNetworkStart() {
493 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
494 return DoLoop(OK);
495}
496
[email protected]102e27c2011-02-23 01:01:31497void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
498 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00499 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50500 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38501 DCHECK(stream_request_.get());
502
sclittlefb249892015-09-10 21:33:22503 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19504 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22505 total_sent_bytes_ += stream_->GetTotalSentBytes();
506 }
bnc5029f4632017-06-08 16:19:00507 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04508 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14509 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31510 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52511 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21512 response_.alpn_negotiated_protocol =
513 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38514 response_.was_fetched_via_spdy = stream_request_->using_spdy();
515 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15516 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47517 response_.proxy_server = proxy_info_.proxy_server();
518 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
519 response_.proxy_server = ProxyServer::Direct();
520 else
521 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38522 OnIOComplete(OK);
523}
524
xunjieli5749218c2016-03-22 16:43:06525void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08526 const SSLConfig& used_ssl_config,
527 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00528 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08529 NOTREACHED();
530}
531
[email protected]a9cf2b92013-10-30 12:08:49532void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50533 const SSLConfig& used_ssl_config,
534 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00535 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
536 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50537}
538
Ryan Hamilton75f197262017-08-17 14:00:07539void HttpNetworkTransaction::OnStreamFailed(
540 int result,
541 const NetErrorDetails& net_error_details,
542 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50543 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38544 DCHECK_NE(OK, result);
545 DCHECK(stream_request_.get());
546 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14547 server_ssl_config_ = used_ssl_config;
Ryan Hamilton75f197262017-08-17 14:00:07548 net_error_details_ = net_error_details;
[email protected]8e6441ca2010-08-19 05:56:38549
550 OnIOComplete(result);
551}
552
[email protected]102e27c2011-02-23 01:01:31553void HttpNetworkTransaction::OnCertificateError(
554 int result,
555 const SSLConfig& used_ssl_config,
556 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50557 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38558 DCHECK_NE(OK, result);
559 DCHECK(stream_request_.get());
560 DCHECK(!stream_.get());
561
562 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14563 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38564
565 // TODO(mbelshe): For now, we're going to pass the error through, and that
566 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50567 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
568 // good and the user chooses to ignore the error. This is not ideal, but not
569 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38570
571 OnIOComplete(result);
572}
573
574void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50575 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31576 const SSLConfig& used_ssl_config,
577 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50578 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38579 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50580 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38581
582 establishing_tunnel_ = true;
583 response_.headers = proxy_response.headers;
584 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20585
586 if (response_.headers.get() && !ContentEncodingsValid()) {
587 DoCallback(ERR_CONTENT_DECODING_FAILED);
588 return;
589 }
590
[email protected]8e6441ca2010-08-19 05:56:38591 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14592 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31593 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38594
595 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
596 pending_auth_target_ = HttpAuth::AUTH_PROXY;
597
598 DoCallback(OK);
599}
600
601void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31602 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50603 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50604 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38605
[email protected]102957f2011-09-02 17:10:14606 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38607 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58608 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38609}
610
[email protected]511f6f52010-12-17 03:58:29611void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
612 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31613 const SSLConfig& used_ssl_config,
614 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01615 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29616 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
617
ttuttle1f2d7e92015-04-28 16:17:47618 CopyConnectionAttemptsFromStreamRequest();
619
[email protected]511f6f52010-12-17 03:58:29620 headers_valid_ = true;
621 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14622 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31623 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22624 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19625 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22626 total_sent_bytes_ += stream_->GetTotalSentBytes();
627 }
Bence Béky6d05ebd2017-05-16 00:09:01628 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04629 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29630 stream_request_.reset(); // we're done with the stream request
631 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
632}
633
zhongyi48704c182015-12-07 07:52:02634void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12635 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02636}
637
ttuttle1f2d7e92015-04-28 16:17:47638void HttpNetworkTransaction::GetConnectionAttempts(
639 ConnectionAttempts* out) const {
640 *out = connection_attempts_;
641}
642
rdsmithbf8c3c12016-11-18 18:16:24643void HttpNetworkTransaction::OnThrottleUnblocked(
644 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50645 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24646 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50647 // be so in the future.
648 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
649
650 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
651
652 DoLoop(OK);
653}
654
Adam Ricecb76ac62015-02-20 05:33:25655bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37656 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52657}
658
nharperb7441ef2016-01-25 23:54:14659bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
660 if (!IsSecureRequest())
661 return false;
662 SSLInfo ssl_info;
663 stream_->GetSSLInfo(&ssl_info);
664 return ssl_info.token_binding_negotiated &&
665 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
mmenke6ddfbea2017-05-31 21:48:41666 session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14667}
668
669void HttpNetworkTransaction::RecordTokenBindingSupport() const {
670 // This enum is used for an UMA histogram - do not change or re-use values.
671 enum {
672 DISABLED = 0,
673 CLIENT_ONLY = 1,
674 CLIENT_AND_SERVER = 2,
675 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
676 TOKEN_BINDING_SUPPORT_MAX
677 } supported;
678 if (!IsSecureRequest())
679 return;
680 SSLInfo ssl_info;
681 stream_->GetSSLInfo(&ssl_info);
682 if (!session_->params().enable_token_binding) {
683 supported = DISABLED;
mmenke6ddfbea2017-05-31 21:48:41684 } else if (!session_->context().channel_id_service) {
nharperb7441ef2016-01-25 23:54:14685 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
686 } else if (ssl_info.token_binding_negotiated) {
687 supported = CLIENT_AND_SERVER;
688 } else {
689 supported = CLIENT_ONLY;
690 }
691 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
692 TOKEN_BINDING_SUPPORT_MAX);
693}
694
Adam Rice425cf122015-01-19 06:18:24695bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12696 return (proxy_info_.is_http() || proxy_info_.is_https() ||
697 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24698 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
699}
700
initial.commit586acc5fe2008-07-26 22:42:52701void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50702 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41703 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52704
[email protected]96d570e42008-08-05 22:43:04705 // Since Run may result in Read being called, clear user_callback_ up front.
Bence Békybc1de292018-02-01 15:48:03706 base::ResetAndReturn(&callback_).Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52707}
708
709void HttpNetworkTransaction::OnIOComplete(int result) {
710 int rv = DoLoop(result);
711 if (rv != ERR_IO_PENDING)
712 DoCallback(rv);
713}
714
715int HttpNetworkTransaction::DoLoop(int result) {
716 DCHECK(next_state_ != STATE_NONE);
717
718 int rv = result;
719 do {
720 State state = next_state_;
721 next_state_ = STATE_NONE;
722 switch (state) {
rdsmith1d343be52016-10-21 20:37:50723 case STATE_THROTTLE:
724 DCHECK_EQ(OK, rv);
725 rv = DoThrottle();
726 break;
727 case STATE_THROTTLE_COMPLETE:
728 DCHECK_EQ(OK, rv);
729 rv = DoThrottleComplete();
730 break;
[email protected]1826a402014-01-08 15:40:48731 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
732 DCHECK_EQ(OK, rv);
733 rv = DoNotifyBeforeCreateStream();
734 break;
[email protected]82918cc2010-08-25 17:24:50735 case STATE_CREATE_STREAM:
736 DCHECK_EQ(OK, rv);
737 rv = DoCreateStream();
738 break;
739 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46740 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
741 // solved.
742 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50743 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46744 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50745 break;
[email protected]351ab642010-08-05 16:55:31746 case STATE_INIT_STREAM:
747 DCHECK_EQ(OK, rv);
748 rv = DoInitStream();
749 break;
750 case STATE_INIT_STREAM_COMPLETE:
751 rv = DoInitStreamComplete(rv);
752 break;
[email protected]044de0642010-06-17 10:42:15753 case STATE_GENERATE_PROXY_AUTH_TOKEN:
754 DCHECK_EQ(OK, rv);
755 rv = DoGenerateProxyAuthToken();
756 break;
757 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
758 rv = DoGenerateProxyAuthTokenComplete(rv);
759 break;
760 case STATE_GENERATE_SERVER_AUTH_TOKEN:
761 DCHECK_EQ(OK, rv);
762 rv = DoGenerateServerAuthToken();
763 break;
764 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
765 rv = DoGenerateServerAuthTokenComplete(rv);
766 break;
nharperd6e65822016-03-30 23:05:48767 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14768 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48769 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14770 break;
nharperd6e65822016-03-30 23:05:48771 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
772 rv = DoGetProvidedTokenBindingKeyComplete(rv);
773 break;
774 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
775 DCHECK_EQ(OK, rv);
776 rv = DoGetReferredTokenBindingKey();
777 break;
778 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
779 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14780 break;
[email protected]daddea62012-09-19 05:51:13781 case STATE_INIT_REQUEST_BODY:
782 DCHECK_EQ(OK, rv);
783 rv = DoInitRequestBody();
784 break;
785 case STATE_INIT_REQUEST_BODY_COMPLETE:
786 rv = DoInitRequestBodyComplete(rv);
787 break;
[email protected]4875ba12011-03-30 22:31:51788 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55789 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00790 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51791 rv = DoBuildRequest();
792 break;
793 case STATE_BUILD_REQUEST_COMPLETE:
794 rv = DoBuildRequestComplete(rv);
795 break;
796 case STATE_SEND_REQUEST:
797 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57798 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52799 break;
[email protected]0877e3d2009-10-17 22:29:57800 case STATE_SEND_REQUEST_COMPLETE:
801 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43802 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00803 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52804 break;
805 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55806 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00807 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52808 rv = DoReadHeaders();
809 break;
810 case STATE_READ_HEADERS_COMPLETE:
811 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43812 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00813 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52814 break;
815 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55816 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00817 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52818 rv = DoReadBody();
819 break;
820 case STATE_READ_BODY_COMPLETE:
821 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43822 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00823 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52824 break;
[email protected]2d2697f92009-02-18 21:00:32825 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55826 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53827 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00828 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32829 rv = DoDrainBodyForAuthRestart();
830 break;
831 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
832 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43833 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00834 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32835 break;
initial.commit586acc5fe2008-07-26 22:42:52836 default:
837 NOTREACHED() << "bad state";
838 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04839 break;
initial.commit586acc5fe2008-07-26 22:42:52840 }
841 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
842
843 return rv;
844}
845
rdsmith1d343be52016-10-21 20:37:50846int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24847 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50848 throttle_ = session_->throttler()->CreateThrottle(
849 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
850 next_state_ = STATE_THROTTLE_COMPLETE;
851
rdsmithbf8c3c12016-11-18 18:16:24852 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50853 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
854 return ERR_IO_PENDING;
855 }
856
857 return OK;
858}
859
860int HttpNetworkTransaction::DoThrottleComplete() {
861 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24862 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50863
864 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
865
866 return OK;
867}
868
[email protected]1826a402014-01-08 15:40:48869int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
870 next_state_ = STATE_CREATE_STREAM;
871 bool defer = false;
872 if (!before_network_start_callback_.is_null())
873 before_network_start_callback_.Run(&defer);
874 if (!defer)
875 return OK;
876 return ERR_IO_PENDING;
877}
878
[email protected]82918cc2010-08-25 17:24:50879int HttpNetworkTransaction::DoCreateStream() {
ttuttlec0c828492015-05-15 01:25:55880 response_.network_accessed = true;
881
[email protected]82918cc2010-08-25 17:24:50882 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17883 // IP based pooling is only enabled on a retry after 421 Misdirected Request
884 // is received. Alternative Services are also disabled in this case (though
885 // they can also be disabled when retrying after a QUIC error).
886 if (!enable_ip_based_pooling_)
887 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44888 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27889 stream_request_ =
Bence Béky8cae04e2018-01-15 18:37:06890 session_->http_stream_factory()->RequestWebSocketHandshakeStream(
891 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
892 websocket_handshake_stream_base_create_helper_,
893 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44894 } else {
xunjieli96f2a402017-06-05 17:24:27895 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29896 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27897 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44898 }
[email protected]26816882010-10-14 18:03:09899 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38900 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31901}
902
[email protected]82918cc2010-08-25 17:24:50903int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47904 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
905 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
906 // which resets the stream request first. Therefore, we have to grab the
907 // connection attempts in *that* function instead of here in that case.
908 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
909 CopyConnectionAttemptsFromStreamRequest();
910
[email protected]394816e92010-08-03 07:38:59911 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50912 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38913 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33914 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
915 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29916 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
917 // Return OK and let the caller read the proxy's error page
918 next_state_ = STATE_NONE;
919 return OK;
bncfacdd852015-01-09 19:22:54920 } else if (result == ERR_HTTP_1_1_REQUIRED ||
921 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
922 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59923 }
924
David Benjamin76a40ad2018-02-24 22:22:08925 // Handle possible client certificate errors that may have occurred if the
926 // stream used SSL for one or more of the layers.
927 result = HandleSSLClientAuthError(result);
[email protected]bd0b6772011-01-11 19:59:30928
[email protected]8e6441ca2010-08-19 05:56:38929 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09930 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38931 return result;
[email protected]394816e92010-08-03 07:38:59932}
933
[email protected]82918cc2010-08-25 17:24:50934int HttpNetworkTransaction::DoInitStream() {
935 DCHECK(stream_.get());
936 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59937
938 stream_->GetRemoteEndpoint(&remote_endpoint_);
939
Steven Valdezb4ff0412018-01-18 22:39:27940 return stream_->InitializeStream(request_, can_send_early_data_, priority_,
941 net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50942}
943
944int HttpNetworkTransaction::DoInitStreamComplete(int result) {
945 if (result == OK) {
946 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50947 } else {
[email protected]82918cc2010-08-25 17:24:50948 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26949 result = HandleIOError(result);
950
951 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22952 if (stream_) {
953 total_received_bytes_ += stream_->GetTotalReceivedBytes();
954 total_sent_bytes_ += stream_->GetTotalSentBytes();
955 }
zhongyica364fbb2015-12-12 03:39:12956 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50957 }
958
959 return result;
960}
961
[email protected]044de0642010-06-17 10:42:15962int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
963 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
964 if (!ShouldApplyProxyAuth())
965 return OK;
[email protected]394816e92010-08-03 07:38:59966 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
967 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09968 auth_controllers_[target] =
969 new HttpAuthController(target,
970 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31971 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09972 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59973 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41974 io_callback_,
[email protected]394816e92010-08-03 07:38:59975 net_log_);
[email protected]044de0642010-06-17 10:42:15976}
977
978int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
979 DCHECK_NE(ERR_IO_PENDING, rv);
980 if (rv == OK)
981 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
982 return rv;
983}
984
985int HttpNetworkTransaction::DoGenerateServerAuthToken() {
986 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59987 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54988 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09989 auth_controllers_[target] =
990 new HttpAuthController(target,
991 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31992 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09993 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54994 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
995 auth_controllers_[target]->DisableEmbeddedIdentity();
996 }
[email protected]044de0642010-06-17 10:42:15997 if (!ShouldApplyServerAuth())
998 return OK;
[email protected]394816e92010-08-03 07:38:59999 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:411000 io_callback_,
[email protected]394816e92010-08-03 07:38:591001 net_log_);
[email protected]044de0642010-06-17 10:42:151002}
1003
1004int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
1005 DCHECK_NE(ERR_IO_PENDING, rv);
1006 if (rv == OK)
nharperd6e65822016-03-30 23:05:481007 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:151008 return rv;
1009}
1010
nharperd6e65822016-03-30 23:05:481011int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
1012 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:141013 if (!IsTokenBindingEnabled())
1014 return OK;
1015
mikecirone8b85c432016-09-08 19:11:001016 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:411017 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:141018 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:481019 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:141020 &token_binding_request_);
1021}
1022
nharperd6e65822016-03-30 23:05:481023int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:141024 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:481025 if (IsTokenBindingEnabled()) {
1026 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001027 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481028 }
1029
1030 if (rv == OK)
1031 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
1032 return rv;
1033}
1034
1035int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1036 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1037 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141038 return OK;
1039
mikecirone8b85c432016-09-08 19:11:001040 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:411041 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperd6e65822016-03-30 23:05:481042 return channel_id_service->GetOrCreateChannelID(
1043 request_->token_binding_referrer, &referred_token_binding_key_,
1044 io_callback_, &token_binding_request_);
1045}
1046
1047int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1048 DCHECK_NE(ERR_IO_PENDING, rv);
1049 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1050 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001051 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481052 }
1053 if (rv == OK)
1054 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141055 return rv;
1056}
1057
1058int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241059 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141060 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1061 GetHostAndOptionalPort(request_->url));
1062
1063 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241064 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141065 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1066 "keep-alive");
1067 } else {
1068 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1069 }
1070
[email protected]2979a492011-04-06 00:29:141071 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111072 if (request_->upload_data_stream) {
1073 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141074 request_headers_.SetHeader(
1075 HttpRequestHeaders::kTransferEncoding, "chunked");
1076 } else {
1077 request_headers_.SetHeader(
1078 HttpRequestHeaders::kContentLength,
Daniel Cheng3d199b12017-12-12 03:51:091079 base::NumberToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141080 }
csharrisonf473dd192015-08-18 13:54:131081 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141082 // An empty POST/PUT request still needs a content length. As for HEAD,
1083 // IE and Safari also add a content length header. Presumably it is to
1084 // support sending a HEAD request to an URL that only expects to be sent a
1085 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131086 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1087 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141088 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1089 }
1090
nharperb7441ef2016-01-25 23:54:141091 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481092 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141093 std::string token_binding_header;
1094 int rv = BuildTokenBindingHeader(&token_binding_header);
1095 if (rv != OK)
1096 return rv;
1097 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1098 token_binding_header);
1099 }
1100
[email protected]2979a492011-04-06 00:29:141101 // Honor load flags that impact proxy caches.
1102 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1103 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1104 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1105 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1106 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1107 }
1108
1109 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1110 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1111 &request_headers_);
1112 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1113 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1114 &request_headers_);
1115
[email protected]c10450102011-06-27 09:06:161116 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201117
ryansturm49a8cb12016-06-15 16:51:091118 if (!before_headers_sent_callback_.is_null())
1119 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201120
[email protected]173f8e22013-04-10 04:18:201121 response_.did_use_http_auth =
1122 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1123 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141124 return OK;
1125}
1126
1127int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471128 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141129 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351130 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1131 TokenBindingType::PROVIDED,
1132 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141133 if (rv != OK)
1134 return rv;
1135 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481136 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1137 provided_token_binding_key_.get(), signed_ekm,
1138 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141139 if (rv != OK)
1140 return rv;
nharperd6e65822016-03-30 23:05:481141
nharperb7441ef2016-01-25 23:54:141142 std::vector<base::StringPiece> token_bindings;
1143 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481144
1145 std::string referred_token_binding;
1146 if (referred_token_binding_key_) {
1147 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351148 int rv = stream_->GetTokenBindingSignature(
1149 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1150 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481151 if (rv != OK)
1152 return rv;
1153 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1154 referred_token_binding_key_.get(),
1155 referred_signed_ekm, &referred_token_binding);
1156 if (rv != OK)
1157 return rv;
1158 token_bindings.push_back(referred_token_binding);
1159 }
nharperb7441ef2016-01-25 23:54:141160 std::string header;
1161 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1162 if (rv != OK)
1163 return rv;
nharper78e6d2b2016-09-21 05:42:351164 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471165 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1166 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1167 header_creation_time,
1168 base::TimeDelta::FromMilliseconds(1),
1169 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141170 return OK;
[email protected]2979a492011-04-06 00:29:141171}
1172
[email protected]daddea62012-09-19 05:51:131173int HttpNetworkTransaction::DoInitRequestBody() {
1174 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131175 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111176 if (request_->upload_data_stream)
Matt Menkecc1d3a902018-02-05 18:27:331177 rv = request_->upload_data_stream->Init(
1178 base::BindOnce(&HttpNetworkTransaction::OnIOComplete,
1179 base::Unretained(this)),
1180 net_log_);
[email protected]daddea62012-09-19 05:51:131181 return rv;
1182}
[email protected]4875ba12011-03-30 22:31:511183
[email protected]daddea62012-09-19 05:51:131184int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1185 if (result == OK)
1186 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131187 return result;
1188}
1189
1190int HttpNetworkTransaction::DoBuildRequest() {
1191 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511192 headers_valid_ = false;
1193
1194 // This is constructed lazily (instead of within our Start method), so that
1195 // we have proxy info available.
1196 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241197 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141198 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511199 }
1200
[email protected]4875ba12011-03-30 22:31:511201 return OK;
1202}
1203
1204int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241205 if (result == OK)
1206 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511207 return result;
1208}
1209
[email protected]0877e3d2009-10-17 22:29:571210int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:251211 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571212 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1213
[email protected]bf3eb002012-11-15 05:50:111214 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521215}
1216
[email protected]0877e3d2009-10-17 22:29:571217int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251218 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361219
1220 if (result == ERR_HTTP_1_1_REQUIRED ||
1221 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1222 return HandleHttp11Required(result);
1223 }
1224
initial.commit586acc5fe2008-07-26 22:42:521225 if (result < 0)
1226 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571227 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521228 return OK;
1229}
1230
1231int HttpNetworkTransaction::DoReadHeaders() {
1232 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411233 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521234}
1235
1236int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111237 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1238 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381239 if (IsCertificateError(result)) {
1240 // We don't handle a certificate error during SSL renegotiation, so we
1241 // have to return an error that's not in the certificate error range
1242 // (-2xx).
1243 LOG(ERROR) << "Got a server certificate with error " << result
1244 << " during SSL renegotiation";
1245 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1246 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1247 // TODO(wtc): Need a test case for this code path!
1248 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251249 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381250 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501251 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381252 result = HandleCertificateRequest(result);
1253 if (result == OK)
1254 return result;
[email protected]2181ea002009-06-09 01:37:271255 }
1256
bncfacdd852015-01-09 19:22:541257 if (result == ERR_HTTP_1_1_REQUIRED ||
1258 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1259 return HandleHttp11Required(result);
1260 }
1261
[email protected]c871864d72014-03-13 19:56:191262 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1263 // response headers were received, we do the best we can to make sense of it
1264 // and send it back up the stack.
1265 //
1266 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1267 // bizarre for SPDY. Assuming this logic is useful at all.
1268 // TODO(davidben): Bubble the error code up so we do not cache?
1269 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1270 result = OK;
1271
1272 if (result < 0)
1273 return HandleIOError(result);
1274
[email protected]90499482013-06-01 00:39:501275 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521276
eustasc7d27da2017-04-06 10:33:201277 if (response_.headers.get() && !ContentEncodingsValid())
1278 return ERR_CONTENT_DECODING_FAILED;
1279
[email protected]d58ceea82014-06-04 10:55:541280 // On a 408 response from the server ("Request Timeout") on a stale socket,
1281 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041282 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181283 if (response_.headers.get() &&
1284 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541285 stream_->IsConnectionReused()) {
1286 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001287 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541288 response_.headers->response_code());
1289 // This will close the socket - it would be weird to try and reuse it, even
1290 // if the server doesn't actually close it.
1291 ResetConnectionAndRequestForResend();
1292 return OK;
1293 }
1294
[email protected]93f8b562012-03-27 01:00:161295 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211296 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161297 const int response_code = response_.headers->response_code();
1298 UMA_HISTOGRAM_ENUMERATION(
1299 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1300 }
1301
[email protected]3abacd62012-06-10 20:20:321302 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001303 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321304 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
Andrey Kosyakov2e893e62017-08-31 17:00:521305 if (response_headers_callback_)
1306 response_headers_callback_.Run(response_.headers);
[email protected]dbb83db2010-05-11 18:13:391307
bncbe0f6af2015-10-15 17:49:561308 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571309 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1310 // indicates a buggy server. See:
1311 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1312 if (request_->method == "PUT")
1313 return ERR_METHOD_NOT_SUPPORTED;
1314 }
[email protected]4ddaf2502008-10-23 18:26:191315
[email protected]0877e3d2009-10-17 22:29:571316 // Check for an intermediate 100 Continue response. An origin server is
1317 // allowed to send this response even if we didn't ask for it, so we just
1318 // need to skip over it.
1319 // We treat any other 1xx in this same way (although in practice getting
1320 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441321 // Unless this is a WebSocket request, in which case we pass it on up.
1322 if (response_.headers->response_code() / 100 == 1 &&
1323 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451324 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571325 next_state_ = STATE_READ_HEADERS;
1326 return OK;
1327 }
1328
davidbence688ae2017-05-04 15:12:591329 if (response_.headers->response_code() == 421 &&
1330 (enable_ip_based_pooling_ || enable_alternative_services_)) {
1331 // Retry the request with both IP based pooling and Alternative Services
1332 // disabled.
1333 enable_ip_based_pooling_ = false;
1334 enable_alternative_services_ = false;
1335 net_log_.AddEvent(
1336 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1337 ResetConnectionAndRequestForResend();
1338 return OK;
bnc8016c1f2017-03-31 02:11:291339 }
1340
bncb26024382016-06-29 02:39:451341 if (IsSecureRequest()) {
asanka5ffd5d72016-03-23 16:20:491342 stream_->GetSSLInfo(&response_.ssl_info);
Bence Béky230ac612017-08-30 19:17:081343 if (response_.ssl_info.is_valid() &&
1344 !IsCertStatusError(response_.ssl_info.cert_status)) {
1345 session_->http_stream_factory()->ProcessAlternativeServices(
1346 session_, response_.headers.get(),
1347 url::SchemeHostPort(request_->url));
1348 }
1349 }
asanka5ffd5d72016-03-23 16:20:491350
[email protected]e772db3f2010-07-12 18:11:131351 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571352 if (rv != OK)
1353 return rv;
1354
[email protected]0877e3d2009-10-17 22:29:571355 headers_valid_ = true;
Shivani Sharmafdcaefd2017-11-02 00:12:261356
1357 // We have reached the end of Start state machine, set the RequestInfo to
1358 // null.
1359 // RequestInfo is a member of the HttpTransaction's consumer and is useful
1360 // only until the final response headers are received. Clearing it will ensure
1361 // that HttpRequestInfo is only used up until final response headers are
1362 // received. Clearing is allowed so that the transaction can be disassociated
1363 // from its creating consumer in cases where it is shared for writing to the
1364 // cache. It is also safe to set it to null at this point since
1365 // upload_data_stream is also not used in the Read state machine.
1366 if (pending_auth_target_ == HttpAuth::AUTH_NONE)
1367 request_ = nullptr;
1368
[email protected]0877e3d2009-10-17 22:29:571369 return OK;
initial.commit586acc5fe2008-07-26 22:42:521370}
1371
1372int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501373 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131374 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381375 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521376
1377 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501378 return stream_->ReadResponseBody(
1379 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521380}
1381
1382int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1383 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381384 bool done = false;
1385 if (result <= 0) {
1386 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521387 done = true;
[email protected]8e6441ca2010-08-19 05:56:381388 }
[email protected]9492e4a2010-02-24 00:58:461389
mmenkebd84c392015-09-02 14:12:341390 // Clean up connection if we are done.
1391 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381392 // Note: Just because IsResponseBodyComplete is true, we're not
1393 // necessarily "done". We're only "done" when it is the last
1394 // read on this HttpNetworkTransaction, which will be signified
1395 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341396 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381397 // the stream. No need to compute it here just to pass back
1398 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341399 bool keep_alive =
1400 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521401
[email protected]8e6441ca2010-08-19 05:56:381402 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011403 // Note: we don't reset the stream here. We've closed it, but we still
1404 // need it around so that callers can call methods such as
1405 // GetUploadProgress() and have them be meaningful.
1406 // TODO(mbelshe): This means we closed the stream here, and we close it
1407 // again in ~HttpNetworkTransaction. Clean that up.
1408
[email protected]8e6441ca2010-08-19 05:56:381409 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171410
1411 // This transaction was successful. If it had been retried because of an
1412 // error with an alternative service, mark that alternative service broken.
1413 if (!enable_alternative_services_ &&
1414 retried_alternative_service_.protocol != kProtoUnknown) {
1415 session_->http_server_properties()->MarkAlternativeServiceBroken(
1416 retried_alternative_service_);
1417 }
initial.commit586acc5fe2008-07-26 22:42:521418 }
1419
1420 // Clear these to avoid leaving around old state.
1421 read_buf_ = NULL;
1422 read_buf_len_ = 0;
1423
1424 return result;
1425}
1426
[email protected]2d2697f92009-02-18 21:00:321427int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1428 // This method differs from DoReadBody only in the next_state_. So we just
1429 // call DoReadBody and override the next_state_. Perhaps there is a more
1430 // elegant way for these two methods to share code.
1431 int rv = DoReadBody();
1432 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1433 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1434 return rv;
1435}
1436
[email protected]0877e3d2009-10-17 22:29:571437// TODO(wtc): This method and the DoReadBodyComplete method are almost
1438// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321439int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231440 // keep_alive defaults to true because the very reason we're draining the
1441 // response body is to reuse the connection for auth restart.
1442 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321443 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571444 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321445 done = true;
[email protected]68873ba2009-06-04 21:49:231446 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311447 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571448 done = true;
[email protected]2d2697f92009-02-18 21:00:321449 }
1450
1451 if (done) {
1452 DidDrainBodyForAuthRestart(keep_alive);
1453 } else {
1454 // Keep draining.
1455 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1456 }
1457
1458 return OK;
1459}
1460
[email protected]5e363962009-06-19 19:57:011461int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381462 // There are two paths through which the server can request a certificate
1463 // from us. The first is during the initial handshake, the second is
1464 // during SSL renegotiation.
1465 //
1466 // In both cases, we want to close the connection before proceeding.
1467 // We do this for two reasons:
1468 // First, we don't want to keep the connection to the server hung for a
1469 // long time while the user selects a certificate.
1470 // Second, even if we did keep the connection open, NSS has a bug where
1471 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581472 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381473
1474 if (stream_.get()) {
1475 // Since we already have a stream, we're being called as part of SSL
1476 // renegotiation.
1477 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191478 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221479 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381480 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121481 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381482 }
1483
[email protected]26816882010-10-14 18:03:091484 // The server is asking for a client certificate during the initial
1485 // handshake.
1486 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011487
[email protected]ec229bc92010-11-22 09:51:451488 // If the user selected one of the certificates in client_certs or declined
1489 // to provide one for this server before, use the past decision
1490 // automatically.
1491 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541492 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451493 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541494 response_.cert_request_info->host_and_port, &client_cert,
1495 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451496 if (!found_cached_cert)
1497 return error;
1498
1499 // Check that the certificate selected is still a certificate the server
1500 // is likely to accept, based on the criteria supplied in the
1501 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501502 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261503 const std::vector<std::string>& cert_authorities =
1504 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451505
[email protected]f1958c382013-02-07 00:15:261506 bool cert_still_valid = cert_authorities.empty() ||
1507 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451508 if (!cert_still_valid)
1509 return error;
[email protected]5e363962009-06-19 19:57:011510 }
[email protected]ec229bc92010-11-22 09:51:451511
David Benjamin76a40ad2018-02-24 22:22:081512 if (!response_.cert_request_info->is_proxy) {
1513 server_ssl_client_cert_was_cached_ = true;
1514 }
1515
[email protected]ec229bc92010-11-22 09:51:451516 // TODO(davidben): Add a unit test which covers this path; we need to be
1517 // able to send a legitimate certificate and also bypass/clear the
1518 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141519 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1520 &proxy_ssl_config_ : &server_ssl_config_;
1521 ssl_config->send_client_cert = true;
1522 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541523 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451524 next_state_ = STATE_CREATE_STREAM;
1525 // Reset the other member variables.
1526 // Note: this is necessary only with SSL renegotiation.
1527 ResetStateForRestart();
1528 return OK;
[email protected]0b45559b2009-06-12 21:45:111529}
1530
bncfacdd852015-01-09 19:22:541531int HttpNetworkTransaction::HandleHttp11Required(int error) {
1532 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1533 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1534
1535 if (error == ERR_HTTP_1_1_REQUIRED) {
1536 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1537 } else {
1538 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1539 }
1540 ResetConnectionAndRequestForResend();
1541 return OK;
1542}
1543
David Benjamin76a40ad2018-02-24 22:22:081544int HttpNetworkTransaction::HandleSSLClientAuthError(int error) {
1545 // TODO(davidben): This does handle client certificate errors from the
1546 // proxy. https://crbug.com/814911.
[email protected]384cc73a2013-12-08 22:41:031547 if (server_ssl_config_.send_client_cert &&
1548 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1549 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411550 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031551
David Benjamin76a40ad2018-02-24 22:22:081552 // The private key handle may have gone stale due to, e.g., the user
1553 // unplugging their smartcard. Operating systems do not provide reliable
1554 // notifications for this, so if the signature failed and the private key
1555 // came from SSLClientAuthCache, retry to ask the user for a new one.
1556 if (error == ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED &&
1557 server_ssl_client_cert_was_cached_ && !HasExceededMaxRetries()) {
1558 server_ssl_client_cert_was_cached_ = false;
1559 server_ssl_config_.send_client_cert = false;
1560 server_ssl_config_.client_cert = nullptr;
1561 server_ssl_config_.client_private_key = nullptr;
1562 retry_attempts_++;
1563 net_log_.AddEventWithNetErrorCode(
1564 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1565 ResetConnectionAndRequestForResend();
1566 return OK;
1567 }
1568 }
initial.commit586acc5fe2008-07-26 22:42:521569 return error;
1570}
1571
[email protected]bd0b6772011-01-11 19:59:301572// This method determines whether it is safe to resend the request after an
1573// IO error. It can only be called in response to request header or body
1574// write errors or response header read errors. It should not be used in
1575// other cases, such as a Connect error.
1576int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031577 // Because the peer may request renegotiation with client authentication at
1578 // any time, check and handle client authentication errors.
David Benjamin76a40ad2018-02-24 22:22:081579 error = HandleSSLClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301580
1581 switch (error) {
1582 // If we try to reuse a connection that the server is in the process of
1583 // closing, we may end up successfully writing out our request (or a
1584 // portion of our request) only to find a connection error when we try to
1585 // read from (or finish writing to) the socket.
1586 case ERR_CONNECTION_RESET:
1587 case ERR_CONNECTION_CLOSED:
1588 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511589 // There can be a race between the socket pool checking checking whether a
1590 // socket is still connected, receiving the FIN, and sending/reading data
1591 // on a reused socket. If we receive the FIN between the connectedness
1592 // check and writing/reading from the socket, we may first learn the socket
1593 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1594 // likely happen when trying to retrieve its IP address.
1595 // See http://crbug.com/105824 for more details.
1596 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491597 // If a socket is closed on its initial request, HttpStreamParser returns
1598 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1599 // preconnected but failed to be used before the server timed it out.
1600 case ERR_EMPTY_RESPONSE:
1601 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381602 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001603 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301604 ResetConnectionAndRequestForResend();
1605 error = OK;
1606 }
1607 break;
[email protected]8753a122011-10-16 08:05:081608 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001609 case ERR_SPDY_SERVER_REFUSED_STREAM:
Bence Béky1a5d8562018-01-05 17:29:281610 case ERR_SPDY_PUSHED_STREAM_NOT_AVAILABLE:
Yoav Weiss9693572f2018-01-04 09:37:341611 case ERR_SPDY_CLAIMED_PUSHED_STREAM_RESET_BY_SERVER:
[email protected]4d283b32013-10-17 12:57:271612 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141613 if (HasExceededMaxRetries())
1614 break;
[email protected]b6754252012-06-13 23:14:381615 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001616 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141617 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001618 ResetConnectionAndRequestForResend();
1619 error = OK;
1620 break;
rch2f2991c2017-04-13 19:28:171621 case ERR_QUIC_PROTOCOL_ERROR:
1622 if (GetResponseHeaders() != nullptr ||
1623 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1624 // If the response headers have already been recieved and passed up
1625 // then the request can not be retried. Also, if there was no
1626 // alternative service used for this request, then there is no
1627 // alternative service to be disabled.
1628 break;
1629 }
Biljith Jayan45a41722017-08-16 18:43:141630 if (HasExceededMaxRetries())
1631 break;
rch2f2991c2017-04-13 19:28:171632 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1633 retried_alternative_service_)) {
1634 // If the alternative service was marked as broken while the request
1635 // was in flight, retry the request which will not use the broken
1636 // alternative service.
1637 net_log_.AddEventWithNetErrorCode(
1638 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141639 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171640 ResetConnectionAndRequestForResend();
1641 error = OK;
1642 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1643 // Disable alternative services for this request and retry it. If the
1644 // retry succeeds, then the alternative service will be marked as
1645 // broken then.
1646 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361647 net_log_.AddEventWithNetErrorCode(
1648 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141649 retry_attempts_++;
rch514a44a82017-04-13 04:46:361650 ResetConnectionAndRequestForResend();
1651 error = OK;
1652 }
1653 break;
[email protected]bd0b6772011-01-11 19:59:301654 }
1655 return error;
1656}
1657
[email protected]c3b35c22008-09-27 03:19:421658void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581659 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221660 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191661 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221662 total_sent_bytes_ += stream_->GetTotalSentBytes();
1663 }
zhongyica364fbb2015-12-12 03:39:121664 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581665}
1666
1667void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251668 send_start_time_ = base::TimeTicks();
1669 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251670
[email protected]0757e7702009-03-27 04:00:221671 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421672 read_buf_ = NULL;
1673 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571674 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201675 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141676 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381677 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591678 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121679 net_error_details_.quic_broken = false;
1680 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481681 provided_token_binding_key_.reset();
1682 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121683}
1684
1685void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1686 if (stream_)
1687 stream_->PopulateNetErrorDetails(&net_error_details_);
1688 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571689}
1690
[email protected]0877e3d2009-10-17 22:29:571691HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501692 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421693}
1694
[email protected]a34f61ee2014-03-18 20:59:491695bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381696 bool connection_is_proven = stream_->IsConnectionReused();
1697 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161698
[email protected]2a5c76b2008-09-25 22:15:161699 // NOTE: we resend a request only if we reused a keep-alive connection.
1700 // This automatically prevents an infinite resend loop because we'll run
1701 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381702 if (connection_is_proven && !has_received_headers)
1703 return true;
1704 return false;
[email protected]1c773ea12009-04-28 19:58:421705}
1706
Biljith Jayan45a41722017-08-16 18:43:141707bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1708 return (retry_attempts_ >= kMaxRetryAttempts);
1709}
1710
David Benjamin83ddfb32018-03-30 01:07:521711bool HttpNetworkTransaction::CheckMaxRestarts() {
1712 num_restarts_++;
1713 return num_restarts_ < kMaxRestarts;
1714}
1715
[email protected]1c773ea12009-04-28 19:58:421716void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381717 if (stream_.get()) {
1718 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121719 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161720 }
1721
[email protected]0877e3d2009-10-17 22:29:571722 // We need to clear request_headers_ because it contains the real request
1723 // headers, but we may need to resend the CONNECT request first to recreate
1724 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201725 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501726 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541727}
1728
[email protected]1c773ea12009-04-28 19:58:421729bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241730 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421731}
license.botbf09a502008-08-24 00:55:551732
[email protected]1c773ea12009-04-28 19:58:421733bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041734 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421735}
1736
[email protected]e772db3f2010-07-12 18:11:131737int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271738 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501739 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421740
[email protected]0877e3d2009-10-17 22:29:571741 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581742 if (status != HTTP_UNAUTHORIZED &&
1743 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421744 return OK;
[email protected]9094b602012-02-27 21:44:581745 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111746 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161747 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1748 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421749
[email protected]9094b602012-02-27 21:44:581750 // This case can trigger when an HTTPS server responds with a "Proxy
1751 // authentication required" status code through a non-authenticating
1752 // proxy.
[email protected]7a67a8152010-11-05 18:31:101753 if (!auth_controllers_[target].get())
1754 return ERR_UNEXPECTED_PROXY_AUTH;
1755
[email protected]a7ea8832010-07-12 17:54:541756 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491757 headers, response_.ssl_info,
1758 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311759 net_log_);
[email protected]228404f2010-06-24 04:31:411760 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491761 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411762
1763 scoped_refptr<AuthChallengeInfo> auth_info =
1764 auth_controllers_[target]->auth_info();
1765 if (auth_info.get())
1766 response_.auth_challenge = auth_info;
1767
[email protected]228404f2010-06-24 04:31:411768 return rv;
[email protected]f9ee6b52008-11-08 06:46:231769}
1770
[email protected]8e6441ca2010-08-19 05:56:381771bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1772 return auth_controllers_[target].get() &&
1773 auth_controllers_[target]->HaveAuth();
1774}
1775
[email protected]228404f2010-06-24 04:31:411776GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1777 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461778 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411779 if (!proxy_info_.proxy_server().is_valid() ||
1780 proxy_info_.proxy_server().is_direct()) {
1781 return GURL(); // There is no proxy server.
1782 }
[email protected]2df19bb2010-08-25 20:13:461783 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1784 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351785 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461786 }
[email protected]228404f2010-06-24 04:31:411787 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291788 if (ForWebSocketHandshake()) {
1789 const GURL& url = request_->url;
1790 url::Replacements<char> ws_to_http;
1791 if (url.SchemeIs("ws")) {
1792 ws_to_http.SetScheme("http", url::Component(0, 4));
1793 } else {
1794 DCHECK(url.SchemeIs("wss"));
1795 ws_to_http.SetScheme("https", url::Component(0, 5));
1796 }
1797 return url.ReplaceComponents(ws_to_http);
1798 }
[email protected]228404f2010-06-24 04:31:411799 return request_->url;
1800 default:
1801 return GURL();
1802 }
[email protected]c3b35c22008-09-27 03:19:421803}
1804
[email protected]831e4a32013-11-14 02:14:441805bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141806 return websocket_handshake_stream_base_create_helper_ &&
1807 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441808}
1809
ttuttle1f2d7e92015-04-28 16:17:471810void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1811 DCHECK(stream_request_);
1812
1813 // Since the transaction can restart with auth credentials, it may create a
1814 // stream more than once. Accumulate all of the connection attempts across
1815 // those streams by appending them to the vector:
1816 for (const auto& attempt : stream_request_->connection_attempts())
1817 connection_attempts_.push_back(attempt);
1818}
1819
eustasc7d27da2017-04-06 10:33:201820bool HttpNetworkTransaction::ContentEncodingsValid() const {
1821 HttpResponseHeaders* headers = GetResponseHeaders();
1822 DCHECK(headers);
1823
1824 std::string accept_encoding;
1825 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1826 &accept_encoding);
1827 std::set<std::string> allowed_encodings;
1828 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1829 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1830 return false;
1831 }
1832
1833 std::string content_encoding;
1834 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1835 std::set<std::string> used_encodings;
1836 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1837 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1838 return false;
1839 }
1840
1841 // When "Accept-Encoding" is not specified, it is parsed as "*".
1842 // If "*" encoding is advertised, then any encoding should be "accepted".
1843 // This does not mean, that it will be successfully decoded.
1844 if (allowed_encodings.find("*") != allowed_encodings.end())
1845 return true;
1846
sky50576f32017-05-01 19:28:031847 bool result = true;
eustasc7d27da2017-04-06 10:33:201848 for (auto const& encoding : used_encodings) {
1849 SourceStream::SourceType source_type =
1850 FilterSourceStream::ParseEncodingType(encoding);
1851 // We don't reject encodings we are not aware. They just will not decode.
1852 if (source_type == SourceStream::TYPE_UNKNOWN)
1853 continue;
1854 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031855 FilterSourceStream::ReportContentDecodingFailed(
1856 SourceStream::TYPE_REJECTED);
1857 result = false;
1858 break;
eustasc7d27da2017-04-06 10:33:201859 }
1860 }
sky50576f32017-05-01 19:28:031861
1862 // Temporary workaround for http://crbug.com/714514
1863 if (headers->IsRedirect(nullptr)) {
1864 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1865 !result);
1866 return true;
1867 }
1868
1869 return result;
eustasc7d27da2017-04-06 10:33:201870}
1871
[email protected]c3b35c22008-09-27 03:19:421872} // namespace net