]> git.ipfire.org Git - thirdparty/squid.git/blob - src/refresh.cc
adding
[thirdparty/squid.git] / src / refresh.cc
1
2 /*
3 * $Id: refresh.cc,v 1.1 1996/10/27 08:40:23 wessels Exp $
4 *
5 * DEBUG: section 22 Refresh Calculation
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Internet Object Cache http://www.nlanr.net/Squid/
9 * --------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from the
12 * Internet community. Development is led by Duane Wessels of the
13 * National Laboratory for Applied Network Research and funded by
14 * the National Science Foundation.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 */
31
32 #ifndef USE_POSIX_REGEX
33 #define USE_POSIX_REGEX /* put before includes; always use POSIX */
34 #endif
35
36 #include "squid.h"
37
38 /*
39 * Defaults:
40 * MIN NONE
41 * PCT 20%
42 * MAX 3 days
43 */
44 #define REFRESH_DEFAULT_MIN 0
45 #define REFRESH_DEFAULT_PCT 20
46 #define REFRESH_DEFAULT_MAX 259200
47
48 typedef struct _refresh_t {
49 char *pattern;
50 regex_t compiled_pattern;
51 time_t min;
52 int pct;
53 time_t max;
54 struct _refresh_t *next;
55 } refresh_t;
56
57 static refresh_t *Refresh_tbl = NULL;
58 static refresh_t *Refresh_tail = NULL;
59
60 static void
61 refreshFreeList(refresh_t * t)
62 {
63 refresh_t *tnext;
64
65 for (; t; t = tnext) {
66 tnext = t->next;
67 safe_free(t->pattern);
68 regfree(&t->compiled_pattern);
69 safe_free(t);
70 }
71 }
72
73 void
74 refreshFreeMemory(void)
75 {
76 refreshFreeList(Refresh_tbl);
77 Refresh_tail = Refresh_tbl = NULL;
78 }
79
80 void
81 refreshAddToList(char *pattern, int opts, time_t min, int pct, time_t max)
82 {
83 refresh_t *t;
84 regex_t comp;
85 int flags = REG_EXTENDED;
86 if (opts & REFRESH_ICASE)
87 flags |= REG_ICASE;
88 if (regcomp(&comp, pattern, flags) != REG_NOERROR) {
89 debug(22, 0, "refreshAddToList: Invalid regular expression: %s\n",
90 pattern);
91 return;
92 }
93 pct = pct < 0 ? 0 : pct;
94 max = max < 0 ? 0 : max;
95 t = xcalloc(1, sizeof(refresh_t));
96 t->pattern = (char *) xstrdup(pattern);
97 t->compiled_pattern = comp;
98 t->min = min;
99 t->pct = pct;
100 t->max = max;
101 t->next = NULL;
102 if (!Refresh_tbl)
103 Refresh_tbl = t;
104 if (Refresh_tail)
105 Refresh_tail->next = t;
106 Refresh_tail = t;
107 }
108
109 /*
110 * refreshCheck():
111 * return 1 if its time to revalidate this entry, 0 otherwise
112 */
113 int
114 refreshCheck(StoreEntry * entry, request_t * request_unused)
115 {
116 refresh_t *R;
117 time_t min = REFRESH_DEFAULT_MIN;
118 int pct = REFRESH_DEFAULT_PCT;
119 time_t max = REFRESH_DEFAULT_MAX;
120 char *pattern = ".";
121 time_t age;
122 int factor;
123 debug(22,1,"refreshCheck: '%s'\n", entry->url);
124 for (R = Refresh_tbl; R; R = R->next) {
125 if (regexec(&(R->compiled_pattern), entry->url, 0, 0, 0) != 0)
126 continue;
127 min = R->min;
128 pct = R->pct;
129 max = R->max;
130 pattern = R->pattern;
131 break;
132 }
133 debug(22, 1, "refreshCheck: Matched '%s %d %d%% %d'\n",
134 pattern, (int) min, pct, (int) max);
135 age = squid_curtime - entry->timestamp;
136 debug(22,1,"refreshCheck: age = %d\n", (int) age);
137 if (age <= min) {
138 debug(22,1,"refreshCheck: NO: age < min\n");
139 return 0;
140 }
141 if (-1 < entry->expires && entry->expires <= squid_curtime) {
142 debug(22,1,"refreshCheck: YES: expires <= curtime\n");
143 return 1;
144 }
145 if (age > max) {
146 debug(22,1,"refreshCheck: YES: age > max\n");
147 return 1;
148 }
149 if (entry->timestamp <= entry->lastmod) {
150 debug(22,1,"refreshCheck: YES: lastvalid <= lastmod\n");
151 return 1;
152 }
153 factor = 100 * age / (entry->timestamp - entry->lastmod);
154 debug(22,1,"refreshCheck: factor = %d\n", factor);
155 if (factor > pct) {
156 debug(22,1,"refreshCheck: YES: factor > pc\n");
157 return 1;
158 }
159 return 0;
160 }