blob: 5961f423e766591d02a2c748242bad60d9a8ad64 [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"
[email protected]3d498f72013-10-28 21:17:4024#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5325#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3026#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3527#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5528#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1929#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5230#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2531#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2632#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4434#include "net/base/url_util.h"
eustasc7d27da2017-04-06 10:33:2035#include "net/filter/filter_source_stream.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"
[email protected]65d56aa2010-06-14 04:13:4061#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3262#include "net/spdy/spdy_session.h"
63#include "net/spdy/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]1c773ea12009-04-28 19:58:4273namespace {
74
danakj1fd259a02016-04-16 03:17:0975std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1776 const GURL* url,
77 int net_error,
78 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0979 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2580 dict->SetString("host_and_port", GetHostAndPort(*url));
81 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4882 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2583}
84
[email protected]1c773ea12009-04-28 19:58:4285} // namespace
86
initial.commit586acc5fe2008-07-26 22:42:5287//-----------------------------------------------------------------------------
88
[email protected]262eec82013-03-19 21:01:3689HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
90 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2291 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3292 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
93 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5294 session_(session),
95 request_(NULL),
[email protected]262eec82013-03-19 21:01:3696 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5797 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2098 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5299 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19100 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22101 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54102 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44103 establishing_tunnel_(false),
bnc8016c1f2017-03-31 02:11:29104 enable_ip_based_pooling_(true),
zhongyi48704c182015-12-07 07:52:02105 websocket_handshake_stream_base_create_helper_(NULL),
bnc8016c1f2017-03-31 02:11:29106 net_error_details_() {}
[email protected]3ce7df0f2010-03-03 00:30:50107
[email protected]0b0bf032010-09-21 18:08:50108HttpNetworkTransaction::~HttpNetworkTransaction() {
109 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50110 // TODO(mbelshe): The stream_ should be able to compute whether or not the
111 // stream should be kept alive. No reason to compute here
112 // and pass it in.
mmenkebd84c392015-09-02 14:12:34113 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50114 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34115 } else if (stream_->IsResponseBodyComplete()) {
116 // If the response body is complete, we can just reuse the socket.
117 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50118 } else {
mmenkebd84c392015-09-02 14:12:34119 // Otherwise, we try to drain the response body.
120 HttpStream* stream = stream_.release();
121 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50122 }
123 }
[email protected]02cad5d2013-10-02 08:14:03124 if (request_ && request_->upload_data_stream)
125 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50126}
127
[email protected]684970b2009-08-14 04:54:46128int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41129 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20130 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53131 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04132 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15133 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04134
nharper8cdb0fb2016-04-22 21:34:59135 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
136 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
137
[email protected]99ffa5a2011-10-06 04:20:19138 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14139 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19140 proxy_ssl_config_.rev_checking_enabled = false;
141 }
[email protected]6fbac162011-06-20 00:29:04142
jkarlinfb1d5172015-01-12 14:10:29143 if (request_->load_flags & LOAD_PREFETCH)
144 response_.unused_since_prefetch = true;
145
rdsmith1d343be52016-10-21 20:37:50146 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04147 int rv = DoLoop(OK);
148 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41149 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04150 return rv;
151}
152
153int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41154 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38155 DCHECK(!stream_.get());
156 DCHECK(!stream_request_.get());
157 DCHECK_EQ(STATE_NONE, next_state_);
158
[email protected]82918cc2010-08-25 17:24:50159 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38160
[email protected]ccb40e52008-09-17 20:54:40161 int rv = DoLoop(OK);
162 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41163 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11164 return rv;
[email protected]96d570e42008-08-05 22:43:04165}
166
[email protected]0b45559b2009-06-12 21:45:11167int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54168 X509Certificate* client_cert,
169 SSLPrivateKey* client_private_key,
170 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38171 // In HandleCertificateRequest(), we always tear down existing stream
172 // requests to force a new connection. So we shouldn't have one here.
173 DCHECK(!stream_request_.get());
174 DCHECK(!stream_.get());
175 DCHECK_EQ(STATE_NONE, next_state_);
176
[email protected]102957f2011-09-02 17:10:14177 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
178 &proxy_ssl_config_ : &server_ssl_config_;
179 ssl_config->send_client_cert = true;
180 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54181 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45182 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54183 response_.cert_request_info->host_and_port, client_cert,
184 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11185 // Reset the other member variables.
186 // Note: this is necessary only with SSL renegotiation.
187 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50188 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11189 int rv = DoLoop(OK);
190 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41191 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11192 return rv;
193}
194
[email protected]49639fa2011-12-20 23:22:41195int HttpNetworkTransaction::RestartWithAuth(
196 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22197 HttpAuth::Target target = pending_auth_target_;
198 if (target == HttpAuth::AUTH_NONE) {
199 NOTREACHED();
200 return ERR_UNEXPECTED;
201 }
[email protected]0757e7702009-03-27 04:00:22202 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42203
[email protected]f3cf9802011-10-28 18:44:58204 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13205
[email protected]49639fa2011-12-20 23:22:41206 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38207
208 int rv = OK;
209 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
210 // In this case, we've gathered credentials for use with proxy
211 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50212 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38213 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59214 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54215 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39216 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54217 } else {
[email protected]8e6441ca2010-08-19 05:56:38218 // In this case, we've gathered credentials for the server or the proxy
219 // but it is not during the tunneling phase.
220 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54221 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38222 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54223 }
[email protected]c3b35c22008-09-27 03:19:42224
[email protected]c3b35c22008-09-27 03:19:42225 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41226 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42227 return rv;
[email protected]96d570e42008-08-05 22:43:04228}
229
[email protected]f9ee6b52008-11-08 06:46:23230void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
231 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38232 DCHECK(!stream_request_.get());
233
[email protected]2d2697f92009-02-18 21:00:32234 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57235 // Even if the server says the connection is keep-alive, we have to be
236 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34237 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57238 // If the response body hasn't been completely read, we need to drain
239 // it first.
[email protected]351ab642010-08-05 16:55:31240 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32241 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57242 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32243 read_buf_len_ = kDrainBodyBufferSize;
244 return;
245 }
[email protected]0877e3d2009-10-17 22:29:57246 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09247 }
248
[email protected]2d2697f92009-02-18 21:00:32249 // We don't need to drain the response body, so we act as if we had drained
250 // the response body.
251 DidDrainBodyForAuthRestart(keep_alive);
252}
253
254void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38255 DCHECK(!stream_request_.get());
256
257 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19258 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22259 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58260 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34261 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38262 // We should call connection_->set_idle_time(), but this doesn't occur
263 // often enough to be worth the trouble.
264 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39265 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38266 }
[email protected]697ef4c2010-10-14 16:38:58267
268 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46269 // Close the stream and mark it as not_reusable. Even in the
270 // keep_alive case, we've determined that the stream_ is not
271 // reusable if new_stream is NULL.
272 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58273 next_state_ = STATE_CREATE_STREAM;
274 } else {
sclittlefb249892015-09-10 21:33:22275 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19276 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22277 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58278 next_state_ = STATE_INIT_STREAM;
279 }
280 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32281 }
[email protected]f9ee6b52008-11-08 06:46:23282
283 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58284 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23285}
286
[email protected]8e6441ca2010-08-19 05:56:38287bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
288 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
289 HaveAuth(pending_auth_target_);
290}
291
[email protected]9dea9e1f2009-01-29 00:30:47292int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41293 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04294 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35295 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04296
[email protected]ad8e04a2010-11-01 04:16:27297 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38298 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04299 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29300 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04301 // bytes when establishing a tunnel because they might be controlled by
302 // an active network attacker. We don't worry about this for HTTP
303 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29304 // We reach this case when the user cancels a 407 proxy auth prompt. We
305 // also don't worry about this for an HTTPS Proxy, because the
306 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04307 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46308 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58309 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54310 LOG(WARNING) << "Blocked proxy response with status "
311 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15312 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04313 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44314 }
315
[email protected]e60e47a2010-07-14 03:37:18316 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15317 next_state_ = STATE_READ_BODY;
318
319 // We have reached the end of Start state machine, reset the requestinfo to
320 // null.
321 // RequestInfo is a member of the HttpTransaction's consumer and is useful
322 // only till final response headers are received. A reset will ensure that
323 // HttpRequestInfo is only used up until final response headers are received.
324 // Resetting is allowed so that the transaction can be disassociated from its
325 // creating consumer in cases where it is shared for writing to the cache.
326 // It is also safe to reset it to null at this point since upload_data_stream
327 // is also not used in the Read state machine.
328 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18329
[email protected]96d570e42008-08-05 22:43:04330 read_buf_ = buf;
331 read_buf_len_ = buf_len;
332
[email protected]96d570e42008-08-05 22:43:04333 int rv = DoLoop(OK);
334 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41335 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04336 return rv;
337}
338
[email protected]8cd06c02014-01-25 07:50:14339void HttpNetworkTransaction::StopCaching() {}
340
[email protected]79e1fd62013-06-20 06:50:04341bool HttpNetworkTransaction::GetFullRequestHeaders(
342 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32343 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04344 *headers = request_headers_;
345 return true;
346}
347
sclittle4de1bab92015-09-22 21:28:24348int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
349 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19350 if (stream_)
351 total_received_bytes += stream_->GetTotalReceivedBytes();
352 return total_received_bytes;
353}
354
sclittlefb249892015-09-10 21:33:22355int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
356 int64_t total_sent_bytes = total_sent_bytes_;
357 if (stream_)
358 total_sent_bytes += stream_->GetTotalSentBytes();
359 return total_sent_bytes;
360}
361
[email protected]8cd06c02014-01-25 07:50:14362void HttpNetworkTransaction::DoneReading() {}
363
[email protected]96d570e42008-08-05 22:43:04364const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55365 return &response_;
[email protected]96d570e42008-08-05 22:43:04366}
367
368LoadState HttpNetworkTransaction::GetLoadState() const {
369 // TODO(wtc): Define a new LoadState value for the
370 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
371 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50372 case STATE_THROTTLE_COMPLETE:
373 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48374 case STATE_CREATE_STREAM:
375 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50376 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38377 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15378 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
379 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57380 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04381 return LOAD_STATE_SENDING_REQUEST;
382 case STATE_READ_HEADERS_COMPLETE:
383 return LOAD_STATE_WAITING_FOR_RESPONSE;
384 case STATE_READ_BODY_COMPLETE:
385 return LOAD_STATE_READING_RESPONSE;
386 default:
387 return LOAD_STATE_IDLE;
388 }
389}
390
[email protected]8cd06c02014-01-25 07:50:14391void HttpNetworkTransaction::SetQuicServerInfo(
392 QuicServerInfo* quic_server_info) {}
393
[email protected]5033ab82013-03-22 20:17:46394bool HttpNetworkTransaction::GetLoadTimingInfo(
395 LoadTimingInfo* load_timing_info) const {
396 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
397 return false;
398
399 load_timing_info->proxy_resolve_start =
400 proxy_info_.proxy_resolve_start_time();
401 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
402 load_timing_info->send_start = send_start_time_;
403 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46404 return true;
405}
406
ttuttled9dbc652015-09-29 20:00:59407bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36408 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59409 return false;
410
411 *endpoint = remote_endpoint_;
412 return true;
413}
414
zhongyi48704c182015-12-07 07:52:02415void HttpNetworkTransaction::PopulateNetErrorDetails(
416 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12417 *details = net_error_details_;
418 if (stream_)
419 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02420}
421
[email protected]5033ab82013-03-22 20:17:46422void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
423 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50424
rdsmith29dbad12017-02-17 02:22:18425 // TODO(rdsmith): Note that if any code indirectly executed by
426 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
427 // ever implements a throttling mechanism where changing a request's
428 // priority may cause a this or another request to synchronously succeed
429 // or fail, that callback could synchronously delete |*this|, causing
430 // a crash on return to this code.
431 //
432 // |throttle_->SetPriority()| has exactly the above attributes, which
433 // is why it's the last call in this function.
434
[email protected]bf828982013-08-14 18:01:47435 if (stream_request_)
436 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03437 if (stream_)
438 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50439
440 if (throttle_)
441 throttle_->SetPriority(priority);
442 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46443}
444
[email protected]831e4a32013-11-14 02:14:44445void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
446 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
447 websocket_handshake_stream_base_create_helper_ = create_helper;
448}
449
[email protected]1826a402014-01-08 15:40:48450void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
451 const BeforeNetworkStartCallback& callback) {
452 before_network_start_callback_ = callback;
453}
454
ryansturm49a8cb12016-06-15 16:51:09455void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
456 const BeforeHeadersSentCallback& callback) {
457 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27458}
459
[email protected]1826a402014-01-08 15:40:48460int HttpNetworkTransaction::ResumeNetworkStart() {
461 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
462 return DoLoop(OK);
463}
464
[email protected]102e27c2011-02-23 01:01:31465void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
466 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39467 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50468 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38469 DCHECK(stream_request_.get());
470
sclittlefb249892015-09-10 21:33:22471 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19472 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22473 total_sent_bytes_ += stream_->GetTotalSentBytes();
474 }
[email protected]8e6441ca2010-08-19 05:56:38475 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14476 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31477 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52478 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21479 response_.alpn_negotiated_protocol =
480 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38481 response_.was_fetched_via_spdy = stream_request_->using_spdy();
482 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15483 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47484 response_.proxy_server = proxy_info_.proxy_server();
485 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
486 response_.proxy_server = ProxyServer::Direct();
487 else
488 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38489 OnIOComplete(OK);
490}
491
xunjieli5749218c2016-03-22 16:43:06492void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08493 const SSLConfig& used_ssl_config,
494 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06495 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08496 NOTREACHED();
497}
498
[email protected]a9cf2b92013-10-30 12:08:49499void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50500 const SSLConfig& used_ssl_config,
501 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49502 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44503 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50504}
505
[email protected]102e27c2011-02-23 01:01:31506void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13507 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50508 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38509 DCHECK_NE(OK, result);
510 DCHECK(stream_request_.get());
511 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14512 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38513
514 OnIOComplete(result);
515}
516
[email protected]102e27c2011-02-23 01:01:31517void HttpNetworkTransaction::OnCertificateError(
518 int result,
519 const SSLConfig& used_ssl_config,
520 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50521 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38522 DCHECK_NE(OK, result);
523 DCHECK(stream_request_.get());
524 DCHECK(!stream_.get());
525
526 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14527 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38528
529 // TODO(mbelshe): For now, we're going to pass the error through, and that
530 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50531 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
532 // good and the user chooses to ignore the error. This is not ideal, but not
533 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38534
535 OnIOComplete(result);
536}
537
538void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50539 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31540 const SSLConfig& used_ssl_config,
541 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50542 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38543 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50544 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38545
546 establishing_tunnel_ = true;
547 response_.headers = proxy_response.headers;
548 response_.auth_challenge = proxy_response.auth_challenge;
eustasc7d27da2017-04-06 10:33:20549
550 if (response_.headers.get() && !ContentEncodingsValid()) {
551 DoCallback(ERR_CONTENT_DECODING_FAILED);
552 return;
553 }
554
[email protected]8e6441ca2010-08-19 05:56:38555 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14556 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31557 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38558
559 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
560 pending_auth_target_ = HttpAuth::AUTH_PROXY;
561
562 DoCallback(OK);
563}
564
565void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31566 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50567 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50568 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38569
[email protected]102957f2011-09-02 17:10:14570 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38571 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58572 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38573}
574
[email protected]511f6f52010-12-17 03:58:29575void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
576 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31577 const SSLConfig& used_ssl_config,
578 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39579 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29580 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
581
ttuttle1f2d7e92015-04-28 16:17:47582 CopyConnectionAttemptsFromStreamRequest();
583
[email protected]511f6f52010-12-17 03:58:29584 headers_valid_ = true;
585 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14586 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31587 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22588 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19589 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22590 total_sent_bytes_ += stream_->GetTotalSentBytes();
591 }
[email protected]511f6f52010-12-17 03:58:29592 stream_.reset(stream);
593 stream_request_.reset(); // we're done with the stream request
594 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
595}
596
zhongyi48704c182015-12-07 07:52:02597void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12598 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02599}
600
ttuttle1f2d7e92015-04-28 16:17:47601void HttpNetworkTransaction::GetConnectionAttempts(
602 ConnectionAttempts* out) const {
603 *out = connection_attempts_;
604}
605
rdsmithbf8c3c12016-11-18 18:16:24606void HttpNetworkTransaction::OnThrottleUnblocked(
607 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50608 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24609 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50610 // be so in the future.
611 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
612
613 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
614
615 DoLoop(OK);
616}
617
Adam Ricecb76ac62015-02-20 05:33:25618bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37619 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52620}
621
nharperb7441ef2016-01-25 23:54:14622bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
623 if (!IsSecureRequest())
624 return false;
625 SSLInfo ssl_info;
626 stream_->GetSSLInfo(&ssl_info);
627 return ssl_info.token_binding_negotiated &&
628 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
629 session_->params().channel_id_service;
630}
631
632void HttpNetworkTransaction::RecordTokenBindingSupport() const {
633 // This enum is used for an UMA histogram - do not change or re-use values.
634 enum {
635 DISABLED = 0,
636 CLIENT_ONLY = 1,
637 CLIENT_AND_SERVER = 2,
638 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
639 TOKEN_BINDING_SUPPORT_MAX
640 } supported;
641 if (!IsSecureRequest())
642 return;
643 SSLInfo ssl_info;
644 stream_->GetSSLInfo(&ssl_info);
645 if (!session_->params().enable_token_binding) {
646 supported = DISABLED;
647 } else if (!session_->params().channel_id_service) {
648 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
649 } else if (ssl_info.token_binding_negotiated) {
650 supported = CLIENT_AND_SERVER;
651 } else {
652 supported = CLIENT_ONLY;
653 }
654 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
655 TOKEN_BINDING_SUPPORT_MAX);
656}
657
Adam Rice425cf122015-01-19 06:18:24658bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12659 return (proxy_info_.is_http() || proxy_info_.is_https() ||
660 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24661 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
662}
663
initial.commit586acc5fe2008-07-26 22:42:52664void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50665 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41666 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52667
[email protected]96d570e42008-08-05 22:43:04668 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41669 CompletionCallback c = callback_;
670 callback_.Reset();
671 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52672}
673
674void HttpNetworkTransaction::OnIOComplete(int result) {
675 int rv = DoLoop(result);
676 if (rv != ERR_IO_PENDING)
677 DoCallback(rv);
678}
679
680int HttpNetworkTransaction::DoLoop(int result) {
681 DCHECK(next_state_ != STATE_NONE);
682
683 int rv = result;
684 do {
685 State state = next_state_;
686 next_state_ = STATE_NONE;
687 switch (state) {
rdsmith1d343be52016-10-21 20:37:50688 case STATE_THROTTLE:
689 DCHECK_EQ(OK, rv);
690 rv = DoThrottle();
691 break;
692 case STATE_THROTTLE_COMPLETE:
693 DCHECK_EQ(OK, rv);
694 rv = DoThrottleComplete();
695 break;
[email protected]1826a402014-01-08 15:40:48696 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
697 DCHECK_EQ(OK, rv);
698 rv = DoNotifyBeforeCreateStream();
699 break;
[email protected]82918cc2010-08-25 17:24:50700 case STATE_CREATE_STREAM:
701 DCHECK_EQ(OK, rv);
702 rv = DoCreateStream();
703 break;
704 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46705 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
706 // solved.
707 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50708 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46709 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50710 break;
[email protected]351ab642010-08-05 16:55:31711 case STATE_INIT_STREAM:
712 DCHECK_EQ(OK, rv);
713 rv = DoInitStream();
714 break;
715 case STATE_INIT_STREAM_COMPLETE:
716 rv = DoInitStreamComplete(rv);
717 break;
[email protected]044de0642010-06-17 10:42:15718 case STATE_GENERATE_PROXY_AUTH_TOKEN:
719 DCHECK_EQ(OK, rv);
720 rv = DoGenerateProxyAuthToken();
721 break;
722 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
723 rv = DoGenerateProxyAuthTokenComplete(rv);
724 break;
725 case STATE_GENERATE_SERVER_AUTH_TOKEN:
726 DCHECK_EQ(OK, rv);
727 rv = DoGenerateServerAuthToken();
728 break;
729 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
730 rv = DoGenerateServerAuthTokenComplete(rv);
731 break;
nharperd6e65822016-03-30 23:05:48732 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14733 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48734 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14735 break;
nharperd6e65822016-03-30 23:05:48736 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
737 rv = DoGetProvidedTokenBindingKeyComplete(rv);
738 break;
739 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
740 DCHECK_EQ(OK, rv);
741 rv = DoGetReferredTokenBindingKey();
742 break;
743 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
744 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14745 break;
[email protected]daddea62012-09-19 05:51:13746 case STATE_INIT_REQUEST_BODY:
747 DCHECK_EQ(OK, rv);
748 rv = DoInitRequestBody();
749 break;
750 case STATE_INIT_REQUEST_BODY_COMPLETE:
751 rv = DoInitRequestBodyComplete(rv);
752 break;
[email protected]4875ba12011-03-30 22:31:51753 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55754 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00755 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51756 rv = DoBuildRequest();
757 break;
758 case STATE_BUILD_REQUEST_COMPLETE:
759 rv = DoBuildRequestComplete(rv);
760 break;
761 case STATE_SEND_REQUEST:
762 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57763 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52764 break;
[email protected]0877e3d2009-10-17 22:29:57765 case STATE_SEND_REQUEST_COMPLETE:
766 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43767 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00768 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52769 break;
770 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55771 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00772 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52773 rv = DoReadHeaders();
774 break;
775 case STATE_READ_HEADERS_COMPLETE:
776 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43777 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00778 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52779 break;
780 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55781 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00782 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52783 rv = DoReadBody();
784 break;
785 case STATE_READ_BODY_COMPLETE:
786 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52789 break;
[email protected]2d2697f92009-02-18 21:00:32790 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55791 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53792 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00793 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32794 rv = DoDrainBodyForAuthRestart();
795 break;
796 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
797 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43798 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00799 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32800 break;
initial.commit586acc5fe2008-07-26 22:42:52801 default:
802 NOTREACHED() << "bad state";
803 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04804 break;
initial.commit586acc5fe2008-07-26 22:42:52805 }
806 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
807
808 return rv;
809}
810
rdsmith1d343be52016-10-21 20:37:50811int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24812 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50813 throttle_ = session_->throttler()->CreateThrottle(
814 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
815 next_state_ = STATE_THROTTLE_COMPLETE;
816
rdsmithbf8c3c12016-11-18 18:16:24817 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50818 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
819 return ERR_IO_PENDING;
820 }
821
822 return OK;
823}
824
825int HttpNetworkTransaction::DoThrottleComplete() {
826 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24827 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50828
829 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
830
831 return OK;
832}
833
[email protected]1826a402014-01-08 15:40:48834int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
835 next_state_ = STATE_CREATE_STREAM;
836 bool defer = false;
837 if (!before_network_start_callback_.is_null())
838 before_network_start_callback_.Run(&defer);
839 if (!defer)
840 return OK;
841 return ERR_IO_PENDING;
842}
843
[email protected]82918cc2010-08-25 17:24:50844int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34845 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26846 tracked_objects::ScopedTracker tracking_profile(
847 FROM_HERE_WITH_EXPLICIT_FUNCTION(
848 "424359 HttpNetworkTransaction::DoCreateStream"));
849
ttuttlec0c828492015-05-15 01:25:55850 response_.network_accessed = true;
851
[email protected]82918cc2010-08-25 17:24:50852 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44853 if (ForWebSocketHandshake()) {
854 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23855 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44856 ->RequestWebSocketHandshakeStream(
bnc8016c1f2017-03-31 02:11:29857 *request_, priority_, server_ssl_config_, proxy_ssl_config_,
858 this, websocket_handshake_stream_base_create_helper_,
859 enable_ip_based_pooling_, net_log_));
[email protected]831e4a32013-11-14 02:14:44860 } else {
bnc8016c1f2017-03-31 02:11:29861 stream_request_.reset(session_->http_stream_factory()->RequestStream(
862 *request_, priority_, server_ssl_config_, proxy_ssl_config_, this,
863 enable_ip_based_pooling_, net_log_));
[email protected]831e4a32013-11-14 02:14:44864 }
[email protected]26816882010-10-14 18:03:09865 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38866 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31867}
868
[email protected]82918cc2010-08-25 17:24:50869int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47870 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
871 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
872 // which resets the stream request first. Therefore, we have to grab the
873 // connection attempts in *that* function instead of here in that case.
874 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
875 CopyConnectionAttemptsFromStreamRequest();
876
davidben701ca982015-05-18 21:21:42877 if (request_->url.SchemeIsCryptographic())
878 RecordSSLFallbackMetrics(result);
879
[email protected]394816e92010-08-03 07:38:59880 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50881 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38882 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33883 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
884 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29885 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
886 // Return OK and let the caller read the proxy's error page
887 next_state_ = STATE_NONE;
888 return OK;
bncfacdd852015-01-09 19:22:54889 } else if (result == ERR_HTTP_1_1_REQUIRED ||
890 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
891 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59892 }
893
[email protected]bd0b6772011-01-11 19:59:30894 // Handle possible handshake errors that may have occurred if the stream
895 // used SSL for one or more of the layers.
896 result = HandleSSLHandshakeError(result);
897
[email protected]8e6441ca2010-08-19 05:56:38898 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09899 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38900 return result;
[email protected]394816e92010-08-03 07:38:59901}
902
[email protected]82918cc2010-08-25 17:24:50903int HttpNetworkTransaction::DoInitStream() {
904 DCHECK(stream_.get());
905 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59906
907 stream_->GetRemoteEndpoint(&remote_endpoint_);
908
[email protected]262eec82013-03-19 21:01:36909 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50910}
911
912int HttpNetworkTransaction::DoInitStreamComplete(int result) {
913 if (result == OK) {
914 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50915 } else {
[email protected]82918cc2010-08-25 17:24:50916 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26917 result = HandleIOError(result);
918
919 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22920 if (stream_) {
921 total_received_bytes_ += stream_->GetTotalReceivedBytes();
922 total_sent_bytes_ += stream_->GetTotalSentBytes();
923 }
zhongyica364fbb2015-12-12 03:39:12924 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50925 }
926
927 return result;
928}
929
[email protected]044de0642010-06-17 10:42:15930int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
931 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
932 if (!ShouldApplyProxyAuth())
933 return OK;
[email protected]394816e92010-08-03 07:38:59934 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
935 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09936 auth_controllers_[target] =
937 new HttpAuthController(target,
938 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31939 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09940 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59941 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41942 io_callback_,
[email protected]394816e92010-08-03 07:38:59943 net_log_);
[email protected]044de0642010-06-17 10:42:15944}
945
946int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
947 DCHECK_NE(ERR_IO_PENDING, rv);
948 if (rv == OK)
949 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
950 return rv;
951}
952
953int HttpNetworkTransaction::DoGenerateServerAuthToken() {
954 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59955 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54956 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09957 auth_controllers_[target] =
958 new HttpAuthController(target,
959 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31960 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09961 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54962 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
963 auth_controllers_[target]->DisableEmbeddedIdentity();
964 }
[email protected]044de0642010-06-17 10:42:15965 if (!ShouldApplyServerAuth())
966 return OK;
[email protected]394816e92010-08-03 07:38:59967 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41968 io_callback_,
[email protected]394816e92010-08-03 07:38:59969 net_log_);
[email protected]044de0642010-06-17 10:42:15970}
971
972int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
973 DCHECK_NE(ERR_IO_PENDING, rv);
974 if (rv == OK)
nharperd6e65822016-03-30 23:05:48975 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15976 return rv;
977}
978
nharperd6e65822016-03-30 23:05:48979int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
980 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14981 if (!IsTokenBindingEnabled())
982 return OK;
983
mikecirone8b85c432016-09-08 19:11:00984 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperb7441ef2016-01-25 23:54:14985 ChannelIDService* channel_id_service = session_->params().channel_id_service;
986 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48987 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14988 &token_binding_request_);
989}
990
nharperd6e65822016-03-30 23:05:48991int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14992 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48993 if (IsTokenBindingEnabled()) {
994 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00995 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48996 }
997
998 if (rv == OK)
999 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
1000 return rv;
1001}
1002
1003int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1004 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1005 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141006 return OK;
1007
mikecirone8b85c432016-09-08 19:11:001008 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperd6e65822016-03-30 23:05:481009 ChannelIDService* channel_id_service = session_->params().channel_id_service;
1010 return channel_id_service->GetOrCreateChannelID(
1011 request_->token_binding_referrer, &referred_token_binding_key_,
1012 io_callback_, &token_binding_request_);
1013}
1014
1015int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1016 DCHECK_NE(ERR_IO_PENDING, rv);
1017 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1018 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001019 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481020 }
1021 if (rv == OK)
1022 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141023 return rv;
1024}
1025
1026int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241027 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141028 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1029 GetHostAndOptionalPort(request_->url));
1030
1031 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241032 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141033 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1034 "keep-alive");
1035 } else {
1036 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1037 }
1038
[email protected]2979a492011-04-06 00:29:141039 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111040 if (request_->upload_data_stream) {
1041 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141042 request_headers_.SetHeader(
1043 HttpRequestHeaders::kTransferEncoding, "chunked");
1044 } else {
1045 request_headers_.SetHeader(
1046 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111047 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141048 }
csharrisonf473dd192015-08-18 13:54:131049 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141050 // An empty POST/PUT request still needs a content length. As for HEAD,
1051 // IE and Safari also add a content length header. Presumably it is to
1052 // support sending a HEAD request to an URL that only expects to be sent a
1053 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131054 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1055 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141056 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1057 }
1058
nharperb7441ef2016-01-25 23:54:141059 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481060 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141061 std::string token_binding_header;
1062 int rv = BuildTokenBindingHeader(&token_binding_header);
1063 if (rv != OK)
1064 return rv;
1065 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1066 token_binding_header);
1067 }
1068
[email protected]2979a492011-04-06 00:29:141069 // Honor load flags that impact proxy caches.
1070 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1071 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1072 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1073 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1074 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1075 }
1076
1077 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1078 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1079 &request_headers_);
1080 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1081 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1082 &request_headers_);
1083
[email protected]c10450102011-06-27 09:06:161084 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201085
ryansturm49a8cb12016-06-15 16:51:091086 if (!before_headers_sent_callback_.is_null())
1087 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201088
[email protected]173f8e22013-04-10 04:18:201089 response_.did_use_http_auth =
1090 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1091 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141092 return OK;
1093}
1094
1095int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471096 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141097 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351098 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1099 TokenBindingType::PROVIDED,
1100 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141101 if (rv != OK)
1102 return rv;
1103 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481104 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1105 provided_token_binding_key_.get(), signed_ekm,
1106 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141107 if (rv != OK)
1108 return rv;
nharperd6e65822016-03-30 23:05:481109
nharperb7441ef2016-01-25 23:54:141110 std::vector<base::StringPiece> token_bindings;
1111 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481112
1113 std::string referred_token_binding;
1114 if (referred_token_binding_key_) {
1115 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351116 int rv = stream_->GetTokenBindingSignature(
1117 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1118 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481119 if (rv != OK)
1120 return rv;
1121 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1122 referred_token_binding_key_.get(),
1123 referred_signed_ekm, &referred_token_binding);
1124 if (rv != OK)
1125 return rv;
1126 token_bindings.push_back(referred_token_binding);
1127 }
nharperb7441ef2016-01-25 23:54:141128 std::string header;
1129 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1130 if (rv != OK)
1131 return rv;
nharper78e6d2b2016-09-21 05:42:351132 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471133 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1134 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1135 header_creation_time,
1136 base::TimeDelta::FromMilliseconds(1),
1137 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141138 return OK;
[email protected]2979a492011-04-06 00:29:141139}
1140
[email protected]daddea62012-09-19 05:51:131141int HttpNetworkTransaction::DoInitRequestBody() {
1142 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131143 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111144 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521145 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131146 return rv;
1147}
[email protected]4875ba12011-03-30 22:31:511148
[email protected]daddea62012-09-19 05:51:131149int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1150 if (result == OK)
1151 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131152 return result;
1153}
1154
1155int HttpNetworkTransaction::DoBuildRequest() {
1156 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511157 headers_valid_ = false;
1158
1159 // This is constructed lazily (instead of within our Start method), so that
1160 // we have proxy info available.
1161 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241162 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141163 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511164 }
1165
[email protected]4875ba12011-03-30 22:31:511166 return OK;
1167}
1168
1169int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241170 if (result == OK)
1171 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511172 return result;
1173}
1174
[email protected]0877e3d2009-10-17 22:29:571175int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341176 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261177 tracked_objects::ScopedTracker tracking_profile(
1178 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1179 "424359 HttpNetworkTransaction::DoSendRequest"));
1180
[email protected]58e32bb2013-01-21 18:23:251181 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571182 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1183
[email protected]bf3eb002012-11-15 05:50:111184 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521185}
1186
[email protected]0877e3d2009-10-17 22:29:571187int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251188 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361189
1190 if (result == ERR_HTTP_1_1_REQUIRED ||
1191 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1192 return HandleHttp11Required(result);
1193 }
1194
initial.commit586acc5fe2008-07-26 22:42:521195 if (result < 0)
1196 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571197 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521198 return OK;
1199}
1200
1201int HttpNetworkTransaction::DoReadHeaders() {
1202 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411203 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521204}
1205
1206int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111207 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1208 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381209 if (IsCertificateError(result)) {
1210 // We don't handle a certificate error during SSL renegotiation, so we
1211 // have to return an error that's not in the certificate error range
1212 // (-2xx).
1213 LOG(ERROR) << "Got a server certificate with error " << result
1214 << " during SSL renegotiation";
1215 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1216 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1217 // TODO(wtc): Need a test case for this code path!
1218 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251219 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381220 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501221 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381222 result = HandleCertificateRequest(result);
1223 if (result == OK)
1224 return result;
[email protected]2181ea002009-06-09 01:37:271225 }
1226
bncfacdd852015-01-09 19:22:541227 if (result == ERR_HTTP_1_1_REQUIRED ||
1228 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1229 return HandleHttp11Required(result);
1230 }
1231
[email protected]c871864d72014-03-13 19:56:191232 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1233 // response headers were received, we do the best we can to make sense of it
1234 // and send it back up the stack.
1235 //
1236 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1237 // bizarre for SPDY. Assuming this logic is useful at all.
1238 // TODO(davidben): Bubble the error code up so we do not cache?
1239 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1240 result = OK;
1241
1242 if (result < 0)
1243 return HandleIOError(result);
1244
[email protected]90499482013-06-01 00:39:501245 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521246
eustasc7d27da2017-04-06 10:33:201247 if (response_.headers.get() && !ContentEncodingsValid())
1248 return ERR_CONTENT_DECODING_FAILED;
1249
[email protected]d58ceea82014-06-04 10:55:541250 // On a 408 response from the server ("Request Timeout") on a stale socket,
1251 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041252 // Headers can be NULL because of http://crbug.com/384554.
1253 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541254 stream_->IsConnectionReused()) {
1255 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001256 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541257 response_.headers->response_code());
1258 // This will close the socket - it would be weird to try and reuse it, even
1259 // if the server doesn't actually close it.
1260 ResetConnectionAndRequestForResend();
1261 return OK;
1262 }
1263
[email protected]93f8b562012-03-27 01:00:161264 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211265 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161266 const int response_code = response_.headers->response_code();
1267 UMA_HISTOGRAM_ENUMERATION(
1268 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1269 }
1270
[email protected]3abacd62012-06-10 20:20:321271 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001272 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321273 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391274
bncbe0f6af2015-10-15 17:49:561275 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571276 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1277 // indicates a buggy server. See:
1278 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1279 if (request_->method == "PUT")
1280 return ERR_METHOD_NOT_SUPPORTED;
1281 }
[email protected]4ddaf2502008-10-23 18:26:191282
[email protected]0877e3d2009-10-17 22:29:571283 // Check for an intermediate 100 Continue response. An origin server is
1284 // allowed to send this response even if we didn't ask for it, so we just
1285 // need to skip over it.
1286 // We treat any other 1xx in this same way (although in practice getting
1287 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441288 // Unless this is a WebSocket request, in which case we pass it on up.
1289 if (response_.headers->response_code() / 100 == 1 &&
1290 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451291 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571292 next_state_ = STATE_READ_HEADERS;
1293 return OK;
1294 }
1295
bnc8016c1f2017-03-31 02:11:291296 if (response_.headers->response_code() == 421) {
1297 return HandleIOError(ERR_MISDIRECTED_REQUEST);
1298 }
1299
bncb26024382016-06-29 02:39:451300 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111301 session_->http_stream_factory()->ProcessAlternativeServices(
1302 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1303 }
[email protected]564b4912010-03-09 16:30:421304
asanka5ffd5d72016-03-23 16:20:491305 if (IsSecureRequest())
1306 stream_->GetSSLInfo(&response_.ssl_info);
1307
[email protected]e772db3f2010-07-12 18:11:131308 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571309 if (rv != OK)
1310 return rv;
1311
[email protected]0877e3d2009-10-17 22:29:571312 headers_valid_ = true;
1313 return OK;
initial.commit586acc5fe2008-07-26 22:42:521314}
1315
1316int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501317 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131318 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381319 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521320
1321 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501322 return stream_->ReadResponseBody(
1323 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521324}
1325
1326int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1327 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381328 bool done = false;
1329 if (result <= 0) {
1330 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521331 done = true;
[email protected]8e6441ca2010-08-19 05:56:381332 }
[email protected]9492e4a2010-02-24 00:58:461333
mmenkebd84c392015-09-02 14:12:341334 // Clean up connection if we are done.
1335 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381336 // Note: Just because IsResponseBodyComplete is true, we're not
1337 // necessarily "done". We're only "done" when it is the last
1338 // read on this HttpNetworkTransaction, which will be signified
1339 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341340 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381341 // the stream. No need to compute it here just to pass back
1342 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341343 bool keep_alive =
1344 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521345
[email protected]8e6441ca2010-08-19 05:56:381346 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011347 // Note: we don't reset the stream here. We've closed it, but we still
1348 // need it around so that callers can call methods such as
1349 // GetUploadProgress() and have them be meaningful.
1350 // TODO(mbelshe): This means we closed the stream here, and we close it
1351 // again in ~HttpNetworkTransaction. Clean that up.
1352
[email protected]8e6441ca2010-08-19 05:56:381353 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521354 }
1355
1356 // Clear these to avoid leaving around old state.
1357 read_buf_ = NULL;
1358 read_buf_len_ = 0;
1359
1360 return result;
1361}
1362
[email protected]2d2697f92009-02-18 21:00:321363int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1364 // This method differs from DoReadBody only in the next_state_. So we just
1365 // call DoReadBody and override the next_state_. Perhaps there is a more
1366 // elegant way for these two methods to share code.
1367 int rv = DoReadBody();
1368 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1369 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1370 return rv;
1371}
1372
[email protected]0877e3d2009-10-17 22:29:571373// TODO(wtc): This method and the DoReadBodyComplete method are almost
1374// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321375int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231376 // keep_alive defaults to true because the very reason we're draining the
1377 // response body is to reuse the connection for auth restart.
1378 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321379 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571380 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321381 done = true;
[email protected]68873ba2009-06-04 21:49:231382 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311383 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571384 done = true;
[email protected]2d2697f92009-02-18 21:00:321385 }
1386
1387 if (done) {
1388 DidDrainBodyForAuthRestart(keep_alive);
1389 } else {
1390 // Keep draining.
1391 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1392 }
1393
1394 return OK;
1395}
1396
[email protected]5e363962009-06-19 19:57:011397int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381398 // There are two paths through which the server can request a certificate
1399 // from us. The first is during the initial handshake, the second is
1400 // during SSL renegotiation.
1401 //
1402 // In both cases, we want to close the connection before proceeding.
1403 // We do this for two reasons:
1404 // First, we don't want to keep the connection to the server hung for a
1405 // long time while the user selects a certificate.
1406 // Second, even if we did keep the connection open, NSS has a bug where
1407 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581408 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381409
1410 if (stream_.get()) {
1411 // Since we already have a stream, we're being called as part of SSL
1412 // renegotiation.
1413 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191414 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221415 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381416 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121417 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381418 }
1419
[email protected]26816882010-10-14 18:03:091420 // The server is asking for a client certificate during the initial
1421 // handshake.
1422 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011423
[email protected]ec229bc92010-11-22 09:51:451424 // If the user selected one of the certificates in client_certs or declined
1425 // to provide one for this server before, use the past decision
1426 // automatically.
1427 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541428 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451429 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541430 response_.cert_request_info->host_and_port, &client_cert,
1431 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451432 if (!found_cached_cert)
1433 return error;
1434
1435 // Check that the certificate selected is still a certificate the server
1436 // is likely to accept, based on the criteria supplied in the
1437 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501438 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261439 const std::vector<std::string>& cert_authorities =
1440 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451441
[email protected]f1958c382013-02-07 00:15:261442 bool cert_still_valid = cert_authorities.empty() ||
1443 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451444 if (!cert_still_valid)
1445 return error;
[email protected]5e363962009-06-19 19:57:011446 }
[email protected]ec229bc92010-11-22 09:51:451447
1448 // TODO(davidben): Add a unit test which covers this path; we need to be
1449 // able to send a legitimate certificate and also bypass/clear the
1450 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141451 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1452 &proxy_ssl_config_ : &server_ssl_config_;
1453 ssl_config->send_client_cert = true;
1454 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541455 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451456 next_state_ = STATE_CREATE_STREAM;
1457 // Reset the other member variables.
1458 // Note: this is necessary only with SSL renegotiation.
1459 ResetStateForRestart();
1460 return OK;
[email protected]0b45559b2009-06-12 21:45:111461}
1462
bncfacdd852015-01-09 19:22:541463int HttpNetworkTransaction::HandleHttp11Required(int error) {
1464 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1465 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1466
1467 if (error == ERR_HTTP_1_1_REQUIRED) {
1468 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1469 } else {
1470 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1471 }
1472 ResetConnectionAndRequestForResend();
1473 return OK;
1474}
1475
[email protected]384cc73a2013-12-08 22:41:031476void HttpNetworkTransaction::HandleClientAuthError(int error) {
1477 if (server_ssl_config_.send_client_cert &&
1478 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1479 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411480 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031481 }
1482}
1483
[email protected]bd0b6772011-01-11 19:59:301484// TODO(rch): This does not correctly handle errors when an SSL proxy is
1485// being used, as all of the errors are handled as if they were generated
1486// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381487// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301488int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1489 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031490 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301491
davidbena4c9d062015-04-03 22:34:251492 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1493 // reflect servers require a deprecated cipher rather than merely prefer
1494 // it. This, however, has no security benefit until the ciphers are actually
1495 // removed.
davidben14b1a532015-10-30 16:01:091496 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251497 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1498 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1499 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001500 NetLogEventType::SSL_CIPHER_FALLBACK,
davidbena4c9d062015-04-03 22:34:251501 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091502 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251503 ResetConnectionAndRequestForResend();
1504 return OK;
1505 }
1506
initial.commit586acc5fe2008-07-26 22:42:521507 return error;
1508}
1509
[email protected]bd0b6772011-01-11 19:59:301510// This method determines whether it is safe to resend the request after an
1511// IO error. It can only be called in response to request header or body
1512// write errors or response header read errors. It should not be used in
1513// other cases, such as a Connect error.
1514int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031515 // Because the peer may request renegotiation with client authentication at
1516 // any time, check and handle client authentication errors.
1517 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301518
1519 switch (error) {
1520 // If we try to reuse a connection that the server is in the process of
1521 // closing, we may end up successfully writing out our request (or a
1522 // portion of our request) only to find a connection error when we try to
1523 // read from (or finish writing to) the socket.
1524 case ERR_CONNECTION_RESET:
1525 case ERR_CONNECTION_CLOSED:
1526 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511527 // There can be a race between the socket pool checking checking whether a
1528 // socket is still connected, receiving the FIN, and sending/reading data
1529 // on a reused socket. If we receive the FIN between the connectedness
1530 // check and writing/reading from the socket, we may first learn the socket
1531 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1532 // likely happen when trying to retrieve its IP address.
1533 // See http://crbug.com/105824 for more details.
1534 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491535 // If a socket is closed on its initial request, HttpStreamParser returns
1536 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1537 // preconnected but failed to be used before the server timed it out.
1538 case ERR_EMPTY_RESPONSE:
1539 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381540 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001541 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301542 ResetConnectionAndRequestForResend();
1543 error = OK;
1544 }
1545 break;
[email protected]8753a122011-10-16 08:05:081546 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001547 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271548 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381549 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001550 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001551 ResetConnectionAndRequestForResend();
1552 error = OK;
1553 break;
bnc8016c1f2017-03-31 02:11:291554 case ERR_MISDIRECTED_REQUEST:
1555 // If this is the second try, just give up.
1556 if (!enable_ip_based_pooling_)
1557 return OK;
1558 // Otherwise, since the response status was 421 Misdirected Request,
1559 // retry the request with IP based pooling disabled.
1560 enable_ip_based_pooling_ = false;
1561 net_log_.AddEventWithNetErrorCode(
1562 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1563 ResetConnectionAndRequestForResend();
1564 error = OK;
1565 break;
[email protected]bd0b6772011-01-11 19:59:301566 }
1567 return error;
1568}
1569
[email protected]c3b35c22008-09-27 03:19:421570void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581571 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221572 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191573 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221574 total_sent_bytes_ += stream_->GetTotalSentBytes();
1575 }
zhongyica364fbb2015-12-12 03:39:121576 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581577}
1578
1579void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251580 send_start_time_ = base::TimeTicks();
1581 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251582
[email protected]0757e7702009-03-27 04:00:221583 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421584 read_buf_ = NULL;
1585 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571586 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201587 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141588 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381589 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591590 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121591 net_error_details_.quic_broken = false;
1592 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481593 provided_token_binding_key_.reset();
1594 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121595}
1596
1597void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1598 if (stream_)
1599 stream_->PopulateNetErrorDetails(&net_error_details_);
1600 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571601}
1602
davidben701ca982015-05-18 21:21:421603void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421604 if (result != OK)
1605 return;
1606
davidbenca9d69162015-05-04 20:18:451607 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091608 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451609}
1610
[email protected]0877e3d2009-10-17 22:29:571611HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501612 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421613}
1614
[email protected]a34f61ee2014-03-18 20:59:491615bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381616 bool connection_is_proven = stream_->IsConnectionReused();
1617 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161618
[email protected]2a5c76b2008-09-25 22:15:161619 // NOTE: we resend a request only if we reused a keep-alive connection.
1620 // This automatically prevents an infinite resend loop because we'll run
1621 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381622 if (connection_is_proven && !has_received_headers)
1623 return true;
1624 return false;
[email protected]1c773ea12009-04-28 19:58:421625}
1626
1627void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381628 if (stream_.get()) {
1629 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121630 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161631 }
1632
[email protected]0877e3d2009-10-17 22:29:571633 // We need to clear request_headers_ because it contains the real request
1634 // headers, but we may need to resend the CONNECT request first to recreate
1635 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201636 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501637 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541638}
1639
[email protected]1c773ea12009-04-28 19:58:421640bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241641 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421642}
license.botbf09a502008-08-24 00:55:551643
[email protected]1c773ea12009-04-28 19:58:421644bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041645 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421646}
1647
[email protected]e772db3f2010-07-12 18:11:131648int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271649 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501650 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421651
[email protected]0877e3d2009-10-17 22:29:571652 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581653 if (status != HTTP_UNAUTHORIZED &&
1654 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421655 return OK;
[email protected]9094b602012-02-27 21:44:581656 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111657 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161658 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1659 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421660
[email protected]9094b602012-02-27 21:44:581661 // This case can trigger when an HTTPS server responds with a "Proxy
1662 // authentication required" status code through a non-authenticating
1663 // proxy.
[email protected]7a67a8152010-11-05 18:31:101664 if (!auth_controllers_[target].get())
1665 return ERR_UNEXPECTED_PROXY_AUTH;
1666
[email protected]a7ea8832010-07-12 17:54:541667 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491668 headers, response_.ssl_info,
1669 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311670 net_log_);
[email protected]228404f2010-06-24 04:31:411671 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491672 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411673
1674 scoped_refptr<AuthChallengeInfo> auth_info =
1675 auth_controllers_[target]->auth_info();
1676 if (auth_info.get())
1677 response_.auth_challenge = auth_info;
1678
[email protected]228404f2010-06-24 04:31:411679 return rv;
[email protected]f9ee6b52008-11-08 06:46:231680}
1681
[email protected]8e6441ca2010-08-19 05:56:381682bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1683 return auth_controllers_[target].get() &&
1684 auth_controllers_[target]->HaveAuth();
1685}
1686
[email protected]228404f2010-06-24 04:31:411687GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1688 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461689 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411690 if (!proxy_info_.proxy_server().is_valid() ||
1691 proxy_info_.proxy_server().is_direct()) {
1692 return GURL(); // There is no proxy server.
1693 }
[email protected]2df19bb2010-08-25 20:13:461694 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1695 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351696 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461697 }
[email protected]228404f2010-06-24 04:31:411698 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291699 if (ForWebSocketHandshake()) {
1700 const GURL& url = request_->url;
1701 url::Replacements<char> ws_to_http;
1702 if (url.SchemeIs("ws")) {
1703 ws_to_http.SetScheme("http", url::Component(0, 4));
1704 } else {
1705 DCHECK(url.SchemeIs("wss"));
1706 ws_to_http.SetScheme("https", url::Component(0, 5));
1707 }
1708 return url.ReplaceComponents(ws_to_http);
1709 }
[email protected]228404f2010-06-24 04:31:411710 return request_->url;
1711 default:
1712 return GURL();
1713 }
[email protected]c3b35c22008-09-27 03:19:421714}
1715
[email protected]831e4a32013-11-14 02:14:441716bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141717 return websocket_handshake_stream_base_create_helper_ &&
1718 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441719}
1720
ttuttle1f2d7e92015-04-28 16:17:471721void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1722 DCHECK(stream_request_);
1723
1724 // Since the transaction can restart with auth credentials, it may create a
1725 // stream more than once. Accumulate all of the connection attempts across
1726 // those streams by appending them to the vector:
1727 for (const auto& attempt : stream_request_->connection_attempts())
1728 connection_attempts_.push_back(attempt);
1729}
1730
eustasc7d27da2017-04-06 10:33:201731bool HttpNetworkTransaction::ContentEncodingsValid() const {
1732 HttpResponseHeaders* headers = GetResponseHeaders();
1733 DCHECK(headers);
1734
1735 std::string accept_encoding;
1736 request_headers_.GetHeader(HttpRequestHeaders::kAcceptEncoding,
1737 &accept_encoding);
1738 std::set<std::string> allowed_encodings;
1739 if (!HttpUtil::ParseAcceptEncoding(accept_encoding, &allowed_encodings)) {
1740 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1741 return false;
1742 }
1743
1744 std::string content_encoding;
1745 headers->GetNormalizedHeader("Content-Encoding", &content_encoding);
1746 std::set<std::string> used_encodings;
1747 if (!HttpUtil::ParseContentEncoding(content_encoding, &used_encodings)) {
1748 FilterSourceStream::ReportContentDecodingFailed(SourceStream::TYPE_INVALID);
1749 return false;
1750 }
1751
1752 // When "Accept-Encoding" is not specified, it is parsed as "*".
1753 // If "*" encoding is advertised, then any encoding should be "accepted".
1754 // This does not mean, that it will be successfully decoded.
1755 if (allowed_encodings.find("*") != allowed_encodings.end())
1756 return true;
1757
1758 for (auto const& encoding : used_encodings) {
1759 SourceStream::SourceType source_type =
1760 FilterSourceStream::ParseEncodingType(encoding);
1761 // We don't reject encodings we are not aware. They just will not decode.
1762 if (source_type == SourceStream::TYPE_UNKNOWN)
1763 continue;
1764 if (allowed_encodings.find(encoding) == allowed_encodings.end()) {
1765 FilterSourceStream::ReportContentDecodingFailed(source_type);
1766 return false;
1767 }
1768 }
1769 return true;
1770}
1771
[email protected]c3b35c22008-09-27 03:19:421772} // namespace net