blob: 3d39fd6e139736aec6f28279d497fe901c4a2468 [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
danakj1fd259a02016-04-16 03:17:097#include <memory>
[email protected]2fbaecf22010-07-22 22:20:358#include <set>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]2fbaecf22010-07-22 22:20:3510#include <vector>
11
nharperb7441ef2016-01-25 23:54:1412#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4113#include "base/bind.h"
14#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4120#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4025#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5326#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3027#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3528#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5529#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1930#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2633#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.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"
[email protected]a796bcec2010-03-22 17:17:2658#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4459#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1860#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2861#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4062#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3263#include "net/spdy/spdy_session.h"
64#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5765#include "net/ssl/ssl_cert_request_info.h"
66#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5467#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1468#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5469#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2970#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5271
initial.commit586acc5fe2008-07-26 22:42:5272namespace net {
73
[email protected]1c773ea12009-04-28 19:58:4274namespace {
75
danakj1fd259a02016-04-16 03:17:0976std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1777 const GURL* url,
78 int net_error,
79 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0980 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2581 dict->SetString("host_and_port", GetHostAndPort(*url));
82 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4883 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2584}
85
[email protected]1c773ea12009-04-28 19:58:4286} // namespace
87
initial.commit586acc5fe2008-07-26 22:42:5288//-----------------------------------------------------------------------------
89
[email protected]262eec82013-03-19 21:01:3690HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
91 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2292 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3293 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
94 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5295 session_(session),
96 request_(NULL),
[email protected]262eec82013-03-19 21:01:3697 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5798 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2099 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52100 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19101 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22102 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54103 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44104 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02105 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12106 net_error_details_() {
[email protected]3ce7df0f2010-03-03 00:30:50107}
108
[email protected]0b0bf032010-09-21 18:08:50109HttpNetworkTransaction::~HttpNetworkTransaction() {
110 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50111 // TODO(mbelshe): The stream_ should be able to compute whether or not the
112 // stream should be kept alive. No reason to compute here
113 // and pass it in.
mmenkebd84c392015-09-02 14:12:34114 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50115 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34116 } else if (stream_->IsResponseBodyComplete()) {
117 // If the response body is complete, we can just reuse the socket.
118 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50119 } else {
mmenkebd84c392015-09-02 14:12:34120 // Otherwise, we try to drain the response body.
121 HttpStream* stream = stream_.release();
122 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50123 }
124 }
[email protected]02cad5d2013-10-02 08:14:03125 if (request_ && request_->upload_data_stream)
126 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50127}
128
[email protected]684970b2009-08-14 04:54:46129int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41130 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20131 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53132 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04133 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15134 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04135
nharper8cdb0fb2016-04-22 21:34:59136 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
137 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
138
[email protected]99ffa5a2011-10-06 04:20:19139 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14140 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19141 proxy_ssl_config_.rev_checking_enabled = false;
142 }
[email protected]6fbac162011-06-20 00:29:04143
jkarlinfb1d5172015-01-12 14:10:29144 if (request_->load_flags & LOAD_PREFETCH)
145 response_.unused_since_prefetch = true;
146
rdsmith1d343be52016-10-21 20:37:50147 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04148 int rv = DoLoop(OK);
149 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41150 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04151 return rv;
152}
153
154int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41155 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38156 DCHECK(!stream_.get());
157 DCHECK(!stream_request_.get());
158 DCHECK_EQ(STATE_NONE, next_state_);
159
[email protected]82918cc2010-08-25 17:24:50160 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38161
[email protected]ccb40e52008-09-17 20:54:40162 int rv = DoLoop(OK);
163 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41164 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11165 return rv;
[email protected]96d570e42008-08-05 22:43:04166}
167
[email protected]0b45559b2009-06-12 21:45:11168int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54169 X509Certificate* client_cert,
170 SSLPrivateKey* client_private_key,
171 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38172 // In HandleCertificateRequest(), we always tear down existing stream
173 // requests to force a new connection. So we shouldn't have one here.
174 DCHECK(!stream_request_.get());
175 DCHECK(!stream_.get());
176 DCHECK_EQ(STATE_NONE, next_state_);
177
[email protected]102957f2011-09-02 17:10:14178 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
179 &proxy_ssl_config_ : &server_ssl_config_;
180 ssl_config->send_client_cert = true;
181 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54182 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45183 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54184 response_.cert_request_info->host_and_port, client_cert,
185 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11186 // Reset the other member variables.
187 // Note: this is necessary only with SSL renegotiation.
188 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50189 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11190 int rv = DoLoop(OK);
191 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41192 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11193 return rv;
194}
195
[email protected]49639fa2011-12-20 23:22:41196int HttpNetworkTransaction::RestartWithAuth(
197 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22198 HttpAuth::Target target = pending_auth_target_;
199 if (target == HttpAuth::AUTH_NONE) {
200 NOTREACHED();
201 return ERR_UNEXPECTED;
202 }
[email protected]0757e7702009-03-27 04:00:22203 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42204
[email protected]f3cf9802011-10-28 18:44:58205 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13206
[email protected]49639fa2011-12-20 23:22:41207 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38208
209 int rv = OK;
210 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
211 // In this case, we've gathered credentials for use with proxy
212 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50213 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38214 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59215 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54216 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58217 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54218 } else {
[email protected]8e6441ca2010-08-19 05:56:38219 // In this case, we've gathered credentials for the server or the proxy
220 // but it is not during the tunneling phase.
221 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54222 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38223 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54224 }
[email protected]c3b35c22008-09-27 03:19:42225
[email protected]c3b35c22008-09-27 03:19:42226 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41227 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42228 return rv;
[email protected]96d570e42008-08-05 22:43:04229}
230
[email protected]f9ee6b52008-11-08 06:46:23231void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
232 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38233 DCHECK(!stream_request_.get());
234
[email protected]2d2697f92009-02-18 21:00:32235 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57236 // Even if the server says the connection is keep-alive, we have to be
237 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34238 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57239 // If the response body hasn't been completely read, we need to drain
240 // it first.
[email protected]351ab642010-08-05 16:55:31241 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32242 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57243 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32244 read_buf_len_ = kDrainBodyBufferSize;
245 return;
246 }
[email protected]0877e3d2009-10-17 22:29:57247 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09248 }
249
[email protected]2d2697f92009-02-18 21:00:32250 // We don't need to drain the response body, so we act as if we had drained
251 // the response body.
252 DidDrainBodyForAuthRestart(keep_alive);
253}
254
255void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38256 DCHECK(!stream_request_.get());
257
258 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19259 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22260 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58261 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34262 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38263 // We should call connection_->set_idle_time(), but this doesn't occur
264 // often enough to be worth the trouble.
265 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39266 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38267 }
[email protected]697ef4c2010-10-14 16:38:58268
269 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46270 // Close the stream and mark it as not_reusable. Even in the
271 // keep_alive case, we've determined that the stream_ is not
272 // reusable if new_stream is NULL.
273 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58274 next_state_ = STATE_CREATE_STREAM;
275 } else {
sclittlefb249892015-09-10 21:33:22276 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19277 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22278 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58279 next_state_ = STATE_INIT_STREAM;
280 }
281 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32282 }
[email protected]f9ee6b52008-11-08 06:46:23283
284 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58285 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23286}
287
[email protected]8e6441ca2010-08-19 05:56:38288bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
289 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
290 HaveAuth(pending_auth_target_);
291}
292
[email protected]9dea9e1f2009-01-29 00:30:47293int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41294 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04295 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35296 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04297
[email protected]ad8e04a2010-11-01 04:16:27298 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38299 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04300 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29301 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04302 // bytes when establishing a tunnel because they might be controlled by
303 // an active network attacker. We don't worry about this for HTTP
304 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29305 // We reach this case when the user cancels a 407 proxy auth prompt. We
306 // also don't worry about this for an HTTPS Proxy, because the
307 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04308 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46309 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58310 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54311 LOG(WARNING) << "Blocked proxy response with status "
312 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15313 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04314 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44315 }
316
[email protected]e60e47a2010-07-14 03:37:18317 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15318 next_state_ = STATE_READ_BODY;
319
320 // We have reached the end of Start state machine, reset the requestinfo to
321 // null.
322 // RequestInfo is a member of the HttpTransaction's consumer and is useful
323 // only till final response headers are received. A reset will ensure that
324 // HttpRequestInfo is only used up until final response headers are received.
325 // Resetting is allowed so that the transaction can be disassociated from its
326 // creating consumer in cases where it is shared for writing to the cache.
327 // It is also safe to reset it to null at this point since upload_data_stream
328 // is also not used in the Read state machine.
329 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18330
[email protected]96d570e42008-08-05 22:43:04331 read_buf_ = buf;
332 read_buf_len_ = buf_len;
333
[email protected]96d570e42008-08-05 22:43:04334 int rv = DoLoop(OK);
335 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41336 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04337 return rv;
338}
339
[email protected]8cd06c02014-01-25 07:50:14340void HttpNetworkTransaction::StopCaching() {}
341
[email protected]79e1fd62013-06-20 06:50:04342bool HttpNetworkTransaction::GetFullRequestHeaders(
343 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32344 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04345 *headers = request_headers_;
346 return true;
347}
348
sclittle4de1bab92015-09-22 21:28:24349int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
350 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19351 if (stream_)
352 total_received_bytes += stream_->GetTotalReceivedBytes();
353 return total_received_bytes;
354}
355
sclittlefb249892015-09-10 21:33:22356int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
357 int64_t total_sent_bytes = total_sent_bytes_;
358 if (stream_)
359 total_sent_bytes += stream_->GetTotalSentBytes();
360 return total_sent_bytes;
361}
362
[email protected]8cd06c02014-01-25 07:50:14363void HttpNetworkTransaction::DoneReading() {}
364
[email protected]96d570e42008-08-05 22:43:04365const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55366 return &response_;
[email protected]96d570e42008-08-05 22:43:04367}
368
369LoadState HttpNetworkTransaction::GetLoadState() const {
370 // TODO(wtc): Define a new LoadState value for the
371 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
372 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50373 case STATE_THROTTLE_COMPLETE:
374 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48375 case STATE_CREATE_STREAM:
376 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50377 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38378 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15379 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
380 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57381 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04382 return LOAD_STATE_SENDING_REQUEST;
383 case STATE_READ_HEADERS_COMPLETE:
384 return LOAD_STATE_WAITING_FOR_RESPONSE;
385 case STATE_READ_BODY_COMPLETE:
386 return LOAD_STATE_READING_RESPONSE;
387 default:
388 return LOAD_STATE_IDLE;
389 }
390}
391
[email protected]8cd06c02014-01-25 07:50:14392void HttpNetworkTransaction::SetQuicServerInfo(
393 QuicServerInfo* quic_server_info) {}
394
[email protected]5033ab82013-03-22 20:17:46395bool HttpNetworkTransaction::GetLoadTimingInfo(
396 LoadTimingInfo* load_timing_info) const {
397 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
398 return false;
399
400 load_timing_info->proxy_resolve_start =
401 proxy_info_.proxy_resolve_start_time();
402 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
403 load_timing_info->send_start = send_start_time_;
404 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46405 return true;
406}
407
ttuttled9dbc652015-09-29 20:00:59408bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36409 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59410 return false;
411
412 *endpoint = remote_endpoint_;
413 return true;
414}
415
zhongyi48704c182015-12-07 07:52:02416void HttpNetworkTransaction::PopulateNetErrorDetails(
417 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12418 *details = net_error_details_;
419 if (stream_)
420 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02421}
422
[email protected]5033ab82013-03-22 20:17:46423void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
424 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50425
426 // TODO: Note that if throttling is ever implemented below this
427 // level, either of the two below calls may result in request
428 // completion, callbacks, and the potential deletion of this object
429 // (like the call below to throttle_->SetPriority()). In that case,
430 // this code will need to be modified.
[email protected]bf828982013-08-14 18:01:47431 if (stream_request_)
432 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03433 if (stream_)
434 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50435
436 if (throttle_)
437 throttle_->SetPriority(priority);
438 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46439}
440
[email protected]831e4a32013-11-14 02:14:44441void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
442 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
443 websocket_handshake_stream_base_create_helper_ = create_helper;
444}
445
[email protected]1826a402014-01-08 15:40:48446void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
447 const BeforeNetworkStartCallback& callback) {
448 before_network_start_callback_ = callback;
449}
450
ryansturm49a8cb12016-06-15 16:51:09451void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
452 const BeforeHeadersSentCallback& callback) {
453 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27454}
455
[email protected]1826a402014-01-08 15:40:48456int HttpNetworkTransaction::ResumeNetworkStart() {
457 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
458 return DoLoop(OK);
459}
460
[email protected]102e27c2011-02-23 01:01:31461void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
462 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39463 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50464 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38465 DCHECK(stream_request_.get());
466
sclittlefb249892015-09-10 21:33:22467 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19468 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22469 total_sent_bytes_ += stream_->GetTotalSentBytes();
470 }
[email protected]8e6441ca2010-08-19 05:56:38471 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14472 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31473 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52474 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
475 response_.alpn_negotiated_protocol = SSLClientSocket::NextProtoToString(
bnc6227b26e2016-08-12 02:00:43476 stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38477 response_.was_fetched_via_spdy = stream_request_->using_spdy();
478 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15479 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47480 response_.proxy_server = proxy_info_.proxy_server();
481 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
482 response_.proxy_server = ProxyServer::Direct();
483 else
484 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38485 OnIOComplete(OK);
486}
487
xunjieli5749218c2016-03-22 16:43:06488void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08489 const SSLConfig& used_ssl_config,
490 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06491 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08492 NOTREACHED();
493}
494
[email protected]a9cf2b92013-10-30 12:08:49495void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50496 const SSLConfig& used_ssl_config,
497 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49498 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44499 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50500}
501
[email protected]102e27c2011-02-23 01:01:31502void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13503 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50504 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38505 DCHECK_NE(OK, result);
506 DCHECK(stream_request_.get());
507 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14508 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38509
510 OnIOComplete(result);
511}
512
[email protected]102e27c2011-02-23 01:01:31513void HttpNetworkTransaction::OnCertificateError(
514 int result,
515 const SSLConfig& used_ssl_config,
516 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50517 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38518 DCHECK_NE(OK, result);
519 DCHECK(stream_request_.get());
520 DCHECK(!stream_.get());
521
522 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14523 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38524
525 // TODO(mbelshe): For now, we're going to pass the error through, and that
526 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50527 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
528 // good and the user chooses to ignore the error. This is not ideal, but not
529 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38530
531 OnIOComplete(result);
532}
533
534void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50535 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31536 const SSLConfig& used_ssl_config,
537 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50538 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38539 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50540 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38541
542 establishing_tunnel_ = true;
543 response_.headers = proxy_response.headers;
544 response_.auth_challenge = proxy_response.auth_challenge;
545 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14546 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31547 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38548
549 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
550 pending_auth_target_ = HttpAuth::AUTH_PROXY;
551
552 DoCallback(OK);
553}
554
555void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31556 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50557 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50558 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38559
[email protected]102957f2011-09-02 17:10:14560 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38561 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58562 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38563}
564
[email protected]511f6f52010-12-17 03:58:29565void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
566 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31567 const SSLConfig& used_ssl_config,
568 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39569 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29570 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
571
ttuttle1f2d7e92015-04-28 16:17:47572 CopyConnectionAttemptsFromStreamRequest();
573
[email protected]511f6f52010-12-17 03:58:29574 headers_valid_ = true;
575 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14576 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31577 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22578 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19579 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22580 total_sent_bytes_ += stream_->GetTotalSentBytes();
581 }
[email protected]511f6f52010-12-17 03:58:29582 stream_.reset(stream);
583 stream_request_.reset(); // we're done with the stream request
584 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
585}
586
zhongyi48704c182015-12-07 07:52:02587void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12588 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02589}
590
ttuttle1f2d7e92015-04-28 16:17:47591void HttpNetworkTransaction::GetConnectionAttempts(
592 ConnectionAttempts* out) const {
593 *out = connection_attempts_;
594}
595
rdsmith1d343be52016-10-21 20:37:50596void HttpNetworkTransaction::OnThrottleStateChanged() {
597 // TODO(rdsmith): This DCHECK is dependent on the only transition
598 // being from throttled->unthrottled. That is true right now, but may not
599 // be so in the future.
600 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
601
602 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
603
604 DoLoop(OK);
605}
606
Adam Ricecb76ac62015-02-20 05:33:25607bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37608 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52609}
610
nharperb7441ef2016-01-25 23:54:14611bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
612 if (!IsSecureRequest())
613 return false;
614 SSLInfo ssl_info;
615 stream_->GetSSLInfo(&ssl_info);
616 return ssl_info.token_binding_negotiated &&
617 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
618 session_->params().channel_id_service;
619}
620
621void HttpNetworkTransaction::RecordTokenBindingSupport() const {
622 // This enum is used for an UMA histogram - do not change or re-use values.
623 enum {
624 DISABLED = 0,
625 CLIENT_ONLY = 1,
626 CLIENT_AND_SERVER = 2,
627 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
628 TOKEN_BINDING_SUPPORT_MAX
629 } supported;
630 if (!IsSecureRequest())
631 return;
632 SSLInfo ssl_info;
633 stream_->GetSSLInfo(&ssl_info);
634 if (!session_->params().enable_token_binding) {
635 supported = DISABLED;
636 } else if (!session_->params().channel_id_service) {
637 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
638 } else if (ssl_info.token_binding_negotiated) {
639 supported = CLIENT_AND_SERVER;
640 } else {
641 supported = CLIENT_ONLY;
642 }
643 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
644 TOKEN_BINDING_SUPPORT_MAX);
645}
646
Adam Rice425cf122015-01-19 06:18:24647bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12648 return (proxy_info_.is_http() || proxy_info_.is_https() ||
649 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24650 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
651}
652
initial.commit586acc5fe2008-07-26 22:42:52653void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50654 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41655 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52656
[email protected]96d570e42008-08-05 22:43:04657 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41658 CompletionCallback c = callback_;
659 callback_.Reset();
660 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52661}
662
663void HttpNetworkTransaction::OnIOComplete(int result) {
664 int rv = DoLoop(result);
665 if (rv != ERR_IO_PENDING)
666 DoCallback(rv);
667}
668
669int HttpNetworkTransaction::DoLoop(int result) {
670 DCHECK(next_state_ != STATE_NONE);
671
672 int rv = result;
673 do {
674 State state = next_state_;
675 next_state_ = STATE_NONE;
676 switch (state) {
rdsmith1d343be52016-10-21 20:37:50677 case STATE_THROTTLE:
678 DCHECK_EQ(OK, rv);
679 rv = DoThrottle();
680 break;
681 case STATE_THROTTLE_COMPLETE:
682 DCHECK_EQ(OK, rv);
683 rv = DoThrottleComplete();
684 break;
[email protected]1826a402014-01-08 15:40:48685 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
686 DCHECK_EQ(OK, rv);
687 rv = DoNotifyBeforeCreateStream();
688 break;
[email protected]82918cc2010-08-25 17:24:50689 case STATE_CREATE_STREAM:
690 DCHECK_EQ(OK, rv);
691 rv = DoCreateStream();
692 break;
693 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46694 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
695 // solved.
696 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50697 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46698 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50699 break;
[email protected]351ab642010-08-05 16:55:31700 case STATE_INIT_STREAM:
701 DCHECK_EQ(OK, rv);
702 rv = DoInitStream();
703 break;
704 case STATE_INIT_STREAM_COMPLETE:
705 rv = DoInitStreamComplete(rv);
706 break;
[email protected]044de0642010-06-17 10:42:15707 case STATE_GENERATE_PROXY_AUTH_TOKEN:
708 DCHECK_EQ(OK, rv);
709 rv = DoGenerateProxyAuthToken();
710 break;
711 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
712 rv = DoGenerateProxyAuthTokenComplete(rv);
713 break;
714 case STATE_GENERATE_SERVER_AUTH_TOKEN:
715 DCHECK_EQ(OK, rv);
716 rv = DoGenerateServerAuthToken();
717 break;
718 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
719 rv = DoGenerateServerAuthTokenComplete(rv);
720 break;
nharperd6e65822016-03-30 23:05:48721 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14722 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48723 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14724 break;
nharperd6e65822016-03-30 23:05:48725 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
726 rv = DoGetProvidedTokenBindingKeyComplete(rv);
727 break;
728 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
729 DCHECK_EQ(OK, rv);
730 rv = DoGetReferredTokenBindingKey();
731 break;
732 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
733 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14734 break;
[email protected]daddea62012-09-19 05:51:13735 case STATE_INIT_REQUEST_BODY:
736 DCHECK_EQ(OK, rv);
737 rv = DoInitRequestBody();
738 break;
739 case STATE_INIT_REQUEST_BODY_COMPLETE:
740 rv = DoInitRequestBodyComplete(rv);
741 break;
[email protected]4875ba12011-03-30 22:31:51742 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55743 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00744 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51745 rv = DoBuildRequest();
746 break;
747 case STATE_BUILD_REQUEST_COMPLETE:
748 rv = DoBuildRequestComplete(rv);
749 break;
750 case STATE_SEND_REQUEST:
751 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57752 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52753 break;
[email protected]0877e3d2009-10-17 22:29:57754 case STATE_SEND_REQUEST_COMPLETE:
755 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43756 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00757 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52758 break;
759 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55760 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00761 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52762 rv = DoReadHeaders();
763 break;
764 case STATE_READ_HEADERS_COMPLETE:
765 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43766 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00767 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52768 break;
769 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55770 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00771 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52772 rv = DoReadBody();
773 break;
774 case STATE_READ_BODY_COMPLETE:
775 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43776 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00777 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52778 break;
[email protected]2d2697f92009-02-18 21:00:32779 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55780 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53781 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00782 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32783 rv = DoDrainBodyForAuthRestart();
784 break;
785 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
786 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32789 break;
initial.commit586acc5fe2008-07-26 22:42:52790 default:
791 NOTREACHED() << "bad state";
792 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 }
795 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
796
797 return rv;
798}
799
rdsmith1d343be52016-10-21 20:37:50800int HttpNetworkTransaction::DoThrottle() {
801 throttle_ = session_->throttler()->CreateThrottle(
802 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
803 next_state_ = STATE_THROTTLE_COMPLETE;
804
805 if (throttle_->IsThrottled()) {
806 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
807 return ERR_IO_PENDING;
808 }
809
810 return OK;
811}
812
813int HttpNetworkTransaction::DoThrottleComplete() {
814 DCHECK(throttle_);
815 DCHECK(!throttle_->IsThrottled());
816
817 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
818
819 return OK;
820}
821
[email protected]1826a402014-01-08 15:40:48822int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
823 next_state_ = STATE_CREATE_STREAM;
824 bool defer = false;
825 if (!before_network_start_callback_.is_null())
826 before_network_start_callback_.Run(&defer);
827 if (!defer)
828 return OK;
829 return ERR_IO_PENDING;
830}
831
[email protected]82918cc2010-08-25 17:24:50832int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34833 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26834 tracked_objects::ScopedTracker tracking_profile(
835 FROM_HERE_WITH_EXPLICIT_FUNCTION(
836 "424359 HttpNetworkTransaction::DoCreateStream"));
837
ttuttlec0c828492015-05-15 01:25:55838 response_.network_accessed = true;
839
[email protected]82918cc2010-08-25 17:24:50840 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44841 if (ForWebSocketHandshake()) {
842 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23843 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44844 ->RequestWebSocketHandshakeStream(
845 *request_,
846 priority_,
847 server_ssl_config_,
848 proxy_ssl_config_,
849 this,
850 websocket_handshake_stream_base_create_helper_,
851 net_log_));
852 } else {
853 stream_request_.reset(
854 session_->http_stream_factory()->RequestStream(
855 *request_,
856 priority_,
857 server_ssl_config_,
858 proxy_ssl_config_,
859 this,
860 net_log_));
861 }
[email protected]26816882010-10-14 18:03:09862 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38863 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31864}
865
[email protected]82918cc2010-08-25 17:24:50866int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47867 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
868 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
869 // which resets the stream request first. Therefore, we have to grab the
870 // connection attempts in *that* function instead of here in that case.
871 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
872 CopyConnectionAttemptsFromStreamRequest();
873
davidben701ca982015-05-18 21:21:42874 if (request_->url.SchemeIsCryptographic())
875 RecordSSLFallbackMetrics(result);
876
[email protected]394816e92010-08-03 07:38:59877 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50878 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38879 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33880 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
881 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29882 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
883 // Return OK and let the caller read the proxy's error page
884 next_state_ = STATE_NONE;
885 return OK;
bncfacdd852015-01-09 19:22:54886 } else if (result == ERR_HTTP_1_1_REQUIRED ||
887 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
888 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59889 }
890
[email protected]bd0b6772011-01-11 19:59:30891 // Handle possible handshake errors that may have occurred if the stream
892 // used SSL for one or more of the layers.
893 result = HandleSSLHandshakeError(result);
894
[email protected]8e6441ca2010-08-19 05:56:38895 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09896 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38897 return result;
[email protected]394816e92010-08-03 07:38:59898}
899
[email protected]82918cc2010-08-25 17:24:50900int HttpNetworkTransaction::DoInitStream() {
901 DCHECK(stream_.get());
902 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59903
904 stream_->GetRemoteEndpoint(&remote_endpoint_);
905
[email protected]262eec82013-03-19 21:01:36906 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50907}
908
909int HttpNetworkTransaction::DoInitStreamComplete(int result) {
910 if (result == OK) {
911 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50912 } else {
[email protected]82918cc2010-08-25 17:24:50913 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26914 result = HandleIOError(result);
915
916 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22917 if (stream_) {
918 total_received_bytes_ += stream_->GetTotalReceivedBytes();
919 total_sent_bytes_ += stream_->GetTotalSentBytes();
920 }
zhongyica364fbb2015-12-12 03:39:12921 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50922 }
923
924 return result;
925}
926
[email protected]044de0642010-06-17 10:42:15927int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
928 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
929 if (!ShouldApplyProxyAuth())
930 return OK;
[email protected]394816e92010-08-03 07:38:59931 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
932 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09933 auth_controllers_[target] =
934 new HttpAuthController(target,
935 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31936 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09937 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59938 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41939 io_callback_,
[email protected]394816e92010-08-03 07:38:59940 net_log_);
[email protected]044de0642010-06-17 10:42:15941}
942
943int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
944 DCHECK_NE(ERR_IO_PENDING, rv);
945 if (rv == OK)
946 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
947 return rv;
948}
949
950int HttpNetworkTransaction::DoGenerateServerAuthToken() {
951 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59952 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54953 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09954 auth_controllers_[target] =
955 new HttpAuthController(target,
956 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31957 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09958 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54959 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
960 auth_controllers_[target]->DisableEmbeddedIdentity();
961 }
[email protected]044de0642010-06-17 10:42:15962 if (!ShouldApplyServerAuth())
963 return OK;
[email protected]394816e92010-08-03 07:38:59964 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41965 io_callback_,
[email protected]394816e92010-08-03 07:38:59966 net_log_);
[email protected]044de0642010-06-17 10:42:15967}
968
969int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
970 DCHECK_NE(ERR_IO_PENDING, rv);
971 if (rv == OK)
nharperd6e65822016-03-30 23:05:48972 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15973 return rv;
974}
975
nharperd6e65822016-03-30 23:05:48976int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
977 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14978 if (!IsTokenBindingEnabled())
979 return OK;
980
mikecirone8b85c432016-09-08 19:11:00981 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperb7441ef2016-01-25 23:54:14982 ChannelIDService* channel_id_service = session_->params().channel_id_service;
983 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48984 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14985 &token_binding_request_);
986}
987
nharperd6e65822016-03-30 23:05:48988int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14989 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48990 if (IsTokenBindingEnabled()) {
991 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00992 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48993 }
994
995 if (rv == OK)
996 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
997 return rv;
998}
999
1000int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1001 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1002 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141003 return OK;
1004
mikecirone8b85c432016-09-08 19:11:001005 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperd6e65822016-03-30 23:05:481006 ChannelIDService* channel_id_service = session_->params().channel_id_service;
1007 return channel_id_service->GetOrCreateChannelID(
1008 request_->token_binding_referrer, &referred_token_binding_key_,
1009 io_callback_, &token_binding_request_);
1010}
1011
1012int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1013 DCHECK_NE(ERR_IO_PENDING, rv);
1014 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1015 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001016 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481017 }
1018 if (rv == OK)
1019 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141020 return rv;
1021}
1022
1023int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241024 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141025 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1026 GetHostAndOptionalPort(request_->url));
1027
1028 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241029 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141030 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1031 "keep-alive");
1032 } else {
1033 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1034 }
1035
[email protected]2979a492011-04-06 00:29:141036 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111037 if (request_->upload_data_stream) {
1038 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141039 request_headers_.SetHeader(
1040 HttpRequestHeaders::kTransferEncoding, "chunked");
1041 } else {
1042 request_headers_.SetHeader(
1043 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111044 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141045 }
csharrisonf473dd192015-08-18 13:54:131046 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141047 // An empty POST/PUT request still needs a content length. As for HEAD,
1048 // IE and Safari also add a content length header. Presumably it is to
1049 // support sending a HEAD request to an URL that only expects to be sent a
1050 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131051 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1052 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141053 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1054 }
1055
nharperb7441ef2016-01-25 23:54:141056 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481057 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141058 std::string token_binding_header;
1059 int rv = BuildTokenBindingHeader(&token_binding_header);
1060 if (rv != OK)
1061 return rv;
1062 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1063 token_binding_header);
1064 }
1065
[email protected]2979a492011-04-06 00:29:141066 // Honor load flags that impact proxy caches.
1067 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1068 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1069 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1070 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1071 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1072 }
1073
1074 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1075 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1076 &request_headers_);
1077 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1078 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1079 &request_headers_);
1080
[email protected]c10450102011-06-27 09:06:161081 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201082
ryansturm49a8cb12016-06-15 16:51:091083 if (!before_headers_sent_callback_.is_null())
1084 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201085
[email protected]173f8e22013-04-10 04:18:201086 response_.did_use_http_auth =
1087 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1088 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141089 return OK;
1090}
1091
1092int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471093 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141094 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351095 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1096 TokenBindingType::PROVIDED,
1097 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141098 if (rv != OK)
1099 return rv;
1100 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481101 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1102 provided_token_binding_key_.get(), signed_ekm,
1103 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141104 if (rv != OK)
1105 return rv;
nharperd6e65822016-03-30 23:05:481106
nharperb7441ef2016-01-25 23:54:141107 std::vector<base::StringPiece> token_bindings;
1108 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481109
1110 std::string referred_token_binding;
1111 if (referred_token_binding_key_) {
1112 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351113 int rv = stream_->GetTokenBindingSignature(
1114 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1115 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481116 if (rv != OK)
1117 return rv;
1118 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1119 referred_token_binding_key_.get(),
1120 referred_signed_ekm, &referred_token_binding);
1121 if (rv != OK)
1122 return rv;
1123 token_bindings.push_back(referred_token_binding);
1124 }
nharperb7441ef2016-01-25 23:54:141125 std::string header;
1126 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1127 if (rv != OK)
1128 return rv;
nharper78e6d2b2016-09-21 05:42:351129 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471130 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1131 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1132 header_creation_time,
1133 base::TimeDelta::FromMilliseconds(1),
1134 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141135 return OK;
[email protected]2979a492011-04-06 00:29:141136}
1137
[email protected]daddea62012-09-19 05:51:131138int HttpNetworkTransaction::DoInitRequestBody() {
1139 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131140 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111141 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521142 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131143 return rv;
1144}
[email protected]4875ba12011-03-30 22:31:511145
[email protected]daddea62012-09-19 05:51:131146int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1147 if (result == OK)
1148 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131149 return result;
1150}
1151
1152int HttpNetworkTransaction::DoBuildRequest() {
1153 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511154 headers_valid_ = false;
1155
1156 // This is constructed lazily (instead of within our Start method), so that
1157 // we have proxy info available.
1158 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241159 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141160 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511161 }
1162
[email protected]4875ba12011-03-30 22:31:511163 return OK;
1164}
1165
1166int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241167 if (result == OK)
1168 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511169 return result;
1170}
1171
[email protected]0877e3d2009-10-17 22:29:571172int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341173 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261174 tracked_objects::ScopedTracker tracking_profile(
1175 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1176 "424359 HttpNetworkTransaction::DoSendRequest"));
1177
[email protected]58e32bb2013-01-21 18:23:251178 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571179 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1180
[email protected]bf3eb002012-11-15 05:50:111181 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521182}
1183
[email protected]0877e3d2009-10-17 22:29:571184int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251185 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361186
1187 if (result == ERR_HTTP_1_1_REQUIRED ||
1188 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1189 return HandleHttp11Required(result);
1190 }
1191
initial.commit586acc5fe2008-07-26 22:42:521192 if (result < 0)
1193 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571194 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521195 return OK;
1196}
1197
1198int HttpNetworkTransaction::DoReadHeaders() {
1199 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411200 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521201}
1202
1203int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111204 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1205 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381206 if (IsCertificateError(result)) {
1207 // We don't handle a certificate error during SSL renegotiation, so we
1208 // have to return an error that's not in the certificate error range
1209 // (-2xx).
1210 LOG(ERROR) << "Got a server certificate with error " << result
1211 << " during SSL renegotiation";
1212 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1213 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1214 // TODO(wtc): Need a test case for this code path!
1215 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251216 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381217 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501218 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381219 result = HandleCertificateRequest(result);
1220 if (result == OK)
1221 return result;
[email protected]2181ea002009-06-09 01:37:271222 }
1223
bncfacdd852015-01-09 19:22:541224 if (result == ERR_HTTP_1_1_REQUIRED ||
1225 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1226 return HandleHttp11Required(result);
1227 }
1228
[email protected]c871864d72014-03-13 19:56:191229 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1230 // response headers were received, we do the best we can to make sense of it
1231 // and send it back up the stack.
1232 //
1233 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1234 // bizarre for SPDY. Assuming this logic is useful at all.
1235 // TODO(davidben): Bubble the error code up so we do not cache?
1236 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1237 result = OK;
1238
1239 if (result < 0)
1240 return HandleIOError(result);
1241
[email protected]90499482013-06-01 00:39:501242 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521243
[email protected]d58ceea82014-06-04 10:55:541244 // On a 408 response from the server ("Request Timeout") on a stale socket,
1245 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041246 // Headers can be NULL because of http://crbug.com/384554.
1247 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541248 stream_->IsConnectionReused()) {
1249 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001250 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541251 response_.headers->response_code());
1252 // This will close the socket - it would be weird to try and reuse it, even
1253 // if the server doesn't actually close it.
1254 ResetConnectionAndRequestForResend();
1255 return OK;
1256 }
1257
[email protected]93f8b562012-03-27 01:00:161258 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211259 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161260 const int response_code = response_.headers->response_code();
1261 UMA_HISTOGRAM_ENUMERATION(
1262 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1263 }
1264
[email protected]3abacd62012-06-10 20:20:321265 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001266 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321267 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391268
bncbe0f6af2015-10-15 17:49:561269 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571270 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1271 // indicates a buggy server. See:
1272 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1273 if (request_->method == "PUT")
1274 return ERR_METHOD_NOT_SUPPORTED;
1275 }
[email protected]4ddaf2502008-10-23 18:26:191276
[email protected]0877e3d2009-10-17 22:29:571277 // Check for an intermediate 100 Continue response. An origin server is
1278 // allowed to send this response even if we didn't ask for it, so we just
1279 // need to skip over it.
1280 // We treat any other 1xx in this same way (although in practice getting
1281 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441282 // Unless this is a WebSocket request, in which case we pass it on up.
1283 if (response_.headers->response_code() / 100 == 1 &&
1284 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451285 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571286 next_state_ = STATE_READ_HEADERS;
1287 return OK;
1288 }
1289
bncb26024382016-06-29 02:39:451290 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111291 session_->http_stream_factory()->ProcessAlternativeServices(
1292 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1293 }
[email protected]564b4912010-03-09 16:30:421294
asanka5ffd5d72016-03-23 16:20:491295 if (IsSecureRequest())
1296 stream_->GetSSLInfo(&response_.ssl_info);
1297
[email protected]e772db3f2010-07-12 18:11:131298 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571299 if (rv != OK)
1300 return rv;
1301
[email protected]0877e3d2009-10-17 22:29:571302 headers_valid_ = true;
1303 return OK;
initial.commit586acc5fe2008-07-26 22:42:521304}
1305
1306int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501307 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131308 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381309 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521310
1311 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501312 return stream_->ReadResponseBody(
1313 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521314}
1315
1316int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1317 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381318 bool done = false;
1319 if (result <= 0) {
1320 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521321 done = true;
[email protected]8e6441ca2010-08-19 05:56:381322 }
[email protected]9492e4a2010-02-24 00:58:461323
mmenkebd84c392015-09-02 14:12:341324 // Clean up connection if we are done.
1325 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381326 // Note: Just because IsResponseBodyComplete is true, we're not
1327 // necessarily "done". We're only "done" when it is the last
1328 // read on this HttpNetworkTransaction, which will be signified
1329 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341330 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381331 // the stream. No need to compute it here just to pass back
1332 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341333 bool keep_alive =
1334 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521335
[email protected]8e6441ca2010-08-19 05:56:381336 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011337 // Note: we don't reset the stream here. We've closed it, but we still
1338 // need it around so that callers can call methods such as
1339 // GetUploadProgress() and have them be meaningful.
1340 // TODO(mbelshe): This means we closed the stream here, and we close it
1341 // again in ~HttpNetworkTransaction. Clean that up.
1342
[email protected]8e6441ca2010-08-19 05:56:381343 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521344 }
1345
1346 // Clear these to avoid leaving around old state.
1347 read_buf_ = NULL;
1348 read_buf_len_ = 0;
1349
1350 return result;
1351}
1352
[email protected]2d2697f92009-02-18 21:00:321353int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1354 // This method differs from DoReadBody only in the next_state_. So we just
1355 // call DoReadBody and override the next_state_. Perhaps there is a more
1356 // elegant way for these two methods to share code.
1357 int rv = DoReadBody();
1358 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1359 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1360 return rv;
1361}
1362
[email protected]0877e3d2009-10-17 22:29:571363// TODO(wtc): This method and the DoReadBodyComplete method are almost
1364// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321365int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231366 // keep_alive defaults to true because the very reason we're draining the
1367 // response body is to reuse the connection for auth restart.
1368 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321369 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571370 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321371 done = true;
[email protected]68873ba2009-06-04 21:49:231372 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311373 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571374 done = true;
[email protected]2d2697f92009-02-18 21:00:321375 }
1376
1377 if (done) {
1378 DidDrainBodyForAuthRestart(keep_alive);
1379 } else {
1380 // Keep draining.
1381 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1382 }
1383
1384 return OK;
1385}
1386
[email protected]5e363962009-06-19 19:57:011387int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381388 // There are two paths through which the server can request a certificate
1389 // from us. The first is during the initial handshake, the second is
1390 // during SSL renegotiation.
1391 //
1392 // In both cases, we want to close the connection before proceeding.
1393 // We do this for two reasons:
1394 // First, we don't want to keep the connection to the server hung for a
1395 // long time while the user selects a certificate.
1396 // Second, even if we did keep the connection open, NSS has a bug where
1397 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581398 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381399
1400 if (stream_.get()) {
1401 // Since we already have a stream, we're being called as part of SSL
1402 // renegotiation.
1403 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191404 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221405 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381406 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121407 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381408 }
1409
[email protected]26816882010-10-14 18:03:091410 // The server is asking for a client certificate during the initial
1411 // handshake.
1412 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011413
[email protected]ec229bc92010-11-22 09:51:451414 // If the user selected one of the certificates in client_certs or declined
1415 // to provide one for this server before, use the past decision
1416 // automatically.
1417 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541418 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451419 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541420 response_.cert_request_info->host_and_port, &client_cert,
1421 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451422 if (!found_cached_cert)
1423 return error;
1424
1425 // Check that the certificate selected is still a certificate the server
1426 // is likely to accept, based on the criteria supplied in the
1427 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501428 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261429 const std::vector<std::string>& cert_authorities =
1430 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451431
[email protected]f1958c382013-02-07 00:15:261432 bool cert_still_valid = cert_authorities.empty() ||
1433 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451434 if (!cert_still_valid)
1435 return error;
[email protected]5e363962009-06-19 19:57:011436 }
[email protected]ec229bc92010-11-22 09:51:451437
1438 // TODO(davidben): Add a unit test which covers this path; we need to be
1439 // able to send a legitimate certificate and also bypass/clear the
1440 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141441 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1442 &proxy_ssl_config_ : &server_ssl_config_;
1443 ssl_config->send_client_cert = true;
1444 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541445 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451446 next_state_ = STATE_CREATE_STREAM;
1447 // Reset the other member variables.
1448 // Note: this is necessary only with SSL renegotiation.
1449 ResetStateForRestart();
1450 return OK;
[email protected]0b45559b2009-06-12 21:45:111451}
1452
bncfacdd852015-01-09 19:22:541453int HttpNetworkTransaction::HandleHttp11Required(int error) {
1454 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1455 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1456
1457 if (error == ERR_HTTP_1_1_REQUIRED) {
1458 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1459 } else {
1460 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1461 }
1462 ResetConnectionAndRequestForResend();
1463 return OK;
1464}
1465
[email protected]384cc73a2013-12-08 22:41:031466void HttpNetworkTransaction::HandleClientAuthError(int error) {
1467 if (server_ssl_config_.send_client_cert &&
1468 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1469 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411470 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031471 }
1472}
1473
[email protected]bd0b6772011-01-11 19:59:301474// TODO(rch): This does not correctly handle errors when an SSL proxy is
1475// being used, as all of the errors are handled as if they were generated
1476// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381477// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301478int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1479 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031480 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301481
davidbena4c9d062015-04-03 22:34:251482 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1483 // reflect servers require a deprecated cipher rather than merely prefer
1484 // it. This, however, has no security benefit until the ciphers are actually
1485 // removed.
davidben14b1a532015-10-30 16:01:091486 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251487 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1488 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1489 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001490 NetLogEventType::SSL_CIPHER_FALLBACK,
davidbena4c9d062015-04-03 22:34:251491 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091492 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251493 ResetConnectionAndRequestForResend();
1494 return OK;
1495 }
1496
initial.commit586acc5fe2008-07-26 22:42:521497 return error;
1498}
1499
[email protected]bd0b6772011-01-11 19:59:301500// This method determines whether it is safe to resend the request after an
1501// IO error. It can only be called in response to request header or body
1502// write errors or response header read errors. It should not be used in
1503// other cases, such as a Connect error.
1504int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031505 // Because the peer may request renegotiation with client authentication at
1506 // any time, check and handle client authentication errors.
1507 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301508
1509 switch (error) {
1510 // If we try to reuse a connection that the server is in the process of
1511 // closing, we may end up successfully writing out our request (or a
1512 // portion of our request) only to find a connection error when we try to
1513 // read from (or finish writing to) the socket.
1514 case ERR_CONNECTION_RESET:
1515 case ERR_CONNECTION_CLOSED:
1516 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511517 // There can be a race between the socket pool checking checking whether a
1518 // socket is still connected, receiving the FIN, and sending/reading data
1519 // on a reused socket. If we receive the FIN between the connectedness
1520 // check and writing/reading from the socket, we may first learn the socket
1521 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1522 // likely happen when trying to retrieve its IP address.
1523 // See http://crbug.com/105824 for more details.
1524 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491525 // If a socket is closed on its initial request, HttpStreamParser returns
1526 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1527 // preconnected but failed to be used before the server timed it out.
1528 case ERR_EMPTY_RESPONSE:
1529 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381530 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001531 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301532 ResetConnectionAndRequestForResend();
1533 error = OK;
1534 }
1535 break;
[email protected]8753a122011-10-16 08:05:081536 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001537 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271538 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381539 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001540 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001541 ResetConnectionAndRequestForResend();
1542 error = OK;
1543 break;
[email protected]bd0b6772011-01-11 19:59:301544 }
1545 return error;
1546}
1547
[email protected]c3b35c22008-09-27 03:19:421548void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581549 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221550 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191551 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221552 total_sent_bytes_ += stream_->GetTotalSentBytes();
1553 }
zhongyica364fbb2015-12-12 03:39:121554 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581555}
1556
1557void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251558 send_start_time_ = base::TimeTicks();
1559 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251560
[email protected]0757e7702009-03-27 04:00:221561 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421562 read_buf_ = NULL;
1563 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571564 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201565 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141566 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381567 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591568 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121569 net_error_details_.quic_broken = false;
1570 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481571 provided_token_binding_key_.reset();
1572 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121573}
1574
1575void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1576 if (stream_)
1577 stream_->PopulateNetErrorDetails(&net_error_details_);
1578 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571579}
1580
davidben701ca982015-05-18 21:21:421581void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421582 if (result != OK)
1583 return;
1584
davidbenca9d69162015-05-04 20:18:451585 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091586 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451587}
1588
[email protected]0877e3d2009-10-17 22:29:571589HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501590 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421591}
1592
[email protected]a34f61ee2014-03-18 20:59:491593bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381594 bool connection_is_proven = stream_->IsConnectionReused();
1595 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161596
[email protected]2a5c76b2008-09-25 22:15:161597 // NOTE: we resend a request only if we reused a keep-alive connection.
1598 // This automatically prevents an infinite resend loop because we'll run
1599 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381600 if (connection_is_proven && !has_received_headers)
1601 return true;
1602 return false;
[email protected]1c773ea12009-04-28 19:58:421603}
1604
1605void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381606 if (stream_.get()) {
1607 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121608 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161609 }
1610
[email protected]0877e3d2009-10-17 22:29:571611 // We need to clear request_headers_ because it contains the real request
1612 // headers, but we may need to resend the CONNECT request first to recreate
1613 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201614 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501615 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541616}
1617
[email protected]1c773ea12009-04-28 19:58:421618bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241619 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421620}
license.botbf09a502008-08-24 00:55:551621
[email protected]1c773ea12009-04-28 19:58:421622bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041623 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421624}
1625
[email protected]e772db3f2010-07-12 18:11:131626int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271627 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501628 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421629
[email protected]0877e3d2009-10-17 22:29:571630 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581631 if (status != HTTP_UNAUTHORIZED &&
1632 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421633 return OK;
[email protected]9094b602012-02-27 21:44:581634 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111635 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161636 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1637 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421638
[email protected]9094b602012-02-27 21:44:581639 // This case can trigger when an HTTPS server responds with a "Proxy
1640 // authentication required" status code through a non-authenticating
1641 // proxy.
[email protected]7a67a8152010-11-05 18:31:101642 if (!auth_controllers_[target].get())
1643 return ERR_UNEXPECTED_PROXY_AUTH;
1644
[email protected]a7ea8832010-07-12 17:54:541645 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491646 headers, response_.ssl_info,
1647 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311648 net_log_);
[email protected]228404f2010-06-24 04:31:411649 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491650 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411651
1652 scoped_refptr<AuthChallengeInfo> auth_info =
1653 auth_controllers_[target]->auth_info();
1654 if (auth_info.get())
1655 response_.auth_challenge = auth_info;
1656
[email protected]228404f2010-06-24 04:31:411657 return rv;
[email protected]f9ee6b52008-11-08 06:46:231658}
1659
[email protected]8e6441ca2010-08-19 05:56:381660bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1661 return auth_controllers_[target].get() &&
1662 auth_controllers_[target]->HaveAuth();
1663}
1664
[email protected]228404f2010-06-24 04:31:411665GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1666 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461667 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411668 if (!proxy_info_.proxy_server().is_valid() ||
1669 proxy_info_.proxy_server().is_direct()) {
1670 return GURL(); // There is no proxy server.
1671 }
[email protected]2df19bb2010-08-25 20:13:461672 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1673 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351674 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461675 }
[email protected]228404f2010-06-24 04:31:411676 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291677 if (ForWebSocketHandshake()) {
1678 const GURL& url = request_->url;
1679 url::Replacements<char> ws_to_http;
1680 if (url.SchemeIs("ws")) {
1681 ws_to_http.SetScheme("http", url::Component(0, 4));
1682 } else {
1683 DCHECK(url.SchemeIs("wss"));
1684 ws_to_http.SetScheme("https", url::Component(0, 5));
1685 }
1686 return url.ReplaceComponents(ws_to_http);
1687 }
[email protected]228404f2010-06-24 04:31:411688 return request_->url;
1689 default:
1690 return GURL();
1691 }
[email protected]c3b35c22008-09-27 03:19:421692}
1693
[email protected]831e4a32013-11-14 02:14:441694bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141695 return websocket_handshake_stream_base_create_helper_ &&
1696 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441697}
1698
ttuttle1f2d7e92015-04-28 16:17:471699void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1700 DCHECK(stream_request_);
1701
1702 // Since the transaction can restart with auth credentials, it may create a
1703 // stream more than once. Accumulate all of the connection attempts across
1704 // those streams by appending them to the vector:
1705 for (const auto& attempt : stream_request_->connection_attempts())
1706 connection_attempts_.push_back(attempt);
1707}
1708
[email protected]c3b35c22008-09-27 03:19:421709} // namespace net