http_vary_data.h 3.2 KB

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