blob: 76e352eed923aaabcb6d8011f253cd02629acf2a [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
[email protected]30d4c022013-07-18 22:58:1670void ProcessAlternateProtocol(
[email protected]d7599122014-05-24 03:37:2371 HttpNetworkSession* session,
[email protected]30d4c022013-07-18 22:58:1672 const HttpResponseHeaders& headers,
73 const HostPortPair& http_host_port_pair) {
[email protected]287d9412014-07-08 23:01:0074 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3875 return;
[email protected]287d9412014-07-08 23:01:0076
77 std::vector<std::string> alternate_protocol_values;
78 void* iter = NULL;
79 std::string alternate_protocol_str;
80 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
81 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5282 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
83 &alternate_protocol_str);
84 if (!alternate_protocol_str.empty()) {
85 alternate_protocol_values.push_back(alternate_protocol_str);
86 }
[email protected]564b4912010-03-09 16:30:4287 }
88
[email protected]d7599122014-05-24 03:37:2389 session->http_stream_factory()->ProcessAlternateProtocol(
90 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:0091 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:2392 http_host_port_pair,
93 *session);
[email protected]f45c1ee2010-08-03 00:54:3094}
95
estade5e5529d2015-05-21 20:59:1196scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
[email protected]ea5ef4c2013-06-13 22:50:2797 const GURL* url,
98 int net_error,
davidbenf2eaaf92015-05-15 22:18:4299 SSLFailureState ssl_failure_state,
[email protected]ea5ef4c2013-06-13 22:50:27100 uint16 version_before,
101 uint16 version_after,
eroman001c3742015-04-23 03:11:17102 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11103 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
[email protected]b6754252012-06-13 23:14:38104 dict->SetString("host_and_port", GetHostAndPort(*url));
105 dict->SetInteger("net_error", net_error);
davidbenf2eaaf92015-05-15 22:18:42106 dict->SetInteger("ssl_failure_state", ssl_failure_state);
[email protected]b6754252012-06-13 23:14:38107 dict->SetInteger("version_before", version_before);
108 dict->SetInteger("version_after", version_after);
estade5e5529d2015-05-21 20:59:11109 return dict.Pass();
[email protected]b6754252012-06-13 23:14:38110}
[email protected]db74b0102012-05-31 19:55:53111
estade5e5529d2015-05-21 20:59:11112scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
eroman001c3742015-04-23 03:11:17113 const GURL* url,
114 int net_error,
115 NetLogCaptureMode /* capture_mode */) {
estade5e5529d2015-05-21 20:59:11116 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
davidbena4c9d062015-04-03 22:34:25117 dict->SetString("host_and_port", GetHostAndPort(*url));
118 dict->SetInteger("net_error", net_error);
estade5e5529d2015-05-21 20:59:11119 return dict.Pass();
davidbena4c9d062015-04-03 22:34:25120}
121
[email protected]1c773ea12009-04-28 19:58:42122} // namespace
123
initial.commit586acc5fe2008-07-26 22:42:52124//-----------------------------------------------------------------------------
125
[email protected]262eec82013-03-19 21:01:36126HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
127 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22128 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32129 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
130 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52131 session_(session),
132 request_(NULL),
[email protected]262eec82013-03-19 21:01:36133 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57134 headers_valid_(false),
davidbenf2eaaf92015-05-15 22:18:42135 server_ssl_failure_state_(SSL_FAILURE_NONE),
[email protected]a53e4d12013-12-07 16:37:24136 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
davidbenf2eaaf92015-05-15 22:18:42137 fallback_failure_state_(SSL_FAILURE_NONE),
[email protected]b94f92d2010-10-27 16:45:20138 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52139 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19140 total_received_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54141 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44142 establishing_tunnel_(false),
143 websocket_handshake_stream_base_create_helper_(NULL) {
[email protected]102957f2011-09-02 17:10:14144 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]d7599122014-05-24 03:37:23145 session->GetNextProtos(&server_ssl_config_.next_protos);
[email protected]99ffa5a2011-10-06 04:20:19146 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50147}
148
[email protected]0b0bf032010-09-21 18:08:50149HttpNetworkTransaction::~HttpNetworkTransaction() {
150 if (stream_.get()) {
151 HttpResponseHeaders* headers = GetResponseHeaders();
152 // TODO(mbelshe): The stream_ should be able to compute whether or not the
153 // stream should be kept alive. No reason to compute here
154 // and pass it in.
155 bool try_to_keep_alive =
156 next_state_ == STATE_NONE &&
157 stream_->CanFindEndOfResponse() &&
158 (!headers || headers->IsKeepAlive());
159 if (!try_to_keep_alive) {
160 stream_->Close(true /* not reusable */);
161 } else {
162 if (stream_->IsResponseBodyComplete()) {
163 // If the response body is complete, we can just reuse the socket.
164 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44165 } else if (stream_->IsSpdyHttpStream()) {
166 // Doesn't really matter for SpdyHttpStream. Just close it.
167 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50168 } else {
169 // Otherwise, we try to drain the response body.
yhiranoa7e05bb2014-11-06 05:40:39170 HttpStream* stream = stream_.release();
[email protected]3fe8d2f82013-10-17 08:56:07171 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50172 }
173 }
174 }
[email protected]02cad5d2013-10-02 08:14:03175
176 if (request_ && request_->upload_data_stream)
177 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50178}
179
[email protected]684970b2009-08-14 04:54:46180int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41181 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53182 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53183 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04184 request_ = request_info;
185
[email protected]99ffa5a2011-10-06 04:20:19186 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14187 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19188 proxy_ssl_config_.rev_checking_enabled = false;
189 }
[email protected]6fbac162011-06-20 00:29:04190
jkarlinfb1d5172015-01-12 14:10:29191 if (request_->load_flags & LOAD_PREFETCH)
192 response_.unused_since_prefetch = true;
193
[email protected]1ea4f46a2014-04-22 22:33:56194 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49195 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
196 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40197
jeremyim8d44fadd2015-02-10 19:18:15198 if (server_ssl_config_.fastradio_padding_enabled) {
199 server_ssl_config_.fastradio_padding_eligible =
200 session_->ssl_config_service()->SupportsFastradioPadding(
201 request_info->url);
202 }
203
[email protected]1826a402014-01-08 15:40:48204 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04205 int rv = DoLoop(OK);
206 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41207 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04208 return rv;
209}
210
211int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41212 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38213 DCHECK(!stream_.get());
214 DCHECK(!stream_request_.get());
215 DCHECK_EQ(STATE_NONE, next_state_);
216
[email protected]82918cc2010-08-25 17:24:50217 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38218
[email protected]ccb40e52008-09-17 20:54:40219 int rv = DoLoop(OK);
220 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41221 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11222 return rv;
[email protected]96d570e42008-08-05 22:43:04223}
224
[email protected]0b45559b2009-06-12 21:45:11225int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41226 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38227 // In HandleCertificateRequest(), we always tear down existing stream
228 // requests to force a new connection. So we shouldn't have one here.
229 DCHECK(!stream_request_.get());
230 DCHECK(!stream_.get());
231 DCHECK_EQ(STATE_NONE, next_state_);
232
[email protected]102957f2011-09-02 17:10:14233 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
234 &proxy_ssl_config_ : &server_ssl_config_;
235 ssl_config->send_client_cert = true;
236 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45237 session_->ssl_client_auth_cache()->Add(
238 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11239 // Reset the other member variables.
240 // Note: this is necessary only with SSL renegotiation.
241 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50242 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11243 int rv = DoLoop(OK);
244 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41245 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11246 return rv;
247}
248
[email protected]49639fa2011-12-20 23:22:41249int HttpNetworkTransaction::RestartWithAuth(
250 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22251 HttpAuth::Target target = pending_auth_target_;
252 if (target == HttpAuth::AUTH_NONE) {
253 NOTREACHED();
254 return ERR_UNEXPECTED;
255 }
[email protected]0757e7702009-03-27 04:00:22256 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42257
[email protected]f3cf9802011-10-28 18:44:58258 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13259
[email protected]49639fa2011-12-20 23:22:41260 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38261
262 int rv = OK;
263 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
264 // In this case, we've gathered credentials for use with proxy
265 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50266 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38267 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59268 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54269 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58270 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54271 } else {
[email protected]8e6441ca2010-08-19 05:56:38272 // In this case, we've gathered credentials for the server or the proxy
273 // but it is not during the tunneling phase.
274 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54275 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38276 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54277 }
[email protected]c3b35c22008-09-27 03:19:42278
[email protected]c3b35c22008-09-27 03:19:42279 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41280 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42281 return rv;
[email protected]96d570e42008-08-05 22:43:04282}
283
[email protected]f9ee6b52008-11-08 06:46:23284void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
285 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38286 DCHECK(!stream_request_.get());
287
[email protected]2d2697f92009-02-18 21:00:32288 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57289 // Even if the server says the connection is keep-alive, we have to be
290 // able to find the end of each response in order to reuse the connection.
291 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31292 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57293 // If the response body hasn't been completely read, we need to drain
294 // it first.
[email protected]351ab642010-08-05 16:55:31295 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32296 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57297 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32298 read_buf_len_ = kDrainBodyBufferSize;
299 return;
300 }
[email protected]0877e3d2009-10-17 22:29:57301 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09302 }
303
[email protected]2d2697f92009-02-18 21:00:32304 // We don't need to drain the response body, so we act as if we had drained
305 // the response body.
306 DidDrainBodyForAuthRestart(keep_alive);
307}
308
309void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38310 DCHECK(!stream_request_.get());
311
312 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19313 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:58314 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46315 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38316 // We should call connection_->set_idle_time(), but this doesn't occur
317 // often enough to be worth the trouble.
318 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39319 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38320 }
[email protected]697ef4c2010-10-14 16:38:58321
322 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46323 // Close the stream and mark it as not_reusable. Even in the
324 // keep_alive case, we've determined that the stream_ is not
325 // reusable if new_stream is NULL.
326 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58327 next_state_ = STATE_CREATE_STREAM;
328 } else {
[email protected]b8015c42013-12-24 15:18:19329 // Renewed streams shouldn't carry over received bytes.
330 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
[email protected]697ef4c2010-10-14 16:38:58331 next_state_ = STATE_INIT_STREAM;
332 }
333 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32334 }
[email protected]f9ee6b52008-11-08 06:46:23335
336 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58337 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23338}
339
[email protected]8e6441ca2010-08-19 05:56:38340bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
341 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
342 HaveAuth(pending_auth_target_);
343}
344
[email protected]9dea9e1f2009-01-29 00:30:47345int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41346 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04347 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35348 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04349
[email protected]1f14a912009-12-21 20:32:44350 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04351
[email protected]ad8e04a2010-11-01 04:16:27352 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38353 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04354 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29355 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04356 // bytes when establishing a tunnel because they might be controlled by
357 // an active network attacker. We don't worry about this for HTTP
358 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29359 // We reach this case when the user cancels a 407 proxy auth prompt. We
360 // also don't worry about this for an HTTPS Proxy, because the
361 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04362 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46363 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58364 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54365 LOG(WARNING) << "Blocked proxy response with status "
366 << headers->response_code() << " to CONNECT request for "
367 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04368 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44369 }
370
[email protected]e60e47a2010-07-14 03:37:18371 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31372 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18373
[email protected]96d570e42008-08-05 22:43:04374 read_buf_ = buf;
375 read_buf_len_ = buf_len;
376
[email protected]1f14a912009-12-21 20:32:44377 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04378 int rv = DoLoop(OK);
379 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41380 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04381 return rv;
382}
383
[email protected]8cd06c02014-01-25 07:50:14384void HttpNetworkTransaction::StopCaching() {}
385
[email protected]79e1fd62013-06-20 06:50:04386bool HttpNetworkTransaction::GetFullRequestHeaders(
387 HttpRequestHeaders* headers) const {
388 // TODO(ttuttle): Make sure we've populated request_headers_.
389 *headers = request_headers_;
390 return true;
391}
392
[email protected]b8015c42013-12-24 15:18:19393int64 HttpNetworkTransaction::GetTotalReceivedBytes() const {
394 int64 total_received_bytes = total_received_bytes_;
395 if (stream_)
396 total_received_bytes += stream_->GetTotalReceivedBytes();
397 return total_received_bytes;
398}
399
[email protected]8cd06c02014-01-25 07:50:14400void HttpNetworkTransaction::DoneReading() {}
401
[email protected]96d570e42008-08-05 22:43:04402const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
ttuttlec0c828492015-05-15 01:25:55403 return &response_;
[email protected]96d570e42008-08-05 22:43:04404}
405
406LoadState HttpNetworkTransaction::GetLoadState() const {
407 // TODO(wtc): Define a new LoadState value for the
408 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
409 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48410 case STATE_CREATE_STREAM:
411 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50412 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38413 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15414 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
415 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57416 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04417 return LOAD_STATE_SENDING_REQUEST;
418 case STATE_READ_HEADERS_COMPLETE:
419 return LOAD_STATE_WAITING_FOR_RESPONSE;
420 case STATE_READ_BODY_COMPLETE:
421 return LOAD_STATE_READING_RESPONSE;
422 default:
423 return LOAD_STATE_IDLE;
424 }
425}
426
[email protected]196d18a2012-08-30 03:47:31427UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31428 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31429 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04430
yhiranoa7e05bb2014-11-06 05:40:39431 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04432}
433
[email protected]8cd06c02014-01-25 07:50:14434void HttpNetworkTransaction::SetQuicServerInfo(
435 QuicServerInfo* quic_server_info) {}
436
[email protected]5033ab82013-03-22 20:17:46437bool HttpNetworkTransaction::GetLoadTimingInfo(
438 LoadTimingInfo* load_timing_info) const {
439 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
440 return false;
441
442 load_timing_info->proxy_resolve_start =
443 proxy_info_.proxy_resolve_start_time();
444 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
445 load_timing_info->send_start = send_start_time_;
446 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46447 return true;
448}
449
450void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
451 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47452 if (stream_request_)
453 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03454 if (stream_)
455 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46456}
457
[email protected]831e4a32013-11-14 02:14:44458void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
459 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
460 websocket_handshake_stream_base_create_helper_ = create_helper;
461}
462
[email protected]1826a402014-01-08 15:40:48463void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
464 const BeforeNetworkStartCallback& callback) {
465 before_network_start_callback_ = callback;
466}
467
[email protected]597a1ab2014-06-26 08:12:27468void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
469 const BeforeProxyHeadersSentCallback& callback) {
470 before_proxy_headers_sent_callback_ = callback;
471}
472
[email protected]1826a402014-01-08 15:40:48473int HttpNetworkTransaction::ResumeNetworkStart() {
474 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
475 return DoLoop(OK);
476}
477
[email protected]102e27c2011-02-23 01:01:31478void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
479 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39480 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50481 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38482 DCHECK(stream_request_.get());
483
[email protected]b8015c42013-12-24 15:18:19484 if (stream_)
485 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:38486 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14487 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31488 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38489 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51490 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
491 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38492 response_.was_fetched_via_spdy = stream_request_->using_spdy();
493 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15494 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
495 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38496 OnIOComplete(OK);
497}
498
[email protected]a9cf2b92013-10-30 12:08:49499void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50500 const SSLConfig& used_ssl_config,
501 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49502 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44503 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50504}
505
[email protected]102e27c2011-02-23 01:01:31506void HttpNetworkTransaction::OnStreamFailed(int result,
davidbenf2eaaf92015-05-15 22:18:42507 const SSLConfig& used_ssl_config,
508 SSLFailureState ssl_failure_state) {
[email protected]82918cc2010-08-25 17:24:50509 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38510 DCHECK_NE(OK, result);
511 DCHECK(stream_request_.get());
512 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14513 server_ssl_config_ = used_ssl_config;
davidbenf2eaaf92015-05-15 22:18:42514 server_ssl_failure_state_ = ssl_failure_state;
[email protected]8e6441ca2010-08-19 05:56:38515
516 OnIOComplete(result);
517}
518
[email protected]102e27c2011-02-23 01:01:31519void HttpNetworkTransaction::OnCertificateError(
520 int result,
521 const SSLConfig& used_ssl_config,
522 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50523 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38524 DCHECK_NE(OK, result);
525 DCHECK(stream_request_.get());
526 DCHECK(!stream_.get());
527
528 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14529 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38530
531 // TODO(mbelshe): For now, we're going to pass the error through, and that
532 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50533 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
534 // good and the user chooses to ignore the error. This is not ideal, but not
535 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38536
537 OnIOComplete(result);
538}
539
540void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50541 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31542 const SSLConfig& used_ssl_config,
543 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50544 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38545 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50546 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38547
548 establishing_tunnel_ = true;
549 response_.headers = proxy_response.headers;
550 response_.auth_challenge = proxy_response.auth_challenge;
551 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14552 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31553 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38554
555 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
556 pending_auth_target_ = HttpAuth::AUTH_PROXY;
557
558 DoCallback(OK);
559}
560
561void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31562 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50563 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50564 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38565
[email protected]102957f2011-09-02 17:10:14566 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38567 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58568 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38569}
570
[email protected]511f6f52010-12-17 03:58:29571void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
572 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31573 const SSLConfig& used_ssl_config,
574 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39575 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29576 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
577
ttuttle1f2d7e92015-04-28 16:17:47578 CopyConnectionAttemptsFromStreamRequest();
579
[email protected]511f6f52010-12-17 03:58:29580 headers_valid_ = true;
581 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14582 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31583 proxy_info_ = used_proxy_info;
[email protected]b8015c42013-12-24 15:18:19584 if (stream_)
585 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]511f6f52010-12-17 03:58:29586 stream_.reset(stream);
587 stream_request_.reset(); // we're done with the stream request
588 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
589}
590
ttuttle1f2d7e92015-04-28 16:17:47591void HttpNetworkTransaction::GetConnectionAttempts(
592 ConnectionAttempts* out) const {
593 *out = connection_attempts_;
594}
595
Adam Ricecb76ac62015-02-20 05:33:25596bool HttpNetworkTransaction::IsSecureRequest() const {
lgarrona774b922015-05-14 22:56:37597 return request_->url.SchemeIsCryptographic();
initial.commit586acc5fe2008-07-26 22:42:52598}
599
Adam Rice425cf122015-01-19 06:18:24600bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12601 return (proxy_info_.is_http() || proxy_info_.is_https() ||
602 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24603 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
604}
605
initial.commit586acc5fe2008-07-26 22:42:52606void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50607 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41608 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52609
[email protected]96d570e42008-08-05 22:43:04610 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41611 CompletionCallback c = callback_;
612 callback_.Reset();
613 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52614}
615
616void HttpNetworkTransaction::OnIOComplete(int result) {
617 int rv = DoLoop(result);
618 if (rv != ERR_IO_PENDING)
619 DoCallback(rv);
620}
621
622int HttpNetworkTransaction::DoLoop(int result) {
623 DCHECK(next_state_ != STATE_NONE);
624
625 int rv = result;
626 do {
627 State state = next_state_;
628 next_state_ = STATE_NONE;
629 switch (state) {
[email protected]1826a402014-01-08 15:40:48630 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
631 DCHECK_EQ(OK, rv);
632 rv = DoNotifyBeforeCreateStream();
633 break;
[email protected]82918cc2010-08-25 17:24:50634 case STATE_CREATE_STREAM:
635 DCHECK_EQ(OK, rv);
636 rv = DoCreateStream();
637 break;
638 case STATE_CREATE_STREAM_COMPLETE:
639 rv = DoCreateStreamComplete(rv);
640 break;
[email protected]351ab642010-08-05 16:55:31641 case STATE_INIT_STREAM:
642 DCHECK_EQ(OK, rv);
643 rv = DoInitStream();
644 break;
645 case STATE_INIT_STREAM_COMPLETE:
646 rv = DoInitStreamComplete(rv);
647 break;
[email protected]044de0642010-06-17 10:42:15648 case STATE_GENERATE_PROXY_AUTH_TOKEN:
649 DCHECK_EQ(OK, rv);
650 rv = DoGenerateProxyAuthToken();
651 break;
652 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
653 rv = DoGenerateProxyAuthTokenComplete(rv);
654 break;
655 case STATE_GENERATE_SERVER_AUTH_TOKEN:
656 DCHECK_EQ(OK, rv);
657 rv = DoGenerateServerAuthToken();
658 break;
659 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
660 rv = DoGenerateServerAuthTokenComplete(rv);
661 break;
[email protected]daddea62012-09-19 05:51:13662 case STATE_INIT_REQUEST_BODY:
663 DCHECK_EQ(OK, rv);
664 rv = DoInitRequestBody();
665 break;
666 case STATE_INIT_REQUEST_BODY_COMPLETE:
667 rv = DoInitRequestBodyComplete(rv);
668 break;
[email protected]4875ba12011-03-30 22:31:51669 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55670 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38671 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51672 rv = DoBuildRequest();
673 break;
674 case STATE_BUILD_REQUEST_COMPLETE:
675 rv = DoBuildRequestComplete(rv);
676 break;
677 case STATE_SEND_REQUEST:
678 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57679 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52680 break;
[email protected]0877e3d2009-10-17 22:29:57681 case STATE_SEND_REQUEST_COMPLETE:
682 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43683 net_log_.EndEventWithNetErrorCode(
684 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52685 break;
686 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55687 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38688 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52689 rv = DoReadHeaders();
690 break;
691 case STATE_READ_HEADERS_COMPLETE:
692 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43693 net_log_.EndEventWithNetErrorCode(
694 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52695 break;
696 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55697 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38698 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52699 rv = DoReadBody();
700 break;
701 case STATE_READ_BODY_COMPLETE:
702 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43703 net_log_.EndEventWithNetErrorCode(
704 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52705 break;
[email protected]2d2697f92009-02-18 21:00:32706 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55707 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53708 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38709 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32710 rv = DoDrainBodyForAuthRestart();
711 break;
712 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
713 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43714 net_log_.EndEventWithNetErrorCode(
715 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32716 break;
initial.commit586acc5fe2008-07-26 22:42:52717 default:
718 NOTREACHED() << "bad state";
719 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04720 break;
initial.commit586acc5fe2008-07-26 22:42:52721 }
722 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
723
724 return rv;
725}
726
[email protected]1826a402014-01-08 15:40:48727int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
728 next_state_ = STATE_CREATE_STREAM;
729 bool defer = false;
730 if (!before_network_start_callback_.is_null())
731 before_network_start_callback_.Run(&defer);
732 if (!defer)
733 return OK;
734 return ERR_IO_PENDING;
735}
736
[email protected]82918cc2010-08-25 17:24:50737int HttpNetworkTransaction::DoCreateStream() {
pkastingec2cdb52015-05-02 01:19:34738 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26739 tracked_objects::ScopedTracker tracking_profile(
740 FROM_HERE_WITH_EXPLICIT_FUNCTION(
741 "424359 HttpNetworkTransaction::DoCreateStream"));
742
ttuttlec0c828492015-05-15 01:25:55743 response_.network_accessed = true;
744
[email protected]82918cc2010-08-25 17:24:50745 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44746 if (ForWebSocketHandshake()) {
747 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23748 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44749 ->RequestWebSocketHandshakeStream(
750 *request_,
751 priority_,
752 server_ssl_config_,
753 proxy_ssl_config_,
754 this,
755 websocket_handshake_stream_base_create_helper_,
756 net_log_));
757 } else {
758 stream_request_.reset(
759 session_->http_stream_factory()->RequestStream(
760 *request_,
761 priority_,
762 server_ssl_config_,
763 proxy_ssl_config_,
764 this,
765 net_log_));
766 }
[email protected]26816882010-10-14 18:03:09767 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38768 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31769}
770
[email protected]82918cc2010-08-25 17:24:50771int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
ttuttle1f2d7e92015-04-28 16:17:47772 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
773 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
774 // which resets the stream request first. Therefore, we have to grab the
775 // connection attempts in *that* function instead of here in that case.
776 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
777 CopyConnectionAttemptsFromStreamRequest();
778
davidben701ca982015-05-18 21:21:42779 if (request_->url.SchemeIsCryptographic())
780 RecordSSLFallbackMetrics(result);
781
[email protected]394816e92010-08-03 07:38:59782 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50783 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38784 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33785 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
786 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29787 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
788 // Return OK and let the caller read the proxy's error page
789 next_state_ = STATE_NONE;
790 return OK;
bncfacdd852015-01-09 19:22:54791 } else if (result == ERR_HTTP_1_1_REQUIRED ||
792 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
793 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59794 }
795
[email protected]bd0b6772011-01-11 19:59:30796 // Handle possible handshake errors that may have occurred if the stream
797 // used SSL for one or more of the layers.
798 result = HandleSSLHandshakeError(result);
799
[email protected]8e6441ca2010-08-19 05:56:38800 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09801 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38802 return result;
[email protected]394816e92010-08-03 07:38:59803}
804
[email protected]82918cc2010-08-25 17:24:50805int HttpNetworkTransaction::DoInitStream() {
806 DCHECK(stream_.get());
807 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36808 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50809}
810
811int HttpNetworkTransaction::DoInitStreamComplete(int result) {
812 if (result == OK) {
813 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50814 } else {
[email protected]82918cc2010-08-25 17:24:50815 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26816 result = HandleIOError(result);
817
818 // The stream initialization failed, so this stream will never be useful.
[email protected]b8015c42013-12-24 15:18:19819 if (stream_)
820 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]044dcc52010-09-17 15:44:26821 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50822 }
823
824 return result;
825}
826
[email protected]044de0642010-06-17 10:42:15827int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
828 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
829 if (!ShouldApplyProxyAuth())
830 return OK;
[email protected]394816e92010-08-03 07:38:59831 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
832 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09833 auth_controllers_[target] =
834 new HttpAuthController(target,
835 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31836 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09837 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59838 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41839 io_callback_,
[email protected]394816e92010-08-03 07:38:59840 net_log_);
[email protected]044de0642010-06-17 10:42:15841}
842
843int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
844 DCHECK_NE(ERR_IO_PENDING, rv);
845 if (rv == OK)
846 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
847 return rv;
848}
849
850int HttpNetworkTransaction::DoGenerateServerAuthToken() {
851 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59852 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54853 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09854 auth_controllers_[target] =
855 new HttpAuthController(target,
856 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31857 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09858 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54859 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
860 auth_controllers_[target]->DisableEmbeddedIdentity();
861 }
[email protected]044de0642010-06-17 10:42:15862 if (!ShouldApplyServerAuth())
863 return OK;
[email protected]394816e92010-08-03 07:38:59864 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41865 io_callback_,
[email protected]394816e92010-08-03 07:38:59866 net_log_);
[email protected]044de0642010-06-17 10:42:15867}
868
869int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
870 DCHECK_NE(ERR_IO_PENDING, rv);
871 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13872 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15873 return rv;
874}
875
Adam Rice425cf122015-01-19 06:18:24876void HttpNetworkTransaction::BuildRequestHeaders(
877 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14878 request_headers_.SetHeader(HttpRequestHeaders::kHost,
879 GetHostAndOptionalPort(request_->url));
880
881 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24882 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14883 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
884 "keep-alive");
885 } else {
886 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
887 }
888
[email protected]2979a492011-04-06 00:29:14889 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11890 if (request_->upload_data_stream) {
891 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14892 request_headers_.SetHeader(
893 HttpRequestHeaders::kTransferEncoding, "chunked");
894 } else {
895 request_headers_.SetHeader(
896 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11897 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14898 }
899 } else if (request_->method == "POST" || request_->method == "PUT" ||
900 request_->method == "HEAD") {
901 // An empty POST/PUT request still needs a content length. As for HEAD,
902 // IE and Safari also add a content length header. Presumably it is to
903 // support sending a HEAD request to an URL that only expects to be sent a
904 // POST or some other method that normally would have a message body.
905 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
906 }
907
908 // Honor load flags that impact proxy caches.
909 if (request_->load_flags & LOAD_BYPASS_CACHE) {
910 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
911 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
912 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
913 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
914 }
915
916 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
917 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
918 &request_headers_);
919 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
920 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
921 &request_headers_);
922
[email protected]c10450102011-06-27 09:06:16923 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20924
Adam Rice425cf122015-01-19 06:18:24925 if (using_http_proxy_without_tunnel &&
926 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20927 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
928
[email protected]173f8e22013-04-10 04:18:20929 response_.did_use_http_auth =
930 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
931 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14932}
933
[email protected]daddea62012-09-19 05:51:13934int HttpNetworkTransaction::DoInitRequestBody() {
935 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13936 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11937 if (request_->upload_data_stream)
938 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13939 return rv;
940}
[email protected]4875ba12011-03-30 22:31:51941
[email protected]daddea62012-09-19 05:51:13942int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
943 if (result == OK)
944 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13945 return result;
946}
947
948int HttpNetworkTransaction::DoBuildRequest() {
949 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51950 headers_valid_ = false;
951
952 // This is constructed lazily (instead of within our Start method), so that
953 // we have proxy info available.
954 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:24955 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
956 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51957 }
958
[email protected]4875ba12011-03-30 22:31:51959 return OK;
960}
961
962int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24963 if (result == OK)
964 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51965 return result;
966}
967
[email protected]0877e3d2009-10-17 22:29:57968int HttpNetworkTransaction::DoSendRequest() {
pkastingec2cdb52015-05-02 01:19:34969 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
pkastinga9269aa42015-04-10 01:42:26970 tracked_objects::ScopedTracker tracking_profile(
971 FROM_HERE_WITH_EXPLICIT_FUNCTION(
972 "424359 HttpNetworkTransaction::DoSendRequest"));
973
[email protected]58e32bb2013-01-21 18:23:25974 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57975 next_state_ = STATE_SEND_REQUEST_COMPLETE;
976
[email protected]bf3eb002012-11-15 05:50:11977 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52978}
979
[email protected]0877e3d2009-10-17 22:29:57980int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25981 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52982 if (result < 0)
983 return HandleIOError(result);
[email protected]0877e3d2009-10-17 22:29:57984 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52985 return OK;
986}
987
988int HttpNetworkTransaction::DoReadHeaders() {
989 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41990 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52991}
992
993int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:11994 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
995 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:38996 if (IsCertificateError(result)) {
997 // We don't handle a certificate error during SSL renegotiation, so we
998 // have to return an error that's not in the certificate error range
999 // (-2xx).
1000 LOG(ERROR) << "Got a server certificate with error " << result
1001 << " during SSL renegotiation";
1002 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1003 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1004 // TODO(wtc): Need a test case for this code path!
1005 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251006 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381007 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501008 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381009 result = HandleCertificateRequest(result);
1010 if (result == OK)
1011 return result;
[email protected]2181ea002009-06-09 01:37:271012 }
1013
bncfacdd852015-01-09 19:22:541014 if (result == ERR_HTTP_1_1_REQUIRED ||
1015 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1016 return HandleHttp11Required(result);
1017 }
1018
[email protected]c871864d72014-03-13 19:56:191019 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1020 // response headers were received, we do the best we can to make sense of it
1021 // and send it back up the stack.
1022 //
1023 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1024 // bizarre for SPDY. Assuming this logic is useful at all.
1025 // TODO(davidben): Bubble the error code up so we do not cache?
1026 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1027 result = OK;
1028
1029 if (result < 0)
1030 return HandleIOError(result);
1031
[email protected]90499482013-06-01 00:39:501032 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521033
[email protected]d58ceea82014-06-04 10:55:541034 // On a 408 response from the server ("Request Timeout") on a stale socket,
1035 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041036 // Headers can be NULL because of http://crbug.com/384554.
1037 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541038 stream_->IsConnectionReused()) {
1039 net_log_.AddEventWithNetErrorCode(
1040 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1041 response_.headers->response_code());
1042 // This will close the socket - it would be weird to try and reuse it, even
1043 // if the server doesn't actually close it.
1044 ResetConnectionAndRequestForResend();
1045 return OK;
1046 }
1047
[email protected]93f8b562012-03-27 01:00:161048 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1049 if (request_->load_flags & LOAD_MAIN_FRAME) {
1050 const int response_code = response_.headers->response_code();
1051 UMA_HISTOGRAM_ENUMERATION(
1052 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1053 }
1054
[email protected]3abacd62012-06-10 20:20:321055 net_log_.AddEvent(
1056 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1057 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391058
[email protected]a7e41312009-12-16 23:18:141059 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571060 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1061 // indicates a buggy server. See:
1062 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1063 if (request_->method == "PUT")
1064 return ERR_METHOD_NOT_SUPPORTED;
1065 }
[email protected]4ddaf2502008-10-23 18:26:191066
[email protected]0877e3d2009-10-17 22:29:571067 // Check for an intermediate 100 Continue response. An origin server is
1068 // allowed to send this response even if we didn't ask for it, so we just
1069 // need to skip over it.
1070 // We treat any other 1xx in this same way (although in practice getting
1071 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441072 // Unless this is a WebSocket request, in which case we pass it on up.
1073 if (response_.headers->response_code() / 100 == 1 &&
1074 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451075 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571076 next_state_ = STATE_READ_HEADERS;
1077 return OK;
1078 }
1079
pkastingcba13292014-11-20 03:35:211080 ProcessAlternateProtocol(session_, *response_.headers.get(),
1081 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421082
[email protected]e772db3f2010-07-12 18:11:131083 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571084 if (rv != OK)
1085 return rv;
1086
Adam Ricecb76ac62015-02-20 05:33:251087 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211088 stream_->GetSSLInfo(&response_.ssl_info);
1089
[email protected]0877e3d2009-10-17 22:29:571090 headers_valid_ = true;
1091 return OK;
initial.commit586acc5fe2008-07-26 22:42:521092}
1093
1094int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501095 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131096 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381097 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521098
1099 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501100 return stream_->ReadResponseBody(
1101 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521102}
1103
1104int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1105 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381106 bool done = false;
1107 if (result <= 0) {
1108 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521109 done = true;
[email protected]8e6441ca2010-08-19 05:56:381110 }
[email protected]9492e4a2010-02-24 00:58:461111
[email protected]8e6441ca2010-08-19 05:56:381112 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:311113 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:381114 // Note: Just because IsResponseBodyComplete is true, we're not
1115 // necessarily "done". We're only "done" when it is the last
1116 // read on this HttpNetworkTransaction, which will be signified
1117 // by a zero-length read.
1118 // TODO(mbelshe): The keepalive property is really a property of
1119 // the stream. No need to compute it here just to pass back
1120 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:471121 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1122 // ResponseHeaders.
1123 if (stream_->CanFindEndOfResponse()) {
1124 HttpResponseHeaders* headers = GetResponseHeaders();
1125 if (headers)
1126 keep_alive = headers->IsKeepAlive();
1127 }
initial.commit586acc5fe2008-07-26 22:42:521128 }
1129
[email protected]8e6441ca2010-08-19 05:56:381130 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521131 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381132 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011133 // Note: we don't reset the stream here. We've closed it, but we still
1134 // need it around so that callers can call methods such as
1135 // GetUploadProgress() and have them be meaningful.
1136 // TODO(mbelshe): This means we closed the stream here, and we close it
1137 // again in ~HttpNetworkTransaction. Clean that up.
1138
[email protected]8e6441ca2010-08-19 05:56:381139 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521140 }
1141
1142 // Clear these to avoid leaving around old state.
1143 read_buf_ = NULL;
1144 read_buf_len_ = 0;
1145
1146 return result;
1147}
1148
[email protected]2d2697f92009-02-18 21:00:321149int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1150 // This method differs from DoReadBody only in the next_state_. So we just
1151 // call DoReadBody and override the next_state_. Perhaps there is a more
1152 // elegant way for these two methods to share code.
1153 int rv = DoReadBody();
1154 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1155 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1156 return rv;
1157}
1158
[email protected]0877e3d2009-10-17 22:29:571159// TODO(wtc): This method and the DoReadBodyComplete method are almost
1160// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321161int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231162 // keep_alive defaults to true because the very reason we're draining the
1163 // response body is to reuse the connection for auth restart.
1164 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321165 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571166 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321167 done = true;
[email protected]68873ba2009-06-04 21:49:231168 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311169 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571170 done = true;
[email protected]2d2697f92009-02-18 21:00:321171 }
1172
1173 if (done) {
1174 DidDrainBodyForAuthRestart(keep_alive);
1175 } else {
1176 // Keep draining.
1177 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1178 }
1179
1180 return OK;
1181}
1182
[email protected]5e363962009-06-19 19:57:011183int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381184 // There are two paths through which the server can request a certificate
1185 // from us. The first is during the initial handshake, the second is
1186 // during SSL renegotiation.
1187 //
1188 // In both cases, we want to close the connection before proceeding.
1189 // We do this for two reasons:
1190 // First, we don't want to keep the connection to the server hung for a
1191 // long time while the user selects a certificate.
1192 // Second, even if we did keep the connection open, NSS has a bug where
1193 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581194 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381195
1196 if (stream_.get()) {
1197 // Since we already have a stream, we're being called as part of SSL
1198 // renegotiation.
1199 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191200 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:381201 stream_->Close(true);
1202 stream_.reset();
1203 }
1204
[email protected]26816882010-10-14 18:03:091205 // The server is asking for a client certificate during the initial
1206 // handshake.
1207 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011208
[email protected]ec229bc92010-11-22 09:51:451209 // If the user selected one of the certificates in client_certs or declined
1210 // to provide one for this server before, use the past decision
1211 // automatically.
1212 scoped_refptr<X509Certificate> client_cert;
1213 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1214 response_.cert_request_info->host_and_port, &client_cert);
1215 if (!found_cached_cert)
1216 return error;
1217
1218 // Check that the certificate selected is still a certificate the server
1219 // is likely to accept, based on the criteria supplied in the
1220 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501221 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261222 const std::vector<std::string>& cert_authorities =
1223 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451224
[email protected]f1958c382013-02-07 00:15:261225 bool cert_still_valid = cert_authorities.empty() ||
1226 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451227 if (!cert_still_valid)
1228 return error;
[email protected]5e363962009-06-19 19:57:011229 }
[email protected]ec229bc92010-11-22 09:51:451230
1231 // TODO(davidben): Add a unit test which covers this path; we need to be
1232 // able to send a legitimate certificate and also bypass/clear the
1233 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141234 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1235 &proxy_ssl_config_ : &server_ssl_config_;
1236 ssl_config->send_client_cert = true;
1237 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451238 next_state_ = STATE_CREATE_STREAM;
1239 // Reset the other member variables.
1240 // Note: this is necessary only with SSL renegotiation.
1241 ResetStateForRestart();
1242 return OK;
[email protected]0b45559b2009-06-12 21:45:111243}
1244
bncfacdd852015-01-09 19:22:541245int HttpNetworkTransaction::HandleHttp11Required(int error) {
1246 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1247 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1248
1249 if (error == ERR_HTTP_1_1_REQUIRED) {
1250 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1251 } else {
1252 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1253 }
1254 ResetConnectionAndRequestForResend();
1255 return OK;
1256}
1257
[email protected]384cc73a2013-12-08 22:41:031258void HttpNetworkTransaction::HandleClientAuthError(int error) {
1259 if (server_ssl_config_.send_client_cert &&
1260 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1261 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411262 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031263 }
1264}
1265
[email protected]bd0b6772011-01-11 19:59:301266// TODO(rch): This does not correctly handle errors when an SSL proxy is
1267// being used, as all of the errors are handled as if they were generated
1268// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381269// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301270int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1271 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031272 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301273
davidbena4c9d062015-04-03 22:34:251274 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1275 // reflect servers require a deprecated cipher rather than merely prefer
1276 // it. This, however, has no security benefit until the ciphers are actually
1277 // removed.
1278 if (!server_ssl_config_.enable_deprecated_cipher_suites &&
1279 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1280 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1281 net_log_.AddEvent(
1282 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1283 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
1284 server_ssl_config_.enable_deprecated_cipher_suites = true;
1285 ResetConnectionAndRequestForResend();
1286 return OK;
1287 }
1288
[email protected]02d1d442013-08-10 13:38:261289 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231290 uint16 version_max = server_ssl_config_.version_max;
1291
initial.commit586acc5fe2008-07-26 22:42:521292 switch (error) {
[email protected]12833302014-07-02 01:57:311293 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301294 case ERR_SSL_PROTOCOL_ERROR:
1295 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231296 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1297 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171298 // This could be a TLS-intolerant server or a server that chose a
1299 // cipher suite defined only for higher protocol versions (such as
1300 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1301 // back to the next lower version and retry.
1302 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1303 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1304 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1305 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1306 // version_max should match the maximum protocol version supported
1307 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231308 version_max--;
[email protected]c57ec022013-11-25 21:30:511309
1310 // Fallback to the lower SSL version.
1311 // While SSL 3.0 fallback should be eliminated because of security
1312 // reasons, there is a high risk of breaking the servers if this is
1313 // done in general.
[email protected]001e547e2013-12-06 16:08:391314 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421315 }
initial.commit586acc5fe2008-07-26 22:42:521316 break;
[email protected]12833302014-07-02 01:57:311317 case ERR_CONNECTION_RESET:
1318 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1319 version_max > server_ssl_config_.version_min) {
1320 // Some network devices that inspect application-layer packets seem to
1321 // inject TCP reset packets to break the connections when they see TLS
1322 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1323 //
1324 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1325 // 1.2. We don't lose much in this fallback because the explicit IV for
1326 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1327 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1328 // support.
1329 //
1330 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1331 // to trigger a version fallback in general, especially the TLS 1.0 ->
1332 // SSL 3.0 fallback, which would drop TLS extensions.
1333 version_max--;
1334 should_fallback = true;
1335 }
1336 break;
[email protected]02d1d442013-08-10 13:38:261337 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1338 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1339 version_max > server_ssl_config_.version_min) {
1340 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1341 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1342 // crbug.com/260358. In order to make the fallback as minimal as
1343 // possible, this fallback is only triggered for >= TLS 1.1.
1344 version_max--;
1345 should_fallback = true;
1346 }
1347 break;
[email protected]a53e4d12013-12-07 16:37:241348 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1349 // The server told us that we should not have fallen back. A buggy server
1350 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1351 // connection. |fallback_error_code_| is initialised to
1352 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1353 error = fallback_error_code_;
1354 break;
[email protected]02d1d442013-08-10 13:38:261355 }
1356
1357 if (should_fallback) {
1358 net_log_.AddEvent(
1359 NetLog::TYPE_SSL_VERSION_FALLBACK,
davidbenf2eaaf92015-05-15 22:18:421360 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1361 server_ssl_failure_state_, server_ssl_config_.version_max,
[email protected]02d1d442013-08-10 13:38:261362 version_max));
[email protected]a53e4d12013-12-07 16:37:241363 fallback_error_code_ = error;
davidbenf2eaaf92015-05-15 22:18:421364 fallback_failure_state_ = server_ssl_failure_state_;
[email protected]02d1d442013-08-10 13:38:261365 server_ssl_config_.version_max = version_max;
1366 server_ssl_config_.version_fallback = true;
1367 ResetConnectionAndRequestForResend();
1368 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521369 }
[email protected]158ac972013-04-19 23:29:231370
initial.commit586acc5fe2008-07-26 22:42:521371 return error;
1372}
1373
[email protected]bd0b6772011-01-11 19:59:301374// This method determines whether it is safe to resend the request after an
1375// IO error. It can only be called in response to request header or body
1376// write errors or response header read errors. It should not be used in
1377// other cases, such as a Connect error.
1378int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031379 // Because the peer may request renegotiation with client authentication at
1380 // any time, check and handle client authentication errors.
1381 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301382
1383 switch (error) {
1384 // If we try to reuse a connection that the server is in the process of
1385 // closing, we may end up successfully writing out our request (or a
1386 // portion of our request) only to find a connection error when we try to
1387 // read from (or finish writing to) the socket.
1388 case ERR_CONNECTION_RESET:
1389 case ERR_CONNECTION_CLOSED:
1390 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511391 // There can be a race between the socket pool checking checking whether a
1392 // socket is still connected, receiving the FIN, and sending/reading data
1393 // on a reused socket. If we receive the FIN between the connectedness
1394 // check and writing/reading from the socket, we may first learn the socket
1395 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1396 // likely happen when trying to retrieve its IP address.
1397 // See http://crbug.com/105824 for more details.
1398 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491399 // If a socket is closed on its initial request, HttpStreamParser returns
1400 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1401 // preconnected but failed to be used before the server timed it out.
1402 case ERR_EMPTY_RESPONSE:
1403 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381404 net_log_.AddEventWithNetErrorCode(
1405 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301406 ResetConnectionAndRequestForResend();
1407 error = OK;
1408 }
1409 break;
[email protected]8753a122011-10-16 08:05:081410 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001411 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271412 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381413 net_log_.AddEventWithNetErrorCode(
1414 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001415 ResetConnectionAndRequestForResend();
1416 error = OK;
1417 break;
[email protected]bd0b6772011-01-11 19:59:301418 }
1419 return error;
1420}
1421
[email protected]c3b35c22008-09-27 03:19:421422void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581423 ResetStateForAuthRestart();
[email protected]b8015c42013-12-24 15:18:191424 if (stream_)
1425 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:581426 stream_.reset();
1427}
1428
1429void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251430 send_start_time_ = base::TimeTicks();
1431 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251432
[email protected]0757e7702009-03-27 04:00:221433 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421434 read_buf_ = NULL;
1435 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571436 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201437 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141438 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381439 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571440}
1441
davidben701ca982015-05-18 21:21:421442void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1443 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1444 return;
1445
1446 const std::string& host = request_->url.host();
brettw17b32ad2015-06-12 23:46:411447 bool is_google = base::EndsWith(host, "google.com", true) &&
davidben701ca982015-05-18 21:21:421448 (host.size() == 10 || host[host.size() - 11] == '.');
1449 if (is_google) {
1450 // Some fraction of successful connections use the fallback, but only due to
1451 // a spurious network failure. To estimate this fraction, compare handshakes
1452 // to Google servers which succeed against those that fail with an
1453 // inappropriate_fallback alert. Google servers are known to implement
1454 // FALLBACK_SCSV, so a spurious network failure while connecting would
1455 // trigger the fallback, successfully connect, but fail with this alert.
1456 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1457 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1458 }
1459
1460 if (result != OK)
1461 return;
1462
davidbenca9d69162015-05-04 20:18:451463 // Note: these values are used in histograms, so new values must be appended.
1464 enum FallbackVersion {
davidbenb937d6c2015-05-14 04:53:421465 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1466 // Obsolete: FALLBACK_SSL3 = 1,
davidbenca9d69162015-05-04 20:18:451467 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1468 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1469 FALLBACK_MAX,
1470 };
1471
1472 FallbackVersion fallback = FALLBACK_NONE;
1473 if (server_ssl_config_.version_fallback) {
1474 switch (server_ssl_config_.version_max) {
davidbenca9d69162015-05-04 20:18:451475 case SSL_PROTOCOL_VERSION_TLS1:
1476 fallback = FALLBACK_TLS1;
1477 break;
1478 case SSL_PROTOCOL_VERSION_TLS1_1:
1479 fallback = FALLBACK_TLS1_1;
1480 break;
1481 default:
1482 NOTREACHED();
1483 }
1484 }
1485 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1486 FALLBACK_MAX);
1487
1488 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1489 // should be impossible to successfully connect to them with the fallback.
1490 // This helps estimate intolerant locally-configured SSL MITMs.
davidben701ca982015-05-18 21:21:421491 if (is_google) {
davidbenca9d69162015-05-04 20:18:451492 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1493 fallback, FALLBACK_MAX);
1494 }
1495
1496 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
1497 server_ssl_config_.enable_deprecated_cipher_suites);
davidbenf2eaaf92015-05-15 22:18:421498
1499 if (server_ssl_config_.version_fallback) {
1500 // Record the error code which triggered the fallback and the state the
1501 // handshake was in.
1502 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1503 -fallback_error_code_);
1504 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1505 fallback_failure_state_, SSL_FAILURE_MAX);
1506 }
davidbenca9d69162015-05-04 20:18:451507}
1508
[email protected]0877e3d2009-10-17 22:29:571509HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501510 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421511}
1512
[email protected]a34f61ee2014-03-18 20:59:491513bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381514 bool connection_is_proven = stream_->IsConnectionReused();
1515 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161516
[email protected]2a5c76b2008-09-25 22:15:161517 // NOTE: we resend a request only if we reused a keep-alive connection.
1518 // This automatically prevents an infinite resend loop because we'll run
1519 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381520 if (connection_is_proven && !has_received_headers)
1521 return true;
1522 return false;
[email protected]1c773ea12009-04-28 19:58:421523}
1524
1525void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381526 if (stream_.get()) {
1527 stream_->Close(true);
1528 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161529 }
1530
[email protected]0877e3d2009-10-17 22:29:571531 // We need to clear request_headers_ because it contains the real request
1532 // headers, but we may need to resend the CONNECT request first to recreate
1533 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201534 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501535 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541536}
1537
[email protected]1c773ea12009-04-28 19:58:421538bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241539 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421540}
license.botbf09a502008-08-24 00:55:551541
[email protected]1c773ea12009-04-28 19:58:421542bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041543 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421544}
1545
[email protected]e772db3f2010-07-12 18:11:131546int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271547 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501548 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421549
[email protected]0877e3d2009-10-17 22:29:571550 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581551 if (status != HTTP_UNAUTHORIZED &&
1552 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421553 return OK;
[email protected]9094b602012-02-27 21:44:581554 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111555 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161556 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1557 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421558
[email protected]9094b602012-02-27 21:44:581559 // This case can trigger when an HTTPS server responds with a "Proxy
1560 // authentication required" status code through a non-authenticating
1561 // proxy.
[email protected]7a67a8152010-11-05 18:31:101562 if (!auth_controllers_[target].get())
1563 return ERR_UNEXPECTED_PROXY_AUTH;
1564
[email protected]a7ea8832010-07-12 17:54:541565 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311566 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1567 net_log_);
[email protected]228404f2010-06-24 04:31:411568 if (auth_controllers_[target]->HaveAuthHandler())
1569 pending_auth_target_ = target;
1570
1571 scoped_refptr<AuthChallengeInfo> auth_info =
1572 auth_controllers_[target]->auth_info();
1573 if (auth_info.get())
1574 response_.auth_challenge = auth_info;
1575
[email protected]228404f2010-06-24 04:31:411576 return rv;
[email protected]f9ee6b52008-11-08 06:46:231577}
1578
[email protected]8e6441ca2010-08-19 05:56:381579bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1580 return auth_controllers_[target].get() &&
1581 auth_controllers_[target]->HaveAuth();
1582}
1583
[email protected]228404f2010-06-24 04:31:411584GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1585 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461586 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411587 if (!proxy_info_.proxy_server().is_valid() ||
1588 proxy_info_.proxy_server().is_direct()) {
1589 return GURL(); // There is no proxy server.
1590 }
[email protected]2df19bb2010-08-25 20:13:461591 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1592 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351593 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461594 }
[email protected]228404f2010-06-24 04:31:411595 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291596 if (ForWebSocketHandshake()) {
1597 const GURL& url = request_->url;
1598 url::Replacements<char> ws_to_http;
1599 if (url.SchemeIs("ws")) {
1600 ws_to_http.SetScheme("http", url::Component(0, 4));
1601 } else {
1602 DCHECK(url.SchemeIs("wss"));
1603 ws_to_http.SetScheme("https", url::Component(0, 5));
1604 }
1605 return url.ReplaceComponents(ws_to_http);
1606 }
[email protected]228404f2010-06-24 04:31:411607 return request_->url;
1608 default:
1609 return GURL();
1610 }
[email protected]c3b35c22008-09-27 03:19:421611}
1612
[email protected]831e4a32013-11-14 02:14:441613bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141614 return websocket_handshake_stream_base_create_helper_ &&
1615 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441616}
1617
[email protected]d8eb84242010-09-25 02:25:061618#define STATE_CASE(s) \
1619 case s: \
1620 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1621 break
[email protected]aef04272010-06-28 18:03:041622
1623std::string HttpNetworkTransaction::DescribeState(State state) {
1624 std::string description;
1625 switch (state) {
[email protected]1826a402014-01-08 15:40:481626 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501627 STATE_CASE(STATE_CREATE_STREAM);
1628 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131629 STATE_CASE(STATE_INIT_REQUEST_BODY);
1630 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511631 STATE_CASE(STATE_BUILD_REQUEST);
1632 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041633 STATE_CASE(STATE_SEND_REQUEST);
1634 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1635 STATE_CASE(STATE_READ_HEADERS);
1636 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041637 STATE_CASE(STATE_READ_BODY);
1638 STATE_CASE(STATE_READ_BODY_COMPLETE);
1639 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1640 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041641 STATE_CASE(STATE_NONE);
1642 default:
[email protected]d8eb84242010-09-25 02:25:061643 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1644 state);
[email protected]aef04272010-06-28 18:03:041645 break;
1646 }
1647 return description;
1648}
1649
1650#undef STATE_CASE
1651
ttuttle1f2d7e92015-04-28 16:17:471652void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1653 DCHECK(stream_request_);
1654
1655 // Since the transaction can restart with auth credentials, it may create a
1656 // stream more than once. Accumulate all of the connection attempts across
1657 // those streams by appending them to the vector:
1658 for (const auto& attempt : stream_request_->connection_attempts())
1659 connection_attempts_.push_back(attempt);
1660}
1661
[email protected]c3b35c22008-09-27 03:19:421662} // namespace net