1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
47 * ====================================================================
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/>.
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.
60 ** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
61 ** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
62 ** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
63 ** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
66 ** URL Rewriting Module
68 ** This module uses a rule-based rewriting engine (based on a
69 ** regular-expression parser) to rewrite requested URLs on the fly.
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
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.
83 ** This module was originally written in April 1996 and
84 ** gifted exclusively to the The Apache Software Foundation in July 1997 by
86 ** Ralf S. Engelschall
87 ** rse@engelschall.com
88 ** www.engelschall.com
92 #include "apr_strings.h"
96 #define APR_WANT_STRFUNC
97 #define APR_WANT_IOVEC
100 #if APR_HAVE_UNISTD_H
103 #if APR_HAVE_SYS_TYPES_H
104 #include <sys/types.h>
107 #include "ap_config.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"
116 #if !defined(OS2) && !defined(WIN32) && !defined(BEOS)
121 ** +-------------------------------------------------------+
123 ** | static module configuration
125 ** +-------------------------------------------------------+
130 ** Our interface to the Apache server kernel:
132 ** o Runtime logic of a request is as following:
133 ** while(request or subrequest)
134 ** foreach(stage #0...#9)
135 ** foreach(module) (**)
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!
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
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
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
166 . ./helpers/find-dbm-lib
167 if [ "x$found_dbm" = "x1" ]; then
168 echo " enabling DBM support for mod_rewrite"
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"
175 * MODULE-DEFINITION-END
178 /* the module (predeclaration) */
179 module AP_MODULE_DECLARE_DATA rewrite_module;
182 static cache *cachep;
184 /* whether proxy module is available or not */
185 static int proxy_available;
187 static const char *lockname;
188 static apr_lock_t *rewrite_mapr_lock_aquire = NULL;
189 static apr_lock_t *rewrite_log_lock = NULL;
192 ** +-------------------------------------------------------+
194 ** | configuration directive handling
196 ** +-------------------------------------------------------+
201 ** per-server configuration structure handling
205 static void *config_server_create(apr_pool_t *p, server_rec *s)
207 rewrite_server_conf *a;
209 a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
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));
224 static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
226 rewrite_server_conf *a, *base, *overrides;
228 a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
229 base = (rewrite_server_conf *)basev;
230 overrides = (rewrite_server_conf *)overridesv;
232 a->state = overrides->state;
233 a->options = overrides->options;
234 a->server = overrides->server;
236 if (a->options & OPTION_INHERIT) {
238 * local directives override
239 * and anything else is inherited
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,
252 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
254 a->rewriterules = apr_array_append(p, overrides->rewriterules,
259 * local directives override
260 * and anything else gets defaults
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;
276 ** per-directory configuration structure handling
280 static void *config_perdir_create(apr_pool_t *p, char *path)
282 rewrite_perdir_conf *a;
284 a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
286 a->state = ENGINE_DISABLED;
287 a->options = OPTION_NONE;
289 a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
290 a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
296 /* make sure it has a trailing slash */
297 if (path[strlen(path)-1] == '/') {
298 a->directory = apr_pstrdup(p, path);
301 a->directory = apr_pstrcat(p, path, "/", NULL);
308 static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
310 rewrite_perdir_conf *a, *base, *overrides;
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;
317 a->state = overrides->state;
318 a->options = overrides->options;
319 a->directory = overrides->directory;
320 a->baseurl = overrides->baseurl;
322 if (a->options & OPTION_INHERIT) {
323 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
325 a->rewriterules = apr_array_append(p, overrides->rewriterules,
329 a->rewriteconds = overrides->rewriteconds;
330 a->rewriterules = overrides->rewriterules;
339 ** the configuration commands
343 static const char *cmd_rewriteengine(cmd_parms *cmd,
344 void *in_dconf, int flag)
346 rewrite_perdir_conf *dconf = in_dconf;
347 rewrite_server_conf *sconf;
350 (rewrite_server_conf *)ap_get_module_config(cmd->server->module_config,
353 if (cmd->path == NULL) { /* is server command */
354 sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
356 else /* is per-directory command */ {
357 dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
363 static const char *cmd_rewriteoptions(cmd_parms *cmd,
364 void *in_dconf, const char *option)
366 rewrite_perdir_conf *dconf = in_dconf;
367 rewrite_server_conf *sconf;
370 sconf = (rewrite_server_conf *)
371 ap_get_module_config(cmd->server->module_config, &rewrite_module);
373 if (cmd->path == NULL) { /* is server command */
374 err = cmd_rewriteoptions_setoption(cmd->pool,
375 &(sconf->options), option);
377 else { /* is per-directory command */
378 err = cmd_rewriteoptions_setoption(cmd->pool,
379 &(dconf->options), option);
385 static const char *cmd_rewriteoptions_setoption(apr_pool_t *p, int *options,
388 if (strcasecmp(name, "inherit") == 0) {
389 *options |= OPTION_INHERIT;
392 return apr_pstrcat(p, "RewriteOptions: unknown option '",
398 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1)
400 rewrite_server_conf *sconf;
402 sconf = (rewrite_server_conf *)
403 ap_get_module_config(cmd->server->module_config, &rewrite_module);
405 sconf->rewritelogfile = a1;
410 static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, const char *a1)
412 rewrite_server_conf *sconf;
414 sconf = (rewrite_server_conf *)
415 ap_get_module_config(cmd->server->module_config, &rewrite_module);
417 sconf->rewriteloglevel = atoi(a1);
422 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
425 rewrite_server_conf *sconf;
426 rewritemap_entry *newmap;
429 sconf = (rewrite_server_conf *)
430 ap_get_module_config(cmd->server->module_config, &rewrite_module);
432 newmap = apr_array_push(sconf->rewritemaps);
436 if (strncmp(a2, "txt:", 4) == 0) {
437 newmap->type = MAPTYPE_TXT;
438 newmap->datafile = a2+4;
439 newmap->checkfile = a2+4;
441 else if (strncmp(a2, "rnd:", 4) == 0) {
442 newmap->type = MAPTYPE_RND;
443 newmap->datafile = a2+4;
444 newmap->checkfile = a2+4;
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);
452 return apr_pstrdup(cmd->pool, "RewriteMap: cannot use NDBM mapfile, "
453 "because no NDBM support is compiled in");
456 else if (strncmp(a2, "prg:", 4) == 0) {
457 newmap->type = MAPTYPE_PRG;
458 newmap->datafile = a2+4;
459 newmap->checkfile = a2+4;
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;
468 else if (strcmp(a2+4, "toupper") == 0) {
469 newmap->func = rewrite_mapfunc_toupper;
471 else if (strcmp(a2+4, "escape") == 0) {
472 newmap->func = rewrite_mapfunc_escape;
474 else if (strcmp(a2+4, "unescape") == 0) {
475 newmap->func = rewrite_mapfunc_unescape;
477 else if (sconf->state == ENGINE_ENABLED) {
478 return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
483 newmap->type = MAPTYPE_TXT;
484 newmap->datafile = a2;
485 newmap->checkfile = a2;
488 newmap->fpout = NULL;
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);
501 static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1)
505 if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
513 static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
516 rewrite_perdir_conf *dconf = in_dconf;
518 if (cmd->path == NULL || dconf == NULL) {
519 return "RewriteBase: only valid in per-directory config files";
522 return "RewriteBase: empty URL not allowed";
525 return "RewriteBase: argument is not a valid URL";
533 static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
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;
548 sconf = (rewrite_server_conf *)
549 ap_get_module_config(cmd->server->module_config, &rewrite_module);
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);
555 else { /* is per-directory command */
556 newcond = apr_array_push(dconf->rewriteconds);
559 /* parse the argument line ourself */
560 if (parseargline(str, &a1, &a2, &a3)) {
561 return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
565 /* arg1: the input string */
566 newcond->input = apr_pstrdup(cmd->pool, a1);
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;
573 if ((err = cmd_rewritecond_parseflagfield(cmd->pool, newcond,
580 try to compile the regexp to test if is ok */
583 newcond->flags |= CONDFLAG_NOTMATCH;
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))
595 rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL);
598 return apr_pstrcat(cmd->pool,
599 "RewriteCond: cannot compile regular expression '",
603 newcond->pattern = apr_pstrdup(cmd->pool, cp);
604 newcond->regexp = regexp;
609 static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p,
610 rewritecond_entry *cfg,
621 if (str[0] != '[' || str[strlen(str)-1] != ']') {
622 return "RewriteCond: bad flag delimiters";
626 str[strlen(str)-1] = ','; /* for simpler parsing */
627 for ( ; *cp != '\0'; ) {
628 /* skip whitespaces */
629 for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
635 if ((cp2 = strchr(cp, ',')) != NULL) {
637 for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
640 if ((cp3 = strchr(cp1, '=')) != NULL) {
649 if ((err = cmd_rewritecond_setflag(p, cfg, key, val)) != NULL) {
661 static const char *cmd_rewritecond_setflag(apr_pool_t *p, rewritecond_entry *cfg,
662 char *key, char *val)
664 if ( strcasecmp(key, "nocase") == 0
665 || strcasecmp(key, "NC") == 0 ) {
666 cfg->flags |= CONDFLAG_NOCASE;
668 else if ( strcasecmp(key, "ornext") == 0
669 || strcasecmp(key, "OR") == 0 ) {
670 cfg->flags |= CONDFLAG_ORNEXT;
673 return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL);
678 static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
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;
693 sconf = (rewrite_server_conf *)
694 ap_get_module_config(cmd->server->module_config, &rewrite_module);
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);
700 else { /* is per-directory command */
701 newrule = apr_array_push(dconf->rewriterules);
704 /* parse the argument line ourself */
705 if (parseargline(str, &a1, &a2, &a3)) {
706 return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
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;
717 if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule,
724 * try to compile the regexp to test if is ok
728 newrule->flags |= RULEFLAG_NOTMATCH;
732 if (newrule->flags & RULEFLAG_NOCASE) {
735 if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) {
736 return apr_pstrcat(cmd->pool,
737 "RewriteRule: cannot compile regular expression '",
740 newrule->pattern = apr_pstrdup(cmd->pool, cp);
741 newrule->regexp = regexp;
743 /* arg2: the output string
744 * replace the $<N> by \<n> which is needed by the currently
745 * used Regular Expression library
747 * TODO: Is this still required for PCRE? If not, does it *work* with PCRE?
749 newrule->output = apr_pstrdup(cmd->pool, a2);
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
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));
760 else { /* is per-directory command */
761 newrule->rewriteconds = dconf->rewriteconds;
762 dconf->rewriteconds = apr_array_make(cmd->pool, 2,
763 sizeof(rewritecond_entry));
769 static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p,
770 rewriterule_entry *cfg,
781 if (str[0] != '[' || str[strlen(str)-1] != ']') {
782 return "RewriteRule: bad flag delimiters";
786 str[strlen(str)-1] = ','; /* for simpler parsing */
787 for ( ; *cp != '\0'; ) {
788 /* skip whitespaces */
789 for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
795 if ((cp2 = strchr(cp, ',')) != NULL) {
797 for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
800 if ((cp3 = strchr(cp1, '=')) != NULL) {
809 if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) {
821 static const char *cmd_rewriterule_setflag(apr_pool_t *p, rewriterule_entry *cfg,
822 char *key, char *val)
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;
834 else if (strcasecmp(val, "temp") == 0) {
835 status = HTTP_MOVED_TEMPORARILY;
837 else if (strcasecmp(val, "seeother") == 0) {
838 status = HTTP_SEE_OTHER;
840 else if (apr_isdigit(*val)) {
843 if (!ap_is_HTTP_REDIRECT(status)) {
844 return "RewriteRule: invalid HTTP response code "
847 cfg->forced_responsecode = status;
850 else if ( strcasecmp(key, "last") == 0
851 || strcasecmp(key, "L") == 0 ) {
852 cfg->flags |= RULEFLAG_LASTRULE;
854 else if ( strcasecmp(key, "next") == 0
855 || strcasecmp(key, "N") == 0 ) {
856 cfg->flags |= RULEFLAG_NEWROUND;
858 else if ( strcasecmp(key, "chain") == 0
859 || strcasecmp(key, "C") == 0 ) {
860 cfg->flags |= RULEFLAG_CHAIN;
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);
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++)
871 if (i < MAX_ENV_FLAGS) {
872 cfg->env[i] = apr_pstrdup(p, val);
873 cfg->env[i+1] = NULL;
876 return "RewriteRule: too many environment flags 'E'";
879 else if ( strcasecmp(key, "nosubreq") == 0
880 || strcasecmp(key, "NS") == 0 ) {
881 cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
883 else if ( strcasecmp(key, "proxy") == 0
884 || strcasecmp(key, "P") == 0 ) {
885 cfg->flags |= RULEFLAG_PROXY;
887 else if ( strcasecmp(key, "passthrough") == 0
888 || strcasecmp(key, "PT") == 0 ) {
889 cfg->flags |= RULEFLAG_PASSTHROUGH;
891 else if ( strcasecmp(key, "skip") == 0
892 || strcasecmp(key, "S") == 0 ) {
893 cfg->skip = atoi(val);
895 else if ( strcasecmp(key, "forbidden") == 0
896 || strcasecmp(key, "F") == 0 ) {
897 cfg->flags |= RULEFLAG_FORBIDDEN;
899 else if ( strcasecmp(key, "gone") == 0
900 || strcasecmp(key, "G") == 0 ) {
901 cfg->flags |= RULEFLAG_GONE;
903 else if ( strcasecmp(key, "qsappend") == 0
904 || strcasecmp(key, "QSA") == 0 ) {
905 cfg->flags |= RULEFLAG_QSAPPEND;
907 else if ( strcasecmp(key, "nocase") == 0
908 || strcasecmp(key, "NC") == 0 ) {
909 cfg->flags |= RULEFLAG_NOCASE;
912 return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL);
920 ** Global Module Initialization
921 ** [called from read_config() after all
922 ** config commands were already called]
926 static void init_module(apr_pool_t *p,
934 const char *userdata_key = "rewrite_init_module";
936 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
939 apr_pool_userdata_set((const void *)1, userdata_key,
940 apr_pool_cleanup_null, s->process->pool);
943 /* check if proxy module is available */
944 proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
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");
954 rewritelock_create(s, p);
955 apr_pool_cleanup_register(p, (void *)s, rewritelock_remove, apr_pool_cleanup_null);
957 /* step through the servers and
958 * - open each rewriting logfile
959 * - open the RewriteMap prg:xxx programs
961 for (; s; s = s->next) {
962 open_rewritelog(s, p);
964 run_rewritemap_programs(s, p);
971 ** Per-Child Module Initialization
972 ** [called after a child process is spawned]
976 static void init_child(apr_pool_t *p, server_rec *s)
980 if (lockname != NULL && *(lockname) != '\0')
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 "
990 /* create the lookup cache */
991 cachep = init_cache(p);
996 ** +-------------------------------------------------------+
1000 ** +-------------------------------------------------------+
1005 ** URI-to-filename hook
1007 ** [used for the rewriting engine triggered by
1008 ** the per-server 'RewriteRule' directives]
1012 static int hook_uri2file(request_rec *r)
1015 rewrite_server_conf *conf;
1017 const char *thisserver;
1019 const char *thisurl;
1030 * retrieve the config structures
1032 sconf = r->server->module_config;
1033 conf = (rewrite_server_conf *)ap_get_module_config(sconf,
1037 * only do something under runtime if the engine is really enabled,
1038 * else return immediately!
1040 if (conf->state == ENGINE_DISABLED) {
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.
1052 if (conf->server != r->server) {
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
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);
1065 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
1068 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1072 var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
1073 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1077 * create the SCRIPT_URI variable for the env
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)) {
1087 apr_snprintf(buf, sizeof(buf), ":%u", port);
1090 thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
1092 /* set the variable */
1093 var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
1095 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
1097 /* if filename was not initially set,
1098 * we start with the requested URI
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",
1107 * now apply the rules ...
1109 if (apply_rewrite_list(r, conf->rewriterules, NULL)) {
1111 if (strlen(r->filename) > 6 &&
1112 strncmp(r->filename, "proxy:", 6) == 0) {
1113 /* it should be go on as an internal proxy request */
1115 /* check if the proxy module is enabled, so
1116 * we can actually use it!
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;
1125 /* make sure the QUERY_STRING and
1126 * PATH_INFO parts get incorporated
1128 if (r->path_info != NULL) {
1129 r->filename = apr_pstrcat(r->pool, r->filename,
1130 r->path_info, NULL);
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);
1139 /* now make sure the request gets handled by the proxy handler */
1141 r->handler = "proxy-server";
1143 rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
1147 else if (is_absolute_uri(r->filename)) {
1148 /* it was finally rewritten to a remote URL */
1150 /* skip 'scheme:' */
1151 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1155 /* skip host part */
1156 for ( ; *cp != '/' && *cp != '\0'; cp++)
1159 rewritelog(r, 1, "escaping %s for redirect", r->filename);
1160 cp2 = ap_escape_uri(r->pool, cp);
1162 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
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);
1171 /* determine HTTP redirect response code */
1172 if (ap_is_HTTP_REDIRECT(r->status)) {
1174 r->status = HTTP_OK; /* make Apache kernel happy */
1177 n = HTTP_MOVED_TEMPORARILY;
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);
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;
1190 else if (strlen(r->filename) > 5 &&
1191 strncmp(r->filename, "gone:", 5) == 0) {
1192 /* This URLs is forced to be gone */
1195 else if (strlen(r->filename) > 12 &&
1196 strncmp(r->filename, "passthrough:", 12) == 0) {
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
1205 r->uri = apr_pstrdup(r->pool, r->filename+12);
1209 /* it was finally rewritten to a local path */
1211 /* expand "/~user" prefix */
1213 r->filename = expand_tildepaths(r, r->filename);
1215 rewritelog(r, 2, "local path result: %s", r->filename);
1217 /* the filename has to start with a slash! */
1218 if (r->filename[0] != '/') {
1219 return HTTP_BAD_REQUEST;
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
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 -
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!
1242 n = prefix_stat(r->filename, &finfo);
1244 if ((ccp = ap_document_root(r)) != NULL) {
1245 l = apr_cpystrn(docroot, ccp, sizeof(docroot)) - docroot;
1247 /* always NOT have a trailing slash */
1248 if (docroot[l-1] == '/') {
1249 docroot[l-1] = '\0';
1252 && !strncmp(r->filename, r->server->path,
1253 r->server->pathlen)) {
1254 r->filename = apr_pstrcat(r->pool, docroot,
1256 r->server->pathlen), NULL);
1259 r->filename = apr_pstrcat(r->pool, docroot,
1262 rewritelog(r, 2, "prefixed with document_root to %s",
1267 rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
1272 rewritelog(r, 1, "pass through %s", r->filename);
1282 ** [used to support the forced-MIME-type feature]
1286 static int hook_mimetype(request_rec *r)
1290 /* now check if we have to force a MIME-type */
1291 t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
1296 rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
1298 r->content_type = t;
1308 ** [used for the rewriting engine triggered by
1309 ** the per-directory 'RewriteRule' directives]
1313 static int hook_fixup(request_rec *r)
1315 rewrite_perdir_conf *dconf;
1324 dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
1327 /* if there is no per-dir config we return immediately */
1328 if (dconf == NULL) {
1332 /* we shouldn't do anything in subrequests */
1333 if (r->main != NULL) {
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) {
1344 * only do something under runtime if the engine is really enabled,
1345 * for this directory, else return immediately!
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: "
1353 return HTTP_FORBIDDEN;
1356 /* FollowSymLinks is given, but the user can
1357 * still turn off the rewriting engine
1359 if (dconf->state == ENGINE_DISABLED) {
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.
1369 ofilename = r->filename;
1372 * now apply the rules ...
1374 if (apply_rewrite_list(r, dconf->rewriterules, dconf->directory)) {
1376 if (strlen(r->filename) > 6 &&
1377 strncmp(r->filename, "proxy:", 6) == 0) {
1378 /* it should go on as an internal proxy request */
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!)
1385 if (r->args != NULL) {
1386 r->filename = apr_pstrcat(r->pool, r->filename,
1387 "?", r->args, NULL);
1390 /* now make sure the request gets handled by the proxy handler */
1392 r->handler = "proxy-server";
1394 rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
1395 "%s [OK]", dconf->directory, r->filename);
1398 else if (is_absolute_uri(r->filename)) {
1399 /* it was finally rewritten to a remote URL */
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
1405 if (dconf->baseurl != NULL) {
1406 /* skip 'scheme:' */
1407 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1411 if ((cp = strchr(cp, '/')) != NULL) {
1413 "[per-dir %s] trying to replace "
1414 "prefix %s with %s",
1415 dconf->directory, dconf->directory,
1417 cp2 = subst_prefix_path(r, cp, dconf->directory,
1419 if (strcmp(cp2, cp) != 0) {
1421 r->filename = apr_pstrcat(r->pool, r->filename,
1427 /* now prepare the redirect... */
1429 /* skip 'scheme:' */
1430 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1434 /* skip host part */
1435 for ( ; *cp != '/' && *cp != '\0'; cp++)
1438 rewritelog(r, 1, "[per-dir %s] escaping %s for redirect",
1439 dconf->directory, r->filename);
1440 cp2 = ap_escape_uri(r->pool, cp);
1442 r->filename = apr_pstrcat(r->pool, r->filename, cp2, NULL);
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);
1451 /* determine HTTP redirect response code */
1452 if (ap_is_HTTP_REDIRECT(r->status)) {
1454 r->status = HTTP_OK; /* make Apache kernel happy */
1457 n = HTTP_MOVED_TEMPORARILY;
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);
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;
1471 else if (strlen(r->filename) > 5 &&
1472 strncmp(r->filename, "gone:", 5) == 0) {
1473 /* This URL is forced to be gone */
1477 /* it was finally rewritten to a local path */
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
1483 if (strlen(r->filename) > 12 &&
1484 strncmp(r->filename, "passthrough:", 12) == 0) {
1485 r->filename = apr_pstrdup(r->pool, r->filename+12);
1488 /* the filename has to start with a slash! */
1489 if (r->filename[0] != '/') {
1490 return HTTP_BAD_REQUEST;
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.
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);
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
1513 if (dconf->baseurl != NULL) {
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,
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
1527 if ((ccp = ap_document_root(r)) != NULL) {
1528 prefix = apr_pstrdup(r->pool, ccp);
1529 /* always NOT have a trailing slash */
1531 if (prefix[l-1] == '/') {
1535 if (strncmp(r->filename, prefix, l) == 0) {
1537 "[per-dir %s] strip document_root "
1539 dconf->directory, r->filename,
1541 r->filename = apr_pstrdup(r->pool, r->filename+l);
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";
1555 rewritelog(r, 1, "[per-dir %s] pass through %s",
1556 dconf->directory, r->filename);
1566 ** [used for redirect support]
1570 static int handler_redirect(request_rec *r)
1572 if (strcmp(r->handler, "redirect-handler")) {
1576 /* just make sure that we are really meant! */
1577 if (strncmp(r->filename, "redirect:", 9) != 0) {
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);
1585 /* and return gracefully */
1591 ** +-------------------------------------------------------+
1593 ** | the rewriting engine
1595 ** +-------------------------------------------------------+
1599 * Apply a complete rule set,
1600 * i.e. a list of rewrite rules
1602 static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
1605 rewriterule_entry *entries;
1606 rewriterule_entry *p;
1613 * Iterate over all existing rules
1615 entries = (rewriterule_entry *)rewriterules->elts;
1618 for (i = 0; i < rewriterules->nelts; i++) {
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.
1626 if (r->main != NULL &&
1627 (p->flags & RULEFLAG_IGNOREONSUBREQ ||
1628 p->flags & RULEFLAG_PROXY ||
1629 p->flags & RULEFLAG_FORCEREDIRECT )) {
1634 * Apply the current rule.
1636 rc = apply_rewrite_rule(r, p, perdir);
1639 * Indicate a change if this was not a match-only rule.
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.
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:",
1661 * Rule has the "forbidden" flag set which means that
1662 * we stop processing and indicate this to the caller.
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:",
1673 * Rule has the "gone" flag set which means that
1674 * we stop processing and indicate this to the caller.
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);
1684 * Stop processing also on proxy pass-through and
1685 * last-rule and new-round flags.
1687 if (p->flags & RULEFLAG_PROXY) {
1690 if (p->flags & RULEFLAG_LASTRULE) {
1695 * On "new-round" flag we just start from the top of
1696 * the rewriting ruleset again.
1698 if (p->flags & RULEFLAG_NEWROUND) {
1703 * If we are forced to skip N next rules, do it now.
1707 while ( i < rewriterules->nelts
1717 * If current rule is chained with next rule(s),
1718 * skip all this next rule(s)
1720 while ( i < rewriterules->nelts
1721 && p->flags & RULEFLAG_CHAIN) {
1731 * Apply a single(!) rewrite rule
1733 static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
1739 char newuri[MAX_STRING_LEN];
1741 regmatch_t regmatch[MAX_NMATCH];
1742 backrefinfo *briRR = NULL;
1743 backrefinfo *briRC = NULL;
1746 apr_array_header_t *rewriteconds;
1747 rewritecond_entry *conds;
1748 rewritecond_entry *c;
1760 * Add (perhaps splitted away) PATH_INFO postfix to URL to
1761 * make sure we really match against the complete URL.
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);
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.
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);
1787 * Try to match the URI against the RewriteRule pattern
1788 * and exit immeddiately if it didn't apply.
1790 if (perdir == NULL) {
1791 rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
1795 rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
1796 perdir, p->pattern, uri);
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)) ) ) {
1805 * Else create the RewriteRule `regsubinfo' structure which
1806 * holds the substitution information.
1808 briRR = (backrefinfo *)apr_palloc(r->pool, sizeof(backrefinfo));
1809 if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
1810 /* empty info on negative patterns */
1815 briRR->source = apr_pstrdup(r->pool, uri);
1816 briRR->nsub = regexp->re_nsub;
1817 memcpy((void *)(briRR->regmatch), (void *)(regmatch),
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!!)
1826 briRC = (backrefinfo *)apr_pcalloc(r->pool, sizeof(backrefinfo));
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!!
1837 rewriteconds = p->rewriteconds;
1838 conds = (rewritecond_entry *)rewriteconds->elts;
1840 for (i = 0; i < rewriteconds->nelts; i++) {
1842 rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
1843 if (c->flags & CONDFLAG_ORNEXT) {
1848 /* One condition is false, but another can be
1849 * still true, so we have to continue...
1851 apr_table_unset(r->notes, VARY_KEY_THIS);
1855 /* One true condition is enough in "or" case, so
1856 * skip the other conditions which are "ornext"
1859 while ( i < rewriteconds->nelts
1860 && c->flags & CONDFLAG_ORNEXT) {
1869 * The "AND" case, i.e. no "or" flag,
1870 * so a single failure means total failure.
1877 vary = apr_table_get(r->notes, VARY_KEY_THIS);
1879 apr_table_merge(r->notes, VARY_KEY, vary);
1880 apr_table_unset(r->notes, VARY_KEY_THIS);
1883 /* if any condition fails the complete rule fails */
1885 apr_table_unset(r->notes, VARY_KEY);
1886 apr_table_unset(r->notes, VARY_KEY_THIS);
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.
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);
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=...]')
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.
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);
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...
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;
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'.
1942 do_expand(r, output, newuri, sizeof(newuri), briRR, briRC);
1943 if (perdir == NULL) {
1944 rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
1947 rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
1951 * Additionally do expansion for the environment variable
1952 * strings (`RewriteRule .. .. [E=<string>]').
1954 do_expand_env(r, p->env, briRR, briRC);
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
1961 r->filename = apr_pstrdup(r->pool, newuri);
1962 splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
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.
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);
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
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);
1991 rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
1992 perdir, r->filename);
1994 r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
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).
2004 if (p->flags & RULEFLAG_FORCEREDIRECT) {
2005 fully_qualify_uri(r);
2006 if (perdir == NULL) {
2008 "explicitly forcing redirect with %s", r->filename);
2012 "[per-dir %s] explicitly forcing redirect with %s",
2013 perdir, r->filename);
2015 r->status = p->forced_responsecode;
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
2031 * If this rule is still implicitly forced for HTTP
2032 * redirection (`RewriteRule .. <scheme>://...') then
2033 * directly force an external HTTP redirect.
2035 if (is_absolute_uri(r->filename)) {
2036 if (perdir == NULL) {
2038 "implicitly forcing redirect (rc=%d) with %s",
2039 p->forced_responsecode, r->filename);
2042 rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
2043 "(rc=%d) with %s", perdir, p->forced_responsecode,
2046 r->status = p->forced_responsecode;
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.
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);
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 ;-)
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);
2080 "[per-dir %s] remember %s to have MIME-type '%s'",
2081 perdir, r->filename, p->forced_mimetype);
2086 * Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
2087 * But now we're done for this particular rule.
2092 static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
2093 char *perdir, backrefinfo *briRR,
2096 char input[MAX_STRING_LEN];
2099 regmatch_t regmatch[MAX_NMATCH];
2103 * Construct the string we match against
2106 do_expand(r, p->input, input, sizeof(input), briRR, briRC);
2109 * Apply the patterns
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) {
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) {
2127 else if (strcmp(p->pattern, "-l") == 0) {
2129 if (apr_lstat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS) {
2130 if (sb.filetype == APR_LNK) {
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) {
2143 else if (strcmp(p->pattern, "-U") == 0) {
2144 /* avoid infinite subrequest recursion */
2145 if (strlen(input) > 0 && subreq_ok(r)) {
2147 /* run a URI-based subrequest */
2148 rsub = ap_sub_req_lookup_uri(input, r, NULL);
2150 /* URI exists for any result up to 3xx, redirects allowed */
2151 if (rsub->status < 400)
2155 rewritelog(r, 5, "RewriteCond URI (-U) check: "
2156 "path=%s -> status=%d", input, rsub->status);
2158 /* cleanup by destroying the subrequest */
2159 ap_destroy_sub_req(rsub);
2162 else if (strcmp(p->pattern, "-F") == 0) {
2163 /* avoid infinite subrequest recursion */
2164 if (strlen(input) > 0 && subreq_ok(r)) {
2166 /* process a file-based subrequest:
2167 * this differs from -U in that no path translation is done.
2169 rsub = ap_sub_req_lookup_file(input, r, NULL);
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) {
2180 rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
2181 "-> file=%s status=%d", input, rsub->filename,
2184 /* cleanup by destroying the subrequest */
2185 ap_destroy_sub_req(rsub);
2188 else if (strlen(p->pattern) > 1 && *(p->pattern) == '>') {
2189 rc = (compare_lexicography(input, p->pattern+1) == 1 ? 1 : 0);
2191 else if (strlen(p->pattern) > 1 && *(p->pattern) == '<') {
2192 rc = (compare_lexicography(input, p->pattern+1) == -1 ? 1 : 0);
2194 else if (strlen(p->pattern) > 1 && *(p->pattern) == '=') {
2195 if (strcmp(p->pattern+1, "\"\"") == 0) {
2196 rc = (*input == '\0');
2199 rc = (strcmp(input, p->pattern+1) == 0 ? 1 : 0);
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);
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),
2217 /* if this is a non-matching regexp, just negate the result */
2218 if (p->flags & CONDFLAG_NOTMATCH) {
2222 rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s' => %s",
2223 input, (p->flags & CONDFLAG_NOTMATCH ? "!" : ""),
2224 p->pattern, rc ? "matched" : "not-matched");
2226 /* end just return the result */
2232 ** +-------------------------------------------------------+
2234 ** | URL transformation functions
2236 ** +-------------------------------------------------------+
2242 ** perform all the expansions on the input string
2243 ** leaving the result in the supplied buffer
2247 static void do_expand(request_rec *r, char *input, char *buffer, int nbuf,
2248 backrefinfo *briRR, backrefinfo *briRC)
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.
2263 space = nbuf - 1; /* room for '\0' */
2266 span = strcspn(inp, "$%");
2270 memcpy(outp, inp, span);
2274 if (space == 0 || *inp == '\0') {
2277 /* now we have a '$' or a '%' */
2278 if (inp[1] == '{') {
2280 endp = find_closing_bracket(inp+2, '{', '}');
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
2291 if (inp[0] == '$') {
2292 /* ${...} map lookup expansion */
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.
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, ':', '{', '}');
2311 map = apr_pstrndup(r->pool, inp+2, key-inp-2);
2312 dflt = find_char_in_brackets(key+1, '|', '{', '}');
2314 key = apr_pstrndup(r->pool, key+1, endp-key-1);
2317 key = apr_pstrndup(r->pool, key+1, dflt-key-1);
2318 dflt = apr_pstrndup(r->pool, dflt+1, endp-dflt-1);
2320 do_expand(r, key, xkey, sizeof(xkey), briRR, briRC);
2321 result = lookup_map(r, map, xkey);
2323 span = apr_cpystrn(outp, result, space) - outp;
2325 do_expand(r, dflt, xdflt, sizeof(xdflt), briRR, briRC);
2326 span = apr_cpystrn(outp, xdflt, space) - outp;
2329 else if (inp[0] == '%') {
2330 /* %{...} variable lookup expansion */
2332 var = apr_pstrndup(r->pool, inp+2, endp-inp-2);
2333 span = apr_cpystrn(outp, lookup_variable(r, var), space) - outp;
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 */
2350 else if (inp[0] == '%') {
2351 /* %N RewriteCond regexp backref expansion */
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;
2361 memcpy(outp, bri->source + bri->regmatch[n].rm_so, span);
2378 ** perform all the expansions on the environment variables
2382 static void do_expand_env(request_rec *r, char *env[],
2383 backrefinfo *briRR, backrefinfo *briRC)
2386 char buf[MAX_STRING_LEN];
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);
2397 ** split out a QUERY_STRING part from
2398 ** the current URI string
2402 static void splitout_queryargs(request_rec *r, int qsappend)
2407 q = strchr(r->filename, '?');
2409 olduri = apr_pstrdup(r->pool, r->filename);
2412 r->args = apr_pstrcat(r->pool, q, "&", r->args, NULL);
2415 r->args = apr_pstrdup(r->pool, q);
2417 if (strlen(r->args) == 0) {
2419 rewritelog(r, 3, "split uri=%s -> uri=%s, args=<none>", olduri,
2423 if (r->args[strlen(r->args)-1] == '&') {
2424 r->args[strlen(r->args)-1] = '\0';
2426 rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
2427 r->filename, r->args);
2436 ** strip 'http[s]://ourhost/' from URI
2440 static void reduce_uri(request_rec *r)
2443 unsigned short port;
2448 char host[LONG_STRING_LEN];
2449 char buf[MAX_STRING_LEN];
2453 cp = (char *)ap_http_method(r);
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 */
2462 olduri = apr_pstrdup(r->pool, r->filename); /* save for logging */
2464 /* cut the hostname and port out of the URI */
2465 apr_cpystrn(buf, r->filename+(l+3), sizeof(buf));
2467 for (cp = hostp; *cp != '\0' && *cp != '/' && *cp != ':'; cp++)
2472 apr_cpystrn(host, hostp, sizeof(host));
2475 for (; *cp != '\0' && *cp != '/'; cp++)
2481 /* set remaining url */
2484 else if (*cp == '/') {
2487 apr_cpystrn(host, hostp, sizeof(host));
2490 port = ap_default_port(r);
2491 /* set remaining url */
2496 apr_cpystrn(host, hostp, sizeof(host));
2498 port = ap_default_port(r);
2499 /* set remaining url */
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);
2516 ** add 'http[s]://ourhost[:ourport]/' to URI
2517 ** if URI is still not fully qualified
2521 static void fully_qualify_uri(request_rec *r)
2524 const char *thisserver;
2528 if (!is_absolute_uri(r->filename)) {
2530 thisserver = ap_get_server_name(r);
2531 port = ap_get_server_port(r);
2532 if (ap_is_default_port(port,r)) {
2536 apr_snprintf(buf, sizeof(buf), ":%u", port);
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);
2546 r->filename = apr_psprintf(r->pool, "%s://%s%s/%s",
2547 ap_http_method(r), thisserver,
2548 thisport, r->filename);
2557 ** return non-zero if the URI is absolute (includes a scheme etc.)
2561 static int is_absolute_uri(char *uri)
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) ) {
2581 ** Expand tilde-paths (/~user) through Unix /etc/passwd
2582 ** database information (or other OS-specific database)
2586 static char *expand_tildepaths(request_rec *r, char *uri)
2588 char user[LONG_STRING_LEN];
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
2598 && uri[i] != '/' ; ) {
2599 user[j++] = uri[i++];
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';
2611 newuri = apr_pstrcat(r->pool, homedir, uri+i, NULL);
2614 /* only ~user has to be expanded */
2621 #endif /* if APR_HAS_USER */
2626 ** +-------------------------------------------------------+
2628 ** | DBM hashfile support
2630 ** +-------------------------------------------------------+
2634 static char *lookup_map(request_rec *r, char *name, char *key)
2637 rewrite_server_conf *conf;
2638 apr_array_header_t *rewritemaps;
2639 rewritemap_entry *entries;
2640 rewritemap_entry *s;
2646 /* get map configuration */
2647 sconf = r->server->module_config;
2648 conf = (rewrite_server_conf *)ap_get_module_config(sconf,
2650 rewritemaps = conf->rewritemaps;
2652 entries = (rewritemap_entry *)rewritemaps->elts;
2653 for (i = 0; i < rewritemaps->nelts; 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, "
2666 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2668 if (value == NULL) {
2669 rewritelog(r, 6, "cache lookup FAILED, forcing new "
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);
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,
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;
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, "
2704 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2706 if (value == NULL) {
2708 "cache lookup FAILED, forcing new map lookup");
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);
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,
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;
2734 else if (s->type == MAPTYPE_PRG) {
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);
2742 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
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);
2753 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
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, "
2767 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2769 if (value == NULL) {
2770 rewritelog(r, 6, "cache lookup FAILED, forcing new "
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);
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,
2788 rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2789 "-> val=%s", s->name, key, value);
2791 if (value[0] != '\0') {
2792 value = select_random_value_part(r, value);
2793 rewritelog(r, 5, "randomly choosen the subvalue `%s'", value);
2805 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
2807 apr_file_t *fp = NULL;
2816 rc = apr_file_open(&fp, file, APR_READ, APR_OS_DEFAULT, r->pool);
2817 if (rc != APR_SUCCESS) {
2821 while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
2823 continue; /* ignore comments */
2826 skip = strcspn(cpT," \t\r\n");
2828 continue; /* ignore lines that start with a space, tab, CR, or LF */
2831 if (strcmp(curkey, key) != 0)
2832 continue; /* key does not match... */
2834 /* found a matching key; now extract and return the value */
2836 skip = strspn(cpT, " \t\r\n");
2839 skip = strcspn(cpT, " \t\r\n");
2841 continue; /* no value... */
2844 value = apr_pstrdup(r->pool, curval);
2851 #ifndef NO_DBM_REWRITEMAP
2852 static char *lookup_map_dbmfile(request_rec *r, const char *file, char *key)
2858 char buf[MAX_STRING_LEN];
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);
2877 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
2878 apr_file_t *fpout, char *key)
2880 char buf[LONG_STRING_LEN];
2886 struct iovec iova[2];
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.
2895 if (fpin == NULL || fpout == NULL) {
2901 if (rewrite_mapr_lock_aquire) {
2902 apr_lock_aquire(rewrite_mapr_lock_aquire);
2905 /* write out the request key */
2907 nbytes = strlen(key);
2908 apr_file_write(fpin, key, &nbytes);
2910 apr_file_write(fpin, "\n", &nbytes);
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;
2918 apr_file_writev(fpin, iova, niov, &nbytes);
2921 /* read in the response value */
2924 apr_file_read(fpout, &c, &nbytes);
2925 while (nbytes == 1 && (i < LONG_STRING_LEN-1)) {
2931 apr_file_read(fpout, &c, &nbytes);
2935 /* give the lock back */
2936 if (rewrite_mapr_lock_aquire) {
2937 apr_lock_release(rewrite_mapr_lock_aquire);
2940 if (strcasecmp(buf, "NULL") == 0) {
2944 return apr_pstrdup(r->pool, buf);
2948 static char *lookup_map_internal(request_rec *r,
2949 char *(*func)(request_rec *, char *),
2952 /* currently we just let the function convert
2953 the key to a corresponding value */
2954 return func(r, key);
2957 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
2961 for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2963 *cp = apr_toupper(*cp);
2968 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
2972 for (cp = value = apr_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2974 *cp = apr_tolower(*cp);
2979 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
2983 value = ap_escape_uri(r->pool, key);
2987 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
2991 value = apr_pstrdup(r->pool, key);
2992 ap_unescape_url(value);
2996 static int rewrite_rand_init_done = 0;
2998 static void rewrite_rand_init(void)
3000 if (!rewrite_rand_init_done) {
3001 srand((unsigned)(getpid()));
3002 rewrite_rand_init_done = 1;
3007 static int rewrite_rand(int l, int h)
3009 rewrite_rand_init();
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.
3016 return ((double)(rand() % RAND_MAX) / RAND_MAX) * (h - l + 1) + l;
3019 static char *select_random_value_part(request_rec *r, char *value)
3024 /* count number of distinct values */
3025 for (n = 1, i = 0; value[i] != '\0'; i++) {
3026 if (value[i] == '|') {
3031 /* when only one value we have no option to choose */
3036 /* else randomly select one */
3037 k = rewrite_rand(1, n);
3039 /* and grep it out */
3040 for (n = 1, i = 0; value[i] != '\0'; i++) {
3044 if (value[i] == '|') {
3048 buf = apr_pstrdup(r->pool, &value[i]);
3049 for (i = 0; buf[i] != '\0' && buf[i] != '|'; i++)
3057 ** +-------------------------------------------------------+
3059 ** | rewriting logfile support
3061 ** +-------------------------------------------------------+
3065 static void open_rewritelog(server_rec *s, apr_pool_t *p)
3067 rewrite_server_conf *conf;
3071 int rewritelog_flags = ( APR_WRITE | APR_APPEND | APR_CREATE );
3072 mode_t rewritelog_mode = ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD );
3074 conf = ap_get_module_config(s->module_config, &rewrite_module);
3076 if (conf->rewritelogfile == NULL) {
3079 if (*(conf->rewritelogfile) == '\0') {
3082 if (conf->rewritelogfp != NULL) {
3083 return; /* virtual log shared w/ main server */
3086 fname = ap_server_root_relative(p, conf->rewritelogfile);
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);
3095 conf->rewritelogfp = ap_piped_log_write_fd(pl);
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 "
3109 static void rewritelog(request_rec *r, int level, const char *text, ...)
3111 rewrite_server_conf *conf;
3126 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
3127 conn = r->connection;
3129 if (conf->rewritelogfp == NULL) {
3132 if (conf->rewritelogfile == NULL) {
3135 if (*(conf->rewritelogfile) == '\0') {
3139 if (level > conf->rewriteloglevel) {
3143 if (r->user == NULL) {
3146 else if (strlen(r->user) != 0) {
3153 rhost = ap_get_remote_host(conn, r->server->module_config,
3155 if (rhost == NULL) {
3156 rhost = "UNKNOWN-HOST";
3159 str1 = apr_pstrcat(r->pool, rhost, " ",
3160 (conn->remote_logname != NULL ?
3161 conn->remote_logname : "-"), " ",
3163 apr_vsnprintf(str2, sizeof(str2), text, ap);
3165 if (r->main == NULL) {
3166 strcpy(type, "initial");
3169 strcpy(type, "subreq");
3172 for (i = 0, req = r; req->prev != NULL; req = req->prev) {
3179 apr_snprintf(redir, sizeof(redir), "/redir#%d", i);
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);
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);
3197 static char *current_logtime(request_rec *r)
3199 apr_exploded_time_t t;
3203 apr_explode_localtime(&t, apr_time_now());
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);
3216 ** +-------------------------------------------------------+
3218 ** | rewriting lockfile support
3220 ** +-------------------------------------------------------+
3223 #define REWRITELOCK_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
3225 static void rewritelock_create(server_rec *s, apr_pool_t *p)
3229 /* only operate if a lockfile is used */
3230 if (lockname == NULL || *(lockname) == '\0') {
3234 /* fixup the path, especially for rewritelock_remove() */
3235 lockname = ap_server_root_relative(p, lockname);
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);
3249 static apr_status_t rewritelock_remove(void *data)
3251 /* only operate if a lockfile is used */
3252 if (lockname == NULL || *(lockname) == '\0') {
3256 /* destroy the rewritelock */
3257 apr_lock_destroy (rewrite_mapr_lock_aquire);
3258 rewrite_mapr_lock_aquire = NULL;
3265 ** +-------------------------------------------------------+
3267 ** | program map support
3269 ** +-------------------------------------------------------+
3272 static void run_rewritemap_programs(server_rec *s, apr_pool_t *p)
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;
3284 conf = ap_get_module_config(s->module_config, &rewrite_module);
3286 /* If the engine isn't turned on,
3287 * don't even try to do anything.
3289 if (conf->state == ENGINE_DISABLED) {
3293 rewritemaps = conf->rewritemaps;
3294 entries = (rewritemap_entry *)rewritemaps->elts;
3295 for (i = 0; i < rewritemaps->nelts; i++) {
3297 if (map->type != MAPTYPE_PRG) {
3300 if (map->datafile == NULL
3301 || *(map->datafile) == '\0'
3302 || map->fpin != NULL
3303 || map->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");
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,
3329 apr_procattr_t *procattr;
3330 apr_proc_t *procnew;
3333 apr_signal(SIGHUP, SIG_IGN);
3337 if (((rc = apr_procattr_create(&procattr, p)) != APR_SUCCESS) ||
3338 ((rc = apr_procattr_io_set(procattr, APR_FULL_BLOCK,
3340 APR_FULL_NONBLOCK)) != APR_SUCCESS) ||
3341 ((rc = apr_procattr_dir_set(procattr,
3342 ap_make_dirstr_parent(p, progname)))
3344 ((rc = apr_procattr_cmdtype_set(procattr, APR_PROGRAM)) != APR_SUCCESS)) {
3345 /* Something bad happened, give up and go away. */
3348 procnew = apr_pcalloc(p, sizeof(*procnew));
3349 rc = apr_proc_create(procnew, progname, NULL, NULL, procattr, p);
3351 if (rc == APR_SUCCESS) {
3352 apr_pool_note_subprocess(p, procnew, kill_after_timeout);
3355 (*fpin) = procnew->in;
3359 (*fpout) = procnew->out;
3363 (*fperr) = procnew->err;
3375 ** +-------------------------------------------------------+
3377 ** | environment variable support
3379 ** +-------------------------------------------------------+
3383 static char *lookup_variable(request_rec *r, char *var)
3386 char resultbuf[LONG_STRING_LEN];
3387 apr_exploded_time_t tm;
3393 if (strcasecmp(var, "HTTP_USER_AGENT") == 0) {
3394 result = lookup_header(r, "User-Agent");
3396 else if (strcasecmp(var, "HTTP_REFERER") == 0) {
3397 result = lookup_header(r, "Referer");
3399 else if (strcasecmp(var, "HTTP_COOKIE") == 0) {
3400 result = lookup_header(r, "Cookie");
3402 else if (strcasecmp(var, "HTTP_FORWARDED") == 0) {
3403 result = lookup_header(r, "Forwarded");
3405 else if (strcasecmp(var, "HTTP_HOST") == 0) {
3406 result = lookup_header(r, "Host");
3408 else if (strcasecmp(var, "HTTP_PROXY_CONNECTION") == 0) {
3409 result = lookup_header(r, "Proxy-Connection");
3411 else if (strcasecmp(var, "HTTP_ACCEPT") == 0) {
3412 result = lookup_header(r, "Accept");
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);
3419 /* connection stuff */
3420 else if (strcasecmp(var, "REMOTE_ADDR") == 0) {
3421 result = r->connection->remote_ip;
3423 else if (strcasecmp(var, "REMOTE_HOST") == 0) {
3424 result = (char *)ap_get_remote_host(r->connection,
3425 r->per_dir_config, REMOTE_NAME);
3427 else if (strcasecmp(var, "REMOTE_USER") == 0) {
3430 else if (strcasecmp(var, "REMOTE_IDENT") == 0) {
3431 result = (char *)ap_get_remote_logname(r);
3435 else if (strcasecmp(var, "THE_REQUEST") == 0) { /* non-standard */
3436 result = r->the_request;
3438 else if (strcasecmp(var, "REQUEST_METHOD") == 0) {
3441 else if (strcasecmp(var, "REQUEST_URI") == 0) { /* non-standard */
3444 else if (strcasecmp(var, "SCRIPT_FILENAME") == 0 ||
3445 strcasecmp(var, "REQUEST_FILENAME") == 0 ) {
3446 result = r->filename;
3448 else if (strcasecmp(var, "PATH_INFO") == 0) {
3449 result = r->path_info;
3451 else if (strcasecmp(var, "QUERY_STRING") == 0) {
3454 else if (strcasecmp(var, "AUTH_TYPE") == 0) {
3455 result = r->ap_auth_type;
3457 else if (strcasecmp(var, "IS_SUBREQ") == 0) { /* non-standard */
3458 result = (r->main != NULL ? "true" : "false");
3461 /* internal server stuff */
3462 else if (strcasecmp(var, "DOCUMENT_ROOT") == 0) {
3463 result = ap_document_root(r);
3465 else if (strcasecmp(var, "SERVER_ADMIN") == 0) {
3466 result = r->server->server_admin;
3468 else if (strcasecmp(var, "SERVER_NAME") == 0) {
3469 result = ap_get_server_name(r);
3471 else if (strcasecmp(var, "SERVER_ADDR") == 0) { /* non-standard */
3472 result = r->connection->local_ip;
3474 else if (strcasecmp(var, "SERVER_PORT") == 0) {
3475 apr_snprintf(resultbuf, sizeof(resultbuf), "%u", ap_get_server_port(r));
3478 else if (strcasecmp(var, "SERVER_PROTOCOL") == 0) {
3479 result = r->protocol;
3481 else if (strcasecmp(var, "SERVER_SOFTWARE") == 0) {
3482 result = ap_get_server_version();
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);
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);
3497 #define MKTIMESTR(format, tmfield) \
3498 apr_explode_localtime(&tm, apr_time_now()); \
3499 apr_snprintf(resultbuf, sizeof(resultbuf), format, tm.tmfield); \
3501 else if (strcasecmp(var, "TIME_MON") == 0) {
3502 MKTIMESTR("%02d", tm_mon+1)
3504 else if (strcasecmp(var, "TIME_DAY") == 0) {
3505 MKTIMESTR("%02d", tm_mday)
3507 else if (strcasecmp(var, "TIME_HOUR") == 0) {
3508 MKTIMESTR("%02d", tm_hour)
3510 else if (strcasecmp(var, "TIME_MIN") == 0) {
3511 MKTIMESTR("%02d", tm_min)
3513 else if (strcasecmp(var, "TIME_SEC") == 0) {
3514 MKTIMESTR("%02d", tm_sec)
3516 else if (strcasecmp(var, "TIME_WDAY") == 0) {
3517 MKTIMESTR("%d", tm_wday)
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);
3526 rewritelog(r, 1, "RESULT='%s'", result);
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);
3537 /* third try the external OS env */
3538 if (result == NULL) {
3539 result = getenv(var+4);
3543 #define LOOKAHEAD(subrecfunc) \
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); \
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; \
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)
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)
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);
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);
3591 if (result == NULL) {
3592 return apr_pstrdup(r->pool, "");
3595 return apr_pstrdup(r->pool, result);
3599 static char *lookup_header(request_rec *r, const char *name)
3601 apr_array_header_t *hdrs_arr;
3602 apr_table_entry_t *hdrs;
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) {
3611 if (strcasecmp(hdrs[i].key, name) == 0) {
3612 apr_table_merge(r->notes, VARY_KEY_THIS, name);
3623 ** +-------------------------------------------------------+
3625 ** | caching support
3627 ** +-------------------------------------------------------+
3631 static cache *init_cache(apr_pool_t *p)
3635 c = (cache *)apr_palloc(p, sizeof(cache));
3636 if (apr_pool_create(&c->pool, p) != APR_SUCCESS)
3638 c->lists = apr_array_make(c->pool, 2, sizeof(cachelist));
3642 static void set_cache_string(cache *c, const char *res, int mode, time_t t,
3643 char *key, char *value)
3650 store_cache_string(c, res, &ce);
3654 static char *get_cache_string(cache *c, const char *res, int mode,
3655 time_t t, char *key)
3659 ce = retrieve_cache_string(c, res, key);
3663 if (mode & CACHEMODE_TS) {
3664 if (t != ce->time) {
3668 else if (mode & CACHEMODE_TTL) {
3673 return apr_pstrdup(c->pool, ce->value);
3676 static int cache_tlb_hash(char *key)
3682 for (p = key; *p != '\0'; p++) {
3683 n = ((n << 5) + n) ^ (unsigned long)(*p++);
3686 return n % CACHE_TLB_ROWS;
3689 static cacheentry *cache_tlb_lookup(cachetlbentry *tlb, cacheentry *elt,
3692 int ix = cache_tlb_hash(key);
3696 for (i=0; i < CACHE_TLB_COLS; ++i) {
3700 if (strcmp(elt[j].key, key) == 0)
3706 static void cache_tlb_replace(cachetlbentry *tlb, cacheentry *elt,
3709 int ix = cache_tlb_hash(e->key);
3714 for (i=1; i < CACHE_TLB_COLS; ++i)
3715 tlb->t[i] = tlb->t[i-1];
3717 tlb->t[0] = e - elt;
3720 static void store_cache_string(cache *c, const char *res, cacheentry *ce)
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) {
3736 e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3737 (cacheentry *)l->entries->elts, ce->key);
3740 e->value = apr_pstrdup(c->pool, ce->value);
3744 for (j = 0; j < l->entries->nelts; j++) {
3745 e = &(((cacheentry *)l->entries->elts)[j]);
3746 if (strcmp(e->key, ce->key) == 0) {
3748 e->value = apr_pstrdup(c->pool, ce->value);
3749 cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3750 (cacheentry *)l->entries->elts, e);
3757 /* create a needed new 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)
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);
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);
3785 /* not reached, but when it is no problem... */
3789 static cacheentry *retrieve_cache_string(cache *c, const char *res, char *key)
3796 for (i = 0; i < c->lists->nelts; i++) {
3797 l = &(((cachelist *)c->lists->elts)[i]);
3798 if (strcmp(l->resource, res) == 0) {
3800 e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3801 (cacheentry *)l->entries->elts, key);
3805 for (j = 0; j < l->entries->nelts; j++) {
3806 e = &(((cacheentry *)l->entries->elts)[j]);
3807 if (strcmp(e->key, key) == 0) {
3820 ** +-------------------------------------------------------+
3824 ** +-------------------------------------------------------+
3827 static char *subst_prefix_path(request_rec *r, char *input, char *match,
3830 char matchbuf[LONG_STRING_LEN];
3831 char substbuf[LONG_STRING_LEN];
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] != '/') {
3841 matchbuf[l+1] = '\0';
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);
3849 /* and now add the base-URL as replacement prefix */
3850 l = apr_cpystrn(substbuf, subst, sizeof(substbuf)) - substbuf;
3851 if (substbuf[l-1] != '/') {
3853 substbuf[l+1] = '\0';
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);
3862 rewritelog(r, 4, "add subst prefix: %s -> %s%s",
3863 output, substbuf, output);
3864 output = apr_pstrcat(r->pool, substbuf, output, NULL);
3873 ** own command line parser which don't have the '\\' problem
3877 static int parseargline(char *str, char **a1, char **a2, char **a3)
3882 #define SKIP_WHITESPACE(cp) \
3883 for ( ; *cp == ' ' || *cp == '\t'; ) { \
3887 #define CHECK_QUOTATION(cp,isquoted) \
3894 #define DETERMINE_NEXTSTRING(cp,isquoted) \
3895 for ( ; *cp != '\0'; cp++) { \
3896 if ( (isquoted && (*cp == ' ' || *cp == '\t')) \
3897 || (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t'))) { \
3901 if ( (!isquoted && (*cp == ' ' || *cp == '\t')) \
3902 || (isquoted && *cp == '"') ) { \
3908 SKIP_WHITESPACE(cp);
3910 /* determine first argument */
3911 CHECK_QUOTATION(cp, isquoted);
3913 DETERMINE_NEXTSTRING(cp, isquoted);
3919 SKIP_WHITESPACE(cp);
3921 /* determine second argument */
3922 CHECK_QUOTATION(cp, isquoted);
3924 DETERMINE_NEXTSTRING(cp, isquoted);
3932 SKIP_WHITESPACE(cp);
3934 /* again check if there are only two arguments */
3941 /* determine second argument */
3942 CHECK_QUOTATION(cp, isquoted);
3944 DETERMINE_NEXTSTRING(cp, isquoted);
3951 static void add_env_variable(request_rec *r, char *s)
3953 char var[MAX_STRING_LEN];
3954 char val[MAX_STRING_LEN];
3958 if ((cp = strchr(s, ':')) != NULL) {
3959 n = ((cp-s) > MAX_STRING_LEN-1 ? MAX_STRING_LEN-1 : (cp-s));
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);
3971 ** check that a subrequest won't cause infinite recursion
3975 static int subreq_ok(request_rec *r)
3978 * either not in a subrequest, or in a subrequest
3979 * and URIs aren't NULL and sub/main URIs differ
3981 return (r->main == NULL ||
3982 (r->main->uri != NULL && r->uri != NULL &&
3983 strcmp(r->main->uri, r->uri) != 0));
3989 ** stat() for only the prefix of a path
3993 static int prefix_stat(const char *path, apr_finfo_t *sb)
3995 char curpath[LONG_STRING_LEN];
3998 apr_cpystrn(curpath, path, sizeof(curpath));
3999 if (curpath[0] != '/') {
4002 if ((cp = strchr(curpath+1, '/')) != NULL) {
4005 if (apr_stat(sb, curpath, APR_FINFO_MIN, NULL) == APR_SUCCESS) {
4016 ** Lexicographic Compare
4020 static int compare_lexicography(char *cpNum1, char *cpNum2)
4025 n1 = strlen(cpNum1);
4026 n2 = strlen(cpNum2);
4033 for (i = 0; i < n1; i++) {
4034 if (cpNum1[i] > cpNum2[i]) {
4037 if (cpNum1[i] < cpNum2[i]) {
4046 ** Bracketed expression handling
4047 ** s points after the opening bracket
4051 static char *find_closing_bracket(char *s, int left, int right)
4055 for (depth = 1; *s; ++s) {
4056 if (*s == right && --depth == 0) {
4059 else if (*s == left) {
4066 static char *find_char_in_brackets(char *s, int c, int left, int right)
4070 for (depth = 1; *s; ++s) {
4071 if (*s == c && depth == 1) {
4074 else if (*s == right && --depth == 0) {
4077 else if (*s == left) {
4086 ** Module paraphernalia
4091 int main(int argc, char *argv[])
4093 ExitThread(TSR_THREAD, 0);
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 "
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)"),
4123 static void register_hooks(apr_pool_t *p)
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);
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);
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 */