]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.h
337130c444ef3fbfe588bed70a8cbd8c6d6d6a85
[thirdparty/squid.git] / src / HttpHdrCc.h
1 /*
2 * Copyright (C) 1996-2016 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 "dlink.h"
13 #include "mem/forward.h"
14 #include "SquidString.h"
15 #include <iosfwd>
16
17 class Packable;
18
19 enum HttpHdrCcType : unsigned char {
20 CC_PUBLIC = 0,
21 CC_PRIVATE,
22 CC_NO_CACHE,
23 CC_NO_STORE,
24 CC_NO_TRANSFORM,
25 CC_MUST_REVALIDATE,
26 CC_PROXY_REVALIDATE,
27 CC_MAX_AGE,
28 CC_S_MAXAGE,
29 CC_MAX_STALE,
30 CC_MIN_FRESH,
31 CC_ONLY_IF_CACHED,
32 CC_STALE_IF_ERROR,
33 CC_IMMUTABLE, /* draft-mcmanus-immutable-00 */
34 CC_OTHER,
35 CC_ENUM_END /* also used to mean "invalid" */
36 };
37
38 /** Http Cache-Control header representation
39 *
40 * Store and parse the Cache-Control HTTP header.
41 */
42 class HttpHdrCc
43 {
44 MEMPROXY_CLASS(HttpHdrCc);
45
46 public:
47 static const int32_t MAX_AGE_UNKNOWN=-1; //max-age is unset
48 static const int32_t S_MAXAGE_UNKNOWN=-1; //s-maxage is unset
49 static const int32_t MAX_STALE_UNKNOWN=-1; //max-stale is unset
50 ///used to mark a valueless Cache-Control: max-stale directive, which instructs
51 /// us to treat responses of any age as fresh
52 static const int32_t MAX_STALE_ANY=0x7fffffff;
53 static const int32_t STALE_IF_ERROR_UNKNOWN=-1; //stale_if_error is unset
54 static const int32_t MIN_FRESH_UNKNOWN=-1; //min_fresh is unset
55
56 HttpHdrCc() :
57 mask(0), max_age(MAX_AGE_UNKNOWN), s_maxage(S_MAXAGE_UNKNOWN),
58 max_stale(MAX_STALE_UNKNOWN), stale_if_error(STALE_IF_ERROR_UNKNOWN),
59 min_fresh(MIN_FRESH_UNKNOWN) {}
60
61 /// reset data-members to default state
62 void clear();
63
64 /// parse a header-string and fill in appropriate values.
65 bool parse(const String & s);
66
67 //manipulation for Cache-Control: public header
68 bool hasPublic() const {return isSet(HttpHdrCcType::CC_PUBLIC);}
69 bool Public() 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 {return isSet(HttpHdrCcType::CC_PRIVATE);}
75 const String &Private() const {return private_;}
76 void Private(const String &v) {
77 setMask(HttpHdrCcType::CC_PRIVATE,true);
78 if (!v.size())
79 return;
80 // uses append for multi-line headers
81 if (private_.size() > 0)
82 private_.append(",");
83 private_.append(v);
84 }
85 void clearPrivate() {setMask(HttpHdrCcType::CC_PRIVATE,false); private_.clean();}
86
87 //manipulation for Cache-Control: no-cache header
88 bool hasNoCache() const {return isSet(HttpHdrCcType::CC_NO_CACHE);}
89 const String &noCache() const {return no_cache;}
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 bool noStore() const {return isSet(HttpHdrCcType::CC_NO_STORE);}
104 void noStore(bool v) {setMask(HttpHdrCcType::CC_NO_STORE,v);}
105 void clearNoStore() {setMask(HttpHdrCcType::CC_NO_STORE,false);}
106
107 //manipulation for Cache-Control: no-transform header
108 bool hasNoTransform() const {return isSet(HttpHdrCcType::CC_NO_TRANSFORM);}
109 bool noTransform() const {return isSet(HttpHdrCcType::CC_NO_TRANSFORM);}
110 void noTransform(bool v) {setMask(HttpHdrCcType::CC_NO_TRANSFORM,v);}
111 void clearNoTransform() {setMask(HttpHdrCcType::CC_NO_TRANSFORM,false);}
112
113 //manipulation for Cache-Control: must-revalidate header
114 bool hasMustRevalidate() const {return isSet(HttpHdrCcType::CC_MUST_REVALIDATE);}
115 bool mustRevalidate() const {return isSet(HttpHdrCcType::CC_MUST_REVALIDATE);}
116 void mustRevalidate(bool v) {setMask(HttpHdrCcType::CC_MUST_REVALIDATE,v);}
117 void clearMustRevalidate() {setMask(HttpHdrCcType::CC_MUST_REVALIDATE,false);}
118
119 //manipulation for Cache-Control: proxy-revalidate header
120 bool hasProxyRevalidate() const {return isSet(HttpHdrCcType::CC_PROXY_REVALIDATE);}
121 bool proxyRevalidate() const {return isSet(HttpHdrCcType::CC_PROXY_REVALIDATE);}
122 void proxyRevalidate(bool v) {setMask(HttpHdrCcType::CC_PROXY_REVALIDATE,v);}
123 void clearProxyRevalidate() {setMask(HttpHdrCcType::CC_PROXY_REVALIDATE,false);}
124
125 //manipulation for Cache-Control: max-age header
126 bool hasMaxAge() const {return isSet(HttpHdrCcType::CC_MAX_AGE);}
127 int32_t maxAge() const { return max_age;}
128 void maxAge(int32_t v) {setValue(max_age,v,HttpHdrCcType::CC_MAX_AGE); }
129 void clearMaxAge() {setValue(max_age,MAX_AGE_UNKNOWN,HttpHdrCcType::CC_MAX_AGE,false);}
130
131 //manipulation for Cache-Control: s-maxage header
132 bool hasSMaxAge() const {return isSet(HttpHdrCcType::CC_S_MAXAGE);}
133 int32_t sMaxAge() const { return s_maxage;}
134 void sMaxAge(int32_t v) {setValue(s_maxage,v,HttpHdrCcType::CC_S_MAXAGE); }
135 void clearSMaxAge() {setValue(s_maxage,MAX_AGE_UNKNOWN,HttpHdrCcType::CC_S_MAXAGE,false);}
136
137 //manipulation for Cache-Control: max-stale header
138 bool hasMaxStale() const {return isSet(HttpHdrCcType::CC_MAX_STALE);}
139 int32_t maxStale() const { return max_stale;}
140 // max-stale has a special value (MAX_STALE_ANY) which correspond to having
141 // the directive without a numeric specification, and directs to consider the object
142 // as always-expired.
143 void maxStale(int32_t v) {setValue(max_stale,v,HttpHdrCcType::CC_MAX_STALE);}
144 void clearMaxStale() {setValue(max_stale,MAX_STALE_UNKNOWN,HttpHdrCcType::CC_MAX_STALE,false);}
145
146 //manipulation for Cache-Control:min-fresh header
147 bool hasMinFresh() const {return isSet(HttpHdrCcType::CC_MIN_FRESH);}
148 int32_t minFresh() const { return min_fresh;}
149 void minFresh(int32_t v) {if (v < 0) return; setValue(min_fresh,v,HttpHdrCcType::CC_MIN_FRESH); }
150 void clearMinFresh() {setValue(min_fresh,MIN_FRESH_UNKNOWN,HttpHdrCcType::CC_MIN_FRESH,false);}
151
152 //manipulation for Cache-Control: only-if-cached header
153 bool hasOnlyIfCached() const {return isSet(HttpHdrCcType::CC_ONLY_IF_CACHED);}
154 bool onlyIfCached() const {return isSet(HttpHdrCcType::CC_ONLY_IF_CACHED);}
155 void onlyIfCached(bool v) {setMask(HttpHdrCcType::CC_ONLY_IF_CACHED,v);}
156 void clearOnlyIfCached() {setMask(HttpHdrCcType::CC_ONLY_IF_CACHED,false);}
157
158 //manipulation for Cache-Control: stale-if-error header
159 bool hasStaleIfError() const {return isSet(HttpHdrCcType::CC_STALE_IF_ERROR);}
160 int32_t staleIfError() const { return stale_if_error;}
161 void staleIfError(int32_t v) {setValue(stale_if_error,v,HttpHdrCcType::CC_STALE_IF_ERROR); }
162 void clearStaleIfError() {setValue(stale_if_error,STALE_IF_ERROR_UNKNOWN,HttpHdrCcType::CC_STALE_IF_ERROR,false);}
163
164 //manipulation for Cache-Control: immutable header
165 bool Immutable() const {return isSet(HttpHdrCcType::CC_IMMUTABLE);}
166 void Immutable(bool v) {setMask(HttpHdrCcType::CC_IMMUTABLE,v);}
167 void clearImmutable() {setMask(HttpHdrCcType::CC_IMMUTABLE,false);}
168
169 /// check whether the attribute value supplied by id is set
170 _SQUID_INLINE_ bool isSet(HttpHdrCcType id) const;
171
172 void packInto(Packable * p) const;
173
174 /** bit-mask representing what header values are set among those
175 * recognized by squid.
176 *
177 * managed via EBIT_SET/TEST/CLR
178 */
179 private:
180 int32_t mask;
181 int32_t max_age;
182 int32_t s_maxage;
183 int32_t max_stale;
184 int32_t stale_if_error;
185 int32_t min_fresh;
186 String private_; ///< List of headers sent as value for CC:private="...". May be empty/undefined if the value is missing.
187 String no_cache; ///< List of headers sent as value for CC:no-cache="...". May be empty/undefined if the value is missing.
188
189 /// low-level part of the public set method, performs no checks
190 _SQUID_INLINE_ void setMask(HttpHdrCcType id, bool newval=true);
191 _SQUID_INLINE_ void setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting=true);
192
193 public:
194 /**comma-separated representation of the header values which were
195 * received but are not recognized.
196 */
197 String other;
198 };
199
200 class StatHist;
201 class StoreEntry;
202
203 void httpHdrCcInitModule(void);
204 void httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist);
205 void httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val, double size, int count);
206
207 std::ostream&
208 operator<< (std::ostream &, HttpHdrCcType);
209
210 #if _USE_INLINE_
211 #include "HttpHdrCc.cci"
212 #endif
213
214 #endif /* SQUID_HTTPHDRCC_H */
215