blob: 8d02fedbe208d1d28e038c555bf62342c4a54d32 [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>
8#include <vector>
9
[email protected]49639fa2011-12-20 23:22:4110#include "base/bind.h"
11#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3012#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4713#include "base/format_macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/scoped_ptr.h"
[email protected]835d7c82010-10-14 04:38:3815#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3716#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4217#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4118#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4720#include "base/strings/string_number_conversions.h"
21#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4232#include "net/base/net_util.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
[email protected]c3b35c22008-09-27 03:19:4234#include "net/http/http_auth.h"
35#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2436#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3637#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/http/http_chunked_decoder.h"
39#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5440#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1341#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4742#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2144#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5745#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5346#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5847#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3948#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3149#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2351#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3652#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4453#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2654#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4455#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1856#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2857#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4058#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3259#include "net/spdy/spdy_session.h"
60#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5761#include "net/ssl/ssl_cert_request_info.h"
62#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5463#include "net/ssl/ssl_private_key.h"
[email protected]f89276a72013-07-12 06:41:5464#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2965#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5266
initial.commit586acc5fe2008-07-26 22:42:5267namespace net {
68
[email protected]1c773ea12009-04-28 19:58:4269namespace {
70
bncc958faa2015-07-31 18:14:5271void ProcessAlternativeServices(HttpNetworkSession* session,
72 const HttpResponseHeaders& headers,
73 const HostPortPair& http_host_port_pair) {
bnc54ec34b72015-08-26 19:34:5674 if (session->params().use_alternative_services &&
75 headers.HasHeader(kAlternativeServiceHeader)) {
bncc958faa2015-07-31 18:14:5276 std::string alternative_service_str;
77 headers.GetNormalizedHeader(kAlternativeServiceHeader,
78 &alternative_service_str);
79 session->http_stream_factory()->ProcessAlternativeService(
80 session->http_server_properties(), alternative_service_str,
81 http_host_port_pair, *session);
82 // If there is an "Alt-Svc" header, then ignore "Alternate-Protocol".
83 return;
84 }
85
[email protected]287d9412014-07-08 23:01:0086 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3887 return;
[email protected]287d9412014-07-08 23:01:0088
89 std::vector<std::string> alternate_protocol_values;
90 void* iter = NULL;
91 std::string alternate_protocol_str;
92 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
93 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5294 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
95 &alternate_protocol_str);
96 if (!alternate_protocol_str.empty()) {
97 alternate_protocol_values.push_back(alternate_protocol_str);
98 }
[email protected]564b4912010-03-09 16:30:4299 }
100
[email protected]d7599122014-05-24 03:37:23101 session->http_stream_factory()->ProcessAlternateProtocol(
102 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:00103 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:23104 http_host_port_pair,
105 *session);
[email protected]f45c1ee2010-08-03 00:54:30106}
107
estade5e5529d2015-05-21 20:59:11108scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
[email protected]ea5ef4c2013-06-13 22:50:27109 const GURL* url,
110 int net_error,
davidbenf2eaaf92015-05-15 22:18:42111 SSLFailureState ssl_failure_state,
Avi Drissman13fc8932015-12-20 04:40:46112 uint16_t version_before,
113 uint16_t version_after,
eroman001c3742015-04-23 03:11:17114 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11115 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b6754252012-06-13 23:14:38116 dict->SetString("host_and_port", GetHostAndPort(*url));
117 dict->SetInteger("net_error", net_error);
davidbenf2eaaf92015-05-15 22:18:42118 dict->SetInteger("ssl_failure_state", ssl_failure_state);
[email protected]b6754252012-06-13 23:14:38119 dict->SetInteger("version_before", version_before);
120 dict->SetInteger("version_after", version_after);
estade5e5529d2015-05-21 20:59:11121 return dict.Pass();
[email protected]b6754252012-06-13 23:14:38122}
[email protected]db74b0102012-05-31 19:55:53123
estade5e5529d2015-05-21 20:59:11124scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:17125 const GURL* url,
126 int net_error,
127 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11128 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:25129 dict->SetString("host_and_port", GetHostAndPort(*url));
130 dict->SetInteger("net_error", net_error);
estade5e5529d2015-05-21 20:59:11131 return dict.Pass();
davidbena4c9d062015-04-03 22:34:25132}
133
[email protected]1c773ea12009-04-28 19:58:42134} // namespace
135
initial.commit586acc5fe2008-07-26 22:42:52136//-----------------------------------------------------------------------------
137
[email protected]262eec82013-03-19 21:01:36138HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
139 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22140 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32141 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
142 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52143 session_(session),
144 request_(NULL),
[email protected]262eec82013-03-19 21:01:36145 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57146 headers_valid_(false),
davidbenf2eaaf92015-05-15 22:18:42147 server_ssl_failure_state_(SSL_FAILURE_NONE),
[email protected]a53e4d12013-12-07 16:37:24148 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
davidbenf2eaaf92015-05-15 22:18:42149 fallback_failure_state_(SSL_FAILURE_NONE),
[email protected]b94f92d2010-10-27 16:45:20150 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52151 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19152 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22153 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54154 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44155 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02156 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12157 net_error_details_() {
[email protected]102957f2011-09-02 17:10:14158 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
bnc1f295372015-10-21 23:24:22159 session->GetAlpnProtos(&server_ssl_config_.alpn_protos);
160 session->GetNpnProtos(&server_ssl_config_.npn_protos);
[email protected]99ffa5a2011-10-06 04:20:19161 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50162}
163
[email protected]0b0bf032010-09-21 18:08:50164HttpNetworkTransaction::~HttpNetworkTransaction() {
165 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50166 // TODO(mbelshe): The stream_ should be able to compute whether or not the
167 // stream should be kept alive. No reason to compute here
168 // and pass it in.
mmenkebd84c392015-09-02 14:12:34169 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50170 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34171 } else if (stream_->IsResponseBodyComplete()) {
172 // If the response body is complete, we can just reuse the socket.
173 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50174 } else {
mmenkebd84c392015-09-02 14:12:34175 // Otherwise, we try to drain the response body.
176 HttpStream* stream = stream_.release();
177 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50178 }
179 }
[email protected]02cad5d2013-10-02 08:14:03180
181 if (request_ && request_->upload_data_stream)
182 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50183}
184
[email protected]684970b2009-08-14 04:54:46185int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41186 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53187 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53188 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04189 request_ = request_info;
190
[email protected]99ffa5a2011-10-06 04:20:19191 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14192 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19193 proxy_ssl_config_.rev_checking_enabled = false;
194 }
[email protected]6fbac162011-06-20 00:29:04195
jkarlinfb1d5172015-01-12 14:10:29196 if (request_->load_flags & LOAD_PREFETCH)
197 response_.unused_since_prefetch = true;
198
[email protected]1ea4f46a2014-04-22 22:33:56199 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49200 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
201 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40202
[email protected]1826a402014-01-08 15:40:48203 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04204 int rv = DoLoop(OK);
205 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41206 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04207 return rv;
208}
209
210int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41211 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38212 DCHECK(!stream_.get());
213 DCHECK(!stream_request_.get());
214 DCHECK_EQ(STATE_NONE, next_state_);
215
[email protected]82918cc2010-08-25 17:24:50216 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38217
[email protected]ccb40e52008-09-17 20:54:40218 int rv = DoLoop(OK);
219 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41220 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11221 return rv;
[email protected]96d570e42008-08-05 22:43:04222}
223
[email protected]0b45559b2009-06-12 21:45:11224int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54225 X509Certificate* client_cert,
226 SSLPrivateKey* client_private_key,
227 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38228 // In HandleCertificateRequest(), we always tear down existing stream
229 // requests to force a new connection. So we shouldn't have one here.
230 DCHECK(!stream_request_.get());
231 DCHECK(!stream_.get());
232 DCHECK_EQ(STATE_NONE, next_state_);
233
[email protected]102957f2011-09-02 17:10:14234 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
235 &proxy_ssl_config_ : &server_ssl_config_;
236 ssl_config->send_client_cert = true;
237 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54238 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45239 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54240 response_.cert_request_info->host_and_port, client_cert,
241 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11242 // Reset the other member variables.
243 // Note: this is necessary only with SSL renegotiation.
244 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50245 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11246 int rv = DoLoop(OK);
247 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41248 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11249 return rv;
250}
251
[email protected]49639fa2011-12-20 23:22:41252int HttpNetworkTransaction::RestartWithAuth(
253 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22254 HttpAuth::Target target = pending_auth_target_;
255 if (target == HttpAuth::AUTH_NONE) {
256 NOTREACHED();
257 return ERR_UNEXPECTED;
258 }
[email protected]0757e7702009-03-27 04:00:22259 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42260
[email protected]f3cf9802011-10-28 18:44:58261 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13262
[email protected]49639fa2011-12-20 23:22:41263 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38264
265 int rv = OK;
266 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
267 // In this case, we've gathered credentials for use with proxy
268 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50269 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38270 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59271 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54272 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58273 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54274 } else {
[email protected]8e6441ca2010-08-19 05:56:38275 // In this case, we've gathered credentials for the server or the proxy
276 // but it is not during the tunneling phase.
277 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54278 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38279 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54280 }
[email protected]c3b35c22008-09-27 03:19:42281
[email protected]c3b35c22008-09-27 03:19:42282 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41283 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42284 return rv;
[email protected]96d570e42008-08-05 22:43:04285}
286
[email protected]f9ee6b52008-11-08 06:46:23287void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
288 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38289 DCHECK(!stream_request_.get());
290
[email protected]2d2697f92009-02-18 21:00:32291 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57292 // Even if the server says the connection is keep-alive, we have to be
293 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34294 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57295 // If the response body hasn't been completely read, we need to drain
296 // it first.
[email protected]351ab642010-08-05 16:55:31297 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32298 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57299 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32300 read_buf_len_ = kDrainBodyBufferSize;
301 return;
302 }
[email protected]0877e3d2009-10-17 22:29:57303 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09304 }
305
[email protected]2d2697f92009-02-18 21:00:32306 // We don't need to drain the response body, so we act as if we had drained
307 // the response body.
308 DidDrainBodyForAuthRestart(keep_alive);
309}
310
311void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38312 DCHECK(!stream_request_.get());
313
314 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19315 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22316 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58317 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34318 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38319 // We should call connection_->set_idle_time(), but this doesn't occur
320 // often enough to be worth the trouble.
321 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39322 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38323 }
[email protected]697ef4c2010-10-14 16:38:58324
325 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46326 // Close the stream and mark it as not_reusable. Even in the
327 // keep_alive case, we've determined that the stream_ is not
328 // reusable if new_stream is NULL.
329 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58330 next_state_ = STATE_CREATE_STREAM;
331 } else {
sclittlefb249892015-09-10 21:33:22332 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19333 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22334 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58335 next_state_ = STATE_INIT_STREAM;
336 }
337 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32338 }
[email protected]f9ee6b52008-11-08 06:46:23339
340 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58341 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23342}
343
[email protected]8e6441ca2010-08-19 05:56:38344bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
345 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
346 HaveAuth(pending_auth_target_);
347}
348
[email protected]9dea9e1f2009-01-29 00:30:47349int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41350 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04351 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35352 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04353
[email protected]1f14a912009-12-21 20:32:44354 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04355
[email protected]ad8e04a2010-11-01 04:16:27356 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38357 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04358 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29359 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04360 // bytes when establishing a tunnel because they might be controlled by
361 // an active network attacker. We don't worry about this for HTTP
362 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29363 // We reach this case when the user cancels a 407 proxy auth prompt. We
364 // also don't worry about this for an HTTPS Proxy, because the
365 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04366 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46367 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58368 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54369 LOG(WARNING) << "Blocked proxy response with status "
370 << headers->response_code() << " to CONNECT request for "
371 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04372 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44373 }
374
[email protected]e60e47a2010-07-14 03:37:18375 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31376 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18377
[email protected]96d570e42008-08-05 22:43:04378 read_buf_ = buf;
379 read_buf_len_ = buf_len;
380
[email protected]1f14a912009-12-21 20:32:44381 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04382 int rv = DoLoop(OK);
383 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41384 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04385 return rv;
386}
387
[email protected]8cd06c02014-01-25 07:50:14388void HttpNetworkTransaction::StopCaching() {}
389
[email protected]79e1fd62013-06-20 06:50:04390bool HttpNetworkTransaction::GetFullRequestHeaders(
391 HttpRequestHeaders* headers) const {
392 // TODO(ttuttle): Make sure we've populated request_headers_.
393 *headers = request_headers_;
394 return true;
395}
396
sclittle4de1bab92015-09-22 21:28:24397int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
398 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19399 if (stream_)
400 total_received_bytes += stream_->GetTotalReceivedBytes();
401 return total_received_bytes;
402}
403
sclittlefb249892015-09-10 21:33:22404int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
405 int64_t total_sent_bytes = total_sent_bytes_;
406 if (stream_)
407 total_sent_bytes += stream_->GetTotalSentBytes();
408 return total_sent_bytes;
409}
410
[email protected]8cd06c02014-01-25 07:50:14411void HttpNetworkTransaction::DoneReading() {}
412
[email protected]96d570e42008-08-05 22:43:04413const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55414 return &response_;
[email protected]96d570e42008-08-05 22:43:04415}
416
417LoadState HttpNetworkTransaction::GetLoadState() const {
418 // TODO(wtc): Define a new LoadState value for the
419 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
420 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48421 case STATE_CREATE_STREAM:
422 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50423 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38424 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15425 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
426 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57427 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04428 return LOAD_STATE_SENDING_REQUEST;
429 case STATE_READ_HEADERS_COMPLETE:
430 return LOAD_STATE_WAITING_FOR_RESPONSE;
431 case STATE_READ_BODY_COMPLETE:
432 return LOAD_STATE_READING_RESPONSE;
433 default:
434 return LOAD_STATE_IDLE;
435 }
436}
437
[email protected]196d18a2012-08-30 03:47:31438UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31439 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31440 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04441
yhiranoa7e05bb2014-11-06 05:40:39442 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04443}
444
[email protected]8cd06c02014-01-25 07:50:14445void HttpNetworkTransaction::SetQuicServerInfo(
446 QuicServerInfo* quic_server_info) {}
447
[email protected]5033ab82013-03-22 20:17:46448bool HttpNetworkTransaction::GetLoadTimingInfo(
449 LoadTimingInfo* load_timing_info) const {
450 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
451 return false;
452
453 load_timing_info->proxy_resolve_start =
454 proxy_info_.proxy_resolve_start_time();
455 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
456 load_timing_info->send_start = send_start_time_;
457 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46458 return true;
459}
460
ttuttled9dbc652015-09-29 20:00:59461bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
462 if (!remote_endpoint_.address().size())
463 return false;
464
465 *endpoint = remote_endpoint_;
466 return true;
467}
468
zhongyi48704c182015-12-07 07:52:02469void HttpNetworkTransaction::PopulateNetErrorDetails(
470 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12471 *details = net_error_details_;
472 if (stream_)
473 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02474}
475
[email protected]5033ab82013-03-22 20:17:46476void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
477 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47478 if (stream_request_)
479 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03480 if (stream_)
481 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46482}
483
[email protected]831e4a32013-11-14 02:14:44484void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
485 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
486 websocket_handshake_stream_base_create_helper_ = create_helper;
487}
488
[email protected]1826a402014-01-08 15:40:48489void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
490 const BeforeNetworkStartCallback& callback) {
491 before_network_start_callback_ = callback;
492}
493
[email protected]597a1ab2014-06-26 08:12:27494void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
495 const BeforeProxyHeadersSentCallback& callback) {
496 before_proxy_headers_sent_callback_ = callback;
497}
498
[email protected]1826a402014-01-08 15:40:48499int HttpNetworkTransaction::ResumeNetworkStart() {
500 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
501 return DoLoop(OK);
502}
503
[email protected]102e27c2011-02-23 01:01:31504void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
505 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39506 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50507 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38508 DCHECK(stream_request_.get());
509
sclittlefb249892015-09-10 21:33:22510 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19511 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22512 total_sent_bytes_ += stream_->GetTotalSentBytes();
513 }
[email protected]8e6441ca2010-08-19 05:56:38514 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14515 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31516 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38517 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51518 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
519 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38520 response_.was_fetched_via_spdy = stream_request_->using_spdy();
521 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15522 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
523 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38524 OnIOComplete(OK);
525}
526
[email protected]a9cf2b92013-10-30 12:08:49527void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50528 const SSLConfig& used_ssl_config,
529 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49530 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44531 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50532}
533
[email protected]102e27c2011-02-23 01:01:31534void HttpNetworkTransaction::OnStreamFailed(int result,
davidbenf2eaaf92015-05-15 22:18:42535 const SSLConfig& used_ssl_config,
536 SSLFailureState ssl_failure_state) {
[email protected]82918cc2010-08-25 17:24:50537 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38538 DCHECK_NE(OK, result);
539 DCHECK(stream_request_.get());
540 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14541 server_ssl_config_ = used_ssl_config;
davidbenf2eaaf92015-05-15 22:18:42542 server_ssl_failure_state_ = ssl_failure_state;
[email protected]8e6441ca2010-08-19 05:56:38543
544 OnIOComplete(result);
545}
546
[email protected]102e27c2011-02-23 01:01:31547void HttpNetworkTransaction::OnCertificateError(
548 int result,
549 const SSLConfig& used_ssl_config,
550 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50551 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38552 DCHECK_NE(OK, result);
553 DCHECK(stream_request_.get());
554 DCHECK(!stream_.get());
555
556 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14557 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38558
559 // TODO(mbelshe): For now, we're going to pass the error through, and that
560 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50561 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
562 // good and the user chooses to ignore the error. This is not ideal, but not
563 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38564
565 OnIOComplete(result);
566}
567
568void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50569 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31570 const SSLConfig& used_ssl_config,
571 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50572 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38573 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50574 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38575
576 establishing_tunnel_ = true;
577 response_.headers = proxy_response.headers;
578 response_.auth_challenge = proxy_response.auth_challenge;
579 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14580 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31581 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38582
583 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
584 pending_auth_target_ = HttpAuth::AUTH_PROXY;
585
586 DoCallback(OK);
587}
588
589void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31590 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50591 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50592 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38593
[email protected]102957f2011-09-02 17:10:14594 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38595 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58596 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38597}
598
[email protected]511f6f52010-12-17 03:58:29599void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
600 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31601 const SSLConfig& used_ssl_config,
602 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39603 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29604 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
605
ttuttle1f2d7e92015-04-28 16:17:47606 CopyConnectionAttemptsFromStreamRequest();
607
[email protected]511f6f52010-12-17 03:58:29608 headers_valid_ = true;
609 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14610 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31611 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22612 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19613 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22614 total_sent_bytes_ += stream_->GetTotalSentBytes();
615 }
[email protected]511f6f52010-12-17 03:58:29616 stream_.reset(stream);
617 stream_request_.reset(); // we're done with the stream request
618 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
619}
620
zhongyi48704c182015-12-07 07:52:02621void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12622 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02623}
624
ttuttle1f2d7e92015-04-28 16:17:47625void HttpNetworkTransaction::GetConnectionAttempts(
626 ConnectionAttempts* out) const {
627 *out = connection_attempts_;
628}
629
Adam Ricecb76ac62015-02-20 05:33:25630bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37631 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52632}
633
Adam Rice425cf122015-01-19 06:18:24634bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12635 return (proxy_info_.is_http() || proxy_info_.is_https() ||
636 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24637 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
638}
639
initial.commit586acc5fe2008-07-26 22:42:52640void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50641 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41642 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52643
[email protected]96d570e42008-08-05 22:43:04644 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41645 CompletionCallback c = callback_;
646 callback_.Reset();
647 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52648}
649
650void HttpNetworkTransaction::OnIOComplete(int result) {
651 int rv = DoLoop(result);
652 if (rv != ERR_IO_PENDING)
653 DoCallback(rv);
654}
655
656int HttpNetworkTransaction::DoLoop(int result) {
657 DCHECK(next_state_ != STATE_NONE);
658
659 int rv = result;
660 do {
661 State state = next_state_;
662 next_state_ = STATE_NONE;
663 switch (state) {
[email protected]1826a402014-01-08 15:40:48664 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
665 DCHECK_EQ(OK, rv);
666 rv = DoNotifyBeforeCreateStream();
667 break;
[email protected]82918cc2010-08-25 17:24:50668 case STATE_CREATE_STREAM:
669 DCHECK_EQ(OK, rv);
670 rv = DoCreateStream();
671 break;
672 case STATE_CREATE_STREAM_COMPLETE:
673 rv = DoCreateStreamComplete(rv);
674 break;
[email protected]351ab642010-08-05 16:55:31675 case STATE_INIT_STREAM:
676 DCHECK_EQ(OK, rv);
677 rv = DoInitStream();
678 break;
679 case STATE_INIT_STREAM_COMPLETE:
680 rv = DoInitStreamComplete(rv);
681 break;
[email protected]044de0642010-06-17 10:42:15682 case STATE_GENERATE_PROXY_AUTH_TOKEN:
683 DCHECK_EQ(OK, rv);
684 rv = DoGenerateProxyAuthToken();
685 break;
686 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
687 rv = DoGenerateProxyAuthTokenComplete(rv);
688 break;
689 case STATE_GENERATE_SERVER_AUTH_TOKEN:
690 DCHECK_EQ(OK, rv);
691 rv = DoGenerateServerAuthToken();
692 break;
693 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
694 rv = DoGenerateServerAuthTokenComplete(rv);
695 break;
[email protected]daddea62012-09-19 05:51:13696 case STATE_INIT_REQUEST_BODY:
697 DCHECK_EQ(OK, rv);
698 rv = DoInitRequestBody();
699 break;
700 case STATE_INIT_REQUEST_BODY_COMPLETE:
701 rv = DoInitRequestBodyComplete(rv);
702 break;
[email protected]4875ba12011-03-30 22:31:51703 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55704 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38705 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51706 rv = DoBuildRequest();
707 break;
708 case STATE_BUILD_REQUEST_COMPLETE:
709 rv = DoBuildRequestComplete(rv);
710 break;
711 case STATE_SEND_REQUEST:
712 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57713 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52714 break;
[email protected]0877e3d2009-10-17 22:29:57715 case STATE_SEND_REQUEST_COMPLETE:
716 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43717 net_log_.EndEventWithNetErrorCode(
718 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52719 break;
720 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55721 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38722 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52723 rv = DoReadHeaders();
724 break;
725 case STATE_READ_HEADERS_COMPLETE:
726 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43727 net_log_.EndEventWithNetErrorCode(
728 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52729 break;
730 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55731 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38732 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52733 rv = DoReadBody();
734 break;
735 case STATE_READ_BODY_COMPLETE:
736 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43737 net_log_.EndEventWithNetErrorCode(
738 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52739 break;
[email protected]2d2697f92009-02-18 21:00:32740 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55741 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53742 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38743 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32744 rv = DoDrainBodyForAuthRestart();
745 break;
746 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
747 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43748 net_log_.EndEventWithNetErrorCode(
749 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32750 break;
initial.commit586acc5fe2008-07-26 22:42:52751 default:
752 NOTREACHED() << "bad state";
753 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04754 break;
initial.commit586acc5fe2008-07-26 22:42:52755 }
756 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
757
758 return rv;
759}
760
[email protected]1826a402014-01-08 15:40:48761int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
762 next_state_ = STATE_CREATE_STREAM;
763 bool defer = false;
764 if (!before_network_start_callback_.is_null())
765 before_network_start_callback_.Run(&defer);
766 if (!defer)
767 return OK;
768 return ERR_IO_PENDING;
769}
770
[email protected]82918cc2010-08-25 17:24:50771int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34772 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26773 tracked_objects::ScopedTracker tracking_profile(
774 FROM_HERE_WITH_EXPLICIT_FUNCTION(
775 "424359 HttpNetworkTransaction::DoCreateStream"));
776
ttuttlec0c828492015-05-15 01:25:55777 response_.network_accessed = true;
778
[email protected]82918cc2010-08-25 17:24:50779 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44780 if (ForWebSocketHandshake()) {
781 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23782 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44783 ->RequestWebSocketHandshakeStream(
784 *request_,
785 priority_,
786 server_ssl_config_,
787 proxy_ssl_config_,
788 this,
789 websocket_handshake_stream_base_create_helper_,
790 net_log_));
791 } else {
792 stream_request_.reset(
793 session_->http_stream_factory()->RequestStream(
794 *request_,
795 priority_,
796 server_ssl_config_,
797 proxy_ssl_config_,
798 this,
799 net_log_));
800 }
[email protected]26816882010-10-14 18:03:09801 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38802 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31803}
804
[email protected]82918cc2010-08-25 17:24:50805int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47806 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
807 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
808 // which resets the stream request first. Therefore, we have to grab the
809 // connection attempts in *that* function instead of here in that case.
810 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
811 CopyConnectionAttemptsFromStreamRequest();
812
davidben701ca982015-05-18 21:21:42813 if (request_->url.SchemeIsCryptographic())
814 RecordSSLFallbackMetrics(result);
815
[email protected]394816e92010-08-03 07:38:59816 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50817 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38818 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33819 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
820 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29821 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
822 // Return OK and let the caller read the proxy's error page
823 next_state_ = STATE_NONE;
824 return OK;
bncfacdd852015-01-09 19:22:54825 } else if (result == ERR_HTTP_1_1_REQUIRED ||
826 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
827 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59828 }
829
[email protected]bd0b6772011-01-11 19:59:30830 // Handle possible handshake errors that may have occurred if the stream
831 // used SSL for one or more of the layers.
832 result = HandleSSLHandshakeError(result);
833
[email protected]8e6441ca2010-08-19 05:56:38834 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09835 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38836 return result;
[email protected]394816e92010-08-03 07:38:59837}
838
[email protected]82918cc2010-08-25 17:24:50839int HttpNetworkTransaction::DoInitStream() {
840 DCHECK(stream_.get());
841 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59842
843 stream_->GetRemoteEndpoint(&remote_endpoint_);
844
[email protected]262eec82013-03-19 21:01:36845 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50846}
847
848int HttpNetworkTransaction::DoInitStreamComplete(int result) {
849 if (result == OK) {
850 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50851 } else {
[email protected]82918cc2010-08-25 17:24:50852 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26853 result = HandleIOError(result);
854
855 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22856 if (stream_) {
857 total_received_bytes_ += stream_->GetTotalReceivedBytes();
858 total_sent_bytes_ += stream_->GetTotalSentBytes();
859 }
zhongyica364fbb2015-12-12 03:39:12860 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50861 }
862
863 return result;
864}
865
[email protected]044de0642010-06-17 10:42:15866int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
867 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
868 if (!ShouldApplyProxyAuth())
869 return OK;
[email protected]394816e92010-08-03 07:38:59870 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
871 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09872 auth_controllers_[target] =
873 new HttpAuthController(target,
874 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31875 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09876 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59877 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41878 io_callback_,
[email protected]394816e92010-08-03 07:38:59879 net_log_);
[email protected]044de0642010-06-17 10:42:15880}
881
882int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
883 DCHECK_NE(ERR_IO_PENDING, rv);
884 if (rv == OK)
885 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
886 return rv;
887}
888
889int HttpNetworkTransaction::DoGenerateServerAuthToken() {
890 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59891 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54892 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09893 auth_controllers_[target] =
894 new HttpAuthController(target,
895 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31896 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09897 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54898 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
899 auth_controllers_[target]->DisableEmbeddedIdentity();
900 }
[email protected]044de0642010-06-17 10:42:15901 if (!ShouldApplyServerAuth())
902 return OK;
[email protected]394816e92010-08-03 07:38:59903 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41904 io_callback_,
[email protected]394816e92010-08-03 07:38:59905 net_log_);
[email protected]044de0642010-06-17 10:42:15906}
907
908int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
909 DCHECK_NE(ERR_IO_PENDING, rv);
910 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13911 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15912 return rv;
913}
914
Adam Rice425cf122015-01-19 06:18:24915void HttpNetworkTransaction::BuildRequestHeaders(
916 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14917 request_headers_.SetHeader(HttpRequestHeaders::kHost,
918 GetHostAndOptionalPort(request_->url));
919
920 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24921 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14922 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
923 "keep-alive");
924 } else {
925 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
926 }
927
[email protected]2979a492011-04-06 00:29:14928 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11929 if (request_->upload_data_stream) {
930 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14931 request_headers_.SetHeader(
932 HttpRequestHeaders::kTransferEncoding, "chunked");
933 } else {
934 request_headers_.SetHeader(
935 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11936 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14937 }
csharrisonf473dd192015-08-18 13:54:13938 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14939 // An empty POST/PUT request still needs a content length. As for HEAD,
940 // IE and Safari also add a content length header. Presumably it is to
941 // support sending a HEAD request to an URL that only expects to be sent a
942 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13943 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
944 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14945 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
946 }
947
948 // Honor load flags that impact proxy caches.
949 if (request_->load_flags & LOAD_BYPASS_CACHE) {
950 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
951 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
952 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
953 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
954 }
955
956 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
957 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
958 &request_headers_);
959 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
960 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
961 &request_headers_);
962
[email protected]c10450102011-06-27 09:06:16963 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20964
Adam Rice425cf122015-01-19 06:18:24965 if (using_http_proxy_without_tunnel &&
966 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20967 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
968
[email protected]173f8e22013-04-10 04:18:20969 response_.did_use_http_auth =
970 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
971 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14972}
973
[email protected]daddea62012-09-19 05:51:13974int HttpNetworkTransaction::DoInitRequestBody() {
975 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13976 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11977 if (request_->upload_data_stream)
978 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13979 return rv;
980}
[email protected]4875ba12011-03-30 22:31:51981
[email protected]daddea62012-09-19 05:51:13982int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
983 if (result == OK)
984 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13985 return result;
986}
987
988int HttpNetworkTransaction::DoBuildRequest() {
989 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51990 headers_valid_ = false;
991
992 // This is constructed lazily (instead of within our Start method), so that
993 // we have proxy info available.
994 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:24995 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
996 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51997 }
998
[email protected]4875ba12011-03-30 22:31:51999 return OK;
1000}
1001
1002int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241003 if (result == OK)
1004 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511005 return result;
1006}
1007
[email protected]0877e3d2009-10-17 22:29:571008int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341009 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261010 tracked_objects::ScopedTracker tracking_profile(
1011 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1012 "424359 HttpNetworkTransaction::DoSendRequest"));
1013
[email protected]58e32bb2013-01-21 18:23:251014 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571015 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1016
[email protected]bf3eb002012-11-15 05:50:111017 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521018}
1019
[email protected]0877e3d2009-10-17 22:29:571020int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251021 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:521022 if (result < 0)
1023 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571024 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521025 return OK;
1026}
1027
1028int HttpNetworkTransaction::DoReadHeaders() {
1029 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411030 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521031}
1032
1033int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111034 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1035 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381036 if (IsCertificateError(result)) {
1037 // We don't handle a certificate error during SSL renegotiation, so we
1038 // have to return an error that's not in the certificate error range
1039 // (-2xx).
1040 LOG(ERROR) << "Got a server certificate with error " << result
1041 << " during SSL renegotiation";
1042 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1043 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1044 // TODO(wtc): Need a test case for this code path!
1045 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251046 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381047 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501048 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381049 result = HandleCertificateRequest(result);
1050 if (result == OK)
1051 return result;
[email protected]2181ea002009-06-09 01:37:271052 }
1053
bncfacdd852015-01-09 19:22:541054 if (result == ERR_HTTP_1_1_REQUIRED ||
1055 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1056 return HandleHttp11Required(result);
1057 }
1058
[email protected]c871864d72014-03-13 19:56:191059 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1060 // response headers were received, we do the best we can to make sense of it
1061 // and send it back up the stack.
1062 //
1063 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1064 // bizarre for SPDY. Assuming this logic is useful at all.
1065 // TODO(davidben): Bubble the error code up so we do not cache?
1066 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1067 result = OK;
1068
1069 if (result < 0)
1070 return HandleIOError(result);
1071
[email protected]90499482013-06-01 00:39:501072 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521073
[email protected]d58ceea82014-06-04 10:55:541074 // On a 408 response from the server ("Request Timeout") on a stale socket,
1075 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041076 // Headers can be NULL because of http://crbug.com/384554.
1077 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541078 stream_->IsConnectionReused()) {
1079 net_log_.AddEventWithNetErrorCode(
1080 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1081 response_.headers->response_code());
1082 // This will close the socket - it would be weird to try and reuse it, even
1083 // if the server doesn't actually close it.
1084 ResetConnectionAndRequestForResend();
1085 return OK;
1086 }
1087
[email protected]93f8b562012-03-27 01:00:161088 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1089 if (request_->load_flags & LOAD_MAIN_FRAME) {
1090 const int response_code = response_.headers->response_code();
1091 UMA_HISTOGRAM_ENUMERATION(
1092 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1093 }
1094
[email protected]3abacd62012-06-10 20:20:321095 net_log_.AddEvent(
1096 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1097 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391098
bncbe0f6af2015-10-15 17:49:561099 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571100 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1101 // indicates a buggy server. See:
1102 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1103 if (request_->method == "PUT")
1104 return ERR_METHOD_NOT_SUPPORTED;
1105 }
[email protected]4ddaf2502008-10-23 18:26:191106
[email protected]0877e3d2009-10-17 22:29:571107 // Check for an intermediate 100 Continue response. An origin server is
1108 // allowed to send this response even if we didn't ask for it, so we just
1109 // need to skip over it.
1110 // We treat any other 1xx in this same way (although in practice getting
1111 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441112 // Unless this is a WebSocket request, in which case we pass it on up.
1113 if (response_.headers->response_code() / 100 == 1 &&
1114 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451115 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571116 next_state_ = STATE_READ_HEADERS;
1117 return OK;
1118 }
1119
bncc958faa2015-07-31 18:14:521120 ProcessAlternativeServices(session_, *response_.headers.get(),
1121 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421122
[email protected]e772db3f2010-07-12 18:11:131123 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571124 if (rv != OK)
1125 return rv;
1126
Adam Ricecb76ac62015-02-20 05:33:251127 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211128 stream_->GetSSLInfo(&response_.ssl_info);
1129
[email protected]0877e3d2009-10-17 22:29:571130 headers_valid_ = true;
1131 return OK;
initial.commit586acc5fe2008-07-26 22:42:521132}
1133
1134int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501135 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131136 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381137 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521138
1139 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501140 return stream_->ReadResponseBody(
1141 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521142}
1143
1144int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1145 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381146 bool done = false;
1147 if (result <= 0) {
1148 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521149 done = true;
[email protected]8e6441ca2010-08-19 05:56:381150 }
[email protected]9492e4a2010-02-24 00:58:461151
mmenkebd84c392015-09-02 14:12:341152 // Clean up connection if we are done.
1153 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381154 // Note: Just because IsResponseBodyComplete is true, we're not
1155 // necessarily "done". We're only "done" when it is the last
1156 // read on this HttpNetworkTransaction, which will be signified
1157 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341158 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381159 // the stream. No need to compute it here just to pass back
1160 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341161 bool keep_alive =
1162 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521163
[email protected]8e6441ca2010-08-19 05:56:381164 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011165 // Note: we don't reset the stream here. We've closed it, but we still
1166 // need it around so that callers can call methods such as
1167 // GetUploadProgress() and have them be meaningful.
1168 // TODO(mbelshe): This means we closed the stream here, and we close it
1169 // again in ~HttpNetworkTransaction. Clean that up.
1170
[email protected]8e6441ca2010-08-19 05:56:381171 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521172 }
1173
1174 // Clear these to avoid leaving around old state.
1175 read_buf_ = NULL;
1176 read_buf_len_ = 0;
1177
1178 return result;
1179}
1180
[email protected]2d2697f92009-02-18 21:00:321181int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1182 // This method differs from DoReadBody only in the next_state_. So we just
1183 // call DoReadBody and override the next_state_. Perhaps there is a more
1184 // elegant way for these two methods to share code.
1185 int rv = DoReadBody();
1186 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1187 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1188 return rv;
1189}
1190
[email protected]0877e3d2009-10-17 22:29:571191// TODO(wtc): This method and the DoReadBodyComplete method are almost
1192// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321193int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231194 // keep_alive defaults to true because the very reason we're draining the
1195 // response body is to reuse the connection for auth restart.
1196 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321197 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571198 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321199 done = true;
[email protected]68873ba2009-06-04 21:49:231200 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311201 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571202 done = true;
[email protected]2d2697f92009-02-18 21:00:321203 }
1204
1205 if (done) {
1206 DidDrainBodyForAuthRestart(keep_alive);
1207 } else {
1208 // Keep draining.
1209 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1210 }
1211
1212 return OK;
1213}
1214
[email protected]5e363962009-06-19 19:57:011215int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381216 // There are two paths through which the server can request a certificate
1217 // from us. The first is during the initial handshake, the second is
1218 // during SSL renegotiation.
1219 //
1220 // In both cases, we want to close the connection before proceeding.
1221 // We do this for two reasons:
1222 // First, we don't want to keep the connection to the server hung for a
1223 // long time while the user selects a certificate.
1224 // Second, even if we did keep the connection open, NSS has a bug where
1225 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581226 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381227
1228 if (stream_.get()) {
1229 // Since we already have a stream, we're being called as part of SSL
1230 // renegotiation.
1231 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191232 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221233 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381234 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121235 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381236 }
1237
[email protected]26816882010-10-14 18:03:091238 // The server is asking for a client certificate during the initial
1239 // handshake.
1240 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011241
[email protected]ec229bc92010-11-22 09:51:451242 // If the user selected one of the certificates in client_certs or declined
1243 // to provide one for this server before, use the past decision
1244 // automatically.
1245 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541246 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451247 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541248 response_.cert_request_info->host_and_port, &client_cert,
1249 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451250 if (!found_cached_cert)
1251 return error;
1252
1253 // Check that the certificate selected is still a certificate the server
1254 // is likely to accept, based on the criteria supplied in the
1255 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501256 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261257 const std::vector<std::string>& cert_authorities =
1258 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451259
[email protected]f1958c382013-02-07 00:15:261260 bool cert_still_valid = cert_authorities.empty() ||
1261 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451262 if (!cert_still_valid)
1263 return error;
[email protected]5e363962009-06-19 19:57:011264 }
[email protected]ec229bc92010-11-22 09:51:451265
1266 // TODO(davidben): Add a unit test which covers this path; we need to be
1267 // able to send a legitimate certificate and also bypass/clear the
1268 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141269 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1270 &proxy_ssl_config_ : &server_ssl_config_;
1271 ssl_config->send_client_cert = true;
1272 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541273 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451274 next_state_ = STATE_CREATE_STREAM;
1275 // Reset the other member variables.
1276 // Note: this is necessary only with SSL renegotiation.
1277 ResetStateForRestart();
1278 return OK;
[email protected]0b45559b2009-06-12 21:45:111279}
1280
bncfacdd852015-01-09 19:22:541281int HttpNetworkTransaction::HandleHttp11Required(int error) {
1282 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1283 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1284
1285 if (error == ERR_HTTP_1_1_REQUIRED) {
1286 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1287 } else {
1288 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1289 }
1290 ResetConnectionAndRequestForResend();
1291 return OK;
1292}
1293
[email protected]384cc73a2013-12-08 22:41:031294void HttpNetworkTransaction::HandleClientAuthError(int error) {
1295 if (server_ssl_config_.send_client_cert &&
1296 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1297 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411298 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031299 }
1300}
1301
[email protected]bd0b6772011-01-11 19:59:301302// TODO(rch): This does not correctly handle errors when an SSL proxy is
1303// being used, as all of the errors are handled as if they were generated
1304// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381305// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301306int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1307 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031308 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301309
davidbena4c9d062015-04-03 22:34:251310 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1311 // reflect servers require a deprecated cipher rather than merely prefer
1312 // it. This, however, has no security benefit until the ciphers are actually
1313 // removed.
davidben14b1a532015-10-30 16:01:091314 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251315 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1316 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1317 net_log_.AddEvent(
1318 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1319 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091320 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251321 ResetConnectionAndRequestForResend();
1322 return OK;
1323 }
1324
[email protected]02d1d442013-08-10 13:38:261325 bool should_fallback = false;
Avi Drissman13fc8932015-12-20 04:40:461326 uint16_t version_max = server_ssl_config_.version_max;
[email protected]158ac972013-04-19 23:29:231327
initial.commit586acc5fe2008-07-26 22:42:521328 switch (error) {
[email protected]12833302014-07-02 01:57:311329 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301330 case ERR_SSL_PROTOCOL_ERROR:
1331 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231332 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1333 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171334 // This could be a TLS-intolerant server or a server that chose a
1335 // cipher suite defined only for higher protocol versions (such as
1336 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1337 // back to the next lower version and retry.
1338 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1339 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1340 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1341 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1342 // version_max should match the maximum protocol version supported
1343 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231344 version_max--;
[email protected]c57ec022013-11-25 21:30:511345
1346 // Fallback to the lower SSL version.
1347 // While SSL 3.0 fallback should be eliminated because of security
1348 // reasons, there is a high risk of breaking the servers if this is
1349 // done in general.
[email protected]001e547e2013-12-06 16:08:391350 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421351 }
initial.commit586acc5fe2008-07-26 22:42:521352 break;
[email protected]12833302014-07-02 01:57:311353 case ERR_CONNECTION_RESET:
1354 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1355 version_max > server_ssl_config_.version_min) {
1356 // Some network devices that inspect application-layer packets seem to
1357 // inject TCP reset packets to break the connections when they see TLS
1358 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1359 //
1360 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1361 // 1.2. We don't lose much in this fallback because the explicit IV for
1362 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1363 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1364 // support.
1365 //
1366 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1367 // to trigger a version fallback in general, especially the TLS 1.0 ->
1368 // SSL 3.0 fallback, which would drop TLS extensions.
1369 version_max--;
1370 should_fallback = true;
1371 }
1372 break;
[email protected]02d1d442013-08-10 13:38:261373 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1374 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1375 version_max > server_ssl_config_.version_min) {
1376 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1377 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1378 // crbug.com/260358. In order to make the fallback as minimal as
1379 // possible, this fallback is only triggered for >= TLS 1.1.
1380 version_max--;
1381 should_fallback = true;
1382 }
1383 break;
[email protected]a53e4d12013-12-07 16:37:241384 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1385 // The server told us that we should not have fallen back. A buggy server
1386 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1387 // connection. |fallback_error_code_| is initialised to
1388 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1389 error = fallback_error_code_;
1390 break;
[email protected]02d1d442013-08-10 13:38:261391 }
1392
1393 if (should_fallback) {
1394 net_log_.AddEvent(
1395 NetLog::TYPE_SSL_VERSION_FALLBACK,
davidbenf2eaaf92015-05-15 22:18:421396 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1397 server_ssl_failure_state_, server_ssl_config_.version_max,
[email protected]02d1d442013-08-10 13:38:261398 version_max));
[email protected]a53e4d12013-12-07 16:37:241399 fallback_error_code_ = error;
davidbenf2eaaf92015-05-15 22:18:421400 fallback_failure_state_ = server_ssl_failure_state_;
[email protected]02d1d442013-08-10 13:38:261401 server_ssl_config_.version_max = version_max;
1402 server_ssl_config_.version_fallback = true;
1403 ResetConnectionAndRequestForResend();
1404 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521405 }
[email protected]158ac972013-04-19 23:29:231406
initial.commit586acc5fe2008-07-26 22:42:521407 return error;
1408}
1409
[email protected]bd0b6772011-01-11 19:59:301410// This method determines whether it is safe to resend the request after an
1411// IO error. It can only be called in response to request header or body
1412// write errors or response header read errors. It should not be used in
1413// other cases, such as a Connect error.
1414int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031415 // Because the peer may request renegotiation with client authentication at
1416 // any time, check and handle client authentication errors.
1417 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301418
1419 switch (error) {
1420 // If we try to reuse a connection that the server is in the process of
1421 // closing, we may end up successfully writing out our request (or a
1422 // portion of our request) only to find a connection error when we try to
1423 // read from (or finish writing to) the socket.
1424 case ERR_CONNECTION_RESET:
1425 case ERR_CONNECTION_CLOSED:
1426 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511427 // There can be a race between the socket pool checking checking whether a
1428 // socket is still connected, receiving the FIN, and sending/reading data
1429 // on a reused socket. If we receive the FIN between the connectedness
1430 // check and writing/reading from the socket, we may first learn the socket
1431 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1432 // likely happen when trying to retrieve its IP address.
1433 // See http://crbug.com/105824 for more details.
1434 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491435 // If a socket is closed on its initial request, HttpStreamParser returns
1436 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1437 // preconnected but failed to be used before the server timed it out.
1438 case ERR_EMPTY_RESPONSE:
1439 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381440 net_log_.AddEventWithNetErrorCode(
1441 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301442 ResetConnectionAndRequestForResend();
1443 error = OK;
1444 }
1445 break;
[email protected]8753a122011-10-16 08:05:081446 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001447 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271448 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381449 net_log_.AddEventWithNetErrorCode(
1450 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001451 ResetConnectionAndRequestForResend();
1452 error = OK;
1453 break;
[email protected]bd0b6772011-01-11 19:59:301454 }
1455 return error;
1456}
1457
[email protected]c3b35c22008-09-27 03:19:421458void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581459 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221460 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191461 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221462 total_sent_bytes_ += stream_->GetTotalSentBytes();
1463 }
zhongyica364fbb2015-12-12 03:39:121464 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581465}
1466
1467void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251468 send_start_time_ = base::TimeTicks();
1469 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251470
[email protected]0757e7702009-03-27 04:00:221471 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421472 read_buf_ = NULL;
1473 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571474 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201475 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141476 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381477 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591478 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121479 net_error_details_.quic_broken = false;
1480 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
1481}
1482
1483void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1484 if (stream_)
1485 stream_->PopulateNetErrorDetails(&net_error_details_);
1486 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571487}
1488
davidben701ca982015-05-18 21:21:421489void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1490 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1491 return;
1492
1493 const std::string& host = request_->url.host();
brettwa7ff1b292015-07-16 17:49:291494 bool is_google = base::EndsWith(host, "google.com",
1495 base::CompareCase::SENSITIVE) &&
davidben701ca982015-05-18 21:21:421496 (host.size() == 10 || host[host.size() - 11] == '.');
1497 if (is_google) {
1498 // Some fraction of successful connections use the fallback, but only due to
1499 // a spurious network failure. To estimate this fraction, compare handshakes
1500 // to Google servers which succeed against those that fail with an
1501 // inappropriate_fallback alert. Google servers are known to implement
1502 // FALLBACK_SCSV, so a spurious network failure while connecting would
1503 // trigger the fallback, successfully connect, but fail with this alert.
1504 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1505 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1506 }
1507
1508 if (result != OK)
1509 return;
1510
davidbenca9d69162015-05-04 20:18:451511 // Note: these values are used in histograms, so new values must be appended.
1512 enum FallbackVersion {
davidbenb937d6c2015-05-14 04:53:421513 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1514 // Obsolete: FALLBACK_SSL3 = 1,
davidbenca9d69162015-05-04 20:18:451515 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1516 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1517 FALLBACK_MAX,
1518 };
1519
1520 FallbackVersion fallback = FALLBACK_NONE;
1521 if (server_ssl_config_.version_fallback) {
1522 switch (server_ssl_config_.version_max) {
davidbenca9d69162015-05-04 20:18:451523 case SSL_PROTOCOL_VERSION_TLS1:
1524 fallback = FALLBACK_TLS1;
1525 break;
1526 case SSL_PROTOCOL_VERSION_TLS1_1:
1527 fallback = FALLBACK_TLS1_1;
1528 break;
1529 default:
1530 NOTREACHED();
1531 }
1532 }
1533 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1534 FALLBACK_MAX);
1535
1536 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1537 // should be impossible to successfully connect to them with the fallback.
1538 // This helps estimate intolerant locally-configured SSL MITMs.
davidben701ca982015-05-18 21:21:421539 if (is_google) {
davidbenca9d69162015-05-04 20:18:451540 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1541 fallback, FALLBACK_MAX);
1542 }
1543
1544 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091545 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenf2eaaf92015-05-15 22:18:421546
1547 if (server_ssl_config_.version_fallback) {
1548 // Record the error code which triggered the fallback and the state the
1549 // handshake was in.
1550 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1551 -fallback_error_code_);
1552 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1553 fallback_failure_state_, SSL_FAILURE_MAX);
1554 }
davidbenca9d69162015-05-04 20:18:451555}
1556
[email protected]0877e3d2009-10-17 22:29:571557HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501558 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421559}
1560
[email protected]a34f61ee2014-03-18 20:59:491561bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381562 bool connection_is_proven = stream_->IsConnectionReused();
1563 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161564
[email protected]2a5c76b2008-09-25 22:15:161565 // NOTE: we resend a request only if we reused a keep-alive connection.
1566 // This automatically prevents an infinite resend loop because we'll run
1567 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381568 if (connection_is_proven && !has_received_headers)
1569 return true;
1570 return false;
[email protected]1c773ea12009-04-28 19:58:421571}
1572
1573void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381574 if (stream_.get()) {
1575 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121576 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161577 }
1578
[email protected]0877e3d2009-10-17 22:29:571579 // We need to clear request_headers_ because it contains the real request
1580 // headers, but we may need to resend the CONNECT request first to recreate
1581 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201582 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501583 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541584}
1585
[email protected]1c773ea12009-04-28 19:58:421586bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241587 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421588}
license.botbf09a502008-08-24 00:55:551589
[email protected]1c773ea12009-04-28 19:58:421590bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041591 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421592}
1593
[email protected]e772db3f2010-07-12 18:11:131594int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271595 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501596 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421597
[email protected]0877e3d2009-10-17 22:29:571598 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581599 if (status != HTTP_UNAUTHORIZED &&
1600 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421601 return OK;
[email protected]9094b602012-02-27 21:44:581602 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111603 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161604 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1605 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421606
[email protected]9094b602012-02-27 21:44:581607 // This case can trigger when an HTTPS server responds with a "Proxy
1608 // authentication required" status code through a non-authenticating
1609 // proxy.
[email protected]7a67a8152010-11-05 18:31:101610 if (!auth_controllers_[target].get())
1611 return ERR_UNEXPECTED_PROXY_AUTH;
1612
[email protected]a7ea8832010-07-12 17:54:541613 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311614 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1615 net_log_);
[email protected]228404f2010-06-24 04:31:411616 if (auth_controllers_[target]->HaveAuthHandler())
1617 pending_auth_target_ = target;
1618
1619 scoped_refptr<AuthChallengeInfo> auth_info =
1620 auth_controllers_[target]->auth_info();
1621 if (auth_info.get())
1622 response_.auth_challenge = auth_info;
1623
[email protected]228404f2010-06-24 04:31:411624 return rv;
[email protected]f9ee6b52008-11-08 06:46:231625}
1626
[email protected]8e6441ca2010-08-19 05:56:381627bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1628 return auth_controllers_[target].get() &&
1629 auth_controllers_[target]->HaveAuth();
1630}
1631
[email protected]228404f2010-06-24 04:31:411632GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1633 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461634 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411635 if (!proxy_info_.proxy_server().is_valid() ||
1636 proxy_info_.proxy_server().is_direct()) {
1637 return GURL(); // There is no proxy server.
1638 }
[email protected]2df19bb2010-08-25 20:13:461639 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1640 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351641 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461642 }
[email protected]228404f2010-06-24 04:31:411643 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291644 if (ForWebSocketHandshake()) {
1645 const GURL& url = request_->url;
1646 url::Replacements<char> ws_to_http;
1647 if (url.SchemeIs("ws")) {
1648 ws_to_http.SetScheme("http", url::Component(0, 4));
1649 } else {
1650 DCHECK(url.SchemeIs("wss"));
1651 ws_to_http.SetScheme("https", url::Component(0, 5));
1652 }
1653 return url.ReplaceComponents(ws_to_http);
1654 }
[email protected]228404f2010-06-24 04:31:411655 return request_->url;
1656 default:
1657 return GURL();
1658 }
[email protected]c3b35c22008-09-27 03:19:421659}
1660
[email protected]831e4a32013-11-14 02:14:441661bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141662 return websocket_handshake_stream_base_create_helper_ &&
1663 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441664}
1665
[email protected]d8eb84242010-09-25 02:25:061666#define STATE_CASE(s) \
1667 case s: \
1668 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1669 break
[email protected]aef04272010-06-28 18:03:041670
1671std::string HttpNetworkTransaction::DescribeState(State state) {
1672 std::string description;
1673 switch (state) {
[email protected]1826a402014-01-08 15:40:481674 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501675 STATE_CASE(STATE_CREATE_STREAM);
1676 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131677 STATE_CASE(STATE_INIT_REQUEST_BODY);
1678 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511679 STATE_CASE(STATE_BUILD_REQUEST);
1680 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041681 STATE_CASE(STATE_SEND_REQUEST);
1682 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1683 STATE_CASE(STATE_READ_HEADERS);
1684 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041685 STATE_CASE(STATE_READ_BODY);
1686 STATE_CASE(STATE_READ_BODY_COMPLETE);
1687 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1688 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041689 STATE_CASE(STATE_NONE);
1690 default:
[email protected]d8eb84242010-09-25 02:25:061691 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1692 state);
[email protected]aef04272010-06-28 18:03:041693 break;
1694 }
1695 return description;
1696}
1697
1698#undef STATE_CASE
1699
ttuttle1f2d7e92015-04-28 16:17:471700void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1701 DCHECK(stream_request_);
1702
1703 // Since the transaction can restart with auth credentials, it may create a
1704 // stream more than once. Accumulate all of the connection attempts across
1705 // those streams by appending them to the vector:
1706 for (const auto& attempt : stream_request_->connection_attempts())
1707 connection_attempts_.push_back(attempt);
1708}
1709
[email protected]c3b35c22008-09-27 03:19:421710} // namespace net