mod_expires.c revision 3f96d7d0a0739f024c3b30e67718ed8005e311a1
d29201dd5328b88140ce050100693c501852657dChristian Maeder/* ====================================================================
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * The Apache Software License, Version 1.1
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
cacbb5e3100fb85d23d1614cace3a8662801f2e6Eugen Kuksa * reserved.
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu *
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 * are met:
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
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 *
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.
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder *
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.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc *
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 *
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 *
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 *
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
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * <http://www.apache.org/>.
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc *
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.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc */
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc
551f1476dea3f969775527cb15fd512e86279307Karl Luc/*
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * mod_expires.c
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * version 0.0.11
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * status beta
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
59ec6aa4da4c93d0d4ef53ed74fdf866d0654539Eugen Kuksa * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 26.Jan.96
59ec6aa4da4c93d0d4ef53ed74fdf866d0654539Eugen Kuksa *
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.
e16b3696b2c173aac14200321868ed81b8f7dc69Christian Maeder *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * DIRECTIVE SYNTAX
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc *
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * Valid directives are:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresActive on | off
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * ExpiresDefault <code><seconds>
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * ExpiresByType type/encoding <code><seconds>
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder *
0b282d1269b8f9a658f7ce9090575ba1b00e8d66Christian Maeder * Valid values for <code> are:
52c81e62d4a3797b926fb3392369b45a9fef336cSoeren D. Schulze *
52c81e62d4a3797b926fb3392369b45a9fef336cSoeren D. Schulze * 'M' expires header shows file modification date + <seconds>
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 'A' expires header shows access time + <seconds>
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder *
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]
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa *
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * <seconds> should be an integer value [acceptable to atoi()]
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc *
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * There is NO space between the <code> and <seconds>.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc *
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * For example, a directory which contains information which changes
cacbb5e3100fb85d23d1614cace3a8662801f2e6Eugen Kuksa * frequently might contain:
551f1476dea3f969775527cb15fd512e86279307Karl Luc *
5e6915f767bd46767306abbfbb39ebcbc10a2a33Christian Maeder * # reports generated by cron every hour. don't let caches
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc * # hold onto stale information
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc * ExpiresDefault M3600
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov *
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * Another example, our html pages can change all the time, the gifs
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * tend not to change often:
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov *
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * # pages are hot (1 week), images are cold (1 month)
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * ExpiresByType text/html A604800
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ExpiresByType image/gif A2592000
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder *
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc * Expires can be turned on for all URLs on the server by placing the
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * following directive in a conf file:
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresActive on
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * ExpiresActive can also appear in .htaccess files, enabling the
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * behaviour to be turned on or off for each chosen directory.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * # turn off Expires behaviour in this directory
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * # and subdirectories
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder * ExpiresActive off
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder *
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 *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ALTERNATIVE DIRECTIVE SYNTAX
c96c73387a0c41614ba325f737edad6abbf07e79Eugen Kuksa *
c96c73387a0c41614ba325f737edad6abbf07e79Eugen Kuksa * Directives can also be defined in a more readable syntax of the form:
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder *
1da120eb84c9d3ed16f64d6defd9bc30569dc45bChristian Maeder * ExpiresDefault "<base> [plus] {<num> <type>}*"
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * where <base> is one of:
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * access
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * now equivalent to 'access'
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * modification
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * where the 'plus' keyword is optional
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * where <num> should be an integer value [acceptable to atoi()]
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * where <type> is one of:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * years
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * months
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * weeks
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * days
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * hours
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * minutes
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * seconds
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
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:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * ExpiresDefault "access plus 1 month"
1ea7fb6b0f66210bc0d3cb995f1b655277b33884Eugen Kuksa * ExpiresDefault "access plus 4 weeks"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ExpiresDefault "access plus 30 days"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * The expiry time can be fine-tuned by adding several '<num> <type>'
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * clauses:
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
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 *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * ---
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa *
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 *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * TODO
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]
* add per-file expiry and explicit expiry times - duplicates some
* of the mod_cern_meta.c functionality. eg:
* ExpiresExplicit index.html "modification plus 30 days"
*
* BUGS
* Hi, welcome to the internet.
*/
#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"
typedef struct {
int active;
char *expiresdefault;
apr_table_t *expiresbytype;
} expires_dir_config;
/* from mod_dir, why is this alias used?
*/
#define DIR_CMD_PERMS OR_INDEXES
#define ACTIVE_ON 1
#define ACTIVE_OFF 0
#define ACTIVE_DONTCARE 2
module AP_MODULE_DECLARE_DATA expires_module;
static void *create_dir_expires_config(apr_pool_t *p, char *dummy)
{
expires_dir_config *new =
(expires_dir_config *) apr_pcalloc(p, sizeof(expires_dir_config));
new->active = ACTIVE_DONTCARE;
new->expiresdefault = "";
new->expiresbytype = apr_table_make(p, 4);
return (void *) new;
}
static const char *set_expiresactive(cmd_parms *cmd, void *in_dir_config, int arg)
{
expires_dir_config *dir_config = in_dir_config;
/* if we're here at all it's because someone explicitly
* set the active flag
*/
dir_config->active = ACTIVE_ON;
if (arg == 0) {
dir_config->active = ACTIVE_OFF;
};
return NULL;
}
/* check_code() parse 'code' and return NULL or an error response
* string. If we return NULL then real_code contains code converted
* to the cnnnn format.
*/
static char *check_code(apr_pool_t *p, const char *code, char **real_code)
{
char *word;
char base = 'X';
int modifier = 0;
int num = 0;
int factor = 0;
/* 0.0.4 compatibility?
*/
if ((code[0] == 'A') || (code[0] == 'M')) {
*real_code = (char *)code;
return NULL;
};
/* <base> [plus] {<num> <type>}*
*/
/* <base>
*/
word = ap_getword_conf(p, &code);
if (!strncasecmp(word, "now", 1) ||
!strncasecmp(word, "access", 1)) {
base = 'A';
}
else if (!strncasecmp(word, "modification", 1)) {
base = 'M';
}
else {
return apr_pstrcat(p, "bad expires code, unrecognised <base> '",
word, "'", NULL);
};
/* [plus]
*/
word = ap_getword_conf(p, &code);
if (!strncasecmp(word, "plus", 1)) {
word = ap_getword_conf(p, &code);
};
/* {<num> <type>}*
*/
while (word[0]) {
/* <num>
*/
if (apr_isdigit(word[0])) {
num = atoi(word);
}
else {
return apr_pstrcat(p, "bad expires code, numeric value expected <num> '",
word, "'", NULL);
};
/* <type>
*/
word = ap_getword_conf(p, &code);
if (word[0]) {
/* do nothing */
}
else {
return apr_pstrcat(p, "bad expires code, missing <type>", NULL);
};
factor = 0;
if (!strncasecmp(word, "years", 1)) {
factor = 60 * 60 * 24 * 365;
}
else if (!strncasecmp(word, "months", 2)) {
factor = 60 * 60 * 24 * 30;
}
else if (!strncasecmp(word, "weeks", 1)) {
factor = 60 * 60 * 24 * 7;
}
else if (!strncasecmp(word, "days", 1)) {
factor = 60 * 60 * 24;
}
else if (!strncasecmp(word, "hours", 1)) {
factor = 60 * 60;
}
else if (!strncasecmp(word, "minutes", 2)) {
factor = 60;
}
else if (!strncasecmp(word, "seconds", 1)) {
factor = 1;
}
else {
return apr_pstrcat(p, "bad expires code, unrecognised <type>",
"'", word, "'", NULL);
};
modifier = modifier + factor * num;
/* next <num>
*/
word = ap_getword_conf(p, &code);
};
*real_code = apr_psprintf(p, "%c%d", base, modifier);
return NULL;
}
static const char *set_expiresbytype(cmd_parms *cmd, void *in_dir_config,
const char *mime, const char *code)
{
expires_dir_config *dir_config = in_dir_config;
char *response, *real_code;
if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
apr_table_setn(dir_config->expiresbytype, mime, real_code);
return NULL;
};
return apr_pstrcat(cmd->pool,
"'ExpiresByType ", mime, " ", code, "': ", response, NULL);
}
static const char *set_expiresdefault(cmd_parms *cmd, void *in_dir_config,
const char *code)
{
expires_dir_config * dir_config = in_dir_config;
char *response, *real_code;
if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
dir_config->expiresdefault = real_code;
return NULL;
};
return apr_pstrcat(cmd->pool,
"'ExpiresDefault ", code, "': ", response, NULL);
}
static const command_rec expires_cmds[] =
{
AP_INIT_FLAG("ExpiresActive", set_expiresactive, NULL, DIR_CMD_PERMS,
"Limited to 'on' or 'off'"),
AP_INIT_TAKE2("ExpiresBytype", set_expiresbytype, NULL, DIR_CMD_PERMS,
"a MIME type followed by an expiry date code"),
AP_INIT_TAKE1("ExpiresDefault", set_expiresdefault, NULL, DIR_CMD_PERMS,
"an expiry date code"),
{NULL}
};
static void *merge_expires_dir_configs(apr_pool_t *p, void *basev, void *addv)
{
expires_dir_config *new = (expires_dir_config *) apr_pcalloc(p, sizeof(expires_dir_config));
expires_dir_config *base = (expires_dir_config *) basev;
expires_dir_config *add = (expires_dir_config *) addv;
if (add->active == ACTIVE_DONTCARE) {
new->active = base->active;
}
else {
new->active = add->active;
}
if (add->expiresdefault[0] != '\0') {
new->expiresdefault = add->expiresdefault;
}
else {
new->expiresdefault = base->expiresdefault;
}
new->expiresbytype = apr_table_overlay(p, add->expiresbytype,
base->expiresbytype);
return new;
}
static int add_expires(request_rec *r)
{
expires_dir_config *conf;
char *code;
apr_time_t base;
apr_time_t additional;
apr_time_t expires;
char *timestr;
if (ap_is_HTTP_ERROR(r->status)) /* Don't add Expires headers to errors */
return DECLINED;
if (r->main != NULL) /* Say no to subrequests */
return DECLINED;
conf = (expires_dir_config *) ap_get_module_config(r->per_dir_config, &expires_module);
if (conf == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"internal error: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
};
if (conf->active != ACTIVE_ON)
return DECLINED;
/* we perhaps could use the default_type(r) in its place but that
* may be 2nd guesing the desired configuration... calling table_get
* with a NULL key will SEGV us
*
* I still don't know *why* r->content_type would ever be NULL, this
* is possibly a result of fixups being called in many different
* places. Fixups is probably the wrong place to be doing all this
* work... Bah.
*
* Changed as of 08.Jun.96 don't DECLINE, look for an ExpiresDefault.
*/
if (r->content_type == NULL)
code = NULL;
else
code = (char *) apr_table_get(conf->expiresbytype,
ap_field_noparam(r->pool, r->content_type));
if (code == NULL) {
/* no expires defined for that type, is there a default? */
code = conf->expiresdefault;
if (code[0] == '\0')
return OK;
};
/* we have our code */
switch (code[0]) {
case 'M':
if (r->finfo.filetype == 0) {
/* file doesn't exist on disk, so we can't do anything based on
* modification time. Note that this does _not_ log an error.
*/
return DECLINED;
}
base = r->finfo.mtime;
additional = atoi(&code[1]);
break;
case 'A':
/* there's been some discussion and it's possible that
* 'access time' will be stored in request structure
*/
base = r->request_time;
additional = atoi(&code[1]);
break;
default:
/* expecting the add_* routines to be case-hardened this
* is just a reminder that module is beta
*/
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
"internal error: bad expires code: %s", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
};
expires = base + additional;
apr_table_mergen(r->headers_out, "Cache-Control",
apr_psprintf(r->pool, "max-age=%qd",
(expires - r->request_time)
/ APR_USEC_PER_SEC));
timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
apr_rfc822_date(timestr, expires);
apr_table_setn(r->headers_out, "Expires", timestr);
return OK;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_fixups(add_expires,NULL,NULL,APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA expires_module =
{
STANDARD20_MODULE_STUFF,
create_dir_expires_config, /* dir config creater */
merge_expires_dir_configs, /* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server configs */
expires_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};