]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrSc.cc
Reimplemented Surrogate-Control lookup as LookupTable, removed httpHeaderNameById
[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 "base/LookupTable.h"
13 #include "HttpHdrSc.h"
14 #include "HttpHeader.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 #include <vector>
24
25 /* a row in the table used for parsing surrogate-control header and statistics */
26 typedef struct {
27 const char *name;
28 http_hdr_sc_type id;
29 HttpHeaderFieldStat stat;
30 } HttpHeaderScFields;
31
32 /* this table is used for parsing surrogate control header */
33 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
34 //todo: implement constraint
35 static const LookupTable<http_hdr_sc_type>::Record ScAttrs[] {
36 {"no-store", SC_NO_STORE},
37 {"no-store-remote", SC_NO_STORE_REMOTE},
38 {"max-age", SC_MAX_AGE},
39 {"content", SC_CONTENT},
40 {"Other,", SC_OTHER}, /* ',' will protect from matches */
41 {nullptr, SC_ENUM_END} /* SC_ENUM_END taken as invalid value */
42 };
43 LookupTable<http_hdr_sc_type> scLookupTable(SC_ENUM_END, ScAttrs);
44 std::vector<HttpHeaderFieldStat> scHeaderStats(SC_ENUM_END);
45
46 http_hdr_sc_type &operator++ (http_hdr_sc_type &aHeader)
47 {
48 int tmp = (int)aHeader;
49 aHeader = (http_hdr_sc_type)(++tmp);
50 return aHeader;
51 }
52
53 int operator - (http_hdr_sc_type const &anSc, http_hdr_sc_type const &anSc2)
54 {
55 return (int)anSc - (int)anSc2;
56 }
57
58 /* module initialization */
59
60 void
61 httpHdrScInitModule(void)
62 {
63 // check invariant on ScAttrs
64 for (int i = 0; ScAttrs[i].name != nullptr; ++i)
65 assert(i == ScAttrs[i].id);
66 }
67
68 void
69 httpHdrScCleanModule(void)
70 {
71 }
72
73 /* implementation */
74
75 /* creates an sc object from a 0-terminating string */
76 HttpHdrSc *
77 httpHdrScParseCreate(const String & str)
78 {
79 HttpHdrSc *sc = new HttpHdrSc();
80
81 if (!sc->parse(&str)) {
82 delete sc;
83 sc = NULL;
84 }
85
86 return sc;
87 }
88
89 /* parses a 0-terminating string and inits sc */
90 bool
91 HttpHdrSc::parse(const String * str)
92 {
93 HttpHdrSc * sc=this;
94 const char *item;
95 const char *p; /* '=' parameter */
96 const char *pos = NULL;
97 const char *target = NULL; /* ;foo */
98 const char *temp = NULL; /* temp buffer */
99 http_hdr_sc_type type;
100 int ilen, vlen;
101 int initiallen;
102 HttpHdrScTarget *sct;
103 assert(str);
104
105 /* iterate through comma separated list */
106
107 while (strListGetItem(str, ',', &item, &ilen, &pos)) {
108 initiallen = ilen;
109 vlen = 0;
110 /* decrease ilen to still match the token for '=' statements */
111
112 if ((p = strchr(item, '=')) && (p - item < ilen)) {
113 vlen = ilen - (p + 1 - item);
114 ilen = p - item;
115 ++p;
116 }
117
118 /* decrease ilen to still match the token for ';' qualified non '=' statments */
119 else if ((p = strchr(item, ';')) && (p - item < ilen)) {
120 ilen = p - item;
121 ++p;
122 }
123
124 /* find type */
125 type = scLookupTable.lookup(SBuf(item,ilen));
126
127 if (type == SC_ENUM_END) {
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(type)) {
151 if (type != SC_OTHER)
152 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item << "' in '" << str << "'");
153
154 ++ scHeaderStats[type].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(Packable * 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 p->appendf((pcount ? ", %s" : "%s"), ScAttrs[flag].name);
249
250 /* handle options with values */
251
252 if (flag == SC_MAX_AGE)
253 p->appendf("=%d", (int) max_age);
254
255 if (flag == SC_CONTENT)
256 p->appendf("=\"" SQUIDSTRINGPH "\"", SQUIDSTRINGPRINT(content_));
257
258 ++pcount;
259 }
260 }
261
262 if (hasTarget())
263 p->appendf(";" SQUIDSTRINGPH, SQUIDSTRINGPRINT(target));
264 }
265
266 void
267 HttpHdrSc::packInto(Packable * p) const
268 {
269 dlink_node *node;
270 assert(p);
271 node = targets.head;
272
273 while (node) {
274 static_cast<HttpHdrScTarget *>(node->data)->packInto(p);
275 node = node->next;
276 }
277 }
278
279 /* negative max_age will clean old max_Age setting */
280 void
281 HttpHdrSc::setMaxAge(char const *target, int max_age)
282 {
283 HttpHdrScTarget *sct = findTarget(target);
284
285 if (!sct) {
286 sct = new HttpHdrScTarget(target);
287 dlinkAddTail (sct, &sct->node, &targets);
288 }
289
290 sct->maxAge(max_age);
291 }
292
293 void
294 HttpHdrSc::updateStats(StatHist * hist) const
295 {
296 dlink_node *sct = targets.head;
297
298 while (sct) {
299 static_cast<HttpHdrScTarget *>(sct->data)->updateStats(hist);
300 sct = sct->next;
301 }
302 }
303
304 void
305 httpHdrScTargetStatDumper(StoreEntry * sentry, int, double val, double, int count)
306 {
307 extern const HttpHeaderStat *dump_stat; /* argh! */
308 const int id = (int) val;
309 const bool valid_id = id >= 0 && id < SC_ENUM_END;
310 const char *name = valid_id ? ScAttrs[id].name : "INVALID";
311
312 if (count || valid_id)
313 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
314 id, name, count, xdiv(count, dump_stat->scParsedCount));
315 }
316
317 void
318 httpHdrScStatDumper(StoreEntry * sentry, int, double val, double, int count)
319 {
320 extern const HttpHeaderStat *dump_stat; /* argh! */
321 const int id = (int) val;
322 const bool valid_id = id >= 0 && id < SC_ENUM_END;
323 const char *name = valid_id ? ScAttrs[id].name : "INVALID";
324
325 if (count || valid_id)
326 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
327 id, name, count, xdiv(count, dump_stat->scParsedCount));
328 }
329
330 HttpHdrScTarget *
331 HttpHdrSc::findTarget(const char *target)
332 {
333 dlink_node *node;
334 node = targets.head;
335
336 while (node) {
337 HttpHdrScTarget *sct = (HttpHdrScTarget *)node->data;
338
339 if (target && sct->target.size() > 0 && !strcmp(target, sct->target.termedBuf()))
340 return sct;
341 else if (!target && sct->target.size() == 0)
342 return sct;
343
344 node = node->next;
345 }
346
347 return NULL;
348 }
349
350 HttpHdrScTarget *
351 HttpHdrSc::getMergedTarget(const char *ourtarget)
352 {
353 HttpHdrScTarget *sctus = findTarget(ourtarget);
354 HttpHdrScTarget *sctgeneric = findTarget(NULL);
355
356 if (sctgeneric || sctus) {
357 HttpHdrScTarget *sctusable = new HttpHdrScTarget(NULL);
358
359 if (sctgeneric)
360 sctusable->mergeWith(sctgeneric);
361
362 if (sctus)
363 sctusable->mergeWith(sctus);
364
365 return sctusable;
366 }
367
368 return NULL;
369 }
370