blob: 791822b6169dbe631e6355aef77ee57b813a00be [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]528c56d2010-07-30 19:28:4419#include "base/string_number_conversions.h"
[email protected]d8eb84242010-09-25 02:25:0620#include "base/string_util.h"
21#include "base/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]631f1322010-04-30 17:59:1124#include "googleurl/src/gurl.h"
[email protected]277d5942010-08-11 21:02:3525#include "net/base/auth.h"
[email protected]2d6728692011-03-12 01:39:5526#include "net/base/host_port_pair.h"
[email protected]74a85ce2009-02-12 00:03:1927#include "net/base/io_buffer.h"
initial.commit586acc5fe2008-07-26 22:42:5228#include "net/base/load_flags.h"
[email protected]58e32bb2013-01-21 18:23:2529#include "net/base/load_timing_info.h"
[email protected]597cf6e2009-05-29 09:43:2630#include "net/base/net_errors.h"
[email protected]c3b35c22008-09-27 03:19:4231#include "net/base/net_util.h"
initial.commit586acc5fe2008-07-26 22:42:5232#include "net/base/upload_data_stream.h"
[email protected]c3b35c22008-09-27 03:19:4233#include "net/http/http_auth.h"
34#include "net/http/http_auth_handler.h"
[email protected]fa82f932010-05-20 11:09:2435#include "net/http/http_auth_handler_factory.h"
[email protected]8d5a34e2009-06-11 21:21:3636#include "net/http/http_basic_stream.h"
initial.commit586acc5fe2008-07-26 22:42:5237#include "net/http/http_chunked_decoder.h"
38#include "net/http/http_network_session.h"
[email protected]a7ea8832010-07-12 17:54:5439#include "net/http/http_proxy_client_socket.h"
[email protected]e772db3f2010-07-12 18:11:1340#include "net/http/http_proxy_client_socket_pool.h"
[email protected]270c6412010-03-29 22:02:4741#include "net/http/http_request_headers.h"
initial.commit586acc5fe2008-07-26 22:42:5242#include "net/http/http_request_info.h"
[email protected]319d9e6f2009-02-18 19:47:2143#include "net/http/http_response_headers.h"
[email protected]0877e3d2009-10-17 22:29:5744#include "net/http/http_response_info.h"
[email protected]17291a022011-10-10 07:32:5345#include "net/http/http_server_properties.h"
[email protected]9094b602012-02-27 21:44:5846#include "net/http/http_status_code.h"
[email protected]92b9a3e2012-11-09 08:52:5247#include "net/http/http_stream_base.h"
[email protected]102e27c2011-02-23 01:01:3148#include "net/http/http_stream_factory.h"
initial.commit586acc5fe2008-07-26 22:42:5249#include "net/http/http_util.h"
[email protected]158ac972013-04-19 23:29:2350#include "net/http/transport_security_state.h"
[email protected]d7f16632010-03-29 18:02:3651#include "net/http/url_security_manager.h"
[email protected]f7984fc62009-06-22 23:26:4452#include "net/socket/client_socket_factory.h"
[email protected]a796bcec2010-03-22 17:17:2653#include "net/socket/socks_client_socket_pool.h"
[email protected]f7984fc62009-06-22 23:26:4454#include "net/socket/ssl_client_socket.h"
[email protected]e60e47a2010-07-14 03:37:1855#include "net/socket/ssl_client_socket_pool.h"
[email protected]ab739042011-04-07 15:22:2856#include "net/socket/transport_client_socket_pool.h"
[email protected]65d56aa2010-06-14 04:13:4057#include "net/spdy/spdy_http_stream.h"
[email protected]dab9c7d2010-02-06 21:44:3258#include "net/spdy/spdy_session.h"
59#include "net/spdy/spdy_session_pool.h"
[email protected]536fd0b2013-03-14 17:41:5760#include "net/ssl/ssl_cert_request_info.h"
61#include "net/ssl/ssl_connection_status_flags.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]8e6441ca2010-08-19 05:56:3869void ProcessAlternateProtocol(HttpStreamFactory* factory,
[email protected]17291a022011-10-10 07:32:5370 HttpServerProperties* http_server_properties,
[email protected]8e6441ca2010-08-19 05:56:3871 const HttpResponseHeaders& headers,
72 const HostPortPair& http_host_port_pair) {
[email protected]564b4912010-03-09 16:30:4273 std::string alternate_protocol_str;
[email protected]8e6441ca2010-08-19 05:56:3874
[email protected]17291a022011-10-10 07:32:5375 if (!headers.EnumerateHeader(NULL, kAlternateProtocolHeader,
[email protected]564b4912010-03-09 16:30:4276 &alternate_protocol_str)) {
77 // Header is not present.
78 return;
79 }
80
[email protected]17291a022011-10-10 07:32:5381 factory->ProcessAlternateProtocol(http_server_properties,
[email protected]8e6441ca2010-08-19 05:56:3882 alternate_protocol_str,
83 http_host_port_pair);
[email protected]f45c1ee2010-08-03 00:54:3084}
85
[email protected]a1cb2cd2011-02-05 02:02:0886// Returns true if |error| is a client certificate authentication error.
87bool IsClientCertificateError(int error) {
88 switch (error) {
89 case ERR_BAD_SSL_CLIENT_AUTH_CERT:
90 case ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED:
91 case ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY:
92 case ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED:
93 return true;
94 default:
95 return false;
96 }
97}
98
[email protected]b6754252012-06-13 23:14:3899Value* NetLogSSLVersionFallbackCallback(const GURL* url,
100 int net_error,
101 uint16 version_before,
102 uint16 version_after,
103 NetLog::LogLevel /* log_level */) {
104 DictionaryValue* dict = new DictionaryValue();
105 dict->SetString("host_and_port", GetHostAndPort(*url));
106 dict->SetInteger("net_error", net_error);
107 dict->SetInteger("version_before", version_before);
108 dict->SetInteger("version_after", version_after);
109 return dict;
110}
[email protected]db74b0102012-05-31 19:55:53111
[email protected]1c773ea12009-04-28 19:58:42112} // namespace
113
initial.commit586acc5fe2008-07-26 22:42:52114//-----------------------------------------------------------------------------
115
[email protected]262eec82013-03-19 21:01:36116HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
117 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22118 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]49639fa2011-12-20 23:22:41119 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_(
120 base::Bind(&HttpNetworkTransaction::OnIOComplete,
121 base::Unretained(this)))),
initial.commit586acc5fe2008-07-26 22:42:52122 session_(session),
123 request_(NULL),
[email protected]262eec82013-03-19 21:01:36124 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57125 headers_valid_(false),
[email protected]8e3d2d32010-06-13 18:46:23126 logged_response_time_(false),
[email protected]b94f92d2010-10-27 16:45:20127 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52128 read_buf_len_(0),
[email protected]a7ea8832010-07-12 17:54:54129 next_state_(STATE_NONE),
130 establishing_tunnel_(false) {
[email protected]102957f2011-09-02 17:10:14131 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]5285d972011-10-18 18:56:34132 if (session->http_stream_factory()->has_next_protos()) {
[email protected]102957f2011-09-02 17:10:14133 server_ssl_config_.next_protos =
[email protected]5285d972011-10-18 18:56:34134 session->http_stream_factory()->next_protos();
[email protected]102957f2011-09-02 17:10:14135 }
[email protected]99ffa5a2011-10-06 04:20:19136 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50137}
138
[email protected]0b0bf032010-09-21 18:08:50139HttpNetworkTransaction::~HttpNetworkTransaction() {
140 if (stream_.get()) {
141 HttpResponseHeaders* headers = GetResponseHeaders();
142 // TODO(mbelshe): The stream_ should be able to compute whether or not the
143 // stream should be kept alive. No reason to compute here
144 // and pass it in.
145 bool try_to_keep_alive =
146 next_state_ == STATE_NONE &&
147 stream_->CanFindEndOfResponse() &&
148 (!headers || headers->IsKeepAlive());
149 if (!try_to_keep_alive) {
150 stream_->Close(true /* not reusable */);
151 } else {
152 if (stream_->IsResponseBodyComplete()) {
153 // If the response body is complete, we can just reuse the socket.
154 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44155 } else if (stream_->IsSpdyHttpStream()) {
156 // Doesn't really matter for SpdyHttpStream. Just close it.
157 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50158 } else {
159 // Otherwise, we try to drain the response body.
[email protected]92b9a3e2012-11-09 08:52:52160 HttpStreamBase* stream = stream_.release();
[email protected]5a60c8b2011-10-19 20:14:29161 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50162 }
163 }
164 }
[email protected]0b0bf032010-09-21 18:08:50165}
166
[email protected]684970b2009-08-14 04:54:46167int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41168 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53169 const BoundNetLog& net_log) {
[email protected]5e2e6c77d12009-12-24 21:57:16170 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
[email protected]5d0153c512009-01-12 19:08:36171
[email protected]9e743cd2010-03-16 07:03:53172 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04173 request_ = request_info;
[email protected]21b316a2009-03-23 18:25:06174 start_time_ = base::Time::Now();
[email protected]96d570e42008-08-05 22:43:04175
[email protected]99ffa5a2011-10-06 04:20:19176 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14177 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19178 proxy_ssl_config_.rev_checking_enabled = false;
179 }
[email protected]6fbac162011-06-20 00:29:04180
[email protected]82918cc2010-08-25 17:24:50181 next_state_ = STATE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04182 int rv = DoLoop(OK);
183 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41184 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04185 return rv;
186}
187
188int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41189 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38190 DCHECK(!stream_.get());
191 DCHECK(!stream_request_.get());
192 DCHECK_EQ(STATE_NONE, next_state_);
193
[email protected]82918cc2010-08-25 17:24:50194 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38195
[email protected]ccb40e52008-09-17 20:54:40196 int rv = DoLoop(OK);
197 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41198 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11199 return rv;
[email protected]96d570e42008-08-05 22:43:04200}
201
[email protected]0b45559b2009-06-12 21:45:11202int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41203 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38204 // In HandleCertificateRequest(), we always tear down existing stream
205 // requests to force a new connection. So we shouldn't have one here.
206 DCHECK(!stream_request_.get());
207 DCHECK(!stream_.get());
208 DCHECK_EQ(STATE_NONE, next_state_);
209
[email protected]102957f2011-09-02 17:10:14210 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
211 &proxy_ssl_config_ : &server_ssl_config_;
212 ssl_config->send_client_cert = true;
213 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45214 session_->ssl_client_auth_cache()->Add(
215 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11216 // Reset the other member variables.
217 // Note: this is necessary only with SSL renegotiation.
218 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50219 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11220 int rv = DoLoop(OK);
221 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41222 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11223 return rv;
224}
225
[email protected]49639fa2011-12-20 23:22:41226int HttpNetworkTransaction::RestartWithAuth(
227 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22228 HttpAuth::Target target = pending_auth_target_;
229 if (target == HttpAuth::AUTH_NONE) {
230 NOTREACHED();
231 return ERR_UNEXPECTED;
232 }
[email protected]0757e7702009-03-27 04:00:22233 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42234
[email protected]f3cf9802011-10-28 18:44:58235 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13236
[email protected]49639fa2011-12-20 23:22:41237 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38238
239 int rv = OK;
240 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
241 // In this case, we've gathered credentials for use with proxy
242 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50243 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38244 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59245 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54246 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58247 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54248 } else {
[email protected]8e6441ca2010-08-19 05:56:38249 // In this case, we've gathered credentials for the server or the proxy
250 // but it is not during the tunneling phase.
251 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54252 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38253 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54254 }
[email protected]c3b35c22008-09-27 03:19:42255
[email protected]c3b35c22008-09-27 03:19:42256 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41257 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42258 return rv;
[email protected]96d570e42008-08-05 22:43:04259}
260
[email protected]f9ee6b52008-11-08 06:46:23261void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
262 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38263 DCHECK(!stream_request_.get());
264
[email protected]2d2697f92009-02-18 21:00:32265 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57266 // Even if the server says the connection is keep-alive, we have to be
267 // able to find the end of each response in order to reuse the connection.
268 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31269 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57270 // If the response body hasn't been completely read, we need to drain
271 // it first.
[email protected]351ab642010-08-05 16:55:31272 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32273 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57274 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32275 read_buf_len_ = kDrainBodyBufferSize;
276 return;
277 }
[email protected]0877e3d2009-10-17 22:29:57278 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09279 }
280
[email protected]2d2697f92009-02-18 21:00:32281 // We don't need to drain the response body, so we act as if we had drained
282 // the response body.
283 DidDrainBodyForAuthRestart(keep_alive);
284}
285
286void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38287 DCHECK(!stream_request_.get());
288
289 if (stream_.get()) {
[email protected]697ef4c2010-10-14 16:38:58290 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46291 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38292 // We should call connection_->set_idle_time(), but this doesn't occur
293 // often enough to be worth the trouble.
294 stream_->SetConnectionReused();
[email protected]92b9a3e2012-11-09 08:52:52295 new_stream =
296 static_cast<HttpStream*>(stream_.get())->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38297 }
[email protected]697ef4c2010-10-14 16:38:58298
299 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46300 // Close the stream and mark it as not_reusable. Even in the
301 // keep_alive case, we've determined that the stream_ is not
302 // reusable if new_stream is NULL.
303 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58304 next_state_ = STATE_CREATE_STREAM;
305 } else {
306 next_state_ = STATE_INIT_STREAM;
307 }
308 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32309 }
[email protected]f9ee6b52008-11-08 06:46:23310
311 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58312 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23313}
314
[email protected]8e6441ca2010-08-19 05:56:38315bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
316 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
317 HaveAuth(pending_auth_target_);
318}
319
[email protected]9dea9e1f2009-01-29 00:30:47320int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41321 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04322 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35323 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04324
[email protected]1f14a912009-12-21 20:32:44325 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04326
[email protected]ad8e04a2010-11-01 04:16:27327 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38328 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04329 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29330 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04331 // bytes when establishing a tunnel because they might be controlled by
332 // an active network attacker. We don't worry about this for HTTP
333 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29334 // We reach this case when the user cancels a 407 proxy auth prompt. We
335 // also don't worry about this for an HTTPS Proxy, because the
336 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04337 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46338 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58339 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54340 LOG(WARNING) << "Blocked proxy response with status "
341 << headers->response_code() << " to CONNECT request for "
342 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04343 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44344 }
345
[email protected]e60e47a2010-07-14 03:37:18346 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31347 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18348
[email protected]96d570e42008-08-05 22:43:04349 read_buf_ = buf;
350 read_buf_len_ = buf_len;
351
[email protected]1f14a912009-12-21 20:32:44352 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04353 int rv = DoLoop(OK);
354 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41355 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04356 return rv;
357}
358
359const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
[email protected]a7e41312009-12-16 23:18:14360 return ((headers_valid_ && response_.headers) || response_.ssl_info.cert ||
361 response_.cert_request_info) ? &response_ : NULL;
[email protected]96d570e42008-08-05 22:43:04362}
363
364LoadState HttpNetworkTransaction::GetLoadState() const {
365 // TODO(wtc): Define a new LoadState value for the
366 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
367 switch (next_state_) {
[email protected]82918cc2010-08-25 17:24:50368 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38369 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15370 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
371 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57372 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04373 return LOAD_STATE_SENDING_REQUEST;
374 case STATE_READ_HEADERS_COMPLETE:
375 return LOAD_STATE_WAITING_FOR_RESPONSE;
376 case STATE_READ_BODY_COMPLETE:
377 return LOAD_STATE_READING_RESPONSE;
378 default:
379 return LOAD_STATE_IDLE;
380 }
381}
382
[email protected]196d18a2012-08-30 03:47:31383UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31384 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31385 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04386
[email protected]92b9a3e2012-11-09 08:52:52387 // TODO(bashi): This cast is temporary. Remove later.
388 return static_cast<HttpStream*>(stream_.get())->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04389}
390
[email protected]5033ab82013-03-22 20:17:46391bool HttpNetworkTransaction::GetLoadTimingInfo(
392 LoadTimingInfo* load_timing_info) const {
393 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
394 return false;
395
396 load_timing_info->proxy_resolve_start =
397 proxy_info_.proxy_resolve_start_time();
398 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
399 load_timing_info->send_start = send_start_time_;
400 load_timing_info->send_end = send_end_time_;
401 load_timing_info->receive_headers_end = receive_headers_end_;
402 return true;
403}
404
405void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
406 priority_ = priority;
407 // TODO(akalin): Plumb this through to |stream_request_| and
408 // |stream_|.
409}
410
[email protected]102e27c2011-02-23 01:01:31411void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
412 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52413 HttpStreamBase* stream) {
[email protected]82918cc2010-08-25 17:24:50414 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38415 DCHECK(stream_request_.get());
416
417 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14418 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31419 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38420 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51421 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
422 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38423 response_.was_fetched_via_spdy = stream_request_->using_spdy();
424 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]8e6441ca2010-08-19 05:56:38425
426 OnIOComplete(OK);
427}
428
[email protected]102e27c2011-02-23 01:01:31429void HttpNetworkTransaction::OnStreamFailed(int result,
430 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50431 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38432 DCHECK_NE(OK, result);
433 DCHECK(stream_request_.get());
434 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14435 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38436
437 OnIOComplete(result);
438}
439
[email protected]102e27c2011-02-23 01:01:31440void HttpNetworkTransaction::OnCertificateError(
441 int result,
442 const SSLConfig& used_ssl_config,
443 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50444 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38445 DCHECK_NE(OK, result);
446 DCHECK(stream_request_.get());
447 DCHECK(!stream_.get());
448
449 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14450 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38451
452 // TODO(mbelshe): For now, we're going to pass the error through, and that
453 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50454 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
455 // good and the user chooses to ignore the error. This is not ideal, but not
456 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38457
458 OnIOComplete(result);
459}
460
461void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50462 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31463 const SSLConfig& used_ssl_config,
464 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50465 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38466 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50467 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38468
469 establishing_tunnel_ = true;
470 response_.headers = proxy_response.headers;
471 response_.auth_challenge = proxy_response.auth_challenge;
472 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14473 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31474 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38475
476 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
477 pending_auth_target_ = HttpAuth::AUTH_PROXY;
478
479 DoCallback(OK);
480}
481
482void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31483 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50484 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50485 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38486
[email protected]102957f2011-09-02 17:10:14487 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38488 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58489 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38490}
491
[email protected]511f6f52010-12-17 03:58:29492void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
493 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31494 const SSLConfig& used_ssl_config,
495 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52496 HttpStreamBase* stream) {
[email protected]511f6f52010-12-17 03:58:29497 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
498
499 headers_valid_ = true;
500 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14501 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31502 proxy_info_ = used_proxy_info;
[email protected]511f6f52010-12-17 03:58:29503 stream_.reset(stream);
504 stream_request_.reset(); // we're done with the stream request
505 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
506}
507
[email protected]8e6441ca2010-08-19 05:56:38508bool HttpNetworkTransaction::is_https_request() const {
509 return request_->url.SchemeIs("https");
initial.commit586acc5fe2008-07-26 22:42:52510}
511
initial.commit586acc5fe2008-07-26 22:42:52512void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50513 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41514 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52515
[email protected]96d570e42008-08-05 22:43:04516 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41517 CompletionCallback c = callback_;
518 callback_.Reset();
519 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52520}
521
522void HttpNetworkTransaction::OnIOComplete(int result) {
523 int rv = DoLoop(result);
524 if (rv != ERR_IO_PENDING)
525 DoCallback(rv);
526}
527
528int HttpNetworkTransaction::DoLoop(int result) {
529 DCHECK(next_state_ != STATE_NONE);
530
531 int rv = result;
532 do {
533 State state = next_state_;
534 next_state_ = STATE_NONE;
535 switch (state) {
[email protected]82918cc2010-08-25 17:24:50536 case STATE_CREATE_STREAM:
537 DCHECK_EQ(OK, rv);
538 rv = DoCreateStream();
539 break;
540 case STATE_CREATE_STREAM_COMPLETE:
541 rv = DoCreateStreamComplete(rv);
542 break;
[email protected]351ab642010-08-05 16:55:31543 case STATE_INIT_STREAM:
544 DCHECK_EQ(OK, rv);
545 rv = DoInitStream();
546 break;
547 case STATE_INIT_STREAM_COMPLETE:
548 rv = DoInitStreamComplete(rv);
549 break;
[email protected]044de0642010-06-17 10:42:15550 case STATE_GENERATE_PROXY_AUTH_TOKEN:
551 DCHECK_EQ(OK, rv);
552 rv = DoGenerateProxyAuthToken();
553 break;
554 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
555 rv = DoGenerateProxyAuthTokenComplete(rv);
556 break;
557 case STATE_GENERATE_SERVER_AUTH_TOKEN:
558 DCHECK_EQ(OK, rv);
559 rv = DoGenerateServerAuthToken();
560 break;
561 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
562 rv = DoGenerateServerAuthTokenComplete(rv);
563 break;
[email protected]daddea62012-09-19 05:51:13564 case STATE_INIT_REQUEST_BODY:
565 DCHECK_EQ(OK, rv);
566 rv = DoInitRequestBody();
567 break;
568 case STATE_INIT_REQUEST_BODY_COMPLETE:
569 rv = DoInitRequestBodyComplete(rv);
570 break;
[email protected]4875ba12011-03-30 22:31:51571 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55572 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38573 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51574 rv = DoBuildRequest();
575 break;
576 case STATE_BUILD_REQUEST_COMPLETE:
577 rv = DoBuildRequestComplete(rv);
578 break;
579 case STATE_SEND_REQUEST:
580 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57581 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52582 break;
[email protected]0877e3d2009-10-17 22:29:57583 case STATE_SEND_REQUEST_COMPLETE:
584 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43585 net_log_.EndEventWithNetErrorCode(
586 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52587 break;
588 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55589 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38590 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52591 rv = DoReadHeaders();
592 break;
593 case STATE_READ_HEADERS_COMPLETE:
594 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43595 net_log_.EndEventWithNetErrorCode(
596 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52597 break;
598 case STATE_READ_BODY:
[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_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52601 rv = DoReadBody();
602 break;
603 case STATE_READ_BODY_COMPLETE:
604 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43605 net_log_.EndEventWithNetErrorCode(
606 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52607 break;
[email protected]2d2697f92009-02-18 21:00:32608 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55609 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53610 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38611 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32612 rv = DoDrainBodyForAuthRestart();
613 break;
614 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
615 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43616 net_log_.EndEventWithNetErrorCode(
617 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32618 break;
initial.commit586acc5fe2008-07-26 22:42:52619 default:
620 NOTREACHED() << "bad state";
621 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04622 break;
initial.commit586acc5fe2008-07-26 22:42:52623 }
624 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
625
626 return rv;
627}
628
[email protected]82918cc2010-08-25 17:24:50629int HttpNetworkTransaction::DoCreateStream() {
630 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]351ab642010-08-05 16:55:31631
[email protected]26816882010-10-14 18:03:09632 stream_request_.reset(
633 session_->http_stream_factory()->RequestStream(
[email protected]102e27c2011-02-23 01:01:31634 *request_,
[email protected]262eec82013-03-19 21:01:36635 priority_,
[email protected]102957f2011-09-02 17:10:14636 server_ssl_config_,
637 proxy_ssl_config_,
[email protected]26816882010-10-14 18:03:09638 this,
639 net_log_));
640 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38641 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31642}
643
[email protected]82918cc2010-08-25 17:24:50644int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
[email protected]394816e92010-08-03 07:38:59645 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50646 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38647 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33648 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
649 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29650 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
651 // Return OK and let the caller read the proxy's error page
652 next_state_ = STATE_NONE;
653 return OK;
[email protected]394816e92010-08-03 07:38:59654 }
655
[email protected]bd0b6772011-01-11 19:59:30656 // Handle possible handshake errors that may have occurred if the stream
657 // used SSL for one or more of the layers.
658 result = HandleSSLHandshakeError(result);
659
[email protected]8e6441ca2010-08-19 05:56:38660 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09661 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38662 return result;
[email protected]394816e92010-08-03 07:38:59663}
664
[email protected]82918cc2010-08-25 17:24:50665int HttpNetworkTransaction::DoInitStream() {
666 DCHECK(stream_.get());
667 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36668 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50669}
670
671int HttpNetworkTransaction::DoInitStreamComplete(int result) {
672 if (result == OK) {
673 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50674 } else {
[email protected]82918cc2010-08-25 17:24:50675 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26676 result = HandleIOError(result);
677
678 // The stream initialization failed, so this stream will never be useful.
679 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50680 }
681
682 return result;
683}
684
[email protected]044de0642010-06-17 10:42:15685int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
686 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
687 if (!ShouldApplyProxyAuth())
688 return OK;
[email protected]394816e92010-08-03 07:38:59689 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
690 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09691 auth_controllers_[target] =
692 new HttpAuthController(target,
693 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31694 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09695 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59696 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41697 io_callback_,
[email protected]394816e92010-08-03 07:38:59698 net_log_);
[email protected]044de0642010-06-17 10:42:15699}
700
701int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
702 DCHECK_NE(ERR_IO_PENDING, rv);
703 if (rv == OK)
704 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
705 return rv;
706}
707
708int HttpNetworkTransaction::DoGenerateServerAuthToken() {
709 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59710 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
711 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09712 auth_controllers_[target] =
713 new HttpAuthController(target,
714 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31715 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09716 session_->http_auth_handler_factory());
[email protected]044de0642010-06-17 10:42:15717 if (!ShouldApplyServerAuth())
718 return OK;
[email protected]394816e92010-08-03 07:38:59719 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41720 io_callback_,
[email protected]394816e92010-08-03 07:38:59721 net_log_);
[email protected]044de0642010-06-17 10:42:15722}
723
724int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
725 DCHECK_NE(ERR_IO_PENDING, rv);
726 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13727 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15728 return rv;
729}
730
[email protected]2979a492011-04-06 00:29:14731void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy) {
732 request_headers_.SetHeader(HttpRequestHeaders::kHost,
733 GetHostAndOptionalPort(request_->url));
734
735 // For compat with HTTP/1.0 servers and proxies:
736 if (using_proxy) {
737 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
738 "keep-alive");
739 } else {
740 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
741 }
742
[email protected]2979a492011-04-06 00:29:14743 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11744 if (request_->upload_data_stream) {
745 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14746 request_headers_.SetHeader(
747 HttpRequestHeaders::kTransferEncoding, "chunked");
748 } else {
749 request_headers_.SetHeader(
750 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11751 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14752 }
753 } else if (request_->method == "POST" || request_->method == "PUT" ||
754 request_->method == "HEAD") {
755 // An empty POST/PUT request still needs a content length. As for HEAD,
756 // IE and Safari also add a content length header. Presumably it is to
757 // support sending a HEAD request to an URL that only expects to be sent a
758 // POST or some other method that normally would have a message body.
759 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
760 }
761
762 // Honor load flags that impact proxy caches.
763 if (request_->load_flags & LOAD_BYPASS_CACHE) {
764 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
765 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
766 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
767 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
768 }
769
770 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
771 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
772 &request_headers_);
773 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
774 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
775 &request_headers_);
776
[email protected]c10450102011-06-27 09:06:16777 request_headers_.MergeFrom(request_->extra_headers);
[email protected]173f8e22013-04-10 04:18:20778 response_.did_use_http_auth =
779 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
780 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14781}
782
[email protected]daddea62012-09-19 05:51:13783int HttpNetworkTransaction::DoInitRequestBody() {
784 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13785 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11786 if (request_->upload_data_stream)
787 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13788 return rv;
789}
[email protected]4875ba12011-03-30 22:31:51790
[email protected]daddea62012-09-19 05:51:13791int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
792 if (result == OK)
793 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13794 return result;
795}
796
797int HttpNetworkTransaction::DoBuildRequest() {
798 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51799 headers_valid_ = false;
800
801 // This is constructed lazily (instead of within our Start method), so that
802 // we have proxy info available.
803 if (request_headers_.IsEmpty()) {
804 bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) &&
805 !is_https_request();
[email protected]2979a492011-04-06 00:29:14806 BuildRequestHeaders(using_proxy);
[email protected]4875ba12011-03-30 22:31:51807 }
808
[email protected]4875ba12011-03-30 22:31:51809 return OK;
810}
811
812int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24813 if (result == OK)
814 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51815 return result;
816}
817
[email protected]0877e3d2009-10-17 22:29:57818int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:25819 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57820 next_state_ = STATE_SEND_REQUEST_COMPLETE;
821
[email protected]bf3eb002012-11-15 05:50:11822 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52823}
824
[email protected]0877e3d2009-10-17 22:29:57825int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25826 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52827 if (result < 0)
828 return HandleIOError(result);
[email protected]7cf7ccb2013-04-20 02:53:08829 response_.network_accessed = true;
[email protected]0877e3d2009-10-17 22:29:57830 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52831 return OK;
832}
833
834int HttpNetworkTransaction::DoReadHeaders() {
835 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41836 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52837}
838
[email protected]0e75a732008-10-16 20:36:09839int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() {
[email protected]8e6441ca2010-08-19 05:56:38840 if (!response_.headers && !stream_->IsConnectionReused()) {
[email protected]0e75a732008-10-16 20:36:09841 // The connection was closed before any data was sent. Likely an error
842 // rather than empty HTTP/0.9 response.
[email protected]aecfbf22008-10-16 02:02:47843 return ERR_EMPTY_RESPONSE;
844 }
845
[email protected]aecfbf22008-10-16 02:02:47846 return OK;
847}
848
initial.commit586acc5fe2008-07-26 22:42:52849int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25850 receive_headers_end_ = base::TimeTicks::Now();
851
[email protected]0b45559b2009-06-12 21:45:11852 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
853 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:38854 if (IsCertificateError(result)) {
855 // We don't handle a certificate error during SSL renegotiation, so we
856 // have to return an error that's not in the certificate error range
857 // (-2xx).
858 LOG(ERROR) << "Got a server certificate with error " << result
859 << " during SSL renegotiation";
860 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
861 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
862 // TODO(wtc): Need a test case for this code path!
863 DCHECK(stream_.get());
864 DCHECK(is_https_request());
865 response_.cert_request_info = new SSLCertRequestInfo;
866 stream_->GetSSLCertRequestInfo(response_.cert_request_info);
867 result = HandleCertificateRequest(result);
868 if (result == OK)
869 return result;
[email protected]2181ea002009-06-09 01:37:27870 }
871
[email protected]0877e3d2009-10-17 22:29:57872 if (result < 0 && result != ERR_CONNECTION_CLOSED)
initial.commit586acc5fe2008-07-26 22:42:52873 return HandleIOError(result);
874
[email protected]0877e3d2009-10-17 22:29:57875 if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) {
[email protected]1c773ea12009-04-28 19:58:42876 ResetConnectionAndRequestForResend();
[email protected]0877e3d2009-10-17 22:29:57877 return OK;
[email protected]1c773ea12009-04-28 19:58:42878 }
[email protected]2a5c76b2008-09-25 22:15:16879
[email protected]0877e3d2009-10-17 22:29:57880 // After we call RestartWithAuth a new response_time will be recorded, and
881 // we need to be cautious about incorrectly logging the duration across the
882 // authentication activity.
[email protected]8e6441ca2010-08-19 05:56:38883 if (result == OK)
884 LogTransactionConnectedMetrics();
initial.commit586acc5fe2008-07-26 22:42:52885
[email protected]0877e3d2009-10-17 22:29:57886 if (result == ERR_CONNECTION_CLOSED) {
[email protected]02c92c492010-03-08 21:28:14887 // For now, if we get at least some data, we do the best we can to make
[email protected]9492e4a2010-02-24 00:58:46888 // sense of it and send it back up the stack.
[email protected]0e75a732008-10-16 20:36:09889 int rv = HandleConnectionClosedBeforeEndOfHeaders();
[email protected]aecfbf22008-10-16 02:02:47890 if (rv != OK)
891 return rv;
[email protected]0877e3d2009-10-17 22:29:57892 }
[email protected]a1ff6e22012-03-27 15:31:34893 DCHECK(response_.headers);
initial.commit586acc5fe2008-07-26 22:42:52894
[email protected]14b7e9a2012-10-16 19:51:29895 // Server-induced fallback is supported only if this is a PAC configured
896 // proxy. See: http://crbug.com/143712
897 if (response_.was_fetched_via_proxy && proxy_info_.did_use_pac_script()) {
898 if (response_.headers != NULL &&
899 response_.headers->HasHeaderValue("connection", "proxy-bypass")) {
900 ProxyService* proxy_service = session_->proxy_service();
901 if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) {
902 // Only retry in the case of GETs. We don't want to resubmit a POST
903 // if the proxy took some action.
904 if (request_->method == "GET") {
905 ResetConnectionAndRequestForResend();
906 return OK;
907 }
908 }
909 }
910 }
911
[email protected]93f8b562012-03-27 01:00:16912 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
913 if (request_->load_flags & LOAD_MAIN_FRAME) {
914 const int response_code = response_.headers->response_code();
915 UMA_HISTOGRAM_ENUMERATION(
916 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
917 }
918
[email protected]3abacd62012-06-10 20:20:32919 net_log_.AddEvent(
920 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
921 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:39922
[email protected]a7e41312009-12-16 23:18:14923 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:57924 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
925 // indicates a buggy server. See:
926 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
927 if (request_->method == "PUT")
928 return ERR_METHOD_NOT_SUPPORTED;
929 }
[email protected]4ddaf2502008-10-23 18:26:19930
[email protected]0877e3d2009-10-17 22:29:57931 // Check for an intermediate 100 Continue response. An origin server is
932 // allowed to send this response even if we didn't ask for it, so we just
933 // need to skip over it.
934 // We treat any other 1xx in this same way (although in practice getting
935 // a 1xx that isn't a 100 is rare).
[email protected]a7e41312009-12-16 23:18:14936 if (response_.headers->response_code() / 100 == 1) {
[email protected]007b3f82013-04-09 08:46:45937 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:57938 next_state_ = STATE_READ_HEADERS;
939 return OK;
940 }
941
[email protected]8e6441ca2010-08-19 05:56:38942 HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(),
943 request_->url.EffectiveIntPort());
944 ProcessAlternateProtocol(session_->http_stream_factory(),
[email protected]17291a022011-10-10 07:32:53945 session_->http_server_properties(),
[email protected]8e6441ca2010-08-19 05:56:38946 *response_.headers,
947 endpoint);
[email protected]564b4912010-03-09 16:30:42948
[email protected]e772db3f2010-07-12 18:11:13949 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:57950 if (rv != OK)
951 return rv;
952
[email protected]8536ef52010-09-30 16:18:21953 if (is_https_request())
954 stream_->GetSSLInfo(&response_.ssl_info);
955
[email protected]0877e3d2009-10-17 22:29:57956 headers_valid_ = true;
957 return OK;
initial.commit586acc5fe2008-07-26 22:42:52958}
959
960int HttpNetworkTransaction::DoReadBody() {
961 DCHECK(read_buf_);
[email protected]6501bc02009-06-25 20:55:13962 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:38963 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:52964
965 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41966 return stream_->ReadResponseBody(read_buf_, read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52967}
968
969int HttpNetworkTransaction::DoReadBodyComplete(int result) {
970 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:38971 bool done = false;
972 if (result <= 0) {
973 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:52974 done = true;
[email protected]8e6441ca2010-08-19 05:56:38975 }
[email protected]9492e4a2010-02-24 00:58:46976
[email protected]8e6441ca2010-08-19 05:56:38977 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:31978 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:38979 // Note: Just because IsResponseBodyComplete is true, we're not
980 // necessarily "done". We're only "done" when it is the last
981 // read on this HttpNetworkTransaction, which will be signified
982 // by a zero-length read.
983 // TODO(mbelshe): The keepalive property is really a property of
984 // the stream. No need to compute it here just to pass back
985 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:47986 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
987 // ResponseHeaders.
988 if (stream_->CanFindEndOfResponse()) {
989 HttpResponseHeaders* headers = GetResponseHeaders();
990 if (headers)
991 keep_alive = headers->IsKeepAlive();
992 }
initial.commit586acc5fe2008-07-26 22:42:52993 }
994
[email protected]8e6441ca2010-08-19 05:56:38995 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:52996 if (done) {
[email protected]56300172008-11-06 18:42:55997 LogTransactionMetrics();
[email protected]8e6441ca2010-08-19 05:56:38998 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:01999 // Note: we don't reset the stream here. We've closed it, but we still
1000 // need it around so that callers can call methods such as
1001 // GetUploadProgress() and have them be meaningful.
1002 // TODO(mbelshe): This means we closed the stream here, and we close it
1003 // again in ~HttpNetworkTransaction. Clean that up.
1004
[email protected]8e6441ca2010-08-19 05:56:381005 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521006 }
1007
1008 // Clear these to avoid leaving around old state.
1009 read_buf_ = NULL;
1010 read_buf_len_ = 0;
1011
1012 return result;
1013}
1014
[email protected]2d2697f92009-02-18 21:00:321015int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1016 // This method differs from DoReadBody only in the next_state_. So we just
1017 // call DoReadBody and override the next_state_. Perhaps there is a more
1018 // elegant way for these two methods to share code.
1019 int rv = DoReadBody();
1020 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1021 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1022 return rv;
1023}
1024
[email protected]0877e3d2009-10-17 22:29:571025// TODO(wtc): This method and the DoReadBodyComplete method are almost
1026// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321027int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231028 // keep_alive defaults to true because the very reason we're draining the
1029 // response body is to reuse the connection for auth restart.
1030 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321031 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571032 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321033 done = true;
[email protected]68873ba2009-06-04 21:49:231034 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311035 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571036 done = true;
[email protected]2d2697f92009-02-18 21:00:321037 }
1038
1039 if (done) {
1040 DidDrainBodyForAuthRestart(keep_alive);
1041 } else {
1042 // Keep draining.
1043 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1044 }
1045
1046 return OK;
1047}
1048
[email protected]8e3d2d32010-06-13 18:46:231049void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1050 if (logged_response_time_)
1051 return;
1052
1053 logged_response_time_ = true;
1054
[email protected]a7e41312009-12-16 23:18:141055 base::TimeDelta total_duration = response_.response_time - start_time_;
[email protected]9a0a55f2009-04-13 23:23:031056
[email protected]d6b55392011-08-05 04:04:351057 UMA_HISTOGRAM_CUSTOM_TIMES(
1058 "Net.Transaction_Connected",
[email protected]510e854f2009-04-20 18:39:081059 total_duration,
[email protected]9a0a55f2009-04-13 23:23:031060 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1061 100);
[email protected]1fa47592009-07-27 22:45:001062
[email protected]c38ec5a2010-08-25 23:32:381063 bool reused_socket = stream_->IsConnectionReused();
1064 if (!reused_socket) {
[email protected]d6b55392011-08-05 04:04:351065 UMA_HISTOGRAM_CUSTOM_TIMES(
1066 "Net.Transaction_Connected_New_b",
[email protected]b01998a2009-04-21 01:01:111067 total_duration,
1068 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1069 100);
[email protected]0310d432009-08-25 07:49:521070 }
1071
[email protected]31ae7ab2012-04-24 21:09:051072 // Currently, non-HIGHEST priority requests are frame or sub-frame resource
[email protected]510e854f2009-04-20 18:39:081073 // types. This will change when we also prioritize certain subresources like
1074 // css, js, etc.
[email protected]262eec82013-03-19 21:01:361075 if (priority_ != HIGHEST) {
[email protected]d6b55392011-08-05 04:04:351076 UMA_HISTOGRAM_CUSTOM_TIMES(
1077 "Net.Priority_High_Latency_b",
[email protected]510e854f2009-04-20 18:39:081078 total_duration,
1079 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1080 100);
1081 } else {
[email protected]d6b55392011-08-05 04:04:351082 UMA_HISTOGRAM_CUSTOM_TIMES(
1083 "Net.Priority_Low_Latency_b",
[email protected]510e854f2009-04-20 18:39:081084 total_duration,
1085 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1086 100);
1087 }
[email protected]9a0a55f2009-04-13 23:23:031088}
1089
[email protected]56300172008-11-06 18:42:551090void HttpNetworkTransaction::LogTransactionMetrics() const {
[email protected]0877e3d2009-10-17 22:29:571091 base::TimeDelta duration = base::Time::Now() -
[email protected]2227c692010-05-04 15:36:111092 response_.request_time;
[email protected]56300172008-11-06 18:42:551093 if (60 < duration.InMinutes())
1094 return;
[email protected]0b48db42009-03-23 02:45:111095
[email protected]21b316a2009-03-23 18:25:061096 base::TimeDelta total_duration = base::Time::Now() - start_time_;
1097
[email protected]d6b55392011-08-05 04:04:351098 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration,
1099 base::TimeDelta::FromMilliseconds(1),
1100 base::TimeDelta::FromMinutes(10),
1101 100);
1102 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1103 total_duration,
1104 base::TimeDelta::FromMilliseconds(1),
1105 base::TimeDelta::FromMinutes(10), 100);
[email protected]5c68d692011-08-24 04:59:411106
[email protected]c38ec5a2010-08-25 23:32:381107 if (!stream_->IsConnectionReused()) {
[email protected]d6b55392011-08-05 04:04:351108 UMA_HISTOGRAM_CUSTOM_TIMES(
1109 "Net.Transaction_Latency_Total_New_Connection",
[email protected]808f6402009-03-30 20:02:071110 total_duration, base::TimeDelta::FromMilliseconds(1),
1111 base::TimeDelta::FromMinutes(10), 100);
1112 }
[email protected]56300172008-11-06 18:42:551113}
1114
[email protected]5e363962009-06-19 19:57:011115int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381116 // There are two paths through which the server can request a certificate
1117 // from us. The first is during the initial handshake, the second is
1118 // during SSL renegotiation.
1119 //
1120 // In both cases, we want to close the connection before proceeding.
1121 // We do this for two reasons:
1122 // First, we don't want to keep the connection to the server hung for a
1123 // long time while the user selects a certificate.
1124 // Second, even if we did keep the connection open, NSS has a bug where
1125 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581126 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381127
1128 if (stream_.get()) {
1129 // Since we already have a stream, we're being called as part of SSL
1130 // renegotiation.
1131 DCHECK(!stream_request_.get());
1132 stream_->Close(true);
1133 stream_.reset();
1134 }
1135
[email protected]26816882010-10-14 18:03:091136 // The server is asking for a client certificate during the initial
1137 // handshake.
1138 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011139
[email protected]ec229bc92010-11-22 09:51:451140 // If the user selected one of the certificates in client_certs or declined
1141 // to provide one for this server before, use the past decision
1142 // automatically.
1143 scoped_refptr<X509Certificate> client_cert;
1144 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1145 response_.cert_request_info->host_and_port, &client_cert);
1146 if (!found_cached_cert)
1147 return error;
1148
1149 // Check that the certificate selected is still a certificate the server
1150 // is likely to accept, based on the criteria supplied in the
1151 // CertificateRequest message.
[email protected]5e363962009-06-19 19:57:011152 if (client_cert) {
[email protected]f1958c382013-02-07 00:15:261153 const std::vector<std::string>& cert_authorities =
1154 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451155
[email protected]f1958c382013-02-07 00:15:261156 bool cert_still_valid = cert_authorities.empty() ||
1157 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451158 if (!cert_still_valid)
1159 return error;
[email protected]5e363962009-06-19 19:57:011160 }
[email protected]ec229bc92010-11-22 09:51:451161
1162 // TODO(davidben): Add a unit test which covers this path; we need to be
1163 // able to send a legitimate certificate and also bypass/clear the
1164 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141165 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1166 &proxy_ssl_config_ : &server_ssl_config_;
1167 ssl_config->send_client_cert = true;
1168 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451169 next_state_ = STATE_CREATE_STREAM;
1170 // Reset the other member variables.
1171 // Note: this is necessary only with SSL renegotiation.
1172 ResetStateForRestart();
1173 return OK;
[email protected]0b45559b2009-06-12 21:45:111174}
1175
[email protected]bd0b6772011-01-11 19:59:301176// TODO(rch): This does not correctly handle errors when an SSL proxy is
1177// being used, as all of the errors are handled as if they were generated
1178// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381179// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301180int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1181 DCHECK(request_);
[email protected]102957f2011-09-02 17:10:141182 if (server_ssl_config_.send_client_cert &&
[email protected]a1cb2cd2011-02-05 02:02:081183 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
[email protected]bd0b6772011-01-11 19:59:301184 session_->ssl_client_auth_cache()->Remove(
1185 GetHostAndPort(request_->url));
1186 }
1187
[email protected]158ac972013-04-19 23:29:231188 uint16 version_max = server_ssl_config_.version_max;
1189
initial.commit586acc5fe2008-07-26 22:42:521190 switch (error) {
[email protected]bd0b6772011-01-11 19:59:301191 case ERR_SSL_PROTOCOL_ERROR:
1192 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231193 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1194 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171195 // This could be a TLS-intolerant server or a server that chose a
1196 // cipher suite defined only for higher protocol versions (such as
1197 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1198 // back to the next lower version and retry.
1199 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1200 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1201 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1202 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1203 // version_max should match the maximum protocol version supported
1204 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231205 version_max--;
1206
1207 // Fallback to the lower SSL version.
1208 // While SSL 3.0 fallback should be eliminated because of security
1209 // reasons, there is a high risk of breaking the servers if this is
1210 // done in general.
1211 // For now SSL 3.0 fallback is disabled for Google servers first,
1212 // and will be expanded to other servers after enough experiences
1213 // have been gained showing that this experiment works well with
1214 // today's Internet.
1215 if (version_max > SSL_PROTOCOL_VERSION_SSL3 ||
1216 (server_ssl_config_.unrestricted_ssl3_fallback_enabled ||
1217 !TransportSecurityState::IsGooglePinnedProperty(
1218 request_->url.host(), true /* include SNI */))) {
1219 net_log_.AddEvent(
1220 NetLog::TYPE_SSL_VERSION_FALLBACK,
1221 base::Bind(&NetLogSSLVersionFallbackCallback,
1222 &request_->url, error, server_ssl_config_.version_max,
1223 version_max));
1224 server_ssl_config_.version_max = version_max;
1225 server_ssl_config_.version_fallback = true;
1226 ResetConnectionAndRequestForResend();
1227 error = OK;
1228 }
[email protected]1c773ea12009-04-28 19:58:421229 }
initial.commit586acc5fe2008-07-26 22:42:521230 break;
1231 }
[email protected]158ac972013-04-19 23:29:231232
initial.commit586acc5fe2008-07-26 22:42:521233 return error;
1234}
1235
[email protected]bd0b6772011-01-11 19:59:301236// This method determines whether it is safe to resend the request after an
1237// IO error. It can only be called in response to request header or body
1238// write errors or response header read errors. It should not be used in
1239// other cases, such as a Connect error.
1240int HttpNetworkTransaction::HandleIOError(int error) {
1241 // SSL errors may happen at any time during the stream and indicate issues
1242 // with the underlying connection. Because the peer may request
1243 // renegotiation at any time, check and handle any possible SSL handshake
[email protected]80c75f682012-05-26 16:22:171244 // related errors. In addition to renegotiation, TLS False Start may cause
1245 // SSL handshake errors (specifically servers with buggy DEFLATE support)
1246 // to be delayed until the first Read on the underlying connection.
[email protected]bd0b6772011-01-11 19:59:301247 error = HandleSSLHandshakeError(error);
1248
1249 switch (error) {
1250 // If we try to reuse a connection that the server is in the process of
1251 // closing, we may end up successfully writing out our request (or a
1252 // portion of our request) only to find a connection error when we try to
1253 // read from (or finish writing to) the socket.
1254 case ERR_CONNECTION_RESET:
1255 case ERR_CONNECTION_CLOSED:
1256 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511257 // There can be a race between the socket pool checking checking whether a
1258 // socket is still connected, receiving the FIN, and sending/reading data
1259 // on a reused socket. If we receive the FIN between the connectedness
1260 // check and writing/reading from the socket, we may first learn the socket
1261 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1262 // likely happen when trying to retrieve its IP address.
1263 // See http://crbug.com/105824 for more details.
1264 case ERR_SOCKET_NOT_CONNECTED:
[email protected]bd0b6772011-01-11 19:59:301265 if (ShouldResendRequest(error)) {
[email protected]b6754252012-06-13 23:14:381266 net_log_.AddEventWithNetErrorCode(
1267 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301268 ResetConnectionAndRequestForResend();
1269 error = OK;
1270 }
1271 break;
[email protected]5a60c8b2011-10-19 20:14:291272 case ERR_PIPELINE_EVICTION:
[email protected]5477d892012-03-01 21:31:311273 if (!session_->force_http_pipelining()) {
[email protected]b6754252012-06-13 23:14:381274 net_log_.AddEventWithNetErrorCode(
1275 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]5477d892012-03-01 21:31:311276 ResetConnectionAndRequestForResend();
1277 error = OK;
1278 }
1279 break;
[email protected]8753a122011-10-16 08:05:081280 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001281 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]b6754252012-06-13 23:14:381282 net_log_.AddEventWithNetErrorCode(
1283 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001284 ResetConnectionAndRequestForResend();
1285 error = OK;
1286 break;
[email protected]bd0b6772011-01-11 19:59:301287 }
1288 return error;
1289}
1290
[email protected]c3b35c22008-09-27 03:19:421291void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581292 ResetStateForAuthRestart();
1293 stream_.reset();
1294}
1295
1296void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251297 send_start_time_ = base::TimeTicks();
1298 send_end_time_ = base::TimeTicks();
1299 receive_headers_end_ = base::TimeTicks();
1300
[email protected]0757e7702009-03-27 04:00:221301 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421302 read_buf_ = NULL;
1303 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571304 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201305 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141306 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381307 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571308}
1309
1310HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]a7e41312009-12-16 23:18:141311 return response_.headers;
[email protected]c3b35c22008-09-27 03:19:421312}
1313
[email protected]a19f1c602009-08-24 21:35:281314bool HttpNetworkTransaction::ShouldResendRequest(int error) const {
[email protected]8e6441ca2010-08-19 05:56:381315 bool connection_is_proven = stream_->IsConnectionReused();
1316 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161317
[email protected]2a5c76b2008-09-25 22:15:161318 // NOTE: we resend a request only if we reused a keep-alive connection.
1319 // This automatically prevents an infinite resend loop because we'll run
1320 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381321 if (connection_is_proven && !has_received_headers)
1322 return true;
1323 return false;
[email protected]1c773ea12009-04-28 19:58:421324}
1325
1326void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381327 if (stream_.get()) {
1328 stream_->Close(true);
1329 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161330 }
1331
[email protected]0877e3d2009-10-17 22:29:571332 // We need to clear request_headers_ because it contains the real request
1333 // headers, but we may need to resend the CONNECT request first to recreate
1334 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201335 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501336 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541337}
1338
[email protected]1c773ea12009-04-28 19:58:421339bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
[email protected]2df19bb2010-08-25 20:13:461340 return !is_https_request() &&
1341 (proxy_info_.is_https() || proxy_info_.is_http());
[email protected]1c773ea12009-04-28 19:58:421342}
license.botbf09a502008-08-24 00:55:551343
[email protected]1c773ea12009-04-28 19:58:421344bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041345 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421346}
1347
[email protected]e772db3f2010-07-12 18:11:131348int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271349 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]0877e3d2009-10-17 22:29:571350 DCHECK(headers);
[email protected]c3b35c22008-09-27 03:19:421351
[email protected]0877e3d2009-10-17 22:29:571352 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581353 if (status != HTTP_UNAUTHORIZED &&
1354 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421355 return OK;
[email protected]9094b602012-02-27 21:44:581356 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111357 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161358 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1359 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421360
[email protected]9094b602012-02-27 21:44:581361 // This case can trigger when an HTTPS server responds with a "Proxy
1362 // authentication required" status code through a non-authenticating
1363 // proxy.
[email protected]7a67a8152010-11-05 18:31:101364 if (!auth_controllers_[target].get())
1365 return ERR_UNEXPECTED_PROXY_AUTH;
1366
[email protected]a7ea8832010-07-12 17:54:541367 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311368 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1369 net_log_);
[email protected]228404f2010-06-24 04:31:411370 if (auth_controllers_[target]->HaveAuthHandler())
1371 pending_auth_target_ = target;
1372
1373 scoped_refptr<AuthChallengeInfo> auth_info =
1374 auth_controllers_[target]->auth_info();
1375 if (auth_info.get())
1376 response_.auth_challenge = auth_info;
1377
[email protected]228404f2010-06-24 04:31:411378 return rv;
[email protected]f9ee6b52008-11-08 06:46:231379}
1380
[email protected]8e6441ca2010-08-19 05:56:381381bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1382 return auth_controllers_[target].get() &&
1383 auth_controllers_[target]->HaveAuth();
1384}
1385
[email protected]228404f2010-06-24 04:31:411386GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1387 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461388 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411389 if (!proxy_info_.proxy_server().is_valid() ||
1390 proxy_info_.proxy_server().is_direct()) {
1391 return GURL(); // There is no proxy server.
1392 }
[email protected]2df19bb2010-08-25 20:13:461393 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1394 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351395 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461396 }
[email protected]228404f2010-06-24 04:31:411397 case HttpAuth::AUTH_SERVER:
1398 return request_->url;
1399 default:
1400 return GURL();
1401 }
[email protected]c3b35c22008-09-27 03:19:421402}
1403
[email protected]d8eb84242010-09-25 02:25:061404#define STATE_CASE(s) \
1405 case s: \
1406 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1407 break
[email protected]aef04272010-06-28 18:03:041408
1409std::string HttpNetworkTransaction::DescribeState(State state) {
1410 std::string description;
1411 switch (state) {
[email protected]82918cc2010-08-25 17:24:501412 STATE_CASE(STATE_CREATE_STREAM);
1413 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131414 STATE_CASE(STATE_INIT_REQUEST_BODY);
1415 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511416 STATE_CASE(STATE_BUILD_REQUEST);
1417 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041418 STATE_CASE(STATE_SEND_REQUEST);
1419 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1420 STATE_CASE(STATE_READ_HEADERS);
1421 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041422 STATE_CASE(STATE_READ_BODY);
1423 STATE_CASE(STATE_READ_BODY_COMPLETE);
1424 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1425 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041426 STATE_CASE(STATE_NONE);
1427 default:
[email protected]d8eb84242010-09-25 02:25:061428 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1429 state);
[email protected]aef04272010-06-28 18:03:041430 break;
1431 }
1432 return description;
1433}
1434
1435#undef STATE_CASE
1436
[email protected]c3b35c22008-09-27 03:19:421437} // namespace net