blob: ac7dac576ec88f71e51808a59abcf41aed970d84 [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
8#include <vector>
9
[email protected]49639fa2011-12-20 23:22:4110#include "base/bind.h"
11#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3012#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4713#include "base/format_macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/scoped_ptr.h"
[email protected]835d7c82010-10-14 04:38:3815#include "base/metrics/field_trial.h"
asvitkinec3c93722015-06-17 14:48:3716#include "base/metrics/histogram_macros.h"
davidbenf2eaaf92015-05-15 22:18:4217#include "base/metrics/sparse_histogram.h"
vadimt09e7ebe2014-10-29 22:10:4118#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4720#include "base/strings/string_number_conversions.h"
21#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4232#include "net/base/net_util.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
[email protected]c3b35c22008-09-27 03:19:4234#include "net/http/http_auth.h"
35#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2436#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3637#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/http/http_chunked_decoder.h"
39#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5440#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1341#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4742#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2144#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5745#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5346#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5847#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3948#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3149#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2351#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3652#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4453#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2654#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4455#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1856#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2857#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4058#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3259#include "net/spdy/spdy_session.h"
60#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5761#include "net/ssl/ssl_cert_request_info.h"
62#include "net/ssl/ssl_connection_status_flags.h"
[email protected]f89276a72013-07-12 06:41:5463#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2964#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5265
initial.commit586acc5fe2008-07-26 22:42:5266namespace net {
67
[email protected]1c773ea12009-04-28 19:58:4268namespace {
69
bncc958faa2015-07-31 18:14:5270void ProcessAlternativeServices(HttpNetworkSession* session,
71 const HttpResponseHeaders& headers,
72 const HostPortPair& http_host_port_pair) {
73 if (headers.HasHeader(kAlternativeServiceHeader)) {
74 std::string alternative_service_str;
75 headers.GetNormalizedHeader(kAlternativeServiceHeader,
76 &alternative_service_str);
77 session->http_stream_factory()->ProcessAlternativeService(
78 session->http_server_properties(), alternative_service_str,
79 http_host_port_pair, *session);
80 // If there is an "Alt-Svc" header, then ignore "Alternate-Protocol".
81 return;
82 }
83
[email protected]287d9412014-07-08 23:01:0084 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3885 return;
[email protected]287d9412014-07-08 23:01:0086
87 std::vector<std::string> alternate_protocol_values;
88 void* iter = NULL;
89 std::string alternate_protocol_str;
90 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
91 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5292 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
93 &alternate_protocol_str);
94 if (!alternate_protocol_str.empty()) {
95 alternate_protocol_values.push_back(alternate_protocol_str);
96 }
[email protected]564b4912010-03-09 16:30:4297 }
98
[email protected]d7599122014-05-24 03:37:2399 session->http_stream_factory()->ProcessAlternateProtocol(
100 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:00101 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:23102 http_host_port_pair,
103 *session);
[email protected]f45c1ee2010-08-03 00:54:30104}
105
estade5e5529d2015-05-21 20:59:11106scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
[email protected]ea5ef4c2013-06-13 22:50:27107 const GURL* url,
108 int net_error,
davidbenf2eaaf92015-05-15 22:18:42109 SSLFailureState ssl_failure_state,
[email protected]ea5ef4c2013-06-13 22:50:27110 uint16 version_before,
111 uint16 version_after,
eroman001c3742015-04-23 03:11:17112 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11113 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b6754252012-06-13 23:14:38114 dict->SetString("host_and_port", GetHostAndPort(*url));
115 dict->SetInteger("net_error", net_error);
davidbenf2eaaf92015-05-15 22:18:42116 dict->SetInteger("ssl_failure_state", ssl_failure_state);
[email protected]b6754252012-06-13 23:14:38117 dict->SetInteger("version_before", version_before);
118 dict->SetInteger("version_after", version_after);
estade5e5529d2015-05-21 20:59:11119 return dict.Pass();
[email protected]b6754252012-06-13 23:14:38120}
[email protected]db74b0102012-05-31 19:55:53121
estade5e5529d2015-05-21 20:59:11122scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:17123 const GURL* url,
124 int net_error,
125 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11126 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:25127 dict->SetString("host_and_port", GetHostAndPort(*url));
128 dict->SetInteger("net_error", net_error);
estade5e5529d2015-05-21 20:59:11129 return dict.Pass();
davidbena4c9d062015-04-03 22:34:25130}
131
[email protected]1c773ea12009-04-28 19:58:42132} // namespace
133
initial.commit586acc5fe2008-07-26 22:42:52134//-----------------------------------------------------------------------------
135
[email protected]262eec82013-03-19 21:01:36136HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
137 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22138 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32139 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
140 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52141 session_(session),
142 request_(NULL),
[email protected]262eec82013-03-19 21:01:36143 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57144 headers_valid_(false),
davidbenf2eaaf92015-05-15 22:18:42145 server_ssl_failure_state_(SSL_FAILURE_NONE),
[email protected]a53e4d12013-12-07 16:37:24146 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
davidbenf2eaaf92015-05-15 22:18:42147 fallback_failure_state_(SSL_FAILURE_NONE),
[email protected]b94f92d2010-10-27 16:45:20148 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52149 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19150 total_received_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54151 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44152 establishing_tunnel_(false),
153 websocket_handshake_stream_base_create_helper_(NULL) {
[email protected]102957f2011-09-02 17:10:14154 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]d7599122014-05-24 03:37:23155 session->GetNextProtos(&server_ssl_config_.next_protos);
[email protected]99ffa5a2011-10-06 04:20:19156 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50157}
158
[email protected]0b0bf032010-09-21 18:08:50159HttpNetworkTransaction::~HttpNetworkTransaction() {
160 if (stream_.get()) {
161 HttpResponseHeaders* headers = GetResponseHeaders();
162 // TODO(mbelshe): The stream_ should be able to compute whether or not the
163 // stream should be kept alive. No reason to compute here
164 // and pass it in.
165 bool try_to_keep_alive =
166 next_state_ == STATE_NONE &&
167 stream_->CanFindEndOfResponse() &&
168 (!headers || headers->IsKeepAlive());
169 if (!try_to_keep_alive) {
170 stream_->Close(true /* not reusable */);
171 } else {
172 if (stream_->IsResponseBodyComplete()) {
173 // If the response body is complete, we can just reuse the socket.
174 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44175 } else if (stream_->IsSpdyHttpStream()) {
176 // Doesn't really matter for SpdyHttpStream. Just close it.
177 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50178 } else {
179 // Otherwise, we try to drain the response body.
yhiranoa7e05bb2014-11-06 05:40:39180 HttpStream* stream = stream_.release();
[email protected]3fe8d2f82013-10-17 08:56:07181 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50182 }
183 }
184 }
[email protected]02cad5d2013-10-02 08:14:03185
186 if (request_ && request_->upload_data_stream)
187 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50188}
189
[email protected]684970b2009-08-14 04:54:46190int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41191 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53192 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53193 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04194 request_ = request_info;
195
[email protected]99ffa5a2011-10-06 04:20:19196 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14197 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19198 proxy_ssl_config_.rev_checking_enabled = false;
199 }
[email protected]6fbac162011-06-20 00:29:04200
jkarlinfb1d5172015-01-12 14:10:29201 if (request_->load_flags & LOAD_PREFETCH)
202 response_.unused_since_prefetch = true;
203
[email protected]1ea4f46a2014-04-22 22:33:56204 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49205 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
206 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40207
[email protected]1826a402014-01-08 15:40:48208 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04209 int rv = DoLoop(OK);
210 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41211 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04212 return rv;
213}
214
215int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41216 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38217 DCHECK(!stream_.get());
218 DCHECK(!stream_request_.get());
219 DCHECK_EQ(STATE_NONE, next_state_);
220
[email protected]82918cc2010-08-25 17:24:50221 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38222
[email protected]ccb40e52008-09-17 20:54:40223 int rv = DoLoop(OK);
224 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41225 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11226 return rv;
[email protected]96d570e42008-08-05 22:43:04227}
228
[email protected]0b45559b2009-06-12 21:45:11229int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41230 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38231 // In HandleCertificateRequest(), we always tear down existing stream
232 // requests to force a new connection. So we shouldn't have one here.
233 DCHECK(!stream_request_.get());
234 DCHECK(!stream_.get());
235 DCHECK_EQ(STATE_NONE, next_state_);
236
[email protected]102957f2011-09-02 17:10:14237 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
238 &proxy_ssl_config_ : &server_ssl_config_;
239 ssl_config->send_client_cert = true;
240 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45241 session_->ssl_client_auth_cache()->Add(
242 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11243 // Reset the other member variables.
244 // Note: this is necessary only with SSL renegotiation.
245 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50246 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11247 int rv = DoLoop(OK);
248 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41249 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11250 return rv;
251}
252
[email protected]49639fa2011-12-20 23:22:41253int HttpNetworkTransaction::RestartWithAuth(
254 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22255 HttpAuth::Target target = pending_auth_target_;
256 if (target == HttpAuth::AUTH_NONE) {
257 NOTREACHED();
258 return ERR_UNEXPECTED;
259 }
[email protected]0757e7702009-03-27 04:00:22260 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42261
[email protected]f3cf9802011-10-28 18:44:58262 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13263
[email protected]49639fa2011-12-20 23:22:41264 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38265
266 int rv = OK;
267 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
268 // In this case, we've gathered credentials for use with proxy
269 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50270 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38271 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59272 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54273 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58274 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54275 } else {
[email protected]8e6441ca2010-08-19 05:56:38276 // In this case, we've gathered credentials for the server or the proxy
277 // but it is not during the tunneling phase.
278 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54279 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38280 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54281 }
[email protected]c3b35c22008-09-27 03:19:42282
[email protected]c3b35c22008-09-27 03:19:42283 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41284 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42285 return rv;
[email protected]96d570e42008-08-05 22:43:04286}
287
[email protected]f9ee6b52008-11-08 06:46:23288void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
289 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38290 DCHECK(!stream_request_.get());
291
[email protected]2d2697f92009-02-18 21:00:32292 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57293 // Even if the server says the connection is keep-alive, we have to be
294 // able to find the end of each response in order to reuse the connection.
295 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31296 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57297 // If the response body hasn't been completely read, we need to drain
298 // it first.
[email protected]351ab642010-08-05 16:55:31299 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32300 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57301 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32302 read_buf_len_ = kDrainBodyBufferSize;
303 return;
304 }
[email protected]0877e3d2009-10-17 22:29:57305 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09306 }
307
[email protected]2d2697f92009-02-18 21:00:32308 // We don't need to drain the response body, so we act as if we had drained
309 // the response body.
310 DidDrainBodyForAuthRestart(keep_alive);
311}
312
313void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38314 DCHECK(!stream_request_.get());
315
316 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19317 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:58318 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46319 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38320 // We should call connection_->set_idle_time(), but this doesn't occur
321 // often enough to be worth the trouble.
322 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39323 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38324 }
[email protected]697ef4c2010-10-14 16:38:58325
326 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46327 // Close the stream and mark it as not_reusable. Even in the
328 // keep_alive case, we've determined that the stream_ is not
329 // reusable if new_stream is NULL.
330 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58331 next_state_ = STATE_CREATE_STREAM;
332 } else {
[email protected]b8015c42013-12-24 15:18:19333 // Renewed streams shouldn't carry over received bytes.
334 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
[email protected]697ef4c2010-10-14 16:38:58335 next_state_ = STATE_INIT_STREAM;
336 }
337 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32338 }
[email protected]f9ee6b52008-11-08 06:46:23339
340 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58341 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23342}
343
[email protected]8e6441ca2010-08-19 05:56:38344bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
345 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
346 HaveAuth(pending_auth_target_);
347}
348
[email protected]9dea9e1f2009-01-29 00:30:47349int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41350 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04351 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35352 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04353
[email protected]1f14a912009-12-21 20:32:44354 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04355
[email protected]ad8e04a2010-11-01 04:16:27356 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38357 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04358 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29359 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04360 // bytes when establishing a tunnel because they might be controlled by
361 // an active network attacker. We don't worry about this for HTTP
362 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29363 // We reach this case when the user cancels a 407 proxy auth prompt. We
364 // also don't worry about this for an HTTPS Proxy, because the
365 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04366 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46367 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58368 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54369 LOG(WARNING) << "Blocked proxy response with status "
370 << headers->response_code() << " to CONNECT request for "
371 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04372 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44373 }
374
[email protected]e60e47a2010-07-14 03:37:18375 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31376 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18377
[email protected]96d570e42008-08-05 22:43:04378 read_buf_ = buf;
379 read_buf_len_ = buf_len;
380
[email protected]1f14a912009-12-21 20:32:44381 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04382 int rv = DoLoop(OK);
383 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41384 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04385 return rv;
386}
387
[email protected]8cd06c02014-01-25 07:50:14388void HttpNetworkTransaction::StopCaching() {}
389
[email protected]79e1fd62013-06-20 06:50:04390bool HttpNetworkTransaction::GetFullRequestHeaders(
391 HttpRequestHeaders* headers) const {
392 // TODO(ttuttle): Make sure we've populated request_headers_.
393 *headers = request_headers_;
394 return true;
395}
396
[email protected]b8015c42013-12-24 15:18:19397int64 HttpNetworkTransaction::GetTotalReceivedBytes() const {
398 int64 total_received_bytes = total_received_bytes_;
399 if (stream_)
400 total_received_bytes += stream_->GetTotalReceivedBytes();
401 return total_received_bytes;
402}
403
[email protected]8cd06c02014-01-25 07:50:14404void HttpNetworkTransaction::DoneReading() {}
405
[email protected]96d570e42008-08-05 22:43:04406const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55407 return &response_;
[email protected]96d570e42008-08-05 22:43:04408}
409
410LoadState HttpNetworkTransaction::GetLoadState() const {
411 // TODO(wtc): Define a new LoadState value for the
412 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
413 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48414 case STATE_CREATE_STREAM:
415 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50416 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38417 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15418 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
419 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57420 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04421 return LOAD_STATE_SENDING_REQUEST;
422 case STATE_READ_HEADERS_COMPLETE:
423 return LOAD_STATE_WAITING_FOR_RESPONSE;
424 case STATE_READ_BODY_COMPLETE:
425 return LOAD_STATE_READING_RESPONSE;
426 default:
427 return LOAD_STATE_IDLE;
428 }
429}
430
[email protected]196d18a2012-08-30 03:47:31431UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31432 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31433 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04434
yhiranoa7e05bb2014-11-06 05:40:39435 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04436}
437
[email protected]8cd06c02014-01-25 07:50:14438void HttpNetworkTransaction::SetQuicServerInfo(
439 QuicServerInfo* quic_server_info) {}
440
[email protected]5033ab82013-03-22 20:17:46441bool HttpNetworkTransaction::GetLoadTimingInfo(
442 LoadTimingInfo* load_timing_info) const {
443 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
444 return false;
445
446 load_timing_info->proxy_resolve_start =
447 proxy_info_.proxy_resolve_start_time();
448 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
449 load_timing_info->send_start = send_start_time_;
450 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46451 return true;
452}
453
454void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
455 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47456 if (stream_request_)
457 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03458 if (stream_)
459 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46460}
461
[email protected]831e4a32013-11-14 02:14:44462void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
463 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
464 websocket_handshake_stream_base_create_helper_ = create_helper;
465}
466
[email protected]1826a402014-01-08 15:40:48467void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
468 const BeforeNetworkStartCallback& callback) {
469 before_network_start_callback_ = callback;
470}
471
[email protected]597a1ab2014-06-26 08:12:27472void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
473 const BeforeProxyHeadersSentCallback& callback) {
474 before_proxy_headers_sent_callback_ = callback;
475}
476
[email protected]1826a402014-01-08 15:40:48477int HttpNetworkTransaction::ResumeNetworkStart() {
478 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
479 return DoLoop(OK);
480}
481
[email protected]102e27c2011-02-23 01:01:31482void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
483 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39484 HttpStream* stream) {
[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(stream_request_.get());
487
[email protected]b8015c42013-12-24 15:18:19488 if (stream_)
489 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:38490 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14491 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31492 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38493 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51494 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
495 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38496 response_.was_fetched_via_spdy = stream_request_->using_spdy();
497 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15498 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
499 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38500 OnIOComplete(OK);
501}
502
[email protected]a9cf2b92013-10-30 12:08:49503void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50504 const SSLConfig& used_ssl_config,
505 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49506 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44507 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50508}
509
[email protected]102e27c2011-02-23 01:01:31510void HttpNetworkTransaction::OnStreamFailed(int result,
davidbenf2eaaf92015-05-15 22:18:42511 const SSLConfig& used_ssl_config,
512 SSLFailureState ssl_failure_state) {
[email protected]82918cc2010-08-25 17:24:50513 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38514 DCHECK_NE(OK, result);
515 DCHECK(stream_request_.get());
516 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14517 server_ssl_config_ = used_ssl_config;
davidbenf2eaaf92015-05-15 22:18:42518 server_ssl_failure_state_ = ssl_failure_state;
[email protected]8e6441ca2010-08-19 05:56:38519
520 OnIOComplete(result);
521}
522
[email protected]102e27c2011-02-23 01:01:31523void HttpNetworkTransaction::OnCertificateError(
524 int result,
525 const SSLConfig& used_ssl_config,
526 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50527 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38528 DCHECK_NE(OK, result);
529 DCHECK(stream_request_.get());
530 DCHECK(!stream_.get());
531
532 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14533 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38534
535 // TODO(mbelshe): For now, we're going to pass the error through, and that
536 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50537 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
538 // good and the user chooses to ignore the error. This is not ideal, but not
539 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38540
541 OnIOComplete(result);
542}
543
544void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50545 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31546 const SSLConfig& used_ssl_config,
547 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50548 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38549 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50550 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38551
552 establishing_tunnel_ = true;
553 response_.headers = proxy_response.headers;
554 response_.auth_challenge = proxy_response.auth_challenge;
555 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14556 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31557 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38558
559 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
560 pending_auth_target_ = HttpAuth::AUTH_PROXY;
561
562 DoCallback(OK);
563}
564
565void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31566 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50567 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50568 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38569
[email protected]102957f2011-09-02 17:10:14570 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38571 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58572 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38573}
574
[email protected]511f6f52010-12-17 03:58:29575void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
576 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31577 const SSLConfig& used_ssl_config,
578 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39579 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29580 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
581
ttuttle1f2d7e92015-04-28 16:17:47582 CopyConnectionAttemptsFromStreamRequest();
583
[email protected]511f6f52010-12-17 03:58:29584 headers_valid_ = true;
585 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14586 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31587 proxy_info_ = used_proxy_info;
[email protected]b8015c42013-12-24 15:18:19588 if (stream_)
589 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]511f6f52010-12-17 03:58:29590 stream_.reset(stream);
591 stream_request_.reset(); // we're done with the stream request
592 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
593}
594
ttuttle1f2d7e92015-04-28 16:17:47595void HttpNetworkTransaction::GetConnectionAttempts(
596 ConnectionAttempts* out) const {
597 *out = connection_attempts_;
598}
599
Adam Ricecb76ac62015-02-20 05:33:25600bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37601 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52602}
603
Adam Rice425cf122015-01-19 06:18:24604bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12605 return (proxy_info_.is_http() || proxy_info_.is_https() ||
606 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24607 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
608}
609
initial.commit586acc5fe2008-07-26 22:42:52610void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50611 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41612 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52613
[email protected]96d570e42008-08-05 22:43:04614 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41615 CompletionCallback c = callback_;
616 callback_.Reset();
617 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52618}
619
620void HttpNetworkTransaction::OnIOComplete(int result) {
621 int rv = DoLoop(result);
622 if (rv != ERR_IO_PENDING)
623 DoCallback(rv);
624}
625
626int HttpNetworkTransaction::DoLoop(int result) {
627 DCHECK(next_state_ != STATE_NONE);
628
629 int rv = result;
630 do {
631 State state = next_state_;
632 next_state_ = STATE_NONE;
633 switch (state) {
[email protected]1826a402014-01-08 15:40:48634 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
635 DCHECK_EQ(OK, rv);
636 rv = DoNotifyBeforeCreateStream();
637 break;
[email protected]82918cc2010-08-25 17:24:50638 case STATE_CREATE_STREAM:
639 DCHECK_EQ(OK, rv);
640 rv = DoCreateStream();
641 break;
642 case STATE_CREATE_STREAM_COMPLETE:
643 rv = DoCreateStreamComplete(rv);
644 break;
[email protected]351ab642010-08-05 16:55:31645 case STATE_INIT_STREAM:
646 DCHECK_EQ(OK, rv);
647 rv = DoInitStream();
648 break;
649 case STATE_INIT_STREAM_COMPLETE:
650 rv = DoInitStreamComplete(rv);
651 break;
[email protected]044de0642010-06-17 10:42:15652 case STATE_GENERATE_PROXY_AUTH_TOKEN:
653 DCHECK_EQ(OK, rv);
654 rv = DoGenerateProxyAuthToken();
655 break;
656 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
657 rv = DoGenerateProxyAuthTokenComplete(rv);
658 break;
659 case STATE_GENERATE_SERVER_AUTH_TOKEN:
660 DCHECK_EQ(OK, rv);
661 rv = DoGenerateServerAuthToken();
662 break;
663 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
664 rv = DoGenerateServerAuthTokenComplete(rv);
665 break;
[email protected]daddea62012-09-19 05:51:13666 case STATE_INIT_REQUEST_BODY:
667 DCHECK_EQ(OK, rv);
668 rv = DoInitRequestBody();
669 break;
670 case STATE_INIT_REQUEST_BODY_COMPLETE:
671 rv = DoInitRequestBodyComplete(rv);
672 break;
[email protected]4875ba12011-03-30 22:31:51673 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55674 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38675 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51676 rv = DoBuildRequest();
677 break;
678 case STATE_BUILD_REQUEST_COMPLETE:
679 rv = DoBuildRequestComplete(rv);
680 break;
681 case STATE_SEND_REQUEST:
682 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57683 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52684 break;
[email protected]0877e3d2009-10-17 22:29:57685 case STATE_SEND_REQUEST_COMPLETE:
686 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43687 net_log_.EndEventWithNetErrorCode(
688 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52689 break;
690 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55691 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38692 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52693 rv = DoReadHeaders();
694 break;
695 case STATE_READ_HEADERS_COMPLETE:
696 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43697 net_log_.EndEventWithNetErrorCode(
698 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52699 break;
700 case STATE_READ_BODY:
[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_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52703 rv = DoReadBody();
704 break;
705 case STATE_READ_BODY_COMPLETE:
706 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43707 net_log_.EndEventWithNetErrorCode(
708 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52709 break;
[email protected]2d2697f92009-02-18 21:00:32710 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55711 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53712 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38713 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32714 rv = DoDrainBodyForAuthRestart();
715 break;
716 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
717 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43718 net_log_.EndEventWithNetErrorCode(
719 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32720 break;
initial.commit586acc5fe2008-07-26 22:42:52721 default:
722 NOTREACHED() << "bad state";
723 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04724 break;
initial.commit586acc5fe2008-07-26 22:42:52725 }
726 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
727
728 return rv;
729}
730
[email protected]1826a402014-01-08 15:40:48731int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
732 next_state_ = STATE_CREATE_STREAM;
733 bool defer = false;
734 if (!before_network_start_callback_.is_null())
735 before_network_start_callback_.Run(&defer);
736 if (!defer)
737 return OK;
738 return ERR_IO_PENDING;
739}
740
[email protected]82918cc2010-08-25 17:24:50741int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34742 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26743 tracked_objects::ScopedTracker tracking_profile(
744 FROM_HERE_WITH_EXPLICIT_FUNCTION(
745 "424359 HttpNetworkTransaction::DoCreateStream"));
746
ttuttlec0c828492015-05-15 01:25:55747 response_.network_accessed = true;
748
[email protected]82918cc2010-08-25 17:24:50749 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44750 if (ForWebSocketHandshake()) {
751 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23752 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44753 ->RequestWebSocketHandshakeStream(
754 *request_,
755 priority_,
756 server_ssl_config_,
757 proxy_ssl_config_,
758 this,
759 websocket_handshake_stream_base_create_helper_,
760 net_log_));
761 } else {
762 stream_request_.reset(
763 session_->http_stream_factory()->RequestStream(
764 *request_,
765 priority_,
766 server_ssl_config_,
767 proxy_ssl_config_,
768 this,
769 net_log_));
770 }
[email protected]26816882010-10-14 18:03:09771 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38772 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31773}
774
[email protected]82918cc2010-08-25 17:24:50775int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47776 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
777 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
778 // which resets the stream request first. Therefore, we have to grab the
779 // connection attempts in *that* function instead of here in that case.
780 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
781 CopyConnectionAttemptsFromStreamRequest();
782
davidben701ca982015-05-18 21:21:42783 if (request_->url.SchemeIsCryptographic())
784 RecordSSLFallbackMetrics(result);
785
[email protected]394816e92010-08-03 07:38:59786 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50787 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38788 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33789 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
790 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29791 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
792 // Return OK and let the caller read the proxy's error page
793 next_state_ = STATE_NONE;
794 return OK;
bncfacdd852015-01-09 19:22:54795 } else if (result == ERR_HTTP_1_1_REQUIRED ||
796 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
797 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59798 }
799
[email protected]bd0b6772011-01-11 19:59:30800 // Handle possible handshake errors that may have occurred if the stream
801 // used SSL for one or more of the layers.
802 result = HandleSSLHandshakeError(result);
803
[email protected]8e6441ca2010-08-19 05:56:38804 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09805 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38806 return result;
[email protected]394816e92010-08-03 07:38:59807}
808
[email protected]82918cc2010-08-25 17:24:50809int HttpNetworkTransaction::DoInitStream() {
810 DCHECK(stream_.get());
811 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36812 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50813}
814
815int HttpNetworkTransaction::DoInitStreamComplete(int result) {
816 if (result == OK) {
817 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50818 } else {
[email protected]82918cc2010-08-25 17:24:50819 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26820 result = HandleIOError(result);
821
822 // The stream initialization failed, so this stream will never be useful.
[email protected]b8015c42013-12-24 15:18:19823 if (stream_)
824 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]044dcc52010-09-17 15:44:26825 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50826 }
827
828 return result;
829}
830
[email protected]044de0642010-06-17 10:42:15831int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
832 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
833 if (!ShouldApplyProxyAuth())
834 return OK;
[email protected]394816e92010-08-03 07:38:59835 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
836 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09837 auth_controllers_[target] =
838 new HttpAuthController(target,
839 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31840 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09841 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59842 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41843 io_callback_,
[email protected]394816e92010-08-03 07:38:59844 net_log_);
[email protected]044de0642010-06-17 10:42:15845}
846
847int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
848 DCHECK_NE(ERR_IO_PENDING, rv);
849 if (rv == OK)
850 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
851 return rv;
852}
853
854int HttpNetworkTransaction::DoGenerateServerAuthToken() {
855 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59856 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54857 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09858 auth_controllers_[target] =
859 new HttpAuthController(target,
860 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31861 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09862 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54863 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
864 auth_controllers_[target]->DisableEmbeddedIdentity();
865 }
[email protected]044de0642010-06-17 10:42:15866 if (!ShouldApplyServerAuth())
867 return OK;
[email protected]394816e92010-08-03 07:38:59868 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41869 io_callback_,
[email protected]394816e92010-08-03 07:38:59870 net_log_);
[email protected]044de0642010-06-17 10:42:15871}
872
873int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
874 DCHECK_NE(ERR_IO_PENDING, rv);
875 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13876 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15877 return rv;
878}
879
Adam Rice425cf122015-01-19 06:18:24880void HttpNetworkTransaction::BuildRequestHeaders(
881 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14882 request_headers_.SetHeader(HttpRequestHeaders::kHost,
883 GetHostAndOptionalPort(request_->url));
884
885 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24886 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14887 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
888 "keep-alive");
889 } else {
890 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
891 }
892
[email protected]2979a492011-04-06 00:29:14893 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11894 if (request_->upload_data_stream) {
895 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14896 request_headers_.SetHeader(
897 HttpRequestHeaders::kTransferEncoding, "chunked");
898 } else {
899 request_headers_.SetHeader(
900 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11901 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14902 }
903 } else if (request_->method == "POST" || request_->method == "PUT" ||
904 request_->method == "HEAD") {
905 // An empty POST/PUT request still needs a content length. As for HEAD,
906 // IE and Safari also add a content length header. Presumably it is to
907 // support sending a HEAD request to an URL that only expects to be sent a
908 // POST or some other method that normally would have a message body.
909 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
910 }
911
912 // Honor load flags that impact proxy caches.
913 if (request_->load_flags & LOAD_BYPASS_CACHE) {
914 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
915 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
916 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
917 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
918 }
919
920 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
921 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
922 &request_headers_);
923 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
924 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
925 &request_headers_);
926
[email protected]c10450102011-06-27 09:06:16927 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20928
Adam Rice425cf122015-01-19 06:18:24929 if (using_http_proxy_without_tunnel &&
930 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20931 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
932
[email protected]173f8e22013-04-10 04:18:20933 response_.did_use_http_auth =
934 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
935 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14936}
937
[email protected]daddea62012-09-19 05:51:13938int HttpNetworkTransaction::DoInitRequestBody() {
939 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13940 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11941 if (request_->upload_data_stream)
942 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13943 return rv;
944}
[email protected]4875ba12011-03-30 22:31:51945
[email protected]daddea62012-09-19 05:51:13946int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
947 if (result == OK)
948 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13949 return result;
950}
951
952int HttpNetworkTransaction::DoBuildRequest() {
953 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51954 headers_valid_ = false;
955
956 // This is constructed lazily (instead of within our Start method), so that
957 // we have proxy info available.
958 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:24959 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
960 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51961 }
962
[email protected]4875ba12011-03-30 22:31:51963 return OK;
964}
965
966int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24967 if (result == OK)
968 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51969 return result;
970}
971
[email protected]0877e3d2009-10-17 22:29:57972int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:34973 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26974 tracked_objects::ScopedTracker tracking_profile(
975 FROM_HERE_WITH_EXPLICIT_FUNCTION(
976 "424359 HttpNetworkTransaction::DoSendRequest"));
977
[email protected]58e32bb2013-01-21 18:23:25978 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57979 next_state_ = STATE_SEND_REQUEST_COMPLETE;
980
[email protected]bf3eb002012-11-15 05:50:11981 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52982}
983
[email protected]0877e3d2009-10-17 22:29:57984int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25985 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52986 if (result < 0)
987 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:57988 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52989 return OK;
990}
991
992int HttpNetworkTransaction::DoReadHeaders() {
993 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41994 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52995}
996
997int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:11998 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
999 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381000 if (IsCertificateError(result)) {
1001 // We don't handle a certificate error during SSL renegotiation, so we
1002 // have to return an error that's not in the certificate error range
1003 // (-2xx).
1004 LOG(ERROR) << "Got a server certificate with error " << result
1005 << " during SSL renegotiation";
1006 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1007 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1008 // TODO(wtc): Need a test case for this code path!
1009 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251010 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381011 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501012 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381013 result = HandleCertificateRequest(result);
1014 if (result == OK)
1015 return result;
[email protected]2181ea002009-06-09 01:37:271016 }
1017
bncfacdd852015-01-09 19:22:541018 if (result == ERR_HTTP_1_1_REQUIRED ||
1019 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1020 return HandleHttp11Required(result);
1021 }
1022
[email protected]c871864d72014-03-13 19:56:191023 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1024 // response headers were received, we do the best we can to make sense of it
1025 // and send it back up the stack.
1026 //
1027 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1028 // bizarre for SPDY. Assuming this logic is useful at all.
1029 // TODO(davidben): Bubble the error code up so we do not cache?
1030 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1031 result = OK;
1032
1033 if (result < 0)
1034 return HandleIOError(result);
1035
[email protected]90499482013-06-01 00:39:501036 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521037
[email protected]d58ceea82014-06-04 10:55:541038 // On a 408 response from the server ("Request Timeout") on a stale socket,
1039 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041040 // Headers can be NULL because of http://crbug.com/384554.
1041 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541042 stream_->IsConnectionReused()) {
1043 net_log_.AddEventWithNetErrorCode(
1044 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1045 response_.headers->response_code());
1046 // This will close the socket - it would be weird to try and reuse it, even
1047 // if the server doesn't actually close it.
1048 ResetConnectionAndRequestForResend();
1049 return OK;
1050 }
1051
[email protected]93f8b562012-03-27 01:00:161052 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1053 if (request_->load_flags & LOAD_MAIN_FRAME) {
1054 const int response_code = response_.headers->response_code();
1055 UMA_HISTOGRAM_ENUMERATION(
1056 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1057 }
1058
[email protected]3abacd62012-06-10 20:20:321059 net_log_.AddEvent(
1060 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1061 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391062
[email protected]a7e41312009-12-16 23:18:141063 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571064 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1065 // indicates a buggy server. See:
1066 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1067 if (request_->method == "PUT")
1068 return ERR_METHOD_NOT_SUPPORTED;
1069 }
[email protected]4ddaf2502008-10-23 18:26:191070
[email protected]0877e3d2009-10-17 22:29:571071 // Check for an intermediate 100 Continue response. An origin server is
1072 // allowed to send this response even if we didn't ask for it, so we just
1073 // need to skip over it.
1074 // We treat any other 1xx in this same way (although in practice getting
1075 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441076 // Unless this is a WebSocket request, in which case we pass it on up.
1077 if (response_.headers->response_code() / 100 == 1 &&
1078 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451079 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571080 next_state_ = STATE_READ_HEADERS;
1081 return OK;
1082 }
1083
bncc958faa2015-07-31 18:14:521084 ProcessAlternativeServices(session_, *response_.headers.get(),
1085 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421086
[email protected]e772db3f2010-07-12 18:11:131087 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571088 if (rv != OK)
1089 return rv;
1090
Adam Ricecb76ac62015-02-20 05:33:251091 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211092 stream_->GetSSLInfo(&response_.ssl_info);
1093
[email protected]0877e3d2009-10-17 22:29:571094 headers_valid_ = true;
1095 return OK;
initial.commit586acc5fe2008-07-26 22:42:521096}
1097
1098int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501099 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131100 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381101 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521102
1103 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501104 return stream_->ReadResponseBody(
1105 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521106}
1107
1108int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1109 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381110 bool done = false;
1111 if (result <= 0) {
1112 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521113 done = true;
[email protected]8e6441ca2010-08-19 05:56:381114 }
[email protected]9492e4a2010-02-24 00:58:461115
[email protected]8e6441ca2010-08-19 05:56:381116 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:311117 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:381118 // Note: Just because IsResponseBodyComplete is true, we're not
1119 // necessarily "done". We're only "done" when it is the last
1120 // read on this HttpNetworkTransaction, which will be signified
1121 // by a zero-length read.
1122 // TODO(mbelshe): The keepalive property is really a property of
1123 // the stream. No need to compute it here just to pass back
1124 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:471125 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1126 // ResponseHeaders.
1127 if (stream_->CanFindEndOfResponse()) {
1128 HttpResponseHeaders* headers = GetResponseHeaders();
1129 if (headers)
1130 keep_alive = headers->IsKeepAlive();
1131 }
initial.commit586acc5fe2008-07-26 22:42:521132 }
1133
[email protected]8e6441ca2010-08-19 05:56:381134 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521135 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381136 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011137 // Note: we don't reset the stream here. We've closed it, but we still
1138 // need it around so that callers can call methods such as
1139 // GetUploadProgress() and have them be meaningful.
1140 // TODO(mbelshe): This means we closed the stream here, and we close it
1141 // again in ~HttpNetworkTransaction. Clean that up.
1142
[email protected]8e6441ca2010-08-19 05:56:381143 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521144 }
1145
1146 // Clear these to avoid leaving around old state.
1147 read_buf_ = NULL;
1148 read_buf_len_ = 0;
1149
1150 return result;
1151}
1152
[email protected]2d2697f92009-02-18 21:00:321153int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1154 // This method differs from DoReadBody only in the next_state_. So we just
1155 // call DoReadBody and override the next_state_. Perhaps there is a more
1156 // elegant way for these two methods to share code.
1157 int rv = DoReadBody();
1158 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1159 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1160 return rv;
1161}
1162
[email protected]0877e3d2009-10-17 22:29:571163// TODO(wtc): This method and the DoReadBodyComplete method are almost
1164// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321165int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231166 // keep_alive defaults to true because the very reason we're draining the
1167 // response body is to reuse the connection for auth restart.
1168 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321169 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571170 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321171 done = true;
[email protected]68873ba2009-06-04 21:49:231172 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311173 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571174 done = true;
[email protected]2d2697f92009-02-18 21:00:321175 }
1176
1177 if (done) {
1178 DidDrainBodyForAuthRestart(keep_alive);
1179 } else {
1180 // Keep draining.
1181 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1182 }
1183
1184 return OK;
1185}
1186
[email protected]5e363962009-06-19 19:57:011187int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381188 // There are two paths through which the server can request a certificate
1189 // from us. The first is during the initial handshake, the second is
1190 // during SSL renegotiation.
1191 //
1192 // In both cases, we want to close the connection before proceeding.
1193 // We do this for two reasons:
1194 // First, we don't want to keep the connection to the server hung for a
1195 // long time while the user selects a certificate.
1196 // Second, even if we did keep the connection open, NSS has a bug where
1197 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581198 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381199
1200 if (stream_.get()) {
1201 // Since we already have a stream, we're being called as part of SSL
1202 // renegotiation.
1203 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191204 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:381205 stream_->Close(true);
1206 stream_.reset();
1207 }
1208
[email protected]26816882010-10-14 18:03:091209 // The server is asking for a client certificate during the initial
1210 // handshake.
1211 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011212
[email protected]ec229bc92010-11-22 09:51:451213 // If the user selected one of the certificates in client_certs or declined
1214 // to provide one for this server before, use the past decision
1215 // automatically.
1216 scoped_refptr<X509Certificate> client_cert;
1217 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1218 response_.cert_request_info->host_and_port, &client_cert);
1219 if (!found_cached_cert)
1220 return error;
1221
1222 // Check that the certificate selected is still a certificate the server
1223 // is likely to accept, based on the criteria supplied in the
1224 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501225 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261226 const std::vector<std::string>& cert_authorities =
1227 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451228
[email protected]f1958c382013-02-07 00:15:261229 bool cert_still_valid = cert_authorities.empty() ||
1230 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451231 if (!cert_still_valid)
1232 return error;
[email protected]5e363962009-06-19 19:57:011233 }
[email protected]ec229bc92010-11-22 09:51:451234
1235 // TODO(davidben): Add a unit test which covers this path; we need to be
1236 // able to send a legitimate certificate and also bypass/clear the
1237 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141238 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1239 &proxy_ssl_config_ : &server_ssl_config_;
1240 ssl_config->send_client_cert = true;
1241 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451242 next_state_ = STATE_CREATE_STREAM;
1243 // Reset the other member variables.
1244 // Note: this is necessary only with SSL renegotiation.
1245 ResetStateForRestart();
1246 return OK;
[email protected]0b45559b2009-06-12 21:45:111247}
1248
bncfacdd852015-01-09 19:22:541249int HttpNetworkTransaction::HandleHttp11Required(int error) {
1250 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1251 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1252
1253 if (error == ERR_HTTP_1_1_REQUIRED) {
1254 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1255 } else {
1256 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1257 }
1258 ResetConnectionAndRequestForResend();
1259 return OK;
1260}
1261
[email protected]384cc73a2013-12-08 22:41:031262void HttpNetworkTransaction::HandleClientAuthError(int error) {
1263 if (server_ssl_config_.send_client_cert &&
1264 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1265 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411266 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031267 }
1268}
1269
[email protected]bd0b6772011-01-11 19:59:301270// TODO(rch): This does not correctly handle errors when an SSL proxy is
1271// being used, as all of the errors are handled as if they were generated
1272// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381273// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301274int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1275 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031276 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301277
davidbena4c9d062015-04-03 22:34:251278 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1279 // reflect servers require a deprecated cipher rather than merely prefer
1280 // it. This, however, has no security benefit until the ciphers are actually
1281 // removed.
1282 if (!server_ssl_config_.enable_deprecated_cipher_suites &&
1283 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1284 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1285 net_log_.AddEvent(
1286 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1287 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
1288 server_ssl_config_.enable_deprecated_cipher_suites = true;
1289 ResetConnectionAndRequestForResend();
1290 return OK;
1291 }
1292
[email protected]02d1d442013-08-10 13:38:261293 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231294 uint16 version_max = server_ssl_config_.version_max;
1295
initial.commit586acc5fe2008-07-26 22:42:521296 switch (error) {
[email protected]12833302014-07-02 01:57:311297 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301298 case ERR_SSL_PROTOCOL_ERROR:
1299 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231300 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1301 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171302 // This could be a TLS-intolerant server or a server that chose a
1303 // cipher suite defined only for higher protocol versions (such as
1304 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1305 // back to the next lower version and retry.
1306 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1307 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1308 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1309 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1310 // version_max should match the maximum protocol version supported
1311 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231312 version_max--;
[email protected]c57ec022013-11-25 21:30:511313
1314 // Fallback to the lower SSL version.
1315 // While SSL 3.0 fallback should be eliminated because of security
1316 // reasons, there is a high risk of breaking the servers if this is
1317 // done in general.
[email protected]001e547e2013-12-06 16:08:391318 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421319 }
initial.commit586acc5fe2008-07-26 22:42:521320 break;
[email protected]12833302014-07-02 01:57:311321 case ERR_CONNECTION_RESET:
1322 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1323 version_max > server_ssl_config_.version_min) {
1324 // Some network devices that inspect application-layer packets seem to
1325 // inject TCP reset packets to break the connections when they see TLS
1326 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1327 //
1328 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1329 // 1.2. We don't lose much in this fallback because the explicit IV for
1330 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1331 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1332 // support.
1333 //
1334 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1335 // to trigger a version fallback in general, especially the TLS 1.0 ->
1336 // SSL 3.0 fallback, which would drop TLS extensions.
1337 version_max--;
1338 should_fallback = true;
1339 }
1340 break;
[email protected]02d1d442013-08-10 13:38:261341 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1342 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1343 version_max > server_ssl_config_.version_min) {
1344 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1345 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1346 // crbug.com/260358. In order to make the fallback as minimal as
1347 // possible, this fallback is only triggered for >= TLS 1.1.
1348 version_max--;
1349 should_fallback = true;
1350 }
1351 break;
[email protected]a53e4d12013-12-07 16:37:241352 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1353 // The server told us that we should not have fallen back. A buggy server
1354 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1355 // connection. |fallback_error_code_| is initialised to
1356 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1357 error = fallback_error_code_;
1358 break;
[email protected]02d1d442013-08-10 13:38:261359 }
1360
1361 if (should_fallback) {
1362 net_log_.AddEvent(
1363 NetLog::TYPE_SSL_VERSION_FALLBACK,
davidbenf2eaaf92015-05-15 22:18:421364 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1365 server_ssl_failure_state_, server_ssl_config_.version_max,
[email protected]02d1d442013-08-10 13:38:261366 version_max));
[email protected]a53e4d12013-12-07 16:37:241367 fallback_error_code_ = error;
davidbenf2eaaf92015-05-15 22:18:421368 fallback_failure_state_ = server_ssl_failure_state_;
[email protected]02d1d442013-08-10 13:38:261369 server_ssl_config_.version_max = version_max;
1370 server_ssl_config_.version_fallback = true;
1371 ResetConnectionAndRequestForResend();
1372 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521373 }
[email protected]158ac972013-04-19 23:29:231374
initial.commit586acc5fe2008-07-26 22:42:521375 return error;
1376}
1377
[email protected]bd0b6772011-01-11 19:59:301378// This method determines whether it is safe to resend the request after an
1379// IO error. It can only be called in response to request header or body
1380// write errors or response header read errors. It should not be used in
1381// other cases, such as a Connect error.
1382int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031383 // Because the peer may request renegotiation with client authentication at
1384 // any time, check and handle client authentication errors.
1385 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301386
1387 switch (error) {
1388 // If we try to reuse a connection that the server is in the process of
1389 // closing, we may end up successfully writing out our request (or a
1390 // portion of our request) only to find a connection error when we try to
1391 // read from (or finish writing to) the socket.
1392 case ERR_CONNECTION_RESET:
1393 case ERR_CONNECTION_CLOSED:
1394 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511395 // There can be a race between the socket pool checking checking whether a
1396 // socket is still connected, receiving the FIN, and sending/reading data
1397 // on a reused socket. If we receive the FIN between the connectedness
1398 // check and writing/reading from the socket, we may first learn the socket
1399 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1400 // likely happen when trying to retrieve its IP address.
1401 // See http://crbug.com/105824 for more details.
1402 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491403 // If a socket is closed on its initial request, HttpStreamParser returns
1404 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1405 // preconnected but failed to be used before the server timed it out.
1406 case ERR_EMPTY_RESPONSE:
1407 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381408 net_log_.AddEventWithNetErrorCode(
1409 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301410 ResetConnectionAndRequestForResend();
1411 error = OK;
1412 }
1413 break;
[email protected]8753a122011-10-16 08:05:081414 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001415 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271416 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381417 net_log_.AddEventWithNetErrorCode(
1418 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001419 ResetConnectionAndRequestForResend();
1420 error = OK;
1421 break;
[email protected]bd0b6772011-01-11 19:59:301422 }
1423 return error;
1424}
1425
[email protected]c3b35c22008-09-27 03:19:421426void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581427 ResetStateForAuthRestart();
[email protected]b8015c42013-12-24 15:18:191428 if (stream_)
1429 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:581430 stream_.reset();
1431}
1432
1433void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251434 send_start_time_ = base::TimeTicks();
1435 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251436
[email protected]0757e7702009-03-27 04:00:221437 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421438 read_buf_ = NULL;
1439 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571440 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201441 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141442 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381443 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571444}
1445
davidben701ca982015-05-18 21:21:421446void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1447 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1448 return;
1449
1450 const std::string& host = request_->url.host();
brettwa7ff1b292015-07-16 17:49:291451 bool is_google = base::EndsWith(host, "google.com",
1452 base::CompareCase::SENSITIVE) &&
davidben701ca982015-05-18 21:21:421453 (host.size() == 10 || host[host.size() - 11] == '.');
1454 if (is_google) {
1455 // Some fraction of successful connections use the fallback, but only due to
1456 // a spurious network failure. To estimate this fraction, compare handshakes
1457 // to Google servers which succeed against those that fail with an
1458 // inappropriate_fallback alert. Google servers are known to implement
1459 // FALLBACK_SCSV, so a spurious network failure while connecting would
1460 // trigger the fallback, successfully connect, but fail with this alert.
1461 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1462 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1463 }
1464
1465 if (result != OK)
1466 return;
1467
davidbenca9d69162015-05-04 20:18:451468 // Note: these values are used in histograms, so new values must be appended.
1469 enum FallbackVersion {
davidbenb937d6c2015-05-14 04:53:421470 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1471 // Obsolete: FALLBACK_SSL3 = 1,
davidbenca9d69162015-05-04 20:18:451472 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1473 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1474 FALLBACK_MAX,
1475 };
1476
1477 FallbackVersion fallback = FALLBACK_NONE;
1478 if (server_ssl_config_.version_fallback) {
1479 switch (server_ssl_config_.version_max) {
davidbenca9d69162015-05-04 20:18:451480 case SSL_PROTOCOL_VERSION_TLS1:
1481 fallback = FALLBACK_TLS1;
1482 break;
1483 case SSL_PROTOCOL_VERSION_TLS1_1:
1484 fallback = FALLBACK_TLS1_1;
1485 break;
1486 default:
1487 NOTREACHED();
1488 }
1489 }
1490 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1491 FALLBACK_MAX);
1492
1493 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1494 // should be impossible to successfully connect to them with the fallback.
1495 // This helps estimate intolerant locally-configured SSL MITMs.
davidben701ca982015-05-18 21:21:421496 if (is_google) {
davidbenca9d69162015-05-04 20:18:451497 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1498 fallback, FALLBACK_MAX);
1499 }
1500
1501 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
1502 server_ssl_config_.enable_deprecated_cipher_suites);
davidbenf2eaaf92015-05-15 22:18:421503
1504 if (server_ssl_config_.version_fallback) {
1505 // Record the error code which triggered the fallback and the state the
1506 // handshake was in.
1507 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1508 -fallback_error_code_);
1509 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1510 fallback_failure_state_, SSL_FAILURE_MAX);
1511 }
davidbenca9d69162015-05-04 20:18:451512}
1513
[email protected]0877e3d2009-10-17 22:29:571514HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501515 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421516}
1517
[email protected]a34f61ee2014-03-18 20:59:491518bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381519 bool connection_is_proven = stream_->IsConnectionReused();
1520 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161521
[email protected]2a5c76b2008-09-25 22:15:161522 // NOTE: we resend a request only if we reused a keep-alive connection.
1523 // This automatically prevents an infinite resend loop because we'll run
1524 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381525 if (connection_is_proven && !has_received_headers)
1526 return true;
1527 return false;
[email protected]1c773ea12009-04-28 19:58:421528}
1529
1530void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381531 if (stream_.get()) {
1532 stream_->Close(true);
1533 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161534 }
1535
[email protected]0877e3d2009-10-17 22:29:571536 // We need to clear request_headers_ because it contains the real request
1537 // headers, but we may need to resend the CONNECT request first to recreate
1538 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201539 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501540 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541541}
1542
[email protected]1c773ea12009-04-28 19:58:421543bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241544 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421545}
license.botbf09a502008-08-24 00:55:551546
[email protected]1c773ea12009-04-28 19:58:421547bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041548 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421549}
1550
[email protected]e772db3f2010-07-12 18:11:131551int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271552 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501553 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421554
[email protected]0877e3d2009-10-17 22:29:571555 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581556 if (status != HTTP_UNAUTHORIZED &&
1557 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421558 return OK;
[email protected]9094b602012-02-27 21:44:581559 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111560 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161561 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1562 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421563
[email protected]9094b602012-02-27 21:44:581564 // This case can trigger when an HTTPS server responds with a "Proxy
1565 // authentication required" status code through a non-authenticating
1566 // proxy.
[email protected]7a67a8152010-11-05 18:31:101567 if (!auth_controllers_[target].get())
1568 return ERR_UNEXPECTED_PROXY_AUTH;
1569
[email protected]a7ea8832010-07-12 17:54:541570 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311571 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1572 net_log_);
[email protected]228404f2010-06-24 04:31:411573 if (auth_controllers_[target]->HaveAuthHandler())
1574 pending_auth_target_ = target;
1575
1576 scoped_refptr<AuthChallengeInfo> auth_info =
1577 auth_controllers_[target]->auth_info();
1578 if (auth_info.get())
1579 response_.auth_challenge = auth_info;
1580
[email protected]228404f2010-06-24 04:31:411581 return rv;
[email protected]f9ee6b52008-11-08 06:46:231582}
1583
[email protected]8e6441ca2010-08-19 05:56:381584bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1585 return auth_controllers_[target].get() &&
1586 auth_controllers_[target]->HaveAuth();
1587}
1588
[email protected]228404f2010-06-24 04:31:411589GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1590 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461591 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411592 if (!proxy_info_.proxy_server().is_valid() ||
1593 proxy_info_.proxy_server().is_direct()) {
1594 return GURL(); // There is no proxy server.
1595 }
[email protected]2df19bb2010-08-25 20:13:461596 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1597 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351598 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461599 }
[email protected]228404f2010-06-24 04:31:411600 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291601 if (ForWebSocketHandshake()) {
1602 const GURL& url = request_->url;
1603 url::Replacements<char> ws_to_http;
1604 if (url.SchemeIs("ws")) {
1605 ws_to_http.SetScheme("http", url::Component(0, 4));
1606 } else {
1607 DCHECK(url.SchemeIs("wss"));
1608 ws_to_http.SetScheme("https", url::Component(0, 5));
1609 }
1610 return url.ReplaceComponents(ws_to_http);
1611 }
[email protected]228404f2010-06-24 04:31:411612 return request_->url;
1613 default:
1614 return GURL();
1615 }
[email protected]c3b35c22008-09-27 03:19:421616}
1617
[email protected]831e4a32013-11-14 02:14:441618bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141619 return websocket_handshake_stream_base_create_helper_ &&
1620 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441621}
1622
[email protected]d8eb84242010-09-25 02:25:061623#define STATE_CASE(s) \
1624 case s: \
1625 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1626 break
[email protected]aef04272010-06-28 18:03:041627
1628std::string HttpNetworkTransaction::DescribeState(State state) {
1629 std::string description;
1630 switch (state) {
[email protected]1826a402014-01-08 15:40:481631 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501632 STATE_CASE(STATE_CREATE_STREAM);
1633 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131634 STATE_CASE(STATE_INIT_REQUEST_BODY);
1635 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511636 STATE_CASE(STATE_BUILD_REQUEST);
1637 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041638 STATE_CASE(STATE_SEND_REQUEST);
1639 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1640 STATE_CASE(STATE_READ_HEADERS);
1641 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041642 STATE_CASE(STATE_READ_BODY);
1643 STATE_CASE(STATE_READ_BODY_COMPLETE);
1644 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1645 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041646 STATE_CASE(STATE_NONE);
1647 default:
[email protected]d8eb84242010-09-25 02:25:061648 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1649 state);
[email protected]aef04272010-06-28 18:03:041650 break;
1651 }
1652 return description;
1653}
1654
1655#undef STATE_CASE
1656
ttuttle1f2d7e92015-04-28 16:17:471657void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1658 DCHECK(stream_request_);
1659
1660 // Since the transaction can restart with auth credentials, it may create a
1661 // stream more than once. Accumulate all of the connection attempts across
1662 // those streams by appending them to the vector:
1663 for (const auto& attempt : stream_request_->connection_attempts())
1664 connection_attempts_.push_back(attempt);
1665}
1666
[email protected]c3b35c22008-09-27 03:19:421667} // namespace net