Blame modules/metadata/mod_expires.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * mod_expires.c
Packit 90a5c9
 * version 0.0.11
Packit 90a5c9
 * status beta
Packit 90a5c9
 *
Packit 90a5c9
 * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 26.Jan.96
Packit 90a5c9
 *
Packit 90a5c9
 * This module allows you to control the form of the Expires: header
Packit 90a5c9
 * that Apache issues for each access.  Directives can appear in
Packit 90a5c9
 * configuration files or in .htaccess files so expiry semantics can
Packit 90a5c9
 * be defined on a per-directory basis.
Packit 90a5c9
 *
Packit 90a5c9
 * DIRECTIVE SYNTAX
Packit 90a5c9
 *
Packit 90a5c9
 * Valid directives are:
Packit 90a5c9
 *
Packit 90a5c9
 *     ExpiresActive on | off
Packit 90a5c9
 *     ExpiresDefault <seconds>
Packit 90a5c9
 *     ExpiresByType type/encoding <seconds>
Packit 90a5c9
 *
Packit 90a5c9
 * Valid values for  are:
Packit 90a5c9
 *
Packit 90a5c9
 *     'M'      expires header shows file modification date + <seconds>
Packit 90a5c9
 *     'A'      expires header shows access time + <seconds>
Packit 90a5c9
 *
Packit 90a5c9
 *              [I'm not sure which of these is best under different
Packit 90a5c9
 *              circumstances, I guess it's for other people to explore.
Packit 90a5c9
 *              The effects may be indistinguishable for a number of cases]
Packit 90a5c9
 *
Packit 90a5c9
 * <seconds> should be an integer value [acceptable to atoi()]
Packit 90a5c9
 *
Packit 90a5c9
 * There is NO space between the  and <seconds>.
Packit 90a5c9
 *
Packit 90a5c9
 * For example, a directory which contains information which changes
Packit 90a5c9
 * frequently might contain:
Packit 90a5c9
 *
Packit 90a5c9
 *     # reports generated by cron every hour.  don't let caches
Packit 90a5c9
 *     # hold onto stale information
Packit 90a5c9
 *     ExpiresDefault M3600
Packit 90a5c9
 *
Packit 90a5c9
 * Another example, our html pages can change all the time, the gifs
Packit 90a5c9
 * tend not to change often:
Packit 90a5c9
 *
Packit 90a5c9
 *     # pages are hot (1 week), images are cold (1 month)
Packit 90a5c9
 *     ExpiresByType text/html A604800
Packit 90a5c9
 *     ExpiresByType image/gif A2592000
Packit 90a5c9
 *
Packit 90a5c9
 * Expires can be turned on for all URLs on the server by placing the
Packit 90a5c9
 * following directive in a conf file:
Packit 90a5c9
 *
Packit 90a5c9
 *     ExpiresActive on
Packit 90a5c9
 *
Packit 90a5c9
 * ExpiresActive can also appear in .htaccess files, enabling the
Packit 90a5c9
 * behaviour to be turned on or off for each chosen directory.
Packit 90a5c9
 *
Packit 90a5c9
 *     # turn off Expires behaviour in this directory
Packit 90a5c9
 *     # and subdirectories
Packit 90a5c9
 *     ExpiresActive off
Packit 90a5c9
 *
Packit 90a5c9
 * Directives defined for a directory are valid in subdirectories
Packit 90a5c9
 * unless explicitly overridden by new directives in the subdirectory
Packit 90a5c9
 * .htaccess files.
Packit 90a5c9
 *
Packit 90a5c9
 * ALTERNATIVE DIRECTIVE SYNTAX
Packit 90a5c9
 *
Packit 90a5c9
 * Directives can also be defined in a more readable syntax of the form:
Packit 90a5c9
 *
Packit 90a5c9
 *     ExpiresDefault "<base> [plus] {<num> <type>}*"
Packit 90a5c9
 *     ExpiresByType type/encoding "<base> [plus] {<num> <type>}*"
Packit 90a5c9
 *
Packit 90a5c9
 * where <base> is one of:
Packit 90a5c9
 *      access
Packit 90a5c9
 *      now             equivalent to 'access'
