You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by mi...@apache.org on 2015/01/22 18:02:23 UTC

svn commit: r1653941 - in /httpd/httpd/trunk: CHANGES docs/manual/expr.xml docs/manual/mod/mod_alias.xml modules/mappers/mod_alias.c

Author: minfrin
Date: Thu Jan 22 17:02:22 2015
New Revision: 1653941

URL: http://svn.apache.org/r1653941
Log:
mod_alias: Introduce expression parser support for Alias, ScriptAlias
and Redirect.

Modified:
    httpd/httpd/trunk/CHANGES
    httpd/httpd/trunk/docs/manual/expr.xml
    httpd/httpd/trunk/docs/manual/mod/mod_alias.xml
    httpd/httpd/trunk/modules/mappers/mod_alias.c

Modified: httpd/httpd/trunk/CHANGES
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/CHANGES?rev=1653941&r1=1653940&r2=1653941&view=diff
==============================================================================
--- httpd/httpd/trunk/CHANGES [utf-8] (original)
+++ httpd/httpd/trunk/CHANGES [utf-8] Thu Jan 22 17:02:22 2015
@@ -1,6 +1,9 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache 2.5.0
 
+  *) mod_alias: Introduce expression parser support for Alias, ScriptAlias
+     and Redirect. [Graham Leggett]
+
   *) mod_ssl: 'SSLProtocol ALL' was being ignored in virtual host context. 
      PR 57100.  [Michael Kaufmann <apache-bugzilla michael-kaufmann.ch>]
 

Modified: httpd/httpd/trunk/docs/manual/expr.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/expr.xml?rev=1653941&r1=1653940&r2=1653941&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/expr.xml (original)
+++ httpd/httpd/trunk/docs/manual/expr.xml Thu Jan 22 17:02:22 2015
@@ -40,6 +40,9 @@
 <seealso><directive module="core" type="section">If</directive></seealso>
 <seealso><directive module="core" type="section">ElseIf</directive></seealso>
 <seealso><directive module="core" type="section">Else</directive></seealso>
+<seealso><directive module="mod_alias">Alias</directive></seealso>
+<seealso><directive module="mod_alias">ScriptAlias</directive></seealso>
+<seealso><directive module="mod_alias">Redirect</directive></seealso>
 <seealso><directive module="mod_auth_basic">AuthBasicFake</directive></seealso>
 <seealso><directive module="mod_auth_form">AuthFormLoginRequiredLocation</directive></seealso>
 <seealso><directive module="mod_auth_form">AuthFormLoginSuccessLocation</directive></seealso>

Modified: httpd/httpd/trunk/docs/manual/mod/mod_alias.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/mod/mod_alias.xml?rev=1653941&r1=1653940&r2=1653941&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/mod/mod_alias.xml (original)
+++ httpd/httpd/trunk/docs/manual/mod/mod_alias.xml Thu Jan 22 17:02:22 2015
@@ -47,6 +47,15 @@
     a different URL. They are often used when a resource has moved to
     a new location.</p>
 
+    <p>When the <directive module="mod_alias">Alias</directive>,
+    <directive module="mod_alias">ScriptAlias</directive> and 
+    <directive module="mod_alias">Redirect</directive> directives are used
+    within a <directive type="section" module="core">Location</directive>
+    or <directive type="section" module="core">LocationMatch</directive>
+    section, <a href="../expr.html">expression syntax</a> can be used
+    to manipulate the destination path or URL.
+	</p>
+
     <p><module>mod_alias</module> is designed to handle simple URL
     manipulation tasks.  For more complicated tasks such as
     manipulating the query string, use the tools provided by
@@ -90,14 +99,25 @@ Alias /foo /gaq
     module="mod_alias">Alias</directive>, so the latter directive would be
     ignored.</p>
 
+    <p>When the <directive module="mod_alias">Alias</directive>,
+    <directive module="mod_alias">ScriptAlias</directive> and 
+    <directive module="mod_alias">Redirect</directive> directives are used
+    within a <directive type="section" module="core">Location</directive>
+    or <directive type="section" module="core">LocationMatch</directive>
+    section, these directives will take precedence over any globally
+    defined <directive module="mod_alias">Alias</directive>,
+    <directive module="mod_alias">ScriptAlias</directive> and 
+    <directive module="mod_alias">Redirect</directive> directives.</p>
+
 </section>
 
 <directivesynopsis>
 <name>Alias</name>
 <description>Maps URLs to filesystem locations</description>
