blob: 5637236e68df25da7d09da61db2def4aa9ddadc5 [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
initial.commit586acc5fe2008-07-26 22:42:5271namespace net {
72
[email protected]262eec82013-03-19 21:01:3673HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
74 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2275 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3276 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
77 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5278 session_(session),
79 request_(NULL),
[email protected]262eec82013-03-19 21:01:3680 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5781 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2082 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5283 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:1984 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:2285 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:5486 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:4487 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:2988 enable_ip_based_pooling_(true),
bncaccd4962017-04-06 21:00:2689 enable_alternative_services_(true),
zhongyi48704c182015-12-07 07:52:0290 websocket_handshake_stream_base_create_helper_(NULL),
bnc8016c1f2017-03-31 02:11:2991 net_error_details_() {}
[email protected]3ce7df0f2010-03-03 00:30:5092
[email protected]0b0bf032010-09-21 18:08:5093HttpNetworkTransaction::~HttpNetworkTransaction() {
94 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:5095 // TODO(mbelshe): The stream_ should be able to compute whether or not the
96 // stream should be kept alive. No reason to compute here
97 // and pass it in.
mmenkebd84c392015-09-02 14:12:3498 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:5099 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34100 } else if (stream_->IsResponseBodyComplete()) {
101 // If the response body is complete, we can just reuse the socket.
102 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50103 } else {
mmenkebd84c392015-09-02 14:12:34104 // Otherwise, we try to drain the response body.
105 HttpStream* stream = stream_.release();
106 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50107 }
108 }
[email protected]02cad5d2013-10-02 08:14:03109 if (request_ && request_->upload_data_stream)
110 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50111}
112
[email protected]684970b2009-08-14 04:54:46113int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41114 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20115 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53116 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04117 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15118 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04119
nharper8cdb0fb2016-04-22 21:34:59120 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
121 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
122
[email protected]99ffa5a2011-10-06 04:20:19123 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14124 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19125 proxy_ssl_config_.rev_checking_enabled = false;
126 }
[email protected]6fbac162011-06-20 00:29:04127
jkarlinfb1d5172015-01-12 14:10:29128 if (request_->load_flags & LOAD_PREFETCH)
129 response_.unused_since_prefetch = true;
130
rdsmith1d343be52016-10-21 20:37:50131 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04132 int rv = DoLoop(OK);
133 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41134 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04135 return rv;
136}
137
138int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41139 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38140 DCHECK(!stream_.get());
141 DCHECK(!stream_request_.get());
142 DCHECK_EQ(STATE_NONE, next_state_);
143
[email protected]82918cc2010-08-25 17:24:50144 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38145
[email protected]ccb40e52008-09-17 20:54:40146 int rv = DoLoop(OK);
147 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41148 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11149 return rv;
[email protected]96d570e42008-08-05 22:43:04150}
151
[email protected]0b45559b2009-06-12 21:45:11152int HttpNetworkTransaction::RestartWithCertificate(
mattm436ccfe2017-06-19 20:24:08153 scoped_refptr<X509Certificate> client_cert,
154 scoped_refptr<SSLPrivateKey> client_private_key,
svaldez7872fd02015-11-19 21:10:54155 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38156 // In HandleCertificateRequest(), we always tear down existing stream
157 // requests to force a new connection. So we shouldn't have one here.
158 DCHECK(!stream_request_.get());
159 DCHECK(!stream_.get());
160 DCHECK_EQ(STATE_NONE, next_state_);
161
[email protected]102957f2011-09-02 17:10:14162 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
163 &proxy_ssl_config_ : &server_ssl_config_;
164 ssl_config->send_client_cert = true;
165 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54166 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45167 session_->ssl_client_auth_cache()->Add(
mattm436ccfe2017-06-19 20:24:08168 response_.cert_request_info->host_and_port, std::move(client_cert),
169 std::move(client_private_key));
[email protected]0b45559b2009-06-12 21:45:11170 // Reset the other member variables.
171 // Note: this is necessary only with SSL renegotiation.
172 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50173 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11174 int rv = DoLoop(OK);
175 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41176 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11177 return rv;
178}
179
[email protected]49639fa2011-12-20 23:22:41180int HttpNetworkTransaction::RestartWithAuth(
181 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22182 HttpAuth::Target target = pending_auth_target_;
183 if (target == HttpAuth::AUTH_NONE) {
184 NOTREACHED();
185 return ERR_UNEXPECTED;
186 }
[email protected]0757e7702009-03-27 04:00:22187 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42188
[email protected]f3cf9802011-10-28 18:44:58189 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13190
[email protected]49639fa2011-12-20 23:22:41191 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38192
193 int rv = OK;
194 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
195 // In this case, we've gathered credentials for use with proxy
196 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50197 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38198 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59199 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54200 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39201 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54202 } else {
[email protected]8e6441ca2010-08-19 05:56:38203 // In this case, we've gathered credentials for the server or the proxy
204 // but it is not during the tunneling phase.
205 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54206 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38207 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54208 }
[email protected]c3b35c22008-09-27 03:19:42209
[email protected]c3b35c22008-09-27 03:19:42210 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41211 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42212 return rv;
[email protected]96d570e42008-08-05 22:43:04213}
214
[email protected]f9ee6b52008-11-08 06:46:23215void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
216 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38217 DCHECK(!stream_request_.get());
218
[email protected]2d2697f92009-02-18 21:00:32219 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57220 // Even if the server says the connection is keep-alive, we have to be
221 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34222 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57223 // If the response body hasn't been completely read, we need to drain
224 // it first.
[email protected]351ab642010-08-05 16:55:31225 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32226 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57227 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32228 read_buf_len_ = kDrainBodyBufferSize;
229 return;
230 }
[email protected]0877e3d2009-10-17 22:29:57231 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09232 }
233
[email protected]2d2697f92009-02-18 21:00:32234 // We don't need to drain the response body, so we act as if we had drained
235 // the response body.
236 DidDrainBodyForAuthRestart(keep_alive);
237}
238
239void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38240 DCHECK(!stream_request_.get());
241
242 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19243 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22244 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58245 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34246 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38247 // We should call connection_->set_idle_time(), but this doesn't occur
248 // often enough to be worth the trouble.
249 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39250 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38251 }
[email protected]697ef4c2010-10-14 16:38:58252
253 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46254 // Close the stream and mark it as not_reusable. Even in the
255 // keep_alive case, we've determined that the stream_ is not
256 // reusable if new_stream is NULL.
257 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58258 next_state_ = STATE_CREATE_STREAM;
259 } else {
sclittlefb249892015-09-10 21:33:22260 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19261 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22262 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58263 next_state_ = STATE_INIT_STREAM;
264 }
265 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32266 }
[email protected]f9ee6b52008-11-08 06:46:23267
268 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58269 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23270}
271
[email protected]8e6441ca2010-08-19 05:56:38272bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
273 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
274 HaveAuth(pending_auth_target_);
275}
276
[email protected]9dea9e1f2009-01-29 00:30:47277int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41278 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04279 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35280 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04281
[email protected]ad8e04a2010-11-01 04:16:27282 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38283 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04284 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29285 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04286 // bytes when establishing a tunnel because they might be controlled by
287 // an active network attacker. We don't worry about this for HTTP
288 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29289 // We reach this case when the user cancels a 407 proxy auth prompt. We
290 // also don't worry about this for an HTTPS Proxy, because the
291 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04292 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46293 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58294 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54295 LOG(WARNING) << "Blocked proxy response with status "
296 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15297 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04298 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44299 }
300
[email protected]e60e47a2010-07-14 03:37:18301 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15302 next_state_ = STATE_READ_BODY;
303
304 // We have reached the end of Start state machine, reset the requestinfo to
305 // null.
306 // RequestInfo is a member of the HttpTransaction's consumer and is useful
307 // only till final response headers are received. A reset will ensure that
308 // HttpRequestInfo is only used up until final response headers are received.
309 // Resetting is allowed so that the transaction can be disassociated from its
310 // creating consumer in cases where it is shared for writing to the cache.
311 // It is also safe to reset it to null at this point since upload_data_stream
312 // is also not used in the Read state machine.
313 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18314
[email protected]96d570e42008-08-05 22:43:04315 read_buf_ = buf;
316 read_buf_len_ = buf_len;
317
[email protected]96d570e42008-08-05 22:43:04318 int rv = DoLoop(OK);
319 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41320 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04321 return rv;
322}
323
[email protected]8cd06c02014-01-25 07:50:14324void HttpNetworkTransaction::StopCaching() {}
325
[email protected]79e1fd62013-06-20 06:50:04326bool HttpNetworkTransaction::GetFullRequestHeaders(
327 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32328 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04329 *headers = request_headers_;
330 return true;
331}
332
sclittle4de1bab92015-09-22 21:28:24333int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
334 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19335 if (stream_)
336 total_received_bytes += stream_->GetTotalReceivedBytes();
337 return total_received_bytes;
338}
339
sclittlefb249892015-09-10 21:33:22340int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
341 int64_t total_sent_bytes = total_sent_bytes_;
342 if (stream_)
343 total_sent_bytes += stream_->GetTotalSentBytes();
344 return total_sent_bytes;
345}
346
[email protected]8cd06c02014-01-25 07:50:14347void HttpNetworkTransaction::DoneReading() {}
348
[email protected]96d570e42008-08-05 22:43:04349const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55350 return &response_;
[email protected]96d570e42008-08-05 22:43:04351}
352
353LoadState HttpNetworkTransaction::GetLoadState() const {
354 // TODO(wtc): Define a new LoadState value for the
355 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
356 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50357 case STATE_THROTTLE_COMPLETE:
358 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48359 case STATE_CREATE_STREAM:
360 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50361 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38362 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15363 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
364 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57365 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04366 return LOAD_STATE_SENDING_REQUEST;
367 case STATE_READ_HEADERS_COMPLETE:
368 return LOAD_STATE_WAITING_FOR_RESPONSE;
369 case STATE_READ_BODY_COMPLETE:
370 return LOAD_STATE_READING_RESPONSE;
371 default:
372 return LOAD_STATE_IDLE;
373 }
374}
375
[email protected]8cd06c02014-01-25 07:50:14376void HttpNetworkTransaction::SetQuicServerInfo(
377 QuicServerInfo* quic_server_info) {}
378
[email protected]5033ab82013-03-22 20:17:46379bool HttpNetworkTransaction::GetLoadTimingInfo(
380 LoadTimingInfo* load_timing_info) const {
381 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
382 return false;
383
384 load_timing_info->proxy_resolve_start =
385 proxy_info_.proxy_resolve_start_time();
386 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
387 load_timing_info->send_start = send_start_time_;
388 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46389 return true;
390}
391
ttuttled9dbc652015-09-29 20:00:59392bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36393 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59394 return false;
395
396 *endpoint = remote_endpoint_;
397 return true;
398}
399
zhongyi48704c182015-12-07 07:52:02400void HttpNetworkTransaction::PopulateNetErrorDetails(
401 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12402 *details = net_error_details_;
403 if (stream_)
404 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02405}
406
[email protected]5033ab82013-03-22 20:17:46407void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
408 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50409
rdsmith29dbad12017-02-17 02:22:18410 // TODO(rdsmith): Note that if any code indirectly executed by
411 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
412 // ever implements a throttling mechanism where changing a request's
413 // priority may cause a this or another request to synchronously succeed
414 // or fail, that callback could synchronously delete |*this|, causing
415 // a crash on return to this code.
416 //
417 // |throttle_->SetPriority()| has exactly the above attributes, which
418 // is why it's the last call in this function.
419
[email protected]bf828982013-08-14 18:01:47420 if (stream_request_)
421 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03422 if (stream_)
423 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50424
425 if (throttle_)
426 throttle_->SetPriority(priority);
427 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46428}
429
[email protected]831e4a32013-11-14 02:14:44430void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
431 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
432 websocket_handshake_stream_base_create_helper_ = create_helper;
433}
434
[email protected]1826a402014-01-08 15:40:48435void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
436 const BeforeNetworkStartCallback& callback) {
437 before_network_start_callback_ = callback;
438}
439
ryansturm49a8cb12016-06-15 16:51:09440void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
441 const BeforeHeadersSentCallback& callback) {
442 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27443}
444
Andrey Kosyakov83a6eee2017-08-14 19:20:04445void HttpNetworkTransaction::SetRequestHeadersCallback(
446 RequestHeadersCallback callback) {
447 DCHECK(!stream_);
448 request_headers_callback_ = std::move(callback);
449}
450
[email protected]1826a402014-01-08 15:40:48451int HttpNetworkTransaction::ResumeNetworkStart() {
452 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
453 return DoLoop(OK);
454}
455
[email protected]102e27c2011-02-23 01:01:31456void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
457 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00458 std::unique_ptr<HttpStream> stream) {
[email protected]82918cc2010-08-25 17:24:50459 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38460 DCHECK(stream_request_.get());
461
sclittlefb249892015-09-10 21:33:22462 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19463 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22464 total_sent_bytes_ += stream_->GetTotalSentBytes();
465 }
bnc5029f4632017-06-08 16:19:00466 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04467 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]102957f2011-09-02 17:10:14468 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31469 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52470 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21471 response_.alpn_negotiated_protocol =
472 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38473 response_.was_fetched_via_spdy = stream_request_->using_spdy();
474 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15475 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47476 response_.proxy_server = proxy_info_.proxy_server();
477 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
478 response_.proxy_server = ProxyServer::Direct();
479 else
480 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38481 OnIOComplete(OK);
482}
483
xunjieli5749218c2016-03-22 16:43:06484void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08485 const SSLConfig& used_ssl_config,
486 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00487 std::unique_ptr<BidirectionalStreamImpl> stream) {
xunjieli11834f02015-12-22 04:27:08488 NOTREACHED();
489}
490
[email protected]a9cf2b92013-10-30 12:08:49491void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50492 const SSLConfig& used_ssl_config,
493 const ProxyInfo& used_proxy_info,
bnc5029f4632017-06-08 16:19:00494 std::unique_ptr<WebSocketHandshakeStreamBase> stream) {
495 OnStreamReady(used_ssl_config, used_proxy_info, std::move(stream));
[email protected]3732cea2013-06-21 06:50:50496}
497
[email protected]102e27c2011-02-23 01:01:31498void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13499 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50500 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38501 DCHECK_NE(OK, result);
502 DCHECK(stream_request_.get());
503 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14504 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38505
506 OnIOComplete(result);
507}
508
[email protected]102e27c2011-02-23 01:01:31509void HttpNetworkTransaction::OnCertificateError(
510 int result,
511 const SSLConfig& used_ssl_config,
512 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50513 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38514 DCHECK_NE(OK, result);
515 DCHECK(stream_request_.get());
516 DCHECK(!stream_.get());
517
518 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14519 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38520
521 // TODO(mbelshe): For now, we're going to pass the error through, and that
522 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50523 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
524 // good and the user chooses to ignore the error. This is not ideal, but not
525 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38526
527 OnIOComplete(result);
528}
529
530void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50531 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31532 const SSLConfig& used_ssl_config,
533 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50534 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38535 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50536 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38537
538 establishing_tunnel_ = true;
539 response_.headers = proxy_response.headers;
540 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20541
542 if (response_.headers.get() && !ContentEncodingsValid()) {
543 DoCallback(ERR_CONTENT_DECODING_FAILED);
544 return;
545 }
546
[email protected]8e6441ca2010-08-19 05:56:38547 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14548 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31549 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38550
551 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
552 pending_auth_target_ = HttpAuth::AUTH_PROXY;
553
554 DoCallback(OK);
555}
556
557void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31558 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50559 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50560 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38561
[email protected]102957f2011-09-02 17:10:14562 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38563 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58564 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38565}
566
[email protected]511f6f52010-12-17 03:58:29567void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
568 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31569 const SSLConfig& used_ssl_config,
570 const ProxyInfo& used_proxy_info,
Bence Béky6d05ebd2017-05-16 00:09:01571 std::unique_ptr<HttpStream> stream) {
[email protected]511f6f52010-12-17 03:58:29572 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
573
ttuttle1f2d7e92015-04-28 16:17:47574 CopyConnectionAttemptsFromStreamRequest();
575
[email protected]511f6f52010-12-17 03:58:29576 headers_valid_ = true;
577 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14578 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31579 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22580 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19581 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22582 total_sent_bytes_ += stream_->GetTotalSentBytes();
583 }
Bence Béky6d05ebd2017-05-16 00:09:01584 stream_ = std::move(stream);
Andrey Kosyakov83a6eee2017-08-14 19:20:04585 stream_->SetRequestHeadersCallback(request_headers_callback_);
[email protected]511f6f52010-12-17 03:58:29586 stream_request_.reset(); // we're done with the stream request
587 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
588}
589
zhongyi48704c182015-12-07 07:52:02590void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12591 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02592}
593
ttuttle1f2d7e92015-04-28 16:17:47594void HttpNetworkTransaction::GetConnectionAttempts(
595 ConnectionAttempts* out) const {
596 *out = connection_attempts_;
597}
598
rdsmithbf8c3c12016-11-18 18:16:24599void HttpNetworkTransaction::OnThrottleUnblocked(
600 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50601 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24602 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50603 // be so in the future.
604 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
605
606 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
607
608 DoLoop(OK);
609}
610
Adam Ricecb76ac62015-02-20 05:33:25611bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37612 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52613}
614
nharperb7441ef2016-01-25 23:54:14615bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
616 if (!IsSecureRequest())
617 return false;
618 SSLInfo ssl_info;
619 stream_->GetSSLInfo(&ssl_info);
620 return ssl_info.token_binding_negotiated &&
621 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
mmenke6ddfbea2017-05-31 21:48:41622 session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14623}
624
625void HttpNetworkTransaction::RecordTokenBindingSupport() const {
626 // This enum is used for an UMA histogram - do not change or re-use values.
627 enum {
628 DISABLED = 0,
629 CLIENT_ONLY = 1,
630 CLIENT_AND_SERVER = 2,
631 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
632 TOKEN_BINDING_SUPPORT_MAX
633 } supported;
634 if (!IsSecureRequest())
635 return;
636 SSLInfo ssl_info;
637 stream_->GetSSLInfo(&ssl_info);
638 if (!session_->params().enable_token_binding) {
639 supported = DISABLED;
mmenke6ddfbea2017-05-31 21:48:41640 } else if (!session_->context().channel_id_service) {
nharperb7441ef2016-01-25 23:54:14641 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
642 } else if (ssl_info.token_binding_negotiated) {
643 supported = CLIENT_AND_SERVER;
644 } else {
645 supported = CLIENT_ONLY;
646 }
647 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
648 TOKEN_BINDING_SUPPORT_MAX);
649}
650
Adam Rice425cf122015-01-19 06:18:24651bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12652 return (proxy_info_.is_http() || proxy_info_.is_https() ||
653 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24654 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
655}
656
initial.commit586acc5fe2008-07-26 22:42:52657void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50658 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41659 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52660
[email protected]96d570e42008-08-05 22:43:04661 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41662 CompletionCallback c = callback_;
663 callback_.Reset();
664 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52665}
666
667void HttpNetworkTransaction::OnIOComplete(int result) {
668 int rv = DoLoop(result);
669 if (rv != ERR_IO_PENDING)
670 DoCallback(rv);
671}
672
673int HttpNetworkTransaction::DoLoop(int result) {
674 DCHECK(next_state_ != STATE_NONE);
675
676 int rv = result;
677 do {
678 State state = next_state_;
679 next_state_ = STATE_NONE;
680 switch (state) {
rdsmith1d343be52016-10-21 20:37:50681 case STATE_THROTTLE:
682 DCHECK_EQ(OK, rv);
683 rv = DoThrottle();
684 break;
685 case STATE_THROTTLE_COMPLETE:
686 DCHECK_EQ(OK, rv);
687 rv = DoThrottleComplete();
688 break;
[email protected]1826a402014-01-08 15:40:48689 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
690 DCHECK_EQ(OK, rv);
691 rv = DoNotifyBeforeCreateStream();
692 break;
[email protected]82918cc2010-08-25 17:24:50693 case STATE_CREATE_STREAM:
694 DCHECK_EQ(OK, rv);
695 rv = DoCreateStream();
696 break;
697 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46698 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
699 // solved.
700 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50701 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46702 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50703 break;
[email protected]351ab642010-08-05 16:55:31704 case STATE_INIT_STREAM:
705 DCHECK_EQ(OK, rv);
706 rv = DoInitStream();
707 break;
708 case STATE_INIT_STREAM_COMPLETE:
709 rv = DoInitStreamComplete(rv);
710 break;
[email protected]044de0642010-06-17 10:42:15711 case STATE_GENERATE_PROXY_AUTH_TOKEN:
712 DCHECK_EQ(OK, rv);
713 rv = DoGenerateProxyAuthToken();
714 break;
715 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
716 rv = DoGenerateProxyAuthTokenComplete(rv);
717 break;
718 case STATE_GENERATE_SERVER_AUTH_TOKEN:
719 DCHECK_EQ(OK, rv);
720 rv = DoGenerateServerAuthToken();
721 break;
722 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
723 rv = DoGenerateServerAuthTokenComplete(rv);
724 break;
nharperd6e65822016-03-30 23:05:48725 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14726 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48727 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14728 break;
nharperd6e65822016-03-30 23:05:48729 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
730 rv = DoGetProvidedTokenBindingKeyComplete(rv);
731 break;
732 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
733 DCHECK_EQ(OK, rv);
734 rv = DoGetReferredTokenBindingKey();
735 break;
736 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
737 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14738 break;
[email protected]daddea62012-09-19 05:51:13739 case STATE_INIT_REQUEST_BODY:
740 DCHECK_EQ(OK, rv);
741 rv = DoInitRequestBody();
742 break;
743 case STATE_INIT_REQUEST_BODY_COMPLETE:
744 rv = DoInitRequestBodyComplete(rv);
745 break;
[email protected]4875ba12011-03-30 22:31:51746 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55747 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00748 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51749 rv = DoBuildRequest();
750 break;
751 case STATE_BUILD_REQUEST_COMPLETE:
752 rv = DoBuildRequestComplete(rv);
753 break;
754 case STATE_SEND_REQUEST:
755 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57756 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52757 break;
[email protected]0877e3d2009-10-17 22:29:57758 case STATE_SEND_REQUEST_COMPLETE:
759 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43760 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00761 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52762 break;
763 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55764 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00765 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52766 rv = DoReadHeaders();
767 break;
768 case STATE_READ_HEADERS_COMPLETE:
769 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43770 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00771 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52772 break;
773 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55774 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00775 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52776 rv = DoReadBody();
777 break;
778 case STATE_READ_BODY_COMPLETE:
779 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43780 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00781 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52782 break;
[email protected]2d2697f92009-02-18 21:00:32783 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55784 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53785 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00786 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32787 rv = DoDrainBodyForAuthRestart();
788 break;
789 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
790 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43791 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00792 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 default:
795 NOTREACHED() << "bad state";
796 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04797 break;
initial.commit586acc5fe2008-07-26 22:42:52798 }
799 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
800
801 return rv;
802}
803
rdsmith1d343be52016-10-21 20:37:50804int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24805 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50806 throttle_ = session_->throttler()->CreateThrottle(
807 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
808 next_state_ = STATE_THROTTLE_COMPLETE;
809
rdsmithbf8c3c12016-11-18 18:16:24810 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50811 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
812 return ERR_IO_PENDING;
813 }
814
815 return OK;
816}
817
818int HttpNetworkTransaction::DoThrottleComplete() {
819 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24820 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50821
822 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
823
824 return OK;
825}
826
[email protected]1826a402014-01-08 15:40:48827int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
828 next_state_ = STATE_CREATE_STREAM;
829 bool defer = false;
830 if (!before_network_start_callback_.is_null())
831 before_network_start_callback_.Run(&defer);
832 if (!defer)
833 return OK;
834 return ERR_IO_PENDING;
835}
836
[email protected]82918cc2010-08-25 17:24:50837int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34838 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26839 tracked_objects::ScopedTracker tracking_profile(
840 FROM_HERE_WITH_EXPLICIT_FUNCTION(
841 "424359 HttpNetworkTransaction::DoCreateStream"));
842
ttuttlec0c828492015-05-15 01:25:55843 response_.network_accessed = true;
844
[email protected]82918cc2010-08-25 17:24:50845 next_state_ = STATE_CREATE_STREAM_COMPLETE;
rch2f2991c2017-04-13 19:28:17846 // IP based pooling is only enabled on a retry after 421 Misdirected Request
847 // is received. Alternative Services are also disabled in this case (though
848 // they can also be disabled when retrying after a QUIC error).
849 if (!enable_ip_based_pooling_)
850 DCHECK(!enable_alternative_services_);
[email protected]831e4a32013-11-14 02:14:44851 if (ForWebSocketHandshake()) {
xunjieli96f2a402017-06-05 17:24:27852 stream_request_ =
[email protected]0191b51c2013-11-18 10:55:23853 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44854 ->RequestWebSocketHandshakeStream(
bnc8016c1f2017-03-31 02:11:29855 *request_, priority_, server_ssl_config_, proxy_ssl_config_,
856 this, websocket_handshake_stream_base_create_helper_,
bncaccd4962017-04-06 21:00:26857 enable_ip_based_pooling_, enable_alternative_services_,
xunjieli96f2a402017-06-05 17:24:27858 net_log_);
[email protected]831e4a32013-11-14 02:14:44859 } else {
xunjieli96f2a402017-06-05 17:24:27860 stream_request_ = session_->http_stream_factory()->RequestStream(
bnc8016c1f2017-03-31 02:11:29861 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
xunjieli96f2a402017-06-05 17:24:27862 enable_ip_based_pooling_, enable_alternative_services_, net_log_);
[email protected]831e4a32013-11-14 02:14:44863 }
[email protected]26816882010-10-14 18:03:09864 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38865 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31866}
867
[email protected]82918cc2010-08-25 17:24:50868int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47869 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
870 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
871 // which resets the stream request first. Therefore, we have to grab the
872 // connection attempts in *that* function instead of here in that case.
873 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
874 CopyConnectionAttemptsFromStreamRequest();
875
[email protected]394816e92010-08-03 07:38:59876 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50877 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38878 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33879 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
880 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29881 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
882 // Return OK and let the caller read the proxy's error page
883 next_state_ = STATE_NONE;
884 return OK;
bncfacdd852015-01-09 19:22:54885 } else if (result == ERR_HTTP_1_1_REQUIRED ||
886 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
887 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59888 }
889
[email protected]bd0b6772011-01-11 19:59:30890 // Handle possible handshake errors that may have occurred if the stream
891 // used SSL for one or more of the layers.
892 result = HandleSSLHandshakeError(result);
893
[email protected]8e6441ca2010-08-19 05:56:38894 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09895 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38896 return result;
[email protected]394816e92010-08-03 07:38:59897}
898
[email protected]82918cc2010-08-25 17:24:50899int HttpNetworkTransaction::DoInitStream() {
900 DCHECK(stream_.get());
901 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59902
903 stream_->GetRemoteEndpoint(&remote_endpoint_);
904
[email protected]262eec82013-03-19 21:01:36905 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50906}
907
908int HttpNetworkTransaction::DoInitStreamComplete(int result) {
909 if (result == OK) {
910 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50911 } else {
[email protected]82918cc2010-08-25 17:24:50912 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26913 result = HandleIOError(result);
914
915 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22916 if (stream_) {
917 total_received_bytes_ += stream_->GetTotalReceivedBytes();
918 total_sent_bytes_ += stream_->GetTotalSentBytes();
919 }
zhongyica364fbb2015-12-12 03:39:12920 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50921 }
922
923 return result;
924}
925
[email protected]044de0642010-06-17 10:42:15926int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
927 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
928 if (!ShouldApplyProxyAuth())
929 return OK;
[email protected]394816e92010-08-03 07:38:59930 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
931 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09932 auth_controllers_[target] =
933 new HttpAuthController(target,
934 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31935 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09936 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59937 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41938 io_callback_,
[email protected]394816e92010-08-03 07:38:59939 net_log_);
[email protected]044de0642010-06-17 10:42:15940}
941
942int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
943 DCHECK_NE(ERR_IO_PENDING, rv);
944 if (rv == OK)
945 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
946 return rv;
947}
948
949int HttpNetworkTransaction::DoGenerateServerAuthToken() {
950 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59951 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54952 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09953 auth_controllers_[target] =
954 new HttpAuthController(target,
955 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31956 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09957 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54958 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
959 auth_controllers_[target]->DisableEmbeddedIdentity();
960 }
[email protected]044de0642010-06-17 10:42:15961 if (!ShouldApplyServerAuth())
962 return OK;
[email protected]394816e92010-08-03 07:38:59963 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41964 io_callback_,
[email protected]394816e92010-08-03 07:38:59965 net_log_);
[email protected]044de0642010-06-17 10:42:15966}
967
968int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
969 DCHECK_NE(ERR_IO_PENDING, rv);
970 if (rv == OK)
nharperd6e65822016-03-30 23:05:48971 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15972 return rv;
973}
974
nharperd6e65822016-03-30 23:05:48975int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
976 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14977 if (!IsTokenBindingEnabled())
978 return OK;
979
mikecirone8b85c432016-09-08 19:11:00980 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:41981 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperb7441ef2016-01-25 23:54:14982 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48983 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14984 &token_binding_request_);
985}
986
nharperd6e65822016-03-30 23:05:48987int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14988 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48989 if (IsTokenBindingEnabled()) {
990 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00991 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48992 }
993
994 if (rv == OK)
995 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
996 return rv;
997}
998
999int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1000 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1001 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141002 return OK;
1003
mikecirone8b85c432016-09-08 19:11:001004 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
mmenke6ddfbea2017-05-31 21:48:411005 ChannelIDService* channel_id_service = session_->context().channel_id_service;
nharperd6e65822016-03-30 23:05:481006 return channel_id_service->GetOrCreateChannelID(
1007 request_->token_binding_referrer, &referred_token_binding_key_,
1008 io_callback_, &token_binding_request_);
1009}
1010
1011int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1012 DCHECK_NE(ERR_IO_PENDING, rv);
1013 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1014 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001015 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481016 }
1017 if (rv == OK)
1018 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141019 return rv;
1020}
1021
1022int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241023 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141024 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1025 GetHostAndOptionalPort(request_->url));
1026
1027 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241028 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141029 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1030 "keep-alive");
1031 } else {
1032 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1033 }
1034
[email protected]2979a492011-04-06 00:29:141035 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111036 if (request_->upload_data_stream) {
1037 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141038 request_headers_.SetHeader(
1039 HttpRequestHeaders::kTransferEncoding, "chunked");
1040 } else {
1041 request_headers_.SetHeader(
1042 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111043 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141044 }
csharrisonf473dd192015-08-18 13:54:131045 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141046 // An empty POST/PUT request still needs a content length. As for HEAD,
1047 // IE and Safari also add a content length header. Presumably it is to
1048 // support sending a HEAD request to an URL that only expects to be sent a
1049 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131050 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1051 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141052 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1053 }
1054
nharperb7441ef2016-01-25 23:54:141055 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481056 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141057 std::string token_binding_header;
1058 int rv = BuildTokenBindingHeader(&token_binding_header);
1059 if (rv != OK)
1060 return rv;
1061 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1062 token_binding_header);
1063 }
1064
[email protected]2979a492011-04-06 00:29:141065 // Honor load flags that impact proxy caches.
1066 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1067 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1068 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1069 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1070 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1071 }
1072
1073 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1074 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1075 &request_headers_);
1076 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1077 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1078 &request_headers_);
1079
[email protected]c10450102011-06-27 09:06:161080 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201081
ryansturm49a8cb12016-06-15 16:51:091082 if (!before_headers_sent_callback_.is_null())
1083 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201084
[email protected]173f8e22013-04-10 04:18:201085 response_.did_use_http_auth =
1086 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1087 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141088 return OK;
1089}
1090
1091int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471092 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141093 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351094 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1095 TokenBindingType::PROVIDED,
1096 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141097 if (rv != OK)
1098 return rv;
1099 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481100 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1101 provided_token_binding_key_.get(), signed_ekm,
1102 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141103 if (rv != OK)
1104 return rv;
nharperd6e65822016-03-30 23:05:481105
nharperb7441ef2016-01-25 23:54:141106 std::vector<base::StringPiece> token_bindings;
1107 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481108
1109 std::string referred_token_binding;
1110 if (referred_token_binding_key_) {
1111 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351112 int rv = stream_->GetTokenBindingSignature(
1113 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1114 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481115 if (rv != OK)
1116 return rv;
1117 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1118 referred_token_binding_key_.get(),
1119 referred_signed_ekm, &referred_token_binding);
1120 if (rv != OK)
1121 return rv;
1122 token_bindings.push_back(referred_token_binding);
1123 }
nharperb7441ef2016-01-25 23:54:141124 std::string header;
1125 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1126 if (rv != OK)
1127 return rv;
nharper78e6d2b2016-09-21 05:42:351128 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471129 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1130 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1131 header_creation_time,
1132 base::TimeDelta::FromMilliseconds(1),
1133 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141134 return OK;
[email protected]2979a492011-04-06 00:29:141135}
1136
[email protected]daddea62012-09-19 05:51:131137int HttpNetworkTransaction::DoInitRequestBody() {
1138 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131139 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111140 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521141 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131142 return rv;
1143}
[email protected]4875ba12011-03-30 22:31:511144
[email protected]daddea62012-09-19 05:51:131145int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1146 if (result == OK)
1147 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131148 return result;
1149}
1150
1151int HttpNetworkTransaction::DoBuildRequest() {
1152 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511153 headers_valid_ = false;
1154
1155 // This is constructed lazily (instead of within our Start method), so that
1156 // we have proxy info available.
1157 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241158 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141159 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511160 }
1161
[email protected]4875ba12011-03-30 22:31:511162 return OK;
1163}
1164
1165int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241166 if (result == OK)
1167 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511168 return result;
1169}
1170
[email protected]0877e3d2009-10-17 22:29:571171int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341172 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261173 tracked_objects::ScopedTracker tracking_profile(
1174 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1175 "424359 HttpNetworkTransaction::DoSendRequest"));
1176
[email protected]58e32bb2013-01-21 18:23:251177 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571178 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1179
[email protected]bf3eb002012-11-15 05:50:111180 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521181}
1182
[email protected]0877e3d2009-10-17 22:29:571183int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251184 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361185
1186 if (result == ERR_HTTP_1_1_REQUIRED ||
1187 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1188 return HandleHttp11Required(result);
1189 }
1190
initial.commit586acc5fe2008-07-26 22:42:521191 if (result < 0)
1192 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571193 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521194 return OK;
1195}
1196
1197int HttpNetworkTransaction::DoReadHeaders() {
1198 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411199 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521200}
1201
1202int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111203 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1204 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381205 if (IsCertificateError(result)) {
1206 // We don't handle a certificate error during SSL renegotiation, so we
1207 // have to return an error that's not in the certificate error range
1208 // (-2xx).
1209 LOG(ERROR) << "Got a server certificate with error " << result
1210 << " during SSL renegotiation";
1211 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1212 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1213 // TODO(wtc): Need a test case for this code path!
1214 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251215 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381216 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501217 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381218 result = HandleCertificateRequest(result);
1219 if (result == OK)
1220 return result;
[email protected]2181ea002009-06-09 01:37:271221 }
1222
bncfacdd852015-01-09 19:22:541223 if (result == ERR_HTTP_1_1_REQUIRED ||
1224 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1225 return HandleHttp11Required(result);
1226 }
1227
[email protected]c871864d72014-03-13 19:56:191228 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1229 // response headers were received, we do the best we can to make sense of it
1230 // and send it back up the stack.
1231 //
1232 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1233 // bizarre for SPDY. Assuming this logic is useful at all.
1234 // TODO(davidben): Bubble the error code up so we do not cache?
1235 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1236 result = OK;
1237
1238 if (result < 0)
1239 return HandleIOError(result);
1240
[email protected]90499482013-06-01 00:39:501241 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521242
eustasc7d27da2017-04-06 10:33:201243 if (response_.headers.get() && !ContentEncodingsValid())
1244 return ERR_CONTENT_DECODING_FAILED;
1245
[email protected]d58ceea82014-06-04 10:55:541246 // On a 408 response from the server ("Request Timeout") on a stale socket,
1247 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041248 // Headers can be NULL because of http://crbug.com/384554.
tyoshinoe8b3e222017-04-21 03:07:181249 if (response_.headers.get() &&
1250 response_.headers->response_code() == HTTP_REQUEST_TIMEOUT &&
[email protected]d58ceea82014-06-04 10:55:541251 stream_->IsConnectionReused()) {
1252 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001253 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541254 response_.headers->response_code());
1255 // This will close the socket - it would be weird to try and reuse it, even
1256 // if the server doesn't actually close it.
1257 ResetConnectionAndRequestForResend();
1258 return OK;
1259 }
1260
[email protected]93f8b562012-03-27 01:00:161261 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211262 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161263 const int response_code = response_.headers->response_code();
1264 UMA_HISTOGRAM_ENUMERATION(
1265 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1266 }
1267
[email protected]3abacd62012-06-10 20:20:321268 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001269 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321270 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391271
bncbe0f6af2015-10-15 17:49:561272 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571273 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1274 // indicates a buggy server. See:
1275 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1276 if (request_->method == "PUT")
1277 return ERR_METHOD_NOT_SUPPORTED;
1278 }
[email protected]4ddaf2502008-10-23 18:26:191279
[email protected]0877e3d2009-10-17 22:29:571280 // Check for an intermediate 100 Continue response. An origin server is
1281 // allowed to send this response even if we didn't ask for it, so we just
1282 // need to skip over it.
1283 // We treat any other 1xx in this same way (although in practice getting
1284 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441285 // Unless this is a WebSocket request, in which case we pass it on up.
1286 if (response_.headers->response_code() / 100 == 1 &&
1287 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451288 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571289 next_state_ = STATE_READ_HEADERS;
1290 return OK;
1291 }
1292
davidbence688ae2017-05-04 15:12:591293 if (response_.headers->response_code() == 421 &&
1294 (enable_ip_based_pooling_ || enable_alternative_services_)) {
1295 // Retry the request with both IP based pooling and Alternative Services
1296 // disabled.
1297 enable_ip_based_pooling_ = false;
1298 enable_alternative_services_ = false;
1299 net_log_.AddEvent(
1300 NetLogEventType::HTTP_TRANSACTION_RESTART_MISDIRECTED_REQUEST);
1301 ResetConnectionAndRequestForResend();
1302 return OK;
bnc8016c1f2017-03-31 02:11:291303 }
1304
bncb26024382016-06-29 02:39:451305 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111306 session_->http_stream_factory()->ProcessAlternativeServices(
1307 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1308 }
[email protected]564b4912010-03-09 16:30:421309
asanka5ffd5d72016-03-23 16:20:491310 if (IsSecureRequest())
1311 stream_->GetSSLInfo(&response_.ssl_info);
1312
[email protected]e772db3f2010-07-12 18:11:131313 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571314 if (rv != OK)
1315 return rv;
1316
[email protected]0877e3d2009-10-17 22:29:571317 headers_valid_ = true;
1318 return OK;
initial.commit586acc5fe2008-07-26 22:42:521319}
1320
1321int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501322 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131323 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381324 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521325
1326 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501327 return stream_->ReadResponseBody(
1328 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521329}
1330
1331int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1332 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381333 bool done = false;
1334 if (result <= 0) {
1335 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521336 done = true;
[email protected]8e6441ca2010-08-19 05:56:381337 }
[email protected]9492e4a2010-02-24 00:58:461338
mmenkebd84c392015-09-02 14:12:341339 // Clean up connection if we are done.
1340 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381341 // Note: Just because IsResponseBodyComplete is true, we're not
1342 // necessarily "done". We're only "done" when it is the last
1343 // read on this HttpNetworkTransaction, which will be signified
1344 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341345 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381346 // the stream. No need to compute it here just to pass back
1347 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341348 bool keep_alive =
1349 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521350
[email protected]8e6441ca2010-08-19 05:56:381351 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011352 // Note: we don't reset the stream here. We've closed it, but we still
1353 // need it around so that callers can call methods such as
1354 // GetUploadProgress() and have them be meaningful.
1355 // TODO(mbelshe): This means we closed the stream here, and we close it
1356 // again in ~HttpNetworkTransaction. Clean that up.
1357
[email protected]8e6441ca2010-08-19 05:56:381358 // The next Read call will return 0 (EOF).
rch2f2991c2017-04-13 19:28:171359
1360 // This transaction was successful. If it had been retried because of an
1361 // error with an alternative service, mark that alternative service broken.
1362 if (!enable_alternative_services_ &&
1363 retried_alternative_service_.protocol != kProtoUnknown) {
1364 session_->http_server_properties()->MarkAlternativeServiceBroken(
1365 retried_alternative_service_);
1366 }
initial.commit586acc5fe2008-07-26 22:42:521367 }
1368
1369 // Clear these to avoid leaving around old state.
1370 read_buf_ = NULL;
1371 read_buf_len_ = 0;
1372
1373 return result;
1374}
1375
[email protected]2d2697f92009-02-18 21:00:321376int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1377 // This method differs from DoReadBody only in the next_state_. So we just
1378 // call DoReadBody and override the next_state_. Perhaps there is a more
1379 // elegant way for these two methods to share code.
1380 int rv = DoReadBody();
1381 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1382 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1383 return rv;
1384}
1385
[email protected]0877e3d2009-10-17 22:29:571386// TODO(wtc): This method and the DoReadBodyComplete method are almost
1387// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321388int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231389 // keep_alive defaults to true because the very reason we're draining the
1390 // response body is to reuse the connection for auth restart.
1391 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321392 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571393 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321394 done = true;
[email protected]68873ba2009-06-04 21:49:231395 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311396 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571397 done = true;
[email protected]2d2697f92009-02-18 21:00:321398 }
1399
1400 if (done) {
1401 DidDrainBodyForAuthRestart(keep_alive);
1402 } else {
1403 // Keep draining.
1404 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1405 }
1406
1407 return OK;
1408}
1409
[email protected]5e363962009-06-19 19:57:011410int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381411 // There are two paths through which the server can request a certificate
1412 // from us. The first is during the initial handshake, the second is
1413 // during SSL renegotiation.
1414 //
1415 // In both cases, we want to close the connection before proceeding.
1416 // We do this for two reasons:
1417 // First, we don't want to keep the connection to the server hung for a
1418 // long time while the user selects a certificate.
1419 // Second, even if we did keep the connection open, NSS has a bug where
1420 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581421 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381422
1423 if (stream_.get()) {
1424 // Since we already have a stream, we're being called as part of SSL
1425 // renegotiation.
1426 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191427 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221428 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381429 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121430 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381431 }
1432
[email protected]26816882010-10-14 18:03:091433 // The server is asking for a client certificate during the initial
1434 // handshake.
1435 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011436
[email protected]ec229bc92010-11-22 09:51:451437 // If the user selected one of the certificates in client_certs or declined
1438 // to provide one for this server before, use the past decision
1439 // automatically.
1440 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541441 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451442 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541443 response_.cert_request_info->host_and_port, &client_cert,
1444 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451445 if (!found_cached_cert)
1446 return error;
1447
1448 // Check that the certificate selected is still a certificate the server
1449 // is likely to accept, based on the criteria supplied in the
1450 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501451 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261452 const std::vector<std::string>& cert_authorities =
1453 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451454
[email protected]f1958c382013-02-07 00:15:261455 bool cert_still_valid = cert_authorities.empty() ||
1456 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451457 if (!cert_still_valid)
1458 return error;
[email protected]5e363962009-06-19 19:57:011459 }
[email protected]ec229bc92010-11-22 09:51:451460
1461 // TODO(davidben): Add a unit test which covers this path; we need to be
1462 // able to send a legitimate certificate and also bypass/clear the
1463 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141464 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1465 &proxy_ssl_config_ : &server_ssl_config_;
1466 ssl_config->send_client_cert = true;
1467 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541468 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451469 next_state_ = STATE_CREATE_STREAM;
1470 // Reset the other member variables.
1471 // Note: this is necessary only with SSL renegotiation.
1472 ResetStateForRestart();
1473 return OK;
[email protected]0b45559b2009-06-12 21:45:111474}
1475
bncfacdd852015-01-09 19:22:541476int HttpNetworkTransaction::HandleHttp11Required(int error) {
1477 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1478 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1479
1480 if (error == ERR_HTTP_1_1_REQUIRED) {
1481 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1482 } else {
1483 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1484 }
1485 ResetConnectionAndRequestForResend();
1486 return OK;
1487}
1488
[email protected]384cc73a2013-12-08 22:41:031489void HttpNetworkTransaction::HandleClientAuthError(int error) {
1490 if (server_ssl_config_.send_client_cert &&
1491 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1492 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411493 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031494 }
1495}
1496
[email protected]bd0b6772011-01-11 19:59:301497// TODO(rch): This does not correctly handle errors when an SSL proxy is
1498// being used, as all of the errors are handled as if they were generated
1499// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381500// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301501int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1502 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031503 HandleClientAuthError(error);
initial.commit586acc5fe2008-07-26 22:42:521504 return error;
1505}
1506
[email protected]bd0b6772011-01-11 19:59:301507// This method determines whether it is safe to resend the request after an
1508// IO error. It can only be called in response to request header or body
1509// write errors or response header read errors. It should not be used in
1510// other cases, such as a Connect error.
1511int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031512 // Because the peer may request renegotiation with client authentication at
1513 // any time, check and handle client authentication errors.
1514 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301515
1516 switch (error) {
1517 // If we try to reuse a connection that the server is in the process of
1518 // closing, we may end up successfully writing out our request (or a
1519 // portion of our request) only to find a connection error when we try to
1520 // read from (or finish writing to) the socket.
1521 case ERR_CONNECTION_RESET:
1522 case ERR_CONNECTION_CLOSED:
1523 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511524 // There can be a race between the socket pool checking checking whether a
1525 // socket is still connected, receiving the FIN, and sending/reading data
1526 // on a reused socket. If we receive the FIN between the connectedness
1527 // check and writing/reading from the socket, we may first learn the socket
1528 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1529 // likely happen when trying to retrieve its IP address.
1530 // See http://crbug.com/105824 for more details.
1531 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491532 // If a socket is closed on its initial request, HttpStreamParser returns
1533 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1534 // preconnected but failed to be used before the server timed it out.
1535 case ERR_EMPTY_RESPONSE:
1536 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381537 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001538 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301539 ResetConnectionAndRequestForResend();
1540 error = OK;
1541 }
1542 break;
[email protected]8753a122011-10-16 08:05:081543 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001544 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271545 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381546 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001547 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001548 ResetConnectionAndRequestForResend();
1549 error = OK;
1550 break;
rch2f2991c2017-04-13 19:28:171551 case ERR_QUIC_PROTOCOL_ERROR:
1552 if (GetResponseHeaders() != nullptr ||
1553 !stream_->GetAlternativeService(&retried_alternative_service_)) {
1554 // If the response headers have already been recieved and passed up
1555 // then the request can not be retried. Also, if there was no
1556 // alternative service used for this request, then there is no
1557 // alternative service to be disabled.
1558 break;
1559 }
1560 if (session_->http_server_properties()->IsAlternativeServiceBroken(
1561 retried_alternative_service_)) {
1562 // If the alternative service was marked as broken while the request
1563 // was in flight, retry the request which will not use the broken
1564 // alternative service.
1565 net_log_.AddEventWithNetErrorCode(
1566 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1567 ResetConnectionAndRequestForResend();
1568 error = OK;
1569 } else if (session_->params().retry_without_alt_svc_on_quic_errors) {
1570 // Disable alternative services for this request and retry it. If the
1571 // retry succeeds, then the alternative service will be marked as
1572 // broken then.
1573 enable_alternative_services_ = false;
rch514a44a82017-04-13 04:46:361574 net_log_.AddEventWithNetErrorCode(
1575 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1576 ResetConnectionAndRequestForResend();
1577 error = OK;
1578 }
1579 break;
[email protected]bd0b6772011-01-11 19:59:301580 }
1581 return error;
1582}
1583
[email protected]c3b35c22008-09-27 03:19:421584void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581585 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221586 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191587 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221588 total_sent_bytes_ += stream_->GetTotalSentBytes();
1589 }
zhongyica364fbb2015-12-12 03:39:121590 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581591}
1592
1593void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251594 send_start_time_ = base::TimeTicks();
1595 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251596
[email protected]0757e7702009-03-27 04:00:221597 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421598 read_buf_ = NULL;
1599 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571600 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201601 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141602 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381603 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591604 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121605 net_error_details_.quic_broken = false;
1606 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481607 provided_token_binding_key_.reset();
1608 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121609}
1610
1611void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1612 if (stream_)
1613 stream_->PopulateNetErrorDetails(&net_error_details_);
1614 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571615}
1616
[email protected]0877e3d2009-10-17 22:29:571617HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501618 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421619}
1620
[email protected]a34f61ee2014-03-18 20:59:491621bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381622 bool connection_is_proven = stream_->IsConnectionReused();
1623 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161624
[email protected]2a5c76b2008-09-25 22:15:161625 // NOTE: we resend a request only if we reused a keep-alive connection.
1626 // This automatically prevents an infinite resend loop because we'll run
1627 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381628 if (connection_is_proven && !has_received_headers)
1629 return true;
1630 return false;
[email protected]1c773ea12009-04-28 19:58:421631}
1632
1633void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381634 if (stream_.get()) {
1635 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121636 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161637 }
1638
[email protected]0877e3d2009-10-17 22:29:571639 // We need to clear request_headers_ because it contains the real request
1640 // headers, but we may need to resend the CONNECT request first to recreate
1641 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201642 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501643 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541644}
1645
[email protected]1c773ea12009-04-28 19:58:421646bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241647 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421648}
license.botbf09a502008-08-24 00:55:551649
[email protected]1c773ea12009-04-28 19:58:421650bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041651 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421652}
1653
[email protected]e772db3f2010-07-12 18:11:131654int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271655 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501656 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421657
[email protected]0877e3d2009-10-17 22:29:571658 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581659 if (status != HTTP_UNAUTHORIZED &&
1660 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421661 return OK;
[email protected]9094b602012-02-27 21:44:581662 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111663 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161664 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1665 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421666
[email protected]9094b602012-02-27 21:44:581667 // This case can trigger when an HTTPS server responds with a "Proxy
1668 // authentication required" status code through a non-authenticating
1669 // proxy.
[email protected]7a67a8152010-11-05 18:31:101670 if (!auth_controllers_[target].get())
1671 return ERR_UNEXPECTED_PROXY_AUTH;
1672
[email protected]a7ea8832010-07-12 17:54:541673 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491674 headers, response_.ssl_info,
1675 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311676 net_log_);
[email protected]228404f2010-06-24 04:31:411677 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491678 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411679
1680 scoped_refptr<AuthChallengeInfo> auth_info =
1681 auth_controllers_[target]->auth_info();
1682 if (auth_info.get())
1683 response_.auth_challenge = auth_info;
1684
[email protected]228404f2010-06-24 04:31:411685 return rv;
[email protected]f9ee6b52008-11-08 06:46:231686}
1687
[email protected]8e6441ca2010-08-19 05:56:381688bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1689 return auth_controllers_[target].get() &&
1690 auth_controllers_[target]->HaveAuth();
1691}
1692
[email protected]228404f2010-06-24 04:31:411693GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1694 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461695 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411696 if (!proxy_info_.proxy_server().is_valid() ||
1697 proxy_info_.proxy_server().is_direct()) {
1698 return GURL(); // There is no proxy server.
1699 }
[email protected]2df19bb2010-08-25 20:13:461700 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1701 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351702 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461703 }
[email protected]228404f2010-06-24 04:31:411704 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291705 if (ForWebSocketHandshake()) {
1706 const GURL& url = request_->url;
1707 url::Replacements<char> ws_to_http;
1708 if (url.SchemeIs("ws")) {
1709 ws_to_http.SetScheme("http", url::Component(0, 4));
1710 } else {
1711 DCHECK(url.SchemeIs("wss"));
1712 ws_to_http.SetScheme("https", url::Component(0, 5));
1713 }
1714 return url.ReplaceComponents(ws_to_http);
1715 }
[email protected]228404f2010-06-24 04:31:411716 return request_->url;
1717 default:
1718 return GURL();
1719 }
[email protected]c3b35c22008-09-27 03:19:421720}
1721
[email protected]831e4a32013-11-14 02:14:441722bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141723 return websocket_handshake_stream_base_create_helper_ &&
1724 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441725}
1726
ttuttle1f2d7e92015-04-28 16:17:471727void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1728 DCHECK(stream_request_);
1729
1730 // Since the transaction can restart with auth credentials, it may create a
1731 // stream more than once. Accumulate all of the connection attempts across
1732 // those streams by appending them to the vector:
1733 for (const auto& attempt : stream_request_->connection_attempts())
1734 connection_attempts_.push_back(attempt);
1735}
1736
eustasc7d27da2017-04-06 10:33:201737bool HttpNetworkTransaction::ContentEncodingsValid() const {
1738 HttpResponseHeaders* headers = GetResponseHeaders();
1739 DCHECK(headers);
1740
1741 std::string accept_encoding;
1742 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1743 &accept_encoding);
1744 std::set<std::string> allowed_encodings;
1745 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1746 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1747 return false;
1748 }
1749
1750 std::string content_encoding;
1751 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1752 std::set<std::string> used_encodings;
1753 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1754 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1755 return false;
1756 }
1757
1758 // When "Accept-Encoding" is not specified, it is parsed as "*".
1759 // If "*" encoding is advertised, then any encoding should be "accepted".
1760 // This does not mean, that it will be successfully decoded.
1761 if (allowed_encodings.find("*") != allowed_encodings.end())
1762 return true;
1763
sky50576f32017-05-01 19:28:031764 bool result = true;
eustasc7d27da2017-04-06 10:33:201765 for (auto const& encoding : used_encodings) {
1766 SourceStream::SourceType source_type =
1767 FilterSourceStream::ParseEncodingType(encoding);
1768 // We don't reject encodings we are not aware. They just will not decode.
1769 if (source_type == SourceStream::TYPE_UNKNOWN)
1770 continue;
1771 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
sky50576f32017-05-01 19:28:031772 FilterSourceStream::ReportContentDecodingFailed(
1773 SourceStream::TYPE_REJECTED);
1774 result = false;
1775 break;
eustasc7d27da2017-04-06 10:33:201776 }
1777 }
sky50576f32017-05-01 19:28:031778
1779 // Temporary workaround for http://crbug.com/714514
1780 if (headers->IsRedirect(nullptr)) {
1781 UMA_HISTOGRAM_BOOLEAN("Net.RedirectWithUnadvertisedContentEncoding",
1782 !result);
1783 return true;
1784 }
1785
1786 return result;
eustasc7d27da2017-04-06 10:33:201787}
1788
[email protected]c3b35c22008-09-27 03:19:421789} // namespace net