]> git.ipfire.org Git - thirdparty/squid.git/blob - src/refresh.cc
Bug 4269: ignore-must-revalidate broken
[thirdparty/squid.git] / src / refresh.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 /* DEBUG: section 22 Refresh Calculation */
10
11 #ifndef USE_POSIX_REGEX
12 #define USE_POSIX_REGEX /* put before includes; always use POSIX */
13 #endif
14
15 #include "squid.h"
16 #include "HttpHdrCc.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
19 #include "MemObject.h"
20 #include "mgr/Registration.h"
21 #include "RefreshPattern.h"
22 #include "SquidConfig.h"
23 #include "SquidTime.h"
24 #include "Store.h"
25 #include "URL.h"
26 #include "util.h"
27
28 typedef enum {
29 rcHTTP,
30 rcICP,
31 #if USE_HTCP
32 rcHTCP,
33 #endif
34 #if USE_CACHE_DIGESTS
35 rcCDigest,
36 #endif
37 rcStore,
38 rcCount
39 } refreshCountsEnum;
40
41 /**
42 * Flags indicating which staleness algorithm has been applied.
43 */
44 typedef struct {
45 bool expires; ///< Expires: header absolute timestamp limit
46 bool min; ///< Heuristic minimum age limited
47 bool lmfactor; ///< Last-Modified with heuristic determines limit
48 bool max; ///< Configured maximum age limit
49 } stale_flags;
50
51 /*
52 * This enumerated list assigns specific values, ala HTTP/FTP status
53 * codes. All Fresh codes are in the range 100-199 and all stale
54 * codes are 200-299. We might want to use these codes in logging,
55 * so best to keep them consistent over time.
56 */
57 enum {
58 FRESH_REQUEST_MAX_STALE_ALL = 100,
59 FRESH_REQUEST_MAX_STALE_VALUE,
60 FRESH_EXPIRES,
61 FRESH_LMFACTOR_RULE,
62 FRESH_MIN_RULE,
63 FRESH_OVERRIDE_EXPIRES,
64 FRESH_OVERRIDE_LASTMOD,
65 STALE_MUST_REVALIDATE = 200,
66 STALE_RELOAD_INTO_IMS,
67 STALE_FORCED_RELOAD,
68 STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE,
69 STALE_EXPIRES,
70 STALE_MAX_RULE,
71 STALE_LMFACTOR_RULE,
72 STALE_MAX_STALE,
73 STALE_DEFAULT = 299
74 };
75
76 static struct RefreshCounts {
77 const char *proto;
78 int total;
79 int status[STALE_DEFAULT + 1];
80 } refreshCounts[rcCount];
81
82 /*
83 * Defaults:
84 * MIN NONE
85 * PCT 20%
86 * MAX 3 days
87 */
88 #define REFRESH_DEFAULT_MIN (time_t)0
89 #define REFRESH_DEFAULT_PCT 0.20
90 #define REFRESH_DEFAULT_MAX (time_t)259200
91
92 static const RefreshPattern *refreshUncompiledPattern(const char *);
93 static OBJH refreshStats;
94 static int refreshStaleness(const StoreEntry * entry, time_t check_time, const time_t age, const RefreshPattern * R, stale_flags * sf);
95
96 static RefreshPattern DefaultRefresh;
97
98 /** Locate the first refresh_pattern rule that matches the given URL by regex.
99 *
100 * \note regexec() returns 0 if matched, and REG_NOMATCH otherwise
101 *
102 * \return A pointer to the refresh_pattern parameters to use, or NULL if there is no match.
103 */
104 const RefreshPattern *
105 refreshLimits(const char *url)
106 {
107 const RefreshPattern *R;
108
109 for (R = Config.Refresh; R; R = R->next) {
110 ++(R->stats.matchTests);
111 if (!regexec(&(R->compiled_pattern), url, 0, 0, 0)) {
112 ++(R->stats.matchCount);
113 return R;
114 }
115 }
116
117 return NULL;
118 }
119
120 /** Locate the first refresh_pattern rule that has the given uncompiled regex.
121 *
122 * \note There is only one reference to this function, below. It always passes "." as the pattern.
123 * This function is only ever called if there is no URI. Because a regex match is impossible, Squid
124 * forces the "." rule to apply (if it exists)
125 *
126 * \return A pointer to the refresh_pattern parameters to use, or NULL if there is no match.
127 */
128 static const RefreshPattern *
129 refreshUncompiledPattern(const char *pat)
130 {
131 const RefreshPattern *R;
132
133 for (R = Config.Refresh; R; R = R->next) {
134 if (0 == strcmp(R->pattern, pat))
135 return R;
136 }
137
138 return NULL;
139 }
140
141 /**
142 * Calculate how stale the response is (or will be at the check_time).
143 *
144 * We try the following ways until one gives a result:
145 *
146 * 1. response expiration time, if one was set
147 * 2. age greater than configured maximum
148 * 3. last-modified factor algorithm
149 * 4. age less than configured minimum
150 * 5. default (stale)
151 *
152 * \param entry the StoreEntry being examined
153 * \param check_time the time (maybe future) at which we want to know whether $
154 * \param age the age of the entry at check_time
155 * \param R the refresh_pattern rule that matched this entry
156 * \param sf small struct to indicate reason for stale/fresh decision
157 *
158 * \retval -1 If the response is fresh.
159 * \retval >0 The amount of staleness.
160 * \retval 0 NOTE return value of 0 means the response is stale.
161 */
162 static int
163 refreshStaleness(const StoreEntry * entry, time_t check_time, const time_t age, const RefreshPattern * R, stale_flags * sf)
164 {
165 // 1. If the cached object has an explicit expiration time, then we rely on this and
166 // completely ignore the Min, Percent and Max values in the refresh_pattern.
167 if (entry->expires > -1) {
168 sf->expires = true;
169
170 if (entry->expires > check_time) {
171 debugs(22, 3, "FRESH: expires " << entry->expires <<
172 " >= check_time " << check_time << " ");
173
174 return -1;
175 } else {
176 debugs(22, 3, "STALE: expires " << entry->expires <<
177 " < check_time " << check_time << " ");
178
179 return (check_time - entry->expires);
180 }
181 }
182
183 debugs(22, 3, "No explicit expiry given, using heuristics to determine freshness");
184
185 // 2. If the entry is older than the maximum age in the refresh_pattern, it is STALE.
186 if (age > R->max) {
187 debugs(22, 3, "STALE: age " << age << " > max " << R->max << " ");
188 sf->max = true;
189 return (age - R->max);
190 }
191
192 // 3. If there is a Last-Modified header, try the last-modified factor algorithm.
193 if (entry->lastmod > -1 && entry->timestamp > entry->lastmod) {
194
195 /* lastmod_delta is the difference between the last-modified date of the response
196 * and the time we cached it. It's how "old" the response was when we got it.
197 */
198 time_t lastmod_delta = entry->timestamp - entry->lastmod;
199
200 /* stale_age is the age of the response when it became/becomes stale according to
201 * the last-modified factor algorithm. It's how long we can consider the response
202 * fresh from the time we cached it.
203 */
204 time_t stale_age = static_cast<time_t>(lastmod_delta * R->pct);
205
206 debugs(22,3, "Last modified " << lastmod_delta << " sec before we cached it, L-M factor " <<
207 (100.0 * R->pct) << "% = " << stale_age << " sec freshness lifetime");
208 sf->lmfactor = true;
209
210 if (age >= stale_age) {
211 debugs(22, 3, "STALE: age " << age << " > stale_age " << stale_age);
212 return (age - stale_age);
213 } else {
214 debugs(22, 3, "FRESH: age " << age << " <= stale_age " << stale_age);
215 return -1;
216 }
217 }
218
219 // 4. If the entry is not as old as the minimum age in the refresh_pattern, it is FRESH.
220 if (age < R->min) {
221 debugs(22, 3, "FRESH: age (" << age << " sec) is less than configured minimum (" << R->min << " sec)");
222 sf->min = true;
223 return -1;
224 }
225
226 // 5. default is stale, by the amount we missed the minimum by
227 debugs(22, 3, "STALE: No explicit expiry, no last modified, and older than configured minimum.");
228 return (age - R->min);
229 }
230
231 /** Checks whether a store entry is fresh or stale, and why.
232 *
233 * This is where all aspects of request, response and squid configuration
234 * meet to decide whether a response is cacheable or not:
235 *
236 * 1. Client request headers that affect cacheability, e.g.
237 * - Cache-Control: no-cache
238 * - Cache-Control: max-age=N
239 * - Cache-Control: max-stale[=N]
240 * - Pragma: no-cache
241 *
242 * 2. Server response headers that affect cacheability, e.g.
243 * - Age:
244 * - Cache-Control: proxy-revalidate
245 * - Cache-Control: must-revalidate
246 * - Cache-Control: no-cache
247 * - Cache-Control: max-age=N
248 * - Cache-Control: s-maxage=N
249 * - Date:
250 * - Expires:
251 * - Last-Modified:
252 *
253 * 3. Configuration options, e.g.
254 * - reload-into-ims (refresh_pattern)
255 * - ignore-reload (refresh_pattern)
256 * - refresh-ims (refresh_pattern)
257 * - override-lastmod (refresh_pattern)
258 * - override-expire (refresh_pattern)
259 * - reload_into_ims (global option)
260 * - refresh_all_ims (global option)
261 *
262 * \returns a status code (from enum above):
263 * - FRESH_REQUEST_MAX_STALE_ALL
264 * - FRESH_REQUEST_MAX_STALE_VALUE
265 * - FRESH_EXPIRES
266 * - FRESH_LMFACTOR_RULE
267 * - FRESH_MIN_RULE
268 * - FRESH_OVERRIDE_EXPIRES
269 * - FRESH_OVERRIDE_LASTMOD
270 * - STALE_MUST_REVALIDATE
271 * - STALE_RELOAD_INTO_IMS
272 * - STALE_FORCED_RELOAD
273 * - STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
274 * - STALE_EXPIRES
275 * - STALE_MAX_RULE
276 * - STALE_LMFACTOR_RULE
277 * - STALE_MAX_STALE
278 * - STALE_DEFAULT
279 *
280 * \note request may be NULL (e.g. for cache digests build)
281 *
282 * \note the store entry being examined is not necessarily cached (e.g. if
283 * this response is being evaluated for the first time)
284 */
285 static int
286 refreshCheck(const StoreEntry * entry, HttpRequest * request, time_t delta)
287 {
288 const char *uri = NULL;
289 time_t age = 0;
290 time_t check_time = squid_curtime + delta;
291 int staleness;
292 stale_flags sf;
293
294 // get the URL of this entry, if there is one
295 if (entry->mem_obj)
296 uri = entry->mem_obj->storeId();
297 else if (request)
298 uri = urlCanonical(request);
299
300 debugs(22, 3, "checking freshness of '" << (uri ? uri : "<none>") << "'");
301
302 // age is not necessarily the age now, but the age at the given check_time
303 if (check_time > entry->timestamp)
304 age = check_time - entry->timestamp;
305
306 // FIXME: what to do when age < 0 or counter overflow?
307 assert(age >= 0);
308
309 /* We need a refresh rule. In order of preference:
310 *
311 * 1. the rule that matches this URI by regex
312 * 2. the "." rule from the config file
313 * 3. the default "." rule
314 */
315 const RefreshPattern *R = uri ? refreshLimits(uri) : refreshUncompiledPattern(".");
316 if (NULL == R)
317 R = &DefaultRefresh;
318
319 debugs(22, 3, "Matched '" << R->pattern << " " <<
320 (int) R->min << " " << (int) (100.0 * R->pct) << "%% " <<
321 (int) R->max << "'");
322
323 debugs(22, 3, "\tage:\t" << age);
324
325 debugs(22, 3, "\tcheck_time:\t" << mkrfc1123(check_time));
326
327 debugs(22, 3, "\tentry->timestamp:\t" << mkrfc1123(entry->timestamp));
328
329 if (request && !request->flags.ignoreCc) {
330 const HttpHdrCc *const cc = request->cache_control;
331 if (cc && cc->hasMinFresh()) {
332 const int32_t minFresh=cc->minFresh();
333 debugs(22, 3, "\tage + min-fresh:\t" << age << " + " <<
334 minFresh << " = " << age + minFresh);
335 debugs(22, 3, "\tcheck_time + min-fresh:\t" << check_time << " + "
336 << minFresh << " = " <<
337 mkrfc1123(check_time + minFresh));
338 age += minFresh;
339 check_time += minFresh;
340 }
341 }
342
343 memset(&sf, '\0', sizeof(sf));
344
345 staleness = refreshStaleness(entry, check_time, age, R, &sf);
346
347 debugs(22, 3, "Staleness = " << staleness);
348
349 // stale-if-error requires any failure be passed thru when its period is over.
350 if (request && entry->mem_obj && entry->mem_obj->getReply() && entry->mem_obj->getReply()->cache_control &&
351 entry->mem_obj->getReply()->cache_control->hasStaleIfError() &&
352 entry->mem_obj->getReply()->cache_control->staleIfError() < staleness) {
353
354 debugs(22, 3, "stale-if-error period expired. Will produce error if validation fails.");
355 request->flags.failOnValidationError = true;
356 }
357
358 /* If the origin server specified either of:
359 * Cache-Control: must-revalidate
360 * Cache-Control: proxy-revalidate
361 * the spec says the response must always be revalidated if stale.
362 */
363 if (EBIT_TEST(entry->flags, ENTRY_REVALIDATE) && staleness > -1) {
364 debugs(22, 3, "YES: Must revalidate stale object (origin set must-revalidate, proxy-revalidate, no-cache, s-maxage, or private)");
365 if (request)
366 request->flags.failOnValidationError = true;
367 return STALE_MUST_REVALIDATE;
368 }
369
370 /* request-specific checks */
371 if (request && !request->flags.ignoreCc) {
372 HttpHdrCc *cc = request->cache_control;
373
374 /* If the request is an IMS request, and squid is configured NOT to service this from cache
375 * (either by 'refresh-ims' in the refresh pattern or 'refresh_all_ims on' globally)
376 * then force a reload from the origin.
377 */
378 if (request->flags.ims && (R->flags.refresh_ims || Config.onoff.refresh_all_ims)) {
379 // The client's no-cache header is changed into a IMS query
380 debugs(22, 3, "YES: Client IMS request forcing revalidation of object (refresh-ims option)");
381 return STALE_FORCED_RELOAD;
382 }
383
384 #if USE_HTTP_VIOLATIONS
385 /* Normally a client reload request ("Cache-Control: no-cache" or "Pragma: no-cache")
386 * means we must treat this reponse as STALE and fetch a new one.
387 *
388 * However, some options exist to override this behaviour. For example, we might just
389 * revalidate our existing response, or even just serve it up without revalidating it.
390 *
391 * ---- Note on the meaning of nocache_hack -----
392 *
393 * The nocache_hack flag has a very specific and complex meaning:
394 *
395 * (a) this is a reload request ("Cache-Control: no-cache" or "Pragma: no-cache" header)
396 * and (b) the configuration file either has at least one refresh_pattern with
397 * ignore-reload or reload-into-ims (not necessarily the rule matching this request) or
398 * the global reload_into_ims is set to on
399 *
400 * In other words: this is a client reload, and we might need to override
401 * the default behaviour (but we might not).
402 *
403 * "nocache_hack" is a pretty deceptive name for such a complicated meaning.
404 */
405 if (request->flags.noCacheHack()) {
406
407 if (R->flags.ignore_reload) {
408 /* The client's no-cache header is ignored completely - we'll try to serve
409 * what we have (assuming it's still fresh, etc.)
410 */
411 debugs(22, 3, "MAYBE: Ignoring client reload request - trying to serve from cache (ignore-reload option)");
412 } else if (R->flags.reload_into_ims || Config.onoff.reload_into_ims) {
413 /* The client's no-cache header is not honoured completely - we'll just try
414 * to revalidate our cached copy (IMS to origin) instead of fetching a new
415 * copy with an unconditional GET.
416 */
417 debugs(22, 3, "YES: Client reload request - cheating, only revalidating with origin (reload-into-ims option)");
418 return STALE_RELOAD_INTO_IMS;
419 } else {
420 /* The client's no-cache header is honoured - we fetch a new copy from origin */
421 debugs(22, 3, "YES: Client reload request - fetching new copy from origin");
422 request->flags.noCache = true;
423 return STALE_FORCED_RELOAD;
424 }
425 }
426 #endif
427
428 // Check the Cache-Control client request header
429 if (NULL != cc) {
430
431 // max-age directive
432 if (cc->hasMaxAge()) {
433 #if USE_HTTP_VIOLATIONS
434 // Ignore client "Cache-Control: max-age=0" header
435 if (R->flags.ignore_reload && cc->maxAge() == 0) {
436 debugs(22, 3, "MAYBE: Ignoring client reload request - trying to serve from cache (ignore-reload option)");
437 } else
438 #endif
439 {
440 // Honour client "Cache-Control: max-age=x" header
441 if (age > cc->maxAge() || cc->maxAge() == 0) {
442 debugs(22, 3, "YES: Revalidating object - client 'Cache-Control: max-age=" << cc->maxAge() << "'");
443 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE;
444 }
445 }
446 }
447
448 // max-stale directive
449 if (cc->hasMaxStale() && staleness > -1) {
450 if (cc->maxStale()==HttpHdrCc::MAX_STALE_ANY) {
451 debugs(22, 3, "NO: Client accepts a stale response of any age - 'Cache-Control: max-stale'");
452 return FRESH_REQUEST_MAX_STALE_ALL;
453 } else if (staleness < cc->maxStale()) {
454 debugs(22, 3, "NO: Client accepts a stale response - 'Cache-Control: max-stale=" << cc->maxStale() << "'");
455 return FRESH_REQUEST_MAX_STALE_VALUE;
456 }
457 }
458 }
459 }
460
461 // If the object is fresh, return the right FRESH_ code
462 if (-1 == staleness) {
463 debugs(22, 3, "Object isn't stale..");
464 if (sf.expires) {
465 debugs(22, 3, "returning FRESH_EXPIRES");
466 return FRESH_EXPIRES;
467 }
468
469 assert(!sf.max);
470
471 if (sf.lmfactor) {
472 debugs(22, 3, "returning FRESH_LMFACTOR_RULE");
473 return FRESH_LMFACTOR_RULE;
474 }
475
476 assert(sf.min);
477
478 debugs(22, 3, "returning FRESH_MIN_RULE");
479 return FRESH_MIN_RULE;
480 }
481
482 /*
483 * At this point the response is stale, unless one of
484 * the override options kicks in.
485 * NOTE: max-stale config blocks the overrides.
486 */
487 int max_stale = (R->max_stale >= 0 ? R->max_stale : Config.maxStale);
488 if ( max_stale >= 0 && staleness > max_stale) {
489 debugs(22, 3, "YES: refresh_pattern max-stale=N limit from squid.conf");
490 if (request)
491 request->flags.failOnValidationError = true;
492 return STALE_MAX_STALE;
493 }
494
495 if (sf.expires) {
496 #if USE_HTTP_VIOLATIONS
497
498 if (R->flags.override_expire && age < R->min) {
499 debugs(22, 3, "NO: Serving from cache - even though explicit expiry has passed, we enforce Min value (override-expire option)");
500 return FRESH_OVERRIDE_EXPIRES;
501 }
502
503 #endif
504 return STALE_EXPIRES;
505 }
506
507 if (sf.max)
508 return STALE_MAX_RULE;
509
510 if (sf.lmfactor) {
511 #if USE_HTTP_VIOLATIONS
512 if (R->flags.override_lastmod && age < R->min) {
513 debugs(22, 3, "NO: Serving from cache - even though L-M factor says the object is stale, we enforce Min value (override-lastmod option)");
514 return FRESH_OVERRIDE_LASTMOD;
515 }
516 #endif
517 debugs(22, 3, "YES: L-M factor says the object is stale'");
518 return STALE_LMFACTOR_RULE;
519 }
520
521 debugs(22, 3, "returning STALE_DEFAULT");
522 return STALE_DEFAULT;
523 }
524
525 /**
526 * This is called by http.cc once it has received and parsed the origin server's
527 * response headers. It uses the result as part of its algorithm to decide whether a
528 * response should be cached.
529 *
530 * \retval true if the entry is cacheable, regardless of whether FRESH or STALE
531 * \retval false if the entry is not cacheable
532 *
533 * TODO: this algorithm seems a bit odd and might not be quite right. Verify against HTTPbis.
534 */
535 bool
536 refreshIsCachable(const StoreEntry * entry)
537 {
538 /*
539 * Don't look at the request to avoid no-cache and other nuisances.
540 * the object should have a mem_obj so the URL will be found there.
541 * minimum_expiry_time seconds delta (defaults to 60 seconds), to
542 * avoid objects which expire almost immediately, and which can't
543 * be refreshed.
544 */
545 int reason = refreshCheck(entry, NULL, Config.minimum_expiry_time);
546 ++ refreshCounts[rcStore].total;
547 ++ refreshCounts[rcStore].status[reason];
548
549 if (reason < STALE_MUST_REVALIDATE)
550 /* Does not need refresh. This is certainly cachable */
551 return true;
552
553 if (entry->lastmod < 0)
554 /* Last modified is needed to do a refresh */
555 return false;
556
557 if (entry->mem_obj == NULL)
558 /* no mem_obj? */
559 return true;
560
561 if (entry->getReply() == NULL)
562 /* no reply? */
563 return true;
564
565 if (entry->getReply()->content_length == 0)
566 /* No use refreshing (caching?) 0 byte objects */
567 return false;
568
569 /* This seems to be refreshable. Cache it */
570 return true;
571 }
572
573 /// whether reply is stale if it is a hit
574 static bool
575 refreshIsStaleIfHit(const int reason)
576 {
577 switch (reason) {
578 case FRESH_MIN_RULE:
579 case FRESH_LMFACTOR_RULE:
580 case FRESH_EXPIRES:
581 return false;
582 default:
583 return true;
584 }
585 }
586
587 /**
588 * Protocol-specific wrapper around refreshCheck() function.
589 *
590 * Note the reason for STALE/FRESH then return true/false respectively.
591 *
592 * \retval 1 if STALE
593 * \retval 0 if FRESH
594 */
595 int
596 refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
597 {
598 int reason = refreshCheck(entry, request, 0);
599 ++ refreshCounts[rcHTTP].total;
600 ++ refreshCounts[rcHTTP].status[reason];
601 request->flags.staleIfHit = refreshIsStaleIfHit(reason);
602 return (Config.onoff.offline || reason < 200) ? 0 : 1;
603 }
604
605 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
606 int
607 refreshCheckICP(const StoreEntry * entry, HttpRequest * request)
608 {
609 int reason = refreshCheck(entry, request, 30);
610 ++ refreshCounts[rcICP].total;
611 ++ refreshCounts[rcICP].status[reason];
612 return (reason < 200) ? 0 : 1;
613 }
614
615 #if USE_HTCP
616 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
617 int
618 refreshCheckHTCP(const StoreEntry * entry, HttpRequest * request)
619 {
620 int reason = refreshCheck(entry, request, 10);
621 ++ refreshCounts[rcHTCP].total;
622 ++ refreshCounts[rcHTCP].status[reason];
623 return (reason < 200) ? 0 : 1;
624 }
625
626 #endif
627
628 #if USE_CACHE_DIGESTS
629 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
630 int
631 refreshCheckDigest(const StoreEntry * entry, time_t delta)
632 {
633 int reason = refreshCheck(entry,
634 entry->mem_obj ? entry->mem_obj->request : NULL,
635 delta);
636 ++ refreshCounts[rcCDigest].total;
637 ++ refreshCounts[rcCDigest].status[reason];
638 return (reason < 200) ? 0 : 1;
639 }
640 #endif
641
642 /**
643 * Get the configured maximum caching time for objects with this URL
644 * according to refresh_pattern.
645 *
646 * Used by http.cc when generating a upstream requests to ensure that
647 * responses it is given are fresh enough to be worth caching.
648 *
649 * \retval pattern-max if there is a refresh_pattern matching the URL configured.
650 * \retval REFRESH_DEFAULT_MAX if there are no explicit limits configured
651 */
652 time_t
653 getMaxAge(const char *url)
654 {
655 const RefreshPattern *R;
656 debugs(22, 3, "getMaxAge: '" << url << "'");
657
658 if ((R = refreshLimits(url)))
659 return R->max;
660 else
661 return REFRESH_DEFAULT_MAX;
662 }
663
664 static int
665 refreshCountsStatsEntry(StoreEntry * sentry, struct RefreshCounts &rc, int code, const char *desc)
666 {
667 storeAppendPrintf(sentry, "%6d\t%6.2f\t%s\n", rc.status[code], xpercent(rc.status[code], rc.total), desc);
668 return rc.status[code];
669 }
670
671 static void
672 refreshCountsStats(StoreEntry * sentry, struct RefreshCounts &rc)
673 {
674 if (!rc.total)
675 return;
676
677 storeAppendPrintf(sentry, "\n\n%s histogram:\n", rc.proto);
678 storeAppendPrintf(sentry, "Count\t%%Total\tCategory\n");
679
680 int sum = 0;
681 sum += refreshCountsStatsEntry(sentry, rc, FRESH_REQUEST_MAX_STALE_ALL, "Fresh: request max-stale wildcard");
682 sum += refreshCountsStatsEntry(sentry, rc, FRESH_REQUEST_MAX_STALE_VALUE, "Fresh: request max-stale value");
683 sum += refreshCountsStatsEntry(sentry, rc, FRESH_EXPIRES, "Fresh: expires time not reached");
684 sum += refreshCountsStatsEntry(sentry, rc, FRESH_LMFACTOR_RULE, "Fresh: refresh_pattern last-mod factor percentage");
685 sum += refreshCountsStatsEntry(sentry, rc, FRESH_MIN_RULE, "Fresh: refresh_pattern min value");
686 sum += refreshCountsStatsEntry(sentry, rc, FRESH_OVERRIDE_EXPIRES, "Fresh: refresh_pattern override-expires");
687 sum += refreshCountsStatsEntry(sentry, rc, FRESH_OVERRIDE_LASTMOD, "Fresh: refresh_pattern override-lastmod");
688 sum += refreshCountsStatsEntry(sentry, rc, STALE_MUST_REVALIDATE, "Stale: response has must-revalidate");
689 sum += refreshCountsStatsEntry(sentry, rc, STALE_RELOAD_INTO_IMS, "Stale: changed reload into IMS");
690 sum += refreshCountsStatsEntry(sentry, rc, STALE_FORCED_RELOAD, "Stale: request has no-cache directive");
691 sum += refreshCountsStatsEntry(sentry, rc, STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE, "Stale: age exceeds request max-age value");
692 sum += refreshCountsStatsEntry(sentry, rc, STALE_EXPIRES, "Stale: expires time reached");
693 sum += refreshCountsStatsEntry(sentry, rc, STALE_MAX_RULE, "Stale: refresh_pattern max age rule");
694 sum += refreshCountsStatsEntry(sentry, rc, STALE_LMFACTOR_RULE, "Stale: refresh_pattern last-mod factor percentage");
695 sum += refreshCountsStatsEntry(sentry, rc, STALE_DEFAULT, "Stale: by default");
696 storeAppendPrintf(sentry, "\n");
697 }
698
699 static void
700 refreshStats(StoreEntry * sentry)
701 {
702 // display per-rule counts of usage and tests
703 storeAppendPrintf(sentry, "\nRefresh pattern usage:\n\n");
704 storeAppendPrintf(sentry, " Used \tChecks \t%% Matches\tPattern\n");
705 for (const RefreshPattern *R = Config.Refresh; R; R = R->next) {
706 storeAppendPrintf(sentry, " %10" PRIu64 "\t%10" PRIu64 "\t%6.2f\t%s%s\n",
707 R->stats.matchCount,
708 R->stats.matchTests,
709 xpercent(R->stats.matchCount, R->stats.matchTests),
710 (R->flags.icase ? "-i " : ""),
711 R->pattern);
712 }
713
714 int i;
715 int total = 0;
716
717 /* get total usage count */
718
719 for (i = 0; i < rcCount; ++i)
720 total += refreshCounts[i].total;
721
722 /* protocol usage histogram */
723 storeAppendPrintf(sentry, "\nRefreshCheck calls per protocol\n\n");
724
725 storeAppendPrintf(sentry, "Protocol\t#Calls\t%%Calls\n");
726
727 for (i = 0; i < rcCount; ++i)
728 storeAppendPrintf(sentry, "%10s\t%6d\t%6.2f\n",
729 refreshCounts[i].proto,
730 refreshCounts[i].total,
731 xpercent(refreshCounts[i].total, total));
732
733 /* per protocol histograms */
734 storeAppendPrintf(sentry, "\n\nRefreshCheck histograms for various protocols\n");
735
736 for (i = 0; i < rcCount; ++i)
737 refreshCountsStats(sentry, refreshCounts[i]);
738 }
739
740 static void
741 refreshRegisterWithCacheManager(void)
742 {
743 Mgr::RegisterAction("refresh", "Refresh Algorithm Statistics", refreshStats, 0, 1);
744 }
745
746 void
747 refreshInit(void)
748 {
749 memset(refreshCounts, 0, sizeof(refreshCounts));
750 refreshCounts[rcHTTP].proto = "HTTP";
751 refreshCounts[rcICP].proto = "ICP";
752 #if USE_HTCP
753
754 refreshCounts[rcHTCP].proto = "HTCP";
755 #endif
756
757 refreshCounts[rcStore].proto = "On Store";
758 #if USE_CACHE_DIGESTS
759
760 refreshCounts[rcCDigest].proto = "Cache Digests";
761 #endif
762
763 memset(&DefaultRefresh, '\0', sizeof(DefaultRefresh));
764 DefaultRefresh.pattern = "<none>";
765 DefaultRefresh.min = REFRESH_DEFAULT_MIN;
766 DefaultRefresh.pct = REFRESH_DEFAULT_PCT;
767 DefaultRefresh.max = REFRESH_DEFAULT_MAX;
768
769 refreshRegisterWithCacheManager();
770 }
771