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/profiler/scoped_tracker.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.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"
63 #include "url/url_canon.h"
69 void ProcessAlternateProtocol(
70 HttpNetworkSession
* session
,
71 const HttpResponseHeaders
& headers
,
72 const HostPortPair
& http_host_port_pair
) {
73 if (!headers
.HasHeader(kAlternateProtocolHeader
))
76 std::vector
<std::string
> alternate_protocol_values
;
78 std::string alternate_protocol_str
;
79 while (headers
.EnumerateHeader(&iter
, kAlternateProtocolHeader
,
80 &alternate_protocol_str
)) {
81 base::TrimWhitespaceASCII(alternate_protocol_str
, base::TRIM_ALL
,
82 &alternate_protocol_str
);
83 if (!alternate_protocol_str
.empty()) {
84 alternate_protocol_values
.push_back(alternate_protocol_str
);
88 session
->http_stream_factory()->ProcessAlternateProtocol(
89 session
->http_server_properties(),
90 alternate_protocol_values
,
95 base::Value
* NetLogSSLVersionFallbackCallback(
98 uint16 version_before
,
100 NetLog::LogLevel
/* log_level */) {
101 base::DictionaryValue
* dict
= new base::DictionaryValue();
102 dict
->SetString("host_and_port", GetHostAndPort(*url
));
103 dict
->SetInteger("net_error", net_error
);
104 dict
->SetInteger("version_before", version_before
);
105 dict
->SetInteger("version_after", version_after
);
111 //-----------------------------------------------------------------------------
113 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
114 HttpNetworkSession
* session
)
115 : pending_auth_target_(HttpAuth::AUTH_NONE
),
116 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
117 base::Unretained(this))),
121 headers_valid_(false),
122 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
125 total_received_bytes_(0),
126 next_state_(STATE_NONE
),
127 establishing_tunnel_(false),
128 websocket_handshake_stream_base_create_helper_(NULL
) {
129 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
130 session
->GetNextProtos(&server_ssl_config_
.next_protos
);
131 proxy_ssl_config_
= server_ssl_config_
;
134 HttpNetworkTransaction::~HttpNetworkTransaction() {
136 HttpResponseHeaders
* headers
= GetResponseHeaders();
137 // TODO(mbelshe): The stream_ should be able to compute whether or not the
138 // stream should be kept alive. No reason to compute here
140 bool try_to_keep_alive
=
141 next_state_
== STATE_NONE
&&
142 stream_
->CanFindEndOfResponse() &&
143 (!headers
|| headers
->IsKeepAlive());
144 if (!try_to_keep_alive
) {
145 stream_
->Close(true /* not reusable */);
147 if (stream_
->IsResponseBodyComplete()) {
148 // If the response body is complete, we can just reuse the socket.
149 stream_
->Close(false /* reusable */);
150 } else if (stream_
->IsSpdyHttpStream()) {
151 // Doesn't really matter for SpdyHttpStream. Just close it.
152 stream_
->Close(true /* not reusable */);
154 // Otherwise, we try to drain the response body.
155 HttpStream
* stream
= stream_
.release();
156 stream
->Drain(session_
);
161 if (request_
&& request_
->upload_data_stream
)
162 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
165 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
166 const CompletionCallback
& callback
,
167 const BoundNetLog
& net_log
) {
169 request_
= request_info
;
171 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
172 server_ssl_config_
.rev_checking_enabled
= false;
173 proxy_ssl_config_
.rev_checking_enabled
= false;
176 if (request_
->load_flags
& LOAD_PREFETCH
)
177 response_
.unused_since_prefetch
= true;
179 // Channel ID is disabled if privacy mode is enabled for this request.
180 if (request_
->privacy_mode
== PRIVACY_MODE_ENABLED
)
181 server_ssl_config_
.channel_id_enabled
= false;
183 if (server_ssl_config_
.fastradio_padding_enabled
) {
184 server_ssl_config_
.fastradio_padding_eligible
=
185 session_
->ssl_config_service()->SupportsFastradioPadding(
189 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
191 if (rv
== ERR_IO_PENDING
)
192 callback_
= callback
;
196 int HttpNetworkTransaction::RestartIgnoringLastError(
197 const CompletionCallback
& callback
) {
198 DCHECK(!stream_
.get());
199 DCHECK(!stream_request_
.get());
200 DCHECK_EQ(STATE_NONE
, next_state_
);
202 next_state_
= STATE_CREATE_STREAM
;
205 if (rv
== ERR_IO_PENDING
)
206 callback_
= callback
;
210 int HttpNetworkTransaction::RestartWithCertificate(
211 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
212 // In HandleCertificateRequest(), we always tear down existing stream
213 // requests to force a new connection. So we shouldn't have one here.
214 DCHECK(!stream_request_
.get());
215 DCHECK(!stream_
.get());
216 DCHECK_EQ(STATE_NONE
, next_state_
);
218 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
219 &proxy_ssl_config_
: &server_ssl_config_
;
220 ssl_config
->send_client_cert
= true;
221 ssl_config
->client_cert
= client_cert
;
222 session_
->ssl_client_auth_cache()->Add(
223 response_
.cert_request_info
->host_and_port
, client_cert
);
224 // Reset the other member variables.
225 // Note: this is necessary only with SSL renegotiation.
226 ResetStateForRestart();
227 next_state_
= STATE_CREATE_STREAM
;
229 if (rv
== ERR_IO_PENDING
)
230 callback_
= callback
;
234 int HttpNetworkTransaction::RestartWithAuth(
235 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
236 HttpAuth::Target target
= pending_auth_target_
;
237 if (target
== HttpAuth::AUTH_NONE
) {
239 return ERR_UNEXPECTED
;
241 pending_auth_target_
= HttpAuth::AUTH_NONE
;
243 auth_controllers_
[target
]->ResetAuth(credentials
);
245 DCHECK(callback_
.is_null());
248 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
249 // In this case, we've gathered credentials for use with proxy
250 // authentication of a tunnel.
251 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
252 DCHECK(stream_request_
!= NULL
);
253 auth_controllers_
[target
] = NULL
;
254 ResetStateForRestart();
255 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
257 // In this case, we've gathered credentials for the server or the proxy
258 // but it is not during the tunneling phase.
259 DCHECK(stream_request_
== NULL
);
260 PrepareForAuthRestart(target
);
264 if (rv
== ERR_IO_PENDING
)
265 callback_
= callback
;
269 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
270 DCHECK(HaveAuth(target
));
271 DCHECK(!stream_request_
.get());
273 bool keep_alive
= false;
274 // Even if the server says the connection is keep-alive, we have to be
275 // able to find the end of each response in order to reuse the connection.
276 if (GetResponseHeaders()->IsKeepAlive() &&
277 stream_
->CanFindEndOfResponse()) {
278 // If the response body hasn't been completely read, we need to drain
280 if (!stream_
->IsResponseBodyComplete()) {
281 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
282 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
283 read_buf_len_
= kDrainBodyBufferSize
;
289 // We don't need to drain the response body, so we act as if we had drained
290 // the response body.
291 DidDrainBodyForAuthRestart(keep_alive
);
294 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
295 DCHECK(!stream_request_
.get());
298 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
299 HttpStream
* new_stream
= NULL
;
300 if (keep_alive
&& stream_
->IsConnectionReusable()) {
301 // We should call connection_->set_idle_time(), but this doesn't occur
302 // often enough to be worth the trouble.
303 stream_
->SetConnectionReused();
304 new_stream
= stream_
->RenewStreamForAuth();
308 // Close the stream and mark it as not_reusable. Even in the
309 // keep_alive case, we've determined that the stream_ is not
310 // reusable if new_stream is NULL.
311 stream_
->Close(true);
312 next_state_
= STATE_CREATE_STREAM
;
314 // Renewed streams shouldn't carry over received bytes.
315 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
316 next_state_
= STATE_INIT_STREAM
;
318 stream_
.reset(new_stream
);
321 // Reset the other member variables.
322 ResetStateForAuthRestart();
325 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
326 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
327 HaveAuth(pending_auth_target_
);
330 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
331 const CompletionCallback
& callback
) {
333 DCHECK_LT(0, buf_len
);
335 State next_state
= STATE_NONE
;
337 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
338 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
339 // We're trying to read the body of the response but we're still trying
340 // to establish an SSL tunnel through an HTTP proxy. We can't read these
341 // bytes when establishing a tunnel because they might be controlled by
342 // an active network attacker. We don't worry about this for HTTP
343 // because an active network attacker can already control HTTP sessions.
344 // We reach this case when the user cancels a 407 proxy auth prompt. We
345 // also don't worry about this for an HTTPS Proxy, because the
346 // communication with the proxy is secure.
347 // See http://crbug.com/8473.
348 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
349 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
350 LOG(WARNING
) << "Blocked proxy response with status "
351 << headers
->response_code() << " to CONNECT request for "
352 << GetHostAndPort(request_
->url
) << ".";
353 return ERR_TUNNEL_CONNECTION_FAILED
;
356 // Are we using SPDY or HTTP?
357 next_state
= STATE_READ_BODY
;
360 read_buf_len_
= buf_len
;
362 next_state_
= next_state
;
364 if (rv
== ERR_IO_PENDING
)
365 callback_
= callback
;
369 void HttpNetworkTransaction::StopCaching() {}
371 bool HttpNetworkTransaction::GetFullRequestHeaders(
372 HttpRequestHeaders
* headers
) const {
373 // TODO(ttuttle): Make sure we've populated request_headers_.
374 *headers
= request_headers_
;
378 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
379 int64 total_received_bytes
= total_received_bytes_
;
381 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
382 return total_received_bytes
;
385 void HttpNetworkTransaction::DoneReading() {}
387 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
388 return ((headers_valid_
&& response_
.headers
.get()) ||
389 response_
.ssl_info
.cert
.get() || response_
.cert_request_info
.get())
394 LoadState
HttpNetworkTransaction::GetLoadState() const {
395 // TODO(wtc): Define a new LoadState value for the
396 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
397 switch (next_state_
) {
398 case STATE_CREATE_STREAM
:
399 return LOAD_STATE_WAITING_FOR_DELEGATE
;
400 case STATE_CREATE_STREAM_COMPLETE
:
401 return stream_request_
->GetLoadState();
402 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
403 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
404 case STATE_SEND_REQUEST_COMPLETE
:
405 return LOAD_STATE_SENDING_REQUEST
;
406 case STATE_READ_HEADERS_COMPLETE
:
407 return LOAD_STATE_WAITING_FOR_RESPONSE
;
408 case STATE_READ_BODY_COMPLETE
:
409 return LOAD_STATE_READING_RESPONSE
;
411 return LOAD_STATE_IDLE
;
415 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
417 return UploadProgress();
419 return stream_
->GetUploadProgress();
422 void HttpNetworkTransaction::SetQuicServerInfo(
423 QuicServerInfo
* quic_server_info
) {}
425 bool HttpNetworkTransaction::GetLoadTimingInfo(
426 LoadTimingInfo
* load_timing_info
) const {
427 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
430 load_timing_info
->proxy_resolve_start
=
431 proxy_info_
.proxy_resolve_start_time();
432 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
433 load_timing_info
->send_start
= send_start_time_
;
434 load_timing_info
->send_end
= send_end_time_
;
438 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
439 priority_
= priority
;
441 stream_request_
->SetPriority(priority
);
443 stream_
->SetPriority(priority
);
446 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
447 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
448 websocket_handshake_stream_base_create_helper_
= create_helper
;
451 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
452 const BeforeNetworkStartCallback
& callback
) {
453 before_network_start_callback_
= callback
;
456 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
457 const BeforeProxyHeadersSentCallback
& callback
) {
458 before_proxy_headers_sent_callback_
= callback
;
461 int HttpNetworkTransaction::ResumeNetworkStart() {
462 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
466 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
467 const ProxyInfo
& used_proxy_info
,
468 HttpStream
* stream
) {
469 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
470 DCHECK(stream_request_
.get());
473 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
474 stream_
.reset(stream
);
475 server_ssl_config_
= used_ssl_config
;
476 proxy_info_
= used_proxy_info
;
477 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
478 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
479 stream_request_
->protocol_negotiated());
480 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
481 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
482 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
483 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
487 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
488 const SSLConfig
& used_ssl_config
,
489 const ProxyInfo
& used_proxy_info
,
490 WebSocketHandshakeStreamBase
* stream
) {
491 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
494 void HttpNetworkTransaction::OnStreamFailed(int result
,
495 const SSLConfig
& used_ssl_config
) {
496 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
497 DCHECK_NE(OK
, result
);
498 DCHECK(stream_request_
.get());
499 DCHECK(!stream_
.get());
500 server_ssl_config_
= used_ssl_config
;
502 OnIOComplete(result
);
505 void HttpNetworkTransaction::OnCertificateError(
507 const SSLConfig
& used_ssl_config
,
508 const SSLInfo
& ssl_info
) {
509 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
510 DCHECK_NE(OK
, result
);
511 DCHECK(stream_request_
.get());
512 DCHECK(!stream_
.get());
514 response_
.ssl_info
= ssl_info
;
515 server_ssl_config_
= used_ssl_config
;
517 // TODO(mbelshe): For now, we're going to pass the error through, and that
518 // will close the stream_request in all cases. This means that we're always
519 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
520 // good and the user chooses to ignore the error. This is not ideal, but not
521 // the end of the world either.
523 OnIOComplete(result
);
526 void HttpNetworkTransaction::OnNeedsProxyAuth(
527 const HttpResponseInfo
& proxy_response
,
528 const SSLConfig
& used_ssl_config
,
529 const ProxyInfo
& used_proxy_info
,
530 HttpAuthController
* auth_controller
) {
531 DCHECK(stream_request_
.get());
532 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
534 establishing_tunnel_
= true;
535 response_
.headers
= proxy_response
.headers
;
536 response_
.auth_challenge
= proxy_response
.auth_challenge
;
537 headers_valid_
= true;
538 server_ssl_config_
= used_ssl_config
;
539 proxy_info_
= used_proxy_info
;
541 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
542 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
547 void HttpNetworkTransaction::OnNeedsClientAuth(
548 const SSLConfig
& used_ssl_config
,
549 SSLCertRequestInfo
* cert_info
) {
550 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
552 server_ssl_config_
= used_ssl_config
;
553 response_
.cert_request_info
= cert_info
;
554 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
557 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
558 const HttpResponseInfo
& response_info
,
559 const SSLConfig
& used_ssl_config
,
560 const ProxyInfo
& used_proxy_info
,
561 HttpStream
* stream
) {
562 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
564 headers_valid_
= true;
565 response_
= response_info
;
566 server_ssl_config_
= used_ssl_config
;
567 proxy_info_
= used_proxy_info
;
569 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
570 stream_
.reset(stream
);
571 stream_request_
.reset(); // we're done with the stream request
572 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
575 bool HttpNetworkTransaction::IsSecureRequest() const {
576 return request_
->url
.SchemeIsSecure();
579 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
580 return (proxy_info_
.is_http() || proxy_info_
.is_https() ||
581 proxy_info_
.is_quic()) &&
582 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
585 void HttpNetworkTransaction::DoCallback(int rv
) {
586 DCHECK_NE(rv
, ERR_IO_PENDING
);
587 DCHECK(!callback_
.is_null());
589 // Since Run may result in Read being called, clear user_callback_ up front.
590 CompletionCallback c
= callback_
;
595 void HttpNetworkTransaction::OnIOComplete(int result
) {
596 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
597 tracked_objects::ScopedTracker
tracking_profile1(
598 FROM_HERE_WITH_EXPLICIT_FUNCTION(
599 "424359 HttpNetworkTransaction::OnIOComplete 1"));
601 int rv
= DoLoop(result
);
603 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
604 tracked_objects::ScopedTracker
tracking_profile2(
605 FROM_HERE_WITH_EXPLICIT_FUNCTION(
606 "424359 HttpNetworkTransaction::OnIOComplete 2"));
608 if (rv
!= ERR_IO_PENDING
)
612 int HttpNetworkTransaction::DoLoop(int result
) {
613 DCHECK(next_state_
!= STATE_NONE
);
617 State state
= next_state_
;
618 next_state_
= STATE_NONE
;
620 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
622 rv
= DoNotifyBeforeCreateStream();
624 case STATE_CREATE_STREAM
:
626 rv
= DoCreateStream();
628 case STATE_CREATE_STREAM_COMPLETE
:
629 rv
= DoCreateStreamComplete(rv
);
631 case STATE_INIT_STREAM
:
635 case STATE_INIT_STREAM_COMPLETE
:
636 rv
= DoInitStreamComplete(rv
);
638 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
640 rv
= DoGenerateProxyAuthToken();
642 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
643 rv
= DoGenerateProxyAuthTokenComplete(rv
);
645 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
647 rv
= DoGenerateServerAuthToken();
649 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
650 rv
= DoGenerateServerAuthTokenComplete(rv
);
652 case STATE_INIT_REQUEST_BODY
:
654 rv
= DoInitRequestBody();
656 case STATE_INIT_REQUEST_BODY_COMPLETE
:
657 rv
= DoInitRequestBodyComplete(rv
);
659 case STATE_BUILD_REQUEST
:
661 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
662 rv
= DoBuildRequest();
664 case STATE_BUILD_REQUEST_COMPLETE
:
665 rv
= DoBuildRequestComplete(rv
);
667 case STATE_SEND_REQUEST
:
669 rv
= DoSendRequest();
671 case STATE_SEND_REQUEST_COMPLETE
:
672 rv
= DoSendRequestComplete(rv
);
673 net_log_
.EndEventWithNetErrorCode(
674 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
676 case STATE_READ_HEADERS
:
678 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
679 rv
= DoReadHeaders();
681 case STATE_READ_HEADERS_COMPLETE
:
682 rv
= DoReadHeadersComplete(rv
);
683 net_log_
.EndEventWithNetErrorCode(
684 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
686 case STATE_READ_BODY
:
688 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
691 case STATE_READ_BODY_COMPLETE
:
692 rv
= DoReadBodyComplete(rv
);
693 net_log_
.EndEventWithNetErrorCode(
694 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
696 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
699 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
700 rv
= DoDrainBodyForAuthRestart();
702 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
703 rv
= DoDrainBodyForAuthRestartComplete(rv
);
704 net_log_
.EndEventWithNetErrorCode(
705 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
708 NOTREACHED() << "bad state";
712 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
717 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
718 next_state_
= STATE_CREATE_STREAM
;
720 if (!before_network_start_callback_
.is_null())
721 before_network_start_callback_
.Run(&defer
);
724 return ERR_IO_PENDING
;
727 int HttpNetworkTransaction::DoCreateStream() {
728 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
729 if (ForWebSocketHandshake()) {
730 stream_request_
.reset(
731 session_
->http_stream_factory_for_websocket()
732 ->RequestWebSocketHandshakeStream(
738 websocket_handshake_stream_base_create_helper_
,
741 stream_request_
.reset(
742 session_
->http_stream_factory()->RequestStream(
750 DCHECK(stream_request_
.get());
751 return ERR_IO_PENDING
;
754 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
756 next_state_
= STATE_INIT_STREAM
;
757 DCHECK(stream_
.get());
758 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
759 result
= HandleCertificateRequest(result
);
760 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
761 // Return OK and let the caller read the proxy's error page
762 next_state_
= STATE_NONE
;
764 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
765 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
766 return HandleHttp11Required(result
);
769 // Handle possible handshake errors that may have occurred if the stream
770 // used SSL for one or more of the layers.
771 result
= HandleSSLHandshakeError(result
);
773 // At this point we are done with the stream_request_.
774 stream_request_
.reset();
778 int HttpNetworkTransaction::DoInitStream() {
779 DCHECK(stream_
.get());
780 next_state_
= STATE_INIT_STREAM_COMPLETE
;
781 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
784 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
786 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
789 result
= HandleIOError(result
);
791 // The stream initialization failed, so this stream will never be useful.
793 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
800 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
801 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
802 if (!ShouldApplyProxyAuth())
804 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
805 if (!auth_controllers_
[target
].get())
806 auth_controllers_
[target
] =
807 new HttpAuthController(target
,
809 session_
->http_auth_cache(),
810 session_
->http_auth_handler_factory());
811 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
816 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
817 DCHECK_NE(ERR_IO_PENDING
, rv
);
819 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
823 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
824 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
825 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
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 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
833 auth_controllers_
[target
]->DisableEmbeddedIdentity();
835 if (!ShouldApplyServerAuth())
837 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
842 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
843 DCHECK_NE(ERR_IO_PENDING
, rv
);
845 next_state_
= STATE_INIT_REQUEST_BODY
;
849 void HttpNetworkTransaction::BuildRequestHeaders(
850 bool using_http_proxy_without_tunnel
) {
851 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
852 GetHostAndOptionalPort(request_
->url
));
854 // For compat with HTTP/1.0 servers and proxies:
855 if (using_http_proxy_without_tunnel
) {
856 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
859 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
862 // Add a content length header?
863 if (request_
->upload_data_stream
) {
864 if (request_
->upload_data_stream
->is_chunked()) {
865 request_headers_
.SetHeader(
866 HttpRequestHeaders::kTransferEncoding
, "chunked");
868 request_headers_
.SetHeader(
869 HttpRequestHeaders::kContentLength
,
870 base::Uint64ToString(request_
->upload_data_stream
->size()));
872 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
873 request_
->method
== "HEAD") {
874 // An empty POST/PUT request still needs a content length. As for HEAD,
875 // IE and Safari also add a content length header. Presumably it is to
876 // support sending a HEAD request to an URL that only expects to be sent a
877 // POST or some other method that normally would have a message body.
878 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
881 // Honor load flags that impact proxy caches.
882 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
883 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
884 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
885 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
886 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
889 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
890 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
892 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
893 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
896 request_headers_
.MergeFrom(request_
->extra_headers
);
898 if (using_http_proxy_without_tunnel
&&
899 !before_proxy_headers_sent_callback_
.is_null())
900 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
902 response_
.did_use_http_auth
=
903 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
904 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
907 int HttpNetworkTransaction::DoInitRequestBody() {
908 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
910 if (request_
->upload_data_stream
)
911 rv
= request_
->upload_data_stream
->Init(io_callback_
);
915 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
917 next_state_
= STATE_BUILD_REQUEST
;
921 int HttpNetworkTransaction::DoBuildRequest() {
922 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
923 headers_valid_
= false;
925 // This is constructed lazily (instead of within our Start method), so that
926 // we have proxy info available.
927 if (request_headers_
.IsEmpty()) {
928 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
929 BuildRequestHeaders(using_http_proxy_without_tunnel
);
935 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
937 next_state_
= STATE_SEND_REQUEST
;
941 int HttpNetworkTransaction::DoSendRequest() {
942 send_start_time_
= base::TimeTicks::Now();
943 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
945 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
948 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
949 send_end_time_
= base::TimeTicks::Now();
951 return HandleIOError(result
);
952 response_
.network_accessed
= true;
953 next_state_
= STATE_READ_HEADERS
;
957 int HttpNetworkTransaction::DoReadHeaders() {
958 next_state_
= STATE_READ_HEADERS_COMPLETE
;
959 return stream_
->ReadResponseHeaders(io_callback_
);
962 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
963 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
964 // due to SSL renegotiation.
965 if (IsCertificateError(result
)) {
966 // We don't handle a certificate error during SSL renegotiation, so we
967 // have to return an error that's not in the certificate error range
969 LOG(ERROR
) << "Got a server certificate with error " << result
970 << " during SSL renegotiation";
971 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
972 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
973 // TODO(wtc): Need a test case for this code path!
974 DCHECK(stream_
.get());
975 DCHECK(IsSecureRequest());
976 response_
.cert_request_info
= new SSLCertRequestInfo
;
977 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
978 result
= HandleCertificateRequest(result
);
983 if (result
== ERR_HTTP_1_1_REQUIRED
||
984 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
985 return HandleHttp11Required(result
);
988 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
989 // response headers were received, we do the best we can to make sense of it
990 // and send it back up the stack.
992 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
993 // bizarre for SPDY. Assuming this logic is useful at all.
994 // TODO(davidben): Bubble the error code up so we do not cache?
995 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
999 return HandleIOError(result
);
1001 DCHECK(response_
.headers
.get());
1003 // On a 408 response from the server ("Request Timeout") on a stale socket,
1004 // retry the request.
1005 // Headers can be NULL because of http://crbug.com/384554.
1006 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1007 stream_
->IsConnectionReused()) {
1008 net_log_
.AddEventWithNetErrorCode(
1009 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1010 response_
.headers
->response_code());
1011 // This will close the socket - it would be weird to try and reuse it, even
1012 // if the server doesn't actually close it.
1013 ResetConnectionAndRequestForResend();
1017 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1018 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1019 const int response_code
= response_
.headers
->response_code();
1020 UMA_HISTOGRAM_ENUMERATION(
1021 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1025 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1026 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1028 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1029 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1030 // indicates a buggy server. See:
1031 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1032 if (request_
->method
== "PUT")
1033 return ERR_METHOD_NOT_SUPPORTED
;
1036 // Check for an intermediate 100 Continue response. An origin server is
1037 // allowed to send this response even if we didn't ask for it, so we just
1038 // need to skip over it.
1039 // We treat any other 1xx in this same way (although in practice getting
1040 // a 1xx that isn't a 100 is rare).
1041 // Unless this is a WebSocket request, in which case we pass it on up.
1042 if (response_
.headers
->response_code() / 100 == 1 &&
1043 !ForWebSocketHandshake()) {
1044 response_
.headers
= new HttpResponseHeaders(std::string());
1045 next_state_
= STATE_READ_HEADERS
;
1049 ProcessAlternateProtocol(session_
, *response_
.headers
.get(),
1050 HostPortPair::FromURL(request_
->url
));
1052 int rv
= HandleAuthChallenge();
1056 if (IsSecureRequest())
1057 stream_
->GetSSLInfo(&response_
.ssl_info
);
1059 headers_valid_
= true;
1063 int HttpNetworkTransaction::DoReadBody() {
1064 DCHECK(read_buf_
.get());
1065 DCHECK_GT(read_buf_len_
, 0);
1066 DCHECK(stream_
!= NULL
);
1068 next_state_
= STATE_READ_BODY_COMPLETE
;
1069 return stream_
->ReadResponseBody(
1070 read_buf_
.get(), read_buf_len_
, io_callback_
);
1073 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1074 // We are done with the Read call.
1077 DCHECK_NE(ERR_IO_PENDING
, result
);
1081 bool keep_alive
= false;
1082 if (stream_
->IsResponseBodyComplete()) {
1083 // Note: Just because IsResponseBodyComplete is true, we're not
1084 // necessarily "done". We're only "done" when it is the last
1085 // read on this HttpNetworkTransaction, which will be signified
1086 // by a zero-length read.
1087 // TODO(mbelshe): The keepalive property is really a property of
1088 // the stream. No need to compute it here just to pass back
1089 // to the stream's Close function.
1090 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1092 if (stream_
->CanFindEndOfResponse()) {
1093 HttpResponseHeaders
* headers
= GetResponseHeaders();
1095 keep_alive
= headers
->IsKeepAlive();
1099 // Clean up connection if we are done.
1101 stream_
->Close(!keep_alive
);
1102 // Note: we don't reset the stream here. We've closed it, but we still
1103 // need it around so that callers can call methods such as
1104 // GetUploadProgress() and have them be meaningful.
1105 // TODO(mbelshe): This means we closed the stream here, and we close it
1106 // again in ~HttpNetworkTransaction. Clean that up.
1108 // The next Read call will return 0 (EOF).
1111 // Clear these to avoid leaving around old state.
1118 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1119 // This method differs from DoReadBody only in the next_state_. So we just
1120 // call DoReadBody and override the next_state_. Perhaps there is a more
1121 // elegant way for these two methods to share code.
1122 int rv
= DoReadBody();
1123 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1124 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1128 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1129 // the same. Figure out a good way for these two methods to share code.
1130 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1131 // keep_alive defaults to true because the very reason we're draining the
1132 // response body is to reuse the connection for auth restart.
1133 bool done
= false, keep_alive
= true;
1135 // Error or closed connection while reading the socket.
1138 } else if (stream_
->IsResponseBodyComplete()) {
1143 DidDrainBodyForAuthRestart(keep_alive
);
1146 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1152 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1153 // There are two paths through which the server can request a certificate
1154 // from us. The first is during the initial handshake, the second is
1155 // during SSL renegotiation.
1157 // In both cases, we want to close the connection before proceeding.
1158 // We do this for two reasons:
1159 // First, we don't want to keep the connection to the server hung for a
1160 // long time while the user selects a certificate.
1161 // Second, even if we did keep the connection open, NSS has a bug where
1162 // restarting the handshake for ClientAuth is currently broken.
1163 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1165 if (stream_
.get()) {
1166 // Since we already have a stream, we're being called as part of SSL
1168 DCHECK(!stream_request_
.get());
1169 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1170 stream_
->Close(true);
1174 // The server is asking for a client certificate during the initial
1176 stream_request_
.reset();
1178 // If the user selected one of the certificates in client_certs or declined
1179 // to provide one for this server before, use the past decision
1181 scoped_refptr
<X509Certificate
> client_cert
;
1182 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1183 response_
.cert_request_info
->host_and_port
, &client_cert
);
1184 if (!found_cached_cert
)
1187 // Check that the certificate selected is still a certificate the server
1188 // is likely to accept, based on the criteria supplied in the
1189 // CertificateRequest message.
1190 if (client_cert
.get()) {
1191 const std::vector
<std::string
>& cert_authorities
=
1192 response_
.cert_request_info
->cert_authorities
;
1194 bool cert_still_valid
= cert_authorities
.empty() ||
1195 client_cert
->IsIssuedByEncoded(cert_authorities
);
1196 if (!cert_still_valid
)
1200 // TODO(davidben): Add a unit test which covers this path; we need to be
1201 // able to send a legitimate certificate and also bypass/clear the
1202 // SSL session cache.
1203 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1204 &proxy_ssl_config_
: &server_ssl_config_
;
1205 ssl_config
->send_client_cert
= true;
1206 ssl_config
->client_cert
= client_cert
;
1207 next_state_
= STATE_CREATE_STREAM
;
1208 // Reset the other member variables.
1209 // Note: this is necessary only with SSL renegotiation.
1210 ResetStateForRestart();
1214 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1215 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1216 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1218 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1219 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1221 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1223 ResetConnectionAndRequestForResend();
1227 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1228 if (server_ssl_config_
.send_client_cert
&&
1229 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1230 session_
->ssl_client_auth_cache()->Remove(
1231 HostPortPair::FromURL(request_
->url
));
1235 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1236 // being used, as all of the errors are handled as if they were generated
1237 // by the endpoint host, request_->url, rather than considering if they were
1238 // generated by the SSL proxy. http://crbug.com/69329
1239 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1241 HandleClientAuthError(error
);
1243 bool should_fallback
= false;
1244 uint16 version_max
= server_ssl_config_
.version_max
;
1247 case ERR_CONNECTION_CLOSED
:
1248 case ERR_SSL_PROTOCOL_ERROR
:
1249 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1250 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1251 version_max
> server_ssl_config_
.version_min
) {
1252 // This could be a TLS-intolerant server or a server that chose a
1253 // cipher suite defined only for higher protocol versions (such as
1254 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1255 // back to the next lower version and retry.
1256 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1257 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1258 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1259 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1260 // version_max should match the maximum protocol version supported
1261 // by the SSLClientSocket class.
1264 // Fallback to the lower SSL version.
1265 // While SSL 3.0 fallback should be eliminated because of security
1266 // reasons, there is a high risk of breaking the servers if this is
1268 should_fallback
= true;
1271 case ERR_CONNECTION_RESET
:
1272 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1273 version_max
> server_ssl_config_
.version_min
) {
1274 // Some network devices that inspect application-layer packets seem to
1275 // inject TCP reset packets to break the connections when they see TLS
1276 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1278 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1279 // 1.2. We don't lose much in this fallback because the explicit IV for
1280 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1281 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1284 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1285 // to trigger a version fallback in general, especially the TLS 1.0 ->
1286 // SSL 3.0 fallback, which would drop TLS extensions.
1288 should_fallback
= true;
1291 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1292 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1293 version_max
> server_ssl_config_
.version_min
) {
1294 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1295 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1296 // crbug.com/260358. In order to make the fallback as minimal as
1297 // possible, this fallback is only triggered for >= TLS 1.1.
1299 should_fallback
= true;
1302 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1303 // The server told us that we should not have fallen back. A buggy server
1304 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1305 // connection. |fallback_error_code_| is initialised to
1306 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1307 error
= fallback_error_code_
;
1311 if (should_fallback
) {
1313 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1314 base::Bind(&NetLogSSLVersionFallbackCallback
,
1315 &request_
->url
, error
, server_ssl_config_
.version_max
,
1317 fallback_error_code_
= error
;
1318 server_ssl_config_
.version_max
= version_max
;
1319 server_ssl_config_
.version_fallback
= true;
1320 ResetConnectionAndRequestForResend();
1327 // This method determines whether it is safe to resend the request after an
1328 // IO error. It can only be called in response to request header or body
1329 // write errors or response header read errors. It should not be used in
1330 // other cases, such as a Connect error.
1331 int HttpNetworkTransaction::HandleIOError(int error
) {
1332 // Because the peer may request renegotiation with client authentication at
1333 // any time, check and handle client authentication errors.
1334 HandleClientAuthError(error
);
1337 // If we try to reuse a connection that the server is in the process of
1338 // closing, we may end up successfully writing out our request (or a
1339 // portion of our request) only to find a connection error when we try to
1340 // read from (or finish writing to) the socket.
1341 case ERR_CONNECTION_RESET
:
1342 case ERR_CONNECTION_CLOSED
:
1343 case ERR_CONNECTION_ABORTED
:
1344 // There can be a race between the socket pool checking checking whether a
1345 // socket is still connected, receiving the FIN, and sending/reading data
1346 // on a reused socket. If we receive the FIN between the connectedness
1347 // check and writing/reading from the socket, we may first learn the socket
1348 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1349 // likely happen when trying to retrieve its IP address.
1350 // See http://crbug.com/105824 for more details.
1351 case ERR_SOCKET_NOT_CONNECTED
:
1352 // If a socket is closed on its initial request, HttpStreamParser returns
1353 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1354 // preconnected but failed to be used before the server timed it out.
1355 case ERR_EMPTY_RESPONSE
:
1356 if (ShouldResendRequest()) {
1357 net_log_
.AddEventWithNetErrorCode(
1358 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1359 ResetConnectionAndRequestForResend();
1363 case ERR_SPDY_PING_FAILED
:
1364 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1365 case ERR_QUIC_HANDSHAKE_FAILED
:
1366 net_log_
.AddEventWithNetErrorCode(
1367 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1368 ResetConnectionAndRequestForResend();
1375 void HttpNetworkTransaction::ResetStateForRestart() {
1376 ResetStateForAuthRestart();
1378 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1382 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1383 send_start_time_
= base::TimeTicks();
1384 send_end_time_
= base::TimeTicks();
1386 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1389 headers_valid_
= false;
1390 request_headers_
.Clear();
1391 response_
= HttpResponseInfo();
1392 establishing_tunnel_
= false;
1395 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1396 return response_
.headers
.get();
1399 bool HttpNetworkTransaction::ShouldResendRequest() const {
1400 bool connection_is_proven
= stream_
->IsConnectionReused();
1401 bool has_received_headers
= GetResponseHeaders() != NULL
;
1403 // NOTE: we resend a request only if we reused a keep-alive connection.
1404 // This automatically prevents an infinite resend loop because we'll run
1405 // out of the cached keep-alive connections eventually.
1406 if (connection_is_proven
&& !has_received_headers
)
1411 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1412 if (stream_
.get()) {
1413 stream_
->Close(true);
1417 // We need to clear request_headers_ because it contains the real request
1418 // headers, but we may need to resend the CONNECT request first to recreate
1420 request_headers_
.Clear();
1421 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1424 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1425 return UsingHttpProxyWithoutTunnel();
1428 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1429 return !(request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
);
1432 int HttpNetworkTransaction::HandleAuthChallenge() {
1433 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
1434 DCHECK(headers
.get());
1436 int status
= headers
->response_code();
1437 if (status
!= HTTP_UNAUTHORIZED
&&
1438 status
!= HTTP_PROXY_AUTHENTICATION_REQUIRED
)
1440 HttpAuth::Target target
= status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
?
1441 HttpAuth::AUTH_PROXY
: HttpAuth::AUTH_SERVER
;
1442 if (target
== HttpAuth::AUTH_PROXY
&& proxy_info_
.is_direct())
1443 return ERR_UNEXPECTED_PROXY_AUTH
;
1445 // This case can trigger when an HTTPS server responds with a "Proxy
1446 // authentication required" status code through a non-authenticating
1448 if (!auth_controllers_
[target
].get())
1449 return ERR_UNEXPECTED_PROXY_AUTH
;
1451 int rv
= auth_controllers_
[target
]->HandleAuthChallenge(
1452 headers
, (request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
) != 0, false,
1454 if (auth_controllers_
[target
]->HaveAuthHandler())
1455 pending_auth_target_
= target
;
1457 scoped_refptr
<AuthChallengeInfo
> auth_info
=
1458 auth_controllers_
[target
]->auth_info();
1459 if (auth_info
.get())
1460 response_
.auth_challenge
= auth_info
;
1465 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target
) const {
1466 return auth_controllers_
[target
].get() &&
1467 auth_controllers_
[target
]->HaveAuth();
1470 GURL
HttpNetworkTransaction::AuthURL(HttpAuth::Target target
) const {
1472 case HttpAuth::AUTH_PROXY
: {
1473 if (!proxy_info_
.proxy_server().is_valid() ||
1474 proxy_info_
.proxy_server().is_direct()) {
1475 return GURL(); // There is no proxy server.
1477 const char* scheme
= proxy_info_
.is_https() ? "https://" : "http://";
1478 return GURL(scheme
+
1479 proxy_info_
.proxy_server().host_port_pair().ToString());
1481 case HttpAuth::AUTH_SERVER
:
1482 if (ForWebSocketHandshake()) {
1483 const GURL
& url
= request_
->url
;
1484 url::Replacements
<char> ws_to_http
;
1485 if (url
.SchemeIs("ws")) {
1486 ws_to_http
.SetScheme("http", url::Component(0, 4));
1488 DCHECK(url
.SchemeIs("wss"));
1489 ws_to_http
.SetScheme("https", url::Component(0, 5));
1491 return url
.ReplaceComponents(ws_to_http
);
1493 return request_
->url
;
1499 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1500 return websocket_handshake_stream_base_create_helper_
&&
1501 request_
->url
.SchemeIsWSOrWSS();
1504 #define STATE_CASE(s) \
1506 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1509 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1510 std::string description
;
1512 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM
);
1513 STATE_CASE(STATE_CREATE_STREAM
);
1514 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1515 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1516 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1517 STATE_CASE(STATE_BUILD_REQUEST
);
1518 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1519 STATE_CASE(STATE_SEND_REQUEST
);
1520 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1521 STATE_CASE(STATE_READ_HEADERS
);
1522 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1523 STATE_CASE(STATE_READ_BODY
);
1524 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1525 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1526 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1527 STATE_CASE(STATE_NONE
);
1529 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,