From: Jim Jagielski
Date: Thu, 18 Feb 2016 15:04:17 +0000 (+0000)
Subject: Merge r1653941, r1653978, r1656225, r1686853, r1686856 from trunk:
X-Git-Tag: 2.4.19~180
X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3564b9061c73a443ef3e93d25c5c1c4fedca4af1;p=thirdparty%2Fapache%2Fhttpd.git
Merge r1653941, r1653978, r1656225, r1686853, r1686856 from trunk:
mod_alias: Introduce expression parser support for Alias, ScriptAlias
and Redirect.
Use unsigned bit fields.
mod_alias: follow up to r1653941.
Fill empty APLOGNO().
mod_alias: follow up to r1653941.
Limit Redirect expressions to directory (Location) context
and redirect statuses (implicit or explicit).
mod_alias: follow up to r1686853.
Factorize code (no functional change).
Submitted by: minfrin, ylavic, ylavic, ylavic
Reviewed/backported by: jim
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1731081 13f79535-47bb-0310-9956-ffa450edef68
---
diff --git a/CHANGES b/CHANGES
index a01e4643b8a..6bbe8fe3970 100644
--- a/CHANGES
+++ b/CHANGES
@@ -230,6 +230,11 @@ Changes with Apache 2.4.17
records for scalability. [Yingqi Lu ,
Jeff Trawick, Jim Jagielski, Yann Ylavic]
+ *) mod_alias: Introduce expression parser support for Alias, ScriptAlias
+ and Redirect. Limit Redirect expressions to directory (Location) context
+ and redirect statuses (implicit or explicit).
+ [Graham Leggett, Yann Ylavic, Ruediger Pluem]
+
*) mod_proxy: Fix a race condition that caused a failed worker to be retried
before the retry period is over. [Ruediger Pluem]
diff --git a/STATUS b/STATUS
index 4a89e98a072..62b96e721d8 100644
--- a/STATUS
+++ b/STATUS
@@ -112,17 +112,6 @@ RELEASE SHOWSTOPPERS:
PATCHES ACCEPTED TO BACKPORT FROM TRUNK:
[ start all new proposals below, under PATCHES PROPOSED. ]
- *) mod_alias: Introduce expression parser support for Alias, ScriptAlias
- and Redirect. Limit Redirect expressions to directory (Location) context
- and redirect statuses (implicit or explicit).
- trunk patch: http://svn.apache.org/r1653941
- http://svn.apache.org/r1653978
- http://svn.apache.org/r1656225
- http://svn.apache.org/r1686853
- http://svn.apache.org/r1686856
- 2.4.x patch: https://people.apache.org/~minfrin/httpd-mod_alias-expr2.patch
- +1: minfrin, jim, rpluem
-
*) mod_cache_socache: Fix a possible cached entity body corruption when it
is received from an origin server in multiple batches and forwarded by
mod_proxy.
diff --git a/docs/manual/expr.xml b/docs/manual/expr.xml
index 81e95922b72..ca36fa2ef10 100644
--- a/docs/manual/expr.xml
+++ b/docs/manual/expr.xml
@@ -41,6 +41,9 @@
ElseIf
Else
ErrorDocument
+Alias
+ScriptAlias
+Redirect
AuthBasicFake
AuthFormLoginRequiredLocation
AuthFormLoginSuccessLocation
diff --git a/docs/manual/mod/mod_alias.xml b/docs/manual/mod/mod_alias.xml
index ead4e14a50f..7025970d857 100644
--- a/docs/manual/mod/mod_alias.xml
+++ b/docs/manual/mod/mod_alias.xml
@@ -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
@@ -89,14 +98,25 @@ Alias "/foo" "/gaq"
would always match before the /foo/bar
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
@@ -156,6 +176,21 @@ Alias "/image" "/ftp/pub/image"
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>
+
+
@@ -254,7 +289,7 @@ AliasMatch "^/image/(.*)\.gif$" "/files/gif.images/$1.gif"
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
@@ -295,15 +330,18 @@ Redirect "/one" "/two"
Note that POST
s 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
@@ -345,6 +383,24 @@ Redirect "/one" "/two"
Redirect permanent "/one" "http://example.com/two"
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>
+
+
@@ -422,9 +478,10 @@ a different URL
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
@@ -487,6 +544,21 @@ ScriptAlias "/cgi-bin/" "/web/cgi-handler.pl"
ScriptAlias and revealing the source code
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
diff --git a/modules/mappers/mod_alias.c b/modules/mappers/mod_alias.c
index dfba13320e4..22a90aa80c2 100644
--- a/modules/mappers/mod_alias.c
+++ b/modules/mappers/mod_alias.c
@@ -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 {
+ unsigned int alias_set:1;
+ unsigned 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_pool_t *p, void *basev, void *overridesv
(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(cmd_parms *cmd, void *dummy,
/* 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(cmd_parms *cmd, void *dummy,
}
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,
@@ -194,16 +252,64 @@ static const char *add_redirect_internal(cmd_parms *cmd,
status = HTTP_MOVED_TEMPORARILY;
else if (!strcasecmp(arg1, "seeother"))
status = HTTP_SEE_OTHER;
- else if (!strcasecmp(arg1, "gone"))
+ else if (!strcasecmp(arg1, "gone")) {
status = HTTP_GONE;
- else if (apr_isdigit(*arg1))
+ grokarg1 = -1;
+ }
+ else if (apr_isdigit(*arg1)) {
status = atoi(arg1);
- else
+ if (!ap_is_HTTP_REDIRECT(status)) {
+ grokarg1 = -1;
+ }
+ }
+ else {
grokarg1 = 0;
+ }
if (arg3 && !grokarg1)
return "Redirect: invalid first argument (of three)";
+ /*
+ * if we have the 2nd arg and we understand the 1st one as a redirect
+ * status (3xx, but not things like 404 /robots.txt), 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" or a non-redirect status (e.g. 404, 503).
+ */
+ if (!cmd->path) {
+ /* context only for now */
+ ;
+ }
+ else if ((grokarg1 > 0 && arg2 && !arg3) || (!grokarg1 && !arg2)) {
+ const char *expr_err = NULL;
+
+ url = grokarg1 ? arg2 : arg1;
+ dirconf->redirect =
+ ap_expr_parse_cmd(cmd, url, AP_EXPR_FLAG_STRING_RESULT,
+ &expr_err, NULL);
+ if (expr_err) {
+ return apr_pstrcat(cmd->temp_pool,
+ "Cannot parse redirect expression '", url, "': ", expr_err,
+ NULL);
+ }
+
+ dirconf->redirect_status = status;
+ dirconf->redirect_set = 1;
+
+ return NULL;
+
+ }
+ else if (grokarg1 < 0 && !arg2) {
+
+ 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
@@ -269,11 +375,11 @@ static const char *add_redirect_regex(cmd_parms *cmd, void *dirconf,
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 +439,79 @@ static int alias_matches(const char *uri, const char *alias_fakename)
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(02825)
+ "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(02826)
+ "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 +612,9 @@ static int translate_alias_redir(request_rec *r)
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 +647,9 @@ static int translate_alias_redir(request_rec *r)
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 +667,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)) {