xref: /aosp_15_r20/external/cronet/net/quic/quic_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 #ifndef NET_QUIC_QUIC_HTTP_STREAM_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_QUIC_QUIC_HTTP_STREAM_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include <set>
12*6777b538SAndroid Build Coastguard Worker #include <string>
13*6777b538SAndroid Build Coastguard Worker #include <string_view>
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker #include "base/memory/raw_ptr.h"
16*6777b538SAndroid Build Coastguard Worker #include "base/memory/weak_ptr.h"
17*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/completion_once_callback.h"
19*6777b538SAndroid Build Coastguard Worker #include "net/base/idempotency.h"
20*6777b538SAndroid Build Coastguard Worker #include "net/base/io_buffer.h"
21*6777b538SAndroid Build Coastguard Worker #include "net/base/load_timing_info.h"
22*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
23*6777b538SAndroid Build Coastguard Worker #include "net/http/http_response_info.h"
24*6777b538SAndroid Build Coastguard Worker #include "net/http/http_server_properties.h"
25*6777b538SAndroid Build Coastguard Worker #include "net/log/net_log_with_source.h"
26*6777b538SAndroid Build Coastguard Worker #include "net/quic/quic_chromium_client_session.h"
27*6777b538SAndroid Build Coastguard Worker #include "net/quic/quic_chromium_client_stream.h"
28*6777b538SAndroid Build Coastguard Worker #include "net/spdy/multiplexed_http_stream.h"
29*6777b538SAndroid Build Coastguard Worker #include "net/third_party/quiche/src/quiche/quic/core/quic_packets.h"
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker namespace net {
32*6777b538SAndroid Build Coastguard Worker 
33*6777b538SAndroid Build Coastguard Worker namespace test {
34*6777b538SAndroid Build Coastguard Worker class QuicHttpStreamPeer;
35*6777b538SAndroid Build Coastguard Worker }  // namespace test
36*6777b538SAndroid Build Coastguard Worker 
37*6777b538SAndroid Build Coastguard Worker // The QuicHttpStream is a QUIC-specific HttpStream subclass.  It holds a
38*6777b538SAndroid Build Coastguard Worker // handle of QuicChromiumClientStream which it uses to send and receive data.
39*6777b538SAndroid Build Coastguard Worker // The handle hides the details of the underlying stream's lifetime and can be
40*6777b538SAndroid Build Coastguard Worker // used even after the underlying stream is destroyed.
41*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE QuicHttpStream : public MultiplexedHttpStream {
42*6777b538SAndroid Build Coastguard Worker  public:
43*6777b538SAndroid Build Coastguard Worker   explicit QuicHttpStream(
44*6777b538SAndroid Build Coastguard Worker       std::unique_ptr<QuicChromiumClientSession::Handle> session,
45*6777b538SAndroid Build Coastguard Worker       std::set<std::string> dns_aliases);
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker   QuicHttpStream(const QuicHttpStream&) = delete;
48*6777b538SAndroid Build Coastguard Worker   QuicHttpStream& operator=(const QuicHttpStream&) = delete;
49*6777b538SAndroid Build Coastguard Worker 
50*6777b538SAndroid Build Coastguard Worker   ~QuicHttpStream() override;
51*6777b538SAndroid Build Coastguard Worker 
52*6777b538SAndroid Build Coastguard Worker   // HttpStream implementation.
53*6777b538SAndroid Build Coastguard Worker   void RegisterRequest(const HttpRequestInfo* request_info) override;
54*6777b538SAndroid Build Coastguard Worker   int InitializeStream(bool can_send_early,
55*6777b538SAndroid Build Coastguard Worker                        RequestPriority priority,
56*6777b538SAndroid Build Coastguard Worker                        const NetLogWithSource& net_log,
57*6777b538SAndroid Build Coastguard Worker                        CompletionOnceCallback callback) override;
58*6777b538SAndroid Build Coastguard Worker   int SendRequest(const HttpRequestHeaders& request_headers,
59*6777b538SAndroid Build Coastguard Worker                   HttpResponseInfo* response,
60*6777b538SAndroid Build Coastguard Worker                   CompletionOnceCallback callback) override;
61*6777b538SAndroid Build Coastguard Worker   int ReadResponseHeaders(CompletionOnceCallback callback) override;
62*6777b538SAndroid Build Coastguard Worker   int ReadResponseBody(IOBuffer* buf,
63*6777b538SAndroid Build Coastguard Worker                        int buf_len,
64*6777b538SAndroid Build Coastguard Worker                        CompletionOnceCallback callback) override;
65*6777b538SAndroid Build Coastguard Worker   void Close(bool not_reusable) override;
66*6777b538SAndroid Build Coastguard Worker   bool IsResponseBodyComplete() const override;
67*6777b538SAndroid Build Coastguard Worker   bool IsConnectionReused() const override;
68*6777b538SAndroid Build Coastguard Worker   int64_t GetTotalReceivedBytes() const override;
69*6777b538SAndroid Build Coastguard Worker   int64_t GetTotalSentBytes() const override;
70*6777b538SAndroid Build Coastguard Worker   bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override;
71*6777b538SAndroid Build Coastguard Worker   bool GetAlternativeService(
72*6777b538SAndroid Build Coastguard Worker       AlternativeService* alternative_service) const override;
73*6777b538SAndroid Build Coastguard Worker   void PopulateNetErrorDetails(NetErrorDetails* details) override;
74*6777b538SAndroid Build Coastguard Worker   void SetPriority(RequestPriority priority) override;
75*6777b538SAndroid Build Coastguard Worker   void SetRequestIdempotency(Idempotency idempotency) override;
76*6777b538SAndroid Build Coastguard Worker   const std::set<std::string>& GetDnsAliases() const override;
77*6777b538SAndroid Build Coastguard Worker   std::string_view GetAcceptChViaAlps() const override;
78*6777b538SAndroid Build Coastguard Worker   std::optional<QuicErrorDetails> GetQuicErrorDetails() const override;
79*6777b538SAndroid Build Coastguard Worker 
80*6777b538SAndroid Build Coastguard Worker   static HttpConnectionInfo ConnectionInfoFromQuicVersion(
81*6777b538SAndroid Build Coastguard Worker       quic::ParsedQuicVersion quic_version);
82*6777b538SAndroid Build Coastguard Worker 
83*6777b538SAndroid Build Coastguard Worker  private:
84*6777b538SAndroid Build Coastguard Worker   friend class test::QuicHttpStreamPeer;
85*6777b538SAndroid Build Coastguard Worker 
86*6777b538SAndroid Build Coastguard Worker   enum State {
87*6777b538SAndroid Build Coastguard Worker     STATE_NONE,
88*6777b538SAndroid Build Coastguard Worker     STATE_REQUEST_STREAM,
89*6777b538SAndroid Build Coastguard Worker     STATE_REQUEST_STREAM_COMPLETE,
90*6777b538SAndroid Build Coastguard Worker     STATE_SET_REQUEST_PRIORITY,
91*6777b538SAndroid Build Coastguard Worker     STATE_SEND_HEADERS,
92*6777b538SAndroid Build Coastguard Worker     STATE_SEND_HEADERS_COMPLETE,
93*6777b538SAndroid Build Coastguard Worker     STATE_READ_REQUEST_BODY,
94*6777b538SAndroid Build Coastguard Worker     STATE_READ_REQUEST_BODY_COMPLETE,
95*6777b538SAndroid Build Coastguard Worker     STATE_SEND_BODY,
96*6777b538SAndroid Build Coastguard Worker     STATE_SEND_BODY_COMPLETE,
97*6777b538SAndroid Build Coastguard Worker     STATE_OPEN,
98*6777b538SAndroid Build Coastguard Worker   };
99*6777b538SAndroid Build Coastguard Worker 
100*6777b538SAndroid Build Coastguard Worker   void OnIOComplete(int rv);
101*6777b538SAndroid Build Coastguard Worker   void DoCallback(int rv);
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   int DoLoop(int rv);
104*6777b538SAndroid Build Coastguard Worker   int DoRequestStream();
105*6777b538SAndroid Build Coastguard Worker   int DoRequestStreamComplete(int rv);
106*6777b538SAndroid Build Coastguard Worker   int DoSetRequestPriority();
107*6777b538SAndroid Build Coastguard Worker   int DoSendHeaders();
108*6777b538SAndroid Build Coastguard Worker   int DoSendHeadersComplete(int rv);
109*6777b538SAndroid Build Coastguard Worker   int DoReadRequestBody();
110*6777b538SAndroid Build Coastguard Worker   int DoReadRequestBodyComplete(int rv);
111*6777b538SAndroid Build Coastguard Worker   int DoSendBody();
112*6777b538SAndroid Build Coastguard Worker   int DoSendBodyComplete(int rv);
113*6777b538SAndroid Build Coastguard Worker 
114*6777b538SAndroid Build Coastguard Worker   void OnReadResponseHeadersComplete(int rv);
115*6777b538SAndroid Build Coastguard Worker   int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers);
116*6777b538SAndroid Build Coastguard Worker   void ReadTrailingHeaders();
117*6777b538SAndroid Build Coastguard Worker   void OnReadTrailingHeadersComplete(int rv);
118*6777b538SAndroid Build Coastguard Worker 
119*6777b538SAndroid Build Coastguard Worker   void OnReadBodyComplete(int rv);
120*6777b538SAndroid Build Coastguard Worker   int HandleReadComplete(int rv);
121*6777b538SAndroid Build Coastguard Worker 
122*6777b538SAndroid Build Coastguard Worker   void EnterStateSendHeaders();
123*6777b538SAndroid Build Coastguard Worker 
124*6777b538SAndroid Build Coastguard Worker   void ResetStream();
125*6777b538SAndroid Build Coastguard Worker 
126*6777b538SAndroid Build Coastguard Worker   // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and
127*6777b538SAndroid Build Coastguard Worker   // the handshake was never confirmed. Otherwise, returns |rv|.
128*6777b538SAndroid Build Coastguard Worker   int MapStreamError(int rv);
129*6777b538SAndroid Build Coastguard Worker 
130*6777b538SAndroid Build Coastguard Worker   // If |has_response_status_| is false, sets |response_status| to the result
131*6777b538SAndroid Build Coastguard Worker   // of ComputeResponseStatus(). Returns |response_status_|.
132*6777b538SAndroid Build Coastguard Worker   int GetResponseStatus();
133*6777b538SAndroid Build Coastguard Worker   // Sets the result of |ComputeResponseStatus()| as the |response_status_|.
134*6777b538SAndroid Build Coastguard Worker   void SaveResponseStatus();
135*6777b538SAndroid Build Coastguard Worker   // Sets |response_status_| to |response_status| and sets
136*6777b538SAndroid Build Coastguard Worker   // |has_response_status_| to true.
137*6777b538SAndroid Build Coastguard Worker   void SetResponseStatus(int response_status);
138*6777b538SAndroid Build Coastguard Worker   // Computes the correct response status based on the status of the handshake,
139*6777b538SAndroid Build Coastguard Worker   // |session_error|, |connection_error| and |stream_error|.
140*6777b538SAndroid Build Coastguard Worker   int ComputeResponseStatus() const;
141*6777b538SAndroid Build Coastguard Worker 
quic_session()142*6777b538SAndroid Build Coastguard Worker   QuicChromiumClientSession::Handle* quic_session() {
143*6777b538SAndroid Build Coastguard Worker     return static_cast<QuicChromiumClientSession::Handle*>(session());
144*6777b538SAndroid Build Coastguard Worker   }
145*6777b538SAndroid Build Coastguard Worker 
quic_session()146*6777b538SAndroid Build Coastguard Worker   const QuicChromiumClientSession::Handle* quic_session() const {
147*6777b538SAndroid Build Coastguard Worker     return static_cast<const QuicChromiumClientSession::Handle*>(session());
148*6777b538SAndroid Build Coastguard Worker   }
149*6777b538SAndroid Build Coastguard Worker 
150*6777b538SAndroid Build Coastguard Worker   State next_state_ = STATE_NONE;
151*6777b538SAndroid Build Coastguard Worker 
152*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<QuicChromiumClientStream::Handle> stream_;
153*6777b538SAndroid Build Coastguard Worker 
154*6777b538SAndroid Build Coastguard Worker   // The following three fields are all owned by the caller and must
155*6777b538SAndroid Build Coastguard Worker   // outlive this object, according to the HttpStream contract.
156*6777b538SAndroid Build Coastguard Worker 
157*6777b538SAndroid Build Coastguard Worker   // The request to send.
158*6777b538SAndroid Build Coastguard Worker   // Only valid before the response body is read.
159*6777b538SAndroid Build Coastguard Worker   raw_ptr<const HttpRequestInfo> request_info_ = nullptr;
160*6777b538SAndroid Build Coastguard Worker 
161*6777b538SAndroid Build Coastguard Worker   // Whether this request can be sent without confirmation.
162*6777b538SAndroid Build Coastguard Worker   bool can_send_early_ = false;
163*6777b538SAndroid Build Coastguard Worker 
164*6777b538SAndroid Build Coastguard Worker   // The request body to send, if any, owned by the caller.
165*6777b538SAndroid Build Coastguard Worker   // DanglingUntriaged because it is assigned a DanglingUntriaged pointer.
166*6777b538SAndroid Build Coastguard Worker   raw_ptr<UploadDataStream, AcrossTasksDanglingUntriaged> request_body_stream_ =
167*6777b538SAndroid Build Coastguard Worker       nullptr;
168*6777b538SAndroid Build Coastguard Worker   // Time the request was issued.
169*6777b538SAndroid Build Coastguard Worker   base::Time request_time_;
170*6777b538SAndroid Build Coastguard Worker   // The priority of the request.
171*6777b538SAndroid Build Coastguard Worker   RequestPriority priority_ = MINIMUM_PRIORITY;
172*6777b538SAndroid Build Coastguard Worker   // |response_info_| is the HTTP response data object which is filled in
173*6777b538SAndroid Build Coastguard Worker   // when a the response headers are read.  It is not owned by this stream.
174*6777b538SAndroid Build Coastguard Worker   raw_ptr<HttpResponseInfo> response_info_ = nullptr;
175*6777b538SAndroid Build Coastguard Worker   bool has_response_status_ = false;  // true if response_status_ as been set.
176*6777b538SAndroid Build Coastguard Worker   // Because response data is buffered, also buffer the response status if the
177*6777b538SAndroid Build Coastguard Worker   // stream is explicitly closed via OnError or OnClose with an error.
178*6777b538SAndroid Build Coastguard Worker   // Once all buffered data has been returned, this will be used as the final
179*6777b538SAndroid Build Coastguard Worker   // response.
180*6777b538SAndroid Build Coastguard Worker   int response_status_ = ERR_UNEXPECTED;
181*6777b538SAndroid Build Coastguard Worker 
182*6777b538SAndroid Build Coastguard Worker   // Serialized request headers.
183*6777b538SAndroid Build Coastguard Worker   spdy::Http2HeaderBlock request_headers_;
184*6777b538SAndroid Build Coastguard Worker 
185*6777b538SAndroid Build Coastguard Worker   spdy::Http2HeaderBlock response_header_block_;
186*6777b538SAndroid Build Coastguard Worker   bool response_headers_received_ = false;
187*6777b538SAndroid Build Coastguard Worker 
188*6777b538SAndroid Build Coastguard Worker   spdy::Http2HeaderBlock trailing_header_block_;
189*6777b538SAndroid Build Coastguard Worker   bool trailing_headers_received_ = false;
190*6777b538SAndroid Build Coastguard Worker 
191*6777b538SAndroid Build Coastguard Worker   // Number of bytes received by the headers stream on behalf of this stream.
192*6777b538SAndroid Build Coastguard Worker   int64_t headers_bytes_received_ = 0;
193*6777b538SAndroid Build Coastguard Worker   // Number of bytes sent by the headers stream on behalf of this stream.
194*6777b538SAndroid Build Coastguard Worker   int64_t headers_bytes_sent_ = 0;
195*6777b538SAndroid Build Coastguard Worker 
196*6777b538SAndroid Build Coastguard Worker   // Number of bytes received when the stream was closed.
197*6777b538SAndroid Build Coastguard Worker   int64_t closed_stream_received_bytes_ = 0;
198*6777b538SAndroid Build Coastguard Worker   // Number of bytes sent when the stream was closed.
199*6777b538SAndroid Build Coastguard Worker   int64_t closed_stream_sent_bytes_ = 0;
200*6777b538SAndroid Build Coastguard Worker   // True if the stream is the first stream negotiated on the session. Set when
201*6777b538SAndroid Build Coastguard Worker   // the stream was closed. If |stream_| is failed to be created, this takes on
202*6777b538SAndroid Build Coastguard Worker   // the default value of false.
203*6777b538SAndroid Build Coastguard Worker   bool closed_is_first_stream_ = false;
204*6777b538SAndroid Build Coastguard Worker 
205*6777b538SAndroid Build Coastguard Worker   quic::QuicErrorCode connection_error_ = quic::QUIC_NO_ERROR;
206*6777b538SAndroid Build Coastguard Worker   quic::QuicRstStreamErrorCode stream_error_ = quic::QUIC_STREAM_NO_ERROR;
207*6777b538SAndroid Build Coastguard Worker   uint64_t connection_wire_error_ = 0;
208*6777b538SAndroid Build Coastguard Worker   uint64_t ietf_application_error_ = 0;
209*6777b538SAndroid Build Coastguard Worker 
210*6777b538SAndroid Build Coastguard Worker   // The caller's callback to be used for asynchronous operations.
211*6777b538SAndroid Build Coastguard Worker   CompletionOnceCallback callback_;
212*6777b538SAndroid Build Coastguard Worker 
213*6777b538SAndroid Build Coastguard Worker   // Caller provided buffer for the ReadResponseBody() response.
214*6777b538SAndroid Build Coastguard Worker   scoped_refptr<IOBuffer> user_buffer_;
215*6777b538SAndroid Build Coastguard Worker   int user_buffer_len_ = 0;
216*6777b538SAndroid Build Coastguard Worker 
217*6777b538SAndroid Build Coastguard Worker   // Temporary buffer used to read the request body from UploadDataStream.
218*6777b538SAndroid Build Coastguard Worker   scoped_refptr<IOBufferWithSize> raw_request_body_buf_;
219*6777b538SAndroid Build Coastguard Worker   // Wraps raw_request_body_buf_ to read the remaining data progressively.
220*6777b538SAndroid Build Coastguard Worker   scoped_refptr<DrainableIOBuffer> request_body_buf_;
221*6777b538SAndroid Build Coastguard Worker 
222*6777b538SAndroid Build Coastguard Worker   NetLogWithSource stream_net_log_;
223*6777b538SAndroid Build Coastguard Worker 
224*6777b538SAndroid Build Coastguard Worker   int session_error_ =
225*6777b538SAndroid Build Coastguard Worker       ERR_UNEXPECTED;  // Error code from the connection shutdown.
226*6777b538SAndroid Build Coastguard Worker 
227*6777b538SAndroid Build Coastguard Worker   // Set to true when DoLoop() is being executed, false otherwise.
228*6777b538SAndroid Build Coastguard Worker   bool in_loop_ = false;
229*6777b538SAndroid Build Coastguard Worker 
230*6777b538SAndroid Build Coastguard Worker   // Session connect timing info.
231*6777b538SAndroid Build Coastguard Worker   LoadTimingInfo::ConnectTiming connect_timing_;
232*6777b538SAndroid Build Coastguard Worker 
233*6777b538SAndroid Build Coastguard Worker   // Stores any DNS aliases for the remote endpoint. Includes all known
234*6777b538SAndroid Build Coastguard Worker   // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for
235*6777b538SAndroid Build Coastguard Worker   // the connection, in no particular order. These are stored in the stream
236*6777b538SAndroid Build Coastguard Worker   // instead of the session due to complications related to IP-pooling.
237*6777b538SAndroid Build Coastguard Worker   std::set<std::string> dns_aliases_;
238*6777b538SAndroid Build Coastguard Worker 
239*6777b538SAndroid Build Coastguard Worker   base::WeakPtrFactory<QuicHttpStream> weak_factory_{this};
240*6777b538SAndroid Build Coastguard Worker };
241*6777b538SAndroid Build Coastguard Worker 
242*6777b538SAndroid Build Coastguard Worker }  // namespace net
243*6777b538SAndroid Build Coastguard Worker 
244*6777b538SAndroid Build Coastguard Worker #endif  // NET_QUIC_QUIC_HTTP_STREAM_H_
245