xref: /aosp_15_r20/external/cronet/net/http/http_stream.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 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 // HttpStream provides an abstraction for a basic http streams, SPDY, and QUIC.
6*6777b538SAndroid Build Coastguard Worker // The HttpStream subtype is expected to manage the underlying transport
7*6777b538SAndroid Build Coastguard Worker // appropriately.  For example, a basic http stream will return the transport
8*6777b538SAndroid Build Coastguard Worker // socket to the pool for reuse.  SPDY streams on the other hand leave the
9*6777b538SAndroid Build Coastguard Worker // transport socket management to the SpdySession.
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #ifndef NET_HTTP_HTTP_STREAM_H_
12*6777b538SAndroid Build Coastguard Worker #define NET_HTTP_HTTP_STREAM_H_
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker #include <memory>
17*6777b538SAndroid Build Coastguard Worker #include <optional>
18*6777b538SAndroid Build Coastguard Worker #include <set>
19*6777b538SAndroid Build Coastguard Worker #include <string_view>
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker #include "net/base/completion_once_callback.h"
22*6777b538SAndroid Build Coastguard Worker #include "net/base/idempotency.h"
23*6777b538SAndroid Build Coastguard Worker #include "net/base/net_error_details.h"
24*6777b538SAndroid Build Coastguard Worker #include "net/base/net_errors.h"
25*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
26*6777b538SAndroid Build Coastguard Worker #include "net/base/request_priority.h"
27*6777b538SAndroid Build Coastguard Worker #include "net/http/http_raw_request_headers.h"
28*6777b538SAndroid Build Coastguard Worker #include "net/third_party/quiche/src/quiche/quic/core/quic_error_codes.h"
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker namespace net {
31*6777b538SAndroid Build Coastguard Worker 
32*6777b538SAndroid Build Coastguard Worker struct AlternativeService;
33*6777b538SAndroid Build Coastguard Worker class HttpNetworkSession;
34*6777b538SAndroid Build Coastguard Worker class HttpRequestHeaders;
35*6777b538SAndroid Build Coastguard Worker struct HttpRequestInfo;
36*6777b538SAndroid Build Coastguard Worker class HttpResponseInfo;
37*6777b538SAndroid Build Coastguard Worker class IOBuffer;
38*6777b538SAndroid Build Coastguard Worker class IPEndPoint;
39*6777b538SAndroid Build Coastguard Worker struct LoadTimingInfo;
40*6777b538SAndroid Build Coastguard Worker class NetLogWithSource;
41*6777b538SAndroid Build Coastguard Worker class SSLInfo;
42*6777b538SAndroid Build Coastguard Worker 
43*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE HttpStream {
44*6777b538SAndroid Build Coastguard Worker  public:
45*6777b538SAndroid Build Coastguard Worker   HttpStream() = default;
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker   HttpStream(const HttpStream&) = delete;
48*6777b538SAndroid Build Coastguard Worker   HttpStream& operator=(const HttpStream&) = delete;
49*6777b538SAndroid Build Coastguard Worker 
50*6777b538SAndroid Build Coastguard Worker   virtual ~HttpStream() = default;
51*6777b538SAndroid Build Coastguard Worker 
52*6777b538SAndroid Build Coastguard Worker   // Registers the HTTP request for the stream.  Must be called before calling
53*6777b538SAndroid Build Coastguard Worker   // InitializeStream().  Separating the registration of the request from the
54*6777b538SAndroid Build Coastguard Worker   // initialization of the stream allows the connection callback to run prior
55*6777b538SAndroid Build Coastguard Worker   // to stream initialization.
56*6777b538SAndroid Build Coastguard Worker   //
57*6777b538SAndroid Build Coastguard Worker   // The consumer should ensure that request_info points to a valid non-null
58*6777b538SAndroid Build Coastguard Worker   // value till final response headers are received; after that point, the
59*6777b538SAndroid Build Coastguard Worker   // HttpStream will not access |*request_info| and it may be deleted.
60*6777b538SAndroid Build Coastguard Worker   virtual void RegisterRequest(const HttpRequestInfo* request_info) = 0;
61*6777b538SAndroid Build Coastguard Worker 
62*6777b538SAndroid Build Coastguard Worker   // Initializes the stream.  Must be called before calling SendRequest().
63*6777b538SAndroid Build Coastguard Worker   // If |can_send_early| is true, this stream may send data early without
64*6777b538SAndroid Build Coastguard Worker   // confirming the handshake if this is a resumption of a previously
65*6777b538SAndroid Build Coastguard Worker   // established connection.  Returns a net error code, possibly ERR_IO_PENDING.
66*6777b538SAndroid Build Coastguard Worker   virtual int InitializeStream(bool can_send_early,
67*6777b538SAndroid Build Coastguard Worker                                RequestPriority priority,
68*6777b538SAndroid Build Coastguard Worker                                const NetLogWithSource& net_log,
69*6777b538SAndroid Build Coastguard Worker                                CompletionOnceCallback callback) = 0;
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker   // Writes the headers and uploads body data to the underlying socket.
72*6777b538SAndroid Build Coastguard Worker   // ERR_IO_PENDING is returned if the operation could not be completed
73*6777b538SAndroid Build Coastguard Worker   // synchronously, in which case the result will be passed to the callback
74*6777b538SAndroid Build Coastguard Worker   // when available. Returns OK on success.
75*6777b538SAndroid Build Coastguard Worker   //
76*6777b538SAndroid Build Coastguard Worker   // Some fields in |response| may be filled by this method, but it will not
77*6777b538SAndroid Build Coastguard Worker   // contain complete information until ReadResponseHeaders returns.
78*6777b538SAndroid Build Coastguard Worker   //
79*6777b538SAndroid Build Coastguard Worker   // |response| must remain valid until all sets of headers has been read, or
80*6777b538SAndroid Build Coastguard Worker   // the HttpStream is destroyed. There's typically only one set of
81*6777b538SAndroid Build Coastguard Worker   // headers, except in the case of 1xx responses (See ReadResponseHeaders).
82*6777b538SAndroid Build Coastguard Worker   virtual int SendRequest(const HttpRequestHeaders& request_headers,
83*6777b538SAndroid Build Coastguard Worker                           HttpResponseInfo* response,
84*6777b538SAndroid Build Coastguard Worker                           CompletionOnceCallback callback) = 0;
85*6777b538SAndroid Build Coastguard Worker 
86*6777b538SAndroid Build Coastguard Worker   // Reads from the underlying socket until the next set of response headers
87*6777b538SAndroid Build Coastguard Worker   // have been completely received. Normally this is called once per request,
88*6777b538SAndroid Build Coastguard Worker   // however it may be called again in the event of a 1xx response to read the
89*6777b538SAndroid Build Coastguard Worker   // next set of headers.
90*6777b538SAndroid Build Coastguard Worker   //
91*6777b538SAndroid Build Coastguard Worker   // ERR_IO_PENDING is returned if the operation could not be completed
92*6777b538SAndroid Build Coastguard Worker   // synchronously, in which case the result will be passed to the callback when
93*6777b538SAndroid Build Coastguard Worker   // available. Returns OK on success. The response headers are available in
94*6777b538SAndroid Build Coastguard Worker   // the HttpResponseInfo passed in the original call to SendRequest.
95*6777b538SAndroid Build Coastguard Worker   virtual int ReadResponseHeaders(CompletionOnceCallback callback) = 0;
96*6777b538SAndroid Build Coastguard Worker 
97*6777b538SAndroid Build Coastguard Worker   // Reads response body data, up to |buf_len| bytes. |buf_len| should be a
98*6777b538SAndroid Build Coastguard Worker   // reasonable size (<2MB). The number of bytes read is returned, or an
99*6777b538SAndroid Build Coastguard Worker   // error is returned upon failure.  0 indicates that the request has been
100*6777b538SAndroid Build Coastguard Worker   // fully satisfied and there is no more data to read.
101*6777b538SAndroid Build Coastguard Worker   // ERR_CONNECTION_CLOSED is returned when the connection has been closed
102*6777b538SAndroid Build Coastguard Worker   // prematurely.  ERR_IO_PENDING is returned if the operation could not be
103*6777b538SAndroid Build Coastguard Worker   // completed synchronously, in which case the result will be passed to the
104*6777b538SAndroid Build Coastguard Worker   // callback when available. If the operation is not completed immediately,
105*6777b538SAndroid Build Coastguard Worker   // the socket acquires a reference to the provided buffer until the callback
106*6777b538SAndroid Build Coastguard Worker   // is invoked or the socket is destroyed.
107*6777b538SAndroid Build Coastguard Worker   virtual int ReadResponseBody(IOBuffer* buf,
108*6777b538SAndroid Build Coastguard Worker                                int buf_len,
109*6777b538SAndroid Build Coastguard Worker                                CompletionOnceCallback callback) = 0;
110*6777b538SAndroid Build Coastguard Worker 
111*6777b538SAndroid Build Coastguard Worker   // Closes the stream.
112*6777b538SAndroid Build Coastguard Worker   // |not_reusable| indicates if the stream can be used for further requests.
113*6777b538SAndroid Build Coastguard Worker   // In the case of HTTP, where we re-use the byte-stream (e.g. the connection)
114*6777b538SAndroid Build Coastguard Worker   // this means we need to close the connection; in the case of SPDY, where the
115*6777b538SAndroid Build Coastguard Worker   // underlying stream is never reused, it has no effect.
116*6777b538SAndroid Build Coastguard Worker   // TODO(mmenke): We should fold the |not_reusable| flag into the stream
117*6777b538SAndroid Build Coastguard Worker   //               implementation itself so that the caller does not need to
118*6777b538SAndroid Build Coastguard Worker   //               pass it at all.  Ideally we'd be able to remove
119*6777b538SAndroid Build Coastguard Worker   //               CanReuseConnection() and IsResponseBodyComplete().
120*6777b538SAndroid Build Coastguard Worker   // TODO(mmenke): We should try and merge Drain() into this method as well.
121*6777b538SAndroid Build Coastguard Worker   virtual void Close(bool not_reusable) = 0;
122*6777b538SAndroid Build Coastguard Worker 
123*6777b538SAndroid Build Coastguard Worker   // Indicates if the response body has been completely read.
124*6777b538SAndroid Build Coastguard Worker   virtual bool IsResponseBodyComplete() const = 0;
125*6777b538SAndroid Build Coastguard Worker 
126*6777b538SAndroid Build Coastguard Worker   // A stream exists on top of a connection.  If the connection has been used
127*6777b538SAndroid Build Coastguard Worker   // to successfully exchange data in the past, error handling for the
128*6777b538SAndroid Build Coastguard Worker   // stream is done differently.  This method returns true if the underlying
129*6777b538SAndroid Build Coastguard Worker   // connection is reused or has been connected and idle for some time.
130*6777b538SAndroid Build Coastguard Worker   virtual bool IsConnectionReused() const = 0;
131*6777b538SAndroid Build Coastguard Worker   // TODO(mmenke): We should fold this into RenewStreamForAuth(), and make that
132*6777b538SAndroid Build Coastguard Worker   //    method drain the stream as well, if needed (And return asynchronously).
133*6777b538SAndroid Build Coastguard Worker   virtual void SetConnectionReused() = 0;
134*6777b538SAndroid Build Coastguard Worker 
135*6777b538SAndroid Build Coastguard Worker   // Checks whether the underlying connection can be reused.  The stream's
136*6777b538SAndroid Build Coastguard Worker   // connection can be reused if the response headers allow for it, the socket
137*6777b538SAndroid Build Coastguard Worker   // is still connected, and the stream exclusively owns the underlying
138*6777b538SAndroid Build Coastguard Worker   // connection.  SPDY and QUIC streams don't own their own connections, so
139*6777b538SAndroid Build Coastguard Worker   // always return false.
140*6777b538SAndroid Build Coastguard Worker   virtual bool CanReuseConnection() const = 0;
141*6777b538SAndroid Build Coastguard Worker 
142*6777b538SAndroid Build Coastguard Worker   // Get the total number of bytes received from network for this stream.
143*6777b538SAndroid Build Coastguard Worker   virtual int64_t GetTotalReceivedBytes() const = 0;
144*6777b538SAndroid Build Coastguard Worker 
145*6777b538SAndroid Build Coastguard Worker   // Get the total number of bytes sent over the network for this stream.
146*6777b538SAndroid Build Coastguard Worker   virtual int64_t GetTotalSentBytes() const = 0;
147*6777b538SAndroid Build Coastguard Worker 
148*6777b538SAndroid Build Coastguard Worker   // Populates the connection establishment part of |load_timing_info|, and
149*6777b538SAndroid Build Coastguard Worker   // socket ID.  |load_timing_info| must have all null times when called.
150*6777b538SAndroid Build Coastguard Worker   // Returns false and does nothing if there is no underlying connection, either
151*6777b538SAndroid Build Coastguard Worker   // because one has yet to be assigned to the stream, or because the underlying
152*6777b538SAndroid Build Coastguard Worker   // socket has been closed.
153*6777b538SAndroid Build Coastguard Worker   //
154*6777b538SAndroid Build Coastguard Worker   // In practice, this means that this function will always succeed any time
155*6777b538SAndroid Build Coastguard Worker   // between when the full headers have been received and the stream has been
156*6777b538SAndroid Build Coastguard Worker   // closed.
157*6777b538SAndroid Build Coastguard Worker   virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0;
158*6777b538SAndroid Build Coastguard Worker 
159*6777b538SAndroid Build Coastguard Worker   // Get the SSLInfo associated with this stream's connection.  This should
160*6777b538SAndroid Build Coastguard Worker   // only be called for streams over SSL sockets, otherwise the behavior is
161*6777b538SAndroid Build Coastguard Worker   // undefined.
162*6777b538SAndroid Build Coastguard Worker   virtual void GetSSLInfo(SSLInfo* ssl_info) = 0;
163*6777b538SAndroid Build Coastguard Worker 
164*6777b538SAndroid Build Coastguard Worker   // Returns true and populates |alternative_service| if an alternative service
165*6777b538SAndroid Build Coastguard Worker   // was used to for this stream. Otherwise returns false.
166*6777b538SAndroid Build Coastguard Worker   virtual bool GetAlternativeService(
167*6777b538SAndroid Build Coastguard Worker       AlternativeService* alternative_service) const = 0;
168*6777b538SAndroid Build Coastguard Worker 
169*6777b538SAndroid Build Coastguard Worker   // Gets the remote endpoint of the socket that the HTTP stream is using, if
170*6777b538SAndroid Build Coastguard Worker   // any. Returns OK and fills in |endpoint| if it is available; returns an
171*6777b538SAndroid Build Coastguard Worker   // error and does not modify |endpoint| otherwise.
172*6777b538SAndroid Build Coastguard Worker   virtual int GetRemoteEndpoint(IPEndPoint* endpoint) = 0;
173*6777b538SAndroid Build Coastguard Worker 
174*6777b538SAndroid Build Coastguard Worker   // In the case of an HTTP error or redirect, flush the response body (usually
175*6777b538SAndroid Build Coastguard Worker   // a simple error or "this page has moved") so that we can re-use the
176*6777b538SAndroid Build Coastguard Worker   // underlying connection. This stream is responsible for deleting itself when
177*6777b538SAndroid Build Coastguard Worker   // draining is complete.
178*6777b538SAndroid Build Coastguard Worker   virtual void Drain(HttpNetworkSession* session) = 0;
179*6777b538SAndroid Build Coastguard Worker 
180*6777b538SAndroid Build Coastguard Worker   // Get the network error details this stream is encountering.
181*6777b538SAndroid Build Coastguard Worker   // Fills in |details| if it is available; leaves |details| unchanged if it
182*6777b538SAndroid Build Coastguard Worker   // is unavailable.
183*6777b538SAndroid Build Coastguard Worker   virtual void PopulateNetErrorDetails(NetErrorDetails* details) = 0;
184*6777b538SAndroid Build Coastguard Worker 
185*6777b538SAndroid Build Coastguard Worker   // Called when the priority of the parent transaction changes.
186*6777b538SAndroid Build Coastguard Worker   virtual void SetPriority(RequestPriority priority) = 0;
187*6777b538SAndroid Build Coastguard Worker 
188*6777b538SAndroid Build Coastguard Worker   // Returns a new (not initialized) stream using the same underlying
189*6777b538SAndroid Build Coastguard Worker   // connection and invalidates the old stream - no further methods should be
190*6777b538SAndroid Build Coastguard Worker   // called on the old stream.  The caller should ensure that the response body
191*6777b538SAndroid Build Coastguard Worker   // from the previous request is drained before calling this method.  If the
192*6777b538SAndroid Build Coastguard Worker   // subclass does not support renewing the stream, NULL is returned.
193*6777b538SAndroid Build Coastguard Worker   virtual std::unique_ptr<HttpStream> RenewStreamForAuth() = 0;
194*6777b538SAndroid Build Coastguard Worker 
195*6777b538SAndroid Build Coastguard Worker   virtual void SetRequestHeadersCallback(RequestHeadersCallback callback) = 0;
196*6777b538SAndroid Build Coastguard Worker 
197*6777b538SAndroid Build Coastguard Worker   // Set the idempotency of the request. No-op by default.
SetRequestIdempotency(Idempotency idempotency)198*6777b538SAndroid Build Coastguard Worker   virtual void SetRequestIdempotency(Idempotency idempotency) {}
199*6777b538SAndroid Build Coastguard Worker 
200*6777b538SAndroid Build Coastguard Worker   // Retrieves any DNS aliases for the remote endpoint. Includes all known
201*6777b538SAndroid Build Coastguard Worker   // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for
202*6777b538SAndroid Build Coastguard Worker   // the connection, in no particular order.
203*6777b538SAndroid Build Coastguard Worker   virtual const std::set<std::string>& GetDnsAliases() const = 0;
204*6777b538SAndroid Build Coastguard Worker 
205*6777b538SAndroid Build Coastguard Worker   // The value in the ACCEPT_CH frame received during TLS handshake via the
206*6777b538SAndroid Build Coastguard Worker   // ALPS extension, or the empty string if the server did not send one.  Unlike
207*6777b538SAndroid Build Coastguard Worker   // Accept-CH header fields received in HTTP responses, this value is available
208*6777b538SAndroid Build Coastguard Worker   // before any requests are made.
209*6777b538SAndroid Build Coastguard Worker   virtual std::string_view GetAcceptChViaAlps() const = 0;
210*6777b538SAndroid Build Coastguard Worker 
211*6777b538SAndroid Build Coastguard Worker   // Represents detailed QUIC errors returned by GetQuicErrorDetails().
212*6777b538SAndroid Build Coastguard Worker   struct QuicErrorDetails {
213*6777b538SAndroid Build Coastguard Worker     // Internal connection error of the stream.
214*6777b538SAndroid Build Coastguard Worker     quic::QuicErrorCode connection_error = quic::QUIC_NO_ERROR;
215*6777b538SAndroid Build Coastguard Worker     // Internal stream error of the stream.
216*6777b538SAndroid Build Coastguard Worker     quic::QuicRstStreamErrorCode stream_error = quic::QUIC_STREAM_NO_ERROR;
217*6777b538SAndroid Build Coastguard Worker     // Connection error sent or received on the wire protocol.
218*6777b538SAndroid Build Coastguard Worker     uint64_t connection_wire_error = 0;
219*6777b538SAndroid Build Coastguard Worker     // Application error sent or received on the wire protocol.
220*6777b538SAndroid Build Coastguard Worker     uint64_t ietf_application_error = 0;
221*6777b538SAndroid Build Coastguard Worker   };
222*6777b538SAndroid Build Coastguard Worker 
223*6777b538SAndroid Build Coastguard Worker   // If `this` is using a QUIC stream, returns error details of the QUIC stream.
224*6777b538SAndroid Build Coastguard Worker   // Otherwise returns nullopt. Detailed QUIC errors are only available after
225*6777b538SAndroid Build Coastguard Worker   // the stream has been initialized. Use PopulateNetErrorDetails() for errors
226*6777b538SAndroid Build Coastguard Worker   // that happened during the initialization.
227*6777b538SAndroid Build Coastguard Worker   virtual std::optional<QuicErrorDetails> GetQuicErrorDetails() const;
228*6777b538SAndroid Build Coastguard Worker };
229*6777b538SAndroid Build Coastguard Worker 
230*6777b538SAndroid Build Coastguard Worker }  // namespace net
231*6777b538SAndroid Build Coastguard Worker 
232*6777b538SAndroid Build Coastguard Worker #endif  // NET_HTTP_HTTP_STREAM_H_
233