HttpHdrCc.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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_HTTPHDRCC_H
10 #define SQUID_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, max_stale, 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 httpHdrCcInitModule(void);
214 void httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist);
215 void httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val, double size, int count);
216 
217 std::ostream & operator<< (std::ostream &, HttpHdrCcType);
218 
219 #endif /* SQUID_HTTPHDRCC_H */
220 
void clearStaleIfError()
Definition: HttpHdrCc.h:152
int32_t max_age
Definition: HttpHdrCc.h:174
void noCache(const String &v)
Definition: HttpHdrCc.h:90
void Private(const String &v)
Definition: HttpHdrCc.h:75
static const int32_t MAX_STALE_UNKNOWN
Definition: HttpHdrCc.h:50
void clearOnlyIfCached()
Definition: HttpHdrCc.h:147
#define assert(EX)
Definition: assert.h:17
void clearImmutable()
Definition: HttpHdrCc.h:157
String other
Definition: HttpHdrCc.h:207
static const int32_t MIN_FRESH_UNKNOWN
Definition: HttpHdrCc.h:55
void clearMaxAge()
Definition: HttpHdrCc.h:124
bool hasDirective(const HttpHdrCcType hdrType, const Value &parsedVal, Value *outVal=nullptr) const
implements typical has*() method logic
Definition: HttpHdrCc.h:184
void clearPublic()
Definition: HttpHdrCc.h:71
void clearSMaxAge()
Definition: HttpHdrCc.h:129
MEMPROXY_CLASS(HttpHdrCc)
void httpHdrCcStatDumper(StoreEntry *sentry, int idx, double val, double size, int count)
Definition: HttpHdrCc.cc:330
char * p
Definition: membanger.c:43
bool hasMinFresh(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:140
bool hasImmutable() const
Definition: HttpHdrCc.h:155
bool hasPublic() const
Definition: HttpHdrCc.h:69
size_type size() const
Definition: SquidString.h:71
void append(char const *buf, int len)
Definition: String.cc:161
void minFresh(int32_t v)
Definition: HttpHdrCc.h:141
void clearProxyRevalidate()
Definition: HttpHdrCc.h:119
int32_t mask
Definition: HttpHdrCc.h:173
void maxAge(int32_t v)
Definition: HttpHdrCc.h:123
HttpHdrCc()
Definition: HttpHdrCc.h:57
void clearMustRevalidate()
Definition: HttpHdrCc.h:114
bool hasMustRevalidate() const
Definition: HttpHdrCc.h:112
void httpHdrCcUpdateStats(const HttpHdrCc *cc, StatHist *hist)
Definition: HttpHdrCc.cc:320
void maxStale(int32_t v)
Definition: HttpHdrCc.h:136
void clearPrivate()
Definition: HttpHdrCc.h:84
void noTransform(bool v)
Definition: HttpHdrCc.h:108
bool hasNoCache(const String **val=nullptr) const
Definition: HttpHdrCc.h:89
static const int32_t MAX_STALE_ANY
Definition: HttpHdrCc.h:53
void mustRevalidate(bool v)
Definition: HttpHdrCc.h:113
#define EBIT_CLR(flag, bit)
Definition: defines.h:106
static const int32_t S_MAXAGE_UNKNOWN
Definition: HttpHdrCc.h:49
void setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting=true)
Definition: HttpHdrCc.cc:77
int32_t min_fresh
Definition: HttpHdrCc.h:178
int32_t s_maxage
Definition: HttpHdrCc.h:175
int32_t stale_if_error
Definition: HttpHdrCc.h:177
bool parse(const String &s)
parse a header-string and fill in appropriate values.
Definition: HttpHdrCc.cc:94
bool hasMaxStale(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:132
void Public(bool v)
Definition: HttpHdrCc.h:70
void sMaxAge(int32_t v)
Definition: HttpHdrCc.h:128
void clearNoStore()
Definition: HttpHdrCc.h:104
bool hasNoTransform() const
Definition: HttpHdrCc.h:107
bool hasProxyRevalidate() const
Definition: HttpHdrCc.h:117
bool hasPrivate(const String **val=nullptr) const
Definition: HttpHdrCc.h:74
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
void Immutable(bool v)
Definition: HttpHdrCc.h:156
std::ostream & operator<<(std::ostream &, HttpHdrCcType)
Definition: HttpHdrCc.cc:343
void httpHdrCcInitModule(void)
Module initialization hook.
Definition: HttpHdrCc.cc:60
#define EBIT_SET(flag, bit)
Definition: defines.h:105
void noStore(bool v)
Definition: HttpHdrCc.h:103
void staleIfError(int32_t v)
Definition: HttpHdrCc.h:151
bool hasMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:122
bool hasNoCacheWithParameters() const
Definition: HttpHdrCc.h:87
bool hasNoCacheWithoutParameters() const
Definition: HttpHdrCc.h:88
void onlyIfCached(bool v)
Definition: HttpHdrCc.h:146
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 clearNoCache()
Definition: HttpHdrCc.h:99
void proxyRevalidate(bool v)
Definition: HttpHdrCc.h:118
void packInto(Packable *p) const
Definition: HttpHdrCc.cc:246
void clearMinFresh()
Definition: HttpHdrCc.h:142
void clean()
Definition: String.cc:125
bool hasNoStore() const
Definition: HttpHdrCc.h:102
void clearMaxStale()
Definition: HttpHdrCc.h:137
bool isSet(HttpHdrCcType id) const
check whether the attribute value supplied by id is set
Definition: HttpHdrCc.h:160
HttpHdrCcType
Definition: HttpHdrCc.h:20
bool hasOnlyIfCached() const
Definition: HttpHdrCc.h:145
void clear()
reset data-members to default state
Definition: HttpHdrCc.cc:69
static const int32_t MAX_AGE_UNKNOWN
Definition: HttpHdrCc.h:48
int32_t max_stale
Definition: HttpHdrCc.h:176
static const int32_t STALE_IF_ERROR_UNKNOWN
Definition: HttpHdrCc.h:54
#define EBIT_TEST(flag, bit)
Definition: defines.h:107
bool hasSMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:127
void setMask(HttpHdrCcType id, bool newval=true)
low-level part of the public set method, performs no checks
Definition: HttpHdrCc.h:194
int size
Definition: ModDevPoll.cc:77
void clearNoTransform()
Definition: HttpHdrCc.h:109
bool hasStaleIfError(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:150

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors