]> git.ipfire.org Git - thirdparty/squid.git/blob - src/refresh.cc
Cleanup: replace urlCanonical() with HttpRequest::effectiveReuqestUri()
[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 time_t age = 0;
289 time_t check_time = squid_curtime + delta;
290 int staleness;
291 stale_flags sf;
292
293 // get the URL of this entry, if there is one
294 static const SBuf nilUri("<none>");
295 SBuf uri = nilUri;
296 if (entry->mem_obj)
297 uri = entry->mem_obj->storeId();
298 else if (request)
299 uri = request->effectiveRequestUri();
300
301 debugs(22, 3, "checking freshness of URI: " << uri);
302
303 // age is not necessarily the age now, but the age at the given check_time
304 if (check_time > entry->timestamp)
305 age = check_time - entry->timestamp;
306
307 // FIXME: what to do when age < 0 or counter overflow?
308 assert(age >= 0);
309
310 /* We need a refresh rule. In order of preference:
311 *
312 * 1. the rule that matches this URI by regex
313 * 2. the "." rule from the config file
314 * 3. the default "." rule
315 */
316 // XXX: performance regression. c_str() reallocates
317 const RefreshPattern *R = (uri != nilUri) ? refreshLimits(uri.c_str()) : refreshUncompiledPattern(".");
318 if (NULL == R)
319 R = &DefaultRefresh;
320
321 debugs(22, 3, "Matched '" << R->pattern << " " <<
322 (int) R->min << " " << (int) (100.0 * R->pct) << "%% " <<
323 (int) R->max << "'");
324
325 debugs(22, 3, "\tage:\t" << age);
326
327 debugs(22, 3, "\tcheck_time:\t" << mkrfc1123(check_time));
328
329 debugs(22, 3, "\tentry->timestamp:\t" << mkrfc1123(entry->timestamp));
330
331 if (request && !request->flags.ignoreCc) {
332 const HttpHdrCc *const cc = request->cache_control;
333 if (cc && cc->hasMinFresh()) {
334 const int32_t minFresh=cc->minFresh();
335 debugs(22, 3, "\tage + min-fresh:\t" << age << " + " <<
336 minFresh << " = " << age + minFresh);
337 debugs(22, 3, "\tcheck_time + min-fresh:\t" << check_time << " + "
338 << minFresh << " = " <<
339 mkrfc1123(check_time + minFresh));
340 age += minFresh;
341 check_time += minFresh;
342 }
343 }
344
345 memset(&sf, '\0', sizeof(sf));
346
347 staleness = refreshStaleness(entry, check_time, age, R, &sf);
348
349 debugs(22, 3, "Staleness = " << staleness);
350
351 // stale-if-error requires any failure be passed thru when its period is over.
352 if (request && entry->mem_obj && entry->mem_obj->getReply() && entry->mem_obj->getReply()->cache_control &&
353 entry->mem_obj->getReply()->cache_control->hasStaleIfError() &&
354 entry->mem_obj->getReply()->cache_control->staleIfError() < staleness) {
355
356 debugs(22, 3, "stale-if-error period expired. Will produce error if validation fails.");
357 request->flags.failOnValidationError = true;
358 }
359
360 /* If the origin server specified either of:
361 * Cache-Control: must-revalidate
362 * Cache-Control: proxy-revalidate
363 * the spec says the response must always be revalidated if stale.
364 */
365 if (EBIT_TEST(entry->flags, ENTRY_REVALIDATE) && staleness > -1) {
366 debugs(22, 3, "YES: Must revalidate stale object (origin set must-revalidate, proxy-revalidate, no-cache, s-maxage, or private)");
367 if (request)
368 request->flags.failOnValidationError = true;
369 return STALE_MUST_REVALIDATE;
370 }
371
372 /* request-specific checks */
373 if (request && !request->flags.ignoreCc) {
374 HttpHdrCc *cc = request->cache_control;
375
376 /* If the request is an IMS request, and squid is configured NOT to service this from cache
377 * (either by 'refresh-ims' in the refresh pattern or 'refresh_all_ims on' globally)
378 * then force a reload from the origin.
379 */
380 if (request->flags.ims && (R->flags.refresh_ims || Config.onoff.refresh_all_ims)) {
381 // The client's no-cache header is changed into a IMS query
382 debugs(22, 3, "YES: Client IMS request forcing revalidation of object (refresh-ims option)");
383 return STALE_FORCED_RELOAD;
384 }
385
386 #if USE_HTTP_VIOLATIONS
387 /* Normally a client reload request ("Cache-Control: no-cache" or "Pragma: no-cache")
388 * means we must treat this reponse as STALE and fetch a new one.
389 *
390 * However, some options exist to override this behaviour. For example, we might just
391 * revalidate our existing response, or even just serve it up without revalidating it.
392 *
393 * ---- Note on the meaning of nocache_hack -----
394 *
395 * The nocache_hack flag has a very specific and complex meaning:
396 *
397 * (a) this is a reload request ("Cache-Control: no-cache" or "Pragma: no-cache" header)
398 * and (b) the configuration file either has at least one refresh_pattern with
399 * ignore-reload or reload-into-ims (not necessarily the rule matching this request) or
400 * the global reload_into_ims is set to on
401 *
402 * In other words: this is a client reload, and we might need to override
403 * the default behaviour (but we might not).
404 *
405 * "nocache_hack" is a pretty deceptive name for such a complicated meaning.
406 */
407 if (request->flags.noCacheHack()) {
408
409 if (R->flags.ignore_reload) {
410 /* The client's no-cache header is ignored completely - we'll try to serve
411 * what we have (assuming it's still fresh, etc.)
412 */
413 debugs(22, 3, "MAYBE: Ignoring client reload request - trying to serve from cache (ignore-reload option)");
414 } else if (R->flags.reload_into_ims || Config.onoff.reload_into_ims) {
415 /* The client's no-cache header is not honoured completely - we'll just try
416 * to revalidate our cached copy (IMS to origin) instead of fetching a new
417 * copy with an unconditional GET.
418 */
419 debugs(22, 3, "YES: Client reload request - cheating, only revalidating with origin (reload-into-ims option)");
420 return STALE_RELOAD_INTO_IMS;
421 } else {
422 /* The client's no-cache header is honoured - we fetch a new copy from origin */
423 debugs(22, 3, "YES: Client reload request - fetching new copy from origin");
424 request->flags.noCache = true;
425 return STALE_FORCED_RELOAD;
426 }
427 }
428 #endif
429
430 // Check the Cache-Control client request header
431 if (NULL != cc) {
432
433 // max-age directive
434 if (cc->hasMaxAge()) {
435 #if USE_HTTP_VIOLATIONS
436 // Ignore client "Cache-Control: max-age=0" header
437 if (R->flags.ignore_reload && cc->maxAge() == 0) {
438 debugs(22, 3, "MAYBE: Ignoring client reload request - trying to serve from cache (ignore-reload option)");
439 } else
440 #endif
441 {
442 // Honour client "Cache-Control: max-age=x" header
443 if (age > cc->maxAge() || cc->maxAge() == 0) {
444 debugs(22, 3, "YES: Revalidating object - client 'Cache-Control: max-age=" << cc->maxAge() << "'");
445 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE;
446 }
447 }
448 }
449
450 // max-stale directive
451 if (cc->hasMaxStale() && staleness > -1) {
452 if (cc->maxStale()==HttpHdrCc::MAX_STALE_ANY) {
453 debugs(22, 3, "NO: Client accepts a stale response of any age - 'Cache-Control: max-stale'");
454 return FRESH_REQUEST_MAX_STALE_ALL;
455 } else if (staleness < cc->maxStale()) {
456 debugs(22, 3, "NO: Client accepts a stale response - 'Cache-Control: max-stale=" << cc->maxStale() << "'");
457 return FRESH_REQUEST_MAX_STALE_VALUE;
458 }
459 }
460 }
461 }
462
463 // If the object is fresh, return the right FRESH_ code
464 if (-1 == staleness) {
465 debugs(22, 3, "Object isn't stale..");
466 if (sf.expires) {
467 debugs(22, 3, "returning FRESH_EXPIRES");
468 return FRESH_EXPIRES;
469 }
470
471 assert(!sf.max);
472
473 if (sf.lmfactor) {
474 debugs(22, 3, "returning FRESH_LMFACTOR_RULE");
475 return FRESH_LMFACTOR_RULE;
476 }
477
478 assert(sf.min);
479
480 debugs(22, 3, "returning FRESH_MIN_RULE");
481 return FRESH_MIN_RULE;
482 }
483
484 /*
485 * At this point the response is stale, unless one of
486 * the override options kicks in.
487 * NOTE: max-stale config blocks the overrides.
488 */
489 int max_stale = (R->max_stale >= 0 ? R->max_stale : Config.maxStale);
490 if ( max_stale >= 0 && staleness > max_stale) {
491 debugs(22, 3, "YES: refresh_pattern max-stale=N limit from squid.conf");
492 if (request)
493 request->flags.failOnValidationError = true;
494 return STALE_MAX_STALE;
495 }
496
497 if (sf.expires) {
498 #if USE_HTTP_VIOLATIONS
499
500 if (R->flags.override_expire && age < R->min) {
501 debugs(22, 3, "NO: Serving from cache - even though explicit expiry has passed, we enforce Min value (override-expire option)");
502 return FRESH_OVERRIDE_EXPIRES;
503 }
504
505 #endif
506 return STALE_EXPIRES;
507 }
508
509 if (sf.max)
510 return STALE_MAX_RULE;
511
512 if (sf.lmfactor) {
513 #if USE_HTTP_VIOLATIONS
514 if (R->flags.override_lastmod && age < R->min) {
515 debugs(22, 3, "NO: Serving from cache - even though L-M factor says the object is stale, we enforce Min value (override-lastmod option)");
516 return FRESH_OVERRIDE_LASTMOD;
517 }
518 #endif
519 debugs(22, 3, "YES: L-M factor says the object is stale'");
520 return STALE_LMFACTOR_RULE;
521 }
522
523 debugs(22, 3, "returning STALE_DEFAULT");
524 return STALE_DEFAULT;
525 }
526
527 /**
528 * This is called by http.cc once it has received and parsed the origin server's
529 * response headers. It uses the result as part of its algorithm to decide whether a
530 * response should be cached.
531 *
532 * \retval true if the entry is cacheable, regardless of whether FRESH or STALE
533 * \retval false if the entry is not cacheable
534 *
535 * TODO: this algorithm seems a bit odd and might not be quite right. Verify against HTTPbis.
536 */
537 bool
538 refreshIsCachable(const StoreEntry * entry)
539 {
540 /*
541 * Don't look at the request to avoid no-cache and other nuisances.
542 * the object should have a mem_obj so the URL will be found there.
543 * minimum_expiry_time seconds delta (defaults to 60 seconds), to
544 * avoid objects which expire almost immediately, and which can't
545 * be refreshed.
546 */
547 int reason = refreshCheck(entry, NULL, Config.minimum_expiry_time);
548 ++ refreshCounts[rcStore].total;
549 ++ refreshCounts[rcStore].status[reason];
550
551 if (reason < STALE_MUST_REVALIDATE)
552 /* Does not need refresh. This is certainly cachable */
553 return true;
554
555 if (entry->lastmod < 0)
556 /* Last modified is needed to do a refresh */
557 return false;
558
559 if (entry->mem_obj == NULL)
560 /* no mem_obj? */
561 return true;
562
563 if (entry->getReply() == NULL)
564 /* no reply? */
565 return true;
566
567 if (entry->getReply()->content_length == 0)
568 /* No use refreshing (caching?) 0 byte objects */
569 return false;
570
571 /* This seems to be refreshable. Cache it */
572 return true;
573 }
574
575 /// whether reply is stale if it is a hit
576 static bool
577 refreshIsStaleIfHit(const int reason)
578 {
579 switch (reason) {
580 case FRESH_MIN_RULE:
581 case FRESH_LMFACTOR_RULE:
582 case FRESH_EXPIRES:
583 return false;
584 default:
585 return true;
586 }
587 }
588
589 /**
590 * Protocol-specific wrapper around refreshCheck() function.
591 *
592 * Note the reason for STALE/FRESH then return true/false respectively.
593 *
594 * \retval 1 if STALE
595 * \retval 0 if FRESH
596 */
597 int
598 refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
599 {
600 int reason = refreshCheck(entry, request, 0);
601 ++ refreshCounts[rcHTTP].total;
602 ++ refreshCounts[rcHTTP].status[reason];
603 request->flags.staleIfHit = refreshIsStaleIfHit(reason);
604 return (Config.onoff.offline || reason < 200) ? 0 : 1;
605 }
606
607 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
608 int
609 refreshCheckICP(const StoreEntry * entry, HttpRequest * request)
610 {
611 int reason = refreshCheck(entry, request, 30);
612 ++ refreshCounts[rcICP].total;
613 ++ refreshCounts[rcICP].status[reason];
614 return (reason < 200) ? 0 : 1;
615 }
616
617 #if USE_HTCP
618 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
619 int
620 refreshCheckHTCP(const StoreEntry * entry, HttpRequest * request)
621 {
622 int reason = refreshCheck(entry, request, 10);
623 ++ refreshCounts[rcHTCP].total;
624 ++ refreshCounts[rcHTCP].status[reason];
625 return (reason < 200) ? 0 : 1;
626 }
627
628 #endif
629
630 #if USE_CACHE_DIGESTS
631 /// \see int refreshCheckHTTP(const StoreEntry * entry, HttpRequest * request)
632 int
633 refreshCheckDigest(const StoreEntry * entry, time_t delta)
634 {
635 int reason = refreshCheck(entry,
636 entry->mem_obj ? entry->mem_obj->request : NULL,
637 delta);
638 ++ refreshCounts[rcCDigest].total;
639 ++ refreshCounts[rcCDigest].status[reason];
640 return (reason < 200) ? 0 : 1;
641 }
642 #endif
643
644 /**
645 * Get the configured maximum caching time for objects with this URL
646 * according to refresh_pattern.
647 *
648 * Used by http.cc when generating a upstream requests to ensure that
649 * responses it is given are fresh enough to be worth caching.
650 *
651 * \retval pattern-max if there is a refresh_pattern matching the URL configured.
652 * \retval REFRESH_DEFAULT_MAX if there are no explicit limits configured
653 */
654 time_t
655 getMaxAge(const char *url)
656 {
657 const RefreshPattern *R;
658 debugs(22, 3, "getMaxAge: '" << url << "'");
659
660 if ((R = refreshLimits(url)))
661 return R->max;
662 else
663 return REFRESH_DEFAULT_MAX;
664 }
665
666 static int
667 refreshCountsStatsEntry(StoreEntry * sentry, struct RefreshCounts &rc, int code, const char *desc)
668 {
669 storeAppendPrintf(sentry, "%6d\t%6.2f\t%s\n", rc.status[code], xpercent(rc.status[code], rc.total), desc);
670 return rc.status[code];
671 }
672
673 static void
674 refreshCountsStats(StoreEntry * sentry, struct RefreshCounts &rc)
675 {
676 if (!rc.total)
677 return;
678
679 storeAppendPrintf(sentry, "\n\n%s histogram:\n", rc.proto);
680 storeAppendPrintf(sentry, "Count\t%%Total\tCategory\n");
681
682 int sum = 0;
683 sum += refreshCountsStatsEntry(sentry, rc, FRESH_REQUEST_MAX_STALE_ALL, "Fresh: request max-stale wildcard");
684 sum += refreshCountsStatsEntry(sentry, rc, FRESH_REQUEST_MAX_STALE_VALUE, "Fresh: request max-stale value");
685 sum += refreshCountsStatsEntry(sentry, rc, FRESH_EXPIRES, "Fresh: expires time not reached");
686 sum += refreshCountsStatsEntry(sentry, rc, FRESH_LMFACTOR_RULE, "Fresh: refresh_pattern last-mod factor percentage");
687 sum += refreshCountsStatsEntry(sentry, rc, FRESH_MIN_RULE, "Fresh: refresh_pattern min value");
688 sum += refreshCountsStatsEntry(sentry, rc, FRESH_OVERRIDE_EXPIRES, "Fresh: refresh_pattern override-expires");
689 sum += refreshCountsStatsEntry(sentry, rc, FRESH_OVERRIDE_LASTMOD, "Fresh: refresh_pattern override-lastmod");
690 sum += refreshCountsStatsEntry(sentry, rc, STALE_MUST_REVALIDATE, "Stale: response has must-revalidate");
691 sum += refreshCountsStatsEntry(sentry, rc, STALE_RELOAD_INTO_IMS, "Stale: changed reload into IMS");
692 sum += refreshCountsStatsEntry(sentry, rc, STALE_FORCED_RELOAD, "Stale: request has no-cache directive");
693 sum += refreshCountsStatsEntry(sentry, rc, STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE, "Stale: age exceeds request max-age value");
694 sum += refreshCountsStatsEntry(sentry, rc, STALE_EXPIRES, "Stale: expires time reached");
695 sum += refreshCountsStatsEntry(sentry, rc, STALE_MAX_RULE, "Stale: refresh_pattern max age rule");
696 sum += refreshCountsStatsEntry(sentry, rc, STALE_LMFACTOR_RULE, "Stale: refresh_pattern last-mod factor percentage");
697 sum += refreshCountsStatsEntry(sentry, rc, STALE_DEFAULT, "Stale: by default");
698 storeAppendPrintf(sentry, "\n");
699 }
700
701 static void
702 refreshStats(StoreEntry * sentry)
703 {
704 // display per-rule counts of usage and tests
705 storeAppendPrintf(sentry, "\nRefresh pattern usage:\n\n");
706 storeAppendPrintf(sentry, " Used \tChecks \t%% Matches\tPattern\n");
707 for (const RefreshPattern *R = Config.Refresh; R; R = R->next) {
708 storeAppendPrintf(sentry, " %10" PRIu64 "\t%10" PRIu64 "\t%6.2f\t%s%s\n",
709 R->stats.matchCount,
710 R->stats.matchTests,
711 xpercent(R->stats.matchCount, R->stats.matchTests),
712 (R->flags.icase ? "-i " : ""),
713 R->pattern);
714 }
715
716 int i;
717 int total = 0;
718
719 /* get total usage count */
720
721 for (i = 0; i < rcCount; ++i)
722 total += refreshCounts[i].total;
723
724 /* protocol usage histogram */
725 storeAppendPrintf(sentry, "\nRefreshCheck calls per protocol\n\n");
726
727 storeAppendPrintf(sentry, "Protocol\t#Calls\t%%Calls\n");
728
729 for (i = 0; i < rcCount; ++i)
730 storeAppendPrintf(sentry, "%10s\t%6d\t%6.2f\n",
731 refreshCounts[i].proto,
732 refreshCounts[i].total,
733 xpercent(refreshCounts[i].total, total));
734
735 /* per protocol histograms */
736 storeAppendPrintf(sentry, "\n\nRefreshCheck histograms for various protocols\n");
737
738 for (i = 0; i < rcCount; ++i)
739 refreshCountsStats(sentry, refreshCounts[i]);
740 }
741
742 static void
743 refreshRegisterWithCacheManager(void)
744 {
745 Mgr::RegisterAction("refresh", "Refresh Algorithm Statistics", refreshStats, 0, 1);
746 }
747
748 void
749 refreshInit(void)
750 {
751 memset(refreshCounts, 0, sizeof(refreshCounts));
752 refreshCounts[rcHTTP].proto = "HTTP";
753 refreshCounts[rcICP].proto = "ICP";
754 #if USE_HTCP
755
756 refreshCounts[rcHTCP].proto = "HTCP";
757 #endif
758
759 refreshCounts[rcStore].proto = "On Store";
760 #if USE_CACHE_DIGESTS
761
762 refreshCounts[rcCDigest].proto = "Cache Digests";
763 #endif
764
765 memset(&DefaultRefresh, '\0', sizeof(DefaultRefresh));
766 DefaultRefresh.pattern = "<none>";
767 DefaultRefresh.min = REFRESH_DEFAULT_MIN;
768 DefaultRefresh.pct = REFRESH_DEFAULT_PCT;
769 DefaultRefresh.max = REFRESH_DEFAULT_MAX;
770
771 refreshRegisterWithCacheManager();
772 }
773