]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrSc.cc
2 * Copyright (C) 1996-2015 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 "base/LookupTable.h"
13 #include "HttpHdrSc.h"
14 #include "HttpHeader.h"
15 #include "HttpHeaderFieldStat.h"
16 #include "HttpHeaderStat.h"
17 #include "HttpHeaderTools.h"
25 /* a row in the table used for parsing surrogate-control header and statistics */
29 HttpHeaderFieldStat stat
;
32 /* this table is used for parsing surrogate control header */
33 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
34 //todo: implement constraint
35 static const LookupTable
<http_hdr_sc_type
>::Record ScAttrs
[] {
36 {"no-store", SC_NO_STORE
},
37 {"no-store-remote", SC_NO_STORE_REMOTE
},
38 {"max-age", SC_MAX_AGE
},
39 {"content", SC_CONTENT
},
40 {"Other,", SC_OTHER
}, /* ',' will protect from matches */
41 {nullptr, SC_ENUM_END
} /* SC_ENUM_END taken as invalid value */
43 LookupTable
<http_hdr_sc_type
> scLookupTable(SC_ENUM_END
, ScAttrs
);
44 std::vector
<HttpHeaderFieldStat
> scHeaderStats(SC_ENUM_END
);
46 http_hdr_sc_type
&operator++ (http_hdr_sc_type
&aHeader
)
48 int tmp
= (int)aHeader
;
49 aHeader
= (http_hdr_sc_type
)(++tmp
);
53 int operator - (http_hdr_sc_type
const &anSc
, http_hdr_sc_type
const &anSc2
)
55 return (int)anSc
- (int)anSc2
;
58 /* module initialization */
61 httpHdrScInitModule(void)
63 // check invariant on ScAttrs
64 for (int i
= 0; ScAttrs
[i
].name
!= nullptr; ++i
)
65 assert(i
== ScAttrs
[i
].id
);
69 httpHdrScCleanModule(void)
75 /* creates an sc object from a 0-terminating string */
77 httpHdrScParseCreate(const String
& str
)
79 HttpHdrSc
*sc
= new HttpHdrSc();
81 if (!sc
->parse(&str
)) {
89 /* parses a 0-terminating string and inits sc */
91 HttpHdrSc::parse(const String
* str
)
95 const char *p
; /* '=' parameter */
96 const char *pos
= NULL
;
97 const char *target
= NULL
; /* ;foo */
98 const char *temp
= NULL
; /* temp buffer */
99 http_hdr_sc_type type
;
102 HttpHdrScTarget
*sct
;
105 /* iterate through comma separated list */
107 while (strListGetItem(str
, ',', &item
, &ilen
, &pos
)) {
110 /* decrease ilen to still match the token for '=' statements */
112 if ((p
= strchr(item
, '=')) && (p
- item
< ilen
)) {
113 vlen
= ilen
- (p
+ 1 - item
);
118 /* decrease ilen to still match the token for ';' qualified non '=' statments */
119 else if ((p
= strchr(item
, ';')) && (p
- item
< ilen
)) {
125 type
= scLookupTable
.lookup(SBuf(item
,ilen
));
127 if (type
== SC_ENUM_END
) {
128 debugs(90, 2, "hdr sc: unknown control-directive: near '" << item
<< "' in '" << str
<< "'");
132 /* Is this a targeted directive? */
133 /* TODO: remove the temporary useage and use memrchr and the information we have instead */
134 temp
= xstrndup (item
, initiallen
+ 1);
136 if (!((target
= strrchr (temp
, ';')) && !strchr (target
, '"') && *(target
+ 1) != '\0'))
141 sct
= sc
->findTarget(target
);
144 sct
= new HttpHdrScTarget(target
);
150 if (sct
->isSet(type
)) {
151 if (type
!= SC_OTHER
)
152 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item
<< "' in '" << str
<< "'");
154 ++ scHeaderStats
[type
].repCount
;
159 /* process directives */
165 case SC_NO_STORE_REMOTE
:
166 sct
->noStoreRemote(true);
171 if (p
&& httpHeaderParseInt(p
, &ma
)) {
174 if ((p
= strchr (p
, '+'))) {
176 ++p
; //skip the + char
177 if (httpHeaderParseInt(p
, &ms
)) {
180 debugs(90, 2, "sc: invalid max-stale specs near '" << item
<< "'");
181 sct
->clearMaxStale();
182 /* leave the max-age alone */
186 debugs(90, 2, "sc: invalid max-age specs near '" << item
<< "'");
195 if ( p
&& httpHeaderParseQuotedString(p
, vlen
, &sct
->content_
)) {
196 sct
->setMask(SC_CONTENT
,true); // ugly but saves a copy
198 debugs(90, 2, "sc: invalid content= quoted string near '" << item
<< "'");
209 return sc
->targets
.head
!= NULL
;
212 HttpHdrSc::~HttpHdrSc()
215 dlink_node
*sct
= targets
.head
;
218 HttpHdrScTarget
*t
= static_cast<HttpHdrScTarget
*>(sct
->data
);
220 dlinkDelete (&t
->node
, &targets
);
226 HttpHdrSc::HttpHdrSc(const HttpHdrSc
&sc
)
228 dlink_node
*node
= sc
.targets
.head
;
231 HttpHdrScTarget
*dupsct
= new HttpHdrScTarget(*static_cast<HttpHdrScTarget
*>(node
->data
));
232 addTargetAtTail(dupsct
);
238 HttpHdrScTarget::packInto(Packable
* p
) const
240 http_hdr_sc_type flag
;
244 for (flag
= SC_NO_STORE
; flag
< SC_ENUM_END
; ++flag
) {
245 if (isSet(flag
) && flag
!= SC_OTHER
) {
247 /* print option name */
248 p
->appendf((pcount
? ", %s" : "%s"), ScAttrs
[flag
].name
);
250 /* handle options with values */
252 if (flag
== SC_MAX_AGE
)
253 p
->appendf("=%d", (int) max_age
);
255 if (flag
== SC_CONTENT
)
256 p
->appendf("=\"" SQUIDSTRINGPH
"\"", SQUIDSTRINGPRINT(content_
));
263 p
->appendf(";" SQUIDSTRINGPH
, SQUIDSTRINGPRINT(target
));
267 HttpHdrSc::packInto(Packable
* 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, double val
, double, int count
)
307 extern const HttpHeaderStat
*dump_stat
; /* argh! */
308 const int id
= (int) val
;
309 const bool valid_id
= id
>= 0 && id
< SC_ENUM_END
;
310 const char *name
= valid_id
? ScAttrs
[id
].name
: "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, double val
, double, int count
)
320 extern const HttpHeaderStat
*dump_stat
; /* argh! */
321 const int id
= (int) val
;
322 const bool valid_id
= id
>= 0 && id
< SC_ENUM_END
;
323 const char *name
= valid_id
? ScAttrs
[id
].name
: "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
);