blob: 14da6c704043fa978c227136eaa45191ce371729 [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]2fbaecf22010-07-22 22:20:359#include <vector>
10
nharperb7441ef2016-01-25 23:54:1411#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4112#include "base/bind.h"
13#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3014#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4715#include "base/format_macros.h"
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.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"
[email protected]f7984fc62009-06-22 23:26:4455#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2656#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4457#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1858#include "net/socket/ssl_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
bncc958faa2015-07-31 18:14:5274void ProcessAlternativeServices(HttpNetworkSession* session,
75 const HttpResponseHeaders& headers,
76 const HostPortPair& http_host_port_pair) {
rch3f4b8452016-02-23 16:59:3277 if (session->params().parse_alternative_services) {
78 if (headers.HasHeader(kAlternativeServiceHeader)) {
79 std::string alternative_service_str;
80 headers.GetNormalizedHeader(kAlternativeServiceHeader,
81 &alternative_service_str);
82 session->http_stream_factory()->ProcessAlternativeService(
83 session->http_server_properties(), alternative_service_str,
84 http_host_port_pair, *session);
85 }
86 // If "Alt-Svc" is enabled, then ignore "Alternate-Protocol".
bncc958faa2015-07-31 18:14:5287 return;
88 }
89
[email protected]287d9412014-07-08 23:01:0090 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3891 return;
[email protected]287d9412014-07-08 23:01:0092
93 std::vector<std::string> alternate_protocol_values;
olli.raulaee489a52016-01-25 08:37:1094 size_t iter = 0;
[email protected]287d9412014-07-08 23:01:0095 std::string alternate_protocol_str;
96 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
97 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5298 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
99 &alternate_protocol_str);
100 if (!alternate_protocol_str.empty()) {
101 alternate_protocol_values.push_back(alternate_protocol_str);
102 }
[email protected]564b4912010-03-09 16:30:42103 }
104
[email protected]d7599122014-05-24 03:37:23105 session->http_stream_factory()->ProcessAlternateProtocol(
106 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:00107 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:23108 http_host_port_pair,
109 *session);
[email protected]f45c1ee2010-08-03 00:54:30110}
111
estade5e5529d2015-05-21 20:59:11112scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
[email protected]ea5ef4c2013-06-13 22:50:27113 const GURL* url,
114 int net_error,
davidbenf2eaaf92015-05-15 22:18:42115 SSLFailureState ssl_failure_state,
Avi Drissman13fc8932015-12-20 04:40:46116 uint16_t version_before,
117 uint16_t version_after,
eroman001c3742015-04-23 03:11:17118 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11119 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b6754252012-06-13 23:14:38120 dict->SetString("host_and_port", GetHostAndPort(*url));
121 dict->SetInteger("net_error", net_error);
davidbenf2eaaf92015-05-15 22:18:42122 dict->SetInteger("ssl_failure_state", ssl_failure_state);
[email protected]b6754252012-06-13 23:14:38123 dict->SetInteger("version_before", version_before);
124 dict->SetInteger("version_after", version_after);
dchengc7eeda422015-12-26 03:56:48125 return std::move(dict);
[email protected]b6754252012-06-13 23:14:38126}
[email protected]db74b0102012-05-31 19:55:53127
estade5e5529d2015-05-21 20:59:11128scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:17129 const GURL* url,
130 int net_error,
131 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11132 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:25133 dict->SetString("host_and_port", GetHostAndPort(*url));
134 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:48135 return std::move(dict);
davidbena4c9d062015-04-03 22:34:25136}
137
[email protected]1c773ea12009-04-28 19:58:42138} // namespace
139
initial.commit586acc5fe2008-07-26 22:42:52140//-----------------------------------------------------------------------------
141
[email protected]262eec82013-03-19 21:01:36142HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
143 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22144 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32145 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
146 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52147 session_(session),
148 request_(NULL),
[email protected]262eec82013-03-19 21:01:36149 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57150 headers_valid_(false),
davidbenf2eaaf92015-05-15 22:18:42151 server_ssl_failure_state_(SSL_FAILURE_NONE),
[email protected]a53e4d12013-12-07 16:37:24152 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
davidbenf2eaaf92015-05-15 22:18:42153 fallback_failure_state_(SSL_FAILURE_NONE),
[email protected]b94f92d2010-10-27 16:45:20154 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52155 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19156 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22157 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54158 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44159 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02160 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12161 net_error_details_() {
[email protected]102957f2011-09-02 17:10:14162 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
bnc1f295372015-10-21 23:24:22163 session->GetAlpnProtos(&server_ssl_config_.alpn_protos);
164 session->GetNpnProtos(&server_ssl_config_.npn_protos);
[email protected]99ffa5a2011-10-06 04:20:19165 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50166}
167
[email protected]0b0bf032010-09-21 18:08:50168HttpNetworkTransaction::~HttpNetworkTransaction() {
169 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50170 // TODO(mbelshe): The stream_ should be able to compute whether or not the
171 // stream should be kept alive. No reason to compute here
172 // and pass it in.
mmenkebd84c392015-09-02 14:12:34173 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50174 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34175 } else if (stream_->IsResponseBodyComplete()) {
176 // If the response body is complete, we can just reuse the socket.
177 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50178 } else {
mmenkebd84c392015-09-02 14:12:34179 // Otherwise, we try to drain the response body.
180 HttpStream* stream = stream_.release();
181 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50182 }
183 }
[email protected]02cad5d2013-10-02 08:14:03184
185 if (request_ && request_->upload_data_stream)
186 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50187}
188
[email protected]684970b2009-08-14 04:54:46189int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41190 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53191 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53192 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04193 request_ = request_info;
194
[email protected]99ffa5a2011-10-06 04:20:19195 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14196 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19197 proxy_ssl_config_.rev_checking_enabled = false;
198 }
[email protected]6fbac162011-06-20 00:29:04199
jkarlinfb1d5172015-01-12 14:10:29200 if (request_->load_flags & LOAD_PREFETCH)
201 response_.unused_since_prefetch = true;
202
[email protected]1ea4f46a2014-04-22 22:33:56203 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49204 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
205 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40206
nharperb7441ef2016-01-25 23:54:14207 if (session_->params().enable_token_binding &&
208 session_->params().channel_id_service) {
209 server_ssl_config_.token_binding_params.push_back(TB_PARAM_ECDSAP256);
210 }
211
[email protected]1826a402014-01-08 15:40:48212 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04213 int rv = DoLoop(OK);
214 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41215 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04216 return rv;
217}
218
219int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41220 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38221 DCHECK(!stream_.get());
222 DCHECK(!stream_request_.get());
223 DCHECK_EQ(STATE_NONE, next_state_);
224
[email protected]82918cc2010-08-25 17:24:50225 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38226
[email protected]ccb40e52008-09-17 20:54:40227 int rv = DoLoop(OK);
228 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41229 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11230 return rv;
[email protected]96d570e42008-08-05 22:43:04231}
232
[email protected]0b45559b2009-06-12 21:45:11233int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54234 X509Certificate* client_cert,
235 SSLPrivateKey* client_private_key,
236 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38237 // In HandleCertificateRequest(), we always tear down existing stream
238 // requests to force a new connection. So we shouldn't have one here.
239 DCHECK(!stream_request_.get());
240 DCHECK(!stream_.get());
241 DCHECK_EQ(STATE_NONE, next_state_);
242
[email protected]102957f2011-09-02 17:10:14243 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
244 &proxy_ssl_config_ : &server_ssl_config_;
245 ssl_config->send_client_cert = true;
246 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54247 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45248 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54249 response_.cert_request_info->host_and_port, client_cert,
250 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11251 // Reset the other member variables.
252 // Note: this is necessary only with SSL renegotiation.
253 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50254 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11255 int rv = DoLoop(OK);
256 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41257 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11258 return rv;
259}
260
[email protected]49639fa2011-12-20 23:22:41261int HttpNetworkTransaction::RestartWithAuth(
262 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22263 HttpAuth::Target target = pending_auth_target_;
264 if (target == HttpAuth::AUTH_NONE) {
265 NOTREACHED();
266 return ERR_UNEXPECTED;
267 }
[email protected]0757e7702009-03-27 04:00:22268 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42269
[email protected]f3cf9802011-10-28 18:44:58270 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13271
[email protected]49639fa2011-12-20 23:22:41272 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38273
274 int rv = OK;
275 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
276 // In this case, we've gathered credentials for use with proxy
277 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50278 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38279 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59280 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54281 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58282 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54283 } else {
[email protected]8e6441ca2010-08-19 05:56:38284 // In this case, we've gathered credentials for the server or the proxy
285 // but it is not during the tunneling phase.
286 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54287 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38288 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54289 }
[email protected]c3b35c22008-09-27 03:19:42290
[email protected]c3b35c22008-09-27 03:19:42291 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41292 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42293 return rv;
[email protected]96d570e42008-08-05 22:43:04294}
295
[email protected]f9ee6b52008-11-08 06:46:23296void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
297 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38298 DCHECK(!stream_request_.get());
299
[email protected]2d2697f92009-02-18 21:00:32300 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57301 // Even if the server says the connection is keep-alive, we have to be
302 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34303 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57304 // If the response body hasn't been completely read, we need to drain
305 // it first.
[email protected]351ab642010-08-05 16:55:31306 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32307 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57308 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32309 read_buf_len_ = kDrainBodyBufferSize;
310 return;
311 }
[email protected]0877e3d2009-10-17 22:29:57312 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09313 }
314
[email protected]2d2697f92009-02-18 21:00:32315 // We don't need to drain the response body, so we act as if we had drained
316 // the response body.
317 DidDrainBodyForAuthRestart(keep_alive);
318}
319
320void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38321 DCHECK(!stream_request_.get());
322
323 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19324 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22325 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58326 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34327 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38328 // We should call connection_->set_idle_time(), but this doesn't occur
329 // often enough to be worth the trouble.
330 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39331 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38332 }
[email protected]697ef4c2010-10-14 16:38:58333
334 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46335 // Close the stream and mark it as not_reusable. Even in the
336 // keep_alive case, we've determined that the stream_ is not
337 // reusable if new_stream is NULL.
338 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58339 next_state_ = STATE_CREATE_STREAM;
340 } else {
sclittlefb249892015-09-10 21:33:22341 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19342 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22343 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58344 next_state_ = STATE_INIT_STREAM;
345 }
346 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32347 }
[email protected]f9ee6b52008-11-08 06:46:23348
349 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58350 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23351}
352
[email protected]8e6441ca2010-08-19 05:56:38353bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
354 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
355 HaveAuth(pending_auth_target_);
356}
357
[email protected]9dea9e1f2009-01-29 00:30:47358int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41359 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04360 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35361 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04362
[email protected]1f14a912009-12-21 20:32:44363 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04364
[email protected]ad8e04a2010-11-01 04:16:27365 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38366 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04367 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29368 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04369 // bytes when establishing a tunnel because they might be controlled by
370 // an active network attacker. We don't worry about this for HTTP
371 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29372 // We reach this case when the user cancels a 407 proxy auth prompt. We
373 // also don't worry about this for an HTTPS Proxy, because the
374 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04375 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46376 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58377 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54378 LOG(WARNING) << "Blocked proxy response with status "
379 << headers->response_code() << " to CONNECT request for "
380 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04381 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44382 }
383
[email protected]e60e47a2010-07-14 03:37:18384 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31385 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18386
[email protected]96d570e42008-08-05 22:43:04387 read_buf_ = buf;
388 read_buf_len_ = buf_len;
389
[email protected]1f14a912009-12-21 20:32:44390 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04391 int rv = DoLoop(OK);
392 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41393 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04394 return rv;
395}
396
[email protected]8cd06c02014-01-25 07:50:14397void HttpNetworkTransaction::StopCaching() {}
398
[email protected]79e1fd62013-06-20 06:50:04399bool HttpNetworkTransaction::GetFullRequestHeaders(
400 HttpRequestHeaders* headers) const {
401 // TODO(ttuttle): Make sure we've populated request_headers_.
402 *headers = request_headers_;
403 return true;
404}
405
sclittle4de1bab92015-09-22 21:28:24406int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
407 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19408 if (stream_)
409 total_received_bytes += stream_->GetTotalReceivedBytes();
410 return total_received_bytes;
411}
412
sclittlefb249892015-09-10 21:33:22413int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
414 int64_t total_sent_bytes = total_sent_bytes_;
415 if (stream_)
416 total_sent_bytes += stream_->GetTotalSentBytes();
417 return total_sent_bytes;
418}
419
[email protected]8cd06c02014-01-25 07:50:14420void HttpNetworkTransaction::DoneReading() {}
421
[email protected]96d570e42008-08-05 22:43:04422const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55423 return &response_;
[email protected]96d570e42008-08-05 22:43:04424}
425
426LoadState HttpNetworkTransaction::GetLoadState() const {
427 // TODO(wtc): Define a new LoadState value for the
428 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
429 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48430 case STATE_CREATE_STREAM:
431 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50432 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38433 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15434 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
435 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57436 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04437 return LOAD_STATE_SENDING_REQUEST;
438 case STATE_READ_HEADERS_COMPLETE:
439 return LOAD_STATE_WAITING_FOR_RESPONSE;
440 case STATE_READ_BODY_COMPLETE:
441 return LOAD_STATE_READING_RESPONSE;
442 default:
443 return LOAD_STATE_IDLE;
444 }
445}
446
[email protected]196d18a2012-08-30 03:47:31447UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31448 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31449 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04450
yhiranoa7e05bb2014-11-06 05:40:39451 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04452}
453
[email protected]8cd06c02014-01-25 07:50:14454void HttpNetworkTransaction::SetQuicServerInfo(
455 QuicServerInfo* quic_server_info) {}
456
[email protected]5033ab82013-03-22 20:17:46457bool HttpNetworkTransaction::GetLoadTimingInfo(
458 LoadTimingInfo* load_timing_info) const {
459 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
460 return false;
461
462 load_timing_info->proxy_resolve_start =
463 proxy_info_.proxy_resolve_start_time();
464 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
465 load_timing_info->send_start = send_start_time_;
466 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46467 return true;
468}
469
ttuttled9dbc652015-09-29 20:00:59470bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
471 if (!remote_endpoint_.address().size())
472 return false;
473
474 *endpoint = remote_endpoint_;
475 return true;
476}
477
zhongyi48704c182015-12-07 07:52:02478void HttpNetworkTransaction::PopulateNetErrorDetails(
479 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12480 *details = net_error_details_;
481 if (stream_)
482 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02483}
484
[email protected]5033ab82013-03-22 20:17:46485void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
486 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47487 if (stream_request_)
488 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03489 if (stream_)
490 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46491}
492
[email protected]831e4a32013-11-14 02:14:44493void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
494 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
495 websocket_handshake_stream_base_create_helper_ = create_helper;
496}
497
[email protected]1826a402014-01-08 15:40:48498void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
499 const BeforeNetworkStartCallback& callback) {
500 before_network_start_callback_ = callback;
501}
502
[email protected]597a1ab2014-06-26 08:12:27503void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
504 const BeforeProxyHeadersSentCallback& callback) {
505 before_proxy_headers_sent_callback_ = callback;
506}
507
[email protected]1826a402014-01-08 15:40:48508int HttpNetworkTransaction::ResumeNetworkStart() {
509 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
510 return DoLoop(OK);
511}
512
[email protected]102e27c2011-02-23 01:01:31513void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
514 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39515 HttpStream* stream) {
[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(stream_request_.get());
518
sclittlefb249892015-09-10 21:33:22519 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19520 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22521 total_sent_bytes_ += stream_->GetTotalSentBytes();
522 }
[email protected]8e6441ca2010-08-19 05:56:38523 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14524 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31525 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38526 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51527 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
528 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38529 response_.was_fetched_via_spdy = stream_request_->using_spdy();
530 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15531 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
532 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38533 OnIOComplete(OK);
534}
535
xunjieli11834f02015-12-22 04:27:08536void HttpNetworkTransaction::OnBidirectionalStreamJobReady(
537 const SSLConfig& used_ssl_config,
538 const ProxyInfo& used_proxy_info,
539 BidirectionalStreamJob* stream_job) {
540 NOTREACHED();
541}
542
[email protected]a9cf2b92013-10-30 12:08:49543void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50544 const SSLConfig& used_ssl_config,
545 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49546 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44547 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50548}
549
[email protected]102e27c2011-02-23 01:01:31550void HttpNetworkTransaction::OnStreamFailed(int result,
davidbenf2eaaf92015-05-15 22:18:42551 const SSLConfig& used_ssl_config,
552 SSLFailureState ssl_failure_state) {
[email protected]82918cc2010-08-25 17:24:50553 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38554 DCHECK_NE(OK, result);
555 DCHECK(stream_request_.get());
556 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14557 server_ssl_config_ = used_ssl_config;
davidbenf2eaaf92015-05-15 22:18:42558 server_ssl_failure_state_ = ssl_failure_state;
[email protected]8e6441ca2010-08-19 05:56:38559
560 OnIOComplete(result);
561}
562
[email protected]102e27c2011-02-23 01:01:31563void HttpNetworkTransaction::OnCertificateError(
564 int result,
565 const SSLConfig& used_ssl_config,
566 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50567 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38568 DCHECK_NE(OK, result);
569 DCHECK(stream_request_.get());
570 DCHECK(!stream_.get());
571
572 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14573 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38574
575 // TODO(mbelshe): For now, we're going to pass the error through, and that
576 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50577 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
578 // good and the user chooses to ignore the error. This is not ideal, but not
579 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38580
581 OnIOComplete(result);
582}
583
584void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50585 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31586 const SSLConfig& used_ssl_config,
587 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50588 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38589 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50590 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38591
592 establishing_tunnel_ = true;
593 response_.headers = proxy_response.headers;
594 response_.auth_challenge = proxy_response.auth_challenge;
595 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14596 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31597 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38598
599 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
600 pending_auth_target_ = HttpAuth::AUTH_PROXY;
601
602 DoCallback(OK);
603}
604
605void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31606 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50607 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50608 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38609
[email protected]102957f2011-09-02 17:10:14610 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38611 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58612 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38613}
614
[email protected]511f6f52010-12-17 03:58:29615void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
616 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31617 const SSLConfig& used_ssl_config,
618 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39619 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29620 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
621
ttuttle1f2d7e92015-04-28 16:17:47622 CopyConnectionAttemptsFromStreamRequest();
623
[email protected]511f6f52010-12-17 03:58:29624 headers_valid_ = true;
625 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14626 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31627 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22628 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19629 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22630 total_sent_bytes_ += stream_->GetTotalSentBytes();
631 }
[email protected]511f6f52010-12-17 03:58:29632 stream_.reset(stream);
633 stream_request_.reset(); // we're done with the stream request
634 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
635}
636
zhongyi48704c182015-12-07 07:52:02637void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12638 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02639}
640
ttuttle1f2d7e92015-04-28 16:17:47641void HttpNetworkTransaction::GetConnectionAttempts(
642 ConnectionAttempts* out) const {
643 *out = connection_attempts_;
644}
645
Adam Ricecb76ac62015-02-20 05:33:25646bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37647 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52648}
649
nharperb7441ef2016-01-25 23:54:14650bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
651 if (!IsSecureRequest())
652 return false;
653 SSLInfo ssl_info;
654 stream_->GetSSLInfo(&ssl_info);
655 return ssl_info.token_binding_negotiated &&
656 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
657 session_->params().channel_id_service;
658}
659
660void HttpNetworkTransaction::RecordTokenBindingSupport() const {
661 // This enum is used for an UMA histogram - do not change or re-use values.
662 enum {
663 DISABLED = 0,
664 CLIENT_ONLY = 1,
665 CLIENT_AND_SERVER = 2,
666 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
667 TOKEN_BINDING_SUPPORT_MAX
668 } supported;
669 if (!IsSecureRequest())
670 return;
671 SSLInfo ssl_info;
672 stream_->GetSSLInfo(&ssl_info);
673 if (!session_->params().enable_token_binding) {
674 supported = DISABLED;
675 } else if (!session_->params().channel_id_service) {
676 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
677 } else if (ssl_info.token_binding_negotiated) {
678 supported = CLIENT_AND_SERVER;
679 } else {
680 supported = CLIENT_ONLY;
681 }
682 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
683 TOKEN_BINDING_SUPPORT_MAX);
684}
685
Adam Rice425cf122015-01-19 06:18:24686bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12687 return (proxy_info_.is_http() || proxy_info_.is_https() ||
688 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24689 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
690}
691
initial.commit586acc5fe2008-07-26 22:42:52692void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50693 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41694 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52695
[email protected]96d570e42008-08-05 22:43:04696 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41697 CompletionCallback c = callback_;
698 callback_.Reset();
699 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52700}
701
702void HttpNetworkTransaction::OnIOComplete(int result) {
703 int rv = DoLoop(result);
704 if (rv != ERR_IO_PENDING)
705 DoCallback(rv);
706}
707
708int HttpNetworkTransaction::DoLoop(int result) {
709 DCHECK(next_state_ != STATE_NONE);
710
711 int rv = result;
712 do {
713 State state = next_state_;
714 next_state_ = STATE_NONE;
715 switch (state) {
[email protected]1826a402014-01-08 15:40:48716 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
717 DCHECK_EQ(OK, rv);
718 rv = DoNotifyBeforeCreateStream();
719 break;
[email protected]82918cc2010-08-25 17:24:50720 case STATE_CREATE_STREAM:
721 DCHECK_EQ(OK, rv);
722 rv = DoCreateStream();
723 break;
724 case STATE_CREATE_STREAM_COMPLETE:
725 rv = DoCreateStreamComplete(rv);
726 break;
[email protected]351ab642010-08-05 16:55:31727 case STATE_INIT_STREAM:
728 DCHECK_EQ(OK, rv);
729 rv = DoInitStream();
730 break;
731 case STATE_INIT_STREAM_COMPLETE:
732 rv = DoInitStreamComplete(rv);
733 break;
[email protected]044de0642010-06-17 10:42:15734 case STATE_GENERATE_PROXY_AUTH_TOKEN:
735 DCHECK_EQ(OK, rv);
736 rv = DoGenerateProxyAuthToken();
737 break;
738 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
739 rv = DoGenerateProxyAuthTokenComplete(rv);
740 break;
741 case STATE_GENERATE_SERVER_AUTH_TOKEN:
742 DCHECK_EQ(OK, rv);
743 rv = DoGenerateServerAuthToken();
744 break;
745 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
746 rv = DoGenerateServerAuthTokenComplete(rv);
747 break;
nharperb7441ef2016-01-25 23:54:14748 case STATE_GET_TOKEN_BINDING_KEY:
749 DCHECK_EQ(OK, rv);
750 rv = DoGetTokenBindingKey();
751 break;
752 case STATE_GET_TOKEN_BINDING_KEY_COMPLETE:
753 rv = DoGetTokenBindingKeyComplete(rv);
754 break;
[email protected]daddea62012-09-19 05:51:13755 case STATE_INIT_REQUEST_BODY:
756 DCHECK_EQ(OK, rv);
757 rv = DoInitRequestBody();
758 break;
759 case STATE_INIT_REQUEST_BODY_COMPLETE:
760 rv = DoInitRequestBodyComplete(rv);
761 break;
[email protected]4875ba12011-03-30 22:31:51762 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55763 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38764 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51765 rv = DoBuildRequest();
766 break;
767 case STATE_BUILD_REQUEST_COMPLETE:
768 rv = DoBuildRequestComplete(rv);
769 break;
770 case STATE_SEND_REQUEST:
771 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57772 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52773 break;
[email protected]0877e3d2009-10-17 22:29:57774 case STATE_SEND_REQUEST_COMPLETE:
775 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43776 net_log_.EndEventWithNetErrorCode(
777 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52778 break;
779 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55780 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38781 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52782 rv = DoReadHeaders();
783 break;
784 case STATE_READ_HEADERS_COMPLETE:
785 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43786 net_log_.EndEventWithNetErrorCode(
787 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52788 break;
789 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55790 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38791 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52792 rv = DoReadBody();
793 break;
794 case STATE_READ_BODY_COMPLETE:
795 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43796 net_log_.EndEventWithNetErrorCode(
797 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52798 break;
[email protected]2d2697f92009-02-18 21:00:32799 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55800 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53801 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38802 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32803 rv = DoDrainBodyForAuthRestart();
804 break;
805 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
806 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43807 net_log_.EndEventWithNetErrorCode(
808 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32809 break;
initial.commit586acc5fe2008-07-26 22:42:52810 default:
811 NOTREACHED() << "bad state";
812 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04813 break;
initial.commit586acc5fe2008-07-26 22:42:52814 }
815 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
816
817 return rv;
818}
819
[email protected]1826a402014-01-08 15:40:48820int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
821 next_state_ = STATE_CREATE_STREAM;
822 bool defer = false;
823 if (!before_network_start_callback_.is_null())
824 before_network_start_callback_.Run(&defer);
825 if (!defer)
826 return OK;
827 return ERR_IO_PENDING;
828}
829
[email protected]82918cc2010-08-25 17:24:50830int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34831 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26832 tracked_objects::ScopedTracker tracking_profile(
833 FROM_HERE_WITH_EXPLICIT_FUNCTION(
834 "424359 HttpNetworkTransaction::DoCreateStream"));
835
ttuttlec0c828492015-05-15 01:25:55836 response_.network_accessed = true;
837
[email protected]82918cc2010-08-25 17:24:50838 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44839 if (ForWebSocketHandshake()) {
840 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23841 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44842 ->RequestWebSocketHandshakeStream(
843 *request_,
844 priority_,
845 server_ssl_config_,
846 proxy_ssl_config_,
847 this,
848 websocket_handshake_stream_base_create_helper_,
849 net_log_));
850 } else {
851 stream_request_.reset(
852 session_->http_stream_factory()->RequestStream(
853 *request_,
854 priority_,
855 server_ssl_config_,
856 proxy_ssl_config_,
857 this,
858 net_log_));
859 }
[email protected]26816882010-10-14 18:03:09860 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38861 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31862}
863
[email protected]82918cc2010-08-25 17:24:50864int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47865 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
866 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
867 // which resets the stream request first. Therefore, we have to grab the
868 // connection attempts in *that* function instead of here in that case.
869 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
870 CopyConnectionAttemptsFromStreamRequest();
871
davidben701ca982015-05-18 21:21:42872 if (request_->url.SchemeIsCryptographic())
873 RecordSSLFallbackMetrics(result);
874
[email protected]394816e92010-08-03 07:38:59875 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50876 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38877 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33878 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
879 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29880 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
881 // Return OK and let the caller read the proxy's error page
882 next_state_ = STATE_NONE;
883 return OK;
bncfacdd852015-01-09 19:22:54884 } else if (result == ERR_HTTP_1_1_REQUIRED ||
885 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
886 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59887 }
888
[email protected]bd0b6772011-01-11 19:59:30889 // Handle possible handshake errors that may have occurred if the stream
890 // used SSL for one or more of the layers.
891 result = HandleSSLHandshakeError(result);
892
[email protected]8e6441ca2010-08-19 05:56:38893 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09894 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38895 return result;
[email protected]394816e92010-08-03 07:38:59896}
897
[email protected]82918cc2010-08-25 17:24:50898int HttpNetworkTransaction::DoInitStream() {
899 DCHECK(stream_.get());
900 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59901
902 stream_->GetRemoteEndpoint(&remote_endpoint_);
903
[email protected]262eec82013-03-19 21:01:36904 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50905}
906
907int HttpNetworkTransaction::DoInitStreamComplete(int result) {
908 if (result == OK) {
909 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50910 } else {
[email protected]82918cc2010-08-25 17:24:50911 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26912 result = HandleIOError(result);
913
914 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22915 if (stream_) {
916 total_received_bytes_ += stream_->GetTotalReceivedBytes();
917 total_sent_bytes_ += stream_->GetTotalSentBytes();
918 }
zhongyica364fbb2015-12-12 03:39:12919 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50920 }
921
922 return result;
923}
924
[email protected]044de0642010-06-17 10:42:15925int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
926 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
927 if (!ShouldApplyProxyAuth())
928 return OK;
[email protected]394816e92010-08-03 07:38:59929 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
930 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09931 auth_controllers_[target] =
932 new HttpAuthController(target,
933 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31934 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09935 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59936 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41937 io_callback_,
[email protected]394816e92010-08-03 07:38:59938 net_log_);
[email protected]044de0642010-06-17 10:42:15939}
940
941int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
942 DCHECK_NE(ERR_IO_PENDING, rv);
943 if (rv == OK)
944 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
945 return rv;
946}
947
948int HttpNetworkTransaction::DoGenerateServerAuthToken() {
949 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59950 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54951 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09952 auth_controllers_[target] =
953 new HttpAuthController(target,
954 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31955 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09956 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54957 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
958 auth_controllers_[target]->DisableEmbeddedIdentity();
959 }
[email protected]044de0642010-06-17 10:42:15960 if (!ShouldApplyServerAuth())
961 return OK;
[email protected]394816e92010-08-03 07:38:59962 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41963 io_callback_,
[email protected]394816e92010-08-03 07:38:59964 net_log_);
[email protected]044de0642010-06-17 10:42:15965}
966
967int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
968 DCHECK_NE(ERR_IO_PENDING, rv);
969 if (rv == OK)
nharperb7441ef2016-01-25 23:54:14970 next_state_ = STATE_GET_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15971 return rv;
972}
973
nharperb7441ef2016-01-25 23:54:14974int HttpNetworkTransaction::DoGetTokenBindingKey() {
975 next_state_ = STATE_GET_TOKEN_BINDING_KEY_COMPLETE;
976 if (!IsTokenBindingEnabled())
977 return OK;
978
979 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
980 ChannelIDService* channel_id_service = session_->params().channel_id_service;
981 return channel_id_service->GetOrCreateChannelID(
982 request_->url.host(), &token_binding_key_, io_callback_,
983 &token_binding_request_);
984}
985
986int HttpNetworkTransaction::DoGetTokenBindingKeyComplete(int rv) {
987 DCHECK_NE(ERR_IO_PENDING, rv);
988 next_state_ = STATE_INIT_REQUEST_BODY;
989 if (!IsTokenBindingEnabled())
990 return OK;
991
992 net_log_.EndEventWithNetErrorCode(
993 NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
994 return rv;
995}
996
997int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24998 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14999 request_headers_.SetHeader(HttpRequestHeaders::kHost,
1000 GetHostAndOptionalPort(request_->url));
1001
1002 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:241003 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:141004 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
1005 "keep-alive");
1006 } else {
1007 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
1008 }
1009
[email protected]2979a492011-04-06 00:29:141010 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:111011 if (request_->upload_data_stream) {
1012 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:141013 request_headers_.SetHeader(
1014 HttpRequestHeaders::kTransferEncoding, "chunked");
1015 } else {
1016 request_headers_.SetHeader(
1017 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:111018 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:141019 }
csharrisonf473dd192015-08-18 13:54:131020 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:141021 // An empty POST/PUT request still needs a content length. As for HEAD,
1022 // IE and Safari also add a content length header. Presumably it is to
1023 // support sending a HEAD request to an URL that only expects to be sent a
1024 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:131025 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
1026 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:141027 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
1028 }
1029
nharperb7441ef2016-01-25 23:54:141030 RecordTokenBindingSupport();
1031 if (token_binding_key_) {
1032 std::string token_binding_header;
1033 int rv = BuildTokenBindingHeader(&token_binding_header);
1034 if (rv != OK)
1035 return rv;
1036 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1037 token_binding_header);
1038 }
1039
[email protected]2979a492011-04-06 00:29:141040 // Honor load flags that impact proxy caches.
1041 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1042 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1043 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1044 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1045 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1046 }
1047
1048 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1049 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1050 &request_headers_);
1051 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1052 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1053 &request_headers_);
1054
[email protected]c10450102011-06-27 09:06:161055 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201056
Adam Rice425cf122015-01-19 06:18:241057 if (using_http_proxy_without_tunnel &&
1058 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:201059 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
1060
[email protected]173f8e22013-04-10 04:18:201061 response_.did_use_http_auth =
1062 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1063 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141064 return OK;
1065}
1066
1067int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471068 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141069 std::vector<uint8_t> signed_ekm;
1070 int rv = stream_->GetSignedEKMForTokenBinding(token_binding_key_.get(),
1071 &signed_ekm);
1072 if (rv != OK)
1073 return rv;
1074 std::string provided_token_binding;
1075 rv = BuildProvidedTokenBinding(token_binding_key_.get(), signed_ekm,
1076 &provided_token_binding);
1077 if (rv != OK)
1078 return rv;
1079 std::vector<base::StringPiece> token_bindings;
1080 token_bindings.push_back(provided_token_binding);
1081 std::string header;
1082 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1083 if (rv != OK)
1084 return rv;
1085 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
1086 out);
nharpercb66ad0202016-03-11 18:17:471087 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1088 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1089 header_creation_time,
1090 base::TimeDelta::FromMilliseconds(1),
1091 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141092 return OK;
[email protected]2979a492011-04-06 00:29:141093}
1094
[email protected]daddea62012-09-19 05:51:131095int HttpNetworkTransaction::DoInitRequestBody() {
1096 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131097 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111098 if (request_->upload_data_stream)
1099 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:131100 return rv;
1101}
[email protected]4875ba12011-03-30 22:31:511102
[email protected]daddea62012-09-19 05:51:131103int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1104 if (result == OK)
1105 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131106 return result;
1107}
1108
1109int HttpNetworkTransaction::DoBuildRequest() {
1110 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511111 headers_valid_ = false;
1112
1113 // This is constructed lazily (instead of within our Start method), so that
1114 // we have proxy info available.
1115 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241116 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141117 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511118 }
1119
[email protected]4875ba12011-03-30 22:31:511120 return OK;
1121}
1122
1123int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241124 if (result == OK)
1125 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511126 return result;
1127}
1128
[email protected]0877e3d2009-10-17 22:29:571129int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341130 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261131 tracked_objects::ScopedTracker tracking_profile(
1132 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1133 "424359 HttpNetworkTransaction::DoSendRequest"));
1134
[email protected]58e32bb2013-01-21 18:23:251135 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571136 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1137
[email protected]bf3eb002012-11-15 05:50:111138 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521139}
1140
[email protected]0877e3d2009-10-17 22:29:571141int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251142 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:521143 if (result < 0)
1144 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571145 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521146 return OK;
1147}
1148
1149int HttpNetworkTransaction::DoReadHeaders() {
1150 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411151 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521152}
1153
1154int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111155 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1156 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381157 if (IsCertificateError(result)) {
1158 // We don't handle a certificate error during SSL renegotiation, so we
1159 // have to return an error that's not in the certificate error range
1160 // (-2xx).
1161 LOG(ERROR) << "Got a server certificate with error " << result
1162 << " during SSL renegotiation";
1163 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1164 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1165 // TODO(wtc): Need a test case for this code path!
1166 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251167 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381168 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501169 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381170 result = HandleCertificateRequest(result);
1171 if (result == OK)
1172 return result;
[email protected]2181ea002009-06-09 01:37:271173 }
1174
bncfacdd852015-01-09 19:22:541175 if (result == ERR_HTTP_1_1_REQUIRED ||
1176 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1177 return HandleHttp11Required(result);
1178 }
1179
[email protected]c871864d72014-03-13 19:56:191180 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1181 // response headers were received, we do the best we can to make sense of it
1182 // and send it back up the stack.
1183 //
1184 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1185 // bizarre for SPDY. Assuming this logic is useful at all.
1186 // TODO(davidben): Bubble the error code up so we do not cache?
1187 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1188 result = OK;
1189
1190 if (result < 0)
1191 return HandleIOError(result);
1192
[email protected]90499482013-06-01 00:39:501193 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521194
[email protected]d58ceea82014-06-04 10:55:541195 // On a 408 response from the server ("Request Timeout") on a stale socket,
1196 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041197 // Headers can be NULL because of http://crbug.com/384554.
1198 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541199 stream_->IsConnectionReused()) {
1200 net_log_.AddEventWithNetErrorCode(
1201 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1202 response_.headers->response_code());
1203 // This will close the socket - it would be weird to try and reuse it, even
1204 // if the server doesn't actually close it.
1205 ResetConnectionAndRequestForResend();
1206 return OK;
1207 }
1208
[email protected]93f8b562012-03-27 01:00:161209 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1210 if (request_->load_flags & LOAD_MAIN_FRAME) {
1211 const int response_code = response_.headers->response_code();
1212 UMA_HISTOGRAM_ENUMERATION(
1213 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1214 }
1215
[email protected]3abacd62012-06-10 20:20:321216 net_log_.AddEvent(
1217 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1218 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391219
bncbe0f6af2015-10-15 17:49:561220 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571221 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1222 // indicates a buggy server. See:
1223 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1224 if (request_->method == "PUT")
1225 return ERR_METHOD_NOT_SUPPORTED;
1226 }
[email protected]4ddaf2502008-10-23 18:26:191227
[email protected]0877e3d2009-10-17 22:29:571228 // Check for an intermediate 100 Continue response. An origin server is
1229 // allowed to send this response even if we didn't ask for it, so we just
1230 // need to skip over it.
1231 // We treat any other 1xx in this same way (although in practice getting
1232 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441233 // Unless this is a WebSocket request, in which case we pass it on up.
1234 if (response_.headers->response_code() / 100 == 1 &&
1235 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451236 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571237 next_state_ = STATE_READ_HEADERS;
1238 return OK;
1239 }
1240
bncc958faa2015-07-31 18:14:521241 ProcessAlternativeServices(session_, *response_.headers.get(),
1242 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421243
[email protected]e772db3f2010-07-12 18:11:131244 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571245 if (rv != OK)
1246 return rv;
1247
Adam Ricecb76ac62015-02-20 05:33:251248 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211249 stream_->GetSSLInfo(&response_.ssl_info);
1250
[email protected]0877e3d2009-10-17 22:29:571251 headers_valid_ = true;
1252 return OK;
initial.commit586acc5fe2008-07-26 22:42:521253}
1254
1255int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501256 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131257 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381258 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521259
1260 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501261 return stream_->ReadResponseBody(
1262 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521263}
1264
1265int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1266 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381267 bool done = false;
1268 if (result <= 0) {
1269 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521270 done = true;
[email protected]8e6441ca2010-08-19 05:56:381271 }
[email protected]9492e4a2010-02-24 00:58:461272
mmenkebd84c392015-09-02 14:12:341273 // Clean up connection if we are done.
1274 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381275 // Note: Just because IsResponseBodyComplete is true, we're not
1276 // necessarily "done". We're only "done" when it is the last
1277 // read on this HttpNetworkTransaction, which will be signified
1278 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341279 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381280 // the stream. No need to compute it here just to pass back
1281 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341282 bool keep_alive =
1283 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521284
[email protected]8e6441ca2010-08-19 05:56:381285 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011286 // Note: we don't reset the stream here. We've closed it, but we still
1287 // need it around so that callers can call methods such as
1288 // GetUploadProgress() and have them be meaningful.
1289 // TODO(mbelshe): This means we closed the stream here, and we close it
1290 // again in ~HttpNetworkTransaction. Clean that up.
1291
[email protected]8e6441ca2010-08-19 05:56:381292 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521293 }
1294
1295 // Clear these to avoid leaving around old state.
1296 read_buf_ = NULL;
1297 read_buf_len_ = 0;
1298
1299 return result;
1300}
1301
[email protected]2d2697f92009-02-18 21:00:321302int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1303 // This method differs from DoReadBody only in the next_state_. So we just
1304 // call DoReadBody and override the next_state_. Perhaps there is a more
1305 // elegant way for these two methods to share code.
1306 int rv = DoReadBody();
1307 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1308 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1309 return rv;
1310}
1311
[email protected]0877e3d2009-10-17 22:29:571312// TODO(wtc): This method and the DoReadBodyComplete method are almost
1313// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321314int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231315 // keep_alive defaults to true because the very reason we're draining the
1316 // response body is to reuse the connection for auth restart.
1317 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321318 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571319 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321320 done = true;
[email protected]68873ba2009-06-04 21:49:231321 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311322 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571323 done = true;
[email protected]2d2697f92009-02-18 21:00:321324 }
1325
1326 if (done) {
1327 DidDrainBodyForAuthRestart(keep_alive);
1328 } else {
1329 // Keep draining.
1330 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1331 }
1332
1333 return OK;
1334}
1335
[email protected]5e363962009-06-19 19:57:011336int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381337 // There are two paths through which the server can request a certificate
1338 // from us. The first is during the initial handshake, the second is
1339 // during SSL renegotiation.
1340 //
1341 // In both cases, we want to close the connection before proceeding.
1342 // We do this for two reasons:
1343 // First, we don't want to keep the connection to the server hung for a
1344 // long time while the user selects a certificate.
1345 // Second, even if we did keep the connection open, NSS has a bug where
1346 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581347 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381348
1349 if (stream_.get()) {
1350 // Since we already have a stream, we're being called as part of SSL
1351 // renegotiation.
1352 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191353 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221354 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381355 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121356 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381357 }
1358
[email protected]26816882010-10-14 18:03:091359 // The server is asking for a client certificate during the initial
1360 // handshake.
1361 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011362
[email protected]ec229bc92010-11-22 09:51:451363 // If the user selected one of the certificates in client_certs or declined
1364 // to provide one for this server before, use the past decision
1365 // automatically.
1366 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541367 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451368 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541369 response_.cert_request_info->host_and_port, &client_cert,
1370 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451371 if (!found_cached_cert)
1372 return error;
1373
1374 // Check that the certificate selected is still a certificate the server
1375 // is likely to accept, based on the criteria supplied in the
1376 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501377 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261378 const std::vector<std::string>& cert_authorities =
1379 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451380
[email protected]f1958c382013-02-07 00:15:261381 bool cert_still_valid = cert_authorities.empty() ||
1382 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451383 if (!cert_still_valid)
1384 return error;
[email protected]5e363962009-06-19 19:57:011385 }
[email protected]ec229bc92010-11-22 09:51:451386
1387 // TODO(davidben): Add a unit test which covers this path; we need to be
1388 // able to send a legitimate certificate and also bypass/clear the
1389 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141390 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1391 &proxy_ssl_config_ : &server_ssl_config_;
1392 ssl_config->send_client_cert = true;
1393 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541394 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451395 next_state_ = STATE_CREATE_STREAM;
1396 // Reset the other member variables.
1397 // Note: this is necessary only with SSL renegotiation.
1398 ResetStateForRestart();
1399 return OK;
[email protected]0b45559b2009-06-12 21:45:111400}
1401
bncfacdd852015-01-09 19:22:541402int HttpNetworkTransaction::HandleHttp11Required(int error) {
1403 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1404 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1405
1406 if (error == ERR_HTTP_1_1_REQUIRED) {
1407 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1408 } else {
1409 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1410 }
1411 ResetConnectionAndRequestForResend();
1412 return OK;
1413}
1414
[email protected]384cc73a2013-12-08 22:41:031415void HttpNetworkTransaction::HandleClientAuthError(int error) {
1416 if (server_ssl_config_.send_client_cert &&
1417 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1418 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411419 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031420 }
1421}
1422
[email protected]bd0b6772011-01-11 19:59:301423// TODO(rch): This does not correctly handle errors when an SSL proxy is
1424// being used, as all of the errors are handled as if they were generated
1425// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381426// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301427int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1428 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031429 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301430
davidbena4c9d062015-04-03 22:34:251431 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1432 // reflect servers require a deprecated cipher rather than merely prefer
1433 // it. This, however, has no security benefit until the ciphers are actually
1434 // removed.
davidben14b1a532015-10-30 16:01:091435 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251436 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1437 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1438 net_log_.AddEvent(
1439 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1440 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091441 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251442 ResetConnectionAndRequestForResend();
1443 return OK;
1444 }
1445
davidben3b267512016-02-24 19:46:551446 // TODO(davidben): Remove this code once the dedicated error code is no
1447 // longer needed and the flags to re-enable the fallback expire.
[email protected]02d1d442013-08-10 13:38:261448 bool should_fallback = false;
Avi Drissman13fc8932015-12-20 04:40:461449 uint16_t version_max = server_ssl_config_.version_max;
[email protected]158ac972013-04-19 23:29:231450
initial.commit586acc5fe2008-07-26 22:42:521451 switch (error) {
davidben3b267512016-02-24 19:46:551452 // This could be a TLS-intolerant server or a server that chose a
1453 // cipher suite defined only for higher protocol versions (such as
1454 // an TLS 1.1 server that chose a TLS-1.2-only cipher suite). Fall
1455 // back to the next lower version and retry.
[email protected]12833302014-07-02 01:57:311456 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301457 case ERR_SSL_PROTOCOL_ERROR:
1458 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
davidben3b267512016-02-24 19:46:551459 // Some servers trigger the TLS 1.1 fallback with ERR_CONNECTION_RESET
1460 // (https://crbug.com/433406).
[email protected]12833302014-07-02 01:57:311461 case ERR_CONNECTION_RESET:
davidben3b267512016-02-24 19:46:551462 // This was added for the TLS 1.0 fallback (https://crbug.com/260358) which
1463 // has since been removed, but other servers may be relying on it for the
1464 // TLS 1.1 fallback. It will be removed with the remainder of the fallback.
[email protected]02d1d442013-08-10 13:38:261465 case ERR_SSL_BAD_RECORD_MAC_ALERT:
davidben3b267512016-02-24 19:46:551466 // Fallback down to a TLS 1.1 ClientHello. By default, this is rejected
1467 // but surfaces ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION to help diagnose
1468 // server bugs.
1469 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_2 &&
[email protected]02d1d442013-08-10 13:38:261470 version_max > server_ssl_config_.version_min) {
[email protected]02d1d442013-08-10 13:38:261471 version_max--;
1472 should_fallback = true;
1473 }
1474 break;
[email protected]a53e4d12013-12-07 16:37:241475 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1476 // The server told us that we should not have fallen back. A buggy server
1477 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1478 // connection. |fallback_error_code_| is initialised to
1479 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1480 error = fallback_error_code_;
1481 break;
[email protected]02d1d442013-08-10 13:38:261482 }
1483
1484 if (should_fallback) {
1485 net_log_.AddEvent(
1486 NetLog::TYPE_SSL_VERSION_FALLBACK,
davidbenf2eaaf92015-05-15 22:18:421487 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1488 server_ssl_failure_state_, server_ssl_config_.version_max,
[email protected]02d1d442013-08-10 13:38:261489 version_max));
[email protected]a53e4d12013-12-07 16:37:241490 fallback_error_code_ = error;
davidbenf2eaaf92015-05-15 22:18:421491 fallback_failure_state_ = server_ssl_failure_state_;
[email protected]02d1d442013-08-10 13:38:261492 server_ssl_config_.version_max = version_max;
1493 server_ssl_config_.version_fallback = true;
1494 ResetConnectionAndRequestForResend();
1495 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521496 }
[email protected]158ac972013-04-19 23:29:231497
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(
1532 NetLog::TYPE_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(
1541 NetLog::TYPE_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;
nharperb7441ef2016-01-25 23:54:141572 token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121573}
1574
1575void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1576 if (stream_)
1577 stream_->PopulateNetErrorDetails(&net_error_details_);
1578 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571579}
1580
davidben701ca982015-05-18 21:21:421581void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1582 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1583 return;
1584
1585 const std::string& host = request_->url.host();
brettwa7ff1b292015-07-16 17:49:291586 bool is_google = base::EndsWith(host, "google.com",
1587 base::CompareCase::SENSITIVE) &&
davidben701ca982015-05-18 21:21:421588 (host.size() == 10 || host[host.size() - 11] == '.');
1589 if (is_google) {
1590 // Some fraction of successful connections use the fallback, but only due to
1591 // a spurious network failure. To estimate this fraction, compare handshakes
1592 // to Google servers which succeed against those that fail with an
1593 // inappropriate_fallback alert. Google servers are known to implement
1594 // FALLBACK_SCSV, so a spurious network failure while connecting would
1595 // trigger the fallback, successfully connect, but fail with this alert.
1596 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1597 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1598 }
1599
1600 if (result != OK)
1601 return;
1602
davidbenca9d69162015-05-04 20:18:451603 // Note: these values are used in histograms, so new values must be appended.
1604 enum FallbackVersion {
davidbenb937d6c2015-05-14 04:53:421605 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1606 // Obsolete: FALLBACK_SSL3 = 1,
davidbenca9d69162015-05-04 20:18:451607 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1608 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1609 FALLBACK_MAX,
1610 };
1611
1612 FallbackVersion fallback = FALLBACK_NONE;
1613 if (server_ssl_config_.version_fallback) {
1614 switch (server_ssl_config_.version_max) {
davidbenca9d69162015-05-04 20:18:451615 case SSL_PROTOCOL_VERSION_TLS1:
1616 fallback = FALLBACK_TLS1;
1617 break;
1618 case SSL_PROTOCOL_VERSION_TLS1_1:
1619 fallback = FALLBACK_TLS1_1;
1620 break;
1621 default:
1622 NOTREACHED();
1623 }
1624 }
1625 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1626 FALLBACK_MAX);
1627
1628 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1629 // should be impossible to successfully connect to them with the fallback.
1630 // This helps estimate intolerant locally-configured SSL MITMs.
davidben701ca982015-05-18 21:21:421631 if (is_google) {
davidbenca9d69162015-05-04 20:18:451632 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1633 fallback, FALLBACK_MAX);
1634 }
1635
1636 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091637 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenf2eaaf92015-05-15 22:18:421638
1639 if (server_ssl_config_.version_fallback) {
1640 // Record the error code which triggered the fallback and the state the
1641 // handshake was in.
1642 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1643 -fallback_error_code_);
1644 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1645 fallback_failure_state_, SSL_FAILURE_MAX);
1646 }
davidbenca9d69162015-05-04 20:18:451647}
1648
[email protected]0877e3d2009-10-17 22:29:571649HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501650 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421651}
1652
[email protected]a34f61ee2014-03-18 20:59:491653bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381654 bool connection_is_proven = stream_->IsConnectionReused();
1655 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161656
[email protected]2a5c76b2008-09-25 22:15:161657 // NOTE: we resend a request only if we reused a keep-alive connection.
1658 // This automatically prevents an infinite resend loop because we'll run
1659 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381660 if (connection_is_proven && !has_received_headers)
1661 return true;
1662 return false;
[email protected]1c773ea12009-04-28 19:58:421663}
1664
1665void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381666 if (stream_.get()) {
1667 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121668 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161669 }
1670
[email protected]0877e3d2009-10-17 22:29:571671 // We need to clear request_headers_ because it contains the real request
1672 // headers, but we may need to resend the CONNECT request first to recreate
1673 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201674 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501675 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541676}
1677
[email protected]1c773ea12009-04-28 19:58:421678bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241679 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421680}
license.botbf09a502008-08-24 00:55:551681
[email protected]1c773ea12009-04-28 19:58:421682bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041683 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421684}
1685
[email protected]e772db3f2010-07-12 18:11:131686int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271687 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501688 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421689
[email protected]0877e3d2009-10-17 22:29:571690 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581691 if (status != HTTP_UNAUTHORIZED &&
1692 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421693 return OK;
[email protected]9094b602012-02-27 21:44:581694 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111695 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161696 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1697 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421698
[email protected]9094b602012-02-27 21:44:581699 // This case can trigger when an HTTPS server responds with a "Proxy
1700 // authentication required" status code through a non-authenticating
1701 // proxy.
[email protected]7a67a8152010-11-05 18:31:101702 if (!auth_controllers_[target].get())
1703 return ERR_UNEXPECTED_PROXY_AUTH;
1704
[email protected]a7ea8832010-07-12 17:54:541705 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311706 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1707 net_log_);
[email protected]228404f2010-06-24 04:31:411708 if (auth_controllers_[target]->HaveAuthHandler())
1709 pending_auth_target_ = target;
1710
1711 scoped_refptr<AuthChallengeInfo> auth_info =
1712 auth_controllers_[target]->auth_info();
1713 if (auth_info.get())
1714 response_.auth_challenge = auth_info;
1715
[email protected]228404f2010-06-24 04:31:411716 return rv;
[email protected]f9ee6b52008-11-08 06:46:231717}
1718
[email protected]8e6441ca2010-08-19 05:56:381719bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1720 return auth_controllers_[target].get() &&
1721 auth_controllers_[target]->HaveAuth();
1722}
1723
[email protected]228404f2010-06-24 04:31:411724GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1725 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461726 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411727 if (!proxy_info_.proxy_server().is_valid() ||
1728 proxy_info_.proxy_server().is_direct()) {
1729 return GURL(); // There is no proxy server.
1730 }
[email protected]2df19bb2010-08-25 20:13:461731 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1732 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351733 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461734 }
[email protected]228404f2010-06-24 04:31:411735 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291736 if (ForWebSocketHandshake()) {
1737 const GURL& url = request_->url;
1738 url::Replacements<char> ws_to_http;
1739 if (url.SchemeIs("ws")) {
1740 ws_to_http.SetScheme("http", url::Component(0, 4));
1741 } else {
1742 DCHECK(url.SchemeIs("wss"));
1743 ws_to_http.SetScheme("https", url::Component(0, 5));
1744 }
1745 return url.ReplaceComponents(ws_to_http);
1746 }
[email protected]228404f2010-06-24 04:31:411747 return request_->url;
1748 default:
1749 return GURL();
1750 }
[email protected]c3b35c22008-09-27 03:19:421751}
1752
[email protected]831e4a32013-11-14 02:14:441753bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141754 return websocket_handshake_stream_base_create_helper_ &&
1755 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441756}
1757
[email protected]d8eb84242010-09-25 02:25:061758#define STATE_CASE(s) \
1759 case s: \
1760 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1761 break
[email protected]aef04272010-06-28 18:03:041762
1763std::string HttpNetworkTransaction::DescribeState(State state) {
1764 std::string description;
1765 switch (state) {
[email protected]1826a402014-01-08 15:40:481766 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501767 STATE_CASE(STATE_CREATE_STREAM);
1768 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131769 STATE_CASE(STATE_INIT_REQUEST_BODY);
1770 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511771 STATE_CASE(STATE_BUILD_REQUEST);
1772 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041773 STATE_CASE(STATE_SEND_REQUEST);
1774 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1775 STATE_CASE(STATE_READ_HEADERS);
1776 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041777 STATE_CASE(STATE_READ_BODY);
1778 STATE_CASE(STATE_READ_BODY_COMPLETE);
1779 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1780 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041781 STATE_CASE(STATE_NONE);
1782 default:
[email protected]d8eb84242010-09-25 02:25:061783 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1784 state);
[email protected]aef04272010-06-28 18:03:041785 break;
1786 }
1787 return description;
1788}
1789
1790#undef STATE_CASE
1791
ttuttle1f2d7e92015-04-28 16:17:471792void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1793 DCHECK(stream_request_);
1794
1795 // Since the transaction can restart with auth credentials, it may create a
1796 // stream more than once. Accumulate all of the connection attempts across
1797 // those streams by appending them to the vector:
1798 for (const auto& attempt : stream_request_->connection_attempts())
1799 connection_attempts_.push_back(attempt);
1800}
1801
[email protected]c3b35c22008-09-27 03:19:421802} // namespace net