|
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_headers.c: Add/append/remove HTTP response headers
|
|
Packit |
90a5c9 |
* Written by Paul Sutton, paul@ukweb.com, 1 Oct 1996
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* The Header directive can be used to add/replace/remove HTTP headers
|
|
Packit |
90a5c9 |
* within the response message. The RequestHeader directive can be used
|
|
Packit |
90a5c9 |
* to add/replace/remove HTTP headers before a request message is processed.
|
|
Packit |
90a5c9 |
* Valid in both per-server and per-dir configurations.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Syntax is:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Header action header value
|
|
Packit |
90a5c9 |
* RequestHeader action header value
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Where action is one of:
|
|
Packit |
90a5c9 |
* set - set this header, replacing any old value
|
|
Packit |
90a5c9 |
* add - add this header, possible resulting in two or more
|
|
Packit |
90a5c9 |
* headers with the same name
|
|
Packit |
90a5c9 |
* append - append this text onto any existing header of this same
|
|
Packit |
90a5c9 |
* merge - merge this text onto any existing header of this same,
|
|
Packit |
90a5c9 |
* avoiding duplicate values
|
|
Packit |
90a5c9 |
* unset - remove this header
|
|
Packit |
90a5c9 |
* edit - transform the header value according to a regexp
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Where action is unset, the third argument (value) should not be given.
|
|
Packit |
90a5c9 |
* The header name can include the colon, or not.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* The Header and RequestHeader directives can only be used where allowed
|
|
Packit |
90a5c9 |
* by the FileInfo override.
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* When the request is processed, the header directives are processed in
|
|
Packit |
90a5c9 |
* this order: firstly, the main server, then the virtual server handling
|
|
Packit |
90a5c9 |
* this request (if any), then any <Directory> sections (working downwards
|
|
Packit |
90a5c9 |
* from the root dir), then an <Location> sections (working down from
|
|
Packit |
90a5c9 |
* shortest URL component), the any <File> sections. This order is
|
|
Packit |
90a5c9 |
* important if any 'set' or 'unset' actions are used. For example,
|
|
Packit |
90a5c9 |
* the following two directives have different effect if applied in
|
|
Packit |
90a5c9 |
* the reverse order:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Header append Author "John P. Doe"
|
|
Packit |
90a5c9 |
* Header unset Author
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Examples:
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* To set the "Author" header, use
|
|
Packit |
90a5c9 |
* Header add Author "John P. Doe"
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* To remove a header:
|
|
Packit |
90a5c9 |
* Header unset Author
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr.h"
|
|
Packit |
90a5c9 |
#include "apr_lib.h"
|
|
Packit |
90a5c9 |
#include "apr_strings.h"
|
|
Packit |
90a5c9 |
#include "apr_buckets.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "apr_hash.h"
|
|
Packit |
90a5c9 |
#define APR_WANT_STRFUNC
|
|
Packit |
90a5c9 |
#include "apr_want.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "httpd.h"
|
|
Packit |
90a5c9 |
#include "http_config.h"
|
|
Packit |
90a5c9 |
#include "http_request.h"
|
|
Packit |
90a5c9 |
#include "http_log.h"
|
|
Packit |
90a5c9 |
#include "util_filter.h"
|
|
Packit |
90a5c9 |
#include "http_protocol.h"
|
|
Packit |
90a5c9 |
#include "ap_expr.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_ssl.h" /* for the ssl_var_lookup optional function defn */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* format_tag_hash is initialized during pre-config */
|
|
Packit |
90a5c9 |
static apr_hash_t *format_tag_hash;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef enum {
|
|
Packit |
90a5c9 |
hdr_add = 'a', /* add header (could mean multiple hdrs) */
|
|
Packit |
90a5c9 |
hdr_set = 's', /* set (replace old value) */
|
|
Packit |
90a5c9 |
hdr_append = 'm', /* append (merge into any old value) */
|
|
Packit |
90a5c9 |
hdr_merge = 'g', /* merge (merge, but avoid duplicates) */
|
|
Packit |
90a5c9 |
hdr_unset = 'u', /* unset header */
|
|
Packit |
90a5c9 |
hdr_echo = 'e', /* echo headers from request to response */
|
|
Packit |
90a5c9 |
hdr_edit = 'r', /* change value by regexp, match once */
|
|
Packit |
90a5c9 |
hdr_edit_r = 'R', /* change value by regexp, everymatch */
|
|
Packit |
90a5c9 |
hdr_setifempty = 'i', /* set value if header not already present*/
|
|
Packit |
90a5c9 |
hdr_note = 'n' /* set value of header in a note */
|
|
Packit |
90a5c9 |
} hdr_actions;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* magic cmd->info values
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static char hdr_in = '0'; /* RequestHeader */
|
|
Packit |
90a5c9 |
static char hdr_out_onsuccess = '1'; /* Header onsuccess */
|
|
Packit |
90a5c9 |
static char hdr_out_always = '2'; /* Header always */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Callback function type. */
|
|
Packit |
90a5c9 |
typedef const char *format_tag_fn(request_rec *r, char *a);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* There is an array of struct format_tag per Header/RequestHeader
|
|
Packit |
90a5c9 |
* config directive
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
format_tag_fn *func;
|
|
Packit |
90a5c9 |
char *arg;
|
|
Packit |
90a5c9 |
} format_tag;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* 'Magic' condition_var value to run action in post_read_request */
|
|
Packit |
90a5c9 |
static const char* condition_early = "early";
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* There is one "header_entry" per Header/RequestHeader config directive
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
hdr_actions action;
|
|
Packit |
90a5c9 |
const char *header;
|
|
Packit |
90a5c9 |
apr_array_header_t *ta; /* Array of format_tag structs */
|
|
Packit |
90a5c9 |
ap_regex_t *regex;
|
|
Packit |
90a5c9 |
const char *condition_var;
|
|
Packit |
90a5c9 |
const char *subs;
|
|
Packit |
90a5c9 |
ap_expr_info_t *expr;
|
|
Packit |
90a5c9 |
ap_expr_info_t *expr_out;
|
|
Packit |
90a5c9 |
} header_entry;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* echo_do is used for Header echo to iterate through the request headers*/
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
request_rec *r;
|
|
Packit |
90a5c9 |
header_entry *hdr;
|
|
Packit |
90a5c9 |
} echo_do;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* edit_do is used for Header edit to iterate through the request headers */
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
request_rec *r;
|
|
Packit |
90a5c9 |
header_entry *hdr;
|
|
Packit |
90a5c9 |
apr_table_t *t;
|
|
Packit |
90a5c9 |
} edit_do;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* headers_conf is our per-module configuration. This is used as both
|
|
Packit |
90a5c9 |
* a per-dir and per-server config
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
apr_array_header_t *fixup_in;
|
|
Packit |
90a5c9 |
apr_array_header_t *fixup_out;
|
|
Packit |
90a5c9 |
apr_array_header_t *fixup_err;
|
|
Packit |
90a5c9 |
} headers_conf;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
module AP_MODULE_DECLARE_DATA headers_module;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Pointer to ssl_var_lookup, if available. */
|
|
Packit |
90a5c9 |
static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *header_ssl_lookup = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Tag formatting functions
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static const char *constant_item(request_rec *r, char *stuff)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return stuff;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static const char *header_request_duration(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "D=%" APR_TIME_T_FMT,
|
|
Packit |
90a5c9 |
(apr_time_now() - r->request_time));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static const char *header_request_time(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "t=%" APR_TIME_T_FMT, r->request_time);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* unwrap_header returns HDR with any newlines converted into
|
|
Packit |
90a5c9 |
* whitespace if necessary. */
|
|
Packit |
90a5c9 |
static const char *unwrap_header(apr_pool_t *p, const char *hdr)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (ap_strchr_c(hdr, APR_ASCII_LF) || ap_strchr_c(hdr, APR_ASCII_CR)) {
|
|
Packit |
90a5c9 |
char *ptr;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
hdr = ptr = apr_pstrdup(p, hdr);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
do {
|
|
Packit |
90a5c9 |
if (*ptr == APR_ASCII_LF || *ptr == APR_ASCII_CR)
|
|
Packit |
90a5c9 |
*ptr = APR_ASCII_BLANK;
|
|
Packit |
90a5c9 |
} while (*ptr++);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return hdr;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *header_request_env_var(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *s = apr_table_get(r->subprocess_env,a);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (s)
|
|
Packit |
90a5c9 |
return unwrap_header(r->pool, s);
|
|
Packit |
90a5c9 |
else
|
|
Packit |
90a5c9 |
return "(null)";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *header_request_ssl_var(request_rec *r, char *name)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (header_ssl_lookup) {
|
|
Packit |
90a5c9 |
const char *val = header_ssl_lookup(r->pool, r->server,
|
|
Packit |
90a5c9 |
r->connection, r, name);
|
|
Packit |
90a5c9 |
if (val && val[0])
|
|
Packit |
90a5c9 |
return unwrap_header(r->pool, val);
|
|
Packit |
90a5c9 |
else
|
|
Packit |
90a5c9 |
return "(null)";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return "(null)";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *header_request_loadavg(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_loadavg_t t;
|
|
Packit |
90a5c9 |
ap_get_loadavg(&t);
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "l=%.2f/%.2f/%.2f", t.loadavg,
|
|
Packit |
90a5c9 |
t.loadavg5, t.loadavg15);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *header_request_idle(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_sload_t t;
|
|
Packit |
90a5c9 |
ap_get_sload(&t);
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "i=%d", t.idle);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *header_request_busy(request_rec *r, char *a)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_sload_t t;
|
|
Packit |
90a5c9 |
ap_get_sload(&t);
|
|
Packit |
90a5c9 |
return apr_psprintf(r->pool, "b=%d", t.busy);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Config routines
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *create_headers_dir_config(apr_pool_t *p, char *d)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *conf = apr_pcalloc(p, sizeof(*conf));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
conf->fixup_in = apr_array_make(p, 2, sizeof(header_entry));
|
|
Packit |
90a5c9 |
conf->fixup_out = apr_array_make(p, 2, sizeof(header_entry));
|
|
Packit |
90a5c9 |
conf->fixup_err = apr_array_make(p, 2, sizeof(header_entry));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return conf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void *merge_headers_config(apr_pool_t *p, void *basev, void *overridesv)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *newconf = apr_pcalloc(p, sizeof(*newconf));
|
|
Packit |
90a5c9 |
headers_conf *base = basev;
|
|
Packit |
90a5c9 |
headers_conf *overrides = overridesv;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
newconf->fixup_in = apr_array_append(p, base->fixup_in,
|
|
Packit |
90a5c9 |
overrides->fixup_in);
|
|
Packit |
90a5c9 |
newconf->fixup_out = apr_array_append(p, base->fixup_out,
|
|
Packit |
90a5c9 |
overrides->fixup_out);
|
|
Packit |
90a5c9 |
newconf->fixup_err = apr_array_append(p, base->fixup_err,
|
|
Packit |
90a5c9 |
overrides->fixup_err);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return newconf;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static char *parse_misc_string(apr_pool_t *p, format_tag *tag, const char **sa)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *s;
|
|
Packit |
90a5c9 |
char *d;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tag->func = constant_item;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
s = *sa;
|
|
Packit |
90a5c9 |
while (*s && *s != '%') {
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* This might allocate a few chars extra if there's a backslash
|
|
Packit |
90a5c9 |
* escape in the format string.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
tag->arg = apr_palloc(p, s - *sa + 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
d = tag->arg;
|
|
Packit |
90a5c9 |
s = *sa;
|
|
Packit |
90a5c9 |
while (*s && *s != '%') {
|
|
Packit |
90a5c9 |
if (*s != '\\') {
|
|
Packit |
90a5c9 |
*d++ = *s++;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
switch (*s) {
|
|
Packit |
90a5c9 |
case '\\':
|
|
Packit |
90a5c9 |
*d++ = '\\';
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 'r':
|
|
Packit |
90a5c9 |
*d++ = '\r';
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 'n':
|
|
Packit |
90a5c9 |
*d++ = '\n';
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 't':
|
|
Packit |
90a5c9 |
*d++ = '\t';
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* copy verbatim */
|
|
Packit |
90a5c9 |
*d++ = '\\';
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Allow the loop to deal with this *s in the normal
|
|
Packit |
90a5c9 |
* fashion so that it handles end of string etc.
|
|
Packit |
90a5c9 |
* properly.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
*d = '\0';
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*sa = s;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static char *parse_format_tag(apr_pool_t *p, format_tag *tag, const char **sa)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *s = *sa;
|
|
Packit |
90a5c9 |
const char * (*tag_handler)(request_rec *,char *);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Handle string literal/conditionals */
|
|
Packit |
90a5c9 |
if (*s != '%') {
|
|
Packit |
90a5c9 |
return parse_misc_string(p, tag, sa);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
s++; /* skip the % */
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Pass through %% or % at end of string as % */
|
|
Packit |
90a5c9 |
if ((*s == '%') || (*s == '\0')) {
|
|
Packit |
90a5c9 |
tag->func = constant_item;
|
|
Packit |
90a5c9 |
tag->arg = "%";
|
|
Packit |
90a5c9 |
if (*s)
|
|
Packit |
90a5c9 |
s++;
|
|
Packit |
90a5c9 |
*sa = s;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tag->arg = "\0";
|
|
Packit |
90a5c9 |
/* grab the argument if there is one */
|
|
Packit |
90a5c9 |
if (*s == '{') {
|
|
Packit |
90a5c9 |
++s;
|
|
Packit |
90a5c9 |
tag->arg = ap_getword(p,&s,'}');
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tag_handler = (const char * (*)(request_rec *,char *))apr_hash_get(format_tag_hash, s++, 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!tag_handler) {
|
|
Packit |
90a5c9 |
char dummy[2];
|
|
Packit |
90a5c9 |
dummy[0] = s[-1];
|
|
Packit |
90a5c9 |
dummy[1] = '\0';
|
|
Packit |
90a5c9 |
return apr_pstrcat(p, "Unrecognized header format %", dummy, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
tag->func = tag_handler;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*sa = s;
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* A format string consists of white space, text and optional format
|
|
Packit |
90a5c9 |
* tags in any order. E.g.,
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Header add MyHeader "Free form text %D %t more text"
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Decompose the format string into its tags. Each tag (struct format_tag)
|
|
Packit |
90a5c9 |
* contains a pointer to the function used to format the tag. Then save each
|
|
Packit |
90a5c9 |
* tag in the tag array anchored in the header_entry.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static char *parse_format_string(cmd_parms *cmd, header_entry *hdr, const char *s)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_pool_t *p = cmd->pool;
|
|
Packit |
90a5c9 |
char *res;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* No string to parse with unset and echo commands */
|
|
Packit |
90a5c9 |
if (hdr->action == hdr_unset || hdr->action == hdr_echo) {
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Tags are in the replacement value for edit */
|
|
Packit |
90a5c9 |
else if (hdr->action == hdr_edit || hdr->action == hdr_edit_r ) {
|
|
Packit |
90a5c9 |
s = hdr->subs;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strncmp(s, "expr=", 5)) {
|
|
Packit |
90a5c9 |
const char *err;
|
|
Packit |
90a5c9 |
hdr->expr_out = ap_expr_parse_cmd(cmd, s+5,
|
|
Packit |
90a5c9 |
AP_EXPR_FLAG_STRING_RESULT,
|
|
Packit |
90a5c9 |
&err, NULL);
|
|
Packit |
90a5c9 |
if (err) {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->pool,
|
|
Packit |
90a5c9 |
"Can't parse value expression : ", err, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
hdr->ta = apr_array_make(p, 10, sizeof(format_tag));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (*s) {
|
|
Packit |
90a5c9 |
if ((res = parse_format_tag(p, (format_tag *) apr_array_push(hdr->ta), &s))) {
|
|
Packit |
90a5c9 |
return res;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* handle RequestHeader and Header directive */
|
|
Packit |
90a5c9 |
static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd,
|
|
Packit |
90a5c9 |
void *indirconf,
|
|
Packit |
90a5c9 |
const char *action,
|
|
Packit |
90a5c9 |
const char *hdr,
|
|
Packit |
90a5c9 |
const char *value,
|
|
Packit |
90a5c9 |
const char *subs,
|
|
Packit |
90a5c9 |
const char *envclause)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = indirconf;
|
|
Packit |
90a5c9 |
const char *condition_var = NULL;
|
|
Packit |
90a5c9 |
const char *colon;
|
|
Packit |
90a5c9 |
header_entry *new;
|
|
Packit |
90a5c9 |
ap_expr_info_t *expr = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_array_header_t *fixup = (cmd->info == &hdr_in)
|
|
Packit |
90a5c9 |
? dirconf->fixup_in : (cmd->info == &hdr_out_always)
|
|
Packit |
90a5c9 |
? dirconf->fixup_err
|
|
Packit |
90a5c9 |
: dirconf->fixup_out;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
new = (header_entry *) apr_array_push(fixup);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!strcasecmp(action, "set"))
|
|
Packit |
90a5c9 |
new->action = hdr_set;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "setifempty"))
|
|
Packit |
90a5c9 |
new->action = hdr_setifempty;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "add"))
|
|
Packit |
90a5c9 |
new->action = hdr_add;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "append"))
|
|
Packit |
90a5c9 |
new->action = hdr_append;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "merge"))
|
|
Packit |
90a5c9 |
new->action = hdr_merge;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "unset"))
|
|
Packit |
90a5c9 |
new->action = hdr_unset;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "echo"))
|
|
Packit |
90a5c9 |
new->action = hdr_echo;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "edit"))
|
|
Packit |
90a5c9 |
new->action = hdr_edit;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "edit*"))
|
|
Packit |
90a5c9 |
new->action = hdr_edit_r;
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "note"))
|
|
Packit |
90a5c9 |
new->action = hdr_note;
|
|
Packit |
90a5c9 |
else
|
|
Packit |
90a5c9 |
return "first argument must be 'add', 'set', 'setifempty', 'append', 'merge', "
|
|
Packit |
90a5c9 |
"'unset', 'echo', 'note', 'edit', or 'edit*'.";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (new->action == hdr_edit || new->action == hdr_edit_r) {
|
|
Packit |
90a5c9 |
if (subs == NULL) {
|
|
Packit |
90a5c9 |
return "Header edit requires a match and a substitution";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
new->regex = ap_pregcomp(cmd->pool, value, AP_REG_EXTENDED);
|
|
Packit |
90a5c9 |
if (new->regex == NULL) {
|
|
Packit |
90a5c9 |
return "Header edit regex could not be compiled";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
new->subs = subs;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* there's no subs, so envclause is really that argument */
|
|
Packit |
90a5c9 |
if (envclause != NULL) {
|
|
Packit |
90a5c9 |
return "Too many arguments to directive";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
envclause = subs;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (new->action == hdr_unset) {
|
|
Packit |
90a5c9 |
if (value) {
|
|
Packit |
90a5c9 |
if (envclause) {
|
|
Packit |
90a5c9 |
return "header unset takes two arguments";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
envclause = value;
|
|
Packit |
90a5c9 |
value = NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (new->action == hdr_echo) {
|
|
Packit |
90a5c9 |
ap_regex_t *regex;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (value) {
|
|
Packit |
90a5c9 |
if (envclause) {
|
|
Packit |
90a5c9 |
return "Header echo takes two arguments";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
envclause = value;
|
|
Packit |
90a5c9 |
value = NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (cmd->info != &hdr_out_onsuccess && cmd->info != &hdr_out_always)
|
|
Packit |
90a5c9 |
return "Header echo only valid on Header "
|
|
Packit |
90a5c9 |
"directives";
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
regex = ap_pregcomp(cmd->pool, hdr, AP_REG_EXTENDED | AP_REG_NOSUB);
|
|
Packit |
90a5c9 |
if (regex == NULL) {
|
|
Packit |
90a5c9 |
return "Header echo regex could not be compiled";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
new->regex = regex;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!value)
|
|
Packit |
90a5c9 |
return "Header requires three arguments";
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Handle the envclause on Header */
|
|
Packit |
90a5c9 |
if (envclause != NULL) {
|
|
Packit |
90a5c9 |
if (strcasecmp(envclause, "early") == 0) {
|
|
Packit |
90a5c9 |
condition_var = condition_early;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (strncasecmp(envclause, "env=", 4) == 0) {
|
|
Packit |
90a5c9 |
if ((envclause[4] == '\0')
|
|
Packit |
90a5c9 |
|| ((envclause[4] == '!') && (envclause[5] == '\0'))) {
|
|
Packit |
90a5c9 |
return "error: missing environment variable name. "
|
|
Packit |
90a5c9 |
"envclause should be in the form env=envar ";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
condition_var = envclause + 4;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (strncasecmp(envclause, "expr=", 5) == 0) {
|
|
Packit |
90a5c9 |
const char *err = NULL;
|
|
Packit |
90a5c9 |
expr = ap_expr_parse_cmd(cmd, envclause + 5, 0, &err, NULL);
|
|
Packit |
90a5c9 |
if (err) {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->pool,
|
|
Packit |
90a5c9 |
"Can't parse envclause/expression: ", err,
|
|
Packit |
90a5c9 |
NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->pool, "Unknown parameter: ", envclause,
|
|
Packit |
90a5c9 |
NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if ((colon = ap_strchr_c(hdr, ':'))) {
|
|
Packit |
90a5c9 |
hdr = apr_pstrmemdup(cmd->pool, hdr, colon-hdr);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
new->header = hdr;
|
|
Packit |
90a5c9 |
new->condition_var = condition_var;
|
|
Packit |
90a5c9 |
new->expr = expr;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return parse_format_string(cmd, new, value);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Handle all (xxx)Header directives */
|
|
Packit |
90a5c9 |
static const char *header_cmd(cmd_parms *cmd, void *indirconf,
|
|
Packit |
90a5c9 |
const char *args)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
const char *action;
|
|
Packit |
90a5c9 |
const char *hdr;
|
|
Packit |
90a5c9 |
const char *val;
|
|
Packit |
90a5c9 |
const char *envclause;
|
|
Packit |
90a5c9 |
const char *subs;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
action = ap_getword_conf(cmd->temp_pool, &args);
|
|
Packit |
90a5c9 |
if (cmd->info == &hdr_out_onsuccess) {
|
|
Packit |
90a5c9 |
if (!strcasecmp(action, "always")) {
|
|
Packit |
90a5c9 |
cmd->info = &hdr_out_always;
|
|
Packit |
90a5c9 |
action = ap_getword_conf(cmd->temp_pool, &args);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!strcasecmp(action, "onsuccess")) {
|
|
Packit |
90a5c9 |
action = ap_getword_conf(cmd->temp_pool, &args);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
hdr = ap_getword_conf(cmd->pool, &args);
|
|
Packit |
90a5c9 |
val = *args ? ap_getword_conf(cmd->pool, &args) : NULL;
|
|
Packit |
90a5c9 |
subs = *args ? ap_getword_conf(cmd->pool, &args) : NULL;
|
|
Packit |
90a5c9 |
envclause = *args ? ap_getword_conf(cmd->pool, &args) : NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (*args) {
|
|
Packit |
90a5c9 |
return apr_pstrcat(cmd->pool, cmd->cmd->name,
|
|
Packit |
90a5c9 |
" has too many arguments", NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return header_inout_cmd(cmd, indirconf, action, hdr, val, subs, envclause);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Process the tags in the format string. Tags may be format specifiers
|
|
Packit |
90a5c9 |
* (%D, %t, etc.), whitespace or text strings. For each tag, run the handler
|
|
Packit |
90a5c9 |
* (formatter) specific to the tag. Handlers return text strings.
|
|
Packit |
90a5c9 |
* Concatenate the return from each handler into one string that is
|
|
Packit |
90a5c9 |
* returned from this call.
|
|
Packit |
90a5c9 |
* If the original value was prefixed with "expr=", processing is
|
|
Packit |
90a5c9 |
* handled instead by ap_expr.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static char* process_tags(header_entry *hdr, request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
const char *s;
|
|
Packit |
90a5c9 |
char *str = NULL;
|
|
Packit |
90a5c9 |
format_tag *tag = NULL;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (hdr->expr_out) {
|
|
Packit |
90a5c9 |
const char *err;
|
|
Packit |
90a5c9 |
const char *val;
|
|
Packit |
90a5c9 |
val = ap_expr_str_exec(r, hdr->expr_out, &err;;
|
|
Packit |
90a5c9 |
if (err) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02557)
|
|
Packit |
90a5c9 |
"Can't evaluate value expression: %s", err);
|
|
Packit |
90a5c9 |
return "";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return apr_pstrdup(r->pool, val);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tag = (format_tag*) hdr->ta->elts;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < hdr->ta->nelts; i++) {
|
|
Packit |
90a5c9 |
s = tag[i].func(r, tag[i].arg);
|
|
Packit |
90a5c9 |
if (str == NULL)
|
|
Packit |
90a5c9 |
str = apr_pstrdup(r->pool, s);
|
|
Packit |
90a5c9 |
else
|
|
Packit |
90a5c9 |
str = apr_pstrcat(r->pool, str, s, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return str ? str : "";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static const char *process_regexp(header_entry *hdr, const char *value,
|
|
Packit |
90a5c9 |
request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_regmatch_t pmatch[AP_MAX_REG_MATCH];
|
|
Packit |
90a5c9 |
const char *subs;
|
|
Packit |
90a5c9 |
const char *remainder;
|
|
Packit |
90a5c9 |
char *ret;
|
|
Packit |
90a5c9 |
int diffsz;
|
|
Packit |
90a5c9 |
if (ap_regexec(hdr->regex, value, AP_MAX_REG_MATCH, pmatch, 0)) {
|
|
Packit |
90a5c9 |
/* no match, nothing to do */
|
|
Packit |
90a5c9 |
return value;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Process tags in the input string rather than the resulting
|
|
Packit |
90a5c9 |
* substitution to avoid surprises
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
subs = ap_pregsub(r->pool, process_tags(hdr, r), value, AP_MAX_REG_MATCH, pmatch);
|
|
Packit |
90a5c9 |
if (subs == NULL)
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
diffsz = strlen(subs) - (pmatch[0].rm_eo - pmatch[0].rm_so);
|
|
Packit |
90a5c9 |
if (hdr->action == hdr_edit) {
|
|
Packit |
90a5c9 |
remainder = value + pmatch[0].rm_eo;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else { /* recurse to edit multiple matches if applicable */
|
|
Packit |
90a5c9 |
remainder = process_regexp(hdr, value + pmatch[0].rm_eo, r);
|
|
Packit |
90a5c9 |
if (remainder == NULL)
|
|
Packit |
90a5c9 |
return NULL;
|
|
Packit |
90a5c9 |
diffsz += strlen(remainder) - strlen(value + pmatch[0].rm_eo);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ret = apr_palloc(r->pool, strlen(value) + 1 + diffsz);
|
|
Packit |
90a5c9 |
memcpy(ret, value, pmatch[0].rm_so);
|
|
Packit |
90a5c9 |
strcpy(ret + pmatch[0].rm_so, subs);
|
|
Packit |
90a5c9 |
strcat(ret, remainder);
|
|
Packit |
90a5c9 |
return ret;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int echo_header(void *v, const char *key, const char *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
edit_do *ed = v;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If the input header (key) matches the regex, echo it intact to
|
|
Packit |
90a5c9 |
* r->headers_out.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (!ap_regexec(ed->hdr->regex, key, 0, NULL, 0)) {
|
|
Packit |
90a5c9 |
apr_table_add(ed->r->headers_out, key, val);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int edit_header(void *v, const char *key, const char *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
edit_do *ed = (edit_do *)v;
|
|
Packit |
90a5c9 |
const char *repl = process_regexp(ed->hdr, val, ed->r);
|
|
Packit |
90a5c9 |
if (repl == NULL)
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_table_addn(ed->t, key, repl);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int add_them_all(void *v, const char *key, const char *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_table_t *headers = (apr_table_t *)v;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_table_addn(headers, key, val);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int do_headers_fixup(request_rec *r, apr_table_t *headers,
|
|
Packit |
90a5c9 |
apr_array_header_t *fixup, int early)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
echo_do v;
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
const char *val;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < fixup->nelts; ++i) {
|
|
Packit |
90a5c9 |
header_entry *hdr = &((header_entry *) (fixup->elts))[i];
|
|
Packit |
90a5c9 |
const char *envar = hdr->condition_var;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* ignore early headers in late calls */
|
|
Packit |
90a5c9 |
if (!early && (envar == condition_early)) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* ignore late headers in early calls */
|
|
Packit |
90a5c9 |
else if (early && (envar != condition_early)) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Do we have an expression to evaluate? */
|
|
Packit |
90a5c9 |
else if (hdr->expr != NULL) {
|
|
Packit |
90a5c9 |
const char *err = NULL;
|
|
Packit |
90a5c9 |
int eval = ap_expr_exec(r, hdr->expr, &err;;
|
|
Packit |
90a5c9 |
if (err) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01501)
|
|
Packit |
90a5c9 |
"Failed to evaluate expression (%s) - ignoring",
|
|
Packit |
90a5c9 |
err);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!eval) {
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
/* Have any conditional envar-controlled Header processing to do? */
|
|
Packit |
90a5c9 |
else if (envar && !early) {
|
|
Packit |
90a5c9 |
if (*envar != '!') {
|
|
Packit |
90a5c9 |
if (apr_table_get(r->subprocess_env, envar) == NULL)
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
if (apr_table_get(r->subprocess_env, &envar[1]) != NULL)
|
|
Packit |
90a5c9 |
continue;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
switch (hdr->action) {
|
|
Packit |
90a5c9 |
case hdr_add:
|
|
Packit |
90a5c9 |
apr_table_addn(headers, hdr->header, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_append:
|
|
Packit |
90a5c9 |
apr_table_mergen(headers, hdr->header, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_merge:
|
|
Packit |
90a5c9 |
val = apr_table_get(headers, hdr->header);
|
|
Packit |
90a5c9 |
if (val == NULL) {
|
|
Packit |
90a5c9 |
apr_table_addn(headers, hdr->header, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
} else {
|
|
Packit |
90a5c9 |
char *new_val = process_tags(hdr, r);
|
|
Packit |
90a5c9 |
apr_size_t new_val_len = strlen(new_val);
|
|
Packit |
90a5c9 |
int tok_found = 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* modified version of logic in ap_get_token() */
|
|
Packit |
90a5c9 |
while (*val) {
|
|
Packit |
90a5c9 |
const char *tok_start;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (apr_isspace(*val))
|
|
Packit |
90a5c9 |
++val;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
tok_start = val;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (*val && *val != ',') {
|
|
Packit |
90a5c9 |
if (*val++ == '"')
|
|
Packit |
90a5c9 |
while (*val)
|
|
Packit |
90a5c9 |
if (*val++ == '"')
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (new_val_len == (apr_size_t)(val - tok_start)
|
|
Packit |
90a5c9 |
&& !strncmp(tok_start, new_val, new_val_len)) {
|
|
Packit |
90a5c9 |
tok_found = 1;
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (*val)
|
|
Packit |
90a5c9 |
++val;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!tok_found) {
|
|
Packit |
90a5c9 |
apr_table_mergen(headers, hdr->header, new_val);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_set:
|
|
Packit |
90a5c9 |
if (!strcasecmp(hdr->header, "Content-Type")) {
|
|
Packit |
90a5c9 |
ap_set_content_type(r, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_table_setn(headers, hdr->header, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_setifempty:
|
|
Packit |
90a5c9 |
if (NULL == apr_table_get(headers, hdr->header)) {
|
|
Packit |
90a5c9 |
if (!strcasecmp(hdr->header, "Content-Type")) {
|
|
Packit |
90a5c9 |
ap_set_content_type(r, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_table_setn(headers, hdr->header, process_tags(hdr, r));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_unset:
|
|
Packit |
90a5c9 |
apr_table_unset(headers, hdr->header);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_echo:
|
|
Packit |
90a5c9 |
v.r = r;
|
|
Packit |
90a5c9 |
v.hdr = hdr;
|
|
Packit |
90a5c9 |
apr_table_do(echo_header, &v, r->headers_in, NULL);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_edit:
|
|
Packit |
90a5c9 |
case hdr_edit_r:
|
|
Packit |
90a5c9 |
if (!strcasecmp(hdr->header, "Content-Type") && r->content_type) {
|
|
Packit |
90a5c9 |
const char *repl = process_regexp(hdr, r->content_type, r);
|
|
Packit |
90a5c9 |
if (repl == NULL)
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
ap_set_content_type(r, repl);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (apr_table_get(headers, hdr->header)) {
|
|
Packit |
90a5c9 |
edit_do ed;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ed.r = r;
|
|
Packit |
90a5c9 |
ed.hdr = hdr;
|
|
Packit |
90a5c9 |
ed.t = apr_table_make(r->pool, 5);
|
|
Packit |
90a5c9 |
if (!apr_table_do(edit_header, (void *) &ed, headers,
|
|
Packit |
90a5c9 |
hdr->header, NULL))
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
apr_table_unset(headers, hdr->header);
|
|
Packit |
90a5c9 |
apr_table_do(add_them_all, (void *) headers, ed.t, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case hdr_note:
|
|
Packit |
90a5c9 |
apr_table_setn(r->notes, process_tags(hdr, r), apr_table_get(headers, hdr->header));
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ap_headers_insert_output_filter(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (dirconf->fixup_out->nelts || dirconf->fixup_err->nelts) {
|
|
Packit |
90a5c9 |
ap_add_output_filter("FIXUP_HEADERS_OUT", NULL, r, r->connection);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Make sure our error-path filter is in place.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static void ap_headers_insert_error_filter(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (dirconf->fixup_err->nelts) {
|
|
Packit |
90a5c9 |
ap_add_output_filter("FIXUP_HEADERS_ERR", NULL, r, r->connection);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t ap_headers_output_filter(ap_filter_t *f,
|
|
Packit |
90a5c9 |
apr_bucket_brigade *in)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = ap_get_module_config(f->r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_TRACE2, 0, f->r->server, APLOGNO(01502)
|
|
Packit |
90a5c9 |
"headers: ap_headers_output_filter()");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* do the fixup */
|
|
Packit |
90a5c9 |
do_headers_fixup(f->r, f->r->err_headers_out, dirconf->fixup_err, 0);
|
|
Packit |
90a5c9 |
do_headers_fixup(f->r, f->r->headers_out, dirconf->fixup_out, 0);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* remove ourselves from the filter chain */
|
|
Packit |
90a5c9 |
ap_remove_output_filter(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* send the data up the stack */
|
|
Packit |
90a5c9 |
return ap_pass_brigade(f->next,in);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Make sure we propagate any "Header always" settings on the error
|
|
Packit |
90a5c9 |
* path through http_protocol.c.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
static apr_status_t ap_headers_error_filter(ap_filter_t *f,
|
|
Packit |
90a5c9 |
apr_bucket_brigade *in)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dirconf = ap_get_module_config(f->r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, f->r->server, APLOGNO(01503)
|
|
Packit |
90a5c9 |
"headers: ap_headers_error_filter()");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Add any header fields defined by "Header always" to r->err_headers_out.
|
|
Packit |
90a5c9 |
* Server-wide first, then per-directory to allow overriding.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
do_headers_fixup(f->r, f->r->err_headers_out, dirconf->fixup_err, 0);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* We've done our bit; remove ourself from the filter chain so there's
|
|
Packit |
90a5c9 |
* no possibility we'll be called again.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
ap_remove_output_filter(f);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/*
|
|
Packit |
90a5c9 |
* Pass the buck. (euro?)
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
return ap_pass_brigade(f->next, in);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t ap_headers_fixup(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* do the fixup */
|
|
Packit |
90a5c9 |
if (dirconf->fixup_in->nelts) {
|
|
Packit |
90a5c9 |
do_headers_fixup(r, r->headers_in, dirconf->fixup_in, 0);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
static apr_status_t ap_headers_early(request_rec *r)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
headers_conf *dirconf = ap_get_module_config(r->per_dir_config,
|
|
Packit |
90a5c9 |
&headers_module);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* do the fixup */
|
|
Packit |
90a5c9 |
if (dirconf->fixup_in->nelts) {
|
|
Packit |
90a5c9 |
if (!do_headers_fixup(r, r->headers_in, dirconf->fixup_in, 1))
|
|
Packit |
90a5c9 |
goto err;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (dirconf->fixup_err->nelts) {
|
|
Packit |
90a5c9 |
if (!do_headers_fixup(r, r->err_headers_out, dirconf->fixup_err, 1))
|
|
Packit |
90a5c9 |
goto err;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (dirconf->fixup_out->nelts) {
|
|
Packit |
90a5c9 |
if (!do_headers_fixup(r, r->headers_out, dirconf->fixup_out, 1))
|
|
Packit |
90a5c9 |
goto err;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return DECLINED;
|
|
Packit |
90a5c9 |
err:
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(01504)
|
|
Packit |
90a5c9 |
"Regular expression replacement failed (replacement too long?)");
|
|
Packit |
90a5c9 |
return HTTP_INTERNAL_SERVER_ERROR;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const command_rec headers_cmds[] =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
AP_INIT_RAW_ARGS("Header", header_cmd, &hdr_out_onsuccess, OR_FILEINFO,
|
|
Packit |
90a5c9 |
"an optional condition, an action, header and value "
|
|
Packit |
90a5c9 |
"followed by optional env clause"),
|
|
Packit |
90a5c9 |
AP_INIT_RAW_ARGS("RequestHeader", header_cmd, &hdr_in, OR_FILEINFO,
|
|
Packit |
90a5c9 |
"an action, header and value followed by optional env "
|
|
Packit |
90a5c9 |
"clause"),
|
|
Packit |
90a5c9 |
{NULL}
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_format_tag_handler(const char *tag,
|
|
Packit |
90a5c9 |
format_tag_fn *tag_handler)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_hash_set(format_tag_hash, tag, 1, tag_handler);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int header_pre_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
format_tag_hash = apr_hash_make(p);
|
|
Packit |
90a5c9 |
register_format_tag_handler("D", header_request_duration);
|
|
Packit |
90a5c9 |
register_format_tag_handler("t", header_request_time);
|
|
Packit |
90a5c9 |
register_format_tag_handler("e", header_request_env_var);
|
|
Packit |
90a5c9 |
register_format_tag_handler("s", header_request_ssl_var);
|
|
Packit |
90a5c9 |
register_format_tag_handler("l", header_request_loadavg);
|
|
Packit |
90a5c9 |
register_format_tag_handler("i", header_request_idle);
|
|
Packit |
90a5c9 |
register_format_tag_handler("b", header_request_busy);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int header_post_config(apr_pool_t *pconf, apr_pool_t *plog,
|
|
Packit |
90a5c9 |
apr_pool_t *ptemp, server_rec *s)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
header_ssl_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
|
|
Packit |
90a5c9 |
return OK;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void register_hooks(apr_pool_t *p)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_register_output_filter("FIXUP_HEADERS_OUT", ap_headers_output_filter,
|
|
Packit |
90a5c9 |
NULL, AP_FTYPE_CONTENT_SET);
|
|
Packit |
90a5c9 |
ap_register_output_filter("FIXUP_HEADERS_ERR", ap_headers_error_filter,
|
|
Packit |
90a5c9 |
NULL, AP_FTYPE_CONTENT_SET);
|
|
Packit |
90a5c9 |
ap_hook_pre_config(header_pre_config,NULL,NULL,APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_post_config(header_post_config,NULL,NULL,APR_HOOK_MIDDLE);
|
|
Packit |
90a5c9 |
ap_hook_insert_filter(ap_headers_insert_output_filter, NULL, NULL, APR_HOOK_LAST);
|
|
Packit |
90a5c9 |
ap_hook_insert_error_filter(ap_headers_insert_error_filter,
|
|
Packit |
90a5c9 |
NULL, NULL, APR_HOOK_LAST);
|
|
Packit |
90a5c9 |
ap_hook_fixups(ap_headers_fixup, NULL, NULL, APR_HOOK_LAST);
|
|
Packit |
90a5c9 |
ap_hook_post_read_request(ap_headers_early, NULL, NULL, APR_HOOK_FIRST);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
AP_DECLARE_MODULE(headers) =
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
STANDARD20_MODULE_STUFF,
|
|
Packit |
90a5c9 |
create_headers_dir_config, /* dir config creater */
|
|
Packit |
90a5c9 |
merge_headers_config, /* dir merger --- default is to override */
|
|
Packit |
90a5c9 |
NULL, /* server config */
|
|
Packit |
90a5c9 |
NULL, /* merge server configs */
|
|
Packit |
90a5c9 |
headers_cmds, /* command apr_table_t */
|
|
Packit |
90a5c9 |
register_hooks /* register hooks */
|
|
Packit |
90a5c9 |
};
|