Home | History | Annotate | Download | only in http
      1 // Copyright (c) 2011 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 #ifndef NET_HTTP_HTTP_TRANSACTION_H_
      6 #define NET_HTTP_HTTP_TRANSACTION_H_
      7 
      8 #include "net/base/completion_callback.h"
      9 #include "net/base/load_states.h"
     10 #include "net/base/net_export.h"
     11 #include "net/base/request_priority.h"
     12 #include "net/base/upload_progress.h"
     13 #include "net/websockets/websocket_handshake_stream_base.h"
     14 
     15 namespace net {
     16 
     17 class AuthCredentials;
     18 class BoundNetLog;
     19 class HttpRequestHeaders;
     20 struct HttpRequestInfo;
     21 class HttpResponseInfo;
     22 class IOBuffer;
     23 struct LoadTimingInfo;
     24 class QuicServerInfo;
     25 class X509Certificate;
     26 
     27 // Represents a single HTTP transaction (i.e., a single request/response pair).
     28 // HTTP redirects are not followed and authentication challenges are not
     29 // answered.  Cookies are assumed to be managed by the caller.
     30 class NET_EXPORT_PRIVATE HttpTransaction {
     31  public:
     32   // If |*defer| is set to true, the transaction will wait until
     33   // ResumeNetworkStart is called before establishing a connection.
     34   typedef base::Callback<void(bool* defer)> BeforeNetworkStartCallback;
     35 
     36   // Stops any pending IO and destroys the transaction object.
     37   virtual ~HttpTransaction() {}
     38 
     39   // Starts the HTTP transaction (i.e., sends the HTTP request).
     40   //
     41   // Returns OK if the transaction could be started synchronously, which means
     42   // that the request was served from the cache.  ERR_IO_PENDING is returned to
     43   // indicate that the CompletionCallback will be notified once response info is
     44   // available or if an IO error occurs.  Any other return value indicates that
     45   // the transaction could not be started.
     46   //
     47   // Regardless of the return value, the caller is expected to keep the
     48   // request_info object alive until Destroy is called on the transaction.
     49   //
     50   // NOTE: The transaction is not responsible for deleting the callback object.
     51   //
     52   // Profiling information for the request is saved to |net_log| if non-NULL.
     53   virtual int Start(const HttpRequestInfo* request_info,
     54                     const CompletionCallback& callback,
     55                     const BoundNetLog& net_log) = 0;
     56 
     57   // Restarts the HTTP transaction, ignoring the last error.  This call can
     58   // only be made after a call to Start (or RestartIgnoringLastError) failed.
     59   // Once Read has been called, this method cannot be called.  This method is
     60   // used, for example, to continue past various SSL related errors.
     61   //
     62   // Not all errors can be ignored using this method.  See error code
     63   // descriptions for details about errors that can be ignored.
     64   //
     65   // NOTE: The transaction is not responsible for deleting the callback object.
     66   //
     67   virtual int RestartIgnoringLastError(const CompletionCallback& callback) = 0;
     68 
     69   // Restarts the HTTP transaction with a client certificate.
     70   virtual int RestartWithCertificate(X509Certificate* client_cert,
     71                                      const CompletionCallback& callback) = 0;
     72 
     73   // Restarts the HTTP transaction with authentication credentials.
     74   virtual int RestartWithAuth(const AuthCredentials& credentials,
     75                               const CompletionCallback& callback) = 0;
     76 
     77   // Returns true if auth is ready to be continued. Callers should check
     78   // this value anytime Start() completes: if it is true, the transaction
     79   // can be resumed with RestartWithAuth(L"", L"", callback) to resume
     80   // the automatic auth exchange. This notification gives the caller a
     81   // chance to process the response headers from all of the intermediate
     82   // restarts needed for authentication.
     83   virtual bool IsReadyToRestartForAuth() = 0;
     84 
     85   // Once response info is available for the transaction, response data may be
     86   // read by calling this method.
     87   //
     88   // Response data is copied into the given buffer and the number of bytes
     89   // copied is returned.  ERR_IO_PENDING is returned if response data is not
     90   // yet available.  The CompletionCallback is notified when the data copy
     91   // completes, and it is passed the number of bytes that were successfully
     92   // copied.  Or, if a read error occurs, the CompletionCallback is notified of
     93   // the error.  Any other negative return value indicates that the transaction
     94   // could not be read.
     95   //
     96   // NOTE: The transaction is not responsible for deleting the callback object.
     97   // If the operation is not completed immediately, the transaction must acquire
     98   // a reference to the provided buffer.
     99   //
    100   virtual int Read(IOBuffer* buf, int buf_len,
    101                    const CompletionCallback& callback) = 0;
    102 
    103   // Stops further caching of this request by the HTTP cache, if there is any.
    104   virtual void StopCaching() = 0;
    105 
    106   // Gets the full request headers sent to the server.  This is guaranteed to
    107   // work only if Start returns success and the underlying transaction supports
    108   // it.  (Right now, this is only network transactions, not cache ones.)
    109   //
    110   // Returns true and overwrites headers if it can get the request headers;
    111   // otherwise, returns false and does not modify headers.
    112   virtual bool GetFullRequestHeaders(HttpRequestHeaders* headers) const = 0;
    113 
    114   // Get the number of bytes received from network.
    115   virtual int64 GetTotalReceivedBytes() const = 0;
    116 
    117   // Called to tell the transaction that we have successfully reached the end
    118   // of the stream. This is equivalent to performing an extra Read() at the end
    119   // that should return 0 bytes. This method should not be called if the
    120   // transaction is busy processing a previous operation (like a pending Read).
    121   //
    122   // DoneReading may also be called before the first Read() to notify that the
    123   // entire response body is to be ignored (e.g., in a redirect).
    124   virtual void DoneReading() = 0;
    125 
    126   // Returns the response info for this transaction or NULL if the response
    127   // info is not available.
    128   virtual const HttpResponseInfo* GetResponseInfo() const = 0;
    129 
    130   // Returns the load state for this transaction.
    131   virtual LoadState GetLoadState() const = 0;
    132 
    133   // Returns the upload progress in bytes.  If there is no upload data,
    134   // zero will be returned.  This does not include the request headers.
    135   virtual UploadProgress GetUploadProgress() const = 0;
    136 
    137   // SetQuicServerInfo sets a object which reads and writes public information
    138   // about a QUIC server.
    139   virtual void SetQuicServerInfo(QuicServerInfo* quic_server_info) = 0;
    140 
    141   // Populates all of load timing, except for request start times and receive
    142   // headers time.
    143   // |load_timing_info| must have all null times when called.  Returns false and
    144   // does not modify |load_timing_info| if there's no timing information to
    145   // provide.
    146   virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
    147 
    148   // Called when the priority of the parent job changes.
    149   virtual void SetPriority(RequestPriority priority) = 0;
    150 
    151   // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
    152   // request.  Only relevant to WebSocket transactions. Must be called before
    153   // Start(). Ownership of |create_helper| remains with the caller.
    154   virtual void SetWebSocketHandshakeStreamCreateHelper(
    155       WebSocketHandshakeStreamBase::CreateHelper* create_helper) = 0;
    156 
    157   // Set the callback to receive notification just before network use.
    158   virtual void SetBeforeNetworkStartCallback(
    159       const BeforeNetworkStartCallback& callback) = 0;
    160 
    161   // Resumes the transaction after being deferred.
    162   virtual int ResumeNetworkStart() = 0;
    163 };
    164 
    165 }  // namespace net
    166 
    167 #endif  // NET_HTTP_HTTP_TRANSACTION_H_
    168