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 ProcessAlternateProtocol(
71 HttpNetworkSession
* session
,
72 const HttpResponseHeaders
& headers
,
73 const HostPortPair
& http_host_port_pair
) {
74 if (!headers
.HasHeader(kAlternateProtocolHeader
))
77 std::vector
<std::string
> alternate_protocol_values
;
79 std::string alternate_protocol_str
;
80 while (headers
.EnumerateHeader(&iter
, kAlternateProtocolHeader
,
81 &alternate_protocol_str
)) {
82 base::TrimWhitespaceASCII(alternate_protocol_str
, base::TRIM_ALL
,
83 &alternate_protocol_str
);
84 if (!alternate_protocol_str
.empty()) {
85 alternate_protocol_values
.push_back(alternate_protocol_str
);
89 session
->http_stream_factory()->ProcessAlternateProtocol(
90 session
->http_server_properties(),
91 alternate_protocol_values
,
96 scoped_ptr
<base::Value
> NetLogSSLVersionFallbackCallback(
99 SSLFailureState ssl_failure_state
,
100 uint16 version_before
,
101 uint16 version_after
,
102 NetLogCaptureMode
/* capture_mode */) {
103 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
104 dict
->SetString("host_and_port", GetHostAndPort(*url
));
105 dict
->SetInteger("net_error", net_error
);
106 dict
->SetInteger("ssl_failure_state", ssl_failure_state
);
107 dict
->SetInteger("version_before", version_before
);
108 dict
->SetInteger("version_after", version_after
);
112 scoped_ptr
<base::Value
> NetLogSSLCipherFallbackCallback(
115 NetLogCaptureMode
/* capture_mode */) {
116 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
117 dict
->SetString("host_and_port", GetHostAndPort(*url
));
118 dict
->SetInteger("net_error", net_error
);
124 //-----------------------------------------------------------------------------
126 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
127 HttpNetworkSession
* session
)
128 : pending_auth_target_(HttpAuth::AUTH_NONE
),
129 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
130 base::Unretained(this))),
134 headers_valid_(false),
135 server_ssl_failure_state_(SSL_FAILURE_NONE
),
136 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
137 fallback_failure_state_(SSL_FAILURE_NONE
),
140 total_received_bytes_(0),
141 next_state_(STATE_NONE
),
142 establishing_tunnel_(false),
143 websocket_handshake_stream_base_create_helper_(NULL
) {
144 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
145 session
->GetNextProtos(&server_ssl_config_
.next_protos
);
146 proxy_ssl_config_
= server_ssl_config_
;
149 HttpNetworkTransaction::~HttpNetworkTransaction() {
151 HttpResponseHeaders
* headers
= GetResponseHeaders();
152 // TODO(mbelshe): The stream_ should be able to compute whether or not the
153 // stream should be kept alive. No reason to compute here
155 bool try_to_keep_alive
=
156 next_state_
== STATE_NONE
&&
157 stream_
->CanFindEndOfResponse() &&
158 (!headers
|| headers
->IsKeepAlive());
159 if (!try_to_keep_alive
) {
160 stream_
->Close(true /* not reusable */);
162 if (stream_
->IsResponseBodyComplete()) {
163 // If the response body is complete, we can just reuse the socket.
164 stream_
->Close(false /* reusable */);
165 } else if (stream_
->IsSpdyHttpStream()) {
166 // Doesn't really matter for SpdyHttpStream. Just close it.
167 stream_
->Close(true /* not reusable */);
169 // Otherwise, we try to drain the response body.
170 HttpStream
* stream
= stream_
.release();
171 stream
->Drain(session_
);
176 if (request_
&& request_
->upload_data_stream
)
177 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
180 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
181 const CompletionCallback
& callback
,
182 const BoundNetLog
& net_log
) {
184 request_
= request_info
;
186 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
187 server_ssl_config_
.rev_checking_enabled
= false;
188 proxy_ssl_config_
.rev_checking_enabled
= false;
191 if (request_
->load_flags
& LOAD_PREFETCH
)
192 response_
.unused_since_prefetch
= true;
194 // Channel ID is disabled if privacy mode is enabled for this request.
195 if (request_
->privacy_mode
== PRIVACY_MODE_ENABLED
)
196 server_ssl_config_
.channel_id_enabled
= false;
198 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
200 if (rv
== ERR_IO_PENDING
)
201 callback_
= callback
;
205 int HttpNetworkTransaction::RestartIgnoringLastError(
206 const CompletionCallback
& callback
) {
207 DCHECK(!stream_
.get());
208 DCHECK(!stream_request_
.get());
209 DCHECK_EQ(STATE_NONE
, next_state_
);
211 next_state_
= STATE_CREATE_STREAM
;
214 if (rv
== ERR_IO_PENDING
)
215 callback_
= callback
;
219 int HttpNetworkTransaction::RestartWithCertificate(
220 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
221 // In HandleCertificateRequest(), we always tear down existing stream
222 // requests to force a new connection. So we shouldn't have one here.
223 DCHECK(!stream_request_
.get());
224 DCHECK(!stream_
.get());
225 DCHECK_EQ(STATE_NONE
, next_state_
);
227 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
228 &proxy_ssl_config_
: &server_ssl_config_
;
229 ssl_config
->send_client_cert
= true;
230 ssl_config
->client_cert
= client_cert
;
231 session_
->ssl_client_auth_cache()->Add(
232 response_
.cert_request_info
->host_and_port
, client_cert
);
233 // Reset the other member variables.
234 // Note: this is necessary only with SSL renegotiation.
235 ResetStateForRestart();
236 next_state_
= STATE_CREATE_STREAM
;
238 if (rv
== ERR_IO_PENDING
)
239 callback_
= callback
;
243 int HttpNetworkTransaction::RestartWithAuth(
244 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
245 HttpAuth::Target target
= pending_auth_target_
;
246 if (target
== HttpAuth::AUTH_NONE
) {
248 return ERR_UNEXPECTED
;
250 pending_auth_target_
= HttpAuth::AUTH_NONE
;
252 auth_controllers_
[target
]->ResetAuth(credentials
);
254 DCHECK(callback_
.is_null());
257 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
258 // In this case, we've gathered credentials for use with proxy
259 // authentication of a tunnel.
260 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
261 DCHECK(stream_request_
!= NULL
);
262 auth_controllers_
[target
] = NULL
;
263 ResetStateForRestart();
264 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
266 // In this case, we've gathered credentials for the server or the proxy
267 // but it is not during the tunneling phase.
268 DCHECK(stream_request_
== NULL
);
269 PrepareForAuthRestart(target
);
273 if (rv
== ERR_IO_PENDING
)
274 callback_
= callback
;
278 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
279 DCHECK(HaveAuth(target
));
280 DCHECK(!stream_request_
.get());
282 bool keep_alive
= false;
283 // Even if the server says the connection is keep-alive, we have to be
284 // able to find the end of each response in order to reuse the connection.
285 if (GetResponseHeaders()->IsKeepAlive() &&
286 stream_
->CanFindEndOfResponse()) {
287 // If the response body hasn't been completely read, we need to drain
289 if (!stream_
->IsResponseBodyComplete()) {
290 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
291 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
292 read_buf_len_
= kDrainBodyBufferSize
;
298 // We don't need to drain the response body, so we act as if we had drained
299 // the response body.
300 DidDrainBodyForAuthRestart(keep_alive
);
303 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
304 DCHECK(!stream_request_
.get());
307 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
308 HttpStream
* new_stream
= NULL
;
309 if (keep_alive
&& stream_
->IsConnectionReusable()) {
310 // We should call connection_->set_idle_time(), but this doesn't occur
311 // often enough to be worth the trouble.
312 stream_
->SetConnectionReused();
313 new_stream
= stream_
->RenewStreamForAuth();
317 // Close the stream and mark it as not_reusable. Even in the
318 // keep_alive case, we've determined that the stream_ is not
319 // reusable if new_stream is NULL.
320 stream_
->Close(true);
321 next_state_
= STATE_CREATE_STREAM
;
323 // Renewed streams shouldn't carry over received bytes.
324 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
325 next_state_
= STATE_INIT_STREAM
;
327 stream_
.reset(new_stream
);
330 // Reset the other member variables.
331 ResetStateForAuthRestart();
334 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
335 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
336 HaveAuth(pending_auth_target_
);
339 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
340 const CompletionCallback
& callback
) {
342 DCHECK_LT(0, buf_len
);
344 State next_state
= STATE_NONE
;
346 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
347 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
348 // We're trying to read the body of the response but we're still trying
349 // to establish an SSL tunnel through an HTTP proxy. We can't read these
350 // bytes when establishing a tunnel because they might be controlled by
351 // an active network attacker. We don't worry about this for HTTP
352 // because an active network attacker can already control HTTP sessions.
353 // We reach this case when the user cancels a 407 proxy auth prompt. We
354 // also don't worry about this for an HTTPS Proxy, because the
355 // communication with the proxy is secure.
356 // See http://crbug.com/8473.
357 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
358 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
359 LOG(WARNING
) << "Blocked proxy response with status "
360 << headers
->response_code() << " to CONNECT request for "
361 << GetHostAndPort(request_
->url
) << ".";
362 return ERR_TUNNEL_CONNECTION_FAILED
;
365 // Are we using SPDY or HTTP?
366 next_state
= STATE_READ_BODY
;
369 read_buf_len_
= buf_len
;
371 next_state_
= next_state
;
373 if (rv
== ERR_IO_PENDING
)
374 callback_
= callback
;
378 void HttpNetworkTransaction::StopCaching() {}
380 bool HttpNetworkTransaction::GetFullRequestHeaders(
381 HttpRequestHeaders
* headers
) const {
382 // TODO(ttuttle): Make sure we've populated request_headers_.
383 *headers
= request_headers_
;
387 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
388 int64 total_received_bytes
= total_received_bytes_
;
390 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
391 return total_received_bytes
;
394 void HttpNetworkTransaction::DoneReading() {}
396 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
400 LoadState
HttpNetworkTransaction::GetLoadState() const {
401 // TODO(wtc): Define a new LoadState value for the
402 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
403 switch (next_state_
) {
404 case STATE_CREATE_STREAM
:
405 return LOAD_STATE_WAITING_FOR_DELEGATE
;
406 case STATE_CREATE_STREAM_COMPLETE
:
407 return stream_request_
->GetLoadState();
408 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
409 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
410 case STATE_SEND_REQUEST_COMPLETE
:
411 return LOAD_STATE_SENDING_REQUEST
;
412 case STATE_READ_HEADERS_COMPLETE
:
413 return LOAD_STATE_WAITING_FOR_RESPONSE
;
414 case STATE_READ_BODY_COMPLETE
:
415 return LOAD_STATE_READING_RESPONSE
;
417 return LOAD_STATE_IDLE
;
421 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
423 return UploadProgress();
425 return stream_
->GetUploadProgress();
428 void HttpNetworkTransaction::SetQuicServerInfo(
429 QuicServerInfo
* quic_server_info
) {}
431 bool HttpNetworkTransaction::GetLoadTimingInfo(
432 LoadTimingInfo
* load_timing_info
) const {
433 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
436 load_timing_info
->proxy_resolve_start
=
437 proxy_info_
.proxy_resolve_start_time();
438 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
439 load_timing_info
->send_start
= send_start_time_
;
440 load_timing_info
->send_end
= send_end_time_
;
444 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
445 priority_
= priority
;
447 stream_request_
->SetPriority(priority
);
449 stream_
->SetPriority(priority
);
452 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
453 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
454 websocket_handshake_stream_base_create_helper_
= create_helper
;
457 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
458 const BeforeNetworkStartCallback
& callback
) {
459 before_network_start_callback_
= callback
;
462 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
463 const BeforeProxyHeadersSentCallback
& callback
) {
464 before_proxy_headers_sent_callback_
= callback
;
467 int HttpNetworkTransaction::ResumeNetworkStart() {
468 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
472 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
473 const ProxyInfo
& used_proxy_info
,
474 HttpStream
* stream
) {
475 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
476 DCHECK(stream_request_
.get());
479 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
480 stream_
.reset(stream
);
481 server_ssl_config_
= used_ssl_config
;
482 proxy_info_
= used_proxy_info
;
483 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
484 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
485 stream_request_
->protocol_negotiated());
486 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
487 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
488 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
489 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
493 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
494 const SSLConfig
& used_ssl_config
,
495 const ProxyInfo
& used_proxy_info
,
496 WebSocketHandshakeStreamBase
* stream
) {
497 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
500 void HttpNetworkTransaction::OnStreamFailed(int result
,
501 const SSLConfig
& used_ssl_config
,
502 SSLFailureState ssl_failure_state
) {
503 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
504 DCHECK_NE(OK
, result
);
505 DCHECK(stream_request_
.get());
506 DCHECK(!stream_
.get());
507 server_ssl_config_
= used_ssl_config
;
508 server_ssl_failure_state_
= ssl_failure_state
;
510 OnIOComplete(result
);
513 void HttpNetworkTransaction::OnCertificateError(
515 const SSLConfig
& used_ssl_config
,
516 const SSLInfo
& ssl_info
) {
517 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
518 DCHECK_NE(OK
, result
);
519 DCHECK(stream_request_
.get());
520 DCHECK(!stream_
.get());
522 response_
.ssl_info
= ssl_info
;
523 server_ssl_config_
= used_ssl_config
;
525 // TODO(mbelshe): For now, we're going to pass the error through, and that
526 // will close the stream_request in all cases. This means that we're always
527 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
528 // good and the user chooses to ignore the error. This is not ideal, but not
529 // the end of the world either.
531 OnIOComplete(result
);
534 void HttpNetworkTransaction::OnNeedsProxyAuth(
535 const HttpResponseInfo
& proxy_response
,
536 const SSLConfig
& used_ssl_config
,
537 const ProxyInfo
& used_proxy_info
,
538 HttpAuthController
* auth_controller
) {
539 DCHECK(stream_request_
.get());
540 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
542 establishing_tunnel_
= true;
543 response_
.headers
= proxy_response
.headers
;
544 response_
.auth_challenge
= proxy_response
.auth_challenge
;
545 headers_valid_
= true;
546 server_ssl_config_
= used_ssl_config
;
547 proxy_info_
= used_proxy_info
;
549 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
550 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
555 void HttpNetworkTransaction::OnNeedsClientAuth(
556 const SSLConfig
& used_ssl_config
,
557 SSLCertRequestInfo
* cert_info
) {
558 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
560 server_ssl_config_
= used_ssl_config
;
561 response_
.cert_request_info
= cert_info
;
562 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
565 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
566 const HttpResponseInfo
& response_info
,
567 const SSLConfig
& used_ssl_config
,
568 const ProxyInfo
& used_proxy_info
,
569 HttpStream
* stream
) {
570 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
572 CopyConnectionAttemptsFromStreamRequest();
574 headers_valid_
= true;
575 response_
= response_info
;
576 server_ssl_config_
= used_ssl_config
;
577 proxy_info_
= used_proxy_info
;
579 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
580 stream_
.reset(stream
);
581 stream_request_
.reset(); // we're done with the stream request
582 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
585 void HttpNetworkTransaction::GetConnectionAttempts(
586 ConnectionAttempts
* out
) const {
587 *out
= connection_attempts_
;
590 bool HttpNetworkTransaction::IsSecureRequest() const {
591 return request_
->url
.SchemeIsCryptographic();
594 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
595 return (proxy_info_
.is_http() || proxy_info_
.is_https() ||
596 proxy_info_
.is_quic()) &&
597 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
600 void HttpNetworkTransaction::DoCallback(int rv
) {
601 DCHECK_NE(rv
, ERR_IO_PENDING
);
602 DCHECK(!callback_
.is_null());
604 // Since Run may result in Read being called, clear user_callback_ up front.
605 CompletionCallback c
= callback_
;
610 void HttpNetworkTransaction::OnIOComplete(int result
) {
611 int rv
= DoLoop(result
);
612 if (rv
!= ERR_IO_PENDING
)
616 int HttpNetworkTransaction::DoLoop(int result
) {
617 DCHECK(next_state_
!= STATE_NONE
);
621 State state
= next_state_
;
622 next_state_
= STATE_NONE
;
624 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
626 rv
= DoNotifyBeforeCreateStream();
628 case STATE_CREATE_STREAM
:
630 rv
= DoCreateStream();
632 case STATE_CREATE_STREAM_COMPLETE
:
633 rv
= DoCreateStreamComplete(rv
);
635 case STATE_INIT_STREAM
:
639 case STATE_INIT_STREAM_COMPLETE
:
640 rv
= DoInitStreamComplete(rv
);
642 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
644 rv
= DoGenerateProxyAuthToken();
646 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
647 rv
= DoGenerateProxyAuthTokenComplete(rv
);
649 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
651 rv
= DoGenerateServerAuthToken();
653 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
654 rv
= DoGenerateServerAuthTokenComplete(rv
);
656 case STATE_INIT_REQUEST_BODY
:
658 rv
= DoInitRequestBody();
660 case STATE_INIT_REQUEST_BODY_COMPLETE
:
661 rv
= DoInitRequestBodyComplete(rv
);
663 case STATE_BUILD_REQUEST
:
665 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
666 rv
= DoBuildRequest();
668 case STATE_BUILD_REQUEST_COMPLETE
:
669 rv
= DoBuildRequestComplete(rv
);
671 case STATE_SEND_REQUEST
:
673 rv
= DoSendRequest();
675 case STATE_SEND_REQUEST_COMPLETE
:
676 rv
= DoSendRequestComplete(rv
);
677 net_log_
.EndEventWithNetErrorCode(
678 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
680 case STATE_READ_HEADERS
:
682 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
683 rv
= DoReadHeaders();
685 case STATE_READ_HEADERS_COMPLETE
:
686 rv
= DoReadHeadersComplete(rv
);
687 net_log_
.EndEventWithNetErrorCode(
688 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
690 case STATE_READ_BODY
:
692 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
695 case STATE_READ_BODY_COMPLETE
:
696 rv
= DoReadBodyComplete(rv
);
697 net_log_
.EndEventWithNetErrorCode(
698 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
700 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
703 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
704 rv
= DoDrainBodyForAuthRestart();
706 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
707 rv
= DoDrainBodyForAuthRestartComplete(rv
);
708 net_log_
.EndEventWithNetErrorCode(
709 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
712 NOTREACHED() << "bad state";
716 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
721 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
722 next_state_
= STATE_CREATE_STREAM
;
724 if (!before_network_start_callback_
.is_null())
725 before_network_start_callback_
.Run(&defer
);
728 return ERR_IO_PENDING
;
731 int HttpNetworkTransaction::DoCreateStream() {
732 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
733 tracked_objects::ScopedTracker
tracking_profile(
734 FROM_HERE_WITH_EXPLICIT_FUNCTION(
735 "424359 HttpNetworkTransaction::DoCreateStream"));
737 response_
.network_accessed
= true;
739 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
740 if (ForWebSocketHandshake()) {
741 stream_request_
.reset(
742 session_
->http_stream_factory_for_websocket()
743 ->RequestWebSocketHandshakeStream(
749 websocket_handshake_stream_base_create_helper_
,
752 stream_request_
.reset(
753 session_
->http_stream_factory()->RequestStream(
761 DCHECK(stream_request_
.get());
762 return ERR_IO_PENDING
;
765 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
766 // If |result| is ERR_HTTPS_PROXY_TUNNEL_RESPONSE, then
767 // DoCreateStreamComplete is being called from OnHttpsProxyTunnelResponse,
768 // which resets the stream request first. Therefore, we have to grab the
769 // connection attempts in *that* function instead of here in that case.
770 if (result
!= ERR_HTTPS_PROXY_TUNNEL_RESPONSE
)
771 CopyConnectionAttemptsFromStreamRequest();
773 if (request_
->url
.SchemeIsCryptographic())
774 RecordSSLFallbackMetrics(result
);
777 next_state_
= STATE_INIT_STREAM
;
778 DCHECK(stream_
.get());
779 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
780 result
= HandleCertificateRequest(result
);
781 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
782 // Return OK and let the caller read the proxy's error page
783 next_state_
= STATE_NONE
;
785 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
786 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
787 return HandleHttp11Required(result
);
790 // Handle possible handshake errors that may have occurred if the stream
791 // used SSL for one or more of the layers.
792 result
= HandleSSLHandshakeError(result
);
794 // At this point we are done with the stream_request_.
795 stream_request_
.reset();
799 int HttpNetworkTransaction::DoInitStream() {
800 DCHECK(stream_
.get());
801 next_state_
= STATE_INIT_STREAM_COMPLETE
;
802 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
805 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
807 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
810 result
= HandleIOError(result
);
812 // The stream initialization failed, so this stream will never be useful.
814 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
821 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
822 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
823 if (!ShouldApplyProxyAuth())
825 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
826 if (!auth_controllers_
[target
].get())
827 auth_controllers_
[target
] =
828 new HttpAuthController(target
,
830 session_
->http_auth_cache(),
831 session_
->http_auth_handler_factory());
832 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
837 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
838 DCHECK_NE(ERR_IO_PENDING
, rv
);
840 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
844 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
845 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
846 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
847 if (!auth_controllers_
[target
].get()) {
848 auth_controllers_
[target
] =
849 new HttpAuthController(target
,
851 session_
->http_auth_cache(),
852 session_
->http_auth_handler_factory());
853 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
854 auth_controllers_
[target
]->DisableEmbeddedIdentity();
856 if (!ShouldApplyServerAuth())
858 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
863 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
864 DCHECK_NE(ERR_IO_PENDING
, rv
);
866 next_state_
= STATE_INIT_REQUEST_BODY
;
870 void HttpNetworkTransaction::BuildRequestHeaders(
871 bool using_http_proxy_without_tunnel
) {
872 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
873 GetHostAndOptionalPort(request_
->url
));
875 // For compat with HTTP/1.0 servers and proxies:
876 if (using_http_proxy_without_tunnel
) {
877 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
880 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
883 // Add a content length header?
884 if (request_
->upload_data_stream
) {
885 if (request_
->upload_data_stream
->is_chunked()) {
886 request_headers_
.SetHeader(
887 HttpRequestHeaders::kTransferEncoding
, "chunked");
889 request_headers_
.SetHeader(
890 HttpRequestHeaders::kContentLength
,
891 base::Uint64ToString(request_
->upload_data_stream
->size()));
893 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
894 request_
->method
== "HEAD") {
895 // An empty POST/PUT request still needs a content length. As for HEAD,
896 // IE and Safari also add a content length header. Presumably it is to
897 // support sending a HEAD request to an URL that only expects to be sent a
898 // POST or some other method that normally would have a message body.
899 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
902 // Honor load flags that impact proxy caches.
903 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
904 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
905 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
906 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
907 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
910 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
911 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
913 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
914 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
917 request_headers_
.MergeFrom(request_
->extra_headers
);
919 if (using_http_proxy_without_tunnel
&&
920 !before_proxy_headers_sent_callback_
.is_null())
921 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
923 response_
.did_use_http_auth
=
924 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
925 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
928 int HttpNetworkTransaction::DoInitRequestBody() {
929 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
931 if (request_
->upload_data_stream
)
932 rv
= request_
->upload_data_stream
->Init(io_callback_
);
936 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
938 next_state_
= STATE_BUILD_REQUEST
;
942 int HttpNetworkTransaction::DoBuildRequest() {
943 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
944 headers_valid_
= false;
946 // This is constructed lazily (instead of within our Start method), so that
947 // we have proxy info available.
948 if (request_headers_
.IsEmpty()) {
949 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
950 BuildRequestHeaders(using_http_proxy_without_tunnel
);
956 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
958 next_state_
= STATE_SEND_REQUEST
;
962 int HttpNetworkTransaction::DoSendRequest() {
963 // TODO(mmenke): Remove ScopedTracker below once crbug.com/424359 is fixed.
964 tracked_objects::ScopedTracker
tracking_profile(
965 FROM_HERE_WITH_EXPLICIT_FUNCTION(
966 "424359 HttpNetworkTransaction::DoSendRequest"));
968 send_start_time_
= base::TimeTicks::Now();
969 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
971 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
974 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
975 send_end_time_
= base::TimeTicks::Now();
977 return HandleIOError(result
);
978 next_state_
= STATE_READ_HEADERS
;
982 int HttpNetworkTransaction::DoReadHeaders() {
983 next_state_
= STATE_READ_HEADERS_COMPLETE
;
984 return stream_
->ReadResponseHeaders(io_callback_
);
987 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
988 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
989 // due to SSL renegotiation.
990 if (IsCertificateError(result
)) {
991 // We don't handle a certificate error during SSL renegotiation, so we
992 // have to return an error that's not in the certificate error range
994 LOG(ERROR
) << "Got a server certificate with error " << result
995 << " during SSL renegotiation";
996 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
997 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
998 // TODO(wtc): Need a test case for this code path!
999 DCHECK(stream_
.get());
1000 DCHECK(IsSecureRequest());
1001 response_
.cert_request_info
= new SSLCertRequestInfo
;
1002 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
1003 result
= HandleCertificateRequest(result
);
1008 if (result
== ERR_HTTP_1_1_REQUIRED
||
1009 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
1010 return HandleHttp11Required(result
);
1013 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1014 // response headers were received, we do the best we can to make sense of it
1015 // and send it back up the stack.
1017 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1018 // bizarre for SPDY. Assuming this logic is useful at all.
1019 // TODO(davidben): Bubble the error code up so we do not cache?
1020 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
1024 return HandleIOError(result
);
1026 DCHECK(response_
.headers
.get());
1028 // On a 408 response from the server ("Request Timeout") on a stale socket,
1029 // retry the request.
1030 // Headers can be NULL because of http://crbug.com/384554.
1031 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1032 stream_
->IsConnectionReused()) {
1033 net_log_
.AddEventWithNetErrorCode(
1034 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1035 response_
.headers
->response_code());
1036 // This will close the socket - it would be weird to try and reuse it, even
1037 // if the server doesn't actually close it.
1038 ResetConnectionAndRequestForResend();
1042 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1043 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1044 const int response_code
= response_
.headers
->response_code();
1045 UMA_HISTOGRAM_ENUMERATION(
1046 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1050 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1051 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1053 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1054 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1055 // indicates a buggy server. See:
1056 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1057 if (request_
->method
== "PUT")
1058 return ERR_METHOD_NOT_SUPPORTED
;
1061 // Check for an intermediate 100 Continue response. An origin server is
1062 // allowed to send this response even if we didn't ask for it, so we just
1063 // need to skip over it.
1064 // We treat any other 1xx in this same way (although in practice getting
1065 // a 1xx that isn't a 100 is rare).
1066 // Unless this is a WebSocket request, in which case we pass it on up.
1067 if (response_
.headers
->response_code() / 100 == 1 &&
1068 !ForWebSocketHandshake()) {
1069 response_
.headers
= new HttpResponseHeaders(std::string());
1070 next_state_
= STATE_READ_HEADERS
;
1074 ProcessAlternateProtocol(session_
, *response_
.headers
.get(),
1075 HostPortPair::FromURL(request_
->url
));
1077 int rv
= HandleAuthChallenge();
1081 if (IsSecureRequest())
1082 stream_
->GetSSLInfo(&response_
.ssl_info
);
1084 headers_valid_
= true;
1088 int HttpNetworkTransaction::DoReadBody() {
1089 DCHECK(read_buf_
.get());
1090 DCHECK_GT(read_buf_len_
, 0);
1091 DCHECK(stream_
!= NULL
);
1093 next_state_
= STATE_READ_BODY_COMPLETE
;
1094 return stream_
->ReadResponseBody(
1095 read_buf_
.get(), read_buf_len_
, io_callback_
);
1098 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1099 // We are done with the Read call.
1102 DCHECK_NE(ERR_IO_PENDING
, result
);
1106 bool keep_alive
= false;
1107 if (stream_
->IsResponseBodyComplete()) {
1108 // Note: Just because IsResponseBodyComplete is true, we're not
1109 // necessarily "done". We're only "done" when it is the last
1110 // read on this HttpNetworkTransaction, which will be signified
1111 // by a zero-length read.
1112 // TODO(mbelshe): The keepalive property is really a property of
1113 // the stream. No need to compute it here just to pass back
1114 // to the stream's Close function.
1115 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1117 if (stream_
->CanFindEndOfResponse()) {
1118 HttpResponseHeaders
* headers
= GetResponseHeaders();
1120 keep_alive
= headers
->IsKeepAlive();
1124 // Clean up connection if we are done.
1126 stream_
->Close(!keep_alive
);
1127 // Note: we don't reset the stream here. We've closed it, but we still
1128 // need it around so that callers can call methods such as
1129 // GetUploadProgress() and have them be meaningful.
1130 // TODO(mbelshe): This means we closed the stream here, and we close it
1131 // again in ~HttpNetworkTransaction. Clean that up.
1133 // The next Read call will return 0 (EOF).
1136 // Clear these to avoid leaving around old state.
1143 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1144 // This method differs from DoReadBody only in the next_state_. So we just
1145 // call DoReadBody and override the next_state_. Perhaps there is a more
1146 // elegant way for these two methods to share code.
1147 int rv
= DoReadBody();
1148 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1149 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1153 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1154 // the same. Figure out a good way for these two methods to share code.
1155 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1156 // keep_alive defaults to true because the very reason we're draining the
1157 // response body is to reuse the connection for auth restart.
1158 bool done
= false, keep_alive
= true;
1160 // Error or closed connection while reading the socket.
1163 } else if (stream_
->IsResponseBodyComplete()) {
1168 DidDrainBodyForAuthRestart(keep_alive
);
1171 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1177 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1178 // There are two paths through which the server can request a certificate
1179 // from us. The first is during the initial handshake, the second is
1180 // during SSL renegotiation.
1182 // In both cases, we want to close the connection before proceeding.
1183 // We do this for two reasons:
1184 // First, we don't want to keep the connection to the server hung for a
1185 // long time while the user selects a certificate.
1186 // Second, even if we did keep the connection open, NSS has a bug where
1187 // restarting the handshake for ClientAuth is currently broken.
1188 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1190 if (stream_
.get()) {
1191 // Since we already have a stream, we're being called as part of SSL
1193 DCHECK(!stream_request_
.get());
1194 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1195 stream_
->Close(true);
1199 // The server is asking for a client certificate during the initial
1201 stream_request_
.reset();
1203 // If the user selected one of the certificates in client_certs or declined
1204 // to provide one for this server before, use the past decision
1206 scoped_refptr
<X509Certificate
> client_cert
;
1207 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1208 response_
.cert_request_info
->host_and_port
, &client_cert
);
1209 if (!found_cached_cert
)
1212 // Check that the certificate selected is still a certificate the server
1213 // is likely to accept, based on the criteria supplied in the
1214 // CertificateRequest message.
1215 if (client_cert
.get()) {
1216 const std::vector
<std::string
>& cert_authorities
=
1217 response_
.cert_request_info
->cert_authorities
;
1219 bool cert_still_valid
= cert_authorities
.empty() ||
1220 client_cert
->IsIssuedByEncoded(cert_authorities
);
1221 if (!cert_still_valid
)
1225 // TODO(davidben): Add a unit test which covers this path; we need to be
1226 // able to send a legitimate certificate and also bypass/clear the
1227 // SSL session cache.
1228 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1229 &proxy_ssl_config_
: &server_ssl_config_
;
1230 ssl_config
->send_client_cert
= true;
1231 ssl_config
->client_cert
= client_cert
;
1232 next_state_
= STATE_CREATE_STREAM
;
1233 // Reset the other member variables.
1234 // Note: this is necessary only with SSL renegotiation.
1235 ResetStateForRestart();
1239 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1240 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1241 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1243 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1244 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1246 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1248 ResetConnectionAndRequestForResend();
1252 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1253 if (server_ssl_config_
.send_client_cert
&&
1254 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1255 session_
->ssl_client_auth_cache()->Remove(
1256 HostPortPair::FromURL(request_
->url
));
1260 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1261 // being used, as all of the errors are handled as if they were generated
1262 // by the endpoint host, request_->url, rather than considering if they were
1263 // generated by the SSL proxy. http://crbug.com/69329
1264 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1266 HandleClientAuthError(error
);
1268 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1269 // reflect servers require a deprecated cipher rather than merely prefer
1270 // it. This, however, has no security benefit until the ciphers are actually
1272 if (!server_ssl_config_
.enable_deprecated_cipher_suites
&&
1273 (error
== ERR_SSL_VERSION_OR_CIPHER_MISMATCH
||
1274 error
== ERR_CONNECTION_CLOSED
|| error
== ERR_CONNECTION_RESET
)) {
1276 NetLog::TYPE_SSL_CIPHER_FALLBACK
,
1277 base::Bind(&NetLogSSLCipherFallbackCallback
, &request_
->url
, error
));
1278 server_ssl_config_
.enable_deprecated_cipher_suites
= true;
1279 ResetConnectionAndRequestForResend();
1283 bool should_fallback
= false;
1284 uint16 version_max
= server_ssl_config_
.version_max
;
1287 case ERR_CONNECTION_CLOSED
:
1288 case ERR_SSL_PROTOCOL_ERROR
:
1289 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1290 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1291 version_max
> server_ssl_config_
.version_min
) {
1292 // This could be a TLS-intolerant server or a server that chose a
1293 // cipher suite defined only for higher protocol versions (such as
1294 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1295 // back to the next lower version and retry.
1296 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1297 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1298 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1299 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1300 // version_max should match the maximum protocol version supported
1301 // by the SSLClientSocket class.
1304 // Fallback to the lower SSL version.
1305 // While SSL 3.0 fallback should be eliminated because of security
1306 // reasons, there is a high risk of breaking the servers if this is
1308 should_fallback
= true;
1311 case ERR_CONNECTION_RESET
:
1312 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1313 version_max
> server_ssl_config_
.version_min
) {
1314 // Some network devices that inspect application-layer packets seem to
1315 // inject TCP reset packets to break the connections when they see TLS
1316 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1318 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1319 // 1.2. We don't lose much in this fallback because the explicit IV for
1320 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1321 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1324 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1325 // to trigger a version fallback in general, especially the TLS 1.0 ->
1326 // SSL 3.0 fallback, which would drop TLS extensions.
1328 should_fallback
= true;
1331 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1332 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1333 version_max
> server_ssl_config_
.version_min
) {
1334 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1335 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1336 // crbug.com/260358. In order to make the fallback as minimal as
1337 // possible, this fallback is only triggered for >= TLS 1.1.
1339 should_fallback
= true;
1342 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1343 // The server told us that we should not have fallen back. A buggy server
1344 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1345 // connection. |fallback_error_code_| is initialised to
1346 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1347 error
= fallback_error_code_
;
1351 if (should_fallback
) {
1353 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1354 base::Bind(&NetLogSSLVersionFallbackCallback
, &request_
->url
, error
,
1355 server_ssl_failure_state_
, server_ssl_config_
.version_max
,
1357 fallback_error_code_
= error
;
1358 fallback_failure_state_
= server_ssl_failure_state_
;
1359 server_ssl_config_
.version_max
= version_max
;
1360 server_ssl_config_
.version_fallback
= true;
1361 ResetConnectionAndRequestForResend();
1368 // This method determines whether it is safe to resend the request after an
1369 // IO error. It can only be called in response to request header or body
1370 // write errors or response header read errors. It should not be used in
1371 // other cases, such as a Connect error.
1372 int HttpNetworkTransaction::HandleIOError(int error
) {
1373 // Because the peer may request renegotiation with client authentication at
1374 // any time, check and handle client authentication errors.
1375 HandleClientAuthError(error
);
1378 // If we try to reuse a connection that the server is in the process of
1379 // closing, we may end up successfully writing out our request (or a
1380 // portion of our request) only to find a connection error when we try to
1381 // read from (or finish writing to) the socket.
1382 case ERR_CONNECTION_RESET
:
1383 case ERR_CONNECTION_CLOSED
:
1384 case ERR_CONNECTION_ABORTED
:
1385 // There can be a race between the socket pool checking checking whether a
1386 // socket is still connected, receiving the FIN, and sending/reading data
1387 // on a reused socket. If we receive the FIN between the connectedness
1388 // check and writing/reading from the socket, we may first learn the socket
1389 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1390 // likely happen when trying to retrieve its IP address.
1391 // See http://crbug.com/105824 for more details.
1392 case ERR_SOCKET_NOT_CONNECTED
:
1393 // If a socket is closed on its initial request, HttpStreamParser returns
1394 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1395 // preconnected but failed to be used before the server timed it out.
1396 case ERR_EMPTY_RESPONSE
:
1397 if (ShouldResendRequest()) {
1398 net_log_
.AddEventWithNetErrorCode(
1399 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1400 ResetConnectionAndRequestForResend();
1404 case ERR_SPDY_PING_FAILED
:
1405 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1406 case ERR_QUIC_HANDSHAKE_FAILED
:
1407 net_log_
.AddEventWithNetErrorCode(
1408 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1409 ResetConnectionAndRequestForResend();
1416 void HttpNetworkTransaction::ResetStateForRestart() {
1417 ResetStateForAuthRestart();
1419 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1423 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1424 send_start_time_
= base::TimeTicks();
1425 send_end_time_
= base::TimeTicks();
1427 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1430 headers_valid_
= false;
1431 request_headers_
.Clear();
1432 response_
= HttpResponseInfo();
1433 establishing_tunnel_
= false;
1436 void HttpNetworkTransaction::RecordSSLFallbackMetrics(int result
) {
1437 if (result
!= OK
&& result
!= ERR_SSL_INAPPROPRIATE_FALLBACK
)
1440 const std::string
& host
= request_
->url
.host();
1441 bool is_google
= base::EndsWith(host
, "google.com",
1442 base::CompareCase::SENSITIVE
) &&
1443 (host
.size() == 10 || host
[host
.size() - 11] == '.');
1445 // Some fraction of successful connections use the fallback, but only due to
1446 // a spurious network failure. To estimate this fraction, compare handshakes
1447 // to Google servers which succeed against those that fail with an
1448 // inappropriate_fallback alert. Google servers are known to implement
1449 // FALLBACK_SCSV, so a spurious network failure while connecting would
1450 // trigger the fallback, successfully connect, but fail with this alert.
1451 UMA_HISTOGRAM_BOOLEAN("Net.GoogleConnectionInappropriateFallback",
1452 result
== ERR_SSL_INAPPROPRIATE_FALLBACK
);
1458 // Note: these values are used in histograms, so new values must be appended.
1459 enum FallbackVersion
{
1460 FALLBACK_NONE
= 0, // SSL version fallback did not occur.
1461 // Obsolete: FALLBACK_SSL3 = 1,
1462 FALLBACK_TLS1
= 2, // Fell back to TLS 1.0.
1463 FALLBACK_TLS1_1
= 3, // Fell back to TLS 1.1.
1467 FallbackVersion fallback
= FALLBACK_NONE
;
1468 if (server_ssl_config_
.version_fallback
) {
1469 switch (server_ssl_config_
.version_max
) {
1470 case SSL_PROTOCOL_VERSION_TLS1
:
1471 fallback
= FALLBACK_TLS1
;
1473 case SSL_PROTOCOL_VERSION_TLS1_1
:
1474 fallback
= FALLBACK_TLS1_1
;
1480 UMA_HISTOGRAM_ENUMERATION("Net.ConnectionUsedSSLVersionFallback2", fallback
,
1483 // Google servers are known to implement TLS 1.2 and FALLBACK_SCSV, so it
1484 // should be impossible to successfully connect to them with the fallback.
1485 // This helps estimate intolerant locally-configured SSL MITMs.
1487 UMA_HISTOGRAM_ENUMERATION("Net.GoogleConnectionUsedSSLVersionFallback2",
1488 fallback
, FALLBACK_MAX
);
1491 UMA_HISTOGRAM_BOOLEAN("Net.ConnectionUsedSSLDeprecatedCipherFallback2",
1492 server_ssl_config_
.enable_deprecated_cipher_suites
);
1494 if (server_ssl_config_
.version_fallback
) {
1495 // Record the error code which triggered the fallback and the state the
1496 // handshake was in.
1497 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSLFallbackErrorCode",
1498 -fallback_error_code_
);
1499 UMA_HISTOGRAM_ENUMERATION("Net.SSLFallbackFailureState",
1500 fallback_failure_state_
, SSL_FAILURE_MAX
);
1504 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1505 return response_
.headers
.get();
1508 bool HttpNetworkTransaction::ShouldResendRequest() const {
1509 bool connection_is_proven
= stream_
->IsConnectionReused();
1510 bool has_received_headers
= GetResponseHeaders() != NULL
;
1512 // NOTE: we resend a request only if we reused a keep-alive connection.
1513 // This automatically prevents an infinite resend loop because we'll run
1514 // out of the cached keep-alive connections eventually.
1515 if (connection_is_proven
&& !has_received_headers
)
1520 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1521 if (stream_
.get()) {
1522 stream_
->Close(true);
1526 // We need to clear request_headers_ because it contains the real request
1527 // headers, but we may need to resend the CONNECT request first to recreate
1529 request_headers_
.Clear();
1530 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1533 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1534 return UsingHttpProxyWithoutTunnel();
1537 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1538 return !(request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
);
1541 int HttpNetworkTransaction::HandleAuthChallenge() {
1542 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
1543 DCHECK(headers
.get());
1545 int status
= headers
->response_code();
1546 if (status
!= HTTP_UNAUTHORIZED
&&
1547 status
!= HTTP_PROXY_AUTHENTICATION_REQUIRED
)
1549 HttpAuth::Target target
= status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
?
1550 HttpAuth::AUTH_PROXY
: HttpAuth::AUTH_SERVER
;
1551 if (target
== HttpAuth::AUTH_PROXY
&& proxy_info_
.is_direct())
1552 return ERR_UNEXPECTED_PROXY_AUTH
;
1554 // This case can trigger when an HTTPS server responds with a "Proxy
1555 // authentication required" status code through a non-authenticating
1557 if (!auth_controllers_
[target
].get())
1558 return ERR_UNEXPECTED_PROXY_AUTH
;
1560 int rv
= auth_controllers_
[target
]->HandleAuthChallenge(
1561 headers
, (request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
) != 0, false,
1563 if (auth_controllers_
[target
]->HaveAuthHandler())
1564 pending_auth_target_
= target
;
1566 scoped_refptr
<AuthChallengeInfo
> auth_info
=
1567 auth_controllers_
[target
]->auth_info();
1568 if (auth_info
.get())
1569 response_
.auth_challenge
= auth_info
;
1574 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target
) const {
1575 return auth_controllers_
[target
].get() &&
1576 auth_controllers_
[target
]->HaveAuth();
1579 GURL
HttpNetworkTransaction::AuthURL(HttpAuth::Target target
) const {
1581 case HttpAuth::AUTH_PROXY
: {
1582 if (!proxy_info_
.proxy_server().is_valid() ||
1583 proxy_info_
.proxy_server().is_direct()) {
1584 return GURL(); // There is no proxy server.
1586 const char* scheme
= proxy_info_
.is_https() ? "https://" : "http://";
1587 return GURL(scheme
+
1588 proxy_info_
.proxy_server().host_port_pair().ToString());
1590 case HttpAuth::AUTH_SERVER
:
1591 if (ForWebSocketHandshake()) {
1592 const GURL
& url
= request_
->url
;
1593 url::Replacements
<char> ws_to_http
;
1594 if (url
.SchemeIs("ws")) {
1595 ws_to_http
.SetScheme("http", url::Component(0, 4));
1597 DCHECK(url
.SchemeIs("wss"));
1598 ws_to_http
.SetScheme("https", url::Component(0, 5));
1600 return url
.ReplaceComponents(ws_to_http
);
1602 return request_
->url
;
1608 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1609 return websocket_handshake_stream_base_create_helper_
&&
1610 request_
->url
.SchemeIsWSOrWSS();
1613 #define STATE_CASE(s) \
1615 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1618 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1619 std::string description
;
1621 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM
);
1622 STATE_CASE(STATE_CREATE_STREAM
);
1623 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1624 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1625 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1626 STATE_CASE(STATE_BUILD_REQUEST
);
1627 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1628 STATE_CASE(STATE_SEND_REQUEST
);
1629 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1630 STATE_CASE(STATE_READ_HEADERS
);
1631 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1632 STATE_CASE(STATE_READ_BODY
);
1633 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1634 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1635 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1636 STATE_CASE(STATE_NONE
);
1638 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,
1647 void HttpNetworkTransaction::CopyConnectionAttemptsFromStreamRequest() {
1648 DCHECK(stream_request_
);
1650 // Since the transaction can restart with auth credentials, it may create a
1651 // stream more than once. Accumulate all of the connection attempts across
1652 // those streams by appending them to the vector:
1653 for (const auto& attempt
: stream_request_
->connection_attempts())
1654 connection_attempts_
.push_back(attempt
);