blob: 9e24da8863e1c35eb6c5fa25effdf1ded10274b9 [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"
[email protected]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4719#include "base/strings/string_number_conversions.h"
20#include "base/strings/string_util.h"
21#include "base/strings/stringprintf.h"
[email protected]db74b0102012-05-31 19:55:5322#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3023#include "build/build_config.h"
[email protected]277d5942010-08-11 21:02:3524#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5525#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1926#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5227#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2528#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2629#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4230#include "net/base/net_util.h"
initial.commit586acc5fe2008-07-26 22:42:5231#include "net/base/upload_data_stream.h"
[email protected]c3b35c22008-09-27 03:19:4232#include "net/http/http_auth.h"
33#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2434#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3635#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5236#include "net/http/http_chunked_decoder.h"
37#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5438#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1339#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4740#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5241#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2142#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5743#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5344#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5845#include "net/http/http_status_code.h"
[email protected]92b9a3e2012-11-09 08:52:5246#include "net/http/http_stream_base.h"
[email protected]102e27c2011-02-23 01:01:3147#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5248#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2349#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3650#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4451#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2652#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4453#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1854#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2855#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4056#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3257#include "net/spdy/spdy_session.h"
58#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5759#include "net/ssl/ssl_cert_request_info.h"
60#include "net/ssl/ssl_connection_status_flags.h"
[email protected]f89276a72013-07-12 06:41:5461#include "url/gurl.h"
initial.commit586acc5fe2008-07-26 22:42:5262
[email protected]e1acf6f2008-10-27 20:43:3363using base::Time;
64
initial.commit586acc5fe2008-07-26 22:42:5265namespace net {
66
[email protected]1c773ea12009-04-28 19:58:4267namespace {
68
[email protected]30d4c022013-07-18 22:58:1669void ProcessAlternateProtocol(
70 HttpStreamFactory* factory,
71 const base::WeakPtr<HttpServerProperties>& http_server_properties,
72 const HttpResponseHeaders& headers,
73 const HostPortPair& http_host_port_pair) {
[email protected]564b4912010-03-09 16:30:4274 std::string alternate_protocol_str;
[email protected]8e6441ca2010-08-19 05:56:3875
[email protected]17291a022011-10-10 07:32:5376 if (!headers.EnumerateHeader(NULL, kAlternateProtocolHeader,
[email protected]564b4912010-03-09 16:30:4277 &alternate_protocol_str)) {
78 // Header is not present.
79 return;
80 }
81
[email protected]17291a022011-10-10 07:32:5382 factory->ProcessAlternateProtocol(http_server_properties,
[email protected]8e6441ca2010-08-19 05:56:3883 alternate_protocol_str,
84 http_host_port_pair);
[email protected]f45c1ee2010-08-03 00:54:3085}
86
[email protected]a1cb2cd2011-02-05 02:02:0887// Returns true if |error| is a client certificate authentication error.
88bool IsClientCertificateError(int error) {
89 switch (error) {
90 case ERR_BAD_SSL_CLIENT_AUTH_CERT:
91 case ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED:
92 case ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY:
93 case ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED:
94 return true;
95 default:
96 return false;
97 }
98}
99
[email protected]ea5ef4c2013-06-13 22:50:27100base::Value* NetLogSSLVersionFallbackCallback(
101 const GURL* url,
102 int net_error,
103 uint16 version_before,
104 uint16 version_after,
105 NetLog::LogLevel /* log_level */) {
106 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]b6754252012-06-13 23:14:38107 dict->SetString("host_and_port", GetHostAndPort(*url));
108 dict->SetInteger("net_error", net_error);
109 dict->SetInteger("version_before", version_before);
110 dict->SetInteger("version_after", version_after);
111 return dict;
112}
[email protected]db74b0102012-05-31 19:55:53113
[email protected]1c773ea12009-04-28 19:58:42114} // namespace
115
initial.commit586acc5fe2008-07-26 22:42:52116//-----------------------------------------------------------------------------
117
[email protected]262eec82013-03-19 21:01:36118HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
119 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22120 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32121 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
122 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52123 session_(session),
124 request_(NULL),
[email protected]262eec82013-03-19 21:01:36125 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57126 headers_valid_(false),
[email protected]8e3d2d32010-06-13 18:46:23127 logged_response_time_(false),
[email protected]b94f92d2010-10-27 16:45:20128 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52129 read_buf_len_(0),
[email protected]a7ea8832010-07-12 17:54:54130 next_state_(STATE_NONE),
131 establishing_tunnel_(false) {
[email protected]102957f2011-09-02 17:10:14132 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]5285d972011-10-18 18:56:34133 if (session->http_stream_factory()->has_next_protos()) {
[email protected]102957f2011-09-02 17:10:14134 server_ssl_config_.next_protos =
[email protected]5285d972011-10-18 18:56:34135 session->http_stream_factory()->next_protos();
[email protected]102957f2011-09-02 17:10:14136 }
[email protected]99ffa5a2011-10-06 04:20:19137 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50138}
139
[email protected]0b0bf032010-09-21 18:08:50140HttpNetworkTransaction::~HttpNetworkTransaction() {
141 if (stream_.get()) {
142 HttpResponseHeaders* headers = GetResponseHeaders();
143 // TODO(mbelshe): The stream_ should be able to compute whether or not the
144 // stream should be kept alive. No reason to compute here
145 // and pass it in.
146 bool try_to_keep_alive =
147 next_state_ == STATE_NONE &&
148 stream_->CanFindEndOfResponse() &&
149 (!headers || headers->IsKeepAlive());
150 if (!try_to_keep_alive) {
151 stream_->Close(true /* not reusable */);
152 } else {
153 if (stream_->IsResponseBodyComplete()) {
154 // If the response body is complete, we can just reuse the socket.
155 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44156 } else if (stream_->IsSpdyHttpStream()) {
157 // Doesn't really matter for SpdyHttpStream. Just close it.
158 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50159 } else {
160 // Otherwise, we try to drain the response body.
[email protected]92b9a3e2012-11-09 08:52:52161 HttpStreamBase* stream = stream_.release();
[email protected]90499482013-06-01 00:39:50162 stream->Drain(session_.get());
[email protected]0b0bf032010-09-21 18:08:50163 }
164 }
165 }
[email protected]02cad5d2013-10-02 08:14:03166
167 if (request_ && request_->upload_data_stream)
168 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50169}
170
[email protected]684970b2009-08-14 04:54:46171int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41172 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53173 const BoundNetLog& net_log) {
[email protected]5e2e6c77d12009-12-24 21:57:16174 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
[email protected]5d0153c512009-01-12 19:08:36175
[email protected]9e743cd2010-03-16 07:03:53176 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04177 request_ = request_info;
[email protected]21b316a2009-03-23 18:25:06178 start_time_ = base::Time::Now();
[email protected]96d570e42008-08-05 22:43:04179
[email protected]99ffa5a2011-10-06 04:20:19180 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14181 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19182 proxy_ssl_config_.rev_checking_enabled = false;
183 }
[email protected]6fbac162011-06-20 00:29:04184
[email protected]e6d017652013-05-17 18:01:40185 // Channel ID is enabled unless --disable-tls-channel-id flag is set,
186 // or if privacy mode is enabled.
187 bool channel_id_enabled = server_ssl_config_.channel_id_enabled &&
188 (request_->privacy_mode == kPrivacyModeDisabled);
189 server_ssl_config_.channel_id_enabled = channel_id_enabled;
190
[email protected]82918cc2010-08-25 17:24:50191 next_state_ = STATE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04192 int rv = DoLoop(OK);
193 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41194 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04195 return rv;
196}
197
198int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41199 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38200 DCHECK(!stream_.get());
201 DCHECK(!stream_request_.get());
202 DCHECK_EQ(STATE_NONE, next_state_);
203
[email protected]82918cc2010-08-25 17:24:50204 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38205
[email protected]ccb40e52008-09-17 20:54:40206 int rv = DoLoop(OK);
207 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41208 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11209 return rv;
[email protected]96d570e42008-08-05 22:43:04210}
211
[email protected]0b45559b2009-06-12 21:45:11212int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41213 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38214 // In HandleCertificateRequest(), we always tear down existing stream
215 // requests to force a new connection. So we shouldn't have one here.
216 DCHECK(!stream_request_.get());
217 DCHECK(!stream_.get());
218 DCHECK_EQ(STATE_NONE, next_state_);
219
[email protected]102957f2011-09-02 17:10:14220 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
221 &proxy_ssl_config_ : &server_ssl_config_;
222 ssl_config->send_client_cert = true;
223 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45224 session_->ssl_client_auth_cache()->Add(
225 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11226 // Reset the other member variables.
227 // Note: this is necessary only with SSL renegotiation.
228 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50229 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11230 int rv = DoLoop(OK);
231 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41232 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11233 return rv;
234}
235
[email protected]49639fa2011-12-20 23:22:41236int HttpNetworkTransaction::RestartWithAuth(
237 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22238 HttpAuth::Target target = pending_auth_target_;
239 if (target == HttpAuth::AUTH_NONE) {
240 NOTREACHED();
241 return ERR_UNEXPECTED;
242 }
[email protected]0757e7702009-03-27 04:00:22243 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42244
[email protected]f3cf9802011-10-28 18:44:58245 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13246
[email protected]49639fa2011-12-20 23:22:41247 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38248
249 int rv = OK;
250 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
251 // In this case, we've gathered credentials for use with proxy
252 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50253 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38254 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59255 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54256 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58257 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54258 } else {
[email protected]8e6441ca2010-08-19 05:56:38259 // In this case, we've gathered credentials for the server or the proxy
260 // but it is not during the tunneling phase.
261 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54262 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38263 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54264 }
[email protected]c3b35c22008-09-27 03:19:42265
[email protected]c3b35c22008-09-27 03:19:42266 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41267 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42268 return rv;
[email protected]96d570e42008-08-05 22:43:04269}
270
[email protected]f9ee6b52008-11-08 06:46:23271void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
272 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38273 DCHECK(!stream_request_.get());
274
[email protected]2d2697f92009-02-18 21:00:32275 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57276 // Even if the server says the connection is keep-alive, we have to be
277 // able to find the end of each response in order to reuse the connection.
278 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31279 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57280 // If the response body hasn't been completely read, we need to drain
281 // it first.
[email protected]351ab642010-08-05 16:55:31282 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32283 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57284 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32285 read_buf_len_ = kDrainBodyBufferSize;
286 return;
287 }
[email protected]0877e3d2009-10-17 22:29:57288 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09289 }
290
[email protected]2d2697f92009-02-18 21:00:32291 // We don't need to drain the response body, so we act as if we had drained
292 // the response body.
293 DidDrainBodyForAuthRestart(keep_alive);
294}
295
296void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38297 DCHECK(!stream_request_.get());
298
299 if (stream_.get()) {
[email protected]697ef4c2010-10-14 16:38:58300 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46301 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38302 // We should call connection_->set_idle_time(), but this doesn't occur
303 // often enough to be worth the trouble.
304 stream_->SetConnectionReused();
[email protected]92b9a3e2012-11-09 08:52:52305 new_stream =
306 static_cast<HttpStream*>(stream_.get())->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38307 }
[email protected]697ef4c2010-10-14 16:38:58308
309 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46310 // Close the stream and mark it as not_reusable. Even in the
311 // keep_alive case, we've determined that the stream_ is not
312 // reusable if new_stream is NULL.
313 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58314 next_state_ = STATE_CREATE_STREAM;
315 } else {
316 next_state_ = STATE_INIT_STREAM;
317 }
318 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32319 }
[email protected]f9ee6b52008-11-08 06:46:23320
321 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58322 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23323}
324
[email protected]8e6441ca2010-08-19 05:56:38325bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
326 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
327 HaveAuth(pending_auth_target_);
328}
329
[email protected]9dea9e1f2009-01-29 00:30:47330int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41331 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04332 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35333 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04334
[email protected]1f14a912009-12-21 20:32:44335 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04336
[email protected]ad8e04a2010-11-01 04:16:27337 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38338 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04339 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29340 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04341 // bytes when establishing a tunnel because they might be controlled by
342 // an active network attacker. We don't worry about this for HTTP
343 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29344 // We reach this case when the user cancels a 407 proxy auth prompt. We
345 // also don't worry about this for an HTTPS Proxy, because the
346 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04347 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46348 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58349 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54350 LOG(WARNING) << "Blocked proxy response with status "
351 << headers->response_code() << " to CONNECT request for "
352 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04353 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44354 }
355
[email protected]e60e47a2010-07-14 03:37:18356 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31357 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18358
[email protected]96d570e42008-08-05 22:43:04359 read_buf_ = buf;
360 read_buf_len_ = buf_len;
361
[email protected]1f14a912009-12-21 20:32:44362 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04363 int rv = DoLoop(OK);
364 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41365 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04366 return rv;
367}
368
[email protected]79e1fd62013-06-20 06:50:04369bool HttpNetworkTransaction::GetFullRequestHeaders(
370 HttpRequestHeaders* headers) const {
371 // TODO(ttuttle): Make sure we've populated request_headers_.
372 *headers = request_headers_;
373 return true;
374}
375
[email protected]96d570e42008-08-05 22:43:04376const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
[email protected]90499482013-06-01 00:39:50377 return ((headers_valid_ && response_.headers.get()) ||
378 response_.ssl_info.cert.get() || response_.cert_request_info.get())
379 ? &response_
380 : NULL;
[email protected]96d570e42008-08-05 22:43:04381}
382
383LoadState HttpNetworkTransaction::GetLoadState() const {
384 // TODO(wtc): Define a new LoadState value for the
385 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
386 switch (next_state_) {
[email protected]82918cc2010-08-25 17:24:50387 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38388 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15389 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
390 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57391 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04392 return LOAD_STATE_SENDING_REQUEST;
393 case STATE_READ_HEADERS_COMPLETE:
394 return LOAD_STATE_WAITING_FOR_RESPONSE;
395 case STATE_READ_BODY_COMPLETE:
396 return LOAD_STATE_READING_RESPONSE;
397 default:
398 return LOAD_STATE_IDLE;
399 }
400}
401
[email protected]196d18a2012-08-30 03:47:31402UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31403 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31404 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04405
[email protected]92b9a3e2012-11-09 08:52:52406 // TODO(bashi): This cast is temporary. Remove later.
407 return static_cast<HttpStream*>(stream_.get())->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04408}
409
[email protected]5033ab82013-03-22 20:17:46410bool HttpNetworkTransaction::GetLoadTimingInfo(
411 LoadTimingInfo* load_timing_info) const {
412 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
413 return false;
414
415 load_timing_info->proxy_resolve_start =
416 proxy_info_.proxy_resolve_start_time();
417 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
418 load_timing_info->send_start = send_start_time_;
419 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46420 return true;
421}
422
423void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
424 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47425 if (stream_request_)
426 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03427 if (stream_)
428 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46429}
430
[email protected]102e27c2011-02-23 01:01:31431void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
432 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52433 HttpStreamBase* stream) {
[email protected]82918cc2010-08-25 17:24:50434 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38435 DCHECK(stream_request_.get());
436
437 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14438 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31439 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38440 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51441 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
442 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38443 response_.was_fetched_via_spdy = stream_request_->using_spdy();
444 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]8e6441ca2010-08-19 05:56:38445
446 OnIOComplete(OK);
447}
448
[email protected]3732cea2013-06-21 06:50:50449void HttpNetworkTransaction::OnWebSocketStreamReady(
450 const SSLConfig& used_ssl_config,
451 const ProxyInfo& used_proxy_info,
452 WebSocketStreamBase* stream) {
453 NOTREACHED() << "This function should never be called.";
454}
455
[email protected]102e27c2011-02-23 01:01:31456void HttpNetworkTransaction::OnStreamFailed(int result,
457 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50458 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38459 DCHECK_NE(OK, result);
460 DCHECK(stream_request_.get());
461 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14462 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38463
464 OnIOComplete(result);
465}
466
[email protected]102e27c2011-02-23 01:01:31467void HttpNetworkTransaction::OnCertificateError(
468 int result,
469 const SSLConfig& used_ssl_config,
470 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50471 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38472 DCHECK_NE(OK, result);
473 DCHECK(stream_request_.get());
474 DCHECK(!stream_.get());
475
476 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14477 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38478
479 // TODO(mbelshe): For now, we're going to pass the error through, and that
480 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50481 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
482 // good and the user chooses to ignore the error. This is not ideal, but not
483 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38484
485 OnIOComplete(result);
486}
487
488void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50489 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31490 const SSLConfig& used_ssl_config,
491 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50492 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38493 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50494 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38495
496 establishing_tunnel_ = true;
497 response_.headers = proxy_response.headers;
498 response_.auth_challenge = proxy_response.auth_challenge;
499 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14500 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31501 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38502
503 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
504 pending_auth_target_ = HttpAuth::AUTH_PROXY;
505
506 DoCallback(OK);
507}
508
509void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31510 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50511 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50512 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38513
[email protected]102957f2011-09-02 17:10:14514 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38515 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58516 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38517}
518
[email protected]511f6f52010-12-17 03:58:29519void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
520 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31521 const SSLConfig& used_ssl_config,
522 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52523 HttpStreamBase* stream) {
[email protected]511f6f52010-12-17 03:58:29524 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
525
526 headers_valid_ = true;
527 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14528 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31529 proxy_info_ = used_proxy_info;
[email protected]511f6f52010-12-17 03:58:29530 stream_.reset(stream);
531 stream_request_.reset(); // we're done with the stream request
532 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
533}
534
[email protected]8e6441ca2010-08-19 05:56:38535bool HttpNetworkTransaction::is_https_request() const {
536 return request_->url.SchemeIs("https");
initial.commit586acc5fe2008-07-26 22:42:52537}
538
initial.commit586acc5fe2008-07-26 22:42:52539void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50540 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41541 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52542
[email protected]96d570e42008-08-05 22:43:04543 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41544 CompletionCallback c = callback_;
545 callback_.Reset();
546 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52547}
548
549void HttpNetworkTransaction::OnIOComplete(int result) {
550 int rv = DoLoop(result);
551 if (rv != ERR_IO_PENDING)
552 DoCallback(rv);
553}
554
555int HttpNetworkTransaction::DoLoop(int result) {
556 DCHECK(next_state_ != STATE_NONE);
557
558 int rv = result;
559 do {
560 State state = next_state_;
561 next_state_ = STATE_NONE;
562 switch (state) {
[email protected]82918cc2010-08-25 17:24:50563 case STATE_CREATE_STREAM:
564 DCHECK_EQ(OK, rv);
565 rv = DoCreateStream();
566 break;
567 case STATE_CREATE_STREAM_COMPLETE:
568 rv = DoCreateStreamComplete(rv);
569 break;
[email protected]351ab642010-08-05 16:55:31570 case STATE_INIT_STREAM:
571 DCHECK_EQ(OK, rv);
572 rv = DoInitStream();
573 break;
574 case STATE_INIT_STREAM_COMPLETE:
575 rv = DoInitStreamComplete(rv);
576 break;
[email protected]044de0642010-06-17 10:42:15577 case STATE_GENERATE_PROXY_AUTH_TOKEN:
578 DCHECK_EQ(OK, rv);
579 rv = DoGenerateProxyAuthToken();
580 break;
581 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
582 rv = DoGenerateProxyAuthTokenComplete(rv);
583 break;
584 case STATE_GENERATE_SERVER_AUTH_TOKEN:
585 DCHECK_EQ(OK, rv);
586 rv = DoGenerateServerAuthToken();
587 break;
588 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
589 rv = DoGenerateServerAuthTokenComplete(rv);
590 break;
[email protected]daddea62012-09-19 05:51:13591 case STATE_INIT_REQUEST_BODY:
592 DCHECK_EQ(OK, rv);
593 rv = DoInitRequestBody();
594 break;
595 case STATE_INIT_REQUEST_BODY_COMPLETE:
596 rv = DoInitRequestBodyComplete(rv);
597 break;
[email protected]4875ba12011-03-30 22:31:51598 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55599 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38600 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51601 rv = DoBuildRequest();
602 break;
603 case STATE_BUILD_REQUEST_COMPLETE:
604 rv = DoBuildRequestComplete(rv);
605 break;
606 case STATE_SEND_REQUEST:
607 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57608 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52609 break;
[email protected]0877e3d2009-10-17 22:29:57610 case STATE_SEND_REQUEST_COMPLETE:
611 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43612 net_log_.EndEventWithNetErrorCode(
613 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52614 break;
615 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55616 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38617 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52618 rv = DoReadHeaders();
619 break;
620 case STATE_READ_HEADERS_COMPLETE:
621 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43622 net_log_.EndEventWithNetErrorCode(
623 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52624 break;
625 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55626 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38627 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52628 rv = DoReadBody();
629 break;
630 case STATE_READ_BODY_COMPLETE:
631 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43632 net_log_.EndEventWithNetErrorCode(
633 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52634 break;
[email protected]2d2697f92009-02-18 21:00:32635 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55636 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53637 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38638 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32639 rv = DoDrainBodyForAuthRestart();
640 break;
641 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
642 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43643 net_log_.EndEventWithNetErrorCode(
644 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32645 break;
initial.commit586acc5fe2008-07-26 22:42:52646 default:
647 NOTREACHED() << "bad state";
648 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04649 break;
initial.commit586acc5fe2008-07-26 22:42:52650 }
651 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
652
653 return rv;
654}
655
[email protected]82918cc2010-08-25 17:24:50656int HttpNetworkTransaction::DoCreateStream() {
657 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]351ab642010-08-05 16:55:31658
[email protected]26816882010-10-14 18:03:09659 stream_request_.reset(
660 session_->http_stream_factory()->RequestStream(
[email protected]102e27c2011-02-23 01:01:31661 *request_,
[email protected]262eec82013-03-19 21:01:36662 priority_,
[email protected]102957f2011-09-02 17:10:14663 server_ssl_config_,
664 proxy_ssl_config_,
[email protected]26816882010-10-14 18:03:09665 this,
666 net_log_));
667 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38668 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31669}
670
[email protected]82918cc2010-08-25 17:24:50671int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
[email protected]394816e92010-08-03 07:38:59672 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50673 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38674 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33675 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
676 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29677 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
678 // Return OK and let the caller read the proxy's error page
679 next_state_ = STATE_NONE;
680 return OK;
[email protected]394816e92010-08-03 07:38:59681 }
682
[email protected]bd0b6772011-01-11 19:59:30683 // Handle possible handshake errors that may have occurred if the stream
684 // used SSL for one or more of the layers.
685 result = HandleSSLHandshakeError(result);
686
[email protected]8e6441ca2010-08-19 05:56:38687 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09688 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38689 return result;
[email protected]394816e92010-08-03 07:38:59690}
691
[email protected]82918cc2010-08-25 17:24:50692int HttpNetworkTransaction::DoInitStream() {
693 DCHECK(stream_.get());
694 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36695 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50696}
697
698int HttpNetworkTransaction::DoInitStreamComplete(int result) {
699 if (result == OK) {
700 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50701 } else {
[email protected]82918cc2010-08-25 17:24:50702 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26703 result = HandleIOError(result);
704
705 // The stream initialization failed, so this stream will never be useful.
706 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50707 }
708
709 return result;
710}
711
[email protected]044de0642010-06-17 10:42:15712int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
713 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
714 if (!ShouldApplyProxyAuth())
715 return OK;
[email protected]394816e92010-08-03 07:38:59716 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
717 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09718 auth_controllers_[target] =
719 new HttpAuthController(target,
720 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31721 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09722 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59723 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41724 io_callback_,
[email protected]394816e92010-08-03 07:38:59725 net_log_);
[email protected]044de0642010-06-17 10:42:15726}
727
728int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
729 DCHECK_NE(ERR_IO_PENDING, rv);
730 if (rv == OK)
731 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
732 return rv;
733}
734
735int HttpNetworkTransaction::DoGenerateServerAuthToken() {
736 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59737 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
738 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09739 auth_controllers_[target] =
740 new HttpAuthController(target,
741 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31742 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09743 session_->http_auth_handler_factory());
[email protected]044de0642010-06-17 10:42:15744 if (!ShouldApplyServerAuth())
745 return OK;
[email protected]394816e92010-08-03 07:38:59746 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41747 io_callback_,
[email protected]394816e92010-08-03 07:38:59748 net_log_);
[email protected]044de0642010-06-17 10:42:15749}
750
751int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
752 DCHECK_NE(ERR_IO_PENDING, rv);
753 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13754 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15755 return rv;
756}
757
[email protected]2979a492011-04-06 00:29:14758void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy) {
759 request_headers_.SetHeader(HttpRequestHeaders::kHost,
760 GetHostAndOptionalPort(request_->url));
761
762 // For compat with HTTP/1.0 servers and proxies:
763 if (using_proxy) {
764 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
765 "keep-alive");
766 } else {
767 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
768 }
769
[email protected]2979a492011-04-06 00:29:14770 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11771 if (request_->upload_data_stream) {
772 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14773 request_headers_.SetHeader(
774 HttpRequestHeaders::kTransferEncoding, "chunked");
775 } else {
776 request_headers_.SetHeader(
777 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11778 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14779 }
780 } else if (request_->method == "POST" || request_->method == "PUT" ||
781 request_->method == "HEAD") {
782 // An empty POST/PUT request still needs a content length. As for HEAD,
783 // IE and Safari also add a content length header. Presumably it is to
784 // support sending a HEAD request to an URL that only expects to be sent a
785 // POST or some other method that normally would have a message body.
786 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
787 }
788
789 // Honor load flags that impact proxy caches.
790 if (request_->load_flags & LOAD_BYPASS_CACHE) {
791 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
792 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
793 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
794 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
795 }
796
797 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
798 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
799 &request_headers_);
800 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
801 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
802 &request_headers_);
803
[email protected]c10450102011-06-27 09:06:16804 request_headers_.MergeFrom(request_->extra_headers);
[email protected]173f8e22013-04-10 04:18:20805 response_.did_use_http_auth =
806 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
807 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14808}
809
[email protected]daddea62012-09-19 05:51:13810int HttpNetworkTransaction::DoInitRequestBody() {
811 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13812 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11813 if (request_->upload_data_stream)
814 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13815 return rv;
816}
[email protected]4875ba12011-03-30 22:31:51817
[email protected]daddea62012-09-19 05:51:13818int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
819 if (result == OK)
820 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13821 return result;
822}
823
824int HttpNetworkTransaction::DoBuildRequest() {
825 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51826 headers_valid_ = false;
827
828 // This is constructed lazily (instead of within our Start method), so that
829 // we have proxy info available.
830 if (request_headers_.IsEmpty()) {
831 bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) &&
832 !is_https_request();
[email protected]2979a492011-04-06 00:29:14833 BuildRequestHeaders(using_proxy);
[email protected]4875ba12011-03-30 22:31:51834 }
835
[email protected]4875ba12011-03-30 22:31:51836 return OK;
837}
838
839int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24840 if (result == OK)
841 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51842 return result;
843}
844
[email protected]0877e3d2009-10-17 22:29:57845int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:25846 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57847 next_state_ = STATE_SEND_REQUEST_COMPLETE;
848
[email protected]bf3eb002012-11-15 05:50:11849 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52850}
851
[email protected]0877e3d2009-10-17 22:29:57852int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25853 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52854 if (result < 0)
855 return HandleIOError(result);
[email protected]7cf7ccb2013-04-20 02:53:08856 response_.network_accessed = true;
[email protected]0877e3d2009-10-17 22:29:57857 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52858 return OK;
859}
860
861int HttpNetworkTransaction::DoReadHeaders() {
862 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41863 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52864}
865
[email protected]0e75a732008-10-16 20:36:09866int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() {
[email protected]90499482013-06-01 00:39:50867 if (!response_.headers.get() && !stream_->IsConnectionReused()) {
[email protected]0e75a732008-10-16 20:36:09868 // The connection was closed before any data was sent. Likely an error
869 // rather than empty HTTP/0.9 response.
[email protected]aecfbf22008-10-16 02:02:47870 return ERR_EMPTY_RESPONSE;
871 }
872
[email protected]aecfbf22008-10-16 02:02:47873 return OK;
874}
875
initial.commit586acc5fe2008-07-26 22:42:52876int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:11877 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
878 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:38879 if (IsCertificateError(result)) {
880 // We don't handle a certificate error during SSL renegotiation, so we
881 // have to return an error that's not in the certificate error range
882 // (-2xx).
883 LOG(ERROR) << "Got a server certificate with error " << result
884 << " during SSL renegotiation";
885 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
886 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
887 // TODO(wtc): Need a test case for this code path!
888 DCHECK(stream_.get());
889 DCHECK(is_https_request());
890 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:50891 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:38892 result = HandleCertificateRequest(result);
893 if (result == OK)
894 return result;
[email protected]2181ea002009-06-09 01:37:27895 }
896
[email protected]0877e3d2009-10-17 22:29:57897 if (result < 0 && result != ERR_CONNECTION_CLOSED)
initial.commit586acc5fe2008-07-26 22:42:52898 return HandleIOError(result);
899
[email protected]0877e3d2009-10-17 22:29:57900 if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) {
[email protected]1c773ea12009-04-28 19:58:42901 ResetConnectionAndRequestForResend();
[email protected]0877e3d2009-10-17 22:29:57902 return OK;
[email protected]1c773ea12009-04-28 19:58:42903 }
[email protected]2a5c76b2008-09-25 22:15:16904
[email protected]0877e3d2009-10-17 22:29:57905 // After we call RestartWithAuth a new response_time will be recorded, and
906 // we need to be cautious about incorrectly logging the duration across the
907 // authentication activity.
[email protected]8e6441ca2010-08-19 05:56:38908 if (result == OK)
909 LogTransactionConnectedMetrics();
initial.commit586acc5fe2008-07-26 22:42:52910
[email protected]0877e3d2009-10-17 22:29:57911 if (result == ERR_CONNECTION_CLOSED) {
[email protected]02c92c492010-03-08 21:28:14912 // For now, if we get at least some data, we do the best we can to make
[email protected]9492e4a2010-02-24 00:58:46913 // sense of it and send it back up the stack.
[email protected]0e75a732008-10-16 20:36:09914 int rv = HandleConnectionClosedBeforeEndOfHeaders();
[email protected]aecfbf22008-10-16 02:02:47915 if (rv != OK)
916 return rv;
[email protected]0877e3d2009-10-17 22:29:57917 }
[email protected]90499482013-06-01 00:39:50918 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:52919
[email protected]14b7e9a2012-10-16 19:51:29920 // Server-induced fallback is supported only if this is a PAC configured
921 // proxy. See: http://crbug.com/143712
[email protected]2694ceea2013-05-15 04:32:00922 if (response_.was_fetched_via_proxy && proxy_info_.did_use_pac_script() &&
[email protected]90499482013-06-01 00:39:50923 response_.headers.get() != NULL) {
[email protected]2694ceea2013-05-15 04:32:00924 bool should_fallback =
925 response_.headers->HasHeaderValue("connection", "proxy-bypass");
926 // Additionally, fallback if a 500 is returned via the data reduction proxy.
927 // This is conservative, as the 500 might have been generated by the origin,
928 // and not the proxy.
929#if defined(SPDY_PROXY_AUTH_ORIGIN)
930 if (!should_fallback) {
931 should_fallback =
932 response_.headers->response_code() == HTTP_INTERNAL_SERVER_ERROR &&
933 proxy_info_.proxy_server().host_port_pair().Equals(
934 HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)));
935 }
936#endif
937 if (should_fallback) {
[email protected]14b7e9a2012-10-16 19:51:29938 ProxyService* proxy_service = session_->proxy_service();
939 if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) {
940 // Only retry in the case of GETs. We don't want to resubmit a POST
941 // if the proxy took some action.
942 if (request_->method == "GET") {
943 ResetConnectionAndRequestForResend();
944 return OK;
945 }
946 }
947 }
948 }
949
[email protected]93f8b562012-03-27 01:00:16950 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
951 if (request_->load_flags & LOAD_MAIN_FRAME) {
952 const int response_code = response_.headers->response_code();
953 UMA_HISTOGRAM_ENUMERATION(
954 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
955 }
956
[email protected]3abacd62012-06-10 20:20:32957 net_log_.AddEvent(
958 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
959 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:39960
[email protected]a7e41312009-12-16 23:18:14961 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:57962 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
963 // indicates a buggy server. See:
964 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
965 if (request_->method == "PUT")
966 return ERR_METHOD_NOT_SUPPORTED;
967 }
[email protected]4ddaf2502008-10-23 18:26:19968
[email protected]0877e3d2009-10-17 22:29:57969 // Check for an intermediate 100 Continue response. An origin server is
970 // allowed to send this response even if we didn't ask for it, so we just
971 // need to skip over it.
972 // We treat any other 1xx in this same way (although in practice getting
973 // a 1xx that isn't a 100 is rare).
[email protected]a7e41312009-12-16 23:18:14974 if (response_.headers->response_code() / 100 == 1) {
[email protected]007b3f82013-04-09 08:46:45975 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:57976 next_state_ = STATE_READ_HEADERS;
977 return OK;
978 }
979
[email protected]8e6441ca2010-08-19 05:56:38980 HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(),
981 request_->url.EffectiveIntPort());
982 ProcessAlternateProtocol(session_->http_stream_factory(),
[email protected]17291a022011-10-10 07:32:53983 session_->http_server_properties(),
[email protected]90499482013-06-01 00:39:50984 *response_.headers.get(),
[email protected]8e6441ca2010-08-19 05:56:38985 endpoint);
[email protected]564b4912010-03-09 16:30:42986
[email protected]e772db3f2010-07-12 18:11:13987 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:57988 if (rv != OK)
989 return rv;
990
[email protected]8536ef52010-09-30 16:18:21991 if (is_https_request())
992 stream_->GetSSLInfo(&response_.ssl_info);
993
[email protected]0877e3d2009-10-17 22:29:57994 headers_valid_ = true;
995 return OK;
initial.commit586acc5fe2008-07-26 22:42:52996}
997
998int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:50999 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131000 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381001 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521002
1003 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501004 return stream_->ReadResponseBody(
1005 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521006}
1007
1008int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1009 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381010 bool done = false;
1011 if (result <= 0) {
1012 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521013 done = true;
[email protected]8e6441ca2010-08-19 05:56:381014 }
[email protected]9492e4a2010-02-24 00:58:461015
[email protected]8e6441ca2010-08-19 05:56:381016 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:311017 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:381018 // Note: Just because IsResponseBodyComplete is true, we're not
1019 // necessarily "done". We're only "done" when it is the last
1020 // read on this HttpNetworkTransaction, which will be signified
1021 // by a zero-length read.
1022 // TODO(mbelshe): The keepalive property is really a property of
1023 // the stream. No need to compute it here just to pass back
1024 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:471025 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1026 // ResponseHeaders.
1027 if (stream_->CanFindEndOfResponse()) {
1028 HttpResponseHeaders* headers = GetResponseHeaders();
1029 if (headers)
1030 keep_alive = headers->IsKeepAlive();
1031 }
initial.commit586acc5fe2008-07-26 22:42:521032 }
1033
[email protected]8e6441ca2010-08-19 05:56:381034 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521035 if (done) {
[email protected]56300172008-11-06 18:42:551036 LogTransactionMetrics();
[email protected]8e6441ca2010-08-19 05:56:381037 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011038 // Note: we don't reset the stream here. We've closed it, but we still
1039 // need it around so that callers can call methods such as
1040 // GetUploadProgress() and have them be meaningful.
1041 // TODO(mbelshe): This means we closed the stream here, and we close it
1042 // again in ~HttpNetworkTransaction. Clean that up.
1043
[email protected]8e6441ca2010-08-19 05:56:381044 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521045 }
1046
1047 // Clear these to avoid leaving around old state.
1048 read_buf_ = NULL;
1049 read_buf_len_ = 0;
1050
1051 return result;
1052}
1053
[email protected]2d2697f92009-02-18 21:00:321054int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1055 // This method differs from DoReadBody only in the next_state_. So we just
1056 // call DoReadBody and override the next_state_. Perhaps there is a more
1057 // elegant way for these two methods to share code.
1058 int rv = DoReadBody();
1059 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1060 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1061 return rv;
1062}
1063
[email protected]0877e3d2009-10-17 22:29:571064// TODO(wtc): This method and the DoReadBodyComplete method are almost
1065// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321066int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231067 // keep_alive defaults to true because the very reason we're draining the
1068 // response body is to reuse the connection for auth restart.
1069 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321070 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571071 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321072 done = true;
[email protected]68873ba2009-06-04 21:49:231073 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311074 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571075 done = true;
[email protected]2d2697f92009-02-18 21:00:321076 }
1077
1078 if (done) {
1079 DidDrainBodyForAuthRestart(keep_alive);
1080 } else {
1081 // Keep draining.
1082 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1083 }
1084
1085 return OK;
1086}
1087
[email protected]8e3d2d32010-06-13 18:46:231088void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1089 if (logged_response_time_)
1090 return;
1091
1092 logged_response_time_ = true;
1093
[email protected]a7e41312009-12-16 23:18:141094 base::TimeDelta total_duration = response_.response_time - start_time_;
[email protected]9a0a55f2009-04-13 23:23:031095
[email protected]d6b55392011-08-05 04:04:351096 UMA_HISTOGRAM_CUSTOM_TIMES(
1097 "Net.Transaction_Connected",
[email protected]510e854f2009-04-20 18:39:081098 total_duration,
[email protected]9a0a55f2009-04-13 23:23:031099 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1100 100);
[email protected]1fa47592009-07-27 22:45:001101
[email protected]c38ec5a2010-08-25 23:32:381102 bool reused_socket = stream_->IsConnectionReused();
1103 if (!reused_socket) {
[email protected]d6b55392011-08-05 04:04:351104 UMA_HISTOGRAM_CUSTOM_TIMES(
1105 "Net.Transaction_Connected_New_b",
[email protected]b01998a2009-04-21 01:01:111106 total_duration,
1107 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1108 100);
[email protected]0310d432009-08-25 07:49:521109 }
1110
[email protected]31ae7ab2012-04-24 21:09:051111 // Currently, non-HIGHEST priority requests are frame or sub-frame resource
[email protected]510e854f2009-04-20 18:39:081112 // types. This will change when we also prioritize certain subresources like
1113 // css, js, etc.
[email protected]262eec82013-03-19 21:01:361114 if (priority_ != HIGHEST) {
[email protected]d6b55392011-08-05 04:04:351115 UMA_HISTOGRAM_CUSTOM_TIMES(
1116 "Net.Priority_High_Latency_b",
[email protected]510e854f2009-04-20 18:39:081117 total_duration,
1118 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1119 100);
1120 } else {
[email protected]d6b55392011-08-05 04:04:351121 UMA_HISTOGRAM_CUSTOM_TIMES(
1122 "Net.Priority_Low_Latency_b",
[email protected]510e854f2009-04-20 18:39:081123 total_duration,
1124 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1125 100);
1126 }
[email protected]9a0a55f2009-04-13 23:23:031127}
1128
[email protected]56300172008-11-06 18:42:551129void HttpNetworkTransaction::LogTransactionMetrics() const {
[email protected]0877e3d2009-10-17 22:29:571130 base::TimeDelta duration = base::Time::Now() -
[email protected]2227c692010-05-04 15:36:111131 response_.request_time;
[email protected]56300172008-11-06 18:42:551132 if (60 < duration.InMinutes())
1133 return;
[email protected]0b48db42009-03-23 02:45:111134
[email protected]21b316a2009-03-23 18:25:061135 base::TimeDelta total_duration = base::Time::Now() - start_time_;
1136
[email protected]d6b55392011-08-05 04:04:351137 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration,
1138 base::TimeDelta::FromMilliseconds(1),
1139 base::TimeDelta::FromMinutes(10),
1140 100);
1141 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1142 total_duration,
1143 base::TimeDelta::FromMilliseconds(1),
1144 base::TimeDelta::FromMinutes(10), 100);
[email protected]5c68d692011-08-24 04:59:411145
[email protected]c38ec5a2010-08-25 23:32:381146 if (!stream_->IsConnectionReused()) {
[email protected]d6b55392011-08-05 04:04:351147 UMA_HISTOGRAM_CUSTOM_TIMES(
1148 "Net.Transaction_Latency_Total_New_Connection",
[email protected]808f6402009-03-30 20:02:071149 total_duration, base::TimeDelta::FromMilliseconds(1),
1150 base::TimeDelta::FromMinutes(10), 100);
1151 }
[email protected]56300172008-11-06 18:42:551152}
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());
1171 stream_->Close(true);
1172 stream_.reset();
1173 }
1174
[email protected]26816882010-10-14 18:03:091175 // The server is asking for a client certificate during the initial
1176 // handshake.
1177 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011178
[email protected]ec229bc92010-11-22 09:51:451179 // If the user selected one of the certificates in client_certs or declined
1180 // to provide one for this server before, use the past decision
1181 // automatically.
1182 scoped_refptr<X509Certificate> client_cert;
1183 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1184 response_.cert_request_info->host_and_port, &client_cert);
1185 if (!found_cached_cert)
1186 return error;
1187
1188 // Check that the certificate selected is still a certificate the server
1189 // is likely to accept, based on the criteria supplied in the
1190 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501191 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261192 const std::vector<std::string>& cert_authorities =
1193 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451194
[email protected]f1958c382013-02-07 00:15:261195 bool cert_still_valid = cert_authorities.empty() ||
1196 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451197 if (!cert_still_valid)
1198 return error;
[email protected]5e363962009-06-19 19:57:011199 }
[email protected]ec229bc92010-11-22 09:51:451200
1201 // TODO(davidben): Add a unit test which covers this path; we need to be
1202 // able to send a legitimate certificate and also bypass/clear the
1203 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141204 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1205 &proxy_ssl_config_ : &server_ssl_config_;
1206 ssl_config->send_client_cert = true;
1207 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451208 next_state_ = STATE_CREATE_STREAM;
1209 // Reset the other member variables.
1210 // Note: this is necessary only with SSL renegotiation.
1211 ResetStateForRestart();
1212 return OK;
[email protected]0b45559b2009-06-12 21:45:111213}
1214
[email protected]bd0b6772011-01-11 19:59:301215// TODO(rch): This does not correctly handle errors when an SSL proxy is
1216// being used, as all of the errors are handled as if they were generated
1217// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381218// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301219int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1220 DCHECK(request_);
[email protected]102957f2011-09-02 17:10:141221 if (server_ssl_config_.send_client_cert &&
[email protected]a1cb2cd2011-02-05 02:02:081222 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
[email protected]bd0b6772011-01-11 19:59:301223 session_->ssl_client_auth_cache()->Remove(
1224 GetHostAndPort(request_->url));
1225 }
1226
[email protected]02d1d442013-08-10 13:38:261227 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231228 uint16 version_max = server_ssl_config_.version_max;
1229
initial.commit586acc5fe2008-07-26 22:42:521230 switch (error) {
[email protected]bd0b6772011-01-11 19:59:301231 case ERR_SSL_PROTOCOL_ERROR:
1232 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231233 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1234 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171235 // This could be a TLS-intolerant server or a server that chose a
1236 // cipher suite defined only for higher protocol versions (such as
1237 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1238 // back to the next lower version and retry.
1239 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1240 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1241 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1242 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1243 // version_max should match the maximum protocol version supported
1244 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231245 version_max--;
1246
1247 // Fallback to the lower SSL version.
1248 // While SSL 3.0 fallback should be eliminated because of security
1249 // reasons, there is a high risk of breaking the servers if this is
1250 // done in general.
1251 // For now SSL 3.0 fallback is disabled for Google servers first,
1252 // and will be expanded to other servers after enough experiences
1253 // have been gained showing that this experiment works well with
1254 // today's Internet.
1255 if (version_max > SSL_PROTOCOL_VERSION_SSL3 ||
1256 (server_ssl_config_.unrestricted_ssl3_fallback_enabled ||
1257 !TransportSecurityState::IsGooglePinnedProperty(
1258 request_->url.host(), true /* include SNI */))) {
[email protected]02d1d442013-08-10 13:38:261259 should_fallback = true;
[email protected]158ac972013-04-19 23:29:231260 }
[email protected]1c773ea12009-04-28 19:58:421261 }
initial.commit586acc5fe2008-07-26 22:42:521262 break;
[email protected]02d1d442013-08-10 13:38:261263 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1264 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1265 version_max > server_ssl_config_.version_min) {
1266 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1267 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1268 // crbug.com/260358. In order to make the fallback as minimal as
1269 // possible, this fallback is only triggered for >= TLS 1.1.
1270 version_max--;
1271 should_fallback = true;
1272 }
1273 break;
1274 }
1275
1276 if (should_fallback) {
1277 net_log_.AddEvent(
1278 NetLog::TYPE_SSL_VERSION_FALLBACK,
1279 base::Bind(&NetLogSSLVersionFallbackCallback,
1280 &request_->url, error, server_ssl_config_.version_max,
1281 version_max));
1282 server_ssl_config_.version_max = version_max;
1283 server_ssl_config_.version_fallback = true;
1284 ResetConnectionAndRequestForResend();
1285 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521286 }
[email protected]158ac972013-04-19 23:29:231287
initial.commit586acc5fe2008-07-26 22:42:521288 return error;
1289}
1290
[email protected]bd0b6772011-01-11 19:59:301291// This method determines whether it is safe to resend the request after an
1292// IO error. It can only be called in response to request header or body
1293// write errors or response header read errors. It should not be used in
1294// other cases, such as a Connect error.
1295int HttpNetworkTransaction::HandleIOError(int error) {
1296 // SSL errors may happen at any time during the stream and indicate issues
1297 // with the underlying connection. Because the peer may request
1298 // renegotiation at any time, check and handle any possible SSL handshake
[email protected]80c75f682012-05-26 16:22:171299 // related errors. In addition to renegotiation, TLS False Start may cause
1300 // SSL handshake errors (specifically servers with buggy DEFLATE support)
1301 // to be delayed until the first Read on the underlying connection.
[email protected]bd0b6772011-01-11 19:59:301302 error = HandleSSLHandshakeError(error);
1303
1304 switch (error) {
1305 // If we try to reuse a connection that the server is in the process of
1306 // closing, we may end up successfully writing out our request (or a
1307 // portion of our request) only to find a connection error when we try to
1308 // read from (or finish writing to) the socket.
1309 case ERR_CONNECTION_RESET:
1310 case ERR_CONNECTION_CLOSED:
1311 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511312 // There can be a race between the socket pool checking checking whether a
1313 // socket is still connected, receiving the FIN, and sending/reading data
1314 // on a reused socket. If we receive the FIN between the connectedness
1315 // check and writing/reading from the socket, we may first learn the socket
1316 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1317 // likely happen when trying to retrieve its IP address.
1318 // See http://crbug.com/105824 for more details.
1319 case ERR_SOCKET_NOT_CONNECTED:
[email protected]bd0b6772011-01-11 19:59:301320 if (ShouldResendRequest(error)) {
[email protected]b6754252012-06-13 23:14:381321 net_log_.AddEventWithNetErrorCode(
1322 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301323 ResetConnectionAndRequestForResend();
1324 error = OK;
1325 }
1326 break;
[email protected]5a60c8b2011-10-19 20:14:291327 case ERR_PIPELINE_EVICTION:
[email protected]5477d892012-03-01 21:31:311328 if (!session_->force_http_pipelining()) {
[email protected]b6754252012-06-13 23:14:381329 net_log_.AddEventWithNetErrorCode(
1330 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]5477d892012-03-01 21:31:311331 ResetConnectionAndRequestForResend();
1332 error = OK;
1333 }
1334 break;
[email protected]8753a122011-10-16 08:05:081335 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001336 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]b6754252012-06-13 23:14:381337 net_log_.AddEventWithNetErrorCode(
1338 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001339 ResetConnectionAndRequestForResend();
1340 error = OK;
1341 break;
[email protected]bd0b6772011-01-11 19:59:301342 }
1343 return error;
1344}
1345
[email protected]c3b35c22008-09-27 03:19:421346void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581347 ResetStateForAuthRestart();
1348 stream_.reset();
1349}
1350
1351void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251352 send_start_time_ = base::TimeTicks();
1353 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251354
[email protected]0757e7702009-03-27 04:00:221355 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421356 read_buf_ = NULL;
1357 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571358 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201359 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141360 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381361 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571362}
1363
1364HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501365 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421366}
1367
[email protected]a19f1c602009-08-24 21:35:281368bool HttpNetworkTransaction::ShouldResendRequest(int error) const {
[email protected]8e6441ca2010-08-19 05:56:381369 bool connection_is_proven = stream_->IsConnectionReused();
1370 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161371
[email protected]2a5c76b2008-09-25 22:15:161372 // NOTE: we resend a request only if we reused a keep-alive connection.
1373 // This automatically prevents an infinite resend loop because we'll run
1374 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381375 if (connection_is_proven && !has_received_headers)
1376 return true;
1377 return false;
[email protected]1c773ea12009-04-28 19:58:421378}
1379
1380void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381381 if (stream_.get()) {
1382 stream_->Close(true);
1383 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161384 }
1385
[email protected]0877e3d2009-10-17 22:29:571386 // We need to clear request_headers_ because it contains the real request
1387 // headers, but we may need to resend the CONNECT request first to recreate
1388 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201389 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501390 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541391}
1392
[email protected]1c773ea12009-04-28 19:58:421393bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
[email protected]2df19bb2010-08-25 20:13:461394 return !is_https_request() &&
1395 (proxy_info_.is_https() || proxy_info_.is_http());
[email protected]1c773ea12009-04-28 19:58:421396}
license.botbf09a502008-08-24 00:55:551397
[email protected]1c773ea12009-04-28 19:58:421398bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041399 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421400}
1401
[email protected]e772db3f2010-07-12 18:11:131402int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271403 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501404 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421405
[email protected]0877e3d2009-10-17 22:29:571406 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581407 if (status != HTTP_UNAUTHORIZED &&
1408 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421409 return OK;
[email protected]9094b602012-02-27 21:44:581410 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111411 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161412 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1413 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421414
[email protected]9094b602012-02-27 21:44:581415 // This case can trigger when an HTTPS server responds with a "Proxy
1416 // authentication required" status code through a non-authenticating
1417 // proxy.
[email protected]7a67a8152010-11-05 18:31:101418 if (!auth_controllers_[target].get())
1419 return ERR_UNEXPECTED_PROXY_AUTH;
1420
[email protected]a7ea8832010-07-12 17:54:541421 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311422 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1423 net_log_);
[email protected]228404f2010-06-24 04:31:411424 if (auth_controllers_[target]->HaveAuthHandler())
1425 pending_auth_target_ = target;
1426
1427 scoped_refptr<AuthChallengeInfo> auth_info =
1428 auth_controllers_[target]->auth_info();
1429 if (auth_info.get())
1430 response_.auth_challenge = auth_info;
1431
[email protected]228404f2010-06-24 04:31:411432 return rv;
[email protected]f9ee6b52008-11-08 06:46:231433}
1434
[email protected]8e6441ca2010-08-19 05:56:381435bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1436 return auth_controllers_[target].get() &&
1437 auth_controllers_[target]->HaveAuth();
1438}
1439
[email protected]228404f2010-06-24 04:31:411440GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1441 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461442 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411443 if (!proxy_info_.proxy_server().is_valid() ||
1444 proxy_info_.proxy_server().is_direct()) {
1445 return GURL(); // There is no proxy server.
1446 }
[email protected]2df19bb2010-08-25 20:13:461447 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1448 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351449 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461450 }
[email protected]228404f2010-06-24 04:31:411451 case HttpAuth::AUTH_SERVER:
1452 return request_->url;
1453 default:
1454 return GURL();
1455 }
[email protected]c3b35c22008-09-27 03:19:421456}
1457
[email protected]d8eb84242010-09-25 02:25:061458#define STATE_CASE(s) \
1459 case s: \
1460 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1461 break
[email protected]aef04272010-06-28 18:03:041462
1463std::string HttpNetworkTransaction::DescribeState(State state) {
1464 std::string description;
1465 switch (state) {
[email protected]82918cc2010-08-25 17:24:501466 STATE_CASE(STATE_CREATE_STREAM);
1467 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131468 STATE_CASE(STATE_INIT_REQUEST_BODY);
1469 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511470 STATE_CASE(STATE_BUILD_REQUEST);
1471 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041472 STATE_CASE(STATE_SEND_REQUEST);
1473 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1474 STATE_CASE(STATE_READ_HEADERS);
1475 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041476 STATE_CASE(STATE_READ_BODY);
1477 STATE_CASE(STATE_READ_BODY_COMPLETE);
1478 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1479 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041480 STATE_CASE(STATE_NONE);
1481 default:
[email protected]d8eb84242010-09-25 02:25:061482 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1483 state);
[email protected]aef04272010-06-28 18:03:041484 break;
1485 }
1486 return description;
1487}
1488
1489#undef STATE_CASE
1490
[email protected]c3b35c22008-09-27 03:19:421491} // namespace net