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 // HttpStream provides an abstraction for a basic http streams, SPDY, and QUIC. 6 // The HttpStream subtype is expected to manage the underlying transport 7 // appropriately. For example, a basic http stream will return the transport 8 // socket to the pool for reuse. SPDY streams on the other hand leave the 9 // transport socket management to the SpdySession. 10 11 #ifndef NET_HTTP_HTTP_STREAM_H_ 12 #define NET_HTTP_HTTP_STREAM_H_ 13 14 #include <stdint.h> 15 16 #include <memory> 17 #include <optional> 18 #include <set> 19 #include <string_view> 20 21 #include "net/base/completion_once_callback.h" 22 #include "net/base/idempotency.h" 23 #include "net/base/net_error_details.h" 24 #include "net/base/net_errors.h" 25 #include "net/base/net_export.h" 26 #include "net/base/request_priority.h" 27 #include "net/http/http_raw_request_headers.h" 28 #include "net/third_party/quiche/src/quiche/quic/core/quic_error_codes.h" 29 30 namespace net { 31 32 struct AlternativeService; 33 class HttpNetworkSession; 34 class HttpRequestHeaders; 35 struct HttpRequestInfo; 36 class HttpResponseInfo; 37 class IOBuffer; 38 class IPEndPoint; 39 struct LoadTimingInfo; 40 class NetLogWithSource; 41 class SSLInfo; 42 43 class NET_EXPORT_PRIVATE HttpStream { 44 public: 45 HttpStream() = default; 46 47 HttpStream(const HttpStream&) = delete; 48 HttpStream& operator=(const HttpStream&) = delete; 49 50 virtual ~HttpStream() = default; 51 52 // Registers the HTTP request for the stream. Must be called before calling 53 // InitializeStream(). Separating the registration of the request from the 54 // initialization of the stream allows the connection callback to run prior 55 // to stream initialization. 56 // 57 // The consumer should ensure that request_info points to a valid non-null 58 // value till final response headers are received; after that point, the 59 // HttpStream will not access |*request_info| and it may be deleted. 60 virtual void RegisterRequest(const HttpRequestInfo* request_info) = 0; 61 62 // Initializes the stream. Must be called before calling SendRequest(). 63 // If |can_send_early| is true, this stream may send data early without 64 // confirming the handshake if this is a resumption of a previously 65 // established connection. Returns a net error code, possibly ERR_IO_PENDING. 66 virtual int InitializeStream(bool can_send_early, 67 RequestPriority priority, 68 const NetLogWithSource& net_log, 69 CompletionOnceCallback callback) = 0; 70 71 // Writes the headers and uploads body data to the underlying socket. 72 // ERR_IO_PENDING is returned if the operation could not be completed 73 // synchronously, in which case the result will be passed to the callback 74 // when available. Returns OK on success. 75 // 76 // Some fields in |response| may be filled by this method, but it will not 77 // contain complete information until ReadResponseHeaders returns. 78 // 79 // |response| must remain valid until all sets of headers has been read, or 80 // the HttpStream is destroyed. There's typically only one set of 81 // headers, except in the case of 1xx responses (See ReadResponseHeaders). 82 virtual int SendRequest(const HttpRequestHeaders& request_headers, 83 HttpResponseInfo* response, 84 CompletionOnceCallback callback) = 0; 85 86 // Reads from the underlying socket until the next set of response headers 87 // have been completely received. Normally this is called once per request, 88 // however it may be called again in the event of a 1xx response to read the 89 // next set of headers. 90 // 91 // ERR_IO_PENDING is returned if the operation could not be completed 92 // synchronously, in which case the result will be passed to the callback when 93 // available. Returns OK on success. The response headers are available in 94 // the HttpResponseInfo passed in the original call to SendRequest. 95 virtual int ReadResponseHeaders(CompletionOnceCallback callback) = 0; 96 97 // Reads response body data, up to |buf_len| bytes. |buf_len| should be a 98 // reasonable size (<2MB). The number of bytes read is returned, or an 99 // error is returned upon failure. 0 indicates that the request has been 100 // fully satisfied and there is no more data to read. 101 // ERR_CONNECTION_CLOSED is returned when the connection has been closed 102 // prematurely. ERR_IO_PENDING is returned if the operation could not be 103 // completed synchronously, in which case the result will be passed to the 104 // callback when available. If the operation is not completed immediately, 105 // the socket acquires a reference to the provided buffer until the callback 106 // is invoked or the socket is destroyed. 107 virtual int ReadResponseBody(IOBuffer* buf, 108 int buf_len, 109 CompletionOnceCallback callback) = 0; 110 111 // Closes the stream. 112 // |not_reusable| indicates if the stream can be used for further requests. 113 // In the case of HTTP, where we re-use the byte-stream (e.g. the connection) 114 // this means we need to close the connection; in the case of SPDY, where the 115 // underlying stream is never reused, it has no effect. 116 // TODO(mmenke): We should fold the |not_reusable| flag into the stream 117 // implementation itself so that the caller does not need to 118 // pass it at all. Ideally we'd be able to remove 119 // CanReuseConnection() and IsResponseBodyComplete(). 120 // TODO(mmenke): We should try and merge Drain() into this method as well. 121 virtual void Close(bool not_reusable) = 0; 122 123 // Indicates if the response body has been completely read. 124 virtual bool IsResponseBodyComplete() const = 0; 125 126 // A stream exists on top of a connection. If the connection has been used 127 // to successfully exchange data in the past, error handling for the 128 // stream is done differently. This method returns true if the underlying 129 // connection is reused or has been connected and idle for some time. 130 virtual bool IsConnectionReused() const = 0; 131 // TODO(mmenke): We should fold this into RenewStreamForAuth(), and make that 132 // method drain the stream as well, if needed (And return asynchronously). 133 virtual void SetConnectionReused() = 0; 134 135 // Checks whether the underlying connection can be reused. The stream's 136 // connection can be reused if the response headers allow for it, the socket 137 // is still connected, and the stream exclusively owns the underlying 138 // connection. SPDY and QUIC streams don't own their own connections, so 139 // always return false. 140 virtual bool CanReuseConnection() const = 0; 141 142 // Get the total number of bytes received from network for this stream. 143 virtual int64_t GetTotalReceivedBytes() const = 0; 144 145 // Get the total number of bytes sent over the network for this stream. 146 virtual int64_t GetTotalSentBytes() const = 0; 147 148 // Populates the connection establishment part of |load_timing_info|, and 149 // socket ID. |load_timing_info| must have all null times when called. 150 // Returns false and does nothing if there is no underlying connection, either 151 // because one has yet to be assigned to the stream, or because the underlying 152 // socket has been closed. 153 // 154 // In practice, this means that this function will always succeed any time 155 // between when the full headers have been received and the stream has been 156 // closed. 157 virtual bool GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const = 0; 158 159 // Get the SSLInfo associated with this stream's connection. This should 160 // only be called for streams over SSL sockets, otherwise the behavior is 161 // undefined. 162 virtual void GetSSLInfo(SSLInfo* ssl_info) = 0; 163 164 // Returns true and populates |alternative_service| if an alternative service 165 // was used to for this stream. Otherwise returns false. 166 virtual bool GetAlternativeService( 167 AlternativeService* alternative_service) const = 0; 168 169 // Gets the remote endpoint of the socket that the HTTP stream is using, if 170 // any. Returns OK and fills in |endpoint| if it is available; returns an 171 // error and does not modify |endpoint| otherwise. 172 virtual int GetRemoteEndpoint(IPEndPoint* endpoint) = 0; 173 174 // In the case of an HTTP error or redirect, flush the response body (usually 175 // a simple error or "this page has moved") so that we can re-use the 176 // underlying connection. This stream is responsible for deleting itself when 177 // draining is complete. 178 virtual void Drain(HttpNetworkSession* session) = 0; 179 180 // Get the network error details this stream is encountering. 181 // Fills in |details| if it is available; leaves |details| unchanged if it 182 // is unavailable. 183 virtual void PopulateNetErrorDetails(NetErrorDetails* details) = 0; 184 185 // Called when the priority of the parent transaction changes. 186 virtual void SetPriority(RequestPriority priority) = 0; 187 188 // Returns a new (not initialized) stream using the same underlying 189 // connection and invalidates the old stream - no further methods should be 190 // called on the old stream. The caller should ensure that the response body 191 // from the previous request is drained before calling this method. If the 192 // subclass does not support renewing the stream, NULL is returned. 193 virtual std::unique_ptr<HttpStream> RenewStreamForAuth() = 0; 194 195 virtual void SetRequestHeadersCallback(RequestHeadersCallback callback) = 0; 196 197 // Set the idempotency of the request. No-op by default. SetRequestIdempotency(Idempotency idempotency)198 virtual void SetRequestIdempotency(Idempotency idempotency) {} 199 200 // Retrieves any DNS aliases for the remote endpoint. Includes all known 201 // aliases, e.g. from A, AAAA, or HTTPS, not just from the address used for 202 // the connection, in no particular order. 203 virtual const std::set<std::string>& GetDnsAliases() const = 0; 204 205 // The value in the ACCEPT_CH frame received during TLS handshake via the 206 // ALPS extension, or the empty string if the server did not send one. Unlike 207 // Accept-CH header fields received in HTTP responses, this value is available 208 // before any requests are made. 209 virtual std::string_view GetAcceptChViaAlps() const = 0; 210 211 // Represents detailed QUIC errors returned by GetQuicErrorDetails(). 212 struct QuicErrorDetails { 213 // Internal connection error of the stream. 214 quic::QuicErrorCode connection_error = quic::QUIC_NO_ERROR; 215 // Internal stream error of the stream. 216 quic::QuicRstStreamErrorCode stream_error = quic::QUIC_STREAM_NO_ERROR; 217 // Connection error sent or received on the wire protocol. 218 uint64_t connection_wire_error = 0; 219 // Application error sent or received on the wire protocol. 220 uint64_t ietf_application_error = 0; 221 }; 222 223 // If `this` is using a QUIC stream, returns error details of the QUIC stream. 224 // Otherwise returns nullopt. Detailed QUIC errors are only available after 225 // the stream has been initialized. Use PopulateNetErrorDetails() for errors 226 // that happened during the initialization. 227 virtual std::optional<QuicErrorDetails> GetQuicErrorDetails() const; 228 }; 229 230 } // namespace net 231 232 #endif // NET_HTTP_HTTP_STREAM_H_ 233