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