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