]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.cc
Merge from trunk rev.14061
[thirdparty/squid.git] / src / HttpHdrCc.cc
1 /*
2 * Copyright (C) 1996-2015 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 /* DEBUG: section 65 HTTP Cache Control Header */
10
11 #include "squid.h"
12 #include "HttpHdrCc.h"
13 #include "HttpHeader.h"
14 #include "HttpHeaderFieldStat.h"
15 #include "HttpHeaderStat.h"
16 #include "HttpHeaderTools.h"
17 #include "SBuf.h"
18 #include "StatHist.h"
19 #include "Store.h"
20 #include "StrList.h"
21 #include "util.h"
22
23 #include <map>
24
25 /* a row in the table used for parsing cache control header and statistics */
26 class HttpHeaderCcFields
27 {
28 public:
29 HttpHeaderCcFields() : name(NULL), id(CC_BADHDR), stat() {}
30 HttpHeaderCcFields(const char *aName, http_hdr_cc_type aTypeId) : name(aName), id(aTypeId) {}
31 HttpHeaderCcFields(const HttpHeaderCcFields &f) : name(f.name), id(f.id) {}
32 // nothing to do as name is a pointer to global static string
33 ~HttpHeaderCcFields() {}
34
35 const char *name;
36 http_hdr_cc_type id;
37 HttpHeaderFieldStat stat;
38
39 private:
40 HttpHeaderCcFields &operator =(const HttpHeaderCcFields &); // not implemented
41 };
42
43 /* order must match that of enum http_hdr_cc_type. The constraint is verified at initialization time */
44 static HttpHeaderCcFields CcAttrs[CC_ENUM_END] = {
45 HttpHeaderCcFields("public", CC_PUBLIC),
46 HttpHeaderCcFields("private", CC_PRIVATE),
47 HttpHeaderCcFields("no-cache", CC_NO_CACHE),
48 HttpHeaderCcFields("no-store", CC_NO_STORE),
49 HttpHeaderCcFields("no-transform", CC_NO_TRANSFORM),
50 HttpHeaderCcFields("must-revalidate", CC_MUST_REVALIDATE),
51 HttpHeaderCcFields("proxy-revalidate", CC_PROXY_REVALIDATE),
52 HttpHeaderCcFields("max-age", CC_MAX_AGE),
53 HttpHeaderCcFields("s-maxage", CC_S_MAXAGE),
54 HttpHeaderCcFields("max-stale", CC_MAX_STALE),
55 HttpHeaderCcFields("min-fresh", CC_MIN_FRESH),
56 HttpHeaderCcFields("only-if-cached", CC_ONLY_IF_CACHED),
57 HttpHeaderCcFields("stale-if-error", CC_STALE_IF_ERROR),
58 HttpHeaderCcFields("Other,", CC_OTHER) /* ',' will protect from matches */
59 };
60
61 /// Map an header name to its type, to expedite parsing
62 typedef std::map<const SBuf,http_hdr_cc_type> CcNameToIdMap_t;
63 static CcNameToIdMap_t CcNameToIdMap;
64
65 /// used to walk a table of http_header_cc_type structs
66 http_hdr_cc_type &operator++ (http_hdr_cc_type &aHeader)
67 {
68 int tmp = (int)aHeader;
69 aHeader = (http_hdr_cc_type)(++tmp);
70 return aHeader;
71 }
72
73 /// Module initialization hook
74 void
75 httpHdrCcInitModule(void)
76 {
77 /* build lookup and accounting structures */
78 for (int32_t i = 0; i < CC_ENUM_END; ++i) {
79 const HttpHeaderCcFields &f=CcAttrs[i];
80 assert(i == f.id); /* verify assumption: the id is the key into the array */
81 const SBuf k(f.name);
82 CcNameToIdMap[k]=f.id;
83 }
84 }
85
86 /// Module cleanup hook.
87 void
88 httpHdrCcCleanModule(void)
89 {
90 // HdrCcNameToIdMap is self-cleaning
91 }
92
93 void
94 HttpHdrCc::clear()
95 {
96 *this=HttpHdrCc();
97 }
98
99 bool
100 HttpHdrCc::parse(const String & str)
101 {
102 const char *item;
103 const char *p; /* '=' parameter */
104 const char *pos = NULL;
105 http_hdr_cc_type type;
106 int ilen;
107 int nlen;
108
109 /* iterate through comma separated list */
110
111 while (strListGetItem(&str, ',', &item, &ilen, &pos)) {
112 /* isolate directive name */
113
114 if ((p = (const char *)memchr(item, '=', ilen)) && (p - item < ilen)) {
115 nlen = p - item;
116 ++p;
117 } else {
118 nlen = ilen;
119 }
120
121 /* find type */
122 const CcNameToIdMap_t::const_iterator i=CcNameToIdMap.find(SBuf(item,nlen));
123 if (i==CcNameToIdMap.end())
124 type=CC_OTHER;
125 else
126 type=i->second;
127
128 // ignore known duplicate directives
129 if (isSet(type)) {
130 if (type != CC_OTHER) {
131 debugs(65, 2, "hdr cc: ignoring duplicate cache-directive: near '" << item << "' in '" << str << "'");
132 ++CcAttrs[type].stat.repCount;
133 continue;
134 }
135 }
136
137 /* special-case-parsing and attribute-setting */
138 switch (type) {
139
140 case CC_MAX_AGE:
141 if (!p || !httpHeaderParseInt(p, &max_age) || max_age < 0) {
142 debugs(65, 2, "cc: invalid max-age specs near '" << item << "'");
143 clearMaxAge();
144 } else {
145 setMask(type,true);
146 }
147 break;
148
149 case CC_S_MAXAGE:
150 if (!p || !httpHeaderParseInt(p, &s_maxage) || s_maxage < 0) {
151 debugs(65, 2, "cc: invalid s-maxage specs near '" << item << "'");
152 clearSMaxAge();
153 } else {
154 setMask(type,true);
155 }
156 break;
157
158 case CC_MAX_STALE:
159 if (!p || !httpHeaderParseInt(p, &max_stale) || max_stale < 0) {
160 debugs(65, 2, "cc: max-stale directive is valid without value");
161 maxStale(MAX_STALE_ANY);
162 } else {
163 setMask(type,true);
164 }
165 break;
166
167 case CC_MIN_FRESH:
168 if (!p || !httpHeaderParseInt(p, &min_fresh) || min_fresh < 0) {
169 debugs(65, 2, "cc: invalid min-fresh specs near '" << item << "'");
170 clearMinFresh();
171 } else {
172 setMask(type,true);
173 }
174 break;
175
176 case CC_STALE_IF_ERROR:
177 if (!p || !httpHeaderParseInt(p, &stale_if_error) || stale_if_error < 0) {
178 debugs(65, 2, "cc: invalid stale-if-error specs near '" << item << "'");
179 clearStaleIfError();
180 } else {
181 setMask(type,true);
182 }
183 break;
184
185 case CC_PRIVATE: {
186 String temp;
187 if (!p) {
188 // Value parameter is optional.
189 private_.clean();
190 } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) {
191 private_.append(temp);
192 } else {
193 debugs(65, 2, "cc: invalid private= specs near '" << item << "'");
194 }
195 // to be safe we ignore broken parameters, but always remember the 'private' part.
196 setMask(type,true);
197 }
198 break;
199
200 case CC_NO_CACHE: {
201 String temp;
202 if (!p) {
203 // On Requests, missing value parameter is expected syntax.
204 // On Responses, value parameter is optional.
205 setMask(type,true);
206 no_cache.clean();
207 } else if (/* p &&*/ httpHeaderParseQuotedString(p, (ilen-nlen-1), &temp)) {
208 // On Requests, a value parameter is invalid syntax.
209 // XXX: identify when parsing request header and dump err message here.
210 setMask(type,true);
211 no_cache.append(temp);
212 } else {
213 debugs(65, 2, "cc: invalid no-cache= specs near '" << item << "'");
214 }
215 }
216 break;
217
218 case CC_PUBLIC:
219 Public(true);
220 break;
221 case CC_NO_STORE:
222 noStore(true);
223 break;
224 case CC_NO_TRANSFORM:
225 noTransform(true);
226 break;
227 case CC_MUST_REVALIDATE:
228 mustRevalidate(true);
229 break;
230 case CC_PROXY_REVALIDATE:
231 proxyRevalidate(true);
232 break;
233 case CC_ONLY_IF_CACHED:
234 onlyIfCached(true);
235 break;
236
237 case CC_OTHER:
238 if (other.size())
239 other.append(", ");
240
241 other.append(item, ilen);
242 break;
243
244 default:
245 /* note that we ignore most of '=' specs (RFCVIOLATION) */
246 break;
247 }
248 }
249
250 return (mask != 0);
251 }
252
253 void
254 HttpHdrCc::packInto(Packable * p) const
255 {
256 // optimization: if the mask is empty do nothing
257 if (mask==0)
258 return;
259
260 http_hdr_cc_type flag;
261 int pcount = 0;
262 assert(p);
263
264 for (flag = CC_PUBLIC; flag < CC_ENUM_END; ++flag) {
265 if (isSet(flag) && flag != CC_OTHER) {
266
267 /* print option name for all options */
268 p->appendf((pcount ? ", %s": "%s") , CcAttrs[flag].name);
269
270 /* for all options having values, "=value" after the name */
271 switch (flag) {
272 case CC_MAX_AGE:
273 p->appendf("=%d", maxAge());
274 break;
275 case CC_S_MAXAGE:
276 p->appendf("=%d", sMaxAge());
277 break;
278 case CC_MAX_STALE:
279 /* max-stale's value is optional.
280 If we didn't receive it, don't send it */
281 if (maxStale()!=MAX_STALE_ANY)
282 p->appendf("=%d", maxStale());
283 break;
284 case CC_MIN_FRESH:
285 p->appendf("=%d", minFresh());
286 break;
287 default:
288 /* do nothing, directive was already printed */
289 break;
290 }
291
292 ++pcount;
293 }
294 }
295
296 if (other.size() != 0)
297 p->appendf((pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH), SQUIDSTRINGPRINT(other));
298 }
299
300 void
301 httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist)
302 {
303 http_hdr_cc_type c;
304 assert(cc);
305
306 for (c = CC_PUBLIC; c < CC_ENUM_END; ++c)
307 if (cc->isSet(c))
308 hist->count(c);
309 }
310
311 void
312 httpHdrCcStatDumper(StoreEntry * sentry, int, double val, double, int count)
313 {
314 extern const HttpHeaderStat *dump_stat; /* argh! */
315 const int id = (int) val;
316 const int valid_id = id >= 0 && id < CC_ENUM_END;
317 const char *name = valid_id ? CcAttrs[id].name : "INVALID";
318
319 if (count || valid_id)
320 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
321 id, name, count, xdiv(count, dump_stat->ccParsedCount));
322 }
323
324 #if !_USE_INLINE_
325 #include "HttpHdrCc.cci"
326 #endif
327