mod_headers.c revision 26f56d4a3c12077d605362e97490e34522fa4814
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* 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
* merge - merge this text onto any existing header of this same,
* avoiding duplicate values
* unset - remove this header
* edit - transform the header value according to a regexp
*
* 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"
#include "http_protocol.h"
#include "ap_expr.h"
#include "mod_ssl.h" /* for the ssl_var_lookup optional function defn */
/* format_tag_hash is initialized during pre-config */
static apr_hash_t *format_tag_hash;
typedef enum {
} hdr_actions;
/*
* magic cmd->info values
*/
/* Callback function type. */
typedef const char *format_tag_fn(request_rec *r, char *a);
/*
* There is an array of struct format_tag per Header/RequestHeader
* config directive
*/
typedef struct {
char *arg;
} format_tag;
/* 'Magic' condition_var value to run action in post_read_request */
static const char* condition_early = "early";
/*
* There is one "header_entry" per Header/RequestHeader config directive
*/
typedef struct {
const char *header;
const char *condition_var;
const char *subs;
} header_entry;
/* echo_do is used for Header echo to iterate through the request headers*/
typedef struct {
request_rec *r;
} echo_do;
/* edit_do is used for Header edit to iterate through the request headers */
typedef struct {
apr_pool_t *p;
apr_table_t *t;
} edit_do;
/*
* headers_conf is our per-module configuration. This is used as both
* a per-dir and per-server config
*/
typedef struct {
} headers_conf;
/* Pointer to ssl_var_lookup, if available. */
/*
* Tag formatting functions
*/
{
return stuff;
}
static const char *header_request_duration(request_rec *r, char *a)
{
(apr_time_now() - r->request_time));
}
static const char *header_request_time(request_rec *r, char *a)
{
}
/* unwrap_header returns HDR with any newlines converted into
* whitespace if necessary. */
{
char *ptr;
do {
*ptr = APR_ASCII_BLANK;
} while (*ptr++);
}
return hdr;
}
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 unwrap_header(r->pool, s);
else
return "(null)";
}
{
if (header_ssl_lookup) {
r->connection, r, name);
else
return "(null)";
}
else {
return "(null)";
}
}
/*
* Config routines
*/
static void *create_headers_dir_config(apr_pool_t *p, char *d)
{
return conf;
}
{
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 % */
/* Pass through %% or % at end of string as % */
if ((*s == '%') || (*s == '\0')) {
if (*s)
s++;
*sa = s;
return NULL;
}
/* grab the argument if there is one */
if (*s == '{') {
++s;
}
if (!tag_handler) {
char dummy[2];
dummy[0] = s[-1];
}
*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 echo commands */
return NULL;
}
while (*s) {
return res;
}
}
return NULL;
}
/* handle RequestHeader and Header directive */
void *indirconf,
const char *action,
const char *hdr,
const char *value,
const char *subs,
const char *envclause)
{
const char *condition_var = NULL;
const char *colon;
else
return "first argument must be 'add', 'set', 'append', 'merge', "
"'unset', 'echo', 'edit', or 'edit*'.";
return "Header edit requires a match and a substitution";
}
return "Header edit regex could not be compiled";
}
}
else {
/* there's no subs, so envclause is really that argument */
return "Too many arguments to directive";
}
}
if (value) {
if (envclause) {
return "header unset takes two arguments";
}
}
}
if (value) {
if (envclause) {
return "Header echo takes two arguments";
}
}
return "Header echo only valid on Header "
"directives";
else {
return "Header echo regex could not be compiled";
}
}
}
else if (!value)
return "Header requires three arguments";
/* Handle the envclause on Header */
}
return "error: missing environment variable name. "
"envclause should be in the form env=envar ";
}
}
else {
if (err) {
"Can't parse envclause/expression: ", err,
NULL);
}
}
}
}
}
/* Handle all (xxx)Header directives */
const char *args)
{
const char *action;
const char *hdr;
const char *val;
const char *envclause;
const char *subs;
}
}
}
if (*args) {
" has too many arguments", NULL);
}
}
/*
* 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
}
}
{
unsigned int nmatch = 10;
const char *subs;
const char *remainder;
char *ret;
int diffsz;
/* no match, nothing to do */
return value;
}
}
else { /* recurse to edit multiple matches if applicable */
}
return ret;
}
{
/* If the input header (key) matches the regex, echo it intact to
* r->headers_out.
*/
}
return 1;
}
{
return 1;
}
{
return 1;
}
{
echo_do v;
int i;
const char *val;
/* ignore early headers in late calls */
continue;
}
/* ignore late headers in early calls */
continue;
}
/* Do we have an expression to evaluate? */
if (err) {
"Failed to evaluate expression (%s) - ignoring",
err);
}
else if (!eval) {
continue;
}
}
/* Have any conditional envar-controlled Header processing to do? */
if (*envar != '!') {
continue;
}
else {
continue;
}
}
case hdr_add:
break;
case hdr_append:
break;
case hdr_merge:
} else {
int tok_found = 0;
/* modified version of logic in ap_get_token() */
while (*val) {
const char *tok_start;
++val;
if (*val++ == '"')
while (*val)
if (*val++ == '"')
break;
}
tok_found = 1;
break;
}
if (*val)
++val;
}
if (!tok_found) {
}
}
break;
case hdr_set:
}
break;
case hdr_unset:
break;
case hdr_echo:
v.r = r;
apr_table_do((int (*) (void *, const char *, const char *))
break;
case hdr_edit:
case hdr_edit_r:
r->pool));
}
NULL);
}
break;
}
}
}
static void ap_headers_insert_output_filter(request_rec *r)
{
}
}
/*
* Make sure our error-path filter is in place.
*/
static void ap_headers_insert_error_filter(request_rec *r)
{
}
}
{
"headers: ap_headers_output_filter()");
/* do the fixup */
/* remove ourselves from the filter chain */
/* send the data up the stack */
}
/*
* Make sure we propagate any "Header always" settings on the error
* path through http_protocol.c.
*/
{
"headers: ap_headers_error_filter()");
/*
* Add any header fields defined by "Header always" to r->err_headers_out.
* Server-wide first, then per-directory to allow overriding.
*/
/*
* We've done our bit; remove ourself from the filter chain so there's
* no possibility we'll be called again.
*/
/*
* Pass the buck. (euro?)
*/
}
{
/* do the fixup */
}
return DECLINED;
}
{
/* do the fixup */
}
}
}
return DECLINED;
}
static const command_rec headers_cmds[] =
{
"an optional condition, an action, header and value "
"followed by optional env clause"),
"an action, header and value followed by optional env "
"clause"),
{NULL}
};
static void register_format_tag_handler(const char *tag,
{
}
{
format_tag_hash = apr_hash_make(p);
return OK;
}
{
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 */
NULL, /* server config */
NULL, /* merge server configs */
headers_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};