[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" |
| 16 | #include "base/metrics/histogram.h" |
| 17 | #include "base/metrics/stats_counters.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] | e00bfd57 | 2014-04-30 08:06:43 | [diff] [blame] | 58 | #include "net/spdy/hpack_huffman_aggregator.h" |
[email protected] | 65d56aa | 2010-06-14 04:13:40 | [diff] [blame] | 59 | #include "net/spdy/spdy_http_stream.h" |
[email protected] | dab9c7d | 2010-02-06 21:44:32 | [diff] [blame] | 60 | #include "net/spdy/spdy_session.h" |
| 61 | #include "net/spdy/spdy_session_pool.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 62 | #include "net/ssl/ssl_cert_request_info.h" |
| 63 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 64 | #include "url/gurl.h" |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 65 | #include "url/url_canon.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 67 | namespace net { |
| 68 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 69 | namespace { |
| 70 | |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 71 | void ProcessAlternateProtocol( |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 72 | HttpNetworkSession* session, |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 73 | const HttpResponseHeaders& headers, |
| 74 | const HostPortPair& http_host_port_pair) { |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 75 | if (!headers.HasHeader(kAlternateProtocolHeader)) |
[email protected] | ff4a649 | 2014-07-08 19:49:38 | [diff] [blame] | 76 | return; |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 77 | |
| 78 | std::vector<std::string> alternate_protocol_values; |
| 79 | void* iter = NULL; |
| 80 | std::string alternate_protocol_str; |
| 81 | while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader, |
| 82 | &alternate_protocol_str)) { |
| 83 | alternate_protocol_values.push_back(alternate_protocol_str); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 86 | session->http_stream_factory()->ProcessAlternateProtocol( |
| 87 | session->http_server_properties(), |
[email protected] | 287d941 | 2014-07-08 23:01:00 | [diff] [blame] | 88 | alternate_protocol_values, |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 89 | http_host_port_pair, |
| 90 | *session); |
[email protected] | f45c1ee | 2010-08-03 00:54:30 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 93 | base::Value* NetLogSSLVersionFallbackCallback( |
| 94 | const GURL* url, |
| 95 | int net_error, |
| 96 | uint16 version_before, |
| 97 | uint16 version_after, |
| 98 | NetLog::LogLevel /* log_level */) { |
| 99 | base::DictionaryValue* dict = new base::DictionaryValue(); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 100 | dict->SetString("host_and_port", GetHostAndPort(*url)); |
| 101 | dict->SetInteger("net_error", net_error); |
| 102 | dict->SetInteger("version_before", version_before); |
| 103 | dict->SetInteger("version_after", version_after); |
| 104 | return dict; |
| 105 | } |
[email protected] | db74b010 | 2012-05-31 19:55:53 | [diff] [blame] | 106 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 107 | } // namespace |
| 108 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 109 | //----------------------------------------------------------------------------- |
| 110 | |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 111 | HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority, |
| 112 | HttpNetworkSession* session) |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 113 | : pending_auth_target_(HttpAuth::AUTH_NONE), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 114 | io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete, |
| 115 | base::Unretained(this))), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 116 | session_(session), |
| 117 | request_(NULL), |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 118 | priority_(priority), |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 119 | headers_valid_(false), |
[email protected] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 120 | fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK), |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 121 | request_headers_(), |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 122 | read_buf_len_(0), |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 123 | total_received_bytes_(0), |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 124 | next_state_(STATE_NONE), |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 125 | establishing_tunnel_(false), |
| 126 | websocket_handshake_stream_base_create_helper_(NULL) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 127 | session->ssl_config_service()->GetSSLConfig(&server_ssl_config_); |
[email protected] | d759912 | 2014-05-24 03:37:23 | [diff] [blame] | 128 | session->GetNextProtos(&server_ssl_config_.next_protos); |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 129 | proxy_ssl_config_ = server_ssl_config_; |
[email protected] | 3ce7df0f | 2010-03-03 00:30:50 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 132 | HttpNetworkTransaction::~HttpNetworkTransaction() { |
| 133 | if (stream_.get()) { |
| 134 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 135 | // TODO(mbelshe): The stream_ should be able to compute whether or not the |
| 136 | // stream should be kept alive. No reason to compute here |
| 137 | // and pass it in. |
| 138 | bool try_to_keep_alive = |
| 139 | next_state_ == STATE_NONE && |
| 140 | stream_->CanFindEndOfResponse() && |
| 141 | (!headers || headers->IsKeepAlive()); |
| 142 | if (!try_to_keep_alive) { |
| 143 | stream_->Close(true /* not reusable */); |
| 144 | } else { |
| 145 | if (stream_->IsResponseBodyComplete()) { |
| 146 | // If the response body is complete, we can just reuse the socket. |
| 147 | stream_->Close(false /* reusable */); |
[email protected] | 3a8d685 | 2011-03-11 23:43:44 | [diff] [blame] | 148 | } else if (stream_->IsSpdyHttpStream()) { |
| 149 | // Doesn't really matter for SpdyHttpStream. Just close it. |
| 150 | stream_->Close(true /* not reusable */); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 151 | } else { |
| 152 | // Otherwise, we try to drain the response body. |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 153 | HttpStream* stream = stream_.release(); |
[email protected] | 3fe8d2f8 | 2013-10-17 08:56:07 | [diff] [blame] | 154 | stream->Drain(session_); |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
[email protected] | 02cad5d | 2013-10-02 08:14:03 | [diff] [blame] | 158 | |
| 159 | if (request_ && request_->upload_data_stream) |
| 160 | request_->upload_data_stream->Reset(); // Invalidate pending callbacks. |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 163 | int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 164 | const CompletionCallback& callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 165 | const BoundNetLog& net_log) { |
[email protected] | 5e2e6c77d1 | 2009-12-24 21:57:16 | [diff] [blame] | 166 | SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count"); |
[email protected] | 5d0153c51 | 2009-01-12 19:08:36 | [diff] [blame] | 167 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 168 | net_log_ = net_log; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 169 | request_ = request_info; |
| 170 | |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 171 | if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) { |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 172 | server_ssl_config_.rev_checking_enabled = false; |
[email protected] | 99ffa5a | 2011-10-06 04:20:19 | [diff] [blame] | 173 | proxy_ssl_config_.rev_checking_enabled = false; |
| 174 | } |
[email protected] | 6fbac16 | 2011-06-20 00:29:04 | [diff] [blame] | 175 | |
jkarlin | fb1d517 | 2015-01-12 14:10:29 | [diff] [blame] | 176 | if (request_->load_flags & LOAD_PREFETCH) |
| 177 | response_.unused_since_prefetch = true; |
| 178 | |
[email protected] | 1ea4f46a | 2014-04-22 22:33:56 | [diff] [blame] | 179 | // Channel ID is disabled if privacy mode is enabled for this request. |
[email protected] | 66eeb52e | 2014-05-22 06:53:49 | [diff] [blame] | 180 | if (request_->privacy_mode == PRIVACY_MODE_ENABLED) |
| 181 | server_ssl_config_.channel_id_enabled = false; |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 182 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 183 | next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 184 | int rv = DoLoop(OK); |
| 185 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 186 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 187 | return rv; |
| 188 | } |
| 189 | |
| 190 | int HttpNetworkTransaction::RestartIgnoringLastError( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 191 | const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 192 | DCHECK(!stream_.get()); |
| 193 | DCHECK(!stream_request_.get()); |
| 194 | DCHECK_EQ(STATE_NONE, next_state_); |
| 195 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 196 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 197 | |
[email protected] | ccb40e5 | 2008-09-17 20:54:40 | [diff] [blame] | 198 | int rv = DoLoop(OK); |
| 199 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 200 | callback_ = callback; |
[email protected] | aaead50 | 2008-10-15 00:20:11 | [diff] [blame] | 201 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 204 | int HttpNetworkTransaction::RestartWithCertificate( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 205 | X509Certificate* client_cert, const CompletionCallback& callback) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 206 | // In HandleCertificateRequest(), we always tear down existing stream |
| 207 | // requests to force a new connection. So we shouldn't have one here. |
| 208 | DCHECK(!stream_request_.get()); |
| 209 | DCHECK(!stream_.get()); |
| 210 | DCHECK_EQ(STATE_NONE, next_state_); |
| 211 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 212 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 213 | &proxy_ssl_config_ : &server_ssl_config_; |
| 214 | ssl_config->send_client_cert = true; |
| 215 | ssl_config->client_cert = client_cert; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 216 | session_->ssl_client_auth_cache()->Add( |
| 217 | response_.cert_request_info->host_and_port, client_cert); |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 218 | // Reset the other member variables. |
| 219 | // Note: this is necessary only with SSL renegotiation. |
| 220 | ResetStateForRestart(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 221 | next_state_ = STATE_CREATE_STREAM; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 222 | int rv = DoLoop(OK); |
| 223 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 224 | callback_ = callback; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 225 | return rv; |
| 226 | } |
| 227 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 228 | int HttpNetworkTransaction::RestartWithAuth( |
| 229 | const AuthCredentials& credentials, const CompletionCallback& callback) { |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 230 | HttpAuth::Target target = pending_auth_target_; |
| 231 | if (target == HttpAuth::AUTH_NONE) { |
| 232 | NOTREACHED(); |
| 233 | return ERR_UNEXPECTED; |
| 234 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 235 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 236 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 237 | auth_controllers_[target]->ResetAuth(credentials); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 238 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 239 | DCHECK(callback_.is_null()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 240 | |
| 241 | int rv = OK; |
| 242 | if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) { |
| 243 | // In this case, we've gathered credentials for use with proxy |
| 244 | // authentication of a tunnel. |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 245 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 246 | DCHECK(stream_request_ != NULL); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 247 | auth_controllers_[target] = NULL; |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 248 | ResetStateForRestart(); |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 249 | rv = stream_request_->RestartTunnelWithProxyAuth(credentials); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 250 | } else { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 251 | // In this case, we've gathered credentials for the server or the proxy |
| 252 | // but it is not during the tunneling phase. |
| 253 | DCHECK(stream_request_ == NULL); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 254 | PrepareForAuthRestart(target); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 255 | rv = DoLoop(OK); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 256 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 257 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 258 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 259 | callback_ = callback; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 260 | return rv; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 263 | void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) { |
| 264 | DCHECK(HaveAuth(target)); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 265 | DCHECK(!stream_request_.get()); |
| 266 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 267 | bool keep_alive = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 268 | // Even if the server says the connection is keep-alive, we have to be |
| 269 | // able to find the end of each response in order to reuse the connection. |
| 270 | if (GetResponseHeaders()->IsKeepAlive() && |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 271 | stream_->CanFindEndOfResponse()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 272 | // If the response body hasn't been completely read, we need to drain |
| 273 | // it first. |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 274 | if (!stream_->IsResponseBodyComplete()) { |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 275 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 276 | read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 277 | read_buf_len_ = kDrainBodyBufferSize; |
| 278 | return; |
| 279 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 280 | keep_alive = true; |
[email protected] | 37832c6d | 2009-06-05 19:44:09 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 283 | // We don't need to drain the response body, so we act as if we had drained |
| 284 | // the response body. |
| 285 | DidDrainBodyForAuthRestart(keep_alive); |
| 286 | } |
| 287 | |
| 288 | void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 289 | DCHECK(!stream_request_.get()); |
| 290 | |
| 291 | if (stream_.get()) { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 292 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 293 | HttpStream* new_stream = NULL; |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 294 | if (keep_alive && stream_->IsConnectionReusable()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 295 | // We should call connection_->set_idle_time(), but this doesn't occur |
| 296 | // often enough to be worth the trouble. |
| 297 | stream_->SetConnectionReused(); |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 298 | new_stream = stream_->RenewStreamForAuth(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 299 | } |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 300 | |
| 301 | if (!new_stream) { |
[email protected] | 2d0a4f9 | 2011-05-05 16:38:46 | [diff] [blame] | 302 | // Close the stream and mark it as not_reusable. Even in the |
| 303 | // keep_alive case, we've determined that the stream_ is not |
| 304 | // reusable if new_stream is NULL. |
| 305 | stream_->Close(true); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 306 | next_state_ = STATE_CREATE_STREAM; |
| 307 | } else { |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 308 | // Renewed streams shouldn't carry over received bytes. |
| 309 | DCHECK_EQ(0, new_stream->GetTotalReceivedBytes()); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 310 | next_state_ = STATE_INIT_STREAM; |
| 311 | } |
| 312 | stream_.reset(new_stream); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 313 | } |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 314 | |
| 315 | // Reset the other member variables. |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 316 | ResetStateForAuthRestart(); |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 319 | bool HttpNetworkTransaction::IsReadyToRestartForAuth() { |
| 320 | return pending_auth_target_ != HttpAuth::AUTH_NONE && |
| 321 | HaveAuth(pending_auth_target_); |
| 322 | } |
| 323 | |
[email protected] | 9dea9e1f | 2009-01-29 00:30:47 | [diff] [blame] | 324 | int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 325 | const CompletionCallback& callback) { |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 326 | DCHECK(buf); |
[email protected] | e0c27be | 2009-07-15 13:09:35 | [diff] [blame] | 327 | DCHECK_LT(0, buf_len); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 328 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 329 | State next_state = STATE_NONE; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 330 | |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 331 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 332 | if (headers_valid_ && headers.get() && stream_request_.get()) { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 333 | // 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] | 334 | // 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] | 335 | // bytes when establishing a tunnel because they might be controlled by |
| 336 | // an active network attacker. We don't worry about this for HTTP |
| 337 | // because an active network attacker can already control HTTP sessions. |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 338 | // We reach this case when the user cancels a 407 proxy auth prompt. We |
| 339 | // also don't worry about this for an HTTPS Proxy, because the |
| 340 | // communication with the proxy is secure. |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 341 | // See http://crbug.com/8473. |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 342 | DCHECK(proxy_info_.is_http() || proxy_info_.is_https()); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 343 | DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED); |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 344 | LOG(WARNING) << "Blocked proxy response with status " |
| 345 | << headers->response_code() << " to CONNECT request for " |
| 346 | << GetHostAndPort(request_->url) << "."; |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 347 | return ERR_TUNNEL_CONNECTION_FAILED; |
[email protected] | a8e9b16 | 2009-03-12 00:06:44 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 350 | // Are we using SPDY or HTTP? |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 351 | next_state = STATE_READ_BODY; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 352 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 353 | read_buf_ = buf; |
| 354 | read_buf_len_ = buf_len; |
| 355 | |
[email protected] | 1f14a91 | 2009-12-21 20:32:44 | [diff] [blame] | 356 | next_state_ = next_state; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 357 | int rv = DoLoop(OK); |
| 358 | if (rv == ERR_IO_PENDING) |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 359 | callback_ = callback; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 360 | return rv; |
| 361 | } |
| 362 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 363 | void HttpNetworkTransaction::StopCaching() {} |
| 364 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 365 | bool HttpNetworkTransaction::GetFullRequestHeaders( |
| 366 | HttpRequestHeaders* headers) const { |
| 367 | // TODO(ttuttle): Make sure we've populated request_headers_. |
| 368 | *headers = request_headers_; |
| 369 | return true; |
| 370 | } |
| 371 | |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 372 | int64 HttpNetworkTransaction::GetTotalReceivedBytes() const { |
| 373 | int64 total_received_bytes = total_received_bytes_; |
| 374 | if (stream_) |
| 375 | total_received_bytes += stream_->GetTotalReceivedBytes(); |
| 376 | return total_received_bytes; |
| 377 | } |
| 378 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 379 | void HttpNetworkTransaction::DoneReading() {} |
| 380 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 381 | const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 382 | return ((headers_valid_ && response_.headers.get()) || |
| 383 | response_.ssl_info.cert.get() || response_.cert_request_info.get()) |
| 384 | ? &response_ |
| 385 | : NULL; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | LoadState HttpNetworkTransaction::GetLoadState() const { |
| 389 | // TODO(wtc): Define a new LoadState value for the |
| 390 | // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request. |
| 391 | switch (next_state_) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 392 | case STATE_CREATE_STREAM: |
| 393 | return LOAD_STATE_WAITING_FOR_DELEGATE; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 394 | case STATE_CREATE_STREAM_COMPLETE: |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 395 | return stream_request_->GetLoadState(); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 396 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 397 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 398 | case STATE_SEND_REQUEST_COMPLETE: |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 399 | return LOAD_STATE_SENDING_REQUEST; |
| 400 | case STATE_READ_HEADERS_COMPLETE: |
| 401 | return LOAD_STATE_WAITING_FOR_RESPONSE; |
| 402 | case STATE_READ_BODY_COMPLETE: |
| 403 | return LOAD_STATE_READING_RESPONSE; |
| 404 | default: |
| 405 | return LOAD_STATE_IDLE; |
| 406 | } |
| 407 | } |
| 408 | |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 409 | UploadProgress HttpNetworkTransaction::GetUploadProgress() const { |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 410 | if (!stream_.get()) |
[email protected] | 196d18a | 2012-08-30 03:47:31 | [diff] [blame] | 411 | return UploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 412 | |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 413 | return stream_->GetUploadProgress(); |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | 8cd06c0 | 2014-01-25 07:50:14 | [diff] [blame] | 416 | void HttpNetworkTransaction::SetQuicServerInfo( |
| 417 | QuicServerInfo* quic_server_info) {} |
| 418 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 419 | bool HttpNetworkTransaction::GetLoadTimingInfo( |
| 420 | LoadTimingInfo* load_timing_info) const { |
| 421 | if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info)) |
| 422 | return false; |
| 423 | |
| 424 | load_timing_info->proxy_resolve_start = |
| 425 | proxy_info_.proxy_resolve_start_time(); |
| 426 | load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time(); |
| 427 | load_timing_info->send_start = send_start_time_; |
| 428 | load_timing_info->send_end = send_end_time_; |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 429 | return true; |
| 430 | } |
| 431 | |
| 432 | void HttpNetworkTransaction::SetPriority(RequestPriority priority) { |
| 433 | priority_ = priority; |
[email protected] | bf82898 | 2013-08-14 18:01:47 | [diff] [blame] | 434 | if (stream_request_) |
| 435 | stream_request_->SetPriority(priority); |
[email protected] | e86839fd | 2013-08-14 18:29:03 | [diff] [blame] | 436 | if (stream_) |
| 437 | stream_->SetPriority(priority); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 440 | void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper( |
| 441 | WebSocketHandshakeStreamBase::CreateHelper* create_helper) { |
| 442 | websocket_handshake_stream_base_create_helper_ = create_helper; |
| 443 | } |
| 444 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 445 | void HttpNetworkTransaction::SetBeforeNetworkStartCallback( |
| 446 | const BeforeNetworkStartCallback& callback) { |
| 447 | before_network_start_callback_ = callback; |
| 448 | } |
| 449 | |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 450 | void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback( |
| 451 | const BeforeProxyHeadersSentCallback& callback) { |
| 452 | before_proxy_headers_sent_callback_ = callback; |
| 453 | } |
| 454 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 455 | int HttpNetworkTransaction::ResumeNetworkStart() { |
| 456 | DCHECK_EQ(next_state_, STATE_CREATE_STREAM); |
| 457 | return DoLoop(OK); |
| 458 | } |
| 459 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 460 | void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config, |
| 461 | const ProxyInfo& used_proxy_info, |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 462 | HttpStream* stream) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 463 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 464 | DCHECK(stream_request_.get()); |
| 465 | |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 466 | if (stream_) |
| 467 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 468 | stream_.reset(stream); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 469 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 470 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 471 | response_.was_npn_negotiated = stream_request_->was_npn_negotiated(); |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 472 | response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString( |
| 473 | stream_request_->protocol_negotiated()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 474 | response_.was_fetched_via_spdy = stream_request_->using_spdy(); |
| 475 | response_.was_fetched_via_proxy = !proxy_info_.is_direct(); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 476 | if (response_.was_fetched_via_proxy && !proxy_info_.is_empty()) |
| 477 | response_.proxy_server = proxy_info_.proxy_server().host_port_pair(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 478 | OnIOComplete(OK); |
| 479 | } |
| 480 | |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 481 | void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady( |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 482 | const SSLConfig& used_ssl_config, |
| 483 | const ProxyInfo& used_proxy_info, |
[email protected] | a9cf2b9 | 2013-10-30 12:08:49 | [diff] [blame] | 484 | WebSocketHandshakeStreamBase* stream) { |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 485 | OnStreamReady(used_ssl_config, used_proxy_info, stream); |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame] | 486 | } |
| 487 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 488 | void HttpNetworkTransaction::OnStreamFailed(int result, |
| 489 | const SSLConfig& used_ssl_config) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 490 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 491 | DCHECK_NE(OK, result); |
| 492 | DCHECK(stream_request_.get()); |
| 493 | DCHECK(!stream_.get()); |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 494 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 495 | |
| 496 | OnIOComplete(result); |
| 497 | } |
| 498 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 499 | void HttpNetworkTransaction::OnCertificateError( |
| 500 | int result, |
| 501 | const SSLConfig& used_ssl_config, |
| 502 | const SSLInfo& ssl_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 503 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 504 | DCHECK_NE(OK, result); |
| 505 | DCHECK(stream_request_.get()); |
| 506 | DCHECK(!stream_.get()); |
| 507 | |
| 508 | response_.ssl_info = ssl_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 509 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 510 | |
| 511 | // TODO(mbelshe): For now, we're going to pass the error through, and that |
| 512 | // 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] | 513 | // going to restart an entire STATE_CREATE_STREAM, even if the connection is |
| 514 | // good and the user chooses to ignore the error. This is not ideal, but not |
| 515 | // the end of the world either. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 516 | |
| 517 | OnIOComplete(result); |
| 518 | } |
| 519 | |
| 520 | void HttpNetworkTransaction::OnNeedsProxyAuth( |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 521 | const HttpResponseInfo& proxy_response, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 522 | const SSLConfig& used_ssl_config, |
| 523 | const ProxyInfo& used_proxy_info, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 524 | HttpAuthController* auth_controller) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 525 | DCHECK(stream_request_.get()); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 526 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 527 | |
| 528 | establishing_tunnel_ = true; |
| 529 | response_.headers = proxy_response.headers; |
| 530 | response_.auth_challenge = proxy_response.auth_challenge; |
| 531 | headers_valid_ = true; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 532 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 533 | proxy_info_ = used_proxy_info; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 534 | |
| 535 | auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller; |
| 536 | pending_auth_target_ = HttpAuth::AUTH_PROXY; |
| 537 | |
| 538 | DoCallback(OK); |
| 539 | } |
| 540 | |
| 541 | void HttpNetworkTransaction::OnNeedsClientAuth( |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 542 | const SSLConfig& used_ssl_config, |
[email protected] | 6dc476da | 2010-09-01 04:43:50 | [diff] [blame] | 543 | SSLCertRequestInfo* cert_info) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 544 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 545 | |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 546 | server_ssl_config_ = used_ssl_config; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 547 | response_.cert_request_info = cert_info; |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 548 | OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 549 | } |
| 550 | |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 551 | void HttpNetworkTransaction::OnHttpsProxyTunnelResponse( |
| 552 | const HttpResponseInfo& response_info, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 553 | const SSLConfig& used_ssl_config, |
| 554 | const ProxyInfo& used_proxy_info, |
yhirano | a7e05bb | 2014-11-06 05:40:39 | [diff] [blame] | 555 | HttpStream* stream) { |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 556 | DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_); |
| 557 | |
| 558 | headers_valid_ = true; |
| 559 | response_ = response_info; |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 560 | server_ssl_config_ = used_ssl_config; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 561 | proxy_info_ = used_proxy_info; |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 562 | if (stream_) |
| 563 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 564 | stream_.reset(stream); |
| 565 | stream_request_.reset(); // we're done with the stream request |
| 566 | OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); |
| 567 | } |
| 568 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 569 | bool HttpNetworkTransaction::is_https_request() const { |
| 570 | return request_->url.SchemeIs("https"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 571 | } |
| 572 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 573 | bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const { |
tbansal | 7cec381 | 2015-02-05 21:25:12 | [diff] [blame^] | 574 | return (proxy_info_.is_http() || proxy_info_.is_https() || |
| 575 | proxy_info_.is_quic()) && |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 576 | !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS()); |
| 577 | } |
| 578 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 579 | void HttpNetworkTransaction::DoCallback(int rv) { |
[email protected] | 0b0bf03 | 2010-09-21 18:08:50 | [diff] [blame] | 580 | DCHECK_NE(rv, ERR_IO_PENDING); |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 581 | DCHECK(!callback_.is_null()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 582 | |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 583 | // Since Run may result in Read being called, clear user_callback_ up front. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 584 | CompletionCallback c = callback_; |
| 585 | callback_.Reset(); |
| 586 | c.Run(rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void HttpNetworkTransaction::OnIOComplete(int result) { |
vadimt | 09e7ebe | 2014-10-29 22:10:41 | [diff] [blame] | 590 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed. |
| 591 | tracked_objects::ScopedTracker tracking_profile1( |
vadimt | 535be34 | 2014-10-28 00:13:32 | [diff] [blame] | 592 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 593 | "424359 HttpNetworkTransaction::OnIOComplete 1")); |
| 594 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 595 | int rv = DoLoop(result); |
vadimt | 535be34 | 2014-10-28 00:13:32 | [diff] [blame] | 596 | |
vadimt | 09e7ebe | 2014-10-29 22:10:41 | [diff] [blame] | 597 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed. |
| 598 | tracked_objects::ScopedTracker tracking_profile2( |
vadimt | 535be34 | 2014-10-28 00:13:32 | [diff] [blame] | 599 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 600 | "424359 HttpNetworkTransaction::OnIOComplete 2")); |
| 601 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 602 | if (rv != ERR_IO_PENDING) |
| 603 | DoCallback(rv); |
| 604 | } |
| 605 | |
| 606 | int HttpNetworkTransaction::DoLoop(int result) { |
| 607 | DCHECK(next_state_ != STATE_NONE); |
| 608 | |
| 609 | int rv = result; |
| 610 | do { |
| 611 | State state = next_state_; |
| 612 | next_state_ = STATE_NONE; |
| 613 | switch (state) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 614 | case STATE_NOTIFY_BEFORE_CREATE_STREAM: |
| 615 | DCHECK_EQ(OK, rv); |
| 616 | rv = DoNotifyBeforeCreateStream(); |
| 617 | break; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 618 | case STATE_CREATE_STREAM: |
| 619 | DCHECK_EQ(OK, rv); |
| 620 | rv = DoCreateStream(); |
| 621 | break; |
| 622 | case STATE_CREATE_STREAM_COMPLETE: |
| 623 | rv = DoCreateStreamComplete(rv); |
| 624 | break; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 625 | case STATE_INIT_STREAM: |
| 626 | DCHECK_EQ(OK, rv); |
| 627 | rv = DoInitStream(); |
| 628 | break; |
| 629 | case STATE_INIT_STREAM_COMPLETE: |
| 630 | rv = DoInitStreamComplete(rv); |
| 631 | break; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 632 | case STATE_GENERATE_PROXY_AUTH_TOKEN: |
| 633 | DCHECK_EQ(OK, rv); |
| 634 | rv = DoGenerateProxyAuthToken(); |
| 635 | break; |
| 636 | case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE: |
| 637 | rv = DoGenerateProxyAuthTokenComplete(rv); |
| 638 | break; |
| 639 | case STATE_GENERATE_SERVER_AUTH_TOKEN: |
| 640 | DCHECK_EQ(OK, rv); |
| 641 | rv = DoGenerateServerAuthToken(); |
| 642 | break; |
| 643 | case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE: |
| 644 | rv = DoGenerateServerAuthTokenComplete(rv); |
| 645 | break; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 646 | case STATE_INIT_REQUEST_BODY: |
| 647 | DCHECK_EQ(OK, rv); |
| 648 | rv = DoInitRequestBody(); |
| 649 | break; |
| 650 | case STATE_INIT_REQUEST_BODY_COMPLETE: |
| 651 | rv = DoInitRequestBodyComplete(rv); |
| 652 | break; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 653 | case STATE_BUILD_REQUEST: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 654 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 655 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 656 | rv = DoBuildRequest(); |
| 657 | break; |
| 658 | case STATE_BUILD_REQUEST_COMPLETE: |
| 659 | rv = DoBuildRequestComplete(rv); |
| 660 | break; |
| 661 | case STATE_SEND_REQUEST: |
| 662 | DCHECK_EQ(OK, rv); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 663 | rv = DoSendRequest(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 664 | break; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 665 | case STATE_SEND_REQUEST_COMPLETE: |
| 666 | rv = DoSendRequestComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 667 | net_log_.EndEventWithNetErrorCode( |
| 668 | NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 669 | break; |
| 670 | case STATE_READ_HEADERS: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 671 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 672 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 673 | rv = DoReadHeaders(); |
| 674 | break; |
| 675 | case STATE_READ_HEADERS_COMPLETE: |
| 676 | rv = DoReadHeadersComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 677 | net_log_.EndEventWithNetErrorCode( |
| 678 | NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 679 | break; |
| 680 | case STATE_READ_BODY: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 681 | DCHECK_EQ(OK, rv); |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 682 | net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 683 | rv = DoReadBody(); |
| 684 | break; |
| 685 | case STATE_READ_BODY_COMPLETE: |
| 686 | rv = DoReadBodyComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 687 | net_log_.EndEventWithNetErrorCode( |
| 688 | NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 689 | break; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 690 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART: |
[email protected] | 725355a | 2009-03-25 20:42:55 | [diff] [blame] | 691 | DCHECK_EQ(OK, rv); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 692 | net_log_.BeginEvent( |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 693 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 694 | rv = DoDrainBodyForAuthRestart(); |
| 695 | break; |
| 696 | case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE: |
| 697 | rv = DoDrainBodyForAuthRestartComplete(rv); |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 698 | net_log_.EndEventWithNetErrorCode( |
| 699 | NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv); |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 700 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 701 | default: |
| 702 | NOTREACHED() << "bad state"; |
| 703 | rv = ERR_FAILED; |
[email protected] | 96d570e4 | 2008-08-05 22:43:04 | [diff] [blame] | 704 | break; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 705 | } |
| 706 | } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 707 | |
| 708 | return rv; |
| 709 | } |
| 710 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 711 | int HttpNetworkTransaction::DoNotifyBeforeCreateStream() { |
| 712 | next_state_ = STATE_CREATE_STREAM; |
| 713 | bool defer = false; |
| 714 | if (!before_network_start_callback_.is_null()) |
| 715 | before_network_start_callback_.Run(&defer); |
| 716 | if (!defer) |
| 717 | return OK; |
| 718 | return ERR_IO_PENDING; |
| 719 | } |
| 720 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 721 | int HttpNetworkTransaction::DoCreateStream() { |
| 722 | next_state_ = STATE_CREATE_STREAM_COMPLETE; |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 723 | if (ForWebSocketHandshake()) { |
| 724 | stream_request_.reset( |
[email protected] | 0191b51c | 2013-11-18 10:55:23 | [diff] [blame] | 725 | session_->http_stream_factory_for_websocket() |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 726 | ->RequestWebSocketHandshakeStream( |
| 727 | *request_, |
| 728 | priority_, |
| 729 | server_ssl_config_, |
| 730 | proxy_ssl_config_, |
| 731 | this, |
| 732 | websocket_handshake_stream_base_create_helper_, |
| 733 | net_log_)); |
| 734 | } else { |
| 735 | stream_request_.reset( |
| 736 | session_->http_stream_factory()->RequestStream( |
| 737 | *request_, |
| 738 | priority_, |
| 739 | server_ssl_config_, |
| 740 | proxy_ssl_config_, |
| 741 | this, |
| 742 | net_log_)); |
| 743 | } |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 744 | DCHECK(stream_request_.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 745 | return ERR_IO_PENDING; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 746 | } |
| 747 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 748 | int HttpNetworkTransaction::DoCreateStreamComplete(int result) { |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 749 | if (result == OK) { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 750 | next_state_ = STATE_INIT_STREAM; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 751 | DCHECK(stream_.get()); |
[email protected] | adb0024 | 2010-10-29 03:04:33 | [diff] [blame] | 752 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 753 | result = HandleCertificateRequest(result); |
[email protected] | 511f6f5 | 2010-12-17 03:58:29 | [diff] [blame] | 754 | } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) { |
| 755 | // Return OK and let the caller read the proxy's error page |
| 756 | next_state_ = STATE_NONE; |
| 757 | return OK; |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 758 | } else if (result == ERR_HTTP_1_1_REQUIRED || |
| 759 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 760 | return HandleHttp11Required(result); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 761 | } |
| 762 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 763 | // Handle possible handshake errors that may have occurred if the stream |
| 764 | // used SSL for one or more of the layers. |
| 765 | result = HandleSSLHandshakeError(result); |
| 766 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 767 | // At this point we are done with the stream_request_. |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 768 | stream_request_.reset(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 769 | return result; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 770 | } |
| 771 | |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 772 | int HttpNetworkTransaction::DoInitStream() { |
| 773 | DCHECK(stream_.get()); |
| 774 | next_state_ = STATE_INIT_STREAM_COMPLETE; |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 775 | return stream_->InitializeStream(request_, priority_, net_log_, io_callback_); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | int HttpNetworkTransaction::DoInitStreamComplete(int result) { |
| 779 | if (result == OK) { |
| 780 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN; |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 781 | } else { |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 782 | if (result < 0) |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 783 | result = HandleIOError(result); |
| 784 | |
| 785 | // The stream initialization failed, so this stream will never be useful. |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 786 | if (stream_) |
| 787 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 044dcc5 | 2010-09-17 15:44:26 | [diff] [blame] | 788 | stream_.reset(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | return result; |
| 792 | } |
| 793 | |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 794 | int HttpNetworkTransaction::DoGenerateProxyAuthToken() { |
| 795 | next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE; |
| 796 | if (!ShouldApplyProxyAuth()) |
| 797 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 798 | HttpAuth::Target target = HttpAuth::AUTH_PROXY; |
| 799 | if (!auth_controllers_[target].get()) |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 800 | auth_controllers_[target] = |
| 801 | new HttpAuthController(target, |
| 802 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 803 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 804 | session_->http_auth_handler_factory()); |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 805 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 806 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 807 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) { |
| 811 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 812 | if (rv == OK) |
| 813 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN; |
| 814 | return rv; |
| 815 | } |
| 816 | |
| 817 | int HttpNetworkTransaction::DoGenerateServerAuthToken() { |
| 818 | next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 819 | HttpAuth::Target target = HttpAuth::AUTH_SERVER; |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 820 | if (!auth_controllers_[target].get()) { |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 821 | auth_controllers_[target] = |
| 822 | new HttpAuthController(target, |
| 823 | AuthURL(target), |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 824 | session_->http_auth_cache(), |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 825 | session_->http_auth_handler_factory()); |
[email protected] | 2217aa2 | 2013-10-11 03:03:54 | [diff] [blame] | 826 | if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY) |
| 827 | auth_controllers_[target]->DisableEmbeddedIdentity(); |
| 828 | } |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 829 | if (!ShouldApplyServerAuth()) |
| 830 | return OK; |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 831 | return auth_controllers_[target]->MaybeGenerateAuthToken(request_, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 832 | io_callback_, |
[email protected] | 394816e9 | 2010-08-03 07:38:59 | [diff] [blame] | 833 | net_log_); |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) { |
| 837 | DCHECK_NE(ERR_IO_PENDING, rv); |
| 838 | if (rv == OK) |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 839 | next_state_ = STATE_INIT_REQUEST_BODY; |
[email protected] | 044de064 | 2010-06-17 10:42:15 | [diff] [blame] | 840 | return rv; |
| 841 | } |
| 842 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 843 | void HttpNetworkTransaction::BuildRequestHeaders( |
| 844 | bool using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 845 | request_headers_.SetHeader(HttpRequestHeaders::kHost, |
| 846 | GetHostAndOptionalPort(request_->url)); |
| 847 | |
| 848 | // For compat with HTTP/1.0 servers and proxies: |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 849 | if (using_http_proxy_without_tunnel) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 850 | request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection, |
| 851 | "keep-alive"); |
| 852 | } else { |
| 853 | request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive"); |
| 854 | } |
| 855 | |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 856 | // Add a content length header? |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 857 | if (request_->upload_data_stream) { |
| 858 | if (request_->upload_data_stream->is_chunked()) { |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 859 | request_headers_.SetHeader( |
| 860 | HttpRequestHeaders::kTransferEncoding, "chunked"); |
| 861 | } else { |
| 862 | request_headers_.SetHeader( |
| 863 | HttpRequestHeaders::kContentLength, |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 864 | base::Uint64ToString(request_->upload_data_stream->size())); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 865 | } |
| 866 | } else if (request_->method == "POST" || request_->method == "PUT" || |
| 867 | request_->method == "HEAD") { |
| 868 | // An empty POST/PUT request still needs a content length. As for HEAD, |
| 869 | // IE and Safari also add a content length header. Presumably it is to |
| 870 | // support sending a HEAD request to an URL that only expects to be sent a |
| 871 | // POST or some other method that normally would have a message body. |
| 872 | request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0"); |
| 873 | } |
| 874 | |
| 875 | // Honor load flags that impact proxy caches. |
| 876 | if (request_->load_flags & LOAD_BYPASS_CACHE) { |
| 877 | request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache"); |
| 878 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache"); |
| 879 | } else if (request_->load_flags & LOAD_VALIDATE_CACHE) { |
| 880 | request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0"); |
| 881 | } |
| 882 | |
| 883 | if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY)) |
| 884 | auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader( |
| 885 | &request_headers_); |
| 886 | if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER)) |
| 887 | auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader( |
| 888 | &request_headers_); |
| 889 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 890 | request_headers_.MergeFrom(request_->extra_headers); |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 891 | |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 892 | if (using_http_proxy_without_tunnel && |
| 893 | !before_proxy_headers_sent_callback_.is_null()) |
[email protected] | 1252d42f | 2014-07-01 21:20:20 | [diff] [blame] | 894 | before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_); |
| 895 | |
[email protected] | 173f8e2 | 2013-04-10 04:18:20 | [diff] [blame] | 896 | response_.did_use_http_auth = |
| 897 | request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) || |
| 898 | request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization); |
[email protected] | 2979a49 | 2011-04-06 00:29:14 | [diff] [blame] | 899 | } |
| 900 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 901 | int HttpNetworkTransaction::DoInitRequestBody() { |
| 902 | next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 903 | int rv = OK; |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 904 | if (request_->upload_data_stream) |
| 905 | rv = request_->upload_data_stream->Init(io_callback_); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 906 | return rv; |
| 907 | } |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 908 | |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 909 | int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) { |
| 910 | if (result == OK) |
| 911 | next_state_ = STATE_BUILD_REQUEST; |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 912 | return result; |
| 913 | } |
| 914 | |
| 915 | int HttpNetworkTransaction::DoBuildRequest() { |
| 916 | next_state_ = STATE_BUILD_REQUEST_COMPLETE; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 917 | headers_valid_ = false; |
| 918 | |
| 919 | // This is constructed lazily (instead of within our Start method), so that |
| 920 | // we have proxy info available. |
| 921 | if (request_headers_.IsEmpty()) { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 922 | bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel(); |
| 923 | BuildRequestHeaders(using_http_proxy_without_tunnel); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 924 | } |
| 925 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 926 | return OK; |
| 927 | } |
| 928 | |
| 929 | int HttpNetworkTransaction::DoBuildRequestComplete(int result) { |
[email protected] | 9f489d7 | 2011-04-04 23:29:24 | [diff] [blame] | 930 | if (result == OK) |
| 931 | next_state_ = STATE_SEND_REQUEST; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 932 | return result; |
| 933 | } |
| 934 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 935 | int HttpNetworkTransaction::DoSendRequest() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 936 | send_start_time_ = base::TimeTicks::Now(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 937 | next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 938 | |
[email protected] | bf3eb00 | 2012-11-15 05:50:11 | [diff] [blame] | 939 | return stream_->SendRequest(request_headers_, &response_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 940 | } |
| 941 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 942 | int HttpNetworkTransaction::DoSendRequestComplete(int result) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 943 | send_end_time_ = base::TimeTicks::Now(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 944 | if (result < 0) |
| 945 | return HandleIOError(result); |
[email protected] | 7cf7ccb | 2013-04-20 02:53:08 | [diff] [blame] | 946 | response_.network_accessed = true; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 947 | next_state_ = STATE_READ_HEADERS; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 948 | return OK; |
| 949 | } |
| 950 | |
| 951 | int HttpNetworkTransaction::DoReadHeaders() { |
| 952 | next_state_ = STATE_READ_HEADERS_COMPLETE; |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 953 | return stream_->ReadResponseHeaders(io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | int HttpNetworkTransaction::DoReadHeadersComplete(int result) { |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 957 | // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here |
| 958 | // due to SSL renegotiation. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 959 | if (IsCertificateError(result)) { |
| 960 | // We don't handle a certificate error during SSL renegotiation, so we |
| 961 | // have to return an error that's not in the certificate error range |
| 962 | // (-2xx). |
| 963 | LOG(ERROR) << "Got a server certificate with error " << result |
| 964 | << " during SSL renegotiation"; |
| 965 | result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION; |
| 966 | } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 967 | // TODO(wtc): Need a test case for this code path! |
| 968 | DCHECK(stream_.get()); |
| 969 | DCHECK(is_https_request()); |
| 970 | response_.cert_request_info = new SSLCertRequestInfo; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 971 | stream_->GetSSLCertRequestInfo(response_.cert_request_info.get()); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 972 | result = HandleCertificateRequest(result); |
| 973 | if (result == OK) |
| 974 | return result; |
[email protected] | 2181ea00 | 2009-06-09 01:37:27 | [diff] [blame] | 975 | } |
| 976 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 977 | if (result == ERR_HTTP_1_1_REQUIRED || |
| 978 | result == ERR_PROXY_HTTP_1_1_REQUIRED) { |
| 979 | return HandleHttp11Required(result); |
| 980 | } |
| 981 | |
[email protected] | c871864d7 | 2014-03-13 19:56:19 | [diff] [blame] | 982 | // ERR_CONNECTION_CLOSED is treated differently at this point; if partial |
| 983 | // response headers were received, we do the best we can to make sense of it |
| 984 | // and send it back up the stack. |
| 985 | // |
| 986 | // TODO(davidben): Consider moving this to HttpBasicStream, It's a little |
| 987 | // bizarre for SPDY. Assuming this logic is useful at all. |
| 988 | // TODO(davidben): Bubble the error code up so we do not cache? |
| 989 | if (result == ERR_CONNECTION_CLOSED && response_.headers.get()) |
| 990 | result = OK; |
| 991 | |
| 992 | if (result < 0) |
| 993 | return HandleIOError(result); |
| 994 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 995 | DCHECK(response_.headers.get()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 996 | |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 997 | // On a 408 response from the server ("Request Timeout") on a stale socket, |
| 998 | // retry the request. |
[email protected] | 0aff0d8 | 2014-06-14 08:49:04 | [diff] [blame] | 999 | // Headers can be NULL because of http://crbug.com/384554. |
| 1000 | if (response_.headers.get() && response_.headers->response_code() == 408 && |
[email protected] | d58ceea8 | 2014-06-04 10:55:54 | [diff] [blame] | 1001 | stream_->IsConnectionReused()) { |
| 1002 | net_log_.AddEventWithNetErrorCode( |
| 1003 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, |
| 1004 | response_.headers->response_code()); |
| 1005 | // This will close the socket - it would be weird to try and reuse it, even |
| 1006 | // if the server doesn't actually close it. |
| 1007 | ResetConnectionAndRequestForResend(); |
| 1008 | return OK; |
| 1009 | } |
| 1010 | |
[email protected] | 93f8b56 | 2012-03-27 01:00:16 | [diff] [blame] | 1011 | // Like Net.HttpResponseCode, but only for MAIN_FRAME loads. |
| 1012 | if (request_->load_flags & LOAD_MAIN_FRAME) { |
| 1013 | const int response_code = response_.headers->response_code(); |
| 1014 | UMA_HISTOGRAM_ENUMERATION( |
| 1015 | "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10); |
| 1016 | } |
| 1017 | |
[email protected] | 3abacd6 | 2012-06-10 20:20:32 | [diff] [blame] | 1018 | net_log_.AddEvent( |
| 1019 | NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS, |
| 1020 | base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers)); |
[email protected] | dbb83db | 2010-05-11 18:13:39 | [diff] [blame] | 1021 | |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1022 | if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1023 | // HTTP/0.9 doesn't support the PUT method, so lack of response headers |
| 1024 | // indicates a buggy server. See: |
| 1025 | // https://bugzilla.mozilla.org/show_bug.cgi?id=193921 |
| 1026 | if (request_->method == "PUT") |
| 1027 | return ERR_METHOD_NOT_SUPPORTED; |
| 1028 | } |
[email protected] | 4ddaf250 | 2008-10-23 18:26:19 | [diff] [blame] | 1029 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1030 | // Check for an intermediate 100 Continue response. An origin server is |
| 1031 | // allowed to send this response even if we didn't ask for it, so we just |
| 1032 | // need to skip over it. |
| 1033 | // We treat any other 1xx in this same way (although in practice getting |
| 1034 | // a 1xx that isn't a 100 is rare). |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1035 | // Unless this is a WebSocket request, in which case we pass it on up. |
| 1036 | if (response_.headers->response_code() / 100 == 1 && |
| 1037 | !ForWebSocketHandshake()) { |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1038 | response_.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1039 | next_state_ = STATE_READ_HEADERS; |
| 1040 | return OK; |
| 1041 | } |
| 1042 | |
pkasting | cba1329 | 2014-11-20 03:35:21 | [diff] [blame] | 1043 | ProcessAlternateProtocol(session_, *response_.headers.get(), |
| 1044 | HostPortPair::FromURL(request_->url)); |
[email protected] | 564b491 | 2010-03-09 16:30:42 | [diff] [blame] | 1045 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1046 | int rv = HandleAuthChallenge(); |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1047 | if (rv != OK) |
| 1048 | return rv; |
| 1049 | |
[email protected] | 8536ef5 | 2010-09-30 16:18:21 | [diff] [blame] | 1050 | if (is_https_request()) |
| 1051 | stream_->GetSSLInfo(&response_.ssl_info); |
| 1052 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1053 | headers_valid_ = true; |
[email protected] | e00bfd57 | 2014-04-30 08:06:43 | [diff] [blame] | 1054 | |
| 1055 | if (session_->huffman_aggregator()) { |
| 1056 | session_->huffman_aggregator()->AggregateTransactionCharacterCounts( |
| 1057 | *request_, |
| 1058 | request_headers_, |
| 1059 | proxy_info_.proxy_server(), |
dcheng | 48459ac2 | 2014-08-26 00:46:41 | [diff] [blame] | 1060 | *response_.headers.get()); |
[email protected] | e00bfd57 | 2014-04-30 08:06:43 | [diff] [blame] | 1061 | } |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1062 | return OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | int HttpNetworkTransaction::DoReadBody() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1066 | DCHECK(read_buf_.get()); |
[email protected] | 6501bc0 | 2009-06-25 20:55:13 | [diff] [blame] | 1067 | DCHECK_GT(read_buf_len_, 0); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1068 | DCHECK(stream_ != NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1069 | |
| 1070 | next_state_ = STATE_READ_BODY_COMPLETE; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1071 | return stream_->ReadResponseBody( |
| 1072 | read_buf_.get(), read_buf_len_, io_callback_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | int HttpNetworkTransaction::DoReadBodyComplete(int result) { |
| 1076 | // We are done with the Read call. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1077 | bool done = false; |
| 1078 | if (result <= 0) { |
| 1079 | DCHECK_NE(ERR_IO_PENDING, result); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1080 | done = true; |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1081 | } |
[email protected] | 9492e4a | 2010-02-24 00:58:46 | [diff] [blame] | 1082 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1083 | bool keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1084 | if (stream_->IsResponseBodyComplete()) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1085 | // Note: Just because IsResponseBodyComplete is true, we're not |
| 1086 | // necessarily "done". We're only "done" when it is the last |
| 1087 | // read on this HttpNetworkTransaction, which will be signified |
| 1088 | // by a zero-length read. |
| 1089 | // TODO(mbelshe): The keepalive property is really a property of |
| 1090 | // the stream. No need to compute it here just to pass back |
| 1091 | // to the stream's Close function. |
[email protected] | 5c6908e | 2012-08-06 18:53:47 | [diff] [blame] | 1092 | // TODO(rtenneti): CanFindEndOfResponse should return false if there are no |
| 1093 | // ResponseHeaders. |
| 1094 | if (stream_->CanFindEndOfResponse()) { |
| 1095 | HttpResponseHeaders* headers = GetResponseHeaders(); |
| 1096 | if (headers) |
| 1097 | keep_alive = headers->IsKeepAlive(); |
| 1098 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1099 | } |
| 1100 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1101 | // Clean up connection if we are done. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1102 | if (done) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1103 | stream_->Close(!keep_alive); |
[email protected] | e2a915a | 2010-08-19 07:55:01 | [diff] [blame] | 1104 | // Note: we don't reset the stream here. We've closed it, but we still |
| 1105 | // need it around so that callers can call methods such as |
| 1106 | // GetUploadProgress() and have them be meaningful. |
| 1107 | // TODO(mbelshe): This means we closed the stream here, and we close it |
| 1108 | // again in ~HttpNetworkTransaction. Clean that up. |
| 1109 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1110 | // The next Read call will return 0 (EOF). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | // Clear these to avoid leaving around old state. |
| 1114 | read_buf_ = NULL; |
| 1115 | read_buf_len_ = 0; |
| 1116 | |
| 1117 | return result; |
| 1118 | } |
| 1119 | |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1120 | int HttpNetworkTransaction::DoDrainBodyForAuthRestart() { |
| 1121 | // This method differs from DoReadBody only in the next_state_. So we just |
| 1122 | // call DoReadBody and override the next_state_. Perhaps there is a more |
| 1123 | // elegant way for these two methods to share code. |
| 1124 | int rv = DoReadBody(); |
| 1125 | DCHECK(next_state_ == STATE_READ_BODY_COMPLETE); |
| 1126 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE; |
| 1127 | return rv; |
| 1128 | } |
| 1129 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1130 | // TODO(wtc): This method and the DoReadBodyComplete method are almost |
| 1131 | // 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] | 1132 | int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) { |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1133 | // keep_alive defaults to true because the very reason we're draining the |
| 1134 | // response body is to reuse the connection for auth restart. |
| 1135 | bool done = false, keep_alive = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1136 | if (result < 0) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1137 | // Error or closed connection while reading the socket. |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1138 | done = true; |
[email protected] | 68873ba | 2009-06-04 21:49:23 | [diff] [blame] | 1139 | keep_alive = false; |
[email protected] | 351ab64 | 2010-08-05 16:55:31 | [diff] [blame] | 1140 | } else if (stream_->IsResponseBodyComplete()) { |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1141 | done = true; |
[email protected] | 2d2697f9 | 2009-02-18 21:00:32 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | if (done) { |
| 1145 | DidDrainBodyForAuthRestart(keep_alive); |
| 1146 | } else { |
| 1147 | // Keep draining. |
| 1148 | next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART; |
| 1149 | } |
| 1150 | |
| 1151 | return OK; |
| 1152 | } |
| 1153 | |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1154 | int HttpNetworkTransaction::HandleCertificateRequest(int error) { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1155 | // There are two paths through which the server can request a certificate |
| 1156 | // from us. The first is during the initial handshake, the second is |
| 1157 | // during SSL renegotiation. |
| 1158 | // |
| 1159 | // In both cases, we want to close the connection before proceeding. |
| 1160 | // We do this for two reasons: |
| 1161 | // First, we don't want to keep the connection to the server hung for a |
| 1162 | // long time while the user selects a certificate. |
| 1163 | // Second, even if we did keep the connection open, NSS has a bug where |
| 1164 | // restarting the handshake for ClientAuth is currently broken. |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1165 | DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1166 | |
| 1167 | if (stream_.get()) { |
| 1168 | // Since we already have a stream, we're being called as part of SSL |
| 1169 | // renegotiation. |
| 1170 | DCHECK(!stream_request_.get()); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1171 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1172 | stream_->Close(true); |
| 1173 | stream_.reset(); |
| 1174 | } |
| 1175 | |
[email protected] | 2681688 | 2010-10-14 18:03:09 | [diff] [blame] | 1176 | // The server is asking for a client certificate during the initial |
| 1177 | // handshake. |
| 1178 | stream_request_.reset(); |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1179 | |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1180 | // If the user selected one of the certificates in client_certs or declined |
| 1181 | // to provide one for this server before, use the past decision |
| 1182 | // automatically. |
| 1183 | scoped_refptr<X509Certificate> client_cert; |
| 1184 | bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup( |
| 1185 | response_.cert_request_info->host_and_port, &client_cert); |
| 1186 | if (!found_cached_cert) |
| 1187 | return error; |
| 1188 | |
| 1189 | // Check that the certificate selected is still a certificate the server |
| 1190 | // is likely to accept, based on the criteria supplied in the |
| 1191 | // CertificateRequest message. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1192 | if (client_cert.get()) { |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1193 | const std::vector<std::string>& cert_authorities = |
| 1194 | response_.cert_request_info->cert_authorities; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1195 | |
[email protected] | f1958c38 | 2013-02-07 00:15:26 | [diff] [blame] | 1196 | bool cert_still_valid = cert_authorities.empty() || |
| 1197 | client_cert->IsIssuedByEncoded(cert_authorities); |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1198 | if (!cert_still_valid) |
| 1199 | return error; |
[email protected] | 5e36396 | 2009-06-19 19:57:01 | [diff] [blame] | 1200 | } |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1201 | |
| 1202 | // TODO(davidben): Add a unit test which covers this path; we need to be |
| 1203 | // able to send a legitimate certificate and also bypass/clear the |
| 1204 | // SSL session cache. |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 1205 | SSLConfig* ssl_config = response_.cert_request_info->is_proxy ? |
| 1206 | &proxy_ssl_config_ : &server_ssl_config_; |
| 1207 | ssl_config->send_client_cert = true; |
| 1208 | ssl_config->client_cert = client_cert; |
[email protected] | ec229bc9 | 2010-11-22 09:51:45 | [diff] [blame] | 1209 | next_state_ = STATE_CREATE_STREAM; |
| 1210 | // Reset the other member variables. |
| 1211 | // Note: this is necessary only with SSL renegotiation. |
| 1212 | ResetStateForRestart(); |
| 1213 | return OK; |
[email protected] | 0b45559b | 2009-06-12 21:45:11 | [diff] [blame] | 1214 | } |
| 1215 | |
bnc | facdd85 | 2015-01-09 19:22:54 | [diff] [blame] | 1216 | int HttpNetworkTransaction::HandleHttp11Required(int error) { |
| 1217 | DCHECK(error == ERR_HTTP_1_1_REQUIRED || |
| 1218 | error == ERR_PROXY_HTTP_1_1_REQUIRED); |
| 1219 | |
| 1220 | if (error == ERR_HTTP_1_1_REQUIRED) { |
| 1221 | HttpServerProperties::ForceHTTP11(&server_ssl_config_); |
| 1222 | } else { |
| 1223 | HttpServerProperties::ForceHTTP11(&proxy_ssl_config_); |
| 1224 | } |
| 1225 | ResetConnectionAndRequestForResend(); |
| 1226 | return OK; |
| 1227 | } |
| 1228 | |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1229 | void HttpNetworkTransaction::HandleClientAuthError(int error) { |
| 1230 | if (server_ssl_config_.send_client_cert && |
| 1231 | (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) { |
| 1232 | session_->ssl_client_auth_cache()->Remove( |
[email protected] | 791879c | 2013-12-17 07:22:41 | [diff] [blame] | 1233 | HostPortPair::FromURL(request_->url)); |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1234 | } |
| 1235 | } |
| 1236 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1237 | // TODO(rch): This does not correctly handle errors when an SSL proxy is |
| 1238 | // being used, as all of the errors are handled as if they were generated |
| 1239 | // by the endpoint host, request_->url, rather than considering if they were |
[email protected] | 1c53a1f | 2011-01-13 00:36:38 | [diff] [blame] | 1240 | // generated by the SSL proxy. http://crbug.com/69329 |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1241 | int HttpNetworkTransaction::HandleSSLHandshakeError(int error) { |
| 1242 | DCHECK(request_); |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1243 | HandleClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1244 | |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1245 | bool should_fallback = false; |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1246 | uint16 version_max = server_ssl_config_.version_max; |
| 1247 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1248 | switch (error) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 1249 | case ERR_CONNECTION_CLOSED: |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1250 | case ERR_SSL_PROTOCOL_ERROR: |
| 1251 | case ERR_SSL_VERSION_OR_CIPHER_MISMATCH: |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1252 | if (version_max >= SSL_PROTOCOL_VERSION_TLS1 && |
| 1253 | version_max > server_ssl_config_.version_min) { |
[email protected] | 80c75f68 | 2012-05-26 16:22:17 | [diff] [blame] | 1254 | // This could be a TLS-intolerant server or a server that chose a |
| 1255 | // cipher suite defined only for higher protocol versions (such as |
| 1256 | // an SSL 3.0 server that chose a TLS-only cipher suite). Fall |
| 1257 | // back to the next lower version and retry. |
| 1258 | // NOTE: if the SSLClientSocket class doesn't support TLS 1.1, |
| 1259 | // specifying TLS 1.1 in version_max will result in a TLS 1.0 |
| 1260 | // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply |
| 1261 | // repeat the TLS 1.0 handshake. To avoid this problem, the default |
| 1262 | // version_max should match the maximum protocol version supported |
| 1263 | // by the SSLClientSocket class. |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1264 | version_max--; |
[email protected] | c57ec02 | 2013-11-25 21:30:51 | [diff] [blame] | 1265 | |
| 1266 | // Fallback to the lower SSL version. |
| 1267 | // While SSL 3.0 fallback should be eliminated because of security |
| 1268 | // reasons, there is a high risk of breaking the servers if this is |
| 1269 | // done in general. |
[email protected] | 001e547e | 2013-12-06 16:08:39 | [diff] [blame] | 1270 | should_fallback = true; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1271 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1272 | break; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 1273 | case ERR_CONNECTION_RESET: |
| 1274 | if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 && |
| 1275 | version_max > server_ssl_config_.version_min) { |
| 1276 | // Some network devices that inspect application-layer packets seem to |
| 1277 | // inject TCP reset packets to break the connections when they see TLS |
| 1278 | // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293. |
| 1279 | // |
| 1280 | // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or |
| 1281 | // 1.2. We don't lose much in this fallback because the explicit IV for |
| 1282 | // CBC mode in TLS 1.1 is approximated by record splitting in TLS |
| 1283 | // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM |
| 1284 | // support. |
| 1285 | // |
| 1286 | // ERR_CONNECTION_RESET is a common network error, so we don't want it |
| 1287 | // to trigger a version fallback in general, especially the TLS 1.0 -> |
| 1288 | // SSL 3.0 fallback, which would drop TLS extensions. |
| 1289 | version_max--; |
| 1290 | should_fallback = true; |
| 1291 | } |
| 1292 | break; |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1293 | case ERR_SSL_BAD_RECORD_MAC_ALERT: |
| 1294 | if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 && |
| 1295 | version_max > server_ssl_config_.version_min) { |
| 1296 | // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or |
| 1297 | // 1.2 ClientHello, but then return a bad_record_mac alert. See |
| 1298 | // crbug.com/260358. In order to make the fallback as minimal as |
| 1299 | // possible, this fallback is only triggered for >= TLS 1.1. |
| 1300 | version_max--; |
| 1301 | should_fallback = true; |
| 1302 | } |
| 1303 | break; |
[email protected] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 1304 | case ERR_SSL_INAPPROPRIATE_FALLBACK: |
| 1305 | // The server told us that we should not have fallen back. A buggy server |
| 1306 | // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial |
| 1307 | // connection. |fallback_error_code_| is initialised to |
| 1308 | // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case. |
| 1309 | error = fallback_error_code_; |
| 1310 | break; |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | if (should_fallback) { |
| 1314 | net_log_.AddEvent( |
| 1315 | NetLog::TYPE_SSL_VERSION_FALLBACK, |
| 1316 | base::Bind(&NetLogSSLVersionFallbackCallback, |
| 1317 | &request_->url, error, server_ssl_config_.version_max, |
| 1318 | version_max)); |
[email protected] | a53e4d1 | 2013-12-07 16:37:24 | [diff] [blame] | 1319 | fallback_error_code_ = error; |
[email protected] | 02d1d44 | 2013-08-10 13:38:26 | [diff] [blame] | 1320 | server_ssl_config_.version_max = version_max; |
| 1321 | server_ssl_config_.version_fallback = true; |
| 1322 | ResetConnectionAndRequestForResend(); |
| 1323 | error = OK; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1324 | } |
[email protected] | 158ac97 | 2013-04-19 23:29:23 | [diff] [blame] | 1325 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1326 | return error; |
| 1327 | } |
| 1328 | |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1329 | // This method determines whether it is safe to resend the request after an |
| 1330 | // IO error. It can only be called in response to request header or body |
| 1331 | // write errors or response header read errors. It should not be used in |
| 1332 | // other cases, such as a Connect error. |
| 1333 | int HttpNetworkTransaction::HandleIOError(int error) { |
[email protected] | 384cc73a | 2013-12-08 22:41:03 | [diff] [blame] | 1334 | // Because the peer may request renegotiation with client authentication at |
| 1335 | // any time, check and handle client authentication errors. |
| 1336 | HandleClientAuthError(error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1337 | |
| 1338 | switch (error) { |
| 1339 | // If we try to reuse a connection that the server is in the process of |
| 1340 | // closing, we may end up successfully writing out our request (or a |
| 1341 | // portion of our request) only to find a connection error when we try to |
| 1342 | // read from (or finish writing to) the socket. |
| 1343 | case ERR_CONNECTION_RESET: |
| 1344 | case ERR_CONNECTION_CLOSED: |
| 1345 | case ERR_CONNECTION_ABORTED: |
[email protected] | 20296599 | 2011-12-07 23:04:51 | [diff] [blame] | 1346 | // There can be a race between the socket pool checking checking whether a |
| 1347 | // socket is still connected, receiving the FIN, and sending/reading data |
| 1348 | // on a reused socket. If we receive the FIN between the connectedness |
| 1349 | // check and writing/reading from the socket, we may first learn the socket |
| 1350 | // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most |
| 1351 | // likely happen when trying to retrieve its IP address. |
| 1352 | // See http://crbug.com/105824 for more details. |
| 1353 | case ERR_SOCKET_NOT_CONNECTED: |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1354 | // If a socket is closed on its initial request, HttpStreamParser returns |
| 1355 | // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was |
| 1356 | // preconnected but failed to be used before the server timed it out. |
| 1357 | case ERR_EMPTY_RESPONSE: |
| 1358 | if (ShouldResendRequest()) { |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1359 | net_log_.AddEventWithNetErrorCode( |
| 1360 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1361 | ResetConnectionAndRequestForResend(); |
| 1362 | error = OK; |
| 1363 | } |
| 1364 | break; |
[email protected] | 8753a12 | 2011-10-16 08:05:08 | [diff] [blame] | 1365 | case ERR_SPDY_PING_FAILED: |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1366 | case ERR_SPDY_SERVER_REFUSED_STREAM: |
[email protected] | 4d283b3 | 2013-10-17 12:57:27 | [diff] [blame] | 1367 | case ERR_QUIC_HANDSHAKE_FAILED: |
[email protected] | b675425 | 2012-06-13 23:14:38 | [diff] [blame] | 1368 | net_log_.AddEventWithNetErrorCode( |
| 1369 | NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error); |
[email protected] | 721c0ce | 2011-10-13 02:41:00 | [diff] [blame] | 1370 | ResetConnectionAndRequestForResend(); |
| 1371 | error = OK; |
| 1372 | break; |
[email protected] | bd0b677 | 2011-01-11 19:59:30 | [diff] [blame] | 1373 | } |
| 1374 | return error; |
| 1375 | } |
| 1376 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1377 | void HttpNetworkTransaction::ResetStateForRestart() { |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1378 | ResetStateForAuthRestart(); |
[email protected] | b8015c4 | 2013-12-24 15:18:19 | [diff] [blame] | 1379 | if (stream_) |
| 1380 | total_received_bytes_ += stream_->GetTotalReceivedBytes(); |
[email protected] | 697ef4c | 2010-10-14 16:38:58 | [diff] [blame] | 1381 | stream_.reset(); |
| 1382 | } |
| 1383 | |
| 1384 | void HttpNetworkTransaction::ResetStateForAuthRestart() { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1385 | send_start_time_ = base::TimeTicks(); |
| 1386 | send_end_time_ = base::TimeTicks(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 1387 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 1388 | pending_auth_target_ = HttpAuth::AUTH_NONE; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1389 | read_buf_ = NULL; |
| 1390 | read_buf_len_ = 0; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1391 | headers_valid_ = false; |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1392 | request_headers_.Clear(); |
[email protected] | a7e4131 | 2009-12-16 23:18:14 | [diff] [blame] | 1393 | response_ = HttpResponseInfo(); |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1394 | establishing_tunnel_ = false; |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1398 | return response_.headers.get(); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1399 | } |
| 1400 | |
[email protected] | a34f61ee | 2014-03-18 20:59:49 | [diff] [blame] | 1401 | bool HttpNetworkTransaction::ShouldResendRequest() const { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1402 | bool connection_is_proven = stream_->IsConnectionReused(); |
| 1403 | bool has_received_headers = GetResponseHeaders() != NULL; |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1404 | |
[email protected] | 2a5c76b | 2008-09-25 22:15:16 | [diff] [blame] | 1405 | // NOTE: we resend a request only if we reused a keep-alive connection. |
| 1406 | // This automatically prevents an infinite resend loop because we'll run |
| 1407 | // out of the cached keep-alive connections eventually. |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1408 | if (connection_is_proven && !has_received_headers) |
| 1409 | return true; |
| 1410 | return false; |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void HttpNetworkTransaction::ResetConnectionAndRequestForResend() { |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1414 | if (stream_.get()) { |
| 1415 | stream_->Close(true); |
| 1416 | stream_.reset(); |
[email protected] | 58cebf8f | 2010-07-31 19:20:16 | [diff] [blame] | 1417 | } |
| 1418 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1419 | // We need to clear request_headers_ because it contains the real request |
| 1420 | // headers, but we may need to resend the CONNECT request first to recreate |
| 1421 | // the SSL tunnel. |
[email protected] | b94f92d | 2010-10-27 16:45:20 | [diff] [blame] | 1422 | request_headers_.Clear(); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1423 | next_state_ = STATE_CREATE_STREAM; // Resend the request. |
[email protected] | 86ec30d | 2008-09-29 21:53:54 | [diff] [blame] | 1424 | } |
| 1425 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1426 | bool HttpNetworkTransaction::ShouldApplyProxyAuth() const { |
Adam Rice | 425cf12 | 2015-01-19 06:18:24 | [diff] [blame] | 1427 | return UsingHttpProxyWithoutTunnel(); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1428 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1429 | |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1430 | bool HttpNetworkTransaction::ShouldApplyServerAuth() const { |
[email protected] | 8a1f331 | 2010-05-25 19:25:04 | [diff] [blame] | 1431 | return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA); |
[email protected] | 1c773ea1 | 2009-04-28 19:58:42 | [diff] [blame] | 1432 | } |
| 1433 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1434 | int HttpNetworkTransaction::HandleAuthChallenge() { |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1435 | scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1436 | DCHECK(headers.get()); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1437 | |
[email protected] | 0877e3d | 2009-10-17 22:29:57 | [diff] [blame] | 1438 | int status = headers->response_code(); |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1439 | if (status != HTTP_UNAUTHORIZED && |
| 1440 | status != HTTP_PROXY_AUTHENTICATION_REQUIRED) |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1441 | return OK; |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1442 | HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ? |
[email protected] | 2227c69 | 2010-05-04 15:36:11 | [diff] [blame] | 1443 | HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER; |
[email protected] | 038e9a3 | 2008-10-08 22:40:16 | [diff] [blame] | 1444 | if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct()) |
| 1445 | return ERR_UNEXPECTED_PROXY_AUTH; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1446 | |
[email protected] | 9094b60 | 2012-02-27 21:44:58 | [diff] [blame] | 1447 | // This case can trigger when an HTTPS server responds with a "Proxy |
| 1448 | // authentication required" status code through a non-authenticating |
| 1449 | // proxy. |
[email protected] | 7a67a815 | 2010-11-05 18:31:10 | [diff] [blame] | 1450 | if (!auth_controllers_[target].get()) |
| 1451 | return ERR_UNEXPECTED_PROXY_AUTH; |
| 1452 | |
[email protected] | a7ea883 | 2010-07-12 17:54:54 | [diff] [blame] | 1453 | int rv = auth_controllers_[target]->HandleAuthChallenge( |
[email protected] | 560c043 | 2010-07-13 20:45:31 | [diff] [blame] | 1454 | headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false, |
| 1455 | net_log_); |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1456 | if (auth_controllers_[target]->HaveAuthHandler()) |
| 1457 | pending_auth_target_ = target; |
| 1458 | |
| 1459 | scoped_refptr<AuthChallengeInfo> auth_info = |
| 1460 | auth_controllers_[target]->auth_info(); |
| 1461 | if (auth_info.get()) |
| 1462 | response_.auth_challenge = auth_info; |
| 1463 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1464 | return rv; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 1465 | } |
| 1466 | |
[email protected] | 8e6441ca | 2010-08-19 05:56:38 | [diff] [blame] | 1467 | bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const { |
| 1468 | return auth_controllers_[target].get() && |
| 1469 | auth_controllers_[target]->HaveAuth(); |
| 1470 | } |
| 1471 | |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1472 | GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const { |
| 1473 | switch (target) { |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1474 | case HttpAuth::AUTH_PROXY: { |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1475 | if (!proxy_info_.proxy_server().is_valid() || |
| 1476 | proxy_info_.proxy_server().is_direct()) { |
| 1477 | return GURL(); // There is no proxy server. |
| 1478 | } |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1479 | const char* scheme = proxy_info_.is_https() ? "https://" : "http://"; |
| 1480 | return GURL(scheme + |
[email protected] | 2fbaecf2 | 2010-07-22 22:20:35 | [diff] [blame] | 1481 | proxy_info_.proxy_server().host_port_pair().ToString()); |
[email protected] | 2df19bb | 2010-08-25 20:13:46 | [diff] [blame] | 1482 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1483 | case HttpAuth::AUTH_SERVER: |
[email protected] | e69c1cd | 2014-07-29 07:42:29 | [diff] [blame] | 1484 | if (ForWebSocketHandshake()) { |
| 1485 | const GURL& url = request_->url; |
| 1486 | url::Replacements<char> ws_to_http; |
| 1487 | if (url.SchemeIs("ws")) { |
| 1488 | ws_to_http.SetScheme("http", url::Component(0, 4)); |
| 1489 | } else { |
| 1490 | DCHECK(url.SchemeIs("wss")); |
| 1491 | ws_to_http.SetScheme("https", url::Component(0, 5)); |
| 1492 | } |
| 1493 | return url.ReplaceComponents(ws_to_http); |
| 1494 | } |
[email protected] | 228404f | 2010-06-24 04:31:41 | [diff] [blame] | 1495 | return request_->url; |
| 1496 | default: |
| 1497 | return GURL(); |
| 1498 | } |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1499 | } |
| 1500 | |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1501 | bool HttpNetworkTransaction::ForWebSocketHandshake() const { |
[email protected] | 23d3e02 | 2013-11-23 20:38:14 | [diff] [blame] | 1502 | return websocket_handshake_stream_base_create_helper_ && |
| 1503 | request_->url.SchemeIsWSOrWSS(); |
[email protected] | 831e4a3 | 2013-11-14 02:14:44 | [diff] [blame] | 1504 | } |
| 1505 | |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1506 | #define STATE_CASE(s) \ |
| 1507 | case s: \ |
| 1508 | description = base::StringPrintf("%s (0x%08X)", #s, s); \ |
| 1509 | break |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1510 | |
| 1511 | std::string HttpNetworkTransaction::DescribeState(State state) { |
| 1512 | std::string description; |
| 1513 | switch (state) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 1514 | STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM); |
[email protected] | 82918cc | 2010-08-25 17:24:50 | [diff] [blame] | 1515 | STATE_CASE(STATE_CREATE_STREAM); |
| 1516 | STATE_CASE(STATE_CREATE_STREAM_COMPLETE); |
[email protected] | daddea6 | 2012-09-19 05:51:13 | [diff] [blame] | 1517 | STATE_CASE(STATE_INIT_REQUEST_BODY); |
| 1518 | STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE); |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 1519 | STATE_CASE(STATE_BUILD_REQUEST); |
| 1520 | STATE_CASE(STATE_BUILD_REQUEST_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1521 | STATE_CASE(STATE_SEND_REQUEST); |
| 1522 | STATE_CASE(STATE_SEND_REQUEST_COMPLETE); |
| 1523 | STATE_CASE(STATE_READ_HEADERS); |
| 1524 | STATE_CASE(STATE_READ_HEADERS_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1525 | STATE_CASE(STATE_READ_BODY); |
| 1526 | STATE_CASE(STATE_READ_BODY_COMPLETE); |
| 1527 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART); |
| 1528 | STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1529 | STATE_CASE(STATE_NONE); |
| 1530 | default: |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 1531 | description = base::StringPrintf("Unknown state 0x%08X (%u)", state, |
| 1532 | state); |
[email protected] | aef0427 | 2010-06-28 18:03:04 | [diff] [blame] | 1533 | break; |
| 1534 | } |
| 1535 | return description; |
| 1536 | } |
| 1537 | |
| 1538 | #undef STATE_CASE |
| 1539 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1540 | } // namespace net |