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