From: Rainer Jung Date: Mon, 7 Mar 2011 20:39:54 +0000 (+0000) Subject: mod_rewrite: Allow to unset environment variables. X-Git-Tag: 2.2.18~102 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f2dd694496e338d6306ece6c11652f0119fb4cd;p=thirdparty%2Fapache%2Fhttpd.git mod_rewrite: Allow to unset environment variables. PR: 50746 Submitted by: rjung Reviewed by: covener, rpluem git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.2.x@1078933 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/CHANGES b/CHANGES index 488182d7892..7710ed66e9e 100644 --- a/CHANGES +++ b/CHANGES @@ -1,10 +1,13 @@ -*- coding: utf-8 -*- Changes with Apache 2.2.18 + *) mod_rewrite: Allow to unset environment variables. PR 50746. + [Rainer Jung] + *) suEXEC: Add Suexec directive to disable suEXEC without renaming the binary (Suexec Off), or force startup failure if suEXEC is required but not supported (Suexec On). [Jeff Trawick] - + *) mod_proxy: Put the worker in error state if the SSL handshake with the backend fails. PR 50332. [Daniel Ruggeri , Ruediger Pluem] diff --git a/STATUS b/STATUS index 930c1e81bf5..a06be5e2cda 100644 --- a/STATUS +++ b/STATUS @@ -105,14 +105,6 @@ PATCHES ACCEPTED TO BACKPORT FROM TRUNK: Non-optional broken features are worse :) Trunk must be patched identically. - * mod_rewrite: Allow to unset environment variables. - Direct backport from trunk. - PR: 50746 - 2.2.x patch: https://issues.apache.org/bugzilla/attachment.cgi?id=26646 - (patch is simpler if viewed with whitespace changes ignored) - +1 rjung, covener, rpluem - (needs doc too including the behavior change in 2.2.16) - PATCHES PROPOSED TO BACKPORT FROM TRUNK: [ New proposals should be added at the end of the list ] diff --git a/docs/manual/mod/mod_rewrite.xml b/docs/manual/mod/mod_rewrite.xml index 29e924c50cf..5588803d66d 100644 --- a/docs/manual/mod/mod_rewrite.xml +++ b/docs/manual/mod/mod_rewrite.xml @@ -1382,16 +1382,16 @@ cannot use $N in the substitution string! only the direct result of substitutions, without any PATH_INFO appended.

-
- 'env|E=VAR[:VAL]' +
'env|E=!VAR[:VAL]' (set environment variable)
This forces an environment variable named VAR to be set. The value will be VAL if provided, where VAL can contain regexp backreferences ($N and - %N) which will be expanded. You can use this - flag more than once, to set more than one variable. The - variables can later be dereferenced in many situations, most commonly - from within XSSI (via <!--#echo + %N) which will be expanded. The form !VAR causes + the environment variable VAR to be unset and does not accept + any VAL. You can use this flag more than once, to set more + than one variable. The variables can later be dereferenced in many + situations, most commonly from within XSSI (via <!--#echo var="VAR"-->) or CGI ($ENV{'VAR'}). You can also dereference the variable in a later RewriteCond pattern, using %{ENV:VAR}. Use this to strip diff --git a/docs/manual/rewrite/rewrite_flags.xml b/docs/manual/rewrite/rewrite_flags.xml index ae2b97f9f47..2d11ec93186 100644 --- a/docs/manual/rewrite/rewrite_flags.xml +++ b/docs/manual/rewrite/rewrite_flags.xml @@ -108,7 +108,39 @@ is run, thus unsetting what you have set. See the Environment Variables document for more details on how Environment variables work.

-

The following example sets an evironment variable called 'image' to a +

The full syntax for this flag is:

+ + +[E=VAR:VAL] +[E=!VAR] + + +

VAL may contain backreferences ($N or +%N) which will be expanded.

+ +

Using the short form

+ + +[E=VAR] + + +

you can set the environment variable named VAR to an +empty value.

+ +

The form

+ + +[E=!VAR] + + +

allows to unset a previously set environment variable named +VAR.

+ +

Environment variables can then be used in a variety of +contexts, including CGI programs, other RewriteRule directives, or +CustomLog directives.

+ +

The following example sets an environment variable called 'image' to a value of '1' if the requested URI is an image file. Then, that environment variable is used to exclude those requests from the access log.

diff --git a/modules/mappers/mod_rewrite.c b/modules/mappers/mod_rewrite.c index 36609ccaa94..700107385f2 100644 --- a/modules/mappers/mod_rewrite.c +++ b/modules/mappers/mod_rewrite.c @@ -2347,15 +2347,22 @@ static void do_expand_env(data_item *env, rewrite_ctx *ctx) while (env) { name = do_expand(env->data, ctx, NULL); - if ((val = ap_strchr(name, ':')) != NULL) { - *val++ = '\0'; - } else { - val = ""; + if (*name == '!') { + name++; + apr_table_unset(ctx->r->subprocess_env, name); + rewritelog((ctx->r, 5, NULL, "unsetting env variable '%s'", name)); } + else { + if ((val = ap_strchr(name, ':')) != NULL) { + *val++ = '\0'; + } else { + val = ""; + } - apr_table_set(ctx->r->subprocess_env, name, val); - rewritelog((ctx->r, 5, NULL, "setting env variable '%s' to '%s'", - name, val)); + apr_table_set(ctx->r->subprocess_env, name, val); + rewritelog((ctx->r, 5, NULL, "setting env variable '%s' to '%s'", + name, val)); + } env = env->next; }