blob: a5395d758bcc4c9d26ef115ca43da28ce9424a0c [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"
[email protected]c3b35c22008-09-27 03:19:4235#include "net/http/http_auth.h"
36#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2437#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3638#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5239#include "net/http/http_chunked_decoder.h"
40#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5441#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1342#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4743#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5244#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2145#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5746#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5347#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5848#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3949#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3150#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5251#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2352#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3653#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0054#include "net/log/net_log_event_type.h"
tbansal2ecbbc72016-10-06 17:15:4755#include "net/proxy/proxy_server.h"
[email protected]f7984fc62009-06-22 23:26:4456#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2157#include "net/socket/next_proto.h"
[email protected]a796bcec2010-03-22 17:17:2658#include "net/socket/socks_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2859#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4060#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3261#include "net/spdy/spdy_session.h"
62#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5763#include "net/ssl/ssl_cert_request_info.h"
64#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5465#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1466#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5467#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2968#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5269
initial.commit586acc5fe2008-07-26 22:42:5270namespace net {
71
[email protected]1c773ea12009-04-28 19:58:4272namespace {
73
danakj1fd259a02016-04-16 03:17:0974std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1775 const GURL* url,
76 int net_error,
77 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0978 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2579 dict->SetString("host_and_port", GetHostAndPort(*url));
80 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4881 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2582}
83
[email protected]1c773ea12009-04-28 19:58:4284} // namespace
85
initial.commit586acc5fe2008-07-26 22:42:5286//-----------------------------------------------------------------------------
87
[email protected]262eec82013-03-19 21:01:3688HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
89 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2290 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3291 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
92 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5293 session_(session),
94 request_(NULL),
[email protected]262eec82013-03-19 21:01:3695 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5796 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2097 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5298 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:1999 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22100 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54101 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44102 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02103 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12104 net_error_details_() {
[email protected]3ce7df0f2010-03-03 00:30:50105}
106
[email protected]0b0bf032010-09-21 18:08:50107HttpNetworkTransaction::~HttpNetworkTransaction() {
108 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50109 // TODO(mbelshe): The stream_ should be able to compute whether or not the
110 // stream should be kept alive. No reason to compute here
111 // and pass it in.
mmenkebd84c392015-09-02 14:12:34112 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50113 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34114 } else if (stream_->IsResponseBodyComplete()) {
115 // If the response body is complete, we can just reuse the socket.
116 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50117 } else {
mmenkebd84c392015-09-02 14:12:34118 // Otherwise, we try to drain the response body.
119 HttpStream* stream = stream_.release();
120 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50121 }
122 }
[email protected]02cad5d2013-10-02 08:14:03123 if (request_ && request_->upload_data_stream)
124 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50125}
126
[email protected]684970b2009-08-14 04:54:46127int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41128 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20129 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53130 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04131 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15132 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04133
nharper8cdb0fb2016-04-22 21:34:59134 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
135 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
136
[email protected]99ffa5a2011-10-06 04:20:19137 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14138 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19139 proxy_ssl_config_.rev_checking_enabled = false;
140 }
[email protected]6fbac162011-06-20 00:29:04141
jkarlinfb1d5172015-01-12 14:10:29142 if (request_->load_flags & LOAD_PREFETCH)
143 response_.unused_since_prefetch = true;
144
rdsmith1d343be52016-10-21 20:37:50145 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04146 int rv = DoLoop(OK);
147 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41148 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04149 return rv;
150}
151
152int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41153 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38154 DCHECK(!stream_.get());
155 DCHECK(!stream_request_.get());
156 DCHECK_EQ(STATE_NONE, next_state_);
157
[email protected]82918cc2010-08-25 17:24:50158 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38159
[email protected]ccb40e52008-09-17 20:54:40160 int rv = DoLoop(OK);
161 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41162 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11163 return rv;
[email protected]96d570e42008-08-05 22:43:04164}
165
[email protected]0b45559b2009-06-12 21:45:11166int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54167 X509Certificate* client_cert,
168 SSLPrivateKey* client_private_key,
169 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38170 // In HandleCertificateRequest(), we always tear down existing stream
171 // requests to force a new connection. So we shouldn't have one here.
172 DCHECK(!stream_request_.get());
173 DCHECK(!stream_.get());
174 DCHECK_EQ(STATE_NONE, next_state_);
175
[email protected]102957f2011-09-02 17:10:14176 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
177 &proxy_ssl_config_ : &server_ssl_config_;
178 ssl_config->send_client_cert = true;
179 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54180 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45181 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54182 response_.cert_request_info->host_and_port, client_cert,
183 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11184 // Reset the other member variables.
185 // Note: this is necessary only with SSL renegotiation.
186 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50187 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11188 int rv = DoLoop(OK);
189 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41190 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11191 return rv;
192}
193
[email protected]49639fa2011-12-20 23:22:41194int HttpNetworkTransaction::RestartWithAuth(
195 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22196 HttpAuth::Target target = pending_auth_target_;
197 if (target == HttpAuth::AUTH_NONE) {
198 NOTREACHED();
199 return ERR_UNEXPECTED;
200 }
[email protected]0757e7702009-03-27 04:00:22201 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42202
[email protected]f3cf9802011-10-28 18:44:58203 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13204
[email protected]49639fa2011-12-20 23:22:41205 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38206
207 int rv = OK;
208 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
209 // In this case, we've gathered credentials for use with proxy
210 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50211 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38212 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59213 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54214 ResetStateForRestart();
asanka681f02d2017-02-22 17:06:39215 rv = stream_request_->RestartTunnelWithProxyAuth();
[email protected]a7ea8832010-07-12 17:54:54216 } else {
[email protected]8e6441ca2010-08-19 05:56:38217 // In this case, we've gathered credentials for the server or the proxy
218 // but it is not during the tunneling phase.
219 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54220 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38221 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54222 }
[email protected]c3b35c22008-09-27 03:19:42223
[email protected]c3b35c22008-09-27 03:19:42224 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41225 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42226 return rv;
[email protected]96d570e42008-08-05 22:43:04227}
228
[email protected]f9ee6b52008-11-08 06:46:23229void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
230 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38231 DCHECK(!stream_request_.get());
232
[email protected]2d2697f92009-02-18 21:00:32233 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57234 // Even if the server says the connection is keep-alive, we have to be
235 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34236 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57237 // If the response body hasn't been completely read, we need to drain
238 // it first.
[email protected]351ab642010-08-05 16:55:31239 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32240 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57241 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32242 read_buf_len_ = kDrainBodyBufferSize;
243 return;
244 }
[email protected]0877e3d2009-10-17 22:29:57245 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09246 }
247
[email protected]2d2697f92009-02-18 21:00:32248 // We don't need to drain the response body, so we act as if we had drained
249 // the response body.
250 DidDrainBodyForAuthRestart(keep_alive);
251}
252
253void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38254 DCHECK(!stream_request_.get());
255
256 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19257 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22258 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58259 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34260 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38261 // We should call connection_->set_idle_time(), but this doesn't occur
262 // often enough to be worth the trouble.
263 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39264 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38265 }
[email protected]697ef4c2010-10-14 16:38:58266
267 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46268 // Close the stream and mark it as not_reusable. Even in the
269 // keep_alive case, we've determined that the stream_ is not
270 // reusable if new_stream is NULL.
271 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58272 next_state_ = STATE_CREATE_STREAM;
273 } else {
sclittlefb249892015-09-10 21:33:22274 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19275 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22276 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58277 next_state_ = STATE_INIT_STREAM;
278 }
279 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32280 }
[email protected]f9ee6b52008-11-08 06:46:23281
282 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58283 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23284}
285
[email protected]8e6441ca2010-08-19 05:56:38286bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
287 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
288 HaveAuth(pending_auth_target_);
289}
290
[email protected]9dea9e1f2009-01-29 00:30:47291int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41292 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04293 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35294 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04295
[email protected]ad8e04a2010-11-01 04:16:27296 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38297 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04298 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29299 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04300 // bytes when establishing a tunnel because they might be controlled by
301 // an active network attacker. We don't worry about this for HTTP
302 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29303 // We reach this case when the user cancels a 407 proxy auth prompt. We
304 // also don't worry about this for an HTTPS Proxy, because the
305 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04306 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46307 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58308 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54309 LOG(WARNING) << "Blocked proxy response with status "
310 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15311 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04312 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44313 }
314
[email protected]e60e47a2010-07-14 03:37:18315 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15316 next_state_ = STATE_READ_BODY;
317
318 // We have reached the end of Start state machine, reset the requestinfo to
319 // null.
320 // RequestInfo is a member of the HttpTransaction's consumer and is useful
321 // only till final response headers are received. A reset will ensure that
322 // HttpRequestInfo is only used up until final response headers are received.
323 // Resetting is allowed so that the transaction can be disassociated from its
324 // creating consumer in cases where it is shared for writing to the cache.
325 // It is also safe to reset it to null at this point since upload_data_stream
326 // is also not used in the Read state machine.
327 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18328
[email protected]96d570e42008-08-05 22:43:04329 read_buf_ = buf;
330 read_buf_len_ = buf_len;
331
[email protected]96d570e42008-08-05 22:43:04332 int rv = DoLoop(OK);
333 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41334 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04335 return rv;
336}
337
[email protected]8cd06c02014-01-25 07:50:14338void HttpNetworkTransaction::StopCaching() {}
339
[email protected]79e1fd62013-06-20 06:50:04340bool HttpNetworkTransaction::GetFullRequestHeaders(
341 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32342 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04343 *headers = request_headers_;
344 return true;
345}
346
sclittle4de1bab92015-09-22 21:28:24347int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
348 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19349 if (stream_)
350 total_received_bytes += stream_->GetTotalReceivedBytes();
351 return total_received_bytes;
352}
353
sclittlefb249892015-09-10 21:33:22354int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
355 int64_t total_sent_bytes = total_sent_bytes_;
356 if (stream_)
357 total_sent_bytes += stream_->GetTotalSentBytes();
358 return total_sent_bytes;
359}
360
[email protected]8cd06c02014-01-25 07:50:14361void HttpNetworkTransaction::DoneReading() {}
362
[email protected]96d570e42008-08-05 22:43:04363const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55364 return &response_;
[email protected]96d570e42008-08-05 22:43:04365}
366
367LoadState HttpNetworkTransaction::GetLoadState() const {
368 // TODO(wtc): Define a new LoadState value for the
369 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
370 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50371 case STATE_THROTTLE_COMPLETE:
372 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48373 case STATE_CREATE_STREAM:
374 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50375 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38376 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15377 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
378 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57379 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04380 return LOAD_STATE_SENDING_REQUEST;
381 case STATE_READ_HEADERS_COMPLETE:
382 return LOAD_STATE_WAITING_FOR_RESPONSE;
383 case STATE_READ_BODY_COMPLETE:
384 return LOAD_STATE_READING_RESPONSE;
385 default:
386 return LOAD_STATE_IDLE;
387 }
388}
389
[email protected]8cd06c02014-01-25 07:50:14390void HttpNetworkTransaction::SetQuicServerInfo(
391 QuicServerInfo* quic_server_info) {}
392
[email protected]5033ab82013-03-22 20:17:46393bool HttpNetworkTransaction::GetLoadTimingInfo(
394 LoadTimingInfo* load_timing_info) const {
395 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
396 return false;
397
398 load_timing_info->proxy_resolve_start =
399 proxy_info_.proxy_resolve_start_time();
400 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
401 load_timing_info->send_start = send_start_time_;
402 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46403 return true;
404}
405
ttuttled9dbc652015-09-29 20:00:59406bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36407 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59408 return false;
409
410 *endpoint = remote_endpoint_;
411 return true;
412}
413
zhongyi48704c182015-12-07 07:52:02414void HttpNetworkTransaction::PopulateNetErrorDetails(
415 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12416 *details = net_error_details_;
417 if (stream_)
418 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02419}
420
[email protected]5033ab82013-03-22 20:17:46421void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
422 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50423
rdsmith29dbad12017-02-17 02:22:18424 // TODO(rdsmith): Note that if any code indirectly executed by
425 // |stream_request_->SetPriority()| or |stream_->SetPriority()|
426 // ever implements a throttling mechanism where changing a request's
427 // priority may cause a this or another request to synchronously succeed
428 // or fail, that callback could synchronously delete |*this|, causing
429 // a crash on return to this code.
430 //
431 // |throttle_->SetPriority()| has exactly the above attributes, which
432 // is why it's the last call in this function.
433
[email protected]bf828982013-08-14 18:01:47434 if (stream_request_)
435 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03436 if (stream_)
437 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50438
439 if (throttle_)
440 throttle_->SetPriority(priority);
441 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46442}
443
[email protected]831e4a32013-11-14 02:14:44444void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
445 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
446 websocket_handshake_stream_base_create_helper_ = create_helper;
447}
448
[email protected]1826a402014-01-08 15:40:48449void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
450 const BeforeNetworkStartCallback& callback) {
451 before_network_start_callback_ = callback;
452}
453
ryansturm49a8cb12016-06-15 16:51:09454void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
455 const BeforeHeadersSentCallback& callback) {
456 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27457}
458
[email protected]1826a402014-01-08 15:40:48459int HttpNetworkTransaction::ResumeNetworkStart() {
460 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
461 return DoLoop(OK);
462}
463
[email protected]102e27c2011-02-23 01:01:31464void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
465 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39466 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50467 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38468 DCHECK(stream_request_.get());
469
sclittlefb249892015-09-10 21:33:22470 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19471 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22472 total_sent_bytes_ += stream_->GetTotalSentBytes();
473 }
[email protected]8e6441ca2010-08-19 05:56:38474 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14475 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31476 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52477 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21478 response_.alpn_negotiated_protocol =
479 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38480 response_.was_fetched_via_spdy = stream_request_->using_spdy();
481 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15482 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47483 response_.proxy_server = proxy_info_.proxy_server();
484 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
485 response_.proxy_server = ProxyServer::Direct();
486 else
487 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38488 OnIOComplete(OK);
489}
490
xunjieli5749218c2016-03-22 16:43:06491void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08492 const SSLConfig& used_ssl_config,
493 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06494 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08495 NOTREACHED();
496}
497
[email protected]a9cf2b92013-10-30 12:08:49498void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50499 const SSLConfig& used_ssl_config,
500 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49501 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44502 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50503}
504
[email protected]102e27c2011-02-23 01:01:31505void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13506 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50507 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38508 DCHECK_NE(OK, result);
509 DCHECK(stream_request_.get());
510 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14511 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38512
513 OnIOComplete(result);
514}
515
[email protected]102e27c2011-02-23 01:01:31516void HttpNetworkTransaction::OnCertificateError(
517 int result,
518 const SSLConfig& used_ssl_config,
519 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50520 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38521 DCHECK_NE(OK, result);
522 DCHECK(stream_request_.get());
523 DCHECK(!stream_.get());
524
525 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14526 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38527
528 // TODO(mbelshe): For now, we're going to pass the error through, and that
529 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50530 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
531 // good and the user chooses to ignore the error. This is not ideal, but not
532 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38533
534 OnIOComplete(result);
535}
536
537void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50538 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31539 const SSLConfig& used_ssl_config,
540 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50541 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38542 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50543 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38544
545 establishing_tunnel_ = true;
546 response_.headers = proxy_response.headers;
547 response_.auth_challenge = proxy_response.auth_challenge;
548 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14549 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31550 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38551
552 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
553 pending_auth_target_ = HttpAuth::AUTH_PROXY;
554
555 DoCallback(OK);
556}
557
558void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31559 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50560 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50561 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38562
[email protected]102957f2011-09-02 17:10:14563 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38564 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58565 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38566}
567
[email protected]511f6f52010-12-17 03:58:29568void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
569 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31570 const SSLConfig& used_ssl_config,
571 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39572 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29573 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
574
ttuttle1f2d7e92015-04-28 16:17:47575 CopyConnectionAttemptsFromStreamRequest();
576
[email protected]511f6f52010-12-17 03:58:29577 headers_valid_ = true;
578 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14579 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31580 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22581 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19582 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22583 total_sent_bytes_ += stream_->GetTotalSentBytes();
584 }
[email protected]511f6f52010-12-17 03:58:29585 stream_.reset(stream);
586 stream_request_.reset(); // we're done with the stream request
587 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
588}
589
zhongyi48704c182015-12-07 07:52:02590void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12591 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02592}
593
ttuttle1f2d7e92015-04-28 16:17:47594void HttpNetworkTransaction::GetConnectionAttempts(
595 ConnectionAttempts* out) const {
596 *out = connection_attempts_;
597}
598
rdsmithbf8c3c12016-11-18 18:16:24599void HttpNetworkTransaction::OnThrottleUnblocked(
600 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50601 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24602 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50603 // be so in the future.
604 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
605
606 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
607
608 DoLoop(OK);
609}
610
Adam Ricecb76ac62015-02-20 05:33:25611bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37612 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52613}
614
nharperb7441ef2016-01-25 23:54:14615bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
616 if (!IsSecureRequest())
617 return false;
618 SSLInfo ssl_info;
619 stream_->GetSSLInfo(&ssl_info);
620 return ssl_info.token_binding_negotiated &&
621 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
622 session_->params().channel_id_service;
623}
624
625void HttpNetworkTransaction::RecordTokenBindingSupport() const {
626 // This enum is used for an UMA histogram - do not change or re-use values.
627 enum {
628 DISABLED = 0,
629 CLIENT_ONLY = 1,
630 CLIENT_AND_SERVER = 2,
631 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
632 TOKEN_BINDING_SUPPORT_MAX
633 } supported;
634 if (!IsSecureRequest())
635 return;
636 SSLInfo ssl_info;
637 stream_->GetSSLInfo(&ssl_info);
638 if (!session_->params().enable_token_binding) {
639 supported = DISABLED;
640 } else if (!session_->params().channel_id_service) {
641 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
642 } else if (ssl_info.token_binding_negotiated) {
643 supported = CLIENT_AND_SERVER;
644 } else {
645 supported = CLIENT_ONLY;
646 }
647 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
648 TOKEN_BINDING_SUPPORT_MAX);
649}
650
Adam Rice425cf122015-01-19 06:18:24651bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12652 return (proxy_info_.is_http() || proxy_info_.is_https() ||
653 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24654 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
655}
656
initial.commit586acc5fe2008-07-26 22:42:52657void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50658 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41659 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52660
[email protected]96d570e42008-08-05 22:43:04661 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41662 CompletionCallback c = callback_;
663 callback_.Reset();
664 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52665}
666
667void HttpNetworkTransaction::OnIOComplete(int result) {
668 int rv = DoLoop(result);
669 if (rv != ERR_IO_PENDING)
670 DoCallback(rv);
671}
672
673int HttpNetworkTransaction::DoLoop(int result) {
674 DCHECK(next_state_ != STATE_NONE);
675
676 int rv = result;
677 do {
678 State state = next_state_;
679 next_state_ = STATE_NONE;
680 switch (state) {
rdsmith1d343be52016-10-21 20:37:50681 case STATE_THROTTLE:
682 DCHECK_EQ(OK, rv);
683 rv = DoThrottle();
684 break;
685 case STATE_THROTTLE_COMPLETE:
686 DCHECK_EQ(OK, rv);
687 rv = DoThrottleComplete();
688 break;
[email protected]1826a402014-01-08 15:40:48689 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
690 DCHECK_EQ(OK, rv);
691 rv = DoNotifyBeforeCreateStream();
692 break;
[email protected]82918cc2010-08-25 17:24:50693 case STATE_CREATE_STREAM:
694 DCHECK_EQ(OK, rv);
695 rv = DoCreateStream();
696 break;
697 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46698 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
699 // solved.
700 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50701 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46702 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50703 break;
[email protected]351ab642010-08-05 16:55:31704 case STATE_INIT_STREAM:
705 DCHECK_EQ(OK, rv);
706 rv = DoInitStream();
707 break;
708 case STATE_INIT_STREAM_COMPLETE:
709 rv = DoInitStreamComplete(rv);
710 break;
[email protected]044de0642010-06-17 10:42:15711 case STATE_GENERATE_PROXY_AUTH_TOKEN:
712 DCHECK_EQ(OK, rv);
713 rv = DoGenerateProxyAuthToken();
714 break;
715 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
716 rv = DoGenerateProxyAuthTokenComplete(rv);
717 break;
718 case STATE_GENERATE_SERVER_AUTH_TOKEN:
719 DCHECK_EQ(OK, rv);
720 rv = DoGenerateServerAuthToken();
721 break;
722 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
723 rv = DoGenerateServerAuthTokenComplete(rv);
724 break;
nharperd6e65822016-03-30 23:05:48725 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14726 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48727 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14728 break;
nharperd6e65822016-03-30 23:05:48729 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
730 rv = DoGetProvidedTokenBindingKeyComplete(rv);
731 break;
732 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
733 DCHECK_EQ(OK, rv);
734 rv = DoGetReferredTokenBindingKey();
735 break;
736 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
737 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14738 break;
[email protected]daddea62012-09-19 05:51:13739 case STATE_INIT_REQUEST_BODY:
740 DCHECK_EQ(OK, rv);
741 rv = DoInitRequestBody();
742 break;
743 case STATE_INIT_REQUEST_BODY_COMPLETE:
744 rv = DoInitRequestBodyComplete(rv);
745 break;
[email protected]4875ba12011-03-30 22:31:51746 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55747 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00748 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51749 rv = DoBuildRequest();
750 break;
751 case STATE_BUILD_REQUEST_COMPLETE:
752 rv = DoBuildRequestComplete(rv);
753 break;
754 case STATE_SEND_REQUEST:
755 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57756 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52757 break;
[email protected]0877e3d2009-10-17 22:29:57758 case STATE_SEND_REQUEST_COMPLETE:
759 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43760 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00761 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52762 break;
763 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55764 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00765 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52766 rv = DoReadHeaders();
767 break;
768 case STATE_READ_HEADERS_COMPLETE:
769 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43770 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00771 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52772 break;
773 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55774 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00775 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52776 rv = DoReadBody();
777 break;
778 case STATE_READ_BODY_COMPLETE:
779 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43780 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00781 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52782 break;
[email protected]2d2697f92009-02-18 21:00:32783 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55784 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53785 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00786 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32787 rv = DoDrainBodyForAuthRestart();
788 break;
789 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
790 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43791 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00792 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 default:
795 NOTREACHED() << "bad state";
796 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04797 break;
initial.commit586acc5fe2008-07-26 22:42:52798 }
799 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
800
801 return rv;
802}
803
rdsmith1d343be52016-10-21 20:37:50804int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24805 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50806 throttle_ = session_->throttler()->CreateThrottle(
807 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
808 next_state_ = STATE_THROTTLE_COMPLETE;
809
rdsmithbf8c3c12016-11-18 18:16:24810 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50811 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
812 return ERR_IO_PENDING;
813 }
814
815 return OK;
816}
817
818int HttpNetworkTransaction::DoThrottleComplete() {
819 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24820 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50821
822 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
823
824 return OK;
825}
826
[email protected]1826a402014-01-08 15:40:48827int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
828 next_state_ = STATE_CREATE_STREAM;
829 bool defer = false;
830 if (!before_network_start_callback_.is_null())
831 before_network_start_callback_.Run(&defer);
832 if (!defer)
833 return OK;
834 return ERR_IO_PENDING;
835}
836
[email protected]82918cc2010-08-25 17:24:50837int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34838 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26839 tracked_objects::ScopedTracker tracking_profile(
840 FROM_HERE_WITH_EXPLICIT_FUNCTION(
841 "424359 HttpNetworkTransaction::DoCreateStream"));
842
ttuttlec0c828492015-05-15 01:25:55843 response_.network_accessed = true;
844
[email protected]82918cc2010-08-25 17:24:50845 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44846 if (ForWebSocketHandshake()) {
847 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23848 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44849 ->RequestWebSocketHandshakeStream(
850 *request_,
851 priority_,
852 server_ssl_config_,
853 proxy_ssl_config_,
854 this,
855 websocket_handshake_stream_base_create_helper_,
856 net_log_));
857 } else {
858 stream_request_.reset(
859 session_->http_stream_factory()->RequestStream(
860 *request_,
861 priority_,
862 server_ssl_config_,
863 proxy_ssl_config_,
864 this,
865 net_log_));
866 }
[email protected]26816882010-10-14 18:03:09867 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38868 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31869}
870
[email protected]82918cc2010-08-25 17:24:50871int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47872 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
873 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
874 // which resets the stream request first. Therefore, we have to grab the
875 // connection attempts in *that* function instead of here in that case.
876 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
877 CopyConnectionAttemptsFromStreamRequest();
878
davidben701ca982015-05-18 21:21:42879 if (request_->url.SchemeIsCryptographic())
880 RecordSSLFallbackMetrics(result);
881
[email protected]394816e92010-08-03 07:38:59882 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50883 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38884 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33885 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
886 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29887 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
888 // Return OK and let the caller read the proxy's error page
889 next_state_ = STATE_NONE;
890 return OK;
bncfacdd852015-01-09 19:22:54891 } else if (result == ERR_HTTP_1_1_REQUIRED ||
892 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
893 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59894 }
895
[email protected]bd0b6772011-01-11 19:59:30896 // Handle possible handshake errors that may have occurred if the stream
897 // used SSL for one or more of the layers.
898 result = HandleSSLHandshakeError(result);
899
[email protected]8e6441ca2010-08-19 05:56:38900 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09901 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38902 return result;
[email protected]394816e92010-08-03 07:38:59903}
904
[email protected]82918cc2010-08-25 17:24:50905int HttpNetworkTransaction::DoInitStream() {
906 DCHECK(stream_.get());
907 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59908
909 stream_->GetRemoteEndpoint(&remote_endpoint_);
910
[email protected]262eec82013-03-19 21:01:36911 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50912}
913
914int HttpNetworkTransaction::DoInitStreamComplete(int result) {
915 if (result == OK) {
916 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50917 } else {
[email protected]82918cc2010-08-25 17:24:50918 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26919 result = HandleIOError(result);
920
921 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22922 if (stream_) {
923 total_received_bytes_ += stream_->GetTotalReceivedBytes();
924 total_sent_bytes_ += stream_->GetTotalSentBytes();
925 }
zhongyica364fbb2015-12-12 03:39:12926 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50927 }
928
929 return result;
930}
931
[email protected]044de0642010-06-17 10:42:15932int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
933 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
934 if (!ShouldApplyProxyAuth())
935 return OK;
[email protected]394816e92010-08-03 07:38:59936 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
937 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09938 auth_controllers_[target] =
939 new HttpAuthController(target,
940 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31941 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09942 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59943 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41944 io_callback_,
[email protected]394816e92010-08-03 07:38:59945 net_log_);
[email protected]044de0642010-06-17 10:42:15946}
947
948int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
949 DCHECK_NE(ERR_IO_PENDING, rv);
950 if (rv == OK)
951 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
952 return rv;
953}
954
955int HttpNetworkTransaction::DoGenerateServerAuthToken() {
956 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59957 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54958 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09959 auth_controllers_[target] =
960 new HttpAuthController(target,
961 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31962 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09963 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54964 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
965 auth_controllers_[target]->DisableEmbeddedIdentity();
966 }
[email protected]044de0642010-06-17 10:42:15967 if (!ShouldApplyServerAuth())
968 return OK;
[email protected]394816e92010-08-03 07:38:59969 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41970 io_callback_,
[email protected]394816e92010-08-03 07:38:59971 net_log_);
[email protected]044de0642010-06-17 10:42:15972}
973
974int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
975 DCHECK_NE(ERR_IO_PENDING, rv);
976 if (rv == OK)
nharperd6e65822016-03-30 23:05:48977 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15978 return rv;
979}
980
nharperd6e65822016-03-30 23:05:48981int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
982 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14983 if (!IsTokenBindingEnabled())
984 return OK;
985
mikecirone8b85c432016-09-08 19:11:00986 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperb7441ef2016-01-25 23:54:14987 ChannelIDService* channel_id_service = session_->params().channel_id_service;
988 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48989 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14990 &token_binding_request_);
991}
992
nharperd6e65822016-03-30 23:05:48993int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14994 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48995 if (IsTokenBindingEnabled()) {
996 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00997 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48998 }
999
1000 if (rv == OK)
1001 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
1002 return rv;
1003}
1004
1005int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1006 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1007 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141008 return OK;
1009
mikecirone8b85c432016-09-08 19:11:001010 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperd6e65822016-03-30 23:05:481011 ChannelIDService* channel_id_service = session_->params().channel_id_service;
1012 return channel_id_service->GetOrCreateChannelID(
1013 request_->token_binding_referrer, &referred_token_binding_key_,
1014 io_callback_, &token_binding_request_);
1015}
1016
1017int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1018 DCHECK_NE(ERR_IO_PENDING, rv);
1019 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1020 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001021 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481022 }
1023 if (rv == OK)
1024 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141025 return rv;
1026}
1027
1028int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241029 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141030 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1031 GetHostAndOptionalPort(request_->url));
1032
1033 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241034 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141035 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1036 "keep-alive");
1037 } else {
1038 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1039 }
1040
[email protected]2979a492011-04-06 00:29:141041 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111042 if (request_->upload_data_stream) {
1043 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141044 request_headers_.SetHeader(
1045 HttpRequestHeaders::kTransferEncoding, "chunked");
1046 } else {
1047 request_headers_.SetHeader(
1048 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111049 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141050 }
csharrisonf473dd192015-08-18 13:54:131051 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141052 // An empty POST/PUT request still needs a content length. As for HEAD,
1053 // IE and Safari also add a content length header. Presumably it is to
1054 // support sending a HEAD request to an URL that only expects to be sent a
1055 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131056 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1057 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141058 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1059 }
1060
nharperb7441ef2016-01-25 23:54:141061 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481062 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141063 std::string token_binding_header;
1064 int rv = BuildTokenBindingHeader(&token_binding_header);
1065 if (rv != OK)
1066 return rv;
1067 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1068 token_binding_header);
1069 }
1070
[email protected]2979a492011-04-06 00:29:141071 // Honor load flags that impact proxy caches.
1072 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1073 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1074 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1075 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1076 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1077 }
1078
1079 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1080 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1081 &request_headers_);
1082 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1083 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1084 &request_headers_);
1085
[email protected]c10450102011-06-27 09:06:161086 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201087
ryansturm49a8cb12016-06-15 16:51:091088 if (!before_headers_sent_callback_.is_null())
1089 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201090
[email protected]173f8e22013-04-10 04:18:201091 response_.did_use_http_auth =
1092 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1093 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141094 return OK;
1095}
1096
1097int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471098 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141099 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351100 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1101 TokenBindingType::PROVIDED,
1102 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141103 if (rv != OK)
1104 return rv;
1105 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481106 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1107 provided_token_binding_key_.get(), signed_ekm,
1108 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141109 if (rv != OK)
1110 return rv;
nharperd6e65822016-03-30 23:05:481111
nharperb7441ef2016-01-25 23:54:141112 std::vector<base::StringPiece> token_bindings;
1113 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481114
1115 std::string referred_token_binding;
1116 if (referred_token_binding_key_) {
1117 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351118 int rv = stream_->GetTokenBindingSignature(
1119 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1120 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481121 if (rv != OK)
1122 return rv;
1123 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1124 referred_token_binding_key_.get(),
1125 referred_signed_ekm, &referred_token_binding);
1126 if (rv != OK)
1127 return rv;
1128 token_bindings.push_back(referred_token_binding);
1129 }
nharperb7441ef2016-01-25 23:54:141130 std::string header;
1131 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1132 if (rv != OK)
1133 return rv;
nharper78e6d2b2016-09-21 05:42:351134 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471135 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1136 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1137 header_creation_time,
1138 base::TimeDelta::FromMilliseconds(1),
1139 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141140 return OK;
[email protected]2979a492011-04-06 00:29:141141}
1142
[email protected]daddea62012-09-19 05:51:131143int HttpNetworkTransaction::DoInitRequestBody() {
1144 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131145 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111146 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521147 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131148 return rv;
1149}
[email protected]4875ba12011-03-30 22:31:511150
[email protected]daddea62012-09-19 05:51:131151int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1152 if (result == OK)
1153 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131154 return result;
1155}
1156
1157int HttpNetworkTransaction::DoBuildRequest() {
1158 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511159 headers_valid_ = false;
1160
1161 // This is constructed lazily (instead of within our Start method), so that
1162 // we have proxy info available.
1163 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241164 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141165 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511166 }
1167
[email protected]4875ba12011-03-30 22:31:511168 return OK;
1169}
1170
1171int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241172 if (result == OK)
1173 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511174 return result;
1175}
1176
[email protected]0877e3d2009-10-17 22:29:571177int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341178 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261179 tracked_objects::ScopedTracker tracking_profile(
1180 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1181 "424359 HttpNetworkTransaction::DoSendRequest"));
1182
[email protected]58e32bb2013-01-21 18:23:251183 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571184 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1185
[email protected]bf3eb002012-11-15 05:50:111186 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521187}
1188
[email protected]0877e3d2009-10-17 22:29:571189int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251190 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361191
1192 if (result == ERR_HTTP_1_1_REQUIRED ||
1193 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1194 return HandleHttp11Required(result);
1195 }
1196
initial.commit586acc5fe2008-07-26 22:42:521197 if (result < 0)
1198 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571199 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521200 return OK;
1201}
1202
1203int HttpNetworkTransaction::DoReadHeaders() {
1204 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411205 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521206}
1207
1208int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111209 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1210 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381211 if (IsCertificateError(result)) {
1212 // We don't handle a certificate error during SSL renegotiation, so we
1213 // have to return an error that's not in the certificate error range
1214 // (-2xx).
1215 LOG(ERROR) << "Got a server certificate with error " << result
1216 << " during SSL renegotiation";
1217 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1218 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1219 // TODO(wtc): Need a test case for this code path!
1220 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251221 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381222 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501223 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381224 result = HandleCertificateRequest(result);
1225 if (result == OK)
1226 return result;
[email protected]2181ea002009-06-09 01:37:271227 }
1228
bncfacdd852015-01-09 19:22:541229 if (result == ERR_HTTP_1_1_REQUIRED ||
1230 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1231 return HandleHttp11Required(result);
1232 }
1233
[email protected]c871864d72014-03-13 19:56:191234 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1235 // response headers were received, we do the best we can to make sense of it
1236 // and send it back up the stack.
1237 //
1238 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1239 // bizarre for SPDY. Assuming this logic is useful at all.
1240 // TODO(davidben): Bubble the error code up so we do not cache?
1241 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1242 result = OK;
1243
1244 if (result < 0)
1245 return HandleIOError(result);
1246
[email protected]90499482013-06-01 00:39:501247 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521248
[email protected]d58ceea82014-06-04 10:55:541249 // On a 408 response from the server ("Request Timeout") on a stale socket,
1250 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041251 // Headers can be NULL because of http://crbug.com/384554.
1252 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541253 stream_->IsConnectionReused()) {
1254 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001255 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541256 response_.headers->response_code());
1257 // This will close the socket - it would be weird to try and reuse it, even
1258 // if the server doesn't actually close it.
1259 ResetConnectionAndRequestForResend();
1260 return OK;
1261 }
1262
[email protected]93f8b562012-03-27 01:00:161263 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211264 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161265 const int response_code = response_.headers->response_code();
1266 UMA_HISTOGRAM_ENUMERATION(
1267 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1268 }
1269
[email protected]3abacd62012-06-10 20:20:321270 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001271 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321272 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391273
bncbe0f6af2015-10-15 17:49:561274 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571275 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1276 // indicates a buggy server. See:
1277 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1278 if (request_->method == "PUT")
1279 return ERR_METHOD_NOT_SUPPORTED;
1280 }
[email protected]4ddaf2502008-10-23 18:26:191281
[email protected]0877e3d2009-10-17 22:29:571282 // Check for an intermediate 100 Continue response. An origin server is
1283 // allowed to send this response even if we didn't ask for it, so we just
1284 // need to skip over it.
1285 // We treat any other 1xx in this same way (although in practice getting
1286 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441287 // Unless this is a WebSocket request, in which case we pass it on up.
1288 if (response_.headers->response_code() / 100 == 1 &&
1289 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451290 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571291 next_state_ = STATE_READ_HEADERS;
1292 return OK;
1293 }
1294
bncb26024382016-06-29 02:39:451295 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111296 session_->http_stream_factory()->ProcessAlternativeServices(
1297 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1298 }
[email protected]564b4912010-03-09 16:30:421299
asanka5ffd5d72016-03-23 16:20:491300 if (IsSecureRequest())
1301 stream_->GetSSLInfo(&response_.ssl_info);
1302
[email protected]e772db3f2010-07-12 18:11:131303 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571304 if (rv != OK)
1305 return rv;
1306
[email protected]0877e3d2009-10-17 22:29:571307 headers_valid_ = true;
1308 return OK;
initial.commit586acc5fe2008-07-26 22:42:521309}
1310
1311int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501312 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131313 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381314 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521315
1316 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501317 return stream_->ReadResponseBody(
1318 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521319}
1320
1321int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1322 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381323 bool done = false;
1324 if (result <= 0) {
1325 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521326 done = true;
[email protected]8e6441ca2010-08-19 05:56:381327 }
[email protected]9492e4a2010-02-24 00:58:461328
mmenkebd84c392015-09-02 14:12:341329 // Clean up connection if we are done.
1330 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381331 // Note: Just because IsResponseBodyComplete is true, we're not
1332 // necessarily "done". We're only "done" when it is the last
1333 // read on this HttpNetworkTransaction, which will be signified
1334 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341335 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381336 // the stream. No need to compute it here just to pass back
1337 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341338 bool keep_alive =
1339 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521340
[email protected]8e6441ca2010-08-19 05:56:381341 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011342 // Note: we don't reset the stream here. We've closed it, but we still
1343 // need it around so that callers can call methods such as
1344 // GetUploadProgress() and have them be meaningful.
1345 // TODO(mbelshe): This means we closed the stream here, and we close it
1346 // again in ~HttpNetworkTransaction. Clean that up.
1347
[email protected]8e6441ca2010-08-19 05:56:381348 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521349 }
1350
1351 // Clear these to avoid leaving around old state.
1352 read_buf_ = NULL;
1353 read_buf_len_ = 0;
1354
1355 return result;
1356}
1357
[email protected]2d2697f92009-02-18 21:00:321358int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1359 // This method differs from DoReadBody only in the next_state_. So we just
1360 // call DoReadBody and override the next_state_. Perhaps there is a more
1361 // elegant way for these two methods to share code.
1362 int rv = DoReadBody();
1363 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1364 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1365 return rv;
1366}
1367
[email protected]0877e3d2009-10-17 22:29:571368// TODO(wtc): This method and the DoReadBodyComplete method are almost
1369// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321370int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231371 // keep_alive defaults to true because the very reason we're draining the
1372 // response body is to reuse the connection for auth restart.
1373 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321374 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571375 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321376 done = true;
[email protected]68873ba2009-06-04 21:49:231377 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311378 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571379 done = true;
[email protected]2d2697f92009-02-18 21:00:321380 }
1381
1382 if (done) {
1383 DidDrainBodyForAuthRestart(keep_alive);
1384 } else {
1385 // Keep draining.
1386 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1387 }
1388
1389 return OK;
1390}
1391
[email protected]5e363962009-06-19 19:57:011392int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381393 // There are two paths through which the server can request a certificate
1394 // from us. The first is during the initial handshake, the second is
1395 // during SSL renegotiation.
1396 //
1397 // In both cases, we want to close the connection before proceeding.
1398 // We do this for two reasons:
1399 // First, we don't want to keep the connection to the server hung for a
1400 // long time while the user selects a certificate.
1401 // Second, even if we did keep the connection open, NSS has a bug where
1402 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581403 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381404
1405 if (stream_.get()) {
1406 // Since we already have a stream, we're being called as part of SSL
1407 // renegotiation.
1408 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191409 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221410 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381411 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121412 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381413 }
1414
[email protected]26816882010-10-14 18:03:091415 // The server is asking for a client certificate during the initial
1416 // handshake.
1417 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011418
[email protected]ec229bc92010-11-22 09:51:451419 // If the user selected one of the certificates in client_certs or declined
1420 // to provide one for this server before, use the past decision
1421 // automatically.
1422 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541423 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451424 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541425 response_.cert_request_info->host_and_port, &client_cert,
1426 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451427 if (!found_cached_cert)
1428 return error;
1429
1430 // Check that the certificate selected is still a certificate the server
1431 // is likely to accept, based on the criteria supplied in the
1432 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501433 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261434 const std::vector<std::string>& cert_authorities =
1435 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451436
[email protected]f1958c382013-02-07 00:15:261437 bool cert_still_valid = cert_authorities.empty() ||
1438 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451439 if (!cert_still_valid)
1440 return error;
[email protected]5e363962009-06-19 19:57:011441 }
[email protected]ec229bc92010-11-22 09:51:451442
1443 // TODO(davidben): Add a unit test which covers this path; we need to be
1444 // able to send a legitimate certificate and also bypass/clear the
1445 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141446 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1447 &proxy_ssl_config_ : &server_ssl_config_;
1448 ssl_config->send_client_cert = true;
1449 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541450 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451451 next_state_ = STATE_CREATE_STREAM;
1452 // Reset the other member variables.
1453 // Note: this is necessary only with SSL renegotiation.
1454 ResetStateForRestart();
1455 return OK;
[email protected]0b45559b2009-06-12 21:45:111456}
1457
bncfacdd852015-01-09 19:22:541458int HttpNetworkTransaction::HandleHttp11Required(int error) {
1459 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1460 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1461
1462 if (error == ERR_HTTP_1_1_REQUIRED) {
1463 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1464 } else {
1465 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1466 }
1467 ResetConnectionAndRequestForResend();
1468 return OK;
1469}
1470
[email protected]384cc73a2013-12-08 22:41:031471void HttpNetworkTransaction::HandleClientAuthError(int error) {
1472 if (server_ssl_config_.send_client_cert &&
1473 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1474 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411475 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031476 }
1477}
1478
[email protected]bd0b6772011-01-11 19:59:301479// TODO(rch): This does not correctly handle errors when an SSL proxy is
1480// being used, as all of the errors are handled as if they were generated
1481// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381482// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301483int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1484 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031485 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301486
davidbena4c9d062015-04-03 22:34:251487 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1488 // reflect servers require a deprecated cipher rather than merely prefer
1489 // it. This, however, has no security benefit until the ciphers are actually
1490 // removed.
davidben14b1a532015-10-30 16:01:091491 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251492 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1493 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1494 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001495 NetLogEventType::SSL_CIPHER_FALLBACK,
davidbena4c9d062015-04-03 22:34:251496 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091497 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251498 ResetConnectionAndRequestForResend();
1499 return OK;
1500 }
1501
initial.commit586acc5fe2008-07-26 22:42:521502 return error;
1503}
1504
[email protected]bd0b6772011-01-11 19:59:301505// This method determines whether it is safe to resend the request after an
1506// IO error. It can only be called in response to request header or body
1507// write errors or response header read errors. It should not be used in
1508// other cases, such as a Connect error.
1509int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031510 // Because the peer may request renegotiation with client authentication at
1511 // any time, check and handle client authentication errors.
1512 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301513
1514 switch (error) {
1515 // If we try to reuse a connection that the server is in the process of
1516 // closing, we may end up successfully writing out our request (or a
1517 // portion of our request) only to find a connection error when we try to
1518 // read from (or finish writing to) the socket.
1519 case ERR_CONNECTION_RESET:
1520 case ERR_CONNECTION_CLOSED:
1521 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511522 // There can be a race between the socket pool checking checking whether a
1523 // socket is still connected, receiving the FIN, and sending/reading data
1524 // on a reused socket. If we receive the FIN between the connectedness
1525 // check and writing/reading from the socket, we may first learn the socket
1526 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1527 // likely happen when trying to retrieve its IP address.
1528 // See http://crbug.com/105824 for more details.
1529 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491530 // If a socket is closed on its initial request, HttpStreamParser returns
1531 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1532 // preconnected but failed to be used before the server timed it out.
1533 case ERR_EMPTY_RESPONSE:
1534 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381535 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001536 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301537 ResetConnectionAndRequestForResend();
1538 error = OK;
1539 }
1540 break;
[email protected]8753a122011-10-16 08:05:081541 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001542 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271543 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381544 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001545 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001546 ResetConnectionAndRequestForResend();
1547 error = OK;
1548 break;
[email protected]bd0b6772011-01-11 19:59:301549 }
1550 return error;
1551}
1552
[email protected]c3b35c22008-09-27 03:19:421553void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581554 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221555 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191556 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221557 total_sent_bytes_ += stream_->GetTotalSentBytes();
1558 }
zhongyica364fbb2015-12-12 03:39:121559 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581560}
1561
1562void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251563 send_start_time_ = base::TimeTicks();
1564 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251565
[email protected]0757e7702009-03-27 04:00:221566 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421567 read_buf_ = NULL;
1568 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571569 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201570 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141571 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381572 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591573 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121574 net_error_details_.quic_broken = false;
1575 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481576 provided_token_binding_key_.reset();
1577 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121578}
1579
1580void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1581 if (stream_)
1582 stream_->PopulateNetErrorDetails(&net_error_details_);
1583 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571584}
1585
davidben701ca982015-05-18 21:21:421586void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421587 if (result != OK)
1588 return;
1589
davidbenca9d69162015-05-04 20:18:451590 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091591 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451592}
1593
[email protected]0877e3d2009-10-17 22:29:571594HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501595 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421596}
1597
[email protected]a34f61ee2014-03-18 20:59:491598bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381599 bool connection_is_proven = stream_->IsConnectionReused();
1600 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161601
[email protected]2a5c76b2008-09-25 22:15:161602 // NOTE: we resend a request only if we reused a keep-alive connection.
1603 // This automatically prevents an infinite resend loop because we'll run
1604 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381605 if (connection_is_proven && !has_received_headers)
1606 return true;
1607 return false;
[email protected]1c773ea12009-04-28 19:58:421608}
1609
1610void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381611 if (stream_.get()) {
1612 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121613 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161614 }
1615
[email protected]0877e3d2009-10-17 22:29:571616 // We need to clear request_headers_ because it contains the real request
1617 // headers, but we may need to resend the CONNECT request first to recreate
1618 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201619 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501620 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541621}
1622
[email protected]1c773ea12009-04-28 19:58:421623bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241624 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421625}
license.botbf09a502008-08-24 00:55:551626
[email protected]1c773ea12009-04-28 19:58:421627bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041628 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421629}
1630
[email protected]e772db3f2010-07-12 18:11:131631int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271632 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501633 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421634
[email protected]0877e3d2009-10-17 22:29:571635 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581636 if (status != HTTP_UNAUTHORIZED &&
1637 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421638 return OK;
[email protected]9094b602012-02-27 21:44:581639 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111640 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161641 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1642 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421643
[email protected]9094b602012-02-27 21:44:581644 // This case can trigger when an HTTPS server responds with a "Proxy
1645 // authentication required" status code through a non-authenticating
1646 // proxy.
[email protected]7a67a8152010-11-05 18:31:101647 if (!auth_controllers_[target].get())
1648 return ERR_UNEXPECTED_PROXY_AUTH;
1649
[email protected]a7ea8832010-07-12 17:54:541650 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491651 headers, response_.ssl_info,
1652 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311653 net_log_);
[email protected]228404f2010-06-24 04:31:411654 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491655 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411656
1657 scoped_refptr<AuthChallengeInfo> auth_info =
1658 auth_controllers_[target]->auth_info();
1659 if (auth_info.get())
1660 response_.auth_challenge = auth_info;
1661
[email protected]228404f2010-06-24 04:31:411662 return rv;
[email protected]f9ee6b52008-11-08 06:46:231663}
1664
[email protected]8e6441ca2010-08-19 05:56:381665bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1666 return auth_controllers_[target].get() &&
1667 auth_controllers_[target]->HaveAuth();
1668}
1669
[email protected]228404f2010-06-24 04:31:411670GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1671 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461672 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411673 if (!proxy_info_.proxy_server().is_valid() ||
1674 proxy_info_.proxy_server().is_direct()) {
1675 return GURL(); // There is no proxy server.
1676 }
[email protected]2df19bb2010-08-25 20:13:461677 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1678 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351679 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461680 }
[email protected]228404f2010-06-24 04:31:411681 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291682 if (ForWebSocketHandshake()) {
1683 const GURL& url = request_->url;
1684 url::Replacements<char> ws_to_http;
1685 if (url.SchemeIs("ws")) {
1686 ws_to_http.SetScheme("http", url::Component(0, 4));
1687 } else {
1688 DCHECK(url.SchemeIs("wss"));
1689 ws_to_http.SetScheme("https", url::Component(0, 5));
1690 }
1691 return url.ReplaceComponents(ws_to_http);
1692 }
[email protected]228404f2010-06-24 04:31:411693 return request_->url;
1694 default:
1695 return GURL();
1696 }
[email protected]c3b35c22008-09-27 03:19:421697}
1698
[email protected]831e4a32013-11-14 02:14:441699bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141700 return websocket_handshake_stream_base_create_helper_ &&
1701 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441702}
1703
ttuttle1f2d7e92015-04-28 16:17:471704void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1705 DCHECK(stream_request_);
1706
1707 // Since the transaction can restart with auth credentials, it may create a
1708 // stream more than once. Accumulate all of the connection attempts across
1709 // those streams by appending them to the vector:
1710 for (const auto& attempt : stream_request_->connection_attempts())
1711 connection_attempts_.push_back(attempt);
1712}
1713
[email protected]c3b35c22008-09-27 03:19:421714} // namespace net