mod_setenvif.c revision ef5650b61a8e35f3cc93ec07e73efc17ea329894
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed 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.
*/
/*
* Set environment variables based on matching request headers or
* attributes against regex strings
*
* Paul Sutton <paul@ukweb.com> 27 Oct 1996
* Based on mod_browser by Alexei Kosut <akosut@organic.com>
*/
/*
* Used to set environment variables based on the incoming request headers,
* or some selected other attributes of the request (e.g., the remote host
* name).
*
* Usage:
*
* SetEnvIf name regex var ...
*
* where name is either a HTTP request header name, or one of the
* special values (see below). 'name' may be a regex when it is used
* to specify an HTTP request header name. The 'value' of the header
& (or the value of the special value from below) are compared against
* the regex argument. If this is a simple string, a simple sub-string
* match is performed. Otherwise, a request expression match is
* done. If the value matches the string or regular expression, the
* environment variables listed as var ... are set. Each var can
* be in one of three formats: var, which sets the named variable
* (the value value "1"); var=value, which sets the variable to
* the given value; or !var, which unsets the variable is it has
* been previously set.
*
* Normally the strings are compared with regard to case. To ignore
* case, use the directive SetEnvIfNoCase instead.
*
* Special values for 'name' are:
*
* server_addr IP address of interface on which request arrived
* (analogous to SERVER_ADDR set in ap_add_common_vars())
* remote_host Remote host name (if available)
* remote_addr Remote IP address
* request_method Request method (GET, POST, etc)
* request_uri Requested URI
*
* Examples:
*
* To set the enviroment variable LOCALHOST if the client is the local
* machine:
*
* SetEnvIf remote_addr 127.0.0.1 LOCALHOST
*
* To set LOCAL if the client is the local host, or within our company's
* domain (192.168.10):
*
* SetEnvIf remote_addr 192.168.10. LOCAL
* SetEnvIf remote_addr 127.0.0.1 LOCALHOST
*
* This could be written as:
*
* SetEnvIf remote_addr (127.0.0.1|192.168.10.) LOCAL
*
* To set HAVE_TS if the client request contains any header beginning
* with "TS" with a value beginning with a lower case alphabet:
*
* SetEnvIf ^TS* ^[a-z].* HAVE_TS
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_strmatch.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
enum special {
};
typedef struct {
char *name; /* header name */
char *regex; /* regex to match against */
int icase; /* ignoring case? */
} sei_entry;
typedef struct {
} sei_cfg_rec;
/*
* These routines, the create- and merge-config functions, are called
* for both the server-wide and the per-directory contexts. This is
* because the different definitions are used at different times; the
* server-wide ones are used in the post-read-request phase, and the
* per-directory ones are used during the header-parse phase (after
* the URI has been mapped to a file and we have anything from the
* .htaccess file and <Directory> and <Files> containers).
*/
static void *create_setenvif_config(apr_pool_t *p)
{
return (void *) new;
}
{
return create_setenvif_config(p);
}
{
return create_setenvif_config(p);
}
{
return a;
}
/*
* any non-NULL magic constant will do... used to indicate if AP_REG_ICASE should
* be used
*/
#define ICASE_MAGIC ((void *)(&setenvif_module))
#define SEI_MAGIC_HEIRLOOM "setenvif-phase-flag"
{
/* If a Header name contains characters other than:
* -,_,[A-Z\, [a-z] and [0-9].
* assume the header name is a regular expression.
*/
(AP_REG_EXTENDED | AP_REG_NOSUB ));
return 1;
}
return 0;
}
/* If the input string does not take advantage of regular
* expression metacharacters, return a pointer to an equivalent
* string that can be searched using apr_strmatch(). (The
* returned string will often be the input string. But if
* the input string contains escaped characters, the returned
* string will be a copy with the escapes removed.)
*/
static const char *non_regex_pattern(apr_pool_t *p, const char *s)
{
const char *src = s;
int escapes_found = 0;
int in_escape = 0;
while (*src) {
switch (*src) {
case '^':
case '.':
case '$':
case '|':
case '(':
case ')':
case '[':
case ']':
case '*':
case '+':
case '?':
case '{':
case '}':
if (!in_escape) {
return NULL;
}
in_escape = 0;
break;
case '\\':
if (!in_escape) {
in_escape = 1;
escapes_found = 1;
}
else {
in_escape = 0;
}
break;
default:
if (in_escape) {
return NULL;
}
break;
}
src++;
}
if (!escapes_found) {
return s;
}
else {
src = s;
do {
if (*src == '\\') {
src++;
}
return unescaped;
}
}
{
char *regex;
const char *simple_pattern;
const char *feature;
char *var;
int i;
int beenhere = 0;
int icase;
/*
* Determine from our context into which record to put the entry.
* cmd->path == NULL means we're in server-wide context; otherwise,
* we're dealing with a per-directory setting.
*/
? (sei_cfg_rec *) mconfig
/* get regex */
if (!*regex) {
}
/*
* If we've already got a sei_entry with the same name we want to
* just copy the name pointer... so that later on we can compare
* two header names just by comparing the pointers.
*/
break;
}
}
/* if the last entry has an identical headername and regex then
* merge with it
*/
if (i < 0
/* no match, create a new entry */
simple_pattern, !icase);
" pattern could not be compiled.", NULL);
}
}
else {
" regex could not be compiled.", NULL);
}
}
}
}
}
}
}
}
else {
/* Handle fname as a regular expression.
* If fname a simple header string, identify as such
* (new->pnamereg = NULL) to avoid the overhead of searching
* through headers_in for a regex match.
*/
| (icase ? AP_REG_ICASE : 0)));
"Header name regex could not be "
"compiled.", NULL);
}
else {
}
}
}
else {
}
for ( ; ; ) {
if (!*feature) {
break;
}
beenhere++;
if (*feature) {
}
else if (*var == '!') {
}
else {
}
}
if (!beenhere) {
}
return NULL;
}
const char *args)
{
char *fname;
/* get header name */
if (!*fname) {
}
}
/*
* This routine handles the BrowserMatch* directives. It simply turns around
* and feeds them, with the appropriate embellishments, to the general-purpose
* command handler.
*/
{
}
static const command_rec setenvif_module_cmds[] =
{
"A header-name, regex and a list of variables."),
"a header-name, regex and a list of variables."),
"A browser regex and a list of variables."),
"A browser regex and a list of variables."),
{ NULL },
};
/*
* This routine gets called at two different points in request processing:
* once before the URI has been translated (during the post-read-request
* phase) and once after (during the header-parse phase). We use different
* config records for the two different calls to reduce overhead (by not
* re-doing the server-wide settings during directory processing), and
* signal which call it is by having the earlier one pass a flag to the
* later one.
*/
static int match_headers(request_rec *r)
{
const apr_table_entry_t *elts;
const char *val;
apr_size_t val_len = 0;
int i, j;
char *last_name;
}
else {
}
/* Optimize the case where a bunch of directives in a row use the
* same header. Remember we don't need to strcmp the two header
* names because we made sure the pointers were equal during
* configuration.
*/
switch (b->special_type) {
case SPECIAL_REMOTE_ADDR:
break;
case SPECIAL_SERVER_ADDR:
break;
case SPECIAL_REMOTE_HOST:
REMOTE_NAME, NULL);
break;
case SPECIAL_REQUEST_URI:
break;
case SPECIAL_REQUEST_METHOD:
break;
case SPECIAL_REQUEST_PROTOCOL:
break;
case SPECIAL_NOT:
if (b->pnamereg) {
/* Matching headers_in against a regex. Iterate through
* the headers_in until we find a match or run out of
* headers.
*/
const apr_array_header_t
}
}
}
else {
/* Not matching against a regex */
}
}
}
}
/*
* A NULL value indicates that the header field or special entity
* wasn't present or is undefined. Represent that as an empty string
* so that REs like "^$" will work and allow envariable setting
* based on missing or empty field.
*/
val = "";
val_len = 0;
}
0))) {
}
else {
if (!b->pattern) {
if (replaced) {
replaced);
}
}
else {
}
}
}
}
}
return DECLINED;
}
static void register_hooks(apr_pool_t *p)
{
}
{
create_setenvif_config_dir, /* dir config creater */
merge_setenvif_config, /* dir merger --- default is to override */
create_setenvif_config_svr, /* server config */
merge_setenvif_config, /* merge server configs */
setenvif_module_cmds, /* command apr_table_t */
register_hooks /* register hooks */
};