1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * http_alias.c: Stuff for dealing with directory aliases
20 * Original by Rob McCool, rewritten in succession by David Robinson
25 #include "apr_strings.h"
28 #define APR_WANT_STRFUNC
31 #include "ap_config.h"
33 #include "http_core.h"
34 #include "http_config.h"
35 #include "http_request.h"
44 int redir_status
; /* 301, 302, 303, 410, etc */
48 apr_array_header_t
*aliases
;
49 apr_array_header_t
*redirects
;
53 apr_array_header_t
*redirects
;
56 module AP_MODULE_DECLARE_DATA alias_module
;
58 static void *create_alias_config(apr_pool_t
*p
, server_rec
*s
)
60 alias_server_conf
*a
=
61 (alias_server_conf
*) apr_pcalloc(p
, sizeof(alias_server_conf
));
63 a
->aliases
= apr_array_make(p
, 20, sizeof(alias_entry
));
64 a
->redirects
= apr_array_make(p
, 20, sizeof(alias_entry
));
68 static void *create_alias_dir_config(apr_pool_t
*p
, char *d
)
71 (alias_dir_conf
*) apr_pcalloc(p
, sizeof(alias_dir_conf
));
72 a
->redirects
= apr_array_make(p
, 2, sizeof(alias_entry
));
76 static void *merge_alias_config(apr_pool_t
*p
, void *basev
, void *overridesv
)
78 alias_server_conf
*a
=
79 (alias_server_conf
*) apr_pcalloc(p
, sizeof(alias_server_conf
));
80 alias_server_conf
*base
= (alias_server_conf
*) basev
;
81 alias_server_conf
*overrides
= (alias_server_conf
*) overridesv
;
83 a
->aliases
= apr_array_append(p
, overrides
->aliases
, base
->aliases
);
84 a
->redirects
= apr_array_append(p
, overrides
->redirects
, base
->redirects
);
88 static void *merge_alias_dir_config(apr_pool_t
*p
, void *basev
, void *overridesv
)
91 (alias_dir_conf
*) apr_pcalloc(p
, sizeof(alias_dir_conf
));
92 alias_dir_conf
*base
= (alias_dir_conf
*) basev
;
93 alias_dir_conf
*overrides
= (alias_dir_conf
*) overridesv
;
94 a
->redirects
= apr_array_append(p
, overrides
->redirects
, base
->redirects
);
98 /* need prototype for overlap check */
99 static int alias_matches(const char *uri
, const char *alias_fakename
);
101 static const char *add_alias_internal(cmd_parms
*cmd
, void *dummy
,
102 const char *f
, const char *r
,
105 server_rec
*s
= cmd
->server
;
106 alias_server_conf
*conf
= ap_get_module_config(s
->module_config
,
108 alias_entry
*new = apr_array_push(conf
->aliases
);
109 alias_entry
*entries
= (alias_entry
*)conf
->aliases
->elts
;
112 /* XX r can NOT be relative to DocumentRoot here... compat bug. */
115 new->regexp
= ap_pregcomp(cmd
->pool
, f
, AP_REG_EXTENDED
);
116 if (new->regexp
== NULL
)
117 return "Regular expression could not be compiled.";
121 /* XXX This may be optimized, but we must know that new->real
122 * exists. If so, we can dir merge later, trusing new->real
123 * and just canonicalizing the remainder. Not till I finish
124 * cleaning out the old ap_canonical stuff first.
129 new->handler
= cmd
->info
;
131 /* check for overlapping (Script)Alias directives
132 * and throw a warning if found one
135 for (i
= 0; i
< conf
->aliases
->nelts
- 1; ++i
) {
136 alias_entry
*p
= &entries
[i
];
138 if ( (!p
->regexp
&& alias_matches(f
, p
->fake
) > 0)
139 || (p
->regexp
&& !ap_regexec(p
->regexp
, f
, 0, NULL
, 0))) {
140 ap_log_error(APLOG_MARK
, APLOG_WARNING
, 0, cmd
->server
,
141 "The %s directive in %s at line %d will probably "
142 "never match because it overlaps an earlier "
144 cmd
->cmd
->name
, cmd
->directive
->filename
,
145 cmd
->directive
->line_num
,
146 p
->handler
? "Script" : "",
147 p
->regexp
? "Match" : "");
149 break; /* one warning per alias should be sufficient */
157 static const char *add_alias(cmd_parms
*cmd
, void *dummy
, const char *f
,
160 return add_alias_internal(cmd
, dummy
, f
, r
, 0);
163 static const char *add_alias_regex(cmd_parms
*cmd
, void *dummy
, const char *f
,
166 return add_alias_internal(cmd
, dummy
, f
, r
, 1);
169 static const char *add_redirect_internal(cmd_parms
*cmd
,
170 alias_dir_conf
*dirconf
,
171 const char *arg1
, const char *arg2
,
172 const char *arg3
, int use_regex
)
175 server_rec
*s
= cmd
->server
;
176 alias_server_conf
*serverconf
= ap_get_module_config(s
->module_config
,
178 int status
= (int) (long) cmd
->info
;
179 ap_regex_t
*r
= NULL
;
180 const char *f
= arg2
;
181 const char *url
= arg3
;
183 if (!strcasecmp(arg1
, "gone"))
185 else if (!strcasecmp(arg1
, "permanent"))
186 status
= HTTP_MOVED_PERMANENTLY
;
187 else if (!strcasecmp(arg1
, "temp"))
188 status
= HTTP_MOVED_TEMPORARILY
;
189 else if (!strcasecmp(arg1
, "seeother"))
190 status
= HTTP_SEE_OTHER
;
191 else if (apr_isdigit(*arg1
))
199 r
= ap_pregcomp(cmd
->pool
, f
, AP_REG_EXTENDED
);
201 return "Regular expression could not be compiled.";
204 if (ap_is_HTTP_REDIRECT(status
)) {
206 return "URL to redirect to is missing";
207 /* PR#35314: we can allow path components here;
208 * they get correctly resolved to full URLs.
210 if (!use_regex
&& !ap_is_url(url
) && (url
[0] != '/'))
211 return "Redirect to non-URL";
215 return "Redirect URL not valid for this status";
219 new = apr_array_push(dirconf
->redirects
);
221 new = apr_array_push(serverconf
->redirects
);
226 new->redir_status
= status
;
230 static const char *add_redirect(cmd_parms
*cmd
, void *dirconf
,
231 const char *arg1
, const char *arg2
,
234 return add_redirect_internal(cmd
, dirconf
, arg1
, arg2
, arg3
, 0);
237 static const char *add_redirect2(cmd_parms
*cmd
, void *dirconf
,
238 const char *arg1
, const char *arg2
)
240 return add_redirect_internal(cmd
, dirconf
, arg1
, arg2
, NULL
, 0);
243 static const char *add_redirect_regex(cmd_parms
*cmd
, void *dirconf
,
244 const char *arg1
, const char *arg2
,
247 return add_redirect_internal(cmd
, dirconf
, arg1
, arg2
, arg3
, 1);
250 static const command_rec alias_cmds
[] =
252 AP_INIT_TAKE2("Alias", add_alias
, NULL
, RSRC_CONF
,
253 "a fakename and a realname"),
254 AP_INIT_TAKE2("ScriptAlias", add_alias
, "cgi-script", RSRC_CONF
,
255 "a fakename and a realname"),
256 AP_INIT_TAKE23("Redirect", add_redirect
, (void *) HTTP_MOVED_TEMPORARILY
,
258 "an optional status, then document to be redirected and "
260 AP_INIT_TAKE2("AliasMatch", add_alias_regex
, NULL
, RSRC_CONF
,
261 "a regular expression and a filename"),
262 AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex
, "cgi-script", RSRC_CONF
,
263 "a regular expression and a filename"),
264 AP_INIT_TAKE23("RedirectMatch", add_redirect_regex
,
265 (void *) HTTP_MOVED_TEMPORARILY
, OR_FILEINFO
,
266 "an optional status, then a regular expression and "
268 AP_INIT_TAKE2("RedirectTemp", add_redirect2
,
269 (void *) HTTP_MOVED_TEMPORARILY
, OR_FILEINFO
,
270 "a document to be redirected, then the destination URL"),
271 AP_INIT_TAKE2("RedirectPermanent", add_redirect2
,
272 (void *) HTTP_MOVED_PERMANENTLY
, OR_FILEINFO
,
273 "a document to be redirected, then the destination URL"),
277 static int alias_matches(const char *uri
, const char *alias_fakename
)
279 const char *aliasp
= alias_fakename
, *urip
= uri
;
282 if (*aliasp
== '/') {
283 /* any number of '/' in the alias matches any number in
284 * the supplied URI, but there must be at least one...
291 } while (*aliasp
== '/');
294 } while (*urip
== '/');
297 /* Other characters are compared literally */
298 if (*urip
++ != *aliasp
++)
303 /* Check last alias path component matched all the way */
305 if (aliasp
[-1] != '/' && *urip
!= '\0' && *urip
!= '/')
308 /* Return number of characters from URI which matched (may be
309 * greater than length of alias, since we may have matched
316 static char *try_alias_list(request_rec
*r
, apr_array_header_t
*aliases
,
317 int doesc
, int *status
)
319 alias_entry
*entries
= (alias_entry
*) aliases
->elts
;
320 ap_regmatch_t regm
[AP_MAX_REG_MATCH
];
324 for (i
= 0; i
< aliases
->nelts
; ++i
) {
325 alias_entry
*p
= &entries
[i
];
329 if (!ap_regexec(p
->regexp
, r
->uri
, AP_MAX_REG_MATCH
, regm
, 0)) {
331 found
= ap_pregsub(r
->pool
, p
->real
, r
->uri
,
332 AP_MAX_REG_MATCH
, regm
);
333 if (found
&& doesc
) {
335 apr_uri_parse(r
->pool
, found
, &uri
);
336 /* Do not escape the query string or fragment. */
337 found
= apr_uri_unparse(r
->pool
, &uri
,
338 APR_URI_UNP_OMITQUERY
);
339 found
= ap_escape_uri(r
->pool
, found
);
341 found
= apr_pstrcat(r
->pool
, found
, "?",
345 found
= apr_pstrcat(r
->pool
, found
, "#",
351 /* need something non-null */
352 found
= apr_pstrdup(r
->pool
, "");
357 l
= alias_matches(r
->uri
, p
->fake
);
362 escurl
= ap_os_escape_path(r
->pool
, r
->uri
+ l
, 1);
364 found
= apr_pstrcat(r
->pool
, p
->real
, escurl
, NULL
);
367 found
= apr_pstrcat(r
->pool
, p
->real
, r
->uri
+ l
, NULL
);
372 if (p
->handler
) { /* Set handler, and leave a note for mod_cgi */
373 r
->handler
= p
->handler
;
374 apr_table_setn(r
->notes
, "alias-forced-type", r
->handler
);
376 /* XXX This is as SLOW as can be, next step, we optimize
377 * and merge to whatever part of the found path was already
378 * canonicalized. After I finish eliminating os canonical.
379 * Better fail test for ap_server_root_relative needed here.
382 found
= ap_server_root_relative(r
->pool
, found
);
385 *status
= p
->redir_status
;
395 static int translate_alias_redir(request_rec
*r
)
397 ap_conf_vector_t
*sconf
= r
->server
->module_config
;
398 alias_server_conf
*serverconf
= ap_get_module_config(sconf
, &alias_module
);
402 if (r
->uri
[0] != '/' && r
->uri
[0] != '\0') {
406 if ((ret
= try_alias_list(r
, serverconf
->redirects
, 1, &status
)) != NULL
) {
407 if (ap_is_HTTP_REDIRECT(status
)) {
408 /* include QUERY_STRING if any */
410 ret
= apr_pstrcat(r
->pool
, ret
, "?", r
->args
, NULL
);
412 apr_table_setn(r
->headers_out
, "Location", ret
);
417 if ((ret
= try_alias_list(r
, serverconf
->aliases
, 0, &status
)) != NULL
) {
425 static int fixup_redir(request_rec
*r
)
427 void *dconf
= r
->per_dir_config
;
428 alias_dir_conf
*dirconf
=
429 (alias_dir_conf
*) ap_get_module_config(dconf
, &alias_module
);
433 /* It may have changed since last time, so try again */
435 if ((ret
= try_alias_list(r
, dirconf
->redirects
, 1, &status
)) != NULL
) {
436 if (ap_is_HTTP_REDIRECT(status
)) {
438 char *orig_target
= ret
;
440 ret
= ap_construct_url(r
->pool
, ret
, r
);
441 ap_log_rerror(APLOG_MARK
, APLOG_DEBUG
, 0, r
,
442 "incomplete redirection target of '%s' for "
443 "URI '%s' modified to '%s'",
444 orig_target
, r
->uri
, ret
);
446 if (!ap_is_url(ret
)) {
447 status
= HTTP_INTERNAL_SERVER_ERROR
;
448 ap_log_rerror(APLOG_MARK
, APLOG_ERR
, 0, r
,
449 "cannot redirect '%s' to '%s'; "
450 "target is not a valid absoluteURI or abs_path",
454 /* append requested query only, if the config didn't
457 if (r
->args
&& !ap_strchr(ret
, '?')) {
458 ret
= apr_pstrcat(r
->pool
, ret
, "?", r
->args
, NULL
);
460 apr_table_setn(r
->headers_out
, "Location", ret
);
469 static void register_hooks(apr_pool_t
*p
)
471 static const char * const aszSucc
[]={ "mod_userdir.c",
472 "mod_vhost_alias.c",NULL
};
474 ap_hook_translate_name(translate_alias_redir
,NULL
,aszSucc
,APR_HOOK_MIDDLE
);
475 ap_hook_fixups(fixup_redir
,NULL
,NULL
,APR_HOOK_MIDDLE
);
478 module AP_MODULE_DECLARE_DATA alias_module
=
480 STANDARD20_MODULE_STUFF
,
481 create_alias_dir_config
, /* dir config creater */
482 merge_alias_dir_config
, /* dir merger --- default is to override */
483 create_alias_config
, /* server config */
484 merge_alias_config
, /* merge server configs */
485 alias_cmds
, /* command apr_table_t */
486 register_hooks
/* register hooks */