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 14 #include "base/memory/raw_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/strings/string_piece.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/http/quic_client_push_promise_index.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 base::StringPiece GetAcceptChViaAlps() const override; 79 absl::optional<quic::QuicErrorCode> GetQuicErrorCode() const override; 80 absl::optional<quic::QuicRstStreamErrorCode> GetQuicRstStreamErrorCode() 81 const override; 82 83 static HttpResponseInfo::ConnectionInfo ConnectionInfoFromQuicVersion( 84 quic::ParsedQuicVersion quic_version); 85 86 private: 87 friend class test::QuicHttpStreamPeer; 88 89 enum State { 90 STATE_NONE, 91 STATE_HANDLE_PROMISE, 92 STATE_HANDLE_PROMISE_COMPLETE, 93 STATE_REQUEST_STREAM, 94 STATE_REQUEST_STREAM_COMPLETE, 95 STATE_SET_REQUEST_PRIORITY, 96 STATE_SEND_HEADERS, 97 STATE_SEND_HEADERS_COMPLETE, 98 STATE_READ_REQUEST_BODY, 99 STATE_READ_REQUEST_BODY_COMPLETE, 100 STATE_SEND_BODY, 101 STATE_SEND_BODY_COMPLETE, 102 STATE_OPEN, 103 }; 104 105 void OnIOComplete(int rv); 106 void DoCallback(int rv); 107 108 int DoLoop(int rv); 109 int DoHandlePromise(); 110 int DoHandlePromiseComplete(int rv); 111 int DoRequestStream(); 112 int DoRequestStreamComplete(int rv); 113 int DoSetRequestPriority(); 114 int DoSendHeaders(); 115 int DoSendHeadersComplete(int rv); 116 int DoReadRequestBody(); 117 int DoReadRequestBodyComplete(int rv); 118 int DoSendBody(); 119 int DoSendBodyComplete(int rv); 120 121 void OnReadResponseHeadersComplete(int rv); 122 int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers); 123 void ReadTrailingHeaders(); 124 void OnReadTrailingHeadersComplete(int rv); 125 126 void OnReadBodyComplete(int rv); 127 int HandleReadComplete(int rv); 128 129 void EnterStateSendHeaders(); 130 131 void ResetStream(); 132 133 // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and 134 // the handshake was never confirmed. Otherwise, returns |rv|. 135 int MapStreamError(int rv); 136 137 // If |has_response_status_| is false, sets |response_status| to the result 138 // of ComputeResponseStatus(). Returns |response_status_|. 139 int GetResponseStatus(); 140 // Sets the result of |ComputeResponseStatus()| as the |response_status_|. 141 void SaveResponseStatus(); 142 // Sets |response_status_| to |response_status| and sets 143 // |has_response_status_| to true. 144 void SetResponseStatus(int response_status); 145 // Computes the correct response status based on the status of the handshake, 146 // |session_error|, |connection_error| and |stream_error|. 147 int ComputeResponseStatus() const; 148 quic_session()149 QuicChromiumClientSession::Handle* quic_session() { 150 return static_cast<QuicChromiumClientSession::Handle*>(session()); 151 } 152 quic_session()153 const QuicChromiumClientSession::Handle* quic_session() const { 154 return static_cast<const QuicChromiumClientSession::Handle*>(session()); 155 } 156 157 State next_state_ = STATE_NONE; 158 159 std::unique_ptr<QuicChromiumClientStream::Handle> stream_; 160 161 // The following three fields are all owned by the caller and must 162 // outlive this object, according to the HttpStream contract. 163 164 // The request to send. 165 // Only valid before the response body is read. 166 raw_ptr<const HttpRequestInfo> request_info_ = nullptr; 167 168 // Whether this request can be sent without confirmation. 169 bool can_send_early_ = false; 170 171 // The request body to send, if any, owned by the caller. 172 // DanglingUntriaged because it is assigned a DanglingUntriaged pointer. 173 raw_ptr<UploadDataStream, DanglingUntriaged> request_body_stream_ = nullptr; 174 // Time the request was issued. 175 base::Time request_time_; 176 // The priority of the request. 177 RequestPriority priority_ = MINIMUM_PRIORITY; 178 // |response_info_| is the HTTP response data object which is filled in 179 // when a the response headers are read. It is not owned by this stream. 180 raw_ptr<HttpResponseInfo> response_info_ = nullptr; 181 bool has_response_status_ = false; // true if response_status_ as been set. 182 // Because response data is buffered, also buffer the response status if the 183 // stream is explicitly closed via OnError or OnClose with an error. 184 // Once all buffered data has been returned, this will be used as the final 185 // response. 186 int response_status_ = ERR_UNEXPECTED; 187 188 // Serialized request headers. 189 spdy::Http2HeaderBlock request_headers_; 190 191 spdy::Http2HeaderBlock response_header_block_; 192 bool response_headers_received_ = false; 193 194 spdy::Http2HeaderBlock trailing_header_block_; 195 bool trailing_headers_received_ = false; 196 197 // Number of bytes received by the headers stream on behalf of this stream. 198 int64_t headers_bytes_received_ = 0; 199 // Number of bytes sent by the headers stream on behalf of this stream. 200 int64_t headers_bytes_sent_ = 0; 201 202 // Number of bytes received when the stream was closed. 203 int64_t closed_stream_received_bytes_ = 0; 204 // Number of bytes sent when the stream was closed. 205 int64_t closed_stream_sent_bytes_ = 0; 206 // True if the stream is the first stream negotiated on the session. Set when 207 // the stream was closed. If |stream_| is failed to be created, this takes on 208 // the default value of false. 209 bool closed_is_first_stream_ = false; 210 211 absl::optional<quic::QuicErrorCode> connection_error_; 212 absl::optional<quic::QuicRstStreamErrorCode> stream_error_; 213 214 // The caller's callback to be used for asynchronous operations. 215 CompletionOnceCallback callback_; 216 217 // Caller provided buffer for the ReadResponseBody() response. 218 scoped_refptr<IOBuffer> user_buffer_; 219 int user_buffer_len_ = 0; 220 221 // Temporary buffer used to read the request body from UploadDataStream. 222 scoped_refptr<IOBufferWithSize> raw_request_body_buf_; 223 // Wraps raw_request_body_buf_ to read the remaining data progressively. 224 scoped_refptr<DrainableIOBuffer> request_body_buf_; 225 226 NetLogWithSource stream_net_log_; 227 228 int session_error_ = 229 ERR_UNEXPECTED; // Error code from the connection shutdown. 230 231 bool found_promise_ = false; 232 233 // Set to true when DoLoop() is being executed, false otherwise. 234 bool in_loop_ = false; 235 236 // Session connect timing info. 237 LoadTimingInfo::ConnectTiming connect_timing_; 238 239 // Stores any DNS aliases for the remote endpoint. Includes all known 240 // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for 241 // the connection, in no particular order. These are stored in the stream 242 // instead of the session due to complications related to IP-pooling. 243 std::set<std::string> dns_aliases_; 244 245 base::WeakPtrFactory<QuicHttpStream> weak_factory_{this}; 246 }; 247 248 } // namespace net 249 250 #endif // NET_QUIC_QUIC_HTTP_STREAM_H_ 251