/[Apache-SVN]/httpd/httpd/trunk/modules/mappers/mod_alias.c
ViewVC logotype

Contents of /httpd/httpd/trunk/modules/mappers/mod_alias.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 785575 - (hide annotations)
Wed Jun 17 12:45:21 2009 UTC (5 months, 1 week ago) by niq
File MIME type: text/plain
File size: 18282 byte(s)
mod_alias: Ensure Redirect issues a valid URL
PR 44020
Patch by HÃ¥kon Stordahl
1 fielding 420983 /* 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
7 fielding 83751 *
8 nd 102525 * http://www.apache.org/licenses/LICENSE-2.0
9 fielding 83751 *
10 nd 102525 * 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.
15 fielding 83751 */
16    
17     /*
18     * http_alias.c: Stuff for dealing with directory aliases
19 jim 332306 *
20 fielding 83751 * Original by Rob McCool, rewritten in succession by David Robinson
21     * and rst.
22 jim 332306 *
23 fielding 83751 */
24    
25 rbb 85867 #include "apr_strings.h"
26 gstein 88060 #include "apr_lib.h"
27    
28     #define APR_WANT_STRFUNC
29     #include "apr_want.h"
30    
31 rbb 84531 #include "ap_config.h"
32 fielding 83751 #include "httpd.h"
33 coar 93143 #include "http_core.h"
34 fielding 83751 #include "http_config.h"
35 fielding 83770 #include "http_request.h"
36 coar 93137 #include "http_log.h"
37 fielding 83751
38 gstein 88060
39 fielding 83751 typedef struct {
40 ben 85644 const char *real;
41     const char *fake;
42 fielding 83751 char *handler;
43 jorton 153384 ap_regex_t *regexp;
44 jerenkrantz 95503 int redir_status; /* 301, 302, 303, 410, etc */
45 fielding 83751 } alias_entry;
46    
47     typedef struct {
48 dougm 85976 apr_array_header_t *aliases;
49     apr_array_header_t *redirects;
50 fielding 83751 } alias_server_conf;
51    
52     typedef struct {
53 dougm 85976 apr_array_header_t *redirects;
54 fielding 83751 } alias_dir_conf;
55    
56 wrowe 86609 module AP_MODULE_DECLARE_DATA alias_module;
57 fielding 83751
58 dougm 85976 static void *create_alias_config(apr_pool_t *p, server_rec *s)
59 fielding 83751 {
60     alias_server_conf *a =
61 dougm 85976 (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
62 fielding 83751
63 dougm 88019 a->aliases = apr_array_make(p, 20, sizeof(alias_entry));
64     a->redirects = apr_array_make(p, 20, sizeof(alias_entry));
65 fielding 83751 return a;
66     }
67    
68 dougm 85976 static void *create_alias_dir_config(apr_pool_t *p, char *d)
69 fielding 83751 {
70     alias_dir_conf *a =
71 dougm 85976 (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
72 dougm 88019 a->redirects = apr_array_make(p, 2, sizeof(alias_entry));
73 fielding 83751 return a;
74     }
75    
76 dougm 85976 static void *merge_alias_config(apr_pool_t *p, void *basev, void *overridesv)
77 fielding 83751 {
78     alias_server_conf *a =
79 dougm 85976 (alias_server_conf *) apr_pcalloc(p, sizeof(alias_server_conf));
80 jerenkrantz 95503 alias_server_conf *base = (alias_server_conf *) basev;
81     alias_server_conf *overrides = (alias_server_conf *) overridesv;
82 fielding 83751
83 dougm 88019 a->aliases = apr_array_append(p, overrides->aliases, base->aliases);
84     a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
85 fielding 83751 return a;
86     }
87    
88 dougm 85976 static void *merge_alias_dir_config(apr_pool_t *p, void *basev, void *overridesv)
89 fielding 83751 {
90     alias_dir_conf *a =
91 dougm 85976 (alias_dir_conf *) apr_pcalloc(p, sizeof(alias_dir_conf));
92 jerenkrantz 95503 alias_dir_conf *base = (alias_dir_conf *) basev;
93     alias_dir_conf *overrides = (alias_dir_conf *) overridesv;
94 dougm 88019 a->redirects = apr_array_append(p, overrides->redirects, base->redirects);
95 fielding 83751 return a;
96     }
97    
98 nd 98647 /* need prototype for overlap check */
99     static int alias_matches(const char *uri, const char *alias_fakename);
100    
101 ben 85644 static const char *add_alias_internal(cmd_parms *cmd, void *dummy,
102 jerenkrantz 95503 const char *f, const char *r,
103     int use_regex)
104 fielding 83751 {
105     server_rec *s = cmd->server;
106 gstein 88225 alias_server_conf *conf = ap_get_module_config(s->module_config,
107     &alias_module);
108 dougm 88019 alias_entry *new = apr_array_push(conf->aliases);
109 nd 98647 alias_entry *entries = (alias_entry *)conf->aliases->elts;
110     int i;
111 fielding 83751
112     /* XX r can NOT be relative to DocumentRoot here... compat bug. */
113    
114     if (use_regex) {
115 jorton 153384 new->regexp = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
116 jerenkrantz 95503 if (new->regexp == NULL)
117     return "Regular expression could not be compiled.";
118 rbb 88010 new->real = r;
119 fielding 83751 }
120 wrowe 90562 else {
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.
125     */
126     new->real = r;
127     }
128 fielding 83751 new->fake = f;
129     new->handler = cmd->info;
130    
131 nd 98647 /* check for overlapping (Script)Alias directives
132     * and throw a warning if found one
133     */
134     if (!use_regex) {
135     for (i = 0; i < conf->aliases->nelts - 1; ++i) {
136     alias_entry *p = &entries[i];
137    
138 trawick 98741 if ( (!p->regexp && alias_matches(f, p->fake) > 0)
139     || (p->regexp && !ap_regexec(p->regexp, f, 0, NULL, 0))) {
140 nd 98647 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
141 nd 102949 "The %s directive in %s at line %d will probably "
142     "never match because it overlaps an earlier "
143     "%sAlias%s.",
144     cmd->cmd->name, cmd->directive->filename,
145     cmd->directive->line_num,
146 nd 98657 p->handler ? "Script" : "",
147     p->regexp ? "Match" : "");
148 nd 98647
149     break; /* one warning per alias should be sufficient */
150     }
151     }
152     }
153    
154 fielding 83751 return NULL;
155     }
156    
157 ben 85644 static const char *add_alias(cmd_parms *cmd, void *dummy, const char *f,
158 jerenkrantz 95503 const char *r)
159 fielding 83751 {
160     return add_alias_internal(cmd, dummy, f, r, 0);
161     }
162    
163 ben 85644 static const char *add_alias_regex(cmd_parms *cmd, void *dummy, const char *f,
164 jerenkrantz 95503 const char *r)
165 fielding 83751 {
166     return add_alias_internal(cmd, dummy, f, r, 1);
167     }
168    
169 ben 85644 static const char *add_redirect_internal(cmd_parms *cmd,
170 jerenkrantz 95503 alias_dir_conf *dirconf,
171 jim 332306 const char *arg1, const char *arg2,
172 trawick 85627 const char *arg3, int use_regex)
173 fielding 83751 {
174     alias_entry *new;
175     server_rec *s = cmd->server;
176 gstein 88225 alias_server_conf *serverconf = ap_get_module_config(s->module_config,
177     &alias_module);
178 fielding 83751 int status = (int) (long) cmd->info;
179 jim 780692 int grokarg1 = 1;
180 jim 780697 ap_regex_t *r = NULL;
181 ben 85644 const char *f = arg2;
182     const char *url = arg3;
183 fielding 83751
184 jim 780692 /*
185     * Logic flow:
186     * Go ahead and try to grok the 1st arg, in case it is a
187     * Redirect status. Now if we have 3 args, we expect that
188     * we were able to understand that 1st argument (it's something
189 jim 780699 * we expected, so if not, then we bail
190 jim 780692 */
191     if (!strcasecmp(arg1, "permanent"))
192     status = HTTP_MOVED_PERMANENTLY;
193     else if (!strcasecmp(arg1, "temp"))
194     status = HTTP_MOVED_TEMPORARILY;
195     else if (!strcasecmp(arg1, "seeother"))
196     status = HTTP_SEE_OTHER;
197     else if (!strcasecmp(arg1, "gone"))
198 jim 780648 status = HTTP_GONE;
199 rpluem 780655 else if (apr_isdigit(*arg1))
200     status = atoi(arg1);
201 jim 780692 else
202     grokarg1 = 0;
203    
204     if (arg3 && !grokarg1)
205     return "Redirect: invalid first argument (of three)";
206    
207     /*
208     * if we don't have the 3rd arg and we didn't understand the 1st
209     * one, then assume URL-path URL. This also handles case, eg, GONE
210     * we even though we don't have a 3rd arg, we did understand the 1st
211     * one, so we don't want to re-arrange
212     */
213     if (!arg3 && !grokarg1) {
214 jerenkrantz 95503 f = arg1;
215     url = arg2;
216 fielding 83751 }
217    
218     if (use_regex) {
219 jorton 153384 r = ap_pregcomp(cmd->pool, f, AP_REG_EXTENDED);
220 jerenkrantz 95503 if (r == NULL)
221     return "Regular expression could not be compiled.";
222 fielding 83751 }
223    
224     if (ap_is_HTTP_REDIRECT(status)) {
225 jerenkrantz 95503 if (!url)
226     return "URL to redirect to is missing";
227 niq 490142 /* PR#35314: we can allow path components here;
228     * they get correctly resolved to full URLs.
229     */
230     if (!use_regex && !ap_is_url(url) && (url[0] != '/'))
231 jerenkrantz 95503 return "Redirect to non-URL";
232 fielding 83751 }
233     else {
234 jerenkrantz 95503 if (url)
235     return "Redirect URL not valid for this status";
236 fielding 83751 }
237    
238     if (cmd->path)
239 jerenkrantz 95503 new = apr_array_push(dirconf->redirects);
240 fielding 83751 else
241 jerenkrantz 95503 new = apr_array_push(serverconf->redirects);
242 fielding 83751
243     new->fake = f;
244     new->real = url;
245     new->regexp = r;
246     new->redir_status = status;
247     return NULL;
248     }
249    
250 ben 85644 static const char *add_redirect(cmd_parms *cmd, void *dirconf,
251     const char *arg1, const char *arg2,
252 jerenkrantz 95503 const char *arg3)
253 fielding 83751 {
254     return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 0);
255     }
256    
257 ben 85644 static const char *add_redirect2(cmd_parms *cmd, void *dirconf,
258 jerenkrantz 95503 const char *arg1, const char *arg2)
259 fielding 83751 {
260 ben 85644 return add_redirect_internal(cmd, dirconf, arg1, arg2, NULL, 0);
261     }
262    
263     static const char *add_redirect_regex(cmd_parms *cmd, void *dirconf,
264 jerenkrantz 95503 const char *arg1, const char *arg2,
265     const char *arg3)
266 ben 85644 {
267 fielding 83751 return add_redirect_internal(cmd, dirconf, arg1, arg2, arg3, 1);
268     }
269    
270     static const command_rec alias_cmds[] =
271     {
272 trawick 85627 AP_INIT_TAKE2("Alias", add_alias, NULL, RSRC_CONF,
273     "a fakename and a realname"),
274     AP_INIT_TAKE2("ScriptAlias", add_alias, "cgi-script", RSRC_CONF,
275     "a fakename and a realname"),
276     AP_INIT_TAKE23("Redirect", add_redirect, (void *) HTTP_MOVED_TEMPORARILY,
277     OR_FILEINFO,
278     "an optional status, then document to be redirected and "
279     "destination URL"),
280     AP_INIT_TAKE2("AliasMatch", add_alias_regex, NULL, RSRC_CONF,
281     "a regular expression and a filename"),
282     AP_INIT_TAKE2("ScriptAliasMatch", add_alias_regex, "cgi-script", RSRC_CONF,
283     "a regular expression and a filename"),
284 jim 332306 AP_INIT_TAKE23("RedirectMatch", add_redirect_regex,
285 trawick 85627 (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO,
286     "an optional status, then a regular expression and "
287     "destination URL"),
288 ben 85644 AP_INIT_TAKE2("RedirectTemp", add_redirect2,
289 jerenkrantz 95503 (void *) HTTP_MOVED_TEMPORARILY, OR_FILEINFO,
290 trawick 85627 "a document to be redirected, then the destination URL"),
291 jim 332306 AP_INIT_TAKE2("RedirectPermanent", add_redirect2,
292 trawick 85627 (void *) HTTP_MOVED_PERMANENTLY, OR_FILEINFO,
293     "a document to be redirected, then the destination URL"),
294 fielding 83751 {NULL}
295     };
296    
297     static int alias_matches(const char *uri, const char *alias_fakename)
298     {
299     const char *aliasp = alias_fakename, *urip = uri;
300    
301 brianp 92138 while (*aliasp) {
302 jerenkrantz 95503 if (*aliasp == '/') {
303     /* any number of '/' in the alias matches any number in
304     * the supplied URI, but there must be at least one...
305     */
306     if (*urip != '/')
307     return 0;
308 fielding 83751
309 brianp 95584 do {
310 jerenkrantz 95503 ++aliasp;
311 brianp 95584 } while (*aliasp == '/');
312     do {
313 jerenkrantz 95503 ++urip;
314 brianp 95584 } while (*urip == '/');
315 jerenkrantz 95503 }
316     else {
317     /* Other characters are compared literally */
318     if (*urip++ != *aliasp++)
319     return 0;
320     }
321 fielding 83751 }
322    
323     /* Check last alias path component matched all the way */
324    
325     if (aliasp[-1] != '/' && *urip != '\0' && *urip != '/')
326 jerenkrantz 95503 return 0;
327 fielding 83751
328     /* Return number of characters from URI which matched (may be
329     * greater than length of alias, since we may have matched
330     * doubled slashes)
331     */
332    
333     return urip - uri;
334     }
335    
336 jerenkrantz 95503 static char *try_alias_list(request_rec *r, apr_array_header_t *aliases,
337     int doesc, int *status)
338 fielding 83751 {
339     alias_entry *entries = (alias_entry *) aliases->elts;
340 jorton 153384 ap_regmatch_t regm[AP_MAX_REG_MATCH];
341 fielding 83751 char *found = NULL;
342     int i;
343    
344     for (i = 0; i < aliases->nelts; ++i) {
345 jerenkrantz 95503 alias_entry *p = &entries[i];
346     int l;
347 fielding 83751
348 jerenkrantz 95503 if (p->regexp) {
349 striker 101556 if (!ap_regexec(p->regexp, r->uri, AP_MAX_REG_MATCH, regm, 0)) {
350 jerenkrantz 95503 if (p->real) {
351     found = ap_pregsub(r->pool, p->real, r->uri,
352 striker 101556 AP_MAX_REG_MATCH, regm);
353 jerenkrantz 95503 if (found && doesc) {
354 dougm 90361 apr_uri_t uri;
355     apr_uri_parse(r->pool, found, &uri);
356 jerenkrantz 91672 /* Do not escape the query string or fragment. */
357 jim 332306 found = apr_uri_unparse(r->pool, &uri,
358 jerenkrantz 91672 APR_URI_UNP_OMITQUERY);
359     found = ap_escape_uri(r->pool, found);
360 rbb 89041 if (uri.query) {
361 jim 332306 found = apr_pstrcat(r->pool, found, "?",
362 jerenkrantz 91672 uri.query, NULL);
363 rbb 89041 }
364 jerenkrantz 91672 if (uri.fragment) {
365 jim 332306 found = apr_pstrcat(r->pool, found, "#",
366 jerenkrantz 91672 uri.fragment, NULL);
367 rbb 89041 }
368 jerenkrantz 95503 }
369     }
370     else {
371     /* need something non-null */
372     found = apr_pstrdup(r->pool, "");
373     }
374     }
375     }
376     else {
377     l = alias_matches(r->uri, p->fake);
378 fielding 83751
379 jerenkrantz 95503 if (l > 0) {
380     if (doesc) {
381     char *escurl;
382     escurl = ap_os_escape_path(r->pool, r->uri + l, 1);
383 fielding 83751
384 jerenkrantz 95503 found = apr_pstrcat(r->pool, p->real, escurl, NULL);
385     }
386     else
387     found = apr_pstrcat(r->pool, p->real, r->uri + l, NULL);
388     }
389     }
390 fielding 83751
391 jerenkrantz 95503 if (found) {
392     if (p->handler) { /* Set handler, and leave a note for mod_cgi */
393     r->handler = p->handler;
394     apr_table_setn(r->notes, "alias-forced-type", r->handler);
395     }
396 wrowe 90562 /* XXX This is as SLOW as can be, next step, we optimize
397     * and merge to whatever part of the found path was already
398     * canonicalized. After I finish eliminating os canonical.
399 wrowe 93965 * Better fail test for ap_server_root_relative needed here.
400 wrowe 90562 */
401 wrowe 93965 if (!doesc) {
402 wrowe 90562 found = ap_server_root_relative(r->pool, found);
403 wrowe 93965 }
404     if (found) {
405 jerenkrantz 95503 *status = p->redir_status;
406 wrowe 93965 }
407 jerenkrantz 95503 return found;
408     }
409 fielding 83751
410     }
411    
412     return NULL;
413     }
414    
415     static int translate_alias_redir(request_rec *r)
416     {
417 gstein 88225 ap_conf_vector_t *sconf = r->server->module_config;
418     alias_server_conf *serverconf = ap_get_module_config(sconf, &alias_module);
419 fielding 83751 char *ret;
420     int status;
421    
422 jerenkrantz 95503 if (r->uri[0] != '/' && r->uri[0] != '\0') {
423     return DECLINED;
424     }
425 fielding 83751
426     if ((ret = try_alias_list(r, serverconf->redirects, 1, &status)) != NULL) {
427 jerenkrantz 95503 if (ap_is_HTTP_REDIRECT(status)) {
428 niq 785575 if (ret[0] == '/') {
429     char *orig_target = ret;
430    
431     ret = ap_construct_url(r->pool, ret, r);
432     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
433     "incomplete redirection target of '%s' for "
434     "URI '%s' modified to '%s'",
435     orig_target, r->uri, ret);
436 jerenkrantz 95503 }
437 niq 785575 if (!ap_is_url(ret)) {
438     status = HTTP_INTERNAL_SERVER_ERROR;
439     ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
440     "cannot redirect '%s' to '%s'; "
441     "target is not a valid absoluteURI or abs_path",
442     r->uri, ret);
443     }
444     else {
445     /* append requested query only, if the config didn't
446     * supply its own.
447     */
448     if (r->args && !ap_strchr(ret, '?')) {
449     ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
450     }
451     apr_table_setn(r->headers_out, "Location", ret);
452     }
453 jerenkrantz 95503 }
454     return status;
455 fielding 83751 }
456    
457     if ((ret = try_alias_list(r, serverconf->aliases, 0, &status)) != NULL) {
458 jerenkrantz 95503 r->filename = ret;
459     return OK;
460 fielding 83751 }
461    
462     return DECLINED;
463     }
464    
465     static int fixup_redir(request_rec *r)
466     {
467     void *dconf = r->per_dir_config;
468     alias_dir_conf *dirconf =
469     (alias_dir_conf *) ap_get_module_config(dconf, &alias_module);
470     char *ret;
471     int status;
472    
473     /* It may have changed since last time, so try again */
474    
475     if ((ret = try_alias_list(r, dirconf->redirects, 1, &status)) != NULL) {
476 coar 93137 if (ap_is_HTTP_REDIRECT(status)) {
477 coar 93143 if (ret[0] == '/') {
478     char *orig_target = ret;
479    
480     ret = ap_construct_url(r->pool, ret, r);
481 trawick 95150 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
482 coar 93143 "incomplete redirection target of '%s' for "
483     "URI '%s' modified to '%s'",
484     orig_target, r->uri, ret);
485     }
486 coar 93137 if (!ap_is_url(ret)) {
487     status = HTTP_INTERNAL_SERVER_ERROR;
488 trawick 95150 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
489 coar 93137 "cannot redirect '%s' to '%s'; "
490 coar 93143 "target is not a valid absoluteURI or abs_path",
491 coar 93137 r->uri, ret);
492     }
493     else {
494 nd 98796 /* append requested query only, if the config didn't
495     * supply its own.
496     */
497     if (r->args && !ap_strchr(ret, '?')) {
498     ret = apr_pstrcat(r->pool, ret, "?", r->args, NULL);
499     }
500 coar 93137 apr_table_setn(r->headers_out, "Location", ret);
501     }
502     }
503 jerenkrantz 95503 return status;
504 fielding 83751 }
505    
506     return DECLINED;
507     }
508    
509 wrowe 87697 static void register_hooks(apr_pool_t *p)
510 fielding 83770 {
511 slive 96137 static const char * const aszSucc[]={ "mod_userdir.c",
512     "mod_vhost_alias.c",NULL };
513 fielding 83770
514 slive 94985 ap_hook_translate_name(translate_alias_redir,NULL,aszSucc,APR_HOOK_MIDDLE);
515 wrowe 87731 ap_hook_fixups(fixup_redir,NULL,NULL,APR_HOOK_MIDDLE);
516 fielding 83770 }
517    
518 wrowe 86609 module AP_MODULE_DECLARE_DATA alias_module =
519 fielding 83751 {
520 fielding 83770 STANDARD20_MODULE_STUFF,
521 jerenkrantz 95503 create_alias_dir_config, /* dir config creater */
522     merge_alias_dir_config, /* dir merger --- default is to override */
523     create_alias_config, /* server config */
524     merge_alias_config, /* merge server configs */
525     alias_cmds, /* command apr_table_t */
526     register_hooks /* register hooks */
527 fielding 83751 };

Properties

Name Value
svn:eol-style native

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2