]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / HttpHdrCc.h
1 /*
2 * Copyright (C) 1996-2020 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,
22 CC_PRIVATE,
23 CC_NO_CACHE,
24 CC_NO_STORE,
25 CC_NO_TRANSFORM,
26 CC_MUST_REVALIDATE,
27 CC_PROXY_REVALIDATE,
28 CC_MAX_AGE,
29 CC_S_MAXAGE,
30 CC_MAX_STALE,
31 CC_MIN_FRESH,
32 CC_ONLY_IF_CACHED,
33 CC_STALE_IF_ERROR,
34 CC_IMMUTABLE, /* RFC 8246 */
35 CC_OTHER,
36 CC_ENUM_END /* also used to mean "invalid" */
37 };
38
39 /** Http Cache-Control header representation
40 *
41 * Store and parse the Cache-Control HTTP header.
42 */
43 class HttpHdrCc
44 {
45 MEMPROXY_CLASS(HttpHdrCc);
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
51 ///used to mark a valueless Cache-Control: max-stale directive, which instructs
52 /// us to treat responses of any age as fresh
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
57 HttpHdrCc() :
58 mask(0), max_age(MAX_AGE_UNKNOWN), s_maxage(S_MAXAGE_UNKNOWN),
59 max_stale(MAX_STALE_UNKNOWN), stale_if_error(STALE_IF_ERROR_UNKNOWN),
60 min_fresh(MIN_FRESH_UNKNOWN) {}
61
62 /// reset data-members to default state
63 void clear();
64
65 /// parse a header-string and fill in appropriate values.
66 bool parse(const String & s);
67
68 //manipulation for Cache-Control: public header
69 bool hasPublic() const {return isSet(HttpHdrCcType::CC_PUBLIC);}
70 void Public(bool v) {setMask(HttpHdrCcType::CC_PUBLIC,v);}
71 void clearPublic() {setMask(HttpHdrCcType::CC_PUBLIC,false);}
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) {
76 setMask(HttpHdrCcType::CC_PRIVATE,true);
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 }
84 void clearPrivate() {setMask(HttpHdrCcType::CC_PRIVATE,false); private_.clean();}
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) {
91 setMask(HttpHdrCcType::CC_NO_CACHE,true);
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 }
99 void clearNoCache() {setMask(HttpHdrCcType::CC_NO_CACHE,false); no_cache.clean();}
100
101 //manipulation for Cache-Control: no-store header
102 bool hasNoStore() const {return isSet(HttpHdrCcType::CC_NO_STORE);}
103 void noStore(bool v) {setMask(HttpHdrCcType::CC_NO_STORE,v);}
104 void clearNoStore() {setMask(HttpHdrCcType::CC_NO_STORE,false);}
105
106 //manipulation for Cache-Control: no-transform header
107 bool hasNoTransform() const {return isSet(HttpHdrCcType::CC_NO_TRANSFORM);}
108 void noTransform(bool v) {setMask(HttpHdrCcType::CC_NO_TRANSFORM,v);}
109 void clearNoTransform() {setMask(HttpHdrCcType::CC_NO_TRANSFORM,false);}
110
111 //manipulation for Cache-Control: must-revalidate header
112 bool hasMustRevalidate() const {return isSet(HttpHdrCcType::CC_MUST_REVALIDATE);}
113 void mustRevalidate(bool v) {setMask(HttpHdrCcType::CC_MUST_REVALIDATE,v);}
114 void clearMustRevalidate() {setMask(HttpHdrCcType::CC_MUST_REVALIDATE,false);}
115
116 //manipulation for Cache-Control: proxy-revalidate header
117 bool hasProxyRevalidate() const {return isSet(HttpHdrCcType::CC_PROXY_REVALIDATE);}
118 void proxyRevalidate(bool v) {setMask(HttpHdrCcType::CC_PROXY_REVALIDATE,v);}
119 void clearProxyRevalidate() {setMask(HttpHdrCcType::CC_PROXY_REVALIDATE,false);}
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); }
123 void maxAge(int32_t v) {setValue(max_age,v,HttpHdrCcType::CC_MAX_AGE); }
124 void clearMaxAge() {setValue(max_age,MAX_AGE_UNKNOWN,HttpHdrCcType::CC_MAX_AGE,false);}
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); }
128 void sMaxAge(int32_t v) {setValue(s_maxage,v,HttpHdrCcType::CC_S_MAXAGE); }
129 void clearSMaxAge() {setValue(s_maxage,MAX_AGE_UNKNOWN,HttpHdrCcType::CC_S_MAXAGE,false);}
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.
136 void maxStale(int32_t v) {setValue(max_stale,v,HttpHdrCcType::CC_MAX_STALE);}
137 void clearMaxStale() {setValue(max_stale,MAX_STALE_UNKNOWN,HttpHdrCcType::CC_MAX_STALE,false);}
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); }
142 void clearMinFresh() {setValue(min_fresh,MIN_FRESH_UNKNOWN,HttpHdrCcType::CC_MIN_FRESH,false);}
143
144 //manipulation for Cache-Control: only-if-cached header
145 bool hasOnlyIfCached() const {return isSet(HttpHdrCcType::CC_ONLY_IF_CACHED);}
146 void onlyIfCached(bool v) {setMask(HttpHdrCcType::CC_ONLY_IF_CACHED,v);}
147 void clearOnlyIfCached() {setMask(HttpHdrCcType::CC_ONLY_IF_CACHED,false);}
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); }
151 void staleIfError(int32_t v) {setValue(stale_if_error,v,HttpHdrCcType::CC_STALE_IF_ERROR); }
152 void clearStaleIfError() {setValue(stale_if_error,STALE_IF_ERROR_UNKNOWN,HttpHdrCcType::CC_STALE_IF_ERROR,false);}
153
154 //manipulation for Cache-Control: immutable header
155 bool hasImmutable() const {return isSet(HttpHdrCcType::CC_IMMUTABLE);}
156 void Immutable(bool v) {setMask(HttpHdrCcType::CC_IMMUTABLE,v);}
157 void clearImmutable() {setMask(HttpHdrCcType::CC_IMMUTABLE,false);}
158
159 /// check whether the attribute value supplied by id is set
160 bool isSet(HttpHdrCcType id) const {
161 assert(id < HttpHdrCcType::CC_ENUM_END);
162 return EBIT_TEST(mask, static_cast<long>(id));
163 }
164
165 void packInto(Packable * p) const;
166
167 /** bit-mask representing what header values are set among those
168 * recognized by squid.
169 *
170 * managed via EBIT_SET/TEST/CLR
171 */
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;
179 String private_; ///< List of headers sent as value for CC:private="...". May be empty/undefined if the value is missing.
180 String no_cache; ///< List of headers sent as value for CC:no-cache="...". May be empty/undefined if the value is missing.
181
182 /// implements typical has*() method logic
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
193 /// low-level part of the public set method, performs no checks
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:
204 /**comma-separated representation of the header values which were
205 * received but are not recognized.
206 */
207 String other;
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