]> git.ipfire.org Git - thirdparty/squid.git/blame - src/HttpHdrCc.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpHdrCc.cc
CommitLineData
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
29static 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
47LookupTable<HttpHdrCcType> ccLookupTable(HttpHdrCcType::CC_OTHER,CcAttrs);
48std::vector<HttpHeaderFieldStat> ccHeaderStats(HttpHdrCcType::CC_ENUM_END);
7faf2bdb 49
d74ad83f 50/// used to walk a table of http_header_cc_type structs
1da82544 51HttpHdrCcType &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 59void
9bc73deb 60httpHdrCcInitModule(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
68void
69HttpHdrCc::clear()
7faf2bdb 70{
7ebe76de 71 *this=HttpHdrCc();
7faf2bdb 72}
73
ce394734 74bool
7ebe76de 75HttpHdrCc::parse(const String & str)
7faf2bdb 76{
77 const char *item;
f53969cc 78 const char *p; /* '=' parameter */
7faf2bdb 79 const char *pos = NULL;
7faf2bdb 80 int ilen;
1b2e9616 81 int nlen;
7faf2bdb 82
7faf2bdb 83 /* iterate through comma separated list */
62e76326 84
ce394734 85 while (strListGetItem(&str, ',', &item, &ilen, &pos)) {
1b2e9616 86 /* isolate directive name */
62e76326 87
a38ec4b1
FC
88 if ((p = (const char *)memchr(item, '=', ilen)) && (p - item < ilen)) {
89 nlen = p - item;
90 ++p;
91 } else {
1b2e9616 92 nlen = ilen;
a38ec4b1 93 }
62e76326 94
95 /* find type */
1da82544 96 const HttpHdrCcType type = ccLookupTable.lookup(SBuf(item,nlen));
62e76326 97
aa62670a 98 // ignore known duplicate directives
f9517ad8 99 if (isSet(type)) {
1da82544 100 if (type != HttpHdrCcType::CC_OTHER) {
e8466ea9 101 debugs(65, 2, "hdr cc: ignoring duplicate cache-directive: near '" << item << "' in '" << str << "'");
ea295fe4 102 ++ ccHeaderStats[type].repCount;
aa62670a
AR
103 continue;
104 }
62e76326 105 }
106
4ce6e3b5 107 /* special-case-parsing and attribute-setting */
62e76326 108 switch (type) {
109
1da82544 110 case HttpHdrCcType::CC_MAX_AGE:
d74ad83f 111 if (!p || !httpHeaderParseInt(p, &max_age) || max_age < 0) {
bf8fe701 112 debugs(65, 2, "cc: invalid max-age specs near '" << item << "'");
4ce6e3b5 113 clearMaxAge();
1ba0611a 114 } else {
d74ad83f 115 setMask(type,true);
62e76326 116 }
62e76326 117 break;
118
1da82544 119 case HttpHdrCcType::CC_S_MAXAGE:
d74ad83f 120 if (!p || !httpHeaderParseInt(p, &s_maxage) || s_maxage < 0) {
bf8fe701 121 debugs(65, 2, "cc: invalid s-maxage specs near '" << item << "'");
4ce6e3b5 122 clearSMaxAge();
d74ad83f
FC
123 } else {
124 setMask(type,true);
62e76326 125 }
62e76326 126 break;
127
1da82544 128 case HttpHdrCcType::CC_MAX_STALE:
d74ad83f 129 if (!p || !httpHeaderParseInt(p, &max_stale) || max_stale < 0) {
bf8fe701 130 debugs(65, 2, "cc: max-stale directive is valid without value");
d74ad83f
FC
131 maxStale(MAX_STALE_ANY);
132 } else {
133 setMask(type,true);
62e76326 134 }
62e76326 135 break;
136
1da82544 137 case HttpHdrCcType::CC_MIN_FRESH:
d74ad83f 138 if (!p || !httpHeaderParseInt(p, &min_fresh) || min_fresh < 0) {
64f8c2cb 139 debugs(65, 2, "cc: invalid min-fresh specs near '" << item << "'");
4ce6e3b5 140 clearMinFresh();
d74ad83f
FC
141 } else {
142 setMask(type,true);
64f8c2cb 143 }
64f8c2cb
AR
144 break;
145
1da82544 146 case HttpHdrCcType::CC_STALE_IF_ERROR:
d74ad83f 147 if (!p || !httpHeaderParseInt(p, &stale_if_error) || stale_if_error < 0) {
65fd3895 148 debugs(65, 2, "cc: invalid stale-if-error specs near '" << item << "'");
4ce6e3b5 149 clearStaleIfError();
d74ad83f
FC
150 } else {
151 setMask(type,true);
65fd3895
AJ
152 }
153 break;
154
1da82544 155 case HttpHdrCcType::CC_PRIVATE: {
1259f9cf
AJ
156 String temp;
157 if (!p) {
158 // Value parameter is optional.
159 private_.clean();
160 } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) {
161 private_.append(temp);
162 } else {
163 debugs(65, 2, "cc: invalid private= specs near '" << item << "'");
164 }
165 // to be safe we ignore broken parameters, but always remember the 'private' part.
166 setMask(type,true);
167 }
168 break;
169
1da82544 170 case HttpHdrCcType::CC_NO_CACHE: {
1259f9cf
AJ
171 String temp;
172 if (!p) {
173 // On Requests, missing value parameter is expected syntax.
174 // On Responses, value parameter is optional.
175 setMask(type,true);
176 no_cache.clean();
177 } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) {
178 // On Requests, a value parameter is invalid syntax.
179 // XXX: identify when parsing request header and dump err message here.
180 setMask(type,true);
181 no_cache.append(temp);
182 } else {
183 debugs(65, 2, "cc: invalid no-cache= specs near '" << item << "'");
184 }
185 }
186 break;
187
1da82544 188 case HttpHdrCcType::CC_PUBLIC:
77da1817
A
189 Public(true);
190 break;
1da82544 191 case HttpHdrCcType::CC_NO_STORE:
77da1817
A
192 noStore(true);
193 break;
1da82544 194 case HttpHdrCcType::CC_NO_TRANSFORM:
77da1817
A
195 noTransform(true);
196 break;
1da82544 197 case HttpHdrCcType::CC_MUST_REVALIDATE:
77da1817
A
198 mustRevalidate(true);
199 break;
1da82544 200 case HttpHdrCcType::CC_PROXY_REVALIDATE:
77da1817
A
201 proxyRevalidate(true);
202 break;
1da82544 203 case HttpHdrCcType::CC_ONLY_IF_CACHED:
77da1817
A
204 onlyIfCached(true);
205 break;
caf65351
AJ
206 case HttpHdrCcType::CC_IMMUTABLE:
207 Immutable(true);
208 break;
1b2e9616 209
1da82544 210 case HttpHdrCcType::CC_OTHER:
f9517ad8
FC
211 if (other.size())
212 other.append(", ");
1b2e9616 213
f9517ad8 214 other.append(item, ilen);
1b2e9616 215 break;
216
62e76326 217 default:
1b2e9616 218 /* note that we ignore most of '=' specs (RFCVIOLATION) */
62e76326 219 break;
220 }
7faf2bdb 221 }
62e76326 222
f9517ad8 223 return (mask != 0);
7faf2bdb 224}
225
7faf2bdb 226void
17802cf1 227HttpHdrCc::packInto(Packable * p) const
7faf2bdb 228{
2c8c98ad
FC
229 // optimization: if the mask is empty do nothing
230 if (mask==0)
231 return;
232
ce9c4e4c 233 HttpHdrCcType flag;
7faf2bdb 234 int pcount = 0;
4ce6e3b5 235 assert(p);
62e76326 236
1da82544
FC
237 for (flag = HttpHdrCcType::CC_PUBLIC; flag < HttpHdrCcType::CC_ENUM_END; ++flag) {
238 if (isSet(flag) && flag != HttpHdrCcType::CC_OTHER) {
62e76326 239
c397963f 240 /* print option name for all options */
4391cd15 241 p->appendf((pcount ? ", %s": "%s") , CcAttrs[flag].name);
91274dd0 242
c397963f
FC
243 /* for all options having values, "=value" after the name */
244 switch (flag) {
145c72f4
AJ
245 case HttpHdrCcType::CC_PUBLIC:
246 break;
247 case HttpHdrCcType::CC_PRIVATE:
248 if (Private().size())
249 p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(Private()));
250 break;
251
252 case HttpHdrCcType::CC_NO_CACHE:
253 if (noCache().size())
254 p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(noCache()));
255 break;
256 case HttpHdrCcType::CC_NO_STORE:
257 break;
258 case HttpHdrCcType::CC_NO_TRANSFORM:
259 break;
260 case HttpHdrCcType::CC_MUST_REVALIDATE:
261 break;
262 case HttpHdrCcType::CC_PROXY_REVALIDATE:
263 break;
1da82544 264 case HttpHdrCcType::CC_MAX_AGE:
4391cd15 265 p->appendf("=%d", maxAge());
c397963f 266 break;
1da82544 267 case HttpHdrCcType::CC_S_MAXAGE:
4391cd15 268 p->appendf("=%d", sMaxAge());
c397963f 269 break;
1da82544 270 case HttpHdrCcType::CC_MAX_STALE:
c397963f
FC
271 /* max-stale's value is optional.
272 If we didn't receive it, don't send it */
273 if (maxStale()!=MAX_STALE_ANY)
4391cd15 274 p->appendf("=%d", maxStale());
c397963f 275 break;
1da82544 276 case HttpHdrCcType::CC_MIN_FRESH:
4391cd15 277 p->appendf("=%d", minFresh());
c397963f 278 break;
145c72f4
AJ
279 case HttpHdrCcType::CC_ONLY_IF_CACHED:
280 break;
281 case HttpHdrCcType::CC_STALE_IF_ERROR:
282 p->appendf("=%d", staleIfError());
283 break;
caf65351
AJ
284 case HttpHdrCcType::CC_IMMUTABLE:
285 break;
145c72f4
AJ
286 case HttpHdrCcType::CC_OTHER:
287 case HttpHdrCcType::CC_ENUM_END:
288 // done below after the loop
c397963f
FC
289 break;
290 }
64f8c2cb 291
7ebe76de 292 ++pcount;
62e76326 293 }
7faf2bdb 294 }
1b2e9616 295
4ce6e3b5 296 if (other.size() != 0)
4391cd15 297 p->appendf((pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH), SQUIDSTRINGPRINT(other));
7faf2bdb 298}
299
7faf2bdb 300void
301httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist)
302{
7faf2bdb 303 assert(cc);
62e76326 304
1da82544 305 for (HttpHdrCcType c = HttpHdrCcType::CC_PUBLIC; c < HttpHdrCcType::CC_ENUM_END; ++c)
f9517ad8 306 if (cc->isSet(c))
f30f7998 307 hist->count(c);
7faf2bdb 308}
309
310void
ced8def3 311httpHdrCcStatDumper(StoreEntry * sentry, int, double val, double, int count)
7faf2bdb 312{
f53969cc 313 extern const HttpHeaderStat *dump_stat; /* argh! */
383154d7 314 const int id = static_cast<int>(val);
a45f2153 315 const bool valid_id = id >= 0 && id < static_cast<int>(HttpHdrCcType::CC_ENUM_END);
ce394734 316 const char *name = valid_id ? CcAttrs[id].name : "INVALID";
62e76326 317
7faf2bdb 318 if (count || valid_id)
62e76326 319 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
320 id, name, count, xdiv(count, dump_stat->ccParsedCount));
7faf2bdb 321}
1ba0611a 322
6b1c9146
FC
323std::ostream &
324operator<< (std::ostream &s, HttpHdrCcType c)
325{
326 const unsigned char ic = static_cast<int>(c);
a242e34c 327 if (c < HttpHdrCcType::CC_ENUM_END)
6b1c9146
FC
328 s << CcAttrs[ic].name << '[' << ic << ']' ;
329 else
330 s << "*invalid hdrcc* [" << ic << ']';
331 return s;
332}
333
9774b48e
FC
334#if !_USE_INLINE_
335#include "HttpHdrCc.cci"
336#endif
f53969cc 337