-<syntax>Alias <var>URL-path</var>
+<syntax>Alias [<var>URL-path</var>]
 <var>file-path</var>|<var>directory-path</var></syntax>
 <contextlist><context>server config</context><context>virtual host</context>
+<context>directory</context>
 </contextlist>
 
 <usage>
@@ -158,6 +178,21 @@ Alias /image /ftp/pub/image
     <p>Any number slashes in the <var>URL-path</var> parameter 
     matches any number of slashes in the requested URL-path.</p>
 
+    <p>If the <directive>Alias</directive> directive is used within a
+    <directive type="section" module="core">Location</directive>
+    or <directive type="section" module="core">LocationMatch</directive>
+    section the URL-path is omitted, and the file-path is interpreted
+    using <a href="../expr.html">expression syntax</a>.</p>
+
+    <highlight language="config">
+&lt;Location /image&gt;
+    Alias /ftp/pub/image
+&lt;/Location&gt;
+&lt;LocationMatch /error/(?&lt;NUMBER&gt;[0-9]+)&gt;
+    Alias /usr/local/apache/errors/%{env:MATCH_NUMBER}.html
+&lt;/LocationMatch&gt;
+    </highlight>
+
 </usage>
 </directivesynopsis>
 
@@ -256,7 +291,7 @@ expressions</description>
 <name>Redirect</name>
 <description>Sends an external redirect asking the client to fetch
 a different URL</description>
-<syntax>Redirect [<var>status</var>] <var>URL-path</var>
+<syntax>Redirect [<var>status</var>] [<var>URL-path</var>]
 <var>URL</var></syntax>
 <contextlist><context>server config</context><context>virtual host</context>
 <context>directory</context><context>.htaccess</context></contextlist>
@@ -297,15 +332,18 @@ Redirect /one /two
 	Note that <code>POST</code>s will be discarded.<br />
 	Only complete path segments are matched, so the above
     example would not match a request for
-    <code>http://example.com/servicefoo.txt</code>.  For more complex matching
-    using regular expressions, see the <directive
+    <code>http://example.com/servicefoo.txt</code>. For more complex matching
+    using the <a href="../expr.html">expression syntax</a>, omit the URL-path
+    argument as described below. Alternatively, for matching using regular
+    expressions, see the <directive
     module="mod_alias">RedirectMatch</directive> directive.</p>
 
 
     <note><title>Note</title>
     <p>Redirect directives take precedence over Alias and ScriptAlias
     directives, irrespective of their ordering in the configuration
-    file.</p></note>
+    file. Redirect directives inside a Location take precedence over
+    Redirect and Alias directives with an URL-path.</p></note>
 
     <p>If no <var>status</var> argument is given, the redirect will
     be "temporary" (HTTP status 302). This indicates to the client
@@ -348,6 +386,24 @@ Redirect permanent /one http://example.c
 Redirect 303 /three http://example.com/other
     </highlight>
 
+    <p>If the <directive>Redirect</directive> directive is used within a
+    <directive type="section" module="core">Location</directive>
+    or <directive type="section" module="core">LocationMatch</directive>
+    section with the URL-path omitted, then the URL parameter will be
+    interpreted using <a href="../expr.html">expression syntax</a>.</p>
+
+    <highlight language="config">
+&lt;Location /one&gt;
+    Redirect permanent http://example.com/two
+&lt;/Location&gt;<br />
+&lt;Location /three&gt;
+    Redirect 303 http://example.com/other
+&lt;/Location&gt;<br />
+&lt;LocationMatch /error/(?&lt;NUMBER&gt;[0-9]+)&gt;
+    Redirect permanent http://example.com/errors/%{env:MATCH_NUMBER}.html
+&lt;/LocationMatch&gt;<br />
+    </highlight>
+
 </usage>
 </directivesynopsis>
 
@@ -425,9 +481,10 @@ a different URL</description>
 <name>ScriptAlias</name>
 <description>Maps a URL to a filesystem location and designates the
 target as a CGI script</description>
-<syntax>ScriptAlias <var>URL-path</var>
+<syntax>ScriptAlias [<var>URL-path</var>]
 <var>file-path</var>|<var>directory-path</var></syntax>
 <contextlist><context>server config</context><context>virtual host</context>
+<context>directory</context>
 </contextlist>
 
 <usage>
@@ -491,6 +548,21 @@ Alias /cgi-bin/ /web/cgi-bin/
     of the CGI scripts if they are not restricted by a
     <directive module="core">Directory</directive> section.</note>
 
