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