blob: e0c41002ee1fa8b78fdaed33b599b03ea928674e [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"
[email protected]f89276a72013-07-12 06:41:5463#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2964#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5265
initial.commit586acc5fe2008-07-26 22:42:5266namespace net {
67
[email protected]1c773ea12009-04-28 19:58:4268namespace {
69
bncc958faa2015-07-31 18:14:5270void ProcessAlternativeServices(HttpNetworkSession* session,
71 const HttpResponseHeaders& headers,
72 const HostPortPair& http_host_port_pair) {
bnc54ec34b72015-08-26 19:34:5673 if (session->params().use_alternative_services &&
74 headers.HasHeader(kAlternativeServiceHeader)) {
bncc958faa2015-07-31 18:14:5275 std::string alternative_service_str;
76 headers.GetNormalizedHeader(kAlternativeServiceHeader,
77 &alternative_service_str);
78 session->http_stream_factory()->ProcessAlternativeService(
79 session->http_server_properties(), alternative_service_str,
80 http_host_port_pair, *session);
81 // If there is an "Alt-Svc" header, then ignore "Alternate-Protocol".
82 return;
83 }
84
[email protected]287d9412014-07-08 23:01:0085 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3886 return;
[email protected]287d9412014-07-08 23:01:0087
88 std::vector<std::string> alternate_protocol_values;
89 void* iter = NULL;
90 std::string alternate_protocol_str;
91 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
92 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5293 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
94 &alternate_protocol_str);
95 if (!alternate_protocol_str.empty()) {
96 alternate_protocol_values.push_back(alternate_protocol_str);
97 }
[email protected]564b4912010-03-09 16:30:4298 }
99
[email protected]d7599122014-05-24 03:37:23100 session->http_stream_factory()->ProcessAlternateProtocol(
101 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:00102 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:23103 http_host_port_pair,
104 *session);
[email protected]f45c1ee2010-08-03 00:54:30105}
106
estade5e5529d2015-05-21 20:59:11107scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
[email protected]ea5ef4c2013-06-13 22:50:27108 const GURL* url,
109 int net_error,
davidbenf2eaaf92015-05-15 22:18:42110 SSLFailureState ssl_failure_state,
[email protected]ea5ef4c2013-06-13 22:50:27111 uint16 version_before,
112 uint16 version_after,
eroman001c3742015-04-23 03:11:17113 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11114 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b6754252012-06-13 23:14:38115 dict->SetString("host_and_port", GetHostAndPort(*url));
116 dict->SetInteger("net_error", net_error);
davidbenf2eaaf92015-05-15 22:18:42117 dict->SetInteger("ssl_failure_state", ssl_failure_state);
[email protected]b6754252012-06-13 23:14:38118 dict->SetInteger("version_before", version_before);
119 dict->SetInteger("version_after", version_after);
estade5e5529d2015-05-21 20:59:11120 return dict.Pass();
[email protected]b6754252012-06-13 23:14:38121}
[email protected]db74b0102012-05-31 19:55:53122
estade5e5529d2015-05-21 20:59:11123scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:17124 const GURL* url,
125 int net_error,
126 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11127 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:25128 dict->SetString("host_and_port", GetHostAndPort(*url));
129 dict->SetInteger("net_error", net_error);
estade5e5529d2015-05-21 20:59:11130 return dict.Pass();
davidbena4c9d062015-04-03 22:34:25131}
132
[email protected]1c773ea12009-04-28 19:58:42133} // namespace
134
initial.commit586acc5fe2008-07-26 22:42:52135//-----------------------------------------------------------------------------
136
[email protected]262eec82013-03-19 21:01:36137HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
138 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22139 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32140 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
141 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52142 session_(session),
143 request_(NULL),
[email protected]262eec82013-03-19 21:01:36144 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57145 headers_valid_(false),
davidbenf2eaaf92015-05-15 22:18:42146 server_ssl_failure_state_(SSL_FAILURE_NONE),
[email protected]a53e4d12013-12-07 16:37:24147 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
davidbenf2eaaf92015-05-15 22:18:42148 fallback_failure_state_(SSL_FAILURE_NONE),
[email protected]b94f92d2010-10-27 16:45:20149 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52150 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19151 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22152 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54153 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44154 establishing_tunnel_(false),
155 websocket_handshake_stream_base_create_helper_(NULL) {
[email protected]102957f2011-09-02 17:10:14156 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
bnc1f295372015-10-21 23:24:22157 session->GetAlpnProtos(&server_ssl_config_.alpn_protos);
158 session->GetNpnProtos(&server_ssl_config_.npn_protos);
[email protected]99ffa5a2011-10-06 04:20:19159 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50160}
161
[email protected]0b0bf032010-09-21 18:08:50162HttpNetworkTransaction::~HttpNetworkTransaction() {
163 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50164 // TODO(mbelshe): The stream_ should be able to compute whether or not the
165 // stream should be kept alive. No reason to compute here
166 // and pass it in.
mmenkebd84c392015-09-02 14:12:34167 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50168 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34169 } else if (stream_->IsResponseBodyComplete()) {
170 // If the response body is complete, we can just reuse the socket.
171 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50172 } else {
mmenkebd84c392015-09-02 14:12:34173 // Otherwise, we try to drain the response body.
174 HttpStream* stream = stream_.release();
175 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50176 }
177 }
[email protected]02cad5d2013-10-02 08:14:03178
179 if (request_ && request_->upload_data_stream)
180 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50181}
182
[email protected]684970b2009-08-14 04:54:46183int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41184 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53185 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53186 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04187 request_ = request_info;
188
[email protected]99ffa5a2011-10-06 04:20:19189 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14190 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19191 proxy_ssl_config_.rev_checking_enabled = false;
192 }
[email protected]6fbac162011-06-20 00:29:04193
jkarlinfb1d5172015-01-12 14:10:29194 if (request_->load_flags & LOAD_PREFETCH)
195 response_.unused_since_prefetch = true;
196
[email protected]1ea4f46a2014-04-22 22:33:56197 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49198 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
199 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40200
[email protected]1826a402014-01-08 15:40:48201 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04202 int rv = DoLoop(OK);
203 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41204 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04205 return rv;
206}
207
208int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41209 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38210 DCHECK(!stream_.get());
211 DCHECK(!stream_request_.get());
212 DCHECK_EQ(STATE_NONE, next_state_);
213
[email protected]82918cc2010-08-25 17:24:50214 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38215
[email protected]ccb40e52008-09-17 20:54:40216 int rv = DoLoop(OK);
217 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41218 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11219 return rv;
[email protected]96d570e42008-08-05 22:43:04220}
221
[email protected]0b45559b2009-06-12 21:45:11222int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41223 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38224 // In HandleCertificateRequest(), we always tear down existing stream
225 // requests to force a new connection. So we shouldn't have one here.
226 DCHECK(!stream_request_.get());
227 DCHECK(!stream_.get());
228 DCHECK_EQ(STATE_NONE, next_state_);
229
[email protected]102957f2011-09-02 17:10:14230 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
231 &proxy_ssl_config_ : &server_ssl_config_;
232 ssl_config->send_client_cert = true;
233 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45234 session_->ssl_client_auth_cache()->Add(
235 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11236 // Reset the other member variables.
237 // Note: this is necessary only with SSL renegotiation.
238 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50239 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11240 int rv = DoLoop(OK);
241 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41242 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11243 return rv;
244}
245
[email protected]49639fa2011-12-20 23:22:41246int HttpNetworkTransaction::RestartWithAuth(
247 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22248 HttpAuth::Target target = pending_auth_target_;
249 if (target == HttpAuth::AUTH_NONE) {
250 NOTREACHED();
251 return ERR_UNEXPECTED;
252 }
[email protected]0757e7702009-03-27 04:00:22253 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42254
[email protected]f3cf9802011-10-28 18:44:58255 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13256
[email protected]49639fa2011-12-20 23:22:41257 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38258
259 int rv = OK;
260 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
261 // In this case, we've gathered credentials for use with proxy
262 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50263 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38264 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59265 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54266 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58267 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54268 } else {
[email protected]8e6441ca2010-08-19 05:56:38269 // In this case, we've gathered credentials for the server or the proxy
270 // but it is not during the tunneling phase.
271 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54272 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38273 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54274 }
[email protected]c3b35c22008-09-27 03:19:42275
[email protected]c3b35c22008-09-27 03:19:42276 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41277 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42278 return rv;
[email protected]96d570e42008-08-05 22:43:04279}
280
[email protected]f9ee6b52008-11-08 06:46:23281void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
282 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38283 DCHECK(!stream_request_.get());
284
[email protected]2d2697f92009-02-18 21:00:32285 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57286 // Even if the server says the connection is keep-alive, we have to be
287 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34288 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57289 // If the response body hasn't been completely read, we need to drain
290 // it first.
[email protected]351ab642010-08-05 16:55:31291 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32292 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57293 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32294 read_buf_len_ = kDrainBodyBufferSize;
295 return;
296 }
[email protected]0877e3d2009-10-17 22:29:57297 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09298 }
299
[email protected]2d2697f92009-02-18 21:00:32300 // We don't need to drain the response body, so we act as if we had drained
301 // the response body.
302 DidDrainBodyForAuthRestart(keep_alive);
303}
304
305void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38306 DCHECK(!stream_request_.get());
307
308 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19309 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22310 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58311 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34312 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38313 // We should call connection_->set_idle_time(), but this doesn't occur
314 // often enough to be worth the trouble.
315 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39316 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38317 }
[email protected]697ef4c2010-10-14 16:38:58318
319 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46320 // Close the stream and mark it as not_reusable. Even in the
321 // keep_alive case, we've determined that the stream_ is not
322 // reusable if new_stream is NULL.
323 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58324 next_state_ = STATE_CREATE_STREAM;
325 } else {
sclittlefb249892015-09-10 21:33:22326 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19327 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22328 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58329 next_state_ = STATE_INIT_STREAM;
330 }
331 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32332 }
[email protected]f9ee6b52008-11-08 06:46:23333
334 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58335 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23336}
337
[email protected]8e6441ca2010-08-19 05:56:38338bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
339 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
340 HaveAuth(pending_auth_target_);
341}
342
[email protected]9dea9e1f2009-01-29 00:30:47343int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41344 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04345 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35346 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04347
[email protected]1f14a912009-12-21 20:32:44348 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04349
[email protected]ad8e04a2010-11-01 04:16:27350 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38351 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04352 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29353 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04354 // bytes when establishing a tunnel because they might be controlled by
355 // an active network attacker. We don't worry about this for HTTP
356 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29357 // We reach this case when the user cancels a 407 proxy auth prompt. We
358 // also don't worry about this for an HTTPS Proxy, because the
359 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04360 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46361 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58362 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54363 LOG(WARNING) << "Blocked proxy response with status "
364 << headers->response_code() << " to CONNECT request for "
365 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04366 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44367 }
368
[email protected]e60e47a2010-07-14 03:37:18369 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31370 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18371
[email protected]96d570e42008-08-05 22:43:04372 read_buf_ = buf;
373 read_buf_len_ = buf_len;
374
[email protected]1f14a912009-12-21 20:32:44375 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04376 int rv = DoLoop(OK);
377 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41378 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04379 return rv;
380}
381
[email protected]8cd06c02014-01-25 07:50:14382void HttpNetworkTransaction::StopCaching() {}
383
[email protected]79e1fd62013-06-20 06:50:04384bool HttpNetworkTransaction::GetFullRequestHeaders(
385 HttpRequestHeaders* headers) const {
386 // TODO(ttuttle): Make sure we've populated request_headers_.
387 *headers = request_headers_;
388 return true;
389}
390
sclittle4de1bab92015-09-22 21:28:24391int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
392 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19393 if (stream_)
394 total_received_bytes += stream_->GetTotalReceivedBytes();
395 return total_received_bytes;
396}
397
sclittlefb249892015-09-10 21:33:22398int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
399 int64_t total_sent_bytes = total_sent_bytes_;
400 if (stream_)
401 total_sent_bytes += stream_->GetTotalSentBytes();
402 return total_sent_bytes;
403}
404
[email protected]8cd06c02014-01-25 07:50:14405void HttpNetworkTransaction::DoneReading() {}
406
[email protected]96d570e42008-08-05 22:43:04407const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55408 return &response_;
[email protected]96d570e42008-08-05 22:43:04409}
410
411LoadState HttpNetworkTransaction::GetLoadState() const {
412 // TODO(wtc): Define a new LoadState value for the
413 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
414 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48415 case STATE_CREATE_STREAM:
416 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50417 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38418 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15419 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
420 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57421 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04422 return LOAD_STATE_SENDING_REQUEST;
423 case STATE_READ_HEADERS_COMPLETE:
424 return LOAD_STATE_WAITING_FOR_RESPONSE;
425 case STATE_READ_BODY_COMPLETE:
426 return LOAD_STATE_READING_RESPONSE;
427 default:
428 return LOAD_STATE_IDLE;
429 }
430}
431
[email protected]196d18a2012-08-30 03:47:31432UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31433 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31434 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04435
yhiranoa7e05bb2014-11-06 05:40:39436 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04437}
438
[email protected]8cd06c02014-01-25 07:50:14439void HttpNetworkTransaction::SetQuicServerInfo(
440 QuicServerInfo* quic_server_info) {}
441
[email protected]5033ab82013-03-22 20:17:46442bool HttpNetworkTransaction::GetLoadTimingInfo(
443 LoadTimingInfo* load_timing_info) const {
444 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
445 return false;
446
447 load_timing_info->proxy_resolve_start =
448 proxy_info_.proxy_resolve_start_time();
449 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
450 load_timing_info->send_start = send_start_time_;
451 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46452 return true;
453}
454
ttuttled9dbc652015-09-29 20:00:59455bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
456 if (!remote_endpoint_.address().size())
457 return false;
458
459 *endpoint = remote_endpoint_;
460 return true;
461}
462
[email protected]5033ab82013-03-22 20:17:46463void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
464 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47465 if (stream_request_)
466 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03467 if (stream_)
468 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46469}
470
[email protected]831e4a32013-11-14 02:14:44471void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
472 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
473 websocket_handshake_stream_base_create_helper_ = create_helper;
474}
475
[email protected]1826a402014-01-08 15:40:48476void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
477 const BeforeNetworkStartCallback& callback) {
478 before_network_start_callback_ = callback;
479}
480
[email protected]597a1ab2014-06-26 08:12:27481void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
482 const BeforeProxyHeadersSentCallback& callback) {
483 before_proxy_headers_sent_callback_ = callback;
484}
485
[email protected]1826a402014-01-08 15:40:48486int HttpNetworkTransaction::ResumeNetworkStart() {
487 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
488 return DoLoop(OK);
489}
490
[email protected]102e27c2011-02-23 01:01:31491void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
492 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39493 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50494 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38495 DCHECK(stream_request_.get());
496
sclittlefb249892015-09-10 21:33:22497 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19498 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22499 total_sent_bytes_ += stream_->GetTotalSentBytes();
500 }
[email protected]8e6441ca2010-08-19 05:56:38501 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14502 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31503 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38504 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51505 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
506 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38507 response_.was_fetched_via_spdy = stream_request_->using_spdy();
508 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15509 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
510 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38511 OnIOComplete(OK);
512}
513
[email protected]a9cf2b92013-10-30 12:08:49514void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50515 const SSLConfig& used_ssl_config,
516 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49517 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44518 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50519}
520
[email protected]102e27c2011-02-23 01:01:31521void HttpNetworkTransaction::OnStreamFailed(int result,
davidbenf2eaaf92015-05-15 22:18:42522 const SSLConfig& used_ssl_config,
523 SSLFailureState ssl_failure_state) {
[email protected]82918cc2010-08-25 17:24:50524 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38525 DCHECK_NE(OK, result);
526 DCHECK(stream_request_.get());
527 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14528 server_ssl_config_ = used_ssl_config;
davidbenf2eaaf92015-05-15 22:18:42529 server_ssl_failure_state_ = ssl_failure_state;
[email protected]8e6441ca2010-08-19 05:56:38530
531 OnIOComplete(result);
532}
533
[email protected]102e27c2011-02-23 01:01:31534void HttpNetworkTransaction::OnCertificateError(
535 int result,
536 const SSLConfig& used_ssl_config,
537 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50538 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38539 DCHECK_NE(OK, result);
540 DCHECK(stream_request_.get());
541 DCHECK(!stream_.get());
542
543 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14544 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38545
546 // TODO(mbelshe): For now, we're going to pass the error through, and that
547 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50548 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
549 // good and the user chooses to ignore the error. This is not ideal, but not
550 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38551
552 OnIOComplete(result);
553}
554
555void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50556 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31557 const SSLConfig& used_ssl_config,
558 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50559 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38560 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50561 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38562
563 establishing_tunnel_ = true;
564 response_.headers = proxy_response.headers;
565 response_.auth_challenge = proxy_response.auth_challenge;
566 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14567 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31568 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38569
570 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
571 pending_auth_target_ = HttpAuth::AUTH_PROXY;
572
573 DoCallback(OK);
574}
575
576void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31577 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50578 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50579 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38580
[email protected]102957f2011-09-02 17:10:14581 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38582 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58583 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38584}
585
[email protected]511f6f52010-12-17 03:58:29586void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
587 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31588 const SSLConfig& used_ssl_config,
589 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39590 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29591 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
592
ttuttle1f2d7e92015-04-28 16:17:47593 CopyConnectionAttemptsFromStreamRequest();
594
[email protected]511f6f52010-12-17 03:58:29595 headers_valid_ = true;
596 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14597 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31598 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22599 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19600 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22601 total_sent_bytes_ += stream_->GetTotalSentBytes();
602 }
[email protected]511f6f52010-12-17 03:58:29603 stream_.reset(stream);
604 stream_request_.reset(); // we're done with the stream request
605 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
606}
607
ttuttle1f2d7e92015-04-28 16:17:47608void HttpNetworkTransaction::GetConnectionAttempts(
609 ConnectionAttempts* out) const {
610 *out = connection_attempts_;
611}
612
Adam Ricecb76ac62015-02-20 05:33:25613bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37614 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52615}
616
Adam Rice425cf122015-01-19 06:18:24617bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12618 return (proxy_info_.is_http() || proxy_info_.is_https() ||
619 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24620 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
621}
622
initial.commit586acc5fe2008-07-26 22:42:52623void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50624 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41625 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52626
[email protected]96d570e42008-08-05 22:43:04627 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41628 CompletionCallback c = callback_;
629 callback_.Reset();
630 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52631}
632
633void HttpNetworkTransaction::OnIOComplete(int result) {
634 int rv = DoLoop(result);
635 if (rv != ERR_IO_PENDING)
636 DoCallback(rv);
637}
638
639int HttpNetworkTransaction::DoLoop(int result) {
640 DCHECK(next_state_ != STATE_NONE);
641
642 int rv = result;
643 do {
644 State state = next_state_;
645 next_state_ = STATE_NONE;
646 switch (state) {
[email protected]1826a402014-01-08 15:40:48647 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
648 DCHECK_EQ(OK, rv);
649 rv = DoNotifyBeforeCreateStream();
650 break;
[email protected]82918cc2010-08-25 17:24:50651 case STATE_CREATE_STREAM:
652 DCHECK_EQ(OK, rv);
653 rv = DoCreateStream();
654 break;
655 case STATE_CREATE_STREAM_COMPLETE:
656 rv = DoCreateStreamComplete(rv);
657 break;
[email protected]351ab642010-08-05 16:55:31658 case STATE_INIT_STREAM:
659 DCHECK_EQ(OK, rv);
660 rv = DoInitStream();
661 break;
662 case STATE_INIT_STREAM_COMPLETE:
663 rv = DoInitStreamComplete(rv);
664 break;
[email protected]044de0642010-06-17 10:42:15665 case STATE_GENERATE_PROXY_AUTH_TOKEN:
666 DCHECK_EQ(OK, rv);
667 rv = DoGenerateProxyAuthToken();
668 break;
669 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
670 rv = DoGenerateProxyAuthTokenComplete(rv);
671 break;
672 case STATE_GENERATE_SERVER_AUTH_TOKEN:
673 DCHECK_EQ(OK, rv);
674 rv = DoGenerateServerAuthToken();
675 break;
676 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
677 rv = DoGenerateServerAuthTokenComplete(rv);
678 break;
[email protected]daddea62012-09-19 05:51:13679 case STATE_INIT_REQUEST_BODY:
680 DCHECK_EQ(OK, rv);
681 rv = DoInitRequestBody();
682 break;
683 case STATE_INIT_REQUEST_BODY_COMPLETE:
684 rv = DoInitRequestBodyComplete(rv);
685 break;
[email protected]4875ba12011-03-30 22:31:51686 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55687 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38688 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51689 rv = DoBuildRequest();
690 break;
691 case STATE_BUILD_REQUEST_COMPLETE:
692 rv = DoBuildRequestComplete(rv);
693 break;
694 case STATE_SEND_REQUEST:
695 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57696 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52697 break;
[email protected]0877e3d2009-10-17 22:29:57698 case STATE_SEND_REQUEST_COMPLETE:
699 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43700 net_log_.EndEventWithNetErrorCode(
701 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52702 break;
703 case STATE_READ_HEADERS:
[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_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52706 rv = DoReadHeaders();
707 break;
708 case STATE_READ_HEADERS_COMPLETE:
709 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43710 net_log_.EndEventWithNetErrorCode(
711 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52712 break;
713 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55714 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38715 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52716 rv = DoReadBody();
717 break;
718 case STATE_READ_BODY_COMPLETE:
719 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43720 net_log_.EndEventWithNetErrorCode(
721 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52722 break;
[email protected]2d2697f92009-02-18 21:00:32723 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55724 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53725 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38726 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32727 rv = DoDrainBodyForAuthRestart();
728 break;
729 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
730 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43731 net_log_.EndEventWithNetErrorCode(
732 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32733 break;
initial.commit586acc5fe2008-07-26 22:42:52734 default:
735 NOTREACHED() << "bad state";
736 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04737 break;
initial.commit586acc5fe2008-07-26 22:42:52738 }
739 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
740
741 return rv;
742}
743
[email protected]1826a402014-01-08 15:40:48744int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
745 next_state_ = STATE_CREATE_STREAM;
746 bool defer = false;
747 if (!before_network_start_callback_.is_null())
748 before_network_start_callback_.Run(&defer);
749 if (!defer)
750 return OK;
751 return ERR_IO_PENDING;
752}
753
[email protected]82918cc2010-08-25 17:24:50754int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34755 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26756 tracked_objects::ScopedTracker tracking_profile(
757 FROM_HERE_WITH_EXPLICIT_FUNCTION(
758 "424359 HttpNetworkTransaction::DoCreateStream"));
759
ttuttlec0c828492015-05-15 01:25:55760 response_.network_accessed = true;
761
[email protected]82918cc2010-08-25 17:24:50762 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44763 if (ForWebSocketHandshake()) {
764 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23765 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44766 ->RequestWebSocketHandshakeStream(
767 *request_,
768 priority_,
769 server_ssl_config_,
770 proxy_ssl_config_,
771 this,
772 websocket_handshake_stream_base_create_helper_,
773 net_log_));
774 } else {
775 stream_request_.reset(
776 session_->http_stream_factory()->RequestStream(
777 *request_,
778 priority_,
779 server_ssl_config_,
780 proxy_ssl_config_,
781 this,
782 net_log_));
783 }
[email protected]26816882010-10-14 18:03:09784 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38785 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31786}
787
[email protected]82918cc2010-08-25 17:24:50788int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47789 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
790 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
791 // which resets the stream request first. Therefore, we have to grab the
792 // connection attempts in *that* function instead of here in that case.
793 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
794 CopyConnectionAttemptsFromStreamRequest();
795
davidben701ca982015-05-18 21:21:42796 if (request_->url.SchemeIsCryptographic())
797 RecordSSLFallbackMetrics(result);
798
[email protected]394816e92010-08-03 07:38:59799 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50800 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38801 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33802 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
803 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29804 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
805 // Return OK and let the caller read the proxy's error page
806 next_state_ = STATE_NONE;
807 return OK;
bncfacdd852015-01-09 19:22:54808 } else if (result == ERR_HTTP_1_1_REQUIRED ||
809 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
810 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59811 }
812
[email protected]bd0b6772011-01-11 19:59:30813 // Handle possible handshake errors that may have occurred if the stream
814 // used SSL for one or more of the layers.
815 result = HandleSSLHandshakeError(result);
816
[email protected]8e6441ca2010-08-19 05:56:38817 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09818 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38819 return result;
[email protected]394816e92010-08-03 07:38:59820}
821
[email protected]82918cc2010-08-25 17:24:50822int HttpNetworkTransaction::DoInitStream() {
823 DCHECK(stream_.get());
824 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59825
826 stream_->GetRemoteEndpoint(&remote_endpoint_);
827
[email protected]262eec82013-03-19 21:01:36828 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50829}
830
831int HttpNetworkTransaction::DoInitStreamComplete(int result) {
832 if (result == OK) {
833 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50834 } else {
[email protected]82918cc2010-08-25 17:24:50835 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26836 result = HandleIOError(result);
837
838 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22839 if (stream_) {
840 total_received_bytes_ += stream_->GetTotalReceivedBytes();
841 total_sent_bytes_ += stream_->GetTotalSentBytes();
842 }
[email protected]044dcc52010-09-17 15:44:26843 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50844 }
845
846 return result;
847}
848
[email protected]044de0642010-06-17 10:42:15849int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
850 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
851 if (!ShouldApplyProxyAuth())
852 return OK;
[email protected]394816e92010-08-03 07:38:59853 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
854 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09855 auth_controllers_[target] =
856 new HttpAuthController(target,
857 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31858 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09859 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59860 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41861 io_callback_,
[email protected]394816e92010-08-03 07:38:59862 net_log_);
[email protected]044de0642010-06-17 10:42:15863}
864
865int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
866 DCHECK_NE(ERR_IO_PENDING, rv);
867 if (rv == OK)
868 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
869 return rv;
870}
871
872int HttpNetworkTransaction::DoGenerateServerAuthToken() {
873 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59874 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54875 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09876 auth_controllers_[target] =
877 new HttpAuthController(target,
878 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31879 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09880 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54881 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
882 auth_controllers_[target]->DisableEmbeddedIdentity();
883 }
[email protected]044de0642010-06-17 10:42:15884 if (!ShouldApplyServerAuth())
885 return OK;
[email protected]394816e92010-08-03 07:38:59886 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41887 io_callback_,
[email protected]394816e92010-08-03 07:38:59888 net_log_);
[email protected]044de0642010-06-17 10:42:15889}
890
891int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
892 DCHECK_NE(ERR_IO_PENDING, rv);
893 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13894 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15895 return rv;
896}
897
Adam Rice425cf122015-01-19 06:18:24898void HttpNetworkTransaction::BuildRequestHeaders(
899 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14900 request_headers_.SetHeader(HttpRequestHeaders::kHost,
901 GetHostAndOptionalPort(request_->url));
902
903 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24904 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14905 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
906 "keep-alive");
907 } else {
908 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
909 }
910
[email protected]2979a492011-04-06 00:29:14911 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11912 if (request_->upload_data_stream) {
913 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14914 request_headers_.SetHeader(
915 HttpRequestHeaders::kTransferEncoding, "chunked");
916 } else {
917 request_headers_.SetHeader(
918 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11919 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14920 }
csharrisonf473dd192015-08-18 13:54:13921 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14922 // An empty POST/PUT request still needs a content length. As for HEAD,
923 // IE and Safari also add a content length header. Presumably it is to
924 // support sending a HEAD request to an URL that only expects to be sent a
925 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13926 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
927 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14928 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
929 }
930
931 // Honor load flags that impact proxy caches.
932 if (request_->load_flags & LOAD_BYPASS_CACHE) {
933 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
934 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
935 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
936 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
937 }
938
939 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
940 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
941 &request_headers_);
942 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
943 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
944 &request_headers_);
945
[email protected]c10450102011-06-27 09:06:16946 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20947
Adam Rice425cf122015-01-19 06:18:24948 if (using_http_proxy_without_tunnel &&
949 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20950 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
951
[email protected]173f8e22013-04-10 04:18:20952 response_.did_use_http_auth =
953 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
954 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14955}
956
[email protected]daddea62012-09-19 05:51:13957int HttpNetworkTransaction::DoInitRequestBody() {
958 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13959 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11960 if (request_->upload_data_stream)
961 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13962 return rv;
963}
[email protected]4875ba12011-03-30 22:31:51964
[email protected]daddea62012-09-19 05:51:13965int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
966 if (result == OK)
967 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13968 return result;
969}
970
971int HttpNetworkTransaction::DoBuildRequest() {
972 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51973 headers_valid_ = false;
974
975 // This is constructed lazily (instead of within our Start method), so that
976 // we have proxy info available.
977 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:24978 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
979 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51980 }
981
[email protected]4875ba12011-03-30 22:31:51982 return OK;
983}
984
985int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24986 if (result == OK)
987 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51988 return result;
989}
990
[email protected]0877e3d2009-10-17 22:29:57991int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:34992 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26993 tracked_objects::ScopedTracker tracking_profile(
994 FROM_HERE_WITH_EXPLICIT_FUNCTION(
995 "424359 HttpNetworkTransaction::DoSendRequest"));
996
[email protected]58e32bb2013-01-21 18:23:25997 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57998 next_state_ = STATE_SEND_REQUEST_COMPLETE;
999
[email protected]bf3eb002012-11-15 05:50:111000 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521001}
1002
[email protected]0877e3d2009-10-17 22:29:571003int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251004 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:521005 if (result < 0)
1006 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571007 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521008 return OK;
1009}
1010
1011int HttpNetworkTransaction::DoReadHeaders() {
1012 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411013 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521014}
1015
1016int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111017 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1018 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381019 if (IsCertificateError(result)) {
1020 // We don't handle a certificate error during SSL renegotiation, so we
1021 // have to return an error that's not in the certificate error range
1022 // (-2xx).
1023 LOG(ERROR) << "Got a server certificate with error " << result
1024 << " during SSL renegotiation";
1025 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1026 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1027 // TODO(wtc): Need a test case for this code path!
1028 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251029 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381030 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501031 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381032 result = HandleCertificateRequest(result);
1033 if (result == OK)
1034 return result;
[email protected]2181ea002009-06-09 01:37:271035 }
1036
bncfacdd852015-01-09 19:22:541037 if (result == ERR_HTTP_1_1_REQUIRED ||
1038 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1039 return HandleHttp11Required(result);
1040 }
1041
[email protected]c871864d72014-03-13 19:56:191042 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1043 // response headers were received, we do the best we can to make sense of it
1044 // and send it back up the stack.
1045 //
1046 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1047 // bizarre for SPDY. Assuming this logic is useful at all.
1048 // TODO(davidben): Bubble the error code up so we do not cache?
1049 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1050 result = OK;
1051
1052 if (result < 0)
1053 return HandleIOError(result);
1054
[email protected]90499482013-06-01 00:39:501055 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521056
[email protected]d58ceea82014-06-04 10:55:541057 // On a 408 response from the server ("Request Timeout") on a stale socket,
1058 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041059 // Headers can be NULL because of http://crbug.com/384554.
1060 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541061 stream_->IsConnectionReused()) {
1062 net_log_.AddEventWithNetErrorCode(
1063 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1064 response_.headers->response_code());
1065 // This will close the socket - it would be weird to try and reuse it, even
1066 // if the server doesn't actually close it.
1067 ResetConnectionAndRequestForResend();
1068 return OK;
1069 }
1070
[email protected]93f8b562012-03-27 01:00:161071 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1072 if (request_->load_flags & LOAD_MAIN_FRAME) {
1073 const int response_code = response_.headers->response_code();
1074 UMA_HISTOGRAM_ENUMERATION(
1075 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1076 }
1077
[email protected]3abacd62012-06-10 20:20:321078 net_log_.AddEvent(
1079 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1080 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391081
bncbe0f6af2015-10-15 17:49:561082 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571083 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1084 // indicates a buggy server. See:
1085 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1086 if (request_->method == "PUT")
1087 return ERR_METHOD_NOT_SUPPORTED;
1088 }
[email protected]4ddaf2502008-10-23 18:26:191089
[email protected]0877e3d2009-10-17 22:29:571090 // Check for an intermediate 100 Continue response. An origin server is
1091 // allowed to send this response even if we didn't ask for it, so we just
1092 // need to skip over it.
1093 // We treat any other 1xx in this same way (although in practice getting
1094 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441095 // Unless this is a WebSocket request, in which case we pass it on up.
1096 if (response_.headers->response_code() / 100 == 1 &&
1097 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451098 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571099 next_state_ = STATE_READ_HEADERS;
1100 return OK;
1101 }
1102
bncc958faa2015-07-31 18:14:521103 ProcessAlternativeServices(session_, *response_.headers.get(),
1104 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421105
[email protected]e772db3f2010-07-12 18:11:131106 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571107 if (rv != OK)
1108 return rv;
1109
Adam Ricecb76ac62015-02-20 05:33:251110 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211111 stream_->GetSSLInfo(&response_.ssl_info);
1112
[email protected]0877e3d2009-10-17 22:29:571113 headers_valid_ = true;
1114 return OK;
initial.commit586acc5fe2008-07-26 22:42:521115}
1116
1117int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501118 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131119 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381120 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521121
1122 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501123 return stream_->ReadResponseBody(
1124 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521125}
1126
1127int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1128 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381129 bool done = false;
1130 if (result <= 0) {
1131 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521132 done = true;
[email protected]8e6441ca2010-08-19 05:56:381133 }
[email protected]9492e4a2010-02-24 00:58:461134
mmenkebd84c392015-09-02 14:12:341135 // Clean up connection if we are done.
1136 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381137 // Note: Just because IsResponseBodyComplete is true, we're not
1138 // necessarily "done". We're only "done" when it is the last
1139 // read on this HttpNetworkTransaction, which will be signified
1140 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341141 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381142 // the stream. No need to compute it here just to pass back
1143 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341144 bool keep_alive =
1145 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521146
[email protected]8e6441ca2010-08-19 05:56:381147 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011148 // Note: we don't reset the stream here. We've closed it, but we still
1149 // need it around so that callers can call methods such as
1150 // GetUploadProgress() and have them be meaningful.
1151 // TODO(mbelshe): This means we closed the stream here, and we close it
1152 // again in ~HttpNetworkTransaction. Clean that up.
1153
[email protected]8e6441ca2010-08-19 05:56:381154 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521155 }
1156
1157 // Clear these to avoid leaving around old state.
1158 read_buf_ = NULL;
1159 read_buf_len_ = 0;
1160
1161 return result;
1162}
1163
[email protected]2d2697f92009-02-18 21:00:321164int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1165 // This method differs from DoReadBody only in the next_state_. So we just
1166 // call DoReadBody and override the next_state_. Perhaps there is a more
1167 // elegant way for these two methods to share code.
1168 int rv = DoReadBody();
1169 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1170 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1171 return rv;
1172}
1173
[email protected]0877e3d2009-10-17 22:29:571174// TODO(wtc): This method and the DoReadBodyComplete method are almost
1175// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321176int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231177 // keep_alive defaults to true because the very reason we're draining the
1178 // response body is to reuse the connection for auth restart.
1179 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321180 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571181 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321182 done = true;
[email protected]68873ba2009-06-04 21:49:231183 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311184 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571185 done = true;
[email protected]2d2697f92009-02-18 21:00:321186 }
1187
1188 if (done) {
1189 DidDrainBodyForAuthRestart(keep_alive);
1190 } else {
1191 // Keep draining.
1192 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1193 }
1194
1195 return OK;
1196}
1197
[email protected]5e363962009-06-19 19:57:011198int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381199 // There are two paths through which the server can request a certificate
1200 // from us. The first is during the initial handshake, the second is
1201 // during SSL renegotiation.
1202 //
1203 // In both cases, we want to close the connection before proceeding.
1204 // We do this for two reasons:
1205 // First, we don't want to keep the connection to the server hung for a
1206 // long time while the user selects a certificate.
1207 // Second, even if we did keep the connection open, NSS has a bug where
1208 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581209 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381210
1211 if (stream_.get()) {
1212 // Since we already have a stream, we're being called as part of SSL
1213 // renegotiation.
1214 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191215 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221216 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381217 stream_->Close(true);
1218 stream_.reset();
1219 }
1220
[email protected]26816882010-10-14 18:03:091221 // The server is asking for a client certificate during the initial
1222 // handshake.
1223 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011224
[email protected]ec229bc92010-11-22 09:51:451225 // If the user selected one of the certificates in client_certs or declined
1226 // to provide one for this server before, use the past decision
1227 // automatically.
1228 scoped_refptr<X509Certificate> client_cert;
1229 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1230 response_.cert_request_info->host_and_port, &client_cert);
1231 if (!found_cached_cert)
1232 return error;
1233
1234 // Check that the certificate selected is still a certificate the server
1235 // is likely to accept, based on the criteria supplied in the
1236 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501237 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261238 const std::vector<std::string>& cert_authorities =
1239 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451240
[email protected]f1958c382013-02-07 00:15:261241 bool cert_still_valid = cert_authorities.empty() ||
1242 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451243 if (!cert_still_valid)
1244 return error;
[email protected]5e363962009-06-19 19:57:011245 }
[email protected]ec229bc92010-11-22 09:51:451246
1247 // TODO(davidben): Add a unit test which covers this path; we need to be
1248 // able to send a legitimate certificate and also bypass/clear the
1249 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141250 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1251 &proxy_ssl_config_ : &server_ssl_config_;
1252 ssl_config->send_client_cert = true;
1253 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451254 next_state_ = STATE_CREATE_STREAM;
1255 // Reset the other member variables.
1256 // Note: this is necessary only with SSL renegotiation.
1257 ResetStateForRestart();
1258 return OK;
[email protected]0b45559b2009-06-12 21:45:111259}
1260
bncfacdd852015-01-09 19:22:541261int HttpNetworkTransaction::HandleHttp11Required(int error) {
1262 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1263 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1264
1265 if (error == ERR_HTTP_1_1_REQUIRED) {
1266 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1267 } else {
1268 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1269 }
1270 ResetConnectionAndRequestForResend();
1271 return OK;
1272}
1273
[email protected]384cc73a2013-12-08 22:41:031274void HttpNetworkTransaction::HandleClientAuthError(int error) {
1275 if (server_ssl_config_.send_client_cert &&
1276 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1277 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411278 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031279 }
1280}
1281
[email protected]bd0b6772011-01-11 19:59:301282// TODO(rch): This does not correctly handle errors when an SSL proxy is
1283// being used, as all of the errors are handled as if they were generated
1284// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381285// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301286int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1287 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031288 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301289
davidbena4c9d062015-04-03 22:34:251290 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1291 // reflect servers require a deprecated cipher rather than merely prefer
1292 // it. This, however, has no security benefit until the ciphers are actually
1293 // removed.
1294 if (!server_ssl_config_.enable_deprecated_cipher_suites &&
1295 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1296 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1297 net_log_.AddEvent(
1298 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1299 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
1300 server_ssl_config_.enable_deprecated_cipher_suites = true;
1301 ResetConnectionAndRequestForResend();
1302 return OK;
1303 }
1304
[email protected]02d1d442013-08-10 13:38:261305 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231306 uint16 version_max = server_ssl_config_.version_max;
1307
initial.commit586acc5fe2008-07-26 22:42:521308 switch (error) {
[email protected]12833302014-07-02 01:57:311309 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301310 case ERR_SSL_PROTOCOL_ERROR:
1311 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231312 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1313 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171314 // This could be a TLS-intolerant server or a server that chose a
1315 // cipher suite defined only for higher protocol versions (such as
1316 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1317 // back to the next lower version and retry.
1318 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1319 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1320 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1321 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1322 // version_max should match the maximum protocol version supported
1323 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231324 version_max--;
[email protected]c57ec022013-11-25 21:30:511325
1326 // Fallback to the lower SSL version.
1327 // While SSL 3.0 fallback should be eliminated because of security
1328 // reasons, there is a high risk of breaking the servers if this is
1329 // done in general.
[email protected]001e547e2013-12-06 16:08:391330 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421331 }
initial.commit586acc5fe2008-07-26 22:42:521332 break;
[email protected]12833302014-07-02 01:57:311333 case ERR_CONNECTION_RESET:
1334 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1335 version_max > server_ssl_config_.version_min) {
1336 // Some network devices that inspect application-layer packets seem to
1337 // inject TCP reset packets to break the connections when they see TLS
1338 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1339 //
1340 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1341 // 1.2. We don't lose much in this fallback because the explicit IV for
1342 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1343 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1344 // support.
1345 //
1346 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1347 // to trigger a version fallback in general, especially the TLS 1.0 ->
1348 // SSL 3.0 fallback, which would drop TLS extensions.
1349 version_max--;
1350 should_fallback = true;
1351 }
1352 break;
[email protected]02d1d442013-08-10 13:38:261353 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1354 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1355 version_max > server_ssl_config_.version_min) {
1356 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1357 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1358 // crbug.com/260358. In order to make the fallback as minimal as
1359 // possible, this fallback is only triggered for >= TLS 1.1.
1360 version_max--;
1361 should_fallback = true;
1362 }
1363 break;
[email protected]a53e4d12013-12-07 16:37:241364 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1365 // The server told us that we should not have fallen back. A buggy server
1366 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1367 // connection. |fallback_error_code_| is initialised to
1368 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1369 error = fallback_error_code_;
1370 break;
[email protected]02d1d442013-08-10 13:38:261371 }
1372
1373 if (should_fallback) {
1374 net_log_.AddEvent(
1375 NetLog::TYPE_SSL_VERSION_FALLBACK,
davidbenf2eaaf92015-05-15 22:18:421376 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1377 server_ssl_failure_state_, server_ssl_config_.version_max,
[email protected]02d1d442013-08-10 13:38:261378 version_max));
[email protected]a53e4d12013-12-07 16:37:241379 fallback_error_code_ = error;
davidbenf2eaaf92015-05-15 22:18:421380 fallback_failure_state_ = server_ssl_failure_state_;
[email protected]02d1d442013-08-10 13:38:261381 server_ssl_config_.version_max = version_max;
1382 server_ssl_config_.version_fallback = true;
1383 ResetConnectionAndRequestForResend();
1384 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521385 }
[email protected]158ac972013-04-19 23:29:231386
initial.commit586acc5fe2008-07-26 22:42:521387 return error;
1388}
1389
[email protected]bd0b6772011-01-11 19:59:301390// This method determines whether it is safe to resend the request after an
1391// IO error. It can only be called in response to request header or body
1392// write errors or response header read errors. It should not be used in
1393// other cases, such as a Connect error.
1394int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031395 // Because the peer may request renegotiation with client authentication at
1396 // any time, check and handle client authentication errors.
1397 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301398
1399 switch (error) {
1400 // If we try to reuse a connection that the server is in the process of
1401 // closing, we may end up successfully writing out our request (or a
1402 // portion of our request) only to find a connection error when we try to
1403 // read from (or finish writing to) the socket.
1404 case ERR_CONNECTION_RESET:
1405 case ERR_CONNECTION_CLOSED:
1406 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511407 // There can be a race between the socket pool checking checking whether a
1408 // socket is still connected, receiving the FIN, and sending/reading data
1409 // on a reused socket. If we receive the FIN between the connectedness
1410 // check and writing/reading from the socket, we may first learn the socket
1411 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1412 // likely happen when trying to retrieve its IP address.
1413 // See http://crbug.com/105824 for more details.
1414 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491415 // If a socket is closed on its initial request, HttpStreamParser returns
1416 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1417 // preconnected but failed to be used before the server timed it out.
1418 case ERR_EMPTY_RESPONSE:
1419 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381420 net_log_.AddEventWithNetErrorCode(
1421 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301422 ResetConnectionAndRequestForResend();
1423 error = OK;
1424 }
1425 break;
[email protected]8753a122011-10-16 08:05:081426 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001427 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271428 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381429 net_log_.AddEventWithNetErrorCode(
1430 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001431 ResetConnectionAndRequestForResend();
1432 error = OK;
1433 break;
[email protected]bd0b6772011-01-11 19:59:301434 }
1435 return error;
1436}
1437
[email protected]c3b35c22008-09-27 03:19:421438void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581439 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221440 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191441 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221442 total_sent_bytes_ += stream_->GetTotalSentBytes();
1443 }
[email protected]697ef4c2010-10-14 16:38:581444 stream_.reset();
1445}
1446
1447void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251448 send_start_time_ = base::TimeTicks();
1449 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251450
[email protected]0757e7702009-03-27 04:00:221451 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421452 read_buf_ = NULL;
1453 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571454 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201455 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141456 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381457 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591458 remote_endpoint_ = IPEndPoint();
[email protected]0877e3d2009-10-17 22:29:571459}
1460
davidben701ca982015-05-18 21:21:421461void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1462 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1463 return;
1464
1465 const std::string& host = request_->url.host();
brettwa7ff1b292015-07-16 17:49:291466 bool is_google = base::EndsWith(host, "google.com",
1467 base::CompareCase::SENSITIVE) &&
davidben701ca982015-05-18 21:21:421468 (host.size() == 10 || host[host.size() - 11] == '.');
1469 if (is_google) {
1470 // Some fraction of successful connections use the fallback, but only due to
1471 // a spurious network failure. To estimate this fraction, compare handshakes
1472 // to Google servers which succeed against those that fail with an
1473 // inappropriate_fallback alert. Google servers are known to implement
1474 // FALLBACK_SCSV, so a spurious network failure while connecting would
1475 // trigger the fallback, successfully connect, but fail with this alert.
1476 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1477 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1478 }
1479
1480 if (result != OK)
1481 return;
1482
davidbenca9d69162015-05-04 20:18:451483 // Note: these values are used in histograms, so new values must be appended.
1484 enum FallbackVersion {
davidbenb937d6c2015-05-14 04:53:421485 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1486 // Obsolete: FALLBACK_SSL3 = 1,
davidbenca9d69162015-05-04 20:18:451487 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1488 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1489 FALLBACK_MAX,
1490 };
1491
1492 FallbackVersion fallback = FALLBACK_NONE;
1493 if (server_ssl_config_.version_fallback) {
1494 switch (server_ssl_config_.version_max) {
davidbenca9d69162015-05-04 20:18:451495 case SSL_PROTOCOL_VERSION_TLS1:
1496 fallback = FALLBACK_TLS1;
1497 break;
1498 case SSL_PROTOCOL_VERSION_TLS1_1:
1499 fallback = FALLBACK_TLS1_1;
1500 break;
1501 default:
1502 NOTREACHED();
1503 }
1504 }
1505 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1506 FALLBACK_MAX);
1507
1508 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1509 // should be impossible to successfully connect to them with the fallback.
1510 // This helps estimate intolerant locally-configured SSL MITMs.
davidben701ca982015-05-18 21:21:421511 if (is_google) {
davidbenca9d69162015-05-04 20:18:451512 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1513 fallback, FALLBACK_MAX);
1514 }
1515
1516 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
1517 server_ssl_config_.enable_deprecated_cipher_suites);
davidbenf2eaaf92015-05-15 22:18:421518
1519 if (server_ssl_config_.version_fallback) {
1520 // Record the error code which triggered the fallback and the state the
1521 // handshake was in.
1522 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1523 -fallback_error_code_);
1524 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1525 fallback_failure_state_, SSL_FAILURE_MAX);
1526 }
davidbenca9d69162015-05-04 20:18:451527}
1528
[email protected]0877e3d2009-10-17 22:29:571529HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501530 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421531}
1532
[email protected]a34f61ee2014-03-18 20:59:491533bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381534 bool connection_is_proven = stream_->IsConnectionReused();
1535 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161536
[email protected]2a5c76b2008-09-25 22:15:161537 // NOTE: we resend a request only if we reused a keep-alive connection.
1538 // This automatically prevents an infinite resend loop because we'll run
1539 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381540 if (connection_is_proven && !has_received_headers)
1541 return true;
1542 return false;
[email protected]1c773ea12009-04-28 19:58:421543}
1544
1545void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381546 if (stream_.get()) {
1547 stream_->Close(true);
1548 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161549 }
1550
[email protected]0877e3d2009-10-17 22:29:571551 // We need to clear request_headers_ because it contains the real request
1552 // headers, but we may need to resend the CONNECT request first to recreate
1553 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201554 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501555 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541556}
1557
[email protected]1c773ea12009-04-28 19:58:421558bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241559 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421560}
license.botbf09a502008-08-24 00:55:551561
[email protected]1c773ea12009-04-28 19:58:421562bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041563 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421564}
1565
[email protected]e772db3f2010-07-12 18:11:131566int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271567 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501568 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421569
[email protected]0877e3d2009-10-17 22:29:571570 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581571 if (status != HTTP_UNAUTHORIZED &&
1572 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421573 return OK;
[email protected]9094b602012-02-27 21:44:581574 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111575 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161576 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1577 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421578
[email protected]9094b602012-02-27 21:44:581579 // This case can trigger when an HTTPS server responds with a "Proxy
1580 // authentication required" status code through a non-authenticating
1581 // proxy.
[email protected]7a67a8152010-11-05 18:31:101582 if (!auth_controllers_[target].get())
1583 return ERR_UNEXPECTED_PROXY_AUTH;
1584
[email protected]a7ea8832010-07-12 17:54:541585 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311586 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1587 net_log_);
[email protected]228404f2010-06-24 04:31:411588 if (auth_controllers_[target]->HaveAuthHandler())
1589 pending_auth_target_ = target;
1590
1591 scoped_refptr<AuthChallengeInfo> auth_info =
1592 auth_controllers_[target]->auth_info();
1593 if (auth_info.get())
1594 response_.auth_challenge = auth_info;
1595
[email protected]228404f2010-06-24 04:31:411596 return rv;
[email protected]f9ee6b52008-11-08 06:46:231597}
1598
[email protected]8e6441ca2010-08-19 05:56:381599bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1600 return auth_controllers_[target].get() &&
1601 auth_controllers_[target]->HaveAuth();
1602}
1603
[email protected]228404f2010-06-24 04:31:411604GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1605 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461606 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411607 if (!proxy_info_.proxy_server().is_valid() ||
1608 proxy_info_.proxy_server().is_direct()) {
1609 return GURL(); // There is no proxy server.
1610 }
[email protected]2df19bb2010-08-25 20:13:461611 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1612 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351613 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461614 }
[email protected]228404f2010-06-24 04:31:411615 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291616 if (ForWebSocketHandshake()) {
1617 const GURL& url = request_->url;
1618 url::Replacements<char> ws_to_http;
1619 if (url.SchemeIs("ws")) {
1620 ws_to_http.SetScheme("http", url::Component(0, 4));
1621 } else {
1622 DCHECK(url.SchemeIs("wss"));
1623 ws_to_http.SetScheme("https", url::Component(0, 5));
1624 }
1625 return url.ReplaceComponents(ws_to_http);
1626 }
[email protected]228404f2010-06-24 04:31:411627 return request_->url;
1628 default:
1629 return GURL();
1630 }
[email protected]c3b35c22008-09-27 03:19:421631}
1632
[email protected]831e4a32013-11-14 02:14:441633bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141634 return websocket_handshake_stream_base_create_helper_ &&
1635 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441636}
1637
[email protected]d8eb84242010-09-25 02:25:061638#define STATE_CASE(s) \
1639 case s: \
1640 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1641 break
[email protected]aef04272010-06-28 18:03:041642
1643std::string HttpNetworkTransaction::DescribeState(State state) {
1644 std::string description;
1645 switch (state) {
[email protected]1826a402014-01-08 15:40:481646 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501647 STATE_CASE(STATE_CREATE_STREAM);
1648 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131649 STATE_CASE(STATE_INIT_REQUEST_BODY);
1650 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511651 STATE_CASE(STATE_BUILD_REQUEST);
1652 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041653 STATE_CASE(STATE_SEND_REQUEST);
1654 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1655 STATE_CASE(STATE_READ_HEADERS);
1656 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041657 STATE_CASE(STATE_READ_BODY);
1658 STATE_CASE(STATE_READ_BODY_COMPLETE);
1659 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1660 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041661 STATE_CASE(STATE_NONE);
1662 default:
[email protected]d8eb84242010-09-25 02:25:061663 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1664 state);
[email protected]aef04272010-06-28 18:03:041665 break;
1666 }
1667 return description;
1668}
1669
1670#undef STATE_CASE
1671
ttuttle1f2d7e92015-04-28 16:17:471672void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1673 DCHECK(stream_request_);
1674
1675 // Since the transaction can restart with auth credentials, it may create a
1676 // stream more than once. Accumulate all of the connection attempts across
1677 // those streams by appending them to the vector:
1678 for (const auto& attempt : stream_request_->connection_attempts())
1679 connection_attempts_.push_back(attempt);
1680}
1681
[email protected]c3b35c22008-09-27 03:19:421682} // namespace net