Return-Path: X-Original-To: apmail-httpd-dev-archive@www.apache.org Delivered-To: apmail-httpd-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1B46710C4F for ; Sun, 21 Dec 2014 13:48:48 +0000 (UTC) Received: (qmail 91227 invoked by uid 500); 21 Dec 2014 13:48:47 -0000 Delivered-To: apmail-httpd-dev-archive@httpd.apache.org Received: (qmail 91160 invoked by uid 500); 21 Dec 2014 13:48:47 -0000 Mailing-List: contact dev-help@httpd.apache.org; run by ezmlm Precedence: bulk Reply-To: dev@httpd.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list dev@httpd.apache.org Received: (qmail 91149 invoked by uid 99); 21 Dec 2014 13:48:47 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Dec 2014 13:48:47 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of minfrin@sharp.fm designates 80.168.143.5 as permitted sender) Received: from [80.168.143.5] (HELO monica.sharp.fm) (80.168.143.5) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 21 Dec 2014 13:48:41 +0000 Received: from [192.168.43.218] (unknown [41.1.33.4]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) (Authenticated sender: minfrin@sharp.fm) by monica.sharp.fm (Postfix) with ESMTPSA id D6B6F80E47 for ; Sun, 21 Dec 2014 13:48:17 +0000 (GMT) From: Graham Leggett Content-Type: multipart/mixed; boundary="Apple-Mail=_77D46C8B-F340-43B4-9528-FB57A4797CA1" Message-Id: <1E19829D-839B-447D-9B7C-9F345FEDDD96@sharp.fm> Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) Subject: [Patch] Simplifying mod_alias Date: Sun, 21 Dec 2014 15:48:13 +0200 References: <8328F394-A5B6-4EDB-9C31-66C45C488992@sharp.fm> To: dev@httpd.apache.org In-Reply-To: <8328F394-A5B6-4EDB-9C31-66C45C488992@sharp.fm> X-Mailer: Apple Mail (2.1993) X-Virus-Checked: Checked by ClamAV on apache.org --Apple-Mail=_77D46C8B-F340-43B4-9528-FB57A4797CA1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=windows-1252 On 27 Jan 2014, at 12:11 AM, GRAHAM LEGGETT wrote: > A look at mod_alias shows it has 7 directives: >=20 > =95 Alias > =95 AliasMatch > =95 Redirect > =95 RedirectMatch > =95 RedirectPermanent > =95 RedirectTemp > =95 ScriptAlias > =95 ScriptAliasMatch >=20 > In theory we only need these three: >=20 > =95 Alias > =95 Redirect > =95 ScriptAlias >=20 > What I'm keen to do is enable expression support and deprecate all but = the above, with the following as the preferred configuration method = (same as the one used by ProxyPass): >=20 > > Alias /var/lib/bar > =85stuff... > >=20 > or >=20 > [^/]+)> > Alias /var/lib/%{env:MATCH_BAR}/baz > =85stuff... > >=20 > In theory this would be faster as we would not be scanning the list of = Aliases followed by the list of Locations each time, and things get a = lot simpler to use. This patch implements the above. The idea is that the existing syntaxes remain unaltered (and can be = deprecated in future), while we introduce new Location syntaxes with a = single argument, like so: Alias /ftp/pub/image [0-9]+)> Alias /usr/local/apache/errors/%{env:MATCH_NUMBER}.html Redirect permanent http://example.com/two Redirect 303 http://example.com/other [0-9]+)> Redirect permanent http://example.com/errors/%{env:MATCH_NUMBER}.html ScriptAlias /web/cgi-bin/ [0-9]+)> ScriptAlias /web/cgi-bin/errors/%{env:MATCH_NUMBER}.cgi Big win: three fewer reasons to use mod_rewrite (and maybe = mod_vhost_alias). Regards, Graham =97 --Apple-Mail=_77D46C8B-F340-43B4-9528-FB57A4797CA1 Content-Disposition: attachment; filename=httpd-mod_alias-expr.patch Content-Type: application/octet-stream; name="httpd-mod_alias-expr.patch" Content-Transfer-Encoding: 7bit Index: docs/manual/mod/mod_alias.xml =================================================================== --- docs/manual/mod/mod_alias.xml (revision 1647116) +++ docs/manual/mod/mod_alias.xml (working copy) @@ -47,6 +47,15 @@ a different URL. They are often used when a resource has moved to a new location.

+

When the Alias, + ScriptAlias and + Redirect directives are used + within a Location + or LocationMatch + section, expression syntax can be used + to manipulate the destination path or URL. +

+

mod_alias 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 @@ module="mod_alias">Alias, so the latter directive would be ignored.

+

When the Alias, + ScriptAlias and + Redirect directives are used + within a Location + or LocationMatch + section, these directives will take precedence over any globally + defined Alias, + ScriptAlias and + Redirect directives.

+ Alias Maps URLs to filesystem locations -Alias URL-path +Alias [URL-path] file-path|directory-path server configvirtual host +directory @@ -158,6 +178,21 @@

Any number slashes in the URL-path parameter matches any number of slashes in the requested URL-path.

+

If the Alias directive is used within a + Location + or LocationMatch + section the URL-path is omitted, and the file-path is interpreted + using expression syntax.

+ + +<Location /image> + Alias /ftp/pub/image +</Location> +<LocationMatch /error/(?<NUMBER>[0-9]+)> + Alias /usr/local/apache/errors/%{env:MATCH_NUMBER}.html +</LocationMatch> + +
@@ -256,7 +291,7 @@ Redirect Sends an external redirect asking the client to fetch a different URL -Redirect [status] URL-path +Redirect [status] [URL-path] URL server configvirtual host directory.htaccess @@ -297,15 +332,18 @@ Note that POSTs will be discarded.
Only complete path segments are matched, so the above example would not match a request for - http://example.com/servicefoo.txt. For more complex matching - using regular expressions, see the http://example.com/servicefoo.txt. For more complex matching + using the expression syntax, omit the URL-path + argument as described below. Alternatively, for matching using regular + expressions, see the RedirectMatch directive.