Packit 90a5c9
 *      modification
Packit 90a5c9
 *
Packit 90a5c9
 * where the 'plus' keyword is optional
Packit 90a5c9
 *
Packit 90a5c9
 * where <num> should be an integer value [acceptable to atoi()]
Packit 90a5c9
 *
Packit 90a5c9
 * where <type> is one of:
Packit 90a5c9
 *      years
Packit 90a5c9
 *      months
Packit 90a5c9
 *      weeks
Packit 90a5c9
 *      days
Packit 90a5c9
 *      hours
Packit 90a5c9
 *      minutes
Packit 90a5c9
 *      seconds
Packit 90a5c9
 *
Packit 90a5c9
 * For example, any of the following directives can be used to make
Packit 90a5c9
 * documents expire 1 month after being accessed, by default:
Packit 90a5c9
 *
Packit 90a5c9
 *      ExpiresDefault "access plus 1 month"
Packit 90a5c9
 *      ExpiresDefault "access plus 4 weeks"
Packit 90a5c9
 *      ExpiresDefault "access plus 30 days"
Packit 90a5c9
 *
Packit 90a5c9
 * The expiry time can be fine-tuned by adding several '<num> <type>'
Packit 90a5c9
 * clauses:
Packit 90a5c9
 *
Packit 90a5c9
 *      ExpiresByType text/html "access plus 1 month 15 days 2 hours"
Packit 90a5c9
 *      ExpiresByType image/gif "modification plus 5 hours 3 minutes"
Packit 90a5c9
 *
Packit 90a5c9
 * ---
Packit 90a5c9
 *
Packit 90a5c9
 * Change-log:
Packit 90a5c9
 * 29.Jan.96    Hardened the add_* functions.  Server will now bail out
Packit 90a5c9
 *              if bad directives are given in the conf files.
Packit 90a5c9
 * 02.Feb.96    Returns DECLINED if not 'ExpiresActive on', giving other
Packit 90a5c9
 *              expires-aware modules a chance to play with the same
Packit 90a5c9
 *              directives. [Michael Rutman]
Packit 90a5c9
 * 03.Feb.96    Call tzset() before localtime().  Trying to get the module
Packit 90a5c9
 *              to work properly in non GMT timezones.
Packit 90a5c9
 * 12.Feb.96    Modified directive syntax to allow more readable commands:
Packit 90a5c9
 *                ExpiresDefault "now plus 10 days 20 seconds"
Packit 90a5c9
 *                ExpiresDefault "access plus 30 days"
Packit 90a5c9
 *                ExpiresDefault "modification plus 1 year 10 months 30 days"
Packit 90a5c9
 * 13.Feb.96    Fix call to table_get() with NULL 2nd parameter [Rob Hartill]
Packit 90a5c9
 * 19.Feb.96    Call gm_timestr_822() to get time formatted correctly, can't
Packit 90a5c9
 *              rely on presence of HTTP_TIME_FORMAT in Apache 1.1+.
Packit 90a5c9
 * 21.Feb.96    This version (0.0.9) reverses assumptions made in 0.0.8
Packit 90a5c9
 *              about star/star handlers.  Reverting to 0.0.7 behaviour.
Packit 90a5c9
 * 08.Jun.96    allows ExpiresDefault to be used with responses that use
Packit 90a5c9
 *              the DefaultType by not DECLINING, but instead skipping
Packit 90a5c9
 *              the table_get check and then looking for an ExpiresDefault.
Packit 90a5c9
 *              [Rob Hartill]
Packit 90a5c9
 * 04.Nov.96    'const' definitions added.
Packit 90a5c9
 *
Packit 90a5c9
 * TODO
Packit 90a5c9
 * add support for Cache-Control: max-age=20 from the HTTP/1.1
Packit 90a5c9
 * proposal (in this case, a ttl of 20 seconds) [ask roy]
Packit 90a5c9
 * add per-file expiry and explicit expiry times - duplicates some
Packit 90a5c9
 * of the mod_cern_meta.c functionality.  eg:
Packit 90a5c9
 *              ExpiresExplicit index.html "modification plus 30 days"
Packit 90a5c9
 *
