Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / net / http / http_network_transaction.cc
blob0733fdf4187a309b11cfb5eed5eb54228b856c00
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_network_transaction.h"
7 #include <set>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/format_macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/field_trial.h"
16 #include "base/metrics/histogram_macros.h"
17 #include "base/metrics/sparse_histogram.h"
18 #include "base/profiler/scoped_tracker.h"
19 #include "base/stl_util.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/time/time.h"
24 #include "base/values.h"
25 #include "build/build_config.h"
26 #include "net/base/auth.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/load_timing_info.h"
31 #include "net/base/net_errors.h"
32 #include "net/base/net_util.h"
33 #include "net/base/upload_data_stream.h"
34 #include "net/http/http_auth.h"
35 #include "net/http/http_auth_handler.h"
36 #include "net/http/http_auth_handler_factory.h"
37 #include "net/http/http_basic_stream.h"
38 #include "net/http/http_chunked_decoder.h"
39 #include "net/http/http_network_session.h"
40 #include "net/http/http_proxy_client_socket.h"
41 #include "net/http/http_proxy_client_socket_pool.h"
42 #include "net/http/http_request_headers.h"
43 #include "net/http/http_request_info.h"
44 #include "net/http/http_response_headers.h"
45 #include "net/http/http_response_info.h"
46 #include "net/http/http_server_properties.h"
47 #include "net/http/http_status_code.h"
48 #include "net/http/http_stream.h"
49 #include "net/http/http_stream_factory.h"
50 #include "net/http/http_util.h"
51 #include "net/http/transport_security_state.h"
52 #include "net/http/url_security_manager.h"
53 #include "net/socket/client_socket_factory.h"
54 #include "net/socket/socks_client_socket_pool.h"
55 #include "net/socket/ssl_client_socket.h"
56 #include "net/socket/ssl_client_socket_pool.h"
57 #include "net/socket/transport_client_socket_pool.h"
58 #include "net/spdy/spdy_http_stream.h"
59 #include "net/spdy/spdy_session.h"
60 #include "net/spdy/spdy_session_pool.h"
61 #include "net/ssl/ssl_cert_request_info.h"
62 #include "net/ssl/ssl_connection_status_flags.h"
63 #include "url/gurl.h"
64 #include "url/url_canon.h"
66 namespace net {
68 namespace {
70 void ProcessAlternativeServices(HttpNetworkSession* session,
71 const HttpResponseHeaders& headers,
72 const HostPortPair& http_host_port_pair) {
73 if (session->params().use_alternative_services &&
74 headers.HasHeader(kAlternativeServiceHeader)) {
75 std::string alternative_service_str;
76 headers.GetNormalizedHeader(kAlternativeServiceHeader,
77 &alternative_service_str);
78 session->http_stream_factory()->ProcessAlternativeService(
79 session->http_server_properties(), alternative_service_str,
80 http_host_port_pair, *session);
81 // If there is an "Alt-Svc" header, then ignore "Alternate-Protocol".
82 return;
85 if (!headers.HasHeader(kAlternateProtocolHeader))
86 return;
88 std::vector<std::string> alternate_protocol_values;
89 void* iter = NULL;
90 std::string alternate_protocol_str;
91 while (headers.EnumerateHeader(&iter, kAlternateProtocolHeader,
92 &alternate_protocol_str)) {
93 base::TrimWhitespaceASCII(alternate_protocol_str, base::TRIM_ALL,
94 &alternate_protocol_str);
95 if (!alternate_protocol_str.empty()) {
96 alternate_protocol_values.push_back(alternate_protocol_str);
100 session->http_stream_factory()->ProcessAlternateProtocol(
101 session->http_server_properties(),
102 alternate_protocol_values,
103 http_host_port_pair,
104 *session);
107 scoped_ptr<base::Value> NetLogSSLVersionFallbackCallback(
108 const GURL* url,
109 int net_error,
110 SSLFailureState ssl_failure_state,
111 uint16 version_before,
112 uint16 version_after,
113 NetLogCaptureMode /* capture_mode */) {
114 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
115 dict->SetString("host_and_port", GetHostAndPort(*url));
116 dict->SetInteger("net_error", net_error);
117 dict->SetInteger("ssl_failure_state", ssl_failure_state);
118 dict->SetInteger("version_before", version_before);
119 dict->SetInteger("version_after", version_after);
120 return dict.Pass();
123 scoped_ptr<base::Value> NetLogSSLCipherFallbackCallback(
124 const GURL* url,
125 int net_error,
126 NetLogCaptureMode /* capture_mode */) {
127 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
128 dict->SetString("host_and_port", GetHostAndPort(*url));
129 dict->SetInteger("net_error", net_error);
130 return dict.Pass();
133 } // namespace
135 //-----------------------------------------------------------------------------
137 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
138 HttpNetworkSession* session)
139 : pending_auth_target_(HttpAuth::AUTH_NONE),
140 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
141 base::Unretained(this))),
142 session_(session),
143 request_(NULL),
144 priority_(priority),
145 headers_valid_(false),
146 server_ssl_failure_state_(SSL_FAILURE_NONE),
147 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK),
148 fallback_failure_state_(SSL_FAILURE_NONE),
149 request_headers_(),
150 read_buf_len_(0),
151 total_received_bytes_(0),
152 next_state_(STATE_NONE),
153 establishing_tunnel_(false),
154 websocket_handshake_stream_base_create_helper_(NULL) {
155 session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
156 session->GetNextProtos(&server_ssl_config_.next_protos);
157 proxy_ssl_config_ = server_ssl_config_;
160 HttpNetworkTransaction::~HttpNetworkTransaction() {
161 if (stream_.get()) {
162 HttpResponseHeaders* headers = GetResponseHeaders();
163 // TODO(mbelshe): The stream_ should be able to compute whether or not the
164 // stream should be kept alive. No reason to compute here
165 // and pass it in.
166 bool try_to_keep_alive =
167 next_state_ == STATE_NONE &&
168 stream_->CanFindEndOfResponse() &&
169 (!headers || headers->IsKeepAlive());
170 if (!try_to_keep_alive) {
171 stream_->Close(true /* not reusable */);
172 } else {
173 if (stream_->IsResponseBodyComplete()) {
174 // If the response body is complete, we can just reuse the socket.
175 stream_->Close(false /* reusable */);
176 } else if (stream_->IsSpdyHttpStream()) {
177 // Doesn't really matter for SpdyHttpStream. Just close it.
178 stream_->Close(true /* not reusable */);
179 } else {
180 // Otherwise, we try to drain the response body.
181 HttpStream* stream = stream_.release();
182 stream->Drain(session_);
187 if (request_ && request_->upload_data_stream)
188 request_->upload_data_stream->Reset(); // Invalidate pending callbacks.
191 int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
192 const CompletionCallback& callback,
193 const BoundNetLog& net_log) {
194 net_log_ = net_log;
195 request_ = request_info;
197 if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
198 server_ssl_config_.rev_checking_enabled = false;
199 proxy_ssl_config_.rev_checking_enabled = false;
202 if (request_->load_flags & LOAD_PREFETCH)
203 response_.unused_since_prefetch = true;
205 // Channel ID is disabled if privacy mode is enabled for this request.
206 if (request_->privacy_mode == PRIVACY_MODE_ENABLED)
207 server_ssl_config_.channel_id_enabled = false;
209 next_state_ = STATE_NOTIFY_BEFORE_CREATE_STREAM;
210 int rv = DoLoop(OK);
211 if (rv == ERR_IO_PENDING)
212 callback_ = callback;
213 return rv;
216 int HttpNetworkTransaction::RestartIgnoringLastError(
217 const CompletionCallback& callback) {
218 DCHECK(!stream_.get());
219 DCHECK(!stream_request_.get());
220 DCHECK_EQ(STATE_NONE, next_state_);
222 next_state_ = STATE_CREATE_STREAM;
224 int rv = DoLoop(OK);
225 if (rv == ERR_IO_PENDING)
226 callback_ = callback;
227 return rv;
230 int HttpNetworkTransaction::RestartWithCertificate(
231 X509Certificate* client_cert, const CompletionCallback& callback) {
232 // In HandleCertificateRequest(), we always tear down existing stream
233 // requests to force a new connection. So we shouldn't have one here.
234 DCHECK(!stream_request_.get());
235 DCHECK(!stream_.get());
236 DCHECK_EQ(STATE_NONE, next_state_);
238 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
239 &proxy_ssl_config_ : &server_ssl_config_;
240 ssl_config->send_client_cert = true;
241 ssl_config->client_cert = client_cert;
242 session_->ssl_client_auth_cache()->Add(
243 response_.cert_request_info->host_and_port, client_cert);
244 // Reset the other member variables.
245 // Note: this is necessary only with SSL renegotiation.
246 ResetStateForRestart();
247 next_state_ = STATE_CREATE_STREAM;
248 int rv = DoLoop(OK);
249 if (rv == ERR_IO_PENDING)
250 callback_ = callback;
251 return rv;
254 int HttpNetworkTransaction::RestartWithAuth(
255 const AuthCredentials& credentials, const CompletionCallback& callback) {
256 HttpAuth::Target target = pending_auth_target_;
257 if (target == HttpAuth::AUTH_NONE) {
258 NOTREACHED();
259 return ERR_UNEXPECTED;
261 pending_auth_target_ = HttpAuth::AUTH_NONE;
263 auth_controllers_[target]->ResetAuth(credentials);
265 DCHECK(callback_.is_null());
267 int rv = OK;
268 if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
269 // In this case, we've gathered credentials for use with proxy
270 // authentication of a tunnel.
271 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
272 DCHECK(stream_request_ != NULL);
273 auth_controllers_[target] = NULL;
274 ResetStateForRestart();
275 rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
276 } else {
277 // In this case, we've gathered credentials for the server or the proxy
278 // but it is not during the tunneling phase.
279 DCHECK(stream_request_ == NULL);
280 PrepareForAuthRestart(target);
281 rv = DoLoop(OK);
284 if (rv == ERR_IO_PENDING)
285 callback_ = callback;
286 return rv;
289 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
290 DCHECK(HaveAuth(target));
291 DCHECK(!stream_request_.get());
293 bool keep_alive = false;
294 // Even if the server says the connection is keep-alive, we have to be
295 // able to find the end of each response in order to reuse the connection.
296 if (GetResponseHeaders()->IsKeepAlive() &&
297 stream_->CanFindEndOfResponse()) {
298 // If the response body hasn't been completely read, we need to drain
299 // it first.
300 if (!stream_->IsResponseBodyComplete()) {
301 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
302 read_buf_ = new IOBuffer(kDrainBodyBufferSize); // A bit bucket.
303 read_buf_len_ = kDrainBodyBufferSize;
304 return;
306 keep_alive = true;
309 // We don't need to drain the response body, so we act as if we had drained
310 // the response body.
311 DidDrainBodyForAuthRestart(keep_alive);
314 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
315 DCHECK(!stream_request_.get());
317 if (stream_.get()) {
318 total_received_bytes_ += stream_->GetTotalReceivedBytes();
319 HttpStream* new_stream = NULL;
320 if (keep_alive && stream_->IsConnectionReusable()) {
321 // We should call connection_->set_idle_time(), but this doesn't occur
322 // often enough to be worth the trouble.
323 stream_->SetConnectionReused();
324 new_stream = stream_->RenewStreamForAuth();
327 if (!new_stream) {
328 // Close the stream and mark it as not_reusable. Even in the
329 // keep_alive case, we've determined that the stream_ is not
330 // reusable if new_stream is NULL.
331 stream_->Close(true);
332 next_state_ = STATE_CREATE_STREAM;
333 } else {
334 // Renewed streams shouldn't carry over received bytes.
335 DCHECK_EQ(0, new_stream->GetTotalReceivedBytes());
336 next_state_ = STATE_INIT_STREAM;
338 stream_.reset(new_stream);
341 // Reset the other member variables.
342 ResetStateForAuthRestart();
345 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
346 return pending_auth_target_ != HttpAuth::AUTH_NONE &&
347 HaveAuth(pending_auth_target_);
350 int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
351 const CompletionCallback& callback) {
352 DCHECK(buf);
353 DCHECK_LT(0, buf_len);
355 State next_state = STATE_NONE;
357 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
358 if (headers_valid_ && headers.get() && stream_request_.get()) {
359 // We're trying to read the body of the response but we're still trying
360 // to establish an SSL tunnel through an HTTP proxy. We can't read these
361 // bytes when establishing a tunnel because they might be controlled by
362 // an active network attacker. We don't worry about this for HTTP
363 // because an active network attacker can already control HTTP sessions.
364 // We reach this case when the user cancels a 407 proxy auth prompt. We
365 // also don't worry about this for an HTTPS Proxy, because the
366 // communication with the proxy is secure.
367 // See http://crbug.com/8473.
368 DCHECK(proxy_info_.is_http() || proxy_info_.is_https());
369 DCHECK_EQ(headers->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED);
370 LOG(WARNING) << "Blocked proxy response with status "
371 << headers->response_code() << " to CONNECT request for "
372 << GetHostAndPort(request_->url) << ".";
373 return ERR_TUNNEL_CONNECTION_FAILED;
376 // Are we using SPDY or HTTP?
377 next_state = STATE_READ_BODY;
379 read_buf_ = buf;
380 read_buf_len_ = buf_len;
382 next_state_ = next_state;
383 int rv = DoLoop(OK);
384 if (rv == ERR_IO_PENDING)
385 callback_ = callback;
386 return rv;
389 void HttpNetworkTransaction::StopCaching() {}
391 bool HttpNetworkTransaction::GetFullRequestHeaders(
392 HttpRequestHeaders* headers) const {
393 // TODO(ttuttle): Make sure we've populated request_headers_.
394 *headers = request_headers_;
395 return true;
398 int64 HttpNetworkTransaction::GetTotalReceivedBytes() const {
399 int64 total_received_bytes = total_received_bytes_;
400 if (stream_)
401 total_received_bytes += stream_->GetTotalReceivedBytes();
402 return total_received_bytes;
405 void HttpNetworkTransaction::DoneReading() {}
407 const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
408 return &response_;
411 LoadState HttpNetworkTransaction::GetLoadState() const {
412 // TODO(wtc): Define a new LoadState value for the
413 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
414 switch (next_state_) {
415 case STATE_CREATE_STREAM:
416 return LOAD_STATE_WAITING_FOR_DELEGATE;
417 case STATE_CREATE_STREAM_COMPLETE:
418 return stream_request_->GetLoadState();
419 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
420 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
421 case STATE_SEND_REQUEST_COMPLETE:
422 return LOAD_STATE_SENDING_REQUEST;
423 case STATE_READ_HEADERS_COMPLETE:
424 return LOAD_STATE_WAITING_FOR_RESPONSE;
425 case STATE_READ_BODY_COMPLETE:
426 return LOAD_STATE_READING_RESPONSE;
427 default:
428 return LOAD_STATE_IDLE;
432 UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
433 if (!stream_.get())
434 return UploadProgress();
436 return stream_->GetUploadProgress();
439 void HttpNetworkTransaction::SetQuicServerInfo(
440 QuicServerInfo* quic_server_info) {}
442 bool HttpNetworkTransaction::GetLoadTimingInfo(
443 LoadTimingInfo* load_timing_info) const {
444 if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
445 return false;
447 load_timing_info->proxy_resolve_start =
448 proxy_info_.proxy_resolve_start_time();
449 load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
450 load_timing_info->send_start = send_start_time_;
451 load_timing_info->send_end = send_end_time_;
452 return true;
455 void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
456 priority_ = priority;
457 if (stream_request_)
458 stream_request_->SetPriority(priority);
459 if (stream_)
460 stream_->SetPriority(priority);
463 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
464 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
465 websocket_handshake_stream_base_create_helper_ = create_helper;
468 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
469 const BeforeNetworkStartCallback& callback) {
470 before_network_start_callback_ = callback;
473 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
474 const BeforeProxyHeadersSentCallback& callback) {
475 before_proxy_headers_sent_callback_ = callback;
478 int HttpNetworkTransaction::ResumeNetworkStart() {
479 DCHECK_EQ(next_state_, STATE_CREATE_STREAM);
480 return DoLoop(OK);
483 void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
484 const ProxyInfo& used_proxy_info,
485 HttpStream* stream) {
486 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
487 DCHECK(stream_request_.get());
489 if (stream_)
490 total_received_bytes_ += stream_->GetTotalReceivedBytes();
491 stream_.reset(stream);
492 server_ssl_config_ = used_ssl_config;
493 proxy_info_ = used_proxy_info;
494 response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
495 response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
496 stream_request_->protocol_negotiated());
497 response_.was_fetched_via_spdy = stream_request_->using_spdy();
498 response_.was_fetched_via_proxy = !proxy_info_.is_direct();
499 if (response_.was_fetched_via_proxy && !proxy_info_.is_empty())
500 response_.proxy_server = proxy_info_.proxy_server().host_port_pair();
501 OnIOComplete(OK);
504 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
505 const SSLConfig& used_ssl_config,
506 const ProxyInfo& used_proxy_info,
507 WebSocketHandshakeStreamBase* stream) {
508 OnStreamReady(used_ssl_config, used_proxy_info, stream);
511 void HttpNetworkTransaction::OnStreamFailed(int result,
512 const SSLConfig& used_ssl_config,
513 SSLFailureState ssl_failure_state) {
514 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
515 DCHECK_NE(OK, result);
516 DCHECK(stream_request_.get());
517 DCHECK(!stream_.get());
518 server_ssl_config_ = used_ssl_config;
519 server_ssl_failure_state_ = ssl_failure_state;
521 OnIOComplete(result);
524 void HttpNetworkTransaction::OnCertificateError(
525 int result,
526 const SSLConfig& used_ssl_config,
527 const SSLInfo& ssl_info) {
528 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
529 DCHECK_NE(OK, result);
530 DCHECK(stream_request_.get());
531 DCHECK(!stream_.get());
533 response_.ssl_info = ssl_info;
534 server_ssl_config_ = used_ssl_config;
536 // TODO(mbelshe): For now, we're going to pass the error through, and that
537 // will close the stream_request in all cases. This means that we're always
538 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
539 // good and the user chooses to ignore the error. This is not ideal, but not
540 // the end of the world either.
542 OnIOComplete(result);
545 void HttpNetworkTransaction::OnNeedsProxyAuth(
546 const HttpResponseInfo& proxy_response,
547 const SSLConfig& used_ssl_config,
548 const ProxyInfo& used_proxy_info,
549 HttpAuthController* auth_controller) {
550 DCHECK(stream_request_.get());
551 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
553 establishing_tunnel_ = true;
554 response_.headers = proxy_response.headers;
555 response_.auth_challenge = proxy_response.auth_challenge;
556 headers_valid_ = true;
557 server_ssl_config_ = used_ssl_config;
558 proxy_info_ = used_proxy_info;
560 auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
561 pending_auth_target_ = HttpAuth::AUTH_PROXY;
563 DoCallback(OK);
566 void HttpNetworkTransaction::OnNeedsClientAuth(
567 const SSLConfig& used_ssl_config,
568 SSLCertRequestInfo* cert_info) {
569 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
571 server_ssl_config_ = used_ssl_config;
572 response_.cert_request_info = cert_info;
573 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
576 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
577 const HttpResponseInfo& response_info,
578 const SSLConfig& used_ssl_config,
579 const ProxyInfo& used_proxy_info,
580 HttpStream* stream) {
581 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
583 CopyConnectionAttemptsFromStreamRequest();
585 headers_valid_ = true;
586 response_ = response_info;
587 server_ssl_config_ = used_ssl_config;
588 proxy_info_ = used_proxy_info;
589 if (stream_)
590 total_received_bytes_ += stream_->GetTotalReceivedBytes();
591 stream_.reset(stream);
592 stream_request_.reset(); // we're done with the stream request
593 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
596 void HttpNetworkTransaction::GetConnectionAttempts(
597 ConnectionAttempts* out) const {
598 *out = connection_attempts_;
601 bool HttpNetworkTransaction::IsSecureRequest() const {
602 return request_->url.SchemeIsCryptographic();
605 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
606 return (proxy_info_.is_http() || proxy_info_.is_https() ||
607 proxy_info_.is_quic()) &&
608 !(request_->url.SchemeIs("https") || request_->url.SchemeIsWSOrWSS());
611 void HttpNetworkTransaction::DoCallback(int rv) {
612 DCHECK_NE(rv, ERR_IO_PENDING);
613 DCHECK(!callback_.is_null());
615 // Since Run may result in Read being called, clear user_callback_ up front.
616 CompletionCallback c = callback_;
617 callback_.Reset();
618 c.Run(rv);
621 void HttpNetworkTransaction::OnIOComplete(int result) {
622 int rv = DoLoop(result);
623 if (rv != ERR_IO_PENDING)
624 DoCallback(rv);
627 int HttpNetworkTransaction::DoLoop(int result) {
628 DCHECK(next_state_ != STATE_NONE);
630 int rv = result;
631 do {
632 State state = next_state_;
633 next_state_ = STATE_NONE;
634 switch (state) {
635 case STATE_NOTIFY_BEFORE_CREATE_STREAM:
636 DCHECK_EQ(OK, rv);
637 rv = DoNotifyBeforeCreateStream();
638 break;
639 case STATE_CREATE_STREAM:
640 DCHECK_EQ(OK, rv);
641 rv = DoCreateStream();
642 break;
643 case STATE_CREATE_STREAM_COMPLETE:
644 rv = DoCreateStreamComplete(rv);
645 break;
646 case STATE_INIT_STREAM:
647 DCHECK_EQ(OK, rv);
648 rv = DoInitStream();
649 break;
650 case STATE_INIT_STREAM_COMPLETE:
651 rv = DoInitStreamComplete(rv);
652 break;
653 case STATE_GENERATE_PROXY_AUTH_TOKEN:
654 DCHECK_EQ(OK, rv);
655 rv = DoGenerateProxyAuthToken();
656 break;
657 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
658 rv = DoGenerateProxyAuthTokenComplete(rv);
659 break;
660 case STATE_GENERATE_SERVER_AUTH_TOKEN:
661 DCHECK_EQ(OK, rv);
662 rv = DoGenerateServerAuthToken();
663 break;
664 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
665 rv = DoGenerateServerAuthTokenComplete(rv);
666 break;
667 case STATE_INIT_REQUEST_BODY:
668 DCHECK_EQ(OK, rv);
669 rv = DoInitRequestBody();
670 break;
671 case STATE_INIT_REQUEST_BODY_COMPLETE:
672 rv = DoInitRequestBodyComplete(rv);
673 break;
674 case STATE_BUILD_REQUEST:
675 DCHECK_EQ(OK, rv);
676 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
677 rv = DoBuildRequest();
678 break;
679 case STATE_BUILD_REQUEST_COMPLETE:
680 rv = DoBuildRequestComplete(rv);
681 break;
682 case STATE_SEND_REQUEST:
683 DCHECK_EQ(OK, rv);
684 rv = DoSendRequest();
685 break;
686 case STATE_SEND_REQUEST_COMPLETE:
687 rv = DoSendRequestComplete(rv);
688 net_log_.EndEventWithNetErrorCode(
689 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
690 break;
691 case STATE_READ_HEADERS:
692 DCHECK_EQ(OK, rv);
693 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
694 rv = DoReadHeaders();
695 break;
696 case STATE_READ_HEADERS_COMPLETE:
697 rv = DoReadHeadersComplete(rv);
698 net_log_.EndEventWithNetErrorCode(
699 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
700 break;
701 case STATE_READ_BODY:
702 DCHECK_EQ(OK, rv);
703 net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
704 rv = DoReadBody();
705 break;
706 case STATE_READ_BODY_COMPLETE:
707 rv = DoReadBodyComplete(rv);
708 net_log_.EndEventWithNetErrorCode(
709 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
710 break;
711 case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
712 DCHECK_EQ(OK, rv);
713 net_log_.BeginEvent(
714 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
715 rv = DoDrainBodyForAuthRestart();
716 break;
717 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
718 rv = DoDrainBodyForAuthRestartComplete(rv);
719 net_log_.EndEventWithNetErrorCode(
720 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
721 break;
722 default:
723 NOTREACHED() << "bad state";
724 rv = ERR_FAILED;
725 break;
727 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
729 return rv;
732 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
733 next_state_ = STATE_CREATE_STREAM;
734 bool defer = false;
735 if (!before_network_start_callback_.is_null())
736 before_network_start_callback_.Run(&defer);
737 if (!defer)
738 return OK;
739 return ERR_IO_PENDING;
742 int HttpNetworkTransaction::DoCreateStream() {
743 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
744 tracked_objects::ScopedTracker tracking_profile(
745 FROM_HERE_WITH_EXPLICIT_FUNCTION(
746 "424359 HttpNetworkTransaction::DoCreateStream"));
748 response_.network_accessed = true;
750 next_state_ = STATE_CREATE_STREAM_COMPLETE;
751 if (ForWebSocketHandshake()) {
752 stream_request_.reset(
753 session_->http_stream_factory_for_websocket()
754 ->RequestWebSocketHandshakeStream(
755 *request_,
756 priority_,
757 server_ssl_config_,
758 proxy_ssl_config_,
759 this,
760 websocket_handshake_stream_base_create_helper_,
761 net_log_));
762 } else {
763 stream_request_.reset(
764 session_->http_stream_factory()->RequestStream(
765 *request_,
766 priority_,
767 server_ssl_config_,
768 proxy_ssl_config_,
769 this,
770 net_log_));
772 DCHECK(stream_request_.get());
773 return ERR_IO_PENDING;
776 int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
777 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
778 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
779 // which resets the stream request first. Therefore, we have to grab the
780 // connection attempts in *that* function instead of here in that case.
781 if (result != ERR_HTTPS_PROXY_TUNNEL_RESPONSE)
782 CopyConnectionAttemptsFromStreamRequest();
784 if (request_->url.SchemeIsCryptographic())
785 RecordSSLFallbackMetrics(result);
787 if (result == OK) {
788 next_state_ = STATE_INIT_STREAM;
789 DCHECK(stream_.get());
790 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
791 result = HandleCertificateRequest(result);
792 } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
793 // Return OK and let the caller read the proxy's error page
794 next_state_ = STATE_NONE;
795 return OK;
796 } else if (result == ERR_HTTP_1_1_REQUIRED ||
797 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
798 return HandleHttp11Required(result);
801 // Handle possible handshake errors that may have occurred if the stream
802 // used SSL for one or more of the layers.
803 result = HandleSSLHandshakeError(result);
805 // At this point we are done with the stream_request_.
806 stream_request_.reset();
807 return result;
810 int HttpNetworkTransaction::DoInitStream() {
811 DCHECK(stream_.get());
812 next_state_ = STATE_INIT_STREAM_COMPLETE;
813 return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
816 int HttpNetworkTransaction::DoInitStreamComplete(int result) {
817 if (result == OK) {
818 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
819 } else {
820 if (result < 0)
821 result = HandleIOError(result);
823 // The stream initialization failed, so this stream will never be useful.
824 if (stream_)
825 total_received_bytes_ += stream_->GetTotalReceivedBytes();
826 stream_.reset();
829 return result;
832 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
833 next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
834 if (!ShouldApplyProxyAuth())
835 return OK;
836 HttpAuth::Target target = HttpAuth::AUTH_PROXY;
837 if (!auth_controllers_[target].get())
838 auth_controllers_[target] =
839 new HttpAuthController(target,
840 AuthURL(target),
841 session_->http_auth_cache(),
842 session_->http_auth_handler_factory());
843 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
844 io_callback_,
845 net_log_);
848 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
849 DCHECK_NE(ERR_IO_PENDING, rv);
850 if (rv == OK)
851 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
852 return rv;
855 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
856 next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
857 HttpAuth::Target target = HttpAuth::AUTH_SERVER;
858 if (!auth_controllers_[target].get()) {
859 auth_controllers_[target] =
860 new HttpAuthController(target,
861 AuthURL(target),
862 session_->http_auth_cache(),
863 session_->http_auth_handler_factory());
864 if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
865 auth_controllers_[target]->DisableEmbeddedIdentity();
867 if (!ShouldApplyServerAuth())
868 return OK;
869 return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
870 io_callback_,
871 net_log_);
874 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
875 DCHECK_NE(ERR_IO_PENDING, rv);
876 if (rv == OK)
877 next_state_ = STATE_INIT_REQUEST_BODY;
878 return rv;
881 void HttpNetworkTransaction::BuildRequestHeaders(
882 bool using_http_proxy_without_tunnel) {
883 request_headers_.SetHeader(HttpRequestHeaders::kHost,
884 GetHostAndOptionalPort(request_->url));
886 // For compat with HTTP/1.0 servers and proxies:
887 if (using_http_proxy_without_tunnel) {
888 request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
889 "keep-alive");
890 } else {
891 request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
894 // Add a content length header?
895 if (request_->upload_data_stream) {
896 if (request_->upload_data_stream->is_chunked()) {
897 request_headers_.SetHeader(
898 HttpRequestHeaders::kTransferEncoding, "chunked");
899 } else {
900 request_headers_.SetHeader(
901 HttpRequestHeaders::kContentLength,
902 base::Uint64ToString(request_->upload_data_stream->size()));
904 } else if (request_->method == "POST" || request_->method == "PUT") {
905 // An empty POST/PUT request still needs a content length. As for HEAD,
906 // IE and Safari also add a content length header. Presumably it is to
907 // support sending a HEAD request to an URL that only expects to be sent a
908 // POST or some other method that normally would have a message body.
909 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
910 // specify that it should not be sent due to undefined behavior.
911 request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
914 // Honor load flags that impact proxy caches.
915 if (request_->load_flags & LOAD_BYPASS_CACHE) {
916 request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
917 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
918 } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
919 request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
922 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
923 auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
924 &request_headers_);
925 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
926 auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
927 &request_headers_);
929 request_headers_.MergeFrom(request_->extra_headers);
931 if (using_http_proxy_without_tunnel &&
932 !before_proxy_headers_sent_callback_.is_null())
933 before_proxy_headers_sent_callback_.Run(proxy_info_, &request_headers_);
935 response_.did_use_http_auth =
936 request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
937 request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
940 int HttpNetworkTransaction::DoInitRequestBody() {
941 next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
942 int rv = OK;
943 if (request_->upload_data_stream)
944 rv = request_->upload_data_stream->Init(io_callback_);
945 return rv;
948 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
949 if (result == OK)
950 next_state_ = STATE_BUILD_REQUEST;
951 return result;
954 int HttpNetworkTransaction::DoBuildRequest() {
955 next_state_ = STATE_BUILD_REQUEST_COMPLETE;
956 headers_valid_ = false;
958 // This is constructed lazily (instead of within our Start method), so that
959 // we have proxy info available.
960 if (request_headers_.IsEmpty()) {
961 bool using_http_proxy_without_tunnel = UsingHttpProxyWithoutTunnel();
962 BuildRequestHeaders(using_http_proxy_without_tunnel);
965 return OK;
968 int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
969 if (result == OK)
970 next_state_ = STATE_SEND_REQUEST;
971 return result;
974 int HttpNetworkTransaction::DoSendRequest() {
975 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
976 tracked_objects::ScopedTracker tracking_profile(
977 FROM_HERE_WITH_EXPLICIT_FUNCTION(
978 "424359 HttpNetworkTransaction::DoSendRequest"));
980 send_start_time_ = base::TimeTicks::Now();
981 next_state_ = STATE_SEND_REQUEST_COMPLETE;
983 return stream_->SendRequest(request_headers_, &response_, io_callback_);
986 int HttpNetworkTransaction::DoSendRequestComplete(int result) {
987 send_end_time_ = base::TimeTicks::Now();
988 if (result < 0)
989 return HandleIOError(result);
990 next_state_ = STATE_READ_HEADERS;
991 return OK;
994 int HttpNetworkTransaction::DoReadHeaders() {
995 next_state_ = STATE_READ_HEADERS_COMPLETE;
996 return stream_->ReadResponseHeaders(io_callback_);
999 int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
1000 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1001 // due to SSL renegotiation.
1002 if (IsCertificateError(result)) {
1003 // We don't handle a certificate error during SSL renegotiation, so we
1004 // have to return an error that's not in the certificate error range
1005 // (-2xx).
1006 LOG(ERROR) << "Got a server certificate with error " << result
1007 << " during SSL renegotiation";
1008 result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
1009 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1010 // TODO(wtc): Need a test case for this code path!
1011 DCHECK(stream_.get());
1012 DCHECK(IsSecureRequest());
1013 response_.cert_request_info = new SSLCertRequestInfo;
1014 stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
1015 result = HandleCertificateRequest(result);
1016 if (result == OK)
1017 return result;
1020 if (result == ERR_HTTP_1_1_REQUIRED ||
1021 result == ERR_PROXY_HTTP_1_1_REQUIRED) {
1022 return HandleHttp11Required(result);
1025 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1026 // response headers were received, we do the best we can to make sense of it
1027 // and send it back up the stack.
1029 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1030 // bizarre for SPDY. Assuming this logic is useful at all.
1031 // TODO(davidben): Bubble the error code up so we do not cache?
1032 if (result == ERR_CONNECTION_CLOSED && response_.headers.get())
1033 result = OK;
1035 if (result < 0)
1036 return HandleIOError(result);
1038 DCHECK(response_.headers.get());
1040 // On a 408 response from the server ("Request Timeout") on a stale socket,
1041 // retry the request.
1042 // Headers can be NULL because of http://crbug.com/384554.
1043 if (response_.headers.get() && response_.headers->response_code() == 408 &&
1044 stream_->IsConnectionReused()) {
1045 net_log_.AddEventWithNetErrorCode(
1046 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR,
1047 response_.headers->response_code());
1048 // This will close the socket - it would be weird to try and reuse it, even
1049 // if the server doesn't actually close it.
1050 ResetConnectionAndRequestForResend();
1051 return OK;
1054 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1055 if (request_->load_flags & LOAD_MAIN_FRAME) {
1056 const int response_code = response_.headers->response_code();
1057 UMA_HISTOGRAM_ENUMERATION(
1058 "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
1061 net_log_.AddEvent(
1062 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
1063 base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
1065 if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1066 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1067 // indicates a buggy server. See:
1068 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1069 if (request_->method == "PUT")
1070 return ERR_METHOD_NOT_SUPPORTED;
1073 // Check for an intermediate 100 Continue response. An origin server is
1074 // allowed to send this response even if we didn't ask for it, so we just
1075 // need to skip over it.
1076 // We treat any other 1xx in this same way (although in practice getting
1077 // a 1xx that isn't a 100 is rare).
1078 // Unless this is a WebSocket request, in which case we pass it on up.
1079 if (response_.headers->response_code() / 100 == 1 &&
1080 !ForWebSocketHandshake()) {
1081 response_.headers = new HttpResponseHeaders(std::string());
1082 next_state_ = STATE_READ_HEADERS;
1083 return OK;
1086 ProcessAlternativeServices(session_, *response_.headers.get(),
1087 HostPortPair::FromURL(request_->url));
1089 int rv = HandleAuthChallenge();
1090 if (rv != OK)
1091 return rv;
1093 if (IsSecureRequest())
1094 stream_->GetSSLInfo(&response_.ssl_info);
1096 headers_valid_ = true;
1097 return OK;
1100 int HttpNetworkTransaction::DoReadBody() {
1101 DCHECK(read_buf_.get());
1102 DCHECK_GT(read_buf_len_, 0);
1103 DCHECK(stream_ != NULL);
1105 next_state_ = STATE_READ_BODY_COMPLETE;
1106 return stream_->ReadResponseBody(
1107 read_buf_.get(), read_buf_len_, io_callback_);
1110 int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1111 // We are done with the Read call.
1112 bool done = false;
1113 if (result <= 0) {
1114 DCHECK_NE(ERR_IO_PENDING, result);
1115 done = true;
1118 bool keep_alive = false;
1119 if (stream_->IsResponseBodyComplete()) {
1120 // Note: Just because IsResponseBodyComplete is true, we're not
1121 // necessarily "done". We're only "done" when it is the last
1122 // read on this HttpNetworkTransaction, which will be signified
1123 // by a zero-length read.
1124 // TODO(mbelshe): The keepalive property is really a property of
1125 // the stream. No need to compute it here just to pass back
1126 // to the stream's Close function.
1127 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1128 // ResponseHeaders.
1129 if (stream_->CanFindEndOfResponse()) {
1130 HttpResponseHeaders* headers = GetResponseHeaders();
1131 if (headers)
1132 keep_alive = headers->IsKeepAlive();
1136 // Clean up connection if we are done.
1137 if (done) {
1138 stream_->Close(!keep_alive);
1139 // Note: we don't reset the stream here. We've closed it, but we still
1140 // need it around so that callers can call methods such as
1141 // GetUploadProgress() and have them be meaningful.
1142 // TODO(mbelshe): This means we closed the stream here, and we close it
1143 // again in ~HttpNetworkTransaction. Clean that up.
1145 // The next Read call will return 0 (EOF).
1148 // Clear these to avoid leaving around old state.
1149 read_buf_ = NULL;
1150 read_buf_len_ = 0;
1152 return result;
1155 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1156 // This method differs from DoReadBody only in the next_state_. So we just
1157 // call DoReadBody and override the next_state_. Perhaps there is a more
1158 // elegant way for these two methods to share code.
1159 int rv = DoReadBody();
1160 DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1161 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1162 return rv;
1165 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1166 // the same. Figure out a good way for these two methods to share code.
1167 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
1168 // keep_alive defaults to true because the very reason we're draining the
1169 // response body is to reuse the connection for auth restart.
1170 bool done = false, keep_alive = true;
1171 if (result < 0) {
1172 // Error or closed connection while reading the socket.
1173 done = true;
1174 keep_alive = false;
1175 } else if (stream_->IsResponseBodyComplete()) {
1176 done = true;
1179 if (done) {
1180 DidDrainBodyForAuthRestart(keep_alive);
1181 } else {
1182 // Keep draining.
1183 next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1186 return OK;
1189 int HttpNetworkTransaction::HandleCertificateRequest(int error) {
1190 // There are two paths through which the server can request a certificate
1191 // from us. The first is during the initial handshake, the second is
1192 // during SSL renegotiation.
1194 // In both cases, we want to close the connection before proceeding.
1195 // We do this for two reasons:
1196 // First, we don't want to keep the connection to the server hung for a
1197 // long time while the user selects a certificate.
1198 // Second, even if we did keep the connection open, NSS has a bug where
1199 // restarting the handshake for ClientAuth is currently broken.
1200 DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
1202 if (stream_.get()) {
1203 // Since we already have a stream, we're being called as part of SSL
1204 // renegotiation.
1205 DCHECK(!stream_request_.get());
1206 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1207 stream_->Close(true);
1208 stream_.reset();
1211 // The server is asking for a client certificate during the initial
1212 // handshake.
1213 stream_request_.reset();
1215 // If the user selected one of the certificates in client_certs or declined
1216 // to provide one for this server before, use the past decision
1217 // automatically.
1218 scoped_refptr<X509Certificate> client_cert;
1219 bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1220 response_.cert_request_info->host_and_port, &client_cert);
1221 if (!found_cached_cert)
1222 return error;
1224 // Check that the certificate selected is still a certificate the server
1225 // is likely to accept, based on the criteria supplied in the
1226 // CertificateRequest message.
1227 if (client_cert.get()) {
1228 const std::vector<std::string>& cert_authorities =
1229 response_.cert_request_info->cert_authorities;
1231 bool cert_still_valid = cert_authorities.empty() ||
1232 client_cert->IsIssuedByEncoded(cert_authorities);
1233 if (!cert_still_valid)
1234 return error;
1237 // TODO(davidben): Add a unit test which covers this path; we need to be
1238 // able to send a legitimate certificate and also bypass/clear the
1239 // SSL session cache.
1240 SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1241 &proxy_ssl_config_ : &server_ssl_config_;
1242 ssl_config->send_client_cert = true;
1243 ssl_config->client_cert = client_cert;
1244 next_state_ = STATE_CREATE_STREAM;
1245 // Reset the other member variables.
1246 // Note: this is necessary only with SSL renegotiation.
1247 ResetStateForRestart();
1248 return OK;
1251 int HttpNetworkTransaction::HandleHttp11Required(int error) {
1252 DCHECK(error == ERR_HTTP_1_1_REQUIRED ||
1253 error == ERR_PROXY_HTTP_1_1_REQUIRED);
1255 if (error == ERR_HTTP_1_1_REQUIRED) {
1256 HttpServerProperties::ForceHTTP11(&server_ssl_config_);
1257 } else {
1258 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_);
1260 ResetConnectionAndRequestForResend();
1261 return OK;
1264 void HttpNetworkTransaction::HandleClientAuthError(int error) {
1265 if (server_ssl_config_.send_client_cert &&
1266 (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1267 session_->ssl_client_auth_cache()->Remove(
1268 HostPortPair::FromURL(request_->url));
1272 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1273 // being used, as all of the errors are handled as if they were generated
1274 // by the endpoint host, request_->url, rather than considering if they were
1275 // generated by the SSL proxy. http://crbug.com/69329
1276 int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1277 DCHECK(request_);
1278 HandleClientAuthError(error);
1280 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1281 // reflect servers require a deprecated cipher rather than merely prefer
1282 // it. This, however, has no security benefit until the ciphers are actually
1283 // removed.
1284 if (!server_ssl_config_.enable_deprecated_cipher_suites &&
1285 (error == ERR_SSL_VERSION_OR_CIPHER_MISMATCH ||
1286 error == ERR_CONNECTION_CLOSED || error == ERR_CONNECTION_RESET)) {
1287 net_log_.AddEvent(
1288 NetLog::TYPE_SSL_CIPHER_FALLBACK,
1289 base::Bind(&NetLogSSLCipherFallbackCallback, &request_->url, error));
1290 server_ssl_config_.enable_deprecated_cipher_suites = true;
1291 ResetConnectionAndRequestForResend();
1292 return OK;
1295 bool should_fallback = false;
1296 uint16 version_max = server_ssl_config_.version_max;
1298 switch (error) {
1299 case ERR_CONNECTION_CLOSED:
1300 case ERR_SSL_PROTOCOL_ERROR:
1301 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
1302 if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1303 version_max > server_ssl_config_.version_min) {
1304 // This could be a TLS-intolerant server or a server that chose a
1305 // cipher suite defined only for higher protocol versions (such as
1306 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1307 // back to the next lower version and retry.
1308 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1309 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1310 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1311 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1312 // version_max should match the maximum protocol version supported
1313 // by the SSLClientSocket class.
1314 version_max--;
1316 // Fallback to the lower SSL version.
1317 // While SSL 3.0 fallback should be eliminated because of security
1318 // reasons, there is a high risk of breaking the servers if this is
1319 // done in general.
1320 should_fallback = true;
1322 break;
1323 case ERR_CONNECTION_RESET:
1324 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1325 version_max > server_ssl_config_.version_min) {
1326 // Some network devices that inspect application-layer packets seem to
1327 // inject TCP reset packets to break the connections when they see TLS
1328 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1330 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1331 // 1.2. We don't lose much in this fallback because the explicit IV for
1332 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1333 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1334 // support.
1336 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1337 // to trigger a version fallback in general, especially the TLS 1.0 ->
1338 // SSL 3.0 fallback, which would drop TLS extensions.
1339 version_max--;
1340 should_fallback = true;
1342 break;
1343 case ERR_SSL_BAD_RECORD_MAC_ALERT:
1344 if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1345 version_max > server_ssl_config_.version_min) {
1346 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1347 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1348 // crbug.com/260358. In order to make the fallback as minimal as
1349 // possible, this fallback is only triggered for >= TLS 1.1.
1350 version_max--;
1351 should_fallback = true;
1353 break;
1354 case ERR_SSL_INAPPROPRIATE_FALLBACK:
1355 // The server told us that we should not have fallen back. A buggy server
1356 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1357 // connection. |fallback_error_code_| is initialised to
1358 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1359 error = fallback_error_code_;
1360 break;
1363 if (should_fallback) {
1364 net_log_.AddEvent(
1365 NetLog::TYPE_SSL_VERSION_FALLBACK,
1366 base::Bind(&NetLogSSLVersionFallbackCallback, &request_->url, error,
1367 server_ssl_failure_state_, server_ssl_config_.version_max,
1368 version_max));
1369 fallback_error_code_ = error;
1370 fallback_failure_state_ = server_ssl_failure_state_;
1371 server_ssl_config_.version_max = version_max;
1372 server_ssl_config_.version_fallback = true;
1373 ResetConnectionAndRequestForResend();
1374 error = OK;
1377 return error;
1380 // This method determines whether it is safe to resend the request after an
1381 // IO error. It can only be called in response to request header or body
1382 // write errors or response header read errors. It should not be used in
1383 // other cases, such as a Connect error.
1384 int HttpNetworkTransaction::HandleIOError(int error) {
1385 // Because the peer may request renegotiation with client authentication at
1386 // any time, check and handle client authentication errors.
1387 HandleClientAuthError(error);
1389 switch (error) {
1390 // If we try to reuse a connection that the server is in the process of
1391 // closing, we may end up successfully writing out our request (or a
1392 // portion of our request) only to find a connection error when we try to
1393 // read from (or finish writing to) the socket.
1394 case ERR_CONNECTION_RESET:
1395 case ERR_CONNECTION_CLOSED:
1396 case ERR_CONNECTION_ABORTED:
1397 // There can be a race between the socket pool checking checking whether a
1398 // socket is still connected, receiving the FIN, and sending/reading data
1399 // on a reused socket. If we receive the FIN between the connectedness
1400 // check and writing/reading from the socket, we may first learn the socket
1401 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1402 // likely happen when trying to retrieve its IP address.
1403 // See http://crbug.com/105824 for more details.
1404 case ERR_SOCKET_NOT_CONNECTED:
1405 // If a socket is closed on its initial request, HttpStreamParser returns
1406 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1407 // preconnected but failed to be used before the server timed it out.
1408 case ERR_EMPTY_RESPONSE:
1409 if (ShouldResendRequest()) {
1410 net_log_.AddEventWithNetErrorCode(
1411 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1412 ResetConnectionAndRequestForResend();
1413 error = OK;
1415 break;
1416 case ERR_SPDY_PING_FAILED:
1417 case ERR_SPDY_SERVER_REFUSED_STREAM:
1418 case ERR_QUIC_HANDSHAKE_FAILED:
1419 net_log_.AddEventWithNetErrorCode(
1420 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1421 ResetConnectionAndRequestForResend();
1422 error = OK;
1423 break;
1425 return error;
1428 void HttpNetworkTransaction::ResetStateForRestart() {
1429 ResetStateForAuthRestart();
1430 if (stream_)
1431 total_received_bytes_ += stream_->GetTotalReceivedBytes();
1432 stream_.reset();
1435 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1436 send_start_time_ = base::TimeTicks();
1437 send_end_time_ = base::TimeTicks();
1439 pending_auth_target_ = HttpAuth::AUTH_NONE;
1440 read_buf_ = NULL;
1441 read_buf_len_ = 0;
1442 headers_valid_ = false;
1443 request_headers_.Clear();
1444 response_ = HttpResponseInfo();
1445 establishing_tunnel_ = false;
1448 void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result) {
1449 if (result != OK && result != ERR_SSL_INAPPROPRIATE_FALLBACK)
1450 return;
1452 const std::string& host = request_->url.host();
1453 bool is_google = base::EndsWith(host, "google.com",
1454 base::CompareCase::SENSITIVE) &&
1455 (host.size() == 10 || host[host.size() - 11] == '.');
1456 if (is_google) {
1457 // Some fraction of successful connections use the fallback, but only due to
1458 // a spurious network failure. To estimate this fraction, compare handshakes
1459 // to Google servers which succeed against those that fail with an
1460 // inappropriate_fallback alert. Google servers are known to implement
1461 // FALLBACK_SCSV, so a spurious network failure while connecting would
1462 // trigger the fallback, successfully connect, but fail with this alert.
1463 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1464 result == ERR_SSL_INAPPROPRIATE_FALLBACK);
1467 if (result != OK)
1468 return;
1470 // Note: these values are used in histograms, so new values must be appended.
1471 enum FallbackVersion {
1472 FALLBACK_NONE = 0, // SSL version fallback did not occur.
1473 // Obsolete: FALLBACK_SSL3 = 1,
1474 FALLBACK_TLS1 = 2, // Fell back to TLS 1.0.
1475 FALLBACK_TLS1_1 = 3, // Fell back to TLS 1.1.
1476 FALLBACK_MAX,
1479 FallbackVersion fallback = FALLBACK_NONE;
1480 if (server_ssl_config_.version_fallback) {
1481 switch (server_ssl_config_.version_max) {
1482 case SSL_PROTOCOL_VERSION_TLS1:
1483 fallback = FALLBACK_TLS1;
1484 break;
1485 case SSL_PROTOCOL_VERSION_TLS1_1:
1486 fallback = FALLBACK_TLS1_1;
1487 break;
1488 default:
1489 NOTREACHED();
1492 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback,
1493 FALLBACK_MAX);
1495 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1496 // should be impossible to successfully connect to them with the fallback.
1497 // This helps estimate intolerant locally-configured SSL MITMs.
1498 if (is_google) {
1499 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1500 fallback, FALLBACK_MAX);
1503 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
1504 server_ssl_config_.enable_deprecated_cipher_suites);
1506 if (server_ssl_config_.version_fallback) {
1507 // Record the error code which triggered the fallback and the state the
1508 // handshake was in.
1509 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1510 -fallback_error_code_);
1511 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1512 fallback_failure_state_, SSL_FAILURE_MAX);
1516 HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
1517 return response_.headers.get();
1520 bool HttpNetworkTransaction::ShouldResendRequest() const {
1521 bool connection_is_proven = stream_->IsConnectionReused();
1522 bool has_received_headers = GetResponseHeaders() != NULL;
1524 // NOTE: we resend a request only if we reused a keep-alive connection.
1525 // This automatically prevents an infinite resend loop because we'll run
1526 // out of the cached keep-alive connections eventually.
1527 if (connection_is_proven && !has_received_headers)
1528 return true;
1529 return false;
1532 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1533 if (stream_.get()) {
1534 stream_->Close(true);
1535 stream_.reset();
1538 // We need to clear request_headers_ because it contains the real request
1539 // headers, but we may need to resend the CONNECT request first to recreate
1540 // the SSL tunnel.
1541 request_headers_.Clear();
1542 next_state_ = STATE_CREATE_STREAM; // Resend the request.
1545 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1546 return UsingHttpProxyWithoutTunnel();
1549 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1550 return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
1553 int HttpNetworkTransaction::HandleAuthChallenge() {
1554 scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
1555 DCHECK(headers.get());
1557 int status = headers->response_code();
1558 if (status != HTTP_UNAUTHORIZED &&
1559 status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
1560 return OK;
1561 HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
1562 HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
1563 if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1564 return ERR_UNEXPECTED_PROXY_AUTH;
1566 // This case can trigger when an HTTPS server responds with a "Proxy
1567 // authentication required" status code through a non-authenticating
1568 // proxy.
1569 if (!auth_controllers_[target].get())
1570 return ERR_UNEXPECTED_PROXY_AUTH;
1572 int rv = auth_controllers_[target]->HandleAuthChallenge(
1573 headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1574 net_log_);
1575 if (auth_controllers_[target]->HaveAuthHandler())
1576 pending_auth_target_ = target;
1578 scoped_refptr<AuthChallengeInfo> auth_info =
1579 auth_controllers_[target]->auth_info();
1580 if (auth_info.get())
1581 response_.auth_challenge = auth_info;
1583 return rv;
1586 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1587 return auth_controllers_[target].get() &&
1588 auth_controllers_[target]->HaveAuth();
1591 GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1592 switch (target) {
1593 case HttpAuth::AUTH_PROXY: {
1594 if (!proxy_info_.proxy_server().is_valid() ||
1595 proxy_info_.proxy_server().is_direct()) {
1596 return GURL(); // There is no proxy server.
1598 const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1599 return GURL(scheme +
1600 proxy_info_.proxy_server().host_port_pair().ToString());
1602 case HttpAuth::AUTH_SERVER:
1603 if (ForWebSocketHandshake()) {
1604 const GURL& url = request_->url;
1605 url::Replacements<char> ws_to_http;
1606 if (url.SchemeIs("ws")) {
1607 ws_to_http.SetScheme("http", url::Component(0, 4));
1608 } else {
1609 DCHECK(url.SchemeIs("wss"));
1610 ws_to_http.SetScheme("https", url::Component(0, 5));
1612 return url.ReplaceComponents(ws_to_http);
1614 return request_->url;
1615 default:
1616 return GURL();
1620 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1621 return websocket_handshake_stream_base_create_helper_ &&
1622 request_->url.SchemeIsWSOrWSS();
1625 #define STATE_CASE(s) \
1626 case s: \
1627 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1628 break
1630 std::string HttpNetworkTransaction::DescribeState(State state) {
1631 std::string description;
1632 switch (state) {
1633 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM);
1634 STATE_CASE(STATE_CREATE_STREAM);
1635 STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
1636 STATE_CASE(STATE_INIT_REQUEST_BODY);
1637 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
1638 STATE_CASE(STATE_BUILD_REQUEST);
1639 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
1640 STATE_CASE(STATE_SEND_REQUEST);
1641 STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1642 STATE_CASE(STATE_READ_HEADERS);
1643 STATE_CASE(STATE_READ_HEADERS_COMPLETE);
1644 STATE_CASE(STATE_READ_BODY);
1645 STATE_CASE(STATE_READ_BODY_COMPLETE);
1646 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1647 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
1648 STATE_CASE(STATE_NONE);
1649 default:
1650 description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1651 state);
1652 break;
1654 return description;
1657 #undef STATE_CASE
1659 void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1660 DCHECK(stream_request_);
1662 // Since the transaction can restart with auth credentials, it may create a
1663 // stream more than once. Accumulate all of the connection attempts across
1664 // those streams by appending them to the vector:
1665 for (const auto& attempt : stream_request_->connection_attempts())
1666 connection_attempts_.push_back(attempt);
1669 } // namespace net