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