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