xref: /aosp_15_r20/external/cronet/net/http/http_no_vary_search_data.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2022 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_NO_VARY_SEARCH_DATA_H_
6 #define NET_HTTP_HTTP_NO_VARY_SEARCH_DATA_H_
7 
8 #include <string>
9 
10 #include "base/containers/flat_set.h"
11 #include "base/types/expected.h"
12 #include "net/base/net_export.h"
13 #include "net/http/structured_headers.h"
14 #include "url/gurl.h"
15 
16 namespace net {
17 
18 class HttpResponseHeaders;
19 
20 // Data extracted from No-Vary-Search header.
21 //
22 // This data can be used to determine which parts of the URL search
23 // can be ignored when comparing a request with a cached response.
24 class NET_EXPORT_PRIVATE HttpNoVarySearchData {
25  public:
26   enum class ParseErrorEnum {
27     kOk,             // Parsing is correct. Also returned if there is no header.
28     kDefaultValue,   // Parsing is correct but led to default value - the header
29                      // could be removed.
30     kNotDictionary,  // Header value is not a dictionary.
31     kUnknownDictionaryKey,     // Header value contains unknown dictionary keys.
32     kNonBooleanKeyOrder,       // `key-order` is non-boolean.
33     kParamsNotStringList,      // `params` is not a string list.
34     kExceptNotStringList,      // `expect` is not a string list.
35     kExceptWithoutTrueParams,  // `expect` specified without params set to true.
36   };
37   HttpNoVarySearchData(const HttpNoVarySearchData&);
38   HttpNoVarySearchData(HttpNoVarySearchData&&);
39   ~HttpNoVarySearchData();
40   HttpNoVarySearchData& operator=(const HttpNoVarySearchData&);
41   HttpNoVarySearchData& operator=(HttpNoVarySearchData&&);
42 
43   static HttpNoVarySearchData CreateFromNoVaryParams(
44       const std::vector<std::string>& no_vary_params,
45       bool vary_on_key_order);
46   static HttpNoVarySearchData CreateFromVaryParams(
47       const std::vector<std::string>& vary_params,
48       bool vary_on_key_order);
49 
50   // Parse No-Vary-Search from response headers.
51   //
52   // Returns HttpNoVarySearchData if a correct No-Vary-Search header is present
53   // in the response headers or a ParseErrorEnum if the No-Vary-Search header is
54   // incorrect. If no No-Vary-Search is found, returns ParseErrorEnum::kOk.
55   static base::expected<HttpNoVarySearchData,
56                         HttpNoVarySearchData::ParseErrorEnum>
57   ParseFromHeaders(const HttpResponseHeaders& response_headers);
58 
59   bool AreEquivalent(const GURL& a, const GURL& b) const;
60 
61   const base::flat_set<std::string>& no_vary_params() const;
62   const base::flat_set<std::string>& vary_params() const;
63   bool vary_on_key_order() const;
64   bool vary_by_default() const;
65 
66  private:
67   HttpNoVarySearchData();
68 
69   static base::expected<HttpNoVarySearchData,
70                         HttpNoVarySearchData::ParseErrorEnum>
71   ParseNoVarySearchDictionary(const structured_headers::Dictionary& dict);
72 
73   // Query parameters which should be ignored when comparing a request
74   // to a cached response. This is empty if |vary_by_default_| is false.
75   base::flat_set<std::string> no_vary_params_;
76   // Query parameters which should be respected when comparing a request
77   // to a cached response, even if |vary_by_default_| is false. This is empty
78   // if |vary_by_default_| is true.
79   base::flat_set<std::string> vary_params_;
80   // If false, parameters with distinct keys can be reordered in order to find a
81   // cache hit.
82   bool vary_on_key_order_ = true;
83   // If true, all parameters are significant except those in |no_vary_params_|.
84   // If false, only parameters in |vary_params_| are significant.
85   bool vary_by_default_ = true;
86 };
87 
88 }  // namespace net
89 
90 #endif  // NET_HTTP_HTTP_NO_VARY_SEARCH_DATA_H_
91