You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by ni...@apache.org on 2008/04/03 12:23:18 UTC

svn commit: r644253 - in /httpd/httpd/trunk: docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h server/core.c server/request.c

Author: niq
Date: Thu Apr  3 03:23:12 2008
New Revision: 644253

URL: http://svn.apache.org/viewvc?rev=644253&view=rev
Log:
HTTPD Core: Implement <If> sections for conditional (runtime) configuration.
N.B. This is a first pass, and has a way to go!

Modified:
    httpd/httpd/trunk/docs/manual/mod/core.xml
    httpd/httpd/trunk/include/ap_mmn.h
    httpd/httpd/trunk/include/http_core.h
    httpd/httpd/trunk/server/core.c
    httpd/httpd/trunk/server/request.c

Modified: httpd/httpd/trunk/docs/manual/mod/core.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/core.xml?rev=644253&r1=644252&r2=644253&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/mod/core.xml (original)
+++ httpd/httpd/trunk/docs/manual/mod/core.xml Thu Apr  3 03:23:12 2008
@@ -1304,6 +1304,36 @@
 </directivesynopsis>
 
 <directivesynopsis type="section">
+<name>If</name>
+<description>Contains directives that apply only if a condition is
+satisfied by a request at runtime</description>
+<syntax>&lt;If <var>expression</var>&gt; ... &lt;/If&gt;</syntax>
+<contextlist><context>server config</context><context>virtual host</context>
+<context>directory</context><context>.htaccess</context>
+</contextlist>
+<override>All</override>
+
+<usage>
+    <p>The <directive type="section">If</directive> directive
+    evaluates an expression at runtime, and applies the enclosed
+    directives if and only if the expression evaluates to true.
+    For example:</p>
+
+    <example>
+        &lt;If "$req{Host} = ''"&gt;
+    </example>
+
+    <p>would match HTTP/1.0 requests without a <var>Host:</var> header.</p>
+</usage>
+
+<seealso><a href="../sections.html">How &lt;Directory&gt;, &lt;Location&gt;,
+    &lt;Files&gt; sections work</a> for an explanation of how these
+    different sections are combined when a request is received.
+    <directive type="section">If</directive> has the same precedence
+    and usage as <directive type="section">Files</directive></seealso>
+</directivesynopsis>
+
+<directivesynopsis type="section">
 <name>IfDefine</name>
 <description>Encloses directives that will be processed only
 if a test is true at startup</description>
@@ -1633,7 +1663,8 @@
     <code>LOCK</code>, and <code>UNLOCK</code>. <strong>The method name is
     case-sensitive.</strong> If <code>GET</code> is used it will also
     restrict <code>HEAD</code> requests. The <code>TRACE</code> method
-    cannot be limited.</p>
+    cannot be limited (see <directive type="section" module="core"
+    >TraceEnable</directive>).</p>
 
     <note type="warning">A <directive type="section"
     module="core">LimitExcept</directive> section should always be

Modified: httpd/httpd/trunk/include/ap_mmn.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_mmn.h?rev=644253&r1=644252&r2=644253&view=diff
==============================================================================
--- httpd/httpd/trunk/include/ap_mmn.h (original)
+++ httpd/httpd/trunk/include/ap_mmn.h Thu Apr  3 03:23:12 2008
@@ -151,6 +151,8 @@
  * 20071108.9 (2.3.0-dev)  Add chroot support to unixd_config
  * 20071108.10(2.3.0-dev)  Introduce new ap_expr API
  * 20071108.11(2.3.0-dev)  Revise/Expand new ap_expr API
+ * 20071108.12(2.3.0-dev)  Remove ap_expr_clone from the API (same day it was added)
+ * 20000403.0 (2.3.0-dev)  Add condition field to core dir config
  */
 
 #define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */
@@ -158,7 +160,7 @@
 #ifndef MODULE_MAGIC_NUMBER_MAJOR
 #define MODULE_MAGIC_NUMBER_MAJOR 20071108
 #endif
