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