blob: 82668ddabe1b537dd53d815a534cd2529014734b [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
danakj1fd259a02016-04-16 03:17:097#include <memory>
[email protected]2fbaecf22010-07-22 22:20:358#include <set>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]2fbaecf22010-07-22 22:20:3510#include <vector>
11
nharperb7441ef2016-01-25 23:54:1412#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4113#include "base/bind.h"
14#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4120#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4025#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5326#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3027#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3528#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5529#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1930#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2633#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.h"
[email protected]c3b35c22008-09-27 03:19:4236#include "net/http/http_auth.h"
37#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2438#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3639#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/http/http_chunked_decoder.h"
41#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5442#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1343#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4744#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2146#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5747#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5348#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5849#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3950#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3151#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2353#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3654#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4455#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2656#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4457#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1858#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2859#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4060#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3261#include "net/spdy/spdy_session.h"
62#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5763#include "net/ssl/ssl_cert_request_info.h"
64#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5465#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1466#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5467#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2968#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5269
initial.commit586acc5fe2008-07-26 22:42:5270namespace net {
71
[email protected]1c773ea12009-04-28 19:58:4272namespace {
73
danakj1fd259a02016-04-16 03:17:0974std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1775 const GURL* url,
76 int net_error,
77 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0978 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2579 dict->SetString("host_and_port", GetHostAndPort(*url));
80 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4881 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2582}
83
[email protected]1c773ea12009-04-28 19:58:4284} // namespace
85
initial.commit586acc5fe2008-07-26 22:42:5286//-----------------------------------------------------------------------------
87
[email protected]262eec82013-03-19 21:01:3688HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
89 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2290 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3291 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
92 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5293 session_(session),
94 request_(NULL),
[email protected]262eec82013-03-19 21:01:3695 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5796 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2097 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5298 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:1999 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22100 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54101 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44102 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02103 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12104 net_error_details_() {
[email protected]3ce7df0f2010-03-03 00:30:50105}
106
[email protected]0b0bf032010-09-21 18:08:50107HttpNetworkTransaction::~HttpNetworkTransaction() {
108 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50109 // TODO(mbelshe): The stream_ should be able to compute whether or not the
110 // stream should be kept alive. No reason to compute here
111 // and pass it in.
mmenkebd84c392015-09-02 14:12:34112 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50113 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34114 } else if (stream_->IsResponseBodyComplete()) {
115 // If the response body is complete, we can just reuse the socket.
116 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50117 } else {
mmenkebd84c392015-09-02 14:12:34118 // Otherwise, we try to drain the response body.
119 HttpStream* stream = stream_.release();
120 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50121 }
122 }
[email protected]02cad5d2013-10-02 08:14:03123
124 if (request_ && request_->upload_data_stream)
125 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50126}
127
[email protected]684970b2009-08-14 04:54:46128int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41129 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53130 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53131 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04132 request_ = request_info;
133
nharper8cdb0fb2016-04-22 21:34:59134 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
135 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
136
[email protected]99ffa5a2011-10-06 04:20:19137 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14138 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19139 proxy_ssl_config_.rev_checking_enabled = false;
140 }
[email protected]6fbac162011-06-20 00:29:04141
jkarlinfb1d5172015-01-12 14:10:29142 if (request_->load_flags & LOAD_PREFETCH)
143 response_.unused_since_prefetch = true;
144
[email protected]1826a402014-01-08 15:40:48145 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04146 int rv = DoLoop(OK);
147 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41148 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04149 return rv;
150}
151
152int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41153 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38154 DCHECK(!stream_.get());
155 DCHECK(!stream_request_.get());
156 DCHECK_EQ(STATE_NONE, next_state_);
157
[email protected]82918cc2010-08-25 17:24:50158 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38159
[email protected]ccb40e52008-09-17 20:54:40160 int rv = DoLoop(OK);
161 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41162 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11163 return rv;
[email protected]96d570e42008-08-05 22:43:04164}
165
[email protected]0b45559b2009-06-12 21:45:11166int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54167 X509Certificate* client_cert,
168 SSLPrivateKey* client_private_key,
169 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38170 // In HandleCertificateRequest(), we always tear down existing stream
171 // requests to force a new connection. So we shouldn't have one here.
172 DCHECK(!stream_request_.get());
173 DCHECK(!stream_.get());
174 DCHECK_EQ(STATE_NONE, next_state_);
175
[email protected]102957f2011-09-02 17:10:14176 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
177 &proxy_ssl_config_ : &server_ssl_config_;
178 ssl_config->send_client_cert = true;
179 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54180 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45181 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54182 response_.cert_request_info->host_and_port, client_cert,
183 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11184 // Reset the other member variables.
185 // Note: this is necessary only with SSL renegotiation.
186 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50187 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11188 int rv = DoLoop(OK);
189 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41190 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11191 return rv;
192}
193
[email protected]49639fa2011-12-20 23:22:41194int HttpNetworkTransaction::RestartWithAuth(
195 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22196 HttpAuth::Target target = pending_auth_target_;
197 if (target == HttpAuth::AUTH_NONE) {
198 NOTREACHED();
199 return ERR_UNEXPECTED;
200 }
[email protected]0757e7702009-03-27 04:00:22201 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42202
[email protected]f3cf9802011-10-28 18:44:58203 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13204
[email protected]49639fa2011-12-20 23:22:41205 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38206
207 int rv = OK;
208 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
209 // In this case, we've gathered credentials for use with proxy
210 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50211 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38212 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59213 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54214 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58215 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54216 } else {
[email protected]8e6441ca2010-08-19 05:56:38217 // In this case, we've gathered credentials for the server or the proxy
218 // but it is not during the tunneling phase.
219 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54220 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38221 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54222 }
[email protected]c3b35c22008-09-27 03:19:42223
[email protected]c3b35c22008-09-27 03:19:42224 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41225 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42226 return rv;
[email protected]96d570e42008-08-05 22:43:04227}
228
[email protected]f9ee6b52008-11-08 06:46:23229void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
230 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38231 DCHECK(!stream_request_.get());
232
[email protected]2d2697f92009-02-18 21:00:32233 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57234 // Even if the server says the connection is keep-alive, we have to be
235 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34236 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57237 // If the response body hasn't been completely read, we need to drain
238 // it first.
[email protected]351ab642010-08-05 16:55:31239 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32240 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57241 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32242 read_buf_len_ = kDrainBodyBufferSize;
243 return;
244 }
[email protected]0877e3d2009-10-17 22:29:57245 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09246 }
247
[email protected]2d2697f92009-02-18 21:00:32248 // We don't need to drain the response body, so we act as if we had drained
249 // the response body.
250 DidDrainBodyForAuthRestart(keep_alive);
251}
252
253void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38254 DCHECK(!stream_request_.get());
255
256 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19257 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22258 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58259 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34260 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38261 // We should call connection_->set_idle_time(), but this doesn't occur
262 // often enough to be worth the trouble.
263 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39264 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38265 }
[email protected]697ef4c2010-10-14 16:38:58266
267 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46268 // Close the stream and mark it as not_reusable. Even in the
269 // keep_alive case, we've determined that the stream_ is not
270 // reusable if new_stream is NULL.
271 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58272 next_state_ = STATE_CREATE_STREAM;
273 } else {
sclittlefb249892015-09-10 21:33:22274 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19275 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22276 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58277 next_state_ = STATE_INIT_STREAM;
278 }
279 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32280 }
[email protected]f9ee6b52008-11-08 06:46:23281
282 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58283 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23284}
285
[email protected]8e6441ca2010-08-19 05:56:38286bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
287 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
288 HaveAuth(pending_auth_target_);
289}
290
[email protected]9dea9e1f2009-01-29 00:30:47291int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41292 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04293 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35294 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04295
[email protected]1f14a912009-12-21 20:32:44296 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04297
[email protected]ad8e04a2010-11-01 04:16:27298 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38299 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04300 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29301 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04302 // bytes when establishing a tunnel because they might be controlled by
303 // an active network attacker. We don't worry about this for HTTP
304 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29305 // We reach this case when the user cancels a 407 proxy auth prompt. We
306 // also don't worry about this for an HTTPS Proxy, because the
307 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04308 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46309 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58310 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54311 LOG(WARNING) << "Blocked proxy response with status "
312 << headers->response_code() << " to CONNECT request for "
313 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04314 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44315 }
316
[email protected]e60e47a2010-07-14 03:37:18317 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31318 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18319
[email protected]96d570e42008-08-05 22:43:04320 read_buf_ = buf;
321 read_buf_len_ = buf_len;
322
[email protected]1f14a912009-12-21 20:32:44323 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04324 int rv = DoLoop(OK);
325 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41326 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04327 return rv;
328}
329
[email protected]8cd06c02014-01-25 07:50:14330void HttpNetworkTransaction::StopCaching() {}
331
[email protected]79e1fd62013-06-20 06:50:04332bool HttpNetworkTransaction::GetFullRequestHeaders(
333 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32334 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04335 *headers = request_headers_;
336 return true;
337}
338
sclittle4de1bab92015-09-22 21:28:24339int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
340 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19341 if (stream_)
342 total_received_bytes += stream_->GetTotalReceivedBytes();
343 return total_received_bytes;
344}
345
sclittlefb249892015-09-10 21:33:22346int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
347 int64_t total_sent_bytes = total_sent_bytes_;
348 if (stream_)
349 total_sent_bytes += stream_->GetTotalSentBytes();
350 return total_sent_bytes;
351}
352
[email protected]8cd06c02014-01-25 07:50:14353void HttpNetworkTransaction::DoneReading() {}
354
[email protected]96d570e42008-08-05 22:43:04355const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55356 return &response_;
[email protected]96d570e42008-08-05 22:43:04357}
358
359LoadState HttpNetworkTransaction::GetLoadState() const {
360 // TODO(wtc): Define a new LoadState value for the
361 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
362 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48363 case STATE_CREATE_STREAM:
364 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50365 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38366 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15367 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
368 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57369 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04370 return LOAD_STATE_SENDING_REQUEST;
371 case STATE_READ_HEADERS_COMPLETE:
372 return LOAD_STATE_WAITING_FOR_RESPONSE;
373 case STATE_READ_BODY_COMPLETE:
374 return LOAD_STATE_READING_RESPONSE;
375 default:
376 return LOAD_STATE_IDLE;
377 }
378}
379
[email protected]196d18a2012-08-30 03:47:31380UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31381 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31382 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04383
yhiranoa7e05bb2014-11-06 05:40:39384 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04385}
386
[email protected]8cd06c02014-01-25 07:50:14387void HttpNetworkTransaction::SetQuicServerInfo(
388 QuicServerInfo* quic_server_info) {}
389
[email protected]5033ab82013-03-22 20:17:46390bool HttpNetworkTransaction::GetLoadTimingInfo(
391 LoadTimingInfo* load_timing_info) const {
392 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
393 return false;
394
395 load_timing_info->proxy_resolve_start =
396 proxy_info_.proxy_resolve_start_time();
397 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
398 load_timing_info->send_start = send_start_time_;
399 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46400 return true;
401}
402
ttuttled9dbc652015-09-29 20:00:59403bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
404 if (!remote_endpoint_.address().size())
405 return false;
406
407 *endpoint = remote_endpoint_;
408 return true;
409}
410
zhongyi48704c182015-12-07 07:52:02411void HttpNetworkTransaction::PopulateNetErrorDetails(
412 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12413 *details = net_error_details_;
414 if (stream_)
415 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02416}
417
[email protected]5033ab82013-03-22 20:17:46418void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
419 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47420 if (stream_request_)
421 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03422 if (stream_)
423 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46424}
425
[email protected]831e4a32013-11-14 02:14:44426void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
427 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
428 websocket_handshake_stream_base_create_helper_ = create_helper;
429}
430
[email protected]1826a402014-01-08 15:40:48431void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
432 const BeforeNetworkStartCallback& callback) {
433 before_network_start_callback_ = callback;
434}
435
ryansturm49a8cb12016-06-15 16:51:09436void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
437 const BeforeHeadersSentCallback& callback) {
438 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27439}
440
[email protected]1826a402014-01-08 15:40:48441int HttpNetworkTransaction::ResumeNetworkStart() {
442 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
443 return DoLoop(OK);
444}
445
[email protected]102e27c2011-02-23 01:01:31446void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
447 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39448 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50449 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38450 DCHECK(stream_request_.get());
451
sclittlefb249892015-09-10 21:33:22452 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19453 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22454 total_sent_bytes_ += stream_->GetTotalSentBytes();
455 }
[email protected]8e6441ca2010-08-19 05:56:38456 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14457 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31458 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38459 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51460 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
461 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38462 response_.was_fetched_via_spdy = stream_request_->using_spdy();
463 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15464 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
465 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38466 OnIOComplete(OK);
467}
468
xunjieli5749218c2016-03-22 16:43:06469void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08470 const SSLConfig& used_ssl_config,
471 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06472 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08473 NOTREACHED();
474}
475
[email protected]a9cf2b92013-10-30 12:08:49476void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50477 const SSLConfig& used_ssl_config,
478 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49479 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44480 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50481}
482
[email protected]102e27c2011-02-23 01:01:31483void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13484 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50485 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38486 DCHECK_NE(OK, result);
487 DCHECK(stream_request_.get());
488 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14489 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38490
491 OnIOComplete(result);
492}
493
[email protected]102e27c2011-02-23 01:01:31494void HttpNetworkTransaction::OnCertificateError(
495 int result,
496 const SSLConfig& used_ssl_config,
497 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50498 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38499 DCHECK_NE(OK, result);
500 DCHECK(stream_request_.get());
501 DCHECK(!stream_.get());
502
503 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14504 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38505
506 // TODO(mbelshe): For now, we're going to pass the error through, and that
507 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50508 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
509 // good and the user chooses to ignore the error. This is not ideal, but not
510 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38511
512 OnIOComplete(result);
513}
514
515void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50516 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31517 const SSLConfig& used_ssl_config,
518 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50519 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38520 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50521 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38522
523 establishing_tunnel_ = true;
524 response_.headers = proxy_response.headers;
525 response_.auth_challenge = proxy_response.auth_challenge;
526 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14527 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31528 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38529
530 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
531 pending_auth_target_ = HttpAuth::AUTH_PROXY;
532
533 DoCallback(OK);
534}
535
536void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31537 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50538 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50539 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38540
[email protected]102957f2011-09-02 17:10:14541 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38542 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58543 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38544}
545
[email protected]511f6f52010-12-17 03:58:29546void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
547 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31548 const SSLConfig& used_ssl_config,
549 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39550 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29551 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
552
ttuttle1f2d7e92015-04-28 16:17:47553 CopyConnectionAttemptsFromStreamRequest();
554
[email protected]511f6f52010-12-17 03:58:29555 headers_valid_ = true;
556 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14557 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31558 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22559 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19560 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22561 total_sent_bytes_ += stream_->GetTotalSentBytes();
562 }
[email protected]511f6f52010-12-17 03:58:29563 stream_.reset(stream);
564 stream_request_.reset(); // we're done with the stream request
565 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
566}
567
zhongyi48704c182015-12-07 07:52:02568void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12569 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02570}
571
ttuttle1f2d7e92015-04-28 16:17:47572void HttpNetworkTransaction::GetConnectionAttempts(
573 ConnectionAttempts* out) const {
574 *out = connection_attempts_;
575}
576
Adam Ricecb76ac62015-02-20 05:33:25577bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37578 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52579}
580
nharperb7441ef2016-01-25 23:54:14581bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
582 if (!IsSecureRequest())
583 return false;
584 SSLInfo ssl_info;
585 stream_->GetSSLInfo(&ssl_info);
586 return ssl_info.token_binding_negotiated &&
587 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
588 session_->params().channel_id_service;
589}
590
591void HttpNetworkTransaction::RecordTokenBindingSupport() const {
592 // This enum is used for an UMA histogram - do not change or re-use values.
593 enum {
594 DISABLED = 0,
595 CLIENT_ONLY = 1,
596 CLIENT_AND_SERVER = 2,
597 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
598 TOKEN_BINDING_SUPPORT_MAX
599 } supported;
600 if (!IsSecureRequest())
601 return;
602 SSLInfo ssl_info;
603 stream_->GetSSLInfo(&ssl_info);
604 if (!session_->params().enable_token_binding) {
605 supported = DISABLED;
606 } else if (!session_->params().channel_id_service) {
607 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
608 } else if (ssl_info.token_binding_negotiated) {
609 supported = CLIENT_AND_SERVER;
610 } else {
611 supported = CLIENT_ONLY;
612 }
613 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
614 TOKEN_BINDING_SUPPORT_MAX);
615}
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;
nharperd6e65822016-03-30 23:05:48679 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14680 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48681 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14682 break;
nharperd6e65822016-03-30 23:05:48683 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
684 rv = DoGetProvidedTokenBindingKeyComplete(rv);
685 break;
686 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
687 DCHECK_EQ(OK, rv);
688 rv = DoGetReferredTokenBindingKey();
689 break;
690 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
691 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14692 break;
[email protected]daddea62012-09-19 05:51:13693 case STATE_INIT_REQUEST_BODY:
694 DCHECK_EQ(OK, rv);
695 rv = DoInitRequestBody();
696 break;
697 case STATE_INIT_REQUEST_BODY_COMPLETE:
698 rv = DoInitRequestBodyComplete(rv);
699 break;
[email protected]4875ba12011-03-30 22:31:51700 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55701 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38702 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51703 rv = DoBuildRequest();
704 break;
705 case STATE_BUILD_REQUEST_COMPLETE:
706 rv = DoBuildRequestComplete(rv);
707 break;
708 case STATE_SEND_REQUEST:
709 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57710 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52711 break;
[email protected]0877e3d2009-10-17 22:29:57712 case STATE_SEND_REQUEST_COMPLETE:
713 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43714 net_log_.EndEventWithNetErrorCode(
715 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52716 break;
717 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55718 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38719 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52720 rv = DoReadHeaders();
721 break;
722 case STATE_READ_HEADERS_COMPLETE:
723 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43724 net_log_.EndEventWithNetErrorCode(
725 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52726 break;
727 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55728 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38729 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52730 rv = DoReadBody();
731 break;
732 case STATE_READ_BODY_COMPLETE:
733 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43734 net_log_.EndEventWithNetErrorCode(
735 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52736 break;
[email protected]2d2697f92009-02-18 21:00:32737 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55738 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53739 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38740 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32741 rv = DoDrainBodyForAuthRestart();
742 break;
743 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
744 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43745 net_log_.EndEventWithNetErrorCode(
746 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32747 break;
initial.commit586acc5fe2008-07-26 22:42:52748 default:
749 NOTREACHED() << "bad state";
750 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04751 break;
initial.commit586acc5fe2008-07-26 22:42:52752 }
753 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
754
755 return rv;
756}
757
[email protected]1826a402014-01-08 15:40:48758int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
759 next_state_ = STATE_CREATE_STREAM;
760 bool defer = false;
761 if (!before_network_start_callback_.is_null())
762 before_network_start_callback_.Run(&defer);
763 if (!defer)
764 return OK;
765 return ERR_IO_PENDING;
766}
767
[email protected]82918cc2010-08-25 17:24:50768int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34769 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26770 tracked_objects::ScopedTracker tracking_profile(
771 FROM_HERE_WITH_EXPLICIT_FUNCTION(
772 "424359 HttpNetworkTransaction::DoCreateStream"));
773
ttuttlec0c828492015-05-15 01:25:55774 response_.network_accessed = true;
775
[email protected]82918cc2010-08-25 17:24:50776 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44777 if (ForWebSocketHandshake()) {
778 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23779 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44780 ->RequestWebSocketHandshakeStream(
781 *request_,
782 priority_,
783 server_ssl_config_,
784 proxy_ssl_config_,
785 this,
786 websocket_handshake_stream_base_create_helper_,
787 net_log_));
788 } else {
789 stream_request_.reset(
790 session_->http_stream_factory()->RequestStream(
791 *request_,
792 priority_,
793 server_ssl_config_,
794 proxy_ssl_config_,
795 this,
796 net_log_));
797 }
[email protected]26816882010-10-14 18:03:09798 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38799 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31800}
801
[email protected]82918cc2010-08-25 17:24:50802int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47803 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
804 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
805 // which resets the stream request first. Therefore, we have to grab the
806 // connection attempts in *that* function instead of here in that case.
807 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
808 CopyConnectionAttemptsFromStreamRequest();
809
davidben701ca982015-05-18 21:21:42810 if (request_->url.SchemeIsCryptographic())
811 RecordSSLFallbackMetrics(result);
812
[email protected]394816e92010-08-03 07:38:59813 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50814 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38815 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33816 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
817 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29818 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
819 // Return OK and let the caller read the proxy's error page
820 next_state_ = STATE_NONE;
821 return OK;
bncfacdd852015-01-09 19:22:54822 } else if (result == ERR_HTTP_1_1_REQUIRED ||
823 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
824 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59825 }
826
[email protected]bd0b6772011-01-11 19:59:30827 // Handle possible handshake errors that may have occurred if the stream
828 // used SSL for one or more of the layers.
829 result = HandleSSLHandshakeError(result);
830
[email protected]8e6441ca2010-08-19 05:56:38831 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09832 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38833 return result;
[email protected]394816e92010-08-03 07:38:59834}
835
[email protected]82918cc2010-08-25 17:24:50836int HttpNetworkTransaction::DoInitStream() {
837 DCHECK(stream_.get());
838 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59839
840 stream_->GetRemoteEndpoint(&remote_endpoint_);
841
[email protected]262eec82013-03-19 21:01:36842 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50843}
844
845int HttpNetworkTransaction::DoInitStreamComplete(int result) {
846 if (result == OK) {
847 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50848 } else {
[email protected]82918cc2010-08-25 17:24:50849 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26850 result = HandleIOError(result);
851
852 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22853 if (stream_) {
854 total_received_bytes_ += stream_->GetTotalReceivedBytes();
855 total_sent_bytes_ += stream_->GetTotalSentBytes();
856 }
zhongyica364fbb2015-12-12 03:39:12857 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50858 }
859
860 return result;
861}
862
[email protected]044de0642010-06-17 10:42:15863int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
864 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
865 if (!ShouldApplyProxyAuth())
866 return OK;
[email protected]394816e92010-08-03 07:38:59867 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
868 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09869 auth_controllers_[target] =
870 new HttpAuthController(target,
871 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31872 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09873 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59874 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41875 io_callback_,
[email protected]394816e92010-08-03 07:38:59876 net_log_);
[email protected]044de0642010-06-17 10:42:15877}
878
879int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
880 DCHECK_NE(ERR_IO_PENDING, rv);
881 if (rv == OK)
882 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
883 return rv;
884}
885
886int HttpNetworkTransaction::DoGenerateServerAuthToken() {
887 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59888 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54889 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09890 auth_controllers_[target] =
891 new HttpAuthController(target,
892 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31893 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09894 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54895 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
896 auth_controllers_[target]->DisableEmbeddedIdentity();
897 }
[email protected]044de0642010-06-17 10:42:15898 if (!ShouldApplyServerAuth())
899 return OK;
[email protected]394816e92010-08-03 07:38:59900 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41901 io_callback_,
[email protected]394816e92010-08-03 07:38:59902 net_log_);
[email protected]044de0642010-06-17 10:42:15903}
904
905int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
906 DCHECK_NE(ERR_IO_PENDING, rv);
907 if (rv == OK)
nharperd6e65822016-03-30 23:05:48908 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15909 return rv;
910}
911
nharperd6e65822016-03-30 23:05:48912int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
913 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14914 if (!IsTokenBindingEnabled())
915 return OK;
916
917 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
918 ChannelIDService* channel_id_service = session_->params().channel_id_service;
919 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48920 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14921 &token_binding_request_);
922}
923
nharperd6e65822016-03-30 23:05:48924int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14925 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48926 if (IsTokenBindingEnabled()) {
927 net_log_.EndEventWithNetErrorCode(
928 NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
929 }
930
931 if (rv == OK)
932 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
933 return rv;
934}
935
936int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
937 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
938 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:14939 return OK;
940
nharperd6e65822016-03-30 23:05:48941 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
942 ChannelIDService* channel_id_service = session_->params().channel_id_service;
943 return channel_id_service->GetOrCreateChannelID(
944 request_->token_binding_referrer, &referred_token_binding_key_,
945 io_callback_, &token_binding_request_);
946}
947
948int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
949 DCHECK_NE(ERR_IO_PENDING, rv);
950 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
951 net_log_.EndEventWithNetErrorCode(
952 NetLog::TYPE_HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
953 }
954 if (rv == OK)
955 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14956 return rv;
957}
958
959int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24960 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14961 request_headers_.SetHeader(HttpRequestHeaders::kHost,
962 GetHostAndOptionalPort(request_->url));
963
964 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24965 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14966 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
967 "keep-alive");
968 } else {
969 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
970 }
971
[email protected]2979a492011-04-06 00:29:14972 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11973 if (request_->upload_data_stream) {
974 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14975 request_headers_.SetHeader(
976 HttpRequestHeaders::kTransferEncoding, "chunked");
977 } else {
978 request_headers_.SetHeader(
979 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11980 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14981 }
csharrisonf473dd192015-08-18 13:54:13982 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14983 // An empty POST/PUT request still needs a content length. As for HEAD,
984 // IE and Safari also add a content length header. Presumably it is to
985 // support sending a HEAD request to an URL that only expects to be sent a
986 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13987 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
988 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14989 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
990 }
991
nharperb7441ef2016-01-25 23:54:14992 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:48993 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:14994 std::string token_binding_header;
995 int rv = BuildTokenBindingHeader(&token_binding_header);
996 if (rv != OK)
997 return rv;
998 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
999 token_binding_header);
1000 }
1001
[email protected]2979a492011-04-06 00:29:141002 // Honor load flags that impact proxy caches.
1003 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1004 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1005 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1006 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1007 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1008 }
1009
1010 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1011 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1012 &request_headers_);
1013 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1014 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1015 &request_headers_);
1016
[email protected]c10450102011-06-27 09:06:161017 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201018
ryansturm49a8cb12016-06-15 16:51:091019 if (!before_headers_sent_callback_.is_null())
1020 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201021
[email protected]173f8e22013-04-10 04:18:201022 response_.did_use_http_auth =
1023 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1024 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141025 return OK;
1026}
1027
1028int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471029 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141030 std::vector<uint8_t> signed_ekm;
nharperd6e65822016-03-30 23:05:481031 int rv = stream_->GetSignedEKMForTokenBinding(
1032 provided_token_binding_key_.get(), &signed_ekm);
nharperb7441ef2016-01-25 23:54:141033 if (rv != OK)
1034 return rv;
1035 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481036 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1037 provided_token_binding_key_.get(), signed_ekm,
1038 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141039 if (rv != OK)
1040 return rv;
nharperd6e65822016-03-30 23:05:481041
nharperb7441ef2016-01-25 23:54:141042 std::vector<base::StringPiece> token_bindings;
1043 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481044
1045 std::string referred_token_binding;
1046 if (referred_token_binding_key_) {
1047 std::vector<uint8_t> referred_signed_ekm;
1048 int rv = stream_->GetSignedEKMForTokenBinding(
1049 referred_token_binding_key_.get(), &referred_signed_ekm);
1050 if (rv != OK)
1051 return rv;
1052 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1053 referred_token_binding_key_.get(),
1054 referred_signed_ekm, &referred_token_binding);
1055 if (rv != OK)
1056 return rv;
1057 token_bindings.push_back(referred_token_binding);
1058 }
nharperb7441ef2016-01-25 23:54:141059 std::string header;
1060 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1061 if (rv != OK)
1062 return rv;
1063 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
1064 out);
nharpercb66ad0202016-03-11 18:17:471065 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1066 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1067 header_creation_time,
1068 base::TimeDelta::FromMilliseconds(1),
1069 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141070 return OK;
[email protected]2979a492011-04-06 00:29:141071}
1072
[email protected]daddea62012-09-19 05:51:131073int HttpNetworkTransaction::DoInitRequestBody() {
1074 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131075 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111076 if (request_->upload_data_stream)
1077 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:131078 return rv;
1079}
[email protected]4875ba12011-03-30 22:31:511080
[email protected]daddea62012-09-19 05:51:131081int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1082 if (result == OK)
1083 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131084 return result;
1085}
1086
1087int HttpNetworkTransaction::DoBuildRequest() {
1088 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511089 headers_valid_ = false;
1090
1091 // This is constructed lazily (instead of within our Start method), so that
1092 // we have proxy info available.
1093 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241094 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141095 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511096 }
1097
[email protected]4875ba12011-03-30 22:31:511098 return OK;
1099}
1100
1101int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241102 if (result == OK)
1103 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511104 return result;
1105}
1106
[email protected]0877e3d2009-10-17 22:29:571107int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341108 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261109 tracked_objects::ScopedTracker tracking_profile(
1110 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1111 "424359 HttpNetworkTransaction::DoSendRequest"));
1112
[email protected]58e32bb2013-01-21 18:23:251113 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571114 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1115
[email protected]bf3eb002012-11-15 05:50:111116 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521117}
1118
[email protected]0877e3d2009-10-17 22:29:571119int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251120 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:521121 if (result < 0)
1122 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571123 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521124 return OK;
1125}
1126
1127int HttpNetworkTransaction::DoReadHeaders() {
1128 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411129 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521130}
1131
1132int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111133 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1134 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381135 if (IsCertificateError(result)) {
1136 // We don't handle a certificate error during SSL renegotiation, so we
1137 // have to return an error that's not in the certificate error range
1138 // (-2xx).
1139 LOG(ERROR) << "Got a server certificate with error " << result
1140 << " during SSL renegotiation";
1141 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1142 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1143 // TODO(wtc): Need a test case for this code path!
1144 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251145 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381146 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501147 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381148 result = HandleCertificateRequest(result);
1149 if (result == OK)
1150 return result;
[email protected]2181ea002009-06-09 01:37:271151 }
1152
bncfacdd852015-01-09 19:22:541153 if (result == ERR_HTTP_1_1_REQUIRED ||
1154 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1155 return HandleHttp11Required(result);
1156 }
1157
[email protected]c871864d72014-03-13 19:56:191158 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1159 // response headers were received, we do the best we can to make sense of it
1160 // and send it back up the stack.
1161 //
1162 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1163 // bizarre for SPDY. Assuming this logic is useful at all.
1164 // TODO(davidben): Bubble the error code up so we do not cache?
1165 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1166 result = OK;
1167
1168 if (result < 0)
1169 return HandleIOError(result);
1170
[email protected]90499482013-06-01 00:39:501171 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521172
[email protected]d58ceea82014-06-04 10:55:541173 // On a 408 response from the server ("Request Timeout") on a stale socket,
1174 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041175 // Headers can be NULL because of http://crbug.com/384554.
1176 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541177 stream_->IsConnectionReused()) {
1178 net_log_.AddEventWithNetErrorCode(
1179 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1180 response_.headers->response_code());
1181 // This will close the socket - it would be weird to try and reuse it, even
1182 // if the server doesn't actually close it.
1183 ResetConnectionAndRequestForResend();
1184 return OK;
1185 }
1186
[email protected]93f8b562012-03-27 01:00:161187 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1188 if (request_->load_flags & LOAD_MAIN_FRAME) {
1189 const int response_code = response_.headers->response_code();
1190 UMA_HISTOGRAM_ENUMERATION(
1191 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1192 }
1193
[email protected]3abacd62012-06-10 20:20:321194 net_log_.AddEvent(
1195 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1196 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391197
bncbe0f6af2015-10-15 17:49:561198 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571199 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1200 // indicates a buggy server. See:
1201 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1202 if (request_->method == "PUT")
1203 return ERR_METHOD_NOT_SUPPORTED;
1204 }
[email protected]4ddaf2502008-10-23 18:26:191205
[email protected]0877e3d2009-10-17 22:29:571206 // Check for an intermediate 100 Continue response. An origin server is
1207 // allowed to send this response even if we didn't ask for it, so we just
1208 // need to skip over it.
1209 // We treat any other 1xx in this same way (although in practice getting
1210 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441211 // Unless this is a WebSocket request, in which case we pass it on up.
1212 if (response_.headers->response_code() / 100 == 1 &&
1213 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451214 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571215 next_state_ = STATE_READ_HEADERS;
1216 return OK;
1217 }
1218
bncb26024382016-06-29 02:39:451219 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111220 session_->http_stream_factory()->ProcessAlternativeServices(
1221 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1222 }
[email protected]564b4912010-03-09 16:30:421223
asanka5ffd5d72016-03-23 16:20:491224 if (IsSecureRequest())
1225 stream_->GetSSLInfo(&response_.ssl_info);
1226
[email protected]e772db3f2010-07-12 18:11:131227 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571228 if (rv != OK)
1229 return rv;
1230
[email protected]0877e3d2009-10-17 22:29:571231 headers_valid_ = true;
1232 return OK;
initial.commit586acc5fe2008-07-26 22:42:521233}
1234
1235int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501236 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131237 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381238 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521239
1240 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501241 return stream_->ReadResponseBody(
1242 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521243}
1244
1245int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1246 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381247 bool done = false;
1248 if (result <= 0) {
1249 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521250 done = true;
[email protected]8e6441ca2010-08-19 05:56:381251 }
[email protected]9492e4a2010-02-24 00:58:461252
mmenkebd84c392015-09-02 14:12:341253 // Clean up connection if we are done.
1254 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381255 // Note: Just because IsResponseBodyComplete is true, we're not
1256 // necessarily "done". We're only "done" when it is the last
1257 // read on this HttpNetworkTransaction, which will be signified
1258 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341259 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381260 // the stream. No need to compute it here just to pass back
1261 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341262 bool keep_alive =
1263 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521264
[email protected]8e6441ca2010-08-19 05:56:381265 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011266 // Note: we don't reset the stream here. We've closed it, but we still
1267 // need it around so that callers can call methods such as
1268 // GetUploadProgress() and have them be meaningful.
1269 // TODO(mbelshe): This means we closed the stream here, and we close it
1270 // again in ~HttpNetworkTransaction. Clean that up.
1271
[email protected]8e6441ca2010-08-19 05:56:381272 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521273 }
1274
1275 // Clear these to avoid leaving around old state.
1276 read_buf_ = NULL;
1277 read_buf_len_ = 0;
1278
1279 return result;
1280}
1281
[email protected]2d2697f92009-02-18 21:00:321282int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1283 // This method differs from DoReadBody only in the next_state_. So we just
1284 // call DoReadBody and override the next_state_. Perhaps there is a more
1285 // elegant way for these two methods to share code.
1286 int rv = DoReadBody();
1287 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1288 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1289 return rv;
1290}
1291
[email protected]0877e3d2009-10-17 22:29:571292// TODO(wtc): This method and the DoReadBodyComplete method are almost
1293// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321294int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231295 // keep_alive defaults to true because the very reason we're draining the
1296 // response body is to reuse the connection for auth restart.
1297 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321298 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571299 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321300 done = true;
[email protected]68873ba2009-06-04 21:49:231301 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311302 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571303 done = true;
[email protected]2d2697f92009-02-18 21:00:321304 }
1305
1306 if (done) {
1307 DidDrainBodyForAuthRestart(keep_alive);
1308 } else {
1309 // Keep draining.
1310 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1311 }
1312
1313 return OK;
1314}
1315
[email protected]5e363962009-06-19 19:57:011316int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381317 // There are two paths through which the server can request a certificate
1318 // from us. The first is during the initial handshake, the second is
1319 // during SSL renegotiation.
1320 //
1321 // In both cases, we want to close the connection before proceeding.
1322 // We do this for two reasons:
1323 // First, we don't want to keep the connection to the server hung for a
1324 // long time while the user selects a certificate.
1325 // Second, even if we did keep the connection open, NSS has a bug where
1326 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581327 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381328
1329 if (stream_.get()) {
1330 // Since we already have a stream, we're being called as part of SSL
1331 // renegotiation.
1332 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191333 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221334 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381335 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121336 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381337 }
1338
[email protected]26816882010-10-14 18:03:091339 // The server is asking for a client certificate during the initial
1340 // handshake.
1341 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011342
[email protected]ec229bc92010-11-22 09:51:451343 // If the user selected one of the certificates in client_certs or declined
1344 // to provide one for this server before, use the past decision
1345 // automatically.
1346 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541347 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451348 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541349 response_.cert_request_info->host_and_port, &client_cert,
1350 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451351 if (!found_cached_cert)
1352 return error;
1353
1354 // Check that the certificate selected is still a certificate the server
1355 // is likely to accept, based on the criteria supplied in the
1356 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501357 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261358 const std::vector<std::string>& cert_authorities =
1359 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451360
[email protected]f1958c382013-02-07 00:15:261361 bool cert_still_valid = cert_authorities.empty() ||
1362 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451363 if (!cert_still_valid)
1364 return error;
[email protected]5e363962009-06-19 19:57:011365 }
[email protected]ec229bc92010-11-22 09:51:451366
1367 // TODO(davidben): Add a unit test which covers this path; we need to be
1368 // able to send a legitimate certificate and also bypass/clear the
1369 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141370 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1371 &proxy_ssl_config_ : &server_ssl_config_;
1372 ssl_config->send_client_cert = true;
1373 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541374 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451375 next_state_ = STATE_CREATE_STREAM;
1376 // Reset the other member variables.
1377 // Note: this is necessary only with SSL renegotiation.
1378 ResetStateForRestart();
1379 return OK;
[email protected]0b45559b2009-06-12 21:45:111380}
1381
bncfacdd852015-01-09 19:22:541382int HttpNetworkTransaction::HandleHttp11Required(int error) {
1383 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1384 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1385
1386 if (error == ERR_HTTP_1_1_REQUIRED) {
1387 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1388 } else {
1389 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1390 }
1391 ResetConnectionAndRequestForResend();
1392 return OK;
1393}
1394
[email protected]384cc73a2013-12-08 22:41:031395void HttpNetworkTransaction::HandleClientAuthError(int error) {
1396 if (server_ssl_config_.send_client_cert &&
1397 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1398 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411399 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031400 }
1401}
1402
[email protected]bd0b6772011-01-11 19:59:301403// TODO(rch): This does not correctly handle errors when an SSL proxy is
1404// being used, as all of the errors are handled as if they were generated
1405// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381406// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301407int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1408 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031409 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301410
davidbena4c9d062015-04-03 22:34:251411 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1412 // reflect servers require a deprecated cipher rather than merely prefer
1413 // it. This, however, has no security benefit until the ciphers are actually
1414 // removed.
davidben14b1a532015-10-30 16:01:091415 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251416 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1417 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1418 net_log_.AddEvent(
1419 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1420 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091421 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251422 ResetConnectionAndRequestForResend();
1423 return OK;
1424 }
1425
initial.commit586acc5fe2008-07-26 22:42:521426 return error;
1427}
1428
[email protected]bd0b6772011-01-11 19:59:301429// This method determines whether it is safe to resend the request after an
1430// IO error. It can only be called in response to request header or body
1431// write errors or response header read errors. It should not be used in
1432// other cases, such as a Connect error.
1433int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031434 // Because the peer may request renegotiation with client authentication at
1435 // any time, check and handle client authentication errors.
1436 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301437
1438 switch (error) {
1439 // If we try to reuse a connection that the server is in the process of
1440 // closing, we may end up successfully writing out our request (or a
1441 // portion of our request) only to find a connection error when we try to
1442 // read from (or finish writing to) the socket.
1443 case ERR_CONNECTION_RESET:
1444 case ERR_CONNECTION_CLOSED:
1445 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511446 // There can be a race between the socket pool checking checking whether a
1447 // socket is still connected, receiving the FIN, and sending/reading data
1448 // on a reused socket. If we receive the FIN between the connectedness
1449 // check and writing/reading from the socket, we may first learn the socket
1450 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1451 // likely happen when trying to retrieve its IP address.
1452 // See http://crbug.com/105824 for more details.
1453 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491454 // If a socket is closed on its initial request, HttpStreamParser returns
1455 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1456 // preconnected but failed to be used before the server timed it out.
1457 case ERR_EMPTY_RESPONSE:
1458 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381459 net_log_.AddEventWithNetErrorCode(
1460 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301461 ResetConnectionAndRequestForResend();
1462 error = OK;
1463 }
1464 break;
[email protected]8753a122011-10-16 08:05:081465 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001466 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271467 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381468 net_log_.AddEventWithNetErrorCode(
1469 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001470 ResetConnectionAndRequestForResend();
1471 error = OK;
1472 break;
[email protected]bd0b6772011-01-11 19:59:301473 }
1474 return error;
1475}
1476
[email protected]c3b35c22008-09-27 03:19:421477void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581478 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221479 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191480 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221481 total_sent_bytes_ += stream_->GetTotalSentBytes();
1482 }
zhongyica364fbb2015-12-12 03:39:121483 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581484}
1485
1486void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251487 send_start_time_ = base::TimeTicks();
1488 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251489
[email protected]0757e7702009-03-27 04:00:221490 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421491 read_buf_ = NULL;
1492 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571493 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201494 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141495 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381496 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591497 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121498 net_error_details_.quic_broken = false;
1499 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481500 provided_token_binding_key_.reset();
1501 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121502}
1503
1504void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1505 if (stream_)
1506 stream_->PopulateNetErrorDetails(&net_error_details_);
1507 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571508}
1509
davidben701ca982015-05-18 21:21:421510void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421511 if (result != OK)
1512 return;
1513
davidbenca9d69162015-05-04 20:18:451514 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091515 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451516}
1517
[email protected]0877e3d2009-10-17 22:29:571518HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501519 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421520}
1521
[email protected]a34f61ee2014-03-18 20:59:491522bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381523 bool connection_is_proven = stream_->IsConnectionReused();
1524 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161525
[email protected]2a5c76b2008-09-25 22:15:161526 // NOTE: we resend a request only if we reused a keep-alive connection.
1527 // This automatically prevents an infinite resend loop because we'll run
1528 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381529 if (connection_is_proven && !has_received_headers)
1530 return true;
1531 return false;
[email protected]1c773ea12009-04-28 19:58:421532}
1533
1534void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381535 if (stream_.get()) {
1536 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121537 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161538 }
1539
[email protected]0877e3d2009-10-17 22:29:571540 // We need to clear request_headers_ because it contains the real request
1541 // headers, but we may need to resend the CONNECT request first to recreate
1542 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201543 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501544 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541545}
1546
[email protected]1c773ea12009-04-28 19:58:421547bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241548 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421549}
license.botbf09a502008-08-24 00:55:551550
[email protected]1c773ea12009-04-28 19:58:421551bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041552 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421553}
1554
[email protected]e772db3f2010-07-12 18:11:131555int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271556 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501557 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421558
[email protected]0877e3d2009-10-17 22:29:571559 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581560 if (status != HTTP_UNAUTHORIZED &&
1561 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421562 return OK;
[email protected]9094b602012-02-27 21:44:581563 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111564 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161565 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1566 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421567
[email protected]9094b602012-02-27 21:44:581568 // This case can trigger when an HTTPS server responds with a "Proxy
1569 // authentication required" status code through a non-authenticating
1570 // proxy.
[email protected]7a67a8152010-11-05 18:31:101571 if (!auth_controllers_[target].get())
1572 return ERR_UNEXPECTED_PROXY_AUTH;
1573
[email protected]a7ea8832010-07-12 17:54:541574 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491575 headers, response_.ssl_info,
1576 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311577 net_log_);
[email protected]228404f2010-06-24 04:31:411578 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491579 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411580
1581 scoped_refptr<AuthChallengeInfo> auth_info =
1582 auth_controllers_[target]->auth_info();
1583 if (auth_info.get())
1584 response_.auth_challenge = auth_info;
1585
[email protected]228404f2010-06-24 04:31:411586 return rv;
[email protected]f9ee6b52008-11-08 06:46:231587}
1588
[email protected]8e6441ca2010-08-19 05:56:381589bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1590 return auth_controllers_[target].get() &&
1591 auth_controllers_[target]->HaveAuth();
1592}
1593
[email protected]228404f2010-06-24 04:31:411594GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1595 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461596 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411597 if (!proxy_info_.proxy_server().is_valid() ||
1598 proxy_info_.proxy_server().is_direct()) {
1599 return GURL(); // There is no proxy server.
1600 }
[email protected]2df19bb2010-08-25 20:13:461601 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1602 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351603 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461604 }
[email protected]228404f2010-06-24 04:31:411605 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291606 if (ForWebSocketHandshake()) {
1607 const GURL& url = request_->url;
1608 url::Replacements<char> ws_to_http;
1609 if (url.SchemeIs("ws")) {
1610 ws_to_http.SetScheme("http", url::Component(0, 4));
1611 } else {
1612 DCHECK(url.SchemeIs("wss"));
1613 ws_to_http.SetScheme("https", url::Component(0, 5));
1614 }
1615 return url.ReplaceComponents(ws_to_http);
1616 }
[email protected]228404f2010-06-24 04:31:411617 return request_->url;
1618 default:
1619 return GURL();
1620 }
[email protected]c3b35c22008-09-27 03:19:421621}
1622
[email protected]831e4a32013-11-14 02:14:441623bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141624 return websocket_handshake_stream_base_create_helper_ &&
1625 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441626}
1627
[email protected]d8eb84242010-09-25 02:25:061628#define STATE_CASE(s) \
1629 case s: \
1630 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1631 break
[email protected]aef04272010-06-28 18:03:041632
1633std::string HttpNetworkTransaction::DescribeState(State state) {
1634 std::string description;
1635 switch (state) {
[email protected]1826a402014-01-08 15:40:481636 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501637 STATE_CASE(STATE_CREATE_STREAM);
1638 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131639 STATE_CASE(STATE_INIT_REQUEST_BODY);
1640 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511641 STATE_CASE(STATE_BUILD_REQUEST);
1642 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041643 STATE_CASE(STATE_SEND_REQUEST);
1644 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1645 STATE_CASE(STATE_READ_HEADERS);
1646 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041647 STATE_CASE(STATE_READ_BODY);
1648 STATE_CASE(STATE_READ_BODY_COMPLETE);
1649 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1650 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041651 STATE_CASE(STATE_NONE);
1652 default:
[email protected]d8eb84242010-09-25 02:25:061653 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1654 state);
[email protected]aef04272010-06-28 18:03:041655 break;
1656 }
1657 return description;
1658}
1659
1660#undef STATE_CASE
1661
ttuttle1f2d7e92015-04-28 16:17:471662void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1663 DCHECK(stream_request_);
1664
1665 // Since the transaction can restart with auth credentials, it may create a
1666 // stream more than once. Accumulate all of the connection attempts across
1667 // those streams by appending them to the vector:
1668 for (const auto& attempt : stream_request_->connection_attempts())
1669 connection_attempts_.push_back(attempt);
1670}
1671
[email protected]c3b35c22008-09-27 03:19:421672} // namespace net