-#define MODULE_MAGIC_NUMBER_MINOR 11                    /* 0...n */
+#define MODULE_MAGIC_NUMBER_MINOR 0                     /* 0...n */
 
 /**
  * Determine if the server's current MODULE_MAGIC_NUMBER is at least a

Modified: httpd/httpd/trunk/include/http_core.h
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/http_core.h?rev=644253&r1=644252&r2=644253&view=diff
==============================================================================
--- httpd/httpd/trunk/include/http_core.h (original)
+++ httpd/httpd/trunk/include/http_core.h Thu Apr  3 03:23:12 2008
@@ -30,6 +30,7 @@
 #include "apr_hash.h"
 #include "apr_optional.h"
 #include "util_filter.h"
+#include "ap_expr.h"
 
 #if APR_HAVE_STRUCT_RLIMIT
 #include <sys/time.h>
@@ -541,6 +542,7 @@
 #define USE_CANONICAL_PHYS_PORT_UNSET (2)
     unsigned use_canonical_phys_port : 2;
 
+    ap_parse_node_t *condition;   /* Conditionally merge <If> sections */
 } core_dir_config;
 
 /* Per-server core configuration */

Modified: httpd/httpd/trunk/server/core.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/core.c?rev=644253&r1=644252&r2=644253&view=diff
==============================================================================
--- httpd/httpd/trunk/server/core.c (original)
+++ httpd/httpd/trunk/server/core.c Thu Apr  3 03:23:12 2008
@@ -1995,6 +1995,71 @@
 
     return NULL;
 }
