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