1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_network_transaction.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/compiler_specific.h"
13 #include "base/format_macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/field_trial.h"
16 #include "base/metrics/histogram.h"
17 #include "base/metrics/stats_counters.h"
18 #include "base/profiler/scoped_tracker.h"
19 #include "base/stl_util.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/time/time.h"
24 #include "base/values.h"
25 #include "build/build_config.h"
26 #include "net/base/auth.h"
27 #include "net/base/host_port_pair.h"
28 #include "net/base/io_buffer.h"
29 #include "net/base/load_flags.h"
30 #include "net/base/load_timing_info.h"
31 #include "net/base/net_errors.h"
32 #include "net/base/net_util.h"
33 #include "net/base/upload_data_stream.h"
34 #include "net/http/http_auth.h"
35 #include "net/http/http_auth_handler.h"
36 #include "net/http/http_auth_handler_factory.h"
37 #include "net/http/http_basic_stream.h"
38 #include "net/http/http_chunked_decoder.h"
39 #include "net/http/http_network_session.h"
40 #include "net/http/http_proxy_client_socket.h"
41 #include "net/http/http_proxy_client_socket_pool.h"
42 #include "net/http/http_request_headers.h"
43 #include "net/http/http_request_info.h"
44 #include "net/http/http_response_headers.h"
45 #include "net/http/http_response_info.h"
46 #include "net/http/http_server_properties.h"
47 #include "net/http/http_status_code.h"
48 #include "net/http/http_stream.h"
49 #include "net/http/http_stream_factory.h"
50 #include "net/http/http_util.h"
51 #include "net/http/transport_security_state.h"
52 #include "net/http/url_security_manager.h"
53 #include "net/socket/client_socket_factory.h"
54 #include "net/socket/socks_client_socket_pool.h"
55 #include "net/socket/ssl_client_socket.h"
56 #include "net/socket/ssl_client_socket_pool.h"
57 #include "net/socket/transport_client_socket_pool.h"
58 #include "net/spdy/hpack_huffman_aggregator.h"
59 #include "net/spdy/spdy_http_stream.h"
60 #include "net/spdy/spdy_session.h"
61 #include "net/spdy/spdy_session_pool.h"
62 #include "net/ssl/ssl_cert_request_info.h"
63 #include "net/ssl/ssl_connection_status_flags.h"
65 #include "url/url_canon.h"
71 void ProcessAlternateProtocol(
72 HttpNetworkSession
* session
,
73 const HttpResponseHeaders
& headers
,
74 const HostPortPair
& http_host_port_pair
) {
75 if (!headers
.HasHeader(kAlternateProtocolHeader
))
78 std::vector
<std::string
> alternate_protocol_values
;
80 std::string alternate_protocol_str
;
81 while (headers
.EnumerateHeader(&iter
, kAlternateProtocolHeader
,
82 &alternate_protocol_str
)) {
83 alternate_protocol_values
.push_back(alternate_protocol_str
);
86 session
->http_stream_factory()->ProcessAlternateProtocol(
87 session
->http_server_properties(),
88 alternate_protocol_values
,
93 base::Value
* NetLogSSLVersionFallbackCallback(
96 uint16 version_before
,
98 NetLog::LogLevel
/* log_level */) {
99 base::DictionaryValue
* dict
= new base::DictionaryValue();
100 dict
->SetString("host_and_port", GetHostAndPort(*url
));
101 dict
->SetInteger("net_error", net_error
);
102 dict
->SetInteger("version_before", version_before
);
103 dict
->SetInteger("version_after", version_after
);
109 //-----------------------------------------------------------------------------
111 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
112 HttpNetworkSession
* session
)
113 : pending_auth_target_(HttpAuth::AUTH_NONE
),
114 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
115 base::Unretained(this))),
119 headers_valid_(false),
120 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
123 total_received_bytes_(0),
124 next_state_(STATE_NONE
),
125 establishing_tunnel_(false),
126 websocket_handshake_stream_base_create_helper_(NULL
) {
127 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
128 session
->GetNextProtos(&server_ssl_config_
.next_protos
);
129 proxy_ssl_config_
= server_ssl_config_
;
132 HttpNetworkTransaction::~HttpNetworkTransaction() {
134 HttpResponseHeaders
* headers
= GetResponseHeaders();
135 // TODO(mbelshe): The stream_ should be able to compute whether or not the
136 // stream should be kept alive. No reason to compute here
138 bool try_to_keep_alive
=
139 next_state_
== STATE_NONE
&&
140 stream_
->CanFindEndOfResponse() &&
141 (!headers
|| headers
->IsKeepAlive());
142 if (!try_to_keep_alive
) {
143 stream_
->Close(true /* not reusable */);
145 if (stream_
->IsResponseBodyComplete()) {
146 // If the response body is complete, we can just reuse the socket.
147 stream_
->Close(false /* reusable */);
148 } else if (stream_
->IsSpdyHttpStream()) {
149 // Doesn't really matter for SpdyHttpStream. Just close it.
150 stream_
->Close(true /* not reusable */);
152 // Otherwise, we try to drain the response body.
153 HttpStream
* stream
= stream_
.release();
154 stream
->Drain(session_
);
159 if (request_
&& request_
->upload_data_stream
)
160 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
163 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
164 const CompletionCallback
& callback
,
165 const BoundNetLog
& net_log
) {
166 SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
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 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
185 if (rv
== ERR_IO_PENDING
)
186 callback_
= callback
;
190 int HttpNetworkTransaction::RestartIgnoringLastError(
191 const CompletionCallback
& callback
) {
192 DCHECK(!stream_
.get());
193 DCHECK(!stream_request_
.get());
194 DCHECK_EQ(STATE_NONE
, next_state_
);
196 next_state_
= STATE_CREATE_STREAM
;
199 if (rv
== ERR_IO_PENDING
)
200 callback_
= callback
;
204 int HttpNetworkTransaction::RestartWithCertificate(
205 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
206 // In HandleCertificateRequest(), we always tear down existing stream
207 // requests to force a new connection. So we shouldn't have one here.
208 DCHECK(!stream_request_
.get());
209 DCHECK(!stream_
.get());
210 DCHECK_EQ(STATE_NONE
, next_state_
);
212 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
213 &proxy_ssl_config_
: &server_ssl_config_
;
214 ssl_config
->send_client_cert
= true;
215 ssl_config
->client_cert
= client_cert
;
216 session_
->ssl_client_auth_cache()->Add(
217 response_
.cert_request_info
->host_and_port
, client_cert
);
218 // Reset the other member variables.
219 // Note: this is necessary only with SSL renegotiation.
220 ResetStateForRestart();
221 next_state_
= STATE_CREATE_STREAM
;
223 if (rv
== ERR_IO_PENDING
)
224 callback_
= callback
;
228 int HttpNetworkTransaction::RestartWithAuth(
229 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
230 HttpAuth::Target target
= pending_auth_target_
;
231 if (target
== HttpAuth::AUTH_NONE
) {
233 return ERR_UNEXPECTED
;
235 pending_auth_target_
= HttpAuth::AUTH_NONE
;
237 auth_controllers_
[target
]->ResetAuth(credentials
);
239 DCHECK(callback_
.is_null());
242 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
243 // In this case, we've gathered credentials for use with proxy
244 // authentication of a tunnel.
245 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
246 DCHECK(stream_request_
!= NULL
);
247 auth_controllers_
[target
] = NULL
;
248 ResetStateForRestart();
249 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
251 // In this case, we've gathered credentials for the server or the proxy
252 // but it is not during the tunneling phase.
253 DCHECK(stream_request_
== NULL
);
254 PrepareForAuthRestart(target
);
258 if (rv
== ERR_IO_PENDING
)
259 callback_
= callback
;
263 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
264 DCHECK(HaveAuth(target
));
265 DCHECK(!stream_request_
.get());
267 bool keep_alive
= false;
268 // Even if the server says the connection is keep-alive, we have to be
269 // able to find the end of each response in order to reuse the connection.
270 if (GetResponseHeaders()->IsKeepAlive() &&
271 stream_
->CanFindEndOfResponse()) {
272 // If the response body hasn't been completely read, we need to drain
274 if (!stream_
->IsResponseBodyComplete()) {
275 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
276 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
277 read_buf_len_
= kDrainBodyBufferSize
;
283 // We don't need to drain the response body, so we act as if we had drained
284 // the response body.
285 DidDrainBodyForAuthRestart(keep_alive
);
288 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
289 DCHECK(!stream_request_
.get());
292 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
293 HttpStream
* new_stream
= NULL
;
294 if (keep_alive
&& stream_
->IsConnectionReusable()) {
295 // We should call connection_->set_idle_time(), but this doesn't occur
296 // often enough to be worth the trouble.
297 stream_
->SetConnectionReused();
298 new_stream
= stream_
->RenewStreamForAuth();
302 // Close the stream and mark it as not_reusable. Even in the
303 // keep_alive case, we've determined that the stream_ is not
304 // reusable if new_stream is NULL.
305 stream_
->Close(true);
306 next_state_
= STATE_CREATE_STREAM
;
308 // Renewed streams shouldn't carry over received bytes.
309 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
310 next_state_
= STATE_INIT_STREAM
;
312 stream_
.reset(new_stream
);
315 // Reset the other member variables.
316 ResetStateForAuthRestart();
319 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
320 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
321 HaveAuth(pending_auth_target_
);
324 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
325 const CompletionCallback
& callback
) {
327 DCHECK_LT(0, buf_len
);
329 State next_state
= STATE_NONE
;
331 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
332 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
333 // We're trying to read the body of the response but we're still trying
334 // to establish an SSL tunnel through an HTTP proxy. We can't read these
335 // bytes when establishing a tunnel because they might be controlled by
336 // an active network attacker. We don't worry about this for HTTP
337 // because an active network attacker can already control HTTP sessions.
338 // We reach this case when the user cancels a 407 proxy auth prompt. We
339 // also don't worry about this for an HTTPS Proxy, because the
340 // communication with the proxy is secure.
341 // See http://crbug.com/8473.
342 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
343 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
344 LOG(WARNING
) << "Blocked proxy response with status "
345 << headers
->response_code() << " to CONNECT request for "
346 << GetHostAndPort(request_
->url
) << ".";
347 return ERR_TUNNEL_CONNECTION_FAILED
;
350 // Are we using SPDY or HTTP?
351 next_state
= STATE_READ_BODY
;
354 read_buf_len_
= buf_len
;
356 next_state_
= next_state
;
358 if (rv
== ERR_IO_PENDING
)
359 callback_
= callback
;
363 void HttpNetworkTransaction::StopCaching() {}
365 bool HttpNetworkTransaction::GetFullRequestHeaders(
366 HttpRequestHeaders
* headers
) const {
367 // TODO(ttuttle): Make sure we've populated request_headers_.
368 *headers
= request_headers_
;
372 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
373 int64 total_received_bytes
= total_received_bytes_
;
375 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
376 return total_received_bytes
;
379 void HttpNetworkTransaction::DoneReading() {}
381 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
382 return ((headers_valid_
&& response_
.headers
.get()) ||
383 response_
.ssl_info
.cert
.get() || response_
.cert_request_info
.get())
388 LoadState
HttpNetworkTransaction::GetLoadState() const {
389 // TODO(wtc): Define a new LoadState value for the
390 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
391 switch (next_state_
) {
392 case STATE_CREATE_STREAM
:
393 return LOAD_STATE_WAITING_FOR_DELEGATE
;
394 case STATE_CREATE_STREAM_COMPLETE
:
395 return stream_request_
->GetLoadState();
396 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
397 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
398 case STATE_SEND_REQUEST_COMPLETE
:
399 return LOAD_STATE_SENDING_REQUEST
;
400 case STATE_READ_HEADERS_COMPLETE
:
401 return LOAD_STATE_WAITING_FOR_RESPONSE
;
402 case STATE_READ_BODY_COMPLETE
:
403 return LOAD_STATE_READING_RESPONSE
;
405 return LOAD_STATE_IDLE
;
409 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
411 return UploadProgress();
413 return stream_
->GetUploadProgress();
416 void HttpNetworkTransaction::SetQuicServerInfo(
417 QuicServerInfo
* quic_server_info
) {}
419 bool HttpNetworkTransaction::GetLoadTimingInfo(
420 LoadTimingInfo
* load_timing_info
) const {
421 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
424 load_timing_info
->proxy_resolve_start
=
425 proxy_info_
.proxy_resolve_start_time();
426 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
427 load_timing_info
->send_start
= send_start_time_
;
428 load_timing_info
->send_end
= send_end_time_
;
432 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
433 priority_
= priority
;
435 stream_request_
->SetPriority(priority
);
437 stream_
->SetPriority(priority
);
440 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
441 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
442 websocket_handshake_stream_base_create_helper_
= create_helper
;
445 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
446 const BeforeNetworkStartCallback
& callback
) {
447 before_network_start_callback_
= callback
;
450 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
451 const BeforeProxyHeadersSentCallback
& callback
) {
452 before_proxy_headers_sent_callback_
= callback
;
455 int HttpNetworkTransaction::ResumeNetworkStart() {
456 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
460 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
461 const ProxyInfo
& used_proxy_info
,
462 HttpStream
* stream
) {
463 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
464 DCHECK(stream_request_
.get());
467 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
468 stream_
.reset(stream
);
469 server_ssl_config_
= used_ssl_config
;
470 proxy_info_
= used_proxy_info
;
471 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
472 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
473 stream_request_
->protocol_negotiated());
474 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
475 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
476 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
477 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
481 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
482 const SSLConfig
& used_ssl_config
,
483 const ProxyInfo
& used_proxy_info
,
484 WebSocketHandshakeStreamBase
* stream
) {
485 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
488 void HttpNetworkTransaction::OnStreamFailed(int result
,
489 const SSLConfig
& used_ssl_config
) {
490 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
491 DCHECK_NE(OK
, result
);
492 DCHECK(stream_request_
.get());
493 DCHECK(!stream_
.get());
494 server_ssl_config_
= used_ssl_config
;
496 OnIOComplete(result
);
499 void HttpNetworkTransaction::OnCertificateError(
501 const SSLConfig
& used_ssl_config
,
502 const SSLInfo
& ssl_info
) {
503 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
504 DCHECK_NE(OK
, result
);
505 DCHECK(stream_request_
.get());
506 DCHECK(!stream_
.get());
508 response_
.ssl_info
= ssl_info
;
509 server_ssl_config_
= used_ssl_config
;
511 // TODO(mbelshe): For now, we're going to pass the error through, and that
512 // will close the stream_request in all cases. This means that we're always
513 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
514 // good and the user chooses to ignore the error. This is not ideal, but not
515 // the end of the world either.
517 OnIOComplete(result
);
520 void HttpNetworkTransaction::OnNeedsProxyAuth(
521 const HttpResponseInfo
& proxy_response
,
522 const SSLConfig
& used_ssl_config
,
523 const ProxyInfo
& used_proxy_info
,
524 HttpAuthController
* auth_controller
) {
525 DCHECK(stream_request_
.get());
526 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
528 establishing_tunnel_
= true;
529 response_
.headers
= proxy_response
.headers
;
530 response_
.auth_challenge
= proxy_response
.auth_challenge
;
531 headers_valid_
= true;
532 server_ssl_config_
= used_ssl_config
;
533 proxy_info_
= used_proxy_info
;
535 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
536 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
541 void HttpNetworkTransaction::OnNeedsClientAuth(
542 const SSLConfig
& used_ssl_config
,
543 SSLCertRequestInfo
* cert_info
) {
544 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
546 server_ssl_config_
= used_ssl_config
;
547 response_
.cert_request_info
= cert_info
;
548 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
551 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
552 const HttpResponseInfo
& response_info
,
553 const SSLConfig
& used_ssl_config
,
554 const ProxyInfo
& used_proxy_info
,
555 HttpStream
* stream
) {
556 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
558 headers_valid_
= true;
559 response_
= response_info
;
560 server_ssl_config_
= used_ssl_config
;
561 proxy_info_
= used_proxy_info
;
563 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
564 stream_
.reset(stream
);
565 stream_request_
.reset(); // we're done with the stream request
566 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
569 bool HttpNetworkTransaction::is_https_request() const {
570 return request_
->url
.SchemeIs("https");
573 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
574 return (proxy_info_
.is_http() || proxy_info_
.is_https()) &&
575 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
578 void HttpNetworkTransaction::DoCallback(int rv
) {
579 DCHECK_NE(rv
, ERR_IO_PENDING
);
580 DCHECK(!callback_
.is_null());
582 // Since Run may result in Read being called, clear user_callback_ up front.
583 CompletionCallback c
= callback_
;
588 void HttpNetworkTransaction::OnIOComplete(int result
) {
589 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
590 tracked_objects::ScopedTracker
tracking_profile1(
591 FROM_HERE_WITH_EXPLICIT_FUNCTION(
592 "424359 HttpNetworkTransaction::OnIOComplete 1"));
594 int rv
= DoLoop(result
);
596 // TODO(vadimt): Remove ScopedTracker below once crbug.com/424359 is fixed.
597 tracked_objects::ScopedTracker
tracking_profile2(
598 FROM_HERE_WITH_EXPLICIT_FUNCTION(
599 "424359 HttpNetworkTransaction::OnIOComplete 2"));
601 if (rv
!= ERR_IO_PENDING
)
605 int HttpNetworkTransaction::DoLoop(int result
) {
606 DCHECK(next_state_
!= STATE_NONE
);
610 State state
= next_state_
;
611 next_state_
= STATE_NONE
;
613 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
615 rv
= DoNotifyBeforeCreateStream();
617 case STATE_CREATE_STREAM
:
619 rv
= DoCreateStream();
621 case STATE_CREATE_STREAM_COMPLETE
:
622 rv
= DoCreateStreamComplete(rv
);
624 case STATE_INIT_STREAM
:
628 case STATE_INIT_STREAM_COMPLETE
:
629 rv
= DoInitStreamComplete(rv
);
631 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
633 rv
= DoGenerateProxyAuthToken();
635 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
636 rv
= DoGenerateProxyAuthTokenComplete(rv
);
638 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
640 rv
= DoGenerateServerAuthToken();
642 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
643 rv
= DoGenerateServerAuthTokenComplete(rv
);
645 case STATE_INIT_REQUEST_BODY
:
647 rv
= DoInitRequestBody();
649 case STATE_INIT_REQUEST_BODY_COMPLETE
:
650 rv
= DoInitRequestBodyComplete(rv
);
652 case STATE_BUILD_REQUEST
:
654 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
655 rv
= DoBuildRequest();
657 case STATE_BUILD_REQUEST_COMPLETE
:
658 rv
= DoBuildRequestComplete(rv
);
660 case STATE_SEND_REQUEST
:
662 rv
= DoSendRequest();
664 case STATE_SEND_REQUEST_COMPLETE
:
665 rv
= DoSendRequestComplete(rv
);
666 net_log_
.EndEventWithNetErrorCode(
667 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
669 case STATE_READ_HEADERS
:
671 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
672 rv
= DoReadHeaders();
674 case STATE_READ_HEADERS_COMPLETE
:
675 rv
= DoReadHeadersComplete(rv
);
676 net_log_
.EndEventWithNetErrorCode(
677 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
679 case STATE_READ_BODY
:
681 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
684 case STATE_READ_BODY_COMPLETE
:
685 rv
= DoReadBodyComplete(rv
);
686 net_log_
.EndEventWithNetErrorCode(
687 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
689 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
692 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
693 rv
= DoDrainBodyForAuthRestart();
695 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
696 rv
= DoDrainBodyForAuthRestartComplete(rv
);
697 net_log_
.EndEventWithNetErrorCode(
698 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
701 NOTREACHED() << "bad state";
705 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
710 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
711 next_state_
= STATE_CREATE_STREAM
;
713 if (!before_network_start_callback_
.is_null())
714 before_network_start_callback_
.Run(&defer
);
717 return ERR_IO_PENDING
;
720 int HttpNetworkTransaction::DoCreateStream() {
721 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
722 if (ForWebSocketHandshake()) {
723 stream_request_
.reset(
724 session_
->http_stream_factory_for_websocket()
725 ->RequestWebSocketHandshakeStream(
731 websocket_handshake_stream_base_create_helper_
,
734 stream_request_
.reset(
735 session_
->http_stream_factory()->RequestStream(
743 DCHECK(stream_request_
.get());
744 return ERR_IO_PENDING
;
747 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
749 next_state_
= STATE_INIT_STREAM
;
750 DCHECK(stream_
.get());
751 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
752 result
= HandleCertificateRequest(result
);
753 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
754 // Return OK and let the caller read the proxy's error page
755 next_state_
= STATE_NONE
;
757 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
758 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
759 return HandleHttp11Required(result
);
762 // Handle possible handshake errors that may have occurred if the stream
763 // used SSL for one or more of the layers.
764 result
= HandleSSLHandshakeError(result
);
766 // At this point we are done with the stream_request_.
767 stream_request_
.reset();
771 int HttpNetworkTransaction::DoInitStream() {
772 DCHECK(stream_
.get());
773 next_state_
= STATE_INIT_STREAM_COMPLETE
;
774 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
777 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
779 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
782 result
= HandleIOError(result
);
784 // The stream initialization failed, so this stream will never be useful.
786 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
793 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
794 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
795 if (!ShouldApplyProxyAuth())
797 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
798 if (!auth_controllers_
[target
].get())
799 auth_controllers_
[target
] =
800 new HttpAuthController(target
,
802 session_
->http_auth_cache(),
803 session_
->http_auth_handler_factory());
804 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
809 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
810 DCHECK_NE(ERR_IO_PENDING
, rv
);
812 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
816 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
817 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
818 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
819 if (!auth_controllers_
[target
].get()) {
820 auth_controllers_
[target
] =
821 new HttpAuthController(target
,
823 session_
->http_auth_cache(),
824 session_
->http_auth_handler_factory());
825 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
826 auth_controllers_
[target
]->DisableEmbeddedIdentity();
828 if (!ShouldApplyServerAuth())
830 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
835 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
836 DCHECK_NE(ERR_IO_PENDING
, rv
);
838 next_state_
= STATE_INIT_REQUEST_BODY
;
842 void HttpNetworkTransaction::BuildRequestHeaders(
843 bool using_http_proxy_without_tunnel
) {
844 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
845 GetHostAndOptionalPort(request_
->url
));
847 // For compat with HTTP/1.0 servers and proxies:
848 if (using_http_proxy_without_tunnel
) {
849 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
852 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
855 // Add a content length header?
856 if (request_
->upload_data_stream
) {
857 if (request_
->upload_data_stream
->is_chunked()) {
858 request_headers_
.SetHeader(
859 HttpRequestHeaders::kTransferEncoding
, "chunked");
861 request_headers_
.SetHeader(
862 HttpRequestHeaders::kContentLength
,
863 base::Uint64ToString(request_
->upload_data_stream
->size()));
865 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
866 request_
->method
== "HEAD") {
867 // An empty POST/PUT request still needs a content length. As for HEAD,
868 // IE and Safari also add a content length header. Presumably it is to
869 // support sending a HEAD request to an URL that only expects to be sent a
870 // POST or some other method that normally would have a message body.
871 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
874 // Honor load flags that impact proxy caches.
875 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
876 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
877 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
878 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
879 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
882 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
883 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
885 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
886 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
889 request_headers_
.MergeFrom(request_
->extra_headers
);
891 if (using_http_proxy_without_tunnel
&&
892 !before_proxy_headers_sent_callback_
.is_null())
893 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
895 response_
.did_use_http_auth
=
896 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
897 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
900 int HttpNetworkTransaction::DoInitRequestBody() {
901 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
903 if (request_
->upload_data_stream
)
904 rv
= request_
->upload_data_stream
->Init(io_callback_
);
908 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
910 next_state_
= STATE_BUILD_REQUEST
;
914 int HttpNetworkTransaction::DoBuildRequest() {
915 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
916 headers_valid_
= false;
918 // This is constructed lazily (instead of within our Start method), so that
919 // we have proxy info available.
920 if (request_headers_
.IsEmpty()) {
921 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
922 BuildRequestHeaders(using_http_proxy_without_tunnel
);
928 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
930 next_state_
= STATE_SEND_REQUEST
;
934 int HttpNetworkTransaction::DoSendRequest() {
935 send_start_time_
= base::TimeTicks::Now();
936 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
938 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
941 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
942 send_end_time_
= base::TimeTicks::Now();
944 return HandleIOError(result
);
945 response_
.network_accessed
= true;
946 next_state_
= STATE_READ_HEADERS
;
950 int HttpNetworkTransaction::DoReadHeaders() {
951 next_state_
= STATE_READ_HEADERS_COMPLETE
;
952 return stream_
->ReadResponseHeaders(io_callback_
);
955 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
956 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
957 // due to SSL renegotiation.
958 if (IsCertificateError(result
)) {
959 // We don't handle a certificate error during SSL renegotiation, so we
960 // have to return an error that's not in the certificate error range
962 LOG(ERROR
) << "Got a server certificate with error " << result
963 << " during SSL renegotiation";
964 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
965 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
966 // TODO(wtc): Need a test case for this code path!
967 DCHECK(stream_
.get());
968 DCHECK(is_https_request());
969 response_
.cert_request_info
= new SSLCertRequestInfo
;
970 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
971 result
= HandleCertificateRequest(result
);
976 if (result
== ERR_HTTP_1_1_REQUIRED
||
977 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
978 return HandleHttp11Required(result
);
981 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
982 // response headers were received, we do the best we can to make sense of it
983 // and send it back up the stack.
985 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
986 // bizarre for SPDY. Assuming this logic is useful at all.
987 // TODO(davidben): Bubble the error code up so we do not cache?
988 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
992 return HandleIOError(result
);
994 DCHECK(response_
.headers
.get());
996 // On a 408 response from the server ("Request Timeout") on a stale socket,
997 // retry the request.
998 // Headers can be NULL because of http://crbug.com/384554.
999 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1000 stream_
->IsConnectionReused()) {
1001 net_log_
.AddEventWithNetErrorCode(
1002 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1003 response_
.headers
->response_code());
1004 // This will close the socket - it would be weird to try and reuse it, even
1005 // if the server doesn't actually close it.
1006 ResetConnectionAndRequestForResend();
1010 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1011 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1012 const int response_code
= response_
.headers
->response_code();
1013 UMA_HISTOGRAM_ENUMERATION(
1014 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1018 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1019 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1021 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1022 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1023 // indicates a buggy server. See:
1024 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1025 if (request_
->method
== "PUT")
1026 return ERR_METHOD_NOT_SUPPORTED
;
1029 // Check for an intermediate 100 Continue response. An origin server is
1030 // allowed to send this response even if we didn't ask for it, so we just
1031 // need to skip over it.
1032 // We treat any other 1xx in this same way (although in practice getting
1033 // a 1xx that isn't a 100 is rare).
1034 // Unless this is a WebSocket request, in which case we pass it on up.
1035 if (response_
.headers
->response_code() / 100 == 1 &&
1036 !ForWebSocketHandshake()) {
1037 response_
.headers
= new HttpResponseHeaders(std::string());
1038 next_state_
= STATE_READ_HEADERS
;
1042 ProcessAlternateProtocol(session_
, *response_
.headers
.get(),
1043 HostPortPair::FromURL(request_
->url
));
1045 int rv
= HandleAuthChallenge();
1049 if (is_https_request())
1050 stream_
->GetSSLInfo(&response_
.ssl_info
);
1052 headers_valid_
= true;
1054 if (session_
->huffman_aggregator()) {
1055 session_
->huffman_aggregator()->AggregateTransactionCharacterCounts(
1058 proxy_info_
.proxy_server(),
1059 *response_
.headers
.get());
1064 int HttpNetworkTransaction::DoReadBody() {
1065 DCHECK(read_buf_
.get());
1066 DCHECK_GT(read_buf_len_
, 0);
1067 DCHECK(stream_
!= NULL
);
1069 next_state_
= STATE_READ_BODY_COMPLETE
;
1070 return stream_
->ReadResponseBody(
1071 read_buf_
.get(), read_buf_len_
, io_callback_
);
1074 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1075 // We are done with the Read call.
1078 DCHECK_NE(ERR_IO_PENDING
, result
);
1082 bool keep_alive
= false;
1083 if (stream_
->IsResponseBodyComplete()) {
1084 // Note: Just because IsResponseBodyComplete is true, we're not
1085 // necessarily "done". We're only "done" when it is the last
1086 // read on this HttpNetworkTransaction, which will be signified
1087 // by a zero-length read.
1088 // TODO(mbelshe): The keepalive property is really a property of
1089 // the stream. No need to compute it here just to pass back
1090 // to the stream's Close function.
1091 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1093 if (stream_
->CanFindEndOfResponse()) {
1094 HttpResponseHeaders
* headers
= GetResponseHeaders();
1096 keep_alive
= headers
->IsKeepAlive();
1100 // Clean up connection if we are done.
1102 stream_
->Close(!keep_alive
);
1103 // Note: we don't reset the stream here. We've closed it, but we still
1104 // need it around so that callers can call methods such as
1105 // GetUploadProgress() and have them be meaningful.
1106 // TODO(mbelshe): This means we closed the stream here, and we close it
1107 // again in ~HttpNetworkTransaction. Clean that up.
1109 // The next Read call will return 0 (EOF).
1112 // Clear these to avoid leaving around old state.
1119 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1120 // This method differs from DoReadBody only in the next_state_. So we just
1121 // call DoReadBody and override the next_state_. Perhaps there is a more
1122 // elegant way for these two methods to share code.
1123 int rv
= DoReadBody();
1124 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1125 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1129 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1130 // the same. Figure out a good way for these two methods to share code.
1131 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1132 // keep_alive defaults to true because the very reason we're draining the
1133 // response body is to reuse the connection for auth restart.
1134 bool done
= false, keep_alive
= true;
1136 // Error or closed connection while reading the socket.
1139 } else if (stream_
->IsResponseBodyComplete()) {
1144 DidDrainBodyForAuthRestart(keep_alive
);
1147 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1153 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1154 // There are two paths through which the server can request a certificate
1155 // from us. The first is during the initial handshake, the second is
1156 // during SSL renegotiation.
1158 // In both cases, we want to close the connection before proceeding.
1159 // We do this for two reasons:
1160 // First, we don't want to keep the connection to the server hung for a
1161 // long time while the user selects a certificate.
1162 // Second, even if we did keep the connection open, NSS has a bug where
1163 // restarting the handshake for ClientAuth is currently broken.
1164 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1166 if (stream_
.get()) {
1167 // Since we already have a stream, we're being called as part of SSL
1169 DCHECK(!stream_request_
.get());
1170 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1171 stream_
->Close(true);
1175 // The server is asking for a client certificate during the initial
1177 stream_request_
.reset();
1179 // If the user selected one of the certificates in client_certs or declined
1180 // to provide one for this server before, use the past decision
1182 scoped_refptr
<X509Certificate
> client_cert
;
1183 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1184 response_
.cert_request_info
->host_and_port
, &client_cert
);
1185 if (!found_cached_cert
)
1188 // Check that the certificate selected is still a certificate the server
1189 // is likely to accept, based on the criteria supplied in the
1190 // CertificateRequest message.
1191 if (client_cert
.get()) {
1192 const std::vector
<std::string
>& cert_authorities
=
1193 response_
.cert_request_info
->cert_authorities
;
1195 bool cert_still_valid
= cert_authorities
.empty() ||
1196 client_cert
->IsIssuedByEncoded(cert_authorities
);
1197 if (!cert_still_valid
)
1201 // TODO(davidben): Add a unit test which covers this path; we need to be
1202 // able to send a legitimate certificate and also bypass/clear the
1203 // SSL session cache.
1204 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1205 &proxy_ssl_config_
: &server_ssl_config_
;
1206 ssl_config
->send_client_cert
= true;
1207 ssl_config
->client_cert
= client_cert
;
1208 next_state_
= STATE_CREATE_STREAM
;
1209 // Reset the other member variables.
1210 // Note: this is necessary only with SSL renegotiation.
1211 ResetStateForRestart();
1215 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1216 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1217 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1219 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1220 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1222 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1224 ResetConnectionAndRequestForResend();
1228 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1229 if (server_ssl_config_
.send_client_cert
&&
1230 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1231 session_
->ssl_client_auth_cache()->Remove(
1232 HostPortPair::FromURL(request_
->url
));
1236 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1237 // being used, as all of the errors are handled as if they were generated
1238 // by the endpoint host, request_->url, rather than considering if they were
1239 // generated by the SSL proxy. http://crbug.com/69329
1240 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1242 HandleClientAuthError(error
);
1244 bool should_fallback
= false;
1245 uint16 version_max
= server_ssl_config_
.version_max
;
1248 case ERR_CONNECTION_CLOSED
:
1249 case ERR_SSL_PROTOCOL_ERROR
:
1250 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1251 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1252 version_max
> server_ssl_config_
.version_min
) {
1253 // This could be a TLS-intolerant server or a server that chose a
1254 // cipher suite defined only for higher protocol versions (such as
1255 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1256 // back to the next lower version and retry.
1257 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1258 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1259 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1260 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1261 // version_max should match the maximum protocol version supported
1262 // by the SSLClientSocket class.
1265 // Fallback to the lower SSL version.
1266 // While SSL 3.0 fallback should be eliminated because of security
1267 // reasons, there is a high risk of breaking the servers if this is
1269 should_fallback
= true;
1272 case ERR_CONNECTION_RESET
:
1273 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1274 version_max
> server_ssl_config_
.version_min
) {
1275 // Some network devices that inspect application-layer packets seem to
1276 // inject TCP reset packets to break the connections when they see TLS
1277 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1279 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1280 // 1.2. We don't lose much in this fallback because the explicit IV for
1281 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1282 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1285 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1286 // to trigger a version fallback in general, especially the TLS 1.0 ->
1287 // SSL 3.0 fallback, which would drop TLS extensions.
1289 should_fallback
= true;
1292 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1293 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1294 version_max
> server_ssl_config_
.version_min
) {
1295 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1296 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1297 // crbug.com/260358. In order to make the fallback as minimal as
1298 // possible, this fallback is only triggered for >= TLS 1.1.
1300 should_fallback
= true;
1303 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1304 // The server told us that we should not have fallen back. A buggy server
1305 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1306 // connection. |fallback_error_code_| is initialised to
1307 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1308 error
= fallback_error_code_
;
1312 if (should_fallback
) {
1314 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1315 base::Bind(&NetLogSSLVersionFallbackCallback
,
1316 &request_
->url
, error
, server_ssl_config_
.version_max
,
1318 fallback_error_code_
= error
;
1319 server_ssl_config_
.version_max
= version_max
;
1320 server_ssl_config_
.version_fallback
= true;
1321 ResetConnectionAndRequestForResend();
1328 // This method determines whether it is safe to resend the request after an
1329 // IO error. It can only be called in response to request header or body
1330 // write errors or response header read errors. It should not be used in
1331 // other cases, such as a Connect error.
1332 int HttpNetworkTransaction::HandleIOError(int error
) {
1333 // Because the peer may request renegotiation with client authentication at
1334 // any time, check and handle client authentication errors.
1335 HandleClientAuthError(error
);
1338 // If we try to reuse a connection that the server is in the process of
1339 // closing, we may end up successfully writing out our request (or a
1340 // portion of our request) only to find a connection error when we try to
1341 // read from (or finish writing to) the socket.
1342 case ERR_CONNECTION_RESET
:
1343 case ERR_CONNECTION_CLOSED
:
1344 case ERR_CONNECTION_ABORTED
:
1345 // There can be a race between the socket pool checking checking whether a
1346 // socket is still connected, receiving the FIN, and sending/reading data
1347 // on a reused socket. If we receive the FIN between the connectedness
1348 // check and writing/reading from the socket, we may first learn the socket
1349 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1350 // likely happen when trying to retrieve its IP address.
1351 // See http://crbug.com/105824 for more details.
1352 case ERR_SOCKET_NOT_CONNECTED
:
1353 // If a socket is closed on its initial request, HttpStreamParser returns
1354 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1355 // preconnected but failed to be used before the server timed it out.
1356 case ERR_EMPTY_RESPONSE
:
1357 if (ShouldResendRequest()) {
1358 net_log_
.AddEventWithNetErrorCode(
1359 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1360 ResetConnectionAndRequestForResend();
1364 case ERR_SPDY_PING_FAILED
:
1365 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1366 case ERR_QUIC_HANDSHAKE_FAILED
:
1367 net_log_
.AddEventWithNetErrorCode(
1368 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1369 ResetConnectionAndRequestForResend();
1376 void HttpNetworkTransaction::ResetStateForRestart() {
1377 ResetStateForAuthRestart();
1379 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1383 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1384 send_start_time_
= base::TimeTicks();
1385 send_end_time_
= base::TimeTicks();
1387 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1390 headers_valid_
= false;
1391 request_headers_
.Clear();
1392 response_
= HttpResponseInfo();
1393 establishing_tunnel_
= false;
1396 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1397 return response_
.headers
.get();
1400 bool HttpNetworkTransaction::ShouldResendRequest() const {
1401 bool connection_is_proven
= stream_
->IsConnectionReused();
1402 bool has_received_headers
= GetResponseHeaders() != NULL
;
1404 // NOTE: we resend a request only if we reused a keep-alive connection.
1405 // This automatically prevents an infinite resend loop because we'll run
1406 // out of the cached keep-alive connections eventually.
1407 if (connection_is_proven
&& !has_received_headers
)
1412 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1413 if (stream_
.get()) {
1414 stream_
->Close(true);
1418 // We need to clear request_headers_ because it contains the real request
1419 // headers, but we may need to resend the CONNECT request first to recreate
1421 request_headers_
.Clear();
1422 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1425 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1426 return UsingHttpProxyWithoutTunnel();
1429 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1430 return !(request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
);
1433 int HttpNetworkTransaction::HandleAuthChallenge() {
1434 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
1435 DCHECK(headers
.get());
1437 int status
= headers
->response_code();
1438 if (status
!= HTTP_UNAUTHORIZED
&&
1439 status
!= HTTP_PROXY_AUTHENTICATION_REQUIRED
)
1441 HttpAuth::Target target
= status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
?
1442 HttpAuth::AUTH_PROXY
: HttpAuth::AUTH_SERVER
;
1443 if (target
== HttpAuth::AUTH_PROXY
&& proxy_info_
.is_direct())
1444 return ERR_UNEXPECTED_PROXY_AUTH
;
1446 // This case can trigger when an HTTPS server responds with a "Proxy
1447 // authentication required" status code through a non-authenticating
1449 if (!auth_controllers_
[target
].get())
1450 return ERR_UNEXPECTED_PROXY_AUTH
;
1452 int rv
= auth_controllers_
[target
]->HandleAuthChallenge(
1453 headers
, (request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
) != 0, false,
1455 if (auth_controllers_
[target
]->HaveAuthHandler())
1456 pending_auth_target_
= target
;
1458 scoped_refptr
<AuthChallengeInfo
> auth_info
=
1459 auth_controllers_
[target
]->auth_info();
1460 if (auth_info
.get())
1461 response_
.auth_challenge
= auth_info
;
1466 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target
) const {
1467 return auth_controllers_
[target
].get() &&
1468 auth_controllers_
[target
]->HaveAuth();
1471 GURL
HttpNetworkTransaction::AuthURL(HttpAuth::Target target
) const {
1473 case HttpAuth::AUTH_PROXY
: {
1474 if (!proxy_info_
.proxy_server().is_valid() ||
1475 proxy_info_
.proxy_server().is_direct()) {
1476 return GURL(); // There is no proxy server.
1478 const char* scheme
= proxy_info_
.is_https() ? "https://" : "http://";
1479 return GURL(scheme
+
1480 proxy_info_
.proxy_server().host_port_pair().ToString());
1482 case HttpAuth::AUTH_SERVER
:
1483 if (ForWebSocketHandshake()) {
1484 const GURL
& url
= request_
->url
;
1485 url::Replacements
<char> ws_to_http
;
1486 if (url
.SchemeIs("ws")) {
1487 ws_to_http
.SetScheme("http", url::Component(0, 4));
1489 DCHECK(url
.SchemeIs("wss"));
1490 ws_to_http
.SetScheme("https", url::Component(0, 5));
1492 return url
.ReplaceComponents(ws_to_http
);
1494 return request_
->url
;
1500 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1501 return websocket_handshake_stream_base_create_helper_
&&
1502 request_
->url
.SchemeIsWSOrWSS();
1505 #define STATE_CASE(s) \
1507 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1510 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1511 std::string description
;
1513 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM
);
1514 STATE_CASE(STATE_CREATE_STREAM
);
1515 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1516 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1517 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1518 STATE_CASE(STATE_BUILD_REQUEST
);
1519 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1520 STATE_CASE(STATE_SEND_REQUEST
);
1521 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1522 STATE_CASE(STATE_READ_HEADERS
);
1523 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1524 STATE_CASE(STATE_READ_BODY
);
1525 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1526 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1527 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1528 STATE_CASE(STATE_NONE
);
1530 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,