]> granicus.if.org Git - apache/blob - modules/mappers/mod_rewrite.c
Clean up some of the includes:
[apache] / modules / mappers / mod_rewrite.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /*                       _                            _ _
60 **   _ __ ___   ___   __| |    _ __ _____      ___ __(_) |_ ___
61 **  | '_ ` _ \ / _ \ / _` |   | '__/ _ \ \ /\ / / '__| | __/ _ \
62 **  | | | | | | (_) | (_| |   | | |  __/\ V  V /| |  | | ||  __/
63 **  |_| |_| |_|\___/ \__,_|___|_|  \___| \_/\_/ |_|  |_|\__\___|
64 **                       |_____|
65 **
66 **  URL Rewriting Module
67 **
68 **  This module uses a rule-based rewriting engine (based on a
69 **  regular-expression parser) to rewrite requested URLs on the fly.
70 **
71 **  It supports an unlimited number of additional rule conditions (which can
72 **  operate on a lot of variables, even on HTTP headers) for granular
73 **  matching and even external database lookups (either via plain text
74 **  tables, DBM hash files or even external processes) for advanced URL
75 **  substitution.
76 **
77 **  It operates on the full URLs (including the PATH_INFO part) both in
78 **  per-server context (httpd.conf) and per-dir context (.htaccess) and even
79 **  can generate QUERY_STRING parts on result.   The rewriting result finally
80 **  can lead to internal subprocessing, external request redirection or even
81 **  to internal proxy throughput.
82 **
83 **  This module was originally written in April 1996 and
84 **  gifted exclusively to the The Apache Software Foundation in July 1997 by
85 **
86 **      Ralf S. Engelschall
87 **      rse@engelschall.com
88 **      www.engelschall.com
89 */
90
91 #include "apr.h"
92 #include "apr_strings.h"
93 #include "apr_user.h"
94 #include "apr_lib.h"
95
96 #define APR_WANT_STRFUNC
97 #define APR_WANT_IOVEC
98 #include "apr_want.h"
99
100 #if APR_HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
103 #if APR_HAVE_SYS_TYPES_H
104 #include <sys/types.h>
105 #endif
106
107 #include "ap_config.h"
108 #include "httpd.h"
109 #include "http_config.h"
110 #include "http_request.h"
111 #include "http_core.h"
112 #include "http_log.h"
113 #include "http_protocol.h"
114 #include "mod_rewrite.h"
115
116 #if !defined(OS2) && !defined(WIN32) && !defined(BEOS)
117 #include "unixd.h"
118 #endif
119
120 /*
121 ** +-------------------------------------------------------+
122 ** |                                                       |
123 ** |             static module configuration
124 ** |                                                       |
125 ** +-------------------------------------------------------+
126 */
127
128
129 /*
130 **  Our interface to the Apache server kernel:
131 **
132 **  o  Runtime logic of a request is as following:
133 **       while(request or subrequest)
134 **           foreach(stage #0...#9)
135 **               foreach(module) (**)
136 **                   try to run hook
137 **
138 **  o  the order of modules at (**) is the inverted order as
139 **     given in the "Configuration" file, i.e. the last module
140 **     specified is the first one called for each hook!
141 **     The core module is always the last!
142 **
143 **  o  there are two different types of result checking and
144 **     continue processing:
145 **     for hook #0,#1,#4,#5,#6,#8:
146 **         hook run loop stops on first modules which gives
147 **         back a result != DECLINED, i.e. it usually returns OK
148 **         which says "OK, module has handled this _stage_" and for #1
149 **         this have not to mean "Ok, the filename is now valid".
150 **     for hook #2,#3,#7,#9:
151 **         all hooks are run, independend of result
152 **
153 **  o  at the last stage, the core module always
154 **       - says "HTTP_BAD_REQUEST" if r->filename does not begin with "/"
155 **       - prefix URL with document_root or replaced server_root
156 **         with document_root and sets r->filename
157 **       - always return a "OK" independed if the file really exists
158 **         or not!
159 */
160
161     /* The section for the Configure script:
162     * XXX: this needs updating for apache-2.0 configuration method
163      * MODULE-DEFINITION-START
164      * Name: rewrite_module
165      * ConfigStart
166     . ./helpers/find-dbm-lib
167     if [ "x$found_dbm" = "x1" ]; then
168         echo "      enabling DBM support for mod_rewrite"
169     else
170         echo "      disabling DBM support for mod_rewrite"
171         echo "      (perhaps you need to add -ldbm, -lndbm or -lgdbm to EXTRA_LIBS)"
172         CFLAGS="$CFLAGS -DNO_DBM_REWRITEMAP"
173     fi
174      * ConfigEnd
175      * MODULE-DEFINITION-END
176      */
177
178     /* the module (predeclaration) */
179 module AP_MODULE_DECLARE_DATA rewrite_module;
180
181     /* the cache */
182 static cache *cachep;
183
184     /* whether proxy module is available or not */
185 static int proxy_available;
186
187 static const char *lockname;
188 static apr_lock_t *rewrite_mapr_lock_aquire = NULL;
189 static apr_lock_t *rewrite_log_lock = NULL;
190
191 /*
192 ** +-------------------------------------------------------+
193 ** |                                                       |
194 ** |           configuration directive handling
195 ** |                                                       |
196 ** +-------------------------------------------------------+
197 */
198
199 /*
200 **
201 **  per-server configuration structure handling
202 **
203 */
204
205 static void *config_server_create(apr_pool_t *p, server_rec *s)
206 {
207     rewrite_server_conf *a;
208
209     a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
210
211     a->state           = ENGINE_DISABLED;
212     a->options         = OPTION_NONE;
213     a->rewritelogfile  = NULL;
214     a->rewritelogfp    = NULL;
215     a->rewriteloglevel = 0;
216     a->rewritemaps     = apr_array_make(p, 2, sizeof(rewritemap_entry));
217     a->rewriteconds    = apr_array_make(p, 2, sizeof(rewritecond_entry));
218     a->rewriterules    = apr_array_make(p, 2, sizeof(rewriterule_entry));
219     a->server          = s;
220
221     return (void *)a;
222 }
223
224 static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
225 {
226     rewrite_server_conf *a, *base, *overrides;
227
228     a         = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
229     base      = (rewrite_server_conf *)basev;
230     overrides = (rewrite_server_conf *)overridesv;
231
232     a->state   = overrides->state;
233     a->options = overrides->options;
234     a->server  = overrides->server;
235
236     if (a->options & OPTION_INHERIT) {
237         /*
238          *  local directives override
239          *  and anything else is inherited
240          */
241         a->rewriteloglevel = overrides->rewriteloglevel != 0 
242                              ? overrides->rewriteloglevel
243                              : base->rewriteloglevel;
244         a->rewritelogfile  = overrides->rewritelogfile != NULL 
245                              ? overrides->rewritelogfile
246                              : base->rewritelogfile;
247         a->rewritelogfp    = overrides->rewritelogfp != NULL 
248                              ? overrides->rewritelogfp 
249                              : base->rewritelogfp;
250         a->rewritemaps     = apr_array_append(p, overrides->rewritemaps,
251                                               base->rewritemaps);
252         a->rewriteconds    = apr_array_append(p, overrides->rewriteconds,
253                                               base->rewriteconds);
254         a->rewriterules    = apr_array_append(p, overrides->rewriterules,
255                                               base->rewriterules);
256     }
257     else {
258         /*
259          *  local directives override
260          *  and anything else gets defaults
261          */
262         a->rewriteloglevel = overrides->rewriteloglevel;
263         a->rewritelogfile  = overrides->rewritelogfile;
264         a->rewritelogfp    = overrides->rewritelogfp;
265         a->rewritemaps     = overrides->rewritemaps;
266         a->rewriteconds    = overrides->rewriteconds;
267         a->rewriterules    = overrides->rewriterules;
268     }
269
270     return (void *)a;
271 }
272
273
274 /*
275 **
276 **  per-directory configuration structure handling
277 **
278 */
279
280 static void *config_perdir_create(apr_pool_t *p, char *path)
281 {
282     rewrite_perdir_conf *a;
283
284     a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
285
286     a->state           = ENGINE_DISABLED;
287     a->options         = OPTION_NONE;
288     a->baseurl         = NULL;
289     a->rewriteconds    = apr_array_make(p, 2, sizeof(rewritecond_entry));
290     a->rewriterules    = apr_array_make(p, 2, sizeof(rewriterule_entry));
291
292     if (path == NULL) {
293         a->directory = NULL;
294     }
295     else {
296         /* make sure it has a trailing slash */
297         if (path[strlen(path)-1] == '/') {
298             a->directory = apr_pstrdup(p, path);
299         }
300         else {
301             a->directory = apr_pstrcat(p, path, "/", NULL);
302         }
303     }
304
305     return (void *)a;
306 }
307
308 static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
309 {
310     rewrite_perdir_conf *a, *base, *overrides;
311
312     a         = (rewrite_perdir_conf *)apr_pcalloc(p,
313                                                   sizeof(rewrite_perdir_conf));
314     base      = (rewrite_perdir_conf *)basev;
315     overrides = (rewrite_perdir_conf *)overridesv;
316
317     a->state     = overrides->state;
318     a->options   = overrides->options;
319     a->directory = overrides->directory;
320     a->baseurl   = overrides->baseurl;
321
322     if (a->options & OPTION_INHERIT) {
323         a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
324                                            base->rewriteconds);
325         a->rewriterules = apr_array_append(p, overrides->rewriterules,
326                                            base->rewriterules);
327     }
328     else {
329         a->rewriteconds = overrides->rewriteconds;
330         a->rewriterules = overrides->rewriterules;
331     }
332
333     return (void *)a;
334 }
335
336
337 /*
338 **
339 **  the configuration commands
340 **
341 */
342
343 static const char *cmd_rewriteengine(cmd_parms *cmd,
344                                      void *in_dconf, int flag)
345 {
346     rewrite_perdir_conf *dconf = in_dconf;
347     rewrite_server_conf *sconf;
348
349     sconf = 
350         (rewrite_server_conf *)ap_get_module_config(cmd->server->module_config,
351                                                     &rewrite_module);
352
353     if (cmd->path == NULL) { /* is server command */
354         sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
355     }
356     else                   /* is per-directory command */ {
357         dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
358     }
359
360     return NULL;
361 }
362
363 static const char *cmd_rewriteoptions(cmd_parms *cmd,
364                                       void *in_dconf, const char *option)
365 {
366     rewrite_perdir_conf *dconf = in_dconf;
367     rewrite_server_conf *sconf;
368     const char *err;
369
370     sconf = (rewrite_server_conf *)
371             ap_get_module_config(cmd->server->module_config, &rewrite_module);
372
373     if (cmd->path == NULL) { /* is server command */
374         err = cmd_rewriteoptions_setoption(cmd->pool,
375                                            &(sconf->options), option);
376     }
377     else {                 /* is per-directory command */
378         err = cmd_rewriteoptions_setoption(cmd->pool,
379                                            &(dconf->options), option);
380     }
381
382     return err;
383 }
384
385 static const char *cmd_rewriteoptions_setoption(apr_pool_t *p, int *options,
386                                                 const char *name)
387 {
388     if (strcasecmp(name, "inherit") == 0) {
389         *options |= OPTION_INHERIT;
390     }
391     else {
392         return apr_pstrcat(p, "RewriteOptions: unknown option '",
393                           name, "'", NULL);
394     }
395     return NULL;
396 }
397
398 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
399 {
400     rewrite_server_conf *sconf;
401
402     sconf = (rewrite_server_conf *)
403             ap_get_module_config(cmd->server->module_config, &rewrite_module);
404
405     sconf->rewritelogfile = a1;
406
407     return NULL;
408 }
409
410 static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, const char *a1)
411 {
412     rewrite_server_conf *sconf;
413
414     sconf = (rewrite_server_conf *)
415             ap_get_module_config(cmd->server->module_config, &rewrite_module);
416
417     sconf->rewriteloglevel = atoi(a1);
418
419     return NULL;
420 }
421
422 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
423                                   const char *a2)
424 {
425     rewrite_server_conf *sconf;
426     rewritemap_entry *newmap;
427     apr_finfo_t st;
428
429     sconf = (rewrite_server_conf *)
430             ap_get_module_config(cmd->server->module_config, &rewrite_module);
431
432     newmap = apr_array_push(sconf->rewritemaps);
433
434     newmap->name = a1;
435     newmap->func = NULL;
436     if (strncmp(a2, "txt:", 4) == 0) {
437         newmap->type      = MAPTYPE_TXT;
438         newmap->datafile  = a2+4;
439         newmap->checkfile = a2+4;
440     }
441     else if (strncmp(a2, "rnd:", 4) == 0) {
442         newmap->type      = MAPTYPE_RND;
443         newmap->datafile  = a2+4;
444         newmap->checkfile = a2+4;
445     }
446     else if (strncmp(a2, "dbm:", 4) == 0) {
447 #ifndef NO_DBM_REWRITEMAP
448         newmap->type      = MAPTYPE_DBM;
449         newmap->datafile  = a2+4;
450         newmap->checkfile = apr_pstrcat(cmd->pool, a2+4, NDBM_FILE_SUFFIX, NULL);
451 #else
452         return apr_pstrdup(cmd->pool, "RewriteMap: cannot use NDBM mapfile, "
453                           "because no NDBM support is compiled in");
454 #endif
455     }
456     else if (strncmp(a2, "prg:", 4) == 0) {
457         newmap->type = MAPTYPE_PRG;
458         newmap->datafile = a2+4;
459         newmap->checkfile = a2+4;
460     }
461     else if (strncmp(a2, "int:", 4) == 0) {
462         newmap->type      = MAPTYPE_INT;
463         newmap->datafile  = NULL;
464         newmap->checkfile = NULL;
465         if (strcmp(a2+4, "tolower") == 0) {
466             newmap->func = rewrite_mapfunc_tolower;
467         }
468         else if (strcmp(a2+4, "toupper") == 0) {
469             newmap->func = rewrite_mapfunc_toupper;
470         }
471         else if (strcmp(a2+4, "escape") == 0) {
472             newmap->func = rewrite_mapfunc_escape;
473         }
474         else if (strcmp(a2+4, "unescape") == 0) {
475             newmap->func = rewrite_mapfunc_unescape;
476         }
477         else if (sconf->state == ENGINE_ENABLED) {
478             return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
479                               a2+4, NULL);
480         }
481     }
482     else {
483         newmap->type      = MAPTYPE_TXT;
484         newmap->datafile  = a2;
485         newmap->checkfile = a2;
486     }
487     newmap->fpin  = NULL;
488     newmap->fpout = NULL;
489
490     if (newmap->checkfile && (sconf->state == ENGINE_ENABLED)
491         && (apr_stat(&st, newmap->checkfile, APR_FINFO_MIN, 
492                      cmd->pool) != APR_SUCCESS)) {
493         return apr_pstrcat(cmd->pool,
494                           "RewriteMap: map file or program not found:",
495                           newmap->checkfile, NULL);
496     }
497
498     return NULL;
499 }
500
501 static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1)
502 {
503     const char *error;
504
505     if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
506         return error;
507
508     lockname = a1;
509
510     return NULL;
511 }
512
513 static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
514                                    const char *a1)
515 {
516     rewrite_perdir_conf *dconf = in_dconf;
517
518     if (cmd->path == NULL || dconf == NULL) {
519         return "RewriteBase: only valid in per-directory config files";
520     }
521     if (a1[0] == '\0') {
522         return "RewriteBase: empty URL not allowed";
523     }
524     if (a1[0] != '/') {
525         return "RewriteBase: argument is not a valid URL";
526     }
527
528     dconf->baseurl = a1;
529
530     return NULL;
531 }
532
533 static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
534                                    const char *in_str)
535 {
536     rewrite_perdir_conf *dconf = in_dconf;
537     char *str = apr_pstrdup(cmd->pool, in_str);
538     rewrite_server_conf *sconf;
539     rewritecond_entry *newcond;
540     regex_t *regexp;
541     char *a1;
542     char *a2;
543     char *a3;
544     char *cp;
545     const char *err;
546     int rc;
547
548     sconf = (rewrite_server_conf *)
549             ap_get_module_config(cmd->server->module_config, &rewrite_module);
550
551     /*  make a new entry in the internal temporary rewrite rule list */
552     if (cmd->path == NULL) {   /* is server command */
553         newcond = apr_array_push(sconf->rewriteconds);
554     }
555     else {                     /* is per-directory command */
556         newcond = apr_array_push(dconf->rewriteconds);
557     }
558
559     /*  parse the argument line ourself */
560     if (parseargline(str, &a1, &a2, &a3)) {
561         return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
562                           "'", NULL);
563     }
564
565     /*  arg1: the input string */
566     newcond->input = apr_pstrdup(cmd->pool, a1);
567
568     /* arg3: optional flags field
569        (this have to be first parsed, because we need to
570         know if the regex should be compiled with ICASE!) */
571     newcond->flags = CONDFLAG_NONE;
572     if (a3 != NULL) {
573         if ((err = cmd_rewritecond_parseflagfield(cmd->pool, newcond,
574                                                   a3)) != NULL) {
575             return err;
576         }
577     }
578
579     /*  arg2: the pattern
580         try to compile the regexp to test if is ok */
581     cp = a2;
582     if (cp[0] == '!') {
583         newcond->flags |= CONDFLAG_NOTMATCH;
584         cp++;
585     }
586
587     /* now be careful: Under the POSIX regex library
588        we can compile the pattern for case insensitive matching,
589        under the old V8 library we have to do it self via a hack */
590     if (newcond->flags & CONDFLAG_NOCASE) {
591         rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED|REG_ICASE))
592               == NULL);
593     }
594     else {
595         rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL);
596     }
597     if (rc) {
598         return apr_pstrcat(cmd->pool,
599                           "RewriteCond: cannot compile regular expression '",
600                           a2, "'", NULL);
601     }
602
603     newcond->pattern = apr_pstrdup(cmd->pool, cp);
604     newcond->regexp  = regexp;
605
606     return NULL;
607 }
608
609 static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p,
610                                                   rewritecond_entry *cfg,
611                                                   char *str)
612 {
613     char *cp;
614     char *cp1;
615     char *cp2;
616     char *cp3;
617     char *key;
618     char *val;
619     const char *err;
620
621     if (str[0] != '[' || str[strlen(str)-1] != ']') {
622         return "RewriteCond: bad flag delimiters";
623     }
624
625     cp = str+1;
626     str[strlen(str)-1] = ','; /* for simpler parsing */
627     for ( ; *cp != '\0'; ) {
628         /* skip whitespaces */
629         for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
630             ;
631         if (*cp == '\0') {
632             break;
633         }
634         cp1 = cp;
635         if ((cp2 = strchr(cp, ',')) != NULL) {
636             cp = cp2+1;
637             for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
638                 ;
639             *cp2 = '\0';
640             if ((cp3 = strchr(cp1, '=')) != NULL) {
641                 *cp3 = '\0';
642                 key = cp1;
643                 val = cp3+1;
644             }
645             else {
646                 key = cp1;
647                 val = "";
648             }
649             if ((err = cmd_rewritecond_setflag(p, cfg, key, val)) != NULL) {
650                 return err;
651             }
652         }
653         else {
654             break;
655         }
656     }
657
658     return NULL;
659 }
660
661 static const char *cmd_rewritecond_setflag(apr_pool_t *p, rewritecond_entry *cfg,
662                                            char *key, char *val)
663 {
664     if (   strcasecmp(key, "nocase") == 0
665         || strcasecmp(key, "NC") == 0    ) {
666         cfg->flags |= CONDFLAG_NOCASE;
667     }
668     else if (   strcasecmp(key, "ornext") == 0
669              || strcasecmp(key, "OR") == 0    ) {
670         cfg->flags |= CONDFLAG_ORNEXT;
671     }
672     else {
673         return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL);
674     }
675     return NULL;
676 }
677
678 static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
679                                    const char *in_str)
680 {
681     rewrite_perdir_conf *dconf = in_dconf;
682     char *str = apr_pstrdup(cmd->pool, in_str);
683     rewrite_server_conf *sconf;
684     rewriterule_entry *newrule;
685     regex_t *regexp;
686     char *a1;
687     char *a2;
688     char *a3;
689     char *cp;
690     const char *err;
691     int mode;
692
693     sconf = (rewrite_server_conf *)
694             ap_get_module_config(cmd->server->module_config, &rewrite_module);
695
696     /*  make a new entry in the internal rewrite rule list */
697     if (cmd->path == NULL) {   /* is server command */
698         newrule = apr_array_push(sconf->rewriterules);
699     }
700     else {                     /* is per-directory command */
701         newrule = apr_array_push(dconf->rewriterules);
702     }
703
704     /*  parse the argument line ourself */
705     if (parseargline(str, &a1, &a2, &a3)) {
706         return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
707                           "'", NULL);
708     }
709
710     /* arg3: optional flags field */
711     newrule->forced_mimetype     = NULL;
712     newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
713     newrule->flags  = RULEFLAG_NONE;
714     newrule->env[0] = NULL;
715     newrule->skip   = 0;
716     if (a3 != NULL) {
717         if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule,
718                                                   a3)) != NULL) {
719             return err;
720         }
721     }
722
723     /*  arg1: the pattern
724      *  try to compile the regexp to test if is ok
725      */
726     cp = a1;
727     if (cp[0] == '!') {
728         newrule->flags |= RULEFLAG_NOTMATCH;
729         cp++;
730     }
731     mode = REG_EXTENDED;
732     if (newrule->flags & RULEFLAG_NOCASE) {
733         mode |= REG_ICASE;
734     }
735     if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) {
736         return apr_pstrcat(cmd->pool,
737                           "RewriteRule: cannot compile regular expression '",
738                           a1, "'", NULL);
739     }
740     newrule->pattern = apr_pstrdup(cmd->pool, cp);
741     newrule->regexp  = regexp;
742
743     /*  arg2: the output string
744      *  replace the $<N> by \<n> which is needed by the currently
745      *  used Regular Expression library
746      *
747      * TODO: Is this still required for PCRE?  If not, does it *work* with PCRE?
748      */
749     newrule->output = apr_pstrdup(cmd->pool, a2);
750
751     /* now, if the server or per-dir config holds an
752      * array of RewriteCond entries, we take it for us
753      * and clear the array
754      */
755     if (cmd->path == NULL) {  /* is server command */
756         newrule->rewriteconds   = sconf->rewriteconds;
757         sconf->rewriteconds = apr_array_make(cmd->pool, 2,
758                                             sizeof(rewritecond_entry));
759     }
760     else {                    /* is per-directory command */
761         newrule->rewriteconds   = dconf->rewriteconds;
762         dconf->rewriteconds = apr_array_make(cmd->pool, 2,
763                                             sizeof(rewritecond_entry));
764     }
765
766     return NULL;
767 }
768
769 static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p,
770                                                   rewriterule_entry *cfg,
771                                                   char *str)
772 {
773     char *cp;
774     char *cp1;
775     char *cp2;
776     char *cp3;
777     char *key;
778     char *val;
779     const char *err;
780
781     if (str[0] != '[' || str[strlen(str)-1] != ']') {
782         return "RewriteRule: bad flag delimiters";
783     }
784
785     cp = str+1;
786     str[strlen(str)-1] = ','; /* for simpler parsing */
787     for ( ; *cp != '\0'; ) {
788         /* skip whitespaces */
789         for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
790             ;
791         if (*cp == '\0') {
792             break;
793         }
794         cp1 = cp;
795         if ((cp2 = strchr(cp, ',')) != NULL) {
796             cp = cp2+1;
797             for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
798                 ;
799             *cp2 = '\0';
800             if ((cp3 = strchr(cp1, '=')) != NULL) {
801                 *cp3 = '\0';
802                 key = cp1;
803                 val = cp3+1;
804             }
805             else {
806                 key = cp1;
807                 val = "";
808             }
809             if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) {
810                 return err;
811             }
812         }
813         else {
814             break;
815         }
816     }
817
818     return NULL;
819 }
820
821 static const char *cmd_rewriterule_setflag(apr_pool_t *p, rewriterule_entry *cfg,
822                                            char *key, char *val)
823 {
824     int status = 0;
825     int i;
826
827     if (   strcasecmp(key, "redirect") == 0
828         || strcasecmp(key, "R") == 0       ) {
829         cfg->flags |= RULEFLAG_FORCEREDIRECT;
830         if (strlen(val) > 0) {
831             if (strcasecmp(val, "permanent") == 0) {
832                 status = HTTP_MOVED_PERMANENTLY;
833             }
834             else if (strcasecmp(val, "temp") == 0) {
835                 status = HTTP_MOVED_TEMPORARILY;
836             }
837             else if (strcasecmp(val, "seeother") == 0) {
838                 status = HTTP_SEE_OTHER;
839             }
840             else if (apr_isdigit(*val)) {
841                 status = atoi(val);
842             }
843             if (!ap_is_HTTP_REDIRECT(status)) {
844                 return "RewriteRule: invalid HTTP response code "
845                        "for flag 'R'";
846             }
847             cfg->forced_responsecode = status;
848         }
849     }
850     else if (   strcasecmp(key, "last") == 0
851              || strcasecmp(key, "L") == 0   ) {
852         cfg->flags |= RULEFLAG_LASTRULE;
853     }
854     else if (   strcasecmp(key, "next") == 0
855              || strcasecmp(key, "N") == 0   ) {
856         cfg->flags |= RULEFLAG_NEWROUND;
857     }
858     else if (   strcasecmp(key, "chain") == 0
859              || strcasecmp(key, "C") == 0    ) {
860         cfg->flags |= RULEFLAG_CHAIN;
861     }
862     else if (   strcasecmp(key, "type") == 0
863              || strcasecmp(key, "T") == 0   ) {
864         cfg->forced_mimetype = apr_pstrdup(p, val);
865         ap_str_tolower(cfg->forced_mimetype);
866     }
867     else if (   strcasecmp(key, "env") == 0
868              || strcasecmp(key, "E") == 0   ) {
869         for (i = 0; (cfg->env[i] != NULL) && (i < MAX_ENV_FLAGS); i++)
870             ;
871         if (i < MAX_ENV_FLAGS) {
872             cfg->env[i] = apr_pstrdup(p, val);
873             cfg->env[i+1] = NULL;
874         }
875         else {
876             return "RewriteRule: too many environment flags 'E'";
877         }
878     }
879     else if (   strcasecmp(key, "nosubreq") == 0
880              || strcasecmp(key, "NS") == 0      ) {
881         cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
882     }
883     else if (   strcasecmp(key, "proxy") == 0
884              || strcasecmp(key, "P") == 0      ) {
885         cfg->flags |= RULEFLAG_PROXY;
886     }
887     else if (   strcasecmp(key, "passthrough") == 0
888              || strcasecmp(key, "PT") == 0      ) {
889         cfg->flags |= RULEFLAG_PASSTHROUGH;
890     }
891     else if (   strcasecmp(key, "skip") == 0
892              || strcasecmp(key, "S") == 0   ) {
893         cfg->skip = atoi(val);
894     }
895     else if (   strcasecmp(key, "forbidden") == 0
896              || strcasecmp(key, "F") == 0   ) {
897         cfg->flags |= RULEFLAG_FORBIDDEN;
898     }
899     else if (   strcasecmp(key, "gone") == 0
900              || strcasecmp(key, "G") == 0   ) {
901         cfg->flags |= RULEFLAG_GONE;
902     }
903     else if (   strcasecmp(key, "qsappend") == 0
904              || strcasecmp(key, "QSA") == 0   ) {
905         cfg->flags |= RULEFLAG_QSAPPEND;
906     }
907     else if (   strcasecmp(key, "nocase") == 0
908              || strcasecmp(key, "NC") == 0    ) {
909         cfg->flags |= RULEFLAG_NOCASE;
910     }
911     else {
912         return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
913     }
914     return NULL;
915 }
916
917
918 /*
919 **
920 **  Global Module Initialization
921 **  [called from read_config() after all
922 **  config commands were already called]
923 **
924 */
925
926 static void init_module(apr_pool_t *p,
927                         apr_pool_t *plog,
928                         apr_pool_t *ptemp,
929                         server_rec *s)
930 {
931     apr_status_t rv;
932     void *data;
933     int first_time = 0;
934     const char *userdata_key = "rewrite_init_module";
935
936     apr_pool_userdata_get(&data, userdata_key, s->process->pool);
937     if (!data) {
938         first_time = 1;
939         apr_pool_userdata_set((const void *)1, userdata_key,
940                          apr_pool_cleanup_null, s->process->pool);
941     }
942
943     /* check if proxy module is available */
944     proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
945
946     /* create the rewriting lockfiles in the parent */
947     if ((rv = apr_lock_create (&rewrite_log_lock, APR_MUTEX, APR_LOCKALL,
948                                NULL, p)) != APR_SUCCESS) {
949         ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
950                      "mod_rewrite: could not create rewrite_log_lock");
951         exit(1);
952     }
953
954     rewritelock_create(s, p);
955     apr_pool_cleanup_register(p, (void *)s, rewritelock_remove, apr_pool_cleanup_null);
956
957     /* step through the servers and
958      * - open each rewriting logfile
959      * - open the RewriteMap prg:xxx programs
960      */
961     for (; s; s = s->next) {
962         open_rewritelog(s, p);
963         if (!first_time)
964            run_rewritemap_programs(s, p);
965     }
966 }
967
968
969 /*
970 **
971 **  Per-Child Module Initialization
972 **  [called after a child process is spawned]
973 **
974 */
975
976 static void init_child(apr_pool_t *p, server_rec *s)
977 {
978     apr_status_t rv;
979
980     if (lockname != NULL && *(lockname) != '\0')
981     {
982         rv = apr_lock_child_init (&rewrite_mapr_lock_aquire, lockname, p);
983         if (rv != APR_SUCCESS) {
984             ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
985                          "mod_rewrite: could not init rewrite_mapr_lock_aquire "
986                          "in child");
987         }
988     }
989
990     /* create the lookup cache */
991     cachep = init_cache(p);
992 }
993
994
995 /*
996 ** +-------------------------------------------------------+
997 ** |                                                       |
998 ** |                     runtime hooks
999 ** |                                                       |
1000 ** +-------------------------------------------------------+
1001 */
1002
1003 /*
1004 **
1005 **  URI-to-filename hook
1006 **
1007 **  [used for the rewriting engine triggered by
1008 **  the per-server 'RewriteRule' directives]
1009 **
1010 */
1011
1012 static int hook_uri2file(request_rec *r)
1013 {
1014     void *sconf;
1015     rewrite_server_conf *conf;
1016     const char *var;
1017     const char *thisserver;
1018     char *thisport;
1019     const char *thisurl;
1020     char buf[512];
1021     char docroot[512];
1022     char *cp, *cp2;
1023     const char *ccp;
1024     apr_finfo_t finfo;
1025     unsigned int port;
1026     int n;
1027     int l;
1028
1029     /*
1030      *  retrieve the config structures
1031      */
1032     sconf = r->server->module_config;
1033     conf  = (rewrite_server_conf *)ap_get_module_config(sconf,
1034                                                         &rewrite_module);
1035
1036     /*
1037      *  only do something under runtime if the engine is really enabled,
1038      *  else return immediately!
1039      */
1040     if (conf->state == ENGINE_DISABLED) {
1041         return DECLINED;
1042     }
1043
1044     /*
1045      *  check for the ugly API case of a virtual host section where no
1046      *  mod_rewrite directives exists. In this situation we became no chance
1047      *  by the API to setup our default per-server config so we have to
1048      *  on-the-fly assume we have the default config. But because the default
1049      *  config has a disabled rewriting engine we are lucky because can
1050      *  just stop operating now.
1051      */
1052     if (conf->server != r->server) {
1053         return DECLINED;
1054     }
1055
1056     /*
1057      *  add the SCRIPT_URL variable to the env. this is a bit complicated
1058      *  due to the fact that apache uses subrequests and internal redirects
1059      */
1060
1061     if (r->main == NULL) {
1062          var = apr_pstrcat(r->pool, "REDIRECT_", ENVVAR_SCRIPT_URL, NULL);
1063          var = apr_table_get(r->subprocess_env, var);
1064          if (var == NULL) {
1065              apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
1066          }
1067          else {
1068              apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1069          }
1070     }
1071     else {
1072          var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
1073          apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1074     }
1075
1076     /*
1077      *  create the SCRIPT_URI variable for the env
1078      */
1079
1080     /* add the canonical URI of this URL */
1081     thisserver = ap_get_server_name(r);
1082     port = ap_get_server_port(r);
1083     if (ap_is_default_port(port, r)) {
1084         thisport = "";
1085     }
1086     else {
1087         apr_snprintf(buf, sizeof(buf), ":%u", port);
1088         thisport = buf;
1089     }
1090     thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
1091
1092     /* set the variable */
1093     var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
1094                      thisurl, NULL);
1095     apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
1096
1097     /* if filename was not initially set,
1098      * we start with the requested URI
1099      */
1100     if (r->filename == NULL) {
1101         r->filename = apr_pstrdup(r->pool, r->uri);
1102         rewritelog(r, 2, "init rewrite engine with requested uri %s",
1103                    r->filename);
1104     }
1105
1106     /*
1107      *  now apply the rules ...
1108      */
1109     if (apply_rewrite_list(r, conf->rewriterules, NULL)) {
1110
1111         if (strlen(r->filename) > 6 &&
1112             strncmp(r->filename, "proxy:", 6) == 0) {
1113             /* it should be go on as an internal proxy request */
1114
1115             /* check if the proxy module is enabled, so
1116              * we can actually use it!
1117              */
1118             if (!proxy_available) {
1119                 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1120                              "attempt to make remote request from mod_rewrite "
1121                              "without proxy enabled: %s", r->filename);
1122                 return HTTP_FORBIDDEN;
1123             }
1124
1125             /* make sure the QUERY_STRING and
1126              * PATH_INFO parts get incorporated
1127              */
1128             if (r->path_info != NULL) {
1129                 r->filename = apr_pstrcat(r->pool, r->filename,
1130                                          r->path_info, NULL);
1131             }
1132             if (r->args != NULL &&
1133                 r->uri == r->unparsed_uri) {
1134                 /* see proxy_http:proxy_http_canon() */
1135                 r->filename = apr_pstrcat(r->pool, r->filename,
1136                                          "?", r->args, NULL);
1137             }
1138
1139             /* now make sure the request gets handled by the proxy handler */
1140             r->proxyreq = 1;
1141             r->handler  = "proxy-server";
1142
1143             rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
1144                        r->filename);
1145             return OK;
1146         }
1147         else if (is_absolute_uri(r->filename)) {
1148             /* it was finally rewritten to a remote URL */
1149
1150             /* skip 'scheme:' */
1151             for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1152                 ;
1153             /* skip '://' */
1154             cp += 3;
1155             /* skip host part */
1156             for ( ; *cp != '/' && *cp != '\0'; cp++)
1157                 ;
1158             if (*cp != '\0') {
1159                 rewritelog(r, 1, "escaping %s for redirect", r->filename);
1160                 cp2 = ap_escape_uri(r->pool, cp);
1161                 *cp = '\0';
1162                 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
1163             }
1164
1165             /* append the QUERY_STRING part */
1166             if (r->args != NULL) {
1167                 r->filename = apr_pstrcat(r->pool, r->filename, "?", 
1168                                          ap_escape_uri(r->pool, r->args), NULL);
1169             }
1170
1171             /* determine HTTP redirect response code */
1172             if (ap_is_HTTP_REDIRECT(r->status)) {
1173                 n = r->status;
1174                 r->status = HTTP_OK; /* make Apache kernel happy */
1175             }
1176             else {
1177                 n = HTTP_MOVED_TEMPORARILY;
1178             }
1179
1180             /* now do the redirection */
1181             apr_table_setn(r->headers_out, "Location", r->filename);
1182             rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
1183             return n;
1184         }
1185         else if (strlen(r->filename) > 10 &&
1186                  strncmp(r->filename, "forbidden:", 10) == 0) {
1187             /* This URLs is forced to be forbidden for the requester */
1188             return HTTP_FORBIDDEN;
1189         }
1190         else if (strlen(r->filename) > 5 &&
1191                  strncmp(r->filename, "gone:", 5) == 0) {
1192             /* This URLs is forced to be gone */
1193             return HTTP_GONE;
1194         }
1195         else if (strlen(r->filename) > 12 &&
1196                  strncmp(r->filename, "passthrough:", 12) == 0) {
1197             /*
1198              * Hack because of underpowered API: passing the current
1199              * rewritten filename through to other URL-to-filename handlers
1200              * just as it were the requested URL. This is to enable
1201              * post-processing by mod_alias, etc.  which always act on
1202              * r->uri! The difference here is: We do not try to
1203              * add the document root
1204              */
1205             r->uri = apr_pstrdup(r->pool, r->filename+12);
1206             return DECLINED;
1207         }
1208         else {
1209             /* it was finally rewritten to a local path */
1210
1211             /* expand "/~user" prefix */
1212 #if APR_HAS_USER
1213             r->filename = expand_tildepaths(r, r->filename);
1214 #endif
1215             rewritelog(r, 2, "local path result: %s", r->filename);
1216
1217             /* the filename has to start with a slash! */
1218             if (r->filename[0] != '/') {
1219                 return HTTP_BAD_REQUEST;
1220             }
1221
1222             /* if there is no valid prefix, we have
1223              * to emulate the translator from the core and
1224              * prefix the filename with document_root
1225              *
1226              * NOTICE:
1227              * We cannot leave out the prefix_stat because
1228              * - when we always prefix with document_root
1229              *   then no absolute path can be created, e.g. via
1230              *   emulating a ScriptAlias directive, etc.
1231              * - when we always NOT prefix with document_root
1232              *   then the files under document_root have to
1233              *   be references directly and document_root
1234              *   gets never used and will be a dummy parameter -
1235              *   this is also bad
1236              *
1237              * BUT:
1238              * Under real Unix systems this is no problem,
1239              * because we only do stat() on the first directory
1240              * and this gets cached by the kernel for along time!
1241              */
1242             n = prefix_stat(r->filename, &finfo);
1243             if (n == 0) {
1244                 if ((ccp = ap_document_root(r)) != NULL) {
1245                     l = apr_cpystrn(docroot, ccp, sizeof(docroot)) - docroot;
1246
1247                     /* always NOT have a trailing slash */
1248                     if (docroot[l-1] == '/') {
1249                         docroot[l-1] = '\0';
1250                     }
1251                     if (r->server->path
1252                         && !strncmp(r->filename, r->server->path,
1253                                     r->server->pathlen)) {
1254                         r->filename = apr_pstrcat(r->pool, docroot,
1255                                                  (r->filename +
1256                                                   r->server->pathlen), NULL);
1257                     }
1258                     else {
1259                         r->filename = apr_pstrcat(r->pool, docroot, 
1260                                                  r->filename, NULL);
1261                     }
1262                     rewritelog(r, 2, "prefixed with document_root to %s",
1263                                r->filename);
1264                 }
1265             }
1266
1267             rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
1268             return OK;
1269         }
1270     }
1271     else {
1272         rewritelog(r, 1, "pass through %s", r->filename);
1273         return DECLINED;
1274     }
1275 }
1276
1277
1278 /*
1279 **
1280 **  MIME-type hook
1281 **
1282 **  [used to support the forced-MIME-type feature]
1283 **
1284 */
1285
1286 static int hook_mimetype(request_rec *r)
1287 {
1288     const char *t;
1289
1290     /* now check if we have to force a MIME-type */
1291     t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
1292     if (t == NULL) {
1293         return DECLINED;
1294     }
1295     else {
1296         rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
1297                    r->filename, t);
1298         r->content_type = t;
1299         return OK;
1300     }
1301 }
1302
1303
1304 /*
1305 **
1306 **  Fixup hook
1307 **
1308 **  [used for the rewriting engine triggered by
1309 **  the per-directory 'RewriteRule' directives]
1310 **
1311 */
1312
1313 static int hook_fixup(request_rec *r)
1314 {
1315     rewrite_perdir_conf *dconf;
1316     char *cp;
1317     char *cp2;
1318     const char *ccp;
1319     char *prefix;
1320     int l;
1321     int n;
1322     char *ofilename;
1323
1324     dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
1325                                                         &rewrite_module);
1326
1327     /* if there is no per-dir config we return immediately */
1328     if (dconf == NULL) {
1329         return DECLINED;
1330     }
1331
1332     /* we shouldn't do anything in subrequests */
1333     if (r->main != NULL) {
1334         return DECLINED;
1335     }
1336
1337     /* if there are no real (i.e. no RewriteRule directives!)
1338        per-dir config of us, we return also immediately */
1339     if (dconf->directory == NULL) {
1340         return DECLINED;
1341     }
1342
1343     /*
1344      *  only do something under runtime if the engine is really enabled,
1345      *  for this directory, else return immediately!
1346      */
1347     if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) {
1348         /* FollowSymLinks is mandatory! */
1349         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1350                      "Options FollowSymLinks or SymLinksIfOwnerMatch is off "
1351                      "which implies that RewriteRule directive is forbidden: "
1352                      "%s", r->filename);
1353         return HTTP_FORBIDDEN;
1354     }
1355     else {
1356         /* FollowSymLinks is given, but the user can
1357          * still turn off the rewriting engine
1358          */
1359         if (dconf->state == ENGINE_DISABLED) {
1360             return DECLINED;
1361         }
1362     }
1363
1364     /*
1365      *  remember the current filename before rewriting for later check
1366      *  to prevent deadlooping because of internal redirects
1367      *  on final URL/filename which can be equal to the inital one.
1368      */
1369     ofilename = r->filename;
1370
1371     /*
1372      *  now apply the rules ...
1373      */
1374     if (apply_rewrite_list(r, dconf->rewriterules, dconf->directory)) {
1375
1376         if (strlen(r->filename) > 6 &&
1377             strncmp(r->filename, "proxy:", 6) == 0) {
1378             /* it should go on as an internal proxy request */
1379
1380             /* make sure the QUERY_STRING and
1381              * PATH_INFO parts get incorporated
1382              * (r->path_info was already appended by the
1383              * rewriting engine because of the per-dir context!)
1384              */
1385             if (r->args != NULL) {
1386                 r->filename = apr_pstrcat(r->pool, r->filename,
1387                                          "?", r->args, NULL);
1388             }
1389
1390             /* now make sure the request gets handled by the proxy handler */
1391             r->proxyreq = 1;
1392             r->handler  = "proxy-server";
1393
1394             rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
1395                        "%s [OK]", dconf->directory, r->filename);
1396             return OK;
1397         }
1398         else if (is_absolute_uri(r->filename)) {
1399             /* it was finally rewritten to a remote URL */
1400
1401             /* because we are in a per-dir context
1402              * first try to replace the directory with its base-URL
1403              * if there is a base-URL available
1404              */
1405             if (dconf->baseurl != NULL) {
1406                 /* skip 'scheme:' */
1407                 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1408                     ;
1409                 /* skip '://' */
1410                 cp += 3;
1411                 if ((cp = strchr(cp, '/')) != NULL) {
1412                     rewritelog(r, 2,
1413                                "[per-dir %s] trying to replace "
1414                                "prefix %s with %s",
1415                                dconf->directory, dconf->directory,
1416                                dconf->baseurl);
1417                     cp2 = subst_prefix_path(r, cp, dconf->directory,
1418                                             dconf->baseurl);
1419                     if (strcmp(cp2, cp) != 0) {
1420                         *cp = '\0';
1421                         r->filename = apr_pstrcat(r->pool, r->filename,
1422                                                  cp2, NULL);
1423                     }
1424                 }
1425             }
1426
1427             /* now prepare the redirect... */
1428
1429             /* skip 'scheme:' */
1430             for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1431                 ;
1432             /* skip '://' */
1433             cp += 3;
1434             /* skip host part */
1435             for ( ; *cp != '/' && *cp != '\0'; cp++)
1436                 ;
1437             if (*cp != '\0') {
1438                 rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
1439                            dconf->directory, r->filename);
1440                 cp2 = ap_escape_uri(r->pool, cp);
1441                 *cp = '\0';
1442                 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
1443             }
1444
1445             /* append the QUERY_STRING part */
1446             if (r->args != NULL) {
1447                 r->filename = apr_pstrcat(r->pool, r->filename, "?", 
1448                                          ap_escape_uri(r->pool, r->args), NULL);
1449             }
1450
1451             /* determine HTTP redirect response code */
1452             if (ap_is_HTTP_REDIRECT(r->status)) {
1453                 n = r->status;
1454                 r->status = HTTP_OK; /* make Apache kernel happy */
1455             }
1456             else {
1457                 n = HTTP_MOVED_TEMPORARILY;
1458             }
1459
1460             /* now do the redirection */
1461             apr_table_setn(r->headers_out, "Location", r->filename);
1462             rewritelog(r, 1, "[per-dir %s] redirect to %s [REDIRECT/%d]",
1463                        dconf->directory, r->filename, n);
1464             return n;
1465         }
1466         else if (strlen(r->filename) > 10 &&
1467                  strncmp(r->filename, "forbidden:", 10) == 0) {
1468             /* This URL is forced to be forbidden for the requester */
1469             return HTTP_FORBIDDEN;
1470         }
1471         else if (strlen(r->filename) > 5 &&
1472                  strncmp(r->filename, "gone:", 5) == 0) {
1473             /* This URL is forced to be gone */
1474             return HTTP_GONE;
1475         }
1476         else {
1477             /* it was finally rewritten to a local path */
1478
1479             /* if someone used the PASSTHROUGH flag in per-dir
1480              * context we just ignore it. It is only useful
1481              * in per-server context
1482              */
1483             if (strlen(r->filename) > 12 &&
1484                 strncmp(r->filename, "passthrough:", 12) == 0) {
1485                 r->filename = apr_pstrdup(r->pool, r->filename+12);
1486             }
1487
1488             /* the filename has to start with a slash! */
1489             if (r->filename[0] != '/') {
1490                 return HTTP_BAD_REQUEST;
1491             }
1492
1493             /* Check for deadlooping:
1494              * At this point we KNOW that at least one rewriting
1495              * rule was applied, but when the resulting URL is
1496              * the same as the initial URL, we are not allowed to
1497              * use the following internal redirection stuff because
1498              * this would lead to a deadloop.
1499              */
1500             if (strcmp(r->filename, ofilename) == 0) {
1501                 rewritelog(r, 1, "[per-dir %s] initial URL equal rewritten "
1502                            "URL: %s [IGNORING REWRITE]",
1503                            dconf->directory, r->filename);
1504                 return OK;
1505             }
1506
1507             /* if there is a valid base-URL then substitute
1508              * the per-dir prefix with this base-URL if the
1509              * current filename still is inside this per-dir
1510              * context. If not then treat the result as a
1511              * plain URL
1512              */
1513             if (dconf->baseurl != NULL) {
1514                 rewritelog(r, 2,
1515                            "[per-dir %s] trying to replace prefix %s with %s",
1516                            dconf->directory, dconf->directory, dconf->baseurl);
1517                 r->filename = subst_prefix_path(r, r->filename,
1518                                                 dconf->directory,
1519                                                 dconf->baseurl);
1520             }
1521             else {
1522                 /* if no explicit base-URL exists we assume
1523                  * that the directory prefix is also a valid URL
1524                  * for this webserver and only try to remove the
1525                  * document_root if it is prefix
1526                  */
1527                 if ((ccp = ap_document_root(r)) != NULL) {
1528                     prefix = apr_pstrdup(r->pool, ccp);
1529                     /* always NOT have a trailing slash */
1530                     l = strlen(prefix);
1531                     if (prefix[l-1] == '/') {
1532                         prefix[l-1] = '\0';
1533                         l--;
1534                     }
1535                     if (strncmp(r->filename, prefix, l) == 0) {
1536                         rewritelog(r, 2,
1537                                    "[per-dir %s] strip document_root "
1538                                    "prefix: %s -> %s",
1539                                    dconf->directory, r->filename,
1540                                    r->filename+l);
1541                         r->filename = apr_pstrdup(r->pool, r->filename+l);
1542                     }
1543                 }
1544             }
1545
1546             /* now initiate the internal redirect */
1547             rewritelog(r, 1, "[per-dir %s] internal redirect with %s "
1548                        "[INTERNAL REDIRECT]", dconf->directory, r->filename);
1549             r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
1550             r->handler = "redirect-handler";
1551             return OK;
1552         }
1553     }
1554     else {
1555         rewritelog(r, 1, "[per-dir %s] pass through %s", 
1556                    dconf->directory, r->filename);
1557         return DECLINED;
1558     }
1559 }
1560
1561
1562 /*
1563 **
1564 **  Content-Handlers
1565 **
1566 **  [used for redirect support]
1567 **
1568 */
1569
1570 static int handler_redirect(request_rec *r)
1571 {
1572     if (strcmp(r->handler, "redirect-handler")) {
1573         return DECLINED;
1574     }
1575
1576     /* just make sure that we are really meant! */
1577     if (strncmp(r->filename, "redirect:", 9) != 0) {
1578         return DECLINED;
1579     }
1580
1581     /* now do the internal redirect */
1582     ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
1583                                     r->args ? "?" : NULL, r->args, NULL), r);
1584
1585     /* and return gracefully */
1586     return OK;
1587 }
1588
1589
1590 /*
1591 ** +-------------------------------------------------------+
1592 ** |                                                       |
1593 ** |                  the rewriting engine
1594 ** |                                                       |
1595 ** +-------------------------------------------------------+
1596 */
1597
1598 /*
1599  *  Apply a complete rule set,
1600  *  i.e. a list of rewrite rules
1601  */
1602 static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
1603                               char *perdir)
1604 {
1605     rewriterule_entry *entries;
1606     rewriterule_entry *p;
1607     int i;
1608     int changed;
1609     int rc;
1610     int s;
1611
1612     /*
1613      *  Iterate over all existing rules
1614      */
1615     entries = (rewriterule_entry *)rewriterules->elts;
1616     changed = 0;
1617     loop:
1618     for (i = 0; i < rewriterules->nelts; i++) {
1619         p = &entries[i];
1620
1621         /*
1622          *  Ignore this rule on subrequests if we are explicitly
1623          *  asked to do so or this is a proxy-throughput or a
1624          *  forced redirect rule.
1625          */
1626         if (r->main != NULL &&
1627             (p->flags & RULEFLAG_IGNOREONSUBREQ ||
1628              p->flags & RULEFLAG_PROXY          ||
1629              p->flags & RULEFLAG_FORCEREDIRECT    )) {
1630             continue;
1631         }
1632
1633         /*
1634          *  Apply the current rule.
1635          */
1636         rc = apply_rewrite_rule(r, p, perdir);
1637         if (rc) {
1638             /*
1639              *  Indicate a change if this was not a match-only rule.
1640              */
1641             if (rc != 2) {
1642                 changed = 1;
1643             }
1644
1645             /*
1646              *  Pass-Through Feature (`RewriteRule .. .. [PT]'):
1647              *  Because the Apache 1.x API is very limited we
1648              *  need this hack to pass the rewritten URL to other
1649              *  modules like mod_alias, mod_userdir, etc.
1650              */
1651             if (p->flags & RULEFLAG_PASSTHROUGH) {
1652                 rewritelog(r, 2, "forcing '%s' to get passed through "
1653                            "to next API URI-to-filename handler", r->filename);
1654                 r->filename = apr_pstrcat(r->pool, "passthrough:",
1655                                          r->filename, NULL);
1656                 changed = 1;
1657                 break;
1658             }
1659
1660             /*
1661              *  Rule has the "forbidden" flag set which means that
1662              *  we stop processing and indicate this to the caller.
1663              */
1664             if (p->flags & RULEFLAG_FORBIDDEN) {
1665                 rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
1666                 r->filename = apr_pstrcat(r->pool, "forbidden:",
1667                                          r->filename, NULL);
1668                 changed = 1;
1669                 break;
1670             }
1671
1672             /*
1673              *  Rule has the "gone" flag set which means that
1674              *  we stop processing and indicate this to the caller.
1675              */
1676             if (p->flags & RULEFLAG_GONE) {
1677                 rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
1678                 r->filename = apr_pstrcat(r->pool, "gone:", r->filename, NULL);
1679                 changed = 1;
1680                 break;
1681             }
1682
1683             /*
1684              *  Stop processing also on proxy pass-through and
1685              *  last-rule and new-round flags.
1686              */
1687             if (p->flags & RULEFLAG_PROXY) {
1688                 break;
1689             }
1690             if (p->flags & RULEFLAG_LASTRULE) {
1691                 break;
1692             }
1693
1694             /*
1695              *  On "new-round" flag we just start from the top of
1696              *  the rewriting ruleset again.
1697              */
1698             if (p->flags & RULEFLAG_NEWROUND) {
1699                 goto loop;
1700             }
1701
1702             /*
1703              *  If we are forced to skip N next rules, do it now.
1704              */
1705             if (p->skip > 0) {
1706                 s = p->skip;
1707                 while (   i < rewriterules->nelts
1708                        && s > 0) {
1709                     i++;
1710                     p = &entries[i];
1711                     s--;
1712                 }
1713             }
1714         }
1715         else {
1716             /*
1717              *  If current rule is chained with next rule(s),
1718              *  skip all this next rule(s)
1719              */
1720             while (   i < rewriterules->nelts
1721                    && p->flags & RULEFLAG_CHAIN) {
1722                 i++;
1723                 p = &entries[i];
1724             }
1725         }
1726     }
1727     return changed;
1728 }
1729
1730 /*
1731  *  Apply a single(!) rewrite rule
1732  */
1733 static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
1734                               char *perdir)
1735 {
1736     char *uri;
1737     char *output;
1738     const char *vary;
1739     char newuri[MAX_STRING_LEN];
1740     regex_t *regexp;
1741     regmatch_t regmatch[MAX_NMATCH];
1742     backrefinfo *briRR = NULL;
1743     backrefinfo *briRC = NULL;
1744     int prefixstrip;
1745     int failed;
1746     apr_array_header_t *rewriteconds;
1747     rewritecond_entry *conds;
1748     rewritecond_entry *c;
1749     int i;
1750     int rc;
1751
1752     /*
1753      *  Initialisation
1754      */
1755     uri     = r->filename;
1756     regexp  = p->regexp;
1757     output  = p->output;
1758
1759     /*
1760      *  Add (perhaps splitted away) PATH_INFO postfix to URL to
1761      *  make sure we really match against the complete URL.
1762      */
1763     if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
1764         rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
1765                    perdir, uri, uri, r->path_info);
1766         uri = apr_pstrcat(r->pool, uri, r->path_info, NULL);
1767     }
1768
1769     /*
1770      *  On per-directory context (.htaccess) strip the location
1771      *  prefix from the URL to make sure patterns apply only to
1772      *  the local part.  Additionally indicate this special
1773      *  threatment in the logfile.
1774      */
1775     prefixstrip = 0;
1776     if (perdir != NULL) {
1777         if (   strlen(uri) >= strlen(perdir)
1778             && strncmp(uri, perdir, strlen(perdir)) == 0) {
1779             rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
1780                        perdir, uri, uri+strlen(perdir));
1781             uri = uri+strlen(perdir);
1782             prefixstrip = 1;
1783         }
1784     }
1785
1786     /*
1787      *  Try to match the URI against the RewriteRule pattern
1788      *  and exit immeddiately if it didn't apply.
1789      */
1790     if (perdir == NULL) {
1791         rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
1792                    p->pattern, uri);
1793     }
1794     else {
1795         rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
1796                    perdir, p->pattern, uri);
1797     }
1798     rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
1799     if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
1800            (!rc &&  (p->flags & RULEFLAG_NOTMATCH))   ) ) {
1801         return 0;
1802     }
1803
1804     /*
1805      *  Else create the RewriteRule `regsubinfo' structure which
1806      *  holds the substitution information.
1807      */
1808     briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
1809     if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
1810         /*  empty info on negative patterns  */
1811         briRR->source = "";
1812         briRR->nsub   = 0;
1813     }
1814     else {
1815         briRR->source = apr_pstrdup(r->pool, uri);
1816         briRR->nsub   = regexp->re_nsub;
1817         memcpy((void *)(briRR->regmatch), (void *)(regmatch),
1818                sizeof(regmatch));
1819     }
1820
1821     /*
1822      *  Initiallally create the RewriteCond backrefinfo with
1823      *  empty backrefinfo, i.e. not subst parts
1824      *  (this one is adjusted inside apply_rewrite_cond() later!!)
1825      */
1826     briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
1827     briRC->source = "";
1828     briRC->nsub   = 0;
1829
1830     /*
1831      *  Ok, we already know the pattern has matched, but we now
1832      *  additionally have to check for all existing preconditions
1833      *  (RewriteCond) which have to be also true. We do this at
1834      *  this very late stage to avoid unnessesary checks which
1835      *  would slow down the rewriting engine!!
1836      */
1837     rewriteconds = p->rewriteconds;
1838     conds = (rewritecond_entry *)rewriteconds->elts;
1839     failed = 0;
1840     for (i = 0; i < rewriteconds->nelts; i++) {
1841         c = &conds[i];
1842         rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
1843         if (c->flags & CONDFLAG_ORNEXT) {
1844             /*
1845              *  The "OR" case
1846              */
1847             if (rc == 0) {
1848                 /*  One condition is false, but another can be
1849                  *  still true, so we have to continue...
1850                  */
1851                 apr_table_unset(r->notes, VARY_KEY_THIS);
1852                 continue;
1853             }
1854             else {
1855                 /*  One true condition is enough in "or" case, so
1856                  *  skip the other conditions which are "ornext"
1857                  *  chained
1858                  */
1859                 while (   i < rewriteconds->nelts
1860                        && c->flags & CONDFLAG_ORNEXT) {
1861                     i++;
1862                     c = &conds[i];
1863                 }
1864                 continue;
1865             }
1866         }
1867         else {
1868             /*
1869              *  The "AND" case, i.e. no "or" flag,
1870              *  so a single failure means total failure.
1871              */
1872             if (rc == 0) {
1873                 failed = 1;
1874                 break;
1875             }
1876         }
1877         vary = apr_table_get(r->notes, VARY_KEY_THIS);
1878         if (vary != NULL) {
1879             apr_table_merge(r->notes, VARY_KEY, vary);
1880             apr_table_unset(r->notes, VARY_KEY_THIS);
1881         }
1882     }
1883     /*  if any condition fails the complete rule fails  */
1884     if (failed) {
1885         apr_table_unset(r->notes, VARY_KEY);
1886         apr_table_unset(r->notes, VARY_KEY_THIS);
1887         return 0;
1888     }
1889
1890     /*
1891      * Regardless of what we do next, we've found a match.  Check to see
1892      * if any of the request header fields were involved, and add them
1893      * to the Vary field of the response.
1894      */
1895     if ((vary = apr_table_get(r->notes, VARY_KEY)) != NULL) {
1896         apr_table_merge(r->headers_out, "Vary", vary);
1897         apr_table_unset(r->notes, VARY_KEY);
1898     }
1899
1900     /*
1901      *  If this is a pure matching rule (`RewriteRule <pat> -')
1902      *  we stop processing and return immediately. The only thing
1903      *  we have not to forget are the environment variables
1904      *  (`RewriteRule <pat> - [E=...]')
1905      */
1906     if (strcmp(output, "-") == 0) {
1907         do_expand_env(r, p->env, briRR, briRC);
1908         if (p->forced_mimetype != NULL) {
1909             if (perdir == NULL) {
1910                 /* In the per-server context we can force the MIME-type
1911                  * the correct way by notifying our MIME-type hook handler
1912                  * to do the job when the MIME-type API stage is reached.
1913                  */
1914                 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
1915                            r->filename, p->forced_mimetype);
1916                 apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
1917                               p->forced_mimetype);
1918             }
1919             else {
1920                 /* In per-directory context we operate in the Fixup API hook
1921                  * which is after the MIME-type hook, so our MIME-type handler
1922                  * has no chance to set r->content_type. And because we are
1923                  * in the situation where no substitution takes place no
1924                  * sub-request will happen (which could solve the
1925                  * restriction). As a workaround we do it ourself now
1926                  * immediately although this is not strictly API-conforming.
1927                  * But it's the only chance we have...
1928                  */
1929                 rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
1930                            "'%s'", perdir, r->filename, p->forced_mimetype);
1931                 r->content_type = p->forced_mimetype;
1932             }
1933         }
1934         return 2;
1935     }
1936
1937     /*
1938      *  Ok, now we finally know all patterns have matched and
1939      *  that there is something to replace, so we create the
1940      *  substitution URL string in `newuri'.
1941      */
1942     do_expand(r, output, newuri, sizeof(newuri), briRR, briRC);
1943     if (perdir == NULL) {
1944         rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
1945     }
1946     else {
1947         rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
1948     }
1949
1950     /*
1951      *  Additionally do expansion for the environment variable
1952      *  strings (`RewriteRule .. .. [E=<string>]').
1953      */
1954     do_expand_env(r, p->env, briRR, briRC);
1955
1956     /*
1957      *  Now replace API's knowledge of the current URI:
1958      *  Replace r->filename with the new URI string and split out
1959      *  an on-the-fly generated QUERY_STRING part into r->args
1960      */
1961     r->filename = apr_pstrdup(r->pool, newuri);
1962     splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
1963
1964     /*
1965      *   Again add the previously stripped per-directory location
1966      *   prefix if the new URI is not a new one for this
1967      *   location, i.e. if it's not starting with either a slash
1968      *   or a fully qualified URL scheme.
1969      */
1970     if (prefixstrip && r->filename[0] != '/'
1971         && !is_absolute_uri(r->filename)) {
1972         rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
1973                    perdir, r->filename, perdir, r->filename);
1974         r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
1975     }
1976
1977     /*
1978      *  If this rule is forced for proxy throughput
1979      *  (`RewriteRule ... ... [P]') then emulate mod_proxy's
1980      *  URL-to-filename handler to be sure mod_proxy is triggered
1981      *  for this URL later in the Apache API. But make sure it is
1982      *  a fully-qualified URL. (If not it is qualified with
1983      *  ourself).
1984      */
1985     if (p->flags & RULEFLAG_PROXY) {
1986         fully_qualify_uri(r);
1987         if (perdir == NULL) {
1988             rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
1989         }
1990         else {
1991             rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
1992                        perdir, r->filename);
1993         }
1994         r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
1995         return 1;
1996     }
1997
1998     /*
1999      *  If this rule is explicitly forced for HTTP redirection
2000      *  (`RewriteRule .. .. [R]') then force an external HTTP
2001      *  redirect. But make sure it is a fully-qualified URL. (If
2002      *  not it is qualified with ourself).
2003      */
2004     if (p->flags & RULEFLAG_FORCEREDIRECT) {
2005         fully_qualify_uri(r);
2006         if (perdir == NULL) {
2007             rewritelog(r, 2,
2008                        "explicitly forcing redirect with %s", r->filename);
2009         }
2010         else {
2011             rewritelog(r, 2,
2012                        "[per-dir %s] explicitly forcing redirect with %s",
2013                        perdir, r->filename);
2014         }
2015         r->status = p->forced_responsecode;
2016         return 1;
2017     }
2018
2019     /*
2020      *  Special Rewriting Feature: Self-Reduction
2021      *  We reduce the URL by stripping a possible
2022      *  http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
2023      *  corresponds to ourself. This is to simplify rewrite maps
2024      *  and to avoid recursion, etc. When this prefix is not a
2025      *  coincidence then the user has to use [R] explicitly (see
2026      *  above).
2027      */
2028     reduce_uri(r);
2029
2030     /*
2031      *  If this rule is still implicitly forced for HTTP
2032      *  redirection (`RewriteRule .. <scheme>://...') then
2033      *  directly force an external HTTP redirect.
2034      */
2035     if (is_absolute_uri(r->filename)) {
2036         if (perdir == NULL) {
2037             rewritelog(r, 2,
2038                        "implicitly forcing redirect (rc=%d) with %s",
2039                        p->forced_responsecode, r->filename);
2040         }
2041         else {
2042             rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
2043                        "(rc=%d) with %s", perdir, p->forced_responsecode,
2044                        r->filename);
2045         }
2046         r->status = p->forced_responsecode;
2047         return 1;
2048     }
2049
2050     /*
2051      *  Now we are sure it is not a fully qualified URL.  But
2052      *  there is still one special case left: A local rewrite in
2053      *  per-directory context, i.e. a substitution URL which does
2054      *  not start with a slash. Here we add again the initially
2055      *  stripped per-directory prefix.
2056      */
2057     if (prefixstrip && r->filename[0] != '/') {
2058         rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
2059                    perdir, r->filename, perdir, r->filename);
2060         r->filename = apr_pstrcat(r->pool, perdir, r->filename, NULL);
2061     }
2062
2063     /*
2064      *  Finally we had to remember if a MIME-type should be
2065      *  forced for this URL (`RewriteRule .. .. [T=<type>]')
2066      *  Later in the API processing phase this is forced by our
2067      *  MIME API-hook function. This time its no problem even for
2068      *  the per-directory context (where the MIME-type hook was
2069      *  already processed) because a sub-request happens ;-)
2070      */
2071     if (p->forced_mimetype != NULL) {
2072         apr_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
2073                       p->forced_mimetype);
2074         if (perdir == NULL) {
2075             rewritelog(r, 2, "remember %s to have MIME-type '%s'",
2076                        r->filename, p->forced_mimetype);
2077         }
2078         else {
2079             rewritelog(r, 2,
2080                        "[per-dir %s] remember %s to have MIME-type '%s'",
2081                        perdir, r->filename, p->forced_mimetype);
2082         }
2083     }
2084
2085     /*
2086      *  Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
2087      *  But now we're done for this particular rule.
2088      */
2089     return 1;
2090 }
2091
2092 static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
2093                               char *perdir, backrefinfo *briRR,
2094                               backrefinfo *briRC)
2095 {
2096     char input[MAX_STRING_LEN];
2097     apr_finfo_t sb;
2098     request_rec *rsub;
2099     regmatch_t regmatch[MAX_NMATCH];
2100     int rc;
2101
2102     /*
2103      *   Construct the string we match against
2104      */
2105
2106     do_expand(r, p->input, input, sizeof(input), briRR, briRC);
2107
2108     /*
2109      *   Apply the patterns
2110      */
2111
2112     rc = 0;
2113     if (strcmp(p->pattern, "-f") == 0) {
2114         if (apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS) {
2115             if (sb.filetype == APR_REG) {
2116                 rc = 1;
2117             }
2118         }
2119     }
2120     else if (strcmp(p->pattern, "-s") == 0) {
2121         if (apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS) {
2122             if ((sb.filetype == APR_REG) && sb.size > 0) {
2123                 rc = 1;
2124             }
2125         }
2126     }
2127     else if (strcmp(p->pattern, "-l") == 0) {
2128 #if !defined(OS2)
2129         if (apr_lstat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS) {
2130             if (sb.filetype == APR_LNK) {
2131                 rc = 1;
2132             }
2133         }
2134 #endif
2135     }
2136     else if (strcmp(p->pattern, "-d") == 0) {
2137         if (apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS) {
2138             if (sb.filetype == APR_DIR) {
2139                 rc = 1;
2140             }
2141         }
2142     }
2143     else if (strcmp(p->pattern, "-U") == 0) {
2144         /* avoid infinite subrequest recursion */
2145         if (strlen(input) > 0 && subreq_ok(r)) {
2146
2147             /* run a URI-based subrequest */
2148             rsub = ap_sub_req_lookup_uri(input, r, NULL);
2149
2150             /* URI exists for any result up to 3xx, redirects allowed */
2151             if (rsub->status < 400)
2152                 rc = 1;
2153
2154             /* log it */
2155             rewritelog(r, 5, "RewriteCond URI (-U) check: "
2156                        "path=%s -> status=%d", input, rsub->status);
2157
2158             /* cleanup by destroying the subrequest */
2159             ap_destroy_sub_req(rsub);
2160         }
2161     }
2162     else if (strcmp(p->pattern, "-F") == 0) {
2163         /* avoid infinite subrequest recursion */
2164         if (strlen(input) > 0 && subreq_ok(r)) {
2165
2166             /* process a file-based subrequest:
2167              * this differs from -U in that no path translation is done.
2168              */
2169             rsub = ap_sub_req_lookup_file(input, r, NULL);
2170
2171             /* file exists for any result up to 2xx, no redirects */
2172             if (rsub->status < 300 &&
2173                 /* double-check that file exists since default result is 200 */
2174                 apr_stat(&sb, rsub->filename, APR_FINFO_MIN, 
2175                          r->pool) == APR_SUCCESS) {
2176                 rc = 1;
2177             }
2178
2179             /* log it */
2180             rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
2181                        "-> file=%s status=%d", input, rsub->filename, 
2182                        rsub->status);
2183
2184             /* cleanup by destroying the subrequest */
2185             ap_destroy_sub_req(rsub);
2186         }
2187     }
2188     else if (strlen(p->pattern) > 1 && *(p->pattern) == '>') {
2189         rc = (compare_lexicography(input, p->pattern+1) == 1 ? 1 : 0);
2190     }
2191     else if (strlen(p->pattern) > 1 && *(p->pattern) == '<') {
2192         rc = (compare_lexicography(input, p->pattern+1) == -1 ? 1 : 0);
2193     }
2194     else if (strlen(p->pattern) > 1 && *(p->pattern) == '=') {
2195         if (strcmp(p->pattern+1, "\"\"") == 0) {
2196             rc = (*input == '\0');
2197         }
2198         else {
2199             rc = (strcmp(input, p->pattern+1) == 0 ? 1 : 0);
2200         }
2201     }
2202     else {
2203         /* it is really a regexp pattern, so apply it */
2204         rc = (ap_regexec(p->regexp, input,
2205                          p->regexp->re_nsub+1, regmatch,0) == 0);
2206
2207         /* if it isn't a negated pattern and really matched
2208            we update the passed-through regex subst info structure */
2209         if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
2210             briRC->source = apr_pstrdup(r->pool, input);
2211             briRC->nsub   = p->regexp->re_nsub;
2212             memcpy((void *)(briRC->regmatch), (void *)(regmatch),
2213                    sizeof(regmatch));
2214         }
2215     }
2216
2217     /* if this is a non-matching regexp, just negate the result */
2218     if (p->flags & CONDFLAG_NOTMATCH) {
2219         rc = !rc;
2220     }
2221
2222     rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s' => %s",
2223                input, (p->flags & CONDFLAG_NOTMATCH ? "!" : ""),
2224                p->pattern, rc ? "matched" : "not-matched");
2225
2226     /* end just return the result */
2227     return rc;
2228 }
2229
2230
2231 /*
2232 ** +-------------------------------------------------------+
2233 ** |                                                       |
2234 ** |              URL transformation functions
2235 ** |                                                       |
2236 ** +-------------------------------------------------------+
2237 */
2238
2239
2240 /*
2241 **
2242 **  perform all the expansions on the input string
2243 **  leaving the result in the supplied buffer
2244 **
2245 */
2246
2247 static void do_expand(request_rec *r, char *input, char *buffer, int nbuf,
2248                        backrefinfo *briRR, backrefinfo *briRC)
2249 {
2250     char *inp, *outp;
2251     size_t span, space;
2252
2253     /*
2254      * for security reasons this expansion must be perfomed in a
2255      * single pass, otherwise an attacker can arrange for the result
2256      * of an earlier expansion to include expansion specifiers that
2257      * are interpreted by a later expansion, producing results that
2258      * were not intended by the administrator.
2259      */
2260
2261     inp = input;
2262     outp = buffer;
2263     space = nbuf - 1; /* room for '\0' */
2264
2265     for (;;) {
2266         span = strcspn(inp, "$%");
2267         if (span > space) {
2268             span = space;
2269         }
2270         memcpy(outp, inp, span);
2271         inp += span;
2272         outp += span;
2273         space -= span;
2274         if (space == 0 || *inp == '\0') {
2275             break;
2276         }
2277         /* now we have a '$' or a '%' */
2278         if (inp[1] == '{') {
2279             char *endp;
2280             endp = find_closing_bracket(inp+2, '{', '}');
2281             if (endp == NULL) {
2282                 goto skip;
2283             }
2284             /*
2285              * These lookups may be recursive in a very convoluted
2286              * fashion -- see the LA-U and LA-F variable expansion
2287              * prefixes -- so we copy lookup keys to a separate buffer
2288              * rather than adding zero bytes in order to use them in
2289              * place.
2290              */
2291             if (inp[0] == '$') {
2292                 /* ${...} map lookup expansion */
2293                 /*
2294                  * To make rewrite maps useful the lookup key and
2295                  * default values must be expanded, so we make
2296                  * recursive calls to do the work. For security
2297                  * reasons we must never expand a string that includes
2298                  * verbatim data from the network. The recursion here
2299                  * isn't a problem because the result of expansion is
2300                  * only passed to lookup_map() so it cannot be
2301                  * re-expanded, only re-looked-up. Another way of
2302                  * looking at it is that the recursion is entirely
2303                  * driven by the syntax of the nested curly brackets.
2304                  */
2305                 char *map, *key, *dflt, *result;
2306                 char xkey[MAX_STRING_LEN];
2307                 char xdflt[MAX_STRING_LEN];
2308                 key = find_char_in_brackets(inp+2, ':', '{', '}');
2309                 if (key == NULL)
2310                     goto skip;
2311                 map  = apr_pstrndup(r->pool, inp+2, key-inp-2);
2312                 dflt = find_char_in_brackets(key+1, '|', '{', '}');
2313                 if (dflt == NULL) {
2314                     key  = apr_pstrndup(r->pool, key+1, endp-key-1);
2315                     dflt = "";
2316                 } else {
2317                     key  = apr_pstrndup(r->pool, key+1, dflt-key-1);
2318                     dflt = apr_pstrndup(r->pool, dflt+1, endp-dflt-1);
2319                 }
2320                 do_expand(r, key,  xkey,  sizeof(xkey),  briRR, briRC);
2321                 result = lookup_map(r, map, xkey);
2322                 if (result) {
2323                     span = apr_cpystrn(outp, result, space) - outp;
2324                 } else {
2325                     do_expand(r, dflt, xdflt, sizeof(xdflt), briRR, briRC);
2326                     span = apr_cpystrn(outp, xdflt, space) - outp;
2327                 }
2328             }
2329             else if (inp[0] == '%') {
2330                 /* %{...} variable lookup expansion */
2331                 char *var;
2332                 var  = apr_pstrndup(r->pool, inp+2, endp-inp-2);
2333                 span = apr_cpystrn(outp, lookup_variable(r, var), space) - outp;
2334             }
2335             else {
2336                 span = 0;
2337             }
2338             inp = endp+1;
2339             outp += span;
2340             space -= span;
2341             continue;
2342         }
2343         else if (apr_isdigit(inp[1])) {
2344             int n = inp[1] - '0';
2345             backrefinfo *bri = NULL;
2346             if (inp[0] == '$') {
2347                 /* $N RewriteRule regexp backref expansion */
2348                 bri = briRR;
2349             }
2350             else if (inp[0] == '%') {
2351                 /* %N RewriteCond regexp backref expansion */
2352                 bri = briRC;
2353             }
2354             /* see ap_pregsub() in src/main/util.c */
2355             if (bri && n <= bri->nsub &&
2356                 bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
2357                 span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
2358                 if (span > space) {
2359                     span = space;
2360                 }
2361                 memcpy(outp, bri->source + bri->regmatch[n].rm_so, span);
2362                 outp += span;
2363                 space -= span;
2364             }
2365             inp += 2;
2366             continue;
2367         }
2368     skip:
2369         *outp++ = *inp++;
2370         space--;
2371     }
2372     *outp++ = '\0';
2373 }
2374
2375
2376 /*
2377 **
2378 **  perform all the expansions on the environment variables
2379 **
2380 */
2381
2382 static void do_expand_env(request_rec *r, char *env[],
2383                           backrefinfo *briRR, backrefinfo *briRC)
2384 {
2385     int i;
2386     char buf[MAX_STRING_LEN];
2387
2388     for (i = 0; env[i] != NULL; i++) {
2389         do_expand(r, env[i], buf, sizeof(buf), briRR, briRC);
2390         add_env_variable(r, buf);
2391     }
2392 }
2393
2394
2395 /*
2396 **
2397 **  split out a QUERY_STRING part from
2398 **  the current URI string
2399 **
2400 */
2401
2402 static void splitout_queryargs(request_rec *r, int qsappend)
2403 {
2404     char *q;
2405     char *olduri;
2406
2407     q = strchr(r->filename, '?');
2408     if (q != NULL) {
2409         olduri = apr_pstrdup(r->pool, r->filename);
2410         *q++ = '\0';
2411         if (qsappend) {
2412             r->args = apr_pstrcat(r->pool, q, "&", r->args, NULL);
2413         }
2414         else {
2415             r->args = apr_pstrdup(r->pool, q);
2416         }
2417         if (strlen(r->args) == 0) {
2418             r->args = NULL;
2419             rewritelog(r, 3, "split uri=%s -> uri=%s, args=<none>", olduri,
2420                        r->filename);
2421         }
2422         else {
2423             if (r->args[strlen(r->args)-1] == '&') {
2424                 r->args[strlen(r->args)-1] = '\0';
2425             }
2426             rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
2427                        r->filename, r->args);
2428         }
2429     }
2430     return;
2431 }
2432
2433
2434 /*
2435 **
2436 **  strip 'http[s]://ourhost/' from URI
2437 **
2438 */
2439
2440 static void reduce_uri(request_rec *r)
2441 {
2442     char *cp;
2443     unsigned short port;
2444     char *portp;
2445     char *hostp;
2446     char *url;
2447     char c;
2448     char host[LONG_STRING_LEN];
2449     char buf[MAX_STRING_LEN];
2450     char *olduri;
2451     int l;
2452
2453     cp = (char *)ap_http_method(r);
2454     l  = strlen(cp);
2455     if (   strlen(r->filename) > l+3 
2456         && strncasecmp(r->filename, cp, l) == 0
2457         && r->filename[l]   == ':'
2458         && r->filename[l+1] == '/'
2459         && r->filename[l+2] == '/'             ) {
2460         /* there was really a rewrite to a remote path */
2461
2462         olduri = apr_pstrdup(r->pool, r->filename); /* save for logging */
2463
2464         /* cut the hostname and port out of the URI */
2465         apr_cpystrn(buf, r->filename+(l+3), sizeof(buf));
2466         hostp = buf;
2467         for (cp = hostp; *cp != '\0' && *cp != '/' && *cp != ':'; cp++)
2468             ;
2469         if (*cp == ':') {
2470             /* set host */
2471             *cp++ = '\0';
2472             apr_cpystrn(host, hostp, sizeof(host));
2473             /* set port */
2474             portp = cp;
2475             for (; *cp != '\0' && *cp != '/'; cp++)
2476                 ;
2477             c = *cp;
2478             *cp = '\0';
2479             port = atoi(portp);
2480             *cp = c;
2481             /* set remaining url */
2482             url = cp;
2483         }
2484         else if (*cp == '/') {
2485             /* set host */
2486             *cp = '\0';
2487             apr_cpystrn(host, hostp, sizeof(host));
2488             *cp = '/';
2489             /* set port */
2490             port = ap_default_port(r);
2491             /* set remaining url */
2492             url = cp;
2493         }
2494         else {
2495             /* set host */
2496             apr_cpystrn(host, hostp, sizeof(host));
2497             /* set port */
2498             port = ap_default_port(r);
2499             /* set remaining url */
2500             url = "/";
2501         }
2502
2503         /* now check whether we could reduce it to a local path... */
2504         if (ap_matches_request_vhost(r, host, port)) {
2505             /* this is our host, so only the URL remains */
2506             r->filename = apr_pstrdup(r->pool, url);
2507             rewritelog(r, 3, "reduce %s -> %s", olduri, r->filename);
2508         }
2509     }
2510     return;
2511 }
2512
2513
2514 /*
2515 **
2516 **  add 'http[s]://ourhost[:ourport]/' to URI
2517 **  if URI is still not fully qualified
2518 **
2519 */
2520
2521 static void fully_qualify_uri(request_rec *r)
2522 {
2523     char buf[32];
2524     const char *thisserver;
2525     char *thisport;
2526     int port;
2527
2528     if (!is_absolute_uri(r->filename)) {
2529
2530         thisserver = ap_get_server_name(r);
2531         port = ap_get_server_port(r);
2532         if (ap_is_default_port(port,r)) {
2533             thisport = "";
2534         }
2535         else {
2536             apr_snprintf(buf, sizeof(buf), ":%u", port);
2537             thisport = buf;
2538         }
2539
2540         if (r->filename[0] == '/') {
2541             r->filename = apr_psprintf(r->pool, "%s://%s%s%s",
2542                                       ap_http_method(r), thisserver,
2543                                       thisport, r->filename);
2544         }
2545         else {
2546             r->filename = apr_psprintf(r->pool, "%s://%s%s/%s",
2547                                       ap_http_method(r), thisserver,
2548                                       thisport, r->filename);
2549         }
2550     }
2551     return;
2552 }
2553
2554
2555 /*
2556 **
2557 **  return non-zero if the URI is absolute (includes a scheme etc.)
2558 **
2559 */
2560
2561 static int is_absolute_uri(char *uri)
2562 {
2563     int i = strlen(uri);
2564     if (   (i > 7 && strncasecmp(uri, "http://",   7) == 0)
2565         || (i > 8 && strncasecmp(uri, "https://",  8) == 0)
2566         || (i > 9 && strncasecmp(uri, "gopher://", 9) == 0)
2567         || (i > 6 && strncasecmp(uri, "ftp://",    6) == 0)
2568         || (i > 5 && strncasecmp(uri, "ldap:",     5) == 0)
2569         || (i > 5 && strncasecmp(uri, "news:",     5) == 0)
2570         || (i > 7 && strncasecmp(uri, "mailto:",   7) == 0) ) {
2571         return 1;
2572     }
2573     else {
2574         return 0;
2575     }
2576 }
2577
2578
2579 /*
2580 **
2581 **  Expand tilde-paths (/~user) through Unix /etc/passwd 
2582 **  database information (or other OS-specific database)
2583 **
2584 */
2585 #if APR_HAS_USER
2586 static char *expand_tildepaths(request_rec *r, char *uri)
2587 {
2588     char user[LONG_STRING_LEN];
2589     char *newuri;
2590     int i, j;
2591     char *homedir;
2592
2593     newuri = uri;
2594     if (uri != NULL && strlen(uri) > 2 && uri[0] == '/' && uri[1] == '~') {
2595         /* cut out the username */
2596         for (j = 0, i = 2; j < sizeof(user)-1
2597                && uri[i] != '\0'
2598                && uri[i] != '/'  ; ) {
2599             user[j++] = uri[i++];
2600         }
2601         user[j] = '\0';
2602
2603         /* lookup username in systems passwd file */
2604         if (apr_get_home_directory(&homedir, user, r->pool) == APR_SUCCESS) {
2605             /* ok, user was found, so expand the ~user string */
2606             if (uri[i] != '\0') {
2607                 /* ~user/anything...  has to be expanded */
2608                 if (homedir[strlen(homedir)-1] == '/') {
2609                     homedir[strlen(homedir)-1] = '\0';
2610                 }
2611                 newuri = apr_pstrcat(r->pool, homedir, uri+i, NULL);
2612             }
2613             else {
2614                 /* only ~user has to be expanded */
2615                 newuri = homedir;
2616             }
2617         }
2618     }
2619     return newuri;
2620 }
2621 #endif  /* if APR_HAS_USER */
2622
2623
2624
2625 /*
2626 ** +-------------------------------------------------------+
2627 ** |                                                       |
2628 ** |              DBM hashfile support
2629 ** |                                                       |
2630 ** +-------------------------------------------------------+
2631 */
2632
2633
2634 static char *lookup_map(request_rec *r, char *name, char *key)
2635 {
2636     void *sconf;
2637     rewrite_server_conf *conf;
2638     apr_array_header_t *rewritemaps;
2639     rewritemap_entry *entries;
2640     rewritemap_entry *s;
2641     char *value;
2642     apr_finfo_t st;
2643     apr_status_t rv;
2644     int i;
2645
2646     /* get map configuration */
2647     sconf = r->server->module_config;
2648     conf  = (rewrite_server_conf *)ap_get_module_config(sconf, 
2649                                                         &rewrite_module);
2650     rewritemaps = conf->rewritemaps;
2651
2652     entries = (rewritemap_entry *)rewritemaps->elts;
2653     for (i = 0; i < rewritemaps->nelts; i++) {
2654         s = &entries[i];
2655         if (strcmp(s->name, name) == 0) {
2656             if (s->type == MAPTYPE_TXT) {
2657                 if ((rv = apr_stat(&st, s->checkfile, 
2658                                    APR_FINFO_MIN, r->pool)) != APR_SUCCESS) {
2659                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2660                                  "mod_rewrite: can't access text RewriteMap "
2661                                  "file %s", s->checkfile);
2662                     rewritelog(r, 1, "can't open RewriteMap file, "
2663                                "see error log");
2664                     return NULL;
2665                 }
2666                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2667                                          st.mtime, key);
2668                 if (value == NULL) {
2669                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
2670                                "map lookup");
2671                     if ((value =
2672                          lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2673                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2674                                    "-> val=%s", s->name, key, value);
2675                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2676                                          st.mtime, key, value);
2677                         return value;
2678                     }
2679                     else {
2680                         rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2681                                    "key=%s", s->name, key);
2682                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2683                                          st.mtime, key, "");
2684                         return NULL;
2685                     }
2686                 }
2687                 else {
2688                     rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2689                                "-> val=%s", s->name, key, value);
2690                     return value[0] != '\0' ? value : NULL;
2691                 }
2692             }
2693             else if (s->type == MAPTYPE_DBM) {
2694 #ifndef NO_DBM_REWRITEMAP
2695                 if ((rv = apr_stat(&st, s->checkfile,
2696                                    APR_FINFO_MIN, r->pool)) != APR_SUCCESS) {
2697                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2698                                  "mod_rewrite: can't access DBM RewriteMap "
2699                                  "file %s", s->checkfile);
2700                     rewritelog(r, 1, "can't open DBM RewriteMap file, "
2701                                "see error log");
2702                     return NULL;
2703                 }
2704                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2705                                          st.mtime, key);
2706                 if (value == NULL) {
2707                     rewritelog(r, 6,
2708                                "cache lookup FAILED, forcing new map lookup");
2709                     if ((value =
2710                          lookup_map_dbmfile(r, s->datafile, key)) != NULL) {
2711                         rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s "
2712                                    "-> val=%s", s->name, key, value);
2713                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2714                                          st.mtime, key, value);
2715                         return value;
2716                     }
2717                     else {
2718                         rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] "
2719                                    "key=%s", s->name, key);
2720                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2721                                          st.mtime, key, "");
2722                         return NULL;
2723                     }
2724                 }
2725                 else {
2726                     rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s "
2727                                "-> val=%s", s->name, key, value);
2728                     return value[0] != '\0' ? value : NULL;
2729                 }
2730 #else
2731                 return NULL;
2732 #endif
2733             }
2734             else if (s->type == MAPTYPE_PRG) {
2735                 if ((value =
2736                      lookup_map_program(r, s->fpin, s->fpout, key)) != NULL) {
2737                     rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2738                                s->name, key, value);
2739                     return value;
2740                 }
2741                 else {
2742                     rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2743                                s->name, key);
2744                 }
2745             }
2746             else if (s->type == MAPTYPE_INT) {
2747                 if ((value = lookup_map_internal(r, s->func, key)) != NULL) {
2748                     rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2749                                s->name, key, value);
2750                     return value;
2751                 }
2752                 else {
2753                     rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2754                                s->name, key);
2755                 }
2756             }
2757             else if (s->type == MAPTYPE_RND) {
2758                 if ((rv = apr_stat(&st, s->checkfile,
2759                                    APR_FINFO_MIN, r->pool)) != APR_SUCCESS) {
2760                     ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
2761                                  "mod_rewrite: can't access text RewriteMap "
2762                                  "file %s", s->checkfile);
2763                     rewritelog(r, 1, "can't open RewriteMap file, "
2764                                "see error log");
2765                     return NULL;
2766                 }
2767                 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2768                                          st.mtime, key);
2769                 if (value == NULL) {
2770                     rewritelog(r, 6, "cache lookup FAILED, forcing new "
2771                                "map lookup");
2772                     if ((value =
2773                          lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2774                         rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2775                                    "-> val=%s", s->name, key, value);
2776                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2777                                          st.mtime, key, value);
2778                     }
2779                     else {
2780                         rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2781                                    "key=%s", s->name, key);
2782                         set_cache_string(cachep, s->name, CACHEMODE_TS,
2783                                          st.mtime, key, "");
2784                         return NULL;
2785                     }
2786                 }
2787                 else {
2788                     rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2789                                "-> val=%s", s->name, key, value);
2790                 }
2791                 if (value[0] != '\0') {
2792                    value = select_random_value_part(r, value);
2793                    rewritelog(r, 5, "randomly choosen the subvalue `%s'", value);
2794                 }
2795                 else {
2796                     value = NULL;
2797                 }
2798                 return value;
2799             }
2800         }
2801     }
2802     return NULL;
2803 }
2804
2805 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
2806 {
2807     apr_file_t *fp = NULL;
2808     apr_status_t rc;
2809     char line[1024];
2810     char *value = NULL;
2811     char *cpT;
2812     size_t skip;
2813     char *curkey;
2814     char *curval;
2815
2816     rc = apr_file_open(&fp, file, APR_READ, APR_OS_DEFAULT, r->pool);
2817     if (rc != APR_SUCCESS) {
2818        return NULL;
2819     }
2820
2821     while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
2822         if (line[0] == '#')
2823             continue; /* ignore comments */
2824         cpT = line;
2825         curkey = cpT;
2826         skip = strcspn(cpT," \t\r\n");
2827         if (skip == 0)
2828             continue; /* ignore lines that start with a space, tab, CR, or LF */
2829         cpT += skip;
2830         *cpT = '\0';
2831         if (strcmp(curkey, key) != 0)
2832             continue; /* key does not match... */
2833             
2834         /* found a matching key; now extract and return the value */
2835         ++cpT;
2836         skip = strspn(cpT, " \t\r\n");
2837         cpT += skip;
2838         curval = cpT;
2839         skip = strcspn(cpT, " \t\r\n");
2840         if (skip == 0)
2841             continue; /* no value... */
2842         cpT += skip;
2843         *cpT = '\0';
2844         value = apr_pstrdup(r->pool, curval);
2845         break;
2846     }
2847     apr_file_close(fp);
2848     return value;
2849 }
2850
2851 #ifndef NO_DBM_REWRITEMAP
2852 static char *lookup_map_dbmfile(request_rec *r, const char *file, char *key)
2853 {
2854     DBM *dbmfp = NULL;
2855     datum dbmkey;
2856     datum dbmval;
2857     char *value = NULL;
2858     char buf[MAX_STRING_LEN];
2859
2860     dbmkey.dptr  = key;
2861     dbmkey.dsize = strlen(key);
2862     if ((dbmfp = dbm_open(file, O_RDONLY, 0666)) != NULL) {
2863         dbmval = dbm_fetch(dbmfp, dbmkey);
2864         if (dbmval.dptr != NULL) {
2865             memcpy(buf, dbmval.dptr, 
2866                    dbmval.dsize < sizeof(buf)-1 ? 
2867                    dbmval.dsize : sizeof(buf)-1  );
2868             buf[dbmval.dsize] = '\0';
2869             value = apr_pstrdup(r->pool, buf);
2870         }
2871         dbm_close(dbmfp);
2872     }
2873     return value;
2874 }
2875 #endif
2876
2877 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
2878                                 apr_file_t *fpout, char *key)
2879 {
2880     char buf[LONG_STRING_LEN];
2881     char c;
2882     int i;
2883     apr_size_t nbytes;
2884
2885 #ifndef NO_WRITEV
2886     struct iovec iova[2];
2887     apr_size_t niov;
2888 #endif
2889
2890     /* when `RewriteEngine off' was used in the per-server
2891      * context then the rewritemap-programs were not spawned.
2892      * In this case using such a map (usually in per-dir context)
2893      * is useless because it is not available.
2894      */
2895     if (fpin == NULL || fpout == NULL) {
2896         return NULL;
2897     }
2898
2899     /* take the lock */
2900
2901     if (rewrite_mapr_lock_aquire) {
2902         apr_lock_aquire(rewrite_mapr_lock_aquire);
2903     }
2904
2905     /* write out the request key */
2906 #ifdef NO_WRITEV
2907     nbytes = strlen(key);
2908     apr_file_write(fpin, key, &nbytes);
2909     nbytes = 1;
2910     apr_file_write(fpin, "\n", &nbytes);
2911 #else
2912     iova[0].iov_base = key;
2913     iova[0].iov_len = strlen(key);
2914     iova[1].iov_base = "\n";
2915     iova[1].iov_len = 1;
2916
2917     niov = 2;
2918     apr_file_writev(fpin, iova, niov, &nbytes);
2919 #endif
2920
2921     /* read in the response value */
2922     i = 0;
2923     nbytes = 1;
2924     apr_file_read(fpout, &c, &nbytes);
2925     while (nbytes == 1 && (i < LONG_STRING_LEN-1)) {
2926         if (c == '\n') {
2927             break;
2928         }
2929         buf[i++] = c;
2930
2931         apr_file_read(fpout, &c, &nbytes);
2932     }
2933     buf[i] = '\0';
2934
2935     /* give the lock back */
2936     if (rewrite_mapr_lock_aquire) {
2937         apr_lock_release(rewrite_mapr_lock_aquire);
2938     }
2939
2940     if (strcasecmp(buf, "NULL") == 0) {
2941         return NULL;
2942     }
2943     else {
2944         return apr_pstrdup(r->pool, buf);
2945     }
2946 }
2947
2948 static char *lookup_map_internal(request_rec *r,
2949                                  char *(*func)(request_rec *, char *),
2950                                  char *key)
2951 {
2952     /* currently we just let the function convert
2953        the key to a corresponding value */
2954     return func(r, key);
2955 }
2956
2957 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
2958 {
2959     char *value, *cp;
2960
2961     for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2962          cp++) {
2963         *cp = apr_toupper(*cp);
2964     }
2965     return value;
2966 }
2967
2968 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
2969 {
2970     char *value, *cp;
2971
2972     for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2973          cp++) {
2974         *cp = apr_tolower(*cp);
2975     }
2976     return value;
2977 }
2978
2979 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
2980 {
2981     char *value;
2982
2983     value = ap_escape_uri(r->pool, key);
2984     return value;
2985 }
2986
2987 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
2988 {
2989     char *value;
2990
2991     value = apr_pstrdup(r->pool, key);
2992     ap_unescape_url(value);
2993     return value;
2994 }
2995
2996 static int rewrite_rand_init_done = 0;
2997
2998 static void rewrite_rand_init(void)
2999 {
3000     if (!rewrite_rand_init_done) {
3001         srand((unsigned)(getpid()));
3002         rewrite_rand_init_done = 1;
3003     }
3004     return;
3005 }
3006
3007 static int rewrite_rand(int l, int h)
3008 {
3009     rewrite_rand_init();
3010
3011     /* Get [0,1) and then scale to the appropriate range. Note that using
3012      * a floating point value ensures that we use all bits of the rand()
3013      * result. Doing an integer modulus would only use the lower-order bits
3014      * which may not be as uniformly random.
3015      */
3016     return ((double)(rand() % RAND_MAX) / RAND_MAX) * (h - l + 1) + l;
3017 }
3018
3019 static char *select_random_value_part(request_rec *r, char *value)
3020 {
3021     char *buf;
3022     int n, i, k;
3023
3024     /*  count number of distinct values  */
3025     for (n = 1, i = 0; value[i] != '\0'; i++) {
3026         if (value[i] == '|') {
3027             n++;
3028         }
3029     }
3030
3031     /*  when only one value we have no option to choose  */
3032     if (n == 1) {
3033         return value;
3034     }
3035
3036     /*  else randomly select one  */
3037     k = rewrite_rand(1, n);
3038
3039     /*  and grep it out  */
3040     for (n = 1, i = 0; value[i] != '\0'; i++) {
3041         if (n == k) {
3042             break;
3043         }
3044         if (value[i] == '|') {
3045             n++;
3046         }
3047     }
3048     buf = apr_pstrdup(r->pool, &value[i]);
3049     for (i = 0; buf[i] != '\0' && buf[i] != '|'; i++)
3050         ;
3051     buf[i] = '\0';
3052     return buf;
3053 }
3054
3055
3056 /*
3057 ** +-------------------------------------------------------+
3058 ** |                                                       |
3059 ** |              rewriting logfile support
3060 ** |                                                       |
3061 ** +-------------------------------------------------------+
3062 */
3063
3064
3065 static void open_rewritelog(server_rec *s, apr_pool_t *p)
3066 {
3067     rewrite_server_conf *conf;
3068     const char *fname;
3069     apr_status_t rc;
3070     piped_log *pl;
3071     int    rewritelog_flags = ( APR_WRITE | APR_APPEND | APR_CREATE );
3072     mode_t rewritelog_mode  = ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD );
3073
3074     conf = ap_get_module_config(s->module_config, &rewrite_module);
3075
3076     if (conf->rewritelogfile == NULL) {
3077         return;
3078     }
3079     if (*(conf->rewritelogfile) == '\0') {
3080         return;
3081     }
3082     if (conf->rewritelogfp != NULL) {
3083         return; /* virtual log shared w/ main server */
3084     }
3085
3086     fname = ap_server_root_relative(p, conf->rewritelogfile);
3087
3088     if (*conf->rewritelogfile == '|') {
3089         if ((pl = ap_open_piped_log(p, conf->rewritelogfile+1)) == NULL) {
3090             ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, s, 
3091                          "mod_rewrite: could not open reliable pipe "
3092                          "to RewriteLog filter %s", conf->rewritelogfile+1);
3093             exit(1);
3094         }
3095         conf->rewritelogfp = ap_piped_log_write_fd(pl);
3096     }
3097     else if (*conf->rewritelogfile != '\0') {
3098         rc = apr_file_open(&conf->rewritelogfp, fname, rewritelog_flags, rewritelog_mode, p);
3099         if (rc != APR_SUCCESS)  {
3100             ap_log_error(APLOG_MARK, APLOG_ERR, rc, s, 
3101                          "mod_rewrite: could not open RewriteLog "
3102                          "file %s", fname);
3103             exit(1);
3104         }
3105     }
3106     return;
3107 }
3108
3109 static void rewritelog(request_rec *r, int level, const char *text, ...)
3110 {
3111     rewrite_server_conf *conf;
3112     conn_rec *conn;
3113     char *str1;
3114     char str2[512];
3115     char str3[1024];
3116     char type[20];
3117     char redir[20];
3118     va_list ap;
3119     int i;
3120     apr_size_t nbytes;
3121     request_rec *req;
3122     char *ruser;
3123     const char *rhost;
3124
3125     va_start(ap, text);
3126     conf = ap_get_module_config(r->server->module_config, &rewrite_module);
3127     conn = r->connection;
3128
3129     if (conf->rewritelogfp == NULL) {
3130         return;
3131     }
3132     if (conf->rewritelogfile == NULL) {
3133         return;
3134     }
3135     if (*(conf->rewritelogfile) == '\0') {
3136         return;
3137     }
3138
3139     if (level > conf->rewriteloglevel) {
3140         return;
3141     }
3142
3143     if (r->user == NULL) {
3144         ruser = "-";
3145     }
3146     else if (strlen(r->user) != 0) {
3147         ruser = r->user;
3148     }
3149     else {
3150         ruser = "\"\"";
3151     }
3152
3153     rhost = ap_get_remote_host(conn, r->server->module_config, 
3154                                REMOTE_NOLOOKUP);
3155     if (rhost == NULL) {
3156         rhost = "UNKNOWN-HOST";
3157     }
3158
3159     str1 = apr_pstrcat(r->pool, rhost, " ",
3160                       (conn->remote_logname != NULL ?
3161                       conn->remote_logname : "-"), " ",
3162                       ruser, NULL);
3163     apr_vsnprintf(str2, sizeof(str2), text, ap);
3164
3165     if (r->main == NULL) {
3166         strcpy(type, "initial");
3167     }
3168     else {
3169         strcpy(type, "subreq");
3170     }
3171
3172     for (i = 0, req = r; req->prev != NULL; req = req->prev) {
3173         i++;
3174     }
3175     if (i == 0) {
3176         redir[0] = '\0';
3177     }
3178     else {
3179         apr_snprintf(redir, sizeof(redir), "/redir#%d", i);
3180     }
3181
3182     apr_snprintf(str3, sizeof(str3),
3183                 "%s %s [%s/sid#%lx][rid#%lx/%s%s] (%d) %s\n", str1,
3184                 current_logtime(r), ap_get_server_name(r),
3185                 (unsigned long)(r->server), (unsigned long)r,
3186                 type, redir, level, str2);
3187
3188     apr_lock_aquire(rewrite_log_lock);
3189     nbytes = strlen(str3);
3190     apr_file_write(conf->rewritelogfp, str3, &nbytes);
3191     apr_lock_release(rewrite_log_lock);
3192
3193     va_end(ap);
3194     return;
3195 }
3196
3197 static char *current_logtime(request_rec *r)
3198 {
3199     apr_exploded_time_t t;
3200     char tstr[80];
3201     apr_size_t len;
3202
3203     apr_explode_localtime(&t, apr_time_now());
3204
3205     apr_strftime(tstr, &len, 80, "[%d/%b/%Y:%H:%M:%S ", &t);
3206     apr_snprintf(tstr + strlen(tstr), 80-strlen(tstr), "%c%.2d%.2d]",
3207                 t.tm_gmtoff < 0 ? '-' : '+',
3208                 t.tm_gmtoff / (60*60), t.tm_gmtoff % (60*60));
3209     return apr_pstrdup(r->pool, tstr);
3210 }
3211
3212
3213
3214
3215 /*
3216 ** +-------------------------------------------------------+
3217 ** |                                                       |
3218 ** |              rewriting lockfile support
3219 ** |                                                       |
3220 ** +-------------------------------------------------------+
3221 */
3222
3223 #define REWRITELOCK_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
3224
3225 static void rewritelock_create(server_rec *s, apr_pool_t *p)
3226 {
3227     apr_status_t rc;
3228
3229     /* only operate if a lockfile is used */
3230     if (lockname == NULL || *(lockname) == '\0') {
3231         return;
3232     }
3233
3234     /* fixup the path, especially for rewritelock_remove() */
3235     lockname = ap_server_root_relative(p, lockname);
3236
3237     /* create the lockfile */
3238     rc = apr_lock_create (&rewrite_mapr_lock_aquire, APR_MUTEX, APR_LOCKALL, lockname, p);
3239     if (rc != APR_SUCCESS) {
3240         ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
3241                      "mod_rewrite: Parent could not create RewriteLock "
3242                      "file %s", lockname);
3243         exit(1);
3244     }
3245
3246     return;
3247 }
3248
3249 static apr_status_t rewritelock_remove(void *data)
3250 {
3251     /* only operate if a lockfile is used */
3252     if (lockname == NULL || *(lockname) == '\0') {
3253         return APR_SUCCESS;
3254     }
3255
3256     /* destroy the rewritelock */
3257     apr_lock_destroy (rewrite_mapr_lock_aquire);
3258     rewrite_mapr_lock_aquire = NULL;
3259     lockname = NULL;
3260     return(0);
3261 }
3262
3263
3264 /*
3265 ** +-------------------------------------------------------+
3266 ** |                                                       |
3267 ** |                  program map support
3268 ** |                                                       |
3269 ** +-------------------------------------------------------+
3270 */
3271
3272 static void run_rewritemap_programs(server_rec *s, apr_pool_t *p)
3273 {
3274     rewrite_server_conf *conf;
3275     apr_file_t *fpin = NULL;
3276     apr_file_t *fpout = NULL;
3277     apr_file_t *fperr = NULL;
3278     apr_array_header_t *rewritemaps;
3279     rewritemap_entry *entries;
3280     rewritemap_entry *map;
3281     int i;
3282     apr_status_t rc;
3283
3284     conf = ap_get_module_config(s->module_config, &rewrite_module);
3285
3286     /*  If the engine isn't turned on,
3287      *  don't even try to do anything.
3288      */
3289     if (conf->state == ENGINE_DISABLED) {
3290         return;
3291     }
3292
3293     rewritemaps = conf->rewritemaps;
3294     entries = (rewritemap_entry *)rewritemaps->elts;
3295     for (i = 0; i < rewritemaps->nelts; i++) {
3296         map = &entries[i];
3297         if (map->type != MAPTYPE_PRG) {
3298             continue;
3299         }
3300         if (map->datafile == NULL
3301             || *(map->datafile) == '\0'
3302             || map->fpin  != NULL
3303             || map->fpout != NULL        ) {
3304             continue;
3305         }
3306         fpin  = NULL;
3307         fpout = NULL;
3308         rc = rewritemap_program_child(p, map->datafile,
3309                                      &fpout, &fpin, &fperr);
3310         if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
3311             ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
3312                          "mod_rewrite: could not fork child for "
3313                          "RewriteMap process");
3314             exit(1);
3315         }
3316         map->fpin  = fpin;
3317         map->fpout = fpout;
3318         map->fperr = fperr;
3319     }
3320     return;
3321 }
3322
3323 /* child process code */
3324 static apr_status_t rewritemap_program_child(apr_pool_t *p, const char *progname,
3325                                              apr_file_t **fpout, apr_file_t **fpin,
3326                                              apr_file_t **fperr)
3327 {
3328     apr_status_t rc;
3329     apr_procattr_t *procattr;
3330     apr_proc_t *procnew;
3331
3332 #ifdef SIGHUP
3333     apr_signal(SIGHUP, SIG_IGN);
3334 #endif
3335
3336     
3337     if (((rc = apr_procattr_create(&procattr, p)) != APR_SUCCESS) ||
3338         ((rc = apr_procattr_io_set(procattr, APR_FULL_BLOCK,
3339                                   APR_FULL_NONBLOCK,
3340                                   APR_FULL_NONBLOCK)) != APR_SUCCESS) ||
3341         ((rc = apr_procattr_dir_set(procattr, 
3342                                    ap_make_dirstr_parent(p, progname)))
3343          != APR_SUCCESS) ||
3344         ((rc = apr_procattr_cmdtype_set(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
3345         /* Something bad happened, give up and go away. */
3346     }
3347     else {
3348         procnew = apr_pcalloc(p, sizeof(*procnew));
3349         rc = apr_proc_create(procnew, progname, NULL, NULL, procattr, p);
3350     
3351         if (rc == APR_SUCCESS) {
3352             apr_pool_note_subprocess(p, procnew, kill_after_timeout);
3353
3354             if (fpin) {
3355                 (*fpin) = procnew->in;
3356             }
3357
3358             if (fpout) {
3359                 (*fpout) = procnew->out;
3360             }
3361
3362             if (fperr) {
3363                 (*fperr) = procnew->err;
3364             }
3365         }
3366     }
3367
3368     return (rc);
3369 }
3370
3371
3372
3373
3374 /*
3375 ** +-------------------------------------------------------+
3376 ** |                                                       |
3377 ** |             environment variable support
3378 ** |                                                       |
3379 ** +-------------------------------------------------------+
3380 */
3381
3382
3383 static char *lookup_variable(request_rec *r, char *var)
3384 {
3385     const char *result;
3386     char resultbuf[LONG_STRING_LEN];
3387     apr_exploded_time_t tm;
3388     request_rec *rsub;
3389
3390     result = NULL;
3391
3392     /* HTTP headers */
3393     if (strcasecmp(var, "HTTP_USER_AGENT") == 0) {
3394         result = lookup_header(r, "User-Agent");
3395     }
3396     else if (strcasecmp(var, "HTTP_REFERER") == 0) {
3397         result = lookup_header(r, "Referer");
3398     }
3399     else if (strcasecmp(var, "HTTP_COOKIE") == 0) {
3400         result = lookup_header(r, "Cookie");
3401     }
3402     else if (strcasecmp(var, "HTTP_FORWARDED") == 0) {
3403         result = lookup_header(r, "Forwarded");
3404     }
3405     else if (strcasecmp(var, "HTTP_HOST") == 0) {
3406         result = lookup_header(r, "Host");
3407     }
3408     else if (strcasecmp(var, "HTTP_PROXY_CONNECTION") == 0) {
3409         result = lookup_header(r, "Proxy-Connection");
3410     }
3411     else if (strcasecmp(var, "HTTP_ACCEPT") == 0) {
3412         result = lookup_header(r, "Accept");
3413     }
3414     /* all other headers from which we are still not know about */
3415     else if (strlen(var) > 5 && strncasecmp(var, "HTTP:", 5) == 0) {
3416         result = lookup_header(r, var+5);
3417     }
3418
3419     /* connection stuff */
3420     else if (strcasecmp(var, "REMOTE_ADDR") == 0) {
3421         result = r->connection->remote_ip;
3422     }
3423     else if (strcasecmp(var, "REMOTE_HOST") == 0) {
3424         result = (char *)ap_get_remote_host(r->connection,
3425                                          r->per_dir_config, REMOTE_NAME);
3426     }
3427     else if (strcasecmp(var, "REMOTE_USER") == 0) {
3428         result = r->user;
3429     }
3430     else if (strcasecmp(var, "REMOTE_IDENT") == 0) {
3431         result = (char *)ap_get_remote_logname(r);
3432     }
3433
3434     /* request stuff */
3435     else if (strcasecmp(var, "THE_REQUEST") == 0) { /* non-standard */
3436         result = r->the_request;
3437     }
3438     else if (strcasecmp(var, "REQUEST_METHOD") == 0) {
3439         result = r->method;
3440     }
3441     else if (strcasecmp(var, "REQUEST_URI") == 0) { /* non-standard */
3442         result = r->uri;
3443     }
3444     else if (strcasecmp(var, "SCRIPT_FILENAME") == 0 ||
3445              strcasecmp(var, "REQUEST_FILENAME") == 0  ) {
3446         result = r->filename;
3447     }
3448     else if (strcasecmp(var, "PATH_INFO") == 0) {
3449         result = r->path_info;
3450     }
3451     else if (strcasecmp(var, "QUERY_STRING") == 0) {
3452         result = r->args;
3453     }
3454     else if (strcasecmp(var, "AUTH_TYPE") == 0) {
3455         result = r->ap_auth_type;
3456     }
3457     else if (strcasecmp(var, "IS_SUBREQ") == 0) { /* non-standard */
3458         result = (r->main != NULL ? "true" : "false");
3459     }
3460
3461     /* internal server stuff */
3462     else if (strcasecmp(var, "DOCUMENT_ROOT") == 0) {
3463         result = ap_document_root(r);
3464     }
3465     else if (strcasecmp(var, "SERVER_ADMIN") == 0) {
3466         result = r->server->server_admin;
3467     }
3468     else if (strcasecmp(var, "SERVER_NAME") == 0) {
3469         result = ap_get_server_name(r);
3470     }
3471     else if (strcasecmp(var, "SERVER_ADDR") == 0) { /* non-standard */
3472         result = r->connection->local_ip;
3473     }
3474     else if (strcasecmp(var, "SERVER_PORT") == 0) {
3475         apr_snprintf(resultbuf, sizeof(resultbuf), "%u", ap_get_server_port(r));
3476         result = resultbuf;
3477     }
3478     else if (strcasecmp(var, "SERVER_PROTOCOL") == 0) {
3479         result = r->protocol;
3480     }
3481     else if (strcasecmp(var, "SERVER_SOFTWARE") == 0) {
3482         result = ap_get_server_version();
3483     }
3484     else if (strcasecmp(var, "API_VERSION") == 0) { /* non-standard */
3485         apr_snprintf(resultbuf, sizeof(resultbuf), "%d:%d",
3486                     MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
3487         result = resultbuf;
3488     }
3489
3490 /* XXX: wow this has gotta be slow if you actually use it for a lot, recalculates exploded time for each variable */
3491     /* underlaying Unix system stuff */
3492     else if (strcasecmp(var, "TIME_YEAR") == 0) {
3493         apr_explode_localtime(&tm, apr_time_now());
3494         apr_snprintf(resultbuf, sizeof(resultbuf), "%04d", tm.tm_year + 1900);
3495         result = resultbuf;
3496     }
3497 #define MKTIMESTR(format, tmfield) \
3498     apr_explode_localtime(&tm, apr_time_now()); \
3499     apr_snprintf(resultbuf, sizeof(resultbuf), format, tm.tmfield); \
3500     result = resultbuf;
3501     else if (strcasecmp(var, "TIME_MON") == 0) {
3502         MKTIMESTR("%02d", tm_mon+1)
3503     }
3504     else if (strcasecmp(var, "TIME_DAY") == 0) {
3505         MKTIMESTR("%02d", tm_mday)
3506     }
3507     else if (strcasecmp(var, "TIME_HOUR") == 0) {
3508         MKTIMESTR("%02d", tm_hour)
3509     }
3510     else if (strcasecmp(var, "TIME_MIN") == 0) {
3511         MKTIMESTR("%02d", tm_min)
3512     }
3513     else if (strcasecmp(var, "TIME_SEC") == 0) {
3514         MKTIMESTR("%02d", tm_sec)
3515     }
3516     else if (strcasecmp(var, "TIME_WDAY") == 0) {
3517         MKTIMESTR("%d", tm_wday)
3518     }
3519     else if (strcasecmp(var, "TIME") == 0) {
3520         apr_explode_localtime(&tm, apr_time_now());
3521         apr_snprintf(resultbuf, sizeof(resultbuf),
3522                     "%04d%02d%02d%02d%02d%02d", tm.tm_year + 1900,
3523                     tm.tm_mon+1, tm.tm_mday,
3524                     tm.tm_hour, tm.tm_min, tm.tm_sec);
3525         result = resultbuf;
3526         rewritelog(r, 1, "RESULT='%s'", result);
3527     }
3528
3529     /* all other env-variables from the parent Apache process */
3530     else if (strlen(var) > 4 && strncasecmp(var, "ENV:", 4) == 0) {
3531         /* first try the internal Apache notes structure */
3532         result = apr_table_get(r->notes, var+4);
3533         /* second try the internal Apache env structure  */
3534         if (result == NULL) {
3535             result = apr_table_get(r->subprocess_env, var+4);
3536         }
3537         /* third try the external OS env */
3538         if (result == NULL) {
3539             result = getenv(var+4);
3540         }
3541     }
3542
3543 #define LOOKAHEAD(subrecfunc) \
3544         if ( \
3545           /* filename is safe to use */ \
3546           r->filename != NULL \
3547               /* - and we're either not in a subrequest */ \
3548               && ( r->main == NULL \
3549                   /* - or in a subrequest where paths are non-NULL... */ \
3550                     || ( r->main->uri != NULL && r->uri != NULL \
3551                         /*   ...and sub and main paths differ */ \
3552                         && strcmp(r->main->uri, r->uri) != 0))) { \
3553             /* process a file-based subrequest */ \
3554             rsub = subrecfunc(r->filename, r, NULL); \
3555             /* now recursively lookup the variable in the sub_req */ \
3556             result = lookup_variable(rsub, var+5); \
3557             /* copy it up to our scope before we destroy sub_req's apr_pool_t */ \
3558             result = apr_pstrdup(r->pool, result); \
3559             /* cleanup by destroying the subrequest */ \
3560             ap_destroy_sub_req(rsub); \
3561             /* log it */ \
3562             rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s", \
3563                        r->filename, var+5, result); \
3564             /* return ourself to prevent re-pstrdup */ \
3565             return (char *)result; \
3566         }
3567
3568     /* look-ahead for parameter through URI-based sub-request */
3569     else if (strlen(var) > 5 && strncasecmp(var, "LA-U:", 5) == 0) {
3570         LOOKAHEAD(ap_sub_req_lookup_uri)
3571     }
3572     /* look-ahead for parameter through file-based sub-request */
3573     else if (strlen(var) > 5 && strncasecmp(var, "LA-F:", 5) == 0) {
3574         LOOKAHEAD(ap_sub_req_lookup_file)
3575     }
3576
3577     /* file stuff */
3578     else if (strcasecmp(var, "SCRIPT_USER") == 0) {
3579         result = "<unknown>";
3580         if (r->finfo.valid & APR_FINFO_USER) {
3581             apr_get_username((char **)&result, r->finfo.user, r->pool);
3582         }
3583     }
3584     else if (strcasecmp(var, "SCRIPT_GROUP") == 0) {
3585         result = "<unknown>";
3586         if (r->finfo.valid & APR_FINFO_GROUP) {
3587             apr_get_groupname((char **)&result, r->finfo.group, r->pool);
3588         }
3589     }
3590
3591     if (result == NULL) {
3592         return apr_pstrdup(r->pool, "");
3593     }
3594     else {
3595         return apr_pstrdup(r->pool, result);
3596     }
3597 }
3598
3599 static char *lookup_header(request_rec *r, const char *name)
3600 {
3601     apr_array_header_t *hdrs_arr;
3602     apr_table_entry_t *hdrs;
3603     int i;
3604
3605     hdrs_arr = apr_table_elts(r->headers_in);
3606     hdrs = (apr_table_entry_t *)hdrs_arr->elts;
3607     for (i = 0; i < hdrs_arr->nelts; ++i) {
3608         if (hdrs[i].key == NULL) {
3609             continue;
3610         }
3611         if (strcasecmp(hdrs[i].key, name) == 0) {
3612             apr_table_merge(r->notes, VARY_KEY_THIS, name);
3613             return hdrs[i].val;
3614         }
3615     }
3616     return NULL;
3617 }
3618
3619
3620
3621
3622 /*
3623 ** +-------------------------------------------------------+
3624 ** |                                                       |
3625 ** |                    caching support
3626 ** |                                                       |
3627 ** +-------------------------------------------------------+
3628 */
3629
3630
3631 static cache *init_cache(apr_pool_t *p)
3632 {
3633     cache *c;
3634
3635     c = (cache *)apr_palloc(p, sizeof(cache));
3636     if (apr_pool_create(&c->pool, p) != APR_SUCCESS)
3637                 return NULL;
3638     c->lists = apr_array_make(c->pool, 2, sizeof(cachelist));
3639     return c;
3640 }
3641
3642 static void set_cache_string(cache *c, const char *res, int mode, time_t t,
3643                              char *key, char *value)
3644 {
3645     cacheentry ce;
3646
3647     ce.time  = t;
3648     ce.key   = key;
3649     ce.value = value;
3650     store_cache_string(c, res, &ce);
3651     return;
3652 }
3653
3654 static char *get_cache_string(cache *c, const char *res, int mode,
3655                               time_t t, char *key)
3656 {
3657     cacheentry *ce;
3658
3659     ce = retrieve_cache_string(c, res, key);
3660     if (ce == NULL) {
3661         return NULL;
3662     }
3663     if (mode & CACHEMODE_TS) {
3664         if (t != ce->time) {
3665             return NULL;
3666         }
3667     }
3668     else if (mode & CACHEMODE_TTL) {
3669         if (t > ce->time) {
3670             return NULL;
3671         }
3672     }
3673     return apr_pstrdup(c->pool, ce->value);
3674 }
3675
3676 static int cache_tlb_hash(char *key)
3677 {
3678     unsigned long n;
3679     char *p;
3680
3681     n = 0;
3682     for (p = key; *p != '\0'; p++) {
3683         n = ((n << 5) + n) ^ (unsigned long)(*p++);
3684     }
3685
3686     return n % CACHE_TLB_ROWS;
3687 }
3688
3689 static cacheentry *cache_tlb_lookup(cachetlbentry *tlb, cacheentry *elt,
3690                                     char *key)
3691 {
3692     int ix = cache_tlb_hash(key);
3693     int i;
3694     int j;
3695
3696     for (i=0; i < CACHE_TLB_COLS; ++i) {
3697         j = tlb[ix].t[i];
3698         if (j < 0)
3699             return NULL;
3700         if (strcmp(elt[j].key, key) == 0)
3701             return &elt[j];
3702     }
3703     return NULL;
3704 }
3705
3706 static void cache_tlb_replace(cachetlbentry *tlb, cacheentry *elt,
3707                               cacheentry *e)
3708 {
3709     int ix = cache_tlb_hash(e->key);
3710     int i;
3711
3712     tlb = &tlb[ix];
3713
3714     for (i=1; i < CACHE_TLB_COLS; ++i)
3715         tlb->t[i] = tlb->t[i-1];
3716
3717     tlb->t[0] = e - elt;
3718 }
3719
3720 static void store_cache_string(cache *c, const char *res, cacheentry *ce)
3721 {
3722     int i;
3723     int j;
3724     cachelist *l;
3725     cacheentry *e;
3726     cachetlbentry *t;
3727     int found_list;
3728
3729     found_list = 0;
3730     /* first try to edit an existing entry */
3731     for (i = 0; i < c->lists->nelts; i++) {
3732         l = &(((cachelist *)c->lists->elts)[i]);
3733         if (strcmp(l->resource, res) == 0) {
3734             found_list = 1;
3735
3736             e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3737                                  (cacheentry *)l->entries->elts, ce->key);
3738             if (e != NULL) {
3739                 e->time  = ce->time;
3740                 e->value = apr_pstrdup(c->pool, ce->value);
3741                 return;
3742             }
3743
3744             for (j = 0; j < l->entries->nelts; j++) {
3745                 e = &(((cacheentry *)l->entries->elts)[j]);
3746                 if (strcmp(e->key, ce->key) == 0) {
3747                     e->time  = ce->time;
3748                     e->value = apr_pstrdup(c->pool, ce->value);
3749                   cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3750                                     (cacheentry *)l->entries->elts, e);
3751                     return;
3752                 }
3753             }
3754         }
3755     }
3756
3757     /* create a needed new list */
3758     if (!found_list) {
3759         l = apr_array_push(c->lists);
3760         l->resource = apr_pstrdup(c->pool, res);
3761         l->entries  = apr_array_make(c->pool, 2, sizeof(cacheentry));
3762         l->tlb      = apr_array_make(c->pool, CACHE_TLB_ROWS,
3763                                     sizeof(cachetlbentry));
3764         for (i=0; i<CACHE_TLB_ROWS; ++i) {
3765             t = &((cachetlbentry *)l->tlb->elts)[i];
3766                 for (j=0; j<CACHE_TLB_COLS; ++j)
3767                     t->t[j] = -1;
3768         }
3769     }
3770
3771     /* create the new entry */
3772     for (i = 0; i < c->lists->nelts; i++) {
3773         l = &(((cachelist *)c->lists->elts)[i]);
3774         if (strcmp(l->resource, res) == 0) {
3775             e = apr_array_push(l->entries);
3776             e->time  = ce->time;
3777             e->key   = apr_pstrdup(c->pool, ce->key);
3778             e->value = apr_pstrdup(c->pool, ce->value);
3779             cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3780                               (cacheentry *)l->entries->elts, e);
3781             return;
3782         }
3783     }
3784
3785     /* not reached, but when it is no problem... */
3786     return;
3787 }
3788
3789 static cacheentry *retrieve_cache_string(cache *c, const char *res, char *key)
3790 {
3791     int i;
3792     int j;
3793     cachelist *l;
3794     cacheentry *e;
3795
3796     for (i = 0; i < c->lists->nelts; i++) {
3797         l = &(((cachelist *)c->lists->elts)[i]);
3798         if (strcmp(l->resource, res) == 0) {
3799
3800             e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3801                                  (cacheentry *)l->entries->elts, key);
3802             if (e != NULL)
3803                 return e;
3804
3805             for (j = 0; j < l->entries->nelts; j++) {
3806                 e = &(((cacheentry *)l->entries->elts)[j]);
3807                 if (strcmp(e->key, key) == 0) {
3808                     return e;
3809                 }
3810             }
3811         }
3812     }
3813     return NULL;
3814 }
3815
3816
3817
3818
3819 /*
3820 ** +-------------------------------------------------------+
3821 ** |                                                       |
3822 ** |                    misc functions
3823 ** |                                                       |
3824 ** +-------------------------------------------------------+
3825 */
3826
3827 static char *subst_prefix_path(request_rec *r, char *input, char *match,
3828                                const char *subst)
3829 {
3830     char matchbuf[LONG_STRING_LEN];
3831     char substbuf[LONG_STRING_LEN];
3832     char *output;
3833     int l;
3834
3835     output = input;
3836
3837     /* first create a match string which always has a trailing slash */
3838     l = apr_cpystrn(matchbuf, match, sizeof(matchbuf)) - matchbuf;
3839     if (matchbuf[l-1] != '/') {
3840        matchbuf[l] = '/';
3841        matchbuf[l+1] = '\0';
3842        l++;
3843     }
3844     /* now compare the prefix */
3845     if (strncmp(input, matchbuf, l) == 0) {
3846         rewritelog(r, 5, "strip matching prefix: %s -> %s", output, output+l);
3847         output = apr_pstrdup(r->pool, output+l);
3848
3849         /* and now add the base-URL as replacement prefix */
3850         l = apr_cpystrn(substbuf, subst, sizeof(substbuf)) - substbuf;
3851         if (substbuf[l-1] != '/') {
3852            substbuf[l] = '/';
3853            substbuf[l+1] = '\0';
3854            l++;
3855         }
3856         if (output[0] == '/') {
3857             rewritelog(r, 4, "add subst prefix: %s -> %s%s",
3858                        output, substbuf, output+1);
3859             output = apr_pstrcat(r->pool, substbuf, output+1, NULL);
3860         }
3861         else {
3862             rewritelog(r, 4, "add subst prefix: %s -> %s%s",
3863                        output, substbuf, output);
3864             output = apr_pstrcat(r->pool, substbuf, output, NULL);
3865         }
3866     }
3867     return output;
3868 }
3869
3870
3871 /*
3872 **
3873 **  own command line parser which don't have the '\\' problem
3874 **
3875 */
3876
3877 static int parseargline(char *str, char **a1, char **a2, char **a3)
3878 {
3879     char *cp;
3880     int isquoted;
3881
3882 #define SKIP_WHITESPACE(cp) \
3883     for ( ; *cp == ' ' || *cp == '\t'; ) { \
3884         cp++; \
3885     };
3886
3887 #define CHECK_QUOTATION(cp,isquoted) \
3888     isquoted = 0; \
3889     if (*cp == '"') { \
3890         isquoted = 1; \
3891         cp++; \
3892     }
3893
3894 #define DETERMINE_NEXTSTRING(cp,isquoted) \
3895     for ( ; *cp != '\0'; cp++) { \
3896         if (   (isquoted    && (*cp     == ' ' || *cp     == '\t')) \
3897             || (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t'))) { \
3898             cp++; \
3899             continue; \
3900         } \
3901         if (   (!isquoted && (*cp == ' ' || *cp == '\t')) \
3902             || (isquoted  && *cp == '"')                  ) { \
3903             break; \
3904         } \
3905     }
3906
3907     cp = str;
3908     SKIP_WHITESPACE(cp);
3909
3910     /*  determine first argument */
3911     CHECK_QUOTATION(cp, isquoted);
3912     *a1 = cp;
3913     DETERMINE_NEXTSTRING(cp, isquoted);
3914     if (*cp == '\0') {
3915         return 1;
3916     }
3917     *cp++ = '\0';
3918
3919     SKIP_WHITESPACE(cp);
3920
3921     /*  determine second argument */
3922     CHECK_QUOTATION(cp, isquoted);
3923     *a2 = cp;
3924     DETERMINE_NEXTSTRING(cp, isquoted);
3925     if (*cp == '\0') {
3926         *cp++ = '\0';
3927         *a3 = NULL;
3928         return 0;
3929     }
3930     *cp++ = '\0';
3931
3932     SKIP_WHITESPACE(cp);
3933
3934     /* again check if there are only two arguments */
3935     if (*cp == '\0') {
3936         *cp++ = '\0';
3937         *a3 = NULL;
3938         return 0;
3939     }
3940
3941     /*  determine second argument */
3942     CHECK_QUOTATION(cp, isquoted);
3943     *a3 = cp;
3944     DETERMINE_NEXTSTRING(cp, isquoted);
3945     *cp++ = '\0';
3946
3947     return 0;
3948 }
3949
3950
3951 static void add_env_variable(request_rec *r, char *s)
3952 {
3953     char var[MAX_STRING_LEN];
3954     char val[MAX_STRING_LEN];
3955     char *cp;
3956     int n;
3957
3958     if ((cp = strchr(s, ':')) != NULL) {
3959         n = ((cp-s) > MAX_STRING_LEN-1 ? MAX_STRING_LEN-1 : (cp-s));
3960         memcpy(var, s, n);
3961         var[n] = '\0';
3962         apr_cpystrn(val, cp+1, sizeof(val));
3963         apr_table_set(r->subprocess_env, var, val);
3964         rewritelog(r, 5, "setting env variable '%s' to '%s'", var, val);
3965     }
3966 }
3967
3968
3969 /*
3970 **
3971 **  check that a subrequest won't cause infinite recursion
3972 **
3973 */
3974
3975 static int subreq_ok(request_rec *r)
3976 {
3977     /*
3978      * either not in a subrequest, or in a subrequest
3979      * and URIs aren't NULL and sub/main URIs differ
3980      */
3981     return (r->main == NULL ||
3982             (r->main->uri != NULL && r->uri != NULL &&
3983              strcmp(r->main->uri, r->uri) != 0));
3984 }
3985
3986
3987 /*
3988 **
3989 **  stat() for only the prefix of a path
3990 **
3991 */
3992
3993 static int prefix_stat(const char *path, apr_finfo_t *sb)
3994 {
3995     char curpath[LONG_STRING_LEN];
3996     char *cp;
3997
3998     apr_cpystrn(curpath, path, sizeof(curpath));
3999     if (curpath[0] != '/') {
4000         return 0;
4001     }
4002     if ((cp = strchr(curpath+1, '/')) != NULL) {
4003         *cp = '\0';
4004     }
4005     if (apr_stat(sb, curpath, APR_FINFO_MIN, NULL) == APR_SUCCESS) {
4006         return 1;
4007     }
4008     else {
4009         return 0;
4010     }
4011 }
4012
4013
4014 /*
4015 **
4016 **  Lexicographic Compare
4017 **
4018 */
4019
4020 static int compare_lexicography(char *cpNum1, char *cpNum2)
4021 {
4022     int i;
4023     int n1, n2;
4024
4025     n1 = strlen(cpNum1);
4026     n2 = strlen(cpNum2);
4027     if (n1 > n2) {
4028         return 1;
4029     }
4030     if (n1 < n2) {
4031         return -1;
4032     }
4033     for (i = 0; i < n1; i++) {
4034         if (cpNum1[i] > cpNum2[i]) {
4035             return 1;
4036         }
4037         if (cpNum1[i] < cpNum2[i]) {
4038             return -1;
4039         }
4040     }
4041     return 0;
4042 }
4043
4044 /*
4045 **
4046 **  Bracketed expression handling
4047 **  s points after the opening bracket
4048 **
4049 */
4050
4051 static char *find_closing_bracket(char *s, int left, int right)
4052 {
4053     int depth;
4054
4055     for (depth = 1; *s; ++s) {
4056         if (*s == right && --depth == 0) {
4057             return s;
4058         }
4059         else if (*s == left) {
4060             ++depth;
4061         }
4062     }
4063     return NULL;
4064 }
4065
4066 static char *find_char_in_brackets(char *s, int c, int left, int right)
4067 {
4068     int depth;
4069
4070     for (depth = 1; *s; ++s) {
4071         if (*s == c && depth == 1) {
4072             return s;
4073         }
4074         else if (*s == right && --depth == 0) {
4075             return NULL;
4076         }
4077         else if (*s == left) {
4078             ++depth;
4079         }
4080     }
4081     return NULL;
4082 }
4083
4084 /*
4085 **
4086 ** Module paraphernalia
4087 **
4088 */
4089
4090 #ifdef NETWARE
4091 int main(int argc, char *argv[]) 
4092 {
4093     ExitThread(TSR_THREAD, 0);
4094 }
4095 #endif
4096
4097     /* the apr_table_t of commands we provide */
4098 static const command_rec command_table[] = {
4099     AP_INIT_FLAG(    "RewriteEngine",   cmd_rewriteengine,  NULL, OR_FILEINFO,
4100                      "On or Off to enable or disable (default) the whole "
4101                      "rewriting engine"),
4102     AP_INIT_ITERATE( "RewriteOptions",  cmd_rewriteoptions,  NULL, OR_FILEINFO,
4103                      "List of option strings to set"),
4104     AP_INIT_TAKE1(   "RewriteBase",     cmd_rewritebase,     NULL, OR_FILEINFO, 
4105                      "the base URL of the per-directory context"),
4106     AP_INIT_RAW_ARGS("RewriteCond",     cmd_rewritecond,     NULL, OR_FILEINFO,
4107                      "an input string and a to be applied regexp-pattern"),
4108     AP_INIT_RAW_ARGS("RewriteRule",     cmd_rewriterule,     NULL, OR_FILEINFO,
4109                      "an URL-applied regexp-pattern and a substitution URL"),
4110     AP_INIT_TAKE2(   "RewriteMap",      cmd_rewritemap,      NULL, RSRC_CONF,
4111                      "a mapname and a filename"),
4112     AP_INIT_TAKE1(   "RewriteLock",     cmd_rewritelock,     NULL, RSRC_CONF,
4113                      "the filename of a lockfile used for inter-process "
4114                      "synchronization"),
4115     AP_INIT_TAKE1(   "RewriteLog",      cmd_rewritelog,      NULL, RSRC_CONF,
4116                      "the filename of the rewriting logfile"),
4117     AP_INIT_TAKE1(   "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF,
4118                      "the level of the rewriting logfile verbosity "
4119                      "(0=none, 1=std, .., 9=max)"),
4120     { NULL }
4121 };
4122
4123 static void register_hooks(apr_pool_t *p)
4124 {
4125     ap_hook_handler(handler_redirect, NULL, NULL, APR_HOOK_MIDDLE);
4126     ap_hook_post_config(init_module,NULL,NULL,APR_HOOK_MIDDLE);
4127     ap_hook_child_init(init_child,NULL,NULL,APR_HOOK_MIDDLE);
4128
4129     ap_hook_fixups(hook_fixup,NULL,NULL,APR_HOOK_FIRST);
4130     ap_hook_translate_name(hook_uri2file,NULL,NULL,APR_HOOK_FIRST);
4131     ap_hook_type_checker(hook_mimetype,NULL,NULL,APR_HOOK_MIDDLE);
4132 }
4133
4134     /* the main config structure */
4135 module AP_MODULE_DECLARE_DATA rewrite_module = {
4136    STANDARD20_MODULE_STUFF,
4137    config_perdir_create,        /* create per-dir    config structures */
4138    config_perdir_merge,         /* merge  per-dir    config structures */
4139    config_server_create,        /* create per-server config structures */
4140    config_server_merge,         /* merge  per-server config structures */
4141    command_table,               /* apr_table_t of config file commands  */
4142    register_hooks               /* register hooks                      */
4143 };
4144  
4145 /*EOF*/