]>
Commit | Line | Data |
---|---|---|
7faf2bdb | 1 | /* |
4ac4a490 | 2 | * Copyright (C) 1996-2017 The Squid Software Foundation and contributors |
7faf2bdb | 3 | * |
bbc27441 AJ |
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. | |
7faf2bdb | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 65 HTTP Cache Control Header */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
ea295fe4 | 12 | #include "base/LookupTable.h" |
602d9612 | 13 | #include "HttpHdrCc.h" |
e6ccf245 | 14 | #include "HttpHeader.h" |
db2de30a | 15 | #include "HttpHeaderFieldStat.h" |
e1656dc4 | 16 | #include "HttpHeaderStat.h" |
a5bac1d2 | 17 | #include "HttpHeaderTools.h" |
65e41a45 | 18 | #include "sbuf/SBuf.h" |
00a7574e FC |
19 | #include "StatHist.h" |
20 | #include "Store.h" | |
28204b3b | 21 | #include "StrList.h" |
ed6e9fb9 | 22 | #include "util.h" |
ce394734 | 23 | |
ce394734 | 24 | #include <map> |
ea295fe4 | 25 | #include <vector> |
6b1c9146 | 26 | #include <ostream> |
ea295fe4 FC |
27 | |
28 | // invariant: row[j].id == j | |
1da82544 FC |
29 | static LookupTable<HttpHdrCcType>::Record CcAttrs[] = { |
30 | {"public", HttpHdrCcType::CC_PUBLIC}, | |
31 | {"private", HttpHdrCcType::CC_PRIVATE}, | |
32 | {"no-cache", HttpHdrCcType::CC_NO_CACHE}, | |
33 | {"no-store", HttpHdrCcType::CC_NO_STORE}, | |
34 | {"no-transform", HttpHdrCcType::CC_NO_TRANSFORM}, | |
35 | {"must-revalidate", HttpHdrCcType::CC_MUST_REVALIDATE}, | |
36 | {"proxy-revalidate", HttpHdrCcType::CC_PROXY_REVALIDATE}, | |
37 | {"max-age", HttpHdrCcType::CC_MAX_AGE}, | |
38 | {"s-maxage", HttpHdrCcType::CC_S_MAXAGE}, | |
39 | {"max-stale", HttpHdrCcType::CC_MAX_STALE}, | |
40 | {"min-fresh", HttpHdrCcType::CC_MIN_FRESH}, | |
41 | {"only-if-cached", HttpHdrCcType::CC_ONLY_IF_CACHED}, | |
42 | {"stale-if-error", HttpHdrCcType::CC_STALE_IF_ERROR}, | |
caf65351 | 43 | {"immutable", HttpHdrCcType::CC_IMMUTABLE}, |
1da82544 FC |
44 | {"Other,", HttpHdrCcType::CC_OTHER}, /* ',' will protect from matches */ |
45 | {nullptr, HttpHdrCcType::CC_ENUM_END} | |
26735116 | 46 | }; |
1da82544 FC |
47 | LookupTable<HttpHdrCcType> ccLookupTable(HttpHdrCcType::CC_OTHER,CcAttrs); |
48 | std::vector<HttpHeaderFieldStat> ccHeaderStats(HttpHdrCcType::CC_ENUM_END); | |
7faf2bdb | 49 | |
d74ad83f | 50 | /// used to walk a table of http_header_cc_type structs |
1da82544 | 51 | HttpHdrCcType &operator++ (HttpHdrCcType &aHeader) |
e6ccf245 | 52 | { |
1f1ae50a | 53 | int tmp = (int)aHeader; |
1da82544 | 54 | aHeader = (HttpHdrCcType)(++tmp); |
e6ccf245 | 55 | return aHeader; |
56 | } | |
57 | ||
c23f7c32 | 58 | /// Module initialization hook |
7faf2bdb | 59 | void |
9bc73deb | 60 | httpHdrCcInitModule(void) |
7faf2bdb | 61 | { |
ea295fe4 | 62 | // check invariant on initialization table |
1da82544 FC |
63 | for (unsigned int j = 0; CcAttrs[j].name != nullptr; ++j) { |
64 | assert (static_cast<int>(CcAttrs[j].id) == j); | |
ce394734 | 65 | } |
de336bbe | 66 | } |
67 | ||
ce394734 FC |
68 | void |
69 | HttpHdrCc::clear() | |
7faf2bdb | 70 | { |
7ebe76de | 71 | *this=HttpHdrCc(); |
7faf2bdb | 72 | } |
73 | ||
22056ef8 AJ |
74 | /// set a data member to a new value, and set the corresponding mask-bit. |
75 | /// if setting is false, then the mask-bit is cleared. | |
76 | void | |
77 | HttpHdrCc::setValue(int32_t &value, int32_t new_value, HttpHdrCcType hdr, bool setting) | |
78 | { | |
79 | if (setting) { | |
80 | if (new_value < 0) { | |
81 | debugs(65, 3, "rejecting negative-value Cache-Control directive " << hdr | |
82 | << " value " << new_value); | |
83 | return; | |
84 | } | |
85 | } else { | |
86 | new_value = -1; //rely on the convention that "unknown" is -1 | |
87 | } | |
88 | ||
89 | value = new_value; | |
90 | setMask(hdr,setting); | |
91 | } | |
92 | ||
ce394734 | 93 | bool |
7ebe76de | 94 | HttpHdrCc::parse(const String & str) |
7faf2bdb | 95 | { |
96 | const char *item; | |
f53969cc | 97 | const char *p; /* '=' parameter */ |
7faf2bdb | 98 | const char *pos = NULL; |
7faf2bdb | 99 | int ilen; |
1b2e9616 | 100 | int nlen; |
7faf2bdb | 101 | |
7faf2bdb | 102 | /* iterate through comma separated list */ |
62e76326 | 103 | |
ce394734 | 104 | while (strListGetItem(&str, ',', &item, &ilen, &pos)) { |
1b2e9616 | 105 | /* isolate directive name */ |
62e76326 | 106 | |
a38ec4b1 FC |
107 | if ((p = (const char *)memchr(item, '=', ilen)) && (p - item < ilen)) { |
108 | nlen = p - item; | |
109 | ++p; | |
110 | } else { | |
1b2e9616 | 111 | nlen = ilen; |
a38ec4b1 | 112 | } |
62e76326 | 113 | |
114 | /* find type */ | |
1da82544 | 115 | const HttpHdrCcType type = ccLookupTable.lookup(SBuf(item,nlen)); |
62e76326 | 116 | |
aa62670a | 117 | // ignore known duplicate directives |
f9517ad8 | 118 | if (isSet(type)) { |
1da82544 | 119 | if (type != HttpHdrCcType::CC_OTHER) { |
e8466ea9 | 120 | debugs(65, 2, "hdr cc: ignoring duplicate cache-directive: near '" << item << "' in '" << str << "'"); |
ea295fe4 | 121 | ++ ccHeaderStats[type].repCount; |
aa62670a AR |
122 | continue; |
123 | } | |
62e76326 | 124 | } |
125 | ||
4ce6e3b5 | 126 | /* special-case-parsing and attribute-setting */ |
62e76326 | 127 | switch (type) { |
128 | ||
1da82544 | 129 | case HttpHdrCcType::CC_MAX_AGE: |
d74ad83f | 130 | if (!p || !httpHeaderParseInt(p, &max_age) || max_age < 0) { |
bf8fe701 | 131 | debugs(65, 2, "cc: invalid max-age specs near '" << item << "'"); |
4ce6e3b5 | 132 | clearMaxAge(); |
1ba0611a | 133 | } else { |
d74ad83f | 134 | setMask(type,true); |
62e76326 | 135 | } |
62e76326 | 136 | break; |
137 | ||
1da82544 | 138 | case HttpHdrCcType::CC_S_MAXAGE: |
d74ad83f | 139 | if (!p || !httpHeaderParseInt(p, &s_maxage) || s_maxage < 0) { |
bf8fe701 | 140 | debugs(65, 2, "cc: invalid s-maxage specs near '" << item << "'"); |
4ce6e3b5 | 141 | clearSMaxAge(); |
d74ad83f FC |
142 | } else { |
143 | setMask(type,true); | |
62e76326 | 144 | } |
62e76326 | 145 | break; |
146 | ||
1da82544 | 147 | case HttpHdrCcType::CC_MAX_STALE: |
d74ad83f | 148 | if (!p || !httpHeaderParseInt(p, &max_stale) || max_stale < 0) { |
bf8fe701 | 149 | debugs(65, 2, "cc: max-stale directive is valid without value"); |
d74ad83f FC |
150 | maxStale(MAX_STALE_ANY); |
151 | } else { | |
152 | setMask(type,true); | |
62e76326 | 153 | } |
62e76326 | 154 | break; |
155 | ||
1da82544 | 156 | case HttpHdrCcType::CC_MIN_FRESH: |
d74ad83f | 157 | if (!p || !httpHeaderParseInt(p, &min_fresh) || min_fresh < 0) { |
64f8c2cb | 158 | debugs(65, 2, "cc: invalid min-fresh specs near '" << item << "'"); |
4ce6e3b5 | 159 | clearMinFresh(); |
d74ad83f FC |
160 | } else { |
161 | setMask(type,true); | |
64f8c2cb | 162 | } |
64f8c2cb AR |
163 | break; |
164 | ||
1da82544 | 165 | case HttpHdrCcType::CC_STALE_IF_ERROR: |
d74ad83f | 166 | if (!p || !httpHeaderParseInt(p, &stale_if_error) || stale_if_error < 0) { |
65fd3895 | 167 | debugs(65, 2, "cc: invalid stale-if-error specs near '" << item << "'"); |
4ce6e3b5 | 168 | clearStaleIfError(); |
d74ad83f FC |
169 | } else { |
170 | setMask(type,true); | |
65fd3895 AJ |
171 | } |
172 | break; | |
173 | ||
1da82544 | 174 | case HttpHdrCcType::CC_PRIVATE: { |
1259f9cf AJ |
175 | String temp; |
176 | if (!p) { | |
177 | // Value parameter is optional. | |
178 | private_.clean(); | |
179 | } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) { | |
180 | private_.append(temp); | |
181 | } else { | |
182 | debugs(65, 2, "cc: invalid private= specs near '" << item << "'"); | |
183 | } | |
184 | // to be safe we ignore broken parameters, but always remember the 'private' part. | |
185 | setMask(type,true); | |
186 | } | |
187 | break; | |
188 | ||
1da82544 | 189 | case HttpHdrCcType::CC_NO_CACHE: { |
1259f9cf AJ |
190 | String temp; |
191 | if (!p) { | |
192 | // On Requests, missing value parameter is expected syntax. | |
193 | // On Responses, value parameter is optional. | |
194 | setMask(type,true); | |
195 | no_cache.clean(); | |
196 | } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) { | |
197 | // On Requests, a value parameter is invalid syntax. | |
198 | // XXX: identify when parsing request header and dump err message here. | |
199 | setMask(type,true); | |
200 | no_cache.append(temp); | |
201 | } else { | |
202 | debugs(65, 2, "cc: invalid no-cache= specs near '" << item << "'"); | |
203 | } | |
204 | } | |
205 | break; | |
206 | ||
1da82544 | 207 | case HttpHdrCcType::CC_PUBLIC: |
77da1817 A |
208 | Public(true); |
209 | break; | |
1da82544 | 210 | case HttpHdrCcType::CC_NO_STORE: |
77da1817 A |
211 | noStore(true); |
212 | break; | |
1da82544 | 213 | case HttpHdrCcType::CC_NO_TRANSFORM: |
77da1817 A |
214 | noTransform(true); |
215 | break; | |
1da82544 | 216 | case HttpHdrCcType::CC_MUST_REVALIDATE: |
77da1817 A |
217 | mustRevalidate(true); |
218 | break; | |
1da82544 | 219 | case HttpHdrCcType::CC_PROXY_REVALIDATE: |
77da1817 A |
220 | proxyRevalidate(true); |
221 | break; | |
1da82544 | 222 | case HttpHdrCcType::CC_ONLY_IF_CACHED: |
77da1817 A |
223 | onlyIfCached(true); |
224 | break; | |
caf65351 AJ |
225 | case HttpHdrCcType::CC_IMMUTABLE: |
226 | Immutable(true); | |
227 | break; | |
1b2e9616 | 228 | |
1da82544 | 229 | case HttpHdrCcType::CC_OTHER: |
f9517ad8 FC |
230 | if (other.size()) |
231 | other.append(", "); | |
1b2e9616 | 232 | |
f9517ad8 | 233 | other.append(item, ilen); |
1b2e9616 | 234 | break; |
235 | ||
62e76326 | 236 | default: |
1b2e9616 | 237 | /* note that we ignore most of '=' specs (RFCVIOLATION) */ |
62e76326 | 238 | break; |
239 | } | |
7faf2bdb | 240 | } |
62e76326 | 241 | |
f9517ad8 | 242 | return (mask != 0); |
7faf2bdb | 243 | } |
244 | ||
7faf2bdb | 245 | void |
17802cf1 | 246 | HttpHdrCc::packInto(Packable * p) const |
7faf2bdb | 247 | { |
2c8c98ad FC |
248 | // optimization: if the mask is empty do nothing |
249 | if (mask==0) | |
250 | return; | |
251 | ||
ce9c4e4c | 252 | HttpHdrCcType flag; |
7faf2bdb | 253 | int pcount = 0; |
4ce6e3b5 | 254 | assert(p); |
62e76326 | 255 | |
1da82544 FC |
256 | for (flag = HttpHdrCcType::CC_PUBLIC; flag < HttpHdrCcType::CC_ENUM_END; ++flag) { |
257 | if (isSet(flag) && flag != HttpHdrCcType::CC_OTHER) { | |
62e76326 | 258 | |
c397963f | 259 | /* print option name for all options */ |
9e167fa2 | 260 | p->appendf((pcount ? ", %s": "%s"), CcAttrs[flag].name); |
91274dd0 | 261 | |
c397963f FC |
262 | /* for all options having values, "=value" after the name */ |
263 | switch (flag) { | |
145c72f4 AJ |
264 | case HttpHdrCcType::CC_PUBLIC: |
265 | break; | |
266 | case HttpHdrCcType::CC_PRIVATE: | |
810d879f EB |
267 | if (private_.size()) |
268 | p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(private_)); | |
145c72f4 AJ |
269 | break; |
270 | ||
271 | case HttpHdrCcType::CC_NO_CACHE: | |
810d879f EB |
272 | if (no_cache.size()) |
273 | p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(no_cache)); | |
145c72f4 AJ |
274 | break; |
275 | case HttpHdrCcType::CC_NO_STORE: | |
276 | break; | |
277 | case HttpHdrCcType::CC_NO_TRANSFORM: | |
278 | break; | |
279 | case HttpHdrCcType::CC_MUST_REVALIDATE: | |
280 | break; | |
281 | case HttpHdrCcType::CC_PROXY_REVALIDATE: | |
282 | break; | |
1da82544 | 283 | case HttpHdrCcType::CC_MAX_AGE: |
810d879f | 284 | p->appendf("=%d", max_age); |
c397963f | 285 | break; |
1da82544 | 286 | case HttpHdrCcType::CC_S_MAXAGE: |
810d879f | 287 | p->appendf("=%d", s_maxage); |
c397963f | 288 | break; |
1da82544 | 289 | case HttpHdrCcType::CC_MAX_STALE: |
c397963f FC |
290 | /* max-stale's value is optional. |
291 | If we didn't receive it, don't send it */ | |
810d879f EB |
292 | if (max_stale != MAX_STALE_ANY) |
293 | p->appendf("=%d", max_stale); | |
c397963f | 294 | break; |
1da82544 | 295 | case HttpHdrCcType::CC_MIN_FRESH: |
810d879f | 296 | p->appendf("=%d", min_fresh); |
c397963f | 297 | break; |
145c72f4 AJ |
298 | case HttpHdrCcType::CC_ONLY_IF_CACHED: |
299 | break; | |
300 | case HttpHdrCcType::CC_STALE_IF_ERROR: | |
810d879f | 301 | p->appendf("=%d", stale_if_error); |
145c72f4 | 302 | break; |
caf65351 AJ |
303 | case HttpHdrCcType::CC_IMMUTABLE: |
304 | break; | |
145c72f4 AJ |
305 | case HttpHdrCcType::CC_OTHER: |
306 | case HttpHdrCcType::CC_ENUM_END: | |
307 | // done below after the loop | |
c397963f FC |
308 | break; |
309 | } | |
64f8c2cb | 310 | |
7ebe76de | 311 | ++pcount; |
62e76326 | 312 | } |
7faf2bdb | 313 | } |
1b2e9616 | 314 | |
4ce6e3b5 | 315 | if (other.size() != 0) |
4391cd15 | 316 | p->appendf((pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH), SQUIDSTRINGPRINT(other)); |
7faf2bdb | 317 | } |
318 | ||
7faf2bdb | 319 | void |
320 | httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist) | |
321 | { | |
7faf2bdb | 322 | assert(cc); |
62e76326 | 323 | |
1da82544 | 324 | for (HttpHdrCcType c = HttpHdrCcType::CC_PUBLIC; c < HttpHdrCcType::CC_ENUM_END; ++c) |
f9517ad8 | 325 | if (cc->isSet(c)) |
f30f7998 | 326 | hist->count(c); |
7faf2bdb | 327 | } |
328 | ||
329 | void | |
ced8def3 | 330 | httpHdrCcStatDumper(StoreEntry * sentry, int, double val, double, int count) |
7faf2bdb | 331 | { |
f53969cc | 332 | extern const HttpHeaderStat *dump_stat; /* argh! */ |
383154d7 | 333 | const int id = static_cast<int>(val); |
a45f2153 | 334 | const bool valid_id = id >= 0 && id < static_cast<int>(HttpHdrCcType::CC_ENUM_END); |
ce394734 | 335 | const char *name = valid_id ? CcAttrs[id].name : "INVALID"; |
62e76326 | 336 | |
7faf2bdb | 337 | if (count || valid_id) |
62e76326 | 338 | storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n", |
339 | id, name, count, xdiv(count, dump_stat->ccParsedCount)); | |
7faf2bdb | 340 | } |
1ba0611a | 341 | |
6b1c9146 FC |
342 | std::ostream & |
343 | operator<< (std::ostream &s, HttpHdrCcType c) | |
344 | { | |
345 | const unsigned char ic = static_cast<int>(c); | |
a242e34c | 346 | if (c < HttpHdrCcType::CC_ENUM_END) |
6b1c9146 FC |
347 | s << CcAttrs[ic].name << '[' << ic << ']' ; |
348 | else | |
349 | s << "*invalid hdrcc* [" << ic << ']'; | |
350 | return s; | |
351 | } | |
352 |