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 NetLogCaptureMode
/* capture_mode */) {
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(
112 NetLogCaptureMode
/* capture_mode */) {
113 base::DictionaryValue
* dict
= new base::DictionaryValue();
114 dict
->SetString("host_and_port", GetHostAndPort(*url
));
115 dict
->SetInteger("net_error", net_error
);
121 //-----------------------------------------------------------------------------
123 HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority
,
124 HttpNetworkSession
* session
)
125 : pending_auth_target_(HttpAuth::AUTH_NONE
),
126 io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete
,
127 base::Unretained(this))),
131 headers_valid_(false),
132 fallback_error_code_(ERR_SSL_INAPPROPRIATE_FALLBACK
),
135 total_received_bytes_(0),
136 next_state_(STATE_NONE
),
137 establishing_tunnel_(false),
138 websocket_handshake_stream_base_create_helper_(NULL
) {
139 session
->ssl_config_service()->GetSSLConfig(&server_ssl_config_
);
140 session
->GetNextProtos(&server_ssl_config_
.next_protos
);
141 proxy_ssl_config_
= server_ssl_config_
;
144 HttpNetworkTransaction::~HttpNetworkTransaction() {
146 HttpResponseHeaders
* headers
= GetResponseHeaders();
147 // TODO(mbelshe): The stream_ should be able to compute whether or not the
148 // stream should be kept alive. No reason to compute here
150 bool try_to_keep_alive
=
151 next_state_
== STATE_NONE
&&
152 stream_
->CanFindEndOfResponse() &&
153 (!headers
|| headers
->IsKeepAlive());
154 if (!try_to_keep_alive
) {
155 stream_
->Close(true /* not reusable */);
157 if (stream_
->IsResponseBodyComplete()) {
158 // If the response body is complete, we can just reuse the socket.
159 stream_
->Close(false /* reusable */);
160 } else if (stream_
->IsSpdyHttpStream()) {
161 // Doesn't really matter for SpdyHttpStream. Just close it.
162 stream_
->Close(true /* not reusable */);
164 // Otherwise, we try to drain the response body.
165 HttpStream
* stream
= stream_
.release();
166 stream
->Drain(session_
);
171 if (request_
&& request_
->upload_data_stream
)
172 request_
->upload_data_stream
->Reset(); // Invalidate pending callbacks.
175 int HttpNetworkTransaction::Start(const HttpRequestInfo
* request_info
,
176 const CompletionCallback
& callback
,
177 const BoundNetLog
& net_log
) {
179 request_
= request_info
;
181 if (request_
->load_flags
& LOAD_DISABLE_CERT_REVOCATION_CHECKING
) {
182 server_ssl_config_
.rev_checking_enabled
= false;
183 proxy_ssl_config_
.rev_checking_enabled
= false;
186 if (request_
->load_flags
& LOAD_PREFETCH
)
187 response_
.unused_since_prefetch
= true;
189 // Channel ID is disabled if privacy mode is enabled for this request.
190 if (request_
->privacy_mode
== PRIVACY_MODE_ENABLED
)
191 server_ssl_config_
.channel_id_enabled
= false;
193 if (server_ssl_config_
.fastradio_padding_enabled
) {
194 server_ssl_config_
.fastradio_padding_eligible
=
195 session_
->ssl_config_service()->SupportsFastradioPadding(
199 next_state_
= STATE_NOTIFY_BEFORE_CREATE_STREAM
;
201 if (rv
== ERR_IO_PENDING
)
202 callback_
= callback
;
206 int HttpNetworkTransaction::RestartIgnoringLastError(
207 const CompletionCallback
& callback
) {
208 DCHECK(!stream_
.get());
209 DCHECK(!stream_request_
.get());
210 DCHECK_EQ(STATE_NONE
, next_state_
);
212 next_state_
= STATE_CREATE_STREAM
;
215 if (rv
== ERR_IO_PENDING
)
216 callback_
= callback
;
220 int HttpNetworkTransaction::RestartWithCertificate(
221 X509Certificate
* client_cert
, const CompletionCallback
& callback
) {
222 // In HandleCertificateRequest(), we always tear down existing stream
223 // requests to force a new connection. So we shouldn't have one here.
224 DCHECK(!stream_request_
.get());
225 DCHECK(!stream_
.get());
226 DCHECK_EQ(STATE_NONE
, next_state_
);
228 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
229 &proxy_ssl_config_
: &server_ssl_config_
;
230 ssl_config
->send_client_cert
= true;
231 ssl_config
->client_cert
= client_cert
;
232 session_
->ssl_client_auth_cache()->Add(
233 response_
.cert_request_info
->host_and_port
, client_cert
);
234 // Reset the other member variables.
235 // Note: this is necessary only with SSL renegotiation.
236 ResetStateForRestart();
237 next_state_
= STATE_CREATE_STREAM
;
239 if (rv
== ERR_IO_PENDING
)
240 callback_
= callback
;
244 int HttpNetworkTransaction::RestartWithAuth(
245 const AuthCredentials
& credentials
, const CompletionCallback
& callback
) {
246 HttpAuth::Target target
= pending_auth_target_
;
247 if (target
== HttpAuth::AUTH_NONE
) {
249 return ERR_UNEXPECTED
;
251 pending_auth_target_
= HttpAuth::AUTH_NONE
;
253 auth_controllers_
[target
]->ResetAuth(credentials
);
255 DCHECK(callback_
.is_null());
258 if (target
== HttpAuth::AUTH_PROXY
&& establishing_tunnel_
) {
259 // In this case, we've gathered credentials for use with proxy
260 // authentication of a tunnel.
261 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
262 DCHECK(stream_request_
!= NULL
);
263 auth_controllers_
[target
] = NULL
;
264 ResetStateForRestart();
265 rv
= stream_request_
->RestartTunnelWithProxyAuth(credentials
);
267 // In this case, we've gathered credentials for the server or the proxy
268 // but it is not during the tunneling phase.
269 DCHECK(stream_request_
== NULL
);
270 PrepareForAuthRestart(target
);
274 if (rv
== ERR_IO_PENDING
)
275 callback_
= callback
;
279 void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target
) {
280 DCHECK(HaveAuth(target
));
281 DCHECK(!stream_request_
.get());
283 bool keep_alive
= false;
284 // Even if the server says the connection is keep-alive, we have to be
285 // able to find the end of each response in order to reuse the connection.
286 if (GetResponseHeaders()->IsKeepAlive() &&
287 stream_
->CanFindEndOfResponse()) {
288 // If the response body hasn't been completely read, we need to drain
290 if (!stream_
->IsResponseBodyComplete()) {
291 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
292 read_buf_
= new IOBuffer(kDrainBodyBufferSize
); // A bit bucket.
293 read_buf_len_
= kDrainBodyBufferSize
;
299 // We don't need to drain the response body, so we act as if we had drained
300 // the response body.
301 DidDrainBodyForAuthRestart(keep_alive
);
304 void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive
) {
305 DCHECK(!stream_request_
.get());
308 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
309 HttpStream
* new_stream
= NULL
;
310 if (keep_alive
&& stream_
->IsConnectionReusable()) {
311 // We should call connection_->set_idle_time(), but this doesn't occur
312 // often enough to be worth the trouble.
313 stream_
->SetConnectionReused();
314 new_stream
= stream_
->RenewStreamForAuth();
318 // Close the stream and mark it as not_reusable. Even in the
319 // keep_alive case, we've determined that the stream_ is not
320 // reusable if new_stream is NULL.
321 stream_
->Close(true);
322 next_state_
= STATE_CREATE_STREAM
;
324 // Renewed streams shouldn't carry over received bytes.
325 DCHECK_EQ(0, new_stream
->GetTotalReceivedBytes());
326 next_state_
= STATE_INIT_STREAM
;
328 stream_
.reset(new_stream
);
331 // Reset the other member variables.
332 ResetStateForAuthRestart();
335 bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
336 return pending_auth_target_
!= HttpAuth::AUTH_NONE
&&
337 HaveAuth(pending_auth_target_
);
340 int HttpNetworkTransaction::Read(IOBuffer
* buf
, int buf_len
,
341 const CompletionCallback
& callback
) {
343 DCHECK_LT(0, buf_len
);
345 State next_state
= STATE_NONE
;
347 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
348 if (headers_valid_
&& headers
.get() && stream_request_
.get()) {
349 // We're trying to read the body of the response but we're still trying
350 // to establish an SSL tunnel through an HTTP proxy. We can't read these
351 // bytes when establishing a tunnel because they might be controlled by
352 // an active network attacker. We don't worry about this for HTTP
353 // because an active network attacker can already control HTTP sessions.
354 // We reach this case when the user cancels a 407 proxy auth prompt. We
355 // also don't worry about this for an HTTPS Proxy, because the
356 // communication with the proxy is secure.
357 // See http://crbug.com/8473.
358 DCHECK(proxy_info_
.is_http() || proxy_info_
.is_https());
359 DCHECK_EQ(headers
->response_code(), HTTP_PROXY_AUTHENTICATION_REQUIRED
);
360 LOG(WARNING
) << "Blocked proxy response with status "
361 << headers
->response_code() << " to CONNECT request for "
362 << GetHostAndPort(request_
->url
) << ".";
363 return ERR_TUNNEL_CONNECTION_FAILED
;
366 // Are we using SPDY or HTTP?
367 next_state
= STATE_READ_BODY
;
370 read_buf_len_
= buf_len
;
372 next_state_
= next_state
;
374 if (rv
== ERR_IO_PENDING
)
375 callback_
= callback
;
379 void HttpNetworkTransaction::StopCaching() {}
381 bool HttpNetworkTransaction::GetFullRequestHeaders(
382 HttpRequestHeaders
* headers
) const {
383 // TODO(ttuttle): Make sure we've populated request_headers_.
384 *headers
= request_headers_
;
388 int64
HttpNetworkTransaction::GetTotalReceivedBytes() const {
389 int64 total_received_bytes
= total_received_bytes_
;
391 total_received_bytes
+= stream_
->GetTotalReceivedBytes();
392 return total_received_bytes
;
395 void HttpNetworkTransaction::DoneReading() {}
397 const HttpResponseInfo
* HttpNetworkTransaction::GetResponseInfo() const {
398 return ((headers_valid_
&& response_
.headers
.get()) ||
399 response_
.ssl_info
.cert
.get() || response_
.cert_request_info
.get())
404 LoadState
HttpNetworkTransaction::GetLoadState() const {
405 // TODO(wtc): Define a new LoadState value for the
406 // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
407 switch (next_state_
) {
408 case STATE_CREATE_STREAM
:
409 return LOAD_STATE_WAITING_FOR_DELEGATE
;
410 case STATE_CREATE_STREAM_COMPLETE
:
411 return stream_request_
->GetLoadState();
412 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
413 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
414 case STATE_SEND_REQUEST_COMPLETE
:
415 return LOAD_STATE_SENDING_REQUEST
;
416 case STATE_READ_HEADERS_COMPLETE
:
417 return LOAD_STATE_WAITING_FOR_RESPONSE
;
418 case STATE_READ_BODY_COMPLETE
:
419 return LOAD_STATE_READING_RESPONSE
;
421 return LOAD_STATE_IDLE
;
425 UploadProgress
HttpNetworkTransaction::GetUploadProgress() const {
427 return UploadProgress();
429 return stream_
->GetUploadProgress();
432 void HttpNetworkTransaction::SetQuicServerInfo(
433 QuicServerInfo
* quic_server_info
) {}
435 bool HttpNetworkTransaction::GetLoadTimingInfo(
436 LoadTimingInfo
* load_timing_info
) const {
437 if (!stream_
|| !stream_
->GetLoadTimingInfo(load_timing_info
))
440 load_timing_info
->proxy_resolve_start
=
441 proxy_info_
.proxy_resolve_start_time();
442 load_timing_info
->proxy_resolve_end
= proxy_info_
.proxy_resolve_end_time();
443 load_timing_info
->send_start
= send_start_time_
;
444 load_timing_info
->send_end
= send_end_time_
;
448 void HttpNetworkTransaction::SetPriority(RequestPriority priority
) {
449 priority_
= priority
;
451 stream_request_
->SetPriority(priority
);
453 stream_
->SetPriority(priority
);
456 void HttpNetworkTransaction::SetWebSocketHandshakeStreamCreateHelper(
457 WebSocketHandshakeStreamBase::CreateHelper
* create_helper
) {
458 websocket_handshake_stream_base_create_helper_
= create_helper
;
461 void HttpNetworkTransaction::SetBeforeNetworkStartCallback(
462 const BeforeNetworkStartCallback
& callback
) {
463 before_network_start_callback_
= callback
;
466 void HttpNetworkTransaction::SetBeforeProxyHeadersSentCallback(
467 const BeforeProxyHeadersSentCallback
& callback
) {
468 before_proxy_headers_sent_callback_
= callback
;
471 int HttpNetworkTransaction::ResumeNetworkStart() {
472 DCHECK_EQ(next_state_
, STATE_CREATE_STREAM
);
476 void HttpNetworkTransaction::OnStreamReady(const SSLConfig
& used_ssl_config
,
477 const ProxyInfo
& used_proxy_info
,
478 HttpStream
* stream
) {
479 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
480 DCHECK(stream_request_
.get());
483 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
484 stream_
.reset(stream
);
485 server_ssl_config_
= used_ssl_config
;
486 proxy_info_
= used_proxy_info
;
487 response_
.was_npn_negotiated
= stream_request_
->was_npn_negotiated();
488 response_
.npn_negotiated_protocol
= SSLClientSocket::NextProtoToString(
489 stream_request_
->protocol_negotiated());
490 response_
.was_fetched_via_spdy
= stream_request_
->using_spdy();
491 response_
.was_fetched_via_proxy
= !proxy_info_
.is_direct();
492 if (response_
.was_fetched_via_proxy
&& !proxy_info_
.is_empty())
493 response_
.proxy_server
= proxy_info_
.proxy_server().host_port_pair();
497 void HttpNetworkTransaction::OnWebSocketHandshakeStreamReady(
498 const SSLConfig
& used_ssl_config
,
499 const ProxyInfo
& used_proxy_info
,
500 WebSocketHandshakeStreamBase
* stream
) {
501 OnStreamReady(used_ssl_config
, used_proxy_info
, stream
);
504 void HttpNetworkTransaction::OnStreamFailed(int result
,
505 const SSLConfig
& used_ssl_config
) {
506 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
507 DCHECK_NE(OK
, result
);
508 DCHECK(stream_request_
.get());
509 DCHECK(!stream_
.get());
510 server_ssl_config_
= used_ssl_config
;
512 OnIOComplete(result
);
515 void HttpNetworkTransaction::OnCertificateError(
517 const SSLConfig
& used_ssl_config
,
518 const SSLInfo
& ssl_info
) {
519 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
520 DCHECK_NE(OK
, result
);
521 DCHECK(stream_request_
.get());
522 DCHECK(!stream_
.get());
524 response_
.ssl_info
= ssl_info
;
525 server_ssl_config_
= used_ssl_config
;
527 // TODO(mbelshe): For now, we're going to pass the error through, and that
528 // will close the stream_request in all cases. This means that we're always
529 // going to restart an entire STATE_CREATE_STREAM, even if the connection is
530 // good and the user chooses to ignore the error. This is not ideal, but not
531 // the end of the world either.
533 OnIOComplete(result
);
536 void HttpNetworkTransaction::OnNeedsProxyAuth(
537 const HttpResponseInfo
& proxy_response
,
538 const SSLConfig
& used_ssl_config
,
539 const ProxyInfo
& used_proxy_info
,
540 HttpAuthController
* auth_controller
) {
541 DCHECK(stream_request_
.get());
542 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
544 establishing_tunnel_
= true;
545 response_
.headers
= proxy_response
.headers
;
546 response_
.auth_challenge
= proxy_response
.auth_challenge
;
547 headers_valid_
= true;
548 server_ssl_config_
= used_ssl_config
;
549 proxy_info_
= used_proxy_info
;
551 auth_controllers_
[HttpAuth::AUTH_PROXY
] = auth_controller
;
552 pending_auth_target_
= HttpAuth::AUTH_PROXY
;
557 void HttpNetworkTransaction::OnNeedsClientAuth(
558 const SSLConfig
& used_ssl_config
,
559 SSLCertRequestInfo
* cert_info
) {
560 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
562 server_ssl_config_
= used_ssl_config
;
563 response_
.cert_request_info
= cert_info
;
564 OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
567 void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
568 const HttpResponseInfo
& response_info
,
569 const SSLConfig
& used_ssl_config
,
570 const ProxyInfo
& used_proxy_info
,
571 HttpStream
* stream
) {
572 DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE
, next_state_
);
574 headers_valid_
= true;
575 response_
= response_info
;
576 server_ssl_config_
= used_ssl_config
;
577 proxy_info_
= used_proxy_info
;
579 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
580 stream_
.reset(stream
);
581 stream_request_
.reset(); // we're done with the stream request
582 OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE
);
585 bool HttpNetworkTransaction::IsSecureRequest() const {
586 return request_
->url
.SchemeIsSecure();
589 bool HttpNetworkTransaction::UsingHttpProxyWithoutTunnel() const {
590 return (proxy_info_
.is_http() || proxy_info_
.is_https() ||
591 proxy_info_
.is_quic()) &&
592 !(request_
->url
.SchemeIs("https") || request_
->url
.SchemeIsWSOrWSS());
595 void HttpNetworkTransaction::DoCallback(int rv
) {
596 DCHECK_NE(rv
, ERR_IO_PENDING
);
597 DCHECK(!callback_
.is_null());
599 // Since Run may result in Read being called, clear user_callback_ up front.
600 CompletionCallback c
= callback_
;
605 void HttpNetworkTransaction::OnIOComplete(int result
) {
606 int rv
= DoLoop(result
);
607 if (rv
!= ERR_IO_PENDING
)
611 int HttpNetworkTransaction::DoLoop(int result
) {
612 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
613 tracked_objects::ScopedTracker
tracking_profile(
614 FROM_HERE_WITH_EXPLICIT_FUNCTION(
615 "424359 HttpNetworkTransaction::DoLoop"));
617 DCHECK(next_state_
!= STATE_NONE
);
621 State state
= next_state_
;
622 next_state_
= STATE_NONE
;
624 case STATE_NOTIFY_BEFORE_CREATE_STREAM
:
626 rv
= DoNotifyBeforeCreateStream();
628 case STATE_CREATE_STREAM
:
630 rv
= DoCreateStream();
632 case STATE_CREATE_STREAM_COMPLETE
:
633 rv
= DoCreateStreamComplete(rv
);
635 case STATE_INIT_STREAM
:
639 case STATE_INIT_STREAM_COMPLETE
:
640 rv
= DoInitStreamComplete(rv
);
642 case STATE_GENERATE_PROXY_AUTH_TOKEN
:
644 rv
= DoGenerateProxyAuthToken();
646 case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
:
647 rv
= DoGenerateProxyAuthTokenComplete(rv
);
649 case STATE_GENERATE_SERVER_AUTH_TOKEN
:
651 rv
= DoGenerateServerAuthToken();
653 case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
:
654 rv
= DoGenerateServerAuthTokenComplete(rv
);
656 case STATE_INIT_REQUEST_BODY
:
658 rv
= DoInitRequestBody();
660 case STATE_INIT_REQUEST_BODY_COMPLETE
:
661 rv
= DoInitRequestBodyComplete(rv
);
663 case STATE_BUILD_REQUEST
:
665 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
);
666 rv
= DoBuildRequest();
668 case STATE_BUILD_REQUEST_COMPLETE
:
669 rv
= DoBuildRequestComplete(rv
);
671 case STATE_SEND_REQUEST
:
673 rv
= DoSendRequest();
675 case STATE_SEND_REQUEST_COMPLETE
:
676 rv
= DoSendRequestComplete(rv
);
677 net_log_
.EndEventWithNetErrorCode(
678 NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST
, rv
);
680 case STATE_READ_HEADERS
:
682 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
);
683 rv
= DoReadHeaders();
685 case STATE_READ_HEADERS_COMPLETE
:
686 rv
= DoReadHeadersComplete(rv
);
687 net_log_
.EndEventWithNetErrorCode(
688 NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS
, rv
);
690 case STATE_READ_BODY
:
692 net_log_
.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
);
695 case STATE_READ_BODY_COMPLETE
:
696 rv
= DoReadBodyComplete(rv
);
697 net_log_
.EndEventWithNetErrorCode(
698 NetLog::TYPE_HTTP_TRANSACTION_READ_BODY
, rv
);
700 case STATE_DRAIN_BODY_FOR_AUTH_RESTART
:
703 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
);
704 rv
= DoDrainBodyForAuthRestart();
706 case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
:
707 rv
= DoDrainBodyForAuthRestartComplete(rv
);
708 net_log_
.EndEventWithNetErrorCode(
709 NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART
, rv
);
712 NOTREACHED() << "bad state";
716 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
721 int HttpNetworkTransaction::DoNotifyBeforeCreateStream() {
722 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
723 tracked_objects::ScopedTracker
tracking_profile(
724 FROM_HERE_WITH_EXPLICIT_FUNCTION(
725 "424359 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 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
738 tracked_objects::ScopedTracker
tracking_profile(
739 FROM_HERE_WITH_EXPLICIT_FUNCTION(
740 "424359 HttpNetworkTransaction::DoCreateStream"));
742 next_state_
= STATE_CREATE_STREAM_COMPLETE
;
743 if (ForWebSocketHandshake()) {
744 stream_request_
.reset(
745 session_
->http_stream_factory_for_websocket()
746 ->RequestWebSocketHandshakeStream(
752 websocket_handshake_stream_base_create_helper_
,
755 stream_request_
.reset(
756 session_
->http_stream_factory()->RequestStream(
764 DCHECK(stream_request_
.get());
765 return ERR_IO_PENDING
;
768 int HttpNetworkTransaction::DoCreateStreamComplete(int result
) {
769 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
770 tracked_objects::ScopedTracker
tracking_profile(
771 FROM_HERE_WITH_EXPLICIT_FUNCTION(
772 "424359 HttpNetworkTransaction::DoCreateStreamComplete"));
775 next_state_
= STATE_INIT_STREAM
;
776 DCHECK(stream_
.get());
777 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
778 result
= HandleCertificateRequest(result
);
779 } else if (result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
780 // Return OK and let the caller read the proxy's error page
781 next_state_
= STATE_NONE
;
783 } else if (result
== ERR_HTTP_1_1_REQUIRED
||
784 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
785 return HandleHttp11Required(result
);
788 // Handle possible handshake errors that may have occurred if the stream
789 // used SSL for one or more of the layers.
790 result
= HandleSSLHandshakeError(result
);
792 // At this point we are done with the stream_request_.
793 stream_request_
.reset();
797 int HttpNetworkTransaction::DoInitStream() {
798 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
799 tracked_objects::ScopedTracker
tracking_profile(
800 FROM_HERE_WITH_EXPLICIT_FUNCTION(
801 "424359 HttpNetworkTransaction::DoInitStream"));
803 DCHECK(stream_
.get());
804 next_state_
= STATE_INIT_STREAM_COMPLETE
;
805 return stream_
->InitializeStream(request_
, priority_
, net_log_
, io_callback_
);
808 int HttpNetworkTransaction::DoInitStreamComplete(int result
) {
809 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
810 tracked_objects::ScopedTracker
tracking_profile(
811 FROM_HERE_WITH_EXPLICIT_FUNCTION(
812 "424359 HttpNetworkTransaction::DoInitStreamComplete"));
815 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN
;
818 result
= HandleIOError(result
);
820 // The stream initialization failed, so this stream will never be useful.
822 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
829 int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
830 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
831 tracked_objects::ScopedTracker
tracking_profile(
832 FROM_HERE_WITH_EXPLICIT_FUNCTION(
833 "424359 HttpNetworkTransaction::DoGenerateProxyAuthToken"));
835 next_state_
= STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE
;
836 if (!ShouldApplyProxyAuth())
838 HttpAuth::Target target
= HttpAuth::AUTH_PROXY
;
839 if (!auth_controllers_
[target
].get())
840 auth_controllers_
[target
] =
841 new HttpAuthController(target
,
843 session_
->http_auth_cache(),
844 session_
->http_auth_handler_factory());
845 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
850 int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv
) {
851 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
852 tracked_objects::ScopedTracker
tracking_profile(
853 FROM_HERE_WITH_EXPLICIT_FUNCTION(
854 "424359 HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete"));
856 DCHECK_NE(ERR_IO_PENDING
, rv
);
858 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN
;
862 int HttpNetworkTransaction::DoGenerateServerAuthToken() {
863 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
864 tracked_objects::ScopedTracker
tracking_profile(
865 FROM_HERE_WITH_EXPLICIT_FUNCTION(
866 "424359 HttpNetworkTransaction::DoGenerateServerAuthToken"));
868 next_state_
= STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE
;
869 HttpAuth::Target target
= HttpAuth::AUTH_SERVER
;
870 if (!auth_controllers_
[target
].get()) {
871 auth_controllers_
[target
] =
872 new HttpAuthController(target
,
874 session_
->http_auth_cache(),
875 session_
->http_auth_handler_factory());
876 if (request_
->load_flags
& LOAD_DO_NOT_USE_EMBEDDED_IDENTITY
)
877 auth_controllers_
[target
]->DisableEmbeddedIdentity();
879 if (!ShouldApplyServerAuth())
881 return auth_controllers_
[target
]->MaybeGenerateAuthToken(request_
,
886 int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv
) {
887 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
888 tracked_objects::ScopedTracker
tracking_profile(
889 FROM_HERE_WITH_EXPLICIT_FUNCTION(
890 "424359 HttpNetworkTransaction::DoGenerateServerAuthTokenComplete"));
892 DCHECK_NE(ERR_IO_PENDING
, rv
);
894 next_state_
= STATE_INIT_REQUEST_BODY
;
898 void HttpNetworkTransaction::BuildRequestHeaders(
899 bool using_http_proxy_without_tunnel
) {
900 request_headers_
.SetHeader(HttpRequestHeaders::kHost
,
901 GetHostAndOptionalPort(request_
->url
));
903 // For compat with HTTP/1.0 servers and proxies:
904 if (using_http_proxy_without_tunnel
) {
905 request_headers_
.SetHeader(HttpRequestHeaders::kProxyConnection
,
908 request_headers_
.SetHeader(HttpRequestHeaders::kConnection
, "keep-alive");
911 // Add a content length header?
912 if (request_
->upload_data_stream
) {
913 if (request_
->upload_data_stream
->is_chunked()) {
914 request_headers_
.SetHeader(
915 HttpRequestHeaders::kTransferEncoding
, "chunked");
917 request_headers_
.SetHeader(
918 HttpRequestHeaders::kContentLength
,
919 base::Uint64ToString(request_
->upload_data_stream
->size()));
921 } else if (request_
->method
== "POST" || request_
->method
== "PUT" ||
922 request_
->method
== "HEAD") {
923 // An empty POST/PUT request still needs a content length. As for HEAD,
924 // IE and Safari also add a content length header. Presumably it is to
925 // support sending a HEAD request to an URL that only expects to be sent a
926 // POST or some other method that normally would have a message body.
927 request_headers_
.SetHeader(HttpRequestHeaders::kContentLength
, "0");
930 // Honor load flags that impact proxy caches.
931 if (request_
->load_flags
& LOAD_BYPASS_CACHE
) {
932 request_headers_
.SetHeader(HttpRequestHeaders::kPragma
, "no-cache");
933 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "no-cache");
934 } else if (request_
->load_flags
& LOAD_VALIDATE_CACHE
) {
935 request_headers_
.SetHeader(HttpRequestHeaders::kCacheControl
, "max-age=0");
938 if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY
))
939 auth_controllers_
[HttpAuth::AUTH_PROXY
]->AddAuthorizationHeader(
941 if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER
))
942 auth_controllers_
[HttpAuth::AUTH_SERVER
]->AddAuthorizationHeader(
945 request_headers_
.MergeFrom(request_
->extra_headers
);
947 if (using_http_proxy_without_tunnel
&&
948 !before_proxy_headers_sent_callback_
.is_null())
949 before_proxy_headers_sent_callback_
.Run(proxy_info_
, &request_headers_
);
951 response_
.did_use_http_auth
=
952 request_headers_
.HasHeader(HttpRequestHeaders::kAuthorization
) ||
953 request_headers_
.HasHeader(HttpRequestHeaders::kProxyAuthorization
);
956 int HttpNetworkTransaction::DoInitRequestBody() {
957 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
958 tracked_objects::ScopedTracker
tracking_profile(
959 FROM_HERE_WITH_EXPLICIT_FUNCTION(
960 "424359 HttpNetworkTransaction::DoInitRequestBody"));
962 next_state_
= STATE_INIT_REQUEST_BODY_COMPLETE
;
964 if (request_
->upload_data_stream
)
965 rv
= request_
->upload_data_stream
->Init(io_callback_
);
969 int HttpNetworkTransaction::DoInitRequestBodyComplete(int result
) {
970 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
971 tracked_objects::ScopedTracker
tracking_profile(
972 FROM_HERE_WITH_EXPLICIT_FUNCTION(
973 "424359 HttpNetworkTransaction::DoInitRequestBodyComplete"));
976 next_state_
= STATE_BUILD_REQUEST
;
980 int HttpNetworkTransaction::DoBuildRequest() {
981 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
982 tracked_objects::ScopedTracker
tracking_profile(
983 FROM_HERE_WITH_EXPLICIT_FUNCTION(
984 "424359 HttpNetworkTransaction::DoBuildRequest"));
986 next_state_
= STATE_BUILD_REQUEST_COMPLETE
;
987 headers_valid_
= false;
989 // This is constructed lazily (instead of within our Start method), so that
990 // we have proxy info available.
991 if (request_headers_
.IsEmpty()) {
992 bool using_http_proxy_without_tunnel
= UsingHttpProxyWithoutTunnel();
993 BuildRequestHeaders(using_http_proxy_without_tunnel
);
999 int HttpNetworkTransaction::DoBuildRequestComplete(int result
) {
1000 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1001 tracked_objects::ScopedTracker
tracking_profile(
1002 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1003 "424359 HttpNetworkTransaction::DoBuildRequestComplete"));
1006 next_state_
= STATE_SEND_REQUEST
;
1010 int HttpNetworkTransaction::DoSendRequest() {
1011 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1012 tracked_objects::ScopedTracker
tracking_profile(
1013 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1014 "424359 HttpNetworkTransaction::DoSendRequest"));
1016 send_start_time_
= base::TimeTicks::Now();
1017 next_state_
= STATE_SEND_REQUEST_COMPLETE
;
1019 return stream_
->SendRequest(request_headers_
, &response_
, io_callback_
);
1022 int HttpNetworkTransaction::DoSendRequestComplete(int result
) {
1023 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1024 tracked_objects::ScopedTracker
tracking_profile(
1025 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1026 "424359 HttpNetworkTransaction::DoSendRequestComplete"));
1028 send_end_time_
= base::TimeTicks::Now();
1030 return HandleIOError(result
);
1031 response_
.network_accessed
= true;
1032 next_state_
= STATE_READ_HEADERS
;
1036 int HttpNetworkTransaction::DoReadHeaders() {
1037 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1038 tracked_objects::ScopedTracker
tracking_profile(
1039 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1040 "424359 HttpNetworkTransaction::DoReadHeaders"));
1042 next_state_
= STATE_READ_HEADERS_COMPLETE
;
1043 return stream_
->ReadResponseHeaders(io_callback_
);
1046 int HttpNetworkTransaction::DoReadHeadersComplete(int result
) {
1047 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1048 tracked_objects::ScopedTracker
tracking_profile(
1049 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1050 "424359 HttpNetworkTransaction::DoReadHeadersComplete"));
1052 // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
1053 // due to SSL renegotiation.
1054 if (IsCertificateError(result
)) {
1055 // We don't handle a certificate error during SSL renegotiation, so we
1056 // have to return an error that's not in the certificate error range
1058 LOG(ERROR
) << "Got a server certificate with error " << result
1059 << " during SSL renegotiation";
1060 result
= ERR_CERT_ERROR_IN_SSL_RENEGOTIATION
;
1061 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
1062 // TODO(wtc): Need a test case for this code path!
1063 DCHECK(stream_
.get());
1064 DCHECK(IsSecureRequest());
1065 response_
.cert_request_info
= new SSLCertRequestInfo
;
1066 stream_
->GetSSLCertRequestInfo(response_
.cert_request_info
.get());
1067 result
= HandleCertificateRequest(result
);
1072 if (result
== ERR_HTTP_1_1_REQUIRED
||
1073 result
== ERR_PROXY_HTTP_1_1_REQUIRED
) {
1074 return HandleHttp11Required(result
);
1077 // ERR_CONNECTION_CLOSED is treated differently at this point; if partial
1078 // response headers were received, we do the best we can to make sense of it
1079 // and send it back up the stack.
1081 // TODO(davidben): Consider moving this to HttpBasicStream, It's a little
1082 // bizarre for SPDY. Assuming this logic is useful at all.
1083 // TODO(davidben): Bubble the error code up so we do not cache?
1084 if (result
== ERR_CONNECTION_CLOSED
&& response_
.headers
.get())
1088 return HandleIOError(result
);
1090 DCHECK(response_
.headers
.get());
1092 // On a 408 response from the server ("Request Timeout") on a stale socket,
1093 // retry the request.
1094 // Headers can be NULL because of http://crbug.com/384554.
1095 if (response_
.headers
.get() && response_
.headers
->response_code() == 408 &&
1096 stream_
->IsConnectionReused()) {
1097 net_log_
.AddEventWithNetErrorCode(
1098 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
,
1099 response_
.headers
->response_code());
1100 // This will close the socket - it would be weird to try and reuse it, even
1101 // if the server doesn't actually close it.
1102 ResetConnectionAndRequestForResend();
1106 // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
1107 if (request_
->load_flags
& LOAD_MAIN_FRAME
) {
1108 const int response_code
= response_
.headers
->response_code();
1109 UMA_HISTOGRAM_ENUMERATION(
1110 "Net.HttpResponseCode_Nxx_MainFrame", response_code
/100, 10);
1114 NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS
,
1115 base::Bind(&HttpResponseHeaders::NetLogCallback
, response_
.headers
));
1117 if (response_
.headers
->GetParsedHttpVersion() < HttpVersion(1, 0)) {
1118 // HTTP/0.9 doesn't support the PUT method, so lack of response headers
1119 // indicates a buggy server. See:
1120 // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
1121 if (request_
->method
== "PUT")
1122 return ERR_METHOD_NOT_SUPPORTED
;
1125 // Check for an intermediate 100 Continue response. An origin server is
1126 // allowed to send this response even if we didn't ask for it, so we just
1127 // need to skip over it.
1128 // We treat any other 1xx in this same way (although in practice getting
1129 // a 1xx that isn't a 100 is rare).
1130 // Unless this is a WebSocket request, in which case we pass it on up.
1131 if (response_
.headers
->response_code() / 100 == 1 &&
1132 !ForWebSocketHandshake()) {
1133 response_
.headers
= new HttpResponseHeaders(std::string());
1134 next_state_
= STATE_READ_HEADERS
;
1138 ProcessAlternateProtocol(session_
, *response_
.headers
.get(),
1139 HostPortPair::FromURL(request_
->url
));
1141 int rv
= HandleAuthChallenge();
1145 if (IsSecureRequest())
1146 stream_
->GetSSLInfo(&response_
.ssl_info
);
1148 headers_valid_
= true;
1152 int HttpNetworkTransaction::DoReadBody() {
1153 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1154 tracked_objects::ScopedTracker
tracking_profile(
1155 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1156 "424359 HttpNetworkTransaction::DoReadBody"));
1158 DCHECK(read_buf_
.get());
1159 DCHECK_GT(read_buf_len_
, 0);
1160 DCHECK(stream_
!= NULL
);
1162 next_state_
= STATE_READ_BODY_COMPLETE
;
1163 return stream_
->ReadResponseBody(
1164 read_buf_
.get(), read_buf_len_
, io_callback_
);
1167 int HttpNetworkTransaction::DoReadBodyComplete(int result
) {
1168 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1169 tracked_objects::ScopedTracker
tracking_profile(
1170 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1171 "424359 HttpNetworkTransaction::DoReadBodyComplete"));
1173 // We are done with the Read call.
1176 DCHECK_NE(ERR_IO_PENDING
, result
);
1180 bool keep_alive
= false;
1181 if (stream_
->IsResponseBodyComplete()) {
1182 // Note: Just because IsResponseBodyComplete is true, we're not
1183 // necessarily "done". We're only "done" when it is the last
1184 // read on this HttpNetworkTransaction, which will be signified
1185 // by a zero-length read.
1186 // TODO(mbelshe): The keepalive property is really a property of
1187 // the stream. No need to compute it here just to pass back
1188 // to the stream's Close function.
1189 // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1191 if (stream_
->CanFindEndOfResponse()) {
1192 HttpResponseHeaders
* headers
= GetResponseHeaders();
1194 keep_alive
= headers
->IsKeepAlive();
1198 // Clean up connection if we are done.
1200 stream_
->Close(!keep_alive
);
1201 // Note: we don't reset the stream here. We've closed it, but we still
1202 // need it around so that callers can call methods such as
1203 // GetUploadProgress() and have them be meaningful.
1204 // TODO(mbelshe): This means we closed the stream here, and we close it
1205 // again in ~HttpNetworkTransaction. Clean that up.
1207 // The next Read call will return 0 (EOF).
1210 // Clear these to avoid leaving around old state.
1217 int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1218 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1219 tracked_objects::ScopedTracker
tracking_profile(
1220 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1221 "424359 HttpNetworkTransaction::DoDrainBodyForAuthRestart"));
1223 // This method differs from DoReadBody only in the next_state_. So we just
1224 // call DoReadBody and override the next_state_. Perhaps there is a more
1225 // elegant way for these two methods to share code.
1226 int rv
= DoReadBody();
1227 DCHECK(next_state_
== STATE_READ_BODY_COMPLETE
);
1228 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
;
1232 // TODO(wtc): This method and the DoReadBodyComplete method are almost
1233 // the same. Figure out a good way for these two methods to share code.
1234 int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result
) {
1235 // TODO(pkasting): Remove ScopedTracker below once crbug.com/424359 is fixed.
1236 tracked_objects::ScopedTracker
tracking_profile(
1237 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1238 "424359 HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete"));
1240 // keep_alive defaults to true because the very reason we're draining the
1241 // response body is to reuse the connection for auth restart.
1242 bool done
= false, keep_alive
= true;
1244 // Error or closed connection while reading the socket.
1247 } else if (stream_
->IsResponseBodyComplete()) {
1252 DidDrainBodyForAuthRestart(keep_alive
);
1255 next_state_
= STATE_DRAIN_BODY_FOR_AUTH_RESTART
;
1261 int HttpNetworkTransaction::HandleCertificateRequest(int error
) {
1262 // There are two paths through which the server can request a certificate
1263 // from us. The first is during the initial handshake, the second is
1264 // during SSL renegotiation.
1266 // In both cases, we want to close the connection before proceeding.
1267 // We do this for two reasons:
1268 // First, we don't want to keep the connection to the server hung for a
1269 // long time while the user selects a certificate.
1270 // Second, even if we did keep the connection open, NSS has a bug where
1271 // restarting the handshake for ClientAuth is currently broken.
1272 DCHECK_EQ(error
, ERR_SSL_CLIENT_AUTH_CERT_NEEDED
);
1274 if (stream_
.get()) {
1275 // Since we already have a stream, we're being called as part of SSL
1277 DCHECK(!stream_request_
.get());
1278 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1279 stream_
->Close(true);
1283 // The server is asking for a client certificate during the initial
1285 stream_request_
.reset();
1287 // If the user selected one of the certificates in client_certs or declined
1288 // to provide one for this server before, use the past decision
1290 scoped_refptr
<X509Certificate
> client_cert
;
1291 bool found_cached_cert
= session_
->ssl_client_auth_cache()->Lookup(
1292 response_
.cert_request_info
->host_and_port
, &client_cert
);
1293 if (!found_cached_cert
)
1296 // Check that the certificate selected is still a certificate the server
1297 // is likely to accept, based on the criteria supplied in the
1298 // CertificateRequest message.
1299 if (client_cert
.get()) {
1300 const std::vector
<std::string
>& cert_authorities
=
1301 response_
.cert_request_info
->cert_authorities
;
1303 bool cert_still_valid
= cert_authorities
.empty() ||
1304 client_cert
->IsIssuedByEncoded(cert_authorities
);
1305 if (!cert_still_valid
)
1309 // TODO(davidben): Add a unit test which covers this path; we need to be
1310 // able to send a legitimate certificate and also bypass/clear the
1311 // SSL session cache.
1312 SSLConfig
* ssl_config
= response_
.cert_request_info
->is_proxy
?
1313 &proxy_ssl_config_
: &server_ssl_config_
;
1314 ssl_config
->send_client_cert
= true;
1315 ssl_config
->client_cert
= client_cert
;
1316 next_state_
= STATE_CREATE_STREAM
;
1317 // Reset the other member variables.
1318 // Note: this is necessary only with SSL renegotiation.
1319 ResetStateForRestart();
1323 int HttpNetworkTransaction::HandleHttp11Required(int error
) {
1324 DCHECK(error
== ERR_HTTP_1_1_REQUIRED
||
1325 error
== ERR_PROXY_HTTP_1_1_REQUIRED
);
1327 if (error
== ERR_HTTP_1_1_REQUIRED
) {
1328 HttpServerProperties::ForceHTTP11(&server_ssl_config_
);
1330 HttpServerProperties::ForceHTTP11(&proxy_ssl_config_
);
1332 ResetConnectionAndRequestForResend();
1336 void HttpNetworkTransaction::HandleClientAuthError(int error
) {
1337 if (server_ssl_config_
.send_client_cert
&&
1338 (error
== ERR_SSL_PROTOCOL_ERROR
|| IsClientCertificateError(error
))) {
1339 session_
->ssl_client_auth_cache()->Remove(
1340 HostPortPair::FromURL(request_
->url
));
1344 // TODO(rch): This does not correctly handle errors when an SSL proxy is
1345 // being used, as all of the errors are handled as if they were generated
1346 // by the endpoint host, request_->url, rather than considering if they were
1347 // generated by the SSL proxy. http://crbug.com/69329
1348 int HttpNetworkTransaction::HandleSSLHandshakeError(int error
) {
1350 HandleClientAuthError(error
);
1352 // Accept deprecated cipher suites, but only on a fallback. This makes UMA
1353 // reflect servers require a deprecated cipher rather than merely prefer
1354 // it. This, however, has no security benefit until the ciphers are actually
1356 if (!server_ssl_config_
.enable_deprecated_cipher_suites
&&
1357 (error
== ERR_SSL_VERSION_OR_CIPHER_MISMATCH
||
1358 error
== ERR_CONNECTION_CLOSED
|| error
== ERR_CONNECTION_RESET
)) {
1360 NetLog::TYPE_SSL_CIPHER_FALLBACK
,
1361 base::Bind(&NetLogSSLCipherFallbackCallback
, &request_
->url
, error
));
1362 server_ssl_config_
.enable_deprecated_cipher_suites
= true;
1363 ResetConnectionAndRequestForResend();
1367 bool should_fallback
= false;
1368 uint16 version_max
= server_ssl_config_
.version_max
;
1371 case ERR_CONNECTION_CLOSED
:
1372 case ERR_SSL_PROTOCOL_ERROR
:
1373 case ERR_SSL_VERSION_OR_CIPHER_MISMATCH
:
1374 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1
&&
1375 version_max
> server_ssl_config_
.version_min
) {
1376 // This could be a TLS-intolerant server or a server that chose a
1377 // cipher suite defined only for higher protocol versions (such as
1378 // an SSL 3.0 server that chose a TLS-only cipher suite). Fall
1379 // back to the next lower version and retry.
1380 // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1381 // specifying TLS 1.1 in version_max will result in a TLS 1.0
1382 // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1383 // repeat the TLS 1.0 handshake. To avoid this problem, the default
1384 // version_max should match the maximum protocol version supported
1385 // by the SSLClientSocket class.
1388 // Fallback to the lower SSL version.
1389 // While SSL 3.0 fallback should be eliminated because of security
1390 // reasons, there is a high risk of breaking the servers if this is
1392 should_fallback
= true;
1395 case ERR_CONNECTION_RESET
:
1396 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1397 version_max
> server_ssl_config_
.version_min
) {
1398 // Some network devices that inspect application-layer packets seem to
1399 // inject TCP reset packets to break the connections when they see TLS
1400 // 1.1 in ClientHello or ServerHello. See http://crbug.com/130293.
1402 // Only allow ERR_CONNECTION_RESET to trigger a fallback from TLS 1.1 or
1403 // 1.2. We don't lose much in this fallback because the explicit IV for
1404 // CBC mode in TLS 1.1 is approximated by record splitting in TLS
1405 // 1.0. The fallback will be more painful for TLS 1.2 when we have GCM
1408 // ERR_CONNECTION_RESET is a common network error, so we don't want it
1409 // to trigger a version fallback in general, especially the TLS 1.0 ->
1410 // SSL 3.0 fallback, which would drop TLS extensions.
1412 should_fallback
= true;
1415 case ERR_SSL_BAD_RECORD_MAC_ALERT
:
1416 if (version_max
>= SSL_PROTOCOL_VERSION_TLS1_1
&&
1417 version_max
> server_ssl_config_
.version_min
) {
1418 // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1419 // 1.2 ClientHello, but then return a bad_record_mac alert. See
1420 // crbug.com/260358. In order to make the fallback as minimal as
1421 // possible, this fallback is only triggered for >= TLS 1.1.
1423 should_fallback
= true;
1426 case ERR_SSL_INAPPROPRIATE_FALLBACK
:
1427 // The server told us that we should not have fallen back. A buggy server
1428 // could trigger ERR_SSL_INAPPROPRIATE_FALLBACK with the initial
1429 // connection. |fallback_error_code_| is initialised to
1430 // ERR_SSL_INAPPROPRIATE_FALLBACK to catch this case.
1431 error
= fallback_error_code_
;
1435 if (should_fallback
) {
1437 NetLog::TYPE_SSL_VERSION_FALLBACK
,
1438 base::Bind(&NetLogSSLVersionFallbackCallback
,
1439 &request_
->url
, error
, server_ssl_config_
.version_max
,
1441 fallback_error_code_
= error
;
1442 server_ssl_config_
.version_max
= version_max
;
1443 server_ssl_config_
.version_fallback
= true;
1444 ResetConnectionAndRequestForResend();
1451 // This method determines whether it is safe to resend the request after an
1452 // IO error. It can only be called in response to request header or body
1453 // write errors or response header read errors. It should not be used in
1454 // other cases, such as a Connect error.
1455 int HttpNetworkTransaction::HandleIOError(int error
) {
1456 // Because the peer may request renegotiation with client authentication at
1457 // any time, check and handle client authentication errors.
1458 HandleClientAuthError(error
);
1461 // If we try to reuse a connection that the server is in the process of
1462 // closing, we may end up successfully writing out our request (or a
1463 // portion of our request) only to find a connection error when we try to
1464 // read from (or finish writing to) the socket.
1465 case ERR_CONNECTION_RESET
:
1466 case ERR_CONNECTION_CLOSED
:
1467 case ERR_CONNECTION_ABORTED
:
1468 // There can be a race between the socket pool checking checking whether a
1469 // socket is still connected, receiving the FIN, and sending/reading data
1470 // on a reused socket. If we receive the FIN between the connectedness
1471 // check and writing/reading from the socket, we may first learn the socket
1472 // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED. This will most
1473 // likely happen when trying to retrieve its IP address.
1474 // See http://crbug.com/105824 for more details.
1475 case ERR_SOCKET_NOT_CONNECTED
:
1476 // If a socket is closed on its initial request, HttpStreamParser returns
1477 // ERR_EMPTY_RESPONSE. This may still be close/reuse race if the socket was
1478 // preconnected but failed to be used before the server timed it out.
1479 case ERR_EMPTY_RESPONSE
:
1480 if (ShouldResendRequest()) {
1481 net_log_
.AddEventWithNetErrorCode(
1482 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1483 ResetConnectionAndRequestForResend();
1487 case ERR_SPDY_PING_FAILED
:
1488 case ERR_SPDY_SERVER_REFUSED_STREAM
:
1489 case ERR_QUIC_HANDSHAKE_FAILED
:
1490 net_log_
.AddEventWithNetErrorCode(
1491 NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR
, error
);
1492 ResetConnectionAndRequestForResend();
1499 void HttpNetworkTransaction::ResetStateForRestart() {
1500 ResetStateForAuthRestart();
1502 total_received_bytes_
+= stream_
->GetTotalReceivedBytes();
1506 void HttpNetworkTransaction::ResetStateForAuthRestart() {
1507 send_start_time_
= base::TimeTicks();
1508 send_end_time_
= base::TimeTicks();
1510 pending_auth_target_
= HttpAuth::AUTH_NONE
;
1513 headers_valid_
= false;
1514 request_headers_
.Clear();
1515 response_
= HttpResponseInfo();
1516 establishing_tunnel_
= false;
1519 HttpResponseHeaders
* HttpNetworkTransaction::GetResponseHeaders() const {
1520 return response_
.headers
.get();
1523 bool HttpNetworkTransaction::ShouldResendRequest() const {
1524 bool connection_is_proven
= stream_
->IsConnectionReused();
1525 bool has_received_headers
= GetResponseHeaders() != NULL
;
1527 // NOTE: we resend a request only if we reused a keep-alive connection.
1528 // This automatically prevents an infinite resend loop because we'll run
1529 // out of the cached keep-alive connections eventually.
1530 if (connection_is_proven
&& !has_received_headers
)
1535 void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1536 if (stream_
.get()) {
1537 stream_
->Close(true);
1541 // We need to clear request_headers_ because it contains the real request
1542 // headers, but we may need to resend the CONNECT request first to recreate
1544 request_headers_
.Clear();
1545 next_state_
= STATE_CREATE_STREAM
; // Resend the request.
1548 bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1549 return UsingHttpProxyWithoutTunnel();
1552 bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1553 return !(request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
);
1556 int HttpNetworkTransaction::HandleAuthChallenge() {
1557 scoped_refptr
<HttpResponseHeaders
> headers(GetResponseHeaders());
1558 DCHECK(headers
.get());
1560 int status
= headers
->response_code();
1561 if (status
!= HTTP_UNAUTHORIZED
&&
1562 status
!= HTTP_PROXY_AUTHENTICATION_REQUIRED
)
1564 HttpAuth::Target target
= status
== HTTP_PROXY_AUTHENTICATION_REQUIRED
?
1565 HttpAuth::AUTH_PROXY
: HttpAuth::AUTH_SERVER
;
1566 if (target
== HttpAuth::AUTH_PROXY
&& proxy_info_
.is_direct())
1567 return ERR_UNEXPECTED_PROXY_AUTH
;
1569 // This case can trigger when an HTTPS server responds with a "Proxy
1570 // authentication required" status code through a non-authenticating
1572 if (!auth_controllers_
[target
].get())
1573 return ERR_UNEXPECTED_PROXY_AUTH
;
1575 int rv
= auth_controllers_
[target
]->HandleAuthChallenge(
1576 headers
, (request_
->load_flags
& LOAD_DO_NOT_SEND_AUTH_DATA
) != 0, false,
1578 if (auth_controllers_
[target
]->HaveAuthHandler())
1579 pending_auth_target_
= target
;
1581 scoped_refptr
<AuthChallengeInfo
> auth_info
=
1582 auth_controllers_
[target
]->auth_info();
1583 if (auth_info
.get())
1584 response_
.auth_challenge
= auth_info
;
1589 bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target
) const {
1590 return auth_controllers_
[target
].get() &&
1591 auth_controllers_
[target
]->HaveAuth();
1594 GURL
HttpNetworkTransaction::AuthURL(HttpAuth::Target target
) const {
1596 case HttpAuth::AUTH_PROXY
: {
1597 if (!proxy_info_
.proxy_server().is_valid() ||
1598 proxy_info_
.proxy_server().is_direct()) {
1599 return GURL(); // There is no proxy server.
1601 const char* scheme
= proxy_info_
.is_https() ? "https://" : "http://";
1602 return GURL(scheme
+
1603 proxy_info_
.proxy_server().host_port_pair().ToString());
1605 case HttpAuth::AUTH_SERVER
:
1606 if (ForWebSocketHandshake()) {
1607 const GURL
& url
= request_
->url
;
1608 url::Replacements
<char> ws_to_http
;
1609 if (url
.SchemeIs("ws")) {
1610 ws_to_http
.SetScheme("http", url::Component(0, 4));
1612 DCHECK(url
.SchemeIs("wss"));
1613 ws_to_http
.SetScheme("https", url::Component(0, 5));
1615 return url
.ReplaceComponents(ws_to_http
);
1617 return request_
->url
;
1623 bool HttpNetworkTransaction::ForWebSocketHandshake() const {
1624 return websocket_handshake_stream_base_create_helper_
&&
1625 request_
->url
.SchemeIsWSOrWSS();
1628 #define STATE_CASE(s) \
1630 description = base::StringPrintf("%s (0x%08X)", #s, s); \
1633 std::string
HttpNetworkTransaction::DescribeState(State state
) {
1634 std::string description
;
1636 STATE_CASE(STATE_NOTIFY_BEFORE_CREATE_STREAM
);
1637 STATE_CASE(STATE_CREATE_STREAM
);
1638 STATE_CASE(STATE_CREATE_STREAM_COMPLETE
);
1639 STATE_CASE(STATE_INIT_REQUEST_BODY
);
1640 STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE
);
1641 STATE_CASE(STATE_BUILD_REQUEST
);
1642 STATE_CASE(STATE_BUILD_REQUEST_COMPLETE
);
1643 STATE_CASE(STATE_SEND_REQUEST
);
1644 STATE_CASE(STATE_SEND_REQUEST_COMPLETE
);
1645 STATE_CASE(STATE_READ_HEADERS
);
1646 STATE_CASE(STATE_READ_HEADERS_COMPLETE
);
1647 STATE_CASE(STATE_READ_BODY
);
1648 STATE_CASE(STATE_READ_BODY_COMPLETE
);
1649 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART
);
1650 STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE
);
1651 STATE_CASE(STATE_NONE
);
1653 description
= base::StringPrintf("Unknown state 0x%08X (%u)", state
,