blob: 69a3aa58145e2b7d2f9a41505d59df253de560e5 [file] [log] [blame]
[email protected]49ed6cc2012-02-02 08:59:161// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d102f542010-06-30 14:51:052// Use of this source code is governed by a BSD-style license that can be
license.botbf09a502008-08-24 00:55:553// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#include "net/http/http_network_transaction.h"
6
[email protected]2fbaecf22010-07-22 22:20:357#include <set>
8#include <vector>
9
[email protected]49639fa2011-12-20 23:22:4110#include "base/bind.h"
11#include "base/bind_helpers.h"
[email protected]68bf9152008-09-25 19:47:3012#include "base/compiler_specific.h"
[email protected]270c6412010-03-29 22:02:4713#include "base/format_macros.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/scoped_ptr.h"
[email protected]835d7c82010-10-14 04:38:3815#include "base/metrics/field_trial.h"
16#include "base/metrics/histogram.h"
17#include "base/metrics/stats_counters.h"
vadimt09e7ebe2014-10-29 22:10:4118#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2419#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4720#include "base/strings/string_number_conversions.h"
21#include "base/strings/string_util.h"
22#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4023#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5324#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3025#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3526#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5527#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1928#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5229#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2530#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2631#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4232#include "net/base/net_util.h"
initial.commit586acc5fe2008-07-26 22:42:5233#include "net/base/upload_data_stream.h"
[email protected]c3b35c22008-09-27 03:19:4234#include "net/http/http_auth.h"
35#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2436#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3637#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5238#include "net/http/http_chunked_decoder.h"
39#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5440#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1341#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4742#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5243#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2144#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5745#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5346#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5847#include "net/http/http_status_code.h"
yhiranoa7e05bb2014-11-06 05:40:3948#include "net/http/http_stream.h"
[email protected]102e27c2011-02-23 01:01:3149#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5250#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2351#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3652#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4453#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2654#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4455#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1856#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2857#include "net/socket/transport_client_socket_pool.h"
[email protected]e00bfd572014-04-30 08:06:4358#include "net/spdy/hpack_huffman_aggregator.h"
[email protected]65d56aa2010-06-14 04:13:4059#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3260#include "net/spdy/spdy_session.h"
61#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5762#include "net/ssl/ssl_cert_request_info.h"
63#include "net/ssl/ssl_connection_status_flags.h"
[email protected]f89276a72013-07-12 06:41:5464#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2965#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5266
initial.commit586acc5fe2008-07-26 22:42:5267namespace net {
68
[email protected]1c773ea12009-04-28 19:58:4269namespace {
70
[email protected]30d4c022013-07-18 22:58:1671void ProcessAlternateProtocol(
[email protected]d7599122014-05-24 03:37:2372 HttpNetworkSession* session,
[email protected]30d4c022013-07-18 22:58:1673 const HttpResponseHeaders& headers,
74 const HostPortPair& http_host_port_pair) {
[email protected]287d9412014-07-08 23:01:0075 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3876 return;
[email protected]287d9412014-07-08 23:01:0077
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]564b4912010-03-09 16:30:4284 }
85
[email protected]d7599122014-05-24 03:37:2386 session->http_stream_factory()->ProcessAlternateProtocol(
87 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:0088 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:2389 http_host_port_pair,
90 *session);
[email protected]f45c1ee2010-08-03 00:54:3091}
92
[email protected]ea5ef4c2013-06-13 22:50:2793base::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]b6754252012-06-13 23:14:38100 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]db74b0102012-05-31 19:55:53106
[email protected]1c773ea12009-04-28 19:58:42107} // namespace
108
initial.commit586acc5fe2008-07-26 22:42:52109//-----------------------------------------------------------------------------
110
[email protected]262eec82013-03-19 21:01:36111HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
112 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22113 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32114 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
115 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52116 session_(session),
117 request_(NULL),
[email protected]262eec82013-03-19 21:01:36118 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57119 headers_valid_(false),
[email protected]a53e4d12013-12-07 16:37:24120 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
[email protected]b94f92d2010-10-27 16:45:20121 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52122 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19123 total_received_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54124 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44125 establishing_tunnel_(false),
126 websocket_handshake_stream_base_create_helper_(NULL) {
[email protected]102957f2011-09-02 17:10:14127 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]d7599122014-05-24 03:37:23128 session->GetNextProtos(&server_ssl_config_.next_protos);
[email protected]99ffa5a2011-10-06 04:20:19129 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50130}
131
[email protected]0b0bf032010-09-21 18:08:50132HttpNetworkTransaction::~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]3a8d6852011-03-11 23:43:44148 } else if (stream_->IsSpdyHttpStream()) {
149 // Doesn't really matter for SpdyHttpStream. Just close it.
150 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50151 } else {
152 // Otherwise, we try to drain the response body.
yhiranoa7e05bb2014-11-06 05:40:39153 HttpStream* stream = stream_.release();
[email protected]3fe8d2f82013-10-17 08:56:07154 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50155 }
156 }
157 }
[email protected]02cad5d2013-10-02 08:14:03158
159 if (request_ && request_->upload_data_stream)
160 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50161}
162
[email protected]684970b2009-08-14 04:54:46163int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41164 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53165 const BoundNetLog& net_log) {
[email protected]5e2e6c77d12009-12-24 21:57:16166 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
[email protected]5d0153c512009-01-12 19:08:36167
[email protected]9e743cd2010-03-16 07:03:53168 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04169 request_ = request_info;
170
[email protected]99ffa5a2011-10-06 04:20:19171 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14172 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19173 proxy_ssl_config_.rev_checking_enabled = false;
174 }
[email protected]6fbac162011-06-20 00:29:04175
jkarlinfb1d5172015-01-12 14:10:29176 if (request_->load_flags & LOAD_PREFETCH)
177 response_.unused_since_prefetch = true;
178
[email protected]1ea4f46a2014-04-22 22:33:56179 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49180 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
181 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40182
[email protected]1826a402014-01-08 15:40:48183 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04184 int rv = DoLoop(OK);
185 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41186 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04187 return rv;
188}
189
190int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41191 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38192 DCHECK(!stream_.get());
193 DCHECK(!stream_request_.get());
194 DCHECK_EQ(STATE_NONE, next_state_);
195
[email protected]82918cc2010-08-25 17:24:50196 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38197
[email protected]ccb40e52008-09-17 20:54:40198 int rv = DoLoop(OK);
199 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41200 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11201 return rv;
[email protected]96d570e42008-08-05 22:43:04202}
203
[email protected]0b45559b2009-06-12 21:45:11204int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41205 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38206 // 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]102957f2011-09-02 17:10:14212 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]ec229bc92010-11-22 09:51:45216 session_->ssl_client_auth_cache()->Add(
217 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11218 // Reset the other member variables.
219 // Note: this is necessary only with SSL renegotiation.
220 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50221 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11222 int rv = DoLoop(OK);
223 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41224 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11225 return rv;
226}
227
[email protected]49639fa2011-12-20 23:22:41228int HttpNetworkTransaction::RestartWithAuth(
229 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22230 HttpAuth::Target target = pending_auth_target_;
231 if (target == HttpAuth::AUTH_NONE) {
232 NOTREACHED();
233 return ERR_UNEXPECTED;
234 }
[email protected]0757e7702009-03-27 04:00:22235 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42236
[email protected]f3cf9802011-10-28 18:44:58237 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13238
[email protected]49639fa2011-12-20 23:22:41239 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38240
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]82918cc2010-08-25 17:24:50245 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38246 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59247 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54248 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58249 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54250 } else {
[email protected]8e6441ca2010-08-19 05:56:38251 // 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]a7ea8832010-07-12 17:54:54254 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38255 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54256 }
[email protected]c3b35c22008-09-27 03:19:42257
[email protected]c3b35c22008-09-27 03:19:42258 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41259 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42260 return rv;
[email protected]96d570e42008-08-05 22:43:04261}
262
[email protected]f9ee6b52008-11-08 06:46:23263void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
264 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38265 DCHECK(!stream_request_.get());
266
[email protected]2d2697f92009-02-18 21:00:32267 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57268 // 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]351ab642010-08-05 16:55:31271 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57272 // If the response body hasn't been completely read, we need to drain
273 // it first.
[email protected]351ab642010-08-05 16:55:31274 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32275 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57276 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32277 read_buf_len_ = kDrainBodyBufferSize;
278 return;
279 }
[email protected]0877e3d2009-10-17 22:29:57280 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09281 }
282
[email protected]2d2697f92009-02-18 21:00:32283 // 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
288void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38289 DCHECK(!stream_request_.get());
290
291 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19292 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:58293 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46294 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38295 // We should call connection_->set_idle_time(), but this doesn't occur
296 // often enough to be worth the trouble.
297 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39298 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38299 }
[email protected]697ef4c2010-10-14 16:38:58300
301 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46302 // 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]697ef4c2010-10-14 16:38:58306 next_state_ = STATE_CREATE_STREAM;
307 } else {
[email protected]b8015c42013-12-24 15:18:19308 // Renewed streams shouldn't carry over received bytes.
309 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
[email protected]697ef4c2010-10-14 16:38:58310 next_state_ = STATE_INIT_STREAM;
311 }
312 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32313 }
[email protected]f9ee6b52008-11-08 06:46:23314
315 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58316 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23317}
318
[email protected]8e6441ca2010-08-19 05:56:38319bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
320 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
321 HaveAuth(pending_auth_target_);
322}
323
[email protected]9dea9e1f2009-01-29 00:30:47324int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41325 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04326 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35327 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04328
[email protected]1f14a912009-12-21 20:32:44329 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04330
[email protected]ad8e04a2010-11-01 04:16:27331 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38332 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04333 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29334 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04335 // 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]511f6f52010-12-17 03:58:29338 // 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]8a1f3312010-05-25 19:25:04341 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46342 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58343 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54344 LOG(WARNING) << "Blocked proxy response with status "
345 << headers->response_code() << " to CONNECT request for "
346 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04347 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44348 }
349
[email protected]e60e47a2010-07-14 03:37:18350 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31351 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18352
[email protected]96d570e42008-08-05 22:43:04353 read_buf_ = buf;
354 read_buf_len_ = buf_len;
355
[email protected]1f14a912009-12-21 20:32:44356 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04357 int rv = DoLoop(OK);
358 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41359 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04360 return rv;
361}
362
[email protected]8cd06c02014-01-25 07:50:14363void HttpNetworkTransaction::StopCaching() {}
364
[email protected]79e1fd62013-06-20 06:50:04365bool 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]b8015c42013-12-24 15:18:19372int64 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]8cd06c02014-01-25 07:50:14379void HttpNetworkTransaction::DoneReading() {}
380
[email protected]96d570e42008-08-05 22:43:04381const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
[email protected]90499482013-06-01 00:39:50382 return ((headers_valid_ && response_.headers.get()) ||
383 response_.ssl_info.cert.get() || response_.cert_request_info.get())
384 ? &response_
385 : NULL;
[email protected]96d570e42008-08-05 22:43:04386}
387
388LoadState 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]1826a402014-01-08 15:40:48392 case STATE_CREATE_STREAM:
393 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50394 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38395 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15396 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
397 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57398 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04399 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]196d18a2012-08-30 03:47:31409UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31410 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31411 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04412
yhiranoa7e05bb2014-11-06 05:40:39413 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04414}
415
[email protected]8cd06c02014-01-25 07:50:14416void HttpNetworkTransaction::SetQuicServerInfo(
417 QuicServerInfo* quic_server_info) {}
418
[email protected]5033ab82013-03-22 20:17:46419bool 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]5033ab82013-03-22 20:17:46429 return true;
430}
431
432void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
433 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47434 if (stream_request_)
435 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03436 if (stream_)
437 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46438}
439
[email protected]831e4a32013-11-14 02:14:44440void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
441 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
442 websocket_handshake_stream_base_create_helper_ = create_helper;
443}
444
[email protected]1826a402014-01-08 15:40:48445void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
446 const BeforeNetworkStartCallback& callback) {
447 before_network_start_callback_ = callback;
448}
449
[email protected]597a1ab2014-06-26 08:12:27450void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
451 const BeforeProxyHeadersSentCallback& callback) {
452 before_proxy_headers_sent_callback_ = callback;
453}
454
[email protected]1826a402014-01-08 15:40:48455int HttpNetworkTransaction::ResumeNetworkStart() {
456 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
457 return DoLoop(OK);
458}
459
[email protected]102e27c2011-02-23 01:01:31460void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
461 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39462 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50463 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38464 DCHECK(stream_request_.get());
465
[email protected]b8015c42013-12-24 15:18:19466 if (stream_)
467 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:38468 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14469 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31470 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38471 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51472 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
473 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38474 response_.was_fetched_via_spdy = stream_request_->using_spdy();
475 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15476 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
477 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38478 OnIOComplete(OK);
479}
480
[email protected]a9cf2b92013-10-30 12:08:49481void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50482 const SSLConfig& used_ssl_config,
483 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49484 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44485 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50486}
487
[email protected]102e27c2011-02-23 01:01:31488void HttpNetworkTransaction::OnStreamFailed(int result,
489 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50490 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38491 DCHECK_NE(OK, result);
492 DCHECK(stream_request_.get());
493 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14494 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38495
496 OnIOComplete(result);
497}
498
[email protected]102e27c2011-02-23 01:01:31499void HttpNetworkTransaction::OnCertificateError(
500 int result,
501 const SSLConfig& used_ssl_config,
502 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50503 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38504 DCHECK_NE(OK, result);
505 DCHECK(stream_request_.get());
506 DCHECK(!stream_.get());
507
508 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14509 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38510
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]82918cc2010-08-25 17:24:50513 // 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]8e6441ca2010-08-19 05:56:38516
517 OnIOComplete(result);
518}
519
520void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50521 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31522 const SSLConfig& used_ssl_config,
523 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50524 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38525 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50526 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38527
528 establishing_tunnel_ = true;
529 response_.headers = proxy_response.headers;
530 response_.auth_challenge = proxy_response.auth_challenge;
531 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14532 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31533 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38534
535 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
536 pending_auth_target_ = HttpAuth::AUTH_PROXY;
537
538 DoCallback(OK);
539}
540
541void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31542 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50543 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50544 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38545
[email protected]102957f2011-09-02 17:10:14546 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38547 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58548 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38549}
550
[email protected]511f6f52010-12-17 03:58:29551void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
552 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31553 const SSLConfig& used_ssl_config,
554 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39555 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29556 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
557
558 headers_valid_ = true;
559 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14560 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31561 proxy_info_ = used_proxy_info;
[email protected]b8015c42013-12-24 15:18:19562 if (stream_)
563 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]511f6f52010-12-17 03:58:29564 stream_.reset(stream);
565 stream_request_.reset(); // we're done with the stream request
566 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
567}
568
[email protected]8e6441ca2010-08-19 05:56:38569bool HttpNetworkTransaction::is_https_request() const {
570 return request_->url.SchemeIs("https");
initial.commit586acc5fe2008-07-26 22:42:52571}
572
Adam Rice425cf122015-01-19 06:18:24573bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12574 return (proxy_info_.is_http() || proxy_info_.is_https() ||
575 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24576 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
577}
578
initial.commit586acc5fe2008-07-26 22:42:52579void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50580 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41581 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52582
[email protected]96d570e42008-08-05 22:43:04583 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41584 CompletionCallback c = callback_;
585 callback_.Reset();
586 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52587}
588
589void HttpNetworkTransaction::OnIOComplete(int result) {
vadimt09e7ebe2014-10-29 22:10:41590 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
591 tracked_objects::ScopedTracker tracking_profile1(
vadimt535be342014-10-28 00:13:32592 FROM_HERE_WITH_EXPLICIT_FUNCTION(
593 "424359 HttpNetworkTransaction::OnIOComplete 1"));
594
initial.commit586acc5fe2008-07-26 22:42:52595 int rv = DoLoop(result);
vadimt535be342014-10-28 00:13:32596
vadimt09e7ebe2014-10-29 22:10:41597 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
598 tracked_objects::ScopedTracker tracking_profile2(
vadimt535be342014-10-28 00:13:32599 FROM_HERE_WITH_EXPLICIT_FUNCTION(
600 "424359 HttpNetworkTransaction::OnIOComplete 2"));
601
initial.commit586acc5fe2008-07-26 22:42:52602 if (rv != ERR_IO_PENDING)
603 DoCallback(rv);
604}
605
606int 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]1826a402014-01-08 15:40:48614 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
615 DCHECK_EQ(OK, rv);
616 rv = DoNotifyBeforeCreateStream();
617 break;
[email protected]82918cc2010-08-25 17:24:50618 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]351ab642010-08-05 16:55:31625 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]044de0642010-06-17 10:42:15632 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]daddea62012-09-19 05:51:13646 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]4875ba12011-03-30 22:31:51653 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55654 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38655 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51656 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]0877e3d2009-10-17 22:29:57663 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52664 break;
[email protected]0877e3d2009-10-17 22:29:57665 case STATE_SEND_REQUEST_COMPLETE:
666 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43667 net_log_.EndEventWithNetErrorCode(
668 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52669 break;
670 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55671 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38672 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52673 rv = DoReadHeaders();
674 break;
675 case STATE_READ_HEADERS_COMPLETE:
676 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43677 net_log_.EndEventWithNetErrorCode(
678 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52679 break;
680 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55681 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38682 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52683 rv = DoReadBody();
684 break;
685 case STATE_READ_BODY_COMPLETE:
686 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43687 net_log_.EndEventWithNetErrorCode(
688 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52689 break;
[email protected]2d2697f92009-02-18 21:00:32690 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55691 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53692 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38693 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32694 rv = DoDrainBodyForAuthRestart();
695 break;
696 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
697 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43698 net_log_.EndEventWithNetErrorCode(
699 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32700 break;
initial.commit586acc5fe2008-07-26 22:42:52701 default:
702 NOTREACHED() << "bad state";
703 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04704 break;
initial.commit586acc5fe2008-07-26 22:42:52705 }
706 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
707
708 return rv;
709}
710
[email protected]1826a402014-01-08 15:40:48711int 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]82918cc2010-08-25 17:24:50721int HttpNetworkTransaction::DoCreateStream() {
722 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44723 if (ForWebSocketHandshake()) {
724 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23725 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44726 ->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]26816882010-10-14 18:03:09744 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38745 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31746}
747
[email protected]82918cc2010-08-25 17:24:50748int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
[email protected]394816e92010-08-03 07:38:59749 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50750 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38751 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33752 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
753 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29754 } 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;
bncfacdd852015-01-09 19:22:54758 } else if (result == ERR_HTTP_1_1_REQUIRED ||
759 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
760 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59761 }
762
[email protected]bd0b6772011-01-11 19:59:30763 // 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]8e6441ca2010-08-19 05:56:38767 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09768 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38769 return result;
[email protected]394816e92010-08-03 07:38:59770}
771
[email protected]82918cc2010-08-25 17:24:50772int HttpNetworkTransaction::DoInitStream() {
773 DCHECK(stream_.get());
774 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36775 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50776}
777
778int HttpNetworkTransaction::DoInitStreamComplete(int result) {
779 if (result == OK) {
780 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50781 } else {
[email protected]82918cc2010-08-25 17:24:50782 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26783 result = HandleIOError(result);
784
785 // The stream initialization failed, so this stream will never be useful.
[email protected]b8015c42013-12-24 15:18:19786 if (stream_)
787 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]044dcc52010-09-17 15:44:26788 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50789 }
790
791 return result;
792}
793
[email protected]044de0642010-06-17 10:42:15794int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
795 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
796 if (!ShouldApplyProxyAuth())
797 return OK;
[email protected]394816e92010-08-03 07:38:59798 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
799 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09800 auth_controllers_[target] =
801 new HttpAuthController(target,
802 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31803 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09804 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59805 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41806 io_callback_,
[email protected]394816e92010-08-03 07:38:59807 net_log_);
[email protected]044de0642010-06-17 10:42:15808}
809
810int 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
817int HttpNetworkTransaction::DoGenerateServerAuthToken() {
818 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59819 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54820 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09821 auth_controllers_[target] =
822 new HttpAuthController(target,
823 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31824 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09825 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54826 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
827 auth_controllers_[target]->DisableEmbeddedIdentity();
828 }
[email protected]044de0642010-06-17 10:42:15829 if (!ShouldApplyServerAuth())
830 return OK;
[email protected]394816e92010-08-03 07:38:59831 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41832 io_callback_,
[email protected]394816e92010-08-03 07:38:59833 net_log_);
[email protected]044de0642010-06-17 10:42:15834}
835
836int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
837 DCHECK_NE(ERR_IO_PENDING, rv);
838 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13839 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15840 return rv;
841}
842
Adam Rice425cf122015-01-19 06:18:24843void HttpNetworkTransaction::BuildRequestHeaders(
844 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14845 request_headers_.SetHeader(HttpRequestHeaders::kHost,
846 GetHostAndOptionalPort(request_->url));
847
848 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24849 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14850 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
851 "keep-alive");
852 } else {
853 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
854 }
855
[email protected]2979a492011-04-06 00:29:14856 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11857 if (request_->upload_data_stream) {
858 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14859 request_headers_.SetHeader(
860 HttpRequestHeaders::kTransferEncoding, "chunked");
861 } else {
862 request_headers_.SetHeader(
863 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11864 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14865 }
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]c10450102011-06-27 09:06:16890 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20891
Adam Rice425cf122015-01-19 06:18:24892 if (using_http_proxy_without_tunnel &&
893 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20894 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
895
[email protected]173f8e22013-04-10 04:18:20896 response_.did_use_http_auth =
897 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
898 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14899}
900
[email protected]daddea62012-09-19 05:51:13901int HttpNetworkTransaction::DoInitRequestBody() {
902 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13903 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11904 if (request_->upload_data_stream)
905 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13906 return rv;
907}
[email protected]4875ba12011-03-30 22:31:51908
[email protected]daddea62012-09-19 05:51:13909int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
910 if (result == OK)
911 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13912 return result;
913}
914
915int HttpNetworkTransaction::DoBuildRequest() {
916 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51917 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 Rice425cf122015-01-19 06:18:24922 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
923 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51924 }
925
[email protected]4875ba12011-03-30 22:31:51926 return OK;
927}
928
929int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24930 if (result == OK)
931 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51932 return result;
933}
934
[email protected]0877e3d2009-10-17 22:29:57935int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:25936 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57937 next_state_ = STATE_SEND_REQUEST_COMPLETE;
938
[email protected]bf3eb002012-11-15 05:50:11939 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52940}
941
[email protected]0877e3d2009-10-17 22:29:57942int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25943 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52944 if (result < 0)
945 return HandleIOError(result);
[email protected]7cf7ccb2013-04-20 02:53:08946 response_.network_accessed = true;
[email protected]0877e3d2009-10-17 22:29:57947 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52948 return OK;
949}
950
951int HttpNetworkTransaction::DoReadHeaders() {
952 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41953 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52954}
955
956int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:11957 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
958 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:38959 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]90499482013-06-01 00:39:50971 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:38972 result = HandleCertificateRequest(result);
973 if (result == OK)
974 return result;
[email protected]2181ea002009-06-09 01:37:27975 }
976
bncfacdd852015-01-09 19:22:54977 if (result == ERR_HTTP_1_1_REQUIRED ||
978 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
979 return HandleHttp11Required(result);
980 }
981
[email protected]c871864d72014-03-13 19:56:19982 // 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]90499482013-06-01 00:39:50995 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:52996
[email protected]d58ceea82014-06-04 10:55:54997 // On a 408 response from the server ("Request Timeout") on a stale socket,
998 // retry the request.
[email protected]0aff0d82014-06-14 08:49:04999 // Headers can be NULL because of http://crbug.com/384554.
1000 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541001 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]93f8b562012-03-27 01:00:161011 // 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]3abacd62012-06-10 20:20:321018 net_log_.AddEvent(
1019 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1020 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391021
[email protected]a7e41312009-12-16 23:18:141022 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571023 // 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]4ddaf2502008-10-23 18:26:191029
[email protected]0877e3d2009-10-17 22:29:571030 // 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]831e4a32013-11-14 02:14:441035 // 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]007b3f82013-04-09 08:46:451038 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571039 next_state_ = STATE_READ_HEADERS;
1040 return OK;
1041 }
1042
pkastingcba13292014-11-20 03:35:211043 ProcessAlternateProtocol(session_, *response_.headers.get(),
1044 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421045
[email protected]e772db3f2010-07-12 18:11:131046 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571047 if (rv != OK)
1048 return rv;
1049
[email protected]8536ef52010-09-30 16:18:211050 if (is_https_request())
1051 stream_->GetSSLInfo(&response_.ssl_info);
1052
[email protected]0877e3d2009-10-17 22:29:571053 headers_valid_ = true;
[email protected]e00bfd572014-04-30 08:06:431054
1055 if (session_->huffman_aggregator()) {
1056 session_->huffman_aggregator()->AggregateTransactionCharacterCounts(
1057 *request_,
1058 request_headers_,
1059 proxy_info_.proxy_server(),
dcheng48459ac22014-08-26 00:46:411060 *response_.headers.get());
[email protected]e00bfd572014-04-30 08:06:431061 }
[email protected]0877e3d2009-10-17 22:29:571062 return OK;
initial.commit586acc5fe2008-07-26 22:42:521063}
1064
1065int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:501066 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131067 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381068 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521069
1070 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501071 return stream_->ReadResponseBody(
1072 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521073}
1074
1075int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1076 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381077 bool done = false;
1078 if (result <= 0) {
1079 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521080 done = true;
[email protected]8e6441ca2010-08-19 05:56:381081 }
[email protected]9492e4a2010-02-24 00:58:461082
[email protected]8e6441ca2010-08-19 05:56:381083 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:311084 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:381085 // 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]5c6908e2012-08-06 18:53:471092 // 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.commit586acc5fe2008-07-26 22:42:521099 }
1100
[email protected]8e6441ca2010-08-19 05:56:381101 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521102 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381103 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011104 // 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]8e6441ca2010-08-19 05:56:381110 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521111 }
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]2d2697f92009-02-18 21:00:321120int 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]0877e3d2009-10-17 22:29:571130// 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]2d2697f92009-02-18 21:00:321132int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231133 // 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]2d2697f92009-02-18 21:00:321136 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571137 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321138 done = true;
[email protected]68873ba2009-06-04 21:49:231139 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311140 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571141 done = true;
[email protected]2d2697f92009-02-18 21:00:321142 }
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]5e363962009-06-19 19:57:011154int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381155 // 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]65a3b912010-08-21 05:46:581165 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381166
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]b8015c42013-12-24 15:18:191171 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:381172 stream_->Close(true);
1173 stream_.reset();
1174 }
1175
[email protected]26816882010-10-14 18:03:091176 // The server is asking for a client certificate during the initial
1177 // handshake.
1178 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011179
[email protected]ec229bc92010-11-22 09:51:451180 // 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]90499482013-06-01 00:39:501192 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261193 const std::vector<std::string>& cert_authorities =
1194 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451195
[email protected]f1958c382013-02-07 00:15:261196 bool cert_still_valid = cert_authorities.empty() ||
1197 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451198 if (!cert_still_valid)
1199 return error;
[email protected]5e363962009-06-19 19:57:011200 }
[email protected]ec229bc92010-11-22 09:51:451201
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]102957f2011-09-02 17:10:141205 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]ec229bc92010-11-22 09:51:451209 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]0b45559b2009-06-12 21:45:111214}
1215
bncfacdd852015-01-09 19:22:541216int 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]384cc73a2013-12-08 22:41:031229void 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]791879c2013-12-17 07:22:411233 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031234 }
1235}
1236
[email protected]bd0b6772011-01-11 19:59:301237// 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]1c53a1f2011-01-13 00:36:381240// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301241int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1242 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031243 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301244
[email protected]02d1d442013-08-10 13:38:261245 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231246 uint16 version_max = server_ssl_config_.version_max;
1247
initial.commit586acc5fe2008-07-26 22:42:521248 switch (error) {
[email protected]12833302014-07-02 01:57:311249 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301250 case ERR_SSL_PROTOCOL_ERROR:
1251 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231252 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1253 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171254 // 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]158ac972013-04-19 23:29:231264 version_max--;
[email protected]c57ec022013-11-25 21:30:511265
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]001e547e2013-12-06 16:08:391270 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421271 }
initial.commit586acc5fe2008-07-26 22:42:521272 break;
[email protected]12833302014-07-02 01:57:311273 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]02d1d442013-08-10 13:38:261293 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]a53e4d12013-12-07 16:37:241304 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]02d1d442013-08-10 13:38:261311 }
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]a53e4d12013-12-07 16:37:241319 fallback_error_code_ = error;
[email protected]02d1d442013-08-10 13:38:261320 server_ssl_config_.version_max = version_max;
1321 server_ssl_config_.version_fallback = true;
1322 ResetConnectionAndRequestForResend();
1323 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521324 }
[email protected]158ac972013-04-19 23:29:231325
initial.commit586acc5fe2008-07-26 22:42:521326 return error;
1327}
1328
[email protected]bd0b6772011-01-11 19:59:301329// 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.
1333int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031334 // Because the peer may request renegotiation with client authentication at
1335 // any time, check and handle client authentication errors.
1336 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301337
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]202965992011-12-07 23:04:511346 // 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]a34f61ee2014-03-18 20:59:491354 // 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]b6754252012-06-13 23:14:381359 net_log_.AddEventWithNetErrorCode(
1360 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301361 ResetConnectionAndRequestForResend();
1362 error = OK;
1363 }
1364 break;
[email protected]8753a122011-10-16 08:05:081365 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001366 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271367 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381368 net_log_.AddEventWithNetErrorCode(
1369 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001370 ResetConnectionAndRequestForResend();
1371 error = OK;
1372 break;
[email protected]bd0b6772011-01-11 19:59:301373 }
1374 return error;
1375}
1376
[email protected]c3b35c22008-09-27 03:19:421377void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581378 ResetStateForAuthRestart();
[email protected]b8015c42013-12-24 15:18:191379 if (stream_)
1380 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:581381 stream_.reset();
1382}
1383
1384void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251385 send_start_time_ = base::TimeTicks();
1386 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251387
[email protected]0757e7702009-03-27 04:00:221388 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421389 read_buf_ = NULL;
1390 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571391 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201392 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141393 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381394 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571395}
1396
1397HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501398 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421399}
1400
[email protected]a34f61ee2014-03-18 20:59:491401bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381402 bool connection_is_proven = stream_->IsConnectionReused();
1403 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161404
[email protected]2a5c76b2008-09-25 22:15:161405 // 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]8e6441ca2010-08-19 05:56:381408 if (connection_is_proven && !has_received_headers)
1409 return true;
1410 return false;
[email protected]1c773ea12009-04-28 19:58:421411}
1412
1413void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381414 if (stream_.get()) {
1415 stream_->Close(true);
1416 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161417 }
1418
[email protected]0877e3d2009-10-17 22:29:571419 // 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]b94f92d2010-10-27 16:45:201422 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501423 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541424}
1425
[email protected]1c773ea12009-04-28 19:58:421426bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241427 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421428}
license.botbf09a502008-08-24 00:55:551429
[email protected]1c773ea12009-04-28 19:58:421430bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041431 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421432}
1433
[email protected]e772db3f2010-07-12 18:11:131434int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271435 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501436 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421437
[email protected]0877e3d2009-10-17 22:29:571438 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581439 if (status != HTTP_UNAUTHORIZED &&
1440 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421441 return OK;
[email protected]9094b602012-02-27 21:44:581442 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111443 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161444 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1445 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421446
[email protected]9094b602012-02-27 21:44:581447 // 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]7a67a8152010-11-05 18:31:101450 if (!auth_controllers_[target].get())
1451 return ERR_UNEXPECTED_PROXY_AUTH;
1452
[email protected]a7ea8832010-07-12 17:54:541453 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311454 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1455 net_log_);
[email protected]228404f2010-06-24 04:31:411456 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]228404f2010-06-24 04:31:411464 return rv;
[email protected]f9ee6b52008-11-08 06:46:231465}
1466
[email protected]8e6441ca2010-08-19 05:56:381467bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1468 return auth_controllers_[target].get() &&
1469 auth_controllers_[target]->HaveAuth();
1470}
1471
[email protected]228404f2010-06-24 04:31:411472GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1473 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461474 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411475 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]2df19bb2010-08-25 20:13:461479 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1480 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351481 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461482 }
[email protected]228404f2010-06-24 04:31:411483 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291484 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]228404f2010-06-24 04:31:411495 return request_->url;
1496 default:
1497 return GURL();
1498 }
[email protected]c3b35c22008-09-27 03:19:421499}
1500
[email protected]831e4a32013-11-14 02:14:441501bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141502 return websocket_handshake_stream_base_create_helper_ &&
1503 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441504}
1505
[email protected]d8eb84242010-09-25 02:25:061506#define STATE_CASE(s) \
1507 case s: \
1508 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1509 break
[email protected]aef04272010-06-28 18:03:041510
1511std::string HttpNetworkTransaction::DescribeState(State state) {
1512 std::string description;
1513 switch (state) {
[email protected]1826a402014-01-08 15:40:481514 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501515 STATE_CASE(STATE_CREATE_STREAM);
1516 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131517 STATE_CASE(STATE_INIT_REQUEST_BODY);
1518 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511519 STATE_CASE(STATE_BUILD_REQUEST);
1520 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041521 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]aef04272010-06-28 18:03:041525 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]aef04272010-06-28 18:03:041529 STATE_CASE(STATE_NONE);
1530 default:
[email protected]d8eb84242010-09-25 02:25:061531 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1532 state);
[email protected]aef04272010-06-28 18:03:041533 break;
1534 }
1535 return description;
1536}
1537
1538#undef STATE_CASE
1539
[email protected]c3b35c22008-09-27 03:19:421540} // namespace net