Skip to content

Commit

Permalink
Merge r1909137, r1911067 from trunk:
Browse files Browse the repository at this point in the history
mod_alias: When an alias is declared inside a Location, make sure
the balance of the URL is preserved to match the alias declared
outside a location. Fixes an error where all requests are mapped
to the root of the location.


mod_alias: Add AliasPreservePath directive to map the full
path after the alias in a location.

Submitted by: minfrin
Reviewed by: minfrin, rpluem, covener


git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1913009 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
rpluem committed Oct 16, 2023
1 parent b763f5f commit 3f02e37
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ Changes with Apache 2.4.58
*) mod_dav: Add DavBasePath directive to configure the repository root
path. PR 35077. [Joe Orton]

*) mod_alias: Add AliasPreservePath directive to map the full
path after the alias in a location. [Graham Leggett]

*) mod_alias: Add RedirectRelative to allow relative redirect targets to be
issued as-is. [Eric Covener, Graham Leggett]

Expand Down
9 changes: 0 additions & 9 deletions STATUS
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,6 @@ RELEASE SHOWSTOPPERS:
PATCHES ACCEPTED TO BACKPORT FROM TRUNK:
[ start all new proposals below, under PATCHES PROPOSED. ]

*) mod_alias: Add AliasPreservePath directive to map the full path after the
alias in a location. Requires r1861542 / r1861569 above.
Trunk version of patch:
https://svn.apache.org/r1909137
https://svn.apache.org/r1911067
Backport version for 2.4.x of patch:
https://svn.apache.org/repos/asf/httpd/httpd/patches/2.4.x/alias-preserve-path3.diff
+1: minfrin, rpluem, covener

*) mod_ssl: Silence info log message "SSL Library Error: error:0A000126:
SSL routines::unexpected eof while reading" when using
OpenSSL 3 by setting SSL_OP_IGNORE_UNEXPECTED_EOF if
Expand Down
43 changes: 43 additions & 0 deletions docs/manual/mod/mod_alias.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ Alias "/image" "/ftp/pub/image"
</LocationMatch>
</highlight>

<p>Note that when the <directive>AliasPreservePath</directive>
directive is on, the full path is mapped to the destination. When
the directive is off, all URLs are mapped to the single target
URL.</p>

<highlight language="config">
# /files/foo and /files/bar mapped to /ftp/pub/files/foo and /ftp/pub/files/bar
&lt;Location "/files"&gt;
AliasPreservePath on
Alias "/ftp/pub/files"
&lt;/Location&gt;
# /errors/foo and /errors/bar mapped to /var/www/errors.html
&lt;Location "/errors"&gt;
AliasPreservePath off
Alias "/var/www/errors.html"
&lt;/Location&gt;
</highlight>

</usage>
</directivesynopsis>

Expand Down Expand Up @@ -638,5 +656,30 @@ ScriptAliasMatch "(?i)^/cgi-bin(.*)" "/usr/local/apache/cgi-bin$1"
</usage>
</directivesynopsis>

<directivesynopsis>
<name>AliasPreservePath</name>
<description>Map the full path after the alias in a location.</description>
<syntax>AliasPreservePath OFF|ON</syntax>
<default>AliasPreservePath OFF</default>
<contextlist><context>server config</context><context>virtual host</context>
<context>directory</context>
</contextlist>
<compatibility>2.5.1 and later</compatibility>

<usage>
<p>When using the two parameter version of the
<directive>Alias</directive> directive, the full path after the alias
is preserved. When using the one parameter version of the
<directive>Alias</directive> directive inside a
<directive>Location</directive> directive, the full path is dropped,
and all URLs are mapped to the target expression.</p>

<p>To make the one parameter version of the
<directive>Alias</directive> directive preserve paths in the same way
that the two parameter version of the <directive>Alias</directive>
directive, enable this setting.</p>

</usage>
</directivesynopsis>

</modulesynopsis>
25 changes: 25 additions & 0 deletions modules/mappers/mod_alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define ALIAS_FLAG_OFF 0
#define ALIAS_FLAG_ON 1

#define ALIAS_PRESERVE_PATH_DEFAULT 0

typedef struct {
const char *real;
const char *fake;
Expand All @@ -59,10 +61,12 @@ typedef struct {
unsigned int redirect_set:1;
apr_array_header_t *redirects;
const ap_expr_info_t *alias;
const char *alias_fake;
char *handler;
const ap_expr_info_t *redirect;
int redirect_status; /* 301, 302, 303, 410, etc */
int allow_relative; /* skip ap_construct_url() */
int alias_preserve_path; /* map full path */
} alias_dir_conf;

module AP_MODULE_DECLARE_DATA alias_module;
Expand All @@ -86,6 +90,7 @@ static void *create_alias_dir_config(apr_pool_t *p, char *d)
(alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
a->allow_relative = ALIAS_FLAG_DEFAULT;
a->alias_preserve_path = ALIAS_FLAG_DEFAULT;
return a;
}

Expand All @@ -111,6 +116,7 @@ static void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv
a->redirects = apr_array_append(p, overrides->redirects, base->redirects);

a->alias = (overrides->alias_set == 0) ? base->alias : overrides->alias;
a->alias_fake = (overrides->alias_set == 0) ? base->alias_fake : overrides->alias_fake;
a->handler = (overrides->alias_set == 0) ? base->handler : overrides->handler;
a->alias_set = overrides->alias_set || base->alias_set;

Expand All @@ -120,6 +126,10 @@ static void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv
a->allow_relative = (overrides->allow_relative != ALIAS_FLAG_DEFAULT)
? overrides->allow_relative
: base->allow_relative;
a->alias_preserve_path = (overrides->alias_preserve_path != ALIAS_FLAG_DEFAULT)
? overrides->alias_preserve_path
: base->alias_preserve_path;

return a;
}

Expand Down Expand Up @@ -218,6 +228,7 @@ static const char *add_alias(cmd_parms *cmd, void *dummy, const char *fake,
NULL);
}

dirconf->alias_fake = cmd->path;
dirconf->handler = cmd->info;
dirconf->alias_set = 1;

Expand Down Expand Up @@ -436,6 +447,17 @@ static char *try_alias(request_rec *r)
return PREGSUB_ERROR;
}

if (dirconf->alias_fake && dirconf->alias_preserve_path == ALIAS_FLAG_ON) {
int l;

l = alias_matches(r->uri, dirconf->alias_fake);

if (l > 0) {
ap_set_context_info(r, dirconf->alias_fake, found);
found = apr_pstrcat(r->pool, found, r->uri + l, NULL);
}
}

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);
Expand Down Expand Up @@ -715,6 +737,9 @@ static const command_rec alias_cmds[] =
AP_INIT_FLAG("RedirectRelative", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, allow_relative), OR_FILEINFO,
"Set to ON to allow relative redirect targets to be issued as-is"),
AP_INIT_FLAG("AliasPreservePath", ap_set_flag_slot,
(void*)APR_OFFSETOF(alias_dir_conf, alias_preserve_path), OR_FILEINFO,
"Set to ON to map the full path after the fakename to the realname."),

{NULL}
};
Expand Down

0 comments on commit 3f02e37

Please sign in to comment.