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