http_network_transaction.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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_);
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 == ERR_QUIC_HANDSHAKE_FAILED) {
901    ResetConnectionAndRequestForResend();
902    return OK;
903  }
904
905  if (result < 0 && result != ERR_CONNECTION_CLOSED)
906    return HandleIOError(result);
907
908  if (result == ERR_CONNECTION_CLOSED && ShouldResendRequest(result)) {
909    ResetConnectionAndRequestForResend();
910    return OK;
911  }
912
913  // After we call RestartWithAuth a new response_time will be recorded, and
914  // we need to be cautious about incorrectly logging the duration across the
915  // authentication activity.
916  if (result == OK)
917    LogTransactionConnectedMetrics();
918
919  if (result == ERR_CONNECTION_CLOSED) {
920    // For now, if we get at least some data, we do the best we can to make
921    // sense of it and send it back up the stack.
922    int rv = HandleConnectionClosedBeforeEndOfHeaders();
923    if (rv != OK)
924      return rv;
925  }
926  DCHECK(response_.headers.get());
927
928  // Server-induced fallback; see: http://crbug.com/143712
929  if (response_.was_fetched_via_proxy && response_.headers.get() != NULL) {
930    bool should_fallback =
931        response_.headers->HasHeaderValue("connection", "proxy-bypass");
932    // Additionally, fallback if a 500 is returned via the data reduction proxy.
933    // This is conservative, as the 500 might have been generated by the origin,
934    // and not the proxy.
935#if defined(SPDY_PROXY_AUTH_ORIGIN)
936    if (!should_fallback) {
937      should_fallback =
938          response_.headers->response_code() == HTTP_INTERNAL_SERVER_ERROR &&
939          proxy_info_.proxy_server().host_port_pair().Equals(
940              HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN)));
941    }
942#endif
943    if (should_fallback) {
944      ProxyService* proxy_service = session_->proxy_service();
945      if (proxy_service->MarkProxyAsBad(proxy_info_, net_log_)) {
946        // Only retry in the case of GETs. We don't want to resubmit a POST
947        // if the proxy took some action.
948        if (request_->method == "GET") {
949          ResetConnectionAndRequestForResend();
950          return OK;
951        }
952      }
953    }
954  }
955
956  // Like Net.HttpResponseCode, but only for MAIN_FRAME loads.
957  if (request_->load_flags & LOAD_MAIN_FRAME) {
958    const int response_code = response_.headers->response_code();
959    UMA_HISTOGRAM_ENUMERATION(
960        "Net.HttpResponseCode_Nxx_MainFrame", response_code/100, 10);
961  }
962
963  net_log_.AddEvent(
964      NetLog::TYPE_HTTP_TRANSACTION_READ_RESPONSE_HEADERS,
965      base::Bind(&HttpResponseHeaders::NetLogCallback, response_.headers));
966
967  if (response_.headers->GetParsedHttpVersion() < HttpVersion(1, 0)) {
968    // HTTP/0.9 doesn't support the PUT method, so lack of response headers
969    // indicates a buggy server.  See:
970    // https://bugzilla.mozilla.org/show_bug.cgi?id=193921
971    if (request_->method == "PUT")
972      return ERR_METHOD_NOT_SUPPORTED;
973  }
974
975  // Check for an intermediate 100 Continue response.  An origin server is
976  // allowed to send this response even if we didn't ask for it, so we just
977  // need to skip over it.
978  // We treat any other 1xx in this same way (although in practice getting
979  // a 1xx that isn't a 100 is rare).
980  if (response_.headers->response_code() / 100 == 1) {
981    response_.headers = new HttpResponseHeaders(std::string());
982    next_state_ = STATE_READ_HEADERS;
983    return OK;
984  }
985
986  HostPortPair endpoint = HostPortPair(request_->url.HostNoBrackets(),
987                                       request_->url.EffectiveIntPort());
988  ProcessAlternateProtocol(session_->http_stream_factory(),
989                           session_->http_server_properties(),
990                           *response_.headers.get(),
991                           endpoint);
992
993  int rv = HandleAuthChallenge();
994  if (rv != OK)
995    return rv;
996
997  if (is_https_request())
998    stream_->GetSSLInfo(&response_.ssl_info);
999
1000  headers_valid_ = true;
1001  return OK;
1002}
1003
1004int HttpNetworkTransaction::DoReadBody() {
1005  DCHECK(read_buf_.get());
1006  DCHECK_GT(read_buf_len_, 0);
1007  DCHECK(stream_ != NULL);
1008
1009  next_state_ = STATE_READ_BODY_COMPLETE;
1010  return stream_->ReadResponseBody(
1011      read_buf_.get(), read_buf_len_, io_callback_);
1012}
1013
1014int HttpNetworkTransaction::DoReadBodyComplete(int result) {
1015  // We are done with the Read call.
1016  bool done = false;
1017  if (result <= 0) {
1018    DCHECK_NE(ERR_IO_PENDING, result);
1019    done = true;
1020  }
1021
1022  bool keep_alive = false;
1023  if (stream_->IsResponseBodyComplete()) {
1024    // Note: Just because IsResponseBodyComplete is true, we're not
1025    // necessarily "done".  We're only "done" when it is the last
1026    // read on this HttpNetworkTransaction, which will be signified
1027    // by a zero-length read.
1028    // TODO(mbelshe): The keepalive property is really a property of
1029    //    the stream.  No need to compute it here just to pass back
1030    //    to the stream's Close function.
1031    // TODO(rtenneti): CanFindEndOfResponse should return false if there are no
1032    // ResponseHeaders.
1033    if (stream_->CanFindEndOfResponse()) {
1034      HttpResponseHeaders* headers = GetResponseHeaders();
1035      if (headers)
1036        keep_alive = headers->IsKeepAlive();
1037    }
1038  }
1039
1040  // Clean up connection if we are done.
1041  if (done) {
1042    LogTransactionMetrics();
1043    stream_->Close(!keep_alive);
1044    // Note: we don't reset the stream here.  We've closed it, but we still
1045    // need it around so that callers can call methods such as
1046    // GetUploadProgress() and have them be meaningful.
1047    // TODO(mbelshe): This means we closed the stream here, and we close it
1048    // again in ~HttpNetworkTransaction.  Clean that up.
1049
1050    // The next Read call will return 0 (EOF).
1051  }
1052
1053  // Clear these to avoid leaving around old state.
1054  read_buf_ = NULL;
1055  read_buf_len_ = 0;
1056
1057  return result;
1058}
1059
1060int HttpNetworkTransaction::DoDrainBodyForAuthRestart() {
1061  // This method differs from DoReadBody only in the next_state_.  So we just
1062  // call DoReadBody and override the next_state_.  Perhaps there is a more
1063  // elegant way for these two methods to share code.
1064  int rv = DoReadBody();
1065  DCHECK(next_state_ == STATE_READ_BODY_COMPLETE);
1066  next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE;
1067  return rv;
1068}
1069
1070// TODO(wtc): This method and the DoReadBodyComplete method are almost
1071// the same.  Figure out a good way for these two methods to share code.
1072int HttpNetworkTransaction::DoDrainBodyForAuthRestartComplete(int result) {
1073  // keep_alive defaults to true because the very reason we're draining the
1074  // response body is to reuse the connection for auth restart.
1075  bool done = false, keep_alive = true;
1076  if (result < 0) {
1077    // Error or closed connection while reading the socket.
1078    done = true;
1079    keep_alive = false;
1080  } else if (stream_->IsResponseBodyComplete()) {
1081    done = true;
1082  }
1083
1084  if (done) {
1085    DidDrainBodyForAuthRestart(keep_alive);
1086  } else {
1087    // Keep draining.
1088    next_state_ = STATE_DRAIN_BODY_FOR_AUTH_RESTART;
1089  }
1090
1091  return OK;
1092}
1093
1094void HttpNetworkTransaction::LogTransactionConnectedMetrics() {
1095  if (logged_response_time_)
1096    return;
1097
1098  logged_response_time_ = true;
1099
1100  base::TimeDelta total_duration = response_.response_time - start_time_;
1101
1102  UMA_HISTOGRAM_CUSTOM_TIMES(
1103      "Net.Transaction_Connected",
1104      total_duration,
1105      base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1106      100);
1107
1108  bool reused_socket = stream_->IsConnectionReused();
1109  if (!reused_socket) {
1110    UMA_HISTOGRAM_CUSTOM_TIMES(
1111        "Net.Transaction_Connected_New_b",
1112        total_duration,
1113        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1114        100);
1115  }
1116
1117  // Currently, non-HIGHEST priority requests are frame or sub-frame resource
1118  // types.  This will change when we also prioritize certain subresources like
1119  // css, js, etc.
1120  if (priority_ != HIGHEST) {
1121    UMA_HISTOGRAM_CUSTOM_TIMES(
1122        "Net.Priority_High_Latency_b",
1123        total_duration,
1124        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1125        100);
1126  } else {
1127    UMA_HISTOGRAM_CUSTOM_TIMES(
1128        "Net.Priority_Low_Latency_b",
1129        total_duration,
1130        base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(10),
1131        100);
1132  }
1133}
1134
1135void HttpNetworkTransaction::LogTransactionMetrics() const {
1136  base::TimeDelta duration = base::Time::Now() -
1137                             response_.request_time;
1138  if (60 < duration.InMinutes())
1139    return;
1140
1141  base::TimeDelta total_duration = base::Time::Now() - start_time_;
1142
1143  UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_b", duration,
1144                             base::TimeDelta::FromMilliseconds(1),
1145                             base::TimeDelta::FromMinutes(10),
1146                             100);
1147  UMA_HISTOGRAM_CUSTOM_TIMES("Net.Transaction_Latency_Total",
1148                             total_duration,
1149                             base::TimeDelta::FromMilliseconds(1),
1150                             base::TimeDelta::FromMinutes(10), 100);
1151
1152  if (!stream_->IsConnectionReused()) {
1153    UMA_HISTOGRAM_CUSTOM_TIMES(
1154        "Net.Transaction_Latency_Total_New_Connection",
1155        total_duration, base::TimeDelta::FromMilliseconds(1),
1156        base::TimeDelta::FromMinutes(10), 100);
1157  }
1158}
1159
1160int HttpNetworkTransaction::HandleCertificateRequest(int error) {
1161  // There are two paths through which the server can request a certificate
1162  // from us.  The first is during the initial handshake, the second is
1163  // during SSL renegotiation.
1164  //
1165  // In both cases, we want to close the connection before proceeding.
1166  // We do this for two reasons:
1167  //   First, we don't want to keep the connection to the server hung for a
1168  //   long time while the user selects a certificate.
1169  //   Second, even if we did keep the connection open, NSS has a bug where
1170  //   restarting the handshake for ClientAuth is currently broken.
1171  DCHECK_EQ(error, ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
1172
1173  if (stream_.get()) {
1174    // Since we already have a stream, we're being called as part of SSL
1175    // renegotiation.
1176    DCHECK(!stream_request_.get());
1177    stream_->Close(true);
1178    stream_.reset();
1179  }
1180
1181  // The server is asking for a client certificate during the initial
1182  // handshake.
1183  stream_request_.reset();
1184
1185  // If the user selected one of the certificates in client_certs or declined
1186  // to provide one for this server before, use the past decision
1187  // automatically.
1188  scoped_refptr<X509Certificate> client_cert;
1189  bool found_cached_cert = session_->ssl_client_auth_cache()->Lookup(
1190      response_.cert_request_info->host_and_port, &client_cert);
1191  if (!found_cached_cert)
1192    return error;
1193
1194  // Check that the certificate selected is still a certificate the server
1195  // is likely to accept, based on the criteria supplied in the
1196  // CertificateRequest message.
1197  if (client_cert.get()) {
1198    const std::vector<std::string>& cert_authorities =
1199        response_.cert_request_info->cert_authorities;
1200
1201    bool cert_still_valid = cert_authorities.empty() ||
1202        client_cert->IsIssuedByEncoded(cert_authorities);
1203    if (!cert_still_valid)
1204      return error;
1205  }
1206
1207  // TODO(davidben): Add a unit test which covers this path; we need to be
1208  // able to send a legitimate certificate and also bypass/clear the
1209  // SSL session cache.
1210  SSLConfig* ssl_config = response_.cert_request_info->is_proxy ?
1211      &proxy_ssl_config_ : &server_ssl_config_;
1212  ssl_config->send_client_cert = true;
1213  ssl_config->client_cert = client_cert;
1214  next_state_ = STATE_CREATE_STREAM;
1215  // Reset the other member variables.
1216  // Note: this is necessary only with SSL renegotiation.
1217  ResetStateForRestart();
1218  return OK;
1219}
1220
1221// TODO(rch): This does not correctly handle errors when an SSL proxy is
1222// being used, as all of the errors are handled as if they were generated
1223// by the endpoint host, request_->url, rather than considering if they were
1224// generated by the SSL proxy. http://crbug.com/69329
1225int HttpNetworkTransaction::HandleSSLHandshakeError(int error) {
1226  DCHECK(request_);
1227  if (server_ssl_config_.send_client_cert &&
1228      (error == ERR_SSL_PROTOCOL_ERROR || IsClientCertificateError(error))) {
1229    session_->ssl_client_auth_cache()->Remove(
1230        GetHostAndPort(request_->url));
1231  }
1232
1233  bool should_fallback = false;
1234  uint16 version_max = server_ssl_config_.version_max;
1235
1236  switch (error) {
1237    case ERR_SSL_PROTOCOL_ERROR:
1238    case ERR_SSL_VERSION_OR_CIPHER_MISMATCH:
1239      if (version_max >= SSL_PROTOCOL_VERSION_TLS1 &&
1240          version_max > server_ssl_config_.version_min) {
1241        // This could be a TLS-intolerant server or a server that chose a
1242        // cipher suite defined only for higher protocol versions (such as
1243        // an SSL 3.0 server that chose a TLS-only cipher suite).  Fall
1244        // back to the next lower version and retry.
1245        // NOTE: if the SSLClientSocket class doesn't support TLS 1.1,
1246        // specifying TLS 1.1 in version_max will result in a TLS 1.0
1247        // handshake, so falling back from TLS 1.1 to TLS 1.0 will simply
1248        // repeat the TLS 1.0 handshake. To avoid this problem, the default
1249        // version_max should match the maximum protocol version supported
1250        // by the SSLClientSocket class.
1251        version_max--;
1252        should_fallback = true;
1253      }
1254      break;
1255    case ERR_SSL_BAD_RECORD_MAC_ALERT:
1256      if (version_max >= SSL_PROTOCOL_VERSION_TLS1_1 &&
1257          version_max > server_ssl_config_.version_min) {
1258        // Some broken SSL devices negotiate TLS 1.0 when sent a TLS 1.1 or
1259        // 1.2 ClientHello, but then return a bad_record_mac alert. See
1260        // crbug.com/260358. In order to make the fallback as minimal as
1261        // possible, this fallback is only triggered for >= TLS 1.1.
1262        version_max--;
1263        should_fallback = true;
1264      }
1265      break;
1266  }
1267
1268  // While fallback should be eliminated because of security reasons,
1269  // there is a high risk of breaking the servers if this is done in
1270  // general.
1271  //
1272  // For now fallback is disabled for Google servers first, and will be
1273  // expanded to other servers after enough experiences have been gained
1274  // showing that this experiment works well with today's Internet.
1275  //
1276  // The --enable-unrestricted-ssl3-fallback command-line flag exists to allow
1277  // fallback to any version, all the way down to SSLv3.
1278  if (!server_ssl_config_.unrestricted_ssl3_fallback_enabled &&
1279      TransportSecurityState::IsGooglePinnedProperty(request_->url.host(),
1280                                                     true /* include SNI */)) {
1281    should_fallback = false;
1282  }
1283
1284  if (should_fallback) {
1285    net_log_.AddEvent(
1286        NetLog::TYPE_SSL_VERSION_FALLBACK,
1287        base::Bind(&NetLogSSLVersionFallbackCallback,
1288                   &request_->url, error, server_ssl_config_.version_max,
1289                   version_max));
1290    server_ssl_config_.version_max = version_max;
1291    server_ssl_config_.version_fallback = true;
1292    ResetConnectionAndRequestForResend();
1293    error = OK;
1294  }
1295
1296  return error;
1297}
1298
1299// This method determines whether it is safe to resend the request after an
1300// IO error.  It can only be called in response to request header or body
1301// write errors or response header read errors.  It should not be used in
1302// other cases, such as a Connect error.
1303int HttpNetworkTransaction::HandleIOError(int error) {
1304  // SSL errors may happen at any time during the stream and indicate issues
1305  // with the underlying connection. Because the peer may request
1306  // renegotiation at any time, check and handle any possible SSL handshake
1307  // related errors. In addition to renegotiation, TLS False Start may cause
1308  // SSL handshake errors (specifically servers with buggy DEFLATE support)
1309  // to be delayed until the first Read on the underlying connection.
1310  error = HandleSSLHandshakeError(error);
1311
1312  switch (error) {
1313    // If we try to reuse a connection that the server is in the process of
1314    // closing, we may end up successfully writing out our request (or a
1315    // portion of our request) only to find a connection error when we try to
1316    // read from (or finish writing to) the socket.
1317    case ERR_CONNECTION_RESET:
1318    case ERR_CONNECTION_CLOSED:
1319    case ERR_CONNECTION_ABORTED:
1320    // There can be a race between the socket pool checking checking whether a
1321    // socket is still connected, receiving the FIN, and sending/reading data
1322    // on a reused socket.  If we receive the FIN between the connectedness
1323    // check and writing/reading from the socket, we may first learn the socket
1324    // is disconnected when we get a ERR_SOCKET_NOT_CONNECTED.  This will most
1325    // likely happen when trying to retrieve its IP address.
1326    // See http://crbug.com/105824 for more details.
1327    case ERR_SOCKET_NOT_CONNECTED:
1328      if (ShouldResendRequest(error)) {
1329        net_log_.AddEventWithNetErrorCode(
1330            NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1331        ResetConnectionAndRequestForResend();
1332        error = OK;
1333      }
1334      break;
1335    case ERR_PIPELINE_EVICTION:
1336      if (!session_->force_http_pipelining()) {
1337        net_log_.AddEventWithNetErrorCode(
1338            NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1339        ResetConnectionAndRequestForResend();
1340        error = OK;
1341      }
1342      break;
1343    case ERR_SPDY_PING_FAILED:
1344    case ERR_SPDY_SERVER_REFUSED_STREAM:
1345    case ERR_QUIC_HANDSHAKE_FAILED:
1346      net_log_.AddEventWithNetErrorCode(
1347          NetLog::TYPE_HTTP_TRANSACTION_RESTART_AFTER_ERROR, error);
1348      ResetConnectionAndRequestForResend();
1349      error = OK;
1350      break;
1351  }
1352  return error;
1353}
1354
1355void HttpNetworkTransaction::ResetStateForRestart() {
1356  ResetStateForAuthRestart();
1357  stream_.reset();
1358}
1359
1360void HttpNetworkTransaction::ResetStateForAuthRestart() {
1361  send_start_time_ = base::TimeTicks();
1362  send_end_time_ = base::TimeTicks();
1363
1364  pending_auth_target_ = HttpAuth::AUTH_NONE;
1365  read_buf_ = NULL;
1366  read_buf_len_ = 0;
1367  headers_valid_ = false;
1368  request_headers_.Clear();
1369  response_ = HttpResponseInfo();
1370  establishing_tunnel_ = false;
1371}
1372
1373HttpResponseHeaders* HttpNetworkTransaction::GetResponseHeaders() const {
1374  return response_.headers.get();
1375}
1376
1377bool HttpNetworkTransaction::ShouldResendRequest(int error) const {
1378  bool connection_is_proven = stream_->IsConnectionReused();
1379  bool has_received_headers = GetResponseHeaders() != NULL;
1380
1381  // NOTE: we resend a request only if we reused a keep-alive connection.
1382  // This automatically prevents an infinite resend loop because we'll run
1383  // out of the cached keep-alive connections eventually.
1384  if (connection_is_proven && !has_received_headers)
1385    return true;
1386  return false;
1387}
1388
1389void HttpNetworkTransaction::ResetConnectionAndRequestForResend() {
1390  if (stream_.get()) {
1391    stream_->Close(true);
1392    stream_.reset();
1393  }
1394
1395  // We need to clear request_headers_ because it contains the real request
1396  // headers, but we may need to resend the CONNECT request first to recreate
1397  // the SSL tunnel.
1398  request_headers_.Clear();
1399  next_state_ = STATE_CREATE_STREAM;  // Resend the request.
1400}
1401
1402bool HttpNetworkTransaction::ShouldApplyProxyAuth() const {
1403  return !is_https_request() &&
1404      (proxy_info_.is_https() || proxy_info_.is_http());
1405}
1406
1407bool HttpNetworkTransaction::ShouldApplyServerAuth() const {
1408  return !(request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA);
1409}
1410
1411int HttpNetworkTransaction::HandleAuthChallenge() {
1412  scoped_refptr<HttpResponseHeaders> headers(GetResponseHeaders());
1413  DCHECK(headers.get());
1414
1415  int status = headers->response_code();
1416  if (status != HTTP_UNAUTHORIZED &&
1417      status != HTTP_PROXY_AUTHENTICATION_REQUIRED)
1418    return OK;
1419  HttpAuth::Target target = status == HTTP_PROXY_AUTHENTICATION_REQUIRED ?
1420                            HttpAuth::AUTH_PROXY : HttpAuth::AUTH_SERVER;
1421  if (target == HttpAuth::AUTH_PROXY && proxy_info_.is_direct())
1422    return ERR_UNEXPECTED_PROXY_AUTH;
1423
1424  // This case can trigger when an HTTPS server responds with a "Proxy
1425  // authentication required" status code through a non-authenticating
1426  // proxy.
1427  if (!auth_controllers_[target].get())
1428    return ERR_UNEXPECTED_PROXY_AUTH;
1429
1430  int rv = auth_controllers_[target]->HandleAuthChallenge(
1431      headers, (request_->load_flags & LOAD_DO_NOT_SEND_AUTH_DATA) != 0, false,
1432      net_log_);
1433  if (auth_controllers_[target]->HaveAuthHandler())
1434      pending_auth_target_ = target;
1435
1436  scoped_refptr<AuthChallengeInfo> auth_info =
1437      auth_controllers_[target]->auth_info();
1438  if (auth_info.get())
1439      response_.auth_challenge = auth_info;
1440
1441  return rv;
1442}
1443
1444bool HttpNetworkTransaction::HaveAuth(HttpAuth::Target target) const {
1445  return auth_controllers_[target].get() &&
1446      auth_controllers_[target]->HaveAuth();
1447}
1448
1449GURL HttpNetworkTransaction::AuthURL(HttpAuth::Target target) const {
1450  switch (target) {
1451    case HttpAuth::AUTH_PROXY: {
1452      if (!proxy_info_.proxy_server().is_valid() ||
1453          proxy_info_.proxy_server().is_direct()) {
1454        return GURL();  // There is no proxy server.
1455      }
1456      const char* scheme = proxy_info_.is_https() ? "https://" : "http://";
1457      return GURL(scheme +
1458                  proxy_info_.proxy_server().host_port_pair().ToString());
1459    }
1460    case HttpAuth::AUTH_SERVER:
1461      return request_->url;
1462    default:
1463     return GURL();
1464  }
1465}
1466
1467#define STATE_CASE(s) \
1468  case s: \
1469    description = base::StringPrintf("%s (0x%08X)", #s, s); \
1470    break
1471
1472std::string HttpNetworkTransaction::DescribeState(State state) {
1473  std::string description;
1474  switch (state) {
1475    STATE_CASE(STATE_CREATE_STREAM);
1476    STATE_CASE(STATE_CREATE_STREAM_COMPLETE);
1477    STATE_CASE(STATE_INIT_REQUEST_BODY);
1478    STATE_CASE(STATE_INIT_REQUEST_BODY_COMPLETE);
1479    STATE_CASE(STATE_BUILD_REQUEST);
1480    STATE_CASE(STATE_BUILD_REQUEST_COMPLETE);
1481    STATE_CASE(STATE_SEND_REQUEST);
1482    STATE_CASE(STATE_SEND_REQUEST_COMPLETE);
1483    STATE_CASE(STATE_READ_HEADERS);
1484    STATE_CASE(STATE_READ_HEADERS_COMPLETE);
1485    STATE_CASE(STATE_READ_BODY);
1486    STATE_CASE(STATE_READ_BODY_COMPLETE);
1487    STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART);
1488    STATE_CASE(STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE);
1489    STATE_CASE(STATE_NONE);
1490    default:
1491      description = base::StringPrintf("Unknown state 0x%08X (%u)", state,
1492                                       state);
1493      break;
1494  }
1495  return description;
1496}
1497
1498#undef STATE_CASE
1499
1500}  // namespace net
1501