]> git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrCc.cc
Updated copyright
[thirdparty/squid.git] / src / HttpHdrCc.cc
1
2 /*
3 * $Id: HttpHdrCc.cc,v 1.22 2001/01/12 00:37:13 wessels Exp $
4 *
5 * DEBUG: section 65 HTTP Cache Control Header
6 * AUTHOR: Alex Rousskov
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37
38 /* this table is used for parsing cache control header */
39 static const HttpHeaderFieldAttrs CcAttrs[CC_ENUM_END] =
40 {
41 {"public", CC_PUBLIC},
42 {"private", CC_PRIVATE},
43 {"no-cache", CC_NO_CACHE},
44 {"no-store", CC_NO_STORE},
45 {"no-transform", CC_NO_TRANSFORM},
46 {"must-revalidate", CC_MUST_REVALIDATE},
47 {"proxy-revalidate", CC_PROXY_REVALIDATE},
48 {"only-if-cached", CC_ONLY_IF_CACHED},
49 {"max-age", CC_MAX_AGE},
50 {"s-maxage", CC_S_MAXAGE},
51 {"max-stale", CC_MAX_STALE},
52 {"Other,", CC_OTHER} /* ',' will protect from matches */
53 };
54 HttpHeaderFieldInfo *CcFieldsInfo = NULL;
55
56 /* local prototypes */
57 static int httpHdrCcParseInit(HttpHdrCc * cc, const String * str);
58
59
60 /* module initialization */
61
62 void
63 httpHdrCcInitModule(void)
64 {
65 CcFieldsInfo = httpHeaderBuildFieldsInfo(CcAttrs, CC_ENUM_END);
66 }
67
68 void
69 httpHdrCcCleanModule(void)
70 {
71 httpHeaderDestroyFieldsInfo(CcFieldsInfo, CC_ENUM_END);
72 CcFieldsInfo = NULL;
73 }
74
75 /* implementation */
76
77 HttpHdrCc *
78 httpHdrCcCreate(void)
79 {
80 HttpHdrCc *cc = memAllocate(MEM_HTTP_HDR_CC);
81 cc->max_age = cc->s_maxage = cc->max_stale = -1;
82 return cc;
83 }
84
85 /* creates an cc object from a 0-terminating string */
86 HttpHdrCc *
87 httpHdrCcParseCreate(const String * str)
88 {
89 HttpHdrCc *cc = httpHdrCcCreate();
90 if (!httpHdrCcParseInit(cc, str)) {
91 httpHdrCcDestroy(cc);
92 cc = NULL;
93 }
94 return cc;
95 }
96
97 /* parses a 0-terminating string and inits cc */
98 static int
99 httpHdrCcParseInit(HttpHdrCc * cc, const String * str)
100 {
101 const char *item;
102 const char *p; /* '=' parameter */
103 const char *pos = NULL;
104 int type;
105 int ilen;
106 assert(cc && str);
107
108 /* iterate through comma separated list */
109 while (strListGetItem(str, ',', &item, &ilen, &pos)) {
110 /* strip '=' statements @?@ */
111 if ((p = strchr(item, '=')) && (p - item < ilen))
112 ilen = p++ - item;
113 /* find type */
114 type = httpHeaderIdByName(item, ilen,
115 CcFieldsInfo, CC_ENUM_END);
116 if (type < 0) {
117 debug(65, 2) ("hdr cc: unknown cache-directive: near '%s' in '%s'\n", item, strBuf(*str));
118 type = CC_OTHER;
119 }
120 if (EBIT_TEST(cc->mask, type)) {
121 if (type != CC_OTHER)
122 debug(65, 2) ("hdr cc: ignoring duplicate cache-directive: near '%s' in '%s'\n", item, strBuf(*str));
123 CcFieldsInfo[type].stat.repCount++;
124 continue;
125 }
126 /* update mask */
127 EBIT_SET(cc->mask, type);
128 /* post-processing special cases */
129 switch (type) {
130 case CC_MAX_AGE:
131 if (!p || !httpHeaderParseInt(p, &cc->max_age)) {
132 debug(65, 2) ("cc: invalid max-age specs near '%s'\n", item);
133 cc->max_age = -1;
134 EBIT_CLR(cc->mask, type);
135 }
136 break;
137 case CC_S_MAXAGE:
138 if (!p || !httpHeaderParseInt(p, &cc->s_maxage)) {
139 debug(65, 2) ("cc: invalid s-maxage specs near '%s'\n", item);
140 cc->s_maxage = -1;
141 EBIT_CLR(cc->mask, type);
142 }
143 break;
144 case CC_MAX_STALE:
145 if (!p || !httpHeaderParseInt(p, &cc->max_stale)) {
146 debug(65, 2) ("cc: max-stale directive is valid without value\n");
147 cc->max_stale = -1;
148 }
149 break;
150 default:
151 /* note that we ignore most of '=' specs */
152 break;
153 }
154 }
155 return cc->mask != 0;
156 }
157
158 void
159 httpHdrCcDestroy(HttpHdrCc * cc)
160 {
161 assert(cc);
162 memFree(cc, MEM_HTTP_HDR_CC);
163 }
164
165 HttpHdrCc *
166 httpHdrCcDup(const HttpHdrCc * cc)
167 {
168 HttpHdrCc *dup;
169 assert(cc);
170 dup = httpHdrCcCreate();
171 dup->mask = cc->mask;
172 dup->max_age = cc->max_age;
173 dup->s_maxage = cc->s_maxage;
174 dup->max_stale = cc->max_stale;
175 return dup;
176 }
177
178 void
179 httpHdrCcPackInto(const HttpHdrCc * cc, Packer * p)
180 {
181 http_hdr_cc_type flag;
182 int pcount = 0;
183 assert(cc && p);
184 for (flag = 0; flag < CC_ENUM_END; flag++) {
185 if (EBIT_TEST(cc->mask, flag) && flag != CC_OTHER) {
186
187 /* print option name */
188 packerPrintf(p, (pcount ? ", %s" : "%s"), strBuf(CcFieldsInfo[flag].name));
189
190 /* handle options with values */
191 if (flag == CC_MAX_AGE)
192 packerPrintf(p, "=%d", (int) cc->max_age);
193
194 if (flag == CC_S_MAXAGE)
195 packerPrintf(p, "=%d", (int) cc->s_maxage);
196
197 if (flag == CC_MAX_STALE)
198 packerPrintf(p, "=%d", (int) cc->max_stale);
199
200 pcount++;
201 }
202 }
203 }
204
205 void
206 httpHdrCcJoinWith(HttpHdrCc * cc, const HttpHdrCc * new_cc)
207 {
208 assert(cc && new_cc);
209 if (cc->max_age < 0)
210 cc->max_age = new_cc->max_age;
211 if (cc->s_maxage < 0)
212 cc->s_maxage = new_cc->s_maxage;
213 if (cc->max_stale < 0)
214 cc->max_stale = new_cc->max_stale;
215 cc->mask |= new_cc->mask;
216 }
217
218 /* negative max_age will clean old max_Age setting */
219 void
220 httpHdrCcSetMaxAge(HttpHdrCc * cc, int max_age)
221 {
222 assert(cc);
223 cc->max_age = max_age;
224 if (max_age >= 0)
225 EBIT_SET(cc->mask, CC_MAX_AGE);
226 else
227 EBIT_CLR(cc->mask, CC_MAX_AGE);
228 }
229
230 /* negative s_maxage will clean old s-maxage setting */
231 void
232 httpHdrCcSetSMaxAge(HttpHdrCc * cc, int s_maxage)
233 {
234 assert(cc);
235 cc->s_maxage = s_maxage;
236 if (s_maxage >= 0)
237 EBIT_SET(cc->mask, CC_S_MAXAGE);
238 else
239 EBIT_CLR(cc->mask, CC_S_MAXAGE);
240 }
241
242 void
243 httpHdrCcUpdateStats(const HttpHdrCc * cc, StatHist * hist)
244 {
245 http_hdr_cc_type c;
246 assert(cc);
247 for (c = 0; c < CC_ENUM_END; c++)
248 if (EBIT_TEST(cc->mask, c))
249 statHistCount(hist, c);
250 }
251
252 void
253 httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val, double size, int count)
254 {
255 extern const HttpHeaderStat *dump_stat; /* argh! */
256 const int id = (int) val;
257 const int valid_id = id >= 0 && id < CC_ENUM_END;
258 const char *name = valid_id ? strBuf(CcFieldsInfo[id].name) : "INVALID";
259 if (count || valid_id)
260 storeAppendPrintf(sentry, "%2d\t %-20s\t %5d\t %6.2f\n",
261 id, name, count, xdiv(count, dump_stat->ccParsedCount));
262 }