mod_headers: generalise the envclause to support conditional
authorNick Kew <niq@apache.org>
Thu, 27 Aug 2009 01:02:42 +0000 (01:02 +0000)
committerNick Kew <niq@apache.org>
Thu, 27 Aug 2009 01:02:42 +0000 (01:02 +0000)
header rules via ap_expr.

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@808240 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
docs/manual/mod/mod_headers.xml
modules/metadata/mod_headers.c

diff --git a/CHANGES b/CHANGES
index 2f072006721ac96b8a6cfd119bc4acef4813d3ba..eb887ad3f01ae33203606e4c599ec1ba7a997f16 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,9 @@
 
 Changes with Apache 2.3.3
 
+  *) mod_headers: generalise the envclause to support expression
+     evaluation with ap_expr parser [Nick Kew]
+
   *) mod_cache: Introduce the thundering herd lock, a mechanism to keep
      the flood of requests at bay that strike a backend webserver as
      a cached entity goes stale. [Graham Leggett]
index 91674c9d364dfa68ce9ceed391820937135240f3..8fd54a4bda75d2824bf2e0fcd5a1ecb95c1685a1 100644 (file)
@@ -181,6 +181,12 @@ headers</description>
           Cache-Control: no-cache, no-cache, no-store
         </example>
       </li>
+      <li>
+        Set a test cookie if and only if the client didn't send us a cookie
+        <example>
+          Header set Set-Cookie testcookie !$req{Cookie}
+        </example>
+      </li>
     </ol>
 </section>
 
@@ -258,15 +264,20 @@ headers</description>
     replacement string respectively.</p>
 
     <p>The <directive>RequestHeader</directive> directive may be followed by
-    an additional argument, which may be used to specify conditions under
-    which the action will be taken, or may be the keyword <code>early</code>
-    to specify <a href="#early">early processing</a>. If the
-    <a href="../env.html">environment
-    variable</a> specified in the <code>env=<var>...</var></code> argument
-    exists (or if the environment variable does not exist and
-    <code>env=!<var>...</var></code> is specified) then the action specified
-    by the <directive>RequestHeader</directive> directive will take effect.
-    Otherwise, the directive will have no effect on the request.</p>
+    an additional argument, which may be any of:</p>
+    <dl>
+    <dt><code>early</code></dt>
+    <dd>Specifies <a href="#early">early processing</a>.</dd>
+    <dt><code>env=[!]varname</code></dt>
+    <dd>The directive is applied if and only if the <a href="../env.html"
+        >environment variable</a> <code>varname</code> exists.
+        A <code>!</code> in front of <code>varname</code> reverses the test,
+        so the directive applies only if <code>varname</code> is unset.</dd>
+    <dt><code>expr</code></dt>
+    <dd>An string that matches neither of the above is parsed as an
+        expression.  Details of expression syntax and evaluation are
+        currently best documented on the <module>mod_filter</module> page.</dd>
+    </dl>
 
     <p>Except in <a href="#early">early</a> mode, the
     <directive>RequestHeader</directive> directive is processed
@@ -404,16 +415,21 @@ headers</description>
     which is a <glossary ref="regex">regular expression</glossary>,
     and an additional <var>replacement</var> string.</p>
 
-    <p>The <directive>Header</directive> directive may be followed by an
-    an additional argument, which may be used to specify conditions under
-    which the action will be taken, or may be the keyword <code>early</code>
-    to specify <a href="#early">early processing</a>. If the
-    <a href="../env.html">environment variable</a> specified in the
-    <code>env=<var>...</var></code> argument exists (or if the environment
-    variable does not exist and <code>env=!<var>...</var></code> is specified)
-    then the action specified by the <directive>Header</directive> directive
-    will take effect. Otherwise, the directive will have no effect
-    on the request.</p>
+    <p>The <directive>Header</directive> directive may be followed by
+    an additional argument, which may be any of:</p>
+    <dl>
+    <dt><code>early</code></dt>
+    <dd>Specifies <a href="#early">early processing</a>.</dd>
+    <dt><code>env=[!]varname</code></dt>
+    <dd>The directive is applied if and only if the <a href="../env.html"
+        >environment variable</a> <code>varname</code> exists.
+        A <code>!</code> in front of <code>varname</code> reverses the test,
+        so the directive applies only if <code>varname</code> is unset.</dd>
+    <dt><code>expr</code></dt>
+    <dd>An string that matches neither of the above is parsed as an
+        expression.  Details of expression syntax and evaluation are
+        currently best documented on the <module>mod_filter</module> page.</dd>
+    </dl>
 
     <p>Except in <a href="#early">early</a> mode, the
     <directive>Header</directive> directives are processed just
index b65fbfd07716c07aca038b1f195db5199b0bef0f..c9c73c7db94adbe6836484ffe58e4cb08532dc32 100644 (file)
@@ -81,6 +81,7 @@
 #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 */
 
@@ -125,6 +126,7 @@ typedef struct {
     ap_regex_t *regex;
     const char *condition_var;
     const char *subs;
+    ap_parse_node_t *expr;
 } header_entry;
 
 /* echo_do is used for Header echo to iterate through the request headers*/
@@ -391,6 +393,7 @@ static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd,
     const char *condition_var = NULL;
     const char *colon;
     header_entry *new;
+    ap_parse_node_t *expr = NULL;
 
     apr_array_header_t *fixup = (cmd->info == &hdr_in)
         ? dirconf->fixup_in   : (cmd->info == &hdr_err)
@@ -472,10 +475,7 @@ static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd,
         if (strcasecmp(envclause, "early") == 0) {
             condition_var = condition_early;
         }
-        else {
-            if (strncasecmp(envclause, "env=", 4) != 0) {
-                return "error: envclause should be in the form env=envar";
-            }
+        else if (strncasecmp(envclause, "env=", 4) == 0) {
             if ((envclause[4] == '\0')
                 || ((envclause[4] == '!') && (envclause[5] == '\0'))) {
                 return "error: missing environment variable name. "
@@ -483,6 +483,13 @@ static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd,
             }
             condition_var = envclause + 4;
         }
+        else {
+            int err = 0;
+            expr = ap_expr_parse(cmd->pool, envclause, &err);
+            if (err) {
+                return "Can't parse envclause/expression";
+            }
+        }
     }
 
     if ((colon = ap_strchr_c(hdr, ':'))) {
@@ -491,6 +498,7 @@ static APR_INLINE const char *header_inout_cmd(cmd_parms *cmd,
 
     new->header = hdr;
     new->condition_var = condition_var;
+    new->expr = expr;
 
     return parse_format_string(cmd->pool, new, value);
 }
@@ -620,6 +628,19 @@ static void do_headers_fixup(request_rec *r, apr_table_t *headers,
         else if (early && (envar != condition_early)) {
             continue;
         }
+        /* Do we have an expression to evaluate? */
+        else if (hdr->expr != NULL) {
+            int err = 0;
+            int eval = ap_expr_eval(r, hdr->expr, &err, NULL,
+                                    ap_expr_string, NULL);
+            if (err) {
+                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
+                              "Failed to evaluate expression - ignoring");
+            }
+            else if (!eval) {
+                continue;
+            }
+        }
         /* Have any conditional envar-controlled Header processing to do? */
         else if (envar && !early) {
             if (*envar != '!') {