http_transaction.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef NET_HTTP_HTTP_TRANSACTION_H_
  5. #define NET_HTTP_HTTP_TRANSACTION_H_
  6. #include <stdint.h>
  7. #include "net/base/completion_once_callback.h"
  8. #include "net/base/completion_repeating_callback.h"
  9. #include "net/base/load_states.h"
  10. #include "net/base/net_error_details.h"
  11. #include "net/base/net_export.h"
  12. #include "net/base/request_priority.h"
  13. #include "net/base/upload_progress.h"
  14. #include "net/http/http_raw_request_headers.h"
  15. #include "net/http/http_response_headers.h"
  16. #include "net/socket/connection_attempts.h"
  17. #include "net/websockets/websocket_handshake_stream_base.h"
  18. namespace net {
  19. class AuthCredentials;
  20. struct HttpRequestInfo;
  21. class HttpResponseInfo;
  22. class IOBuffer;
  23. struct TransportInfo;
  24. struct LoadTimingInfo;
  25. class NetLogWithSource;
  26. class QuicServerInfo;
  27. class SSLPrivateKey;
  28. class X509Certificate;
  29. // Represents a single HTTP transaction (i.e., a single request/response pair).
  30. // HTTP redirects are not followed and authentication challenges are not
  31. // answered. Cookies are assumed to be managed by the caller.
  32. class NET_EXPORT_PRIVATE HttpTransaction {
  33. public:
  34. // If |*defer| is set to true, the transaction will wait until
  35. // ResumeNetworkStart is called before establishing a connection.
  36. using BeforeNetworkStartCallback = base::OnceCallback<void(bool* defer)>;
  37. // Called each time a connection is obtained, before any data is sent.
  38. //
  39. // |info| describes the newly-obtained connection.
  40. //
  41. // This can be called multiple times for a single transaction, in the case of
  42. // retries, auth challenges, and split range requests.
  43. //
  44. // If this callback returns an error, the transaction fails with that error.
  45. // Otherwise the transaction continues unimpeded.
  46. // Must not return ERR_IO_PENDING.
  47. //
  48. // TODO(crbug.com/986744): Fix handling of OnConnected() when proxy
  49. // authentication is required. We should notify this callback that a
  50. // connection was established, even though the stream might not be ready for
  51. // us to send data through it.
  52. using ConnectedCallback =
  53. base::RepeatingCallback<int(const TransportInfo& info,
  54. CompletionOnceCallback callback)>;
  55. // Stops any pending IO and destroys the transaction object.
  56. virtual ~HttpTransaction() = default;
  57. // Starts the HTTP transaction (i.e., sends the HTTP request).
  58. //
  59. // TODO(crbug.com/723786) The consumer should ensure that request_info points
  60. // to a valid value till final response headers are received; after that
  61. // point, the HttpTransaction will not access |*request_info| and it may be
  62. // deleted.
  63. //
  64. // Returns OK if the transaction could be started synchronously, which means
  65. // that the request was served from the cache. ERR_IO_PENDING is returned to
  66. // indicate that |callback| will be notified once response info is available
  67. // or if an IO error occurs. Any other return value indicates that the
  68. // transaction could not be started.
  69. //
  70. // Regardless of the return value, the caller is expected to keep the
  71. // request_info object alive until Destroy is called on the transaction.
  72. //
  73. // NOTE: The transaction is not responsible for deleting the callback object.
  74. //
  75. // Profiling information for the request is saved to |net_log| if non-NULL.
  76. virtual int Start(const HttpRequestInfo* request_info,
  77. CompletionOnceCallback callback,
  78. const NetLogWithSource& net_log) = 0;
  79. // Restarts the HTTP transaction, ignoring the last error. This call can
  80. // only be made after a call to Start (or RestartIgnoringLastError) failed.
  81. // Once Read has been called, this method cannot be called. This method is
  82. // used, for example, to continue past various SSL related errors.
  83. //
  84. // Not all errors can be ignored using this method. See error code
  85. // descriptions for details about errors that can be ignored.
  86. //
  87. // NOTE: The transaction is not responsible for deleting the callback object.
  88. //
  89. virtual int RestartIgnoringLastError(CompletionOnceCallback callback) = 0;
  90. // Restarts the HTTP transaction with a client certificate.
  91. virtual int RestartWithCertificate(
  92. scoped_refptr<X509Certificate> client_cert,
  93. scoped_refptr<SSLPrivateKey> client_private_key,
  94. CompletionOnceCallback callback) = 0;
  95. // Restarts the HTTP transaction with authentication credentials.
  96. virtual int RestartWithAuth(const AuthCredentials& credentials,
  97. CompletionOnceCallback callback) = 0;
  98. // Returns true if auth is ready to be continued. Callers should check
  99. // this value anytime Start() completes: if it is true, the transaction
  100. // can be resumed with RestartWithAuth(L"", L"", callback) to resume
  101. // the automatic auth exchange. This notification gives the caller a
  102. // chance to process the response headers from all of the intermediate
  103. // restarts needed for authentication.
  104. virtual bool IsReadyToRestartForAuth() = 0;
  105. // Once response info is available for the transaction, response data may be
  106. // read by calling this method.
  107. //
  108. // Response data is copied into the given buffer and the number of bytes
  109. // copied is returned. ERR_IO_PENDING is returned if response data is not yet
  110. // available. |callback| is notified when the data copy completes, and it is
  111. // passed the number of bytes that were successfully copied. Or, if a read
  112. // error occurs, |callback| is notified of the error. Any other negative
  113. // return value indicates that the transaction could not be read.
  114. //
  115. // NOTE: The transaction is not responsible for deleting the callback object.
  116. // If the operation is not completed immediately, the transaction must acquire
  117. // a reference to the provided buffer.
  118. //
  119. virtual int Read(IOBuffer* buf,
  120. int buf_len,
  121. CompletionOnceCallback callback) = 0;
  122. // Stops further caching of this request by the HTTP cache, if there is any.
  123. // Note that this is merely a hint to the transaction which it may choose to
  124. // ignore.
  125. virtual void StopCaching() = 0;
  126. // Get the number of bytes received from network.
  127. virtual int64_t GetTotalReceivedBytes() const = 0;
  128. // Get the number of bytes sent over the network.
  129. virtual int64_t GetTotalSentBytes() const = 0;
  130. // Called to tell the transaction that we have successfully reached the end
  131. // of the stream. This is equivalent to performing an extra Read() at the end
  132. // that should return 0 bytes. This method should not be called if the
  133. // transaction is busy processing a previous operation (like a pending Read).
  134. //
  135. // DoneReading may also be called before the first Read() to notify that the
  136. // entire response body is to be ignored (e.g., in a redirect).
  137. virtual void DoneReading() = 0;
  138. // Returns the response info for this transaction. Must not be called until
  139. // |Start| completes.
  140. virtual const HttpResponseInfo* GetResponseInfo() const = 0;
  141. // Returns the load state for this transaction.
  142. virtual LoadState GetLoadState() const = 0;
  143. // SetQuicServerInfo sets a object which reads and writes public information
  144. // about a QUIC server.
  145. virtual void SetQuicServerInfo(QuicServerInfo* quic_server_info) = 0;
  146. // Populates all of load timing, except for request start times and receive
  147. // headers time.
  148. // |load_timing_info| must have all null times when called. Returns false and
  149. // does not modify |load_timing_info| if there's no timing information to
  150. // provide.
  151. virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
  152. // Gets the remote endpoint of the socket that the transaction's underlying
  153. // stream is using or did use, if any. Returns true and fills in |endpoint|
  154. // if it is available; returns false and leaves |endpoint| unchanged if it is
  155. // unavailable.
  156. virtual bool GetRemoteEndpoint(IPEndPoint* endpoint) const = 0;
  157. // Populates network error details for this transaction.
  158. virtual void PopulateNetErrorDetails(NetErrorDetails* details) const = 0;
  159. // Called when the priority of the parent job changes.
  160. virtual void SetPriority(RequestPriority priority) = 0;
  161. // Set the WebSocketHandshakeStreamBase::CreateHelper to be used for the
  162. // request. Only relevant to WebSocket transactions. Must be called before
  163. // Start(). Ownership of |create_helper| remains with the caller.
  164. virtual void SetWebSocketHandshakeStreamCreateHelper(
  165. WebSocketHandshakeStreamBase::CreateHelper* create_helper) = 0;
  166. // Sets the callback to receive notification just before network use.
  167. virtual void SetBeforeNetworkStartCallback(
  168. BeforeNetworkStartCallback callback) = 0;
  169. // Sets the callback to receive a notification upon connection.
  170. virtual void SetConnectedCallback(const ConnectedCallback& callback) = 0;
  171. virtual void SetRequestHeadersCallback(RequestHeadersCallback callback) = 0;
  172. virtual void SetEarlyResponseHeadersCallback(
  173. ResponseHeadersCallback callback) = 0;
  174. virtual void SetResponseHeadersCallback(ResponseHeadersCallback callback) = 0;
  175. // Resumes the transaction after being deferred.
  176. virtual int ResumeNetworkStart() = 0;
  177. virtual ConnectionAttempts GetConnectionAttempts() const = 0;
  178. // Configures the transaction to close the network connection, if any, on
  179. // destruction. Intended for cases where keeping the socket alive may leak
  180. // data. Does not immediately close the socket. If multiple transactions are
  181. // using the same socket, only closes it once all transactions have completed.
  182. //
  183. // Does not close H2/H3 sessions, but does close H1 tunnels on top of H2/H3
  184. // sessions.
  185. //
  186. // Only applies to currently in-use connections. Does nothing after the last
  187. // byte of the response body has been read, as the connection is no longer in
  188. // use at that point.
  189. virtual void CloseConnectionOnDestruction() = 0;
  190. };
  191. } // namespace net
  192. #endif // NET_HTTP_HTTP_TRANSACTION_H_