2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 90 HTTP Cache Control Header */
12 #include "HttpHdrSc.h"
13 #include "HttpHeader.h"
14 #include "HttpHeaderFieldInfo.h"
15 #include "HttpHeaderFieldStat.h"
16 #include "HttpHeaderStat.h"
17 #include "HttpHeaderTools.h"
23 /* a row in the table used for parsing surrogate-control header and statistics */
27 HttpHeaderFieldStat stat
;
30 /* this table is used for parsing surrogate control header */
31 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
32 //todo: implement constraint
33 static const HttpHeaderFieldAttrs ScAttrs
[SC_ENUM_END
] = {
34 {"no-store", (http_hdr_type
)SC_NO_STORE
},
35 {"no-store-remote", (http_hdr_type
)SC_NO_STORE_REMOTE
},
36 {"max-age", (http_hdr_type
)SC_MAX_AGE
},
37 {"content", (http_hdr_type
)SC_CONTENT
},
38 {"Other,", (http_hdr_type
)SC_OTHER
} /* ',' will protect from matches */
41 HttpHeaderFieldInfo
*ScFieldsInfo
= NULL
;
43 http_hdr_sc_type
&operator++ (http_hdr_sc_type
&aHeader
)
45 int tmp
= (int)aHeader
;
46 aHeader
= (http_hdr_sc_type
)(++tmp
);
50 int operator - (http_hdr_sc_type
const &anSc
, http_hdr_sc_type
const &anSc2
)
52 return (int)anSc
- (int)anSc2
;
55 /* module initialization */
58 httpHdrScInitModule(void)
60 ScFieldsInfo
= httpHeaderBuildFieldsInfo(ScAttrs
, SC_ENUM_END
);
64 httpHdrScCleanModule(void)
66 httpHeaderDestroyFieldsInfo(ScFieldsInfo
, SC_ENUM_END
);
72 /* creates an sc object from a 0-terminating string */
74 httpHdrScParseCreate(const String
& str
)
76 HttpHdrSc
*sc
= new HttpHdrSc();
78 if (!sc
->parse(&str
)) {
86 /* parses a 0-terminating string and inits sc */
88 HttpHdrSc::parse(const String
* str
)
92 const char *p
; /* '=' parameter */
93 const char *pos
= NULL
;
94 const char *target
= NULL
; /* ;foo */
95 const char *temp
= NULL
; /* temp buffer */
102 /* iterate through comma separated list */
104 while (strListGetItem(str
, ',', &item
, &ilen
, &pos
)) {
107 /* decrease ilen to still match the token for '=' statements */
109 if ((p
= strchr(item
, '=')) && (p
- item
< ilen
)) {
110 vlen
= ilen
- (p
+ 1 - item
);
115 /* decrease ilen to still match the token for ';' qualified non '=' statments */
116 else if ((p
= strchr(item
, ';')) && (p
- item
< ilen
)) {
122 /* TODO: use a type-safe map-based lookup */
123 type
= httpHeaderIdByName(item
, ilen
,
124 ScFieldsInfo
, SC_ENUM_END
);
127 debugs(90, 2, "hdr sc: unknown control-directive: near '" << item
<< "' in '" << str
<< "'");
131 /* Is this a targeted directive? */
132 /* TODO: remove the temporary useage and use memrchr and the information we have instead */
133 temp
= xstrndup (item
, initiallen
+ 1);
135 if (!((target
= strrchr (temp
, ';')) && !strchr (target
, '"') && *(target
+ 1) != '\0'))
140 sct
= sc
->findTarget(target
);
143 sct
= new HttpHdrScTarget(target
);
149 if (sct
->isSet(static_cast<http_hdr_sc_type
>(type
))) {
150 if (type
!= SC_OTHER
)
151 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item
<< "' in '" << str
<< "'");
153 ++ ScFieldsInfo
[type
].stat
.repCount
;
158 /* process directives */
164 case SC_NO_STORE_REMOTE
:
165 sct
->noStoreRemote(true);
170 if (p
&& httpHeaderParseInt(p
, &ma
)) {
173 if ((p
= strchr (p
, '+'))) {
175 ++p
; //skip the + char
176 if (httpHeaderParseInt(p
, &ms
)) {
179 debugs(90, 2, "sc: invalid max-stale specs near '" << item
<< "'");
180 sct
->clearMaxStale();
181 /* leave the max-age alone */
185 debugs(90, 2, "sc: invalid max-age specs near '" << item
<< "'");
194 if ( p
&& httpHeaderParseQuotedString(p
, vlen
, &sct
->content_
)) {
195 sct
->setMask(SC_CONTENT
,true); // ugly but saves a copy
197 debugs(90, 2, "sc: invalid content= quoted string near '" << item
<< "'");
208 return sc
->targets
.head
!= NULL
;
211 HttpHdrSc::~HttpHdrSc()
214 dlink_node
*sct
= targets
.head
;
217 HttpHdrScTarget
*t
= static_cast<HttpHdrScTarget
*>(sct
->data
);
219 dlinkDelete (&t
->node
, &targets
);
225 HttpHdrSc::HttpHdrSc(const HttpHdrSc
&sc
)
227 dlink_node
*node
= sc
.targets
.head
;
230 HttpHdrScTarget
*dupsct
= new HttpHdrScTarget(*static_cast<HttpHdrScTarget
*>(node
->data
));
231 addTargetAtTail(dupsct
);
237 HttpHdrScTarget::packInto(Packer
* p
) const
239 http_hdr_sc_type flag
;
243 for (flag
= SC_NO_STORE
; flag
< SC_ENUM_END
; ++flag
) {
244 if (isSet(flag
) && flag
!= SC_OTHER
) {
246 /* print option name */
247 packerPrintf(p
, (pcount
? ", " SQUIDSTRINGPH
: SQUIDSTRINGPH
),
248 SQUIDSTRINGPRINT(ScFieldsInfo
[flag
].name
));
250 /* handle options with values */
252 if (flag
== SC_MAX_AGE
)
253 packerPrintf(p
, "=%d", (int) max_age
);
255 if (flag
== SC_CONTENT
)
256 packerPrintf(p
, "=\"" SQUIDSTRINGPH
"\"", SQUIDSTRINGPRINT(content_
));
263 packerPrintf (p
, ";" SQUIDSTRINGPH
, SQUIDSTRINGPRINT(target
));
267 HttpHdrSc::packInto(Packer
* p
) const
274 static_cast<HttpHdrScTarget
*>(node
->data
)->packInto(p
);
279 /* negative max_age will clean old max_Age setting */
281 HttpHdrSc::setMaxAge(char const *target
, int max_age
)
283 HttpHdrScTarget
*sct
= findTarget(target
);
286 sct
= new HttpHdrScTarget(target
);
287 dlinkAddTail (sct
, &sct
->node
, &targets
);
290 sct
->maxAge(max_age
);
294 HttpHdrSc::updateStats(StatHist
* hist
) const
296 dlink_node
*sct
= targets
.head
;
299 static_cast<HttpHdrScTarget
*>(sct
->data
)->updateStats(hist
);
305 httpHdrScTargetStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
307 extern const HttpHeaderStat
*dump_stat
; /* argh! */
308 const int id
= (int) val
;
309 const int valid_id
= id
>= 0 && id
< SC_ENUM_END
;
310 const char *name
= valid_id
? ScFieldsInfo
[id
].name
.termedBuf() : "INVALID";
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
));
318 httpHdrScStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
320 extern const HttpHeaderStat
*dump_stat
; /* argh! */
321 const int id
= (int) val
;
322 const int valid_id
= id
>= 0 && id
< SC_ENUM_END
;
323 const char *name
= valid_id
? ScFieldsInfo
[id
].name
.termedBuf() : "INVALID";
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
));
331 HttpHdrSc::findTarget(const char *target
)
337 HttpHdrScTarget
*sct
= (HttpHdrScTarget
*)node
->data
;
339 if (target
&& sct
->target
.size() > 0 && !strcmp(target
, sct
->target
.termedBuf()))
341 else if (!target
&& sct
->target
.size() == 0)
351 HttpHdrSc::getMergedTarget(const char *ourtarget
)
353 HttpHdrScTarget
*sctus
= findTarget(ourtarget
);
354 HttpHdrScTarget
*sctgeneric
= findTarget(NULL
);
356 if (sctgeneric
|| sctus
) {
357 HttpHdrScTarget
*sctusable
= new HttpHdrScTarget(NULL
);
360 sctusable
->mergeWith(sctgeneric
);
363 sctusable
->mergeWith(sctus
);