5 * DEBUG: section 90 HTTP Cache Control Header
6 * AUTHOR: Alex Rousskov
7 * Robert Collins (Surrogate-Control is derived from
9 * Francesco Chemolli (c++ refactoring)
11 * SQUID Web Proxy Cache http://www.squid-cache.org/
12 * ----------------------------------------------------------
14 * Squid is the result of efforts by numerous individuals from
15 * the Internet community; see the CONTRIBUTORS file for full
16 * details. Many organizations have provided support for Squid's
17 * development; see the SPONSORS file for full details. Squid is
18 * Copyrighted (C) 2001 by the Regents of the University of
19 * California; see the COPYRIGHT file for full details. Squid
20 * incorporates software developed and/or copyrighted by other
21 * sources; see the CREDITS file for full details.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 #include "squid-old.h"
41 #include "HttpHeader.h"
42 #include "HttpHeaderStat.h"
43 #include "HttpHdrSc.h"
49 /* a row in the table used for parsing surrogate-control header and statistics */
53 HttpHeaderFieldStat stat
;
56 /* this table is used for parsing surrogate control header */
57 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
58 //todo: implement constraint
59 static const HttpHeaderFieldAttrs ScAttrs
[SC_ENUM_END
] = {
60 {"no-store", (http_hdr_type
)SC_NO_STORE
},
61 {"no-store-remote", (http_hdr_type
)SC_NO_STORE_REMOTE
},
62 {"max-age", (http_hdr_type
)SC_MAX_AGE
},
63 {"content", (http_hdr_type
)SC_CONTENT
},
64 {"Other,", (http_hdr_type
)SC_OTHER
} /* ',' will protect from matches */
67 HttpHeaderFieldInfo
*ScFieldsInfo
= NULL
;
69 http_hdr_sc_type
&operator++ (http_hdr_sc_type
&aHeader
)
71 int tmp
= (int)aHeader
;
72 aHeader
= (http_hdr_sc_type
)(++tmp
);
76 int operator - (http_hdr_sc_type
const &anSc
, http_hdr_sc_type
const &anSc2
)
78 return (int)anSc
- (int)anSc2
;
82 /* module initialization */
85 httpHdrScInitModule(void)
87 ScFieldsInfo
= httpHeaderBuildFieldsInfo(ScAttrs
, SC_ENUM_END
);
91 httpHdrScCleanModule(void)
93 httpHeaderDestroyFieldsInfo(ScFieldsInfo
, SC_ENUM_END
);
99 /* creates an sc object from a 0-terminating string */
101 httpHdrScParseCreate(const String
& str
)
103 HttpHdrSc
*sc
= new HttpHdrSc();
105 if (!sc
->parse(&str
)) {
113 /* parses a 0-terminating string and inits sc */
115 HttpHdrSc::parse(const String
* str
)
119 const char *p
; /* '=' parameter */
120 const char *pos
= NULL
;
121 const char *target
= NULL
; /* ;foo */
122 const char *temp
= NULL
; /* temp buffer */
126 HttpHdrScTarget
*sct
;
129 /* iterate through comma separated list */
131 while (strListGetItem(str
, ',', &item
, &ilen
, &pos
)) {
134 /* decrease ilen to still match the token for '=' statements */
136 if ((p
= strchr(item
, '=')) && (p
- item
< ilen
)) {
137 vlen
= ilen
- (p
+ 1 - item
);
142 /* decrease ilen to still match the token for ';' qualified non '=' statments */
143 else if ((p
= strchr(item
, ';')) && (p
- item
< ilen
)) {
149 /* TODO: use a type-safe map-based lookup */
150 type
= httpHeaderIdByName(item
, ilen
,
151 ScFieldsInfo
, SC_ENUM_END
);
154 debugs(90, 2, "hdr sc: unknown control-directive: near '" << item
<< "' in '" << str
<< "'");
158 /* Is this a targeted directive? */
159 /* TODO: remove the temporary useage and use memrchr and the information we have instead */
160 temp
= xstrndup (item
, initiallen
+ 1);
162 if (!((target
= strrchr (temp
, ';')) && !strchr (target
, '"') && *(target
+ 1) != '\0'))
167 sct
= sc
->findTarget(target
);
170 sct
= new HttpHdrScTarget(target
);
176 if (sct
->isSet(static_cast<http_hdr_sc_type
>(type
))) {
177 if (type
!= SC_OTHER
)
178 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item
<< "' in '" << str
<< "'");
180 ++ ScFieldsInfo
[type
].stat
.repCount
;
185 /* process directives */
191 case SC_NO_STORE_REMOTE
:
192 sct
->noStoreRemote(true);
197 if (p
&& httpHeaderParseInt(p
, &ma
)) {
200 debugs(90, 2, "sc: invalid max-age specs near '" << item
<< "'");
204 if ((p
= strchr (p
, '+'))) {
206 ++p
; //skip the + char
207 if (httpHeaderParseInt(p
, &ms
)) {
210 debugs(90, 2, "sc: invalid max-stale specs near '" << item
<< "'");
211 sct
->clearMaxStale();
212 /* leave the max-age alone */
220 if ( p
&& httpHeaderParseQuotedString(p
, vlen
, &sct
->content_
)) {
221 sct
->setMask(SC_CONTENT
,true); // ugly but saves a copy
223 debugs(90, 2, "sc: invalid content= quoted string near '" << item
<< "'");
234 return sc
->targets
.head
!= NULL
;
237 HttpHdrSc::~HttpHdrSc()
240 dlink_node
*sct
= targets
.head
;
243 HttpHdrScTarget
*t
= static_cast<HttpHdrScTarget
*>(sct
->data
);
245 dlinkDelete (&t
->node
, &targets
);
252 HttpHdrSc::HttpHdrSc(const HttpHdrSc
&sc
)
254 dlink_node
*node
= sc
.targets
.head
;
257 HttpHdrScTarget
*dupsct
= new HttpHdrScTarget(*static_cast<HttpHdrScTarget
*>(node
->data
));
258 addTargetAtTail(dupsct
);
264 HttpHdrScTarget::packInto(Packer
* p
) const
266 http_hdr_sc_type flag
;
270 for (flag
= SC_NO_STORE
; flag
< SC_ENUM_END
; ++flag
) {
271 if (isSet(flag
) && flag
!= SC_OTHER
) {
273 /* print option name */
274 packerPrintf(p
, (pcount
? ", " SQUIDSTRINGPH
: SQUIDSTRINGPH
),
275 SQUIDSTRINGPRINT(ScFieldsInfo
[flag
].name
));
277 /* handle options with values */
279 if (flag
== SC_MAX_AGE
)
280 packerPrintf(p
, "=%d", (int) max_age
);
282 if (flag
== SC_CONTENT
)
283 packerPrintf(p
, "=\"" SQUIDSTRINGPH
"\"", SQUIDSTRINGPRINT(content_
));
290 packerPrintf (p
, ";" SQUIDSTRINGPH
, SQUIDSTRINGPRINT(target
));
294 HttpHdrSc::packInto(Packer
* p
) const
301 static_cast<HttpHdrScTarget
*>(node
->data
)->packInto(p
);
306 /* negative max_age will clean old max_Age setting */
308 HttpHdrSc::setMaxAge(char const *target
, int max_age
)
310 HttpHdrScTarget
*sct
= findTarget(target
);
313 sct
= new HttpHdrScTarget(target
);
314 dlinkAddTail (sct
, &sct
->node
, &targets
);
317 sct
->maxAge(max_age
);
321 HttpHdrSc::updateStats(StatHist
* hist
) const
323 dlink_node
*sct
= targets
.head
;
326 static_cast<HttpHdrScTarget
*>(sct
->data
)->updateStats(hist
);
332 httpHdrScTargetStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
334 extern const HttpHeaderStat
*dump_stat
; /* argh! */
335 const int id
= (int) val
;
336 const int valid_id
= id
>= 0 && id
< SC_ENUM_END
;
337 const char *name
= valid_id
? ScFieldsInfo
[id
].name
.termedBuf() : "INVALID";
339 if (count
|| valid_id
)
340 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
341 id
, name
, count
, xdiv(count
, dump_stat
->scParsedCount
));
345 httpHdrScStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
347 extern const HttpHeaderStat
*dump_stat
; /* argh! */
348 const int id
= (int) val
;
349 const int valid_id
= id
>= 0 && id
< SC_ENUM_END
;
350 const char *name
= valid_id
? ScFieldsInfo
[id
].name
.termedBuf() : "INVALID";
352 if (count
|| valid_id
)
353 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
354 id
, name
, count
, xdiv(count
, dump_stat
->scParsedCount
));
358 HttpHdrSc::findTarget(const char *target
)
364 HttpHdrScTarget
*sct
= (HttpHdrScTarget
*)node
->data
;
366 if (target
&& sct
->target
.defined() && !strcmp (target
, sct
->target
.termedBuf()))
368 else if (!target
&& sct
->target
.undefined())
378 HttpHdrSc::getMergedTarget(const char *ourtarget
)
380 HttpHdrScTarget
*sctus
= findTarget(ourtarget
);
381 HttpHdrScTarget
*sctgeneric
= findTarget(NULL
);
383 if (sctgeneric
|| sctus
) {
384 HttpHdrScTarget
*sctusable
= new HttpHdrScTarget(NULL
);
387 sctusable
->mergeWith(sctgeneric
);
390 sctusable
->mergeWith(sctus
);