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.h"
17 #include "base/metrics/stats_counters.h"
18 #include "base/stl_util.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/time/time.h"
23 #include "base/values.h"
24 #include "build/build_config.h"
25 #include "net/base/auth.h"
26 #include "net/base/host_port_pair.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h"
31 #include "net/base/net_util.h"
32 #include "net/base/upload_data_stream.h"
33 #include "net/http/http_auth.h"
34 #include "net/http/http_auth_handler.h"
35 #include "net/http/http_auth_handler_factory.h"
36 #include "net/http/http_basic_stream.h"
37 #include "net/http/http_chunked_decoder.h"
38 #include "net/http/http_network_session.h"
39 #include "net/http/http_proxy_client_socket.h"
40 #include "net/http/http_proxy_client_socket_pool.h"
41 #include "net/http/http_request_headers.h"
42 #include "net/http/http_request_info.h"
43 #include "net/http/http_response_headers.h"
44 #include "net/http/http_response_info.h"
45 #include "net/http/http_server_properties.h"
46 #include "net/http/http_status_code.h"
47 #include "net/http/http_stream_base.h"
48 #include "net/http/http_stream_factory.h"
49 #include "net/http/http_util.h"
50 #include "net/http/transport_security_state.h"
51 #include "net/http/url_security_manager.h"
52 #include "net/socket/client_socket_factory.h"
53 #include "net/socket/socks_client_socket_pool.h"
54 #include "net/socket/ssl_client_socket.h"
55 #include "net/socket/ssl_client_socket_pool.h"
56 #include "net/socket/transport_client_socket_pool.h"
57 #include "net/spdy/spdy_http_stream.h"
58 #include "net/spdy/spdy_session.h"
59 #include "net/spdy/spdy_session_pool.h"
60 #include "net/ssl/ssl_cert_request_info.h"
61 #include "net/ssl/ssl_connection_status_flags.h"
70 void ProcessAlternateProtocol(
71 HttpStreamFactory
* factory
,
72 const base::WeakPtr
<HttpServerProperties
>& http_server_properties
,
73 const HttpResponseHeaders
& headers
,
74 const HostPortPair
& http_host_port_pair
) {
75 std::string alternate_protocol_str
;
77 if (!headers
.EnumerateHeader(NULL
, kAlternateProtocolHeader
,
78 &alternate_protocol_str
)) {
79 // Header is not present.
83 factory
->ProcessAlternateProtocol(http_server_properties
,
84 alternate_protocol_str
,
88 // Returns true if |error| is a client certificate authentication error.
89 bool IsClientCertificateError(int error
) {
91 case ERR_BAD_SSL_CLIENT_AUTH_CERT
:
92 case ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED
:
93 case ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY
:
94 case ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED
:
101 base::Value
* NetLogSSLVersionFallbackCallback(
104 uint16 version_before
,
105 uint16 version_after
,
106 NetLog::LogLevel
/* log_level */) {
107 base::DictionaryValue
* dict
= new base::DictionaryValue();
108 dict
->SetString("host_and_port", GetHostAndPort(*url
));
109 dict
->SetInteger("net_error", net_error
);
110 dict
->SetInteger("version_before", version_before
);
111 dict
->SetInteger("version_after", version_after
);
117 //-----------------------------------------------------------------------------
119 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
120 HttpNetworkSession
* session
)
121 : pending_auth_target_(HttpAuth::AUTH_NONE
),
122 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
123 base::Unretained(this))),
127 headers_valid_(false),
128 logged_response_time_(false),
129 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
132 next_state_(STATE_NONE
),
133 establishing_tunnel_(false),
134 websocket_handshake_stream_base_create_helper_(NULL
) {
135 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
136 if (session
->http_stream_factory()->has_next_protos()) {
137 server_ssl_config_
.next_protos
=
138 session
->http_stream_factory()->next_protos();
140 proxy_ssl_config_
= server_ssl_config_
;
143 HttpNetworkTransaction::~HttpNetworkTransaction() {
145 HttpResponseHeaders
* headers
= GetResponseHeaders();
146 // TODO(mbelshe): The stream_ should be able to compute whether or not the
147 // stream should be kept alive. No reason to compute here
149 bool try_to_keep_alive
=
150 next_state_
== STATE_NONE
&&
151 stream_
->CanFindEndOfResponse() &&
152 (!headers
|| headers
->IsKeepAlive());
153 if (!try_to_keep_alive
) {
154 stream_
->Close(true /* not reusable */);
156 if (stream_
->IsResponseBodyComplete()) {
157 // If the response body is complete, we can just reuse the socket.
158 stream_
->Close(false /* reusable */);
159 } else if (stream_
->IsSpdyHttpStream()) {
160 // Doesn't really matter for SpdyHttpStream. Just close it.
161 stream_
->Close(true /* not reusable */);
163 // Otherwise, we try to drain the response body.
164 HttpStreamBase
* stream
= stream_
.release();
165 stream
->Drain(session_
);
170 if (request_
&& request_
->upload_data_stream
)
171 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
174 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
175 const CompletionCallback
& callback
,
176 const BoundNetLog
& net_log
) {
177 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
180 request_
= request_info
;
181 start_time_
= base::Time::Now();
183 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
184 server_ssl_config_
.rev_checking_enabled
= false;
185 proxy_ssl_config_
.rev_checking_enabled
= false;
188 // Channel ID is enabled unless --disable-tls-channel-id flag is set,
189 // or if privacy mode is enabled.
190 bool channel_id_enabled
= server_ssl_config_
.channel_id_enabled
&&
191 (request_
->privacy_mode
== kPrivacyModeDisabled
);
192 server_ssl_config_
.channel_id_enabled
= channel_id_enabled
;
194 next_state_
= STATE_CREATE_STREAM
;
196 if (rv
== ERR_IO_PENDING
)
197 callback_
= callback
;
201 int HttpNetworkTransaction::RestartIgnoringLastError(
202 const CompletionCallback
& callback
) {
203 DCHECK(!stream_
.get());
204 DCHECK(!stream_request_
.get());
205 DCHECK_EQ(STATE_NONE
, next_state_
);
207 next_state_
= STATE_CREATE_STREAM
;
210 if (rv
== ERR_IO_PENDING
)
211 callback_
= callback
;
215 int HttpNetworkTransaction::RestartWithCertificate(
216 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
217 // In HandleCertificateRequest(), we always tear down existing stream
218 // requests to force a new connection. So we shouldn't have one here.
219 DCHECK(!stream_request_
.get());
220 DCHECK(!stream_
.get());
221 DCHECK_EQ(STATE_NONE
, next_state_
);
223 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
224 &proxy_ssl_config_
: &server_ssl_config_
;
225 ssl_config
->send_client_cert
= true;
226 ssl_config
->client_cert
= client_cert
;
227 session_
->ssl_client_auth_cache()->Add(
228 response_
.cert_request_info
->host_and_port
, client_cert
);
229 // Reset the other member variables.
230 // Note: this is necessary only with SSL renegotiation.
231 ResetStateForRestart();
232 next_state_
= STATE_CREATE_STREAM
;
234 if (rv
== ERR_IO_PENDING
)
235 callback_
= callback
;
239 int HttpNetworkTransaction::RestartWithAuth(
240 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
241 HttpAuth::Target target
= pending_auth_target_
;
242 if (target
== HttpAuth::AUTH_NONE
) {
244 return ERR_UNEXPECTED
;
246 pending_auth_target_
= HttpAuth::AUTH_NONE
;
248 auth_controllers_
[target
]->ResetAuth(credentials
);
250 DCHECK(callback_
.is_null());
253 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
254 // In this case, we've gathered credentials for use with proxy
255 // authentication of a tunnel.
256 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
257 DCHECK(stream_request_
!= NULL
);
258 auth_controllers_
[target
] = NULL
;
259 ResetStateForRestart();
260 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
262 // In this case, we've gathered credentials for the server or the proxy
263 // but it is not during the tunneling phase.
264 DCHECK(stream_request_
== NULL
);
265 PrepareForAuthRestart(target
);
269 if (rv
== ERR_IO_PENDING
)
270 callback_
= callback
;
274 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
275 DCHECK(HaveAuth(target
));
276 DCHECK(!stream_request_
.get());
278 bool keep_alive
= false;
279 // Even if the server says the connection is keep-alive, we have to be
280 // able to find the end of each response in order to reuse the connection.
281 if (GetResponseHeaders()->IsKeepAlive() &&
282 stream_
->CanFindEndOfResponse()) {
283 // If the response body hasn't been completely read, we need to drain
285 if (!stream_
->IsResponseBodyComplete()) {
286 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
287 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
288 read_buf_len_
= kDrainBodyBufferSize
;
294 // We don't need to drain the response body, so we act as if we had drained
295 // the response body.
296 DidDrainBodyForAuthRestart(keep_alive
);
299 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
300 DCHECK(!stream_request_
.get());
303 HttpStream
* new_stream
= NULL
;
304 if (keep_alive
&& stream_
->IsConnectionReusable()) {
305 // We should call connection_->set_idle_time(), but this doesn't occur
306 // often enough to be worth the trouble.
307 stream_
->SetConnectionReused();
309 static_cast<HttpStream
*>(stream_
.get())->RenewStreamForAuth();
313 // Close the stream and mark it as not_reusable. Even in the
314 // keep_alive case, we've determined that the stream_ is not
315 // reusable if new_stream is NULL.
316 stream_
->Close(true);
317 next_state_
= STATE_CREATE_STREAM
;
319 next_state_
= STATE_INIT_STREAM
;
321 stream_
.reset(new_stream
);
324 // Reset the other member variables.
325 ResetStateForAuthRestart();
328 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
329 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
330 HaveAuth(pending_auth_target_
);
333 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
334 const CompletionCallback
& callback
) {
336 DCHECK_LT(0, buf_len
);
338 State next_state
= STATE_NONE
;
340 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
341 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
342 // We're trying to read the body of the response but we're still trying
343 // to establish an SSL tunnel through an HTTP proxy. We can't read these
344 // bytes when establishing a tunnel because they might be controlled by
345 // an active network attacker. We don't worry about this for HTTP
346 // because an active network attacker can already control HTTP sessions.
347 // We reach this case when the user cancels a 407 proxy auth prompt. We
348 // also don't worry about this for an HTTPS Proxy, because the
349 // communication with the proxy is secure.
350 // See http://crbug.com/8473.
351 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
352 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
353 LOG(WARNING
) << "Blocked proxy response with status "
354 << headers
->response_code() << " to CONNECT request for "
355 << GetHostAndPort(request_
->url
) << ".";
356 return ERR_TUNNEL_CONNECTION_FAILED
;
359 // Are we using SPDY or HTTP?
360 next_state
= STATE_READ_BODY
;
363 read_buf_len_
= buf_len
;
365 next_state_
= next_state
;
367 if (rv
== ERR_IO_PENDING
)
368 callback_
= callback
;
372 bool HttpNetworkTransaction::GetFullRequestHeaders(
373 HttpRequestHeaders
* headers
) const {
374 // TODO(ttuttle): Make sure we've populated request_headers_.
375 *headers
= request_headers_
;
379 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
380 return ((headers_valid_
&& response_
.headers
.get()) ||
381 response_
.ssl_info
.cert
.get() || response_
.cert_request_info
.get())
386 LoadState
HttpNetworkTransaction::GetLoadState() const {
387 // TODO(wtc): Define a new LoadState value for the
388 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
389 switch (next_state_
) {
390 case STATE_CREATE_STREAM_COMPLETE
:
391 return stream_request_
->GetLoadState();
392 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
393 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
394 case STATE_SEND_REQUEST_COMPLETE
:
395 return LOAD_STATE_SENDING_REQUEST
;
396 case STATE_READ_HEADERS_COMPLETE
:
397 return LOAD_STATE_WAITING_FOR_RESPONSE
;
398 case STATE_READ_BODY_COMPLETE
:
399 return LOAD_STATE_READING_RESPONSE
;
401 return LOAD_STATE_IDLE
;
405 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
407 return UploadProgress();
409 // TODO(bashi): This cast is temporary. Remove later.
410 return static_cast<HttpStream
*>(stream_
.get())->GetUploadProgress();
413 bool HttpNetworkTransaction::GetLoadTimingInfo(
414 LoadTimingInfo
* load_timing_info
) const {
415 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
418 load_timing_info
->proxy_resolve_start
=
419 proxy_info_
.proxy_resolve_start_time();
420 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
421 load_timing_info
->send_start
= send_start_time_
;
422 load_timing_info
->send_end
= send_end_time_
;
426 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
427 priority_
= priority
;
429 stream_request_
->SetPriority(priority
);
431 stream_
->SetPriority(priority
);
434 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
435 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
436 websocket_handshake_stream_base_create_helper_
= create_helper
;
439 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
440 const ProxyInfo
& used_proxy_info
,
441 HttpStreamBase
* stream
) {
442 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
443 DCHECK(stream_request_
.get());
445 stream_
.reset(stream
);
446 server_ssl_config_
= used_ssl_config
;
447 proxy_info_
= used_proxy_info
;
448 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
449 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
450 stream_request_
->protocol_negotiated());
451 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
452 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
457 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
458 const SSLConfig
& used_ssl_config
,
459 const ProxyInfo
& used_proxy_info
,
460 WebSocketHandshakeStreamBase
* stream
) {
461 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
464 void HttpNetworkTransaction::OnStreamFailed(int result
,
465 const SSLConfig
& used_ssl_config
) {
466 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
467 DCHECK_NE(OK
, result
);
468 DCHECK(stream_request_
.get());
469 DCHECK(!stream_
.get());
470 server_ssl_config_
= used_ssl_config
;
472 OnIOComplete(result
);
475 void HttpNetworkTransaction::OnCertificateError(
477 const SSLConfig
& used_ssl_config
,
478 const SSLInfo
& ssl_info
) {
479 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
480 DCHECK_NE(OK
, result
);
481 DCHECK(stream_request_
.get());
482 DCHECK(!stream_
.get());
484 response_
.ssl_info
= ssl_info
;
485 server_ssl_config_
= used_ssl_config
;
487 // TODO(mbelshe): For now, we're going to pass the error through, and that
488 // will close the stream_request in all cases. This means that we're always
489 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
490 // good and the user chooses to ignore the error. This is not ideal, but not
491 // the end of the world either.
493 OnIOComplete(result
);
496 void HttpNetworkTransaction::OnNeedsProxyAuth(
497 const HttpResponseInfo
& proxy_response
,
498 const SSLConfig
& used_ssl_config
,
499 const ProxyInfo
& used_proxy_info
,
500 HttpAuthController
* auth_controller
) {
501 DCHECK(stream_request_
.get());
502 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
504 establishing_tunnel_
= true;
505 response_
.headers
= proxy_response
.headers
;
506 response_
.auth_challenge
= proxy_response
.auth_challenge
;
507 headers_valid_
= true;
508 server_ssl_config_
= used_ssl_config
;
509 proxy_info_
= used_proxy_info
;
511 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
512 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
517 void HttpNetworkTransaction::OnNeedsClientAuth(
518 const SSLConfig
& used_ssl_config
,
519 SSLCertRequestInfo
* cert_info
) {
520 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
522 server_ssl_config_
= used_ssl_config
;
523 response_
.cert_request_info
= cert_info
;
524 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
527 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
528 const HttpResponseInfo
& response_info
,
529 const SSLConfig
& used_ssl_config
,
530 const ProxyInfo
& used_proxy_info
,
531 HttpStreamBase
* stream
) {
532 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
534 headers_valid_
= true;
535 response_
= response_info
;
536 server_ssl_config_
= used_ssl_config
;
537 proxy_info_
= used_proxy_info
;
538 stream_
.reset(stream
);
539 stream_request_
.reset(); // we're done with the stream request
540 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
543 bool HttpNetworkTransaction::is_https_request() const {
544 return request_
->url
.SchemeIs("https");
547 void HttpNetworkTransaction::DoCallback(int rv
) {
548 DCHECK_NE(rv
, ERR_IO_PENDING
);
549 DCHECK(!callback_
.is_null());
551 // Since Run may result in Read being called, clear user_callback_ up front.
552 CompletionCallback c
= callback_
;
557 void HttpNetworkTransaction::OnIOComplete(int result
) {
558 int rv
= DoLoop(result
);
559 if (rv
!= ERR_IO_PENDING
)
563 int HttpNetworkTransaction::DoLoop(int result
) {
564 DCHECK(next_state_
!= STATE_NONE
);
568 State state
= next_state_
;
569 next_state_
= STATE_NONE
;
571 case STATE_CREATE_STREAM
:
573 rv
= DoCreateStream();
575 case STATE_CREATE_STREAM_COMPLETE
:
576 rv
= DoCreateStreamComplete(rv
);
578 case STATE_INIT_STREAM
:
582 case STATE_INIT_STREAM_COMPLETE
:
583 rv
= DoInitStreamComplete(rv
);
585 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
587 rv
= DoGenerateProxyAuthToken();
589 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
590 rv
= DoGenerateProxyAuthTokenComplete(rv
);
592 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
594 rv
= DoGenerateServerAuthToken();
596 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
597 rv
= DoGenerateServerAuthTokenComplete(rv
);
599 case STATE_INIT_REQUEST_BODY
:
601 rv
= DoInitRequestBody();
603 case STATE_INIT_REQUEST_BODY_COMPLETE
:
604 rv
= DoInitRequestBodyComplete(rv
);
606 case STATE_BUILD_REQUEST
:
608 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
609 rv
= DoBuildRequest();
611 case STATE_BUILD_REQUEST_COMPLETE
:
612 rv
= DoBuildRequestComplete(rv
);
614 case STATE_SEND_REQUEST
:
616 rv
= DoSendRequest();
618 case STATE_SEND_REQUEST_COMPLETE
:
619 rv
= DoSendRequestComplete(rv
);
620 net_log_
.EndEventWithNetErrorCode(
621 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
623 case STATE_READ_HEADERS
:
625 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
626 rv
= DoReadHeaders();
628 case STATE_READ_HEADERS_COMPLETE
:
629 rv
= DoReadHeadersComplete(rv
);
630 net_log_
.EndEventWithNetErrorCode(
631 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
633 case STATE_READ_BODY
:
635 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
638 case STATE_READ_BODY_COMPLETE
:
639 rv
= DoReadBodyComplete(rv
);
640 net_log_
.EndEventWithNetErrorCode(
641 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
643 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
646 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
647 rv
= DoDrainBodyForAuthRestart();
649 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
650 rv
= DoDrainBodyForAuthRestartComplete(rv
);
651 net_log_
.EndEventWithNetErrorCode(
652 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
655 NOTREACHED() << "bad state";
659 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
664 int HttpNetworkTransaction::DoCreateStream() {
665 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
667 if (ForWebSocketHandshake()) {
668 stream_request_
.reset(
669 session_
->http_stream_factory_for_websocket()
670 ->RequestWebSocketHandshakeStream(
676 websocket_handshake_stream_base_create_helper_
,
679 stream_request_
.reset(
680 session_
->http_stream_factory()->RequestStream(
688 DCHECK(stream_request_
.get());
689 return ERR_IO_PENDING
;
692 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
694 next_state_
= STATE_INIT_STREAM
;
695 DCHECK(stream_
.get());
696 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
697 result
= HandleCertificateRequest(result
);
698 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
699 // Return OK and let the caller read the proxy's error page
700 next_state_
= STATE_NONE
;
704 // Handle possible handshake errors that may have occurred if the stream
705 // used SSL for one or more of the layers.
706 result
= HandleSSLHandshakeError(result
);
708 // At this point we are done with the stream_request_.
709 stream_request_
.reset();
713 int HttpNetworkTransaction::DoInitStream() {
714 DCHECK(stream_
.get());
715 next_state_
= STATE_INIT_STREAM_COMPLETE
;
716 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
719 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
721 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
724 result
= HandleIOError(result
);
726 // The stream initialization failed, so this stream will never be useful.
733 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
734 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
735 if (!ShouldApplyProxyAuth())
737 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
738 if (!auth_controllers_
[target
].get())
739 auth_controllers_
[target
] =
740 new HttpAuthController(target
,
742 session_
->http_auth_cache(),
743 session_
->http_auth_handler_factory());
744 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
749 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
750 DCHECK_NE(ERR_IO_PENDING
, rv
);
752 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
756 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
757 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
758 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
759 if (!auth_controllers_
[target
].get()) {
760 auth_controllers_
[target
] =
761 new HttpAuthController(target
,
763 session_
->http_auth_cache(),
764 session_
->http_auth_handler_factory());
765 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
766 auth_controllers_
[target
]->DisableEmbeddedIdentity();
768 if (!ShouldApplyServerAuth())
770 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
775 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
776 DCHECK_NE(ERR_IO_PENDING
, rv
);
778 next_state_
= STATE_INIT_REQUEST_BODY
;
782 void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy
) {
783 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
784 GetHostAndOptionalPort(request_
->url
));
786 // For compat with HTTP/1.0 servers and proxies:
788 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
791 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
794 // Add a content length header?
795 if (request_
->upload_data_stream
) {
796 if (request_
->upload_data_stream
->is_chunked()) {
797 request_headers_
.SetHeader(
798 HttpRequestHeaders::kTransferEncoding
, "chunked");
800 request_headers_
.SetHeader(
801 HttpRequestHeaders::kContentLength
,
802 base::Uint64ToString(request_
->upload_data_stream
->size()));
804 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
805 request_
->method
== "HEAD") {
806 // An empty POST/PUT request still needs a content length. As for HEAD,
807 // IE and Safari also add a content length header. Presumably it is to
808 // support sending a HEAD request to an URL that only expects to be sent a
809 // POST or some other method that normally would have a message body.
810 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
813 // Honor load flags that impact proxy caches.
814 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
815 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
816 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
817 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
818 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
821 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
822 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
824 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
825 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
828 request_headers_
.MergeFrom(request_
->extra_headers
);
829 response_
.did_use_http_auth
=
830 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
831 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
834 int HttpNetworkTransaction::DoInitRequestBody() {
835 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
837 if (request_
->upload_data_stream
)
838 rv
= request_
->upload_data_stream
->Init(io_callback_
);
842 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
844 next_state_
= STATE_BUILD_REQUEST
;
848 int HttpNetworkTransaction::DoBuildRequest() {
849 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
850 headers_valid_
= false;
852 // This is constructed lazily (instead of within our Start method), so that
853 // we have proxy info available.
854 if (request_headers_
.IsEmpty()) {
855 bool using_proxy
= (proxy_info_
.is_http() || proxy_info_
.is_https()) &&
857 BuildRequestHeaders(using_proxy
);
863 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
865 next_state_
= STATE_SEND_REQUEST
;
869 int HttpNetworkTransaction::DoSendRequest() {
870 send_start_time_
= base::TimeTicks::Now();
871 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
873 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
876 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
877 send_end_time_
= base::TimeTicks::Now();
879 return HandleIOError(result
);
880 response_
.network_accessed
= true;
881 next_state_
= STATE_READ_HEADERS
;
885 int HttpNetworkTransaction::DoReadHeaders() {
886 next_state_
= STATE_READ_HEADERS_COMPLETE
;
887 return stream_
->ReadResponseHeaders(io_callback_
);
890 int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() {
891 if (!response_
.headers
.get() && !stream_
->IsConnectionReused()) {
892 // The connection was closed before any data was sent. Likely an error
893 // rather than empty HTTP/0.9 response.
894 return ERR_EMPTY_RESPONSE
;
900 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
901 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
902 // due to SSL renegotiation.
903 if (IsCertificateError(result
)) {
904 // We don't handle a certificate error during SSL renegotiation, so we
905 // have to return an error that's not in the certificate error range
907 LOG(ERROR
) << "Got a server certificate with error " << result
908 << " during SSL renegotiation";
909 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
910 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
911 // TODO(wtc): Need a test case for this code path!
912 DCHECK(stream_
.get());
913 DCHECK(is_https_request());
914 response_
.cert_request_info
= new SSLCertRequestInfo
;
915 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
916 result
= HandleCertificateRequest(result
);
921 if (result
== ERR_QUIC_HANDSHAKE_FAILED
) {
922 ResetConnectionAndRequestForResend();
926 if (result
< 0 && result
!= ERR_CONNECTION_CLOSED
)
927 return HandleIOError(result
);
929 if (result
== ERR_CONNECTION_CLOSED
&& ShouldResendRequest(result
)) {
930 ResetConnectionAndRequestForResend();
934 // After we call RestartWithAuth a new response_time will be recorded, and
935 // we need to be cautious about incorrectly logging the duration across the
936 // authentication activity.
938 LogTransactionConnectedMetrics();
940 if (result
== ERR_CONNECTION_CLOSED
) {
941 // For now, if we get at least some data, we do the best we can to make
942 // sense of it and send it back up the stack.
943 int rv
= HandleConnectionClosedBeforeEndOfHeaders();
947 DCHECK(response_
.headers
.get());
949 #if defined(SPDY_PROXY_AUTH_ORIGIN)
950 // Server-induced fallback; see: http://crbug.com/143712
951 if (response_
.was_fetched_via_proxy
&& response_
.headers
.get() != NULL
) {
952 ProxyService::DataReductionProxyBypassEventType proxy_bypass_event
=
953 ProxyService::BYPASS_EVENT_TYPE_MAX
;
954 base::TimeDelta bypass_duration
;
955 bool chrome_proxy_used
=
956 proxy_info_
.proxy_server().isDataReductionProxy();
957 bool chrome_fallback_proxy_used
= false;
958 #if defined(DATA_REDUCTION_FALLBACK_HOST)
959 if (!chrome_proxy_used
) {
960 chrome_fallback_proxy_used
=
961 proxy_info_
.proxy_server().isDataReductionProxyFallback();
965 if (chrome_proxy_used
|| chrome_fallback_proxy_used
) {
966 if (response_
.headers
->GetChromeProxyInfo(&bypass_duration
)) {
968 (bypass_duration
< base::TimeDelta::FromMinutes(30) ?
969 ProxyService::SHORT_BYPASS
:
970 ProxyService::LONG_BYPASS
);
972 // Additionally, fallback if a 500 or 502 is returned via the data
973 // reduction proxy. This is conservative, as the 500 or 502 might have
974 // been generated by the origin, and not the proxy.
975 if (response_
.headers
->response_code() == HTTP_INTERNAL_SERVER_ERROR
||
976 response_
.headers
->response_code() == HTTP_BAD_GATEWAY
||
977 response_
.headers
->response_code() == HTTP_SERVICE_UNAVAILABLE
) {
978 proxy_bypass_event
= ProxyService::INTERNAL_SERVER_ERROR_BYPASS
;
982 if (proxy_bypass_event
< ProxyService::BYPASS_EVENT_TYPE_MAX
) {
983 ProxyService
* proxy_service
= session_
->proxy_service();
985 proxy_service
->RecordDataReductionProxyBypassInfo(
986 chrome_proxy_used
, proxy_info_
.proxy_server(), proxy_bypass_event
);
988 if (proxy_service
->MarkProxyAsBad(proxy_info_
, bypass_duration
,
990 // Only retry in the case of GETs. We don't want to resubmit a POST
991 // if the proxy took some action.
992 if (request_
->method
== "GET") {
993 ResetConnectionAndRequestForResend();
1002 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1003 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1004 const int response_code
= response_
.headers
->response_code();
1005 UMA_HISTOGRAM_ENUMERATION(
1006 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1010 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1011 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1013 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1014 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1015 // indicates a buggy server. See:
1016 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1017 if (request_
->method
== "PUT")
1018 return ERR_METHOD_NOT_SUPPORTED
;
1021 // Check for an intermediate 100 Continue response. An origin server is
1022 // allowed to send this response even if we didn't ask for it, so we just
1023 // need to skip over it.
1024 // We treat any other 1xx in this same way (although in practice getting
1025 // a 1xx that isn't a 100 is rare).
1026 // Unless this is a WebSocket request, in which case we pass it on up.
1027 if (response_
.headers
->response_code() / 100 == 1 &&
1028 !ForWebSocketHandshake()) {
1029 response_
.headers
= new HttpResponseHeaders(std::string());
1030 next_state_
= STATE_READ_HEADERS
;
1034 HostPortPair endpoint
= HostPortPair(request_
->url
.HostNoBrackets(),
1035 request_
->url
.EffectiveIntPort());
1036 ProcessAlternateProtocol(session_
->http_stream_factory(),
1037 session_
->http_server_properties(),
1038 *response_
.headers
.get(),
1041 int rv
= HandleAuthChallenge();
1045 if (is_https_request())
1046 stream_
->GetSSLInfo(&response_
.ssl_info
);
1048 headers_valid_
= true;
1052 int HttpNetworkTransaction::DoReadBody() {
1053 DCHECK(read_buf_
.get());
1054 DCHECK_GT(read_buf_len_
, 0);
1055 DCHECK(stream_
!= NULL
);
1057 next_state_
= STATE_READ_BODY_COMPLETE
;
1058 return stream_
->ReadResponseBody(
1059 read_buf_
.get(), read_buf_len_
, io_callback_
);
1062 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1063 // We are done with the Read call.
1066 DCHECK_NE(ERR_IO_PENDING
, result
);
1070 bool keep_alive
= false;
1071 if (stream_
->IsResponseBodyComplete()) {
1072 // Note: Just because IsResponseBodyComplete is true, we're not
1073 // necessarily "done". We're only "done" when it is the last
1074 // read on this HttpNetworkTransaction, which will be signified
1075 // by a zero-length read.
1076 // TODO(mbelshe): The keepalive property is really a property of
1077 // the stream. No need to compute it here just to pass back
1078 // to the stream's Close function.
1079 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1081 if (stream_
->CanFindEndOfResponse()) {
1082 HttpResponseHeaders
* headers
= GetResponseHeaders();
1084 keep_alive
= headers
->IsKeepAlive();
1088 // Clean up connection if we are done.
1090 LogTransactionMetrics();
1091 stream_
->Close(!keep_alive
);
1092 // Note: we don't reset the stream here. We've closed it, but we still
1093 // need it around so that callers can call methods such as
1094 // GetUploadProgress() and have them be meaningful.
1095 // TODO(mbelshe): This means we closed the stream here, and we close it
1096 // again in ~HttpNetworkTransaction. Clean that up.
1098 // The next Read call will return 0 (EOF).
1101 // Clear these to avoid leaving around old state.
1108 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1109 // This method differs from DoReadBody only in the next_state_. So we just
1110 // call DoReadBody and override the next_state_. Perhaps there is a more
1111 // elegant way for these two methods to share code.
1112 int rv
= DoReadBody();
1113 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1114 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1118 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1119 // the same. Figure out a good way for these two methods to share code.
1120 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1121 // keep_alive defaults to true because the very reason we're draining the
1122 // response body is to reuse the connection for auth restart.
1123 bool done
= false, keep_alive
= true;
1125 // Error or closed connection while reading the socket.
1128 } else if (stream_
->IsResponseBodyComplete()) {
1133 DidDrainBodyForAuthRestart(keep_alive
);
1136 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1142 void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1143 if (logged_response_time_
)
1146 logged_response_time_
= true;
1148 base::TimeDelta total_duration
= response_
.response_time
- start_time_
;
1150 UMA_HISTOGRAM_CUSTOM_TIMES(
1151 "Net.Transaction_Connected",
1153 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1156 bool reused_socket
= stream_
->IsConnectionReused();
1157 if (!reused_socket
) {
1158 UMA_HISTOGRAM_CUSTOM_TIMES(
1159 "Net.Transaction_Connected_New_b",
1161 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1165 // Currently, non-HIGHEST priority requests are frame or sub-frame resource
1166 // types. This will change when we also prioritize certain subresources like
1168 if (priority_
!= HIGHEST
) {
1169 UMA_HISTOGRAM_CUSTOM_TIMES(
1170 "Net.Priority_High_Latency_b",
1172 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1175 UMA_HISTOGRAM_CUSTOM_TIMES(
1176 "Net.Priority_Low_Latency_b",
1178 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1183 void HttpNetworkTransaction::LogTransactionMetrics() const {
1184 base::TimeDelta duration
= base::Time::Now() -
1185 response_
.request_time
;
1186 if (60 < duration
.InMinutes())
1189 base::TimeDelta total_duration
= base::Time::Now() - start_time_
;
1191 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration
,
1192 base::TimeDelta::FromMilliseconds(1),
1193 base::TimeDelta::FromMinutes(10),
1195 UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1197 base::TimeDelta::FromMilliseconds(1),
1198 base::TimeDelta::FromMinutes(10), 100);
1200 if (!stream_
->IsConnectionReused()) {
1201 UMA_HISTOGRAM_CUSTOM_TIMES(
1202 "Net.Transaction_Latency_Total_New_Connection",
1203 total_duration
, base::TimeDelta::FromMilliseconds(1),
1204 base::TimeDelta::FromMinutes(10), 100);
1208 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1209 // There are two paths through which the server can request a certificate
1210 // from us. The first is during the initial handshake, the second is
1211 // during SSL renegotiation.
1213 // In both cases, we want to close the connection before proceeding.
1214 // We do this for two reasons:
1215 // First, we don't want to keep the connection to the server hung for a
1216 // long time while the user selects a certificate.
1217 // Second, even if we did keep the connection open, NSS has a bug where
1218 // restarting the handshake for ClientAuth is currently broken.
1219 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1221 if (stream_
.get()) {
1222 // Since we already have a stream, we're being called as part of SSL
1224 DCHECK(!stream_request_
.get());
1225 stream_
->Close(true);
1229 // The server is asking for a client certificate during the initial
1231 stream_request_
.reset();
1233 // If the user selected one of the certificates in client_certs or declined
1234 // to provide one for this server before, use the past decision
1236 scoped_refptr
<X509Certificate
> client_cert
;
1237 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1238 response_
.cert_request_info
->host_and_port
, &client_cert
);
1239 if (!found_cached_cert
)
1242 // Check that the certificate selected is still a certificate the server
1243 // is likely to accept, based on the criteria supplied in the
1244 // CertificateRequest message.
1245 if (client_cert
.get()) {
1246 const std::vector
<std::string
>& cert_authorities
=
1247 response_
.cert_request_info
->cert_authorities
;
1249 bool cert_still_valid
= cert_authorities
.empty() ||
1250 client_cert
->IsIssuedByEncoded(cert_authorities
);
1251 if (!cert_still_valid
)
1255 // TODO(davidben): Add a unit test which covers this path; we need to be
1256 // able to send a legitimate certificate and also bypass/clear the
1257 // SSL session cache.
1258 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1259 &proxy_ssl_config_
: &server_ssl_config_
;
1260 ssl_config
->send_client_cert
= true;
1261 ssl_config
->client_cert
= client_cert
;
1262 next_state_
= STATE_CREATE_STREAM
;
1263 // Reset the other member variables.
1264 // Note: this is necessary only with SSL renegotiation.
1265 ResetStateForRestart();
1269 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1270 if (server_ssl_config_
.send_client_cert
&&
1271 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1272 session_
->ssl_client_auth_cache()->Remove(
1273 GetHostAndPort(request_
->url
));
1277 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1278 // being used, as all of the errors are handled as if they were generated
1279 // by the endpoint host, request_->url, rather than considering if they were
1280 // generated by the SSL proxy. http://crbug.com/69329
1281 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1283 HandleClientAuthError(error
);
1285 bool should_fallback
= false;
1286 uint16 version_max
= server_ssl_config_
.version_max
;
1289 case ERR_SSL_PROTOCOL_ERROR
:
1290 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1291 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1292 version_max
> server_ssl_config_
.version_min
) {
1293 // This could be a TLS-intolerant server or a server that chose a
1294 // cipher suite defined only for higher protocol versions (such as
1295 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1296 // back to the next lower version and retry.
1297 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1298 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1299 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1300 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1301 // version_max should match the maximum protocol version supported
1302 // by the SSLClientSocket class.
1305 // Fallback to the lower SSL version.
1306 // While SSL 3.0 fallback should be eliminated because of security
1307 // reasons, there is a high risk of breaking the servers if this is
1309 should_fallback
= true;
1312 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1313 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1314 version_max
> server_ssl_config_
.version_min
) {
1315 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1316 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1317 // crbug.com/260358. In order to make the fallback as minimal as
1318 // possible, this fallback is only triggered for >= TLS 1.1.
1320 should_fallback
= true;
1323 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1324 // The server told us that we should not have fallen back. A buggy server
1325 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1326 // connection. |fallback_error_code_| is initialised to
1327 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1328 error
= fallback_error_code_
;
1332 if (should_fallback
) {
1334 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1335 base::Bind(&NetLogSSLVersionFallbackCallback
,
1336 &request_
->url
, error
, server_ssl_config_
.version_max
,
1338 fallback_error_code_
= error
;
1339 server_ssl_config_
.version_max
= version_max
;
1340 server_ssl_config_
.version_fallback
= true;
1341 ResetConnectionAndRequestForResend();
1348 // This method determines whether it is safe to resend the request after an
1349 // IO error. It can only be called in response to request header or body
1350 // write errors or response header read errors. It should not be used in
1351 // other cases, such as a Connect error.
1352 int HttpNetworkTransaction::HandleIOError(int error
) {
1353 // Because the peer may request renegotiation with client authentication at
1354 // any time, check and handle client authentication errors.
1355 HandleClientAuthError(error
);
1358 // If we try to reuse a connection that the server is in the process of
1359 // closing, we may end up successfully writing out our request (or a
1360 // portion of our request) only to find a connection error when we try to
1361 // read from (or finish writing to) the socket.
1362 case ERR_CONNECTION_RESET
:
1363 case ERR_CONNECTION_CLOSED
:
1364 case ERR_CONNECTION_ABORTED
:
1365 // There can be a race between the socket pool checking checking whether a
1366 // socket is still connected, receiving the FIN, and sending/reading data
1367 // on a reused socket. If we receive the FIN between the connectedness
1368 // check and writing/reading from the socket, we may first learn the socket
1369 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1370 // likely happen when trying to retrieve its IP address.
1371 // See http://crbug.com/105824 for more details.
1372 case ERR_SOCKET_NOT_CONNECTED
:
1373 if (ShouldResendRequest(error
)) {
1374 net_log_
.AddEventWithNetErrorCode(
1375 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1376 ResetConnectionAndRequestForResend();
1380 case ERR_PIPELINE_EVICTION
:
1381 if (!session_
->force_http_pipelining()) {
1382 net_log_
.AddEventWithNetErrorCode(
1383 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1384 ResetConnectionAndRequestForResend();
1388 case ERR_SPDY_PING_FAILED
:
1389 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1390 case ERR_QUIC_HANDSHAKE_FAILED
:
1391 net_log_
.AddEventWithNetErrorCode(
1392 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1393 ResetConnectionAndRequestForResend();
1400 void HttpNetworkTransaction::ResetStateForRestart() {
1401 ResetStateForAuthRestart();
1405 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1406 send_start_time_
= base::TimeTicks();
1407 send_end_time_
= base::TimeTicks();
1409 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1412 headers_valid_
= false;
1413 request_headers_
.Clear();
1414 response_
= HttpResponseInfo();
1415 establishing_tunnel_
= false;
1418 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1419 return response_
.headers
.get();
1422 bool HttpNetworkTransaction::ShouldResendRequest(int error
) const {
1423 bool connection_is_proven
= stream_
->IsConnectionReused();
1424 bool has_received_headers
= GetResponseHeaders() != NULL
;
1426 // NOTE: we resend a request only if we reused a keep-alive connection.
1427 // This automatically prevents an infinite resend loop because we'll run
1428 // out of the cached keep-alive connections eventually.
1429 if (connection_is_proven
&& !has_received_headers
)
1434 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1435 if (stream_
.get()) {
1436 stream_
->Close(true);
1440 // We need to clear request_headers_ because it contains the real request
1441 // headers, but we may need to resend the CONNECT request first to recreate
1443 request_headers_
.Clear();
1444 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1447 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1448 return !is_https_request() &&
1449 (proxy_info_
.is_https() || proxy_info_
.is_http());
1452 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1453 return !(request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
);
1456 int HttpNetworkTransaction::HandleAuthChallenge() {
1457 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
1458 DCHECK(headers
.get());
1460 int status
= headers
->response_code();
1461 if (status
!= HTTP_UNAUTHORIZED
&&
1462 status
!= HTTP_PROXY_AUTHENTICATION_REQUIRED
)
1464 HttpAuth::Target target
= status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
?
1465 HttpAuth::AUTH_PROXY
: HttpAuth::AUTH_SERVER
;
1466 if (target
== HttpAuth::AUTH_PROXY
&& proxy_info_
.is_direct())
1467 return ERR_UNEXPECTED_PROXY_AUTH
;
1469 // This case can trigger when an HTTPS server responds with a "Proxy
1470 // authentication required" status code through a non-authenticating
1472 if (!auth_controllers_
[target
].get())
1473 return ERR_UNEXPECTED_PROXY_AUTH
;
1475 int rv
= auth_controllers_
[target
]->HandleAuthChallenge(
1476 headers
, (request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
) != 0, false,
1478 if (auth_controllers_
[target
]->HaveAuthHandler())
1479 pending_auth_target_
= target
;
1481 scoped_refptr
<AuthChallengeInfo
> auth_info
=
1482 auth_controllers_
[target
]->auth_info();
1483 if (auth_info
.get())
1484 response_
.auth_challenge
= auth_info
;
1489 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target
) const {
1490 return auth_controllers_
[target
].get() &&
1491 auth_controllers_
[target
]->HaveAuth();
1494 GURL
HttpNetworkTransaction::AuthURL(HttpAuth::Target target
) const {
1496 case HttpAuth::AUTH_PROXY
: {
1497 if (!proxy_info_
.proxy_server().is_valid() ||
1498 proxy_info_
.proxy_server().is_direct()) {
1499 return GURL(); // There is no proxy server.
1501 const char* scheme
= proxy_info_
.is_https() ? "https://" : "http://";
1502 return GURL(scheme
+
1503 proxy_info_
.proxy_server().host_port_pair().ToString());
1505 case HttpAuth::AUTH_SERVER
:
1506 return request_
->url
;
1512 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1513 return websocket_handshake_stream_base_create_helper_
&&
1514 request_
->url
.SchemeIsWSOrWSS();
1517 #define STATE_CASE(s) \
1519 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1522 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1523 std::string description
;
1525 STATE_CASE(STATE_CREATE_STREAM
);
1526 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1527 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1528 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1529 STATE_CASE(STATE_BUILD_REQUEST
);
1530 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1531 STATE_CASE(STATE_SEND_REQUEST
);
1532 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1533 STATE_CASE(STATE_READ_HEADERS
);
1534 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1535 STATE_CASE(STATE_READ_BODY
);
1536 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1537 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1538 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1539 STATE_CASE(STATE_NONE
);
1541 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,