blob: e2c92c7c79807a0fb437a820ef4f38ddd5ad48a5 [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"
[email protected]68bf9152008-09-25 19:47:3014#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4715#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3816#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3717#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4218#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4119#include "base/profiler/scoped_tracker.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"
initial.commit586acc5fe2008-07-26 22:42:5232#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4433#include "net/base/url_util.h"
eustasc7d27da2017-04-06 10:33:2034#include "net/filter/filter_source_stream.h"
bnc5029f4632017-06-08 16:19:0035#include "net/http/bidirectional_stream_impl.h"
[email protected]c3b35c22008-09-27 03:19:4236#include "net/http/http_auth.h"
37#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2438#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3639#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/http/http_chunked_decoder.h"
41#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5442#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1343#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4744#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2146#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5747#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5348#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5849#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3950#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3151#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2353#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3654#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0055#include "net/log/net_log_event_type.h"
tbansal2ecbbc72016-10-06 17:15:4756#include "net/proxy/proxy_server.h"
[email protected]f7984fc62009-06-22 23:26:4457#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2158#include "net/socket/next_proto.h"
[email protected]a796bcec2010-03-22 17:17:2659#include "net/socket/socks_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2860#include "net/socket/transport_client_socket_pool.h"
bnc8f8f7d302017-04-24 18:08:0661#include "net/spdy/chromium/spdy_http_stream.h"
62#include "net/spdy/chromium/spdy_session.h"
63#include "net/spdy/chromium/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_cert_request_info.h"
65#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5466#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1467#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5468#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
Biljith Jayan45a41722017-08-16 18:43:1471namespace {
72// Max number of |retry_attempts| (excluding the initial request) after which
73// we give up and show an error page.
74const size_t kMaxRetryAttempts = 2;
75} // namespace
76
initial.commit586acc5fe2008-07-26 22:42:5277namespace net {
78
[email protected]262eec82013-03-19 21:01:3679HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
80 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2281 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3282 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
83 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5284 session_(session),
85 request_(NULL),
[email protected]262eec82013-03-19 21:01:3686 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5787 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2088 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5289 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:1990 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:2291 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:5492 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:4493 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:2994 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:2695 enable_alternative_services_(true),
zhongyi48704c182015-12-07 07:52:0296 websocket_handshake_stream_base_create_helper_(NULL),
Biljith Jayan45a41722017-08-16 18:43:1497 net_error_details_(),
98 retry_attempts_(0) {}
[email protected]3ce7df0f2010-03-03 00:30:5099
[email protected]0b0bf032010-09-21 18:08:50100HttpNetworkTransaction::~HttpNetworkTransaction() {
101 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50102 // TODO(mbelshe): The stream_ should be able to compute whether or not the
103 // stream should be kept alive. No reason to compute here
104 // and pass it in.
mmenkebd84c392015-09-02 14:12:34105 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50106 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34107 } else if (stream_->IsResponseBodyComplete()) {
108 // If the response body is complete, we can just reuse the socket.
109 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50110 } else {
mmenkebd84c392015-09-02 14:12:34111 // Otherwise, we try to drain the response body.
112 HttpStream* stream = stream_.release();
113 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50114 }
115 }
[email protected]02cad5d2013-10-02 08:14:03116 if (request_ && request_->upload_data_stream)
117 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50118}
119
[email protected]684970b2009-08-14 04:54:46120int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41121 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20122 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53123 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04124 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15125 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04126
nharper8cdb0fb2016-04-22 21:34:59127 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
128 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
129
[email protected]99ffa5a2011-10-06 04:20:19130 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14131 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19132 proxy_ssl_config_.rev_checking_enabled = false;
133 }
[email protected]6fbac162011-06-20 00:29:04134
jkarlinfb1d5172015-01-12 14:10:29135 if (request_->load_flags & LOAD_PREFETCH)
136 response_.unused_since_prefetch = true;
137
rdsmith1d343be52016-10-21 20:37:50138 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04139 int rv = DoLoop(OK);
140 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41141 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04142 return rv;
143}
144
145int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41146 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38147 DCHECK(!stream_.get());
148 DCHECK(!stream_request_.get());
149 DCHECK_EQ(STATE_NONE, next_state_);
150
[email protected]82918cc2010-08-25 17:24:50151 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38152
[email protected]ccb40e52008-09-17 20:54:40153 int rv = DoLoop(OK);
154 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41155 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11156 return rv;
[email protected]96d570e42008-08-05 22:43:04157}
158
[email protected]0b45559b2009-06-12 21:45:11159int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08160 scoped_refptr<X509Certificate> client_cert,
161 scoped_refptr<SSLPrivateKey> client_private_key,
svaldez7872fd02015-11-19 21:10:54162 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38163 // In HandleCertificateRequest(), we always tear down existing stream
164 // requests to force a new connection. So we shouldn't have one here.
165 DCHECK(!stream_request_.get());
166 DCHECK(!stream_.get());
167 DCHECK_EQ(STATE_NONE, next_state_);
168
[email protected]102957f2011-09-02 17:10:14169 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
170 &proxy_ssl_config_ : &server_ssl_config_;
171 ssl_config->send_client_cert = true;
172 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54173 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45174 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08175 response_.cert_request_info->host_and_port, std::move(client_cert),
176 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11177 // Reset the other member variables.
178 // Note: this is necessary only with SSL renegotiation.
179 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50180 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11181 int rv = DoLoop(OK);
182 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41183 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11184 return rv;
185}
186
[email protected]49639fa2011-12-20 23:22:41187int HttpNetworkTransaction::RestartWithAuth(
188 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22189 HttpAuth::Target target = pending_auth_target_;
190 if (target == HttpAuth::AUTH_NONE) {
191 NOTREACHED();
192 return ERR_UNEXPECTED;
193 }
[email protected]0757e7702009-03-27 04:00:22194 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42195
[email protected]f3cf9802011-10-28 18:44:58196 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13197
[email protected]49639fa2011-12-20 23:22:41198 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38199
200 int rv = OK;
201 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
202 // In this case, we've gathered credentials for use with proxy
203 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50204 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38205 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59206 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54207 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39208 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54209 } else {
[email protected]8e6441ca2010-08-19 05:56:38210 // In this case, we've gathered credentials for the server or the proxy
211 // but it is not during the tunneling phase.
212 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54213 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38214 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54215 }
[email protected]c3b35c22008-09-27 03:19:42216
[email protected]c3b35c22008-09-27 03:19:42217 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41218 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42219 return rv;
[email protected]96d570e42008-08-05 22:43:04220}
221
[email protected]f9ee6b52008-11-08 06:46:23222void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
223 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38224 DCHECK(!stream_request_.get());
225
[email protected]2d2697f92009-02-18 21:00:32226 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57227 // Even if the server says the connection is keep-alive, we have to be
228 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34229 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57230 // If the response body hasn't been completely read, we need to drain
231 // it first.
[email protected]351ab642010-08-05 16:55:31232 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32233 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57234 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32235 read_buf_len_ = kDrainBodyBufferSize;
236 return;
237 }
[email protected]0877e3d2009-10-17 22:29:57238 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09239 }
240
[email protected]2d2697f92009-02-18 21:00:32241 // We don't need to drain the response body, so we act as if we had drained
242 // the response body.
243 DidDrainBodyForAuthRestart(keep_alive);
244}
245
246void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38247 DCHECK(!stream_request_.get());
248
249 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19250 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22251 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58252 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34253 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38254 // We should call connection_->set_idle_time(), but this doesn't occur
255 // often enough to be worth the trouble.
256 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39257 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38258 }
[email protected]697ef4c2010-10-14 16:38:58259
260 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46261 // Close the stream and mark it as not_reusable. Even in the
262 // keep_alive case, we've determined that the stream_ is not
263 // reusable if new_stream is NULL.
264 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58265 next_state_ = STATE_CREATE_STREAM;
266 } else {
sclittlefb249892015-09-10 21:33:22267 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19268 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22269 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58270 next_state_ = STATE_INIT_STREAM;
271 }
272 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32273 }
[email protected]f9ee6b52008-11-08 06:46:23274
275 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58276 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23277}
278
[email protected]8e6441ca2010-08-19 05:56:38279bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
280 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
281 HaveAuth(pending_auth_target_);
282}
283
[email protected]9dea9e1f2009-01-29 00:30:47284int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41285 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04286 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35287 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04288
[email protected]ad8e04a2010-11-01 04:16:27289 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38290 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04291 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29292 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04293 // bytes when establishing a tunnel because they might be controlled by
294 // an active network attacker. We don't worry about this for HTTP
295 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29296 // We reach this case when the user cancels a 407 proxy auth prompt. We
297 // also don't worry about this for an HTTPS Proxy, because the
298 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04299 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46300 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58301 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54302 LOG(WARNING) << "Blocked proxy response with status "
303 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15304 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04305 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44306 }
307
[email protected]e60e47a2010-07-14 03:37:18308 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15309 next_state_ = STATE_READ_BODY;
310
311 // We have reached the end of Start state machine, reset the requestinfo to
312 // null.
313 // RequestInfo is a member of the HttpTransaction's consumer and is useful
314 // only till final response headers are received. A reset will ensure that
315 // HttpRequestInfo is only used up until final response headers are received.
316 // Resetting is allowed so that the transaction can be disassociated from its
317 // creating consumer in cases where it is shared for writing to the cache.
318 // It is also safe to reset it to null at this point since upload_data_stream
319 // is also not used in the Read state machine.
320 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18321
[email protected]96d570e42008-08-05 22:43:04322 read_buf_ = buf;
323 read_buf_len_ = buf_len;
324
[email protected]96d570e42008-08-05 22:43:04325 int rv = DoLoop(OK);
326 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41327 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04328 return rv;
329}
330
[email protected]8cd06c02014-01-25 07:50:14331void HttpNetworkTransaction::StopCaching() {}
332
[email protected]79e1fd62013-06-20 06:50:04333bool HttpNetworkTransaction::GetFullRequestHeaders(
334 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32335 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04336 *headers = request_headers_;
337 return true;
338}
339
sclittle4de1bab92015-09-22 21:28:24340int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
341 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19342 if (stream_)
343 total_received_bytes += stream_->GetTotalReceivedBytes();
344 return total_received_bytes;
345}
346
sclittlefb249892015-09-10 21:33:22347int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
348 int64_t total_sent_bytes = total_sent_bytes_;
349 if (stream_)
350 total_sent_bytes += stream_->GetTotalSentBytes();
351 return total_sent_bytes;
352}
353
[email protected]8cd06c02014-01-25 07:50:14354void HttpNetworkTransaction::DoneReading() {}
355
[email protected]96d570e42008-08-05 22:43:04356const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55357 return &response_;
[email protected]96d570e42008-08-05 22:43:04358}
359
360LoadState HttpNetworkTransaction::GetLoadState() const {
361 // TODO(wtc): Define a new LoadState value for the
362 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
363 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50364 case STATE_THROTTLE_COMPLETE:
365 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48366 case STATE_CREATE_STREAM:
367 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50368 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38369 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15370 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
371 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57372 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04373 return LOAD_STATE_SENDING_REQUEST;
374 case STATE_READ_HEADERS_COMPLETE:
375 return LOAD_STATE_WAITING_FOR_RESPONSE;
376 case STATE_READ_BODY_COMPLETE:
377 return LOAD_STATE_READING_RESPONSE;
378 default:
379 return LOAD_STATE_IDLE;
380 }
381}
382
[email protected]8cd06c02014-01-25 07:50:14383void HttpNetworkTransaction::SetQuicServerInfo(
384 QuicServerInfo* quic_server_info) {}
385
[email protected]5033ab82013-03-22 20:17:46386bool HttpNetworkTransaction::GetLoadTimingInfo(
387 LoadTimingInfo* load_timing_info) const {
388 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
389 return false;
390
391 load_timing_info->proxy_resolve_start =
392 proxy_info_.proxy_resolve_start_time();
393 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
394 load_timing_info->send_start = send_start_time_;
395 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46396 return true;
397}
398
ttuttled9dbc652015-09-29 20:00:59399bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36400 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59401 return false;
402
403 *endpoint = remote_endpoint_;
404 return true;
405}
406
zhongyi48704c182015-12-07 07:52:02407void HttpNetworkTransaction::PopulateNetErrorDetails(
408 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12409 *details = net_error_details_;
410 if (stream_)
411 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02412}
413
[email protected]5033ab82013-03-22 20:17:46414void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
415 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50416
rdsmith29dbad12017-02-17 02:22:18417 // TODO(rdsmith): Note that if any code indirectly executed by
418 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
419 // ever implements a throttling mechanism where changing a request's
420 // priority may cause a this or another request to synchronously succeed
421 // or fail, that callback could synchronously delete |*this|, causing
422 // a crash on return to this code.
423 //
424 // |throttle_->SetPriority()| has exactly the above attributes, which
425 // is why it's the last call in this function.
426
[email protected]bf828982013-08-14 18:01:47427 if (stream_request_)
428 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03429 if (stream_)
430 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50431
432 if (throttle_)
433 throttle_->SetPriority(priority);
434 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46435}
436
[email protected]831e4a32013-11-14 02:14:44437void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
438 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
439 websocket_handshake_stream_base_create_helper_ = create_helper;
440}
441
[email protected]1826a402014-01-08 15:40:48442void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
443 const BeforeNetworkStartCallback& callback) {
444 before_network_start_callback_ = callback;
445}
446
ryansturm49a8cb12016-06-15 16:51:09447void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
448 const BeforeHeadersSentCallback& callback) {
449 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27450}
451
Andrey Kosyakov83a6eee2017-08-14 19:20:04452void HttpNetworkTransaction::SetRequestHeadersCallback(
453 RequestHeadersCallback callback) {
454 DCHECK(!stream_);
455 request_headers_callback_ = std::move(callback);
456}
457
[email protected]1826a402014-01-08 15:40:48458int HttpNetworkTransaction::ResumeNetworkStart() {
459 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
460 return DoLoop(OK);
461}
462
[email protected]102e27c2011-02-23 01:01:31463void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
464 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00465 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50466 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38467 DCHECK(stream_request_.get());
468
sclittlefb249892015-09-10 21:33:22469 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19470 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22471 total_sent_bytes_ += stream_->GetTotalSentBytes();
472 }
bnc5029f4632017-06-08 16:19:00473 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04474 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14475 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31476 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52477 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21478 response_.alpn_negotiated_protocol =
479 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38480 response_.was_fetched_via_spdy = stream_request_->using_spdy();
481 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15482 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47483 response_.proxy_server = proxy_info_.proxy_server();
484 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
485 response_.proxy_server = ProxyServer::Direct();
486 else
487 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38488 OnIOComplete(OK);
489}
490
xunjieli5749218c2016-03-22 16:43:06491void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08492 const SSLConfig& used_ssl_config,
493 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00494 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08495 NOTREACHED();
496}
497
[email protected]a9cf2b92013-10-30 12:08:49498void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50499 const SSLConfig& used_ssl_config,
500 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00501 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
502 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50503}
504
[email protected]102e27c2011-02-23 01:01:31505void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13506 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50507 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38508 DCHECK_NE(OK, result);
509 DCHECK(stream_request_.get());
510 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14511 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38512
513 OnIOComplete(result);
514}
515
[email protected]102e27c2011-02-23 01:01:31516void HttpNetworkTransaction::OnCertificateError(
517 int result,
518 const SSLConfig& used_ssl_config,
519 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50520 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38521 DCHECK_NE(OK, result);
522 DCHECK(stream_request_.get());
523 DCHECK(!stream_.get());
524
525 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14526 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38527
528 // TODO(mbelshe): For now, we're going to pass the error through, and that
529 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50530 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
531 // good and the user chooses to ignore the error. This is not ideal, but not
532 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38533
534 OnIOComplete(result);
535}
536
537void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50538 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31539 const SSLConfig& used_ssl_config,
540 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50541 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38542 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50543 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38544
545 establishing_tunnel_ = true;
546 response_.headers = proxy_response.headers;
547 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20548
549 if (response_.headers.get() && !ContentEncodingsValid()) {
550 DoCallback(ERR_CONTENT_DECODING_FAILED);
551 return;
552 }
553
[email protected]8e6441ca2010-08-19 05:56:38554 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14555 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31556 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38557
558 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
559 pending_auth_target_ = HttpAuth::AUTH_PROXY;
560
561 DoCallback(OK);
562}
563
564void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31565 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50566 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50567 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38568
[email protected]102957f2011-09-02 17:10:14569 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38570 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58571 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38572}
573
[email protected]511f6f52010-12-17 03:58:29574void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
575 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31576 const SSLConfig& used_ssl_config,
577 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01578 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29579 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
580
ttuttle1f2d7e92015-04-28 16:17:47581 CopyConnectionAttemptsFromStreamRequest();
582
[email protected]511f6f52010-12-17 03:58:29583 headers_valid_ = true;
584 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14585 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31586 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22587 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19588 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22589 total_sent_bytes_ += stream_->GetTotalSentBytes();
590 }
Bence Béky6d05ebd2017-05-16 00:09:01591 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04592 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29593 stream_request_.reset(); // we're done with the stream request
594 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
595}
596
zhongyi48704c182015-12-07 07:52:02597void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12598 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02599}
600
ttuttle1f2d7e92015-04-28 16:17:47601void HttpNetworkTransaction::GetConnectionAttempts(
602 ConnectionAttempts* out) const {
603 *out = connection_attempts_;
604}
605
rdsmithbf8c3c12016-11-18 18:16:24606void HttpNetworkTransaction::OnThrottleUnblocked(
607 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50608 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24609 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50610 // be so in the future.
611 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
612
613 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
614
615 DoLoop(OK);
616}
617
Adam Ricecb76ac62015-02-20 05:33:25618bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37619 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52620}
621
nharperb7441ef2016-01-25 23:54:14622bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
623 if (!IsSecureRequest())
624 return false;
625 SSLInfo ssl_info;
626 stream_->GetSSLInfo(&ssl_info);
627 return ssl_info.token_binding_negotiated &&
628 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
mmenke6ddfbea2017-05-31 21:48:41629 session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14630}
631
632void HttpNetworkTransaction::RecordTokenBindingSupport() const {
633 // This enum is used for an UMA histogram - do not change or re-use values.
634 enum {
635 DISABLED = 0,
636 CLIENT_ONLY = 1,
637 CLIENT_AND_SERVER = 2,
638 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
639 TOKEN_BINDING_SUPPORT_MAX
640 } supported;
641 if (!IsSecureRequest())
642 return;
643 SSLInfo ssl_info;
644 stream_->GetSSLInfo(&ssl_info);
645 if (!session_->params().enable_token_binding) {
646 supported = DISABLED;
mmenke6ddfbea2017-05-31 21:48:41647 } else if (!session_->context().channel_id_service) {
nharperb7441ef2016-01-25 23:54:14648 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
649 } else if (ssl_info.token_binding_negotiated) {
650 supported = CLIENT_AND_SERVER;
651 } else {
652 supported = CLIENT_ONLY;
653 }
654 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
655 TOKEN_BINDING_SUPPORT_MAX);
656}
657
Adam Rice425cf122015-01-19 06:18:24658bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12659 return (proxy_info_.is_http() || proxy_info_.is_https() ||
660 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24661 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
662}
663
initial.commit586acc5fe2008-07-26 22:42:52664void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50665 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41666 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52667
[email protected]96d570e42008-08-05 22:43:04668 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41669 CompletionCallback c = callback_;
670 callback_.Reset();
671 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52672}
673
674void HttpNetworkTransaction::OnIOComplete(int result) {
675 int rv = DoLoop(result);
676 if (rv != ERR_IO_PENDING)
677 DoCallback(rv);
678}
679
680int HttpNetworkTransaction::DoLoop(int result) {
681 DCHECK(next_state_ != STATE_NONE);
682
683 int rv = result;
684 do {
685 State state = next_state_;
686 next_state_ = STATE_NONE;
687 switch (state) {
rdsmith1d343be52016-10-21 20:37:50688 case STATE_THROTTLE:
689 DCHECK_EQ(OK, rv);
690 rv = DoThrottle();
691 break;
692 case STATE_THROTTLE_COMPLETE:
693 DCHECK_EQ(OK, rv);
694 rv = DoThrottleComplete();
695 break;
[email protected]1826a402014-01-08 15:40:48696 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
697 DCHECK_EQ(OK, rv);
698 rv = DoNotifyBeforeCreateStream();
699 break;
[email protected]82918cc2010-08-25 17:24:50700 case STATE_CREATE_STREAM:
701 DCHECK_EQ(OK, rv);
702 rv = DoCreateStream();
703 break;
704 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46705 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
706 // solved.
707 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50708 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46709 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50710 break;
[email protected]351ab642010-08-05 16:55:31711 case STATE_INIT_STREAM:
712 DCHECK_EQ(OK, rv);
713 rv = DoInitStream();
714 break;
715 case STATE_INIT_STREAM_COMPLETE:
716 rv = DoInitStreamComplete(rv);
717 break;
[email protected]044de0642010-06-17 10:42:15718 case STATE_GENERATE_PROXY_AUTH_TOKEN:
719 DCHECK_EQ(OK, rv);
720 rv = DoGenerateProxyAuthToken();
721 break;
722 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
723 rv = DoGenerateProxyAuthTokenComplete(rv);
724 break;
725 case STATE_GENERATE_SERVER_AUTH_TOKEN:
726 DCHECK_EQ(OK, rv);
727 rv = DoGenerateServerAuthToken();
728 break;
729 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
730 rv = DoGenerateServerAuthTokenComplete(rv);
731 break;
nharperd6e65822016-03-30 23:05:48732 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14733 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48734 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14735 break;
nharperd6e65822016-03-30 23:05:48736 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
737 rv = DoGetProvidedTokenBindingKeyComplete(rv);
738 break;
739 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
740 DCHECK_EQ(OK, rv);
741 rv = DoGetReferredTokenBindingKey();
742 break;
743 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
744 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14745 break;
[email protected]daddea62012-09-19 05:51:13746 case STATE_INIT_REQUEST_BODY:
747 DCHECK_EQ(OK, rv);
748 rv = DoInitRequestBody();
749 break;
750 case STATE_INIT_REQUEST_BODY_COMPLETE:
751 rv = DoInitRequestBodyComplete(rv);
752 break;
[email protected]4875ba12011-03-30 22:31:51753 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55754 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00755 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51756 rv = DoBuildRequest();
757 break;
758 case STATE_BUILD_REQUEST_COMPLETE:
759 rv = DoBuildRequestComplete(rv);
760 break;
761 case STATE_SEND_REQUEST:
762 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57763 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52764 break;
[email protected]0877e3d2009-10-17 22:29:57765 case STATE_SEND_REQUEST_COMPLETE:
766 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43767 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00768 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52769 break;
770 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55771 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00772 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52773 rv = DoReadHeaders();
774 break;
775 case STATE_READ_HEADERS_COMPLETE:
776 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43777 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00778 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52779 break;
780 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55781 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00782 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52783 rv = DoReadBody();
784 break;
785 case STATE_READ_BODY_COMPLETE:
786 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52789 break;
[email protected]2d2697f92009-02-18 21:00:32790 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55791 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53792 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00793 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32794 rv = DoDrainBodyForAuthRestart();
795 break;
796 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
797 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43798 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00799 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32800 break;
initial.commit586acc5fe2008-07-26 22:42:52801 default:
802 NOTREACHED() << "bad state";
803 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04804 break;
initial.commit586acc5fe2008-07-26 22:42:52805 }
806 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
807
808 return rv;
809}
810
rdsmith1d343be52016-10-21 20:37:50811int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24812 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50813 throttle_ = session_->throttler()->CreateThrottle(
814 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
815 next_state_ = STATE_THROTTLE_COMPLETE;
816
rdsmithbf8c3c12016-11-18 18:16:24817 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50818 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
819 return ERR_IO_PENDING;
820 }
821
822 return OK;
823}
824
825int HttpNetworkTransaction::DoThrottleComplete() {
826 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24827 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50828
829 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
830
831 return OK;
832}
833
[email protected]1826a402014-01-08 15:40:48834int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
835 next_state_ = STATE_CREATE_STREAM;
836 bool defer = false;
837 if (!before_network_start_callback_.is_null())
838 before_network_start_callback_.Run(&defer);
839 if (!defer)
840 return OK;
841 return ERR_IO_PENDING;
842}
843
[email protected]82918cc2010-08-25 17:24:50844int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34845 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26846 tracked_objects::ScopedTracker tracking_profile(
847 FROM_HERE_WITH_EXPLICIT_FUNCTION(
848 "424359 HttpNetworkTransaction::DoCreateStream"));
849
ttuttlec0c828492015-05-15 01:25:55850 response_.network_accessed = true;
851
[email protected]82918cc2010-08-25 17:24:50852 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17853 // IP based pooling is only enabled on a retry after 421 Misdirected Request
854 // is received. Alternative Services are also disabled in this case (though
855 // they can also be disabled when retrying after a QUIC error).
856 if (!enable_ip_based_pooling_)
857 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44858 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27859 stream_request_ =
[email protected]0191b51c2013-11-18 10:55:23860 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44861 ->RequestWebSocketHandshakeStream(
bnc8016c1f2017-03-31 02:11:29862 *request_, priority_, server_ssl_config_, proxy_ssl_config_,
863 this, websocket_handshake_stream_base_create_helper_,
bncaccd4962017-04-06 21:00:26864 enable_ip_based_pooling_, enable_alternative_services_,
xunjieli96f2a402017-06-05 17:24:27865 net_log_);
[email protected]831e4a32013-11-14 02:14:44866 } else {
xunjieli96f2a402017-06-05 17:24:27867 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29868 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27869 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44870 }
[email protected]26816882010-10-14 18:03:09871 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38872 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31873}
874
[email protected]82918cc2010-08-25 17:24:50875int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47876 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
877 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
878 // which resets the stream request first. Therefore, we have to grab the
879 // connection attempts in *that* function instead of here in that case.
880 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
881 CopyConnectionAttemptsFromStreamRequest();
882
[email protected]394816e92010-08-03 07:38:59883 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50884 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38885 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33886 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
887 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29888 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
889 // Return OK and let the caller read the proxy's error page
890 next_state_ = STATE_NONE;
891 return OK;
bncfacdd852015-01-09 19:22:54892 } else if (result == ERR_HTTP_1_1_REQUIRED ||
893 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
894 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59895 }
896
[email protected]bd0b6772011-01-11 19:59:30897 // Handle possible handshake errors that may have occurred if the stream
898 // used SSL for one or more of the layers.
899 result = HandleSSLHandshakeError(result);
900
[email protected]8e6441ca2010-08-19 05:56:38901 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09902 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38903 return result;
[email protected]394816e92010-08-03 07:38:59904}
905
[email protected]82918cc2010-08-25 17:24:50906int HttpNetworkTransaction::DoInitStream() {
907 DCHECK(stream_.get());
908 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59909
910 stream_->GetRemoteEndpoint(&remote_endpoint_);
911
[email protected]262eec82013-03-19 21:01:36912 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50913}
914
915int HttpNetworkTransaction::DoInitStreamComplete(int result) {
916 if (result == OK) {
917 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50918 } else {
[email protected]82918cc2010-08-25 17:24:50919 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26920 result = HandleIOError(result);
921
922 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22923 if (stream_) {
924 total_received_bytes_ += stream_->GetTotalReceivedBytes();
925 total_sent_bytes_ += stream_->GetTotalSentBytes();
926 }
zhongyica364fbb2015-12-12 03:39:12927 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50928 }
929
930 return result;
931}
932
[email protected]044de0642010-06-17 10:42:15933int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
934 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
935 if (!ShouldApplyProxyAuth())
936 return OK;
[email protected]394816e92010-08-03 07:38:59937 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
938 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09939 auth_controllers_[target] =
940 new HttpAuthController(target,
941 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31942 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09943 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59944 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41945 io_callback_,
[email protected]394816e92010-08-03 07:38:59946 net_log_);
[email protected]044de0642010-06-17 10:42:15947}
948
949int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
950 DCHECK_NE(ERR_IO_PENDING, rv);
951 if (rv == OK)
952 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
953 return rv;
954}
955
956int HttpNetworkTransaction::DoGenerateServerAuthToken() {
957 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59958 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54959 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09960 auth_controllers_[target] =
961 new HttpAuthController(target,
962 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31963 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09964 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54965 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
966 auth_controllers_[target]->DisableEmbeddedIdentity();
967 }
[email protected]044de0642010-06-17 10:42:15968 if (!ShouldApplyServerAuth())
969 return OK;
[email protected]394816e92010-08-03 07:38:59970 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41971 io_callback_,
[email protected]394816e92010-08-03 07:38:59972 net_log_);
[email protected]044de0642010-06-17 10:42:15973}
974
975int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
976 DCHECK_NE(ERR_IO_PENDING, rv);
977 if (rv == OK)
nharperd6e65822016-03-30 23:05:48978 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15979 return rv;
980}
981
nharperd6e65822016-03-30 23:05:48982int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
983 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14984 if (!IsTokenBindingEnabled())
985 return OK;
986
mikecirone8b85c432016-09-08 19:11:00987 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:41988 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14989 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48990 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14991 &token_binding_request_);
992}
993
nharperd6e65822016-03-30 23:05:48994int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14995 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48996 if (IsTokenBindingEnabled()) {
997 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00998 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48999 }
1000
1001 if (rv == OK)
1002 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
1003 return rv;
1004}
1005
1006int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1007 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1008 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141009 return OK;
1010
mikecirone8b85c432016-09-08 19:11:001011 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:411012 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperd6e65822016-03-30 23:05:481013 return channel_id_service->GetOrCreateChannelID(
1014 request_->token_binding_referrer, &referred_token_binding_key_,
1015 io_callback_, &token_binding_request_);
1016}
1017
1018int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1019 DCHECK_NE(ERR_IO_PENDING, rv);
1020 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1021 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001022 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481023 }
1024 if (rv == OK)
1025 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141026 return rv;
1027}
1028
1029int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241030 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141031 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1032 GetHostAndOptionalPort(request_->url));
1033
1034 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241035 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141036 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1037 "keep-alive");
1038 } else {
1039 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1040 }
1041
[email protected]2979a492011-04-06 00:29:141042 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111043 if (request_->upload_data_stream) {
1044 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141045 request_headers_.SetHeader(
1046 HttpRequestHeaders::kTransferEncoding, "chunked");
1047 } else {
1048 request_headers_.SetHeader(
1049 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111050 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141051 }
csharrisonf473dd192015-08-18 13:54:131052 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141053 // An empty POST/PUT request still needs a content length. As for HEAD,
1054 // IE and Safari also add a content length header. Presumably it is to
1055 // support sending a HEAD request to an URL that only expects to be sent a
1056 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131057 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1058 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141059 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1060 }
1061
nharperb7441ef2016-01-25 23:54:141062 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481063 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141064 std::string token_binding_header;
1065 int rv = BuildTokenBindingHeader(&token_binding_header);
1066 if (rv != OK)
1067 return rv;
1068 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1069 token_binding_header);
1070 }
1071
[email protected]2979a492011-04-06 00:29:141072 // Honor load flags that impact proxy caches.
1073 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1074 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1075 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1076 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1077 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1078 }
1079
1080 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1081 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1082 &request_headers_);
1083 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1084 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1085 &request_headers_);
1086
[email protected]c10450102011-06-27 09:06:161087 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201088
ryansturm49a8cb12016-06-15 16:51:091089 if (!before_headers_sent_callback_.is_null())
1090 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201091
[email protected]173f8e22013-04-10 04:18:201092 response_.did_use_http_auth =
1093 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1094 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141095 return OK;
1096}
1097
1098int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471099 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141100 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351101 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1102 TokenBindingType::PROVIDED,
1103 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141104 if (rv != OK)
1105 return rv;
1106 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481107 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1108 provided_token_binding_key_.get(), signed_ekm,
1109 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141110 if (rv != OK)
1111 return rv;
nharperd6e65822016-03-30 23:05:481112
nharperb7441ef2016-01-25 23:54:141113 std::vector<base::StringPiece> token_bindings;
1114 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481115
1116 std::string referred_token_binding;
1117 if (referred_token_binding_key_) {
1118 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351119 int rv = stream_->GetTokenBindingSignature(
1120 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1121 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481122 if (rv != OK)
1123 return rv;
1124 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1125 referred_token_binding_key_.get(),
1126 referred_signed_ekm, &referred_token_binding);
1127 if (rv != OK)
1128 return rv;
1129 token_bindings.push_back(referred_token_binding);
1130 }
nharperb7441ef2016-01-25 23:54:141131 std::string header;
1132 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1133 if (rv != OK)
1134 return rv;
nharper78e6d2b2016-09-21 05:42:351135 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471136 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1137 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1138 header_creation_time,
1139 base::TimeDelta::FromMilliseconds(1),
1140 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141141 return OK;
[email protected]2979a492011-04-06 00:29:141142}
1143
[email protected]daddea62012-09-19 05:51:131144int HttpNetworkTransaction::DoInitRequestBody() {
1145 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131146 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111147 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521148 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131149 return rv;
1150}
[email protected]4875ba12011-03-30 22:31:511151
[email protected]daddea62012-09-19 05:51:131152int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1153 if (result == OK)
1154 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131155 return result;
1156}
1157
1158int HttpNetworkTransaction::DoBuildRequest() {
1159 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511160 headers_valid_ = false;
1161
1162 // This is constructed lazily (instead of within our Start method), so that
1163 // we have proxy info available.
1164 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241165 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141166 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511167 }
1168
[email protected]4875ba12011-03-30 22:31:511169 return OK;
1170}
1171
1172int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241173 if (result == OK)
1174 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511175 return result;
1176}
1177
[email protected]0877e3d2009-10-17 22:29:571178int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341179 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261180 tracked_objects::ScopedTracker tracking_profile(
1181 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1182 "424359 HttpNetworkTransaction::DoSendRequest"));
1183
[email protected]58e32bb2013-01-21 18:23:251184 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571185 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1186
[email protected]bf3eb002012-11-15 05:50:111187 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521188}
1189
[email protected]0877e3d2009-10-17 22:29:571190int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251191 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361192
1193 if (result == ERR_HTTP_1_1_REQUIRED ||
1194 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1195 return HandleHttp11Required(result);
1196 }
1197
initial.commit586acc5fe2008-07-26 22:42:521198 if (result < 0)
1199 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571200 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521201 return OK;
1202}
1203
1204int HttpNetworkTransaction::DoReadHeaders() {
1205 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411206 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521207}
1208
1209int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111210 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1211 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381212 if (IsCertificateError(result)) {
1213 // We don't handle a certificate error during SSL renegotiation, so we
1214 // have to return an error that's not in the certificate error range
1215 // (-2xx).
1216 LOG(ERROR) << "Got a server certificate with error " << result
1217 << " during SSL renegotiation";
1218 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1219 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1220 // TODO(wtc): Need a test case for this code path!
1221 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251222 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381223 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501224 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381225 result = HandleCertificateRequest(result);
1226 if (result == OK)
1227 return result;
[email protected]2181ea002009-06-09 01:37:271228 }
1229
bncfacdd852015-01-09 19:22:541230 if (result == ERR_HTTP_1_1_REQUIRED ||
1231 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1232 return HandleHttp11Required(result);
1233 }
1234
[email protected]c871864d72014-03-13 19:56:191235 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1236 // response headers were received, we do the best we can to make sense of it
1237 // and send it back up the stack.
1238 //
1239 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1240 // bizarre for SPDY. Assuming this logic is useful at all.
1241 // TODO(davidben): Bubble the error code up so we do not cache?
1242 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1243 result = OK;
1244
1245 if (result < 0)
1246 return HandleIOError(result);
1247
[email protected]90499482013-06-01 00:39:501248 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521249
eustasc7d27da2017-04-06 10:33:201250 if (response_.headers.get() && !ContentEncodingsValid())
1251 return ERR_CONTENT_DECODING_FAILED;
1252
[email protected]d58ceea82014-06-04 10:55:541253 // On a 408 response from the server ("Request Timeout") on a stale socket,
1254 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041255 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181256 if (response_.headers.get() &&
1257 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541258 stream_->IsConnectionReused()) {
1259 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001260 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541261 response_.headers->response_code());
1262 // This will close the socket - it would be weird to try and reuse it, even
1263 // if the server doesn't actually close it.
1264 ResetConnectionAndRequestForResend();
1265 return OK;
1266 }
1267
[email protected]93f8b562012-03-27 01:00:161268 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211269 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161270 const int response_code = response_.headers->response_code();
1271 UMA_HISTOGRAM_ENUMERATION(
1272 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1273 }
1274
[email protected]3abacd62012-06-10 20:20:321275 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001276 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321277 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391278
bncbe0f6af2015-10-15 17:49:561279 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571280 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1281 // indicates a buggy server. See:
1282 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1283 if (request_->method == "PUT")
1284 return ERR_METHOD_NOT_SUPPORTED;
1285 }
[email protected]4ddaf2502008-10-23 18:26:191286
[email protected]0877e3d2009-10-17 22:29:571287 // Check for an intermediate 100 Continue response. An origin server is
1288 // allowed to send this response even if we didn't ask for it, so we just
1289 // need to skip over it.
1290 // We treat any other 1xx in this same way (although in practice getting
1291 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441292 // Unless this is a WebSocket request, in which case we pass it on up.
1293 if (response_.headers->response_code() / 100 == 1 &&
1294 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451295 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571296 next_state_ = STATE_READ_HEADERS;
1297 return OK;
1298 }
1299
davidbence688ae2017-05-04 15:12:591300 if (response_.headers->response_code() == 421 &&
1301 (enable_ip_based_pooling_ || enable_alternative_services_)) {
1302 // Retry the request with both IP based pooling and Alternative Services
1303 // disabled.
1304 enable_ip_based_pooling_ = false;
1305 enable_alternative_services_ = false;
1306 net_log_.AddEvent(
1307 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1308 ResetConnectionAndRequestForResend();
1309 return OK;
bnc8016c1f2017-03-31 02:11:291310 }
1311
bncb26024382016-06-29 02:39:451312 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111313 session_->http_stream_factory()->ProcessAlternativeServices(
1314 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1315 }
[email protected]564b4912010-03-09 16:30:421316
asanka5ffd5d72016-03-23 16:20:491317 if (IsSecureRequest())
1318 stream_->GetSSLInfo(&response_.ssl_info);
1319
[email protected]e772db3f2010-07-12 18:11:131320 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571321 if (rv != OK)
1322 return rv;
1323
[email protected]0877e3d2009-10-17 22:29:571324 headers_valid_ = true;
1325 return OK;
initial.commit586acc5fe2008-07-26 22:42:521326}
1327
1328int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501329 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131330 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381331 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521332
1333 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501334 return stream_->ReadResponseBody(
1335 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521336}
1337
1338int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1339 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381340 bool done = false;
1341 if (result <= 0) {
1342 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521343 done = true;
[email protected]8e6441ca2010-08-19 05:56:381344 }
[email protected]9492e4a2010-02-24 00:58:461345
mmenkebd84c392015-09-02 14:12:341346 // Clean up connection if we are done.
1347 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381348 // Note: Just because IsResponseBodyComplete is true, we're not
1349 // necessarily "done". We're only "done" when it is the last
1350 // read on this HttpNetworkTransaction, which will be signified
1351 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341352 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381353 // the stream. No need to compute it here just to pass back
1354 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341355 bool keep_alive =
1356 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521357
[email protected]8e6441ca2010-08-19 05:56:381358 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011359 // Note: we don't reset the stream here. We've closed it, but we still
1360 // need it around so that callers can call methods such as
1361 // GetUploadProgress() and have them be meaningful.
1362 // TODO(mbelshe): This means we closed the stream here, and we close it
1363 // again in ~HttpNetworkTransaction. Clean that up.
1364
[email protected]8e6441ca2010-08-19 05:56:381365 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171366
1367 // This transaction was successful. If it had been retried because of an
1368 // error with an alternative service, mark that alternative service broken.
1369 if (!enable_alternative_services_ &&
1370 retried_alternative_service_.protocol != kProtoUnknown) {
1371 session_->http_server_properties()->MarkAlternativeServiceBroken(
1372 retried_alternative_service_);
1373 }
initial.commit586acc5fe2008-07-26 22:42:521374 }
1375
1376 // Clear these to avoid leaving around old state.
1377 read_buf_ = NULL;
1378 read_buf_len_ = 0;
1379
1380 return result;
1381}
1382
[email protected]2d2697f92009-02-18 21:00:321383int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1384 // This method differs from DoReadBody only in the next_state_. So we just
1385 // call DoReadBody and override the next_state_. Perhaps there is a more
1386 // elegant way for these two methods to share code.
1387 int rv = DoReadBody();
1388 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1389 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1390 return rv;
1391}
1392
[email protected]0877e3d2009-10-17 22:29:571393// TODO(wtc): This method and the DoReadBodyComplete method are almost
1394// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321395int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231396 // keep_alive defaults to true because the very reason we're draining the
1397 // response body is to reuse the connection for auth restart.
1398 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321399 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571400 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321401 done = true;
[email protected]68873ba2009-06-04 21:49:231402 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311403 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571404 done = true;
[email protected]2d2697f92009-02-18 21:00:321405 }
1406
1407 if (done) {
1408 DidDrainBodyForAuthRestart(keep_alive);
1409 } else {
1410 // Keep draining.
1411 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1412 }
1413
1414 return OK;
1415}
1416
[email protected]5e363962009-06-19 19:57:011417int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381418 // There are two paths through which the server can request a certificate
1419 // from us. The first is during the initial handshake, the second is
1420 // during SSL renegotiation.
1421 //
1422 // In both cases, we want to close the connection before proceeding.
1423 // We do this for two reasons:
1424 // First, we don't want to keep the connection to the server hung for a
1425 // long time while the user selects a certificate.
1426 // Second, even if we did keep the connection open, NSS has a bug where
1427 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581428 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381429
1430 if (stream_.get()) {
1431 // Since we already have a stream, we're being called as part of SSL
1432 // renegotiation.
1433 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191434 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221435 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381436 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121437 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381438 }
1439
[email protected]26816882010-10-14 18:03:091440 // The server is asking for a client certificate during the initial
1441 // handshake.
1442 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011443
[email protected]ec229bc92010-11-22 09:51:451444 // If the user selected one of the certificates in client_certs or declined
1445 // to provide one for this server before, use the past decision
1446 // automatically.
1447 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541448 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451449 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541450 response_.cert_request_info->host_and_port, &client_cert,
1451 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451452 if (!found_cached_cert)
1453 return error;
1454
1455 // Check that the certificate selected is still a certificate the server
1456 // is likely to accept, based on the criteria supplied in the
1457 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501458 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261459 const std::vector<std::string>& cert_authorities =
1460 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451461
[email protected]f1958c382013-02-07 00:15:261462 bool cert_still_valid = cert_authorities.empty() ||
1463 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451464 if (!cert_still_valid)
1465 return error;
[email protected]5e363962009-06-19 19:57:011466 }
[email protected]ec229bc92010-11-22 09:51:451467
1468 // TODO(davidben): Add a unit test which covers this path; we need to be
1469 // able to send a legitimate certificate and also bypass/clear the
1470 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141471 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1472 &proxy_ssl_config_ : &server_ssl_config_;
1473 ssl_config->send_client_cert = true;
1474 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541475 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451476 next_state_ = STATE_CREATE_STREAM;
1477 // Reset the other member variables.
1478 // Note: this is necessary only with SSL renegotiation.
1479 ResetStateForRestart();
1480 return OK;
[email protected]0b45559b2009-06-12 21:45:111481}
1482
bncfacdd852015-01-09 19:22:541483int HttpNetworkTransaction::HandleHttp11Required(int error) {
1484 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1485 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1486
1487 if (error == ERR_HTTP_1_1_REQUIRED) {
1488 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1489 } else {
1490 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1491 }
1492 ResetConnectionAndRequestForResend();
1493 return OK;
1494}
1495
[email protected]384cc73a2013-12-08 22:41:031496void HttpNetworkTransaction::HandleClientAuthError(int error) {
1497 if (server_ssl_config_.send_client_cert &&
1498 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1499 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411500 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031501 }
1502}
1503
[email protected]bd0b6772011-01-11 19:59:301504// TODO(rch): This does not correctly handle errors when an SSL proxy is
1505// being used, as all of the errors are handled as if they were generated
1506// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381507// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301508int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1509 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031510 HandleClientAuthError(error);
initial.commit586acc5fe2008-07-26 22:42:521511 return error;
1512}
1513
[email protected]bd0b6772011-01-11 19:59:301514// This method determines whether it is safe to resend the request after an
1515// IO error. It can only be called in response to request header or body
1516// write errors or response header read errors. It should not be used in
1517// other cases, such as a Connect error.
1518int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031519 // Because the peer may request renegotiation with client authentication at
1520 // any time, check and handle client authentication errors.
1521 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301522
1523 switch (error) {
1524 // If we try to reuse a connection that the server is in the process of
1525 // closing, we may end up successfully writing out our request (or a
1526 // portion of our request) only to find a connection error when we try to
1527 // read from (or finish writing to) the socket.
1528 case ERR_CONNECTION_RESET:
1529 case ERR_CONNECTION_CLOSED:
1530 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511531 // There can be a race between the socket pool checking checking whether a
1532 // socket is still connected, receiving the FIN, and sending/reading data
1533 // on a reused socket. If we receive the FIN between the connectedness
1534 // check and writing/reading from the socket, we may first learn the socket
1535 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1536 // likely happen when trying to retrieve its IP address.
1537 // See http://crbug.com/105824 for more details.
1538 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491539 // If a socket is closed on its initial request, HttpStreamParser returns
1540 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1541 // preconnected but failed to be used before the server timed it out.
1542 case ERR_EMPTY_RESPONSE:
1543 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381544 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001545 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301546 ResetConnectionAndRequestForResend();
1547 error = OK;
1548 }
1549 break;
[email protected]8753a122011-10-16 08:05:081550 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001551 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271552 case ERR_QUIC_HANDSHAKE_FAILED:
Biljith Jayan45a41722017-08-16 18:43:141553 if (HasExceededMaxRetries())
1554 break;
[email protected]b6754252012-06-13 23:14:381555 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001556 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141557 retry_attempts_++;
[email protected]721c0ce2011-10-13 02:41:001558 ResetConnectionAndRequestForResend();
1559 error = OK;
1560 break;
rch2f2991c2017-04-13 19:28:171561 case ERR_QUIC_PROTOCOL_ERROR:
1562 if (GetResponseHeaders() != nullptr ||
1563 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1564 // If the response headers have already been recieved and passed up
1565 // then the request can not be retried. Also, if there was no
1566 // alternative service used for this request, then there is no
1567 // alternative service to be disabled.
1568 break;
1569 }
Biljith Jayan45a41722017-08-16 18:43:141570 if (HasExceededMaxRetries())
1571 break;
rch2f2991c2017-04-13 19:28:171572 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1573 retried_alternative_service_)) {
1574 // If the alternative service was marked as broken while the request
1575 // was in flight, retry the request which will not use the broken
1576 // alternative service.
1577 net_log_.AddEventWithNetErrorCode(
1578 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141579 retry_attempts_++;
rch2f2991c2017-04-13 19:28:171580 ResetConnectionAndRequestForResend();
1581 error = OK;
1582 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1583 // Disable alternative services for this request and retry it. If the
1584 // retry succeeds, then the alternative service will be marked as
1585 // broken then.
1586 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361587 net_log_.AddEventWithNetErrorCode(
1588 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
Biljith Jayan45a41722017-08-16 18:43:141589 retry_attempts_++;
rch514a44a82017-04-13 04:46:361590 ResetConnectionAndRequestForResend();
1591 error = OK;
1592 }
1593 break;
[email protected]bd0b6772011-01-11 19:59:301594 }
1595 return error;
1596}
1597
[email protected]c3b35c22008-09-27 03:19:421598void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581599 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221600 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191601 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221602 total_sent_bytes_ += stream_->GetTotalSentBytes();
1603 }
zhongyica364fbb2015-12-12 03:39:121604 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581605}
1606
1607void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251608 send_start_time_ = base::TimeTicks();
1609 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251610
[email protected]0757e7702009-03-27 04:00:221611 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421612 read_buf_ = NULL;
1613 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571614 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201615 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141616 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381617 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591618 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121619 net_error_details_.quic_broken = false;
1620 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481621 provided_token_binding_key_.reset();
1622 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121623}
1624
1625void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1626 if (stream_)
1627 stream_->PopulateNetErrorDetails(&net_error_details_);
1628 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571629}
1630
[email protected]0877e3d2009-10-17 22:29:571631HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501632 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421633}
1634
[email protected]a34f61ee2014-03-18 20:59:491635bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381636 bool connection_is_proven = stream_->IsConnectionReused();
1637 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161638
[email protected]2a5c76b2008-09-25 22:15:161639 // NOTE: we resend a request only if we reused a keep-alive connection.
1640 // This automatically prevents an infinite resend loop because we'll run
1641 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381642 if (connection_is_proven && !has_received_headers)
1643 return true;
1644 return false;
[email protected]1c773ea12009-04-28 19:58:421645}
1646
Biljith Jayan45a41722017-08-16 18:43:141647bool HttpNetworkTransaction::HasExceededMaxRetries() const {
1648 return (retry_attempts_ >= kMaxRetryAttempts);
1649}
1650
[email protected]1c773ea12009-04-28 19:58:421651void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381652 if (stream_.get()) {
1653 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121654 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161655 }
1656
[email protected]0877e3d2009-10-17 22:29:571657 // We need to clear request_headers_ because it contains the real request
1658 // headers, but we may need to resend the CONNECT request first to recreate
1659 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201660 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501661 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541662}
1663
[email protected]1c773ea12009-04-28 19:58:421664bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241665 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421666}
license.botbf09a502008-08-24 00:55:551667
[email protected]1c773ea12009-04-28 19:58:421668bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041669 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421670}
1671
[email protected]e772db3f2010-07-12 18:11:131672int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271673 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501674 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421675
[email protected]0877e3d2009-10-17 22:29:571676 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581677 if (status != HTTP_UNAUTHORIZED &&
1678 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421679 return OK;
[email protected]9094b602012-02-27 21:44:581680 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111681 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161682 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1683 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421684
[email protected]9094b602012-02-27 21:44:581685 // This case can trigger when an HTTPS server responds with a "Proxy
1686 // authentication required" status code through a non-authenticating
1687 // proxy.
[email protected]7a67a8152010-11-05 18:31:101688 if (!auth_controllers_[target].get())
1689 return ERR_UNEXPECTED_PROXY_AUTH;
1690
[email protected]a7ea8832010-07-12 17:54:541691 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491692 headers, response_.ssl_info,
1693 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311694 net_log_);
[email protected]228404f2010-06-24 04:31:411695 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491696 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411697
1698 scoped_refptr<AuthChallengeInfo> auth_info =
1699 auth_controllers_[target]->auth_info();
1700 if (auth_info.get())
1701 response_.auth_challenge = auth_info;
1702
[email protected]228404f2010-06-24 04:31:411703 return rv;
[email protected]f9ee6b52008-11-08 06:46:231704}
1705
[email protected]8e6441ca2010-08-19 05:56:381706bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1707 return auth_controllers_[target].get() &&
1708 auth_controllers_[target]->HaveAuth();
1709}
1710
[email protected]228404f2010-06-24 04:31:411711GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1712 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461713 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411714 if (!proxy_info_.proxy_server().is_valid() ||
1715 proxy_info_.proxy_server().is_direct()) {
1716 return GURL(); // There is no proxy server.
1717 }
[email protected]2df19bb2010-08-25 20:13:461718 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1719 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351720 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461721 }
[email protected]228404f2010-06-24 04:31:411722 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291723 if (ForWebSocketHandshake()) {
1724 const GURL& url = request_->url;
1725 url::Replacements<char> ws_to_http;
1726 if (url.SchemeIs("ws")) {
1727 ws_to_http.SetScheme("http", url::Component(0, 4));
1728 } else {
1729 DCHECK(url.SchemeIs("wss"));
1730 ws_to_http.SetScheme("https", url::Component(0, 5));
1731 }
1732 return url.ReplaceComponents(ws_to_http);
1733 }
[email protected]228404f2010-06-24 04:31:411734 return request_->url;
1735 default:
1736 return GURL();
1737 }
[email protected]c3b35c22008-09-27 03:19:421738}
1739
[email protected]831e4a32013-11-14 02:14:441740bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141741 return websocket_handshake_stream_base_create_helper_ &&
1742 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441743}
1744
ttuttle1f2d7e92015-04-28 16:17:471745void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1746 DCHECK(stream_request_);
1747
1748 // Since the transaction can restart with auth credentials, it may create a
1749 // stream more than once. Accumulate all of the connection attempts across
1750 // those streams by appending them to the vector:
1751 for (const auto& attempt : stream_request_->connection_attempts())
1752 connection_attempts_.push_back(attempt);
1753}
1754
eustasc7d27da2017-04-06 10:33:201755bool HttpNetworkTransaction::ContentEncodingsValid() const {
1756 HttpResponseHeaders* headers = GetResponseHeaders();
1757 DCHECK(headers);
1758
1759 std::string accept_encoding;
1760 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1761 &accept_encoding);
1762 std::set<std::string> allowed_encodings;
1763 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1764 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1765 return false;
1766 }
1767
1768 std::string content_encoding;
1769 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1770 std::set<std::string> used_encodings;
1771 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1772 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1773 return false;
1774 }
1775
1776 // When "Accept-Encoding" is not specified, it is parsed as "*".
1777 // If "*" encoding is advertised, then any encoding should be "accepted".
1778 // This does not mean, that it will be successfully decoded.
1779 if (allowed_encodings.find("*") != allowed_encodings.end())
1780 return true;
1781
sky50576f32017-05-01 19:28:031782 bool result = true;
eustasc7d27da2017-04-06 10:33:201783 for (auto const& encoding : used_encodings) {
1784 SourceStream::SourceType source_type =
1785 FilterSourceStream::ParseEncodingType(encoding);
1786 // We don't reject encodings we are not aware. They just will not decode.
1787 if (source_type == SourceStream::TYPE_UNKNOWN)
1788 continue;
1789 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031790 FilterSourceStream::ReportContentDecodingFailed(
1791 SourceStream::TYPE_REJECTED);
1792 result = false;
1793 break;
eustasc7d27da2017-04-06 10:33:201794 }
1795 }
sky50576f32017-05-01 19:28:031796
1797 // Temporary workaround for http://crbug.com/714514
1798 if (headers->IsRedirect(nullptr)) {
1799 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1800 !result);
1801 return true;
1802 }
1803
1804 return result;
eustasc7d27da2017-04-06 10:33:201805}
1806
[email protected]c3b35c22008-09-27 03:19:421807} // namespace net