+    <p>If the <directive>ScriptAlias</directive> directive is used within
+    a <directive type="section" module="core">Location</directive>
+    or <directive type="section" module="core">LocationMatch</directive>
+    section with the URL-path omitted, then the URL parameter will be
+    interpreted using <a href="../expr.html">expression syntax</a>.</p>
+
+    <highlight language="config">
+&lt;Location /cgi-bin &gt;
+    ScriptAlias /web/cgi-bin/
+&lt;/Location&gt;
+&lt;LocationMatch /cgi-bin/errors/(?&lt;NUMBER&gt;[0-9]+)&gt;
+    ScriptAlias /web/cgi-bin/errors/%{env:MATCH_NUMBER}.cgi
+&lt;/LocationMatch&gt;<br />
+    </highlight>
+
 </usage>
 <seealso><a href="../howto/cgi.html">CGI Tutorial</a></seealso>
 </directivesynopsis>

Modified: httpd/httpd/trunk/modules/mappers/mod_alias.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/mappers/mod_alias.c?rev=1653941&r1=1653940&r2=1653941&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/mappers/mod_alias.c (original)
+++ httpd/httpd/trunk/modules/mappers/mod_alias.c Thu Jan 22 17:02:22 2015
@@ -34,6 +34,7 @@
 #include "http_config.h"
 #include "http_request.h"
 #include "http_log.h"
