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 65 HTTP Cache Control Header */
12 #include "HttpHdrCc.h"
13 #include "HttpHeader.h"
14 #include "HttpHeaderFieldStat.h"
15 #include "HttpHeaderStat.h"
16 #include "HttpHeaderTools.h"
25 /* a row in the table used for parsing cache control header and statistics */
29 HttpHeaderFieldStat stat
;
32 /* order must match that of enum http_hdr_cc_type. The constraint is verified at initialization time */
33 static HttpHeaderCcFields CcAttrs
[CC_ENUM_END
] = {
34 {"public", CC_PUBLIC
},
35 {"private", CC_PRIVATE
},
36 {"no-cache", CC_NO_CACHE
},
37 {"no-store", CC_NO_STORE
},
38 {"no-transform", CC_NO_TRANSFORM
},
39 {"must-revalidate", CC_MUST_REVALIDATE
},
40 {"proxy-revalidate", CC_PROXY_REVALIDATE
},
41 {"max-age", CC_MAX_AGE
},
42 {"s-maxage", CC_S_MAXAGE
},
43 {"max-stale", CC_MAX_STALE
},
44 {"min-fresh", CC_MIN_FRESH
},
45 {"only-if-cached", CC_ONLY_IF_CACHED
},
46 {"stale-if-error", CC_STALE_IF_ERROR
},
47 {"Other,", CC_OTHER
} /* ',' will protect from matches */
50 /// Map an header name to its type, to expedite parsing
51 typedef std::map
<const SBuf
,http_hdr_cc_type
> CcNameToIdMap_t
;
52 static CcNameToIdMap_t CcNameToIdMap
;
54 /// used to walk a table of http_header_cc_type structs
55 http_hdr_cc_type
&operator++ (http_hdr_cc_type
&aHeader
)
57 int tmp
= (int)aHeader
;
58 aHeader
= (http_hdr_cc_type
)(++tmp
);
62 /// Module initialization hook
64 httpHdrCcInitModule(void)
66 /* build lookup and accounting structures */
67 for (int32_t i
= 0; i
< CC_ENUM_END
; ++i
) {
68 const HttpHeaderCcFields
&f
=CcAttrs
[i
];
69 assert(i
== f
.id
); /* verify assumption: the id is the key into the array */
71 CcNameToIdMap
[k
]=f
.id
;
75 /// Module cleanup hook.
77 httpHdrCcCleanModule(void)
79 // HdrCcNameToIdMap is self-cleaning
89 HttpHdrCc::parse(const String
& str
)
92 const char *p
; /* '=' parameter */
93 const char *pos
= NULL
;
94 http_hdr_cc_type type
;
98 /* iterate through comma separated list */
100 while (strListGetItem(&str
, ',', &item
, &ilen
, &pos
)) {
101 /* isolate directive name */
103 if ((p
= (const char *)memchr(item
, '=', ilen
)) && (p
- item
< ilen
)) {
111 const CcNameToIdMap_t::const_iterator i
=CcNameToIdMap
.find(SBuf(item
,nlen
));
112 if (i
==CcNameToIdMap
.end())
117 // ignore known duplicate directives
119 if (type
!= CC_OTHER
) {
120 debugs(65, 2, "hdr cc: ignoring duplicate cache-directive: near '" << item
<< "' in '" << str
<< "'");
121 ++CcAttrs
[type
].stat
.repCount
;
126 /* special-case-parsing and attribute-setting */
130 if (!p
|| !httpHeaderParseInt(p
, &max_age
) || max_age
< 0) {
131 debugs(65, 2, "cc: invalid max-age specs near '" << item
<< "'");
139 if (!p
|| !httpHeaderParseInt(p
, &s_maxage
) || s_maxage
< 0) {
140 debugs(65, 2, "cc: invalid s-maxage specs near '" << item
<< "'");
148 if (!p
|| !httpHeaderParseInt(p
, &max_stale
) || max_stale
< 0) {
149 debugs(65, 2, "cc: max-stale directive is valid without value");
150 maxStale(MAX_STALE_ANY
);
157 if (!p
|| !httpHeaderParseInt(p
, &min_fresh
) || min_fresh
< 0) {
158 debugs(65, 2, "cc: invalid min-fresh specs near '" << item
<< "'");
165 case CC_STALE_IF_ERROR
:
166 if (!p
|| !httpHeaderParseInt(p
, &stale_if_error
) || stale_if_error
< 0) {
167 debugs(65, 2, "cc: invalid stale-if-error specs near '" << item
<< "'");
177 // Value parameter is optional.
179 } else if (/* p &&*/ httpHeaderParseQuotedString(p
, (ilen
-nlen
-1), &temp
)) {
180 private_
.append(temp
);
182 debugs(65, 2, "cc: invalid private= specs near '" << item
<< "'");
184 // to be safe we ignore broken parameters, but always remember the 'private' part.
192 // On Requests, missing value parameter is expected syntax.
193 // On Responses, value parameter is optional.
196 } else if (/* p &&*/ httpHeaderParseQuotedString(p
, (ilen
-nlen
-1), &temp
)) {
197 // On Requests, a value parameter is invalid syntax.
198 // XXX: identify when parsing request header and dump err message here.
200 no_cache
.append(temp
);
202 debugs(65, 2, "cc: invalid no-cache= specs near '" << item
<< "'");
213 case CC_NO_TRANSFORM
:
216 case CC_MUST_REVALIDATE
:
217 mustRevalidate(true);
219 case CC_PROXY_REVALIDATE
:
220 proxyRevalidate(true);
222 case CC_ONLY_IF_CACHED
:
230 other
.append(item
, ilen
);
234 /* note that we ignore most of '=' specs (RFCVIOLATION) */
243 HttpHdrCc::packInto(Packer
* p
) const
245 // optimization: if the mask is empty do nothing
249 http_hdr_cc_type flag
;
253 for (flag
= CC_PUBLIC
; flag
< CC_ENUM_END
; ++flag
) {
254 if (isSet(flag
) && flag
!= CC_OTHER
) {
256 /* print option name for all options */
257 packerPrintf(p
, (pcount
? ", %s": "%s") , CcAttrs
[flag
].name
);
259 /* for all options having values, "=value" after the name */
262 packerPrintf(p
, "=%d", (int) maxAge());
265 packerPrintf(p
, "=%d", (int) sMaxAge());
268 /* max-stale's value is optional.
269 If we didn't receive it, don't send it */
270 if (maxStale()!=MAX_STALE_ANY
)
271 packerPrintf(p
, "=%d", (int) maxStale());
274 packerPrintf(p
, "=%d", (int) minFresh());
277 /* do nothing, directive was already printed */
285 if (other
.size() != 0)
286 packerPrintf(p
, (pcount
? ", " SQUIDSTRINGPH
: SQUIDSTRINGPH
),
287 SQUIDSTRINGPRINT(other
));
291 httpHdrCcUpdateStats(const HttpHdrCc
* cc
, StatHist
* hist
)
296 for (c
= CC_PUBLIC
; c
< CC_ENUM_END
; ++c
)
302 httpHdrCcStatDumper(StoreEntry
* sentry
, int idx
, double val
, double size
, int count
)
304 extern const HttpHeaderStat
*dump_stat
; /* argh! */
305 const int id
= (int) val
;
306 const int valid_id
= id
>= 0 && id
< CC_ENUM_END
;
307 const char *name
= valid_id
? CcAttrs
[id
].name
: "INVALID";
309 if (count
|| valid_id
)
310 storeAppendPrintf(sentry
, "%2d\t %-20s\t %5d\t %6.2f\n",
311 id
, name
, count
, xdiv(count
, dump_stat
->ccParsedCount
));
315 #include "HttpHdrCc.cci"