+static const char *ifsection(cmd_parms *cmd, void *mconfig, const char *arg)
+{
+    const char *errmsg;
+    const char *endp = ap_strrchr_c(arg, '>');
+    int old_overrides = cmd->override;
+    char *old_path = cmd->path;
+    core_dir_config *conf;
+    ap_regex_t *r = NULL;
+    const command_rec *thiscmd = cmd->cmd;
+    core_dir_config *c = mconfig;
+    ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
+    const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
+    const char *condition;
+    int expr_err = 0;
+
+    if (err != NULL) {
+        return err;
+    }
+
+    if (endp == NULL) {
+        return unclosed_directive(cmd);
+    }
+
+    arg = apr_pstrndup(cmd->pool, arg, endp - arg);
+
+    if (!arg[0]) {
+        return missing_container_arg(cmd);
+    }
+
+    //cmd->path = "*";
+    condition = ap_getword_conf(cmd->pool, &arg);
+    /* Only if not an .htaccess file */
+    if (!old_path) {
+        cmd->override = OR_ALL|ACCESS_CONF;
+    }
+
+    /* initialize our config and fetch it */
+    conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
+                                 &core_module, cmd->pool);
+
+    conf->condition = ap_expr_parse(cmd->pool, condition, &expr_err);
+    if (expr_err) {
+        return "Cannot parse condition clause";
+    }
+
+    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
+    if (errmsg != NULL)
+        return errmsg;
+
+    conf->d = cmd->path;
+    conf->d_is_fnmatch = 0;
+    conf->r = NULL;
+
+    ap_add_file_conf(c, new_file_conf);
+
+    if (*arg != '\0') {
+        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
+                           "> arguments not supported.", NULL);
+    }
+
+    cmd->path = old_path;
+    cmd->override = old_overrides;
+
+    return NULL;
+}
 
 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
 {
@@ -3209,6 +3274,8 @@
   "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
 AP_INIT_TAKE1("Define", set_define, NULL, RSRC_CONF,
               "Define the existance of a variable.  Same as passing -D to the command line."),
+AP_INIT_RAW_ARGS("<If", ifsection, NULL, OR_ALL,
+  "Container for directives to be conditionally applied"),
 
 /* Old resource config file commands */
 

Modified: httpd/httpd/trunk/server/request.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/request.c?rev=644253&r1=644252&r2=644253&view=diff
==============================================================================
--- httpd/httpd/trunk/server/request.c (original)
+++ httpd/httpd/trunk/server/request.c Thu Apr  3 03:23:12 2008
@@ -44,6 +44,7 @@
 #include "util_filter.h"
 #include "util_charset.h"
 #include "util_script.h"
+#include "ap_expr.h"
 
 #include "mod_core.h"
 
@@ -1413,16 +1414,24 @@
          * really try them with the most general first.
          */
         for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
-
+            int err = 0;
             core_dir_config *entry_core;
             entry_core = ap_get_module_config(sec_ent[sec_idx], &core_module);
 
-            if (entry_core->r
-                ? ap_regexec(entry_core->r, cache->cached , 0, NULL, 0)
-                : (entry_core->d_is_fnmatch
-                   ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
-                   : strcmp(entry_core->d, cache->cached))) {
-                continue;
+            if (entry_core->condition) {
+                if (!ap_expr_eval(r, entry_core->condition, &err, NULL,
+                                  ap_expr_string, NULL)) {
+                    continue;
+                }
+            }
+            else {
+                if (entry_core->r
+                    ? ap_regexec(entry_core->r, cache->cached , 0, NULL, 0)
+                    : (entry_core->d_is_fnmatch
+                       ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
+                       : strcmp(entry_core->d, cache->cached))) {
+                    continue;
+                }
             }
 
             /* If we merged this same section last time, reuse it



Re: svn commit: r644253 - in /httpd/httpd/trunk: docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h server/core.c server/request.c

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
Plüm wrote:
> 
> I am still not convinced. What makes you sure that a previous merge
> did not set entry_core->r / entry_core->d. But maybe it is just my
> missing experience with the config and merge system.

It is

   1) the job of <location > to do so.

   2) the job of any <section > which throws away the <location >
      results to fix this.

SO...

<location >
   <proxy >
    -or-
   <dir >
     <files >

       <location >  (redux)

somewhere within that ... <if >, <method > etc etc.

You should not fark around with ->d - it isn't yours.  As far as ->r is
concerned that should already be handled.


Re: svn commit: r644253 - in /httpd/httpd/trunk: docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h server/core.c server/request.c

Posted by Plüm, Rüdiger, VF-Group <ru...@vodafone.com>.
 

> -----Ursprüngliche Nachricht-----
> Von: Nick Kew 
> Gesendet: Freitag, 4. April 2008 00:06
> An: dev@httpd.apache.org
> Betreff: Re: svn commit: r644253 - in /httpd/httpd/trunk: 
> docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h 
> server/core.c server/request.c
> 
> On Thu, 03 Apr 2008 21:18:26 +0200
> Ruediger Pluem <rp...@apache.org> wrote:
> 

> 
> > Why does this belong in an else branch? I guess if the condition in
> > entry_core->condition is true we still need to do the 
> previous check.
> 
> No.  The other check applies to <Files> / <FilesMatch> sections,
> whose code path I've hijacked for <If>.  The two are mutually 
> exclusive.

I am still not convinced. What makes you sure that a previous merge
did not set entry_core->r / entry_core->d. But maybe it is just my
missing experience with the config and merge system.

Regards

Rüdiger

Re: svn commit: r644253 - in /httpd/httpd/trunk: docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h server/core.c server/request.c

Posted by Nick Kew <ni...@webthing.com>.
On Thu, 03 Apr 2008 21:18:26 +0200
Ruediger Pluem <rp...@apache.org> wrote:

> > +    ap_parse_node_t *condition;   /* Conditionally merge <If>
> > sections */ } core_dir_config;
> 
> Does this work correctly without adjusting merge_core_dir_configs?

Good point - I'll take a look.  For sure there's more work to do.


> > +    cmd->path = old_path;
> 
> Who changes cmd->path?

Erm ... looks like a cut&paste remnant.

> Why does this belong in an else branch? I guess if the condition in
> entry_core->condition is true we still need to do the previous check.

No.  The other check applies to <Files> / <FilesMatch> sections,
whose code path I've hijacked for <If>.  The two are mutually exclusive.

-- 
Nick Kew

Application Development with Apache - the Apache Modules Book
http://www.apachetutor.org/

Re: svn commit: r644253 - in /httpd/httpd/trunk: docs/manual/mod/core.xml include/ap_mmn.h include/http_core.h server/core.c server/request.c

Posted by Ruediger Pluem <rp...@apache.org>.

On 04/03/2008 12:23 PM, niq@apache.org wrote:
> Author: niq
> Date: Thu Apr  3 03:23:12 2008
> New Revision: 644253
> 
> URL: http://svn.apache.org/viewvc?rev=644253&view=rev
> Log:
> HTTPD Core: Implement <If> sections for conditional (runtime) configuration.
> N.B. This is a first pass, and has a way to go!
> 
> Modified:
>     httpd/httpd/trunk/docs/manual/mod/core.xml
>     httpd/httpd/trunk/include/ap_mmn.h
>     httpd/httpd/trunk/include/http_core.h
>     httpd/httpd/trunk/server/core.c
>     httpd/httpd/trunk/server/request.c
> 

> 
> Modified: httpd/httpd/trunk/include/ap_mmn.h
> URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/ap_mmn.h?rev=644253&r1=644252&r2=644253&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/include/ap_mmn.h (original)
> +++ httpd/httpd/trunk/include/ap_mmn.h Thu Apr  3 03:23:12 2008
> @@ -151,6 +151,8 @@
>   * 20071108.9 (2.3.0-dev)  Add chroot support to unixd_config
>   * 20071108.10(2.3.0-dev)  Introduce new ap_expr API
>   * 20071108.11(2.3.0-dev)  Revise/Expand new ap_expr API
> + * 20071108.12(2.3.0-dev)  Remove ap_expr_clone from the API (same day it was added)
> + * 20000403.0 (2.3.0-dev)  Add condition field to core dir config
>   */
>  
>  #define MODULE_MAGIC_COOKIE 0x41503234UL /* "AP24" */
> @@ -158,7 +160,7 @@
>  #ifndef MODULE_MAGIC_NUMBER_MAJOR
>  #define MODULE_MAGIC_NUMBER_MAJOR 20071108

You missed to change the define and MODULE_MAGIC_NUMBER_MAJOR  in the comment is wrong
2008 instead of 2000.
BTW: Why is this a major bump at all?

>  #endif
> -#define MODULE_MAGIC_NUMBER_MINOR 11                    /* 0...n */
> +#define MODULE_MAGIC_NUMBER_MINOR 0                     /* 0...n */
>  
>  /**
>   * Determine if the server's current MODULE_MAGIC_NUMBER is at least a
> 
> Modified: httpd/httpd/trunk/include/http_core.h
> URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/include/http_core.h?rev=644253&r1=644252&r2=644253&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/include/http_core.h (original)
> +++ httpd/httpd/trunk/include/http_core.h Thu Apr  3 03:23:12 2008
> @@ -30,6 +30,7 @@
>  #include "apr_hash.h"
>  #include "apr_optional.h"
>  #include "util_filter.h"
> +#include "ap_expr.h"
>  
>  #if APR_HAVE_STRUCT_RLIMIT
>  #include <sys/time.h>
> @@ -541,6 +542,7 @@
>  #define USE_CANONICAL_PHYS_PORT_UNSET (2)
>      unsigned use_canonical_phys_port : 2;
>  
> +    ap_parse_node_t *condition;   /* Conditionally merge <If> sections */
>  } core_dir_config;

