blob: 1c55202148edbd05641ba4740990bb46a6fed08b [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]aa249b52013-04-30 01:04:32119 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
120 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52121 session_(session),
122 request_(NULL),
[email protected]262eec82013-03-19 21:01:36123 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57124 headers_valid_(false),
[email protected]8e3d2d32010-06-13 18:46:23125 logged_response_time_(false),
[email protected]b94f92d2010-10-27 16:45:20126 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52127 read_buf_len_(0),
[email protected]a7ea8832010-07-12 17:54:54128 next_state_(STATE_NONE),
129 establishing_tunnel_(false) {
[email protected]102957f2011-09-02 17:10:14130 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]5285d972011-10-18 18:56:34131 if (session->http_stream_factory()->has_next_protos()) {
[email protected]102957f2011-09-02 17:10:14132 server_ssl_config_.next_protos =
[email protected]5285d972011-10-18 18:56:34133 session->http_stream_factory()->next_protos();
[email protected]102957f2011-09-02 17:10:14134 }
[email protected]99ffa5a2011-10-06 04:20:19135 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50136}
137
[email protected]0b0bf032010-09-21 18:08:50138HttpNetworkTransaction::~HttpNetworkTransaction() {
139 if (stream_.get()) {
140 HttpResponseHeaders* headers = GetResponseHeaders();
141 // TODO(mbelshe): The stream_ should be able to compute whether or not the
142 // stream should be kept alive. No reason to compute here
143 // and pass it in.
144 bool try_to_keep_alive =
145 next_state_ == STATE_NONE &&
146 stream_->CanFindEndOfResponse() &&
147 (!headers || headers->IsKeepAlive());
148 if (!try_to_keep_alive) {
149 stream_->Close(true /* not reusable */);
150 } else {
151 if (stream_->IsResponseBodyComplete()) {
152 // If the response body is complete, we can just reuse the socket.
153 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44154 } else if (stream_->IsSpdyHttpStream()) {
155 // Doesn't really matter for SpdyHttpStream. Just close it.
156 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50157 } else {
158 // Otherwise, we try to drain the response body.
[email protected]92b9a3e2012-11-09 08:52:52159 HttpStreamBase* stream = stream_.release();
[email protected]90499482013-06-01 00:39:50160 stream->Drain(session_.get());
[email protected]0b0bf032010-09-21 18:08:50161 }
162 }
163 }
[email protected]0b0bf032010-09-21 18:08:50164}
165
[email protected]684970b2009-08-14 04:54:46166int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41167 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53168 const BoundNetLog& net_log) {
[email protected]5e2e6c77d12009-12-24 21:57:16169 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
[email protected]5d0153c512009-01-12 19:08:36170
[email protected]9e743cd2010-03-16 07:03:53171 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04172 request_ = request_info;
[email protected]21b316a2009-03-23 18:25:06173 start_time_ = base::Time::Now();
[email protected]96d570e42008-08-05 22:43:04174
[email protected]99ffa5a2011-10-06 04:20:19175 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14176 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19177 proxy_ssl_config_.rev_checking_enabled = false;
178 }
[email protected]6fbac162011-06-20 00:29:04179
[email protected]e6d017652013-05-17 18:01:40180 // Channel ID is enabled unless --disable-tls-channel-id flag is set,
181 // or if privacy mode is enabled.
182 bool channel_id_enabled = server_ssl_config_.channel_id_enabled &&
183 (request_->privacy_mode == kPrivacyModeDisabled);
184 server_ssl_config_.channel_id_enabled = channel_id_enabled;
185
[email protected]82918cc2010-08-25 17:24:50186 next_state_ = STATE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04187 int rv = DoLoop(OK);
188 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41189 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04190 return rv;
191}
192
193int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41194 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38195 DCHECK(!stream_.get());
196 DCHECK(!stream_request_.get());
197 DCHECK_EQ(STATE_NONE, next_state_);
198
[email protected]82918cc2010-08-25 17:24:50199 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38200
[email protected]ccb40e52008-09-17 20:54:40201 int rv = DoLoop(OK);
202 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41203 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11204 return rv;
[email protected]96d570e42008-08-05 22:43:04205}
206
[email protected]0b45559b2009-06-12 21:45:11207int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41208 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38209 // In HandleCertificateRequest(), we always tear down existing stream
210 // requests to force a new connection. So we shouldn't have one here.
211 DCHECK(!stream_request_.get());
212 DCHECK(!stream_.get());
213 DCHECK_EQ(STATE_NONE, next_state_);
214
[email protected]102957f2011-09-02 17:10:14215 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
216 &proxy_ssl_config_ : &server_ssl_config_;
217 ssl_config->send_client_cert = true;
218 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45219 session_->ssl_client_auth_cache()->Add(
220 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11221 // Reset the other member variables.
222 // Note: this is necessary only with SSL renegotiation.
223 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50224 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11225 int rv = DoLoop(OK);
226 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41227 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11228 return rv;
229}
230
[email protected]49639fa2011-12-20 23:22:41231int HttpNetworkTransaction::RestartWithAuth(
232 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22233 HttpAuth::Target target = pending_auth_target_;
234 if (target == HttpAuth::AUTH_NONE) {
235 NOTREACHED();
236 return ERR_UNEXPECTED;
237 }
[email protected]0757e7702009-03-27 04:00:22238 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42239
[email protected]f3cf9802011-10-28 18:44:58240 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13241
[email protected]49639fa2011-12-20 23:22:41242 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38243
244 int rv = OK;
245 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
246 // In this case, we've gathered credentials for use with proxy
247 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50248 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38249 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59250 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54251 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58252 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54253 } else {
[email protected]8e6441ca2010-08-19 05:56:38254 // In this case, we've gathered credentials for the server or the proxy
255 // but it is not during the tunneling phase.
256 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54257 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38258 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54259 }
[email protected]c3b35c22008-09-27 03:19:42260
[email protected]c3b35c22008-09-27 03:19:42261 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41262 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42263 return rv;
[email protected]96d570e42008-08-05 22:43:04264}
265
[email protected]f9ee6b52008-11-08 06:46:23266void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
267 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38268 DCHECK(!stream_request_.get());
269
[email protected]2d2697f92009-02-18 21:00:32270 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57271 // Even if the server says the connection is keep-alive, we have to be
272 // able to find the end of each response in order to reuse the connection.
273 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31274 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57275 // If the response body hasn't been completely read, we need to drain
276 // it first.
[email protected]351ab642010-08-05 16:55:31277 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32278 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57279 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32280 read_buf_len_ = kDrainBodyBufferSize;
281 return;
282 }
[email protected]0877e3d2009-10-17 22:29:57283 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09284 }
285
[email protected]2d2697f92009-02-18 21:00:32286 // We don't need to drain the response body, so we act as if we had drained
287 // the response body.
288 DidDrainBodyForAuthRestart(keep_alive);
289}
290
291void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38292 DCHECK(!stream_request_.get());
293
294 if (stream_.get()) {
[email protected]697ef4c2010-10-14 16:38:58295 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46296 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38297 // We should call connection_->set_idle_time(), but this doesn't occur
298 // often enough to be worth the trouble.
299 stream_->SetConnectionReused();
[email protected]92b9a3e2012-11-09 08:52:52300 new_stream =
301 static_cast<HttpStream*>(stream_.get())->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38302 }
[email protected]697ef4c2010-10-14 16:38:58303
304 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46305 // Close the stream and mark it as not_reusable. Even in the
306 // keep_alive case, we've determined that the stream_ is not
307 // reusable if new_stream is NULL.
308 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58309 next_state_ = STATE_CREATE_STREAM;
310 } else {
311 next_state_ = STATE_INIT_STREAM;
312 }
313 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32314 }
[email protected]f9ee6b52008-11-08 06:46:23315
316 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58317 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23318}
319
[email protected]8e6441ca2010-08-19 05:56:38320bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
321 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
322 HaveAuth(pending_auth_target_);
323}
324
[email protected]9dea9e1f2009-01-29 00:30:47325int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41326 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04327 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35328 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04329
[email protected]1f14a912009-12-21 20:32:44330 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04331
[email protected]ad8e04a2010-11-01 04:16:27332 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38333 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04334 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29335 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04336 // bytes when establishing a tunnel because they might be controlled by
337 // an active network attacker. We don't worry about this for HTTP
338 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29339 // We reach this case when the user cancels a 407 proxy auth prompt. We
340 // also don't worry about this for an HTTPS Proxy, because the
341 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04342 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46343 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58344 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54345 LOG(WARNING) << "Blocked proxy response with status "
346 << headers->response_code() << " to CONNECT request for "
347 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04348 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44349 }
350
[email protected]e60e47a2010-07-14 03:37:18351 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31352 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18353
[email protected]96d570e42008-08-05 22:43:04354 read_buf_ = buf;
355 read_buf_len_ = buf_len;
356
[email protected]1f14a912009-12-21 20:32:44357 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04358 int rv = DoLoop(OK);
359 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41360 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04361 return rv;
362}
363
364const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
[email protected]90499482013-06-01 00:39:50365 return ((headers_valid_ && response_.headers.get()) ||
366 response_.ssl_info.cert.get() || response_.cert_request_info.get())
367 ? &response_
368 : NULL;
[email protected]96d570e42008-08-05 22:43:04369}
370
371LoadState HttpNetworkTransaction::GetLoadState() const {
372 // TODO(wtc): Define a new LoadState value for the
373 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
374 switch (next_state_) {
[email protected]82918cc2010-08-25 17:24:50375 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38376 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15377 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
378 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57379 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04380 return LOAD_STATE_SENDING_REQUEST;
381 case STATE_READ_HEADERS_COMPLETE:
382 return LOAD_STATE_WAITING_FOR_RESPONSE;
383 case STATE_READ_BODY_COMPLETE:
384 return LOAD_STATE_READING_RESPONSE;
385 default:
386 return LOAD_STATE_IDLE;
387 }
388}
389
[email protected]196d18a2012-08-30 03:47:31390UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31391 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31392 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04393
[email protected]92b9a3e2012-11-09 08:52:52394 // TODO(bashi): This cast is temporary. Remove later.
395 return static_cast<HttpStream*>(stream_.get())->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04396}
397
[email protected]5033ab82013-03-22 20:17:46398bool HttpNetworkTransaction::GetLoadTimingInfo(
399 LoadTimingInfo* load_timing_info) const {
400 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
401 return false;
402
403 load_timing_info->proxy_resolve_start =
404 proxy_info_.proxy_resolve_start_time();
405 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
406 load_timing_info->send_start = send_start_time_;
407 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46408 return true;
409}
410
411void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
412 priority_ = priority;
413 // TODO(akalin): Plumb this through to |stream_request_| and
414 // |stream_|.
415}
416
[email protected]102e27c2011-02-23 01:01:31417void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
418 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52419 HttpStreamBase* stream) {
[email protected]82918cc2010-08-25 17:24:50420 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38421 DCHECK(stream_request_.get());
422
423 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14424 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31425 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38426 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51427 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
428 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38429 response_.was_fetched_via_spdy = stream_request_->using_spdy();
430 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]8e6441ca2010-08-19 05:56:38431
432 OnIOComplete(OK);
433}
434
[email protected]102e27c2011-02-23 01:01:31435void HttpNetworkTransaction::OnStreamFailed(int result,
436 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50437 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38438 DCHECK_NE(OK, result);
439 DCHECK(stream_request_.get());
440 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14441 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38442
443 OnIOComplete(result);
444}
445
[email protected]102e27c2011-02-23 01:01:31446void HttpNetworkTransaction::OnCertificateError(
447 int result,
448 const SSLConfig& used_ssl_config,
449 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50450 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38451 DCHECK_NE(OK, result);
452 DCHECK(stream_request_.get());
453 DCHECK(!stream_.get());
454
455 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14456 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38457
458 // TODO(mbelshe): For now, we're going to pass the error through, and that
459 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50460 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
461 // good and the user chooses to ignore the error. This is not ideal, but not
462 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38463
464 OnIOComplete(result);
465}
466
467void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50468 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31469 const SSLConfig& used_ssl_config,
470 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50471 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38472 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50473 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38474
475 establishing_tunnel_ = true;
476 response_.headers = proxy_response.headers;
477 response_.auth_challenge = proxy_response.auth_challenge;
478 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14479 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31480 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38481
482 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
483 pending_auth_target_ = HttpAuth::AUTH_PROXY;
484
485 DoCallback(OK);
486}
487
488void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31489 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50490 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50491 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38492
[email protected]102957f2011-09-02 17:10:14493 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38494 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58495 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38496}
497
[email protected]511f6f52010-12-17 03:58:29498void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
499 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31500 const SSLConfig& used_ssl_config,
501 const ProxyInfo& used_proxy_info,
[email protected]92b9a3e2012-11-09 08:52:52502 HttpStreamBase* stream) {
[email protected]511f6f52010-12-17 03:58:29503 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
504
505 headers_valid_ = true;
506 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14507 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31508 proxy_info_ = used_proxy_info;
[email protected]511f6f52010-12-17 03:58:29509 stream_.reset(stream);
510 stream_request_.reset(); // we're done with the stream request
511 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
512}
513
[email protected]8e6441ca2010-08-19 05:56:38514bool HttpNetworkTransaction::is_https_request() const {
515 return request_->url.SchemeIs("https");
initial.commit586acc5fe2008-07-26 22:42:52516}
517
initial.commit586acc5fe2008-07-26 22:42:52518void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50519 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41520 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52521
[email protected]96d570e42008-08-05 22:43:04522 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41523 CompletionCallback c = callback_;
524 callback_.Reset();
525 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52526}
527
528void HttpNetworkTransaction::OnIOComplete(int result) {
529 int rv = DoLoop(result);
530 if (rv != ERR_IO_PENDING)
531 DoCallback(rv);
532}
533
534int HttpNetworkTransaction::DoLoop(int result) {
535 DCHECK(next_state_ != STATE_NONE);
536
537 int rv = result;
538 do {
539 State state = next_state_;
540 next_state_ = STATE_NONE;
541 switch (state) {
[email protected]82918cc2010-08-25 17:24:50542 case STATE_CREATE_STREAM:
543 DCHECK_EQ(OK, rv);
544 rv = DoCreateStream();
545 break;
546 case STATE_CREATE_STREAM_COMPLETE:
547 rv = DoCreateStreamComplete(rv);
548 break;
[email protected]351ab642010-08-05 16:55:31549 case STATE_INIT_STREAM:
550 DCHECK_EQ(OK, rv);
551 rv = DoInitStream();
552 break;
553 case STATE_INIT_STREAM_COMPLETE:
554 rv = DoInitStreamComplete(rv);
555 break;
[email protected]044de0642010-06-17 10:42:15556 case STATE_GENERATE_PROXY_AUTH_TOKEN:
557 DCHECK_EQ(OK, rv);
558 rv = DoGenerateProxyAuthToken();
559 break;
560 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
561 rv = DoGenerateProxyAuthTokenComplete(rv);
562 break;
563 case STATE_GENERATE_SERVER_AUTH_TOKEN:
564 DCHECK_EQ(OK, rv);
565 rv = DoGenerateServerAuthToken();
566 break;
567 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
568 rv = DoGenerateServerAuthTokenComplete(rv);
569 break;
[email protected]daddea62012-09-19 05:51:13570 case STATE_INIT_REQUEST_BODY:
571 DCHECK_EQ(OK, rv);
572 rv = DoInitRequestBody();
573 break;
574 case STATE_INIT_REQUEST_BODY_COMPLETE:
575 rv = DoInitRequestBodyComplete(rv);
576 break;
[email protected]4875ba12011-03-30 22:31:51577 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55578 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38579 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51580 rv = DoBuildRequest();
581 break;
582 case STATE_BUILD_REQUEST_COMPLETE:
583 rv = DoBuildRequestComplete(rv);
584 break;
585 case STATE_SEND_REQUEST:
586 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57587 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52588 break;
[email protected]0877e3d2009-10-17 22:29:57589 case STATE_SEND_REQUEST_COMPLETE:
590 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43591 net_log_.EndEventWithNetErrorCode(
592 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52593 break;
594 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55595 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38596 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52597 rv = DoReadHeaders();
598 break;
599 case STATE_READ_HEADERS_COMPLETE:
600 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43601 net_log_.EndEventWithNetErrorCode(
602 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52603 break;
604 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55605 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38606 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52607 rv = DoReadBody();
608 break;
609 case STATE_READ_BODY_COMPLETE:
610 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43611 net_log_.EndEventWithNetErrorCode(
612 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52613 break;
[email protected]2d2697f92009-02-18 21:00:32614 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55615 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53616 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38617 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32618 rv = DoDrainBodyForAuthRestart();
619 break;
620 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
621 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43622 net_log_.EndEventWithNetErrorCode(
623 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32624 break;
initial.commit586acc5fe2008-07-26 22:42:52625 default:
626 NOTREACHED() << "bad state";
627 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04628 break;
initial.commit586acc5fe2008-07-26 22:42:52629 }
630 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
631
632 return rv;
633}
634
[email protected]82918cc2010-08-25 17:24:50635int HttpNetworkTransaction::DoCreateStream() {
636 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]351ab642010-08-05 16:55:31637
[email protected]26816882010-10-14 18:03:09638 stream_request_.reset(
639 session_->http_stream_factory()->RequestStream(
[email protected]102e27c2011-02-23 01:01:31640 *request_,
[email protected]262eec82013-03-19 21:01:36641 priority_,
[email protected]102957f2011-09-02 17:10:14642 server_ssl_config_,
643 proxy_ssl_config_,
[email protected]26816882010-10-14 18:03:09644 this,
645 net_log_));
646 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38647 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31648}
649
[email protected]82918cc2010-08-25 17:24:50650int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
[email protected]394816e92010-08-03 07:38:59651 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50652 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38653 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33654 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
655 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29656 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
657 // Return OK and let the caller read the proxy's error page
658 next_state_ = STATE_NONE;
659 return OK;
[email protected]394816e92010-08-03 07:38:59660 }
661
[email protected]bd0b6772011-01-11 19:59:30662 // Handle possible handshake errors that may have occurred if the stream
663 // used SSL for one or more of the layers.
664 result = HandleSSLHandshakeError(result);
665
[email protected]8e6441ca2010-08-19 05:56:38666 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09667 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38668 return result;
[email protected]394816e92010-08-03 07:38:59669}
670
[email protected]82918cc2010-08-25 17:24:50671int HttpNetworkTransaction::DoInitStream() {
672 DCHECK(stream_.get());
673 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36674 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50675}
676
677int HttpNetworkTransaction::DoInitStreamComplete(int result) {
678 if (result == OK) {
679 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50680 } else {
[email protected]82918cc2010-08-25 17:24:50681 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26682 result = HandleIOError(result);
683
684 // The stream initialization failed, so this stream will never be useful.
685 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50686 }
687
688 return result;
689}
690
[email protected]044de0642010-06-17 10:42:15691int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
692 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
693 if (!ShouldApplyProxyAuth())
694 return OK;
[email protected]394816e92010-08-03 07:38:59695 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
696 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09697 auth_controllers_[target] =
698 new HttpAuthController(target,
699 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31700 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09701 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59702 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41703 io_callback_,
[email protected]394816e92010-08-03 07:38:59704 net_log_);
[email protected]044de0642010-06-17 10:42:15705}
706
707int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
708 DCHECK_NE(ERR_IO_PENDING, rv);
709 if (rv == OK)
710 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
711 return rv;
712}
713
714int HttpNetworkTransaction::DoGenerateServerAuthToken() {
715 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59716 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
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]044de0642010-06-17 10:42:15723 if (!ShouldApplyServerAuth())
724 return OK;
[email protected]394816e92010-08-03 07:38:59725 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41726 io_callback_,
[email protected]394816e92010-08-03 07:38:59727 net_log_);
[email protected]044de0642010-06-17 10:42:15728}
729
730int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
731 DCHECK_NE(ERR_IO_PENDING, rv);
732 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13733 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15734 return rv;
735}
736
[email protected]2979a492011-04-06 00:29:14737void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy) {
738 request_headers_.SetHeader(HttpRequestHeaders::kHost,
739 GetHostAndOptionalPort(request_->url));
740
741 // For compat with HTTP/1.0 servers and proxies:
742 if (using_proxy) {
743 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
744 "keep-alive");
745 } else {
746 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
747 }
748
[email protected]2979a492011-04-06 00:29:14749 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11750 if (request_->upload_data_stream) {
751 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14752 request_headers_.SetHeader(
753 HttpRequestHeaders::kTransferEncoding, "chunked");
754 } else {
755 request_headers_.SetHeader(
756 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11757 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14758 }
759 } else if (request_->method == "POST" || request_->method == "PUT" ||
760 request_->method == "HEAD") {
761 // An empty POST/PUT request still needs a content length. As for HEAD,
762 // IE and Safari also add a content length header. Presumably it is to
763 // support sending a HEAD request to an URL that only expects to be sent a
764 // POST or some other method that normally would have a message body.
765 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
766 }
767
768 // Honor load flags that impact proxy caches.
769 if (request_->load_flags & LOAD_BYPASS_CACHE) {
770 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
771 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
772 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
773 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
774 }
775
776 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
777 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
778 &request_headers_);
779 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
780 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
781 &request_headers_);
782
[email protected]c10450102011-06-27 09:06:16783 request_headers_.MergeFrom(request_->extra_headers);
[email protected]173f8e22013-04-10 04:18:20784 response_.did_use_http_auth =
785 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
786 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14787}
788
[email protected]daddea62012-09-19 05:51:13789int HttpNetworkTransaction::DoInitRequestBody() {
790 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13791 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11792 if (request_->upload_data_stream)
793 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13794 return rv;
795}
[email protected]4875ba12011-03-30 22:31:51796
[email protected]daddea62012-09-19 05:51:13797int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
798 if (result == OK)
799 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13800 return result;
801}
802
803int HttpNetworkTransaction::DoBuildRequest() {
804 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51805 headers_valid_ = false;
806
807 // This is constructed lazily (instead of within our Start method), so that
808 // we have proxy info available.
809 if (request_headers_.IsEmpty()) {
810 bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) &&
811 !is_https_request();
[email protected]2979a492011-04-06 00:29:14812 BuildRequestHeaders(using_proxy);
[email protected]4875ba12011-03-30 22:31:51813 }
814
[email protected]4875ba12011-03-30 22:31:51815 return OK;
816}
817
818int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
[email protected]9f489d72011-04-04 23:29:24819 if (result == OK)
820 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:51821 return result;
822}
823
[email protected]0877e3d2009-10-17 22:29:57824int HttpNetworkTransaction::DoSendRequest() {
[email protected]58e32bb2013-01-21 18:23:25825 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:57826 next_state_ = STATE_SEND_REQUEST_COMPLETE;
827
[email protected]bf3eb002012-11-15 05:50:11828 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52829}
830
[email protected]0877e3d2009-10-17 22:29:57831int HttpNetworkTransaction::DoSendRequestComplete(int result) {
[email protected]58e32bb2013-01-21 18:23:25832 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:52833 if (result < 0)
834 return HandleIOError(result);
[email protected]7cf7ccb2013-04-20 02:53:08835 response_.network_accessed = true;
[email protected]0877e3d2009-10-17 22:29:57836 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:52837 return OK;
838}
839
840int HttpNetworkTransaction::DoReadHeaders() {
841 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:41842 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52843}
844
[email protected]0e75a732008-10-16 20:36:09845int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() {
[email protected]90499482013-06-01 00:39:50846 if (!response_.headers.get() && !stream_->IsConnectionReused()) {
[email protected]0e75a732008-10-16 20:36:09847 // The connection was closed before any data was sent. Likely an error
848 // rather than empty HTTP/0.9 response.
[email protected]aecfbf22008-10-16 02:02:47849 return ERR_EMPTY_RESPONSE;
850 }
851
[email protected]aecfbf22008-10-16 02:02:47852 return OK;
853}
854
initial.commit586acc5fe2008-07-26 22:42:52855int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
[email protected]0b45559b2009-06-12 21:45:11856 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
857 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:38858 if (IsCertificateError(result)) {
859 // We don't handle a certificate error during SSL renegotiation, so we
860 // have to return an error that's not in the certificate error range
861 // (-2xx).
862 LOG(ERROR) << "Got a server certificate with error " << result
863 << " during SSL renegotiation";
864 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
865 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
866 // TODO(wtc): Need a test case for this code path!
867 DCHECK(stream_.get());
868 DCHECK(is_https_request());
869 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:50870 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:38871 result = HandleCertificateRequest(result);
872 if (result == OK)
873 return result;
[email protected]2181ea002009-06-09 01:37:27874 }
875
[email protected]0877e3d2009-10-17 22:29:57876 if (result < 0 && result != ERR_CONNECTION_CLOSED)
initial.commit586acc5fe2008-07-26 22:42:52877 return HandleIOError(result);
878
[email protected]0877e3d2009-10-17 22:29:57879 if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) {
[email protected]1c773ea12009-04-28 19:58:42880 ResetConnectionAndRequestForResend();
[email protected]0877e3d2009-10-17 22:29:57881 return OK;
[email protected]1c773ea12009-04-28 19:58:42882 }
[email protected]2a5c76b2008-09-25 22:15:16883
[email protected]0877e3d2009-10-17 22:29:57884 // After we call RestartWithAuth a new response_time will be recorded, and
885 // we need to be cautious about incorrectly logging the duration across the
886 // authentication activity.
[email protected]8e6441ca2010-08-19 05:56:38887 if (result == OK)
888 LogTransactionConnectedMetrics();
initial.commit586acc5fe2008-07-26 22:42:52889
[email protected]0877e3d2009-10-17 22:29:57890 if (result == ERR_CONNECTION_CLOSED) {
[email protected]02c92c492010-03-08 21:28:14891 // For now, if we get at least some data, we do the best we can to make
[email protected]9492e4a2010-02-24 00:58:46892 // sense of it and send it back up the stack.
[email protected]0e75a732008-10-16 20:36:09893 int rv = HandleConnectionClosedBeforeEndOfHeaders();
[email protected]aecfbf22008-10-16 02:02:47894 if (rv != OK)
895 return rv;
[email protected]0877e3d2009-10-17 22:29:57896 }
[email protected]90499482013-06-01 00:39:50897 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:52898
[email protected]14b7e9a2012-10-16 19:51:29899 // Server-induced fallback is supported only if this is a PAC configured
900 // proxy. See: http://crbug.com/143712
[email protected]2694ceea2013-05-15 04:32:00901 if (response_.was_fetched_via_proxy && proxy_info_.did_use_pac_script() &&
[email protected]90499482013-06-01 00:39:50902 response_.headers.get() != NULL) {
[email protected]2694ceea2013-05-15 04:32:00903 bool should_fallback =
904 response_.headers->HasHeaderValue("connection", "proxy-bypass");
905 // Additionally, fallback if a 500 is returned via the data reduction proxy.
906 // This is conservative, as the 500 might have been generated by the origin,
907 // and not the proxy.
908#if defined(SPDY_PROXY_AUTH_ORIGIN)
909 if (!should_fallback) {
910 should_fallback =
911 response_.headers->response_code() == HTTP_INTERNAL_SERVER_ERROR &&
912 proxy_info_.proxy_server().host_port_pair().Equals(
913 HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)));
914 }
915#endif
916 if (should_fallback) {
[email protected]14b7e9a2012-10-16 19:51:29917 ProxyService* proxy_service = session_->proxy_service();
918 if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) {
919 // Only retry in the case of GETs. We don't want to resubmit a POST
920 // if the proxy took some action.
921 if (request_->method == "GET") {
922 ResetConnectionAndRequestForResend();
923 return OK;
924 }
925 }
926 }
927 }
928
[email protected]93f8b562012-03-27 01:00:16929 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
930 if (request_->load_flags & LOAD_MAIN_FRAME) {
931 const int response_code = response_.headers->response_code();
932 UMA_HISTOGRAM_ENUMERATION(
933 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
934 }
935
[email protected]3abacd62012-06-10 20:20:32936 net_log_.AddEvent(
937 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
938 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:39939
[email protected]a7e41312009-12-16 23:18:14940 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:57941 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
942 // indicates a buggy server. See:
943 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
944 if (request_->method == "PUT")
945 return ERR_METHOD_NOT_SUPPORTED;
946 }
[email protected]4ddaf2502008-10-23 18:26:19947
[email protected]0877e3d2009-10-17 22:29:57948 // Check for an intermediate 100 Continue response. An origin server is
949 // allowed to send this response even if we didn't ask for it, so we just
950 // need to skip over it.
951 // We treat any other 1xx in this same way (although in practice getting
952 // a 1xx that isn't a 100 is rare).
[email protected]a7e41312009-12-16 23:18:14953 if (response_.headers->response_code() / 100 == 1) {
[email protected]007b3f82013-04-09 08:46:45954 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:57955 next_state_ = STATE_READ_HEADERS;
956 return OK;
957 }
958
[email protected]8e6441ca2010-08-19 05:56:38959 HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(),
960 request_->url.EffectiveIntPort());
961 ProcessAlternateProtocol(session_->http_stream_factory(),
[email protected]17291a022011-10-10 07:32:53962 session_->http_server_properties(),
[email protected]90499482013-06-01 00:39:50963 *response_.headers.get(),
[email protected]8e6441ca2010-08-19 05:56:38964 endpoint);
[email protected]564b4912010-03-09 16:30:42965
[email protected]e772db3f2010-07-12 18:11:13966 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:57967 if (rv != OK)
968 return rv;
969
[email protected]8536ef52010-09-30 16:18:21970 if (is_https_request())
971 stream_->GetSSLInfo(&response_.ssl_info);
972
[email protected]0877e3d2009-10-17 22:29:57973 headers_valid_ = true;
974 return OK;
initial.commit586acc5fe2008-07-26 22:42:52975}
976
977int HttpNetworkTransaction::DoReadBody() {
[email protected]90499482013-06-01 00:39:50978 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:13979 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:38980 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:52981
982 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:50983 return stream_->ReadResponseBody(
984 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:52985}
986
987int HttpNetworkTransaction::DoReadBodyComplete(int result) {
988 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:38989 bool done = false;
990 if (result <= 0) {
991 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:52992 done = true;
[email protected]8e6441ca2010-08-19 05:56:38993 }
[email protected]9492e4a2010-02-24 00:58:46994
[email protected]8e6441ca2010-08-19 05:56:38995 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:31996 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:38997 // Note: Just because IsResponseBodyComplete is true, we're not
998 // necessarily "done". We're only "done" when it is the last
999 // read on this HttpNetworkTransaction, which will be signified
1000 // by a zero-length read.
1001 // TODO(mbelshe): The keepalive property is really a property of
1002 // the stream. No need to compute it here just to pass back
1003 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:471004 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1005 // ResponseHeaders.
1006 if (stream_->CanFindEndOfResponse()) {
1007 HttpResponseHeaders* headers = GetResponseHeaders();
1008 if (headers)
1009 keep_alive = headers->IsKeepAlive();
1010 }
initial.commit586acc5fe2008-07-26 22:42:521011 }
1012
[email protected]8e6441ca2010-08-19 05:56:381013 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521014 if (done) {
[email protected]56300172008-11-06 18:42:551015 LogTransactionMetrics();
[email protected]8e6441ca2010-08-19 05:56:381016 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011017 // Note: we don't reset the stream here. We've closed it, but we still
1018 // need it around so that callers can call methods such as
1019 // GetUploadProgress() and have them be meaningful.
1020 // TODO(mbelshe): This means we closed the stream here, and we close it
1021 // again in ~HttpNetworkTransaction. Clean that up.
1022
[email protected]8e6441ca2010-08-19 05:56:381023 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521024 }
1025
1026 // Clear these to avoid leaving around old state.
1027 read_buf_ = NULL;
1028 read_buf_len_ = 0;
1029
1030 return result;
1031}
1032
[email protected]2d2697f92009-02-18 21:00:321033int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1034 // This method differs from DoReadBody only in the next_state_. So we just
1035 // call DoReadBody and override the next_state_. Perhaps there is a more
1036 // elegant way for these two methods to share code.
1037 int rv = DoReadBody();
1038 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1039 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1040 return rv;
1041}
1042
[email protected]0877e3d2009-10-17 22:29:571043// TODO(wtc): This method and the DoReadBodyComplete method are almost
1044// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321045int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
[email protected]68873ba2009-06-04 21:49:231046 // keep_alive defaults to true because the very reason we're draining the
1047 // response body is to reuse the connection for auth restart.
1048 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321049 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571050 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321051 done = true;
[email protected]68873ba2009-06-04 21:49:231052 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311053 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571054 done = true;
[email protected]2d2697f92009-02-18 21:00:321055 }
1056
1057 if (done) {
1058 DidDrainBodyForAuthRestart(keep_alive);
1059 } else {
1060 // Keep draining.
1061 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1062 }
1063
1064 return OK;
1065}
1066
[email protected]8e3d2d32010-06-13 18:46:231067void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1068 if (logged_response_time_)
1069 return;
1070
1071 logged_response_time_ = true;
1072
[email protected]a7e41312009-12-16 23:18:141073 base::TimeDelta total_duration = response_.response_time - start_time_;
[email protected]9a0a55f2009-04-13 23:23:031074
[email protected]d6b55392011-08-05 04:04:351075 UMA_HISTOGRAM_CUSTOM_TIMES(
1076 "Net.Transaction_Connected",
[email protected]510e854f2009-04-20 18:39:081077 total_duration,
[email protected]9a0a55f2009-04-13 23:23:031078 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1079 100);
[email protected]1fa47592009-07-27 22:45:001080
[email protected]c38ec5a2010-08-25 23:32:381081 bool reused_socket = stream_->IsConnectionReused();
1082 if (!reused_socket) {
[email protected]d6b55392011-08-05 04:04:351083 UMA_HISTOGRAM_CUSTOM_TIMES(
1084 "Net.Transaction_Connected_New_b",
[email protected]b01998a2009-04-21 01:01:111085 total_duration,
1086 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1087 100);
[email protected]0310d432009-08-25 07:49:521088 }
1089
[email protected]31ae7ab2012-04-24 21:09:051090 // Currently, non-HIGHEST priority requests are frame or sub-frame resource
[email protected]510e854f2009-04-20 18:39:081091 // types. This will change when we also prioritize certain subresources like
1092 // css, js, etc.
[email protected]262eec82013-03-19 21:01:361093 if (priority_ != HIGHEST) {
[email protected]d6b55392011-08-05 04:04:351094 UMA_HISTOGRAM_CUSTOM_TIMES(
1095 "Net.Priority_High_Latency_b",
[email protected]510e854f2009-04-20 18:39:081096 total_duration,
1097 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1098 100);
1099 } else {
[email protected]d6b55392011-08-05 04:04:351100 UMA_HISTOGRAM_CUSTOM_TIMES(
1101 "Net.Priority_Low_Latency_b",
[email protected]510e854f2009-04-20 18:39:081102 total_duration,
1103 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1104 100);
1105 }
[email protected]9a0a55f2009-04-13 23:23:031106}
1107
[email protected]56300172008-11-06 18:42:551108void HttpNetworkTransaction::LogTransactionMetrics() const {
[email protected]0877e3d2009-10-17 22:29:571109 base::TimeDelta duration = base::Time::Now() -
[email protected]2227c692010-05-04 15:36:111110 response_.request_time;
[email protected]56300172008-11-06 18:42:551111 if (60 < duration.InMinutes())
1112 return;
[email protected]0b48db42009-03-23 02:45:111113
[email protected]21b316a2009-03-23 18:25:061114 base::TimeDelta total_duration = base::Time::Now() - start_time_;
1115
[email protected]d6b55392011-08-05 04:04:351116 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration,
1117 base::TimeDelta::FromMilliseconds(1),
1118 base::TimeDelta::FromMinutes(10),
1119 100);
1120 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1121 total_duration,
1122 base::TimeDelta::FromMilliseconds(1),
1123 base::TimeDelta::FromMinutes(10), 100);
[email protected]5c68d692011-08-24 04:59:411124
[email protected]c38ec5a2010-08-25 23:32:381125 if (!stream_->IsConnectionReused()) {
[email protected]d6b55392011-08-05 04:04:351126 UMA_HISTOGRAM_CUSTOM_TIMES(
1127 "Net.Transaction_Latency_Total_New_Connection",
[email protected]808f6402009-03-30 20:02:071128 total_duration, base::TimeDelta::FromMilliseconds(1),
1129 base::TimeDelta::FromMinutes(10), 100);
1130 }
[email protected]56300172008-11-06 18:42:551131}
1132
[email protected]5e363962009-06-19 19:57:011133int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381134 // There are two paths through which the server can request a certificate
1135 // from us. The first is during the initial handshake, the second is
1136 // during SSL renegotiation.
1137 //
1138 // In both cases, we want to close the connection before proceeding.
1139 // We do this for two reasons:
1140 // First, we don't want to keep the connection to the server hung for a
1141 // long time while the user selects a certificate.
1142 // Second, even if we did keep the connection open, NSS has a bug where
1143 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581144 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381145
1146 if (stream_.get()) {
1147 // Since we already have a stream, we're being called as part of SSL
1148 // renegotiation.
1149 DCHECK(!stream_request_.get());
1150 stream_->Close(true);
1151 stream_.reset();
1152 }
1153
[email protected]26816882010-10-14 18:03:091154 // The server is asking for a client certificate during the initial
1155 // handshake.
1156 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011157
[email protected]ec229bc92010-11-22 09:51:451158 // If the user selected one of the certificates in client_certs or declined
1159 // to provide one for this server before, use the past decision
1160 // automatically.
1161 scoped_refptr<X509Certificate> client_cert;
1162 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1163 response_.cert_request_info->host_and_port, &client_cert);
1164 if (!found_cached_cert)
1165 return error;
1166
1167 // Check that the certificate selected is still a certificate the server
1168 // is likely to accept, based on the criteria supplied in the
1169 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501170 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261171 const std::vector<std::string>& cert_authorities =
1172 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451173
[email protected]f1958c382013-02-07 00:15:261174 bool cert_still_valid = cert_authorities.empty() ||
1175 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451176 if (!cert_still_valid)
1177 return error;
[email protected]5e363962009-06-19 19:57:011178 }
[email protected]ec229bc92010-11-22 09:51:451179
1180 // TODO(davidben): Add a unit test which covers this path; we need to be
1181 // able to send a legitimate certificate and also bypass/clear the
1182 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141183 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1184 &proxy_ssl_config_ : &server_ssl_config_;
1185 ssl_config->send_client_cert = true;
1186 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451187 next_state_ = STATE_CREATE_STREAM;
1188 // Reset the other member variables.
1189 // Note: this is necessary only with SSL renegotiation.
1190 ResetStateForRestart();
1191 return OK;
[email protected]0b45559b2009-06-12 21:45:111192}
1193
[email protected]bd0b6772011-01-11 19:59:301194// TODO(rch): This does not correctly handle errors when an SSL proxy is
1195// being used, as all of the errors are handled as if they were generated
1196// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381197// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301198int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1199 DCHECK(request_);
[email protected]102957f2011-09-02 17:10:141200 if (server_ssl_config_.send_client_cert &&
[email protected]a1cb2cd2011-02-05 02:02:081201 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
[email protected]bd0b6772011-01-11 19:59:301202 session_->ssl_client_auth_cache()->Remove(
1203 GetHostAndPort(request_->url));
1204 }
1205
[email protected]158ac972013-04-19 23:29:231206 uint16 version_max = server_ssl_config_.version_max;
1207
initial.commit586acc5fe2008-07-26 22:42:521208 switch (error) {
[email protected]bd0b6772011-01-11 19:59:301209 case ERR_SSL_PROTOCOL_ERROR:
1210 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231211 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1212 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171213 // This could be a TLS-intolerant server or a server that chose a
1214 // cipher suite defined only for higher protocol versions (such as
1215 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1216 // back to the next lower version and retry.
1217 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1218 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1219 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1220 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1221 // version_max should match the maximum protocol version supported
1222 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231223 version_max--;
1224
1225 // Fallback to the lower SSL version.
1226 // While SSL 3.0 fallback should be eliminated because of security
1227 // reasons, there is a high risk of breaking the servers if this is
1228 // done in general.
1229 // For now SSL 3.0 fallback is disabled for Google servers first,
1230 // and will be expanded to other servers after enough experiences
1231 // have been gained showing that this experiment works well with
1232 // today's Internet.
1233 if (version_max > SSL_PROTOCOL_VERSION_SSL3 ||
1234 (server_ssl_config_.unrestricted_ssl3_fallback_enabled ||
1235 !TransportSecurityState::IsGooglePinnedProperty(
1236 request_->url.host(), true /* include SNI */))) {
1237 net_log_.AddEvent(
1238 NetLog::TYPE_SSL_VERSION_FALLBACK,
1239 base::Bind(&NetLogSSLVersionFallbackCallback,
1240 &request_->url, error, server_ssl_config_.version_max,
1241 version_max));
1242 server_ssl_config_.version_max = version_max;
1243 server_ssl_config_.version_fallback = true;
1244 ResetConnectionAndRequestForResend();
1245 error = OK;
1246 }
[email protected]1c773ea12009-04-28 19:58:421247 }
initial.commit586acc5fe2008-07-26 22:42:521248 break;
1249 }
[email protected]158ac972013-04-19 23:29:231250
initial.commit586acc5fe2008-07-26 22:42:521251 return error;
1252}
1253
[email protected]bd0b6772011-01-11 19:59:301254// This method determines whether it is safe to resend the request after an
1255// IO error. It can only be called in response to request header or body
1256// write errors or response header read errors. It should not be used in
1257// other cases, such as a Connect error.
1258int HttpNetworkTransaction::HandleIOError(int error) {
1259 // SSL errors may happen at any time during the stream and indicate issues
1260 // with the underlying connection. Because the peer may request
1261 // renegotiation at any time, check and handle any possible SSL handshake
[email protected]80c75f682012-05-26 16:22:171262 // related errors. In addition to renegotiation, TLS False Start may cause
1263 // SSL handshake errors (specifically servers with buggy DEFLATE support)
1264 // to be delayed until the first Read on the underlying connection.
[email protected]bd0b6772011-01-11 19:59:301265 error = HandleSSLHandshakeError(error);
1266
1267 switch (error) {
1268 // If we try to reuse a connection that the server is in the process of
1269 // closing, we may end up successfully writing out our request (or a
1270 // portion of our request) only to find a connection error when we try to
1271 // read from (or finish writing to) the socket.
1272 case ERR_CONNECTION_RESET:
1273 case ERR_CONNECTION_CLOSED:
1274 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511275 // There can be a race between the socket pool checking checking whether a
1276 // socket is still connected, receiving the FIN, and sending/reading data
1277 // on a reused socket. If we receive the FIN between the connectedness
1278 // check and writing/reading from the socket, we may first learn the socket
1279 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1280 // likely happen when trying to retrieve its IP address.
1281 // See http://crbug.com/105824 for more details.
1282 case ERR_SOCKET_NOT_CONNECTED:
[email protected]bd0b6772011-01-11 19:59:301283 if (ShouldResendRequest(error)) {
[email protected]b6754252012-06-13 23:14:381284 net_log_.AddEventWithNetErrorCode(
1285 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301286 ResetConnectionAndRequestForResend();
1287 error = OK;
1288 }
1289 break;
[email protected]5a60c8b2011-10-19 20:14:291290 case ERR_PIPELINE_EVICTION:
[email protected]5477d892012-03-01 21:31:311291 if (!session_->force_http_pipelining()) {
[email protected]b6754252012-06-13 23:14:381292 net_log_.AddEventWithNetErrorCode(
1293 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]5477d892012-03-01 21:31:311294 ResetConnectionAndRequestForResend();
1295 error = OK;
1296 }
1297 break;
[email protected]8753a122011-10-16 08:05:081298 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001299 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]b6754252012-06-13 23:14:381300 net_log_.AddEventWithNetErrorCode(
1301 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001302 ResetConnectionAndRequestForResend();
1303 error = OK;
1304 break;
[email protected]bd0b6772011-01-11 19:59:301305 }
1306 return error;
1307}
1308
[email protected]c3b35c22008-09-27 03:19:421309void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581310 ResetStateForAuthRestart();
1311 stream_.reset();
1312}
1313
1314void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251315 send_start_time_ = base::TimeTicks();
1316 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251317
[email protected]0757e7702009-03-27 04:00:221318 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421319 read_buf_ = NULL;
1320 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571321 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201322 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141323 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381324 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571325}
1326
1327HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501328 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421329}
1330
[email protected]a19f1c602009-08-24 21:35:281331bool HttpNetworkTransaction::ShouldResendRequest(int error) const {
[email protected]8e6441ca2010-08-19 05:56:381332 bool connection_is_proven = stream_->IsConnectionReused();
1333 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161334
[email protected]2a5c76b2008-09-25 22:15:161335 // NOTE: we resend a request only if we reused a keep-alive connection.
1336 // This automatically prevents an infinite resend loop because we'll run
1337 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381338 if (connection_is_proven && !has_received_headers)
1339 return true;
1340 return false;
[email protected]1c773ea12009-04-28 19:58:421341}
1342
1343void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381344 if (stream_.get()) {
1345 stream_->Close(true);
1346 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161347 }
1348
[email protected]0877e3d2009-10-17 22:29:571349 // We need to clear request_headers_ because it contains the real request
1350 // headers, but we may need to resend the CONNECT request first to recreate
1351 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201352 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501353 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541354}
1355
[email protected]1c773ea12009-04-28 19:58:421356bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
[email protected]2df19bb2010-08-25 20:13:461357 return !is_https_request() &&
1358 (proxy_info_.is_https() || proxy_info_.is_http());
[email protected]1c773ea12009-04-28 19:58:421359}
license.botbf09a502008-08-24 00:55:551360
[email protected]1c773ea12009-04-28 19:58:421361bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041362 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421363}
1364
[email protected]e772db3f2010-07-12 18:11:131365int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271366 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501367 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421368
[email protected]0877e3d2009-10-17 22:29:571369 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581370 if (status != HTTP_UNAUTHORIZED &&
1371 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421372 return OK;
[email protected]9094b602012-02-27 21:44:581373 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111374 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161375 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1376 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421377
[email protected]9094b602012-02-27 21:44:581378 // This case can trigger when an HTTPS server responds with a "Proxy
1379 // authentication required" status code through a non-authenticating
1380 // proxy.
[email protected]7a67a8152010-11-05 18:31:101381 if (!auth_controllers_[target].get())
1382 return ERR_UNEXPECTED_PROXY_AUTH;
1383
[email protected]a7ea8832010-07-12 17:54:541384 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311385 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1386 net_log_);
[email protected]228404f2010-06-24 04:31:411387 if (auth_controllers_[target]->HaveAuthHandler())
1388 pending_auth_target_ = target;
1389
1390 scoped_refptr<AuthChallengeInfo> auth_info =
1391 auth_controllers_[target]->auth_info();
1392 if (auth_info.get())
1393 response_.auth_challenge = auth_info;
1394
[email protected]228404f2010-06-24 04:31:411395 return rv;
[email protected]f9ee6b52008-11-08 06:46:231396}
1397
[email protected]8e6441ca2010-08-19 05:56:381398bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1399 return auth_controllers_[target].get() &&
1400 auth_controllers_[target]->HaveAuth();
1401}
1402
[email protected]228404f2010-06-24 04:31:411403GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1404 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461405 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411406 if (!proxy_info_.proxy_server().is_valid() ||
1407 proxy_info_.proxy_server().is_direct()) {
1408 return GURL(); // There is no proxy server.
1409 }
[email protected]2df19bb2010-08-25 20:13:461410 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1411 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351412 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461413 }
[email protected]228404f2010-06-24 04:31:411414 case HttpAuth::AUTH_SERVER:
1415 return request_->url;
1416 default:
1417 return GURL();
1418 }
[email protected]c3b35c22008-09-27 03:19:421419}
1420
[email protected]d8eb84242010-09-25 02:25:061421#define STATE_CASE(s) \
1422 case s: \
1423 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1424 break
[email protected]aef04272010-06-28 18:03:041425
1426std::string HttpNetworkTransaction::DescribeState(State state) {
1427 std::string description;
1428 switch (state) {
[email protected]82918cc2010-08-25 17:24:501429 STATE_CASE(STATE_CREATE_STREAM);
1430 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131431 STATE_CASE(STATE_INIT_REQUEST_BODY);
1432 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511433 STATE_CASE(STATE_BUILD_REQUEST);
1434 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041435 STATE_CASE(STATE_SEND_REQUEST);
1436 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1437 STATE_CASE(STATE_READ_HEADERS);
1438 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041439 STATE_CASE(STATE_READ_BODY);
1440 STATE_CASE(STATE_READ_BODY_COMPLETE);
1441 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1442 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041443 STATE_CASE(STATE_NONE);
1444 default:
[email protected]d8eb84242010-09-25 02:25:061445 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1446 state);
[email protected]aef04272010-06-28 18:03:041447 break;
1448 }
1449 return description;
1450}
1451
1452#undef STATE_CASE
1453
[email protected]c3b35c22008-09-27 03:19:421454} // namespace net