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_HTTP_HTTP_VARY_DATA_H_ 6 #define NET_HTTP_HTTP_VARY_DATA_H_ 7 8 #include <string_view> 9 10 #include "base/hash/md5.h" 11 #include "net/base/net_export.h" 12 13 namespace base { 14 class Pickle; 15 class PickleIterator; 16 } 17 18 namespace net { 19 20 struct HttpRequestInfo; 21 class HttpResponseHeaders; 22 23 // Used to implement the HTTP/1.1 Vary header. This class contains a MD5 hash 24 // over the request headers indicated by a Vary header. 25 // 26 // While RFC 2616 requires strict request header comparisons, it is much 27 // cheaper to store a MD5 sum, which should be sufficient. Storing a hash also 28 // avoids messy privacy issues as some of the request headers could hold 29 // sensitive data (e.g., cookies). 30 // 31 // NOTE: This class does not hold onto the contents of the Vary header. 32 // Instead, it relies on the consumer to store that and to supply it again to 33 // the MatchesRequest function for comparing against future HTTP requests. 34 // 35 class NET_EXPORT_PRIVATE HttpVaryData { 36 public: 37 HttpVaryData(); 38 is_valid()39 bool is_valid() const { return is_valid_; } 40 41 // Initialize from a request and its corresponding response headers. 42 // 43 // Returns true if a Vary header was found in the response headers and that 44 // Vary header was not empty. Upon success, the object is also marked as valid 45 // such that is_valid() will return true. Otherwise, false is returned to 46 // indicate that this object is marked as invalid. 47 // 48 bool Init(const HttpRequestInfo& request_info, 49 const HttpResponseHeaders& response_headers); 50 51 // Initialize from a pickle that contains data generated by a call to the 52 // vary data's Persist method. 53 // 54 // Upon success, true is returned and the object is marked as valid such that 55 // is_valid() will return true. Otherwise, false is returned to indicate 56 // that this object is marked as invalid. 57 // 58 bool InitFromPickle(base::PickleIterator* pickle_iter); 59 60 // Call this method to persist the vary data. Illegal to call this on an 61 // invalid object. 62 void Persist(base::Pickle* pickle) const; 63 64 // Call this method to test if the given request matches the previous request 65 // with which this vary data corresponds. The |cached_response_headers| must 66 // be the same response headers used to generate this vary data. 67 bool MatchesRequest(const HttpRequestInfo& request_info, 68 const HttpResponseHeaders& cached_response_headers) const; 69 70 private: 71 // Append to the MD5 context for the given request header. 72 static void AddField(const HttpRequestInfo& request_info, 73 std::string_view request_header, 74 base::MD5Context* context); 75 76 // A digested version of the request headers corresponding to the Vary header. 77 base::MD5Digest request_digest_; 78 79 // True when request_digest_ contains meaningful data. 80 bool is_valid_ = false; 81 }; 82 83 } // namespace net 84 85 #endif // NET_HTTP_HTTP_VARY_DATA_H_ 86