Does this work correctly without adjusting merge_core_dir_configs?

>  
>  /* Per-server core configuration */
> 
> Modified: httpd/httpd/trunk/server/core.c
> URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/core.c?rev=644253&r1=644252&r2=644253&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/server/core.c (original)
> +++ httpd/httpd/trunk/server/core.c Thu Apr  3 03:23:12 2008
> @@ -1995,6 +1995,71 @@
>  
>      return NULL;
>  }
> +static const char *ifsection(cmd_parms *cmd, void *mconfig, const char *arg)
> +{
> +    const char *errmsg;
> +    const char *endp = ap_strrchr_c(arg, '>');
> +    int old_overrides = cmd->override;
> +    char *old_path = cmd->path;
> +    core_dir_config *conf;
> +    ap_regex_t *r = NULL;
> +    const command_rec *thiscmd = cmd->cmd;
> +    core_dir_config *c = mconfig;
> +    ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
> +    const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
> +    const char *condition;
> +    int expr_err = 0;
> +
> +    if (err != NULL) {
> +        return err;
> +    }
> +
> +    if (endp == NULL) {
> +        return unclosed_directive(cmd);
> +    }
> +
> +    arg = apr_pstrndup(cmd->pool, arg, endp - arg);
> +
> +    if (!arg[0]) {
> +        return missing_container_arg(cmd);
> +    }
> +
> +    //cmd->path = "*";

C++ style comment.

> +    condition = ap_getword_conf(cmd->pool, &arg);
> +    /* Only if not an .htaccess file */
> +    if (!old_path) {
> +        cmd->override = OR_ALL|ACCESS_CONF;
> +    }
> +
> +    /* initialize our config and fetch it */
> +    conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
> +                                 &core_module, cmd->pool);
> +
> +    conf->condition = ap_expr_parse(cmd->pool, condition, &expr_err);
> +    if (expr_err) {
> +        return "Cannot parse condition clause";
> +    }
> +
> +    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
> +    if (errmsg != NULL)
> +        return errmsg;
> +
> +    conf->d = cmd->path;
> +    conf->d_is_fnmatch = 0;
> +    conf->r = NULL;
> +
> +    ap_add_file_conf(c, new_file_conf);
> +
> +    if (*arg != '\0') {
> +        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
> +                           "> arguments not supported.", NULL);
> +    }
> +
> +    cmd->path = old_path;

