1 // Copyright 2011 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_HTTP_HTTP_RESPONSE_INFO_H_ 6 #define NET_HTTP_HTTP_RESPONSE_INFO_H_ 7 8 #include <set> 9 #include <string> 10 11 #include "base/time/time.h" 12 #include "net/base/auth.h" 13 #include "net/base/ip_endpoint.h" 14 #include "net/base/net_export.h" 15 #include "net/base/proxy_server.h" 16 #include "net/dns/public/resolve_error_info.h" 17 #include "net/http/alternate_protocol_usage.h" 18 #include "net/http/http_vary_data.h" 19 #include "net/ssl/ssl_info.h" 20 #include "third_party/abseil-cpp/absl/types/optional.h" 21 22 namespace base { 23 class Pickle; 24 } 25 26 namespace net { 27 28 class HttpResponseHeaders; 29 class SSLCertRequestInfo; 30 31 class NET_EXPORT HttpResponseInfo { 32 public: 33 // Describes the kind of connection used to fetch this response. 34 // 35 // NOTE: Please keep in sync with ConnectionInfo enum in 36 // tools/metrics/histograms/enums.xml. 37 // Because of that, and also because these values are persisted to 38 // the cache, please make sure not to delete or reorder values. 39 enum ConnectionInfo { 40 CONNECTION_INFO_UNKNOWN = 0, 41 CONNECTION_INFO_HTTP1_1 = 1, 42 CONNECTION_INFO_DEPRECATED_SPDY2 = 2, 43 CONNECTION_INFO_DEPRECATED_SPDY3 = 3, 44 CONNECTION_INFO_HTTP2 = 4, // HTTP/2. 45 CONNECTION_INFO_QUIC_UNKNOWN_VERSION = 5, 46 CONNECTION_INFO_DEPRECATED_HTTP2_14 = 6, // HTTP/2 draft-14. 47 CONNECTION_INFO_DEPRECATED_HTTP2_15 = 7, // HTTP/2 draft-15. 48 CONNECTION_INFO_HTTP0_9 = 8, 49 CONNECTION_INFO_HTTP1_0 = 9, 50 CONNECTION_INFO_QUIC_32 = 10, 51 CONNECTION_INFO_QUIC_33 = 11, 52 CONNECTION_INFO_QUIC_34 = 12, 53 CONNECTION_INFO_QUIC_35 = 13, 54 CONNECTION_INFO_QUIC_36 = 14, 55 CONNECTION_INFO_QUIC_37 = 15, 56 CONNECTION_INFO_QUIC_38 = 16, 57 CONNECTION_INFO_QUIC_39 = 17, 58 CONNECTION_INFO_QUIC_40 = 18, 59 CONNECTION_INFO_QUIC_41 = 19, 60 CONNECTION_INFO_QUIC_42 = 20, 61 CONNECTION_INFO_QUIC_43 = 21, 62 CONNECTION_INFO_QUIC_Q099 = 22, 63 CONNECTION_INFO_QUIC_44 = 23, 64 CONNECTION_INFO_QUIC_45 = 24, 65 CONNECTION_INFO_QUIC_46 = 25, 66 CONNECTION_INFO_QUIC_47 = 26, 67 CONNECTION_INFO_QUIC_999 = 27, 68 CONNECTION_INFO_QUIC_Q048 = 28, 69 CONNECTION_INFO_QUIC_Q049 = 29, 70 CONNECTION_INFO_QUIC_Q050 = 30, 71 CONNECTION_INFO_QUIC_T048 = 31, 72 CONNECTION_INFO_QUIC_T049 = 32, 73 CONNECTION_INFO_QUIC_T050 = 33, 74 CONNECTION_INFO_QUIC_T099 = 34, 75 CONNECTION_INFO_QUIC_DRAFT_25 = 35, 76 CONNECTION_INFO_QUIC_DRAFT_27 = 36, 77 CONNECTION_INFO_QUIC_DRAFT_28 = 37, 78 CONNECTION_INFO_QUIC_DRAFT_29 = 38, 79 CONNECTION_INFO_QUIC_T051 = 39, 80 CONNECTION_INFO_QUIC_RFC_V1 = 40, 81 CONNECTION_INFO_DEPRECATED_QUIC_2_DRAFT_1 = 41, 82 CONNECTION_INFO_QUIC_2_DRAFT_8 = 42, 83 NUM_OF_CONNECTION_INFOS, 84 }; 85 86 enum ConnectionInfoCoarse { 87 CONNECTION_INFO_COARSE_HTTP1, // HTTP/0.9, 1.0 and 1.1 88 CONNECTION_INFO_COARSE_HTTP2, 89 CONNECTION_INFO_COARSE_QUIC, 90 CONNECTION_INFO_COARSE_OTHER, 91 }; 92 93 // Used for categorizing transactions for reporting in histograms. 94 // CacheEntryStatus covers relatively common use cases being measured and 95 // considered for optimization. Many use cases that are more complex or 96 // uncommon are binned as OTHER, and details are not reported. 97 // NOTE: This enumeration is used in histograms, so please do not add entries 98 // in the middle. 99 enum CacheEntryStatus { 100 ENTRY_UNDEFINED, 101 // Complex or uncommon case. E.g., auth (401), partial responses (206), ... 102 ENTRY_OTHER, 103 // The response was not in the cache. Implies !was_cached && 104 // network_accessed. 105 ENTRY_NOT_IN_CACHE, 106 // The response was served from the cache and no validation was needed. 107 // Implies was_cached && !network_accessed. 108 ENTRY_USED, 109 // The response was validated and served from the cache. Implies was_cached 110 // && network_accessed. 111 ENTRY_VALIDATED, 112 // There was a stale entry in the cache that was updated. Implies 113 // !was_cached && network_accessed. 114 ENTRY_UPDATED, 115 // The HTTP request didn't allow a conditional request. Implies !was_cached 116 // && network_accessed. 117 ENTRY_CANT_CONDITIONALIZE, 118 ENTRY_MAX, 119 }; 120 121 // Returns a more coarse-grained description of the protocol used to fetch the 122 // response. 123 static ConnectionInfoCoarse ConnectionInfoToCoarse(ConnectionInfo info); 124 125 HttpResponseInfo(); 126 HttpResponseInfo(const HttpResponseInfo& rhs); 127 ~HttpResponseInfo(); 128 HttpResponseInfo& operator=(const HttpResponseInfo& rhs); 129 // Even though we could get away with the copy ctor and default operator=, 130 // that would prevent us from doing a bunch of forward declaration. 131 132 // Initializes from the representation stored in the given pickle. 133 bool InitFromPickle(const base::Pickle& pickle, bool* response_truncated); 134 135 // Call this method to persist the response info. 136 void Persist(base::Pickle* pickle, 137 bool skip_transient_headers, 138 bool response_truncated) const; 139 140 // Whether QUIC is used or not. 141 bool DidUseQuic() const; 142 143 // The following is only defined if the request_time member is set. 144 // If this resource was found in the cache, then this bool is set, and 145 // request_time may corresponds to a time "far" in the past. Note that 146 // stale content (perhaps un-cacheable) may be fetched from cache subject to 147 // the load flags specified on the request info. For example, this is done 148 // when a user presses the back button to re-render pages, or at startup, 149 // when reloading previously visited pages (without going over the network). 150 // Note also that under normal circumstances, was_cached is set to the correct 151 // value even if the request fails. 152 bool was_cached = false; 153 154 // How this response was handled by the HTTP cache. 155 CacheEntryStatus cache_entry_status = CacheEntryStatus::ENTRY_UNDEFINED; 156 157 // True if the request accessed the network in the process of retrieving 158 // data. 159 bool network_accessed = false; 160 161 // True if the request was fetched over a SPDY channel. 162 bool was_fetched_via_spdy = false; 163 164 // True if ALPN was negotiated for this request. 165 bool was_alpn_negotiated = false; 166 167 // True if the response was fetched via an explicit proxy. The proxy could 168 // be any type of proxy, HTTP or SOCKS. Note, we do not know if a 169 // transparent proxy may have been involved. 170 // 171 // If true and this struct was not restored from pickled data, |proxy_server| 172 // contains the proxy server that was used. 173 // 174 // TODO(https://crbug.com/653354): Remove this in favor of |proxy_server|. 175 bool was_fetched_via_proxy = false; 176 177 // Information about the proxy used to fetch this response, if any. 178 // 179 // This field is not persisted by |Persist()| and not restored by 180 // |InitFromPickle()|. 181 // 182 // TODO(https://crbug.com/653354): Support this field in |Persist()| and 183 // |InitFromPickle()| then use it to replace |was_fetched_via_proxy|. 184 ProxyServer proxy_server; 185 186 // Whether the request use http proxy or server authentication. 187 bool did_use_http_auth = false; 188 189 // True if the resource was originally fetched for a prefetch and has not been 190 // used since. 191 bool unused_since_prefetch = false; 192 193 // True if the response is a prefetch whose reuse is "restricted". This means 194 // it can only be reused from the cache by requests that are marked as able to 195 // use restricted prefetches. 196 bool restricted_prefetch = false; 197 198 // True if this resource is stale and needs async revalidation. 199 // This value is not persisted by Persist(); it is only ever set when the 200 // response is retrieved from the cache. 201 bool async_revalidation_requested = false; 202 203 // True if this entry in the single-keyed cache is unusable due to a checksum 204 // mismatch. 205 bool single_keyed_cache_entry_unusable = false; 206 207 // stale-while-revalidate, if any, will be honored until time given by 208 // |stale_revalidate_timeout|. This value is latched the first time 209 // stale-while-revalidate is used until the resource is revalidated. 210 base::Time stale_revalidate_timeout; 211 212 // Remote address of the socket which fetched this resource. 213 // 214 // NOTE: If the response was served from the cache (was_cached is true), 215 // the socket address will be set to the address that the content came from 216 // originally. This is true even if the response was re-validated using a 217 // different remote address, or if some of the content came from a byte-range 218 // request to a different address. 219 IPEndPoint remote_endpoint; 220 221 // Protocol negotiated with the server. 222 std::string alpn_negotiated_protocol; 223 224 // The reason why Chrome uses a specific transport protocol for HTTP 225 // semantics. 226 net::AlternateProtocolUsage alternate_protocol_usage = 227 net::AlternateProtocolUsage::ALTERNATE_PROTOCOL_USAGE_UNSPECIFIED_REASON; 228 229 // The type of connection used for this response. 230 ConnectionInfo connection_info = CONNECTION_INFO_UNKNOWN; 231 232 // The time at which the request was made that resulted in this response. 233 // For cached responses, this is the last time the cache entry was validated. 234 base::Time request_time; 235 236 // The time at which the response headers were received. For cached 237 // this is the last time the cache entry was validated. 238 base::Time response_time; 239 240 // Host resolution error info. 241 ResolveErrorInfo resolve_error_info; 242 243 // If the response headers indicate a 401 or 407 failure, then this structure 244 // will contain additional information about the authentication challenge. 245 absl::optional<AuthChallengeInfo> auth_challenge; 246 247 // The SSL client certificate request info. 248 // TODO(wtc): does this really belong in HttpResponseInfo? I put it here 249 // because it is similar to |auth_challenge|, but unlike HTTP authentication 250 // challenge, client certificate request is not part of an HTTP response. 251 scoped_refptr<SSLCertRequestInfo> cert_request_info; 252 253 // The SSL connection info (if HTTPS). Note that when a response is 254 // served from cache, not every field is present. See 255 // HttpResponseInfo::InitFromPickle(). 256 SSLInfo ssl_info; 257 258 // The parsed response headers and status line. 259 scoped_refptr<HttpResponseHeaders> headers; 260 261 // The "Vary" header data for this response. 262 // Initialized and used by HttpCache::Transaction. May also be passed to an 263 // auxiliary in-memory cache in the network service. 264 HttpVaryData vary_data; 265 266 // Any DNS aliases for the remote endpoint. Includes all known aliases, e.g. 267 // from A, AAAA, or HTTPS, not just from the address used for the connection, 268 // in no particular order. 269 std::set<std::string> dns_aliases; 270 271 // If not null, this indicates the response is stored during a certain browser 272 // session. Used for filtering cache access. 273 absl::optional<int64_t> browser_run_id; 274 275 static std::string ConnectionInfoToString(ConnectionInfo connection_info); 276 }; 277 278 } // namespace net 279 280 #endif // NET_HTTP_HTTP_RESPONSE_INFO_H_ 281