]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.h
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_HTTPHDRCC_H
10 #define SQUID_HTTPHDRCC_H
14 #include "mem/forward.h"
15 #include "SquidString.h"
20 enum HttpHdrCcType
: unsigned char {
34 CC_IMMUTABLE
, /* RFC 8246 */
36 CC_ENUM_END
/* also used to mean "invalid" */
39 /** Http Cache-Control header representation
41 * Store and parse the Cache-Control HTTP header.
45 MEMPROXY_CLASS(HttpHdrCc
);
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
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
) {}
62 /// reset data-members to default state
65 /// parse a header-string and fill in appropriate values.
66 bool parse(const String
& s
);
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);}
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);
79 // uses append for multi-line headers
80 if (private_
.size() > 0)
84 void clearPrivate() {setMask(HttpHdrCcType::CC_PRIVATE
,false); private_
.clean();}
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);
94 // uses append for multi-line headers
95 if (no_cache
.size() > 0 && v
.size() > 0)
99 void clearNoCache() {setMask(HttpHdrCcType::CC_NO_CACHE
,false); no_cache
.clean();}
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);}
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);}
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);}
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);}
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);}
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);}
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);}
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
); }
142 void clearMinFresh() {setValue(min_fresh
,MIN_FRESH_UNKNOWN
,HttpHdrCcType::CC_MIN_FRESH
,false);}
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);}
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);}
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);}
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
));
165 void packInto(Packable
* p
) const;
167 /** bit-mask representing what header values are set among those
168 * recognized by squid.
170 * managed via EBIT_SET/TEST/CLR
177 int32_t stale_if_error
;
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.
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
)) {
193 /// low-level part of the public set method, performs no checks
194 void setMask(HttpHdrCcType id
, bool newval
=true) {
196 EBIT_SET(mask
,static_cast<long>(id
));
198 EBIT_CLR(mask
, static_cast<long>(id
));
201 void setValue(int32_t &value
, int32_t new_value
, HttpHdrCcType hdr
, bool setting
=true);
204 /**comma-separated representation of the header values which were
205 * received but are not recognized.
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
);
217 std::ostream
& operator<< (std::ostream
&, HttpHdrCcType
);
219 #endif /* SQUID_HTTPHDRCC_H */