15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_log.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/request_priority.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_auth.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_request_headers.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_response_info.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_stream_factory.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_transaction.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/proxy/proxy_service.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "net/ssl/ssl_config_service.h"
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "net/websockets/websocket_handshake_stream_base.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass ClientSocketHandle;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpAuthController;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpNetworkSession;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpStreamBase;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HttpStreamRequest;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IOBuffer;
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass SpdySession;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct HttpRequestInfo;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT_PRIVATE HttpNetworkTransaction
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : public HttpTransaction,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      public HttpStreamRequest::Delegate {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  HttpNetworkTransaction(RequestPriority priority,
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         HttpNetworkSession* session);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~HttpNetworkTransaction();
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HttpTransaction methods:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Start(const HttpRequestInfo* request_info,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const CompletionCallback& callback,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    const BoundNetLog& net_log) OVERRIDE;
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int RestartIgnoringLastError(
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const CompletionCallback& callback) OVERRIDE;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int RestartWithCertificate(
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      X509Certificate* client_cert,
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const CompletionCallback& callback) OVERRIDE;
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int RestartWithAuth(const AuthCredentials& credentials,
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const CompletionCallback& callback) OVERRIDE;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool IsReadyToRestartForAuth() OVERRIDE;
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual int Read(IOBuffer* buf,
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   int buf_len,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   const CompletionCallback& callback) OVERRIDE;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void StopCaching() OVERRIDE {}
63eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  virtual bool GetFullRequestHeaders(
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      HttpRequestHeaders* headers) const OVERRIDE;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void DoneReading() OVERRIDE {}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual const HttpResponseInfo* GetResponseInfo() const OVERRIDE;
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual LoadState GetLoadState() const OVERRIDE;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual UploadProgress GetUploadProgress() const OVERRIDE;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool GetLoadTimingInfo(
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LoadTimingInfo* load_timing_info) const OVERRIDE;
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void SetPriority(RequestPriority priority) OVERRIDE;
72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual void SetWebSocketHandshakeStreamCreateHelper(
73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      WebSocketHandshakeStreamBase::CreateHelper* create_helper) OVERRIDE;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HttpStreamRequest::Delegate methods:
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnStreamReady(const SSLConfig& used_ssl_config,
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             const ProxyInfo& used_proxy_info,
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             HttpStreamBase* stream) OVERRIDE;
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void OnWebSocketHandshakeStreamReady(
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const SSLConfig& used_ssl_config,
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const ProxyInfo& used_proxy_info,
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      WebSocketHandshakeStreamBase* stream) OVERRIDE;
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnStreamFailed(int status,
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              const SSLConfig& used_ssl_config) OVERRIDE;
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnCertificateError(int status,
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const SSLConfig& used_ssl_config,
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                  const SSLInfo& ssl_info) OVERRIDE;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnNeedsProxyAuth(
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const HttpResponseInfo& response_info,
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const SSLConfig& used_ssl_config,
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const ProxyInfo& used_proxy_info,
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      HttpAuthController* auth_controller) OVERRIDE;
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 SSLCertRequestInfo* cert_info) OVERRIDE;
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info,
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          const SSLConfig& used_ssl_config,
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          const ProxyInfo& used_proxy_info,
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          HttpStreamBase* stream) OVERRIDE;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
101c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  friend class HttpNetworkTransactionSSLTest;
102c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           ResetStateForRestart);
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           WindowUpdateReceived);
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           WindowUpdateSent);
1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           WindowUpdateOverflow);
1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           FlowControlStallResume);
1137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           FlowControlStallResumeAfterSettings);
1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SpdyNetworkTransactionTest,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           FlowControlNegativeSendWindowSize);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum State {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_CREATE_STREAM,
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_CREATE_STREAM_COMPLETE,
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_INIT_STREAM,
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_INIT_STREAM_COMPLETE,
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_GENERATE_PROXY_AUTH_TOKEN,
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_GENERATE_PROXY_AUTH_TOKEN_COMPLETE,
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_GENERATE_SERVER_AUTH_TOKEN,
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_GENERATE_SERVER_AUTH_TOKEN_COMPLETE,
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_INIT_REQUEST_BODY,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_INIT_REQUEST_BODY_COMPLETE,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_BUILD_REQUEST,
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_BUILD_REQUEST_COMPLETE,
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_SEND_REQUEST,
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_SEND_REQUEST_COMPLETE,
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_READ_HEADERS,
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_READ_HEADERS_COMPLETE,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_READ_BODY,
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_READ_BODY_COMPLETE,
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_DRAIN_BODY_FOR_AUTH_RESTART,
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_DRAIN_BODY_FOR_AUTH_RESTART_COMPLETE,
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    STATE_NONE
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_https_request() const;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DoCallback(int result);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnIOComplete(int result);
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the state transition loop.
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoLoop(int result);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Each of these methods corresponds to a State value.  Those with an input
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // argument receive the result from the previous state.  If a method returns
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ERR_IO_PENDING, then the result from OnIOComplete will be passed to the
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // next state method as the result arg.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoCreateStream();
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoCreateStreamComplete(int result);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoInitStream();
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoInitStreamComplete(int result);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoGenerateProxyAuthToken();
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoGenerateProxyAuthTokenComplete(int result);
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoGenerateServerAuthToken();
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoGenerateServerAuthTokenComplete(int result);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoInitRequestBody();
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoInitRequestBodyComplete(int result);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoBuildRequest();
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoBuildRequestComplete(int result);
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoSendRequest();
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoSendRequestComplete(int result);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoReadHeaders();
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoReadHeadersComplete(int result);
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoReadBody();
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoReadBodyComplete(int result);
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoDrainBodyForAuthRestart();
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int DoDrainBodyForAuthRestartComplete(int result);
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void BuildRequestHeaders(bool using_proxy);
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Record histogram of time until first byte of header is received.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LogTransactionConnectedMetrics();
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Record histogram of latency (durations until last byte received).
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LogTransactionMetrics() const;
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Writes a log message to help debugging in the field when we block a proxy
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // response to a CONNECT request.
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void LogBlockedTunnelResponse(int response_code) const;
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called to handle a client certificate request.
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int HandleCertificateRequest(int error);
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Called to possibly handle a client authentication error.
191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void HandleClientAuthError(int error);
192a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called to possibly recover from an SSL handshake error.  Sets next_state_
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and returns OK if recovering from the error.  Otherwise, the same error
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // code is returned.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int HandleSSLHandshakeError(int error);
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called to possibly recover from the given error.  Sets next_state_ and
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returns OK if recovering from the error.  Otherwise, the same error code
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is returned.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int HandleIOError(int error);
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the response headers from the HttpStream.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpResponseHeaders* GetResponseHeaders() const;
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when we reached EOF or got an error.  Returns true if we should
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // resend the request.  |error| is OK when we reached EOF.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShouldResendRequest(int error) const;
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the connection and the request headers for resend.  Called when
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ShouldResendRequest() is true.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetConnectionAndRequestForResend();
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Decides the policy when the connection is closed before the end of headers
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // has been read. This only applies to reading responses, and not writing
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // requests.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int HandleConnectionClosedBeforeEndOfHeaders();
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets up the state machine to restart the transaction with auth.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void PrepareForAuthRestart(HttpAuth::Target target);
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when we don't need to drain the response body or have drained it.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets |connection_| unless |keep_alive| is true, then calls
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ResetStateForRestart.  Sets |next_state_| appropriately.
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DidDrainBodyForAuthRestart(bool keep_alive);
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the members of the transaction so it can be restarted.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetStateForRestart();
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets the members of the transaction, except |stream_|, which needs
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to be maintained for multi-round auth.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetStateForAuthRestart();
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if we should try to add a Proxy-Authorization header
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShouldApplyProxyAuth() const;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if we should try to add an Authorization header.
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShouldApplyServerAuth() const;
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handles HTTP status code 401 or 407.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HandleAuthChallenge() returns a network error code, or OK on success.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // May update |pending_auth_target_| or |response_.auth_challenge|.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int HandleAuthChallenge();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if we have auth credentials for the given target.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HaveAuth(HttpAuth::Target target) const;
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Get the {scheme, host, path, port} for the authentication target
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  GURL AuthURL(HttpAuth::Target target) const;
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
251f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns true if this transaction is for a WebSocket handshake
252f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool ForWebSocketHandshake() const;
253f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Debug helper.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static std::string DescribeState(State state);
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<HttpAuthController>
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      auth_controllers_[HttpAuth::AUTH_NUM_TARGETS];
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether this transaction is waiting for proxy auth, server auth, or is
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // not waiting for any auth at all. |pending_auth_target_| is read and
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // cleared by RestartWithAuth().
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpAuth::Target pending_auth_target_;
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CompletionCallback io_callback_;
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CompletionCallback callback_;
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  HttpNetworkSession* session_;
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BoundNetLog net_log_;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const HttpRequestInfo* request_;
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  RequestPriority priority_;
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpResponseInfo response_;
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |proxy_info_| is the ProxyInfo used by the HttpStreamRequest.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ProxyInfo proxy_info_;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<HttpStreamRequest> stream_request_;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<HttpStreamBase> stream_;
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if we've validated the headers that the stream parser has returned.
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool headers_valid_;
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if we've logged the time of the first response byte.  Used to
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prevent logging across authentication activity where we see multiple
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // responses.
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool logged_response_time_;
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SSLConfig server_ssl_config_;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SSLConfig proxy_ssl_config_;
291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // fallback_error_code contains the error code that caused the last TLS
292a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // fallback. If the fallback connection results in
293a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // ERR_SSL_INAPPROPRIATE_FALLBACK (i.e. the server indicated that the
294a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // fallback should not have been needed) then we use this value to return the
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // original error that triggered the fallback.
296a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  int fallback_error_code_;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HttpRequestHeaders request_headers_;
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The size in bytes of the buffer we use to drain the response body that
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we want to throw away.  The response body is typically a small error
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // page just a few hundred bytes long.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kDrainBodyBufferSize = 1024;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // User buffer and length passed to the Read method.
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<IOBuffer> read_buf_;
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int read_buf_len_;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The time the Start method was called.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::Time start_time_;
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // When the transaction started / finished sending the request, including
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the body, if present.
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeTicks send_start_time_;
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeTicks send_end_time_;
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The next state in the state machine.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  State next_state_;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True when the tunnel is in the process of being established - we can't
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // read from the socket until the tunnel is done.
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool establishing_tunnel_;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
324f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The helper object to use to create WebSocketHandshakeStreamBase
325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // objects. Only relevant when establishing a WebSocket connection.
326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  WebSocketHandshakeStreamBase::CreateHelper*
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      websocket_handshake_stream_base_create_helper_;
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HttpNetworkTransaction);
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NET_HTTP_HTTP_NETWORK_TRANSACTION_H_
335