http_network_transaction.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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.
4
5#include "net/http/http_network_transaction.h"
6
7#include <set>
8#include <vector>
9
10#include "base/bind.h"
11#include "base/bind_helpers.h"
12#include "base/compiler_specific.h"
13#include "base/format_macros.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/metrics/field_trial.h"
16#include "base/metrics/histogram.h"
17#include "base/metrics/stats_counters.h"
18#include "base/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/values.h"
23#include "build/build_config.h"
24#include "net/base/auth.h"
25#include "net/base/host_port_pair.h"
26#include "net/base/io_buffer.h"
27#include "net/base/load_flags.h"
28#include "net/base/load_timing_info.h"
29#include "net/base/net_errors.h"
30#include "net/base/net_util.h"
31#include "net/base/upload_data_stream.h"
32#include "net/http/http_auth.h"
33#include "net/http/http_auth_handler.h"
34#include "net/http/http_auth_handler_factory.h"
35#include "net/http/http_basic_stream.h"
36#include "net/http/http_chunked_decoder.h"
37#include "net/http/http_network_session.h"
38#include "net/http/http_proxy_client_socket.h"
39#include "net/http/http_proxy_client_socket_pool.h"
40#include "net/http/http_request_headers.h"
41#include "net/http/http_request_info.h"
42#include "net/http/http_response_headers.h"
43#include "net/http/http_response_info.h"
44#include "net/http/http_server_properties.h"
45#include "net/http/http_status_code.h"
46#include "net/http/http_stream_base.h"
47#include "net/http/http_stream_factory.h"
48#include "net/http/http_util.h"
49#include "net/http/transport_security_state.h"
50#include "net/http/url_security_manager.h"
51#include "net/socket/client_socket_factory.h"
52#include "net/socket/socks_client_socket_pool.h"
53#include "net/socket/ssl_client_socket.h"
54#include "net/socket/ssl_client_socket_pool.h"
55#include "net/socket/transport_client_socket_pool.h"
56#include "net/spdy/spdy_http_stream.h"
57#include "net/spdy/spdy_session.h"
58#include "net/spdy/spdy_session_pool.h"
59#include "net/ssl/ssl_cert_request_info.h"
60#include "net/ssl/ssl_connection_status_flags.h"
61#include "url/gurl.h"
62
63using base::Time;
64
65namespace net {
66
67namespace {
68
69void ProcessAlternateProtocol(
70    HttpStreamFactory* factory,
71    const base::WeakPtr<HttpServerProperties>& http_server_properties,
72    const HttpResponseHeaders& headers,
73    const HostPortPair& http_host_port_pair) {
74  std::string alternate_protocol_str;
75
76  if (!headers.EnumerateHeader(NULL, kAlternateProtocolHeader,
77                               &alternate_protocol_str)) {
78    // Header is not present.
79    return;
80  }
81
82  factory->ProcessAlternateProtocol(http_server_properties,
83                                    alternate_protocol_str,
84                                    http_host_port_pair);
85}
86
87// Returns true if |error| is a client certificate authentication error.
88bool IsClientCertificateError(int error) {
89  switch (error) {
90    case ERR_BAD_SSL_CLIENT_AUTH_CERT:
91    case ERR_SSL_CLIENT_AUTH_PRIVATE_KEY_ACCESS_DENIED:
92    case ERR_SSL_CLIENT_AUTH_CERT_NO_PRIVATE_KEY:
93    case ERR_SSL_CLIENT_AUTH_SIGNATURE_FAILED:
94      return true;
95    default:
96      return false;
97  }
98}
99
100base::Value* NetLogSSLVersionFallbackCallback(
101    const GURL* url,
102    int net_error,
103    uint16 version_before,
104    uint16 version_after,
105    NetLog::LogLevel /* log_level */) {
106  base::DictionaryValue* dict = new base::DictionaryValue();
107  dict->SetString("host_and_port", GetHostAndPort(*url));
108  dict->SetInteger("net_error", net_error);
109  dict->SetInteger("version_before", version_before);
110  dict->SetInteger("version_after", version_after);
111  return dict;
112}
113
114}  // namespace
115
116//-----------------------------------------------------------------------------
117
118HttpNetworkTransaction::HttpNetworkTransaction(RequestPriority priority,
119                                               HttpNetworkSession* session)
120    : pending_auth_target_(HttpAuth::AUTH_NONE),
121      io_callback_(base::Bind(&HttpNetworkTransaction::OnIOComplete,
122                              base::Unretained(this))),
123      session_(session),
124      request_(NULL),
125      priority_(priority),
126      headers_valid_(false),
127      logged_response_time_(false),
128      request_headers_(),
129      read_buf_len_(0),
130      next_state_(STATE_NONE),
131      establishing_tunnel_(false) {
132  session->ssl_config_service()->GetSSLConfig(&server_ssl_config_);
133  if (session->http_stream_factory()->has_next_protos()) {
134    server_ssl_config_.next_protos =
135        session->http_stream_factory()->next_protos();
136  }
137  proxy_ssl_config_ = server_ssl_config_;
138}
139
140HttpNetworkTransaction::~HttpNetworkTransaction() {
141  if (stream_.get()) {
142    HttpResponseHeaders* headers = GetResponseHeaders();
143    // TODO(mbelshe): The stream_ should be able to compute whether or not the
144    //                stream should be kept alive.  No reason to compute here
145    //                and pass it in.
146    bool try_to_keep_alive =
147        next_state_ == STATE_NONE &&
148        stream_->CanFindEndOfResponse() &&
149        (!headers || headers->IsKeepAlive());
150    if (!try_to_keep_alive) {
151      stream_->Close(true /* not reusable */);
152    } else {
153      if (stream_->IsResponseBodyComplete()) {
154        // If the response body is complete, we can just reuse the socket.
155        stream_->Close(false /* reusable */);
156      } else if (stream_->IsSpdyHttpStream()) {
157        // Doesn't really matter for SpdyHttpStream. Just close it.
158        stream_->Close(true /* not reusable */);
159      } else {
160        // Otherwise, we try to drain the response body.
161        HttpStreamBase* stream = stream_.release();
162        stream->Drain(session_.get());
163      }
164    }
165  }
166
167  if (request_ && request_->upload_data_stream)
168    request_->upload_data_stream->Reset();  // Invalidate pending callbacks.
169}
170
171int HttpNetworkTransaction::Start(const HttpRequestInfo* request_info,
172                                  const CompletionCallback& callback,
173                                  const BoundNetLog& net_log) {
174  SIMPLE_STATS_COUNTER("HttpNetworkTransaction.Count");
175
176  net_log_ = net_log;
177  request_ = request_info;
178  start_time_ = base::Time::Now();
179
180  if (request_->load_flags & LOAD_DISABLE_CERT_REVOCATION_CHECKING) {
181    server_ssl_config_.rev_checking_enabled = false;
182    proxy_ssl_config_.rev_checking_enabled = false;
183  }
184
185  // Channel ID is enabled unless --disable-tls-channel-id flag is set,
186  // or if privacy mode is enabled.
187  bool channel_id_enabled = server_ssl_config_.channel_id_enabled &&
188      (request_->privacy_mode == kPrivacyModeDisabled);
189  server_ssl_config_.channel_id_enabled = channel_id_enabled;
190
191  next_state_ = STATE_CREATE_STREAM;
192  int rv = DoLoop(OK);
193  if (rv == ERR_IO_PENDING)
194    callback_ = callback;
195  return rv;
196}
197
198int HttpNetworkTransaction::RestartIgnoringLastError(
199    const CompletionCallback& callback) {
200  DCHECK(!stream_.get());
201  DCHECK(!stream_request_.get());
202  DCHECK_EQ(STATE_NONE, next_state_);
203
204  next_state_ = STATE_CREATE_STREAM;
205
206  int rv = DoLoop(OK);
207  if (rv == ERR_IO_PENDING)
208    callback_ = callback;
209  return rv;
210}
211
212int HttpNetworkTransaction::RestartWithCertificate(
213    X509Certificate* client_cert, const CompletionCallback& callback) {
214  // In HandleCertificateRequest(), we always tear down existing stream
215  // requests to force a new connection.  So we shouldn't have one here.
216  DCHECK(!stream_request_.get());
217  DCHECK(!stream_.get());
218  DCHECK_EQ(STATE_NONE, next_state_);
219
220  SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
221      &proxy_ssl_config_ : &server_ssl_config_;
222  ssl_config->send_client_cert = true;
223  ssl_config->client_cert = client_cert;
224  session_->ssl_client_auth_cache()->Add(
225      response_.cert_request_info->host_and_port, client_cert);
226  // Reset the other member variables.
227  // Note: this is necessary only with SSL renegotiation.
228  ResetStateForRestart();
229  next_state_ = STATE_CREATE_STREAM;
230  int rv = DoLoop(OK);
231  if (rv == ERR_IO_PENDING)
232    callback_ = callback;
233  return rv;
234}
235
236int HttpNetworkTransaction::RestartWithAuth(
237    const AuthCredentials& credentials, const CompletionCallback& callback) {
238  HttpAuth::Target target = pending_auth_target_;
239  if (target == HttpAuth::AUTH_NONE) {
240    NOTREACHED();
241    return ERR_UNEXPECTED;
242  }
243  pending_auth_target_ = HttpAuth::AUTH_NONE;
244
245  auth_controllers_[target]->ResetAuth(credentials);
246
247  DCHECK(callback_.is_null());
248
249  int rv = OK;
250  if (target == HttpAuth::AUTH_PROXY && establishing_tunnel_) {
251    // In this case, we've gathered credentials for use with proxy
252    // authentication of a tunnel.
253    DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
254    DCHECK(stream_request_ != NULL);
255    auth_controllers_[target] = NULL;
256    ResetStateForRestart();
257    rv = stream_request_->RestartTunnelWithProxyAuth(credentials);
258  } else {
259    // In this case, we've gathered credentials for the server or the proxy
260    // but it is not during the tunneling phase.
261    DCHECK(stream_request_ == NULL);
262    PrepareForAuthRestart(target);
263    rv = DoLoop(OK);
264  }
265
266  if (rv == ERR_IO_PENDING)
267    callback_ = callback;
268  return rv;
269}
270
271void HttpNetworkTransaction::PrepareForAuthRestart(HttpAuth::Target target) {
272  DCHECK(HaveAuth(target));
273  DCHECK(!stream_request_.get());
274
275  bool keep_alive = false;
276  // Even if the server says the connection is keep-alive, we have to be
277  // able to find the end of each response in order to reuse the connection.
278  if (GetResponseHeaders()->IsKeepAlive() &&
279      stream_->CanFindEndOfResponse()) {
280    // If the response body hasn't been completely read, we need to drain
281    // it first.
282    if (!stream_->IsResponseBodyComplete()) {
283      next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
284      read_buf_ = new IOBuffer(kDrainBodyBufferSize);  // A bit bucket.
285      read_buf_len_ = kDrainBodyBufferSize;
286      return;
287    }
288    keep_alive = true;
289  }
290
291  // We don't need to drain the response body, so we act as if we had drained
292  // the response body.
293  DidDrainBodyForAuthRestart(keep_alive);
294}
295
296void HttpNetworkTransaction::DidDrainBodyForAuthRestart(bool keep_alive) {
297  DCHECK(!stream_request_.get());
298
299  if (stream_.get()) {
300    HttpStream* new_stream = NULL;
301    if (keep_alive && stream_->IsConnectionReusable()) {
302      // We should call connection_->set_idle_time(), but this doesn't occur
303      // often enough to be worth the trouble.
304      stream_->SetConnectionReused();
305      new_stream =
306          static_cast<HttpStream*>(stream_.get())->RenewStreamForAuth();
307    }
308
309    if (!new_stream) {
310      // Close the stream and mark it as not_reusable.  Even in the
311      // keep_alive case, we've determined that the stream_ is not
312      // reusable if new_stream is NULL.
313      stream_->Close(true);
314      next_state_ = STATE_CREATE_STREAM;
315    } else {
316      next_state_ = STATE_INIT_STREAM;
317    }
318    stream_.reset(new_stream);
319  }
320
321  // Reset the other member variables.
322  ResetStateForAuthRestart();
323}
324
325bool HttpNetworkTransaction::IsReadyToRestartForAuth() {
326  return pending_auth_target_ != HttpAuth::AUTH_NONE &&
327      HaveAuth(pending_auth_target_);
328}
329
330int HttpNetworkTransaction::Read(IOBuffer* buf, int buf_len,
331                                 const CompletionCallback& callback) {
332  DCHECK(buf);
333  DCHECK_LT(0, buf_len);
334
335  State next_state = STATE_NONE;
336
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;
354  }
355
356  // Are we using SPDY or HTTP?
357  next_state = STATE_READ_BODY;
358
359  read_buf_ = buf;
360  read_buf_len_ = buf_len;
361
362  next_state_ = next_state;
363  int rv = DoLoop(OK);
364  if (rv == ERR_IO_PENDING)
365    callback_ = callback;
366  return rv;
367}
368
369bool HttpNetworkTransaction::GetFullRequestHeaders(
370    HttpRequestHeaders* headers) const {
371  // TODO(ttuttle): Make sure we've populated request_headers_.
372  *headers = request_headers_;
373  return true;
374}
375
376const HttpResponseInfo* HttpNetworkTransaction::GetResponseInfo() const {
377  return ((headers_valid_ && response_.headers.get()) ||
378          response_.ssl_info.cert.get() || response_.cert_request_info.get())
379             ? &response_
380             : NULL;
381}
382
383LoadState HttpNetworkTransaction::GetLoadState() const {
384  // TODO(wtc): Define a new LoadState value for the
385  // STATE_INIT_CONNECTION_COMPLETE state, which delays the HTTP request.
386  switch (next_state_) {
387    case STATE_CREATE_STREAM_COMPLETE:
388      return stream_request_->GetLoadState();
389    case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
390    case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
391    case STATE_SEND_REQUEST_COMPLETE:
392      return LOAD_STATE_SENDING_REQUEST;
393    case STATE_READ_HEADERS_COMPLETE:
394      return LOAD_STATE_WAITING_FOR_RESPONSE;
395    case STATE_READ_BODY_COMPLETE:
396      return LOAD_STATE_READING_RESPONSE;
397    default:
398      return LOAD_STATE_IDLE;
399  }
400}
401
402UploadProgress HttpNetworkTransaction::GetUploadProgress() const {
403  if (!stream_.get())
404    return UploadProgress();
405
406  // TODO(bashi): This cast is temporary. Remove later.
407  return static_cast<HttpStream*>(stream_.get())->GetUploadProgress();
408}
409
410bool HttpNetworkTransaction::GetLoadTimingInfo(
411    LoadTimingInfo* load_timing_info) const {
412  if (!stream_ || !stream_->GetLoadTimingInfo(load_timing_info))
413    return false;
414
415  load_timing_info->proxy_resolve_start =
416      proxy_info_.proxy_resolve_start_time();
417  load_timing_info->proxy_resolve_end = proxy_info_.proxy_resolve_end_time();
418  load_timing_info->send_start = send_start_time_;
419  load_timing_info->send_end = send_end_time_;
420  return true;
421}
422
423void HttpNetworkTransaction::SetPriority(RequestPriority priority) {
424  priority_ = priority;
425  if (stream_request_)
426    stream_request_->SetPriority(priority);
427  if (stream_)
428    stream_->SetPriority(priority);
429}
430
431void HttpNetworkTransaction::OnStreamReady(const SSLConfig& used_ssl_config,
432                                           const ProxyInfo& used_proxy_info,
433                                           HttpStreamBase* stream) {
434  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
435  DCHECK(stream_request_.get());
436
437  stream_.reset(stream);
438  server_ssl_config_ = used_ssl_config;
439  proxy_info_ = used_proxy_info;
440  response_.was_npn_negotiated = stream_request_->was_npn_negotiated();
441  response_.npn_negotiated_protocol = SSLClientSocket::NextProtoToString(
442      stream_request_->protocol_negotiated());
443  response_.was_fetched_via_spdy = stream_request_->using_spdy();
444  response_.was_fetched_via_proxy = !proxy_info_.is_direct();
445
446  OnIOComplete(OK);
447}
448
449void HttpNetworkTransaction::OnWebSocketStreamReady(
450    const SSLConfig& used_ssl_config,
451    const ProxyInfo& used_proxy_info,
452    WebSocketStreamBase* stream) {
453  NOTREACHED() << "This function should never be called.";
454}
455
456void HttpNetworkTransaction::OnStreamFailed(int result,
457                                            const SSLConfig& used_ssl_config) {
458  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
459  DCHECK_NE(OK, result);
460  DCHECK(stream_request_.get());
461  DCHECK(!stream_.get());
462  server_ssl_config_ = used_ssl_config;
463
464  OnIOComplete(result);
465}
466
467void HttpNetworkTransaction::OnCertificateError(
468    int result,
469    const SSLConfig& used_ssl_config,
470    const SSLInfo& ssl_info) {
471  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
472  DCHECK_NE(OK, result);
473  DCHECK(stream_request_.get());
474  DCHECK(!stream_.get());
475
476  response_.ssl_info = ssl_info;
477  server_ssl_config_ = used_ssl_config;
478
479  // TODO(mbelshe):  For now, we're going to pass the error through, and that
480  // will close the stream_request in all cases.  This means that we're always
481  // going to restart an entire STATE_CREATE_STREAM, even if the connection is
482  // good and the user chooses to ignore the error.  This is not ideal, but not
483  // the end of the world either.
484
485  OnIOComplete(result);
486}
487
488void HttpNetworkTransaction::OnNeedsProxyAuth(
489    const HttpResponseInfo& proxy_response,
490    const SSLConfig& used_ssl_config,
491    const ProxyInfo& used_proxy_info,
492    HttpAuthController* auth_controller) {
493  DCHECK(stream_request_.get());
494  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
495
496  establishing_tunnel_ = true;
497  response_.headers = proxy_response.headers;
498  response_.auth_challenge = proxy_response.auth_challenge;
499  headers_valid_ = true;
500  server_ssl_config_ = used_ssl_config;
501  proxy_info_ = used_proxy_info;
502
503  auth_controllers_[HttpAuth::AUTH_PROXY] = auth_controller;
504  pending_auth_target_ = HttpAuth::AUTH_PROXY;
505
506  DoCallback(OK);
507}
508
509void HttpNetworkTransaction::OnNeedsClientAuth(
510    const SSLConfig& used_ssl_config,
511    SSLCertRequestInfo* cert_info) {
512  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
513
514  server_ssl_config_ = used_ssl_config;
515  response_.cert_request_info = cert_info;
516  OnIOComplete(ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
517}
518
519void HttpNetworkTransaction::OnHttpsProxyTunnelResponse(
520    const HttpResponseInfo& response_info,
521    const SSLConfig& used_ssl_config,
522    const ProxyInfo& used_proxy_info,
523    HttpStreamBase* stream) {
524  DCHECK_EQ(STATE_CREATE_STREAM_COMPLETE, next_state_);
525
526  headers_valid_ = true;
527  response_ = response_info;
528  server_ssl_config_ = used_ssl_config;
529  proxy_info_ = used_proxy_info;
530  stream_.reset(stream);
531  stream_request_.reset();  // we're done with the stream request
532  OnIOComplete(ERR_HTTPS_PROXY_TUNNEL_RESPONSE);
533}
534
535bool HttpNetworkTransaction::is_https_request() const {
536  return request_->url.SchemeIs("https");
537}
538
539void HttpNetworkTransaction::DoCallback(int rv) {
540  DCHECK_NE(rv, ERR_IO_PENDING);
541  DCHECK(!callback_.is_null());
542
543  // Since Run may result in Read being called, clear user_callback_ up front.
544  CompletionCallback c = callback_;
545  callback_.Reset();
546  c.Run(rv);
547}
548
549void HttpNetworkTransaction::OnIOComplete(int result) {
550  int rv = DoLoop(result);
551  if (rv != ERR_IO_PENDING)
552    DoCallback(rv);
553}
554
555int HttpNetworkTransaction::DoLoop(int result) {
556  DCHECK(next_state_ != STATE_NONE);
557
558  int rv = result;
559  do {
560    State state = next_state_;
561    next_state_ = STATE_NONE;
562    switch (state) {
563      case STATE_CREATE_STREAM:
564        DCHECK_EQ(OK, rv);
565        rv = DoCreateStream();
566        break;
567      case STATE_CREATE_STREAM_COMPLETE:
568        rv = DoCreateStreamComplete(rv);
569        break;
570      case STATE_INIT_STREAM:
571        DCHECK_EQ(OK, rv);
572        rv = DoInitStream();
573        break;
574      case STATE_INIT_STREAM_COMPLETE:
575        rv = DoInitStreamComplete(rv);
576        break;
577      case STATE_GENERATE_PROXY_AUTH_TOKEN:
578        DCHECK_EQ(OK, rv);
579        rv = DoGenerateProxyAuthToken();
580        break;
581      case STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE:
582        rv = DoGenerateProxyAuthTokenComplete(rv);
583        break;
584      case STATE_GENERATE_SERVER_AUTH_TOKEN:
585        DCHECK_EQ(OK, rv);
586        rv = DoGenerateServerAuthToken();
587        break;
588      case STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE:
589        rv = DoGenerateServerAuthTokenComplete(rv);
590        break;
591      case STATE_INIT_REQUEST_BODY:
592        DCHECK_EQ(OK, rv);
593        rv = DoInitRequestBody();
594        break;
595      case STATE_INIT_REQUEST_BODY_COMPLETE:
596        rv = DoInitRequestBodyComplete(rv);
597        break;
598      case STATE_BUILD_REQUEST:
599        DCHECK_EQ(OK, rv);
600        net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST);
601        rv = DoBuildRequest();
602        break;
603      case STATE_BUILD_REQUEST_COMPLETE:
604        rv = DoBuildRequestComplete(rv);
605        break;
606      case STATE_SEND_REQUEST:
607        DCHECK_EQ(OK, rv);
608        rv = DoSendRequest();
609        break;
610      case STATE_SEND_REQUEST_COMPLETE:
611        rv = DoSendRequestComplete(rv);
612        net_log_.EndEventWithNetErrorCode(
613            NetLog::TYPE_HTTP_TRANSACTION_SEND_REQUEST, rv);
614        break;
615      case STATE_READ_HEADERS:
616        DCHECK_EQ(OK, rv);
617        net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS);
618        rv = DoReadHeaders();
619        break;
620      case STATE_READ_HEADERS_COMPLETE:
621        rv = DoReadHeadersComplete(rv);
622        net_log_.EndEventWithNetErrorCode(
623            NetLog::TYPE_HTTP_TRANSACTION_READ_HEADERS, rv);
624        break;
625      case STATE_READ_BODY:
626        DCHECK_EQ(OK, rv);
627        net_log_.BeginEvent(NetLog::TYPE_HTTP_TRANSACTION_READ_BODY);
628        rv = DoReadBody();
629        break;
630      case STATE_READ_BODY_COMPLETE:
631        rv = DoReadBodyComplete(rv);
632        net_log_.EndEventWithNetErrorCode(
633            NetLog::TYPE_HTTP_TRANSACTION_READ_BODY, rv);
634        break;
635      case STATE_DRAIN_BODY_FOR_AUTH_RESTART:
636        DCHECK_EQ(OK, rv);
637        net_log_.BeginEvent(
638            NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART);
639        rv = DoDrainBodyForAuthRestart();
640        break;
641      case STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE:
642        rv = DoDrainBodyForAuthRestartComplete(rv);
643        net_log_.EndEventWithNetErrorCode(
644            NetLog::TYPE_HTTP_TRANSACTION_DRAIN_BODY_FOR_AUTH_RESTART, rv);
645        break;
646      default:
647        NOTREACHED() << "bad state";
648        rv = ERR_FAILED;
649        break;
650    }
651  } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
652
653  return rv;
654}
655
656int HttpNetworkTransaction::DoCreateStream() {
657  next_state_ = STATE_CREATE_STREAM_COMPLETE;
658
659  stream_request_.reset(
660      session_->http_stream_factory()->RequestStream(
661          *request_,
662          priority_,
663          server_ssl_config_,
664          proxy_ssl_config_,
665          this,
666          net_log_));
667  DCHECK(stream_request_.get());
668  return ERR_IO_PENDING;
669}
670
671int HttpNetworkTransaction::DoCreateStreamComplete(int result) {
672  if (result == OK) {
673    next_state_ = STATE_INIT_STREAM;
674    DCHECK(stream_.get());
675  } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
676    result = HandleCertificateRequest(result);
677  } else if (result == ERR_HTTPS_PROXY_TUNNEL_RESPONSE) {
678    // Return OK and let the caller read the proxy's error page
679    next_state_ = STATE_NONE;
680    return OK;
681  }
682
683  // Handle possible handshake errors that may have occurred if the stream
684  // used SSL for one or more of the layers.
685  result = HandleSSLHandshakeError(result);
686
687  // At this point we are done with the stream_request_.
688  stream_request_.reset();
689  return result;
690}
691
692int HttpNetworkTransaction::DoInitStream() {
693  DCHECK(stream_.get());
694  next_state_ = STATE_INIT_STREAM_COMPLETE;
695  return stream_->InitializeStream(request_, priority_, net_log_, io_callback_);
696}
697
698int HttpNetworkTransaction::DoInitStreamComplete(int result) {
699  if (result == OK) {
700    next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN;
701  } else {
702    if (result < 0)
703      result = HandleIOError(result);
704
705    // The stream initialization failed, so this stream will never be useful.
706    stream_.reset();
707  }
708
709  return result;
710}
711
712int HttpNetworkTransaction::DoGenerateProxyAuthToken() {
713  next_state_ = STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE;
714  if (!ShouldApplyProxyAuth())
715    return OK;
716  HttpAuth::Target target = HttpAuth::AUTH_PROXY;
717  if (!auth_controllers_[target].get())
718    auth_controllers_[target] =
719        new HttpAuthController(target,
720                               AuthURL(target),
721                               session_->http_auth_cache(),
722                               session_->http_auth_handler_factory());
723  return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
724                                                           io_callback_,
725                                                           net_log_);
726}
727
728int HttpNetworkTransaction::DoGenerateProxyAuthTokenComplete(int rv) {
729  DCHECK_NE(ERR_IO_PENDING, rv);
730  if (rv == OK)
731    next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN;
732  return rv;
733}
734
735int HttpNetworkTransaction::DoGenerateServerAuthToken() {
736  next_state_ = STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE;
737  HttpAuth::Target target = HttpAuth::AUTH_SERVER;
738  if (!auth_controllers_[target].get()) {
739    auth_controllers_[target] =
740        new HttpAuthController(target,
741                               AuthURL(target),
742                               session_->http_auth_cache(),
743                               session_->http_auth_handler_factory());
744    if (request_->load_flags & LOAD_DO_NOT_USE_EMBEDDED_IDENTITY)
745      auth_controllers_[target]->DisableEmbeddedIdentity();
746  }
747  if (!ShouldApplyServerAuth())
748    return OK;
749  return auth_controllers_[target]->MaybeGenerateAuthToken(request_,
750                                                           io_callback_,
751                                                           net_log_);
752}
753
754int HttpNetworkTransaction::DoGenerateServerAuthTokenComplete(int rv) {
755  DCHECK_NE(ERR_IO_PENDING, rv);
756  if (rv == OK)
757    next_state_ = STATE_INIT_REQUEST_BODY;
758  return rv;
759}
760
761void HttpNetworkTransaction::BuildRequestHeaders(bool using_proxy) {
762  request_headers_.SetHeader(HttpRequestHeaders::kHost,
763                             GetHostAndOptionalPort(request_->url));
764
765  // For compat with HTTP/1.0 servers and proxies:
766  if (using_proxy) {
767    request_headers_.SetHeader(HttpRequestHeaders::kProxyConnection,
768                               "keep-alive");
769  } else {
770    request_headers_.SetHeader(HttpRequestHeaders::kConnection, "keep-alive");
771  }
772
773  // Add a content length header?
774  if (request_->upload_data_stream) {
775    if (request_->upload_data_stream->is_chunked()) {
776      request_headers_.SetHeader(
777          HttpRequestHeaders::kTransferEncoding, "chunked");
778    } else {
779      request_headers_.SetHeader(
780          HttpRequestHeaders::kContentLength,
781          base::Uint64ToString(request_->upload_data_stream->size()));
782    }
783  } else if (request_->method == "POST" || request_->method == "PUT" ||
784             request_->method == "HEAD") {
785    // An empty POST/PUT request still needs a content length.  As for HEAD,
786    // IE and Safari also add a content length header.  Presumably it is to
787    // support sending a HEAD request to an URL that only expects to be sent a
788    // POST or some other method that normally would have a message body.
789    request_headers_.SetHeader(HttpRequestHeaders::kContentLength, "0");
790  }
791
792  // Honor load flags that impact proxy caches.
793  if (request_->load_flags & LOAD_BYPASS_CACHE) {
794    request_headers_.SetHeader(HttpRequestHeaders::kPragma, "no-cache");
795    request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "no-cache");
796  } else if (request_->load_flags & LOAD_VALIDATE_CACHE) {
797    request_headers_.SetHeader(HttpRequestHeaders::kCacheControl, "max-age=0");
798  }
799
800  if (ShouldApplyProxyAuth() && HaveAuth(HttpAuth::AUTH_PROXY))
801    auth_controllers_[HttpAuth::AUTH_PROXY]->AddAuthorizationHeader(
802        &request_headers_);
803  if (ShouldApplyServerAuth() && HaveAuth(HttpAuth::AUTH_SERVER))
804    auth_controllers_[HttpAuth::AUTH_SERVER]->AddAuthorizationHeader(
805        &request_headers_);
806
807  request_headers_.MergeFrom(request_->extra_headers);
808  response_.did_use_http_auth =
809      request_headers_.HasHeader(HttpRequestHeaders::kAuthorization) ||
810      request_headers_.HasHeader(HttpRequestHeaders::kProxyAuthorization);
811}
812
813int HttpNetworkTransaction::DoInitRequestBody() {
814  next_state_ = STATE_INIT_REQUEST_BODY_COMPLETE;
815  int rv = OK;
816  if (request_->upload_data_stream)
817    rv = request_->upload_data_stream->Init(io_callback_);
818  return rv;
819}
820
821int HttpNetworkTransaction::DoInitRequestBodyComplete(int result) {
822  if (result == OK)
823    next_state_ = STATE_BUILD_REQUEST;
824  return result;
825}
826
827int HttpNetworkTransaction::DoBuildRequest() {
828  next_state_ = STATE_BUILD_REQUEST_COMPLETE;
829  headers_valid_ = false;
830
831  // This is constructed lazily (instead of within our Start method), so that
832  // we have proxy info available.
833  if (request_headers_.IsEmpty()) {
834    bool using_proxy = (proxy_info_.is_http() || proxy_info_.is_https()) &&
835                        !is_https_request();
836    BuildRequestHeaders(using_proxy);
837  }
838
839  return OK;
840}
841
842int HttpNetworkTransaction::DoBuildRequestComplete(int result) {
843  if (result == OK)
844    next_state_ = STATE_SEND_REQUEST;
845  return result;
846}
847
848int HttpNetworkTransaction::DoSendRequest() {
849  send_start_time_ = base::TimeTicks::Now();
850  next_state_ = STATE_SEND_REQUEST_COMPLETE;
851
852  return stream_->SendRequest(request_headers_, &response_, io_callback_);
853}
854
855int HttpNetworkTransaction::DoSendRequestComplete(int result) {
856  send_end_time_ = base::TimeTicks::Now();
857  if (result < 0)
858    return HandleIOError(result);
859  response_.network_accessed = true;
860  next_state_ = STATE_READ_HEADERS;
861  return OK;
862}
863
864int HttpNetworkTransaction::DoReadHeaders() {
865  next_state_ = STATE_READ_HEADERS_COMPLETE;
866  return stream_->ReadResponseHeaders(io_callback_);
867}
868
869int HttpNetworkTransaction::HandleConnectionClosedBeforeEndOfHeaders() {
870  if (!response_.headers.get() && !stream_->IsConnectionReused()) {
871    // The connection was closed before any data was sent. Likely an error
872    // rather than empty HTTP/0.9 response.
873    return ERR_EMPTY_RESPONSE;
874  }
875
876  return OK;
877}
878
879int HttpNetworkTransaction::DoReadHeadersComplete(int result) {
880  // We can get a certificate error or ERR_SSL_CLIENT_AUTH_CERT_NEEDED here
881  // due to SSL renegotiation.
882  if (IsCertificateError(result)) {
883    // We don't handle a certificate error during SSL renegotiation, so we
884    // have to return an error that's not in the certificate error range
885    // (-2xx).
886    LOG(ERROR) << "Got a server certificate with error " << result
887               << " during SSL renegotiation";
888    result = ERR_CERT_ERROR_IN_SSL_RENEGOTIATION;
889  } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
890    // TODO(wtc): Need a test case for this code path!
891    DCHECK(stream_.get());
892    DCHECK(is_https_request());
893    response_.cert_request_info = new SSLCertRequestInfo;
894    stream_->GetSSLCertRequestInfo(response_.cert_request_info.get());
895    result = HandleCertificateRequest(result);
896    if (result == OK)
897      return result;
898  }
899
900  if (result < 0 && result != ERR_CONNECTION_CLOSED)
901    return HandleIOError(result);
902
903  if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) {
904    ResetConnectionAndRequestForResend();
905    return OK;
906  }
907
908  // After we call RestartWithAuth a new response_time will be recorded, and
909  // we need to be cautious about incorrectly logging the duration across the
910  // authentication activity.
911  if (result == OK)
912    LogTransactionConnectedMetrics();
913
914  if (result == ERR_CONNECTION_CLOSED) {
915    // For now, if we get at least some data, we do the best we can to make
916    // sense of it and send it back up the stack.
917    int rv = HandleConnectionClosedBeforeEndOfHeaders();
918    if (rv != OK)
919      return rv;
920  }
921  DCHECK(response_.headers.get());
922
923  // Server-induced fallback is supported only if this is a PAC configured
924  // proxy. See: http://crbug.com/143712
925  if (response_.was_fetched_via_proxy && proxy_info_.did_use_pac_script() &&
926      response_.headers.get() != NULL) {
927    bool should_fallback =
928        response_.headers->HasHeaderValue("connection", "proxy-bypass");
929    // Additionally, fallback if a 500 is returned via the data reduction proxy.
930    // This is conservative, as the 500 might have been generated by the origin,
931    // and not the proxy.
932#if defined(SPDY_PROXY_AUTH_ORIGIN)
933    if (!should_fallback) {
934      should_fallback =
935          response_.headers->response_code() == HTTP_INTERNAL_SERVER_ERROR &&
936          proxy_info_.proxy_server().host_port_pair().Equals(
937              HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)));
938    }
939#endif
940    if (should_fallback) {
941      ProxyService* proxy_service = session_->proxy_service();
942      if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) {
943        // Only retry in the case of GETs. We don't want to resubmit a POST
944        // if the proxy took some action.
945        if (request_->method == "GET") {
946          ResetConnectionAndRequestForResend();
947          return OK;
948        }
949      }
950    }
951  }
952
953  // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
954  if (request_->load_flags & LOAD_MAIN_FRAME) {
955    const int response_code = response_.headers->response_code();
956    UMA_HISTOGRAM_ENUMERATION(
957        "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
958  }
959
960  net_log_.AddEvent(
961      NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
962      base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
963
964  if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
965    // HTTP/0.9 doesn't support the PUT method, so lack of response headers
966    // indicates a buggy server.  See:
967    // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
968    if (request_->method == "PUT")
969      return ERR_METHOD_NOT_SUPPORTED;
970  }
971
972  // Check for an intermediate 100 Continue response.  An origin server is
973  // allowed to send this response even if we didn't ask for it, so we just
974  // need to skip over it.
975  // We treat any other 1xx in this same way (although in practice getting
976  // a 1xx that isn't a 100 is rare).
977  if (response_.headers->response_code() / 100 == 1) {
978    response_.headers = new HttpResponseHeaders(std::string());
979    next_state_ = STATE_READ_HEADERS;
980    return OK;
981  }
982
983  HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(),
984                                       request_->url.EffectiveIntPort());
985  ProcessAlternateProtocol(session_->http_stream_factory(),
986                           session_->http_server_properties(),
987                           *response_.headers.get(),
988                           endpoint);
989
990  int rv = HandleAuthChallenge();
991  if (rv != OK)
992    return rv;
993
994  if (is_https_request())
995    stream_->GetSSLInfo(&response_.ssl_info);
996
997  headers_valid_ = true;
998  return OK;
999}
1000
1001int HttpNetworkTransaction::DoReadBody() {
1002  DCHECK(read_buf_.get());
1003  DCHECK_GT(read_buf_len_, 0);
1004  DCHECK(stream_ != NULL);
1005
1006  next_state_ = STATE_READ_BODY_COMPLETE;
1007  return stream_->ReadResponseBody(
1008      read_buf_.get(), read_buf_len_, io_callback_);
1009}
1010
1011int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1012  // We are done with the Read call.
1013  bool done = false;
1014  if (result <= 0) {
1015    DCHECK_NE(ERR_IO_PENDING, result);
1016    done = true;
1017  }
1018
1019  bool keep_alive = false;
1020  if (stream_->IsResponseBodyComplete()) {
1021    // Note: Just because IsResponseBodyComplete is true, we're not
1022    // necessarily "done".  We're only "done" when it is the last
1023    // read on this HttpNetworkTransaction, which will be signified
1024    // by a zero-length read.
1025    // TODO(mbelshe): The keepalive property is really a property of
1026    //    the stream.  No need to compute it here just to pass back
1027    //    to the stream's Close function.
1028    // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1029    // ResponseHeaders.
1030    if (stream_->CanFindEndOfResponse()) {
1031      HttpResponseHeaders* headers = GetResponseHeaders();
1032      if (headers)
1033        keep_alive = headers->IsKeepAlive();
1034    }
1035  }
1036
1037  // Clean up connection if we are done.
1038  if (done) {
1039    LogTransactionMetrics();
1040    stream_->Close(!keep_alive);
1041    // Note: we don't reset the stream here.  We've closed it, but we still
1042    // need it around so that callers can call methods such as
1043    // GetUploadProgress() and have them be meaningful.
1044    // TODO(mbelshe): This means we closed the stream here, and we close it
1045    // again in ~HttpNetworkTransaction.  Clean that up.
1046
1047    // The next Read call will return 0 (EOF).
1048  }
1049
1050  // Clear these to avoid leaving around old state.
1051  read_buf_ = NULL;
1052  read_buf_len_ = 0;
1053
1054  return result;
1055}
1056
1057int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1058  // This method differs from DoReadBody only in the next_state_.  So we just
1059  // call DoReadBody and override the next_state_.  Perhaps there is a more
1060  // elegant way for these two methods to share code.
1061  int rv = DoReadBody();
1062  DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1063  next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1064  return rv;
1065}
1066
1067// TODO(wtc): This method and the DoReadBodyComplete method are almost
1068// the same.  Figure out a good way for these two methods to share code.
1069int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
1070  // keep_alive defaults to true because the very reason we're draining the
1071  // response body is to reuse the connection for auth restart.
1072  bool done = false, keep_alive = true;
1073  if (result < 0) {
1074    // Error or closed connection while reading the socket.
1075    done = true;
1076    keep_alive = false;
1077  } else if (stream_->IsResponseBodyComplete()) {
1078    done = true;
1079  }
1080
1081  if (done) {
1082    DidDrainBodyForAuthRestart(keep_alive);
1083  } else {
1084    // Keep draining.
1085    next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1086  }
1087
1088  return OK;
1089}
1090
1091void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1092  if (logged_response_time_)
1093    return;
1094
1095  logged_response_time_ = true;
1096
1097  base::TimeDelta total_duration = response_.response_time - start_time_;
1098
1099  UMA_HISTOGRAM_CUSTOM_TIMES(
1100      "Net.Transaction_Connected",
1101      total_duration,
1102      base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1103      100);
1104
1105  bool reused_socket = stream_->IsConnectionReused();
1106  if (!reused_socket) {
1107    UMA_HISTOGRAM_CUSTOM_TIMES(
1108        "Net.Transaction_Connected_New_b",
1109        total_duration,
1110        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1111        100);
1112  }
1113
1114  // Currently, non-HIGHEST priority requests are frame or sub-frame resource
1115  // types.  This will change when we also prioritize certain subresources like
1116  // css, js, etc.
1117  if (priority_ != HIGHEST) {
1118    UMA_HISTOGRAM_CUSTOM_TIMES(
1119        "Net.Priority_High_Latency_b",
1120        total_duration,
1121        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1122        100);
1123  } else {
1124    UMA_HISTOGRAM_CUSTOM_TIMES(
1125        "Net.Priority_Low_Latency_b",
1126        total_duration,
1127        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1128        100);
1129  }
1130}
1131
1132void HttpNetworkTransaction::LogTransactionMetrics() const {
1133  base::TimeDelta duration = base::Time::Now() -
1134                             response_.request_time;
1135  if (60 < duration.InMinutes())
1136    return;
1137
1138  base::TimeDelta total_duration = base::Time::Now() - start_time_;
1139
1140  UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration,
1141                             base::TimeDelta::FromMilliseconds(1),
1142                             base::TimeDelta::FromMinutes(10),
1143                             100);
1144  UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1145                             total_duration,
1146                             base::TimeDelta::FromMilliseconds(1),
1147                             base::TimeDelta::FromMinutes(10), 100);
1148
1149  if (!stream_->IsConnectionReused()) {
1150    UMA_HISTOGRAM_CUSTOM_TIMES(
1151        "Net.Transaction_Latency_Total_New_Connection",
1152        total_duration, base::TimeDelta::FromMilliseconds(1),
1153        base::TimeDelta::FromMinutes(10), 100);
1154  }
1155}
1156
1157int HttpNetworkTransaction::HandleCertificateRequest(int error) {
1158  // There are two paths through which the server can request a certificate
1159  // from us.  The first is during the initial handshake, the second is
1160  // during SSL renegotiation.
1161  //
1162  // In both cases, we want to close the connection before proceeding.
1163  // We do this for two reasons:
1164  //   First, we don't want to keep the connection to the server hung for a
1165  //   long time while the user selects a certificate.
1166  //   Second, even if we did keep the connection open, NSS has a bug where
1167  //   restarting the handshake for ClientAuth is currently broken.
1168  DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
1169
1170  if (stream_.get()) {
1171    // Since we already have a stream, we're being called as part of SSL
1172    // renegotiation.
1173    DCHECK(!stream_request_.get());
1174    stream_->Close(true);
1175    stream_.reset();
1176  }
1177
1178  // The server is asking for a client certificate during the initial
1179  // handshake.
1180  stream_request_.reset();
1181
1182  // If the user selected one of the certificates in client_certs or declined
1183  // to provide one for this server before, use the past decision
1184  // automatically.
1185  scoped_refptr<X509Certificate> client_cert;
1186  bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1187      response_.cert_request_info->host_and_port, &client_cert);
1188  if (!found_cached_cert)
1189    return error;
1190
1191  // Check that the certificate selected is still a certificate the server
1192  // is likely to accept, based on the criteria supplied in the
1193  // CertificateRequest message.
1194  if (client_cert.get()) {
1195    const std::vector<std::string>& cert_authorities =
1196        response_.cert_request_info->cert_authorities;
1197
1198    bool cert_still_valid = cert_authorities.empty() ||
1199        client_cert->IsIssuedByEncoded(cert_authorities);
1200    if (!cert_still_valid)
1201      return error;
1202  }
1203
1204  // TODO(davidben): Add a unit test which covers this path; we need to be
1205  // able to send a legitimate certificate and also bypass/clear the
1206  // SSL session cache.
1207  SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1208      &proxy_ssl_config_ : &server_ssl_config_;
1209  ssl_config->send_client_cert = true;
1210  ssl_config->client_cert = client_cert;
1211  next_state_ = STATE_CREATE_STREAM;
1212  // Reset the other member variables.
1213  // Note: this is necessary only with SSL renegotiation.
1214  ResetStateForRestart();
1215  return OK;
1216}
1217
1218// TODO(rch): This does not correctly handle errors when an SSL proxy is
1219// being used, as all of the errors are handled as if they were generated
1220// by the endpoint host, request_->url, rather than considering if they were
1221// generated by the SSL proxy. http://crbug.com/69329
1222int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1223  DCHECK(request_);
1224  if (server_ssl_config_.send_client_cert &&
1225      (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1226    session_->ssl_client_auth_cache()->Remove(
1227        GetHostAndPort(request_->url));
1228  }
1229
1230  bool should_fallback = false;
1231  uint16 version_max = server_ssl_config_.version_max;
1232
1233  switch (error) {
1234    case ERR_SSL_PROTOCOL_ERROR:
1235    case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
1236      if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1237          version_max > server_ssl_config_.version_min) {
1238        // This could be a TLS-intolerant server or a server that chose a
1239        // cipher suite defined only for higher protocol versions (such as
1240        // an SSL 3.0 server that chose a TLS-only cipher suite).  Fall
1241        // back to the next lower version and retry.
1242        // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1243        // specifying TLS 1.1 in version_max will result in a TLS 1.0
1244        // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1245        // repeat the TLS 1.0 handshake. To avoid this problem, the default
1246        // version_max should match the maximum protocol version supported
1247        // by the SSLClientSocket class.
1248        version_max--;
1249        should_fallback = true;
1250      }
1251      break;
1252    case ERR_SSL_BAD_RECORD_MAC_ALERT:
1253      if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1254          version_max > server_ssl_config_.version_min) {
1255        // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1256        // 1.2 ClientHello, but then return a bad_record_mac alert. See
1257        // crbug.com/260358. In order to make the fallback as minimal as
1258        // possible, this fallback is only triggered for >= TLS 1.1.
1259        version_max--;
1260        should_fallback = true;
1261      }
1262      break;
1263  }
1264
1265  // While fallback should be eliminated because of security reasons,
1266  // there is a high risk of breaking the servers if this is done in
1267  // general.
1268  //
1269  // For now fallback is disabled for Google servers first, and will be
1270  // expanded to other servers after enough experiences have been gained
1271  // showing that this experiment works well with today's Internet.
1272  //
1273  // The --enable-unrestricted-ssl3-fallback command-line flag exists to allow
1274  // fallback to any version, all the way down to SSLv3.
1275  if (!server_ssl_config_.unrestricted_ssl3_fallback_enabled &&
1276      TransportSecurityState::IsGooglePinnedProperty(request_->url.host(),
1277                                                     true /* include SNI */)) {
1278    should_fallback = false;
1279  }
1280
1281  if (should_fallback) {
1282    net_log_.AddEvent(
1283        NetLog::TYPE_SSL_VERSION_FALLBACK,
1284        base::Bind(&NetLogSSLVersionFallbackCallback,
1285                   &request_->url, error, server_ssl_config_.version_max,
1286                   version_max));
1287    server_ssl_config_.version_max = version_max;
1288    server_ssl_config_.version_fallback = true;
1289    ResetConnectionAndRequestForResend();
1290    error = OK;
1291  }
1292
1293  return error;
1294}
1295
1296// This method determines whether it is safe to resend the request after an
1297// IO error.  It can only be called in response to request header or body
1298// write errors or response header read errors.  It should not be used in
1299// other cases, such as a Connect error.
1300int HttpNetworkTransaction::HandleIOError(int error) {
1301  // SSL errors may happen at any time during the stream and indicate issues
1302  // with the underlying connection. Because the peer may request
1303  // renegotiation at any time, check and handle any possible SSL handshake
1304  // related errors. In addition to renegotiation, TLS False Start may cause
1305  // SSL handshake errors (specifically servers with buggy DEFLATE support)
1306  // to be delayed until the first Read on the underlying connection.
1307  error = HandleSSLHandshakeError(error);
1308
1309  switch (error) {
1310    // If we try to reuse a connection that the server is in the process of
1311    // closing, we may end up successfully writing out our request (or a
1312    // portion of our request) only to find a connection error when we try to
1313    // read from (or finish writing to) the socket.
1314    case ERR_CONNECTION_RESET:
1315    case ERR_CONNECTION_CLOSED:
1316    case ERR_CONNECTION_ABORTED:
1317    // There can be a race between the socket pool checking checking whether a
1318    // socket is still connected, receiving the FIN, and sending/reading data
1319    // on a reused socket.  If we receive the FIN between the connectedness
1320    // check and writing/reading from the socket, we may first learn the socket
1321    // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED.  This will most
1322    // likely happen when trying to retrieve its IP address.
1323    // See http://crbug.com/105824 for more details.
1324    case ERR_SOCKET_NOT_CONNECTED:
1325      if (ShouldResendRequest(error)) {
1326        net_log_.AddEventWithNetErrorCode(
1327            NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1328        ResetConnectionAndRequestForResend();
1329        error = OK;
1330      }
1331      break;
1332    case ERR_PIPELINE_EVICTION:
1333      if (!session_->force_http_pipelining()) {
1334        net_log_.AddEventWithNetErrorCode(
1335            NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1336        ResetConnectionAndRequestForResend();
1337        error = OK;
1338      }
1339      break;
1340    case ERR_SPDY_PING_FAILED:
1341    case ERR_SPDY_SERVER_REFUSED_STREAM:
1342      net_log_.AddEventWithNetErrorCode(
1343          NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1344      ResetConnectionAndRequestForResend();
1345      error = OK;
1346      break;
1347  }
1348  return error;
1349}
1350
1351void HttpNetworkTransaction::ResetStateForRestart() {
1352  ResetStateForAuthRestart();
1353  stream_.reset();
1354}
1355
1356void HttpNetworkTransaction::ResetStateForAuthRestart() {
1357  send_start_time_ = base::TimeTicks();
1358  send_end_time_ = base::TimeTicks();
1359
1360  pending_auth_target_ = HttpAuth::AUTH_NONE;
1361  read_buf_ = NULL;
1362  read_buf_len_ = 0;
1363  headers_valid_ = false;
1364  request_headers_.Clear();
1365  response_ = HttpResponseInfo();
1366  establishing_tunnel_ = false;
1367}
1368
1369HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
1370  return response_.headers.get();
1371}
1372
1373bool HttpNetworkTransaction::ShouldResendRequest(int error) const {
1374  bool connection_is_proven = stream_->IsConnectionReused();
1375  bool has_received_headers = GetResponseHeaders() != NULL;
1376
1377  // NOTE: we resend a request only if we reused a keep-alive connection.
1378  // This automatically prevents an infinite resend loop because we'll run
1379  // out of the cached keep-alive connections eventually.
1380  if (connection_is_proven && !has_received_headers)
1381    return true;
1382  return false;
1383}
1384
1385void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1386  if (stream_.get()) {
1387    stream_->Close(true);
1388    stream_.reset();
1389  }
1390
1391  // We need to clear request_headers_ because it contains the real request
1392  // headers, but we may need to resend the CONNECT request first to recreate
1393  // the SSL tunnel.
1394  request_headers_.Clear();
1395  next_state_ = STATE_CREATE_STREAM;  // Resend the request.
1396}
1397
1398bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1399  return !is_https_request() &&
1400      (proxy_info_.is_https() || proxy_info_.is_http());
1401}
1402
1403bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1404  return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
1405}
1406
1407int HttpNetworkTransaction::HandleAuthChallenge() {
1408  scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
1409  DCHECK(headers.get());
1410
1411  int status = headers->response_code();
1412  if (status != HTTP_UNAUTHORIZED &&
1413      status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
1414    return OK;
1415  HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
1416                            HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
1417  if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1418    return ERR_UNEXPECTED_PROXY_AUTH;
1419
1420  // This case can trigger when an HTTPS server responds with a "Proxy
1421  // authentication required" status code through a non-authenticating
1422  // proxy.
1423  if (!auth_controllers_[target].get())
1424    return ERR_UNEXPECTED_PROXY_AUTH;
1425
1426  int rv = auth_controllers_[target]->HandleAuthChallenge(
1427      headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1428      net_log_);
1429  if (auth_controllers_[target]->HaveAuthHandler())
1430      pending_auth_target_ = target;
1431
1432  scoped_refptr<AuthChallengeInfo> auth_info =
1433      auth_controllers_[target]->auth_info();
1434  if (auth_info.get())
1435      response_.auth_challenge = auth_info;
1436
1437  return rv;
1438}
1439
1440bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1441  return auth_controllers_[target].get() &&
1442      auth_controllers_[target]->HaveAuth();
1443}
1444
1445GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1446  switch (target) {
1447    case HttpAuth::AUTH_PROXY: {
1448      if (!proxy_info_.proxy_server().is_valid() ||
1449          proxy_info_.proxy_server().is_direct()) {
1450        return GURL();  // There is no proxy server.
1451      }
1452      const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1453      return GURL(scheme +
1454                  proxy_info_.proxy_server().host_port_pair().ToString());
1455    }
1456    case HttpAuth::AUTH_SERVER:
1457      return request_->url;
1458    default:
1459     return GURL();
1460  }
1461}
1462
1463#define STATE_CASE(s) \
1464  case s: \
1465    description = base::StringPrintf("%s (0x%08X)", #s, s); \
1466    break
1467
1468std::string HttpNetworkTransaction::DescribeState(State state) {
1469  std::string description;
1470  switch (state) {
1471    STATE_CASE(STATE_CREATE_STREAM);
1472    STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
1473    STATE_CASE(STATE_INIT_REQUEST_BODY);
1474    STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
1475    STATE_CASE(STATE_BUILD_REQUEST);
1476    STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
1477    STATE_CASE(STATE_SEND_REQUEST);
1478    STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1479    STATE_CASE(STATE_READ_HEADERS);
1480    STATE_CASE(STATE_READ_HEADERS_COMPLETE);
1481    STATE_CASE(STATE_READ_BODY);
1482    STATE_CASE(STATE_READ_BODY_COMPLETE);
1483    STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1484    STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
1485    STATE_CASE(STATE_NONE);
1486    default:
1487      description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1488                                       state);
1489      break;
1490  }
1491  return description;
1492}
1493
1494#undef STATE_CASE
1495
1496}  // namespace net
1497