+#include "ap_expr.h"
 
 
 typedef struct {
@@ -50,11 +51,20 @@ typedef struct {
 } alias_server_conf;
 
 typedef struct {
+    int alias_set:1;
+    int redirect_set:1;
     apr_array_header_t *redirects;
+    const ap_expr_info_t *alias;
+    char *handler;
+    const ap_expr_info_t *redirect;
+    int redirect_status;                /* 301, 302, 303, 410, etc */
 } alias_dir_conf;
 
 module AP_MODULE_DECLARE_DATA alias_module;
 
+static char magic_error_value;
+#define PREGSUB_ERROR      (&magic_error_value)
+
 static void *create_alias_config(apr_pool_t *p, server_rec *s)
 {
     alias_server_conf *a =
@@ -91,7 +101,17 @@ static void *merge_alias_dir_config(apr_
     (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
     alias_dir_conf *base = (alias_dir_conf *) basev;
     alias_dir_conf *overrides = (alias_dir_conf *) overridesv;
+
     a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
+
+    a->alias = (overrides->alias_set == 0) ? base->alias : overrides->alias;
+    a->handler = (overrides->alias_set == 0) ? base->handler : overrides->handler;
+    a->alias_set = overrides->alias_set || base->alias_set;
+
+    a->redirect = (overrides->redirect_set == 0) ? base->redirect : overrides->redirect;
+    a->redirect_status = (overrides->redirect_set == 0) ? base->redirect_status : overrides->redirect_status;
+    a->redirect_set = overrides->redirect_set || base->redirect_set;
+
     return a;
 }
 
@@ -111,6 +131,12 @@ static const char *add_alias_internal(cm
 
     /* XXX: real can NOT be relative to DocumentRoot here... compat bug. */
 
+    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
+
+    if (err != NULL) {
+        return err;
+    }
+
     if (use_regex) {
         new->regexp = ap_pregcomp(cmd->pool, fake, AP_REG_EXTENDED);
         if (new->regexp == NULL)
@@ -155,9 +181,41 @@ static const char *add_alias_internal(cm
 }
 
 static const char *add_alias(cmd_parms *cmd, void *dummy, const char *fake,
-                             const char *real)
+        const char *real)
 {
-    return add_alias_internal(cmd, dummy, fake, real, 0);
+    if (real) {
+
+        return add_alias_internal(cmd, dummy, fake, real, 0);
+
+    }
+    else {
+        alias_dir_conf *dirconf = (alias_dir_conf *) dummy;
+
+        const char *err = ap_check_cmd_context(cmd, NOT_IN_DIRECTORY|NOT_IN_FILES);
+
+        if (err != NULL) {
+            return err;
+        }
+
+        if (!cmd->path) {
+            return "Alias must have two arguments when used globally";
+        }
+
+        dirconf->alias =
+                ap_expr_parse_cmd(cmd, fake, AP_EXPR_FLAG_STRING_RESULT,
+                        &err, NULL);
+        if (err) {
+            return apr_pstrcat(cmd->temp_pool,
+                    "Cannot parse alias expression '", fake, "': ", err,
+                    NULL);
+        }
+
+        dirconf->handler = cmd->info;
+        dirconf->alias_set = 1;
+
+        return NULL;
+
+    }
 }
 
 static const char *add_alias_regex(cmd_parms *cmd, void *dummy,
@@ -205,6 +263,59 @@ static const char *add_redirect_internal
         return "Redirect: invalid first argument (of three)";
 
     /*
+     * if we have the 2nd arg and we understand the 1st one, or if we have the
+     * 1st arg but don't understand it, we use the expression syntax assuming
+     * a path from the location.
+     *
+     * if we understand the first arg but have no second arg, we are dealing
+     * with a status like "GONE".
+     */
+    if (grokarg1 && arg2 && !arg3 && HTTP_GONE != status) {
+        const char *expr_err = NULL;
+
+        dirconf->redirect =
+                ap_expr_parse_cmd(cmd, arg2, AP_EXPR_FLAG_STRING_RESULT,
+                        &expr_err, NULL);
+        if (expr_err) {
+            return apr_pstrcat(cmd->temp_pool,
+                    "Cannot parse redirect expression '", arg2, "': ", expr_err,
+                    NULL);
+        }
+
+        dirconf->redirect_status = status;
+        dirconf->redirect_set = 1;
+
+        return NULL;
+
+    }
+    else if (grokarg1 && !arg2 && HTTP_GONE == status) {
+
+        dirconf->redirect_status = status;
+        dirconf->redirect_set = 1;
+
+        return NULL;
+
+    }
+    else if (!grokarg1 && !arg2) {
+        const char *expr_err = NULL;
+
+        dirconf->redirect =
+                ap_expr_parse_cmd(cmd, arg1, AP_EXPR_FLAG_STRING_RESULT,
+                        &expr_err, NULL);
+        if (expr_err) {
+            return apr_pstrcat(cmd->temp_pool,
+                    "Cannot parse redirect expression '", arg1, "': ", expr_err,
+                    NULL);
+        }
+
+        dirconf->redirect_status = status;
+        dirconf->redirect_set = 1;
+
+        return NULL;
+
+    }
+
+    /*
      * if we don't have the 3rd arg and we didn't understand the 1st
      * one, then assume URL-path URL. This also handles case, eg, GONE
      * we even though we don't have a 3rd arg, we did understand the 1st
@@ -269,11 +380,11 @@ static const char *add_redirect_regex(cm
 
 static const command_rec alias_cmds[] =
 {
-    AP_INIT_TAKE2("Alias", add_alias, NULL, RSRC_CONF,
-                  "a fakename and a realname"),
-    AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF,
-                  "a fakename and a realname"),
-    AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
+    AP_INIT_TAKE12("Alias", add_alias, NULL, RSRC_CONF | ACCESS_CONF,
+                  "a fakename and a realname, or a realname in a Location"),
+    AP_INIT_TAKE12("ScriptAlias", add_alias, "cgi-script", RSRC_CONF | ACCESS_CONF,
+                  "a fakename and a realname, or a realname in a Location"),
+    AP_INIT_TAKE123("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
                    OR_FILEINFO,
                    "an optional status, then document to be redirected and "
                    "destination URL"),
@@ -333,8 +444,79 @@ static int alias_matches(const char *uri
     return urip - uri;
 }
 
-static char magic_error_value;
-#define PREGSUB_ERROR      (&magic_error_value)
+static char *try_alias(request_rec *r)
+{
+    alias_dir_conf *dirconf =
+            (alias_dir_conf *) ap_get_module_config(r->per_dir_config, &alias_module);
+
+    if (dirconf->alias) {
+        const char *err = NULL;
+
+        char *found = apr_pstrdup(r->pool,
+                ap_expr_str_exec(r, dirconf->alias, &err));
+        if (err) {
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO()
+                          "Can't evaluate alias expression: %s", err);
+            return PREGSUB_ERROR;
+        }
+
+        if (dirconf->handler) { /* Set handler, and leave a note for mod_cgi */
+            r->handler = dirconf->handler;
+            apr_table_setn(r->notes, "alias-forced-type", r->handler);
+        }
+        /* XXX This is as SLOW as can be, next step, we optimize
+         * and merge to whatever part of the found path was already
+         * canonicalized.  After I finish eliminating os canonical.
+         * Better fail test for ap_server_root_relative needed here.
+         */
+        found = ap_server_root_relative(r->pool, found);
+        return found;
+
+    }
+
+    return NULL;
+}
+
+static char *try_redirect(request_rec *r, int *status)
+{
+    alias_dir_conf *dirconf =
+            (alias_dir_conf *) ap_get_module_config(r->per_dir_config, &alias_module);
+
+    if (dirconf->redirect_set) {
+        apr_uri_t uri;
+        const char *err = NULL;
+        char *found = "";
+
+        if (dirconf->redirect) {
+
+            found = apr_pstrdup(r->pool,
+                    ap_expr_str_exec(r, dirconf->redirect, &err));
+            if (err) {
+                ap_log_rerror(
+                        APLOG_MARK, APLOG_ERR, 0, r, APLOGNO() "Can't evaluate redirect expression: %s", err);
+                return PREGSUB_ERROR;
+            }
+
+            apr_uri_parse(r->pool, found, &uri);
+            /* Do not escape the query string or fragment. */
+            found = apr_uri_unparse(r->pool, &uri, APR_URI_UNP_OMITQUERY);
+            found = ap_escape_uri(r->pool, found);
+            if (uri.query) {
+                found = apr_pstrcat(r->pool, found, "?", uri.query, NULL);
+            }
+            if (uri.fragment) {
+                found = apr_pstrcat(r->pool, found, "#", uri.fragment, NULL);
+            }
+
+        }
+
+        *status = dirconf->redirect_status;
+        return found;
+
+    }
+
+    return NULL;
+}
 
 static char *try_alias_list(request_rec *r, apr_array_header_t *aliases,
                             int is_redir, int *status)
@@ -435,7 +617,9 @@ static int translate_alias_redir(request
         return DECLINED;
     }
 
-    if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) {
+    if ((ret = try_redirect(r, &status)) != NULL
+            || (ret = try_alias_list(r, serverconf->redirects, 1, &status))
+                    != NULL) {
         if (ret == PREGSUB_ERROR)
             return HTTP_INTERNAL_SERVER_ERROR;
         if (ap_is_HTTP_REDIRECT(status)) {
@@ -468,7 +652,9 @@ static int translate_alias_redir(request
         return status;
     }
 
-    if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) {
+    if ((ret = try_alias(r)) != NULL
+            || (ret = try_alias_list(r, serverconf->aliases, 0, &status))
+                    != NULL) {
         r->filename = ret;
         return OK;
     }
@@ -486,7 +672,9 @@ static int fixup_redir(request_rec *r)
 
     /* It may have changed since last time, so try again */
 
-    if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
+    if ((ret = try_redirect(r, &status)) != NULL
+            || (ret = try_alias_list(r, dirconf->redirects, 1, &status))
+                    != NULL) {
         if (ret == PREGSUB_ERROR)
             return HTTP_INTERNAL_SERVER_ERROR;
         if (ap_is_HTTP_REDIRECT(status)) {



Re: svn commit: r1653941 - in /httpd/httpd/trunk: CHANGES docs/manual/expr.xml docs/manual/mod/mod_alias.xml modules/mappers/mod_alias.c

Posted by Graham Leggett <mi...@sharp.fm>.
On 22 Jan 2015, at 7:54 PM, Mike Rumph <mi...@oracle.com> wrote:

> It might be preferred to use unsigned int for bit fields as is used in mod_proxy.h.
> IIRC there have been past discussions on problems resulting from signed bit fields.

Updated in r1653978.

Regards,
Graham
—


Re: svn commit: r1653941 - in /httpd/httpd/trunk: CHANGES docs/manual/expr.xml docs/manual/mod/mod_alias.xml modules/mappers/mod_alias.c

Posted by Mike Rumph <mi...@oracle.com>.
On 1/22/2015 9:02 AM, minfrin@apache.org wrote:
> ==============================================================================
> --- httpd/httpd/trunk/modules/mappers/mod_alias.c (original)
> +++ httpd/httpd/trunk/modules/mappers/mod_alias.c Thu Jan 22 17:02:22 2015
> @@ -34,6 +34,7 @@
>   #include "http_config.h"
>   #include "http_request.h"
>   #include "http_log.h"
> +#include "ap_expr.h"
>   
>   
>   typedef struct {
> @@ -50,11 +51,20 @@ typedef struct {
>   } alias_server_conf;
>   
>   typedef struct {
> +    int alias_set:1;
> +    int redirect_set:1;
>       apr_array_header_t *redirects;
> +    const ap_expr_info_t *alias;
> +    char *handler;
> +    const ap_expr_info_t *redirect;
> +    int redirect_status;                /* 301, 302, 303, 410, etc */
>   } alias_dir_conf;
>
It might be preferred to use unsigned int for bit fields as is used in 
mod_proxy.h.
IIRC there have been past discussions on problems resulting from signed 
bit fields.