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
);
109 base::Value
* NetLogSSLCipherFallbackCallback(const GURL
* url
,
111 NetLog::LogLevel
/* log_level */) {
112 base::DictionaryValue
* dict
= new base::DictionaryValue();
113 dict
->SetString("host_and_port", GetHostAndPort(*url
));
114 dict
->SetInteger("net_error", net_error
);
120 //-----------------------------------------------------------------------------
122 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
123 HttpNetworkSession
* session
)
124 : pending_auth_target_(HttpAuth::AUTH_NONE
),
125 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
126 base::Unretained(this))),
130 headers_valid_(false),
131 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
134 total_received_bytes_(0),
135 next_state_(STATE_NONE
),
136 establishing_tunnel_(false),
137 websocket_handshake_stream_base_create_helper_(NULL
) {
138 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
139 session
->GetNextProtos(&server_ssl_config_
.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 HttpStream
* 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
) {
178 request_
= request_info
;
180 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
181 server_ssl_config_
.rev_checking_enabled
= false;
182 proxy_ssl_config_
.rev_checking_enabled
= false;
185 if (request_
->load_flags
& LOAD_PREFETCH
)
186 response_
.unused_since_prefetch
= true;
188 // Channel ID is disabled if privacy mode is enabled for this request.
189 if (request_
->privacy_mode
== PRIVACY_MODE_ENABLED
)
190 server_ssl_config_
.channel_id_enabled
= false;
192 if (server_ssl_config_
.fastradio_padding_enabled
) {
193 server_ssl_config_
.fastradio_padding_eligible
=
194 session_
->ssl_config_service()->SupportsFastradioPadding(
198 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
200 if (rv
== ERR_IO_PENDING
)
201 callback_
= callback
;
205 int HttpNetworkTransaction::RestartIgnoringLastError(
206 const CompletionCallback
& callback
) {
207 DCHECK(!stream_
.get());
208 DCHECK(!stream_request_
.get());
209 DCHECK_EQ(STATE_NONE
, next_state_
);
211 next_state_
= STATE_CREATE_STREAM
;
214 if (rv
== ERR_IO_PENDING
)
215 callback_
= callback
;
219 int HttpNetworkTransaction::RestartWithCertificate(
220 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
221 // In HandleCertificateRequest(), we always tear down existing stream
222 // requests to force a new connection. So we shouldn't have one here.
223 DCHECK(!stream_request_
.get());
224 DCHECK(!stream_
.get());
225 DCHECK_EQ(STATE_NONE
, next_state_
);
227 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
228 &proxy_ssl_config_
: &server_ssl_config_
;
229 ssl_config
->send_client_cert
= true;
230 ssl_config
->client_cert
= client_cert
;
231 session_
->ssl_client_auth_cache()->Add(
232 response_
.cert_request_info
->host_and_port
, client_cert
);
233 // Reset the other member variables.
234 // Note: this is necessary only with SSL renegotiation.
235 ResetStateForRestart();
236 next_state_
= STATE_CREATE_STREAM
;
238 if (rv
== ERR_IO_PENDING
)
239 callback_
= callback
;
243 int HttpNetworkTransaction::RestartWithAuth(
244 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
245 HttpAuth::Target target
= pending_auth_target_
;
246 if (target
== HttpAuth::AUTH_NONE
) {
248 return ERR_UNEXPECTED
;
250 pending_auth_target_
= HttpAuth::AUTH_NONE
;
252 auth_controllers_
[target
]->ResetAuth(credentials
);
254 DCHECK(callback_
.is_null());
257 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
258 // In this case, we've gathered credentials for use with proxy
259 // authentication of a tunnel.
260 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
261 DCHECK(stream_request_
!= NULL
);
262 auth_controllers_
[target
] = NULL
;
263 ResetStateForRestart();
264 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
266 // In this case, we've gathered credentials for the server or the proxy
267 // but it is not during the tunneling phase.
268 DCHECK(stream_request_
== NULL
);
269 PrepareForAuthRestart(target
);
273 if (rv
== ERR_IO_PENDING
)
274 callback_
= callback
;
278 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
279 DCHECK(HaveAuth(target
));
280 DCHECK(!stream_request_
.get());
282 bool keep_alive
= false;
283 // Even if the server says the connection is keep-alive, we have to be
284 // able to find the end of each response in order to reuse the connection.
285 if (GetResponseHeaders()->IsKeepAlive() &&
286 stream_
->CanFindEndOfResponse()) {
287 // If the response body hasn't been completely read, we need to drain
289 if (!stream_
->IsResponseBodyComplete()) {
290 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
291 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
292 read_buf_len_
= kDrainBodyBufferSize
;
298 // We don't need to drain the response body, so we act as if we had drained
299 // the response body.
300 DidDrainBodyForAuthRestart(keep_alive
);
303 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
304 DCHECK(!stream_request_
.get());
307 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
308 HttpStream
* new_stream
= NULL
;
309 if (keep_alive
&& stream_
->IsConnectionReusable()) {
310 // We should call connection_->set_idle_time(), but this doesn't occur
311 // often enough to be worth the trouble.
312 stream_
->SetConnectionReused();
313 new_stream
= stream_
->RenewStreamForAuth();
317 // Close the stream and mark it as not_reusable. Even in the
318 // keep_alive case, we've determined that the stream_ is not
319 // reusable if new_stream is NULL.
320 stream_
->Close(true);
321 next_state_
= STATE_CREATE_STREAM
;
323 // Renewed streams shouldn't carry over received bytes.
324 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
325 next_state_
= STATE_INIT_STREAM
;
327 stream_
.reset(new_stream
);
330 // Reset the other member variables.
331 ResetStateForAuthRestart();
334 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
335 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
336 HaveAuth(pending_auth_target_
);
339 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
340 const CompletionCallback
& callback
) {
342 DCHECK_LT(0, buf_len
);
344 State next_state
= STATE_NONE
;
346 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
347 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
348 // We're trying to read the body of the response but we're still trying
349 // to establish an SSL tunnel through an HTTP proxy. We can't read these
350 // bytes when establishing a tunnel because they might be controlled by
351 // an active network attacker. We don't worry about this for HTTP
352 // because an active network attacker can already control HTTP sessions.
353 // We reach this case when the user cancels a 407 proxy auth prompt. We
354 // also don't worry about this for an HTTPS Proxy, because the
355 // communication with the proxy is secure.
356 // See http://crbug.com/8473.
357 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
358 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
359 LOG(WARNING
) << "Blocked proxy response with status "
360 << headers
->response_code() << " to CONNECT request for "
361 << GetHostAndPort(request_
->url
) << ".";
362 return ERR_TUNNEL_CONNECTION_FAILED
;
365 // Are we using SPDY or HTTP?
366 next_state
= STATE_READ_BODY
;
369 read_buf_len_
= buf_len
;
371 next_state_
= next_state
;
373 if (rv
== ERR_IO_PENDING
)
374 callback_
= callback
;
378 void HttpNetworkTransaction::StopCaching() {}
380 bool HttpNetworkTransaction::GetFullRequestHeaders(
381 HttpRequestHeaders
* headers
) const {
382 // TODO(ttuttle): Make sure we've populated request_headers_.
383 *headers
= request_headers_
;
387 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
388 int64 total_received_bytes
= total_received_bytes_
;
390 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
391 return total_received_bytes
;
394 void HttpNetworkTransaction::DoneReading() {}
396 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
397 return ((headers_valid_
&& response_
.headers
.get()) ||
398 response_
.ssl_info
.cert
.get() || response_
.cert_request_info
.get())
403 LoadState
HttpNetworkTransaction::GetLoadState() const {
404 // TODO(wtc): Define a new LoadState value for the
405 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
406 switch (next_state_
) {
407 case STATE_CREATE_STREAM
:
408 return LOAD_STATE_WAITING_FOR_DELEGATE
;
409 case STATE_CREATE_STREAM_COMPLETE
:
410 return stream_request_
->GetLoadState();
411 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
412 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
413 case STATE_SEND_REQUEST_COMPLETE
:
414 return LOAD_STATE_SENDING_REQUEST
;
415 case STATE_READ_HEADERS_COMPLETE
:
416 return LOAD_STATE_WAITING_FOR_RESPONSE
;
417 case STATE_READ_BODY_COMPLETE
:
418 return LOAD_STATE_READING_RESPONSE
;
420 return LOAD_STATE_IDLE
;
424 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
426 return UploadProgress();
428 return stream_
->GetUploadProgress();
431 void HttpNetworkTransaction::SetQuicServerInfo(
432 QuicServerInfo
* quic_server_info
) {}
434 bool HttpNetworkTransaction::GetLoadTimingInfo(
435 LoadTimingInfo
* load_timing_info
) const {
436 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
439 load_timing_info
->proxy_resolve_start
=
440 proxy_info_
.proxy_resolve_start_time();
441 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
442 load_timing_info
->send_start
= send_start_time_
;
443 load_timing_info
->send_end
= send_end_time_
;
447 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
448 priority_
= priority
;
450 stream_request_
->SetPriority(priority
);
452 stream_
->SetPriority(priority
);
455 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
456 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
457 websocket_handshake_stream_base_create_helper_
= create_helper
;
460 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
461 const BeforeNetworkStartCallback
& callback
) {
462 before_network_start_callback_
= callback
;
465 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
466 const BeforeProxyHeadersSentCallback
& callback
) {
467 before_proxy_headers_sent_callback_
= callback
;
470 int HttpNetworkTransaction::ResumeNetworkStart() {
471 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
475 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
476 const ProxyInfo
& used_proxy_info
,
477 HttpStream
* stream
) {
478 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
479 DCHECK(stream_request_
.get());
482 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
483 stream_
.reset(stream
);
484 server_ssl_config_
= used_ssl_config
;
485 proxy_info_
= used_proxy_info
;
486 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
487 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
488 stream_request_
->protocol_negotiated());
489 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
490 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
491 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
492 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
496 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
497 const SSLConfig
& used_ssl_config
,
498 const ProxyInfo
& used_proxy_info
,
499 WebSocketHandshakeStreamBase
* stream
) {
500 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
503 void HttpNetworkTransaction::OnStreamFailed(int result
,
504 const SSLConfig
& used_ssl_config
) {
505 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
506 DCHECK_NE(OK
, result
);
507 DCHECK(stream_request_
.get());
508 DCHECK(!stream_
.get());
509 server_ssl_config_
= used_ssl_config
;
511 OnIOComplete(result
);
514 void HttpNetworkTransaction::OnCertificateError(
516 const SSLConfig
& used_ssl_config
,
517 const SSLInfo
& ssl_info
) {
518 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
519 DCHECK_NE(OK
, result
);
520 DCHECK(stream_request_
.get());
521 DCHECK(!stream_
.get());
523 response_
.ssl_info
= ssl_info
;
524 server_ssl_config_
= used_ssl_config
;
526 // TODO(mbelshe): For now, we're going to pass the error through, and that
527 // will close the stream_request in all cases. This means that we're always
528 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
529 // good and the user chooses to ignore the error. This is not ideal, but not
530 // the end of the world either.
532 OnIOComplete(result
);
535 void HttpNetworkTransaction::OnNeedsProxyAuth(
536 const HttpResponseInfo
& proxy_response
,
537 const SSLConfig
& used_ssl_config
,
538 const ProxyInfo
& used_proxy_info
,
539 HttpAuthController
* auth_controller
) {
540 DCHECK(stream_request_
.get());
541 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
543 establishing_tunnel_
= true;
544 response_
.headers
= proxy_response
.headers
;
545 response_
.auth_challenge
= proxy_response
.auth_challenge
;
546 headers_valid_
= true;
547 server_ssl_config_
= used_ssl_config
;
548 proxy_info_
= used_proxy_info
;
550 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
551 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
556 void HttpNetworkTransaction::OnNeedsClientAuth(
557 const SSLConfig
& used_ssl_config
,
558 SSLCertRequestInfo
* cert_info
) {
559 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
561 server_ssl_config_
= used_ssl_config
;
562 response_
.cert_request_info
= cert_info
;
563 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
566 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
567 const HttpResponseInfo
& response_info
,
568 const SSLConfig
& used_ssl_config
,
569 const ProxyInfo
& used_proxy_info
,
570 HttpStream
* stream
) {
571 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
573 headers_valid_
= true;
574 response_
= response_info
;
575 server_ssl_config_
= used_ssl_config
;
576 proxy_info_
= used_proxy_info
;
578 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
579 stream_
.reset(stream
);
580 stream_request_
.reset(); // we're done with the stream request
581 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
584 bool HttpNetworkTransaction::IsSecureRequest() const {
585 return request_
->url
.SchemeIsSecure();
588 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
589 return (proxy_info_
.is_http() || proxy_info_
.is_https() ||
590 proxy_info_
.is_quic()) &&
591 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
594 void HttpNetworkTransaction::DoCallback(int rv
) {
595 DCHECK_NE(rv
, ERR_IO_PENDING
);
596 DCHECK(!callback_
.is_null());
598 // Since Run may result in Read being called, clear user_callback_ up front.
599 CompletionCallback c
= callback_
;
604 void HttpNetworkTransaction::OnIOComplete(int result
) {
605 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
606 tracked_objects::ScopedTracker
tracking_profile1(
607 FROM_HERE_WITH_EXPLICIT_FUNCTION(
608 "424359 HttpNetworkTransaction::OnIOComplete 1"));
610 int rv
= DoLoop(result
);
612 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
613 tracked_objects::ScopedTracker
tracking_profile2(
614 FROM_HERE_WITH_EXPLICIT_FUNCTION(
615 "424359 HttpNetworkTransaction::OnIOComplete 2"));
617 if (rv
!= ERR_IO_PENDING
)
621 int HttpNetworkTransaction::DoLoop(int result
) {
622 DCHECK(next_state_
!= STATE_NONE
);
626 State state
= next_state_
;
627 next_state_
= STATE_NONE
;
629 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
631 rv
= DoNotifyBeforeCreateStream();
633 case STATE_CREATE_STREAM
:
635 rv
= DoCreateStream();
637 case STATE_CREATE_STREAM_COMPLETE
:
638 rv
= DoCreateStreamComplete(rv
);
640 case STATE_INIT_STREAM
:
644 case STATE_INIT_STREAM_COMPLETE
:
645 rv
= DoInitStreamComplete(rv
);
647 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
649 rv
= DoGenerateProxyAuthToken();
651 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
652 rv
= DoGenerateProxyAuthTokenComplete(rv
);
654 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
656 rv
= DoGenerateServerAuthToken();
658 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
659 rv
= DoGenerateServerAuthTokenComplete(rv
);
661 case STATE_INIT_REQUEST_BODY
:
663 rv
= DoInitRequestBody();
665 case STATE_INIT_REQUEST_BODY_COMPLETE
:
666 rv
= DoInitRequestBodyComplete(rv
);
668 case STATE_BUILD_REQUEST
:
670 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
671 rv
= DoBuildRequest();
673 case STATE_BUILD_REQUEST_COMPLETE
:
674 rv
= DoBuildRequestComplete(rv
);
676 case STATE_SEND_REQUEST
:
678 rv
= DoSendRequest();
680 case STATE_SEND_REQUEST_COMPLETE
:
681 rv
= DoSendRequestComplete(rv
);
682 net_log_
.EndEventWithNetErrorCode(
683 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
685 case STATE_READ_HEADERS
:
687 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
688 rv
= DoReadHeaders();
690 case STATE_READ_HEADERS_COMPLETE
:
691 rv
= DoReadHeadersComplete(rv
);
692 net_log_
.EndEventWithNetErrorCode(
693 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
695 case STATE_READ_BODY
:
697 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
700 case STATE_READ_BODY_COMPLETE
:
701 rv
= DoReadBodyComplete(rv
);
702 net_log_
.EndEventWithNetErrorCode(
703 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
705 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
708 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
709 rv
= DoDrainBodyForAuthRestart();
711 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
712 rv
= DoDrainBodyForAuthRestartComplete(rv
);
713 net_log_
.EndEventWithNetErrorCode(
714 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
717 NOTREACHED() << "bad state";
721 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
726 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
727 next_state_
= STATE_CREATE_STREAM
;
729 if (!before_network_start_callback_
.is_null())
730 before_network_start_callback_
.Run(&defer
);
733 return ERR_IO_PENDING
;
736 int HttpNetworkTransaction::DoCreateStream() {
737 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
738 if (ForWebSocketHandshake()) {
739 stream_request_
.reset(
740 session_
->http_stream_factory_for_websocket()
741 ->RequestWebSocketHandshakeStream(
747 websocket_handshake_stream_base_create_helper_
,
750 stream_request_
.reset(
751 session_
->http_stream_factory()->RequestStream(
759 DCHECK(stream_request_
.get());
760 return ERR_IO_PENDING
;
763 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
765 next_state_
= STATE_INIT_STREAM
;
766 DCHECK(stream_
.get());
767 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
768 result
= HandleCertificateRequest(result
);
769 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
770 // Return OK and let the caller read the proxy's error page
771 next_state_
= STATE_NONE
;
773 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
774 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
775 return HandleHttp11Required(result
);
778 // Handle possible handshake errors that may have occurred if the stream
779 // used SSL for one or more of the layers.
780 result
= HandleSSLHandshakeError(result
);
782 // At this point we are done with the stream_request_.
783 stream_request_
.reset();
787 int HttpNetworkTransaction::DoInitStream() {
788 DCHECK(stream_
.get());
789 next_state_
= STATE_INIT_STREAM_COMPLETE
;
790 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
793 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
795 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
798 result
= HandleIOError(result
);
800 // The stream initialization failed, so this stream will never be useful.
802 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
809 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
810 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
811 if (!ShouldApplyProxyAuth())
813 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
814 if (!auth_controllers_
[target
].get())
815 auth_controllers_
[target
] =
816 new HttpAuthController(target
,
818 session_
->http_auth_cache(),
819 session_
->http_auth_handler_factory());
820 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
825 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
826 DCHECK_NE(ERR_IO_PENDING
, rv
);
828 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
832 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
833 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
834 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
835 if (!auth_controllers_
[target
].get()) {
836 auth_controllers_
[target
] =
837 new HttpAuthController(target
,
839 session_
->http_auth_cache(),
840 session_
->http_auth_handler_factory());
841 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
842 auth_controllers_
[target
]->DisableEmbeddedIdentity();
844 if (!ShouldApplyServerAuth())
846 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
851 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
852 DCHECK_NE(ERR_IO_PENDING
, rv
);
854 next_state_
= STATE_INIT_REQUEST_BODY
;
858 void HttpNetworkTransaction::BuildRequestHeaders(
859 bool using_http_proxy_without_tunnel
) {
860 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
861 GetHostAndOptionalPort(request_
->url
));
863 // For compat with HTTP/1.0 servers and proxies:
864 if (using_http_proxy_without_tunnel
) {
865 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
868 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
871 // Add a content length header?
872 if (request_
->upload_data_stream
) {
873 if (request_
->upload_data_stream
->is_chunked()) {
874 request_headers_
.SetHeader(
875 HttpRequestHeaders::kTransferEncoding
, "chunked");
877 request_headers_
.SetHeader(
878 HttpRequestHeaders::kContentLength
,
879 base::Uint64ToString(request_
->upload_data_stream
->size()));
881 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
882 request_
->method
== "HEAD") {
883 // An empty POST/PUT request still needs a content length. As for HEAD,
884 // IE and Safari also add a content length header. Presumably it is to
885 // support sending a HEAD request to an URL that only expects to be sent a
886 // POST or some other method that normally would have a message body.
887 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
890 // Honor load flags that impact proxy caches.
891 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
892 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
893 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
894 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
895 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
898 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
899 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
901 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
902 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
905 request_headers_
.MergeFrom(request_
->extra_headers
);
907 if (using_http_proxy_without_tunnel
&&
908 !before_proxy_headers_sent_callback_
.is_null())
909 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
911 response_
.did_use_http_auth
=
912 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
913 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
916 int HttpNetworkTransaction::DoInitRequestBody() {
917 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
919 if (request_
->upload_data_stream
)
920 rv
= request_
->upload_data_stream
->Init(io_callback_
);
924 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
926 next_state_
= STATE_BUILD_REQUEST
;
930 int HttpNetworkTransaction::DoBuildRequest() {
931 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
932 headers_valid_
= false;
934 // This is constructed lazily (instead of within our Start method), so that
935 // we have proxy info available.
936 if (request_headers_
.IsEmpty()) {
937 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
938 BuildRequestHeaders(using_http_proxy_without_tunnel
);
944 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
946 next_state_
= STATE_SEND_REQUEST
;
950 int HttpNetworkTransaction::DoSendRequest() {
951 send_start_time_
= base::TimeTicks::Now();
952 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
954 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
957 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
958 send_end_time_
= base::TimeTicks::Now();
960 return HandleIOError(result
);
961 response_
.network_accessed
= true;
962 next_state_
= STATE_READ_HEADERS
;
966 int HttpNetworkTransaction::DoReadHeaders() {
967 next_state_
= STATE_READ_HEADERS_COMPLETE
;
968 return stream_
->ReadResponseHeaders(io_callback_
);
971 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
972 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
973 // due to SSL renegotiation.
974 if (IsCertificateError(result
)) {
975 // We don't handle a certificate error during SSL renegotiation, so we
976 // have to return an error that's not in the certificate error range
978 LOG(ERROR
) << "Got a server certificate with error " << result
979 << " during SSL renegotiation";
980 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
981 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
982 // TODO(wtc): Need a test case for this code path!
983 DCHECK(stream_
.get());
984 DCHECK(IsSecureRequest());
985 response_
.cert_request_info
= new SSLCertRequestInfo
;
986 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
987 result
= HandleCertificateRequest(result
);
992 if (result
== ERR_HTTP_1_1_REQUIRED
||
993 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
994 return HandleHttp11Required(result
);
997 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
998 // response headers were received, we do the best we can to make sense of it
999 // and send it back up the stack.
1001 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1002 // bizarre for SPDY. Assuming this logic is useful at all.
1003 // TODO(davidben): Bubble the error code up so we do not cache?
1004 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
1008 return HandleIOError(result
);
1010 DCHECK(response_
.headers
.get());
1012 // On a 408 response from the server ("Request Timeout") on a stale socket,
1013 // retry the request.
1014 // Headers can be NULL because of http://crbug.com/384554.
1015 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1016 stream_
->IsConnectionReused()) {
1017 net_log_
.AddEventWithNetErrorCode(
1018 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1019 response_
.headers
->response_code());
1020 // This will close the socket - it would be weird to try and reuse it, even
1021 // if the server doesn't actually close it.
1022 ResetConnectionAndRequestForResend();
1026 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1027 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1028 const int response_code
= response_
.headers
->response_code();
1029 UMA_HISTOGRAM_ENUMERATION(
1030 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1034 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1035 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1037 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1038 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1039 // indicates a buggy server. See:
1040 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1041 if (request_
->method
== "PUT")
1042 return ERR_METHOD_NOT_SUPPORTED
;
1045 // Check for an intermediate 100 Continue response. An origin server is
1046 // allowed to send this response even if we didn't ask for it, so we just
1047 // need to skip over it.
1048 // We treat any other 1xx in this same way (although in practice getting
1049 // a 1xx that isn't a 100 is rare).
1050 // Unless this is a WebSocket request, in which case we pass it on up.
1051 if (response_
.headers
->response_code() / 100 == 1 &&
1052 !ForWebSocketHandshake()) {
1053 response_
.headers
= new HttpResponseHeaders(std::string());
1054 next_state_
= STATE_READ_HEADERS
;
1058 ProcessAlternateProtocol(session_
, *response_
.headers
.get(),
1059 HostPortPair::FromURL(request_
->url
));
1061 int rv
= HandleAuthChallenge();
1065 if (IsSecureRequest())
1066 stream_
->GetSSLInfo(&response_
.ssl_info
);
1068 headers_valid_
= true;
1072 int HttpNetworkTransaction::DoReadBody() {
1073 DCHECK(read_buf_
.get());
1074 DCHECK_GT(read_buf_len_
, 0);
1075 DCHECK(stream_
!= NULL
);
1077 next_state_
= STATE_READ_BODY_COMPLETE
;
1078 return stream_
->ReadResponseBody(
1079 read_buf_
.get(), read_buf_len_
, io_callback_
);
1082 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1083 // We are done with the Read call.
1086 DCHECK_NE(ERR_IO_PENDING
, result
);
1090 bool keep_alive
= false;
1091 if (stream_
->IsResponseBodyComplete()) {
1092 // Note: Just because IsResponseBodyComplete is true, we're not
1093 // necessarily "done". We're only "done" when it is the last
1094 // read on this HttpNetworkTransaction, which will be signified
1095 // by a zero-length read.
1096 // TODO(mbelshe): The keepalive property is really a property of
1097 // the stream. No need to compute it here just to pass back
1098 // to the stream's Close function.
1099 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1101 if (stream_
->CanFindEndOfResponse()) {
1102 HttpResponseHeaders
* headers
= GetResponseHeaders();
1104 keep_alive
= headers
->IsKeepAlive();
1108 // Clean up connection if we are done.
1110 stream_
->Close(!keep_alive
);
1111 // Note: we don't reset the stream here. We've closed it, but we still
1112 // need it around so that callers can call methods such as
1113 // GetUploadProgress() and have them be meaningful.
1114 // TODO(mbelshe): This means we closed the stream here, and we close it
1115 // again in ~HttpNetworkTransaction. Clean that up.
1117 // The next Read call will return 0 (EOF).
1120 // Clear these to avoid leaving around old state.
1127 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1128 // This method differs from DoReadBody only in the next_state_. So we just
1129 // call DoReadBody and override the next_state_. Perhaps there is a more
1130 // elegant way for these two methods to share code.
1131 int rv
= DoReadBody();
1132 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1133 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1137 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1138 // the same. Figure out a good way for these two methods to share code.
1139 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1140 // keep_alive defaults to true because the very reason we're draining the
1141 // response body is to reuse the connection for auth restart.
1142 bool done
= false, keep_alive
= true;
1144 // Error or closed connection while reading the socket.
1147 } else if (stream_
->IsResponseBodyComplete()) {
1152 DidDrainBodyForAuthRestart(keep_alive
);
1155 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1161 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1162 // There are two paths through which the server can request a certificate
1163 // from us. The first is during the initial handshake, the second is
1164 // during SSL renegotiation.
1166 // In both cases, we want to close the connection before proceeding.
1167 // We do this for two reasons:
1168 // First, we don't want to keep the connection to the server hung for a
1169 // long time while the user selects a certificate.
1170 // Second, even if we did keep the connection open, NSS has a bug where
1171 // restarting the handshake for ClientAuth is currently broken.
1172 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1174 if (stream_
.get()) {
1175 // Since we already have a stream, we're being called as part of SSL
1177 DCHECK(!stream_request_
.get());
1178 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1179 stream_
->Close(true);
1183 // The server is asking for a client certificate during the initial
1185 stream_request_
.reset();
1187 // If the user selected one of the certificates in client_certs or declined
1188 // to provide one for this server before, use the past decision
1190 scoped_refptr
<X509Certificate
> client_cert
;
1191 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1192 response_
.cert_request_info
->host_and_port
, &client_cert
);
1193 if (!found_cached_cert
)
1196 // Check that the certificate selected is still a certificate the server
1197 // is likely to accept, based on the criteria supplied in the
1198 // CertificateRequest message.
1199 if (client_cert
.get()) {
1200 const std::vector
<std::string
>& cert_authorities
=
1201 response_
.cert_request_info
->cert_authorities
;
1203 bool cert_still_valid
= cert_authorities
.empty() ||
1204 client_cert
->IsIssuedByEncoded(cert_authorities
);
1205 if (!cert_still_valid
)
1209 // TODO(davidben): Add a unit test which covers this path; we need to be
1210 // able to send a legitimate certificate and also bypass/clear the
1211 // SSL session cache.
1212 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1213 &proxy_ssl_config_
: &server_ssl_config_
;
1214 ssl_config
->send_client_cert
= true;
1215 ssl_config
->client_cert
= client_cert
;
1216 next_state_
= STATE_CREATE_STREAM
;
1217 // Reset the other member variables.
1218 // Note: this is necessary only with SSL renegotiation.
1219 ResetStateForRestart();
1223 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1224 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1225 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1227 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1228 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1230 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1232 ResetConnectionAndRequestForResend();
1236 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1237 if (server_ssl_config_
.send_client_cert
&&
1238 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1239 session_
->ssl_client_auth_cache()->Remove(
1240 HostPortPair::FromURL(request_
->url
));
1244 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1245 // being used, as all of the errors are handled as if they were generated
1246 // by the endpoint host, request_->url, rather than considering if they were
1247 // generated by the SSL proxy. http://crbug.com/69329
1248 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1250 HandleClientAuthError(error
);
1252 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1253 // reflect servers require a deprecated cipher rather than merely prefer
1254 // it. This, however, has no security benefit until the ciphers are actually
1256 if (!server_ssl_config_
.enable_deprecated_cipher_suites
&&
1257 (error
== ERR_SSL_VERSION_OR_CIPHER_MISMATCH
||
1258 error
== ERR_CONNECTION_CLOSED
|| error
== ERR_CONNECTION_RESET
)) {
1260 NetLog::TYPE_SSL_CIPHER_FALLBACK
,
1261 base::Bind(&NetLogSSLCipherFallbackCallback
, &request_
->url
, error
));
1262 server_ssl_config_
.enable_deprecated_cipher_suites
= true;
1263 ResetConnectionAndRequestForResend();
1267 bool should_fallback
= false;
1268 uint16 version_max
= server_ssl_config_
.version_max
;
1271 case ERR_CONNECTION_CLOSED
:
1272 case ERR_SSL_PROTOCOL_ERROR
:
1273 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1274 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1275 version_max
> server_ssl_config_
.version_min
) {
1276 // This could be a TLS-intolerant server or a server that chose a
1277 // cipher suite defined only for higher protocol versions (such as
1278 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1279 // back to the next lower version and retry.
1280 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1281 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1282 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1283 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1284 // version_max should match the maximum protocol version supported
1285 // by the SSLClientSocket class.
1288 // Fallback to the lower SSL version.
1289 // While SSL 3.0 fallback should be eliminated because of security
1290 // reasons, there is a high risk of breaking the servers if this is
1292 should_fallback
= true;
1295 case ERR_CONNECTION_RESET
:
1296 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1297 version_max
> server_ssl_config_
.version_min
) {
1298 // Some network devices that inspect application-layer packets seem to
1299 // inject TCP reset packets to break the connections when they see TLS
1300 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1302 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1303 // 1.2. We don't lose much in this fallback because the explicit IV for
1304 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1305 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1308 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1309 // to trigger a version fallback in general, especially the TLS 1.0 ->
1310 // SSL 3.0 fallback, which would drop TLS extensions.
1312 should_fallback
= true;
1315 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1316 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1317 version_max
> server_ssl_config_
.version_min
) {
1318 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1319 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1320 // crbug.com/260358. In order to make the fallback as minimal as
1321 // possible, this fallback is only triggered for >= TLS 1.1.
1323 should_fallback
= true;
1326 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1327 // The server told us that we should not have fallen back. A buggy server
1328 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1329 // connection. |fallback_error_code_| is initialised to
1330 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1331 error
= fallback_error_code_
;
1335 if (should_fallback
) {
1337 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1338 base::Bind(&NetLogSSLVersionFallbackCallback
,
1339 &request_
->url
, error
, server_ssl_config_
.version_max
,
1341 fallback_error_code_
= error
;
1342 server_ssl_config_
.version_max
= version_max
;
1343 server_ssl_config_
.version_fallback
= true;
1344 ResetConnectionAndRequestForResend();
1351 // This method determines whether it is safe to resend the request after an
1352 // IO error. It can only be called in response to request header or body
1353 // write errors or response header read errors. It should not be used in
1354 // other cases, such as a Connect error.
1355 int HttpNetworkTransaction::HandleIOError(int error
) {
1356 // Because the peer may request renegotiation with client authentication at
1357 // any time, check and handle client authentication errors.
1358 HandleClientAuthError(error
);
1361 // If we try to reuse a connection that the server is in the process of
1362 // closing, we may end up successfully writing out our request (or a
1363 // portion of our request) only to find a connection error when we try to
1364 // read from (or finish writing to) the socket.
1365 case ERR_CONNECTION_RESET
:
1366 case ERR_CONNECTION_CLOSED
:
1367 case ERR_CONNECTION_ABORTED
:
1368 // There can be a race between the socket pool checking checking whether a
1369 // socket is still connected, receiving the FIN, and sending/reading data
1370 // on a reused socket. If we receive the FIN between the connectedness
1371 // check and writing/reading from the socket, we may first learn the socket
1372 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1373 // likely happen when trying to retrieve its IP address.
1374 // See http://crbug.com/105824 for more details.
1375 case ERR_SOCKET_NOT_CONNECTED
:
1376 // If a socket is closed on its initial request, HttpStreamParser returns
1377 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1378 // preconnected but failed to be used before the server timed it out.
1379 case ERR_EMPTY_RESPONSE
:
1380 if (ShouldResendRequest()) {
1381 net_log_
.AddEventWithNetErrorCode(
1382 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1383 ResetConnectionAndRequestForResend();
1387 case ERR_SPDY_PING_FAILED
:
1388 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1389 case ERR_QUIC_HANDSHAKE_FAILED
:
1390 net_log_
.AddEventWithNetErrorCode(
1391 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1392 ResetConnectionAndRequestForResend();
1399 void HttpNetworkTransaction::ResetStateForRestart() {
1400 ResetStateForAuthRestart();
1402 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1406 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1407 send_start_time_
= base::TimeTicks();
1408 send_end_time_
= base::TimeTicks();
1410 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1413 headers_valid_
= false;
1414 request_headers_
.Clear();
1415 response_
= HttpResponseInfo();
1416 establishing_tunnel_
= false;
1419 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1420 return response_
.headers
.get();
1423 bool HttpNetworkTransaction::ShouldResendRequest() const {
1424 bool connection_is_proven
= stream_
->IsConnectionReused();
1425 bool has_received_headers
= GetResponseHeaders() != NULL
;
1427 // NOTE: we resend a request only if we reused a keep-alive connection.
1428 // This automatically prevents an infinite resend loop because we'll run
1429 // out of the cached keep-alive connections eventually.
1430 if (connection_is_proven
&& !has_received_headers
)
1435 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1436 if (stream_
.get()) {
1437 stream_
->Close(true);
1441 // We need to clear request_headers_ because it contains the real request
1442 // headers, but we may need to resend the CONNECT request first to recreate
1444 request_headers_
.Clear();
1445 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1448 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1449 return UsingHttpProxyWithoutTunnel();
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 if (ForWebSocketHandshake()) {
1507 const GURL
& url
= request_
->url
;
1508 url::Replacements
<char> ws_to_http
;
1509 if (url
.SchemeIs("ws")) {
1510 ws_to_http
.SetScheme("http", url::Component(0, 4));
1512 DCHECK(url
.SchemeIs("wss"));
1513 ws_to_http
.SetScheme("https", url::Component(0, 5));
1515 return url
.ReplaceComponents(ws_to_http
);
1517 return request_
->url
;
1523 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1524 return websocket_handshake_stream_base_create_helper_
&&
1525 request_
->url
.SchemeIsWSOrWSS();
1528 #define STATE_CASE(s) \
1530 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1533 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1534 std::string description
;
1536 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM
);
1537 STATE_CASE(STATE_CREATE_STREAM
);
1538 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1539 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1540 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1541 STATE_CASE(STATE_BUILD_REQUEST
);
1542 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1543 STATE_CASE(STATE_SEND_REQUEST
);
1544 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1545 STATE_CASE(STATE_READ_HEADERS
);
1546 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1547 STATE_CASE(STATE_READ_BODY
);
1548 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1549 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1550 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1551 STATE_CASE(STATE_NONE
);
1553 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,