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