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/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 base::StringPiece GetAcceptChViaAlps() const override; 78 absl::optional<quic::QuicErrorCode> GetQuicErrorCode() const override; 79 absl::optional<quic::QuicRstStreamErrorCode> GetQuicRstStreamErrorCode() 80 const override; 81 82 static HttpConnectionInfo ConnectionInfoFromQuicVersion( 83 quic::ParsedQuicVersion quic_version); 84 85 private: 86 friend class test::QuicHttpStreamPeer; 87 88 enum State { 89 STATE_NONE, 90 STATE_REQUEST_STREAM, 91 STATE_REQUEST_STREAM_COMPLETE, 92 STATE_SET_REQUEST_PRIORITY, 93 STATE_SEND_HEADERS, 94 STATE_SEND_HEADERS_COMPLETE, 95 STATE_READ_REQUEST_BODY, 96 STATE_READ_REQUEST_BODY_COMPLETE, 97 STATE_SEND_BODY, 98 STATE_SEND_BODY_COMPLETE, 99 STATE_OPEN, 100 }; 101 102 void OnIOComplete(int rv); 103 void DoCallback(int rv); 104 105 int DoLoop(int rv); 106 int DoRequestStream(); 107 int DoRequestStreamComplete(int rv); 108 int DoSetRequestPriority(); 109 int DoSendHeaders(); 110 int DoSendHeadersComplete(int rv); 111 int DoReadRequestBody(); 112 int DoReadRequestBodyComplete(int rv); 113 int DoSendBody(); 114 int DoSendBodyComplete(int rv); 115 116 void OnReadResponseHeadersComplete(int rv); 117 int ProcessResponseHeaders(const spdy::Http2HeaderBlock& headers); 118 void ReadTrailingHeaders(); 119 void OnReadTrailingHeadersComplete(int rv); 120 121 void OnReadBodyComplete(int rv); 122 int HandleReadComplete(int rv); 123 124 void EnterStateSendHeaders(); 125 126 void ResetStream(); 127 128 // Returns ERR_QUIC_HANDSHAKE_FAILED, if |rv| is ERR_QUIC_PROTOCOL_ERROR and 129 // the handshake was never confirmed. Otherwise, returns |rv|. 130 int MapStreamError(int rv); 131 132 // If |has_response_status_| is false, sets |response_status| to the result 133 // of ComputeResponseStatus(). Returns |response_status_|. 134 int GetResponseStatus(); 135 // Sets the result of |ComputeResponseStatus()| as the |response_status_|. 136 void SaveResponseStatus(); 137 // Sets |response_status_| to |response_status| and sets 138 // |has_response_status_| to true. 139 void SetResponseStatus(int response_status); 140 // Computes the correct response status based on the status of the handshake, 141 // |session_error|, |connection_error| and |stream_error|. 142 int ComputeResponseStatus() const; 143 quic_session()144 QuicChromiumClientSession::Handle* quic_session() { 145 return static_cast<QuicChromiumClientSession::Handle*>(session()); 146 } 147 quic_session()148 const QuicChromiumClientSession::Handle* quic_session() const { 149 return static_cast<const QuicChromiumClientSession::Handle*>(session()); 150 } 151 152 State next_state_ = STATE_NONE; 153 154 std::unique_ptr<QuicChromiumClientStream::Handle> stream_; 155 156 // The following three fields are all owned by the caller and must 157 // outlive this object, according to the HttpStream contract. 158 159 // The request to send. 160 // Only valid before the response body is read. 161 raw_ptr<const HttpRequestInfo> request_info_ = nullptr; 162 163 // Whether this request can be sent without confirmation. 164 bool can_send_early_ = false; 165 166 // The request body to send, if any, owned by the caller. 167 // DanglingUntriaged because it is assigned a DanglingUntriaged pointer. 168 raw_ptr<UploadDataStream, AcrossTasksDanglingUntriaged> request_body_stream_ = 169 nullptr; 170 // Time the request was issued. 171 base::Time request_time_; 172 // The priority of the request. 173 RequestPriority priority_ = MINIMUM_PRIORITY; 174 // |response_info_| is the HTTP response data object which is filled in 175 // when a the response headers are read. It is not owned by this stream. 176 raw_ptr<HttpResponseInfo> response_info_ = nullptr; 177 bool has_response_status_ = false; // true if response_status_ as been set. 178 // Because response data is buffered, also buffer the response status if the 179 // stream is explicitly closed via OnError or OnClose with an error. 180 // Once all buffered data has been returned, this will be used as the final 181 // response. 182 int response_status_ = ERR_UNEXPECTED; 183 184 // Serialized request headers. 185 spdy::Http2HeaderBlock request_headers_; 186 187 spdy::Http2HeaderBlock response_header_block_; 188 bool response_headers_received_ = false; 189 190 spdy::Http2HeaderBlock trailing_header_block_; 191 bool trailing_headers_received_ = false; 192 193 // Number of bytes received by the headers stream on behalf of this stream. 194 int64_t headers_bytes_received_ = 0; 195 // Number of bytes sent by the headers stream on behalf of this stream. 196 int64_t headers_bytes_sent_ = 0; 197 198 // Number of bytes received when the stream was closed. 199 int64_t closed_stream_received_bytes_ = 0; 200 // Number of bytes sent when the stream was closed. 201 int64_t closed_stream_sent_bytes_ = 0; 202 // True if the stream is the first stream negotiated on the session. Set when 203 // the stream was closed. If |stream_| is failed to be created, this takes on 204 // the default value of false. 205 bool closed_is_first_stream_ = false; 206 207 absl::optional<quic::QuicErrorCode> connection_error_; 208 absl::optional<quic::QuicRstStreamErrorCode> stream_error_; 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