Who changes cmd->path?

> +    cmd->override = old_overrides;
> +
> +    return NULL;
> +}
>  
>  static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
>  {
> @@ -3209,6 +3274,8 @@
>    "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
>  AP_INIT_TAKE1("Define", set_define, NULL, RSRC_CONF,
>                "Define the existance of a variable.  Same as passing -D to the command line."),
> +AP_INIT_RAW_ARGS("<If", ifsection, NULL, OR_ALL,
> +  "Container for directives to be conditionally applied"),
>  
>  /* Old resource config file commands */
>  
> 
> Modified: httpd/httpd/trunk/server/request.c
> URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/server/request.c?rev=644253&r1=644252&r2=644253&view=diff
> ==============================================================================
> --- httpd/httpd/trunk/server/request.c (original)
> +++ httpd/httpd/trunk/server/request.c Thu Apr  3 03:23:12 2008
> @@ -44,6 +44,7 @@
>  #include "util_filter.h"
>  #include "util_charset.h"
>  #include "util_script.h"
> +#include "ap_expr.h"
>  
>  #include "mod_core.h"
>  
> @@ -1413,16 +1414,24 @@
>           * really try them with the most general first.
>           */
>          for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
> -
> +            int err = 0;
>              core_dir_config *entry_core;
>              entry_core = ap_get_module_config(sec_ent[sec_idx], &core_module);
>  
> -            if (entry_core->r
> -                ? ap_regexec(entry_core->r, cache->cached , 0, NULL, 0)
> -                : (entry_core->d_is_fnmatch
> -                   ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
> -                   : strcmp(entry_core->d, cache->cached))) {
> -                continue;
> +            if (entry_core->condition) {
> +                if (!ap_expr_eval(r, entry_core->condition, &err, NULL,
> +                                  ap_expr_string, NULL)) {
> +                    continue;
> +                }
> +            }
> +            else {
> +                if (entry_core->r
> +                    ? ap_regexec(entry_core->r, cache->cached , 0, NULL, 0)
> +                    : (entry_core->d_is_fnmatch
> +                       ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
> +                       : strcmp(entry_core->d, cache->cached))) {
> +                    continue;
> +                }
>              }

Why does this belong in an else branch? I guess if the condition in entry_core->condition is
true we still need to do the previous check.

Regards

Rüdiger