Note

Redirect directives take precedence over Alias and ScriptAlias directives, irrespective of their ordering in the configuration - file.

+ file. Redirect directives inside a Location take precedence over + Redirect and Alias directives with an URL-path.

If no status argument is given, the redirect will be "temporary" (HTTP status 302). This indicates to the client @@ -348,6 +386,24 @@ Redirect 303 /three http://example.com/other +

If the Redirect directive is used within a + Location + or LocationMatch + section with the URL-path omitted, then the URL parameter will be + interpreted using expression syntax.

+ + +<Location /one> + Redirect permanent http://example.com/two +</Location>
+<Location /three> + Redirect 303 http://example.com/other +</Location>
+<LocationMatch /error/(?<NUMBER>[0-9]+)> + Redirect permanent http://example.com/errors/%{env:MATCH_NUMBER}.html +</LocationMatch>
+
+ @@ -425,9 +481,10 @@ ScriptAlias Maps a URL to a filesystem location and designates the target as a CGI script -ScriptAlias URL-path +ScriptAlias [URL-path] file-path|directory-path server configvirtual host +directory @@ -491,6 +548,21 @@ of the CGI scripts if they are not restricted by a Directory section. +

If the ScriptAlias directive is used within + a Location + or LocationMatch + section with the URL-path omitted, then the URL parameter will be + interpreted using expression syntax.

+ + +<Location /cgi-bin > + ScriptAlias /web/cgi-bin/ +</Location> +<LocationMatch /cgi-bin/errors/(?<NUMBER>[0-9]+)> + ScriptAlias /web/cgi-bin/errors/%{env:MATCH_NUMBER}.cgi +</LocationMatch>
+
+
CGI Tutorial Index: modules/mappers/mod_alias.c =================================================================== --- modules/mappers/mod_alias.c (revision 1647116) +++ modules/mappers/mod_alias.c (working copy) @@ -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 @@ } 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 @@ (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 @@ /* 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(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 @@ 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 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,9 +444,80 @@ 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 @@ 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 @@ 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 @@ /* 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)) { --Apple-Mail=_77D46C8B-F340-43B4-9528-FB57A4797CA1 Content-Disposition: attachment; filename=httpd-mod_alias-expr-test.patch Content-Type: application/octet-stream; name="httpd-mod_alias-expr-test.patch" Content-Transfer-Encoding: 7bit Index: t/modules/alias.t =================================================================== --- t/modules/alias.t (revision 1632074) +++ t/modules/alias.t (working copy) @@ -36,8 +36,8 @@ #XXX: find something that'll on other platforms (/bin/sh aint it) my $script_tests = WINFU ? 0 : 4; -my $tests = (keys %redirect) + (keys %rm_body) * 10 + - (keys %rm_rc) * 10 + 12 + $script_tests; +my $tests = (keys %redirect) + (keys %rm_body) * 20 + + (keys %rm_rc) * 20 + 23 + $script_tests; #LWP required to follow redirects plan tests => $tests, need need_module('alias'), need_lwp; @@ -60,6 +60,13 @@ "/ali$i"); } +t_debug "verifying expression alias match with /expr/ali[0-9]."; +for (my $i=0 ; $i <= 9 ; $i++) { + ok t_cmp((GET_BODY "/expr/ali$i"), + $i, + "/ali$i"); +} + my ($actual, $expected); foreach (sort keys %redirect) { ## make LWP not follow the redirect since we @@ -84,6 +91,17 @@ } } +print "verifying body of perm and temp redirect match with expression support\n"; +foreach (sort keys %rm_body) { + for (my $i=0 ; $i <= 9 ; $i++) { + $expected = $i; + $actual = GET_BODY "/expr/$_$i"; + ok t_cmp($actual, + $expected, + "/$_$i"); + } +} + print "verifying return code of seeother and gone redirect match\n"; foreach (keys %rm_rc) { ## make LWP not follow the redirect since we @@ -99,6 +117,21 @@ } } +print "verifying return code of seeother and gone redirect match with expression support\n"; +foreach (keys %rm_rc) { + ## make LWP not follow the redirect since we + ## are just interested in the return code. + local $Apache::TestRequest::RedirectOK = 0; + + $expected = $rm_rc{$_}; + for (my $i=0 ; $i <= 9 ; $i++) { + $actual = GET_RC "/expr/$_$i"; + ok t_cmp($actual, + $expected, + "$_$i"); + } +} + ## create a little cgi to test ScriptAlias and ScriptAliasMatch ## my $string = "this is a shell script cgi."; my $cgi =<[0-9])> + Alias @SERVERROOT@/htdocs/modules/alias/%{env:MATCH_NUMBER}.html + + .*)> + ScriptAlias @SERVERROOT@/htdocs/modules/alias/%{env:MATCH_SUFFIX} + + [0-9])> + Redirect permanent http://@SERVERNAME@:@PORT@/alias/%{env:MATCH_NUMBER}.html + + [0-9])> + Redirect temp http://@SERVERNAME@:@PORT@/alias/%{env:MATCH_NUMBER}.html + + [0-9])> + Redirect seeother http://@SERVERNAME@:@PORT@/alias/%{env:MATCH_NUMBER}.html + + + Redirect gone + Alias /manual @inherit_documentroot@/manual --Apple-Mail=_77D46C8B-F340-43B4-9528-FB57A4797CA1--