]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/HttpHdrSc.cc
74f0cfcb6b0273ad7a9c27c588bfe6031f823ad6
2 * Copyright (C) 1996-2017 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" // pulled in by HttpHdrScTarget.h
14 #include "HttpHdrScTarget.h"
15 #include "HttpHeader.h"
16 #include "HttpHeaderFieldStat.h"
17 #include "HttpHeaderStat.h"
18 #include "HttpHeaderTools.h"
26 /* this table is used for parsing surrogate control header */
27 /* order must match that of enum http_hdr_sc_type. The constraint is verified at initialization time */
28 //todo: implement constraint
29 static const LookupTable
<http_hdr_sc_type
>::Record ScAttrs
[] {
30 {"no-store", SC_NO_STORE
},
31 {"no-store-remote", SC_NO_STORE_REMOTE
},
32 {"max-age", SC_MAX_AGE
},
33 {"content", SC_CONTENT
},
34 {"Other,", SC_OTHER
}, /* ',' will protect from matches */
35 {nullptr, SC_ENUM_END
} /* SC_ENUM_END taken as invalid value */
37 LookupTable
<http_hdr_sc_type
> scLookupTable(SC_ENUM_END
, ScAttrs
);
38 std::vector
<HttpHeaderFieldStat
> scHeaderStats(SC_ENUM_END
);
40 // used when iterating over flags
41 http_hdr_sc_type
&operator++ (http_hdr_sc_type
&aHeader
)
43 int tmp
= static_cast<int>(aHeader
);
44 aHeader
= static_cast<http_hdr_sc_type
>(++tmp
);
49 httpHdrScInitModule(void)
51 // check invariant on ScAttrs
52 for (int i
= 0; ScAttrs
[i
].name
!= nullptr; ++i
)
53 assert(i
== ScAttrs
[i
].id
);
58 /* creates an sc object from a 0-terminating string */
60 httpHdrScParseCreate(const String
& str
)
62 HttpHdrSc
*sc
= new HttpHdrSc();
64 if (!sc
->parse(&str
)) {
72 /* parses a 0-terminating string and inits sc */
74 HttpHdrSc::parse(const String
* str
)
78 const char *p
; /* '=' parameter */
79 const char *pos
= NULL
;
80 const char *target
= NULL
; /* ;foo */
81 const char *temp
= NULL
; /* temp buffer */
82 http_hdr_sc_type type
;
88 /* iterate through comma separated list */
90 while (strListGetItem(str
, ',', &item
, &ilen
, &pos
)) {
93 /* decrease ilen to still match the token for '=' statements */
95 if ((p
= strchr(item
, '=')) && (p
- item
< ilen
)) {
96 vlen
= ilen
- (p
+ 1 - item
);
101 /* decrease ilen to still match the token for ';' qualified non '=' statments */
102 else if ((p
= strchr(item
, ';')) && (p
- item
< ilen
)) {
108 type
= scLookupTable
.lookup(SBuf(item
,ilen
));
110 if (type
== SC_ENUM_END
) {
111 debugs(90, 2, "hdr sc: unknown control-directive: near '" << item
<< "' in '" << str
<< "'");
115 /* Is this a targeted directive? */
116 /* TODO: remove the temporary useage and use memrchr and the information we have instead */
117 temp
= xstrndup (item
, initiallen
+ 1);
119 if (!((target
= strrchr (temp
, ';')) && !strchr (target
, '"') && *(target
+ 1) != '\0'))
124 sct
= sc
->findTarget(target
);
127 sct
= new HttpHdrScTarget(target
);
133 if (sct
->isSet(type
)) {
134 if (type
!= SC_OTHER
)
135 debugs(90, 2, "hdr sc: ignoring duplicate control-directive: near '" << item
<< "' in '" << str
<< "'");
137 ++ scHeaderStats
[type
].repCount
;
142 /* process directives */
148 case SC_NO_STORE_REMOTE
:
149 sct
->noStoreRemote(true);
154 if (p
&& httpHeaderParseInt(p
, &ma
)) {
157 if ((p
= strchr (p
, '+'))) {
159 ++p
; //skip the + char
160 if (httpHeaderParseInt(p
, &ms
)) {
163 debugs(90, 2, "sc: invalid max-stale specs near '" << item
<< "'");
164 sct
->clearMaxStale();
165 /* leave the max-age alone */
169 debugs(90, 2, "sc: invalid max-age specs near '" << item
<< "'");
178 if ( p
&& httpHeaderParseQuotedString(p
, vlen
, &sct
->content_
)) {
179 sct
->setMask(SC_CONTENT
,true); // ugly but saves a copy
181 debugs(90, 2, "sc: invalid content= quoted string near '" << item
<< "'");
192 return sc
->targets
.head
!= NULL
;
195 HttpHdrSc::~HttpHdrSc()
198 dlink_node
*sct
= targets
.head
;
201 HttpHdrScTarget
*t
= static_cast<HttpHdrScTarget
*>(sct
->data
);
203 dlinkDelete (&t
->node
, &targets
);
209 HttpHdrSc::HttpHdrSc(const HttpHdrSc
&sc
)
211 dlink_node
*node
= sc
.targets
.head
;
214 HttpHdrScTarget
*dupsct
= new HttpHdrScTarget(*static_cast<HttpHdrScTarget
*>(node
->data
));
215 addTargetAtTail(dupsct
);
221 HttpHdrScTarget::packInto(Packable
* p
) const
223 http_hdr_sc_type flag
;
227 for (flag
= SC_NO_STORE
; flag
< SC_ENUM_END
; ++flag
) {
228 if (isSet(flag
) && flag
!= SC_OTHER
) {
230 /* print option name */
231 p
->appendf((pcount
? ", %s" : "%s"), ScAttrs
[flag
].name
);
233 /* handle options with values */
235 if (flag
== SC_MAX_AGE
)
236 p
->appendf("=%d", (int) max_age
);
238 if (flag
== SC_CONTENT
)
239 p
->appendf("=\"" SQUIDSTRINGPH
"\"", SQUIDSTRINGPRINT(content_
));
246 p
->appendf(";" SQUIDSTRINGPH
, SQUIDSTRINGPRINT(target
));
250 HttpHdrSc::packInto(Packable
* p
) const
257 static_cast<HttpHdrScTarget
*>(node
->data
)->packInto(p
);
262 /* negative max_age will clean old max_Age setting */
264 HttpHdrSc::setMaxAge(char const *target
, int max_age
)
266 HttpHdrScTarget
*sct
= findTarget(target
);
269 sct
= new HttpHdrScTarget(target
);
270 dlinkAddTail (sct
, &sct
->node
, &targets
);
273 sct
->maxAge(max_age
);
277 HttpHdrSc::updateStats(StatHist
* hist
) const
279 dlink_node
*sct
= targets
.head
;
282 static_cast<HttpHdrScTarget
*>(sct
->data
)->updateStats(hist
);
288 httpHdrScTargetStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
290 extern const HttpHeaderStat
*dump_stat
; /* argh! */
291 const int id
= (int) val
;
292 const bool valid_id
= id
>= 0 && id
< SC_ENUM_END
;
293 const char *name
= valid_id
? ScAttrs
[id
].name
: "INVALID";
295 if (count
|| valid_id
)
296 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
297 id
, name
, count
, xdiv(count
, dump_stat
->scParsedCount
));
301 httpHdrScStatDumper(StoreEntry
* sentry
, int, double val
, double, int count
)
303 extern const HttpHeaderStat
*dump_stat
; /* argh! */
304 const int id
= (int) val
;
305 const bool valid_id
= id
>= 0 && id
< SC_ENUM_END
;
306 const char *name
= valid_id
? ScAttrs
[id
].name
: "INVALID";
308 if (count
|| valid_id
)
309 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
310 id
, name
, count
, xdiv(count
, dump_stat
->scParsedCount
));
314 HttpHdrSc::findTarget(const char *target
)
320 HttpHdrScTarget
*sct
= (HttpHdrScTarget
*)node
->data
;
322 if (target
&& sct
->target
.size() > 0 && !strcmp(target
, sct
->target
.termedBuf()))
324 else if (!target
&& sct
->target
.size() == 0)
334 HttpHdrSc::getMergedTarget(const char *ourtarget
)
336 HttpHdrScTarget
*sctus
= findTarget(ourtarget
);
337 HttpHdrScTarget
*sctgeneric
= findTarget(NULL
);
339 if (sctgeneric
|| sctus
) {
340 HttpHdrScTarget
*sctusable
= new HttpHdrScTarget(NULL
);
343 sctusable
->mergeWith(sctgeneric
);
346 sctusable
->mergeWith(sctus
);
355 HttpHdrSc::addTarget(HttpHdrScTarget
*t
) {
356 dlinkAdd(t
, &t
->node
, &targets
);
360 HttpHdrSc::addTargetAtTail(HttpHdrScTarget
*t
) {
361 dlinkAddTail (t
, &t
->node
, &targets
);