mod_expires.c revision 3f96d7d0a0739f024c3b30e67718ed8005e311a1
d29201dd5328b88140ce050100693c501852657dChristian Maeder/* ====================================================================
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * The Apache Software License, Version 1.1
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * Redistribution and use in source and binary forms, with or without
cacbb5e3100fb85d23d1614cace3a8662801f2e6Eugen Kuksa * modification, are permitted provided that the following conditions
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 1. Redistributions of source code must retain the above copyright
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * notice, this list of conditions and the following disclaimer.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 2. Redistributions in binary form must reproduce the above copyright
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * notice, this list of conditions and the following disclaimer in
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * the documentation and/or other materials provided with the
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * distribution.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * 3. The end-user documentation included with the redistribution,
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * if any, must include the following acknowledgment:
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * "This product includes software developed by the
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Apache Software Foundation (http://www.apache.org/)."
6342ef8f9f785e79c91f217c30b3ba2b6fa0ad4fEugen Kuksa * Alternately, this acknowledgment may appear in the software itself,
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * if and wherever such third-party acknowledgments normally appear.
52c81e62d4a3797b926fb3392369b45a9fef336cSoeren D. Schulze * 4. The names "Apache" and "Apache Software Foundation" must
52c81e62d4a3797b926fb3392369b45a9fef336cSoeren D. Schulze * not be used to endorse or promote products derived from this
66977d201b3ff7ee9c1f992c0f3f701b69eac2c9Karl Luc * software without prior written permission. For written
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * permission, please contact apache@apache.org.
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * 5. Products derived from this software may not be called "Apache",
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * nor may "Apache" appear in their name, without prior written
a35bacbc16daf5c10f9accfdfadc4971e9d6f648Iulia Ignatov * permission of the Apache Software Foundation.
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * SUCH DAMAGE.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ====================================================================
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * This software consists of voluntary contributions made by many
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * individuals on behalf of the Apache Software Foundation. For more
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * information on the Apache Software Foundation, please see
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * Portions of this software are based upon public domain software
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * originally written at the National Center for Supercomputing Applications,
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * University of Illinois, Urbana-Champaign.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * version 0.0.11
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * status beta
59ec6aa4da4c93d0d4ef53ed74fdf866d0654539Eugen Kuksa * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 26.Jan.96
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * This module allows you to control the form of the Expires: header
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * that Apache issues for each access. Directives can appear in
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * configuration files or in .htaccess files so expiry semantics can
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder * be defined on a per-directory basis.
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * DIRECTIVE SYNTAX
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * Valid directives are:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresActive on | off
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * ExpiresDefault <code><seconds>
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * ExpiresByType type/encoding <code><seconds>
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * Valid values for <code> are:
52c81e62d4a3797b926fb3392369b45a9fef336cSoeren D. Schulze * 'M' expires header shows file modification date + <seconds>
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 'A' expires header shows access time + <seconds>
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * [I'm not sure which of these is best under different
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc * circumstances, I guess it's for other people to explore.
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * The effects may be indistinguishable for a number of cases]
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * <seconds> should be an integer value [acceptable to atoi()]
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * There is NO space between the <code> and <seconds>.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * For example, a directory which contains information which changes
cacbb5e3100fb85d23d1614cace3a8662801f2e6Eugen Kuksa * frequently might contain:
5e6915f767bd46767306abbfbb39ebcbc10a2a33Christian Maeder * # reports generated by cron every hour. don't let caches
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc * # hold onto stale information
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc * ExpiresDefault M3600
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * Another example, our html pages can change all the time, the gifs
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * tend not to change often:
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * # pages are hot (1 week), images are cold (1 month)
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * ExpiresByType text/html A604800
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ExpiresByType image/gif A2592000
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc * Expires can be turned on for all URLs on the server by placing the
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * following directive in a conf file:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresActive on
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * ExpiresActive can also appear in .htaccess files, enabling the
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * behaviour to be turned on or off for each chosen directory.
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * # turn off Expires behaviour in this directory
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * # and subdirectories
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * ExpiresActive off
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * Directives defined for a directory are valid in subdirectories
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * unless explicitly overridden by new directives in the subdirectory
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * .htaccess files.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ALTERNATIVE DIRECTIVE SYNTAX
c96c73387a0c41614ba325f737edad6abbf07e79Eugen Kuksa * Directives can also be defined in a more readable syntax of the form:
1da120eb84c9d3ed16f64d6defd9bc30569dc45bChristian Maeder * ExpiresDefault "<base> [plus] {<num> <type>}*"
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * where <base> is one of:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * now equivalent to 'access'
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * modification
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * where the 'plus' keyword is optional
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * where <num> should be an integer value [acceptable to atoi()]
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * where <type> is one of:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * For example, any of the following directives can be used to make
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * documents expire 1 month after being accessed, by default:
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * ExpiresDefault "access plus 1 month"
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * ExpiresDefault "access plus 4 weeks"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresDefault "access plus 30 days"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * The expiry time can be fine-tuned by adding several '<num> <type>'
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresByType text/html "access plus 1 month 15 days 2 hours"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresByType image/gif "modification plus 5 hours 3 minutes"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * Change-log:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 29.Jan.96 Hardened the add_* functions. Server will now bail out
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * if bad directives are given in the conf files.
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * 02.Feb.96 Returns DECLINED if not 'ExpiresActive on', giving other
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * expires-aware modules a chance to play with the same
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * directives. [Michael Rutman]
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 03.Feb.96 Call tzset() before localtime(). Trying to get the module
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * to work properly in non GMT timezones.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 12.Feb.96 Modified directive syntax to allow more readable commands:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresDefault "now plus 10 days 20 seconds"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresDefault "access plus 30 days"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresDefault "modification plus 1 year 10 months 30 days"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 13.Feb.96 Fix call to table_get() with NULL 2nd parameter [Rob Hartill]
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 19.Feb.96 Call gm_timestr_822() to get time formatted correctly, can't
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * rely on presence of HTTP_TIME_FORMAT in Apache 1.1+.
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * 21.Feb.96 This version (0.0.9) reverses assumptions made in 0.0.8
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * about star/star handlers. Reverting to 0.0.7 behaviour.
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * 08.Jun.96 allows ExpiresDefault to be used with responses that use
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * the DefaultType by not DECLINING, but instead skipping
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * the table_get check and then looking for an ExpiresDefault.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * [Rob Hartill]
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * 04.Nov.96 'const' definitions added.
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * add support for Cache-Control: max-age=20 from the HTTP/1.1
5e6915f767bd46767306abbfbb39ebcbc10a2a33Christian Maeder * proposal (in this case, a ttl of 20 seconds) [ask roy]
* of the mod_cern_meta.c functionality. eg:
* ExpiresExplicit index.html "modification plus 30 days"
#include "apr.h"
#include "apr_strings.h"
#include "apr_lib.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_log.h"
#include "http_request.h"
int active;
char *expiresdefault;
#define ACTIVE_OFF 0
return (void *) new;
if (arg == 0) {
return NULL;
char *word;
int modifier = 0;
int num = 0;
int factor = 0;
return NULL;
while (word[0]) {
if (word[0]) {
factor = 0;
return NULL;
return NULL;
const char *code)
return NULL;
{NULL}
return new;
char *code;
char *timestr;
return DECLINED;
return DECLINED;
return HTTP_INTERNAL_SERVER_ERROR;
return DECLINED;
return OK;
switch (code[0]) {
return DECLINED;
return HTTP_INTERNAL_SERVER_ERROR;
/ APR_USEC_PER_SEC));
return OK;