Packit 90a5c9
 * BUGS
Packit 90a5c9
 * Hi, welcome to the internet.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    int active;
Packit 90a5c9
    int wildcards;
Packit 90a5c9
    char *expiresdefault;
Packit 90a5c9
    apr_table_t *expiresbytype;
Packit 90a5c9
} expires_dir_config;
Packit 90a5c9
Packit 90a5c9
/* from mod_dir, why is this alias used?
Packit 90a5c9
 */
Packit 90a5c9
#define DIR_CMD_PERMS OR_INDEXES
Packit 90a5c9
Packit 90a5c9
#define ACTIVE_ON       1
Packit 90a5c9
#define ACTIVE_OFF      0
Packit 90a5c9
#define ACTIVE_DONTCARE 2
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA expires_module;
Packit 90a5c9
Packit 90a5c9
static void *create_dir_expires_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config *new =
Packit 90a5c9
    (expires_dir_config *) apr_pcalloc(p, sizeof(expires_dir_config));
Packit 90a5c9
    new->active = ACTIVE_DONTCARE;
Packit 90a5c9
    new->wildcards = 0;
Packit 90a5c9
    new->expiresdefault = NULL;
Packit 90a5c9
    new->expiresbytype = apr_table_make(p, 4);
Packit 90a5c9
    return (void *) new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_expiresactive(cmd_parms *cmd, void *in_dir_config, int arg)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config *dir_config = in_dir_config;
Packit 90a5c9
Packit 90a5c9
    /* if we're here at all it's because someone explicitly
Packit 90a5c9
     * set the active flag
Packit 90a5c9
     */
Packit 90a5c9
    dir_config->active = ACTIVE_ON;
Packit 90a5c9
    if (arg == 0) {
Packit 90a5c9
        dir_config->active = ACTIVE_OFF;
Packit 90a5c9
    }
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* check_code() parse 'code' and return NULL or an error response
Packit 90a5c9
 * string.  If we return NULL then real_code contains code converted
Packit 90a5c9
 * to the cnnnn format.
Packit 90a5c9
 */
