]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrSc.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / HttpHdrSc.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 90 HTTP Cache Control Header */
10
11 #include "squid.h"
12 #include "HttpHdrSc.h"
13 #include "HttpHeader.h"
14 #include "HttpHeaderFieldInfo.h"
15 #include "HttpHeaderFieldStat.h"
16 #include "HttpHeaderStat.h"
17 #include "HttpHeaderTools.h"
18 #include "Store.h"
19 #include "StrList.h"
20 #include "util.h"
21
22 #include <map>
23
24 /* a row in the table used for parsing surrogate-control header and statistics */
25 typedef struct {
26 const char *name;
27 http_hdr_sc_type id;
28 HttpHeaderFieldStat stat;
29 } HttpHeaderScFields;
30
31 /* this table is used for parsing surrogate control header */
32 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
33 //todo: implement constraint
34 static const HttpHeaderFieldAttrs ScAttrs[SC_ENUM_END] = {
35 HttpHeaderFieldAttrs("no-store", (http_hdr_type)SC_NO_STORE),
36 HttpHeaderFieldAttrs("no-store-remote", (http_hdr_type)SC_NO_STORE_REMOTE),
37 HttpHeaderFieldAttrs("max-age", (http_hdr_type)SC_MAX_AGE),
38 HttpHeaderFieldAttrs("content", (http_hdr_type)SC_CONTENT),
39 HttpHeaderFieldAttrs("Other,", (http_hdr_type)SC_OTHER) /* ',' will protect from matches */
40 };
41
42 HttpHeaderFieldInfo *ScFieldsInfo = NULL;
43
44 http_hdr_sc_type &operator++ (http_hdr_sc_type &aHeader)
45 {
46 int tmp = (int)aHeader;
47 aHeader = (http_hdr_sc_type)(++tmp);
48 return aHeader;
49 }
50
51 int operator - (http_hdr_sc_type const &anSc, http_hdr_sc_type const &anSc2)
52 {
53 return (int)anSc - (int)anSc2;
54 }
55
56 /* module initialization */
57
58 void
59 httpHdrScInitModule(void)
60 {
61 ScFieldsInfo = httpHeaderBuildFieldsInfo(ScAttrs, SC_ENUM_END);
62 }
63
64 void
65 httpHdrScCleanModule(void)
66 {
67 httpHeaderDestroyFieldsInfo(ScFieldsInfo, SC_ENUM_END);
68 ScFieldsInfo = NULL;
69 }
70
71 /* implementation */
72
73 /* creates an sc object from a 0-terminating string */
74 HttpHdrSc *
75 httpHdrScParseCreate(const String & str)
76 {
77 HttpHdrSc *sc = new HttpHdrSc();
78
79 if (!sc->parse(&str)) {
80 delete sc;
81 sc = NULL;
82 }
83
84 return sc;
85 }
86
87 /* parses a 0-terminating string and inits sc */
88 bool
89 HttpHdrSc::parse(const String * str)
90 {
91 HttpHdrSc * sc=this;
92 const char *item;
93 const char *p; /* '=' parameter */
94 const char *pos = NULL;
95 const char *target = NULL; /* ;foo */
96 const char *temp = NULL; /* temp buffer */
97 int type;
98 int ilen, vlen;
99 int initiallen;
100 HttpHdrScTarget *sct;
101 assert(str);
102
103 /* iterate through comma separated list */
104
105 while (strListGetItem(str, ',', &item, &ilen, &pos)) {
106 initiallen = ilen;
107 vlen = 0;
108 /* decrease ilen to still match the token for '=' statements */
109
110 if ((p = strchr(item, '=')) && (p - item < ilen)) {
111 vlen = ilen - (p + 1 - item);
112 ilen = p - item;
113 ++p;
114 }
115
116 /* decrease ilen to still match the token for ';' qualified non '=' statments */
117 else if ((p = strchr(item, ';')) && (p - item < ilen)) {
118 ilen = p - item;
119 ++p;
120 }
121
122 /* find type */
123 /* TODO: use a type-safe map-based lookup */
124 type = httpHeaderIdByName(item, ilen,
125 ScFieldsInfo, SC_ENUM_END);
126
127 if (type < 0) {
128 debugs(90, 2, "hdr sc: unknown control-directive: near '" << item << "' in '" << str << "'");
129 type = SC_OTHER;
130 }
131
132 /* Is this a targeted directive? */
133 /* TODO: remove the temporary useage and use memrchr and the information we have instead */
134 temp = xstrndup (item, initiallen + 1);
135
136 if (!((target = strrchr (temp, ';')) && !strchr (target, '"') && *(target + 1) != '\0'))
137 target = NULL;
138 else
139 ++target;
140
141 sct = sc->findTarget(target);
142
143 if (!sct) {
144 sct = new HttpHdrScTarget(target);
145 addTarget(sct);
146 }
147
148 safe_free (temp);
149
150 if (sct->isSet(static_cast<http_hdr_sc_type>(type))) {
151 if (type != SC_OTHER)
152 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item << "' in '" << str << "'");
153
154 ++ ScFieldsInfo[type].stat.repCount;
155
156 continue;
157 }
158
159 /* process directives */
160 switch (type) {
161 case SC_NO_STORE:
162 sct->noStore(true);
163 break;
164
165 case SC_NO_STORE_REMOTE:
166 sct->noStoreRemote(true);
167 break;
168
169 case SC_MAX_AGE: {
170 int ma;
171 if (p && httpHeaderParseInt(p, &ma)) {
172 sct->maxAge(ma);
173
174 if ((p = strchr (p, '+'))) {
175 int ms;
176 ++p; //skip the + char
177 if (httpHeaderParseInt(p, &ms)) {
178 sct->maxStale(ms);
179 } else {
180 debugs(90, 2, "sc: invalid max-stale specs near '" << item << "'");
181 sct->clearMaxStale();
182 /* leave the max-age alone */
183 }
184 }
185 } else {
186 debugs(90, 2, "sc: invalid max-age specs near '" << item << "'");
187 sct->clearMaxAge();
188 }
189
190 break;
191 }
192
193 case SC_CONTENT:
194
195 if ( p && httpHeaderParseQuotedString(p, vlen, &sct->content_)) {
196 sct->setMask(SC_CONTENT,true); // ugly but saves a copy
197 } else {
198 debugs(90, 2, "sc: invalid content= quoted string near '" << item << "'");
199 sct->clearContent();
200 }
201 break;
202
203 case SC_OTHER:
204 default:
205 break;
206 }
207 }
208
209 return sc->targets.head != NULL;
210 }
211
212 HttpHdrSc::~HttpHdrSc()
213 {
214 if (targets.head) {
215 dlink_node *sct = targets.head;
216
217 while (sct) {
218 HttpHdrScTarget *t = static_cast<HttpHdrScTarget *>(sct->data);
219 sct = sct->next;
220 dlinkDelete (&t->node, &targets);
221 delete t;
222 }
223 }
224 }
225
226 HttpHdrSc::HttpHdrSc(const HttpHdrSc &sc)
227 {
228 dlink_node *node = sc.targets.head;
229
230 while (node) {
231 HttpHdrScTarget *dupsct = new HttpHdrScTarget(*static_cast<HttpHdrScTarget *>(node->data));
232 addTargetAtTail(dupsct);
233 node = node->next;
234 }
235 }
236
237 void
238 HttpHdrScTarget::packInto(Packer * p) const
239 {
240 http_hdr_sc_type flag;
241 int pcount = 0;
242 assert (p);
243
244 for (flag = SC_NO_STORE; flag < SC_ENUM_END; ++flag) {
245 if (isSet(flag) && flag != SC_OTHER) {
246
247 /* print option name */
248 packerPrintf(p, (pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH),
249 SQUIDSTRINGPRINT(ScFieldsInfo[flag].name));
250
251 /* handle options with values */
252
253 if (flag == SC_MAX_AGE)
254 packerPrintf(p, "=%d", (int) max_age);
255
256 if (flag == SC_CONTENT)
257 packerPrintf(p, "=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(content_));
258
259 ++pcount;
260 }
261 }
262
263 if (hasTarget())
264 packerPrintf (p, ";" SQUIDSTRINGPH, SQUIDSTRINGPRINT(target));
265 }
266
267 void
268 HttpHdrSc::packInto(Packer * p) const
269 {
270 dlink_node *node;
271 assert(p);
272 node = targets.head;
273
274 while (node) {
275 static_cast<HttpHdrScTarget *>(node->data)->packInto(p);
276 node = node->next;
277 }
278 }
279
280 /* negative max_age will clean old max_Age setting */
281 void
282 HttpHdrSc::setMaxAge(char const *target, int max_age)
283 {
284 HttpHdrScTarget *sct = findTarget(target);
285
286 if (!sct) {
287 sct = new HttpHdrScTarget(target);
288 dlinkAddTail (sct, &sct->node, &targets);
289 }
290
291 sct->maxAge(max_age);
292 }
293
294 void
295 HttpHdrSc::updateStats(StatHist * hist) const
296 {
297 dlink_node *sct = targets.head;
298
299 while (sct) {
300 static_cast<HttpHdrScTarget *>(sct->data)->updateStats(hist);
301 sct = sct->next;
302 }
303 }
304
305 void
306 httpHdrScTargetStatDumper(StoreEntry * sentry, int, double val, double, int count)
307 {
308 extern const HttpHeaderStat *dump_stat; /* argh! */
309 const int id = (int) val;
310 const int valid_id = id >= 0 && id < SC_ENUM_END;
311 const char *name = valid_id ? ScFieldsInfo[id].name.termedBuf() : "INVALID";
312
313 if (count || valid_id)
314 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
315 id, name, count, xdiv(count, dump_stat->scParsedCount));
316 }
317
318 void
319 httpHdrScStatDumper(StoreEntry * sentry, int, double val, double, int count)
320 {
321 extern const HttpHeaderStat *dump_stat; /* argh! */
322 const int id = (int) val;
323 const int valid_id = id >= 0 && id < SC_ENUM_END;
324 const char *name = valid_id ? ScFieldsInfo[id].name.termedBuf() : "INVALID";
325
326 if (count || valid_id)
327 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
328 id, name, count, xdiv(count, dump_stat->scParsedCount));
329 }
330
331 HttpHdrScTarget *
332 HttpHdrSc::findTarget(const char *target)
333 {
334 dlink_node *node;
335 node = targets.head;
336
337 while (node) {
338 HttpHdrScTarget *sct = (HttpHdrScTarget *)node->data;
339
340 if (target && sct->target.size() > 0 && !strcmp(target, sct->target.termedBuf()))
341 return sct;
342 else if (!target && sct->target.size() == 0)
343 return sct;
344
345 node = node->next;
346 }
347
348 return NULL;
349 }
350
351 HttpHdrScTarget *
352 HttpHdrSc::getMergedTarget(const char *ourtarget)
353 {
354 HttpHdrScTarget *sctus = findTarget(ourtarget);
355 HttpHdrScTarget *sctgeneric = findTarget(NULL);
356
357 if (sctgeneric || sctus) {
358 HttpHdrScTarget *sctusable = new HttpHdrScTarget(NULL);
359
360 if (sctgeneric)
361 sctusable->mergeWith(sctgeneric);
362
363 if (sctus)
364 sctusable->mergeWith(sctus);
365
366 return sctusable;
367 }
368
369 return NULL;
370 }
371