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