HttpHdrCc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SRC_HTTPHDRCC_H
10 #define SQUID_SRC_HTTPHDRCC_H
11 
12 #include "defines.h"
13 #include "dlink.h"
14 #include "mem/forward.h"
15 #include "SquidString.h"
16 #include <iosfwd>
17 
18 class Packable;
19 
20 enum HttpHdrCcType : unsigned char {
21  CC_PUBLIC = 0,
34  CC_IMMUTABLE, /* RFC 8246 */
36  CC_ENUM_END /* also used to mean "invalid" */
37 };
38 
43 class HttpHdrCc
44 {
46 
47 public:
48  static const int32_t MAX_AGE_UNKNOWN=-1; //max-age is unset
49  static const int32_t S_MAXAGE_UNKNOWN=-1; //s-maxage is unset
50  static const int32_t MAX_STALE_UNKNOWN=-1; //max-stale is unset
53  static const int32_t MAX_STALE_ANY=0x7fffffff;
54  static const int32_t STALE_IF_ERROR_UNKNOWN=-1; //stale_if_error is unset
55  static const int32_t MIN_FRESH_UNKNOWN=-1; //min_fresh is unset
56 
61 
63  void clear();
64 
66  bool parse(const String & s);
67 
68  //manipulation for Cache-Control: public header
69  bool hasPublic() const {return isSet(HttpHdrCcType::CC_PUBLIC);}
72 
73  //manipulation for Cache-Control: private header
74  bool hasPrivate(const String **val = nullptr) const { return hasDirective(HttpHdrCcType::CC_PRIVATE, &private_, val); }
75  void Private(const String &v) {
77  if (!v.size())
78  return;
79  // uses append for multi-line headers
80  if (private_.size() > 0)
81  private_.append(",");
82  private_.append(v);
83  }
85 
86  //manipulation for Cache-Control: no-cache header
87  bool hasNoCacheWithParameters() const { return hasNoCache() && no_cache.size(); }
88  bool hasNoCacheWithoutParameters() const { return hasNoCache() && !no_cache.size(); }
89  bool hasNoCache(const String **val = nullptr) const { return hasDirective(HttpHdrCcType::CC_NO_CACHE, &no_cache, val); }
90  void noCache(const String &v) {
92  if (!v.size())
93  return;
94  // uses append for multi-line headers
95  if (no_cache.size() > 0 && v.size() > 0)
96  no_cache.append(",");
97  no_cache.append(v);
98  }
100 
101  //manipulation for Cache-Control: no-store header
105 
106  //manipulation for Cache-Control: no-transform header
110 
111  //manipulation for Cache-Control: must-revalidate header
115 
116  //manipulation for Cache-Control: proxy-revalidate header
120 
121  //manipulation for Cache-Control: max-age header
122  bool hasMaxAge(int32_t *val = nullptr) const { return hasDirective(HttpHdrCcType::CC_MAX_AGE, max_age, val); }
125 
126  //manipulation for Cache-Control: s-maxage header
127  bool hasSMaxAge(int32_t *val = nullptr) const { return hasDirective(HttpHdrCcType::CC_S_MAXAGE, s_maxage, val); }
130 
131  //manipulation for Cache-Control: max-stale header
132  bool hasMaxStale(int32_t *val = nullptr) const { return hasDirective(HttpHdrCcType::CC_MAX_STALE, max_stale, val); }
133  // max-stale has a special value (MAX_STALE_ANY) which correspond to having
134  // the directive without a numeric specification, and directs to consider the object
135  // as always-expired.
138 
139  //manipulation for Cache-Control:min-fresh header
140  bool hasMinFresh(int32_t *val = nullptr) const { return hasDirective(HttpHdrCcType::CC_MIN_FRESH, min_fresh, val); }
141  void minFresh(int32_t v) {if (v < 0) return; setValue(min_fresh,v,HttpHdrCcType::CC_MIN_FRESH); }
143 
144  //manipulation for Cache-Control: only-if-cached header
148 
149  //manipulation for Cache-Control: stale-if-error header
150  bool hasStaleIfError(int32_t *val = nullptr) const { return hasDirective(HttpHdrCcType::CC_STALE_IF_ERROR, stale_if_error, val); }
153 
154  //manipulation for Cache-Control: immutable header
158 
160  bool isSet(HttpHdrCcType id) const {
162  return EBIT_TEST(mask, static_cast<long>(id));
163  }
164 
165  void packInto(Packable * p) const;
166 
172 private:
173  int32_t mask;
174  int32_t max_age;
175  int32_t s_maxage;
176  int32_t max_stale;
177  int32_t stale_if_error;
178  int32_t min_fresh;
181 
183  template<class Value>
184  bool hasDirective(const HttpHdrCcType hdrType, const Value &parsedVal, Value *outVal = nullptr) const {
185  if (isSet(hdrType)) {
186  if (outVal)
187  *outVal = parsedVal;
188  return true;
189  }
190  return false;
191  }
192 
194  void setMask(HttpHdrCcType id, bool newval=true) {
195  if (newval)
196  EBIT_SET(mask,static_cast<long>(id));
197  else
198  EBIT_CLR(mask, static_cast<long>(id));
199  }
200 
201  void setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting=true);
202 
203 public:
208 };
209 
210 class StatHist;
211 class StoreEntry;
212 
213 void httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist);
214 void httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val, double size, int count);
215 
216 std::ostream & operator<< (std::ostream &, HttpHdrCcType);
217 
218 #endif /* SQUID_SRC_HTTPHDRCC_H */
219 
#define EBIT_CLR(flag, bit)
Definition: defines.h:66
@ CC_NO_TRANSFORM
Definition: HttpHdrCc.h:25
void httpHdrCcUpdateStats(const HttpHdrCc *cc, StatHist *hist)
Definition: HttpHdrCc.cc:346
bool hasProxyRevalidate() const
Definition: HttpHdrCc.h:117
static const int32_t MAX_STALE_UNKNOWN
Definition: HttpHdrCc.h:50
void clearOnlyIfCached()
Definition: HttpHdrCc.h:147
bool hasPrivate(const String **val=nullptr) const
Definition: HttpHdrCc.h:74
String other
Definition: HttpHdrCc.h:207
#define EBIT_SET(flag, bit)
Definition: defines.h:65
@ CC_MIN_FRESH
Definition: HttpHdrCc.h:31
int32_t max_stale
Definition: HttpHdrCc.h:176
bool hasMinFresh(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:140
HttpHdrCcType
Definition: HttpHdrCc.h:20
@ CC_MAX_AGE
Definition: HttpHdrCc.h:28
void clearProxyRevalidate()
Definition: HttpHdrCc.h:119
bool hasImmutable() const
Definition: HttpHdrCc.h:155
void minFresh(int32_t v)
Definition: HttpHdrCc.h:141
std::ostream & operator<<(std::ostream &, HttpHdrCcType)
Definition: HttpHdrCc.cc:367
@ CC_MAX_STALE
Definition: HttpHdrCc.h:30
void clearMaxAge()
Definition: HttpHdrCc.h:124
int32_t s_maxage
Definition: HttpHdrCc.h:175
int32_t mask
Definition: HttpHdrCc.h:173
bool hasMaxStale(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:132
bool hasDirective(const HttpHdrCcType hdrType, const Value &parsedVal, Value *outVal=nullptr) const
implements typical has*() method logic
Definition: HttpHdrCc.h:184
@ CC_OTHER
Definition: HttpHdrCc.h:35
HttpHdrCc()
Definition: HttpHdrCc.h:57
void maxStale(int32_t v)
Definition: HttpHdrCc.h:136
static const int32_t MAX_STALE_ANY
Definition: HttpHdrCc.h:53
@ CC_IMMUTABLE
Definition: HttpHdrCc.h:34
void clearPublic()
Definition: HttpHdrCc.h:71
int size
Definition: ModDevPoll.cc:69
void clearSMaxAge()
Definition: HttpHdrCc.h:129
void maxAge(int32_t v)
Definition: HttpHdrCc.h:123
void sMaxAge(int32_t v)
Definition: HttpHdrCc.h:128
void clearNoStore()
Definition: HttpHdrCc.h:104
bool parse(const String &s)
parse a header-string and fill in appropriate values.
Definition: HttpHdrCc.cc:120
bool hasNoCacheWithParameters() const
Definition: HttpHdrCc.h:87
void setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting=true)
Definition: HttpHdrCc.cc:103
void append(char const *buf, int len)
Definition: String.cc:130
int32_t min_fresh
Definition: HttpHdrCc.h:178
void noStore(bool v)
Definition: HttpHdrCc.h:103
void staleIfError(int32_t v)
Definition: HttpHdrCc.h:151
#define EBIT_TEST(flag, bit)
Definition: defines.h:67
void Public(bool v)
Definition: HttpHdrCc.h:70
@ CC_MUST_REVALIDATE
Definition: HttpHdrCc.h:26
bool hasSMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:127
@ CC_NO_STORE
Definition: HttpHdrCc.h:24
void noTransform(bool v)
Definition: HttpHdrCc.h:108
bool hasNoCache(const String **val=nullptr) const
Definition: HttpHdrCc.h:89
bool hasOnlyIfCached() const
Definition: HttpHdrCc.h:145
#define assert(EX)
Definition: assert.h:17
void mustRevalidate(bool v)
Definition: HttpHdrCc.h:113
void clearMustRevalidate()
Definition: HttpHdrCc.h:114
void httpHdrCcStatDumper(StoreEntry *sentry, int idx, double val, double size, int count)
Definition: HttpHdrCc.cc:356
@ CC_S_MAXAGE
Definition: HttpHdrCc.h:29
void onlyIfCached(bool v)
Definition: HttpHdrCc.h:146
int32_t stale_if_error
Definition: HttpHdrCc.h:177
void clearPrivate()
Definition: HttpHdrCc.h:84
bool isSet(HttpHdrCcType id) const
check whether the attribute value supplied by id is set
Definition: HttpHdrCc.h:160
bool hasStaleIfError(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:150
void clearNoCache()
Definition: HttpHdrCc.h:99
static const int32_t S_MAXAGE_UNKNOWN
Definition: HttpHdrCc.h:49
void Immutable(bool v)
Definition: HttpHdrCc.h:156
String no_cache
List of headers sent as value for CC:no-cache="...". May be empty/undefined if the value is missing.
Definition: HttpHdrCc.h:180
@ CC_PRIVATE
Definition: HttpHdrCc.h:22
void proxyRevalidate(bool v)
Definition: HttpHdrCc.h:118
bool hasMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:122
size_type size() const
Definition: SquidString.h:73
void clearMinFresh()
Definition: HttpHdrCc.h:142
static const int32_t STALE_IF_ERROR_UNKNOWN
Definition: HttpHdrCc.h:54
@ CC_STALE_IF_ERROR
Definition: HttpHdrCc.h:33
void setMask(HttpHdrCcType id, bool newval=true)
low-level part of the public set method, performs no checks
Definition: HttpHdrCc.h:194
@ CC_PROXY_REVALIDATE
Definition: HttpHdrCc.h:27
bool hasNoTransform() const
Definition: HttpHdrCc.h:107
bool hasNoCacheWithoutParameters() const
Definition: HttpHdrCc.h:88
bool hasMustRevalidate() const
Definition: HttpHdrCc.h:112
void noCache(const String &v)
Definition: HttpHdrCc.h:90
void Private(const String &v)
Definition: HttpHdrCc.h:75
void clearMaxStale()
Definition: HttpHdrCc.h:137
@ CC_ENUM_END
Definition: HttpHdrCc.h:36
@ CC_NO_CACHE
Definition: HttpHdrCc.h:23
void clear()
reset data-members to default state
Definition: HttpHdrCc.cc:95
void packInto(Packable *p) const
Definition: HttpHdrCc.cc:272
bool hasNoStore() const
Definition: HttpHdrCc.h:102
String private_
List of headers sent as value for CC:private="...". May be empty/undefined if the value is missing.
Definition: HttpHdrCc.h:179
void clearImmutable()
Definition: HttpHdrCc.h:157
void clearNoTransform()
Definition: HttpHdrCc.h:109
@ CC_PUBLIC
Definition: HttpHdrCc.h:21
static const int32_t MAX_AGE_UNKNOWN
Definition: HttpHdrCc.h:48
bool hasPublic() const
Definition: HttpHdrCc.h:69
static const int32_t MIN_FRESH_UNKNOWN
Definition: HttpHdrCc.h:55
void clearStaleIfError()
Definition: HttpHdrCc.h:152
int32_t max_age
Definition: HttpHdrCc.h:174
void clean()
Definition: String.cc:103
MEMPROXY_CLASS(HttpHdrCc)
@ CC_ONLY_IF_CACHED
Definition: HttpHdrCc.h:32

 

Introduction

Documentation

Support

Miscellaneous