]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / HttpHdrCc.cc
1 /*
2 *
3 * DEBUG: section 65 HTTP Cache Control Header
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid-old.h"
34 #include "base/StringArea.h"
35 #include "HttpHeader.h"
36 #include "HttpHeaderStat.h"
37 #include "HttpHdrCc.h"
38 #include "StatHist.h"
39 #include "Store.h"
40
41 #if HAVE_MAP
42 #include <map>
43 #endif
44
45 /* a row in the table used for parsing cache control header and statistics */
46 typedef struct {
47 const char *name;
48 http_hdr_cc_type id;
49 HttpHeaderFieldStat stat;
50 } HttpHeaderCcFields;
51
52 /* order must match that of enum http_hdr_cc_type. The constraint is verified at initialization time */
53 static HttpHeaderCcFields CcAttrs[CC_ENUM_END] = {
54 {"public", CC_PUBLIC},
55 {"private", CC_PRIVATE},
56 {"no-cache", CC_NO_CACHE},
57 {"no-store", CC_NO_STORE},
58 {"no-transform", CC_NO_TRANSFORM},
59 {"must-revalidate", CC_MUST_REVALIDATE},
60 {"proxy-revalidate", CC_PROXY_REVALIDATE},
61 {"max-age", CC_MAX_AGE},
62 {"s-maxage", CC_S_MAXAGE},
63 {"max-stale", CC_MAX_STALE},
64 {"min-fresh", CC_MIN_FRESH},
65 {"only-if-cached", CC_ONLY_IF_CACHED},
66 {"stale-if-error", CC_STALE_IF_ERROR},
67 {"Other,", CC_OTHER} /* ',' will protect from matches */
68 };
69
70 /// Map an header name to its type, to expedite parsing
71 typedef std::map<const StringArea,http_hdr_cc_type> CcNameToIdMap_t;
72 static CcNameToIdMap_t CcNameToIdMap;
73
74 /// used to walk a table of http_header_cc_type structs
75 http_hdr_cc_type &operator++ (http_hdr_cc_type &aHeader)
76 {
77 int tmp = (int)aHeader;
78 aHeader = (http_hdr_cc_type)(++tmp);
79 return aHeader;
80 }
81
82
83 /// Module initialization hook
84 void
85 httpHdrCcInitModule(void)
86 {
87 /* build lookup and accounting structures */
88 for (int32_t i = 0; i < CC_ENUM_END; ++i) {
89 const HttpHeaderCcFields &f=CcAttrs[i];
90 assert(i == f.id); /* verify assumption: the id is the key into the array */
91 const StringArea k(f.name,strlen(f.name));
92 CcNameToIdMap[k]=f.id;
93 }
94 }
95
96 /// Module cleanup hook.
97 void
98 httpHdrCcCleanModule(void)
99 {
100 // HdrCcNameToIdMap is self-cleaning
101 }
102
103 void
104 HttpHdrCc::clear()
105 {
106 *this=HttpHdrCc();
107 }
108
109 bool
110 HttpHdrCc::parse(const String & str)
111 {
112 const char *item;
113 const char *p; /* '=' parameter */
114 const char *pos = NULL;
115 http_hdr_cc_type type;
116 int ilen;
117 int nlen;
118
119 /* iterate through comma separated list */
120
121 while (strListGetItem(&str, ',', &item, &ilen, &pos)) {
122 /* isolate directive name */
123
124 if ((p = (const char *)memchr(item, '=', ilen)) && (p - item < ilen))
125 nlen = p++ - item;
126 else
127 nlen = ilen;
128
129 /* find type */
130 const CcNameToIdMap_t::const_iterator i=CcNameToIdMap.find(StringArea(item,nlen));
131 if (i==CcNameToIdMap.end())
132 type=CC_OTHER;
133 else
134 type=i->second;
135
136 // ignore known duplicate directives
137 if (isSet(type)) {
138 if (type != CC_OTHER) {
139 debugs(65, 2, "hdr cc: ignoring duplicate cache-directive: near '" << item << "' in '" << str << "'");
140 ++CcAttrs[type].stat.repCount;
141 continue;
142 }
143 }
144
145 /* special-case-parsing and attribute-setting */
146 switch (type) {
147
148 case CC_MAX_AGE:
149 if (!p || !httpHeaderParseInt(p, &max_age) || max_age < 0) {
150 debugs(65, 2, "cc: invalid max-age specs near '" << item << "'");
151 clearMaxAge();
152 } else {
153 setMask(type,true);
154 }
155 break;
156
157 case CC_S_MAXAGE:
158 if (!p || !httpHeaderParseInt(p, &s_maxage) || s_maxage < 0) {
159 debugs(65, 2, "cc: invalid s-maxage specs near '" << item << "'");
160 clearSMaxAge();
161 } else {
162 setMask(type,true);
163 }
164 break;
165
166 case CC_MAX_STALE:
167 if (!p || !httpHeaderParseInt(p, &max_stale) || max_stale < 0) {
168 debugs(65, 2, "cc: max-stale directive is valid without value");
169 maxStale(MAX_STALE_ANY);
170 } else {
171 setMask(type,true);
172 }
173 break;
174
175 case CC_MIN_FRESH:
176 if (!p || !httpHeaderParseInt(p, &min_fresh) || min_fresh < 0) {
177 debugs(65, 2, "cc: invalid min-fresh specs near '" << item << "'");
178 clearMinFresh();
179 } else {
180 setMask(type,true);
181 }
182 break;
183
184 case CC_STALE_IF_ERROR:
185 if (!p || !httpHeaderParseInt(p, &stale_if_error) || stale_if_error < 0) {
186 debugs(65, 2, "cc: invalid stale-if-error specs near '" << item << "'");
187 clearStaleIfError();
188 } else {
189 setMask(type,true);
190 }
191 break;
192
193 case CC_PUBLIC:
194 Public(true);
195 break;
196 case CC_PRIVATE:
197 Private(true);
198 break;
199 case CC_NO_CACHE:
200 noCache(true);
201 break;
202 case CC_NO_STORE:
203 noStore(true);
204 break;
205 case CC_NO_TRANSFORM:
206 noTransform(true);
207 break;
208 case CC_MUST_REVALIDATE:
209 mustRevalidate(true);
210 break;
211 case CC_PROXY_REVALIDATE:
212 proxyRevalidate(true);
213 break;
214 case CC_ONLY_IF_CACHED:
215 onlyIfCached(true);
216 break;
217
218 case CC_OTHER:
219 if (other.size())
220 other.append(", ");
221
222 other.append(item, ilen);
223 break;
224
225 default:
226 /* note that we ignore most of '=' specs (RFCVIOLATION) */
227 break;
228 }
229 }
230
231 return (mask != 0);
232 }
233
234 void
235 HttpHdrCc::packInto(Packer * p) const
236 {
237 // optimization: if the mask is empty do nothing
238 if (mask==0)
239 return;
240
241 http_hdr_cc_type flag;
242 int pcount = 0;
243 assert(p);
244
245 for (flag = CC_PUBLIC; flag < CC_ENUM_END; ++flag) {
246 if (isSet(flag) && flag != CC_OTHER) {
247
248 /* print option name for all options */
249 packerPrintf(p, (pcount ? ", %s": "%s") , CcAttrs[flag].name);
250
251 /* for all options having values, "=value" after the name */
252 switch (flag) {
253 case CC_MAX_AGE:
254 packerPrintf(p, "=%d", (int) maxAge());
255 break;
256 case CC_S_MAXAGE:
257 packerPrintf(p, "=%d", (int) sMaxAge());
258 break;
259 case CC_MAX_STALE:
260 /* max-stale's value is optional.
261 If we didn't receive it, don't send it */
262 if (maxStale()!=MAX_STALE_ANY)
263 packerPrintf(p, "=%d", (int) maxStale());
264 break;
265 case CC_MIN_FRESH:
266 packerPrintf(p, "=%d", (int) minFresh());
267 break;
268 default:
269 /* do nothing, directive was already printed */
270 break;
271 }
272
273 ++pcount;
274 }
275 }
276
277 if (other.size() != 0)
278 packerPrintf(p, (pcount ? ", " SQUIDSTRINGPH : SQUIDSTRINGPH),
279 SQUIDSTRINGPRINT(other));
280 }
281
282 void
283 httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist)
284 {
285 http_hdr_cc_type c;
286 assert(cc);
287
288 for (c = CC_PUBLIC; c < CC_ENUM_END; ++c)
289 if (cc->isSet(c))
290 hist->count(c);
291 }
292
293 void
294 httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
295 {
296 extern const HttpHeaderStat *dump_stat; /* argh! */
297 const int id = (int) val;
298 const int valid_id = id >= 0 && id < CC_ENUM_END;
299 const char *name = valid_id ? CcAttrs[id].name : "INVALID";
300
301 if (count || valid_id)
302 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
303 id, name, count, xdiv(count, dump_stat->ccParsedCount));
304 }
305
306 #if !_USE_INLINE_
307 #include "HttpHdrCc.cci"
308 #endif