blob: b5e5d5c1a24efb6ea8b784f93c53f49c65fae882 [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"
vadimt09e7ebe2014-10-29 22:10:4117#include "base/profiler/scoped_tracker.h"
[email protected]7286e3fc2011-07-19 22:13:2418#include "base/stl_util.h"
[email protected]125ef482013-06-11 18:32:4719#include "base/strings/string_number_conversions.h"
20#include "base/strings/string_util.h"
21#include "base/strings/stringprintf.h"
[email protected]3d498f72013-10-28 21:17:4022#include "base/time/time.h"
[email protected]db74b0102012-05-31 19:55:5323#include "base/values.h"
[email protected]68bf9152008-09-25 19:47:3024#include "build/build_config.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"
yhiranoa7e05bb2014-11-06 05:40:3947#include "net/http/http_stream.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"
[email protected]f89276a72013-07-12 06:41:5462#include "url/gurl.h"
[email protected]e69c1cd2014-07-29 07:42:2963#include "url/url_canon.h"
initial.commit586acc5fe2008-07-26 22:42:5264
initial.commit586acc5fe2008-07-26 22:42:5265namespace net {
66
[email protected]1c773ea12009-04-28 19:58:4267namespace {
68
[email protected]30d4c022013-07-18 22:58:1669void ProcessAlternateProtocol(
[email protected]d7599122014-05-24 03:37:2370 HttpNetworkSession* session,
[email protected]30d4c022013-07-18 22:58:1671 const HttpResponseHeaders& headers,
72 const HostPortPair& http_host_port_pair) {
[email protected]287d9412014-07-08 23:01:0073 if (!headers.HasHeader(kAlternateProtocolHeader))
[email protected]ff4a6492014-07-08 19:49:3874 return;
[email protected]287d9412014-07-08 23:01:0075
76 std::vector<std::string> alternate_protocol_values;
77 void* iter = NULL;
78 std::string alternate_protocol_str;
79 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
80 &alternate_protocol_str)) {
rch89c6e102015-03-18 18:56:5281 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
82 &alternate_protocol_str);
83 if (!alternate_protocol_str.empty()) {
84 alternate_protocol_values.push_back(alternate_protocol_str);
85 }
[email protected]564b4912010-03-09 16:30:4286 }
87
[email protected]d7599122014-05-24 03:37:2388 session->http_stream_factory()->ProcessAlternateProtocol(
89 session->http_server_properties(),
[email protected]287d9412014-07-08 23:01:0090 alternate_protocol_values,
[email protected]d7599122014-05-24 03:37:2391 http_host_port_pair,
92 *session);
[email protected]f45c1ee2010-08-03 00:54:3093}
94
[email protected]ea5ef4c2013-06-13 22:50:2795base::Value* NetLogSSLVersionFallbackCallback(
96 const GURL* url,
97 int net_error,
98 uint16 version_before,
99 uint16 version_after,
100 NetLog::LogLevel /* log_level */) {
101 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]b6754252012-06-13 23:14:38102 dict->SetString("host_and_port", GetHostAndPort(*url));
103 dict->SetInteger("net_error", net_error);
104 dict->SetInteger("version_before", version_before);
105 dict->SetInteger("version_after", version_after);
106 return dict;
107}
[email protected]db74b0102012-05-31 19:55:53108
davidbena4c9d062015-04-03 22:34:25109base::Value* NetLogSSLCipherFallbackCallback(const GURL* url,
110 int net_error,
111 NetLog::LogLevel /* log_level */) {
112 base::DictionaryValue* dict = new base::DictionaryValue();
113 dict->SetString("host_and_port", GetHostAndPort(*url));
114 dict->SetInteger("net_error", net_error);
115 return dict;
116}
117
[email protected]1c773ea12009-04-28 19:58:42118} // namespace
119
initial.commit586acc5fe2008-07-26 22:42:52120//-----------------------------------------------------------------------------
121
[email protected]262eec82013-03-19 21:01:36122HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
123 HttpNetworkSession* session)
[email protected]0757e7702009-03-27 04:00:22124 : pending_auth_target_(HttpAuth::AUTH_NONE),
[email protected]aa249b52013-04-30 01:04:32125 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
126 base::Unretained(this))),
initial.commit586acc5fe2008-07-26 22:42:52127 session_(session),
128 request_(NULL),
[email protected]262eec82013-03-19 21:01:36129 priority_(priority),
[email protected]0877e3d2009-10-17 22:29:57130 headers_valid_(false),
[email protected]a53e4d12013-12-07 16:37:24131 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
[email protected]b94f92d2010-10-27 16:45:20132 request_headers_(),
initial.commit586acc5fe2008-07-26 22:42:52133 read_buf_len_(0),
[email protected]b8015c42013-12-24 15:18:19134 total_received_bytes_(0),
[email protected]a7ea8832010-07-12 17:54:54135 next_state_(STATE_NONE),
[email protected]831e4a32013-11-14 02:14:44136 establishing_tunnel_(false),
137 websocket_handshake_stream_base_create_helper_(NULL) {
[email protected]102957f2011-09-02 17:10:14138 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
[email protected]d7599122014-05-24 03:37:23139 session->GetNextProtos(&server_ssl_config_.next_protos);
[email protected]99ffa5a2011-10-06 04:20:19140 proxy_ssl_config_ = server_ssl_config_;
[email protected]3ce7df0f2010-03-03 00:30:50141}
142
[email protected]0b0bf032010-09-21 18:08:50143HttpNetworkTransaction::~HttpNetworkTransaction() {
144 if (stream_.get()) {
145 HttpResponseHeaders* headers = GetResponseHeaders();
146 // TODO(mbelshe): The stream_ should be able to compute whether or not the
147 // stream should be kept alive. No reason to compute here
148 // and pass it in.
149 bool try_to_keep_alive =
150 next_state_ == STATE_NONE &&
151 stream_->CanFindEndOfResponse() &&
152 (!headers || headers->IsKeepAlive());
153 if (!try_to_keep_alive) {
154 stream_->Close(true /* not reusable */);
155 } else {
156 if (stream_->IsResponseBodyComplete()) {
157 // If the response body is complete, we can just reuse the socket.
158 stream_->Close(false /* reusable */);
[email protected]3a8d6852011-03-11 23:43:44159 } else if (stream_->IsSpdyHttpStream()) {
160 // Doesn't really matter for SpdyHttpStream. Just close it.
161 stream_->Close(true /* not reusable */);
[email protected]0b0bf032010-09-21 18:08:50162 } else {
163 // Otherwise, we try to drain the response body.
yhiranoa7e05bb2014-11-06 05:40:39164 HttpStream* stream = stream_.release();
[email protected]3fe8d2f82013-10-17 08:56:07165 stream->Drain(session_);
[email protected]0b0bf032010-09-21 18:08:50166 }
167 }
168 }
[email protected]02cad5d2013-10-02 08:14:03169
170 if (request_ && request_->upload_data_stream)
171 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
[email protected]0b0bf032010-09-21 18:08:50172}
173
[email protected]684970b2009-08-14 04:54:46174int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
[email protected]49639fa2011-12-20 23:22:41175 const CompletionCallback& callback,
[email protected]9e743cd2010-03-16 07:03:53176 const BoundNetLog& net_log) {
[email protected]9e743cd2010-03-16 07:03:53177 net_log_ = net_log;
[email protected]96d570e42008-08-05 22:43:04178 request_ = request_info;
179
[email protected]99ffa5a2011-10-06 04:20:19180 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
[email protected]102957f2011-09-02 17:10:14181 server_ssl_config_.rev_checking_enabled = false;
[email protected]99ffa5a2011-10-06 04:20:19182 proxy_ssl_config_.rev_checking_enabled = false;
183 }
[email protected]6fbac162011-06-20 00:29:04184
jkarlinfb1d5172015-01-12 14:10:29185 if (request_->load_flags & LOAD_PREFETCH)
186 response_.unused_since_prefetch = true;
187
[email protected]1ea4f46a2014-04-22 22:33:56188 // Channel ID is disabled if privacy mode is enabled for this request.
[email protected]66eeb52e2014-05-22 06:53:49189 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
190 server_ssl_config_.channel_id_enabled = false;
[email protected]e6d017652013-05-17 18:01:40191
jeremyim8d44fadd2015-02-10 19:18:15192 if (server_ssl_config_.fastradio_padding_enabled) {
193 server_ssl_config_.fastradio_padding_eligible =
194 session_->ssl_config_service()->SupportsFastradioPadding(
195 request_info->url);
196 }
197
[email protected]1826a402014-01-08 15:40:48198 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
[email protected]96d570e42008-08-05 22:43:04199 int rv = DoLoop(OK);
200 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41201 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04202 return rv;
203}
204
205int HttpNetworkTransaction::RestartIgnoringLastError(
[email protected]49639fa2011-12-20 23:22:41206 const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38207 DCHECK(!stream_.get());
208 DCHECK(!stream_request_.get());
209 DCHECK_EQ(STATE_NONE, next_state_);
210
[email protected]82918cc2010-08-25 17:24:50211 next_state_ = STATE_CREATE_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38212
[email protected]ccb40e52008-09-17 20:54:40213 int rv = DoLoop(OK);
214 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41215 callback_ = callback;
[email protected]aaead502008-10-15 00:20:11216 return rv;
[email protected]96d570e42008-08-05 22:43:04217}
218
[email protected]0b45559b2009-06-12 21:45:11219int HttpNetworkTransaction::RestartWithCertificate(
[email protected]49639fa2011-12-20 23:22:41220 X509Certificate* client_cert, const CompletionCallback& callback) {
[email protected]8e6441ca2010-08-19 05:56:38221 // In HandleCertificateRequest(), we always tear down existing stream
222 // requests to force a new connection. So we shouldn't have one here.
223 DCHECK(!stream_request_.get());
224 DCHECK(!stream_.get());
225 DCHECK_EQ(STATE_NONE, next_state_);
226
[email protected]102957f2011-09-02 17:10:14227 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
228 &proxy_ssl_config_ : &server_ssl_config_;
229 ssl_config->send_client_cert = true;
230 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:45231 session_->ssl_client_auth_cache()->Add(
232 response_.cert_request_info->host_and_port, client_cert);
[email protected]0b45559b2009-06-12 21:45:11233 // Reset the other member variables.
234 // Note: this is necessary only with SSL renegotiation.
235 ResetStateForRestart();
[email protected]82918cc2010-08-25 17:24:50236 next_state_ = STATE_CREATE_STREAM;
[email protected]0b45559b2009-06-12 21:45:11237 int rv = DoLoop(OK);
238 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41239 callback_ = callback;
[email protected]0b45559b2009-06-12 21:45:11240 return rv;
241}
242
[email protected]49639fa2011-12-20 23:22:41243int HttpNetworkTransaction::RestartWithAuth(
244 const AuthCredentials& credentials, const CompletionCallback& callback) {
[email protected]0757e7702009-03-27 04:00:22245 HttpAuth::Target target = pending_auth_target_;
246 if (target == HttpAuth::AUTH_NONE) {
247 NOTREACHED();
248 return ERR_UNEXPECTED;
249 }
[email protected]0757e7702009-03-27 04:00:22250 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:42251
[email protected]f3cf9802011-10-28 18:44:58252 auth_controllers_[target]->ResetAuth(credentials);
[email protected]e772db3f2010-07-12 18:11:13253
[email protected]49639fa2011-12-20 23:22:41254 DCHECK(callback_.is_null());
[email protected]8e6441ca2010-08-19 05:56:38255
256 int rv = OK;
257 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
258 // In this case, we've gathered credentials for use with proxy
259 // authentication of a tunnel.
[email protected]82918cc2010-08-25 17:24:50260 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38261 DCHECK(stream_request_ != NULL);
[email protected]394816e92010-08-03 07:38:59262 auth_controllers_[target] = NULL;
[email protected]a7ea8832010-07-12 17:54:54263 ResetStateForRestart();
[email protected]f3cf9802011-10-28 18:44:58264 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
[email protected]a7ea8832010-07-12 17:54:54265 } else {
[email protected]8e6441ca2010-08-19 05:56:38266 // In this case, we've gathered credentials for the server or the proxy
267 // but it is not during the tunneling phase.
268 DCHECK(stream_request_ == NULL);
[email protected]a7ea8832010-07-12 17:54:54269 PrepareForAuthRestart(target);
[email protected]8e6441ca2010-08-19 05:56:38270 rv = DoLoop(OK);
[email protected]a7ea8832010-07-12 17:54:54271 }
[email protected]c3b35c22008-09-27 03:19:42272
[email protected]c3b35c22008-09-27 03:19:42273 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41274 callback_ = callback;
[email protected]c3b35c22008-09-27 03:19:42275 return rv;
[email protected]96d570e42008-08-05 22:43:04276}
277
[email protected]f9ee6b52008-11-08 06:46:23278void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
279 DCHECK(HaveAuth(target));
[email protected]8e6441ca2010-08-19 05:56:38280 DCHECK(!stream_request_.get());
281
[email protected]2d2697f92009-02-18 21:00:32282 bool keep_alive = false;
[email protected]0877e3d2009-10-17 22:29:57283 // Even if the server says the connection is keep-alive, we have to be
284 // able to find the end of each response in order to reuse the connection.
285 if (GetResponseHeaders()->IsKeepAlive() &&
[email protected]351ab642010-08-05 16:55:31286 stream_->CanFindEndOfResponse()) {
[email protected]0877e3d2009-10-17 22:29:57287 // If the response body hasn't been completely read, we need to drain
288 // it first.
[email protected]351ab642010-08-05 16:55:31289 if (!stream_->IsResponseBodyComplete()) {
[email protected]2d2697f92009-02-18 21:00:32290 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
[email protected]0877e3d2009-10-17 22:29:57291 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
[email protected]2d2697f92009-02-18 21:00:32292 read_buf_len_ = kDrainBodyBufferSize;
293 return;
294 }
[email protected]0877e3d2009-10-17 22:29:57295 keep_alive = true;
[email protected]37832c6d2009-06-05 19:44:09296 }
297
[email protected]2d2697f92009-02-18 21:00:32298 // We don't need to drain the response body, so we act as if we had drained
299 // the response body.
300 DidDrainBodyForAuthRestart(keep_alive);
301}
302
303void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
[email protected]8e6441ca2010-08-19 05:56:38304 DCHECK(!stream_request_.get());
305
306 if (stream_.get()) {
[email protected]b8015c42013-12-24 15:18:19307 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:58308 HttpStream* new_stream = NULL;
[email protected]2d0a4f92011-05-05 16:38:46309 if (keep_alive && stream_->IsConnectionReusable()) {
[email protected]8e6441ca2010-08-19 05:56:38310 // We should call connection_->set_idle_time(), but this doesn't occur
311 // often enough to be worth the trouble.
312 stream_->SetConnectionReused();
yhiranoa7e05bb2014-11-06 05:40:39313 new_stream = stream_->RenewStreamForAuth();
[email protected]8e6441ca2010-08-19 05:56:38314 }
[email protected]697ef4c2010-10-14 16:38:58315
316 if (!new_stream) {
[email protected]2d0a4f92011-05-05 16:38:46317 // Close the stream and mark it as not_reusable. Even in the
318 // keep_alive case, we've determined that the stream_ is not
319 // reusable if new_stream is NULL.
320 stream_->Close(true);
[email protected]697ef4c2010-10-14 16:38:58321 next_state_ = STATE_CREATE_STREAM;
322 } else {
[email protected]b8015c42013-12-24 15:18:19323 // Renewed streams shouldn't carry over received bytes.
324 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
[email protected]697ef4c2010-10-14 16:38:58325 next_state_ = STATE_INIT_STREAM;
326 }
327 stream_.reset(new_stream);
[email protected]2d2697f92009-02-18 21:00:32328 }
[email protected]f9ee6b52008-11-08 06:46:23329
330 // Reset the other member variables.
[email protected]697ef4c2010-10-14 16:38:58331 ResetStateForAuthRestart();
[email protected]f9ee6b52008-11-08 06:46:23332}
333
[email protected]8e6441ca2010-08-19 05:56:38334bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
335 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
336 HaveAuth(pending_auth_target_);
337}
338
[email protected]9dea9e1f2009-01-29 00:30:47339int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
[email protected]49639fa2011-12-20 23:22:41340 const CompletionCallback& callback) {
[email protected]96d570e42008-08-05 22:43:04341 DCHECK(buf);
[email protected]e0c27be2009-07-15 13:09:35342 DCHECK_LT(0, buf_len);
[email protected]96d570e42008-08-05 22:43:04343
[email protected]1f14a912009-12-21 20:32:44344 State next_state = STATE_NONE;
[email protected]96d570e42008-08-05 22:43:04345
[email protected]ad8e04a2010-11-01 04:16:27346 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]8e6441ca2010-08-19 05:56:38347 if (headers_valid_ && headers.get() && stream_request_.get()) {
[email protected]8a1f3312010-05-25 19:25:04348 // We're trying to read the body of the response but we're still trying
[email protected]511f6f52010-12-17 03:58:29349 // to establish an SSL tunnel through an HTTP proxy. We can't read these
[email protected]8a1f3312010-05-25 19:25:04350 // bytes when establishing a tunnel because they might be controlled by
351 // an active network attacker. We don't worry about this for HTTP
352 // because an active network attacker can already control HTTP sessions.
[email protected]511f6f52010-12-17 03:58:29353 // We reach this case when the user cancels a 407 proxy auth prompt. We
354 // also don't worry about this for an HTTPS Proxy, because the
355 // communication with the proxy is secure.
[email protected]8a1f3312010-05-25 19:25:04356 // See http://crbug.com/8473.
[email protected]2df19bb2010-08-25 20:13:46357 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
[email protected]9094b602012-02-27 21:44:58358 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
[email protected]a7ea8832010-07-12 17:54:54359 LOG(WARNING) << "Blocked proxy response with status "
360 << headers->response_code() << " to CONNECT request for "
361 << GetHostAndPort(request_->url) << ".";
[email protected]8a1f3312010-05-25 19:25:04362 return ERR_TUNNEL_CONNECTION_FAILED;
[email protected]a8e9b162009-03-12 00:06:44363 }
364
[email protected]e60e47a2010-07-14 03:37:18365 // Are we using SPDY or HTTP?
[email protected]351ab642010-08-05 16:55:31366 next_state = STATE_READ_BODY;
[email protected]e60e47a2010-07-14 03:37:18367
[email protected]96d570e42008-08-05 22:43:04368 read_buf_ = buf;
369 read_buf_len_ = buf_len;
370
[email protected]1f14a912009-12-21 20:32:44371 next_state_ = next_state;
[email protected]96d570e42008-08-05 22:43:04372 int rv = DoLoop(OK);
373 if (rv == ERR_IO_PENDING)
[email protected]49639fa2011-12-20 23:22:41374 callback_ = callback;
[email protected]96d570e42008-08-05 22:43:04375 return rv;
376}
377
[email protected]8cd06c02014-01-25 07:50:14378void HttpNetworkTransaction::StopCaching() {}
379
[email protected]79e1fd62013-06-20 06:50:04380bool HttpNetworkTransaction::GetFullRequestHeaders(
381 HttpRequestHeaders* headers) const {
382 // TODO(ttuttle): Make sure we've populated request_headers_.
383 *headers = request_headers_;
384 return true;
385}
386
[email protected]b8015c42013-12-24 15:18:19387int64 HttpNetworkTransaction::GetTotalReceivedBytes() const {
388 int64 total_received_bytes = total_received_bytes_;
389 if (stream_)
390 total_received_bytes += stream_->GetTotalReceivedBytes();
391 return total_received_bytes;
392}
393
[email protected]8cd06c02014-01-25 07:50:14394void HttpNetworkTransaction::DoneReading() {}
395
[email protected]96d570e42008-08-05 22:43:04396const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
[email protected]90499482013-06-01 00:39:50397 return ((headers_valid_ && response_.headers.get()) ||
398 response_.ssl_info.cert.get() || response_.cert_request_info.get())
399 ? &response_
400 : NULL;
[email protected]96d570e42008-08-05 22:43:04401}
402
403LoadState HttpNetworkTransaction::GetLoadState() const {
404 // TODO(wtc): Define a new LoadState value for the
405 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
406 switch (next_state_) {
[email protected]1826a402014-01-08 15:40:48407 case STATE_CREATE_STREAM:
408 return LOAD_STATE_WAITING_FOR_DELEGATE;
[email protected]82918cc2010-08-25 17:24:50409 case STATE_CREATE_STREAM_COMPLETE:
[email protected]8e6441ca2010-08-19 05:56:38410 return stream_request_->GetLoadState();
[email protected]044de0642010-06-17 10:42:15411 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
412 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
[email protected]0877e3d2009-10-17 22:29:57413 case STATE_SEND_REQUEST_COMPLETE:
[email protected]96d570e42008-08-05 22:43:04414 return LOAD_STATE_SENDING_REQUEST;
415 case STATE_READ_HEADERS_COMPLETE:
416 return LOAD_STATE_WAITING_FOR_RESPONSE;
417 case STATE_READ_BODY_COMPLETE:
418 return LOAD_STATE_READING_RESPONSE;
419 default:
420 return LOAD_STATE_IDLE;
421 }
422}
423
[email protected]196d18a2012-08-30 03:47:31424UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
[email protected]351ab642010-08-05 16:55:31425 if (!stream_.get())
[email protected]196d18a2012-08-30 03:47:31426 return UploadProgress();
[email protected]96d570e42008-08-05 22:43:04427
yhiranoa7e05bb2014-11-06 05:40:39428 return stream_->GetUploadProgress();
[email protected]96d570e42008-08-05 22:43:04429}
430
[email protected]8cd06c02014-01-25 07:50:14431void HttpNetworkTransaction::SetQuicServerInfo(
432 QuicServerInfo* quic_server_info) {}
433
[email protected]5033ab82013-03-22 20:17:46434bool HttpNetworkTransaction::GetLoadTimingInfo(
435 LoadTimingInfo* load_timing_info) const {
436 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
437 return false;
438
439 load_timing_info->proxy_resolve_start =
440 proxy_info_.proxy_resolve_start_time();
441 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
442 load_timing_info->send_start = send_start_time_;
443 load_timing_info->send_end = send_end_time_;
[email protected]5033ab82013-03-22 20:17:46444 return true;
445}
446
447void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
448 priority_ = priority;
[email protected]bf828982013-08-14 18:01:47449 if (stream_request_)
450 stream_request_->SetPriority(priority);
[email protected]e86839fd2013-08-14 18:29:03451 if (stream_)
452 stream_->SetPriority(priority);
[email protected]5033ab82013-03-22 20:17:46453}
454
[email protected]831e4a32013-11-14 02:14:44455void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
456 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
457 websocket_handshake_stream_base_create_helper_ = create_helper;
458}
459
[email protected]1826a402014-01-08 15:40:48460void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
461 const BeforeNetworkStartCallback& callback) {
462 before_network_start_callback_ = callback;
463}
464
[email protected]597a1ab2014-06-26 08:12:27465void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
466 const BeforeProxyHeadersSentCallback& callback) {
467 before_proxy_headers_sent_callback_ = callback;
468}
469
[email protected]1826a402014-01-08 15:40:48470int HttpNetworkTransaction::ResumeNetworkStart() {
471 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
472 return DoLoop(OK);
473}
474
[email protected]102e27c2011-02-23 01:01:31475void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
476 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39477 HttpStream* stream) {
[email protected]82918cc2010-08-25 17:24:50478 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38479 DCHECK(stream_request_.get());
480
[email protected]b8015c42013-12-24 15:18:19481 if (stream_)
482 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:38483 stream_.reset(stream);
[email protected]102957f2011-09-02 17:10:14484 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31485 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38486 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
[email protected]c30bcce2011-12-20 17:50:51487 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
488 stream_request_->protocol_negotiated());
[email protected]8e6441ca2010-08-19 05:56:38489 response_.was_fetched_via_spdy = stream_request_->using_spdy();
490 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
[email protected]d8fc4722014-06-13 13:17:15491 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
492 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
[email protected]8e6441ca2010-08-19 05:56:38493 OnIOComplete(OK);
494}
495
[email protected]a9cf2b92013-10-30 12:08:49496void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
[email protected]3732cea2013-06-21 06:50:50497 const SSLConfig& used_ssl_config,
498 const ProxyInfo& used_proxy_info,
[email protected]a9cf2b92013-10-30 12:08:49499 WebSocketHandshakeStreamBase* stream) {
[email protected]831e4a32013-11-14 02:14:44500 OnStreamReady(used_ssl_config, used_proxy_info, stream);
[email protected]3732cea2013-06-21 06:50:50501}
502
[email protected]102e27c2011-02-23 01:01:31503void HttpNetworkTransaction::OnStreamFailed(int result,
504 const SSLConfig& used_ssl_config) {
[email protected]82918cc2010-08-25 17:24:50505 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38506 DCHECK_NE(OK, result);
507 DCHECK(stream_request_.get());
508 DCHECK(!stream_.get());
[email protected]102957f2011-09-02 17:10:14509 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38510
511 OnIOComplete(result);
512}
513
[email protected]102e27c2011-02-23 01:01:31514void HttpNetworkTransaction::OnCertificateError(
515 int result,
516 const SSLConfig& used_ssl_config,
517 const SSLInfo& ssl_info) {
[email protected]82918cc2010-08-25 17:24:50518 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38519 DCHECK_NE(OK, result);
520 DCHECK(stream_request_.get());
521 DCHECK(!stream_.get());
522
523 response_.ssl_info = ssl_info;
[email protected]102957f2011-09-02 17:10:14524 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38525
526 // TODO(mbelshe): For now, we're going to pass the error through, and that
527 // will close the stream_request in all cases. This means that we're always
[email protected]82918cc2010-08-25 17:24:50528 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
529 // good and the user chooses to ignore the error. This is not ideal, but not
530 // the end of the world either.
[email protected]8e6441ca2010-08-19 05:56:38531
532 OnIOComplete(result);
533}
534
535void HttpNetworkTransaction::OnNeedsProxyAuth(
[email protected]6dc476da2010-09-01 04:43:50536 const HttpResponseInfo& proxy_response,
[email protected]102e27c2011-02-23 01:01:31537 const SSLConfig& used_ssl_config,
538 const ProxyInfo& used_proxy_info,
[email protected]6dc476da2010-09-01 04:43:50539 HttpAuthController* auth_controller) {
[email protected]8e6441ca2010-08-19 05:56:38540 DCHECK(stream_request_.get());
[email protected]82918cc2010-08-25 17:24:50541 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38542
543 establishing_tunnel_ = true;
544 response_.headers = proxy_response.headers;
545 response_.auth_challenge = proxy_response.auth_challenge;
546 headers_valid_ = true;
[email protected]102957f2011-09-02 17:10:14547 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31548 proxy_info_ = used_proxy_info;
[email protected]8e6441ca2010-08-19 05:56:38549
550 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
551 pending_auth_target_ = HttpAuth::AUTH_PROXY;
552
553 DoCallback(OK);
554}
555
556void HttpNetworkTransaction::OnNeedsClientAuth(
[email protected]102e27c2011-02-23 01:01:31557 const SSLConfig& used_ssl_config,
[email protected]6dc476da2010-09-01 04:43:50558 SSLCertRequestInfo* cert_info) {
[email protected]82918cc2010-08-25 17:24:50559 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
[email protected]8e6441ca2010-08-19 05:56:38560
[email protected]102957f2011-09-02 17:10:14561 server_ssl_config_ = used_ssl_config;
[email protected]8e6441ca2010-08-19 05:56:38562 response_.cert_request_info = cert_info;
[email protected]65a3b912010-08-21 05:46:58563 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:38564}
565
[email protected]511f6f52010-12-17 03:58:29566void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
567 const HttpResponseInfo& response_info,
[email protected]102e27c2011-02-23 01:01:31568 const SSLConfig& used_ssl_config,
569 const ProxyInfo& used_proxy_info,
yhiranoa7e05bb2014-11-06 05:40:39570 HttpStream* stream) {
[email protected]511f6f52010-12-17 03:58:29571 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
572
573 headers_valid_ = true;
574 response_ = response_info;
[email protected]102957f2011-09-02 17:10:14575 server_ssl_config_ = used_ssl_config;
[email protected]102e27c2011-02-23 01:01:31576 proxy_info_ = used_proxy_info;
[email protected]b8015c42013-12-24 15:18:19577 if (stream_)
578 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]511f6f52010-12-17 03:58:29579 stream_.reset(stream);
580 stream_request_.reset(); // we're done with the stream request
581 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
582}
583
Adam Ricecb76ac62015-02-20 05:33:25584bool HttpNetworkTransaction::IsSecureRequest() const {
585 return request_->url.SchemeIsSecure();
initial.commit586acc5fe2008-07-26 22:42:52586}
587
Adam Rice425cf122015-01-19 06:18:24588bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
tbansal7cec3812015-02-05 21:25:12589 return (proxy_info_.is_http() || proxy_info_.is_https() ||
590 proxy_info_.is_quic()) &&
Adam Rice425cf122015-01-19 06:18:24591 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
592}
593
initial.commit586acc5fe2008-07-26 22:42:52594void HttpNetworkTransaction::DoCallback(int rv) {
[email protected]0b0bf032010-09-21 18:08:50595 DCHECK_NE(rv, ERR_IO_PENDING);
[email protected]49639fa2011-12-20 23:22:41596 DCHECK(!callback_.is_null());
initial.commit586acc5fe2008-07-26 22:42:52597
[email protected]96d570e42008-08-05 22:43:04598 // Since Run may result in Read being called, clear user_callback_ up front.
[email protected]49639fa2011-12-20 23:22:41599 CompletionCallback c = callback_;
600 callback_.Reset();
601 c.Run(rv);
initial.commit586acc5fe2008-07-26 22:42:52602}
603
604void HttpNetworkTransaction::OnIOComplete(int result) {
initial.commit586acc5fe2008-07-26 22:42:52605 int rv = DoLoop(result);
initial.commit586acc5fe2008-07-26 22:42:52606 if (rv != ERR_IO_PENDING)
607 DoCallback(rv);
608}
609
610int HttpNetworkTransaction::DoLoop(int result) {
pkastinga9269aa42015-04-10 01:42:26611 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
612 tracked_objects::ScopedTracker tracking_profile(
613 FROM_HERE_WITH_EXPLICIT_FUNCTION(
614 "424359 HttpNetworkTransaction::DoLoop"));
615
initial.commit586acc5fe2008-07-26 22:42:52616 DCHECK(next_state_ != STATE_NONE);
617
618 int rv = result;
619 do {
620 State state = next_state_;
621 next_state_ = STATE_NONE;
622 switch (state) {
[email protected]1826a402014-01-08 15:40:48623 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
624 DCHECK_EQ(OK, rv);
625 rv = DoNotifyBeforeCreateStream();
626 break;
[email protected]82918cc2010-08-25 17:24:50627 case STATE_CREATE_STREAM:
628 DCHECK_EQ(OK, rv);
629 rv = DoCreateStream();
630 break;
631 case STATE_CREATE_STREAM_COMPLETE:
632 rv = DoCreateStreamComplete(rv);
633 break;
[email protected]351ab642010-08-05 16:55:31634 case STATE_INIT_STREAM:
635 DCHECK_EQ(OK, rv);
636 rv = DoInitStream();
637 break;
638 case STATE_INIT_STREAM_COMPLETE:
639 rv = DoInitStreamComplete(rv);
640 break;
[email protected]044de0642010-06-17 10:42:15641 case STATE_GENERATE_PROXY_AUTH_TOKEN:
642 DCHECK_EQ(OK, rv);
643 rv = DoGenerateProxyAuthToken();
644 break;
645 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
646 rv = DoGenerateProxyAuthTokenComplete(rv);
647 break;
648 case STATE_GENERATE_SERVER_AUTH_TOKEN:
649 DCHECK_EQ(OK, rv);
650 rv = DoGenerateServerAuthToken();
651 break;
652 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
653 rv = DoGenerateServerAuthTokenComplete(rv);
654 break;
[email protected]daddea62012-09-19 05:51:13655 case STATE_INIT_REQUEST_BODY:
656 DCHECK_EQ(OK, rv);
657 rv = DoInitRequestBody();
658 break;
659 case STATE_INIT_REQUEST_BODY_COMPLETE:
660 rv = DoInitRequestBodyComplete(rv);
661 break;
[email protected]4875ba12011-03-30 22:31:51662 case STATE_BUILD_REQUEST:
[email protected]725355a2009-03-25 20:42:55663 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38664 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
[email protected]4875ba12011-03-30 22:31:51665 rv = DoBuildRequest();
666 break;
667 case STATE_BUILD_REQUEST_COMPLETE:
668 rv = DoBuildRequestComplete(rv);
669 break;
670 case STATE_SEND_REQUEST:
671 DCHECK_EQ(OK, rv);
[email protected]0877e3d2009-10-17 22:29:57672 rv = DoSendRequest();
initial.commit586acc5fe2008-07-26 22:42:52673 break;
[email protected]0877e3d2009-10-17 22:29:57674 case STATE_SEND_REQUEST_COMPLETE:
675 rv = DoSendRequestComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43676 net_log_.EndEventWithNetErrorCode(
677 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
initial.commit586acc5fe2008-07-26 22:42:52678 break;
679 case STATE_READ_HEADERS:
[email protected]725355a2009-03-25 20:42:55680 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38681 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
initial.commit586acc5fe2008-07-26 22:42:52682 rv = DoReadHeaders();
683 break;
684 case STATE_READ_HEADERS_COMPLETE:
685 rv = DoReadHeadersComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43686 net_log_.EndEventWithNetErrorCode(
687 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
initial.commit586acc5fe2008-07-26 22:42:52688 break;
689 case STATE_READ_BODY:
[email protected]725355a2009-03-25 20:42:55690 DCHECK_EQ(OK, rv);
[email protected]b6754252012-06-13 23:14:38691 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
initial.commit586acc5fe2008-07-26 22:42:52692 rv = DoReadBody();
693 break;
694 case STATE_READ_BODY_COMPLETE:
695 rv = DoReadBodyComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43696 net_log_.EndEventWithNetErrorCode(
697 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
initial.commit586acc5fe2008-07-26 22:42:52698 break;
[email protected]2d2697f92009-02-18 21:00:32699 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
[email protected]725355a2009-03-25 20:42:55700 DCHECK_EQ(OK, rv);
[email protected]9e743cd2010-03-16 07:03:53701 net_log_.BeginEvent(
[email protected]b6754252012-06-13 23:14:38702 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
[email protected]2d2697f92009-02-18 21:00:32703 rv = DoDrainBodyForAuthRestart();
704 break;
705 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
706 rv = DoDrainBodyForAuthRestartComplete(rv);
[email protected]d7fd1782011-02-08 19:16:43707 net_log_.EndEventWithNetErrorCode(
708 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
[email protected]2d2697f92009-02-18 21:00:32709 break;
initial.commit586acc5fe2008-07-26 22:42:52710 default:
711 NOTREACHED() << "bad state";
712 rv = ERR_FAILED;
[email protected]96d570e42008-08-05 22:43:04713 break;
initial.commit586acc5fe2008-07-26 22:42:52714 }
715 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
716
717 return rv;
718}
719
[email protected]1826a402014-01-08 15:40:48720int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
pkastinga9269aa42015-04-10 01:42:26721 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
722 tracked_objects::ScopedTracker tracking_profile(
723 FROM_HERE_WITH_EXPLICIT_FUNCTION(
724 "424359 HttpNetworkTransaction::DoNotifyBeforeCreateStream"));
725
[email protected]1826a402014-01-08 15:40:48726 next_state_ = STATE_CREATE_STREAM;
727 bool defer = false;
728 if (!before_network_start_callback_.is_null())
729 before_network_start_callback_.Run(&defer);
730 if (!defer)
731 return OK;
732 return ERR_IO_PENDING;
733}
734
[email protected]82918cc2010-08-25 17:24:50735int HttpNetworkTransaction::DoCreateStream() {
pkastinga9269aa42015-04-10 01:42:26736 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
737 tracked_objects::ScopedTracker tracking_profile(
738 FROM_HERE_WITH_EXPLICIT_FUNCTION(
739 "424359 HttpNetworkTransaction::DoCreateStream"));
740
[email protected]82918cc2010-08-25 17:24:50741 next_state_ = STATE_CREATE_STREAM_COMPLETE;
[email protected]831e4a32013-11-14 02:14:44742 if (ForWebSocketHandshake()) {
743 stream_request_.reset(
[email protected]0191b51c2013-11-18 10:55:23744 session_->http_stream_factory_for_websocket()
[email protected]831e4a32013-11-14 02:14:44745 ->RequestWebSocketHandshakeStream(
746 *request_,
747 priority_,
748 server_ssl_config_,
749 proxy_ssl_config_,
750 this,
751 websocket_handshake_stream_base_create_helper_,
752 net_log_));
753 } else {
754 stream_request_.reset(
755 session_->http_stream_factory()->RequestStream(
756 *request_,
757 priority_,
758 server_ssl_config_,
759 proxy_ssl_config_,
760 this,
761 net_log_));
762 }
[email protected]26816882010-10-14 18:03:09763 DCHECK(stream_request_.get());
[email protected]8e6441ca2010-08-19 05:56:38764 return ERR_IO_PENDING;
[email protected]351ab642010-08-05 16:55:31765}
766
[email protected]82918cc2010-08-25 17:24:50767int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
pkastinga9269aa42015-04-10 01:42:26768 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
769 tracked_objects::ScopedTracker tracking_profile(
770 FROM_HERE_WITH_EXPLICIT_FUNCTION(
771 "424359 HttpNetworkTransaction::DoCreateStreamComplete"));
772
[email protected]394816e92010-08-03 07:38:59773 if (result == OK) {
[email protected]82918cc2010-08-25 17:24:50774 next_state_ = STATE_INIT_STREAM;
[email protected]8e6441ca2010-08-19 05:56:38775 DCHECK(stream_.get());
[email protected]adb00242010-10-29 03:04:33776 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
777 result = HandleCertificateRequest(result);
[email protected]511f6f52010-12-17 03:58:29778 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
779 // Return OK and let the caller read the proxy's error page
780 next_state_ = STATE_NONE;
781 return OK;
bncfacdd852015-01-09 19:22:54782 } else if (result == ERR_HTTP_1_1_REQUIRED ||
783 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
784 return HandleHttp11Required(result);
[email protected]394816e92010-08-03 07:38:59785 }
786
[email protected]bd0b6772011-01-11 19:59:30787 // Handle possible handshake errors that may have occurred if the stream
788 // used SSL for one or more of the layers.
789 result = HandleSSLHandshakeError(result);
790
[email protected]8e6441ca2010-08-19 05:56:38791 // At this point we are done with the stream_request_.
[email protected]26816882010-10-14 18:03:09792 stream_request_.reset();
[email protected]8e6441ca2010-08-19 05:56:38793 return result;
[email protected]394816e92010-08-03 07:38:59794}
795
[email protected]82918cc2010-08-25 17:24:50796int HttpNetworkTransaction::DoInitStream() {
pkastinga9269aa42015-04-10 01:42:26797 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
798 tracked_objects::ScopedTracker tracking_profile(
799 FROM_HERE_WITH_EXPLICIT_FUNCTION(
800 "424359 HttpNetworkTransaction::DoInitStream"));
801
[email protected]82918cc2010-08-25 17:24:50802 DCHECK(stream_.get());
803 next_state_ = STATE_INIT_STREAM_COMPLETE;
[email protected]262eec82013-03-19 21:01:36804 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
[email protected]82918cc2010-08-25 17:24:50805}
806
807int HttpNetworkTransaction::DoInitStreamComplete(int result) {
pkastinga9269aa42015-04-10 01:42:26808 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
809 tracked_objects::ScopedTracker tracking_profile(
810 FROM_HERE_WITH_EXPLICIT_FUNCTION(
811 "424359 HttpNetworkTransaction::DoInitStreamComplete"));
812
[email protected]82918cc2010-08-25 17:24:50813 if (result == OK) {
814 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
[email protected]82918cc2010-08-25 17:24:50815 } else {
[email protected]82918cc2010-08-25 17:24:50816 if (result < 0)
[email protected]044dcc52010-09-17 15:44:26817 result = HandleIOError(result);
818
819 // The stream initialization failed, so this stream will never be useful.
[email protected]b8015c42013-12-24 15:18:19820 if (stream_)
821 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]044dcc52010-09-17 15:44:26822 stream_.reset();
[email protected]82918cc2010-08-25 17:24:50823 }
824
825 return result;
826}
827
[email protected]044de0642010-06-17 10:42:15828int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
pkastinga9269aa42015-04-10 01:42:26829 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
830 tracked_objects::ScopedTracker tracking_profile(
831 FROM_HERE_WITH_EXPLICIT_FUNCTION(
832 "424359 HttpNetworkTransaction::DoGenerateProxyAuthToken"));
833
[email protected]044de0642010-06-17 10:42:15834 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
835 if (!ShouldApplyProxyAuth())
836 return OK;
[email protected]394816e92010-08-03 07:38:59837 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
838 if (!auth_controllers_[target].get())
[email protected]3598c6022010-09-17 23:13:09839 auth_controllers_[target] =
840 new HttpAuthController(target,
841 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31842 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09843 session_->http_auth_handler_factory());
[email protected]394816e92010-08-03 07:38:59844 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41845 io_callback_,
[email protected]394816e92010-08-03 07:38:59846 net_log_);
[email protected]044de0642010-06-17 10:42:15847}
848
849int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
pkastinga9269aa42015-04-10 01:42:26850 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
851 tracked_objects::ScopedTracker tracking_profile(
852 FROM_HERE_WITH_EXPLICIT_FUNCTION(
853 "424359 HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete"));
854
[email protected]044de0642010-06-17 10:42:15855 DCHECK_NE(ERR_IO_PENDING, rv);
856 if (rv == OK)
857 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
858 return rv;
859}
860
861int HttpNetworkTransaction::DoGenerateServerAuthToken() {
pkastinga9269aa42015-04-10 01:42:26862 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
863 tracked_objects::ScopedTracker tracking_profile(
864 FROM_HERE_WITH_EXPLICIT_FUNCTION(
865 "424359 HttpNetworkTransaction::DoGenerateServerAuthToken"));
866
[email protected]044de0642010-06-17 10:42:15867 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
[email protected]394816e92010-08-03 07:38:59868 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
[email protected]2217aa22013-10-11 03:03:54869 if (!auth_controllers_[target].get()) {
[email protected]3598c6022010-09-17 23:13:09870 auth_controllers_[target] =
871 new HttpAuthController(target,
872 AuthURL(target),
[email protected]102e27c2011-02-23 01:01:31873 session_->http_auth_cache(),
[email protected]3598c6022010-09-17 23:13:09874 session_->http_auth_handler_factory());
[email protected]2217aa22013-10-11 03:03:54875 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
876 auth_controllers_[target]->DisableEmbeddedIdentity();
877 }
[email protected]044de0642010-06-17 10:42:15878 if (!ShouldApplyServerAuth())
879 return OK;
[email protected]394816e92010-08-03 07:38:59880 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
[email protected]49639fa2011-12-20 23:22:41881 io_callback_,
[email protected]394816e92010-08-03 07:38:59882 net_log_);
[email protected]044de0642010-06-17 10:42:15883}
884
885int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
pkastinga9269aa42015-04-10 01:42:26886 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
887 tracked_objects::ScopedTracker tracking_profile(
888 FROM_HERE_WITH_EXPLICIT_FUNCTION(
889 "424359 HttpNetworkTransaction::DoGenerateServerAuthTokenComplete"));
890
[email protected]044de0642010-06-17 10:42:15891 DCHECK_NE(ERR_IO_PENDING, rv);
892 if (rv == OK)
[email protected]daddea62012-09-19 05:51:13893 next_state_ = STATE_INIT_REQUEST_BODY;
[email protected]044de0642010-06-17 10:42:15894 return rv;
895}
896
Adam Rice425cf122015-01-19 06:18:24897void HttpNetworkTransaction::BuildRequestHeaders(
898 bool using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14899 request_headers_.SetHeader(HttpRequestHeaders::kHost,
900 GetHostAndOptionalPort(request_->url));
901
902 // For compat with HTTP/1.0 servers and proxies:
Adam Rice425cf122015-01-19 06:18:24903 if (using_http_proxy_without_tunnel) {
[email protected]2979a492011-04-06 00:29:14904 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
905 "keep-alive");
906 } else {
907 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
908 }
909
[email protected]2979a492011-04-06 00:29:14910 // Add a content length header?
[email protected]bf3eb002012-11-15 05:50:11911 if (request_->upload_data_stream) {
912 if (request_->upload_data_stream->is_chunked()) {
[email protected]2979a492011-04-06 00:29:14913 request_headers_.SetHeader(
914 HttpRequestHeaders::kTransferEncoding, "chunked");
915 } else {
916 request_headers_.SetHeader(
917 HttpRequestHeaders::kContentLength,
[email protected]bf3eb002012-11-15 05:50:11918 base::Uint64ToString(request_->upload_data_stream->size()));
[email protected]2979a492011-04-06 00:29:14919 }
920 } else if (request_->method == "POST" || request_->method == "PUT" ||
921 request_->method == "HEAD") {
922 // An empty POST/PUT request still needs a content length. As for HEAD,
923 // IE and Safari also add a content length header. Presumably it is to
924 // support sending a HEAD request to an URL that only expects to be sent a
925 // POST or some other method that normally would have a message body.
926 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
927 }
928
929 // Honor load flags that impact proxy caches.
930 if (request_->load_flags & LOAD_BYPASS_CACHE) {
931 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
932 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
933 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
934 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
935 }
936
937 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
938 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
939 &request_headers_);
940 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
941 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
942 &request_headers_);
943
[email protected]c10450102011-06-27 09:06:16944 request_headers_.MergeFrom(request_->extra_headers);
[email protected]1252d42f2014-07-01 21:20:20945
Adam Rice425cf122015-01-19 06:18:24946 if (using_http_proxy_without_tunnel &&
947 !before_proxy_headers_sent_callback_.is_null())
[email protected]1252d42f2014-07-01 21:20:20948 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
949
[email protected]173f8e22013-04-10 04:18:20950 response_.did_use_http_auth =
951 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
952 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
[email protected]2979a492011-04-06 00:29:14953}
954
[email protected]daddea62012-09-19 05:51:13955int HttpNetworkTransaction::DoInitRequestBody() {
pkastinga9269aa42015-04-10 01:42:26956 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
957 tracked_objects::ScopedTracker tracking_profile(
958 FROM_HERE_WITH_EXPLICIT_FUNCTION(
959 "424359 HttpNetworkTransaction::DoInitRequestBody"));
960
[email protected]daddea62012-09-19 05:51:13961 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
[email protected]daddea62012-09-19 05:51:13962 int rv = OK;
[email protected]bf3eb002012-11-15 05:50:11963 if (request_->upload_data_stream)
964 rv = request_->upload_data_stream->Init(io_callback_);
[email protected]daddea62012-09-19 05:51:13965 return rv;
966}
[email protected]4875ba12011-03-30 22:31:51967
[email protected]daddea62012-09-19 05:51:13968int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
pkastinga9269aa42015-04-10 01:42:26969 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
970 tracked_objects::ScopedTracker tracking_profile(
971 FROM_HERE_WITH_EXPLICIT_FUNCTION(
972 "424359 HttpNetworkTransaction::DoInitRequestBodyComplete"));
973
[email protected]daddea62012-09-19 05:51:13974 if (result == OK)
975 next_state_ = STATE_BUILD_REQUEST;
[email protected]daddea62012-09-19 05:51:13976 return result;
977}
978
979int HttpNetworkTransaction::DoBuildRequest() {
pkastinga9269aa42015-04-10 01:42:26980 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
981 tracked_objects::ScopedTracker tracking_profile(
982 FROM_HERE_WITH_EXPLICIT_FUNCTION(
983 "424359 HttpNetworkTransaction::DoBuildRequest"));
984
[email protected]daddea62012-09-19 05:51:13985 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
[email protected]4875ba12011-03-30 22:31:51986 headers_valid_ = false;
987
988 // This is constructed lazily (instead of within our Start method), so that
989 // we have proxy info available.
990 if (request_headers_.IsEmpty()) {
Adam Rice425cf122015-01-19 06:18:24991 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
992 BuildRequestHeaders(using_http_proxy_without_tunnel);
[email protected]4875ba12011-03-30 22:31:51993 }
994
[email protected]4875ba12011-03-30 22:31:51995 return OK;
996}
997
998int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
pkastinga9269aa42015-04-10 01:42:26999 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1000 tracked_objects::ScopedTracker tracking_profile(
1001 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1002 "424359 HttpNetworkTransaction::DoBuildRequestComplete"));
1003
[email protected]9f489d72011-04-04 23:29:241004 if (result == OK)
1005 next_state_ = STATE_SEND_REQUEST;
[email protected]4875ba12011-03-30 22:31:511006 return result;
1007}
1008
[email protected]0877e3d2009-10-17 22:29:571009int HttpNetworkTransaction::DoSendRequest() {
pkastinga9269aa42015-04-10 01:42:261010 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1011 tracked_objects::ScopedTracker tracking_profile(
1012 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1013 "424359 HttpNetworkTransaction::DoSendRequest"));
1014
[email protected]58e32bb2013-01-21 18:23:251015 send_start_time_ = base::TimeTicks::Now();
[email protected]0877e3d2009-10-17 22:29:571016 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1017
[email protected]bf3eb002012-11-15 05:50:111018 return stream_->SendRequest(request_headers_, &response_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521019}
1020
[email protected]0877e3d2009-10-17 22:29:571021int HttpNetworkTransaction::DoSendRequestComplete(int result) {
pkastinga9269aa42015-04-10 01:42:261022 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1023 tracked_objects::ScopedTracker tracking_profile(
1024 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1025 "424359 HttpNetworkTransaction::DoSendRequestComplete"));
1026
[email protected]58e32bb2013-01-21 18:23:251027 send_end_time_ = base::TimeTicks::Now();
initial.commit586acc5fe2008-07-26 22:42:521028 if (result < 0)
1029 return HandleIOError(result);
[email protected]7cf7ccb2013-04-20 02:53:081030 response_.network_accessed = true;
[email protected]0877e3d2009-10-17 22:29:571031 next_state_ = STATE_READ_HEADERS;
initial.commit586acc5fe2008-07-26 22:42:521032 return OK;
1033}
1034
1035int HttpNetworkTransaction::DoReadHeaders() {
pkastinga9269aa42015-04-10 01:42:261036 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1037 tracked_objects::ScopedTracker tracking_profile(
1038 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1039 "424359 HttpNetworkTransaction::DoReadHeaders"));
1040
initial.commit586acc5fe2008-07-26 22:42:521041 next_state_ = STATE_READ_HEADERS_COMPLETE;
[email protected]49639fa2011-12-20 23:22:411042 return stream_->ReadResponseHeaders(io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521043}
1044
1045int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
pkastinga9269aa42015-04-10 01:42:261046 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1047 tracked_objects::ScopedTracker tracking_profile(
1048 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1049 "424359 HttpNetworkTransaction::DoReadHeadersComplete"));
1050
[email protected]0b45559b2009-06-12 21:45:111051 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1052 // due to SSL renegotiation.
[email protected]8e6441ca2010-08-19 05:56:381053 if (IsCertificateError(result)) {
1054 // We don't handle a certificate error during SSL renegotiation, so we
1055 // have to return an error that's not in the certificate error range
1056 // (-2xx).
1057 LOG(ERROR) << "Got a server certificate with error " << result
1058 << " during SSL renegotiation";
1059 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1060 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1061 // TODO(wtc): Need a test case for this code path!
1062 DCHECK(stream_.get());
Adam Ricecb76ac62015-02-20 05:33:251063 DCHECK(IsSecureRequest());
[email protected]8e6441ca2010-08-19 05:56:381064 response_.cert_request_info = new SSLCertRequestInfo;
[email protected]90499482013-06-01 00:39:501065 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
[email protected]8e6441ca2010-08-19 05:56:381066 result = HandleCertificateRequest(result);
1067 if (result == OK)
1068 return result;
[email protected]2181ea002009-06-09 01:37:271069 }
1070
bncfacdd852015-01-09 19:22:541071 if (result == ERR_HTTP_1_1_REQUIRED ||
1072 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1073 return HandleHttp11Required(result);
1074 }
1075
[email protected]c871864d72014-03-13 19:56:191076 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1077 // response headers were received, we do the best we can to make sense of it
1078 // and send it back up the stack.
1079 //
1080 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1081 // bizarre for SPDY. Assuming this logic is useful at all.
1082 // TODO(davidben): Bubble the error code up so we do not cache?
1083 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1084 result = OK;
1085
1086 if (result < 0)
1087 return HandleIOError(result);
1088
[email protected]90499482013-06-01 00:39:501089 DCHECK(response_.headers.get());
initial.commit586acc5fe2008-07-26 22:42:521090
[email protected]d58ceea82014-06-04 10:55:541091 // On a 408 response from the server ("Request Timeout") on a stale socket,
1092 // retry the request.
[email protected]0aff0d82014-06-14 08:49:041093 // Headers can be NULL because of http://crbug.com/384554.
1094 if (response_.headers.get() && response_.headers->response_code() == 408 &&
[email protected]d58ceea82014-06-04 10:55:541095 stream_->IsConnectionReused()) {
1096 net_log_.AddEventWithNetErrorCode(
1097 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1098 response_.headers->response_code());
1099 // This will close the socket - it would be weird to try and reuse it, even
1100 // if the server doesn't actually close it.
1101 ResetConnectionAndRequestForResend();
1102 return OK;
1103 }
1104
[email protected]93f8b562012-03-27 01:00:161105 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1106 if (request_->load_flags & LOAD_MAIN_FRAME) {
1107 const int response_code = response_.headers->response_code();
1108 UMA_HISTOGRAM_ENUMERATION(
1109 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1110 }
1111
[email protected]3abacd62012-06-10 20:20:321112 net_log_.AddEvent(
1113 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1114 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
[email protected]dbb83db2010-05-11 18:13:391115
[email protected]a7e41312009-12-16 23:18:141116 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
[email protected]0877e3d2009-10-17 22:29:571117 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1118 // indicates a buggy server. See:
1119 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1120 if (request_->method == "PUT")
1121 return ERR_METHOD_NOT_SUPPORTED;
1122 }
[email protected]4ddaf2502008-10-23 18:26:191123
[email protected]0877e3d2009-10-17 22:29:571124 // Check for an intermediate 100 Continue response. An origin server is
1125 // allowed to send this response even if we didn't ask for it, so we just
1126 // need to skip over it.
1127 // We treat any other 1xx in this same way (although in practice getting
1128 // a 1xx that isn't a 100 is rare).
[email protected]831e4a32013-11-14 02:14:441129 // Unless this is a WebSocket request, in which case we pass it on up.
1130 if (response_.headers->response_code() / 100 == 1 &&
1131 !ForWebSocketHandshake()) {
[email protected]007b3f82013-04-09 08:46:451132 response_.headers = new HttpResponseHeaders(std::string());
[email protected]0877e3d2009-10-17 22:29:571133 next_state_ = STATE_READ_HEADERS;
1134 return OK;
1135 }
1136
pkastingcba13292014-11-20 03:35:211137 ProcessAlternateProtocol(session_, *response_.headers.get(),
1138 HostPortPair::FromURL(request_->url));
[email protected]564b4912010-03-09 16:30:421139
[email protected]e772db3f2010-07-12 18:11:131140 int rv = HandleAuthChallenge();
[email protected]0877e3d2009-10-17 22:29:571141 if (rv != OK)
1142 return rv;
1143
Adam Ricecb76ac62015-02-20 05:33:251144 if (IsSecureRequest())
[email protected]8536ef52010-09-30 16:18:211145 stream_->GetSSLInfo(&response_.ssl_info);
1146
[email protected]0877e3d2009-10-17 22:29:571147 headers_valid_ = true;
[email protected]0877e3d2009-10-17 22:29:571148 return OK;
initial.commit586acc5fe2008-07-26 22:42:521149}
1150
1151int HttpNetworkTransaction::DoReadBody() {
pkastinga9269aa42015-04-10 01:42:261152 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1153 tracked_objects::ScopedTracker tracking_profile(
1154 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1155 "424359 HttpNetworkTransaction::DoReadBody"));
1156
[email protected]90499482013-06-01 00:39:501157 DCHECK(read_buf_.get());
[email protected]6501bc02009-06-25 20:55:131158 DCHECK_GT(read_buf_len_, 0);
[email protected]8e6441ca2010-08-19 05:56:381159 DCHECK(stream_ != NULL);
initial.commit586acc5fe2008-07-26 22:42:521160
1161 next_state_ = STATE_READ_BODY_COMPLETE;
[email protected]90499482013-06-01 00:39:501162 return stream_->ReadResponseBody(
1163 read_buf_.get(), read_buf_len_, io_callback_);
initial.commit586acc5fe2008-07-26 22:42:521164}
1165
1166int HttpNetworkTransaction::DoReadBodyComplete(int result) {
pkastinga9269aa42015-04-10 01:42:261167 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1168 tracked_objects::ScopedTracker tracking_profile(
1169 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1170 "424359 HttpNetworkTransaction::DoReadBodyComplete"));
1171
initial.commit586acc5fe2008-07-26 22:42:521172 // We are done with the Read call.
[email protected]8e6441ca2010-08-19 05:56:381173 bool done = false;
1174 if (result <= 0) {
1175 DCHECK_NE(ERR_IO_PENDING, result);
initial.commit586acc5fe2008-07-26 22:42:521176 done = true;
[email protected]8e6441ca2010-08-19 05:56:381177 }
[email protected]9492e4a2010-02-24 00:58:461178
[email protected]8e6441ca2010-08-19 05:56:381179 bool keep_alive = false;
[email protected]351ab642010-08-05 16:55:311180 if (stream_->IsResponseBodyComplete()) {
[email protected]8e6441ca2010-08-19 05:56:381181 // Note: Just because IsResponseBodyComplete is true, we're not
1182 // necessarily "done". We're only "done" when it is the last
1183 // read on this HttpNetworkTransaction, which will be signified
1184 // by a zero-length read.
1185 // TODO(mbelshe): The keepalive property is really a property of
1186 // the stream. No need to compute it here just to pass back
1187 // to the stream's Close function.
[email protected]5c6908e2012-08-06 18:53:471188 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1189 // ResponseHeaders.
1190 if (stream_->CanFindEndOfResponse()) {
1191 HttpResponseHeaders* headers = GetResponseHeaders();
1192 if (headers)
1193 keep_alive = headers->IsKeepAlive();
1194 }
initial.commit586acc5fe2008-07-26 22:42:521195 }
1196
[email protected]8e6441ca2010-08-19 05:56:381197 // Clean up connection if we are done.
initial.commit586acc5fe2008-07-26 22:42:521198 if (done) {
[email protected]8e6441ca2010-08-19 05:56:381199 stream_->Close(!keep_alive);
[email protected]e2a915a2010-08-19 07:55:011200 // Note: we don't reset the stream here. We've closed it, but we still
1201 // need it around so that callers can call methods such as
1202 // GetUploadProgress() and have them be meaningful.
1203 // TODO(mbelshe): This means we closed the stream here, and we close it
1204 // again in ~HttpNetworkTransaction. Clean that up.
1205
[email protected]8e6441ca2010-08-19 05:56:381206 // The next Read call will return 0 (EOF).
initial.commit586acc5fe2008-07-26 22:42:521207 }
1208
1209 // Clear these to avoid leaving around old state.
1210 read_buf_ = NULL;
1211 read_buf_len_ = 0;
1212
1213 return result;
1214}
1215
[email protected]2d2697f92009-02-18 21:00:321216int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
pkastinga9269aa42015-04-10 01:42:261217 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1218 tracked_objects::ScopedTracker tracking_profile(
1219 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1220 "424359 HttpNetworkTransaction::DoDrainBodyForAuthRestart"));
1221
[email protected]2d2697f92009-02-18 21:00:321222 // This method differs from DoReadBody only in the next_state_. So we just
1223 // call DoReadBody and override the next_state_. Perhaps there is a more
1224 // elegant way for these two methods to share code.
1225 int rv = DoReadBody();
1226 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1227 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1228 return rv;
1229}
1230
[email protected]0877e3d2009-10-17 22:29:571231// TODO(wtc): This method and the DoReadBodyComplete method are almost
1232// the same. Figure out a good way for these two methods to share code.
[email protected]2d2697f92009-02-18 21:00:321233int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
pkastinga9269aa42015-04-10 01:42:261234 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1235 tracked_objects::ScopedTracker tracking_profile(
1236 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1237 "424359 HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete"));
1238
[email protected]68873ba2009-06-04 21:49:231239 // keep_alive defaults to true because the very reason we're draining the
1240 // response body is to reuse the connection for auth restart.
1241 bool done = false, keep_alive = true;
[email protected]2d2697f92009-02-18 21:00:321242 if (result < 0) {
[email protected]0877e3d2009-10-17 22:29:571243 // Error or closed connection while reading the socket.
[email protected]2d2697f92009-02-18 21:00:321244 done = true;
[email protected]68873ba2009-06-04 21:49:231245 keep_alive = false;
[email protected]351ab642010-08-05 16:55:311246 } else if (stream_->IsResponseBodyComplete()) {
[email protected]0877e3d2009-10-17 22:29:571247 done = true;
[email protected]2d2697f92009-02-18 21:00:321248 }
1249
1250 if (done) {
1251 DidDrainBodyForAuthRestart(keep_alive);
1252 } else {
1253 // Keep draining.
1254 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1255 }
1256
1257 return OK;
1258}
1259
[email protected]5e363962009-06-19 19:57:011260int HttpNetworkTransaction::HandleCertificateRequest(int error) {
[email protected]8e6441ca2010-08-19 05:56:381261 // There are two paths through which the server can request a certificate
1262 // from us. The first is during the initial handshake, the second is
1263 // during SSL renegotiation.
1264 //
1265 // In both cases, we want to close the connection before proceeding.
1266 // We do this for two reasons:
1267 // First, we don't want to keep the connection to the server hung for a
1268 // long time while the user selects a certificate.
1269 // Second, even if we did keep the connection open, NSS has a bug where
1270 // restarting the handshake for ClientAuth is currently broken.
[email protected]65a3b912010-08-21 05:46:581271 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
[email protected]8e6441ca2010-08-19 05:56:381272
1273 if (stream_.get()) {
1274 // Since we already have a stream, we're being called as part of SSL
1275 // renegotiation.
1276 DCHECK(!stream_request_.get());
[email protected]b8015c42013-12-24 15:18:191277 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]8e6441ca2010-08-19 05:56:381278 stream_->Close(true);
1279 stream_.reset();
1280 }
1281
[email protected]26816882010-10-14 18:03:091282 // The server is asking for a client certificate during the initial
1283 // handshake.
1284 stream_request_.reset();
[email protected]5e363962009-06-19 19:57:011285
[email protected]ec229bc92010-11-22 09:51:451286 // If the user selected one of the certificates in client_certs or declined
1287 // to provide one for this server before, use the past decision
1288 // automatically.
1289 scoped_refptr<X509Certificate> client_cert;
1290 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1291 response_.cert_request_info->host_and_port, &client_cert);
1292 if (!found_cached_cert)
1293 return error;
1294
1295 // Check that the certificate selected is still a certificate the server
1296 // is likely to accept, based on the criteria supplied in the
1297 // CertificateRequest message.
[email protected]90499482013-06-01 00:39:501298 if (client_cert.get()) {
[email protected]f1958c382013-02-07 00:15:261299 const std::vector<std::string>& cert_authorities =
1300 response_.cert_request_info->cert_authorities;
[email protected]ec229bc92010-11-22 09:51:451301
[email protected]f1958c382013-02-07 00:15:261302 bool cert_still_valid = cert_authorities.empty() ||
1303 client_cert->IsIssuedByEncoded(cert_authorities);
[email protected]ec229bc92010-11-22 09:51:451304 if (!cert_still_valid)
1305 return error;
[email protected]5e363962009-06-19 19:57:011306 }
[email protected]ec229bc92010-11-22 09:51:451307
1308 // TODO(davidben): Add a unit test which covers this path; we need to be
1309 // able to send a legitimate certificate and also bypass/clear the
1310 // SSL session cache.
[email protected]102957f2011-09-02 17:10:141311 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1312 &proxy_ssl_config_ : &server_ssl_config_;
1313 ssl_config->send_client_cert = true;
1314 ssl_config->client_cert = client_cert;
[email protected]ec229bc92010-11-22 09:51:451315 next_state_ = STATE_CREATE_STREAM;
1316 // Reset the other member variables.
1317 // Note: this is necessary only with SSL renegotiation.
1318 ResetStateForRestart();
1319 return OK;
[email protected]0b45559b2009-06-12 21:45:111320}
1321
bncfacdd852015-01-09 19:22:541322int HttpNetworkTransaction::HandleHttp11Required(int error) {
1323 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1324 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1325
1326 if (error == ERR_HTTP_1_1_REQUIRED) {
1327 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1328 } else {
1329 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1330 }
1331 ResetConnectionAndRequestForResend();
1332 return OK;
1333}
1334
[email protected]384cc73a2013-12-08 22:41:031335void HttpNetworkTransaction::HandleClientAuthError(int error) {
1336 if (server_ssl_config_.send_client_cert &&
1337 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1338 session_->ssl_client_auth_cache()->Remove(
[email protected]791879c2013-12-17 07:22:411339 HostPortPair::FromURL(request_->url));
[email protected]384cc73a2013-12-08 22:41:031340 }
1341}
1342
[email protected]bd0b6772011-01-11 19:59:301343// TODO(rch): This does not correctly handle errors when an SSL proxy is
1344// being used, as all of the errors are handled as if they were generated
1345// by the endpoint host, request_->url, rather than considering if they were
[email protected]1c53a1f2011-01-13 00:36:381346// generated by the SSL proxy. http://crbug.com/69329
[email protected]bd0b6772011-01-11 19:59:301347int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1348 DCHECK(request_);
[email protected]384cc73a2013-12-08 22:41:031349 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301350
davidbena4c9d062015-04-03 22:34:251351 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1352 // reflect servers require a deprecated cipher rather than merely prefer
1353 // it. This, however, has no security benefit until the ciphers are actually
1354 // removed.
1355 if (!server_ssl_config_.enable_deprecated_cipher_suites &&
1356 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1357 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1358 net_log_.AddEvent(
1359 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1360 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
1361 server_ssl_config_.enable_deprecated_cipher_suites = true;
1362 ResetConnectionAndRequestForResend();
1363 return OK;
1364 }
1365
[email protected]02d1d442013-08-10 13:38:261366 bool should_fallback = false;
[email protected]158ac972013-04-19 23:29:231367 uint16 version_max = server_ssl_config_.version_max;
1368
initial.commit586acc5fe2008-07-26 22:42:521369 switch (error) {
[email protected]12833302014-07-02 01:57:311370 case ERR_CONNECTION_CLOSED:
[email protected]bd0b6772011-01-11 19:59:301371 case ERR_SSL_PROTOCOL_ERROR:
1372 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
[email protected]158ac972013-04-19 23:29:231373 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1374 version_max > server_ssl_config_.version_min) {
[email protected]80c75f682012-05-26 16:22:171375 // This could be a TLS-intolerant server or a server that chose a
1376 // cipher suite defined only for higher protocol versions (such as
1377 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1378 // back to the next lower version and retry.
1379 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1380 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1381 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1382 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1383 // version_max should match the maximum protocol version supported
1384 // by the SSLClientSocket class.
[email protected]158ac972013-04-19 23:29:231385 version_max--;
[email protected]c57ec022013-11-25 21:30:511386
1387 // Fallback to the lower SSL version.
1388 // While SSL 3.0 fallback should be eliminated because of security
1389 // reasons, there is a high risk of breaking the servers if this is
1390 // done in general.
[email protected]001e547e2013-12-06 16:08:391391 should_fallback = true;
[email protected]1c773ea12009-04-28 19:58:421392 }
initial.commit586acc5fe2008-07-26 22:42:521393 break;
[email protected]12833302014-07-02 01:57:311394 case ERR_CONNECTION_RESET:
1395 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1396 version_max > server_ssl_config_.version_min) {
1397 // Some network devices that inspect application-layer packets seem to
1398 // inject TCP reset packets to break the connections when they see TLS
1399 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1400 //
1401 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1402 // 1.2. We don't lose much in this fallback because the explicit IV for
1403 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1404 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1405 // support.
1406 //
1407 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1408 // to trigger a version fallback in general, especially the TLS 1.0 ->
1409 // SSL 3.0 fallback, which would drop TLS extensions.
1410 version_max--;
1411 should_fallback = true;
1412 }
1413 break;
[email protected]02d1d442013-08-10 13:38:261414 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1415 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1416 version_max > server_ssl_config_.version_min) {
1417 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1418 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1419 // crbug.com/260358. In order to make the fallback as minimal as
1420 // possible, this fallback is only triggered for >= TLS 1.1.
1421 version_max--;
1422 should_fallback = true;
1423 }
1424 break;
[email protected]a53e4d12013-12-07 16:37:241425 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1426 // The server told us that we should not have fallen back. A buggy server
1427 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1428 // connection. |fallback_error_code_| is initialised to
1429 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1430 error = fallback_error_code_;
1431 break;
[email protected]02d1d442013-08-10 13:38:261432 }
1433
1434 if (should_fallback) {
1435 net_log_.AddEvent(
1436 NetLog::TYPE_SSL_VERSION_FALLBACK,
1437 base::Bind(&NetLogSSLVersionFallbackCallback,
1438 &request_->url, error, server_ssl_config_.version_max,
1439 version_max));
[email protected]a53e4d12013-12-07 16:37:241440 fallback_error_code_ = error;
[email protected]02d1d442013-08-10 13:38:261441 server_ssl_config_.version_max = version_max;
1442 server_ssl_config_.version_fallback = true;
1443 ResetConnectionAndRequestForResend();
1444 error = OK;
initial.commit586acc5fe2008-07-26 22:42:521445 }
[email protected]158ac972013-04-19 23:29:231446
initial.commit586acc5fe2008-07-26 22:42:521447 return error;
1448}
1449
[email protected]bd0b6772011-01-11 19:59:301450// This method determines whether it is safe to resend the request after an
1451// IO error. It can only be called in response to request header or body
1452// write errors or response header read errors. It should not be used in
1453// other cases, such as a Connect error.
1454int HttpNetworkTransaction::HandleIOError(int error) {
[email protected]384cc73a2013-12-08 22:41:031455 // Because the peer may request renegotiation with client authentication at
1456 // any time, check and handle client authentication errors.
1457 HandleClientAuthError(error);
[email protected]bd0b6772011-01-11 19:59:301458
1459 switch (error) {
1460 // If we try to reuse a connection that the server is in the process of
1461 // closing, we may end up successfully writing out our request (or a
1462 // portion of our request) only to find a connection error when we try to
1463 // read from (or finish writing to) the socket.
1464 case ERR_CONNECTION_RESET:
1465 case ERR_CONNECTION_CLOSED:
1466 case ERR_CONNECTION_ABORTED:
[email protected]202965992011-12-07 23:04:511467 // There can be a race between the socket pool checking checking whether a
1468 // socket is still connected, receiving the FIN, and sending/reading data
1469 // on a reused socket. If we receive the FIN between the connectedness
1470 // check and writing/reading from the socket, we may first learn the socket
1471 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1472 // likely happen when trying to retrieve its IP address.
1473 // See http://crbug.com/105824 for more details.
1474 case ERR_SOCKET_NOT_CONNECTED:
[email protected]a34f61ee2014-03-18 20:59:491475 // If a socket is closed on its initial request, HttpStreamParser returns
1476 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1477 // preconnected but failed to be used before the server timed it out.
1478 case ERR_EMPTY_RESPONSE:
1479 if (ShouldResendRequest()) {
[email protected]b6754252012-06-13 23:14:381480 net_log_.AddEventWithNetErrorCode(
1481 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]bd0b6772011-01-11 19:59:301482 ResetConnectionAndRequestForResend();
1483 error = OK;
1484 }
1485 break;
[email protected]8753a122011-10-16 08:05:081486 case ERR_SPDY_PING_FAILED:
[email protected]721c0ce2011-10-13 02:41:001487 case ERR_SPDY_SERVER_REFUSED_STREAM:
[email protected]4d283b32013-10-17 12:57:271488 case ERR_QUIC_HANDSHAKE_FAILED:
[email protected]b6754252012-06-13 23:14:381489 net_log_.AddEventWithNetErrorCode(
1490 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
[email protected]721c0ce2011-10-13 02:41:001491 ResetConnectionAndRequestForResend();
1492 error = OK;
1493 break;
[email protected]bd0b6772011-01-11 19:59:301494 }
1495 return error;
1496}
1497
[email protected]c3b35c22008-09-27 03:19:421498void HttpNetworkTransaction::ResetStateForRestart() {
[email protected]697ef4c2010-10-14 16:38:581499 ResetStateForAuthRestart();
[email protected]b8015c42013-12-24 15:18:191500 if (stream_)
1501 total_received_bytes_ += stream_->GetTotalReceivedBytes();
[email protected]697ef4c2010-10-14 16:38:581502 stream_.reset();
1503}
1504
1505void HttpNetworkTransaction::ResetStateForAuthRestart() {
[email protected]58e32bb2013-01-21 18:23:251506 send_start_time_ = base::TimeTicks();
1507 send_end_time_ = base::TimeTicks();
[email protected]58e32bb2013-01-21 18:23:251508
[email protected]0757e7702009-03-27 04:00:221509 pending_auth_target_ = HttpAuth::AUTH_NONE;
[email protected]c3b35c22008-09-27 03:19:421510 read_buf_ = NULL;
1511 read_buf_len_ = 0;
[email protected]0877e3d2009-10-17 22:29:571512 headers_valid_ = false;
[email protected]b94f92d2010-10-27 16:45:201513 request_headers_.Clear();
[email protected]a7e41312009-12-16 23:18:141514 response_ = HttpResponseInfo();
[email protected]8e6441ca2010-08-19 05:56:381515 establishing_tunnel_ = false;
[email protected]0877e3d2009-10-17 22:29:571516}
1517
1518HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
[email protected]90499482013-06-01 00:39:501519 return response_.headers.get();
[email protected]c3b35c22008-09-27 03:19:421520}
1521
[email protected]a34f61ee2014-03-18 20:59:491522bool HttpNetworkTransaction::ShouldResendRequest() const {
[email protected]8e6441ca2010-08-19 05:56:381523 bool connection_is_proven = stream_->IsConnectionReused();
1524 bool has_received_headers = GetResponseHeaders() != NULL;
[email protected]58cebf8f2010-07-31 19:20:161525
[email protected]2a5c76b2008-09-25 22:15:161526 // NOTE: we resend a request only if we reused a keep-alive connection.
1527 // This automatically prevents an infinite resend loop because we'll run
1528 // out of the cached keep-alive connections eventually.
[email protected]8e6441ca2010-08-19 05:56:381529 if (connection_is_proven && !has_received_headers)
1530 return true;
1531 return false;
[email protected]1c773ea12009-04-28 19:58:421532}
1533
1534void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
[email protected]8e6441ca2010-08-19 05:56:381535 if (stream_.get()) {
1536 stream_->Close(true);
1537 stream_.reset();
[email protected]58cebf8f2010-07-31 19:20:161538 }
1539
[email protected]0877e3d2009-10-17 22:29:571540 // We need to clear request_headers_ because it contains the real request
1541 // headers, but we may need to resend the CONNECT request first to recreate
1542 // the SSL tunnel.
[email protected]b94f92d2010-10-27 16:45:201543 request_headers_.Clear();
[email protected]82918cc2010-08-25 17:24:501544 next_state_ = STATE_CREATE_STREAM; // Resend the request.
[email protected]86ec30d2008-09-29 21:53:541545}
1546
[email protected]1c773ea12009-04-28 19:58:421547bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
Adam Rice425cf122015-01-19 06:18:241548 return UsingHttpProxyWithoutTunnel();
[email protected]1c773ea12009-04-28 19:58:421549}
license.botbf09a502008-08-24 00:55:551550
[email protected]1c773ea12009-04-28 19:58:421551bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
[email protected]8a1f3312010-05-25 19:25:041552 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
[email protected]1c773ea12009-04-28 19:58:421553}
1554
[email protected]e772db3f2010-07-12 18:11:131555int HttpNetworkTransaction::HandleAuthChallenge() {
[email protected]ad8e04a2010-11-01 04:16:271556 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
[email protected]90499482013-06-01 00:39:501557 DCHECK(headers.get());
[email protected]c3b35c22008-09-27 03:19:421558
[email protected]0877e3d2009-10-17 22:29:571559 int status = headers->response_code();
[email protected]9094b602012-02-27 21:44:581560 if (status != HTTP_UNAUTHORIZED &&
1561 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
[email protected]c3b35c22008-09-27 03:19:421562 return OK;
[email protected]9094b602012-02-27 21:44:581563 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
[email protected]2227c692010-05-04 15:36:111564 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
[email protected]038e9a32008-10-08 22:40:161565 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1566 return ERR_UNEXPECTED_PROXY_AUTH;
[email protected]c3b35c22008-09-27 03:19:421567
[email protected]9094b602012-02-27 21:44:581568 // This case can trigger when an HTTPS server responds with a "Proxy
1569 // authentication required" status code through a non-authenticating
1570 // proxy.
[email protected]7a67a8152010-11-05 18:31:101571 if (!auth_controllers_[target].get())
1572 return ERR_UNEXPECTED_PROXY_AUTH;
1573
[email protected]a7ea8832010-07-12 17:54:541574 int rv = auth_controllers_[target]->HandleAuthChallenge(
[email protected]560c0432010-07-13 20:45:311575 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1576 net_log_);
[email protected]228404f2010-06-24 04:31:411577 if (auth_controllers_[target]->HaveAuthHandler())
1578 pending_auth_target_ = target;
1579
1580 scoped_refptr<AuthChallengeInfo> auth_info =
1581 auth_controllers_[target]->auth_info();
1582 if (auth_info.get())
1583 response_.auth_challenge = auth_info;
1584
[email protected]228404f2010-06-24 04:31:411585 return rv;
[email protected]f9ee6b52008-11-08 06:46:231586}
1587
[email protected]8e6441ca2010-08-19 05:56:381588bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1589 return auth_controllers_[target].get() &&
1590 auth_controllers_[target]->HaveAuth();
1591}
1592
[email protected]228404f2010-06-24 04:31:411593GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1594 switch (target) {
[email protected]2df19bb2010-08-25 20:13:461595 case HttpAuth::AUTH_PROXY: {
[email protected]228404f2010-06-24 04:31:411596 if (!proxy_info_.proxy_server().is_valid() ||
1597 proxy_info_.proxy_server().is_direct()) {
1598 return GURL(); // There is no proxy server.
1599 }
[email protected]2df19bb2010-08-25 20:13:461600 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1601 return GURL(scheme +
[email protected]2fbaecf22010-07-22 22:20:351602 proxy_info_.proxy_server().host_port_pair().ToString());
[email protected]2df19bb2010-08-25 20:13:461603 }
[email protected]228404f2010-06-24 04:31:411604 case HttpAuth::AUTH_SERVER:
[email protected]e69c1cd2014-07-29 07:42:291605 if (ForWebSocketHandshake()) {
1606 const GURL& url = request_->url;
1607 url::Replacements<char> ws_to_http;
1608 if (url.SchemeIs("ws")) {
1609 ws_to_http.SetScheme("http", url::Component(0, 4));
1610 } else {
1611 DCHECK(url.SchemeIs("wss"));
1612 ws_to_http.SetScheme("https", url::Component(0, 5));
1613 }
1614 return url.ReplaceComponents(ws_to_http);
1615 }
[email protected]228404f2010-06-24 04:31:411616 return request_->url;
1617 default:
1618 return GURL();
1619 }
[email protected]c3b35c22008-09-27 03:19:421620}
1621
[email protected]831e4a32013-11-14 02:14:441622bool HttpNetworkTransaction::ForWebSocketHandshake() const {
[email protected]23d3e022013-11-23 20:38:141623 return websocket_handshake_stream_base_create_helper_ &&
1624 request_->url.SchemeIsWSOrWSS();
[email protected]831e4a32013-11-14 02:14:441625}
1626
[email protected]d8eb84242010-09-25 02:25:061627#define STATE_CASE(s) \
1628 case s: \
1629 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1630 break
[email protected]aef04272010-06-28 18:03:041631
1632std::string HttpNetworkTransaction::DescribeState(State state) {
1633 std::string description;
1634 switch (state) {
[email protected]1826a402014-01-08 15:40:481635 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
[email protected]82918cc2010-08-25 17:24:501636 STATE_CASE(STATE_CREATE_STREAM);
1637 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
[email protected]daddea62012-09-19 05:51:131638 STATE_CASE(STATE_INIT_REQUEST_BODY);
1639 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
[email protected]4875ba12011-03-30 22:31:511640 STATE_CASE(STATE_BUILD_REQUEST);
1641 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
[email protected]aef04272010-06-28 18:03:041642 STATE_CASE(STATE_SEND_REQUEST);
1643 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1644 STATE_CASE(STATE_READ_HEADERS);
1645 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
[email protected]aef04272010-06-28 18:03:041646 STATE_CASE(STATE_READ_BODY);
1647 STATE_CASE(STATE_READ_BODY_COMPLETE);
1648 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1649 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
[email protected]aef04272010-06-28 18:03:041650 STATE_CASE(STATE_NONE);
1651 default:
[email protected]d8eb84242010-09-25 02:25:061652 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1653 state);
[email protected]aef04272010-06-28 18:03:041654 break;
1655 }
1656 return description;
1657}
1658
1659#undef STATE_CASE
1660
[email protected]c3b35c22008-09-27 03:19:421661} // namespace net