[email protected] | 49ed6cc | 2012-02-02 08:59:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d102f54 | 2010-06-30 14:51:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include "net/http/http_network_transaction.h" |
| 6 | |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 7 | #include <set> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 13 | #include "base/format_macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/scoped_ptr.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/field_trial.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame^] | 16 | #include "base/metrics/histogram_macros.h" |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 17 | #include "base/metrics/sparse_histogram.h" |
vadimt | 09e7ebe | 2014-10-29 22:10:41 | [diff] [blame] | 18 | #include "base/profiler/scoped_tracker.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 19 | #include "base/stl_util.h" |
[email protected] | 125ef48 | 2013-06-11 18:32:47 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
| 21 | #include "base/strings/string_util.h" |
| 22 | #include "base/strings/stringprintf.h" |
[email protected] | 3d498f7 | 2013-10-28 21:17:40 | [diff] [blame] | 23 | #include "base/time/time.h" |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 24 | #include "base/values.h" |
[email protected] | 68bf915 | 2008-09-25 19:47:30 | [diff] [blame] | 25 | #include "build/build_config.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 26 | #include "net/base/auth.h" |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 27 | #include "net/base/host_port_pair.h" |
[email protected] | 74a85ce | 2009-02-12 00:03:19 | [diff] [blame] | 28 | #include "net/base/io_buffer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 29 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 30 | #include "net/base/load_timing_info.h" |
[email protected] | 597cf6e | 2009-05-29 09:43:26 | [diff] [blame] | 31 | #include "net/base/net_errors.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 32 | #include "net/base/net_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/base/upload_data_stream.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 34 | #include "net/http/http_auth.h" |
| 35 | #include "net/http/http_auth_handler.h" |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 36 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | 8d5a34e | 2009-06-11 21:21:36 | [diff] [blame] | 37 | #include "net/http/http_basic_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/http/http_chunked_decoder.h" |
| 39 | #include "net/http/http_network_session.h" |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 40 | #include "net/http/http_proxy_client_socket.h" |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 41 | #include "net/http/http_proxy_client_socket_pool.h" |
[email protected] | 270c641 | 2010-03-29 22:02:47 | [diff] [blame] | 42 | #include "net/http/http_request_headers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_request_info.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 44 | #include "net/http/http_response_headers.h" |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 45 | #include "net/http/http_response_info.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 46 | #include "net/http/http_server_properties.h" |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 47 | #include "net/http/http_status_code.h" |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 48 | #include "net/http/http_stream.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 49 | #include "net/http/http_stream_factory.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | #include "net/http/http_util.h" |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 51 | #include "net/http/transport_security_state.h" |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 52 | #include "net/http/url_security_manager.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 53 | #include "net/socket/client_socket_factory.h" |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 54 | #include "net/socket/socks_client_socket_pool.h" |
[email protected] | f7984fc6 | 2009-06-22 23:26:44 | [diff] [blame] | 55 | #include "net/socket/ssl_client_socket.h" |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 56 | #include "net/socket/ssl_client_socket_pool.h" |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 57 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 58 | #include "net/spdy/spdy_http_stream.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 59 | #include "net/spdy/spdy_session.h" |
| 60 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 61 | #include "net/ssl/ssl_cert_request_info.h" |
| 62 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 63 | #include "url/gurl.h" |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 64 | #include "url/url_canon.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 65 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | namespace net { |
| 67 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 68 | namespace { |
| 69 | |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 70 | void ProcessAlternateProtocol( |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 71 | HttpNetworkSession* session, |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 72 | const HttpResponseHeaders& headers, |
| 73 | const HostPortPair& http_host_port_pair) { |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 74 | if (!headers.HasHeader(kAlternateProtocolHeader)) |
[email protected] | ff4a649 | 2014-07-08 19:49:38 | [diff] [blame] | 75 | return; |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 76 | |
| 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)) { |
rch | 89c6e10 | 2015-03-18 18:56:52 | [diff] [blame] | 82 | 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] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 89 | session->http_stream_factory()->ProcessAlternateProtocol( |
| 90 | session->http_server_properties(), |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 91 | alternate_protocol_values, |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 92 | http_host_port_pair, |
| 93 | *session); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 94 | } |
| 95 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 96 | scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback( |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 97 | const GURL* url, |
| 98 | int net_error, |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 99 | SSLFailureState ssl_failure_state, |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 100 | uint16 version_before, |
| 101 | uint16 version_after, |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 102 | NetLogCaptureMode /* capture_mode */) { |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 103 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 104 | dict->SetString("host_and_port", GetHostAndPort(*url)); |
| 105 | dict->SetInteger("net_error", net_error); |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 106 | dict->SetInteger("ssl_failure_state", ssl_failure_state); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 107 | dict->SetInteger("version_before", version_before); |
| 108 | dict->SetInteger("version_after", version_after); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 109 | return dict.Pass(); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 110 | } |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 111 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 112 | scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback( |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 113 | const GURL* url, |
| 114 | int net_error, |
| 115 | NetLogCaptureMode /* capture_mode */) { |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 116 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 117 | dict->SetString("host_and_port", GetHostAndPort(*url)); |
| 118 | dict->SetInteger("net_error", net_error); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 119 | return dict.Pass(); |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 122 | } // namespace |
| 123 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 124 | //----------------------------------------------------------------------------- |
| 125 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 126 | HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority, |
| 127 | HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 128 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 129 | io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete, |
| 130 | base::Unretained(this))), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 131 | session_(session), |
| 132 | request_(NULL), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 133 | priority_(priority), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 134 | headers_valid_(false), |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 135 | server_ssl_failure_state_(SSL_FAILURE_NONE), |
[email protected] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 136 | fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK), |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 137 | fallback_failure_state_(SSL_FAILURE_NONE), |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 138 | request_headers_(), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 139 | read_buf_len_(0), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 140 | total_received_bytes_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 141 | next_state_(STATE_NONE), |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 142 | establishing_tunnel_(false), |
| 143 | websocket_handshake_stream_base_create_helper_(NULL) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 144 | session->ssl_config_service()->GetSSLConfig(&server_ssl_config_); |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 145 | session->GetNextProtos(&server_ssl_config_.next_protos); |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 146 | proxy_ssl_config_ = server_ssl_config_; |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 149 | HttpNetworkTransaction::~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] | 3a8d685 | 2011-03-11 23:43:44 | [diff] [blame] | 165 | } else if (stream_->IsSpdyHttpStream()) { |
| 166 | // Doesn't really matter for SpdyHttpStream. Just close it. |
| 167 | stream_->Close(true /* not reusable */); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 168 | } else { |
| 169 | // Otherwise, we try to drain the response body. |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 170 | HttpStream* stream = stream_.release(); |
[email protected] | 3fe8d2f8 | 2013-10-17 08:56:07 | [diff] [blame] | 171 | stream->Drain(session_); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 175 | |
| 176 | if (request_ && request_->upload_data_stream) |
| 177 | request_->upload_data_stream->Reset(); // Invalidate pending callbacks. |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 180 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 181 | const CompletionCallback& callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 182 | const BoundNetLog& net_log) { |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 183 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 184 | request_ = request_info; |
| 185 | |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 186 | if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 187 | server_ssl_config_.rev_checking_enabled = false; |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 188 | proxy_ssl_config_.rev_checking_enabled = false; |
| 189 | } |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 190 | |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 191 | if (request_->load_flags & LOAD_PREFETCH) |
| 192 | response_.unused_since_prefetch = true; |
| 193 | |
[email protected] | 1ea4f46a | 2014-04-22 22:33:56 | [diff] [blame] | 194 | // Channel ID is disabled if privacy mode is enabled for this request. |
[email protected] | 66eeb52e | 2014-05-22 06:53:49 | [diff] [blame] | 195 | if (request_->privacy_mode == PRIVACY_MODE_ENABLED) |
| 196 | server_ssl_config_.channel_id_enabled = false; |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 197 | |
jeremyim | 8d44fadd | 2015-02-10 19:18:15 | [diff] [blame] | 198 | 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] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 204 | next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 205 | int rv = DoLoop(OK); |
| 206 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 207 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 208 | return rv; |
| 209 | } |
| 210 | |
| 211 | int HttpNetworkTransaction::RestartIgnoringLastError( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 212 | const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 213 | DCHECK(!stream_.get()); |
| 214 | DCHECK(!stream_request_.get()); |
| 215 | DCHECK_EQ(STATE_NONE, next_state_); |
| 216 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 217 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 218 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 219 | int rv = DoLoop(OK); |
| 220 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 221 | callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 222 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 225 | int HttpNetworkTransaction::RestartWithCertificate( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 226 | X509Certificate* client_cert, const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 227 | // 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] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 233 | 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] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 237 | session_->ssl_client_auth_cache()->Add( |
| 238 | response_.cert_request_info->host_and_port, client_cert); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 239 | // Reset the other member variables. |
| 240 | // Note: this is necessary only with SSL renegotiation. |
| 241 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 242 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 243 | int rv = DoLoop(OK); |
| 244 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 245 | callback_ = callback; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 246 | return rv; |
| 247 | } |
| 248 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 249 | int HttpNetworkTransaction::RestartWithAuth( |
| 250 | const AuthCredentials& credentials, const CompletionCallback& callback) { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 251 | HttpAuth::Target target = pending_auth_target_; |
| 252 | if (target == HttpAuth::AUTH_NONE) { |
| 253 | NOTREACHED(); |
| 254 | return ERR_UNEXPECTED; |
| 255 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 256 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 257 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 258 | auth_controllers_[target]->ResetAuth(credentials); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 259 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 260 | DCHECK(callback_.is_null()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 261 | |
| 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] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 266 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 267 | DCHECK(stream_request_ != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 268 | auth_controllers_[target] = NULL; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 269 | ResetStateForRestart(); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 270 | rv = stream_request_->RestartTunnelWithProxyAuth(credentials); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 271 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 272 | // 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] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 275 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 276 | rv = DoLoop(OK); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 277 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 278 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 279 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 280 | callback_ = callback; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 281 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 282 | } |
| 283 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 284 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 285 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 286 | DCHECK(!stream_request_.get()); |
| 287 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 288 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 289 | // 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] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 292 | stream_->CanFindEndOfResponse()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 293 | // If the response body hasn't been completely read, we need to drain |
| 294 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 295 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 296 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 297 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 298 | read_buf_len_ = kDrainBodyBufferSize; |
| 299 | return; |
| 300 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 301 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 304 | // 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 | |
| 309 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 310 | DCHECK(!stream_request_.get()); |
| 311 | |
| 312 | if (stream_.get()) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 313 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 314 | HttpStream* new_stream = NULL; |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 315 | if (keep_alive && stream_->IsConnectionReusable()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 316 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 317 | // often enough to be worth the trouble. |
| 318 | stream_->SetConnectionReused(); |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 319 | new_stream = stream_->RenewStreamForAuth(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 320 | } |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 321 | |
| 322 | if (!new_stream) { |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 323 | // 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] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 327 | next_state_ = STATE_CREATE_STREAM; |
| 328 | } else { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 329 | // Renewed streams shouldn't carry over received bytes. |
| 330 | DCHECK_EQ(0, new_stream->GetTotalReceivedBytes()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 331 | next_state_ = STATE_INIT_STREAM; |
| 332 | } |
| 333 | stream_.reset(new_stream); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 334 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 335 | |
| 336 | // Reset the other member variables. |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 337 | ResetStateForAuthRestart(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 340 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 341 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 342 | HaveAuth(pending_auth_target_); |
| 343 | } |
| 344 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 345 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 346 | const CompletionCallback& callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 347 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 348 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 349 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 350 | State next_state = STATE_NONE; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 351 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 352 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 353 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 354 | // We're trying to read the body of the response but we're still trying |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 355 | // to establish an SSL tunnel through an HTTP proxy. We can't read these |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 356 | // 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] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 359 | // 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] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 362 | // See http://crbug.com/8473. |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 363 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https()); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 364 | DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 365 | LOG(WARNING) << "Blocked proxy response with status " |
| 366 | << headers->response_code() << " to CONNECT request for " |
| 367 | << GetHostAndPort(request_->url) << "."; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 368 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 371 | // Are we using SPDY or HTTP? |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 372 | next_state = STATE_READ_BODY; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 373 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 374 | read_buf_ = buf; |
| 375 | read_buf_len_ = buf_len; |
| 376 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 377 | next_state_ = next_state; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 378 | int rv = DoLoop(OK); |
| 379 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 380 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 381 | return rv; |
| 382 | } |
| 383 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 384 | void HttpNetworkTransaction::StopCaching() {} |
| 385 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 386 | bool 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] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 393 | int64 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] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 400 | void HttpNetworkTransaction::DoneReading() {} |
| 401 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 402 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 403 | return &response_; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | LoadState 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] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 410 | case STATE_CREATE_STREAM: |
| 411 | return LOAD_STATE_WAITING_FOR_DELEGATE; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 412 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 413 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 414 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 415 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 416 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 417 | 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] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 427 | UploadProgress HttpNetworkTransaction::GetUploadProgress() const { |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 428 | if (!stream_.get()) |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 429 | return UploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 430 | |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 431 | return stream_->GetUploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 432 | } |
| 433 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 434 | void HttpNetworkTransaction::SetQuicServerInfo( |
| 435 | QuicServerInfo* quic_server_info) {} |
| 436 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 437 | bool 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] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | |
| 450 | void HttpNetworkTransaction::SetPriority(RequestPriority priority) { |
| 451 | priority_ = priority; |
[email protected] | bf82898 | 2013-08-14 18:01:47 | [diff] [blame] | 452 | if (stream_request_) |
| 453 | stream_request_->SetPriority(priority); |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 454 | if (stream_) |
| 455 | stream_->SetPriority(priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 456 | } |
| 457 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 458 | void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 459 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 460 | websocket_handshake_stream_base_create_helper_ = create_helper; |
| 461 | } |
| 462 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 463 | void HttpNetworkTransaction::SetBeforeNetworkStartCallback( |
| 464 | const BeforeNetworkStartCallback& callback) { |
| 465 | before_network_start_callback_ = callback; |
| 466 | } |
| 467 | |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 468 | void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback( |
| 469 | const BeforeProxyHeadersSentCallback& callback) { |
| 470 | before_proxy_headers_sent_callback_ = callback; |
| 471 | } |
| 472 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 473 | int HttpNetworkTransaction::ResumeNetworkStart() { |
| 474 | DCHECK_EQ(next_state_, STATE_CREATE_STREAM); |
| 475 | return DoLoop(OK); |
| 476 | } |
| 477 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 478 | void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config, |
| 479 | const ProxyInfo& used_proxy_info, |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 480 | HttpStream* stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 481 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 482 | DCHECK(stream_request_.get()); |
| 483 | |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 484 | if (stream_) |
| 485 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 486 | stream_.reset(stream); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 487 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 488 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 489 | response_.was_npn_negotiated = stream_request_->was_npn_negotiated(); |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 490 | response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString( |
| 491 | stream_request_->protocol_negotiated()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 492 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
| 493 | response_.was_fetched_via_proxy = !proxy_info_.is_direct(); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 494 | if (response_.was_fetched_via_proxy && !proxy_info_.is_empty()) |
| 495 | response_.proxy_server = proxy_info_.proxy_server().host_port_pair(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 496 | OnIOComplete(OK); |
| 497 | } |
| 498 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 499 | void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady( |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 500 | const SSLConfig& used_ssl_config, |
| 501 | const ProxyInfo& used_proxy_info, |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 502 | WebSocketHandshakeStreamBase* stream) { |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 503 | OnStreamReady(used_ssl_config, used_proxy_info, stream); |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 504 | } |
| 505 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 506 | void HttpNetworkTransaction::OnStreamFailed(int result, |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 507 | const SSLConfig& used_ssl_config, |
| 508 | SSLFailureState ssl_failure_state) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 509 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 510 | DCHECK_NE(OK, result); |
| 511 | DCHECK(stream_request_.get()); |
| 512 | DCHECK(!stream_.get()); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 513 | server_ssl_config_ = used_ssl_config; |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 514 | server_ssl_failure_state_ = ssl_failure_state; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 515 | |
| 516 | OnIOComplete(result); |
| 517 | } |
| 518 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 519 | void HttpNetworkTransaction::OnCertificateError( |
| 520 | int result, |
| 521 | const SSLConfig& used_ssl_config, |
| 522 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 523 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 524 | DCHECK_NE(OK, result); |
| 525 | DCHECK(stream_request_.get()); |
| 526 | DCHECK(!stream_.get()); |
| 527 | |
| 528 | response_.ssl_info = ssl_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 529 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 530 | |
| 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] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 533 | // 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] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 536 | |
| 537 | OnIOComplete(result); |
| 538 | } |
| 539 | |
| 540 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 541 | const HttpResponseInfo& proxy_response, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 542 | const SSLConfig& used_ssl_config, |
| 543 | const ProxyInfo& used_proxy_info, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 544 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 545 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 546 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 547 | |
| 548 | establishing_tunnel_ = true; |
| 549 | response_.headers = proxy_response.headers; |
| 550 | response_.auth_challenge = proxy_response.auth_challenge; |
| 551 | headers_valid_ = true; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 552 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 553 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 554 | |
| 555 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 556 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 557 | |
| 558 | DoCallback(OK); |
| 559 | } |
| 560 | |
| 561 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 562 | const SSLConfig& used_ssl_config, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 563 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 564 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 565 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 566 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 567 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 568 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 569 | } |
| 570 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 571 | void HttpNetworkTransaction::OnHttpsProxyTunnelResponse( |
| 572 | const HttpResponseInfo& response_info, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 573 | const SSLConfig& used_ssl_config, |
| 574 | const ProxyInfo& used_proxy_info, |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 575 | HttpStream* stream) { |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 576 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
| 577 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 578 | CopyConnectionAttemptsFromStreamRequest(); |
| 579 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 580 | headers_valid_ = true; |
| 581 | response_ = response_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 582 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 583 | proxy_info_ = used_proxy_info; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 584 | if (stream_) |
| 585 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 586 | stream_.reset(stream); |
| 587 | stream_request_.reset(); // we're done with the stream request |
| 588 | OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 589 | } |
| 590 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 591 | void HttpNetworkTransaction::GetConnectionAttempts( |
| 592 | ConnectionAttempts* out) const { |
| 593 | *out = connection_attempts_; |
| 594 | } |
| 595 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 596 | bool HttpNetworkTransaction::IsSecureRequest() const { |
lgarron | a774b92 | 2015-05-14 22:56:37 | [diff] [blame] | 597 | return request_->url.SchemeIsCryptographic(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 598 | } |
| 599 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 600 | bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const { |
tbansal | 7cec381 | 2015-02-05 21:25:12 | [diff] [blame] | 601 | return (proxy_info_.is_http() || proxy_info_.is_https() || |
| 602 | proxy_info_.is_quic()) && |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 603 | !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS()); |
| 604 | } |
| 605 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 606 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 607 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 608 | DCHECK(!callback_.is_null()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 609 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 610 | // Since Run may result in Read being called, clear user_callback_ up front. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 611 | CompletionCallback c = callback_; |
| 612 | callback_.Reset(); |
| 613 | c.Run(rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void HttpNetworkTransaction::OnIOComplete(int result) { |
| 617 | int rv = DoLoop(result); |
| 618 | if (rv != ERR_IO_PENDING) |
| 619 | DoCallback(rv); |
| 620 | } |
| 621 | |
| 622 | int 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] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 630 | case STATE_NOTIFY_BEFORE_CREATE_STREAM: |
| 631 | DCHECK_EQ(OK, rv); |
| 632 | rv = DoNotifyBeforeCreateStream(); |
| 633 | break; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 634 | 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] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 641 | 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] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 648 | 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] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 662 | 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] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 669 | case STATE_BUILD_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 670 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 671 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 672 | 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] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 679 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 680 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 681 | case STATE_SEND_REQUEST_COMPLETE: |
| 682 | rv = DoSendRequestComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 683 | net_log_.EndEventWithNetErrorCode( |
| 684 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 685 | break; |
| 686 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 687 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 688 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 689 | rv = DoReadHeaders(); |
| 690 | break; |
| 691 | case STATE_READ_HEADERS_COMPLETE: |
| 692 | rv = DoReadHeadersComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 693 | net_log_.EndEventWithNetErrorCode( |
| 694 | NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 695 | break; |
| 696 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 697 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 698 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 699 | rv = DoReadBody(); |
| 700 | break; |
| 701 | case STATE_READ_BODY_COMPLETE: |
| 702 | rv = DoReadBodyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 703 | net_log_.EndEventWithNetErrorCode( |
| 704 | NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 705 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 706 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 707 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 708 | net_log_.BeginEvent( |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 709 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 710 | rv = DoDrainBodyForAuthRestart(); |
| 711 | break; |
| 712 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 713 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 714 | net_log_.EndEventWithNetErrorCode( |
| 715 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 716 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 717 | default: |
| 718 | NOTREACHED() << "bad state"; |
| 719 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 720 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 721 | } |
| 722 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 723 | |
| 724 | return rv; |
| 725 | } |
| 726 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 727 | int 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] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 737 | int HttpNetworkTransaction::DoCreateStream() { |
pkasting | ec2cdb5 | 2015-05-02 01:19:34 | [diff] [blame] | 738 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed. |
pkasting | a9269aa4 | 2015-04-10 01:42:26 | [diff] [blame] | 739 | tracked_objects::ScopedTracker tracking_profile( |
| 740 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 741 | "424359 HttpNetworkTransaction::DoCreateStream")); |
| 742 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 743 | response_.network_accessed = true; |
| 744 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 745 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 746 | if (ForWebSocketHandshake()) { |
| 747 | stream_request_.reset( |
[email protected] | 0191b51c | 2013-11-18 10:55:23 | [diff] [blame] | 748 | session_->http_stream_factory_for_websocket() |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 749 | ->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] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 767 | DCHECK(stream_request_.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 768 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 769 | } |
| 770 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 771 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 772 | // 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 | |
davidben | 701ca98 | 2015-05-18 21:21:42 | [diff] [blame] | 779 | if (request_->url.SchemeIsCryptographic()) |
| 780 | RecordSSLFallbackMetrics(result); |
| 781 | |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 782 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 783 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 784 | DCHECK(stream_.get()); |
[email protected] | adb0024 | 2010-10-29 03:04:33 | [diff] [blame] | 785 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 786 | result = HandleCertificateRequest(result); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 787 | } 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; |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 791 | } else if (result == ERR_HTTP_1_1_REQUIRED || |
| 792 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 793 | return HandleHttp11Required(result); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 794 | } |
| 795 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 796 | // 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] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 800 | // At this point we are done with the stream_request_. |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 801 | stream_request_.reset(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 802 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 803 | } |
| 804 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 805 | int HttpNetworkTransaction::DoInitStream() { |
| 806 | DCHECK(stream_.get()); |
| 807 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 808 | return stream_->InitializeStream(request_, priority_, net_log_, io_callback_); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 812 | if (result == OK) { |
| 813 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 814 | } else { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 815 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 816 | result = HandleIOError(result); |
| 817 | |
| 818 | // The stream initialization failed, so this stream will never be useful. |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 819 | if (stream_) |
| 820 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 821 | stream_.reset(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | return result; |
| 825 | } |
| 826 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 827 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 828 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 829 | if (!ShouldApplyProxyAuth()) |
| 830 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 831 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 832 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 833 | auth_controllers_[target] = |
| 834 | new HttpAuthController(target, |
| 835 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 836 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 837 | session_->http_auth_handler_factory()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 838 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 839 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 840 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | int 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 | |
| 850 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 851 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 852 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 853 | if (!auth_controllers_[target].get()) { |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 854 | auth_controllers_[target] = |
| 855 | new HttpAuthController(target, |
| 856 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 857 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 858 | session_->http_auth_handler_factory()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 859 | if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY) |
| 860 | auth_controllers_[target]->DisableEmbeddedIdentity(); |
| 861 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 862 | if (!ShouldApplyServerAuth()) |
| 863 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 864 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 865 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 866 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 870 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 871 | if (rv == OK) |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 872 | next_state_ = STATE_INIT_REQUEST_BODY; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 873 | return rv; |
| 874 | } |
| 875 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 876 | void HttpNetworkTransaction::BuildRequestHeaders( |
| 877 | bool using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 878 | request_headers_.SetHeader(HttpRequestHeaders::kHost, |
| 879 | GetHostAndOptionalPort(request_->url)); |
| 880 | |
| 881 | // For compat with HTTP/1.0 servers and proxies: |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 882 | if (using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 883 | request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection, |
| 884 | "keep-alive"); |
| 885 | } else { |
| 886 | request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 887 | } |
| 888 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 889 | // Add a content length header? |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 890 | if (request_->upload_data_stream) { |
| 891 | if (request_->upload_data_stream->is_chunked()) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 892 | request_headers_.SetHeader( |
| 893 | HttpRequestHeaders::kTransferEncoding, "chunked"); |
| 894 | } else { |
| 895 | request_headers_.SetHeader( |
| 896 | HttpRequestHeaders::kContentLength, |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 897 | base::Uint64ToString(request_->upload_data_stream->size())); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 898 | } |
| 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] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 923 | request_headers_.MergeFrom(request_->extra_headers); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 924 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 925 | if (using_http_proxy_without_tunnel && |
| 926 | !before_proxy_headers_sent_callback_.is_null()) |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 927 | before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_); |
| 928 | |
[email protected] | 173f8e2 | 2013-04-10 04:18:20 | [diff] [blame] | 929 | response_.did_use_http_auth = |
| 930 | request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) || |
| 931 | request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 932 | } |
| 933 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 934 | int HttpNetworkTransaction::DoInitRequestBody() { |
| 935 | next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 936 | int rv = OK; |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 937 | if (request_->upload_data_stream) |
| 938 | rv = request_->upload_data_stream->Init(io_callback_); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 939 | return rv; |
| 940 | } |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 941 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 942 | int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) { |
| 943 | if (result == OK) |
| 944 | next_state_ = STATE_BUILD_REQUEST; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 945 | return result; |
| 946 | } |
| 947 | |
| 948 | int HttpNetworkTransaction::DoBuildRequest() { |
| 949 | next_state_ = STATE_BUILD_REQUEST_COMPLETE; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 950 | 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 Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 955 | bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel(); |
| 956 | BuildRequestHeaders(using_http_proxy_without_tunnel); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 957 | } |
| 958 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 959 | return OK; |
| 960 | } |
| 961 | |
| 962 | int HttpNetworkTransaction::DoBuildRequestComplete(int result) { |
[email protected] | 9f489d7 | 2011-04-04 23:29:24 | [diff] [blame] | 963 | if (result == OK) |
| 964 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 965 | return result; |
| 966 | } |
| 967 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 968 | int HttpNetworkTransaction::DoSendRequest() { |
pkasting | ec2cdb5 | 2015-05-02 01:19:34 | [diff] [blame] | 969 | // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed. |
pkasting | a9269aa4 | 2015-04-10 01:42:26 | [diff] [blame] | 970 | tracked_objects::ScopedTracker tracking_profile( |
| 971 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 972 | "424359 HttpNetworkTransaction::DoSendRequest")); |
| 973 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 974 | send_start_time_ = base::TimeTicks::Now(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 975 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 976 | |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 977 | return stream_->SendRequest(request_headers_, &response_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 978 | } |
| 979 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 980 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 981 | send_end_time_ = base::TimeTicks::Now(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 982 | if (result < 0) |
| 983 | return HandleIOError(result); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 984 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 985 | return OK; |
| 986 | } |
| 987 | |
| 988 | int HttpNetworkTransaction::DoReadHeaders() { |
| 989 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 990 | return stream_->ReadResponseHeaders(io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 994 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 995 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 996 | 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 Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 1006 | DCHECK(IsSecureRequest()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1007 | response_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1008 | stream_->GetSSLCertRequestInfo(response_.cert_request_info.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1009 | result = HandleCertificateRequest(result); |
| 1010 | if (result == OK) |
| 1011 | return result; |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 1012 | } |
| 1013 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1014 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 1015 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 1016 | return HandleHttp11Required(result); |
| 1017 | } |
| 1018 | |
[email protected] | c871864d7 | 2014-03-13 19:56:19 | [diff] [blame] | 1019 | // 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] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1032 | DCHECK(response_.headers.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1033 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1034 | // On a 408 response from the server ("Request Timeout") on a stale socket, |
| 1035 | // retry the request. |
[email protected] | 0aff0d8 | 2014-06-14 08:49:04 | [diff] [blame] | 1036 | // Headers can be NULL because of http://crbug.com/384554. |
| 1037 | if (response_.headers.get() && response_.headers->response_code() == 408 && |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1038 | 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] | 93f8b56 | 2012-03-27 01:00:16 | [diff] [blame] | 1048 | // 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] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 1055 | net_log_.AddEvent( |
| 1056 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 1057 | base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers)); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1058 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1059 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1060 | // 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] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1066 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1067 | // 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] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1072 | // 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] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1075 | response_.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1076 | next_state_ = STATE_READ_HEADERS; |
| 1077 | return OK; |
| 1078 | } |
| 1079 | |
pkasting | cba1329 | 2014-11-20 03:35:21 | [diff] [blame] | 1080 | ProcessAlternateProtocol(session_, *response_.headers.get(), |
| 1081 | HostPortPair::FromURL(request_->url)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 1082 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1083 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1084 | if (rv != OK) |
| 1085 | return rv; |
| 1086 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 1087 | if (IsSecureRequest()) |
[email protected] | 8536ef5 | 2010-09-30 16:18:21 | [diff] [blame] | 1088 | stream_->GetSSLInfo(&response_.ssl_info); |
| 1089 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1090 | headers_valid_ = true; |
| 1091 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | int HttpNetworkTransaction::DoReadBody() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1095 | DCHECK(read_buf_.get()); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 1096 | DCHECK_GT(read_buf_len_, 0); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1097 | DCHECK(stream_ != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1098 | |
| 1099 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1100 | return stream_->ReadResponseBody( |
| 1101 | read_buf_.get(), read_buf_len_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 1105 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1106 | bool done = false; |
| 1107 | if (result <= 0) { |
| 1108 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1109 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1110 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1111 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1112 | bool keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1113 | if (stream_->IsResponseBodyComplete()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1114 | // 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] | 5c6908e | 2012-08-06 18:53:47 | [diff] [blame] | 1121 | // 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.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1128 | } |
| 1129 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1130 | // Clean up connection if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1131 | if (done) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1132 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 1133 | // 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] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1139 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1140 | } |
| 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] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1149 | int 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] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1159 | // 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] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1161 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1162 | // 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] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1165 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1166 | // Error or closed connection while reading the socket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1167 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1168 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1169 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1170 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1171 | } |
| 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] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1183 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1184 | // 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] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1194 | DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1195 | |
| 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] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1200 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1201 | stream_->Close(true); |
| 1202 | stream_.reset(); |
| 1203 | } |
| 1204 | |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 1205 | // The server is asking for a client certificate during the initial |
| 1206 | // handshake. |
| 1207 | stream_request_.reset(); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1208 | |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1209 | // 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] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1221 | if (client_cert.get()) { |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1222 | const std::vector<std::string>& cert_authorities = |
| 1223 | response_.cert_request_info->cert_authorities; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1224 | |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1225 | bool cert_still_valid = cert_authorities.empty() || |
| 1226 | client_cert->IsIssuedByEncoded(cert_authorities); |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1227 | if (!cert_still_valid) |
| 1228 | return error; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1229 | } |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1230 | |
| 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] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 1234 | 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] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1238 | 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] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1243 | } |
| 1244 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1245 | int 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] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1258 | void 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] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 1262 | HostPortPair::FromURL(request_->url)); |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1266 | // 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] | 1c53a1f | 2011-01-13 00:36:38 | [diff] [blame] | 1269 | // generated by the SSL proxy. http://crbug.com/69329 |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1270 | int HttpNetworkTransaction::HandleSSLHandshakeError(int error) { |
| 1271 | DCHECK(request_); |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1272 | HandleClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1273 | |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 1274 | // 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] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1289 | bool should_fallback = false; |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1290 | uint16 version_max = server_ssl_config_.version_max; |
| 1291 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1292 | switch (error) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 1293 | case ERR_CONNECTION_CLOSED: |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1294 | case ERR_SSL_PROTOCOL_ERROR: |
| 1295 | case ERR_SSL_VERSION_OR_CIPHER_MISMATCH: |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1296 | if (version_max >= SSL_PROTOCOL_VERSION_TLS1 && |
| 1297 | version_max > server_ssl_config_.version_min) { |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 1298 | // 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] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1308 | version_max--; |
[email protected] | c57ec02 | 2013-11-25 21:30:51 | [diff] [blame] | 1309 | |
| 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] | 001e547e | 2013-12-06 16:08:39 | [diff] [blame] | 1314 | should_fallback = true; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1315 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1316 | break; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 1317 | 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] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1337 | 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] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 1348 | 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] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | if (should_fallback) { |
| 1358 | net_log_.AddEvent( |
| 1359 | NetLog::TYPE_SSL_VERSION_FALLBACK, |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 1360 | base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error, |
| 1361 | server_ssl_failure_state_, server_ssl_config_.version_max, |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1362 | version_max)); |
[email protected] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 1363 | fallback_error_code_ = error; |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 1364 | fallback_failure_state_ = server_ssl_failure_state_; |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1365 | server_ssl_config_.version_max = version_max; |
| 1366 | server_ssl_config_.version_fallback = true; |
| 1367 | ResetConnectionAndRequestForResend(); |
| 1368 | error = OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1369 | } |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1370 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1371 | return error; |
| 1372 | } |
| 1373 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1374 | // 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. |
| 1378 | int HttpNetworkTransaction::HandleIOError(int error) { |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1379 | // Because the peer may request renegotiation with client authentication at |
| 1380 | // any time, check and handle client authentication errors. |
| 1381 | HandleClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1382 | |
| 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] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1391 | // 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] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1399 | // 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] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1404 | net_log_.AddEventWithNetErrorCode( |
| 1405 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1406 | ResetConnectionAndRequestForResend(); |
| 1407 | error = OK; |
| 1408 | } |
| 1409 | break; |
[email protected] | 8753a12 | 2011-10-16 08:05:08 | [diff] [blame] | 1410 | case ERR_SPDY_PING_FAILED: |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1411 | case ERR_SPDY_SERVER_REFUSED_STREAM: |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 1412 | case ERR_QUIC_HANDSHAKE_FAILED: |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1413 | net_log_.AddEventWithNetErrorCode( |
| 1414 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1415 | ResetConnectionAndRequestForResend(); |
| 1416 | error = OK; |
| 1417 | break; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1418 | } |
| 1419 | return error; |
| 1420 | } |
| 1421 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1422 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1423 | ResetStateForAuthRestart(); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1424 | if (stream_) |
| 1425 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1426 | stream_.reset(); |
| 1427 | } |
| 1428 | |
| 1429 | void HttpNetworkTransaction::ResetStateForAuthRestart() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1430 | send_start_time_ = base::TimeTicks(); |
| 1431 | send_end_time_ = base::TimeTicks(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1432 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1433 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1434 | read_buf_ = NULL; |
| 1435 | read_buf_len_ = 0; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1436 | headers_valid_ = false; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1437 | request_headers_.Clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1438 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1439 | establishing_tunnel_ = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1440 | } |
| 1441 | |
davidben | 701ca98 | 2015-05-18 21:21:42 | [diff] [blame] | 1442 | void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) { |
| 1443 | if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK) |
| 1444 | return; |
| 1445 | |
| 1446 | const std::string& host = request_->url.host(); |
brettw | 17b32ad | 2015-06-12 23:46:41 | [diff] [blame] | 1447 | bool is_google = base::EndsWith(host, "google.com", true) && |
davidben | 701ca98 | 2015-05-18 21:21:42 | [diff] [blame] | 1448 | (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 | |
davidben | ca9d6916 | 2015-05-04 20:18:45 | [diff] [blame] | 1463 | // Note: these values are used in histograms, so new values must be appended. |
| 1464 | enum FallbackVersion { |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 1465 | FALLBACK_NONE = 0, // SSL version fallback did not occur. |
| 1466 | // Obsolete: FALLBACK_SSL3 = 1, |
davidben | ca9d6916 | 2015-05-04 20:18:45 | [diff] [blame] | 1467 | 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) { |
davidben | ca9d6916 | 2015-05-04 20:18:45 | [diff] [blame] | 1475 | 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. |
davidben | 701ca98 | 2015-05-18 21:21:42 | [diff] [blame] | 1491 | if (is_google) { |
davidben | ca9d6916 | 2015-05-04 20:18:45 | [diff] [blame] | 1492 | 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); |
davidben | f2eaaf9 | 2015-05-15 22:18:42 | [diff] [blame] | 1498 | |
| 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 | } |
davidben | ca9d6916 | 2015-05-04 20:18:45 | [diff] [blame] | 1507 | } |
| 1508 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1509 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1510 | return response_.headers.get(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1511 | } |
| 1512 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1513 | bool HttpNetworkTransaction::ShouldResendRequest() const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1514 | bool connection_is_proven = stream_->IsConnectionReused(); |
| 1515 | bool has_received_headers = GetResponseHeaders() != NULL; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1516 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1517 | // 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] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1520 | if (connection_is_proven && !has_received_headers) |
| 1521 | return true; |
| 1522 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1526 | if (stream_.get()) { |
| 1527 | stream_->Close(true); |
| 1528 | stream_.reset(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1529 | } |
| 1530 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1531 | // 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] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1534 | request_headers_.Clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1535 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1536 | } |
| 1537 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1538 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1539 | return UsingHttpProxyWithoutTunnel(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1540 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1541 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1542 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1543 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1544 | } |
| 1545 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1546 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1547 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1548 | DCHECK(headers.get()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1549 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1550 | int status = headers->response_code(); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1551 | if (status != HTTP_UNAUTHORIZED && |
| 1552 | status != HTTP_PROXY_AUTHENTICATION_REQUIRED) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1553 | return OK; |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1554 | HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1555 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1556 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1557 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1558 | |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1559 | // 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] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1562 | if (!auth_controllers_[target].get()) |
| 1563 | return ERR_UNEXPECTED_PROXY_AUTH; |
| 1564 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1565 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1566 | headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
| 1567 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1568 | 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] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1576 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1577 | } |
| 1578 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1579 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1580 | return auth_controllers_[target].get() && |
| 1581 | auth_controllers_[target]->HaveAuth(); |
| 1582 | } |
| 1583 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1584 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1585 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1586 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1587 | 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] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1591 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1592 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1593 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1594 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1595 | case HttpAuth::AUTH_SERVER: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1596 | 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] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1607 | return request_->url; |
| 1608 | default: |
| 1609 | return GURL(); |
| 1610 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1611 | } |
| 1612 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1613 | bool HttpNetworkTransaction::ForWebSocketHandshake() const { |
[email protected] | 23d3e02 | 2013-11-23 20:38:14 | [diff] [blame] | 1614 | return websocket_handshake_stream_base_create_helper_ && |
| 1615 | request_->url.SchemeIsWSOrWSS(); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1616 | } |
| 1617 | |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1618 | #define STATE_CASE(s) \ |
| 1619 | case s: \ |
| 1620 | description = base::StringPrintf("%s (0x%08X)", #s, s); \ |
| 1621 | break |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1622 | |
| 1623 | std::string HttpNetworkTransaction::DescribeState(State state) { |
| 1624 | std::string description; |
| 1625 | switch (state) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 1626 | STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1627 | STATE_CASE(STATE_CREATE_STREAM); |
| 1628 | STATE_CASE(STATE_CREATE_STREAM_COMPLETE); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1629 | STATE_CASE(STATE_INIT_REQUEST_BODY); |
| 1630 | STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1631 | STATE_CASE(STATE_BUILD_REQUEST); |
| 1632 | STATE_CASE(STATE_BUILD_REQUEST_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1633 | 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] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1637 | 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] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1641 | STATE_CASE(STATE_NONE); |
| 1642 | default: |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1643 | description = base::StringPrintf("Unknown state 0x%08X (%u)", state, |
| 1644 | state); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1645 | break; |
| 1646 | } |
| 1647 | return description; |
| 1648 | } |
| 1649 | |
| 1650 | #undef STATE_CASE |
| 1651 | |
ttuttle | 1f2d7e9 | 2015-04-28 16:17:47 | [diff] [blame] | 1652 | void 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] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1662 | } // namespace net |