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