📄 mod_setenvif.c
字号:
/* 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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_setenvif.c * 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 { SPECIAL_NOT, SPECIAL_REMOTE_ADDR, SPECIAL_REMOTE_HOST, SPECIAL_REQUEST_URI, SPECIAL_REQUEST_METHOD, SPECIAL_REQUEST_PROTOCOL, SPECIAL_SERVER_ADDR};typedef struct { char *name; /* header name */ regex_t *pnamereg; /* compiled header name regex */ char *regex; /* regex to match against */ regex_t *preg; /* compiled regex */ const apr_strmatch_pattern *pattern; /* non-regex pattern to match */ apr_table_t *features; /* env vars to set (or unset) */ enum special special_type; /* is it a "special" header ? */ int icase; /* ignoring case? */} sei_entry;typedef struct { apr_array_header_t *conditionals;} sei_cfg_rec;module AP_MODULE_DECLARE_DATA setenvif_module;/* * 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){ sei_cfg_rec *new = (sei_cfg_rec *) apr_palloc(p, sizeof(sei_cfg_rec)); new->conditionals = apr_array_make(p, 20, sizeof(sei_entry)); return (void *) new;}static void *create_setenvif_config_svr(apr_pool_t *p, server_rec *dummy){ return create_setenvif_config(p);}static void *create_setenvif_config_dir(apr_pool_t *p, char *dummy){ return create_setenvif_config(p);}static void *merge_setenvif_config(apr_pool_t *p, void *basev, void *overridesv){ sei_cfg_rec *a = apr_pcalloc(p, sizeof(sei_cfg_rec)); sei_cfg_rec *base = basev, *overrides = overridesv; a->conditionals = apr_array_append(p, base->conditionals, overrides->conditionals); return a;}/* * any non-NULL magic constant will do... used to indicate if REG_ICASE should * be used */#define ICASE_MAGIC ((void *)(&setenvif_module))#define SEI_MAGIC_HEIRLOOM "setenvif-phase-flag"static int is_header_regex(apr_pool_t *p, const char* name) { /* If a Header name contains characters other than: * -,_,[A-Z\, [a-z] and [0-9]. * assume the header name is a regular expression. */ regex_t *preg = ap_pregcomp(p, "^[-A-Za-z0-9_]*$", (REG_EXTENDED | REG_NOSUB )); ap_assert(preg != NULL); if (ap_regexec(preg, name, 0, NULL, 0)) { 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 { char *unescaped = (char *)apr_palloc(p, src - s + 1); char *dst = unescaped; src = s; do { if (*src == '\\') { src++; } } while ((*dst++ = *src++)); return unescaped; }}static const char *add_setenvif_core(cmd_parms *cmd, void *mconfig, char *fname, const char *args){ char *regex; const char *simple_pattern; const char *feature; sei_cfg_rec *sconf; sei_entry *new; sei_entry *entries; 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. */ sconf = (cmd->path != NULL) ? (sei_cfg_rec *) mconfig : (sei_cfg_rec *) ap_get_module_config(cmd->server->module_config, &setenvif_module); entries = (sei_entry *) sconf->conditionals->elts; /* get regex */ regex = ap_getword_conf(cmd->pool, &args); if (!*regex) { return apr_pstrcat(cmd->pool, "Missing regular expression for ", cmd->cmd->name, NULL); } /* * 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. */ for (i = 0; i < sconf->conditionals->nelts; ++i) { new = &entries[i]; if (!strcasecmp(new->name, fname)) { fname = new->name; break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -