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"
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"
64 #include "url/url_canon.h"
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".
85 if (!headers
.HasHeader(kAlternateProtocolHeader
))
88 std::vector
<std::string
> alternate_protocol_values
;
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
,
107 scoped_ptr
<base::Value
> NetLogSSLVersionFallbackCallback(
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
);
123 scoped_ptr
<base::Value
> NetLogSSLCipherFallbackCallback(
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
);
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))),
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
),
151 total_received_bytes_(0),
152 total_sent_bytes_(0),
153 next_state_(STATE_NONE
),
154 establishing_tunnel_(false),
155 websocket_handshake_stream_base_create_helper_(NULL
) {
156 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
157 session
->GetNextProtos(&server_ssl_config_
.next_protos
);
158 proxy_ssl_config_
= server_ssl_config_
;
161 HttpNetworkTransaction::~HttpNetworkTransaction() {
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
166 if (!stream_
->CanReuseConnection() || next_state_
!= STATE_NONE
) {
167 stream_
->Close(true /* not reusable */);
168 } else if (stream_
->IsResponseBodyComplete()) {
169 // If the response body is complete, we can just reuse the socket.
170 stream_
->Close(false /* reusable */);
172 // Otherwise, we try to drain the response body.
173 HttpStream
* stream
= stream_
.release();
174 stream
->Drain(session_
);
178 if (request_
&& request_
->upload_data_stream
)
179 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
182 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
183 const CompletionCallback
& callback
,
184 const BoundNetLog
& net_log
) {
186 request_
= request_info
;
188 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
189 server_ssl_config_
.rev_checking_enabled
= false;
190 proxy_ssl_config_
.rev_checking_enabled
= false;
193 if (request_
->load_flags
& LOAD_PREFETCH
)
194 response_
.unused_since_prefetch
= true;
196 // Channel ID is disabled if privacy mode is enabled for this request.
197 if (request_
->privacy_mode
== PRIVACY_MODE_ENABLED
)
198 server_ssl_config_
.channel_id_enabled
= false;
200 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
202 if (rv
== ERR_IO_PENDING
)
203 callback_
= callback
;
207 int HttpNetworkTransaction::RestartIgnoringLastError(
208 const CompletionCallback
& callback
) {
209 DCHECK(!stream_
.get());
210 DCHECK(!stream_request_
.get());
211 DCHECK_EQ(STATE_NONE
, next_state_
);
213 next_state_
= STATE_CREATE_STREAM
;
216 if (rv
== ERR_IO_PENDING
)
217 callback_
= callback
;
221 int HttpNetworkTransaction::RestartWithCertificate(
222 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
223 // In HandleCertificateRequest(), we always tear down existing stream
224 // requests to force a new connection. So we shouldn't have one here.
225 DCHECK(!stream_request_
.get());
226 DCHECK(!stream_
.get());
227 DCHECK_EQ(STATE_NONE
, next_state_
);
229 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
230 &proxy_ssl_config_
: &server_ssl_config_
;
231 ssl_config
->send_client_cert
= true;
232 ssl_config
->client_cert
= client_cert
;
233 session_
->ssl_client_auth_cache()->Add(
234 response_
.cert_request_info
->host_and_port
, client_cert
);
235 // Reset the other member variables.
236 // Note: this is necessary only with SSL renegotiation.
237 ResetStateForRestart();
238 next_state_
= STATE_CREATE_STREAM
;
240 if (rv
== ERR_IO_PENDING
)
241 callback_
= callback
;
245 int HttpNetworkTransaction::RestartWithAuth(
246 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
247 HttpAuth::Target target
= pending_auth_target_
;
248 if (target
== HttpAuth::AUTH_NONE
) {
250 return ERR_UNEXPECTED
;
252 pending_auth_target_
= HttpAuth::AUTH_NONE
;
254 auth_controllers_
[target
]->ResetAuth(credentials
);
256 DCHECK(callback_
.is_null());
259 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
260 // In this case, we've gathered credentials for use with proxy
261 // authentication of a tunnel.
262 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
263 DCHECK(stream_request_
!= NULL
);
264 auth_controllers_
[target
] = NULL
;
265 ResetStateForRestart();
266 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
268 // In this case, we've gathered credentials for the server or the proxy
269 // but it is not during the tunneling phase.
270 DCHECK(stream_request_
== NULL
);
271 PrepareForAuthRestart(target
);
275 if (rv
== ERR_IO_PENDING
)
276 callback_
= callback
;
280 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
281 DCHECK(HaveAuth(target
));
282 DCHECK(!stream_request_
.get());
284 bool keep_alive
= false;
285 // Even if the server says the connection is keep-alive, we have to be
286 // able to find the end of each response in order to reuse the connection.
287 if (stream_
->CanReuseConnection()) {
288 // If the response body hasn't been completely read, we need to drain
290 if (!stream_
->IsResponseBodyComplete()) {
291 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
292 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
293 read_buf_len_
= kDrainBodyBufferSize
;
299 // We don't need to drain the response body, so we act as if we had drained
300 // the response body.
301 DidDrainBodyForAuthRestart(keep_alive
);
304 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
305 DCHECK(!stream_request_
.get());
308 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
309 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
310 HttpStream
* new_stream
= NULL
;
311 if (keep_alive
&& stream_
->CanReuseConnection()) {
312 // We should call connection_->set_idle_time(), but this doesn't occur
313 // often enough to be worth the trouble.
314 stream_
->SetConnectionReused();
315 new_stream
= stream_
->RenewStreamForAuth();
319 // Close the stream and mark it as not_reusable. Even in the
320 // keep_alive case, we've determined that the stream_ is not
321 // reusable if new_stream is NULL.
322 stream_
->Close(true);
323 next_state_
= STATE_CREATE_STREAM
;
325 // Renewed streams shouldn't carry over sent or received bytes.
326 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
327 DCHECK_EQ(0, new_stream
->GetTotalSentBytes());
328 next_state_
= STATE_INIT_STREAM
;
330 stream_
.reset(new_stream
);
333 // Reset the other member variables.
334 ResetStateForAuthRestart();
337 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
338 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
339 HaveAuth(pending_auth_target_
);
342 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
343 const CompletionCallback
& callback
) {
345 DCHECK_LT(0, buf_len
);
347 State next_state
= STATE_NONE
;
349 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
350 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
351 // We're trying to read the body of the response but we're still trying
352 // to establish an SSL tunnel through an HTTP proxy. We can't read these
353 // bytes when establishing a tunnel because they might be controlled by
354 // an active network attacker. We don't worry about this for HTTP
355 // because an active network attacker can already control HTTP sessions.
356 // We reach this case when the user cancels a 407 proxy auth prompt. We
357 // also don't worry about this for an HTTPS Proxy, because the
358 // communication with the proxy is secure.
359 // See http://crbug.com/8473.
360 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
361 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
362 LOG(WARNING
) << "Blocked proxy response with status "
363 << headers
->response_code() << " to CONNECT request for "
364 << GetHostAndPort(request_
->url
) << ".";
365 return ERR_TUNNEL_CONNECTION_FAILED
;
368 // Are we using SPDY or HTTP?
369 next_state
= STATE_READ_BODY
;
372 read_buf_len_
= buf_len
;
374 next_state_
= next_state
;
376 if (rv
== ERR_IO_PENDING
)
377 callback_
= callback
;
381 void HttpNetworkTransaction::StopCaching() {}
383 bool HttpNetworkTransaction::GetFullRequestHeaders(
384 HttpRequestHeaders
* headers
) const {
385 // TODO(ttuttle): Make sure we've populated request_headers_.
386 *headers
= request_headers_
;
390 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
391 int64 total_received_bytes
= total_received_bytes_
;
393 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
394 return total_received_bytes
;
397 int64_t HttpNetworkTransaction::GetTotalSentBytes() const {
398 int64_t total_sent_bytes
= total_sent_bytes_
;
400 total_sent_bytes
+= stream_
->GetTotalSentBytes();
401 return total_sent_bytes
;
404 void HttpNetworkTransaction::DoneReading() {}
406 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
410 LoadState
HttpNetworkTransaction::GetLoadState() const {
411 // TODO(wtc): Define a new LoadState value for the
412 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
413 switch (next_state_
) {
414 case STATE_CREATE_STREAM
:
415 return LOAD_STATE_WAITING_FOR_DELEGATE
;
416 case STATE_CREATE_STREAM_COMPLETE
:
417 return stream_request_
->GetLoadState();
418 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
419 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
420 case STATE_SEND_REQUEST_COMPLETE
:
421 return LOAD_STATE_SENDING_REQUEST
;
422 case STATE_READ_HEADERS_COMPLETE
:
423 return LOAD_STATE_WAITING_FOR_RESPONSE
;
424 case STATE_READ_BODY_COMPLETE
:
425 return LOAD_STATE_READING_RESPONSE
;
427 return LOAD_STATE_IDLE
;
431 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
433 return UploadProgress();
435 return stream_
->GetUploadProgress();
438 void HttpNetworkTransaction::SetQuicServerInfo(
439 QuicServerInfo
* quic_server_info
) {}
441 bool HttpNetworkTransaction::GetLoadTimingInfo(
442 LoadTimingInfo
* load_timing_info
) const {
443 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
446 load_timing_info
->proxy_resolve_start
=
447 proxy_info_
.proxy_resolve_start_time();
448 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
449 load_timing_info
->send_start
= send_start_time_
;
450 load_timing_info
->send_end
= send_end_time_
;
454 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
455 priority_
= priority
;
457 stream_request_
->SetPriority(priority
);
459 stream_
->SetPriority(priority
);
462 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
463 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
464 websocket_handshake_stream_base_create_helper_
= create_helper
;
467 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
468 const BeforeNetworkStartCallback
& callback
) {
469 before_network_start_callback_
= callback
;
472 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
473 const BeforeProxyHeadersSentCallback
& callback
) {
474 before_proxy_headers_sent_callback_
= callback
;
477 int HttpNetworkTransaction::ResumeNetworkStart() {
478 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
482 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
483 const ProxyInfo
& used_proxy_info
,
484 HttpStream
* stream
) {
485 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
486 DCHECK(stream_request_
.get());
489 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
490 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
492 stream_
.reset(stream
);
493 server_ssl_config_
= used_ssl_config
;
494 proxy_info_
= used_proxy_info
;
495 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
496 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
497 stream_request_
->protocol_negotiated());
498 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
499 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
500 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
501 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
505 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
506 const SSLConfig
& used_ssl_config
,
507 const ProxyInfo
& used_proxy_info
,
508 WebSocketHandshakeStreamBase
* stream
) {
509 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
512 void HttpNetworkTransaction::OnStreamFailed(int result
,
513 const SSLConfig
& used_ssl_config
,
514 SSLFailureState ssl_failure_state
) {
515 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
516 DCHECK_NE(OK
, result
);
517 DCHECK(stream_request_
.get());
518 DCHECK(!stream_
.get());
519 server_ssl_config_
= used_ssl_config
;
520 server_ssl_failure_state_
= ssl_failure_state
;
522 OnIOComplete(result
);
525 void HttpNetworkTransaction::OnCertificateError(
527 const SSLConfig
& used_ssl_config
,
528 const SSLInfo
& ssl_info
) {
529 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
530 DCHECK_NE(OK
, result
);
531 DCHECK(stream_request_
.get());
532 DCHECK(!stream_
.get());
534 response_
.ssl_info
= ssl_info
;
535 server_ssl_config_
= used_ssl_config
;
537 // TODO(mbelshe): For now, we're going to pass the error through, and that
538 // will close the stream_request in all cases. This means that we're always
539 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
540 // good and the user chooses to ignore the error. This is not ideal, but not
541 // the end of the world either.
543 OnIOComplete(result
);
546 void HttpNetworkTransaction::OnNeedsProxyAuth(
547 const HttpResponseInfo
& proxy_response
,
548 const SSLConfig
& used_ssl_config
,
549 const ProxyInfo
& used_proxy_info
,
550 HttpAuthController
* auth_controller
) {
551 DCHECK(stream_request_
.get());
552 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
554 establishing_tunnel_
= true;
555 response_
.headers
= proxy_response
.headers
;
556 response_
.auth_challenge
= proxy_response
.auth_challenge
;
557 headers_valid_
= true;
558 server_ssl_config_
= used_ssl_config
;
559 proxy_info_
= used_proxy_info
;
561 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
562 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
567 void HttpNetworkTransaction::OnNeedsClientAuth(
568 const SSLConfig
& used_ssl_config
,
569 SSLCertRequestInfo
* cert_info
) {
570 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
572 server_ssl_config_
= used_ssl_config
;
573 response_
.cert_request_info
= cert_info
;
574 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
577 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
578 const HttpResponseInfo
& response_info
,
579 const SSLConfig
& used_ssl_config
,
580 const ProxyInfo
& used_proxy_info
,
581 HttpStream
* stream
) {
582 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
584 CopyConnectionAttemptsFromStreamRequest();
586 headers_valid_
= true;
587 response_
= response_info
;
588 server_ssl_config_
= used_ssl_config
;
589 proxy_info_
= used_proxy_info
;
591 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
592 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
594 stream_
.reset(stream
);
595 stream_request_
.reset(); // we're done with the stream request
596 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
599 void HttpNetworkTransaction::GetConnectionAttempts(
600 ConnectionAttempts
* out
) const {
601 *out
= connection_attempts_
;
604 bool HttpNetworkTransaction::IsSecureRequest() const {
605 return request_
->url
.SchemeIsCryptographic();
608 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
609 return (proxy_info_
.is_http() || proxy_info_
.is_https() ||
610 proxy_info_
.is_quic()) &&
611 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
614 void HttpNetworkTransaction::DoCallback(int rv
) {
615 DCHECK_NE(rv
, ERR_IO_PENDING
);
616 DCHECK(!callback_
.is_null());
618 // Since Run may result in Read being called, clear user_callback_ up front.
619 CompletionCallback c
= callback_
;
624 void HttpNetworkTransaction::OnIOComplete(int result
) {
625 int rv
= DoLoop(result
);
626 if (rv
!= ERR_IO_PENDING
)
630 int HttpNetworkTransaction::DoLoop(int result
) {
631 DCHECK(next_state_
!= STATE_NONE
);
635 State state
= next_state_
;
636 next_state_
= STATE_NONE
;
638 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
640 rv
= DoNotifyBeforeCreateStream();
642 case STATE_CREATE_STREAM
:
644 rv
= DoCreateStream();
646 case STATE_CREATE_STREAM_COMPLETE
:
647 rv
= DoCreateStreamComplete(rv
);
649 case STATE_INIT_STREAM
:
653 case STATE_INIT_STREAM_COMPLETE
:
654 rv
= DoInitStreamComplete(rv
);
656 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
658 rv
= DoGenerateProxyAuthToken();
660 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
661 rv
= DoGenerateProxyAuthTokenComplete(rv
);
663 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
665 rv
= DoGenerateServerAuthToken();
667 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
668 rv
= DoGenerateServerAuthTokenComplete(rv
);
670 case STATE_INIT_REQUEST_BODY
:
672 rv
= DoInitRequestBody();
674 case STATE_INIT_REQUEST_BODY_COMPLETE
:
675 rv
= DoInitRequestBodyComplete(rv
);
677 case STATE_BUILD_REQUEST
:
679 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
680 rv
= DoBuildRequest();
682 case STATE_BUILD_REQUEST_COMPLETE
:
683 rv
= DoBuildRequestComplete(rv
);
685 case STATE_SEND_REQUEST
:
687 rv
= DoSendRequest();
689 case STATE_SEND_REQUEST_COMPLETE
:
690 rv
= DoSendRequestComplete(rv
);
691 net_log_
.EndEventWithNetErrorCode(
692 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
694 case STATE_READ_HEADERS
:
696 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
697 rv
= DoReadHeaders();
699 case STATE_READ_HEADERS_COMPLETE
:
700 rv
= DoReadHeadersComplete(rv
);
701 net_log_
.EndEventWithNetErrorCode(
702 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
704 case STATE_READ_BODY
:
706 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
709 case STATE_READ_BODY_COMPLETE
:
710 rv
= DoReadBodyComplete(rv
);
711 net_log_
.EndEventWithNetErrorCode(
712 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
714 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
717 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
718 rv
= DoDrainBodyForAuthRestart();
720 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
721 rv
= DoDrainBodyForAuthRestartComplete(rv
);
722 net_log_
.EndEventWithNetErrorCode(
723 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
726 NOTREACHED() << "bad state";
730 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
735 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
736 next_state_
= STATE_CREATE_STREAM
;
738 if (!before_network_start_callback_
.is_null())
739 before_network_start_callback_
.Run(&defer
);
742 return ERR_IO_PENDING
;
745 int HttpNetworkTransaction::DoCreateStream() {
746 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
747 tracked_objects::ScopedTracker
tracking_profile(
748 FROM_HERE_WITH_EXPLICIT_FUNCTION(
749 "424359 HttpNetworkTransaction::DoCreateStream"));
751 response_
.network_accessed
= true;
753 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
754 if (ForWebSocketHandshake()) {
755 stream_request_
.reset(
756 session_
->http_stream_factory_for_websocket()
757 ->RequestWebSocketHandshakeStream(
763 websocket_handshake_stream_base_create_helper_
,
766 stream_request_
.reset(
767 session_
->http_stream_factory()->RequestStream(
775 DCHECK(stream_request_
.get());
776 return ERR_IO_PENDING
;
779 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
780 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
781 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
782 // which resets the stream request first. Therefore, we have to grab the
783 // connection attempts in *that* function instead of here in that case.
784 if (result
!= ERR_HTTPS_PROXY_TUNNEL_RESPONSE
)
785 CopyConnectionAttemptsFromStreamRequest();
787 if (request_
->url
.SchemeIsCryptographic())
788 RecordSSLFallbackMetrics(result
);
791 next_state_
= STATE_INIT_STREAM
;
792 DCHECK(stream_
.get());
793 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
794 result
= HandleCertificateRequest(result
);
795 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
796 // Return OK and let the caller read the proxy's error page
797 next_state_
= STATE_NONE
;
799 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
800 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
801 return HandleHttp11Required(result
);
804 // Handle possible handshake errors that may have occurred if the stream
805 // used SSL for one or more of the layers.
806 result
= HandleSSLHandshakeError(result
);
808 // At this point we are done with the stream_request_.
809 stream_request_
.reset();
813 int HttpNetworkTransaction::DoInitStream() {
814 DCHECK(stream_
.get());
815 next_state_
= STATE_INIT_STREAM_COMPLETE
;
816 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
819 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
821 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
824 result
= HandleIOError(result
);
826 // The stream initialization failed, so this stream will never be useful.
828 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
829 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
837 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
838 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
839 if (!ShouldApplyProxyAuth())
841 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
842 if (!auth_controllers_
[target
].get())
843 auth_controllers_
[target
] =
844 new HttpAuthController(target
,
846 session_
->http_auth_cache(),
847 session_
->http_auth_handler_factory());
848 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
853 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
854 DCHECK_NE(ERR_IO_PENDING
, rv
);
856 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
860 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
861 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
862 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
863 if (!auth_controllers_
[target
].get()) {
864 auth_controllers_
[target
] =
865 new HttpAuthController(target
,
867 session_
->http_auth_cache(),
868 session_
->http_auth_handler_factory());
869 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
870 auth_controllers_
[target
]->DisableEmbeddedIdentity();
872 if (!ShouldApplyServerAuth())
874 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
879 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
880 DCHECK_NE(ERR_IO_PENDING
, rv
);
882 next_state_
= STATE_INIT_REQUEST_BODY
;
886 void HttpNetworkTransaction::BuildRequestHeaders(
887 bool using_http_proxy_without_tunnel
) {
888 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
889 GetHostAndOptionalPort(request_
->url
));
891 // For compat with HTTP/1.0 servers and proxies:
892 if (using_http_proxy_without_tunnel
) {
893 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
896 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
899 // Add a content length header?
900 if (request_
->upload_data_stream
) {
901 if (request_
->upload_data_stream
->is_chunked()) {
902 request_headers_
.SetHeader(
903 HttpRequestHeaders::kTransferEncoding
, "chunked");
905 request_headers_
.SetHeader(
906 HttpRequestHeaders::kContentLength
,
907 base::Uint64ToString(request_
->upload_data_stream
->size()));
909 } else if (request_
->method
== "POST" || request_
->method
== "PUT") {
910 // An empty POST/PUT request still needs a content length. As for HEAD,
911 // IE and Safari also add a content length header. Presumably it is to
912 // support sending a HEAD request to an URL that only expects to be sent a
913 // POST or some other method that normally would have a message body.
914 // Firefox (40.0) does not send the header, and RFC 7230 & 7231
915 // specify that it should not be sent due to undefined behavior.
916 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
919 // Honor load flags that impact proxy caches.
920 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
921 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
922 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
923 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
924 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
927 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
928 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
930 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
931 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
934 request_headers_
.MergeFrom(request_
->extra_headers
);
936 if (using_http_proxy_without_tunnel
&&
937 !before_proxy_headers_sent_callback_
.is_null())
938 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
940 response_
.did_use_http_auth
=
941 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
942 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
945 int HttpNetworkTransaction::DoInitRequestBody() {
946 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
948 if (request_
->upload_data_stream
)
949 rv
= request_
->upload_data_stream
->Init(io_callback_
);
953 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
955 next_state_
= STATE_BUILD_REQUEST
;
959 int HttpNetworkTransaction::DoBuildRequest() {
960 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
961 headers_valid_
= false;
963 // This is constructed lazily (instead of within our Start method), so that
964 // we have proxy info available.
965 if (request_headers_
.IsEmpty()) {
966 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
967 BuildRequestHeaders(using_http_proxy_without_tunnel
);
973 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
975 next_state_
= STATE_SEND_REQUEST
;
979 int HttpNetworkTransaction::DoSendRequest() {
980 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
981 tracked_objects::ScopedTracker
tracking_profile(
982 FROM_HERE_WITH_EXPLICIT_FUNCTION(
983 "424359 HttpNetworkTransaction::DoSendRequest"));
985 send_start_time_
= base::TimeTicks::Now();
986 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
988 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
991 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
992 send_end_time_
= base::TimeTicks::Now();
994 return HandleIOError(result
);
995 next_state_
= STATE_READ_HEADERS
;
999 int HttpNetworkTransaction::DoReadHeaders() {
1000 next_state_
= STATE_READ_HEADERS_COMPLETE
;
1001 return stream_
->ReadResponseHeaders(io_callback_
);
1004 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
1005 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1006 // due to SSL renegotiation.
1007 if (IsCertificateError(result
)) {
1008 // We don't handle a certificate error during SSL renegotiation, so we
1009 // have to return an error that's not in the certificate error range
1011 LOG(ERROR
) << "Got a server certificate with error " << result
1012 << " during SSL renegotiation";
1013 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
1014 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1015 // TODO(wtc): Need a test case for this code path!
1016 DCHECK(stream_
.get());
1017 DCHECK(IsSecureRequest());
1018 response_
.cert_request_info
= new SSLCertRequestInfo
;
1019 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
1020 result
= HandleCertificateRequest(result
);
1025 if (result
== ERR_HTTP_1_1_REQUIRED
||
1026 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
1027 return HandleHttp11Required(result
);
1030 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1031 // response headers were received, we do the best we can to make sense of it
1032 // and send it back up the stack.
1034 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1035 // bizarre for SPDY. Assuming this logic is useful at all.
1036 // TODO(davidben): Bubble the error code up so we do not cache?
1037 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
1041 return HandleIOError(result
);
1043 DCHECK(response_
.headers
.get());
1045 // On a 408 response from the server ("Request Timeout") on a stale socket,
1046 // retry the request.
1047 // Headers can be NULL because of http://crbug.com/384554.
1048 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1049 stream_
->IsConnectionReused()) {
1050 net_log_
.AddEventWithNetErrorCode(
1051 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1052 response_
.headers
->response_code());
1053 // This will close the socket - it would be weird to try and reuse it, even
1054 // if the server doesn't actually close it.
1055 ResetConnectionAndRequestForResend();
1059 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1060 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1061 const int response_code
= response_
.headers
->response_code();
1062 UMA_HISTOGRAM_ENUMERATION(
1063 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1067 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1068 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1070 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1071 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1072 // indicates a buggy server. See:
1073 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1074 if (request_
->method
== "PUT")
1075 return ERR_METHOD_NOT_SUPPORTED
;
1078 // Check for an intermediate 100 Continue response. An origin server is
1079 // allowed to send this response even if we didn't ask for it, so we just
1080 // need to skip over it.
1081 // We treat any other 1xx in this same way (although in practice getting
1082 // a 1xx that isn't a 100 is rare).
1083 // Unless this is a WebSocket request, in which case we pass it on up.
1084 if (response_
.headers
->response_code() / 100 == 1 &&
1085 !ForWebSocketHandshake()) {
1086 response_
.headers
= new HttpResponseHeaders(std::string());
1087 next_state_
= STATE_READ_HEADERS
;
1091 ProcessAlternativeServices(session_
, *response_
.headers
.get(),
1092 HostPortPair::FromURL(request_
->url
));
1094 int rv
= HandleAuthChallenge();
1098 if (IsSecureRequest())
1099 stream_
->GetSSLInfo(&response_
.ssl_info
);
1101 headers_valid_
= true;
1105 int HttpNetworkTransaction::DoReadBody() {
1106 DCHECK(read_buf_
.get());
1107 DCHECK_GT(read_buf_len_
, 0);
1108 DCHECK(stream_
!= NULL
);
1110 next_state_
= STATE_READ_BODY_COMPLETE
;
1111 return stream_
->ReadResponseBody(
1112 read_buf_
.get(), read_buf_len_
, io_callback_
);
1115 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1116 // We are done with the Read call.
1119 DCHECK_NE(ERR_IO_PENDING
, result
);
1123 // Clean up connection if we are done.
1125 // Note: Just because IsResponseBodyComplete is true, we're not
1126 // necessarily "done". We're only "done" when it is the last
1127 // read on this HttpNetworkTransaction, which will be signified
1128 // by a zero-length read.
1129 // TODO(mbelshe): The keep-alive property is really a property of
1130 // the stream. No need to compute it here just to pass back
1131 // to the stream's Close function.
1133 stream_
->IsResponseBodyComplete() && stream_
->CanReuseConnection();
1135 stream_
->Close(!keep_alive
);
1136 // Note: we don't reset the stream here. We've closed it, but we still
1137 // need it around so that callers can call methods such as
1138 // GetUploadProgress() and have them be meaningful.
1139 // TODO(mbelshe): This means we closed the stream here, and we close it
1140 // again in ~HttpNetworkTransaction. Clean that up.
1142 // The next Read call will return 0 (EOF).
1145 // Clear these to avoid leaving around old state.
1152 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1153 // This method differs from DoReadBody only in the next_state_. So we just
1154 // call DoReadBody and override the next_state_. Perhaps there is a more
1155 // elegant way for these two methods to share code.
1156 int rv
= DoReadBody();
1157 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1158 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1162 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1163 // the same. Figure out a good way for these two methods to share code.
1164 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1165 // keep_alive defaults to true because the very reason we're draining the
1166 // response body is to reuse the connection for auth restart.
1167 bool done
= false, keep_alive
= true;
1169 // Error or closed connection while reading the socket.
1172 } else if (stream_
->IsResponseBodyComplete()) {
1177 DidDrainBodyForAuthRestart(keep_alive
);
1180 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1186 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1187 // There are two paths through which the server can request a certificate
1188 // from us. The first is during the initial handshake, the second is
1189 // during SSL renegotiation.
1191 // In both cases, we want to close the connection before proceeding.
1192 // We do this for two reasons:
1193 // First, we don't want to keep the connection to the server hung for a
1194 // long time while the user selects a certificate.
1195 // Second, even if we did keep the connection open, NSS has a bug where
1196 // restarting the handshake for ClientAuth is currently broken.
1197 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1199 if (stream_
.get()) {
1200 // Since we already have a stream, we're being called as part of SSL
1202 DCHECK(!stream_request_
.get());
1203 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1204 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
1205 stream_
->Close(true);
1209 // The server is asking for a client certificate during the initial
1211 stream_request_
.reset();
1213 // If the user selected one of the certificates in client_certs or declined
1214 // to provide one for this server before, use the past decision
1216 scoped_refptr
<X509Certificate
> client_cert
;
1217 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1218 response_
.cert_request_info
->host_and_port
, &client_cert
);
1219 if (!found_cached_cert
)
1222 // Check that the certificate selected is still a certificate the server
1223 // is likely to accept, based on the criteria supplied in the
1224 // CertificateRequest message.
1225 if (client_cert
.get()) {
1226 const std::vector
<std::string
>& cert_authorities
=
1227 response_
.cert_request_info
->cert_authorities
;
1229 bool cert_still_valid
= cert_authorities
.empty() ||
1230 client_cert
->IsIssuedByEncoded(cert_authorities
);
1231 if (!cert_still_valid
)
1235 // TODO(davidben): Add a unit test which covers this path; we need to be
1236 // able to send a legitimate certificate and also bypass/clear the
1237 // SSL session cache.
1238 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1239 &proxy_ssl_config_
: &server_ssl_config_
;
1240 ssl_config
->send_client_cert
= true;
1241 ssl_config
->client_cert
= client_cert
;
1242 next_state_
= STATE_CREATE_STREAM
;
1243 // Reset the other member variables.
1244 // Note: this is necessary only with SSL renegotiation.
1245 ResetStateForRestart();
1249 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1250 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1251 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1253 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1254 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1256 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1258 ResetConnectionAndRequestForResend();
1262 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1263 if (server_ssl_config_
.send_client_cert
&&
1264 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1265 session_
->ssl_client_auth_cache()->Remove(
1266 HostPortPair::FromURL(request_
->url
));
1270 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1271 // being used, as all of the errors are handled as if they were generated
1272 // by the endpoint host, request_->url, rather than considering if they were
1273 // generated by the SSL proxy. http://crbug.com/69329
1274 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1276 HandleClientAuthError(error
);
1278 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1279 // reflect servers require a deprecated cipher rather than merely prefer
1280 // it. This, however, has no security benefit until the ciphers are actually
1282 if (!server_ssl_config_
.enable_deprecated_cipher_suites
&&
1283 (error
== ERR_SSL_VERSION_OR_CIPHER_MISMATCH
||
1284 error
== ERR_CONNECTION_CLOSED
|| error
== ERR_CONNECTION_RESET
)) {
1286 NetLog::TYPE_SSL_CIPHER_FALLBACK
,
1287 base::Bind(&NetLogSSLCipherFallbackCallback
, &request_
->url
, error
));
1288 server_ssl_config_
.enable_deprecated_cipher_suites
= true;
1289 ResetConnectionAndRequestForResend();
1293 bool should_fallback
= false;
1294 uint16 version_max
= server_ssl_config_
.version_max
;
1297 case ERR_CONNECTION_CLOSED
:
1298 case ERR_SSL_PROTOCOL_ERROR
:
1299 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1300 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1301 version_max
> server_ssl_config_
.version_min
) {
1302 // This could be a TLS-intolerant server or a server that chose a
1303 // cipher suite defined only for higher protocol versions (such as
1304 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1305 // back to the next lower version and retry.
1306 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1307 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1308 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1309 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1310 // version_max should match the maximum protocol version supported
1311 // by the SSLClientSocket class.
1314 // Fallback to the lower SSL version.
1315 // While SSL 3.0 fallback should be eliminated because of security
1316 // reasons, there is a high risk of breaking the servers if this is
1318 should_fallback
= true;
1321 case ERR_CONNECTION_RESET
:
1322 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1323 version_max
> server_ssl_config_
.version_min
) {
1324 // Some network devices that inspect application-layer packets seem to
1325 // inject TCP reset packets to break the connections when they see TLS
1326 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1328 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1329 // 1.2. We don't lose much in this fallback because the explicit IV for
1330 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1331 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1334 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1335 // to trigger a version fallback in general, especially the TLS 1.0 ->
1336 // SSL 3.0 fallback, which would drop TLS extensions.
1338 should_fallback
= true;
1341 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1342 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1343 version_max
> server_ssl_config_
.version_min
) {
1344 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1345 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1346 // crbug.com/260358. In order to make the fallback as minimal as
1347 // possible, this fallback is only triggered for >= TLS 1.1.
1349 should_fallback
= true;
1352 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1353 // The server told us that we should not have fallen back. A buggy server
1354 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1355 // connection. |fallback_error_code_| is initialised to
1356 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1357 error
= fallback_error_code_
;
1361 if (should_fallback
) {
1363 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1364 base::Bind(&NetLogSSLVersionFallbackCallback
, &request_
->url
, error
,
1365 server_ssl_failure_state_
, server_ssl_config_
.version_max
,
1367 fallback_error_code_
= error
;
1368 fallback_failure_state_
= server_ssl_failure_state_
;
1369 server_ssl_config_
.version_max
= version_max
;
1370 server_ssl_config_
.version_fallback
= true;
1371 ResetConnectionAndRequestForResend();
1378 // This method determines whether it is safe to resend the request after an
1379 // IO error. It can only be called in response to request header or body
1380 // write errors or response header read errors. It should not be used in
1381 // other cases, such as a Connect error.
1382 int HttpNetworkTransaction::HandleIOError(int error
) {
1383 // Because the peer may request renegotiation with client authentication at
1384 // any time, check and handle client authentication errors.
1385 HandleClientAuthError(error
);
1388 // If we try to reuse a connection that the server is in the process of
1389 // closing, we may end up successfully writing out our request (or a
1390 // portion of our request) only to find a connection error when we try to
1391 // read from (or finish writing to) the socket.
1392 case ERR_CONNECTION_RESET
:
1393 case ERR_CONNECTION_CLOSED
:
1394 case ERR_CONNECTION_ABORTED
:
1395 // There can be a race between the socket pool checking checking whether a
1396 // socket is still connected, receiving the FIN, and sending/reading data
1397 // on a reused socket. If we receive the FIN between the connectedness
1398 // check and writing/reading from the socket, we may first learn the socket
1399 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1400 // likely happen when trying to retrieve its IP address.
1401 // See http://crbug.com/105824 for more details.
1402 case ERR_SOCKET_NOT_CONNECTED
:
1403 // If a socket is closed on its initial request, HttpStreamParser returns
1404 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1405 // preconnected but failed to be used before the server timed it out.
1406 case ERR_EMPTY_RESPONSE
:
1407 if (ShouldResendRequest()) {
1408 net_log_
.AddEventWithNetErrorCode(
1409 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1410 ResetConnectionAndRequestForResend();
1414 case ERR_SPDY_PING_FAILED
:
1415 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1416 case ERR_QUIC_HANDSHAKE_FAILED
:
1417 net_log_
.AddEventWithNetErrorCode(
1418 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1419 ResetConnectionAndRequestForResend();
1426 void HttpNetworkTransaction::ResetStateForRestart() {
1427 ResetStateForAuthRestart();
1429 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1430 total_sent_bytes_
+= stream_
->GetTotalSentBytes();
1435 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1436 send_start_time_
= base::TimeTicks();
1437 send_end_time_
= base::TimeTicks();
1439 pending_auth_target_
= HttpAuth::AUTH_NONE
;
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
)
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] == '.');
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
);
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.
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
;
1485 case SSL_PROTOCOL_VERSION_TLS1_1
:
1486 fallback
= FALLBACK_TLS1_1
;
1492 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback
,
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.
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
)
1532 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1533 if (stream_
.get()) {
1534 stream_
->Close(true);
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
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
)
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
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,
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
;
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 {
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));
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
;
1620 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1621 return websocket_handshake_stream_base_create_helper_
&&
1622 request_
->url
.SchemeIsWSOrWSS();
1625 #define STATE_CASE(s) \
1627 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1630 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1631 std::string description
;
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
);
1650 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,
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
);