blob: afe2857e052570f1f264e68904b77ba7c54fe7ae [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
danakj1fd259a02016-04-16 03:17:097#include <memory>
[email protected]2fbaecf22010-07-22 22:20:358#include <set>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]2fbaecf22010-07-22 22:20:3510#include <vector>
11
nharperb7441ef2016-01-25 23:54:1412#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4113#include "base/bind.h"
14#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4120#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4025#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5326#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3027#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3528#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5529#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1930#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2633#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.h"
[email protected]c3b35c22008-09-27 03:19:4236#include "net/http/http_auth.h"
37#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2438#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3639#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/http/http_chunked_decoder.h"
41#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5442#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1343#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4744#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2146#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5747#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5348#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5849#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3950#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3151#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2353#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3654#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0055#include "net/log/net_log_event_type.h"
tbansal2ecbbc72016-10-06 17:15:4756#include "net/proxy/proxy_server.h"
[email protected]f7984fc62009-06-22 23:26:4457#include "net/socket/client_socket_factory.h"
bnc3472afd2016-11-17 15:27:2158#include "net/socket/next_proto.h"
[email protected]a796bcec2010-03-22 17:17:2659#include "net/socket/socks_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2860#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4061#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3262#include "net/spdy/spdy_session.h"
63#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_cert_request_info.h"
65#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5466#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1467#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5468#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
initial.commit586acc5fe2008-07-26 22:42:5271namespace net {
72
[email protected]1c773ea12009-04-28 19:58:4273namespace {
74
danakj1fd259a02016-04-16 03:17:0975std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1776 const GURL* url,
77 int net_error,
78 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0979 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2580 dict->SetString("host_and_port", GetHostAndPort(*url));
81 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4882 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2583}
84
[email protected]1c773ea12009-04-28 19:58:4285} // namespace
86
initial.commit586acc5fe2008-07-26 22:42:5287//-----------------------------------------------------------------------------
88
[email protected]262eec82013-03-19 21:01:3689HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
90 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2291 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3292 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
93 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5294 session_(session),
95 request_(NULL),
[email protected]262eec82013-03-19 21:01:3696 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5797 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2098 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5299 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19100 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22101 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54102 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44103 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02104 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12105 net_error_details_() {
[email protected]3ce7df0f2010-03-03 00:30:50106}
107
[email protected]0b0bf032010-09-21 18:08:50108HttpNetworkTransaction::~HttpNetworkTransaction() {
109 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50110 // TODO(mbelshe): The stream_ should be able to compute whether or not the
111 // stream should be kept alive. No reason to compute here
112 // and pass it in.
mmenkebd84c392015-09-02 14:12:34113 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50114 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34115 } else if (stream_->IsResponseBodyComplete()) {
116 // If the response body is complete, we can just reuse the socket.
117 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50118 } else {
mmenkebd84c392015-09-02 14:12:34119 // Otherwise, we try to drain the response body.
120 HttpStream* stream = stream_.release();
121 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50122 }
123 }
[email protected]02cad5d2013-10-02 08:14:03124 if (request_ && request_->upload_data_stream)
125 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50126}
127
[email protected]684970b2009-08-14 04:54:46128int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41129 const CompletionCallback& callback,
tfarina42834112016-09-22 13:38:20130 const NetLogWithSource& net_log) {
[email protected]9e743cd2010-03-16 07:03:53131 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04132 request_ = request_info;
shivanisha0b440852016-10-18 15:48:15133 url_ = request_->url;
[email protected]96d570e42008-08-05 22:43:04134
nharper8cdb0fb2016-04-22 21:34:59135 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
136 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
137
[email protected]99ffa5a2011-10-06 04:20:19138 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14139 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19140 proxy_ssl_config_.rev_checking_enabled = false;
141 }
[email protected]6fbac162011-06-20 00:29:04142
jkarlinfb1d5172015-01-12 14:10:29143 if (request_->load_flags & LOAD_PREFETCH)
144 response_.unused_since_prefetch = true;
145
rdsmith1d343be52016-10-21 20:37:50146 next_state_ = STATE_THROTTLE;
[email protected]96d570e42008-08-05 22:43:04147 int rv = DoLoop(OK);
148 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41149 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04150 return rv;
151}
152
153int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41154 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38155 DCHECK(!stream_.get());
156 DCHECK(!stream_request_.get());
157 DCHECK_EQ(STATE_NONE, next_state_);
158
[email protected]82918cc2010-08-25 17:24:50159 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38160
[email protected]ccb40e52008-09-17 20:54:40161 int rv = DoLoop(OK);
162 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41163 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11164 return rv;
[email protected]96d570e42008-08-05 22:43:04165}
166
[email protected]0b45559b2009-06-12 21:45:11167int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54168 X509Certificate* client_cert,
169 SSLPrivateKey* client_private_key,
170 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38171 // In HandleCertificateRequest(), we always tear down existing stream
172 // requests to force a new connection. So we shouldn't have one here.
173 DCHECK(!stream_request_.get());
174 DCHECK(!stream_.get());
175 DCHECK_EQ(STATE_NONE, next_state_);
176
[email protected]102957f2011-09-02 17:10:14177 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
178 &proxy_ssl_config_ : &server_ssl_config_;
179 ssl_config->send_client_cert = true;
180 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54181 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45182 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54183 response_.cert_request_info->host_and_port, client_cert,
184 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11185 // Reset the other member variables.
186 // Note: this is necessary only with SSL renegotiation.
187 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50188 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11189 int rv = DoLoop(OK);
190 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41191 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11192 return rv;
193}
194
[email protected]49639fa2011-12-20 23:22:41195int HttpNetworkTransaction::RestartWithAuth(
196 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22197 HttpAuth::Target target = pending_auth_target_;
198 if (target == HttpAuth::AUTH_NONE) {
199 NOTREACHED();
200 return ERR_UNEXPECTED;
201 }
[email protected]0757e7702009-03-27 04:00:22202 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42203
[email protected]f3cf9802011-10-28 18:44:58204 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13205
[email protected]49639fa2011-12-20 23:22:41206 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38207
208 int rv = OK;
209 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
210 // In this case, we've gathered credentials for use with proxy
211 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50212 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38213 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59214 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54215 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58216 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54217 } else {
[email protected]8e6441ca2010-08-19 05:56:38218 // In this case, we've gathered credentials for the server or the proxy
219 // but it is not during the tunneling phase.
220 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54221 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38222 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54223 }
[email protected]c3b35c22008-09-27 03:19:42224
[email protected]c3b35c22008-09-27 03:19:42225 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41226 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42227 return rv;
[email protected]96d570e42008-08-05 22:43:04228}
229
[email protected]f9ee6b52008-11-08 06:46:23230void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
231 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38232 DCHECK(!stream_request_.get());
233
[email protected]2d2697f92009-02-18 21:00:32234 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57235 // Even if the server says the connection is keep-alive, we have to be
236 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34237 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57238 // If the response body hasn't been completely read, we need to drain
239 // it first.
[email protected]351ab642010-08-05 16:55:31240 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32241 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57242 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32243 read_buf_len_ = kDrainBodyBufferSize;
244 return;
245 }
[email protected]0877e3d2009-10-17 22:29:57246 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09247 }
248
[email protected]2d2697f92009-02-18 21:00:32249 // We don't need to drain the response body, so we act as if we had drained
250 // the response body.
251 DidDrainBodyForAuthRestart(keep_alive);
252}
253
254void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38255 DCHECK(!stream_request_.get());
256
257 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19258 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22259 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58260 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34261 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38262 // We should call connection_->set_idle_time(), but this doesn't occur
263 // often enough to be worth the trouble.
264 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39265 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38266 }
[email protected]697ef4c2010-10-14 16:38:58267
268 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46269 // Close the stream and mark it as not_reusable. Even in the
270 // keep_alive case, we've determined that the stream_ is not
271 // reusable if new_stream is NULL.
272 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58273 next_state_ = STATE_CREATE_STREAM;
274 } else {
sclittlefb249892015-09-10 21:33:22275 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19276 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22277 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58278 next_state_ = STATE_INIT_STREAM;
279 }
280 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32281 }
[email protected]f9ee6b52008-11-08 06:46:23282
283 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58284 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23285}
286
[email protected]8e6441ca2010-08-19 05:56:38287bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
288 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
289 HaveAuth(pending_auth_target_);
290}
291
[email protected]9dea9e1f2009-01-29 00:30:47292int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41293 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04294 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35295 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04296
[email protected]ad8e04a2010-11-01 04:16:27297 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38298 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04299 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29300 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04301 // bytes when establishing a tunnel because they might be controlled by
302 // an active network attacker. We don't worry about this for HTTP
303 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29304 // We reach this case when the user cancels a 407 proxy auth prompt. We
305 // also don't worry about this for an HTTPS Proxy, because the
306 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04307 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46308 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58309 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54310 LOG(WARNING) << "Blocked proxy response with status "
311 << headers->response_code() << " to CONNECT request for "
shivanisha0b440852016-10-18 15:48:15312 << GetHostAndPort(url_) << ".";
[email protected]8a1f3312010-05-25 19:25:04313 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44314 }
315
[email protected]e60e47a2010-07-14 03:37:18316 // Are we using SPDY or HTTP?
shivanisha0b440852016-10-18 15:48:15317 next_state_ = STATE_READ_BODY;
318
319 // We have reached the end of Start state machine, reset the requestinfo to
320 // null.
321 // RequestInfo is a member of the HttpTransaction's consumer and is useful
322 // only till final response headers are received. A reset will ensure that
323 // HttpRequestInfo is only used up until final response headers are received.
324 // Resetting is allowed so that the transaction can be disassociated from its
325 // creating consumer in cases where it is shared for writing to the cache.
326 // It is also safe to reset it to null at this point since upload_data_stream
327 // is also not used in the Read state machine.
328 request_ = nullptr;
[email protected]e60e47a2010-07-14 03:37:18329
[email protected]96d570e42008-08-05 22:43:04330 read_buf_ = buf;
331 read_buf_len_ = buf_len;
332
[email protected]96d570e42008-08-05 22:43:04333 int rv = DoLoop(OK);
334 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41335 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04336 return rv;
337}
338
[email protected]8cd06c02014-01-25 07:50:14339void HttpNetworkTransaction::StopCaching() {}
340
[email protected]79e1fd62013-06-20 06:50:04341bool HttpNetworkTransaction::GetFullRequestHeaders(
342 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32343 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04344 *headers = request_headers_;
345 return true;
346}
347
sclittle4de1bab92015-09-22 21:28:24348int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
349 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19350 if (stream_)
351 total_received_bytes += stream_->GetTotalReceivedBytes();
352 return total_received_bytes;
353}
354
sclittlefb249892015-09-10 21:33:22355int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
356 int64_t total_sent_bytes = total_sent_bytes_;
357 if (stream_)
358 total_sent_bytes += stream_->GetTotalSentBytes();
359 return total_sent_bytes;
360}
361
[email protected]8cd06c02014-01-25 07:50:14362void HttpNetworkTransaction::DoneReading() {}
363
[email protected]96d570e42008-08-05 22:43:04364const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55365 return &response_;
[email protected]96d570e42008-08-05 22:43:04366}
367
368LoadState HttpNetworkTransaction::GetLoadState() const {
369 // TODO(wtc): Define a new LoadState value for the
370 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
371 switch (next_state_) {
rdsmith1d343be52016-10-21 20:37:50372 case STATE_THROTTLE_COMPLETE:
373 return LOAD_STATE_THROTTLED;
[email protected]1826a402014-01-08 15:40:48374 case STATE_CREATE_STREAM:
375 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50376 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38377 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15378 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
379 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57380 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04381 return LOAD_STATE_SENDING_REQUEST;
382 case STATE_READ_HEADERS_COMPLETE:
383 return LOAD_STATE_WAITING_FOR_RESPONSE;
384 case STATE_READ_BODY_COMPLETE:
385 return LOAD_STATE_READING_RESPONSE;
386 default:
387 return LOAD_STATE_IDLE;
388 }
389}
390
[email protected]8cd06c02014-01-25 07:50:14391void HttpNetworkTransaction::SetQuicServerInfo(
392 QuicServerInfo* quic_server_info) {}
393
[email protected]5033ab82013-03-22 20:17:46394bool HttpNetworkTransaction::GetLoadTimingInfo(
395 LoadTimingInfo* load_timing_info) const {
396 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
397 return false;
398
399 load_timing_info->proxy_resolve_start =
400 proxy_info_.proxy_resolve_start_time();
401 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
402 load_timing_info->send_start = send_start_time_;
403 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46404 return true;
405}
406
ttuttled9dbc652015-09-29 20:00:59407bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36408 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59409 return false;
410
411 *endpoint = remote_endpoint_;
412 return true;
413}
414
zhongyi48704c182015-12-07 07:52:02415void HttpNetworkTransaction::PopulateNetErrorDetails(
416 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12417 *details = net_error_details_;
418 if (stream_)
419 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02420}
421
[email protected]5033ab82013-03-22 20:17:46422void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
423 priority_ = priority;
rdsmith1d343be52016-10-21 20:37:50424
425 // TODO: Note that if throttling is ever implemented below this
426 // level, either of the two below calls may result in request
427 // completion, callbacks, and the potential deletion of this object
428 // (like the call below to throttle_->SetPriority()). In that case,
429 // this code will need to be modified.
[email protected]bf828982013-08-14 18:01:47430 if (stream_request_)
431 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03432 if (stream_)
433 stream_->SetPriority(priority);
rdsmith1d343be52016-10-21 20:37:50434
435 if (throttle_)
436 throttle_->SetPriority(priority);
437 // The above call may have resulted in deleting |*this|.
[email protected]5033ab82013-03-22 20:17:46438}
439
[email protected]831e4a32013-11-14 02:14:44440void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
441 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
442 websocket_handshake_stream_base_create_helper_ = create_helper;
443}
444
[email protected]1826a402014-01-08 15:40:48445void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
446 const BeforeNetworkStartCallback& callback) {
447 before_network_start_callback_ = callback;
448}
449
ryansturm49a8cb12016-06-15 16:51:09450void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
451 const BeforeHeadersSentCallback& callback) {
452 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27453}
454
[email protected]1826a402014-01-08 15:40:48455int HttpNetworkTransaction::ResumeNetworkStart() {
456 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
457 return DoLoop(OK);
458}
459
[email protected]102e27c2011-02-23 01:01:31460void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
461 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39462 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50463 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38464 DCHECK(stream_request_.get());
465
sclittlefb249892015-09-10 21:33:22466 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19467 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22468 total_sent_bytes_ += stream_->GetTotalSentBytes();
469 }
[email protected]8e6441ca2010-08-19 05:56:38470 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14471 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31472 proxy_info_ = used_proxy_info;
bnc94c92842016-09-21 15:22:52473 response_.was_alpn_negotiated = stream_request_->was_alpn_negotiated();
bnc3472afd2016-11-17 15:27:21474 response_.alpn_negotiated_protocol =
475 NextProtoToString(stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38476 response_.was_fetched_via_spdy = stream_request_->using_spdy();
477 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15478 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
tbansal2ecbbc72016-10-06 17:15:47479 response_.proxy_server = proxy_info_.proxy_server();
480 else if (!response_.was_fetched_via_proxy && proxy_info_.is_direct())
481 response_.proxy_server = ProxyServer::Direct();
482 else
483 response_.proxy_server = ProxyServer();
[email protected]8e6441ca2010-08-19 05:56:38484 OnIOComplete(OK);
485}
486
xunjieli5749218c2016-03-22 16:43:06487void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08488 const SSLConfig& used_ssl_config,
489 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06490 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08491 NOTREACHED();
492}
493
[email protected]a9cf2b92013-10-30 12:08:49494void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50495 const SSLConfig& used_ssl_config,
496 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49497 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44498 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50499}
500
[email protected]102e27c2011-02-23 01:01:31501void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13502 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50503 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38504 DCHECK_NE(OK, result);
505 DCHECK(stream_request_.get());
506 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14507 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38508
509 OnIOComplete(result);
510}
511
[email protected]102e27c2011-02-23 01:01:31512void HttpNetworkTransaction::OnCertificateError(
513 int result,
514 const SSLConfig& used_ssl_config,
515 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50516 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38517 DCHECK_NE(OK, result);
518 DCHECK(stream_request_.get());
519 DCHECK(!stream_.get());
520
521 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14522 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38523
524 // TODO(mbelshe): For now, we're going to pass the error through, and that
525 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50526 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
527 // good and the user chooses to ignore the error. This is not ideal, but not
528 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38529
530 OnIOComplete(result);
531}
532
533void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50534 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31535 const SSLConfig& used_ssl_config,
536 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50537 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38538 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50539 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38540
541 establishing_tunnel_ = true;
542 response_.headers = proxy_response.headers;
543 response_.auth_challenge = proxy_response.auth_challenge;
544 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14545 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31546 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38547
548 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
549 pending_auth_target_ = HttpAuth::AUTH_PROXY;
550
551 DoCallback(OK);
552}
553
554void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31555 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50556 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50557 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38558
[email protected]102957f2011-09-02 17:10:14559 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38560 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58561 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38562}
563
[email protected]511f6f52010-12-17 03:58:29564void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
565 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31566 const SSLConfig& used_ssl_config,
567 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39568 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29569 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
570
ttuttle1f2d7e92015-04-28 16:17:47571 CopyConnectionAttemptsFromStreamRequest();
572
[email protected]511f6f52010-12-17 03:58:29573 headers_valid_ = true;
574 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14575 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31576 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22577 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19578 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22579 total_sent_bytes_ += stream_->GetTotalSentBytes();
580 }
[email protected]511f6f52010-12-17 03:58:29581 stream_.reset(stream);
582 stream_request_.reset(); // we're done with the stream request
583 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
584}
585
zhongyi48704c182015-12-07 07:52:02586void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12587 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02588}
589
ttuttle1f2d7e92015-04-28 16:17:47590void HttpNetworkTransaction::GetConnectionAttempts(
591 ConnectionAttempts* out) const {
592 *out = connection_attempts_;
593}
594
rdsmithbf8c3c12016-11-18 18:16:24595void HttpNetworkTransaction::OnThrottleUnblocked(
596 NetworkThrottleManager::Throttle* throttle) {
rdsmith1d343be52016-10-21 20:37:50597 // TODO(rdsmith): This DCHECK is dependent on the only transition
rdsmithbf8c3c12016-11-18 18:16:24598 // being from blocked->unblocked. That is true right now, but may not
rdsmith1d343be52016-10-21 20:37:50599 // be so in the future.
600 DCHECK_EQ(STATE_THROTTLE_COMPLETE, next_state_);
601
602 net_log_.EndEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
603
604 DoLoop(OK);
605}
606
Adam Ricecb76ac62015-02-20 05:33:25607bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37608 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52609}
610
nharperb7441ef2016-01-25 23:54:14611bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
612 if (!IsSecureRequest())
613 return false;
614 SSLInfo ssl_info;
615 stream_->GetSSLInfo(&ssl_info);
616 return ssl_info.token_binding_negotiated &&
617 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
618 session_->params().channel_id_service;
619}
620
621void HttpNetworkTransaction::RecordTokenBindingSupport() const {
622 // This enum is used for an UMA histogram - do not change or re-use values.
623 enum {
624 DISABLED = 0,
625 CLIENT_ONLY = 1,
626 CLIENT_AND_SERVER = 2,
627 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
628 TOKEN_BINDING_SUPPORT_MAX
629 } supported;
630 if (!IsSecureRequest())
631 return;
632 SSLInfo ssl_info;
633 stream_->GetSSLInfo(&ssl_info);
634 if (!session_->params().enable_token_binding) {
635 supported = DISABLED;
636 } else if (!session_->params().channel_id_service) {
637 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
638 } else if (ssl_info.token_binding_negotiated) {
639 supported = CLIENT_AND_SERVER;
640 } else {
641 supported = CLIENT_ONLY;
642 }
643 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
644 TOKEN_BINDING_SUPPORT_MAX);
645}
646
Adam Rice425cf122015-01-19 06:18:24647bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12648 return (proxy_info_.is_http() || proxy_info_.is_https() ||
649 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24650 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
651}
652
initial.commit586acc5fe2008-07-26 22:42:52653void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50654 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41655 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52656
[email protected]96d570e42008-08-05 22:43:04657 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41658 CompletionCallback c = callback_;
659 callback_.Reset();
660 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52661}
662
663void HttpNetworkTransaction::OnIOComplete(int result) {
664 int rv = DoLoop(result);
665 if (rv != ERR_IO_PENDING)
666 DoCallback(rv);
667}
668
669int HttpNetworkTransaction::DoLoop(int result) {
670 DCHECK(next_state_ != STATE_NONE);
671
672 int rv = result;
673 do {
674 State state = next_state_;
675 next_state_ = STATE_NONE;
676 switch (state) {
rdsmith1d343be52016-10-21 20:37:50677 case STATE_THROTTLE:
678 DCHECK_EQ(OK, rv);
679 rv = DoThrottle();
680 break;
681 case STATE_THROTTLE_COMPLETE:
682 DCHECK_EQ(OK, rv);
683 rv = DoThrottleComplete();
684 break;
[email protected]1826a402014-01-08 15:40:48685 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
686 DCHECK_EQ(OK, rv);
687 rv = DoNotifyBeforeCreateStream();
688 break;
[email protected]82918cc2010-08-25 17:24:50689 case STATE_CREATE_STREAM:
690 DCHECK_EQ(OK, rv);
691 rv = DoCreateStream();
692 break;
693 case STATE_CREATE_STREAM_COMPLETE:
zhongyi3aedde92016-10-14 02:29:46694 // TODO(zhongyi): remove liveness checks when crbug.com/652868 is
695 // solved.
696 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50697 rv = DoCreateStreamComplete(rv);
zhongyi3aedde92016-10-14 02:29:46698 net_log_.CrashIfInvalid();
[email protected]82918cc2010-08-25 17:24:50699 break;
[email protected]351ab642010-08-05 16:55:31700 case STATE_INIT_STREAM:
701 DCHECK_EQ(OK, rv);
702 rv = DoInitStream();
703 break;
704 case STATE_INIT_STREAM_COMPLETE:
705 rv = DoInitStreamComplete(rv);
706 break;
[email protected]044de0642010-06-17 10:42:15707 case STATE_GENERATE_PROXY_AUTH_TOKEN:
708 DCHECK_EQ(OK, rv);
709 rv = DoGenerateProxyAuthToken();
710 break;
711 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
712 rv = DoGenerateProxyAuthTokenComplete(rv);
713 break;
714 case STATE_GENERATE_SERVER_AUTH_TOKEN:
715 DCHECK_EQ(OK, rv);
716 rv = DoGenerateServerAuthToken();
717 break;
718 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
719 rv = DoGenerateServerAuthTokenComplete(rv);
720 break;
nharperd6e65822016-03-30 23:05:48721 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14722 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48723 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14724 break;
nharperd6e65822016-03-30 23:05:48725 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
726 rv = DoGetProvidedTokenBindingKeyComplete(rv);
727 break;
728 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
729 DCHECK_EQ(OK, rv);
730 rv = DoGetReferredTokenBindingKey();
731 break;
732 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
733 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14734 break;
[email protected]daddea62012-09-19 05:51:13735 case STATE_INIT_REQUEST_BODY:
736 DCHECK_EQ(OK, rv);
737 rv = DoInitRequestBody();
738 break;
739 case STATE_INIT_REQUEST_BODY_COMPLETE:
740 rv = DoInitRequestBodyComplete(rv);
741 break;
[email protected]4875ba12011-03-30 22:31:51742 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55743 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00744 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51745 rv = DoBuildRequest();
746 break;
747 case STATE_BUILD_REQUEST_COMPLETE:
748 rv = DoBuildRequestComplete(rv);
749 break;
750 case STATE_SEND_REQUEST:
751 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57752 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52753 break;
[email protected]0877e3d2009-10-17 22:29:57754 case STATE_SEND_REQUEST_COMPLETE:
755 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43756 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00757 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52758 break;
759 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55760 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00761 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52762 rv = DoReadHeaders();
763 break;
764 case STATE_READ_HEADERS_COMPLETE:
765 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43766 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00767 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52768 break;
769 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55770 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00771 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52772 rv = DoReadBody();
773 break;
774 case STATE_READ_BODY_COMPLETE:
775 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43776 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00777 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52778 break;
[email protected]2d2697f92009-02-18 21:00:32779 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55780 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53781 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00782 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32783 rv = DoDrainBodyForAuthRestart();
784 break;
785 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
786 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43787 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00788 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32789 break;
initial.commit586acc5fe2008-07-26 22:42:52790 default:
791 NOTREACHED() << "bad state";
792 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04793 break;
initial.commit586acc5fe2008-07-26 22:42:52794 }
795 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
796
797 return rv;
798}
799
rdsmith1d343be52016-10-21 20:37:50800int HttpNetworkTransaction::DoThrottle() {
rdsmithbf8c3c12016-11-18 18:16:24801 DCHECK(!throttle_);
rdsmith1d343be52016-10-21 20:37:50802 throttle_ = session_->throttler()->CreateThrottle(
803 this, priority_, (request_->load_flags & LOAD_IGNORE_LIMITS) != 0);
804 next_state_ = STATE_THROTTLE_COMPLETE;
805
rdsmithbf8c3c12016-11-18 18:16:24806 if (throttle_->IsBlocked()) {
rdsmith1d343be52016-10-21 20:37:50807 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_THROTTLED);
808 return ERR_IO_PENDING;
809 }
810
811 return OK;
812}
813
814int HttpNetworkTransaction::DoThrottleComplete() {
815 DCHECK(throttle_);
rdsmithbf8c3c12016-11-18 18:16:24816 DCHECK(!throttle_->IsBlocked());
rdsmith1d343be52016-10-21 20:37:50817
818 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
819
820 return OK;
821}
822
[email protected]1826a402014-01-08 15:40:48823int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
824 next_state_ = STATE_CREATE_STREAM;
825 bool defer = false;
826 if (!before_network_start_callback_.is_null())
827 before_network_start_callback_.Run(&defer);
828 if (!defer)
829 return OK;
830 return ERR_IO_PENDING;
831}
832
[email protected]82918cc2010-08-25 17:24:50833int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34834 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26835 tracked_objects::ScopedTracker tracking_profile(
836 FROM_HERE_WITH_EXPLICIT_FUNCTION(
837 "424359 HttpNetworkTransaction::DoCreateStream"));
838
ttuttlec0c828492015-05-15 01:25:55839 response_.network_accessed = true;
840
[email protected]82918cc2010-08-25 17:24:50841 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44842 if (ForWebSocketHandshake()) {
843 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23844 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44845 ->RequestWebSocketHandshakeStream(
846 *request_,
847 priority_,
848 server_ssl_config_,
849 proxy_ssl_config_,
850 this,
851 websocket_handshake_stream_base_create_helper_,
852 net_log_));
853 } else {
854 stream_request_.reset(
855 session_->http_stream_factory()->RequestStream(
856 *request_,
857 priority_,
858 server_ssl_config_,
859 proxy_ssl_config_,
860 this,
861 net_log_));
862 }
[email protected]26816882010-10-14 18:03:09863 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38864 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31865}
866
[email protected]82918cc2010-08-25 17:24:50867int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47868 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
869 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
870 // which resets the stream request first. Therefore, we have to grab the
871 // connection attempts in *that* function instead of here in that case.
872 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
873 CopyConnectionAttemptsFromStreamRequest();
874
davidben701ca982015-05-18 21:21:42875 if (request_->url.SchemeIsCryptographic())
876 RecordSSLFallbackMetrics(result);
877
[email protected]394816e92010-08-03 07:38:59878 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50879 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38880 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33881 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
882 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29883 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
884 // Return OK and let the caller read the proxy's error page
885 next_state_ = STATE_NONE;
886 return OK;
bncfacdd852015-01-09 19:22:54887 } else if (result == ERR_HTTP_1_1_REQUIRED ||
888 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
889 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59890 }
891
[email protected]bd0b6772011-01-11 19:59:30892 // Handle possible handshake errors that may have occurred if the stream
893 // used SSL for one or more of the layers.
894 result = HandleSSLHandshakeError(result);
895
[email protected]8e6441ca2010-08-19 05:56:38896 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09897 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38898 return result;
[email protected]394816e92010-08-03 07:38:59899}
900
[email protected]82918cc2010-08-25 17:24:50901int HttpNetworkTransaction::DoInitStream() {
902 DCHECK(stream_.get());
903 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59904
905 stream_->GetRemoteEndpoint(&remote_endpoint_);
906
[email protected]262eec82013-03-19 21:01:36907 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50908}
909
910int HttpNetworkTransaction::DoInitStreamComplete(int result) {
911 if (result == OK) {
912 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50913 } else {
[email protected]82918cc2010-08-25 17:24:50914 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26915 result = HandleIOError(result);
916
917 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22918 if (stream_) {
919 total_received_bytes_ += stream_->GetTotalReceivedBytes();
920 total_sent_bytes_ += stream_->GetTotalSentBytes();
921 }
zhongyica364fbb2015-12-12 03:39:12922 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50923 }
924
925 return result;
926}
927
[email protected]044de0642010-06-17 10:42:15928int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
929 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
930 if (!ShouldApplyProxyAuth())
931 return OK;
[email protected]394816e92010-08-03 07:38:59932 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
933 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09934 auth_controllers_[target] =
935 new HttpAuthController(target,
936 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31937 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09938 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59939 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41940 io_callback_,
[email protected]394816e92010-08-03 07:38:59941 net_log_);
[email protected]044de0642010-06-17 10:42:15942}
943
944int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
945 DCHECK_NE(ERR_IO_PENDING, rv);
946 if (rv == OK)
947 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
948 return rv;
949}
950
951int HttpNetworkTransaction::DoGenerateServerAuthToken() {
952 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59953 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54954 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09955 auth_controllers_[target] =
956 new HttpAuthController(target,
957 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31958 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09959 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54960 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
961 auth_controllers_[target]->DisableEmbeddedIdentity();
962 }
[email protected]044de0642010-06-17 10:42:15963 if (!ShouldApplyServerAuth())
964 return OK;
[email protected]394816e92010-08-03 07:38:59965 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41966 io_callback_,
[email protected]394816e92010-08-03 07:38:59967 net_log_);
[email protected]044de0642010-06-17 10:42:15968}
969
970int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
971 DCHECK_NE(ERR_IO_PENDING, rv);
972 if (rv == OK)
nharperd6e65822016-03-30 23:05:48973 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15974 return rv;
975}
976
nharperd6e65822016-03-30 23:05:48977int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
978 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14979 if (!IsTokenBindingEnabled())
980 return OK;
981
mikecirone8b85c432016-09-08 19:11:00982 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperb7441ef2016-01-25 23:54:14983 ChannelIDService* channel_id_service = session_->params().channel_id_service;
984 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48985 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14986 &token_binding_request_);
987}
988
nharperd6e65822016-03-30 23:05:48989int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14990 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48991 if (IsTokenBindingEnabled()) {
992 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00993 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48994 }
995
996 if (rv == OK)
997 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
998 return rv;
999}
1000
1001int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
1002 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
1003 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:141004 return OK;
1005
mikecirone8b85c432016-09-08 19:11:001006 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperd6e65822016-03-30 23:05:481007 ChannelIDService* channel_id_service = session_->params().channel_id_service;
1008 return channel_id_service->GetOrCreateChannelID(
1009 request_->token_binding_referrer, &referred_token_binding_key_,
1010 io_callback_, &token_binding_request_);
1011}
1012
1013int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
1014 DCHECK_NE(ERR_IO_PENDING, rv);
1015 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
1016 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001017 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:481018 }
1019 if (rv == OK)
1020 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:141021 return rv;
1022}
1023
1024int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:241025 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141026 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1027 GetHostAndOptionalPort(request_->url));
1028
1029 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241030 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141031 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1032 "keep-alive");
1033 } else {
1034 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1035 }
1036
[email protected]2979a492011-04-06 00:29:141037 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111038 if (request_->upload_data_stream) {
1039 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141040 request_headers_.SetHeader(
1041 HttpRequestHeaders::kTransferEncoding, "chunked");
1042 } else {
1043 request_headers_.SetHeader(
1044 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111045 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141046 }
csharrisonf473dd192015-08-18 13:54:131047 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141048 // An empty POST/PUT request still needs a content length. As for HEAD,
1049 // IE and Safari also add a content length header. Presumably it is to
1050 // support sending a HEAD request to an URL that only expects to be sent a
1051 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131052 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1053 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141054 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1055 }
1056
nharperb7441ef2016-01-25 23:54:141057 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:481058 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:141059 std::string token_binding_header;
1060 int rv = BuildTokenBindingHeader(&token_binding_header);
1061 if (rv != OK)
1062 return rv;
1063 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1064 token_binding_header);
1065 }
1066
[email protected]2979a492011-04-06 00:29:141067 // Honor load flags that impact proxy caches.
1068 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1069 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1070 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1071 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1072 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1073 }
1074
1075 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1076 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1077 &request_headers_);
1078 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1079 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1080 &request_headers_);
1081
[email protected]c10450102011-06-27 09:06:161082 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201083
ryansturm49a8cb12016-06-15 16:51:091084 if (!before_headers_sent_callback_.is_null())
1085 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201086
[email protected]173f8e22013-04-10 04:18:201087 response_.did_use_http_auth =
1088 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1089 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141090 return OK;
1091}
1092
1093int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471094 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141095 std::vector<uint8_t> signed_ekm;
nharper78e6d2b2016-09-21 05:42:351096 int rv = stream_->GetTokenBindingSignature(provided_token_binding_key_.get(),
1097 TokenBindingType::PROVIDED,
1098 &signed_ekm);
nharperb7441ef2016-01-25 23:54:141099 if (rv != OK)
1100 return rv;
1101 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481102 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1103 provided_token_binding_key_.get(), signed_ekm,
1104 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141105 if (rv != OK)
1106 return rv;
nharperd6e65822016-03-30 23:05:481107
nharperb7441ef2016-01-25 23:54:141108 std::vector<base::StringPiece> token_bindings;
1109 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481110
1111 std::string referred_token_binding;
1112 if (referred_token_binding_key_) {
1113 std::vector<uint8_t> referred_signed_ekm;
nharper78e6d2b2016-09-21 05:42:351114 int rv = stream_->GetTokenBindingSignature(
1115 referred_token_binding_key_.get(), TokenBindingType::REFERRED,
1116 &referred_signed_ekm);
nharperd6e65822016-03-30 23:05:481117 if (rv != OK)
1118 return rv;
1119 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1120 referred_token_binding_key_.get(),
1121 referred_signed_ekm, &referred_token_binding);
1122 if (rv != OK)
1123 return rv;
1124 token_bindings.push_back(referred_token_binding);
1125 }
nharperb7441ef2016-01-25 23:54:141126 std::string header;
1127 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1128 if (rv != OK)
1129 return rv;
nharper78e6d2b2016-09-21 05:42:351130 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::OMIT_PADDING, out);
nharpercb66ad0202016-03-11 18:17:471131 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1132 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1133 header_creation_time,
1134 base::TimeDelta::FromMilliseconds(1),
1135 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141136 return OK;
[email protected]2979a492011-04-06 00:29:141137}
1138
[email protected]daddea62012-09-19 05:51:131139int HttpNetworkTransaction::DoInitRequestBody() {
1140 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131141 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111142 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521143 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131144 return rv;
1145}
[email protected]4875ba12011-03-30 22:31:511146
[email protected]daddea62012-09-19 05:51:131147int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1148 if (result == OK)
1149 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131150 return result;
1151}
1152
1153int HttpNetworkTransaction::DoBuildRequest() {
1154 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511155 headers_valid_ = false;
1156
1157 // This is constructed lazily (instead of within our Start method), so that
1158 // we have proxy info available.
1159 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241160 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141161 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511162 }
1163
[email protected]4875ba12011-03-30 22:31:511164 return OK;
1165}
1166
1167int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241168 if (result == OK)
1169 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511170 return result;
1171}
1172
[email protected]0877e3d2009-10-17 22:29:571173int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341174 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261175 tracked_objects::ScopedTracker tracking_profile(
1176 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1177 "424359 HttpNetworkTransaction::DoSendRequest"));
1178
[email protected]58e32bb2013-01-21 18:23:251179 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571180 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1181
[email protected]bf3eb002012-11-15 05:50:111182 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521183}
1184
[email protected]0877e3d2009-10-17 22:29:571185int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251186 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361187
1188 if (result == ERR_HTTP_1_1_REQUIRED ||
1189 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1190 return HandleHttp11Required(result);
1191 }
1192
initial.commit586acc5fe2008-07-26 22:42:521193 if (result < 0)
1194 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571195 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521196 return OK;
1197}
1198
1199int HttpNetworkTransaction::DoReadHeaders() {
1200 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411201 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521202}
1203
1204int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111205 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1206 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381207 if (IsCertificateError(result)) {
1208 // We don't handle a certificate error during SSL renegotiation, so we
1209 // have to return an error that's not in the certificate error range
1210 // (-2xx).
1211 LOG(ERROR) << "Got a server certificate with error " << result
1212 << " during SSL renegotiation";
1213 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1214 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1215 // TODO(wtc): Need a test case for this code path!
1216 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251217 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381218 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501219 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381220 result = HandleCertificateRequest(result);
1221 if (result == OK)
1222 return result;
[email protected]2181ea002009-06-09 01:37:271223 }
1224
bncfacdd852015-01-09 19:22:541225 if (result == ERR_HTTP_1_1_REQUIRED ||
1226 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1227 return HandleHttp11Required(result);
1228 }
1229
[email protected]c871864d72014-03-13 19:56:191230 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1231 // response headers were received, we do the best we can to make sense of it
1232 // and send it back up the stack.
1233 //
1234 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1235 // bizarre for SPDY. Assuming this logic is useful at all.
1236 // TODO(davidben): Bubble the error code up so we do not cache?
1237 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1238 result = OK;
1239
1240 if (result < 0)
1241 return HandleIOError(result);
1242
[email protected]90499482013-06-01 00:39:501243 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521244
[email protected]d58ceea82014-06-04 10:55:541245 // On a 408 response from the server ("Request Timeout") on a stale socket,
1246 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041247 // Headers can be NULL because of http://crbug.com/384554.
1248 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541249 stream_->IsConnectionReused()) {
1250 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001251 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541252 response_.headers->response_code());
1253 // This will close the socket - it would be weird to try and reuse it, even
1254 // if the server doesn't actually close it.
1255 ResetConnectionAndRequestForResend();
1256 return OK;
1257 }
1258
[email protected]93f8b562012-03-27 01:00:161259 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211260 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161261 const int response_code = response_.headers->response_code();
1262 UMA_HISTOGRAM_ENUMERATION(
1263 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1264 }
1265
[email protected]3abacd62012-06-10 20:20:321266 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001267 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321268 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391269
bncbe0f6af2015-10-15 17:49:561270 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571271 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1272 // indicates a buggy server. See:
1273 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1274 if (request_->method == "PUT")
1275 return ERR_METHOD_NOT_SUPPORTED;
1276 }
[email protected]4ddaf2502008-10-23 18:26:191277
[email protected]0877e3d2009-10-17 22:29:571278 // Check for an intermediate 100 Continue response. An origin server is
1279 // allowed to send this response even if we didn't ask for it, so we just
1280 // need to skip over it.
1281 // We treat any other 1xx in this same way (although in practice getting
1282 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441283 // Unless this is a WebSocket request, in which case we pass it on up.
1284 if (response_.headers->response_code() / 100 == 1 &&
1285 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451286 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571287 next_state_ = STATE_READ_HEADERS;
1288 return OK;
1289 }
1290
bncb26024382016-06-29 02:39:451291 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111292 session_->http_stream_factory()->ProcessAlternativeServices(
1293 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1294 }
[email protected]564b4912010-03-09 16:30:421295
asanka5ffd5d72016-03-23 16:20:491296 if (IsSecureRequest())
1297 stream_->GetSSLInfo(&response_.ssl_info);
1298
[email protected]e772db3f2010-07-12 18:11:131299 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571300 if (rv != OK)
1301 return rv;
1302
[email protected]0877e3d2009-10-17 22:29:571303 headers_valid_ = true;
1304 return OK;
initial.commit586acc5fe2008-07-26 22:42:521305}
1306
1307int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501308 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131309 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381310 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521311
1312 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501313 return stream_->ReadResponseBody(
1314 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521315}
1316
1317int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1318 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381319 bool done = false;
1320 if (result <= 0) {
1321 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521322 done = true;
[email protected]8e6441ca2010-08-19 05:56:381323 }
[email protected]9492e4a2010-02-24 00:58:461324
mmenkebd84c392015-09-02 14:12:341325 // Clean up connection if we are done.
1326 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381327 // Note: Just because IsResponseBodyComplete is true, we're not
1328 // necessarily "done". We're only "done" when it is the last
1329 // read on this HttpNetworkTransaction, which will be signified
1330 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341331 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381332 // the stream. No need to compute it here just to pass back
1333 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341334 bool keep_alive =
1335 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521336
[email protected]8e6441ca2010-08-19 05:56:381337 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011338 // Note: we don't reset the stream here. We've closed it, but we still
1339 // need it around so that callers can call methods such as
1340 // GetUploadProgress() and have them be meaningful.
1341 // TODO(mbelshe): This means we closed the stream here, and we close it
1342 // again in ~HttpNetworkTransaction. Clean that up.
1343
[email protected]8e6441ca2010-08-19 05:56:381344 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521345 }
1346
1347 // Clear these to avoid leaving around old state.
1348 read_buf_ = NULL;
1349 read_buf_len_ = 0;
1350
1351 return result;
1352}
1353
[email protected]2d2697f92009-02-18 21:00:321354int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1355 // This method differs from DoReadBody only in the next_state_. So we just
1356 // call DoReadBody and override the next_state_. Perhaps there is a more
1357 // elegant way for these two methods to share code.
1358 int rv = DoReadBody();
1359 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1360 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1361 return rv;
1362}
1363
[email protected]0877e3d2009-10-17 22:29:571364// TODO(wtc): This method and the DoReadBodyComplete method are almost
1365// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321366int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231367 // keep_alive defaults to true because the very reason we're draining the
1368 // response body is to reuse the connection for auth restart.
1369 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321370 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571371 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321372 done = true;
[email protected]68873ba2009-06-04 21:49:231373 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311374 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571375 done = true;
[email protected]2d2697f92009-02-18 21:00:321376 }
1377
1378 if (done) {
1379 DidDrainBodyForAuthRestart(keep_alive);
1380 } else {
1381 // Keep draining.
1382 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1383 }
1384
1385 return OK;
1386}
1387
[email protected]5e363962009-06-19 19:57:011388int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381389 // There are two paths through which the server can request a certificate
1390 // from us. The first is during the initial handshake, the second is
1391 // during SSL renegotiation.
1392 //
1393 // In both cases, we want to close the connection before proceeding.
1394 // We do this for two reasons:
1395 // First, we don't want to keep the connection to the server hung for a
1396 // long time while the user selects a certificate.
1397 // Second, even if we did keep the connection open, NSS has a bug where
1398 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581399 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381400
1401 if (stream_.get()) {
1402 // Since we already have a stream, we're being called as part of SSL
1403 // renegotiation.
1404 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191405 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221406 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381407 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121408 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381409 }
1410
[email protected]26816882010-10-14 18:03:091411 // The server is asking for a client certificate during the initial
1412 // handshake.
1413 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011414
[email protected]ec229bc92010-11-22 09:51:451415 // If the user selected one of the certificates in client_certs or declined
1416 // to provide one for this server before, use the past decision
1417 // automatically.
1418 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541419 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451420 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541421 response_.cert_request_info->host_and_port, &client_cert,
1422 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451423 if (!found_cached_cert)
1424 return error;
1425
1426 // Check that the certificate selected is still a certificate the server
1427 // is likely to accept, based on the criteria supplied in the
1428 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501429 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261430 const std::vector<std::string>& cert_authorities =
1431 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451432
[email protected]f1958c382013-02-07 00:15:261433 bool cert_still_valid = cert_authorities.empty() ||
1434 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451435 if (!cert_still_valid)
1436 return error;
[email protected]5e363962009-06-19 19:57:011437 }
[email protected]ec229bc92010-11-22 09:51:451438
1439 // TODO(davidben): Add a unit test which covers this path; we need to be
1440 // able to send a legitimate certificate and also bypass/clear the
1441 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141442 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1443 &proxy_ssl_config_ : &server_ssl_config_;
1444 ssl_config->send_client_cert = true;
1445 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541446 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451447 next_state_ = STATE_CREATE_STREAM;
1448 // Reset the other member variables.
1449 // Note: this is necessary only with SSL renegotiation.
1450 ResetStateForRestart();
1451 return OK;
[email protected]0b45559b2009-06-12 21:45:111452}
1453
bncfacdd852015-01-09 19:22:541454int HttpNetworkTransaction::HandleHttp11Required(int error) {
1455 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1456 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1457
1458 if (error == ERR_HTTP_1_1_REQUIRED) {
1459 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1460 } else {
1461 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1462 }
1463 ResetConnectionAndRequestForResend();
1464 return OK;
1465}
1466
[email protected]384cc73a2013-12-08 22:41:031467void HttpNetworkTransaction::HandleClientAuthError(int error) {
1468 if (server_ssl_config_.send_client_cert &&
1469 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1470 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411471 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031472 }
1473}
1474
[email protected]bd0b6772011-01-11 19:59:301475// TODO(rch): This does not correctly handle errors when an SSL proxy is
1476// being used, as all of the errors are handled as if they were generated
1477// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381478// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301479int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1480 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031481 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301482
davidbena4c9d062015-04-03 22:34:251483 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1484 // reflect servers require a deprecated cipher rather than merely prefer
1485 // it. This, however, has no security benefit until the ciphers are actually
1486 // removed.
davidben14b1a532015-10-30 16:01:091487 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251488 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1489 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1490 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001491 NetLogEventType::SSL_CIPHER_FALLBACK,
davidbena4c9d062015-04-03 22:34:251492 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091493 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251494 ResetConnectionAndRequestForResend();
1495 return OK;
1496 }
1497
initial.commit586acc5fe2008-07-26 22:42:521498 return error;
1499}
1500
[email protected]bd0b6772011-01-11 19:59:301501// This method determines whether it is safe to resend the request after an
1502// IO error. It can only be called in response to request header or body
1503// write errors or response header read errors. It should not be used in
1504// other cases, such as a Connect error.
1505int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031506 // Because the peer may request renegotiation with client authentication at
1507 // any time, check and handle client authentication errors.
1508 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301509
1510 switch (error) {
1511 // If we try to reuse a connection that the server is in the process of
1512 // closing, we may end up successfully writing out our request (or a
1513 // portion of our request) only to find a connection error when we try to
1514 // read from (or finish writing to) the socket.
1515 case ERR_CONNECTION_RESET:
1516 case ERR_CONNECTION_CLOSED:
1517 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511518 // There can be a race between the socket pool checking checking whether a
1519 // socket is still connected, receiving the FIN, and sending/reading data
1520 // on a reused socket. If we receive the FIN between the connectedness
1521 // check and writing/reading from the socket, we may first learn the socket
1522 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1523 // likely happen when trying to retrieve its IP address.
1524 // See http://crbug.com/105824 for more details.
1525 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491526 // If a socket is closed on its initial request, HttpStreamParser returns
1527 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1528 // preconnected but failed to be used before the server timed it out.
1529 case ERR_EMPTY_RESPONSE:
1530 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381531 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001532 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301533 ResetConnectionAndRequestForResend();
1534 error = OK;
1535 }
1536 break;
[email protected]8753a122011-10-16 08:05:081537 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001538 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271539 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381540 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001541 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001542 ResetConnectionAndRequestForResend();
1543 error = OK;
1544 break;
[email protected]bd0b6772011-01-11 19:59:301545 }
1546 return error;
1547}
1548
[email protected]c3b35c22008-09-27 03:19:421549void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581550 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221551 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191552 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221553 total_sent_bytes_ += stream_->GetTotalSentBytes();
1554 }
zhongyica364fbb2015-12-12 03:39:121555 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581556}
1557
1558void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251559 send_start_time_ = base::TimeTicks();
1560 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251561
[email protected]0757e7702009-03-27 04:00:221562 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421563 read_buf_ = NULL;
1564 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571565 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201566 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141567 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381568 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591569 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121570 net_error_details_.quic_broken = false;
1571 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481572 provided_token_binding_key_.reset();
1573 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121574}
1575
1576void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1577 if (stream_)
1578 stream_->PopulateNetErrorDetails(&net_error_details_);
1579 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571580}
1581
davidben701ca982015-05-18 21:21:421582void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421583 if (result != OK)
1584 return;
1585
davidbenca9d69162015-05-04 20:18:451586 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091587 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451588}
1589
[email protected]0877e3d2009-10-17 22:29:571590HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501591 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421592}
1593
[email protected]a34f61ee2014-03-18 20:59:491594bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381595 bool connection_is_proven = stream_->IsConnectionReused();
1596 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161597
[email protected]2a5c76b2008-09-25 22:15:161598 // NOTE: we resend a request only if we reused a keep-alive connection.
1599 // This automatically prevents an infinite resend loop because we'll run
1600 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381601 if (connection_is_proven && !has_received_headers)
1602 return true;
1603 return false;
[email protected]1c773ea12009-04-28 19:58:421604}
1605
1606void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381607 if (stream_.get()) {
1608 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121609 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161610 }
1611
[email protected]0877e3d2009-10-17 22:29:571612 // We need to clear request_headers_ because it contains the real request
1613 // headers, but we may need to resend the CONNECT request first to recreate
1614 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201615 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501616 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541617}
1618
[email protected]1c773ea12009-04-28 19:58:421619bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241620 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421621}
license.botbf09a502008-08-24 00:55:551622
[email protected]1c773ea12009-04-28 19:58:421623bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041624 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421625}
1626
[email protected]e772db3f2010-07-12 18:11:131627int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271628 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501629 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421630
[email protected]0877e3d2009-10-17 22:29:571631 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581632 if (status != HTTP_UNAUTHORIZED &&
1633 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421634 return OK;
[email protected]9094b602012-02-27 21:44:581635 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111636 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161637 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1638 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421639
[email protected]9094b602012-02-27 21:44:581640 // This case can trigger when an HTTPS server responds with a "Proxy
1641 // authentication required" status code through a non-authenticating
1642 // proxy.
[email protected]7a67a8152010-11-05 18:31:101643 if (!auth_controllers_[target].get())
1644 return ERR_UNEXPECTED_PROXY_AUTH;
1645
[email protected]a7ea8832010-07-12 17:54:541646 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491647 headers, response_.ssl_info,
1648 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311649 net_log_);
[email protected]228404f2010-06-24 04:31:411650 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491651 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411652
1653 scoped_refptr<AuthChallengeInfo> auth_info =
1654 auth_controllers_[target]->auth_info();
1655 if (auth_info.get())
1656 response_.auth_challenge = auth_info;
1657
[email protected]228404f2010-06-24 04:31:411658 return rv;
[email protected]f9ee6b52008-11-08 06:46:231659}
1660
[email protected]8e6441ca2010-08-19 05:56:381661bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1662 return auth_controllers_[target].get() &&
1663 auth_controllers_[target]->HaveAuth();
1664}
1665
[email protected]228404f2010-06-24 04:31:411666GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1667 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461668 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411669 if (!proxy_info_.proxy_server().is_valid() ||
1670 proxy_info_.proxy_server().is_direct()) {
1671 return GURL(); // There is no proxy server.
1672 }
[email protected]2df19bb2010-08-25 20:13:461673 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1674 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351675 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461676 }
[email protected]228404f2010-06-24 04:31:411677 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291678 if (ForWebSocketHandshake()) {
1679 const GURL& url = request_->url;
1680 url::Replacements<char> ws_to_http;
1681 if (url.SchemeIs("ws")) {
1682 ws_to_http.SetScheme("http", url::Component(0, 4));
1683 } else {
1684 DCHECK(url.SchemeIs("wss"));
1685 ws_to_http.SetScheme("https", url::Component(0, 5));
1686 }
1687 return url.ReplaceComponents(ws_to_http);
1688 }
[email protected]228404f2010-06-24 04:31:411689 return request_->url;
1690 default:
1691 return GURL();
1692 }
[email protected]c3b35c22008-09-27 03:19:421693}
1694
[email protected]831e4a32013-11-14 02:14:441695bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141696 return websocket_handshake_stream_base_create_helper_ &&
1697 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441698}
1699
ttuttle1f2d7e92015-04-28 16:17:471700void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1701 DCHECK(stream_request_);
1702
1703 // Since the transaction can restart with auth credentials, it may create a
1704 // stream more than once. Accumulate all of the connection attempts across
1705 // those streams by appending them to the vector:
1706 for (const auto& attempt : stream_request_->connection_attempts())
1707 connection_attempts_.push_back(attempt);
1708}
1709
[email protected]c3b35c22008-09-27 03:19:421710} // namespace net