1 /* ====================================================================
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
53 *
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
57 */
58
59 /*
60 * mod_expires.c
61 * version 0.0.11
62 * status beta
63 *
64 * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 26.Jan.96
65 *
66 * This module allows you to control the form of the Expires: header
67 * that Apache issues for each access. Directives can appear in
68 * configuration files or in .htaccess files so expiry semantics can
69 * be defined on a per-directory basis.
70 *
71 * DIRECTIVE SYNTAX
72 *
73 * Valid directives are:
74 *
75 * ExpiresActive on | off
76 * ExpiresDefault <code><seconds>
77 * ExpiresByType type/encoding <code><seconds>
78 *
79 * Valid values for <code> are:
80 *
81 * 'M' expires header shows file modification date + <seconds>
82 * 'A' expires header shows access time + <seconds>
83 *
84 * [I'm not sure which of these is best under different
85 * circumstances, I guess it's for other people to explore.
86 * The effects may be indistinguishable for a number of cases]
87 *
88 * <seconds> should be an integer value [acceptable to atoi()]
89 *
90 * There is NO space between the <code> and <seconds>.
91 *
92 * For example, a directory which contains information which changes
93 * frequently might contain:
94 *
95 * # reports generated by cron every hour. don't let caches
96 * # hold onto stale information
97 * ExpiresDefault M3600
98 *
99 * Another example, our html pages can change all the time, the gifs
100 * tend not to change often:
101 *
102 * # pages are hot (1 week), images are cold (1 month)
103 * ExpiresByType text/html A604800
104 * ExpiresByType image/gif A2592000
105 *
106 * Expires can be turned on for all URLs on the server by placing the
107 * following directive in a conf file:
108 *
109 * ExpiresActive on
110 *
111 * ExpiresActive can also appear in .htaccess files, enabling the
112 * behaviour to be turned on or off for each chosen directory.
113 *
114 * # turn off Expires behaviour in this directory
115 * # and subdirectories
116 * ExpiresActive off
117 *
118 * Directives defined for a directory are valid in subdirectories
119 * unless explicitly overridden by new directives in the subdirectory
120 * .htaccess files.
121 *
122 * ALTERNATIVE DIRECTIVE SYNTAX
123 *
124 * Directives can also be defined in a more readable syntax of the form:
125 *
126 * ExpiresDefault "<base> [plus] {<num> <type>}*"
127 * ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
128 *
129 * where <base> is one of:
130 * access
131 * now equivalent to 'access'
132 * modification
133 *
134 * where the 'plus' keyword is optional
135 *
136 * where <num> should be an integer value [acceptable to atoi()]
137 *
138 * where <type> is one of:
139 * years
140 * months
141 * weeks
142 * days
143 * hours
144 * minutes
145 * seconds
146 *
147 * For example, any of the following directives can be used to make
148 * documents expire 1 month after being accessed, by default:
149 *
150 * ExpiresDefault "access plus 1 month"
151 * ExpiresDefault "access plus 4 weeks"
152 * ExpiresDefault "access plus 30 days"
153 *
154 * The expiry time can be fine-tuned by adding several '<num> <type>'
155 * clauses:
156 *
157 * ExpiresByType text/html "access plus 1 month 15 days 2 hours"
158 * ExpiresByType image/gif "modification plus 5 hours 3 minutes"
159 *
160 * ---
161 *
162 * Change-log:
163 * 29.Jan.96 Hardened the add_* functions. Server will now bail out
164 * if bad directives are given in the conf files.
165 * 02.Feb.96 Returns DECLINED if not 'ExpiresActive on', giving other
166 * expires-aware modules a chance to play with the same
167 * directives. [Michael Rutman]
168 * 03.Feb.96 Call tzset() before localtime(). Trying to get the module
169 * to work properly in non GMT timezones.
170 * 12.Feb.96 Modified directive syntax to allow more readable commands:
171 * ExpiresDefault "now plus 10 days 20 seconds"
172 * ExpiresDefault "access plus 30 days"
173 * ExpiresDefault "modification plus 1 year 10 months 30 days"
174 * 13.Feb.96 Fix call to table_get() with NULL 2nd parameter [Rob Hartill]
175 * 19.Feb.96 Call gm_timestr_822() to get time formatted correctly, can't
176 * rely on presence of HTTP_TIME_FORMAT in Apache 1.1+.
177 * 21.Feb.96 This version (0.0.9) reverses assumptions made in 0.0.8
178 * about star/star handlers. Reverting to 0.0.7 behaviour.
179 * 08.Jun.96 allows ExpiresDefault to be used with responses that use
180 * the DefaultType by not DECLINING, but instead skipping
181 * the table_get check and then looking for an ExpiresDefault.
182 * [Rob Hartill]
183 * 04.Nov.96 'const' definitions added.
184 *
185 * TODO
186 * add support for Cache-Control: max-age=20 from the HTTP/1.1
187 * proposal (in this case, a ttl of 20 seconds) [ask roy]
188 * add per-file expiry and explicit expiry times - duplicates some
189 * of the mod_cern_meta.c functionality. eg:
190 * ExpiresExplicit index.html "modification plus 30 days"
191 *
192 * BUGS
193 * Hi, welcome to the internet.
194 */
195
196 #include <ctype.h>
197 #include "httpd.h"
198 #include "http_config.h"
199 #include "http_log.h"
200
201 typedef struct {
202 int active;
203 char *expiresdefault;
204 table *expiresbytype;
205 } expires_dir_config;
206
207 /* from mod_dir, why is this alias used?
208 */
209 #define DIR_CMD_PERMS OR_INDEXES
210
211 #define ACTIVE_ON 1
212 #define ACTIVE_OFF 0
213 #define ACTIVE_DONTCARE 2
214
215 module MODULE_VAR_EXPORT expires_module;
216
create_dir_expires_config(pool * p,char * dummy)217 static void *create_dir_expires_config(pool *p, char *dummy)
218 {
219 expires_dir_config *new =
220 (expires_dir_config *) ap_pcalloc(p, sizeof(expires_dir_config));
221 new->active = ACTIVE_DONTCARE;
222 new->expiresdefault = "";
223 new->expiresbytype = ap_make_table(p, 4);
224 return (void *) new;
225 }
226
set_expiresactive(cmd_parms * cmd,expires_dir_config * dir_config,int arg)227 static const char *set_expiresactive(cmd_parms *cmd, expires_dir_config * dir_config, int arg)
228 {
229 /* if we're here at all it's because someone explicitly
230 * set the active flag
231 */
232 dir_config->active = ACTIVE_ON;
233 if (arg == 0) {
234 dir_config->active = ACTIVE_OFF;
235 };
236 return NULL;
237 }
238
239 /* check_code() parse 'code' and return NULL or an error response
240 * string. If we return NULL then real_code contains code converted
241 * to the cnnnn format.
242 */
check_code(pool * p,const char * code,char ** real_code)243 static char *check_code(pool *p, const char *code, char **real_code)
244 {
245 char *word;
246 char base = 'X';
247 int modifier = 0;
248 int num = 0;
249 int factor = 0;
250
251 /* 0.0.4 compatibility?
252 */
253 if ((code[0] == 'A') || (code[0] == 'M')) {
254 *real_code = (char *)code;
255 return NULL;
256 };
257
258 /* <base> [plus] {<num> <type>}*
259 */
260
261 /* <base>
262 */
263 word = ap_getword_conf(p, &code);
264 if (!strncasecmp(word, "now", 1) ||
265 !strncasecmp(word, "access", 1)) {
266 base = 'A';
267 }
268 else if (!strncasecmp(word, "modification", 1)) {
269 base = 'M';
270 }
271 else {
272 return ap_pstrcat(p, "bad expires code, unrecognised <base> '",
273 word, "'", NULL);
274 };
275
276 /* [plus]
277 */
278 word = ap_getword_conf(p, &code);
279 if (!strncasecmp(word, "plus", 1)) {
280 word = ap_getword_conf(p, &code);
281 };
282
283 /* {<num> <type>}*
284 */
285 while (word[0]) {
286 /* <num>
287 */
288 if (isdigit((unsigned char)word[0])) {
289 num = atoi(word);
290 }
291 else {
292 return ap_pstrcat(p, "bad expires code, numeric value expected <num> '",
293 word, "'", NULL);
294 };
295
296 /* <type>
297 */
298 word = ap_getword_conf(p, &code);
299 if (word[0]) {
300 /* do nothing */
301 }
302 else {
303 return ap_pstrcat(p, "bad expires code, missing <type>", NULL);
304 };
305
306 factor = 0;
307 if (!strncasecmp(word, "years", 1)) {
308 factor = 60 * 60 * 24 * 365;
309 }
310 else if (!strncasecmp(word, "months", 2)) {
311 factor = 60 * 60 * 24 * 30;
312 }
313 else if (!strncasecmp(word, "weeks", 1)) {
314 factor = 60 * 60 * 24 * 7;
315 }
316 else if (!strncasecmp(word, "days", 1)) {
317 factor = 60 * 60 * 24;
318 }
319 else if (!strncasecmp(word, "hours", 1)) {
320 factor = 60 * 60;
321 }
322 else if (!strncasecmp(word, "minutes", 2)) {
323 factor = 60;
324 }
325 else if (!strncasecmp(word, "seconds", 1)) {
326 factor = 1;
327 }
328 else {
329 return ap_pstrcat(p, "bad expires code, unrecognised <type>",
330 "'", word, "'", NULL);
331 };
332
333 modifier = modifier + factor * num;
334
335 /* next <num>
336 */
337 word = ap_getword_conf(p, &code);
338 };
339
340 *real_code = ap_psprintf(p, "%c%d", base, modifier);
341
342 return NULL;
343 }
344
set_expiresbytype(cmd_parms * cmd,expires_dir_config * dir_config,char * mime,char * code)345 static const char *set_expiresbytype(cmd_parms *cmd, expires_dir_config * dir_config, char *mime, char *code)
346 {
347 char *response, *real_code;
348
349 if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
350 ap_table_setn(dir_config->expiresbytype, mime, real_code);
351 return NULL;
352 };
353 return ap_pstrcat(cmd->pool,
354 "'ExpiresByType ", mime, " ", code, "': ", response, NULL);
355 }
356
set_expiresdefault(cmd_parms * cmd,expires_dir_config * dir_config,char * code)357 static const char *set_expiresdefault(cmd_parms *cmd, expires_dir_config * dir_config, char *code)
358 {
359 char *response, *real_code;
360
361 if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
362 dir_config->expiresdefault = real_code;
363 return NULL;
364 };
365 return ap_pstrcat(cmd->pool,
366 "'ExpiresDefault ", code, "': ", response, NULL);
367 }
368
369 static const command_rec expires_cmds[] =
370 {
371 {"ExpiresActive", set_expiresactive, NULL, DIR_CMD_PERMS, FLAG,
372 "Limited to 'on' or 'off'"},
373 {"ExpiresBytype", set_expiresbytype, NULL, DIR_CMD_PERMS, TAKE2,
374 "a MIME type followed by an expiry date code"},
375 {"ExpiresDefault", set_expiresdefault, NULL, DIR_CMD_PERMS, TAKE1,
376 "an expiry date code"},
377 {NULL}
378 };
379
merge_expires_dir_configs(pool * p,void * basev,void * addv)380 static void *merge_expires_dir_configs(pool *p, void *basev, void *addv)
381 {
382 expires_dir_config *new = (expires_dir_config *) ap_pcalloc(p, sizeof(expires_dir_config));
383 expires_dir_config *base = (expires_dir_config *) basev;
384 expires_dir_config *add = (expires_dir_config *) addv;
385
386 if (add->active == ACTIVE_DONTCARE) {
387 new->active = base->active;
388 }
389 else {
390 new->active = add->active;
391 };
392
393 if (add->expiresdefault != '\0') {
394 new->expiresdefault = add->expiresdefault;
395 };
396
397 new->expiresbytype = ap_overlay_tables(p, add->expiresbytype,
398 base->expiresbytype);
399 return new;
400 }
401
add_expires(request_rec * r)402 static int add_expires(request_rec *r)
403 {
404 expires_dir_config *conf;
405 char *code;
406 time_t base;
407 time_t additional;
408 time_t expires;
409 char age[20];
410
411 if (ap_is_HTTP_ERROR(r->status)) /* Don't add Expires headers to errors */
412 return DECLINED;
413
414 if (r->main != NULL) /* Say no to subrequests */
415 return DECLINED;
416
417 conf = (expires_dir_config *) ap_get_module_config(r->per_dir_config, &expires_module);
418 if (conf == NULL) {
419 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
420 "internal error: %s", r->filename);
421 return SERVER_ERROR;
422 };
423
424 if (conf->active != ACTIVE_ON)
425 return DECLINED;
426
427 /* we perhaps could use the default_type(r) in its place but that
428 * may be 2nd guesing the desired configuration... calling table_get
429 * with a NULL key will SEGV us
430 *
431 * I still don't know *why* r->content_type would ever be NULL, this
432 * is possibly a result of fixups being called in many different
433 * places. Fixups is probably the wrong place to be doing all this
434 * work... Bah.
435 *
436 * Changed as of 08.Jun.96 don't DECLINE, look for an ExpiresDefault.
437 */
438 if (r->content_type == NULL)
439 code = NULL;
440 else
441 code = (char *) ap_table_get(conf->expiresbytype,
442 ap_field_noparam(r->pool, r->content_type));
443
444 if (code == NULL) {
445 /* no expires defined for that type, is there a default? */
446 code = conf->expiresdefault;
447
448 if (code[0] == '\0')
449 return OK;
450 };
451
452 /* we have our code */
453
454 switch (code[0]) {
455 case 'M':
456 if (r->finfo.st_mode == 0) {
457 /* file doesn't exist on disk, so we can't do anything based on
458 * modification time. Note that this does _not_ log an error.
459 */
460 return DECLINED;
461 }
462 base = r->finfo.st_mtime;
463 additional = atoi(&code[1]);
464 break;
465 case 'A':
466 /* there's been some discussion and it's possible that
467 * 'access time' will be stored in request structure
468 */
469 base = r->request_time;
470 additional = atoi(&code[1]);
471 break;
472 default:
473 /* expecting the add_* routines to be case-hardened this
474 * is just a reminder that module is beta
475 */
476 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
477 "internal error: bad expires code: %s", r->filename);
478 return SERVER_ERROR;
479 };
480
481 expires = base + additional;
482 snprintf(age, sizeof(age), "max-age=%d",
483 (int) expires - (int) r->request_time);
484 ap_table_mergen(r->headers_out, "Cache-Control", ap_pstrdup(r->pool, age));
485 tzset(); /* redundant? called implicitly by localtime,
486 * at least under FreeBSD
487 */
488 ap_table_setn(r->headers_out, "Expires",
489 ap_gm_timestr_822(r->pool, expires));
490 return OK;
491 }
492
493 module MODULE_VAR_EXPORT expires_module =
494 {
495 STANDARD_MODULE_STUFF,
496 NULL, /* initializer */
497 create_dir_expires_config, /* dir config creater */
498 merge_expires_dir_configs, /* dir merger --- default is to override */
499 NULL, /* server config */
500 NULL, /* merge server configs */
501 expires_cmds, /* command table */
502 NULL, /* handlers */
503 NULL, /* filename translation */
504 NULL, /* check_user_id */
505 NULL, /* check auth */
506 NULL, /* check access */
507 NULL, /* type_checker */
508 add_expires, /* fixups */
509 NULL, /* logger */
510 NULL, /* header parser */
511 NULL, /* child_init */
512 NULL, /* child_exit */
513 NULL /* post read-request */
514 };
515
516