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_SPDY_SPDY_HTTP_STREAM_H_ 6 #define NET_SPDY_SPDY_HTTP_STREAM_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <set> 12 #include <string_view> 13 14 #include "base/memory/raw_ptr.h" 15 #include "base/memory/raw_ptr_exclusion.h" 16 #include "base/memory/scoped_refptr.h" 17 #include "base/memory/weak_ptr.h" 18 #include "base/timer/timer.h" 19 #include "net/base/completion_once_callback.h" 20 #include "net/base/load_timing_info.h" 21 #include "net/base/net_export.h" 22 #include "net/log/net_log_source.h" 23 #include "net/spdy/multiplexed_http_stream.h" 24 #include "net/spdy/spdy_read_queue.h" 25 #include "net/spdy/spdy_session.h" 26 #include "net/spdy/spdy_stream.h" 27 #include "net/third_party/quiche/src/quiche/common/http/http_header_block.h" 28 29 namespace net { 30 31 struct HttpRequestInfo; 32 class HttpResponseInfo; 33 class IOBuffer; 34 class SpdySession; 35 class UploadDataStream; 36 37 // The SpdyHttpStream is a HTTP-specific type of stream known to a SpdySession. 38 class NET_EXPORT_PRIVATE SpdyHttpStream : public SpdyStream::Delegate, 39 public MultiplexedHttpStream { 40 public: 41 static const size_t kRequestBodyBufferSize; 42 // |spdy_session| must not be NULL. 43 SpdyHttpStream(const base::WeakPtr<SpdySession>& spdy_session, 44 NetLogSource source_dependency, 45 std::set<std::string> dns_aliases); 46 47 SpdyHttpStream(const SpdyHttpStream&) = delete; 48 SpdyHttpStream& operator=(const SpdyHttpStream&) = delete; 49 50 ~SpdyHttpStream() override; 51 stream()52 SpdyStream* stream() { return stream_; } 53 54 // Cancels any callbacks from being invoked and deletes the stream. 55 void Cancel(); 56 57 // HttpStream implementation. 58 void RegisterRequest(const HttpRequestInfo* request_info) override; 59 int InitializeStream(bool can_send_early, 60 RequestPriority priority, 61 const NetLogWithSource& net_log, 62 CompletionOnceCallback callback) override; 63 64 int SendRequest(const HttpRequestHeaders& headers, 65 HttpResponseInfo* response, 66 CompletionOnceCallback callback) override; 67 int ReadResponseHeaders(CompletionOnceCallback callback) override; 68 int ReadResponseBody(IOBuffer* buf, 69 int buf_len, 70 CompletionOnceCallback callback) override; 71 void Close(bool not_reusable) override; 72 bool IsResponseBodyComplete() const override; 73 74 // Must not be called if a NULL SpdySession was pssed into the 75 // constructor. 76 bool IsConnectionReused() const override; 77 78 // Total number of bytes received over the network of SPDY data, headers, and 79 // push_promise frames associated with this stream, including the size of 80 // frame headers, after SSL decryption and not including proxy overhead. 81 int64_t GetTotalReceivedBytes() const override; 82 // Total number of bytes sent over the network of SPDY frames associated with 83 // this stream, including the size of frame headers, before SSL encryption and 84 // not including proxy overhead. Note that some SPDY frames such as pings are 85 // not associated with any stream, and are not included in this value. 86 int64_t GetTotalSentBytes() const override; 87 bool GetAlternativeService( 88 AlternativeService* alternative_service) const override; 89 bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const override; 90 int GetRemoteEndpoint(IPEndPoint* endpoint) override; 91 void PopulateNetErrorDetails(NetErrorDetails* details) override; 92 void SetPriority(RequestPriority priority) override; 93 const std::set<std::string>& GetDnsAliases() const override; 94 std::string_view GetAcceptChViaAlps() const override; 95 96 // SpdyStream::Delegate implementation. 97 void OnHeadersSent() override; 98 void OnEarlyHintsReceived(const quiche::HttpHeaderBlock& headers) override; 99 void OnHeadersReceived( 100 const quiche::HttpHeaderBlock& response_headers) override; 101 void OnDataReceived(std::unique_ptr<SpdyBuffer> buffer) override; 102 void OnDataSent() override; 103 void OnTrailers(const quiche::HttpHeaderBlock& trailers) override; 104 void OnClose(int status) override; 105 bool CanGreaseFrameType() const override; 106 NetLogSource source_dependency() const override; 107 108 private: 109 // Helper function used to initialize private members and to set delegate on 110 // stream when stream is created. 111 void InitializeStreamHelper(); 112 113 // Helper function used for resetting stream from inside the stream. 114 void ResetStream(int error); 115 116 // Must be called only when |request_info_| is non-NULL. 117 bool HasUploadData() const; 118 119 void OnStreamCreated(CompletionOnceCallback callback, int rv); 120 121 // Reads the remaining data (whether chunked or not) from the 122 // request body stream and sends it if there's any. The read and 123 // subsequent sending may happen asynchronously. Must be called only 124 // when HasUploadData() is true. 125 void ReadAndSendRequestBodyData(); 126 127 // Send an empty body. Must only be called if there is no upload data and 128 // sending greased HTTP/2 frames is enabled. This allows SpdyStream to 129 // prepend a greased HTTP/2 frame to the empty DATA frame that closes the 130 // stream. 131 void SendEmptyBody(); 132 133 // Called when data has just been read from the request body stream; 134 // does the actual sending of data. 135 void OnRequestBodyReadCompleted(int status); 136 137 // Call the user callback associated with sending the request. 138 void DoRequestCallback(int rv); 139 140 // Method to PostTask for calling request callback asynchronously. 141 void MaybeDoRequestCallback(int rv); 142 143 // Post the request callback if not null. 144 // This is necessary because the request callback might destroy |stream_|, 145 // which does not support that. 146 void MaybePostRequestCallback(int rv); 147 148 // Call the user callback associated with reading the response. 149 void DoResponseCallback(int rv); 150 151 void MaybeScheduleBufferedReadCallback(); 152 void DoBufferedReadCallback(); 153 154 const base::WeakPtr<SpdySession> spdy_session_; 155 156 bool is_reused_; 157 SpdyStreamRequest stream_request_; 158 const NetLogSource source_dependency_; 159 160 // |stream_| is owned by SpdySession. 161 // Before InitializeStream() is called, stream_ == nullptr. 162 // After InitializeStream() is called but before OnClose() is called, 163 // |*stream_| is guaranteed to be valid. 164 // After OnClose() is called, stream_ == nullptr. 165 raw_ptr<SpdyStream> stream_ = nullptr; 166 167 // False before OnClose() is called, true after. 168 bool stream_closed_ = false; 169 170 // Set only when |stream_closed_| is true. 171 int closed_stream_status_ = ERR_FAILED; 172 spdy::SpdyStreamId closed_stream_id_ = 0; 173 bool closed_stream_has_load_timing_info_; 174 LoadTimingInfo closed_stream_load_timing_info_; 175 // After |stream_| has been closed, this keeps track of the total number of 176 // bytes received over the network for |stream_| while it was open. 177 int64_t closed_stream_received_bytes_ = 0; 178 // After |stream_| has been closed, this keeps track of the total number of 179 // bytes sent over the network for |stream_| while it was open. 180 int64_t closed_stream_sent_bytes_ = 0; 181 182 // The request to send. 183 // Set to null before response body is starting to be read. This is to allow 184 // |this| to be shared for reading and to possibly outlive request_info_'s 185 // owner. Setting to null happens after headers are completely read or upload 186 // data stream is uploaded, whichever is later. 187 raw_ptr<const HttpRequestInfo> request_info_ = nullptr; 188 189 // |response_info_| is the HTTP response data object which is filled in 190 // when a response HEADERS comes in for the stream. 191 // It is not owned by this stream object. 192 raw_ptr<HttpResponseInfo> response_info_ = nullptr; 193 194 bool response_headers_complete_ = false; 195 196 bool upload_stream_in_progress_ = false; 197 198 // We buffer the response body as it arrives asynchronously from the stream. 199 SpdyReadQueue response_body_queue_; 200 201 CompletionOnceCallback request_callback_; 202 CompletionOnceCallback response_callback_; 203 204 // User provided buffer for the ReadResponseBody() response. 205 scoped_refptr<IOBuffer> user_buffer_; 206 int user_buffer_len_ = 0; 207 208 // Temporary buffer used to read the request body from UploadDataStream. 209 scoped_refptr<IOBufferWithSize> request_body_buf_; 210 int request_body_buf_size_ = 0; 211 212 // Timer to execute DoBufferedReadCallback() with a delay. 213 base::OneShotTimer buffered_read_timer_; 214 215 // Stores any DNS aliases for the remote endpoint. Includes all known aliases, 216 // e.g. from A, AAAA, or HTTPS, not just from the address used for the 217 // connection, in no particular order. These are stored in the stream instead 218 // of the session due to complications related to IP-pooling. 219 std::set<std::string> dns_aliases_; 220 221 // Keep track of the priority of the request for setting the priority header 222 // right before sending the request. 223 RequestPriority priority_ = RequestPriority::DEFAULT_PRIORITY; 224 225 base::WeakPtrFactory<SpdyHttpStream> weak_factory_{this}; 226 }; 227 228 } // namespace net 229 230 #endif // NET_SPDY_SPDY_HTTP_STREAM_H_ 231