blob: e889bbfb7526b73500d272bc56d1ea43258d53bf [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
danakj1fd259a02016-04-16 03:17:097#include <memory>
[email protected]2fbaecf22010-07-22 22:20:358#include <set>
dchengc7eeda422015-12-26 03:56:489#include <utility>
[email protected]2fbaecf22010-07-22 22:20:3510#include <vector>
11
nharperb7441ef2016-01-25 23:54:1412#include "base/base64url.h"
[email protected]49639fa2011-12-20 23:22:4113#include "base/bind.h"
14#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3015#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4716#include "base/format_macros.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3718#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4219#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4120#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2421#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4722#include "base/strings/string_number_conversions.h"
23#include "base/strings/string_util.h"
24#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4025#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5326#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3027#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3528#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5529#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1930#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2532#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2633#include "net/base/net_errors.h"
initial.commit586acc5fe2008-07-26 22:42:5234#include "net/base/upload_data_stream.h"
tfarina7a4a7fd2016-01-20 14:23:4435#include "net/base/url_util.h"
[email protected]c3b35c22008-09-27 03:19:4236#include "net/http/http_auth.h"
37#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2438#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3639#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5240#include "net/http/http_chunked_decoder.h"
41#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5442#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1343#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4744#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5245#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2146#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5747#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5348#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5849#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3950#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3151#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5252#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2353#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3654#include "net/http/url_security_manager.h"
mikecirone8b85c432016-09-08 19:11:0055#include "net/log/net_log_event_type.h"
[email protected]f7984fc62009-06-22 23:26:4456#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2657#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4458#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1859#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2860#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4061#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3262#include "net/spdy/spdy_session.h"
63#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5764#include "net/ssl/ssl_cert_request_info.h"
65#include "net/ssl/ssl_connection_status_flags.h"
svaldez7872fd02015-11-19 21:10:5466#include "net/ssl/ssl_private_key.h"
nharperb7441ef2016-01-25 23:54:1467#include "net/ssl/token_binding.h"
[email protected]f89276a72013-07-12 06:41:5468#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2969#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5270
initial.commit586acc5fe2008-07-26 22:42:5271namespace net {
72
[email protected]1c773ea12009-04-28 19:58:4273namespace {
74
danakj1fd259a02016-04-16 03:17:0975std::unique_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:1776 const GURL* url,
77 int net_error,
78 NetLogCaptureMode /* capture_mode */) {
danakj1fd259a02016-04-16 03:17:0979 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:2580 dict->SetString("host_and_port", GetHostAndPort(*url));
81 dict->SetInteger("net_error", net_error);
dchengc7eeda422015-12-26 03:56:4882 return std::move(dict);
davidbena4c9d062015-04-03 22:34:2583}
84
[email protected]1c773ea12009-04-28 19:58:4285} // namespace
86
initial.commit586acc5fe2008-07-26 22:42:5287//-----------------------------------------------------------------------------
88
[email protected]262eec82013-03-19 21:01:3689HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
90 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:2291 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:3292 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
93 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:5294 session_(session),
95 request_(NULL),
[email protected]262eec82013-03-19 21:01:3696 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:5797 headers_valid_(false),
[email protected]b94f92d2010-10-27 16:45:2098 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:5299 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19100 total_received_bytes_(0),
sclittlefb249892015-09-10 21:33:22101 total_sent_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54102 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44103 establishing_tunnel_(false),
zhongyi48704c182015-12-07 07:52:02104 websocket_handshake_stream_base_create_helper_(NULL),
zhongyica364fbb2015-12-12 03:39:12105 net_error_details_() {
[email protected]3ce7df0f2010-03-03 00:30:50106}
107
[email protected]0b0bf032010-09-21 18:08:50108HttpNetworkTransaction::~HttpNetworkTransaction() {
109 if (stream_.get()) {
[email protected]0b0bf032010-09-21 18:08:50110 // TODO(mbelshe): The stream_ should be able to compute whether or not the
111 // stream should be kept alive. No reason to compute here
112 // and pass it in.
mmenkebd84c392015-09-02 14:12:34113 if (!stream_->CanReuseConnection() || next_state_ != STATE_NONE) {
[email protected]0b0bf032010-09-21 18:08:50114 stream_->Close(true /* not reusable */);
mmenkebd84c392015-09-02 14:12:34115 } else if (stream_->IsResponseBodyComplete()) {
116 // If the response body is complete, we can just reuse the socket.
117 stream_->Close(false /* reusable */);
[email protected]0b0bf032010-09-21 18:08:50118 } else {
mmenkebd84c392015-09-02 14:12:34119 // Otherwise, we try to drain the response body.
120 HttpStream* stream = stream_.release();
121 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50122 }
123 }
[email protected]02cad5d2013-10-02 08:14:03124
125 if (request_ && request_->upload_data_stream)
126 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50127}
128
[email protected]684970b2009-08-14 04:54:46129int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41130 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53131 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53132 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04133 request_ = request_info;
134
nharper8cdb0fb2016-04-22 21:34:59135 // Now that we have an HttpRequestInfo object, update server_ssl_config_.
136 session_->GetSSLConfig(*request_, &server_ssl_config_, &proxy_ssl_config_);
137
[email protected]99ffa5a2011-10-06 04:20:19138 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14139 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19140 proxy_ssl_config_.rev_checking_enabled = false;
141 }
[email protected]6fbac162011-06-20 00:29:04142
jkarlinfb1d5172015-01-12 14:10:29143 if (request_->load_flags & LOAD_PREFETCH)
144 response_.unused_since_prefetch = true;
145
[email protected]1826a402014-01-08 15:40:48146 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04147 int rv = DoLoop(OK);
148 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41149 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04150 return rv;
151}
152
153int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41154 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38155 DCHECK(!stream_.get());
156 DCHECK(!stream_request_.get());
157 DCHECK_EQ(STATE_NONE, next_state_);
158
[email protected]82918cc2010-08-25 17:24:50159 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38160
[email protected]ccb40e52008-09-17 20:54:40161 int rv = DoLoop(OK);
162 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41163 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11164 return rv;
[email protected]96d570e42008-08-05 22:43:04165}
166
[email protected]0b45559b2009-06-12 21:45:11167int HttpNetworkTransaction::RestartWithCertificate(
svaldez7872fd02015-11-19 21:10:54168 X509Certificate* client_cert,
169 SSLPrivateKey* client_private_key,
170 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38171 // In HandleCertificateRequest(), we always tear down existing stream
172 // requests to force a new connection. So we shouldn't have one here.
173 DCHECK(!stream_request_.get());
174 DCHECK(!stream_.get());
175 DCHECK_EQ(STATE_NONE, next_state_);
176
[email protected]102957f2011-09-02 17:10:14177 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
178 &proxy_ssl_config_ : &server_ssl_config_;
179 ssl_config->send_client_cert = true;
180 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:54181 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:45182 session_->ssl_client_auth_cache()->Add(
svaldez7872fd02015-11-19 21:10:54183 response_.cert_request_info->host_and_port, client_cert,
184 client_private_key);
[email protected]0b45559b2009-06-12 21:45:11185 // Reset the other member variables.
186 // Note: this is necessary only with SSL renegotiation.
187 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50188 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11189 int rv = DoLoop(OK);
190 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41191 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11192 return rv;
193}
194
[email protected]49639fa2011-12-20 23:22:41195int HttpNetworkTransaction::RestartWithAuth(
196 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22197 HttpAuth::Target target = pending_auth_target_;
198 if (target == HttpAuth::AUTH_NONE) {
199 NOTREACHED();
200 return ERR_UNEXPECTED;
201 }
[email protected]0757e7702009-03-27 04:00:22202 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42203
[email protected]f3cf9802011-10-28 18:44:58204 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13205
[email protected]49639fa2011-12-20 23:22:41206 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38207
208 int rv = OK;
209 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
210 // In this case, we've gathered credentials for use with proxy
211 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50212 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38213 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59214 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54215 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58216 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54217 } else {
[email protected]8e6441ca2010-08-19 05:56:38218 // In this case, we've gathered credentials for the server or the proxy
219 // but it is not during the tunneling phase.
220 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54221 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38222 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54223 }
[email protected]c3b35c22008-09-27 03:19:42224
[email protected]c3b35c22008-09-27 03:19:42225 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41226 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42227 return rv;
[email protected]96d570e42008-08-05 22:43:04228}
229
[email protected]f9ee6b52008-11-08 06:46:23230void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
231 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38232 DCHECK(!stream_request_.get());
233
[email protected]2d2697f92009-02-18 21:00:32234 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57235 // Even if the server says the connection is keep-alive, we have to be
236 // able to find the end of each response in order to reuse the connection.
mmenkebd84c392015-09-02 14:12:34237 if (stream_->CanReuseConnection()) {
[email protected]0877e3d2009-10-17 22:29:57238 // If the response body hasn't been completely read, we need to drain
239 // it first.
[email protected]351ab642010-08-05 16:55:31240 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32241 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57242 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32243 read_buf_len_ = kDrainBodyBufferSize;
244 return;
245 }
[email protected]0877e3d2009-10-17 22:29:57246 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09247 }
248
[email protected]2d2697f92009-02-18 21:00:32249 // We don't need to drain the response body, so we act as if we had drained
250 // the response body.
251 DidDrainBodyForAuthRestart(keep_alive);
252}
253
254void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38255 DCHECK(!stream_request_.get());
256
257 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19258 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22259 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]697ef4c2010-10-14 16:38:58260 HttpStream* new_stream = NULL;
mmenkebd84c392015-09-02 14:12:34261 if (keep_alive && stream_->CanReuseConnection()) {
[email protected]8e6441ca2010-08-19 05:56:38262 // We should call connection_->set_idle_time(), but this doesn't occur
263 // often enough to be worth the trouble.
264 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39265 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38266 }
[email protected]697ef4c2010-10-14 16:38:58267
268 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46269 // Close the stream and mark it as not_reusable. Even in the
270 // keep_alive case, we've determined that the stream_ is not
271 // reusable if new_stream is NULL.
272 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58273 next_state_ = STATE_CREATE_STREAM;
274 } else {
sclittlefb249892015-09-10 21:33:22275 // Renewed streams shouldn't carry over sent or received bytes.
[email protected]b8015c42013-12-24 15:18:19276 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
sclittlefb249892015-09-10 21:33:22277 DCHECK_EQ(0, new_stream->GetTotalSentBytes());
[email protected]697ef4c2010-10-14 16:38:58278 next_state_ = STATE_INIT_STREAM;
279 }
280 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32281 }
[email protected]f9ee6b52008-11-08 06:46:23282
283 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58284 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23285}
286
[email protected]8e6441ca2010-08-19 05:56:38287bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
288 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
289 HaveAuth(pending_auth_target_);
290}
291
[email protected]9dea9e1f2009-01-29 00:30:47292int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41293 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04294 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35295 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04296
[email protected]1f14a912009-12-21 20:32:44297 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04298
[email protected]ad8e04a2010-11-01 04:16:27299 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38300 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04301 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29302 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04303 // bytes when establishing a tunnel because they might be controlled by
304 // an active network attacker. We don't worry about this for HTTP
305 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29306 // We reach this case when the user cancels a 407 proxy auth prompt. We
307 // also don't worry about this for an HTTPS Proxy, because the
308 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04309 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46310 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58311 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54312 LOG(WARNING) << "Blocked proxy response with status "
313 << headers->response_code() << " to CONNECT request for "
314 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04315 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44316 }
317
[email protected]e60e47a2010-07-14 03:37:18318 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31319 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18320
[email protected]96d570e42008-08-05 22:43:04321 read_buf_ = buf;
322 read_buf_len_ = buf_len;
323
[email protected]1f14a912009-12-21 20:32:44324 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04325 int rv = DoLoop(OK);
326 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41327 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04328 return rv;
329}
330
[email protected]8cd06c02014-01-25 07:50:14331void HttpNetworkTransaction::StopCaching() {}
332
[email protected]79e1fd62013-06-20 06:50:04333bool HttpNetworkTransaction::GetFullRequestHeaders(
334 HttpRequestHeaders* headers) const {
ttuttle960fcbf2016-04-19 13:26:32335 // TODO(juliatuttle): Make sure we've populated request_headers_.
[email protected]79e1fd62013-06-20 06:50:04336 *headers = request_headers_;
337 return true;
338}
339
sclittle4de1bab92015-09-22 21:28:24340int64_t HttpNetworkTransaction::GetTotalReceivedBytes() const {
341 int64_t total_received_bytes = total_received_bytes_;
[email protected]b8015c42013-12-24 15:18:19342 if (stream_)
343 total_received_bytes += stream_->GetTotalReceivedBytes();
344 return total_received_bytes;
345}
346
sclittlefb249892015-09-10 21:33:22347int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
348 int64_t total_sent_bytes = total_sent_bytes_;
349 if (stream_)
350 total_sent_bytes += stream_->GetTotalSentBytes();
351 return total_sent_bytes;
352}
353
[email protected]8cd06c02014-01-25 07:50:14354void HttpNetworkTransaction::DoneReading() {}
355
[email protected]96d570e42008-08-05 22:43:04356const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55357 return &response_;
[email protected]96d570e42008-08-05 22:43:04358}
359
360LoadState HttpNetworkTransaction::GetLoadState() const {
361 // TODO(wtc): Define a new LoadState value for the
362 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
363 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48364 case STATE_CREATE_STREAM:
365 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50366 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38367 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15368 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
369 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57370 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04371 return LOAD_STATE_SENDING_REQUEST;
372 case STATE_READ_HEADERS_COMPLETE:
373 return LOAD_STATE_WAITING_FOR_RESPONSE;
374 case STATE_READ_BODY_COMPLETE:
375 return LOAD_STATE_READING_RESPONSE;
376 default:
377 return LOAD_STATE_IDLE;
378 }
379}
380
[email protected]196d18a2012-08-30 03:47:31381UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31382 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31383 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04384
yhiranoa7e05bb2014-11-06 05:40:39385 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04386}
387
[email protected]8cd06c02014-01-25 07:50:14388void HttpNetworkTransaction::SetQuicServerInfo(
389 QuicServerInfo* quic_server_info) {}
390
[email protected]5033ab82013-03-22 20:17:46391bool HttpNetworkTransaction::GetLoadTimingInfo(
392 LoadTimingInfo* load_timing_info) const {
393 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
394 return false;
395
396 load_timing_info->proxy_resolve_start =
397 proxy_info_.proxy_resolve_start_time();
398 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
399 load_timing_info->send_start = send_start_time_;
400 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46401 return true;
402}
403
ttuttled9dbc652015-09-29 20:00:59404bool HttpNetworkTransaction::GetRemoteEndpoint(IPEndPoint* endpoint) const {
thestiga74ad2b2016-07-11 20:52:36405 if (remote_endpoint_.address().empty())
ttuttled9dbc652015-09-29 20:00:59406 return false;
407
408 *endpoint = remote_endpoint_;
409 return true;
410}
411
zhongyi48704c182015-12-07 07:52:02412void HttpNetworkTransaction::PopulateNetErrorDetails(
413 NetErrorDetails* details) const {
zhongyica364fbb2015-12-12 03:39:12414 *details = net_error_details_;
415 if (stream_)
416 stream_->PopulateNetErrorDetails(details);
zhongyi48704c182015-12-07 07:52:02417}
418
[email protected]5033ab82013-03-22 20:17:46419void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
420 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47421 if (stream_request_)
422 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03423 if (stream_)
424 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46425}
426
[email protected]831e4a32013-11-14 02:14:44427void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
428 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
429 websocket_handshake_stream_base_create_helper_ = create_helper;
430}
431
[email protected]1826a402014-01-08 15:40:48432void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
433 const BeforeNetworkStartCallback& callback) {
434 before_network_start_callback_ = callback;
435}
436
ryansturm49a8cb12016-06-15 16:51:09437void HttpNetworkTransaction::SetBeforeHeadersSentCallback(
438 const BeforeHeadersSentCallback& callback) {
439 before_headers_sent_callback_ = callback;
[email protected]597a1ab2014-06-26 08:12:27440}
441
[email protected]1826a402014-01-08 15:40:48442int HttpNetworkTransaction::ResumeNetworkStart() {
443 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
444 return DoLoop(OK);
445}
446
[email protected]102e27c2011-02-23 01:01:31447void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
448 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39449 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50450 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38451 DCHECK(stream_request_.get());
452
sclittlefb249892015-09-10 21:33:22453 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19454 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22455 total_sent_bytes_ += stream_->GetTotalSentBytes();
456 }
[email protected]8e6441ca2010-08-19 05:56:38457 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14458 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31459 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38460 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51461 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
bnc6227b26e2016-08-12 02:00:43462 stream_request_->negotiated_protocol());
[email protected]8e6441ca2010-08-19 05:56:38463 response_.was_fetched_via_spdy = stream_request_->using_spdy();
464 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15465 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
466 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38467 OnIOComplete(OK);
468}
469
xunjieli5749218c2016-03-22 16:43:06470void HttpNetworkTransaction::OnBidirectionalStreamImplReady(
xunjieli11834f02015-12-22 04:27:08471 const SSLConfig& used_ssl_config,
472 const ProxyInfo& used_proxy_info,
xunjieli5749218c2016-03-22 16:43:06473 BidirectionalStreamImpl* stream) {
xunjieli11834f02015-12-22 04:27:08474 NOTREACHED();
475}
476
[email protected]a9cf2b92013-10-30 12:08:49477void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50478 const SSLConfig& used_ssl_config,
479 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49480 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44481 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50482}
483
[email protected]102e27c2011-02-23 01:01:31484void HttpNetworkTransaction::OnStreamFailed(int result,
davidben849365422016-06-24 19:04:13485 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50486 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38487 DCHECK_NE(OK, result);
488 DCHECK(stream_request_.get());
489 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14490 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38491
492 OnIOComplete(result);
493}
494
[email protected]102e27c2011-02-23 01:01:31495void HttpNetworkTransaction::OnCertificateError(
496 int result,
497 const SSLConfig& used_ssl_config,
498 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50499 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38500 DCHECK_NE(OK, result);
501 DCHECK(stream_request_.get());
502 DCHECK(!stream_.get());
503
504 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14505 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38506
507 // TODO(mbelshe): For now, we're going to pass the error through, and that
508 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50509 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
510 // good and the user chooses to ignore the error. This is not ideal, but not
511 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38512
513 OnIOComplete(result);
514}
515
516void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50517 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31518 const SSLConfig& used_ssl_config,
519 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50520 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38521 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50522 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38523
524 establishing_tunnel_ = true;
525 response_.headers = proxy_response.headers;
526 response_.auth_challenge = proxy_response.auth_challenge;
527 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14528 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31529 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38530
531 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
532 pending_auth_target_ = HttpAuth::AUTH_PROXY;
533
534 DoCallback(OK);
535}
536
537void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31538 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50539 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50540 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38541
[email protected]102957f2011-09-02 17:10:14542 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38543 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58544 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38545}
546
[email protected]511f6f52010-12-17 03:58:29547void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
548 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31549 const SSLConfig& used_ssl_config,
550 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39551 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29552 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
553
ttuttle1f2d7e92015-04-28 16:17:47554 CopyConnectionAttemptsFromStreamRequest();
555
[email protected]511f6f52010-12-17 03:58:29556 headers_valid_ = true;
557 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14558 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31559 proxy_info_ = used_proxy_info;
sclittlefb249892015-09-10 21:33:22560 if (stream_) {
[email protected]b8015c42013-12-24 15:18:19561 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:22562 total_sent_bytes_ += stream_->GetTotalSentBytes();
563 }
[email protected]511f6f52010-12-17 03:58:29564 stream_.reset(stream);
565 stream_request_.reset(); // we're done with the stream request
566 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
567}
568
zhongyi48704c182015-12-07 07:52:02569void HttpNetworkTransaction::OnQuicBroken() {
zhongyica364fbb2015-12-12 03:39:12570 net_error_details_.quic_broken = true;
zhongyi48704c182015-12-07 07:52:02571}
572
ttuttle1f2d7e92015-04-28 16:17:47573void HttpNetworkTransaction::GetConnectionAttempts(
574 ConnectionAttempts* out) const {
575 *out = connection_attempts_;
576}
577
Adam Ricecb76ac62015-02-20 05:33:25578bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37579 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52580}
581
nharperb7441ef2016-01-25 23:54:14582bool HttpNetworkTransaction::IsTokenBindingEnabled() const {
583 if (!IsSecureRequest())
584 return false;
585 SSLInfo ssl_info;
586 stream_->GetSSLInfo(&ssl_info);
587 return ssl_info.token_binding_negotiated &&
588 ssl_info.token_binding_key_param == TB_PARAM_ECDSAP256 &&
589 session_->params().channel_id_service;
590}
591
592void HttpNetworkTransaction::RecordTokenBindingSupport() const {
593 // This enum is used for an UMA histogram - do not change or re-use values.
594 enum {
595 DISABLED = 0,
596 CLIENT_ONLY = 1,
597 CLIENT_AND_SERVER = 2,
598 CLIENT_NO_CHANNEL_ID_SERVICE = 3,
599 TOKEN_BINDING_SUPPORT_MAX
600 } supported;
601 if (!IsSecureRequest())
602 return;
603 SSLInfo ssl_info;
604 stream_->GetSSLInfo(&ssl_info);
605 if (!session_->params().enable_token_binding) {
606 supported = DISABLED;
607 } else if (!session_->params().channel_id_service) {
608 supported = CLIENT_NO_CHANNEL_ID_SERVICE;
609 } else if (ssl_info.token_binding_negotiated) {
610 supported = CLIENT_AND_SERVER;
611 } else {
612 supported = CLIENT_ONLY;
613 }
614 UMA_HISTOGRAM_ENUMERATION("Net.TokenBinding.Support", supported,
615 TOKEN_BINDING_SUPPORT_MAX);
616}
617
Adam Rice425cf122015-01-19 06:18:24618bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12619 return (proxy_info_.is_http() || proxy_info_.is_https() ||
620 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24621 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
622}
623
initial.commit586acc5fe2008-07-26 22:42:52624void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50625 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41626 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52627
[email protected]96d570e42008-08-05 22:43:04628 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41629 CompletionCallback c = callback_;
630 callback_.Reset();
631 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52632}
633
634void HttpNetworkTransaction::OnIOComplete(int result) {
635 int rv = DoLoop(result);
636 if (rv != ERR_IO_PENDING)
637 DoCallback(rv);
638}
639
640int HttpNetworkTransaction::DoLoop(int result) {
641 DCHECK(next_state_ != STATE_NONE);
642
643 int rv = result;
644 do {
645 State state = next_state_;
646 next_state_ = STATE_NONE;
647 switch (state) {
[email protected]1826a402014-01-08 15:40:48648 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
649 DCHECK_EQ(OK, rv);
650 rv = DoNotifyBeforeCreateStream();
651 break;
[email protected]82918cc2010-08-25 17:24:50652 case STATE_CREATE_STREAM:
653 DCHECK_EQ(OK, rv);
654 rv = DoCreateStream();
655 break;
656 case STATE_CREATE_STREAM_COMPLETE:
657 rv = DoCreateStreamComplete(rv);
658 break;
[email protected]351ab642010-08-05 16:55:31659 case STATE_INIT_STREAM:
660 DCHECK_EQ(OK, rv);
661 rv = DoInitStream();
662 break;
663 case STATE_INIT_STREAM_COMPLETE:
664 rv = DoInitStreamComplete(rv);
665 break;
[email protected]044de0642010-06-17 10:42:15666 case STATE_GENERATE_PROXY_AUTH_TOKEN:
667 DCHECK_EQ(OK, rv);
668 rv = DoGenerateProxyAuthToken();
669 break;
670 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
671 rv = DoGenerateProxyAuthTokenComplete(rv);
672 break;
673 case STATE_GENERATE_SERVER_AUTH_TOKEN:
674 DCHECK_EQ(OK, rv);
675 rv = DoGenerateServerAuthToken();
676 break;
677 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
678 rv = DoGenerateServerAuthTokenComplete(rv);
679 break;
nharperd6e65822016-03-30 23:05:48680 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY:
nharperb7441ef2016-01-25 23:54:14681 DCHECK_EQ(OK, rv);
nharperd6e65822016-03-30 23:05:48682 rv = DoGetProvidedTokenBindingKey();
nharperb7441ef2016-01-25 23:54:14683 break;
nharperd6e65822016-03-30 23:05:48684 case STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE:
685 rv = DoGetProvidedTokenBindingKeyComplete(rv);
686 break;
687 case STATE_GET_REFERRED_TOKEN_BINDING_KEY:
688 DCHECK_EQ(OK, rv);
689 rv = DoGetReferredTokenBindingKey();
690 break;
691 case STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE:
692 rv = DoGetReferredTokenBindingKeyComplete(rv);
nharperb7441ef2016-01-25 23:54:14693 break;
[email protected]daddea62012-09-19 05:51:13694 case STATE_INIT_REQUEST_BODY:
695 DCHECK_EQ(OK, rv);
696 rv = DoInitRequestBody();
697 break;
698 case STATE_INIT_REQUEST_BODY_COMPLETE:
699 rv = DoInitRequestBodyComplete(rv);
700 break;
[email protected]4875ba12011-03-30 22:31:51701 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55702 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00703 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51704 rv = DoBuildRequest();
705 break;
706 case STATE_BUILD_REQUEST_COMPLETE:
707 rv = DoBuildRequestComplete(rv);
708 break;
709 case STATE_SEND_REQUEST:
710 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57711 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52712 break;
[email protected]0877e3d2009-10-17 22:29:57713 case STATE_SEND_REQUEST_COMPLETE:
714 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43715 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00716 NetLogEventType::HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52717 break;
718 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55719 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00720 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52721 rv = DoReadHeaders();
722 break;
723 case STATE_READ_HEADERS_COMPLETE:
724 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43725 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00726 NetLogEventType::HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52727 break;
728 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55729 DCHECK_EQ(OK, rv);
mikecirone8b85c432016-09-08 19:11:00730 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52731 rv = DoReadBody();
732 break;
733 case STATE_READ_BODY_COMPLETE:
734 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43735 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00736 NetLogEventType::HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52737 break;
[email protected]2d2697f92009-02-18 21:00:32738 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55739 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53740 net_log_.BeginEvent(
mikecirone8b85c432016-09-08 19:11:00741 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32742 rv = DoDrainBodyForAuthRestart();
743 break;
744 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
745 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43746 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00747 NetLogEventType::HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32748 break;
initial.commit586acc5fe2008-07-26 22:42:52749 default:
750 NOTREACHED() << "bad state";
751 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04752 break;
initial.commit586acc5fe2008-07-26 22:42:52753 }
754 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
755
756 return rv;
757}
758
[email protected]1826a402014-01-08 15:40:48759int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
760 next_state_ = STATE_CREATE_STREAM;
761 bool defer = false;
762 if (!before_network_start_callback_.is_null())
763 before_network_start_callback_.Run(&defer);
764 if (!defer)
765 return OK;
766 return ERR_IO_PENDING;
767}
768
[email protected]82918cc2010-08-25 17:24:50769int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34770 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26771 tracked_objects::ScopedTracker tracking_profile(
772 FROM_HERE_WITH_EXPLICIT_FUNCTION(
773 "424359 HttpNetworkTransaction::DoCreateStream"));
774
ttuttlec0c828492015-05-15 01:25:55775 response_.network_accessed = true;
776
[email protected]82918cc2010-08-25 17:24:50777 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44778 if (ForWebSocketHandshake()) {
779 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23780 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44781 ->RequestWebSocketHandshakeStream(
782 *request_,
783 priority_,
784 server_ssl_config_,
785 proxy_ssl_config_,
786 this,
787 websocket_handshake_stream_base_create_helper_,
788 net_log_));
789 } else {
790 stream_request_.reset(
791 session_->http_stream_factory()->RequestStream(
792 *request_,
793 priority_,
794 server_ssl_config_,
795 proxy_ssl_config_,
796 this,
797 net_log_));
798 }
[email protected]26816882010-10-14 18:03:09799 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38800 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31801}
802
[email protected]82918cc2010-08-25 17:24:50803int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47804 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
805 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
806 // which resets the stream request first. Therefore, we have to grab the
807 // connection attempts in *that* function instead of here in that case.
808 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
809 CopyConnectionAttemptsFromStreamRequest();
810
davidben701ca982015-05-18 21:21:42811 if (request_->url.SchemeIsCryptographic())
812 RecordSSLFallbackMetrics(result);
813
[email protected]394816e92010-08-03 07:38:59814 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50815 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38816 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33817 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
818 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29819 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
820 // Return OK and let the caller read the proxy's error page
821 next_state_ = STATE_NONE;
822 return OK;
bncfacdd852015-01-09 19:22:54823 } else if (result == ERR_HTTP_1_1_REQUIRED ||
824 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
825 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59826 }
827
[email protected]bd0b6772011-01-11 19:59:30828 // Handle possible handshake errors that may have occurred if the stream
829 // used SSL for one or more of the layers.
830 result = HandleSSLHandshakeError(result);
831
[email protected]8e6441ca2010-08-19 05:56:38832 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09833 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38834 return result;
[email protected]394816e92010-08-03 07:38:59835}
836
[email protected]82918cc2010-08-25 17:24:50837int HttpNetworkTransaction::DoInitStream() {
838 DCHECK(stream_.get());
839 next_state_ = STATE_INIT_STREAM_COMPLETE;
ttuttled9dbc652015-09-29 20:00:59840
841 stream_->GetRemoteEndpoint(&remote_endpoint_);
842
[email protected]262eec82013-03-19 21:01:36843 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50844}
845
846int HttpNetworkTransaction::DoInitStreamComplete(int result) {
847 if (result == OK) {
848 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50849 } else {
[email protected]82918cc2010-08-25 17:24:50850 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26851 result = HandleIOError(result);
852
853 // The stream initialization failed, so this stream will never be useful.
sclittlefb249892015-09-10 21:33:22854 if (stream_) {
855 total_received_bytes_ += stream_->GetTotalReceivedBytes();
856 total_sent_bytes_ += stream_->GetTotalSentBytes();
857 }
zhongyica364fbb2015-12-12 03:39:12858 CacheNetErrorDetailsAndResetStream();
[email protected]82918cc2010-08-25 17:24:50859 }
860
861 return result;
862}
863
[email protected]044de0642010-06-17 10:42:15864int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
865 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
866 if (!ShouldApplyProxyAuth())
867 return OK;
[email protected]394816e92010-08-03 07:38:59868 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
869 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09870 auth_controllers_[target] =
871 new HttpAuthController(target,
872 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31873 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09874 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59875 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41876 io_callback_,
[email protected]394816e92010-08-03 07:38:59877 net_log_);
[email protected]044de0642010-06-17 10:42:15878}
879
880int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
881 DCHECK_NE(ERR_IO_PENDING, rv);
882 if (rv == OK)
883 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
884 return rv;
885}
886
887int HttpNetworkTransaction::DoGenerateServerAuthToken() {
888 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59889 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54890 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09891 auth_controllers_[target] =
892 new HttpAuthController(target,
893 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31894 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09895 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54896 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
897 auth_controllers_[target]->DisableEmbeddedIdentity();
898 }
[email protected]044de0642010-06-17 10:42:15899 if (!ShouldApplyServerAuth())
900 return OK;
[email protected]394816e92010-08-03 07:38:59901 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41902 io_callback_,
[email protected]394816e92010-08-03 07:38:59903 net_log_);
[email protected]044de0642010-06-17 10:42:15904}
905
906int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
907 DCHECK_NE(ERR_IO_PENDING, rv);
908 if (rv == OK)
nharperd6e65822016-03-30 23:05:48909 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY;
[email protected]044de0642010-06-17 10:42:15910 return rv;
911}
912
nharperd6e65822016-03-30 23:05:48913int HttpNetworkTransaction::DoGetProvidedTokenBindingKey() {
914 next_state_ = STATE_GET_PROVIDED_TOKEN_BINDING_KEY_COMPLETE;
nharperb7441ef2016-01-25 23:54:14915 if (!IsTokenBindingEnabled())
916 return OK;
917
mikecirone8b85c432016-09-08 19:11:00918 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperb7441ef2016-01-25 23:54:14919 ChannelIDService* channel_id_service = session_->params().channel_id_service;
920 return channel_id_service->GetOrCreateChannelID(
nharperd6e65822016-03-30 23:05:48921 request_->url.host(), &provided_token_binding_key_, io_callback_,
nharperb7441ef2016-01-25 23:54:14922 &token_binding_request_);
923}
924
nharperd6e65822016-03-30 23:05:48925int HttpNetworkTransaction::DoGetProvidedTokenBindingKeyComplete(int rv) {
nharperb7441ef2016-01-25 23:54:14926 DCHECK_NE(ERR_IO_PENDING, rv);
nharperd6e65822016-03-30 23:05:48927 if (IsTokenBindingEnabled()) {
928 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00929 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48930 }
931
932 if (rv == OK)
933 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY;
934 return rv;
935}
936
937int HttpNetworkTransaction::DoGetReferredTokenBindingKey() {
938 next_state_ = STATE_GET_REFERRED_TOKEN_BINDING_KEY_COMPLETE;
939 if (!IsTokenBindingEnabled() || request_->token_binding_referrer.empty())
nharperb7441ef2016-01-25 23:54:14940 return OK;
941
mikecirone8b85c432016-09-08 19:11:00942 net_log_.BeginEvent(NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY);
nharperd6e65822016-03-30 23:05:48943 ChannelIDService* channel_id_service = session_->params().channel_id_service;
944 return channel_id_service->GetOrCreateChannelID(
945 request_->token_binding_referrer, &referred_token_binding_key_,
946 io_callback_, &token_binding_request_);
947}
948
949int HttpNetworkTransaction::DoGetReferredTokenBindingKeyComplete(int rv) {
950 DCHECK_NE(ERR_IO_PENDING, rv);
951 if (IsTokenBindingEnabled() && !request_->token_binding_referrer.empty()) {
952 net_log_.EndEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:00953 NetLogEventType::HTTP_TRANSACTION_GET_TOKEN_BINDING_KEY, rv);
nharperd6e65822016-03-30 23:05:48954 }
955 if (rv == OK)
956 next_state_ = STATE_INIT_REQUEST_BODY;
nharperb7441ef2016-01-25 23:54:14957 return rv;
958}
959
960int HttpNetworkTransaction::BuildRequestHeaders(
Adam Rice425cf122015-01-19 06:18:24961 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14962 request_headers_.SetHeader(HttpRequestHeaders::kHost,
963 GetHostAndOptionalPort(request_->url));
964
965 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24966 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14967 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
968 "keep-alive");
969 } else {
970 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
971 }
972
[email protected]2979a492011-04-06 00:29:14973 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11974 if (request_->upload_data_stream) {
975 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14976 request_headers_.SetHeader(
977 HttpRequestHeaders::kTransferEncoding, "chunked");
978 } else {
979 request_headers_.SetHeader(
980 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11981 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14982 }
csharrisonf473dd192015-08-18 13:54:13983 } else if (request_->method == "POST" || request_->method == "PUT") {
[email protected]2979a492011-04-06 00:29:14984 // An empty POST/PUT request still needs a content length. As for HEAD,
985 // IE and Safari also add a content length header. Presumably it is to
986 // support sending a HEAD request to an URL that only expects to be sent a
987 // POST or some other method that normally would have a message body.
csharrisonf473dd192015-08-18 13:54:13988 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
989 // specify that it should not be sent due to undefined behavior.
[email protected]2979a492011-04-06 00:29:14990 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
991 }
992
nharperb7441ef2016-01-25 23:54:14993 RecordTokenBindingSupport();
nharperd6e65822016-03-30 23:05:48994 if (provided_token_binding_key_) {
nharperb7441ef2016-01-25 23:54:14995 std::string token_binding_header;
996 int rv = BuildTokenBindingHeader(&token_binding_header);
997 if (rv != OK)
998 return rv;
999 request_headers_.SetHeader(HttpRequestHeaders::kTokenBinding,
1000 token_binding_header);
1001 }
1002
[email protected]2979a492011-04-06 00:29:141003 // Honor load flags that impact proxy caches.
1004 if (request_->load_flags & LOAD_BYPASS_CACHE) {
1005 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
1006 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
1007 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
1008 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
1009 }
1010
1011 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
1012 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
1013 &request_headers_);
1014 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
1015 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
1016 &request_headers_);
1017
[email protected]c10450102011-06-27 09:06:161018 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:201019
ryansturm49a8cb12016-06-15 16:51:091020 if (!before_headers_sent_callback_.is_null())
1021 before_headers_sent_callback_.Run(proxy_info_, &request_headers_);
[email protected]1252d42f2014-07-01 21:20:201022
[email protected]173f8e22013-04-10 04:18:201023 response_.did_use_http_auth =
1024 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
1025 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
nharperb7441ef2016-01-25 23:54:141026 return OK;
1027}
1028
1029int HttpNetworkTransaction::BuildTokenBindingHeader(std::string* out) {
nharpercb66ad0202016-03-11 18:17:471030 base::TimeTicks start = base::TimeTicks::Now();
nharperb7441ef2016-01-25 23:54:141031 std::vector<uint8_t> signed_ekm;
nharperd6e65822016-03-30 23:05:481032 int rv = stream_->GetSignedEKMForTokenBinding(
1033 provided_token_binding_key_.get(), &signed_ekm);
nharperb7441ef2016-01-25 23:54:141034 if (rv != OK)
1035 return rv;
1036 std::string provided_token_binding;
nharperd6e65822016-03-30 23:05:481037 rv = BuildTokenBinding(TokenBindingType::PROVIDED,
1038 provided_token_binding_key_.get(), signed_ekm,
1039 &provided_token_binding);
nharperb7441ef2016-01-25 23:54:141040 if (rv != OK)
1041 return rv;
nharperd6e65822016-03-30 23:05:481042
nharperb7441ef2016-01-25 23:54:141043 std::vector<base::StringPiece> token_bindings;
1044 token_bindings.push_back(provided_token_binding);
nharperd6e65822016-03-30 23:05:481045
1046 std::string referred_token_binding;
1047 if (referred_token_binding_key_) {
1048 std::vector<uint8_t> referred_signed_ekm;
1049 int rv = stream_->GetSignedEKMForTokenBinding(
1050 referred_token_binding_key_.get(), &referred_signed_ekm);
1051 if (rv != OK)
1052 return rv;
1053 rv = BuildTokenBinding(TokenBindingType::REFERRED,
1054 referred_token_binding_key_.get(),
1055 referred_signed_ekm, &referred_token_binding);
1056 if (rv != OK)
1057 return rv;
1058 token_bindings.push_back(referred_token_binding);
1059 }
nharperb7441ef2016-01-25 23:54:141060 std::string header;
1061 rv = BuildTokenBindingMessageFromTokenBindings(token_bindings, &header);
1062 if (rv != OK)
1063 return rv;
1064 base::Base64UrlEncode(header, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
1065 out);
nharpercb66ad0202016-03-11 18:17:471066 base::TimeDelta header_creation_time = base::TimeTicks::Now() - start;
1067 UMA_HISTOGRAM_CUSTOM_TIMES("Net.TokenBinding.HeaderCreationTime",
1068 header_creation_time,
1069 base::TimeDelta::FromMilliseconds(1),
1070 base::TimeDelta::FromMinutes(1), 50);
nharperb7441ef2016-01-25 23:54:141071 return OK;
[email protected]2979a492011-04-06 00:29:141072}
1073
[email protected]daddea62012-09-19 05:51:131074int HttpNetworkTransaction::DoInitRequestBody() {
1075 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:131076 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:111077 if (request_->upload_data_stream)
maksim.sisov819ba852016-08-17 08:22:521078 rv = request_->upload_data_stream->Init(io_callback_, net_log_);
[email protected]daddea62012-09-19 05:51:131079 return rv;
1080}
[email protected]4875ba12011-03-30 22:31:511081
[email protected]daddea62012-09-19 05:51:131082int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
1083 if (result == OK)
1084 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:131085 return result;
1086}
1087
1088int HttpNetworkTransaction::DoBuildRequest() {
1089 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:511090 headers_valid_ = false;
1091
1092 // This is constructed lazily (instead of within our Start method), so that
1093 // we have proxy info available.
1094 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:241095 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
nharperb7441ef2016-01-25 23:54:141096 return BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:511097 }
1098
[email protected]4875ba12011-03-30 22:31:511099 return OK;
1100}
1101
1102int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:241103 if (result == OK)
1104 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511105 return result;
1106}
1107
[email protected]0877e3d2009-10-17 22:29:571108int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:341109 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:261110 tracked_objects::ScopedTracker tracking_profile(
1111 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1112 "424359 HttpNetworkTransaction::DoSendRequest"));
1113
[email protected]58e32bb2013-01-21 18:23:251114 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571115 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1116
[email protected]bf3eb002012-11-15 05:50:111117 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521118}
1119
[email protected]0877e3d2009-10-17 22:29:571120int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:251121 send_end_time_ = base::TimeTicks::Now();
bncaa171332016-07-20 14:47:361122
1123 if (result == ERR_HTTP_1_1_REQUIRED ||
1124 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1125 return HandleHttp11Required(result);
1126 }
1127
initial.commit586acc5fe2008-07-26 22:42:521128 if (result < 0)
1129 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:571130 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521131 return OK;
1132}
1133
1134int HttpNetworkTransaction::DoReadHeaders() {
1135 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411136 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521137}
1138
1139int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:111140 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1141 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381142 if (IsCertificateError(result)) {
1143 // We don't handle a certificate error during SSL renegotiation, so we
1144 // have to return an error that's not in the certificate error range
1145 // (-2xx).
1146 LOG(ERROR) << "Got a server certificate with error " << result
1147 << " during SSL renegotiation";
1148 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1149 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1150 // TODO(wtc): Need a test case for this code path!
1151 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251152 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381153 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501154 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381155 result = HandleCertificateRequest(result);
1156 if (result == OK)
1157 return result;
[email protected]2181ea002009-06-09 01:37:271158 }
1159
bncfacdd852015-01-09 19:22:541160 if (result == ERR_HTTP_1_1_REQUIRED ||
1161 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1162 return HandleHttp11Required(result);
1163 }
1164
[email protected]c871864d72014-03-13 19:56:191165 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1166 // response headers were received, we do the best we can to make sense of it
1167 // and send it back up the stack.
1168 //
1169 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1170 // bizarre for SPDY. Assuming this logic is useful at all.
1171 // TODO(davidben): Bubble the error code up so we do not cache?
1172 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1173 result = OK;
1174
1175 if (result < 0)
1176 return HandleIOError(result);
1177
[email protected]90499482013-06-01 00:39:501178 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521179
[email protected]d58ceea82014-06-04 10:55:541180 // On a 408 response from the server ("Request Timeout") on a stale socket,
1181 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041182 // Headers can be NULL because of http://crbug.com/384554.
1183 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541184 stream_->IsConnectionReused()) {
1185 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001186 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR,
[email protected]d58ceea82014-06-04 10:55:541187 response_.headers->response_code());
1188 // This will close the socket - it would be weird to try and reuse it, even
1189 // if the server doesn't actually close it.
1190 ResetConnectionAndRequestForResend();
1191 return OK;
1192 }
1193
[email protected]93f8b562012-03-27 01:00:161194 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
rdsmith4de1fcf2016-08-30 05:35:211195 if (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED) {
[email protected]93f8b562012-03-27 01:00:161196 const int response_code = response_.headers->response_code();
1197 UMA_HISTOGRAM_ENUMERATION(
1198 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1199 }
1200
[email protected]3abacd62012-06-10 20:20:321201 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001202 NetLogEventType::HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
[email protected]3abacd62012-06-10 20:20:321203 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391204
bncbe0f6af2015-10-15 17:49:561205 if (response_.headers->GetHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571206 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1207 // indicates a buggy server. See:
1208 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1209 if (request_->method == "PUT")
1210 return ERR_METHOD_NOT_SUPPORTED;
1211 }
[email protected]4ddaf2502008-10-23 18:26:191212
[email protected]0877e3d2009-10-17 22:29:571213 // Check for an intermediate 100 Continue response. An origin server is
1214 // allowed to send this response even if we didn't ask for it, so we just
1215 // need to skip over it.
1216 // We treat any other 1xx in this same way (although in practice getting
1217 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441218 // Unless this is a WebSocket request, in which case we pass it on up.
1219 if (response_.headers->response_code() / 100 == 1 &&
1220 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451221 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571222 next_state_ = STATE_READ_HEADERS;
1223 return OK;
1224 }
1225
bncb26024382016-06-29 02:39:451226 if (IsSecureRequest()) {
bnce3dd56f2016-06-01 10:37:111227 session_->http_stream_factory()->ProcessAlternativeServices(
1228 session_, response_.headers.get(), url::SchemeHostPort(request_->url));
1229 }
[email protected]564b4912010-03-09 16:30:421230
asanka5ffd5d72016-03-23 16:20:491231 if (IsSecureRequest())
1232 stream_->GetSSLInfo(&response_.ssl_info);
1233
[email protected]e772db3f2010-07-12 18:11:131234 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571235 if (rv != OK)
1236 return rv;
1237
[email protected]0877e3d2009-10-17 22:29:571238 headers_valid_ = true;
1239 return OK;
initial.commit586acc5fe2008-07-26 22:42:521240}
1241
1242int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501243 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131244 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381245 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521246
1247 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501248 return stream_->ReadResponseBody(
1249 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521250}
1251
1252int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1253 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381254 bool done = false;
1255 if (result <= 0) {
1256 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521257 done = true;
[email protected]8e6441ca2010-08-19 05:56:381258 }
[email protected]9492e4a2010-02-24 00:58:461259
mmenkebd84c392015-09-02 14:12:341260 // Clean up connection if we are done.
1261 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381262 // Note: Just because IsResponseBodyComplete is true, we're not
1263 // necessarily "done". We're only "done" when it is the last
1264 // read on this HttpNetworkTransaction, which will be signified
1265 // by a zero-length read.
mmenkebd84c392015-09-02 14:12:341266 // TODO(mbelshe): The keep-alive property is really a property of
[email protected]8e6441ca2010-08-19 05:56:381267 // the stream. No need to compute it here just to pass back
1268 // to the stream's Close function.
mmenkebd84c392015-09-02 14:12:341269 bool keep_alive =
1270 stream_->IsResponseBodyComplete() && stream_->CanReuseConnection();
initial.commit586acc5fe2008-07-26 22:42:521271
[email protected]8e6441ca2010-08-19 05:56:381272 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011273 // Note: we don't reset the stream here. We've closed it, but we still
1274 // need it around so that callers can call methods such as
1275 // GetUploadProgress() and have them be meaningful.
1276 // TODO(mbelshe): This means we closed the stream here, and we close it
1277 // again in ~HttpNetworkTransaction. Clean that up.
1278
[email protected]8e6441ca2010-08-19 05:56:381279 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521280 }
1281
1282 // Clear these to avoid leaving around old state.
1283 read_buf_ = NULL;
1284 read_buf_len_ = 0;
1285
1286 return result;
1287}
1288
[email protected]2d2697f92009-02-18 21:00:321289int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1290 // This method differs from DoReadBody only in the next_state_. So we just
1291 // call DoReadBody and override the next_state_. Perhaps there is a more
1292 // elegant way for these two methods to share code.
1293 int rv = DoReadBody();
1294 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1295 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1296 return rv;
1297}
1298
[email protected]0877e3d2009-10-17 22:29:571299// TODO(wtc): This method and the DoReadBodyComplete method are almost
1300// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321301int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231302 // keep_alive defaults to true because the very reason we're draining the
1303 // response body is to reuse the connection for auth restart.
1304 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321305 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571306 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321307 done = true;
[email protected]68873ba2009-06-04 21:49:231308 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311309 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571310 done = true;
[email protected]2d2697f92009-02-18 21:00:321311 }
1312
1313 if (done) {
1314 DidDrainBodyForAuthRestart(keep_alive);
1315 } else {
1316 // Keep draining.
1317 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1318 }
1319
1320 return OK;
1321}
1322
[email protected]5e363962009-06-19 19:57:011323int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381324 // There are two paths through which the server can request a certificate
1325 // from us. The first is during the initial handshake, the second is
1326 // during SSL renegotiation.
1327 //
1328 // In both cases, we want to close the connection before proceeding.
1329 // We do this for two reasons:
1330 // First, we don't want to keep the connection to the server hung for a
1331 // long time while the user selects a certificate.
1332 // Second, even if we did keep the connection open, NSS has a bug where
1333 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581334 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381335
1336 if (stream_.get()) {
1337 // Since we already have a stream, we're being called as part of SSL
1338 // renegotiation.
1339 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191340 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221341 total_sent_bytes_ += stream_->GetTotalSentBytes();
[email protected]8e6441ca2010-08-19 05:56:381342 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121343 CacheNetErrorDetailsAndResetStream();
[email protected]8e6441ca2010-08-19 05:56:381344 }
1345
[email protected]26816882010-10-14 18:03:091346 // The server is asking for a client certificate during the initial
1347 // handshake.
1348 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011349
[email protected]ec229bc92010-11-22 09:51:451350 // If the user selected one of the certificates in client_certs or declined
1351 // to provide one for this server before, use the past decision
1352 // automatically.
1353 scoped_refptr<X509Certificate> client_cert;
svaldez7872fd02015-11-19 21:10:541354 scoped_refptr<SSLPrivateKey> client_private_key;
[email protected]ec229bc92010-11-22 09:51:451355 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
svaldez7872fd02015-11-19 21:10:541356 response_.cert_request_info->host_and_port, &client_cert,
1357 &client_private_key);
[email protected]ec229bc92010-11-22 09:51:451358 if (!found_cached_cert)
1359 return error;
1360
1361 // Check that the certificate selected is still a certificate the server
1362 // is likely to accept, based on the criteria supplied in the
1363 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501364 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261365 const std::vector<std::string>& cert_authorities =
1366 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451367
[email protected]f1958c382013-02-07 00:15:261368 bool cert_still_valid = cert_authorities.empty() ||
1369 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451370 if (!cert_still_valid)
1371 return error;
[email protected]5e363962009-06-19 19:57:011372 }
[email protected]ec229bc92010-11-22 09:51:451373
1374 // TODO(davidben): Add a unit test which covers this path; we need to be
1375 // able to send a legitimate certificate and also bypass/clear the
1376 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141377 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1378 &proxy_ssl_config_ : &server_ssl_config_;
1379 ssl_config->send_client_cert = true;
1380 ssl_config->client_cert = client_cert;
svaldez7872fd02015-11-19 21:10:541381 ssl_config->client_private_key = client_private_key;
[email protected]ec229bc92010-11-22 09:51:451382 next_state_ = STATE_CREATE_STREAM;
1383 // Reset the other member variables.
1384 // Note: this is necessary only with SSL renegotiation.
1385 ResetStateForRestart();
1386 return OK;
[email protected]0b45559b2009-06-12 21:45:111387}
1388
bncfacdd852015-01-09 19:22:541389int HttpNetworkTransaction::HandleHttp11Required(int error) {
1390 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1391 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1392
1393 if (error == ERR_HTTP_1_1_REQUIRED) {
1394 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1395 } else {
1396 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1397 }
1398 ResetConnectionAndRequestForResend();
1399 return OK;
1400}
1401
[email protected]384cc73a2013-12-08 22:41:031402void HttpNetworkTransaction::HandleClientAuthError(int error) {
1403 if (server_ssl_config_.send_client_cert &&
1404 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1405 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411406 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031407 }
1408}
1409
[email protected]bd0b6772011-01-11 19:59:301410// TODO(rch): This does not correctly handle errors when an SSL proxy is
1411// being used, as all of the errors are handled as if they were generated
1412// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381413// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301414int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1415 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031416 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301417
davidbena4c9d062015-04-03 22:34:251418 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1419 // reflect servers require a deprecated cipher rather than merely prefer
1420 // it. This, however, has no security benefit until the ciphers are actually
1421 // removed.
davidben14b1a532015-10-30 16:01:091422 if (!server_ssl_config_.deprecated_cipher_suites_enabled &&
davidbena4c9d062015-04-03 22:34:251423 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1424 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1425 net_log_.AddEvent(
mikecirone8b85c432016-09-08 19:11:001426 NetLogEventType::SSL_CIPHER_FALLBACK,
davidbena4c9d062015-04-03 22:34:251427 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
davidben14b1a532015-10-30 16:01:091428 server_ssl_config_.deprecated_cipher_suites_enabled = true;
davidbena4c9d062015-04-03 22:34:251429 ResetConnectionAndRequestForResend();
1430 return OK;
1431 }
1432
initial.commit586acc5fe2008-07-26 22:42:521433 return error;
1434}
1435
[email protected]bd0b6772011-01-11 19:59:301436// This method determines whether it is safe to resend the request after an
1437// IO error. It can only be called in response to request header or body
1438// write errors or response header read errors. It should not be used in
1439// other cases, such as a Connect error.
1440int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031441 // Because the peer may request renegotiation with client authentication at
1442 // any time, check and handle client authentication errors.
1443 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301444
1445 switch (error) {
1446 // If we try to reuse a connection that the server is in the process of
1447 // closing, we may end up successfully writing out our request (or a
1448 // portion of our request) only to find a connection error when we try to
1449 // read from (or finish writing to) the socket.
1450 case ERR_CONNECTION_RESET:
1451 case ERR_CONNECTION_CLOSED:
1452 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511453 // There can be a race between the socket pool checking checking whether a
1454 // socket is still connected, receiving the FIN, and sending/reading data
1455 // on a reused socket. If we receive the FIN between the connectedness
1456 // check and writing/reading from the socket, we may first learn the socket
1457 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1458 // likely happen when trying to retrieve its IP address.
1459 // See http://crbug.com/105824 for more details.
1460 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491461 // If a socket is closed on its initial request, HttpStreamParser returns
1462 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1463 // preconnected but failed to be used before the server timed it out.
1464 case ERR_EMPTY_RESPONSE:
1465 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381466 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001467 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301468 ResetConnectionAndRequestForResend();
1469 error = OK;
1470 }
1471 break;
[email protected]8753a122011-10-16 08:05:081472 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001473 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271474 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381475 net_log_.AddEventWithNetErrorCode(
mikecirone8b85c432016-09-08 19:11:001476 NetLogEventType::HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001477 ResetConnectionAndRequestForResend();
1478 error = OK;
1479 break;
[email protected]bd0b6772011-01-11 19:59:301480 }
1481 return error;
1482}
1483
[email protected]c3b35c22008-09-27 03:19:421484void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581485 ResetStateForAuthRestart();
sclittlefb249892015-09-10 21:33:221486 if (stream_) {
[email protected]b8015c42013-12-24 15:18:191487 total_received_bytes_ += stream_->GetTotalReceivedBytes();
sclittlefb249892015-09-10 21:33:221488 total_sent_bytes_ += stream_->GetTotalSentBytes();
1489 }
zhongyica364fbb2015-12-12 03:39:121490 CacheNetErrorDetailsAndResetStream();
[email protected]697ef4c2010-10-14 16:38:581491}
1492
1493void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251494 send_start_time_ = base::TimeTicks();
1495 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251496
[email protected]0757e7702009-03-27 04:00:221497 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421498 read_buf_ = NULL;
1499 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571500 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201501 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141502 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381503 establishing_tunnel_ = false;
ttuttled9dbc652015-09-29 20:00:591504 remote_endpoint_ = IPEndPoint();
zhongyica364fbb2015-12-12 03:39:121505 net_error_details_.quic_broken = false;
1506 net_error_details_.quic_connection_error = QUIC_NO_ERROR;
nharperd6e65822016-03-30 23:05:481507 provided_token_binding_key_.reset();
1508 referred_token_binding_key_.reset();
zhongyica364fbb2015-12-12 03:39:121509}
1510
1511void HttpNetworkTransaction::CacheNetErrorDetailsAndResetStream() {
1512 if (stream_)
1513 stream_->PopulateNetErrorDetails(&net_error_details_);
1514 stream_.reset();
[email protected]0877e3d2009-10-17 22:29:571515}
1516
davidben701ca982015-05-18 21:21:421517void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
davidben701ca982015-05-18 21:21:421518 if (result != OK)
1519 return;
1520
davidbenca9d69162015-05-04 20:18:451521 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
davidben14b1a532015-10-30 16:01:091522 server_ssl_config_.deprecated_cipher_suites_enabled);
davidbenca9d69162015-05-04 20:18:451523}
1524
[email protected]0877e3d2009-10-17 22:29:571525HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501526 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421527}
1528
[email protected]a34f61ee2014-03-18 20:59:491529bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381530 bool connection_is_proven = stream_->IsConnectionReused();
1531 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161532
[email protected]2a5c76b2008-09-25 22:15:161533 // NOTE: we resend a request only if we reused a keep-alive connection.
1534 // This automatically prevents an infinite resend loop because we'll run
1535 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381536 if (connection_is_proven && !has_received_headers)
1537 return true;
1538 return false;
[email protected]1c773ea12009-04-28 19:58:421539}
1540
1541void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381542 if (stream_.get()) {
1543 stream_->Close(true);
zhongyica364fbb2015-12-12 03:39:121544 CacheNetErrorDetailsAndResetStream();
[email protected]58cebf8f2010-07-31 19:20:161545 }
1546
[email protected]0877e3d2009-10-17 22:29:571547 // We need to clear request_headers_ because it contains the real request
1548 // headers, but we may need to resend the CONNECT request first to recreate
1549 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201550 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501551 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541552}
1553
[email protected]1c773ea12009-04-28 19:58:421554bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241555 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421556}
license.botbf09a502008-08-24 00:55:551557
[email protected]1c773ea12009-04-28 19:58:421558bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041559 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421560}
1561
[email protected]e772db3f2010-07-12 18:11:131562int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271563 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501564 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421565
[email protected]0877e3d2009-10-17 22:29:571566 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581567 if (status != HTTP_UNAUTHORIZED &&
1568 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421569 return OK;
[email protected]9094b602012-02-27 21:44:581570 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111571 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161572 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1573 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421574
[email protected]9094b602012-02-27 21:44:581575 // This case can trigger when an HTTPS server responds with a "Proxy
1576 // authentication required" status code through a non-authenticating
1577 // proxy.
[email protected]7a67a8152010-11-05 18:31:101578 if (!auth_controllers_[target].get())
1579 return ERR_UNEXPECTED_PROXY_AUTH;
1580
[email protected]a7ea8832010-07-12 17:54:541581 int rv = auth_controllers_[target]->HandleAuthChallenge(
asanka5ffd5d72016-03-23 16:20:491582 headers, response_.ssl_info,
1583 (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
[email protected]560c0432010-07-13 20:45:311584 net_log_);
[email protected]228404f2010-06-24 04:31:411585 if (auth_controllers_[target]->HaveAuthHandler())
asanka5ffd5d72016-03-23 16:20:491586 pending_auth_target_ = target;
[email protected]228404f2010-06-24 04:31:411587
1588 scoped_refptr<AuthChallengeInfo> auth_info =
1589 auth_controllers_[target]->auth_info();
1590 if (auth_info.get())
1591 response_.auth_challenge = auth_info;
1592
[email protected]228404f2010-06-24 04:31:411593 return rv;
[email protected]f9ee6b52008-11-08 06:46:231594}
1595
[email protected]8e6441ca2010-08-19 05:56:381596bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1597 return auth_controllers_[target].get() &&
1598 auth_controllers_[target]->HaveAuth();
1599}
1600
[email protected]228404f2010-06-24 04:31:411601GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1602 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461603 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411604 if (!proxy_info_.proxy_server().is_valid() ||
1605 proxy_info_.proxy_server().is_direct()) {
1606 return GURL(); // There is no proxy server.
1607 }
[email protected]2df19bb2010-08-25 20:13:461608 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1609 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351610 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461611 }
[email protected]228404f2010-06-24 04:31:411612 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291613 if (ForWebSocketHandshake()) {
1614 const GURL& url = request_->url;
1615 url::Replacements<char> ws_to_http;
1616 if (url.SchemeIs("ws")) {
1617 ws_to_http.SetScheme("http", url::Component(0, 4));
1618 } else {
1619 DCHECK(url.SchemeIs("wss"));
1620 ws_to_http.SetScheme("https", url::Component(0, 5));
1621 }
1622 return url.ReplaceComponents(ws_to_http);
1623 }
[email protected]228404f2010-06-24 04:31:411624 return request_->url;
1625 default:
1626 return GURL();
1627 }
[email protected]c3b35c22008-09-27 03:19:421628}
1629
[email protected]831e4a32013-11-14 02:14:441630bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141631 return websocket_handshake_stream_base_create_helper_ &&
1632 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441633}
1634
[email protected]d8eb84242010-09-25 02:25:061635#define STATE_CASE(s) \
1636 case s: \
1637 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1638 break
[email protected]aef04272010-06-28 18:03:041639
1640std::string HttpNetworkTransaction::DescribeState(State state) {
1641 std::string description;
1642 switch (state) {
[email protected]1826a402014-01-08 15:40:481643 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501644 STATE_CASE(STATE_CREATE_STREAM);
1645 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131646 STATE_CASE(STATE_INIT_REQUEST_BODY);
1647 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511648 STATE_CASE(STATE_BUILD_REQUEST);
1649 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041650 STATE_CASE(STATE_SEND_REQUEST);
1651 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1652 STATE_CASE(STATE_READ_HEADERS);
1653 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041654 STATE_CASE(STATE_READ_BODY);
1655 STATE_CASE(STATE_READ_BODY_COMPLETE);
1656 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1657 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041658 STATE_CASE(STATE_NONE);
1659 default:
[email protected]d8eb84242010-09-25 02:25:061660 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1661 state);
[email protected]aef04272010-06-28 18:03:041662 break;
1663 }
1664 return description;
1665}
1666
1667#undef STATE_CASE
1668
ttuttle1f2d7e92015-04-28 16:17:471669void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1670 DCHECK(stream_request_);
1671
1672 // Since the transaction can restart with auth credentials, it may create a
1673 // stream more than once. Accumulate all of the connection attempts across
1674 // those streams by appending them to the vector:
1675 for (const auto& attempt : stream_request_->connection_attempts())
1676 connection_attempts_.push_back(attempt);
1677}
1678
[email protected]c3b35c22008-09-27 03:19:421679} // namespace net