mod_headers.c revision 1f863242c3901a633082bab8a34e8cb7422ab421
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* mod_headers.c: Add/append/remove HTTP response headers
* Written by Paul Sutton, paul@ukweb.com, 1 Oct 1996
*
* within the response message. The RequestHeader directive can be used
* Valid in both per-server and per-dir configurations.
*
* Syntax is:
*
* Header action header value
* RequestHeader action header value
*
* Where action is one of:
* set - set this header, replacing any old value
* add - add this header, possible resulting in two or more
* headers with the same name
* append - append this text onto any existing header of this same
* unset - remove this header
*
* Where action is unset, the third argument (value) should not be given.
* The header name can include the colon, or not.
*
* The Header and RequestHeader directives can only be used where allowed
* by the FileInfo override.
*
* When the request is processed, the header directives are processed in
* this order: firstly, the main server, then the virtual server handling
* this request (if any), then any <Directory> sections (working downwards
* from the root dir), then an <Location> sections (working down from
* shortest URL component), the any <File> sections. This order is
* important if any 'set' or 'unset' actions are used. For example,
* the following two directives have different effect if applied in
* the reverse order:
*
* Header append Author "John P. Doe"
* Header unset Author
*
* Examples:
*
* To set the "Author" header, use
* Header add Author "John P. Doe"
*
* To remove a header:
* Header unset Author
*
*/
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_buckets.h"
#include "apr_hash.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_log.h"
#include "util_filter.h"
/* format_tag_hash is initialized during pre-config */
static apr_hash_t *format_tag_hash;
typedef enum {
} hdr_actions;
typedef enum {
hdr_in = 0, /* RequestHeader */
} hdr_inout;
/*
* There is an array of struct format_tag per Header/RequestHeader
* config directive
*/
typedef struct {
char *arg;
} format_tag;
/*
* There is one "header_entry" per Header/RequestHeader config directive
*/
typedef struct {
char *header;
const char *condition_var;
} header_entry;
/* echo_do is used for Header echo to iterate through the request headers*/
typedef struct {
request_rec *r;
} echo_do;
/*
* headers_conf is our per-module configuration. This is used as both
* a per-dir and per-server config
*/
typedef struct {
} headers_conf;
/*
* Tag formatting functions
*/
{
return stuff;
}
static const char *header_request_duration(request_rec *r, char *a)
{
}
static const char *header_request_time(request_rec *r, char *a)
{
}
static const char *header_request_env_var(request_rec *r, char *a)
{
const char *s = apr_table_get(r->subprocess_env,a);
if (s)
return s;
else
return "(null)";
}
/*
* Config routines
*/
{
return conf;
}
static void *create_headers_dir_config(apr_pool_t *p, char *d)
{
return create_headers_config(p, NULL);
}
{
return newconf;
}
{
const char *s;
char *d;
s = *sa;
while (*s && *s != '%') {
s++;
}
/*
* This might allocate a few chars extra if there's a backslash
* escape in the format string.
*/
s = *sa;
while (*s && *s != '%') {
if (*s != '\\') {
*d++ = *s++;
}
else {
s++;
switch (*s) {
case '\\':
*d++ = '\\';
s++;
break;
case 'r':
*d++ = '\r';
s++;
break;
case 'n':
*d++ = '\n';
s++;
break;
case 't':
*d++ = '\t';
s++;
break;
default:
/* copy verbatim */
*d++ = '\\';
/*
* Allow the loop to deal with this *s in the normal
* fashion so that it handles end of string etc.
* properly.
*/
break;
}
}
}
*d = '\0';
*sa = s;
return NULL;
}
{
const char *s = *sa;
const char * (*tag_handler)(request_rec *,char *);
/* Handle string literal/conditionals */
if (*s != '%') {
}
s++; /* skip the % */
/* grab the argument if there is one */
if (*s == '{') {
++s;
}
if (!tag_handler) {
char dummy[2];
dummy[0] = s[-1];
return apr_pstrcat(p, "Unrecognized Header or RequestHeader directive %",
}
*sa = s;
return NULL;
}
/*
* A format string consists of white space, text and optional format
* tags in any order. E.g.,
*
* Header add MyHeader "Free form text %D %t more text"
*
* Decompose the format string into its tags. Each tag (struct format_tag)
* contains a pointer to the function used to format the tag. Then save each
* tag in the tag array anchored in the header_entry.
*/
{
char *res;
/* No string to parse with unset and copy commands */
return NULL;
}
while (*s) {
return res;
}
}
return NULL;
}
/* handle RequestHeader and Header directive */
{
const char *condition_var = NULL;
char *colon;
}
else {
new = (header_entry *) apr_array_push((hdr_in == inout) ? serverconf->fixup_in : serverconf->fixup_out);
}
else
return "first argument must be add, set, append, unset or echo.";
if (value)
return "header unset takes two arguments";
}
if (value)
return "Header echo takes two arguments";
return "Header echo only valid on Header directive";
else {
return "Header echo regex could not be compiled";
}
}
}
else if (!value)
return "header requires three arguments";
/* Handle the envclause on Header */
return "error: envclause (env=...) only valid on Header directive";
}
return "error: envclause should be in the form env=envar";
}
return "error: missing environment variable name. envclause should be in the form env=envar ";
}
}
*colon = '\0';
}
/* Handle Header directive */
const char *args)
{
const char *s;
const char *action;
const char *hdr;
const char *val;
const char *envclause;
}
/* handle RequestHeader directive */
const char *value)
{
}
/*
* Process the tags in the format string. Tags may be format specifiers
* (%D, %t, etc.), whitespace or text strings. For each tag, run the handler
* (formatter) specific to the tag. Handlers return text strings.
* Concatenate the return from each handler into one string that is
* returned from this call.
*/
{
int i;
const char *s;
else
}
return str;
}
{
/* If the input header (key) matches the regex, echo it intact to
* r->headers_out.
*/
}
return 1;
}
{
int i;
/* Have any conditional envar-controlled Header processing to do? */
if (hdr->condition_var) {
if (*envar != '!') {
continue;
}
else {
continue;
}
}
case hdr_add:
break;
case hdr_append:
break;
case hdr_set:
break;
case hdr_unset:
break;
case hdr_echo:
{
echo_do v;
v.r = r;
apr_table_do((int (*) (void *, const char *, const char *))
break;
}
}
}
}
static void ap_headers_insert_output_filter(request_rec *r)
{
}
}
{
"headers: ap_headers_output_filter()");
/* do the fixup */
/* remove ourselves from the filter chain */
/* send the data up the stack */
}
{
/* do the fixup */
}
return DECLINED;
}
static const command_rec headers_cmds[] =
{
"an action, header and value followed by optional env clause"),
"an action, header and value"),
{NULL}
};
{
const void *h = apr_palloc(p, sizeof(h));
h = tag_handler;
}
{
format_tag_hash = apr_hash_make(p);
return OK;
}
static void register_hooks(apr_pool_t *p)
{
}
{
create_headers_dir_config, /* dir config creater */
merge_headers_config, /* dir merger --- default is to override */
create_headers_config, /* server config */
merge_headers_config, /* merge server configs */
headers_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};