Packit 90a5c9
static char *check_code(apr_pool_t *p, const char *code, char **real_code)
Packit 90a5c9
{
Packit 90a5c9
    char *word;
Packit 90a5c9
    char base = 'X';
Packit 90a5c9
    int modifier = 0;
Packit 90a5c9
    int num = 0;
Packit 90a5c9
    int factor;
Packit 90a5c9
Packit 90a5c9
    /* 0.0.4 compatibility?
Packit 90a5c9
     */
Packit 90a5c9
    if ((code[0] == 'A') || (code[0] == 'M')) {
Packit 90a5c9
        *real_code = (char *)code;
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* <base> [plus] {<num> <type>}*
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    /* <base>
Packit 90a5c9
     */
Packit 90a5c9
    word = ap_getword_conf(p, &code);
Packit 90a5c9
    if (!strncasecmp(word, "now", 1) ||
Packit 90a5c9
        !strncasecmp(word, "access", 1)) {
Packit 90a5c9
        base = 'A';
Packit 90a5c9
    }
Packit 90a5c9
    else if (!strncasecmp(word, "modification", 1)) {
Packit 90a5c9
        base = 'M';
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return apr_pstrcat(p, "bad expires code, unrecognised <base> '",
Packit 90a5c9
                       word, "'", NULL);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* [plus]
Packit 90a5c9
     */
Packit 90a5c9
    word = ap_getword_conf(p, &code);
Packit 90a5c9
    if (!strncasecmp(word, "plus", 1)) {
Packit 90a5c9
        word = ap_getword_conf(p, &code);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* {<num> <type>}*
Packit 90a5c9
     */
Packit 90a5c9
    while (word[0]) {
Packit 90a5c9
        /* <num>
Packit 90a5c9
         */
Packit 90a5c9
        if (apr_isdigit(word[0])) {
Packit 90a5c9
            num = atoi(word);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return apr_pstrcat(p, "bad expires code, numeric value expected <num> '",
Packit 90a5c9
                           word, "'", NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* <type>
Packit 90a5c9
         */
Packit 90a5c9
        word = ap_getword_conf(p, &code);
Packit 90a5c9
        if (word[0] == '\0') {
Packit 90a5c9
            return apr_pstrcat(p, "bad expires code, missing <type>", NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (!strncasecmp(word, "years", 1)) {
Packit 90a5c9
            factor = 60 * 60 * 24 * 365;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "months", 2)) {
Packit 90a5c9
            factor = 60 * 60 * 24 * 30;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "weeks", 1)) {
Packit 90a5c9
            factor = 60 * 60 * 24 * 7;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "days", 1)) {
Packit 90a5c9
            factor = 60 * 60 * 24;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "hours", 1)) {
Packit 90a5c9
            factor = 60 * 60;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "minutes", 2)) {
Packit 90a5c9
            factor = 60;
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strncasecmp(word, "seconds", 1)) {
Packit 90a5c9
            factor = 1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            return apr_pstrcat(p, "bad expires code, unrecognised <type>",
Packit 90a5c9
                           "'", word, "'", NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        modifier = modifier + factor * num;
Packit 90a5c9
Packit 90a5c9
        /* next <num>
Packit 90a5c9
         */
Packit 90a5c9
        word = ap_getword_conf(p, &code);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *real_code = apr_psprintf(p, "%c%d", base, modifier);
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_expiresbytype(cmd_parms *cmd, void *in_dir_config,
Packit 90a5c9
                                     const char *mime, const char *code)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config *dir_config = in_dir_config;
Packit 90a5c9
    char *response, *real_code;
Packit 90a5c9
    const char *check;
Packit 90a5c9
Packit 90a5c9
    check = ap_strrchr_c(mime, '/');
Packit 90a5c9
    if (check == NULL) {
Packit 90a5c9
        return "Invalid mimetype: should contain a slash";
Packit 90a5c9
    }
Packit 90a5c9
    if ((strlen(++check) == 1) && (*check == '*')) {
Packit 90a5c9
        dir_config->wildcards = 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
Packit 90a5c9
        apr_table_setn(dir_config->expiresbytype, mime, real_code);
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    return apr_pstrcat(cmd->pool,
Packit 90a5c9
                 "'ExpiresByType ", mime, " ", code, "': ", response, NULL);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_expiresdefault(cmd_parms *cmd, void *in_dir_config,
Packit 90a5c9
                                      const char *code)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config * dir_config = in_dir_config;
Packit 90a5c9
    char *response, *real_code;
Packit 90a5c9
Packit 90a5c9
    if ((response = check_code(cmd->pool, code, &real_code)) == NULL) {
Packit 90a5c9
        dir_config->expiresdefault = real_code;
Packit 90a5c9
        return NULL;
Packit 90a5c9
    }
Packit 90a5c9
    return apr_pstrcat(cmd->pool,
Packit 90a5c9
                   "'ExpiresDefault ", code, "': ", response, NULL);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const command_rec expires_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_FLAG("ExpiresActive", set_expiresactive, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                 "Limited to 'on' or 'off'"),
Packit 90a5c9
    AP_INIT_TAKE2("ExpiresByType", set_expiresbytype, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                  "a MIME type followed by an expiry date code"),
Packit 90a5c9
    AP_INIT_TAKE1("ExpiresDefault", set_expiresdefault, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                  "an expiry date code"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static void *merge_expires_dir_configs(apr_pool_t *p, void *basev, void *addv)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config *new = (expires_dir_config *) apr_pcalloc(p, sizeof(expires_dir_config));
Packit 90a5c9
    expires_dir_config *base = (expires_dir_config *) basev;
Packit 90a5c9
    expires_dir_config *add = (expires_dir_config *) addv;
Packit 90a5c9
Packit 90a5c9
    if (add->active == ACTIVE_DONTCARE) {
Packit 90a5c9
        new->active = base->active;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        new->active = add->active;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (add->expiresdefault != NULL) {
Packit 90a5c9
        new->expiresdefault = add->expiresdefault;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        new->expiresdefault = base->expiresdefault;
Packit 90a5c9
    }
Packit 90a5c9
    new->wildcards = add->wildcards;
Packit 90a5c9
    new->expiresbytype = apr_table_overlay(p, add->expiresbytype,
Packit 90a5c9
                                        base->expiresbytype);
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Handle the setting of the expiration response header fields according
Packit 90a5c9
 * to our criteria.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
static int set_expiration_fields(request_rec *r, const char *code,
Packit 90a5c9
                                 apr_table_t *t)
Packit 90a5c9
{
Packit 90a5c9
    apr_time_t base;
Packit 90a5c9
    apr_time_t additional;
Packit 90a5c9
    apr_time_t expires;
Packit 90a5c9
    int additional_sec;
Packit 90a5c9
    char *timestr;
Packit 90a5c9
Packit 90a5c9
    switch (code[0]) {
Packit 90a5c9
    case 'M':
Packit 90a5c9
        if (r->finfo.filetype == APR_NOFILE) {
Packit 90a5c9
            /* file doesn't exist on disk, so we can't do anything based on
Packit 90a5c9
             * modification time.  Note that this does _not_ log an error.
Packit 90a5c9
             */
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
        base = r->finfo.mtime;
Packit 90a5c9
        additional_sec = atoi(&code[1]);
Packit 90a5c9
        additional = apr_time_from_sec(additional_sec);
Packit 90a5c9
        break;
Packit 90a5c9
    case 'A':
Packit 90a5c9
        /* there's been some discussion and it's possible that
Packit 90a5c9
         * 'access time' will be stored in request structure
Packit 90a5c9
         */
Packit 90a5c9
        base = r->request_time;
Packit 90a5c9
        additional_sec = atoi(&code[1]);
Packit 90a5c9
        additional = apr_time_from_sec(additional_sec);
Packit 90a5c9
        break;
Packit 90a5c9
    default:
Packit 90a5c9
        /* expecting the add_* routines to be case-hardened this
Packit 90a5c9
         * is just a reminder that module is beta
Packit 90a5c9
         */
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01500)
Packit 90a5c9
                    "internal error: bad expires code: %s", r->filename);
Packit 90a5c9
        return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    expires = base + additional;
Packit 90a5c9
    if (expires < r->request_time) {
Packit 90a5c9
        expires = r->request_time;
Packit 90a5c9
    }
Packit 90a5c9
    apr_table_mergen(t, "Cache-Control",
Packit 90a5c9
                     apr_psprintf(r->pool, "max-age=%" APR_TIME_T_FMT,
Packit 90a5c9
                                  apr_time_sec(expires - r->request_time)));
Packit 90a5c9
    timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
Packit 90a5c9
    apr_rfc822_date(timestr, expires);
Packit 90a5c9
    apr_table_setn(t, "Expires", timestr);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Output filter to set the Expires response header field
Packit 90a5c9
 * according to the content-type of the response -- if it hasn't
Packit 90a5c9
 * already been set.
Packit 90a5c9
 */
Packit 90a5c9
static apr_status_t expires_filter(ap_filter_t *f,
Packit 90a5c9
                                   apr_bucket_brigade *b)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *r;
Packit 90a5c9
    expires_dir_config *conf;
Packit 90a5c9
    const char *expiry;
Packit 90a5c9
    apr_table_t *t;
Packit 90a5c9
Packit 90a5c9
    /* Don't add Expires headers to errors */
Packit 90a5c9
    if (ap_is_HTTP_ERROR(f->r->status)) {
Packit 90a5c9
        ap_remove_output_filter(f);
Packit 90a5c9
        return ap_pass_brigade(f->next, b);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    r = f->r;
Packit 90a5c9
    conf = (expires_dir_config *) ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                       &expires_module);
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Check to see which output header table we should use;
Packit 90a5c9
     * mod_cgi loads script fields into r->err_headers_out,
Packit 90a5c9
     * for instance.
Packit 90a5c9
     */
Packit 90a5c9
    expiry = apr_table_get(r->err_headers_out, "Expires");
Packit 90a5c9
    if (expiry != NULL) {
Packit 90a5c9
        t = r->err_headers_out;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        expiry = apr_table_get(r->headers_out, "Expires");
Packit 90a5c9
        t = r->headers_out;
Packit 90a5c9
    }
Packit 90a5c9
    if (expiry == NULL) {
Packit 90a5c9
        /*
Packit 90a5c9
         * No expiration has been set, so we can apply any managed by
Packit 90a5c9
         * this module.  First, check to see if there is an applicable
Packit 90a5c9
         * ExpiresByType directive.
Packit 90a5c9
         */
Packit 90a5c9
        expiry = apr_table_get(conf->expiresbytype,
Packit 90a5c9
                               ap_field_noparam(r->pool, r->content_type));
Packit 90a5c9
        if (expiry == NULL) {
Packit 90a5c9
            int usedefault = 1;
Packit 90a5c9
            /*
Packit 90a5c9
             * See if we have a wildcard entry for the major type.
Packit 90a5c9
             */
Packit 90a5c9
            if (conf->wildcards) {
Packit 90a5c9
                char *checkmime;
Packit 90a5c9
                char *spos;
Packit 90a5c9
                checkmime = apr_pstrdup(r->pool, r->content_type);
Packit 90a5c9
                spos = checkmime ? ap_strchr(checkmime, '/') : NULL;
Packit 90a5c9
                if (spos != NULL) {
Packit 90a5c9
                    /*
Packit 90a5c9
                     * Without a '/' character, nothing we have will match.
Packit 90a5c9
                     * However, we have one.
Packit 90a5c9
                     */
Packit 90a5c9
                    if (strlen(++spos) > 0) {
Packit 90a5c9
                        *spos++ = '*';
Packit 90a5c9
                        *spos = '\0';
Packit 90a5c9
                    }
Packit 90a5c9
                    else {
Packit 90a5c9
                        checkmime = apr_pstrcat(r->pool, checkmime, "*", NULL);
Packit 90a5c9
                    }
Packit 90a5c9
                    expiry = apr_table_get(conf->expiresbytype, checkmime);
Packit 90a5c9
                    usedefault = (expiry == NULL);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            if (usedefault) {
Packit 90a5c9
                /*
Packit 90a5c9
                 * Use the ExpiresDefault directive
Packit 90a5c9
                 */
Packit 90a5c9
                expiry = conf->expiresdefault;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        if (expiry != NULL) {
Packit 90a5c9
            set_expiration_fields(r, expiry, t);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ap_remove_output_filter(f);
Packit 90a5c9
    return ap_pass_brigade(f->next, b);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void expires_insert_filter(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    expires_dir_config *conf;
Packit 90a5c9
Packit 90a5c9
    /* Don't add Expires headers to errors */
Packit 90a5c9
    if (ap_is_HTTP_ERROR(r->status)) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
    /* Say no to subrequests */
Packit 90a5c9
    if (r->main != NULL) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
    conf = (expires_dir_config *) ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                                       &expires_module);
Packit 90a5c9
Packit 90a5c9
    /* Check to see if the filter is enabled and if there are any applicable
Packit 90a5c9
     * config directives for this directory scope
Packit 90a5c9
     */
Packit 90a5c9
    if (conf->active != ACTIVE_ON ||
Packit 90a5c9
        (apr_is_empty_table(conf->expiresbytype) && !conf->expiresdefault)) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
    ap_add_output_filter("MOD_EXPIRES", NULL, r, r->connection);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    /* mod_expires needs to run *before* the cache save filter which is
Packit 90a5c9
     * AP_FTYPE_CONTENT_SET-1.  Otherwise, our expires won't be honored.
Packit 90a5c9
     */
Packit 90a5c9
    ap_register_output_filter("MOD_EXPIRES", expires_filter, NULL,
Packit 90a5c9
                              AP_FTYPE_CONTENT_SET-2);
Packit 90a5c9
    ap_hook_insert_error_filter(expires_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
    ap_hook_insert_filter(expires_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(expires) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_dir_expires_config,  /* dir config creater */
Packit 90a5c9
    merge_expires_dir_configs,  /* dir merger --- default is to override */
Packit 90a5c9
    NULL,                       /* server config */
Packit 90a5c9
    NULL,                       /* merge server configs */
Packit 90a5c9
    expires_cmds,               /* command apr_table_t */
Packit 90a5c9
    register_hooks              /* register hooks */
Packit 90a5c9
};