1 /* ====================================================================
2 * Copyright (c) 1996-1999 The Apache Group. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
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 Group in July 1997 by
86 ** Ralf S. Engelschall
87 ** rse@engelschall.com
88 ** www.engelschall.com
92 #include "mod_rewrite.h"
95 #include <sys/types.h>
100 ** +-------------------------------------------------------+
102 ** | static module configuration
104 ** +-------------------------------------------------------+
109 ** Our interface to the Apache server kernel:
111 ** o Runtime logic of a request is as following:
112 ** while(request or subrequest)
113 ** foreach(stage #0...#9)
114 ** foreach(module) (**)
117 ** o the order of modules at (**) is the inverted order as
118 ** given in the "Configuration" file, i.e. the last module
119 ** specified is the first one called for each hook!
120 ** The core module is always the last!
122 ** o there are two different types of result checking and
123 ** continue processing:
124 ** for hook #0,#1,#4,#5,#6,#8:
125 ** hook run loop stops on first modules which gives
126 ** back a result != DECLINED, i.e. it usually returns OK
127 ** which says "OK, module has handled this _stage_" and for #1
128 ** this have not to mean "Ok, the filename is now valid".
129 ** for hook #2,#3,#7,#9:
130 ** all hooks are run, independend of result
132 ** o at the last stage, the core module always
133 ** - says "BAD_REQUEST" if r->filename does not begin with "/"
134 ** - prefix URL with document_root or replaced server_root
135 ** with document_root and sets r->filename
136 ** - always return a "OK" independed if the file really exists
140 /* The section for the Configure script:
141 * MODULE-DEFINITION-START
142 * Name: rewrite_module
144 . ./helpers/find-dbm-lib
145 if [ "x$found_dbm" = "x1" ]; then
146 echo " enabling DBM support for mod_rewrite"
148 echo " disabling DBM support for mod_rewrite"
149 echo " (perhaps you need to add -ldbm, -lndbm or -lgdbm to EXTRA_LIBS)"
150 CFLAGS="$CFLAGS -DNO_DBM_REWRITEMAP"
153 * MODULE-DEFINITION-END
156 /* the ap_table_t of commands we provide */
157 static const command_rec command_table[] = {
158 { "RewriteEngine", cmd_rewriteengine, NULL, OR_FILEINFO, FLAG,
159 "On or Off to enable or disable (default) the whole rewriting engine" },
160 { "RewriteOptions", cmd_rewriteoptions, NULL, OR_FILEINFO, ITERATE,
161 "List of option strings to set" },
162 { "RewriteBase", cmd_rewritebase, NULL, OR_FILEINFO, TAKE1,
163 "the base URL of the per-directory context" },
164 { "RewriteCond", cmd_rewritecond, NULL, OR_FILEINFO, RAW_ARGS,
165 "an input string and a to be applied regexp-pattern" },
166 { "RewriteRule", cmd_rewriterule, NULL, OR_FILEINFO, RAW_ARGS,
167 "an URL-applied regexp-pattern and a substitution URL" },
168 { "RewriteMap", cmd_rewritemap, NULL, RSRC_CONF, TAKE2,
169 "a mapname and a filename" },
170 { "RewriteLock", cmd_rewritelock, NULL, RSRC_CONF, TAKE1,
171 "the filename of a lockfile used for inter-process synchronization"},
172 { "RewriteLog", cmd_rewritelog, NULL, RSRC_CONF, TAKE1,
173 "the filename of the rewriting logfile" },
174 { "RewriteLogLevel", cmd_rewriteloglevel, NULL, RSRC_CONF, TAKE1,
175 "the level of the rewriting logfile verbosity "
176 "(0=none, 1=std, .., 9=max)" },
180 /* the ap_table_t of content handlers we provide */
181 static const handler_rec handler_table[] = {
182 { "redirect-handler", handler_redirect },
186 /* the main config structure */
187 module MODULE_VAR_EXPORT rewrite_module = {
188 STANDARD_MODULE_STUFF,
189 init_module, /* module initializer */
190 config_perdir_create, /* create per-dir config structures */
191 config_perdir_merge, /* merge per-dir config structures */
192 config_server_create, /* create per-server config structures */
193 config_server_merge, /* merge per-server config structures */
194 command_table, /* ap_table_t of config file commands */
195 handler_table, /* [#8] MIME-typed-dispatched handlers */
196 hook_uri2file, /* [#1] URI to filename translation */
197 NULL, /* [#4] validate user id from request */
198 NULL, /* [#5] check if the user is ok _here_ */
199 NULL, /* [#3] check access by host address */
200 hook_mimetype, /* [#6] determine MIME type */
201 hook_fixup, /* [#7] pre-run fixups */
202 NULL, /* [#9] log a transaction */
203 NULL, /* [#2] header parser */
204 init_child, /* child_init */
205 NULL, /* child_exit */
206 NULL /* [#0] post read-request */
210 static cache *cachep;
212 /* whether proxy module is available or not */
213 static int proxy_available;
215 static char *lockname;
216 static int lockfd = -1;
219 ** +-------------------------------------------------------+
221 ** | configuration directive handling
223 ** +-------------------------------------------------------+
228 ** per-server configuration structure handling
232 static void *config_server_create(ap_context_t *p, server_rec *s)
234 rewrite_server_conf *a;
236 a = (rewrite_server_conf *)ap_pcalloc(p, sizeof(rewrite_server_conf));
238 a->state = ENGINE_DISABLED;
239 a->options = OPTION_NONE;
240 a->rewritelogfile = NULL;
241 a->rewritelogfp = -1;
242 a->rewriteloglevel = 0;
243 a->rewritemaps = ap_make_array(p, 2, sizeof(rewritemap_entry));
244 a->rewriteconds = ap_make_array(p, 2, sizeof(rewritecond_entry));
245 a->rewriterules = ap_make_array(p, 2, sizeof(rewriterule_entry));
251 static void *config_server_merge(ap_context_t *p, void *basev, void *overridesv)
253 rewrite_server_conf *a, *base, *overrides;
255 a = (rewrite_server_conf *)ap_pcalloc(p, sizeof(rewrite_server_conf));
256 base = (rewrite_server_conf *)basev;
257 overrides = (rewrite_server_conf *)overridesv;
259 a->state = overrides->state;
260 a->options = overrides->options;
261 a->server = overrides->server;
263 if (a->options & OPTION_INHERIT) {
265 * local directives override
266 * and anything else is inherited
268 a->rewriteloglevel = overrides->rewriteloglevel != 0
269 ? overrides->rewriteloglevel
270 : base->rewriteloglevel;
271 a->rewritelogfile = overrides->rewritelogfile != NULL
272 ? overrides->rewritelogfile
273 : base->rewritelogfile;
274 a->rewritelogfp = overrides->rewritelogfp != -1
275 ? overrides->rewritelogfp
276 : base->rewritelogfp;
277 a->rewritemaps = ap_append_arrays(p, overrides->rewritemaps,
279 a->rewriteconds = ap_append_arrays(p, overrides->rewriteconds,
281 a->rewriterules = ap_append_arrays(p, overrides->rewriterules,
286 * local directives override
287 * and anything else gets defaults
289 a->rewriteloglevel = overrides->rewriteloglevel;
290 a->rewritelogfile = overrides->rewritelogfile;
291 a->rewritelogfp = overrides->rewritelogfp;
292 a->rewritemaps = overrides->rewritemaps;
293 a->rewriteconds = overrides->rewriteconds;
294 a->rewriterules = overrides->rewriterules;
303 ** per-directory configuration structure handling
307 static void *config_perdir_create(ap_context_t *p, char *path)
309 rewrite_perdir_conf *a;
311 a = (rewrite_perdir_conf *)ap_pcalloc(p, sizeof(rewrite_perdir_conf));
313 a->state = ENGINE_DISABLED;
314 a->options = OPTION_NONE;
316 a->rewriteconds = ap_make_array(p, 2, sizeof(rewritecond_entry));
317 a->rewriterules = ap_make_array(p, 2, sizeof(rewriterule_entry));
323 /* make sure it has a trailing slash */
324 if (path[strlen(path)-1] == '/') {
325 a->directory = ap_pstrdup(p, path);
328 a->directory = ap_pstrcat(p, path, "/", NULL);
335 static void *config_perdir_merge(ap_context_t *p, void *basev, void *overridesv)
337 rewrite_perdir_conf *a, *base, *overrides;
339 a = (rewrite_perdir_conf *)ap_pcalloc(p,
340 sizeof(rewrite_perdir_conf));
341 base = (rewrite_perdir_conf *)basev;
342 overrides = (rewrite_perdir_conf *)overridesv;
344 a->state = overrides->state;
345 a->options = overrides->options;
346 a->directory = overrides->directory;
347 a->baseurl = overrides->baseurl;
349 if (a->options & OPTION_INHERIT) {
350 a->rewriteconds = ap_append_arrays(p, overrides->rewriteconds,
352 a->rewriterules = ap_append_arrays(p, overrides->rewriterules,
356 a->rewriteconds = overrides->rewriteconds;
357 a->rewriterules = overrides->rewriterules;
366 ** the configuration commands
370 static const char *cmd_rewriteengine(cmd_parms *cmd,
371 rewrite_perdir_conf *dconf, int flag)
373 rewrite_server_conf *sconf;
376 (rewrite_server_conf *)ap_get_module_config(cmd->server->module_config,
379 if (cmd->path == NULL) { /* is server command */
380 sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
382 else /* is per-directory command */ {
383 dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
389 static const char *cmd_rewriteoptions(cmd_parms *cmd,
390 rewrite_perdir_conf *dconf, char *option)
392 rewrite_server_conf *sconf;
395 sconf = (rewrite_server_conf *)
396 ap_get_module_config(cmd->server->module_config, &rewrite_module);
398 if (cmd->path == NULL) { /* is server command */
399 err = cmd_rewriteoptions_setoption(cmd->pool,
400 &(sconf->options), option);
402 else { /* is per-directory command */
403 err = cmd_rewriteoptions_setoption(cmd->pool,
404 &(dconf->options), option);
410 static const char *cmd_rewriteoptions_setoption(ap_context_t *p, int *options,
413 if (strcasecmp(name, "inherit") == 0) {
414 *options |= OPTION_INHERIT;
417 return ap_pstrcat(p, "RewriteOptions: unknown option '",
423 static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, char *a1)
425 rewrite_server_conf *sconf;
427 sconf = (rewrite_server_conf *)
428 ap_get_module_config(cmd->server->module_config, &rewrite_module);
430 sconf->rewritelogfile = a1;
435 static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, char *a1)
437 rewrite_server_conf *sconf;
439 sconf = (rewrite_server_conf *)
440 ap_get_module_config(cmd->server->module_config, &rewrite_module);
442 sconf->rewriteloglevel = atoi(a1);
447 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, char *a1,
450 rewrite_server_conf *sconf;
451 rewritemap_entry *new;
454 sconf = (rewrite_server_conf *)
455 ap_get_module_config(cmd->server->module_config, &rewrite_module);
457 new = ap_push_array(sconf->rewritemaps);
461 if (strncmp(a2, "txt:", 4) == 0) {
462 new->type = MAPTYPE_TXT;
463 new->datafile = a2+4;
464 new->checkfile = a2+4;
466 else if (strncmp(a2, "rnd:", 4) == 0) {
467 new->type = MAPTYPE_RND;
468 new->datafile = a2+4;
469 new->checkfile = a2+4;
471 else if (strncmp(a2, "dbm:", 4) == 0) {
472 #ifndef NO_DBM_REWRITEMAP
473 new->type = MAPTYPE_DBM;
474 new->datafile = a2+4;
475 new->checkfile = ap_pstrcat(cmd->pool, a2+4, NDBM_FILE_SUFFIX, NULL);
477 return ap_pstrdup(cmd->pool, "RewriteMap: cannot use NDBM mapfile, "
478 "because no NDBM support is compiled in");
481 else if (strncmp(a2, "prg:", 4) == 0) {
482 new->type = MAPTYPE_PRG;
483 new->datafile = a2+4;
484 new->checkfile = a2+4;
486 else if (strncmp(a2, "int:", 4) == 0) {
487 new->type = MAPTYPE_INT;
488 new->datafile = NULL;
489 new->checkfile = NULL;
490 if (strcmp(a2+4, "tolower") == 0) {
491 new->func = rewrite_mapfunc_tolower;
493 else if (strcmp(a2+4, "toupper") == 0) {
494 new->func = rewrite_mapfunc_toupper;
496 else if (strcmp(a2+4, "escape") == 0) {
497 new->func = rewrite_mapfunc_escape;
499 else if (strcmp(a2+4, "unescape") == 0) {
500 new->func = rewrite_mapfunc_unescape;
502 else if (sconf->state == ENGINE_ENABLED) {
503 return ap_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
508 new->type = MAPTYPE_TXT;
515 if (new->checkfile && (sconf->state == ENGINE_ENABLED)
516 && (stat(new->checkfile, &st) == -1)) {
517 return ap_pstrcat(cmd->pool,
518 "RewriteMap: map file or program not found:",
519 new->checkfile, NULL);
525 static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, char *a1)
529 if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL)
537 static const char *cmd_rewritebase(cmd_parms *cmd, rewrite_perdir_conf *dconf,
540 if (cmd->path == NULL || dconf == NULL) {
541 return "RewriteBase: only valid in per-directory config files";
544 return "RewriteBase: empty URL not allowed";
547 return "RewriteBase: argument is not a valid URL";
555 static const char *cmd_rewritecond(cmd_parms *cmd, rewrite_perdir_conf *dconf,
558 rewrite_server_conf *sconf;
559 rewritecond_entry *new;
568 sconf = (rewrite_server_conf *)
569 ap_get_module_config(cmd->server->module_config, &rewrite_module);
571 /* make a new entry in the internal temporary rewrite rule list */
572 if (cmd->path == NULL) { /* is server command */
573 new = ap_push_array(sconf->rewriteconds);
575 else { /* is per-directory command */
576 new = ap_push_array(dconf->rewriteconds);
579 /* parse the argument line ourself */
580 if (parseargline(str, &a1, &a2, &a3)) {
581 return ap_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
585 /* arg1: the input string */
586 new->input = ap_pstrdup(cmd->pool, a1);
588 /* arg3: optional flags field
589 (this have to be first parsed, because we need to
590 know if the regex should be compiled with ICASE!) */
591 new->flags = CONDFLAG_NONE;
593 if ((err = cmd_rewritecond_parseflagfield(cmd->pool, new,
600 try to compile the regexp to test if is ok */
603 new->flags |= CONDFLAG_NOTMATCH;
607 /* now be careful: Under the POSIX regex library
608 we can compile the pattern for case insensitive matching,
609 under the old V8 library we have to do it self via a hack */
610 if (new->flags & CONDFLAG_NOCASE) {
611 rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED|REG_ICASE))
615 rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL);
618 return ap_pstrcat(cmd->pool,
619 "RewriteCond: cannot compile regular expression '",
623 new->pattern = ap_pstrdup(cmd->pool, cp);
624 new->regexp = regexp;
629 static const char *cmd_rewritecond_parseflagfield(ap_context_t *p,
630 rewritecond_entry *cfg,
641 if (str[0] != '[' || str[strlen(str)-1] != ']') {
642 return "RewriteCond: bad flag delimiters";
646 str[strlen(str)-1] = ','; /* for simpler parsing */
647 for ( ; *cp != '\0'; ) {
648 /* skip whitespaces */
649 for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
655 if ((cp2 = strchr(cp, ',')) != NULL) {
657 for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
660 if ((cp3 = strchr(cp1, '=')) != NULL) {
669 if ((err = cmd_rewritecond_setflag(p, cfg, key, val)) != NULL) {
681 static const char *cmd_rewritecond_setflag(ap_context_t *p, rewritecond_entry *cfg,
682 char *key, char *val)
684 if ( strcasecmp(key, "nocase") == 0
685 || strcasecmp(key, "NC") == 0 ) {
686 cfg->flags |= CONDFLAG_NOCASE;
688 else if ( strcasecmp(key, "ornext") == 0
689 || strcasecmp(key, "OR") == 0 ) {
690 cfg->flags |= CONDFLAG_ORNEXT;
693 return ap_pstrcat(p, "RewriteCond: unknown flag '", key, "'\n", NULL);
698 static const char *cmd_rewriterule(cmd_parms *cmd, rewrite_perdir_conf *dconf,
701 rewrite_server_conf *sconf;
702 rewriterule_entry *new;
711 sconf = (rewrite_server_conf *)
712 ap_get_module_config(cmd->server->module_config, &rewrite_module);
714 /* make a new entry in the internal rewrite rule list */
715 if (cmd->path == NULL) { /* is server command */
716 new = ap_push_array(sconf->rewriterules);
718 else { /* is per-directory command */
719 new = ap_push_array(dconf->rewriterules);
722 /* parse the argument line ourself */
723 if (parseargline(str, &a1, &a2, &a3)) {
724 return ap_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
728 /* arg3: optional flags field */
729 new->forced_mimetype = NULL;
730 new->forced_responsecode = HTTP_MOVED_TEMPORARILY;
731 new->flags = RULEFLAG_NONE;
735 if ((err = cmd_rewriterule_parseflagfield(cmd->pool, new,
742 * try to compile the regexp to test if is ok
746 new->flags |= RULEFLAG_NOTMATCH;
750 if (new->flags & RULEFLAG_NOCASE) {
753 if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) {
754 return ap_pstrcat(cmd->pool,
755 "RewriteRule: cannot compile regular expression '",
758 new->pattern = ap_pstrdup(cmd->pool, cp);
759 new->regexp = regexp;
761 /* arg2: the output string
762 * replace the $<N> by \<n> which is needed by the currently
763 * used Regular Expression library
765 new->output = ap_pstrdup(cmd->pool, a2);
767 /* now, if the server or per-dir config holds an
768 * array of RewriteCond entries, we take it for us
769 * and clear the array
771 if (cmd->path == NULL) { /* is server command */
772 new->rewriteconds = sconf->rewriteconds;
773 sconf->rewriteconds = ap_make_array(cmd->pool, 2,
774 sizeof(rewritecond_entry));
776 else { /* is per-directory command */
777 new->rewriteconds = dconf->rewriteconds;
778 dconf->rewriteconds = ap_make_array(cmd->pool, 2,
779 sizeof(rewritecond_entry));
785 static const char *cmd_rewriterule_parseflagfield(ap_context_t *p,
786 rewriterule_entry *cfg,
797 if (str[0] != '[' || str[strlen(str)-1] != ']') {
798 return "RewriteRule: bad flag delimiters";
802 str[strlen(str)-1] = ','; /* for simpler parsing */
803 for ( ; *cp != '\0'; ) {
804 /* skip whitespaces */
805 for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++)
811 if ((cp2 = strchr(cp, ',')) != NULL) {
813 for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--)
816 if ((cp3 = strchr(cp1, '=')) != NULL) {
825 if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) {
837 static const char *cmd_rewriterule_setflag(ap_context_t *p, rewriterule_entry *cfg,
838 char *key, char *val)
843 if ( strcasecmp(key, "redirect") == 0
844 || strcasecmp(key, "R") == 0 ) {
845 cfg->flags |= RULEFLAG_FORCEREDIRECT;
846 if (strlen(val) > 0) {
847 if (strcasecmp(val, "permanent") == 0) {
848 status = HTTP_MOVED_PERMANENTLY;
850 else if (strcasecmp(val, "temp") == 0) {
851 status = HTTP_MOVED_TEMPORARILY;
853 else if (strcasecmp(val, "seeother") == 0) {
854 status = HTTP_SEE_OTHER;
856 else if (ap_isdigit(*val)) {
859 if (!ap_is_HTTP_REDIRECT(status)) {
860 return "RewriteRule: invalid HTTP response code "
863 cfg->forced_responsecode = status;
866 else if ( strcasecmp(key, "last") == 0
867 || strcasecmp(key, "L") == 0 ) {
868 cfg->flags |= RULEFLAG_LASTRULE;
870 else if ( strcasecmp(key, "next") == 0
871 || strcasecmp(key, "N") == 0 ) {
872 cfg->flags |= RULEFLAG_NEWROUND;
874 else if ( strcasecmp(key, "chain") == 0
875 || strcasecmp(key, "C") == 0 ) {
876 cfg->flags |= RULEFLAG_CHAIN;
878 else if ( strcasecmp(key, "type") == 0
879 || strcasecmp(key, "T") == 0 ) {
880 cfg->forced_mimetype = ap_pstrdup(p, val);
881 ap_str_tolower(cfg->forced_mimetype);
883 else if ( strcasecmp(key, "env") == 0
884 || strcasecmp(key, "E") == 0 ) {
885 for (i = 0; (cfg->env[i] != NULL) && (i < MAX_ENV_FLAGS); i++)
887 if (i < MAX_ENV_FLAGS) {
888 cfg->env[i] = ap_pstrdup(p, val);
889 cfg->env[i+1] = NULL;
892 return "RewriteRule: too many environment flags 'E'";
895 else if ( strcasecmp(key, "nosubreq") == 0
896 || strcasecmp(key, "NS") == 0 ) {
897 cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
899 else if ( strcasecmp(key, "proxy") == 0
900 || strcasecmp(key, "P") == 0 ) {
901 cfg->flags |= RULEFLAG_PROXY;
903 else if ( strcasecmp(key, "passthrough") == 0
904 || strcasecmp(key, "PT") == 0 ) {
905 cfg->flags |= RULEFLAG_PASSTHROUGH;
907 else if ( strcasecmp(key, "skip") == 0
908 || strcasecmp(key, "S") == 0 ) {
909 cfg->skip = atoi(val);
911 else if ( strcasecmp(key, "forbidden") == 0
912 || strcasecmp(key, "F") == 0 ) {
913 cfg->flags |= RULEFLAG_FORBIDDEN;
915 else if ( strcasecmp(key, "gone") == 0
916 || strcasecmp(key, "G") == 0 ) {
917 cfg->flags |= RULEFLAG_GONE;
919 else if ( strcasecmp(key, "qsappend") == 0
920 || strcasecmp(key, "QSA") == 0 ) {
921 cfg->flags |= RULEFLAG_QSAPPEND;
923 else if ( strcasecmp(key, "nocase") == 0
924 || strcasecmp(key, "NC") == 0 ) {
925 cfg->flags |= RULEFLAG_NOCASE;
928 return ap_pstrcat(p, "RewriteRule: unknown flag '", key, "'\n", NULL);
936 ** Global Module Initialization
937 ** [called from read_config() after all
938 ** config commands were already called]
942 static void init_module(server_rec *s, ap_context_t *p)
944 /* check if proxy module is available */
945 proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
947 /* create the rewriting lockfile in the parent */
948 rewritelock_create(s, p);
949 ap_register_cleanup(p, (void *)s, rewritelock_remove, ap_null_cleanup);
951 /* step through the servers and
952 * - open each rewriting logfile
953 * - open the RewriteMap prg:xxx programs
955 for (; s; s = s->next) {
956 open_rewritelog(s, p);
957 run_rewritemap_programs(s, p);
964 ** Per-Child Module Initialization
965 ** [called after a child process is spawned]
969 static void init_child(server_rec *s, ap_context_t *p)
971 /* open the rewriting lockfile */
972 rewritelock_open(s, p);
974 /* create the lookup cache */
975 cachep = init_cache(p);
980 ** +-------------------------------------------------------+
984 ** +-------------------------------------------------------+
989 ** URI-to-filename hook
991 ** [used for the rewriting engine triggered by
992 ** the per-server 'RewriteRule' directives]
996 static int hook_uri2file(request_rec *r)
999 rewrite_server_conf *conf;
1001 const char *thisserver;
1003 const char *thisurl;
1014 * retrieve the config structures
1016 sconf = r->server->module_config;
1017 conf = (rewrite_server_conf *)ap_get_module_config(sconf,
1021 * only do something under runtime if the engine is really enabled,
1022 * else return immediately!
1024 if (conf->state == ENGINE_DISABLED) {
1029 * check for the ugly API case of a virtual host section where no
1030 * mod_rewrite directives exists. In this situation we became no chance
1031 * by the API to setup our default per-server config so we have to
1032 * on-the-fly assume we have the default config. But because the default
1033 * config has a disabled rewriting engine we are lucky because can
1034 * just stop operating now.
1036 if (conf->server != r->server) {
1041 * add the SCRIPT_URL variable to the env. this is a bit complicated
1042 * due to the fact that apache uses subrequests and internal redirects
1045 if (r->main == NULL) {
1046 var = ap_pstrcat(r->pool, "REDIRECT_", ENVVAR_SCRIPT_URL, NULL);
1047 var = ap_table_get(r->subprocess_env, var);
1049 ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
1052 ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1056 var = ap_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
1057 ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
1061 * create the SCRIPT_URI variable for the env
1064 /* add the canonical URI of this URL */
1065 thisserver = ap_get_server_name(r);
1066 port = ap_get_server_port(r);
1067 if (ap_is_default_port(port, r)) {
1071 ap_snprintf(buf, sizeof(buf), ":%u", port);
1074 thisurl = ap_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
1076 /* set the variable */
1077 var = ap_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport,
1079 ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
1081 /* if filename was not initially set,
1082 * we start with the requested URI
1084 if (r->filename == NULL) {
1085 r->filename = ap_pstrdup(r->pool, r->uri);
1086 rewritelog(r, 2, "init rewrite engine with requested uri %s",
1091 * now apply the rules ...
1093 if (apply_rewrite_list(r, conf->rewriterules, NULL)) {
1095 if (strlen(r->filename) > 6 &&
1096 strncmp(r->filename, "proxy:", 6) == 0) {
1097 /* it should be go on as an internal proxy request */
1099 /* check if the proxy module is enabled, so
1100 * we can actually use it!
1102 if (!proxy_available) {
1103 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1104 "attempt to make remote request from mod_rewrite "
1105 "without proxy enabled: %s", r->filename);
1109 /* make sure the QUERY_STRING and
1110 * PATH_INFO parts get incorporated
1112 if (r->path_info != NULL) {
1113 r->filename = ap_pstrcat(r->pool, r->filename,
1114 r->path_info, NULL);
1116 if (r->args != NULL &&
1117 r->uri == r->unparsed_uri) {
1118 /* see proxy_http:proxy_http_canon() */
1119 r->filename = ap_pstrcat(r->pool, r->filename,
1120 "?", r->args, NULL);
1123 /* now make sure the request gets handled by the proxy handler */
1125 r->handler = "proxy-server";
1127 rewritelog(r, 1, "go-ahead with proxy request %s [OK]",
1131 else if ( (strlen(r->filename) > 7 &&
1132 strncasecmp(r->filename, "http://", 7) == 0)
1133 || (strlen(r->filename) > 8 &&
1134 strncasecmp(r->filename, "https://", 8) == 0)
1135 || (strlen(r->filename) > 9 &&
1136 strncasecmp(r->filename, "gopher://", 9) == 0)
1137 || (strlen(r->filename) > 6 &&
1138 strncasecmp(r->filename, "ftp://", 6) == 0) ) {
1139 /* it was finally rewritten to a remote URL */
1141 /* skip 'scheme:' */
1142 for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++)
1146 /* skip host part */
1147 for ( ; *cp != '/' && *cp != '\0'; cp++)
1150 rewritelog(r, 1, "escaping %s for redirect", r->filename);
1151 cp2 = ap_escape_uri(r->pool, cp);
1153 r->filename = ap_pstrcat(r->pool, r->filename, cp2, NULL);
1156 /* append the QUERY_STRING part */
1157 if (r->args != NULL) {
1158 r->filename = ap_pstrcat(r->pool, r->filename,
1159 "?", r->args, NULL);
1162 /* determine HTTP redirect response code */
1163 if (ap_is_HTTP_REDIRECT(r->status)) {
1165 r->status = HTTP_OK; /* make Apache kernel happy */
1171 /* now do the redirection */
1172 ap_table_setn(r->headers_out, "Location", r->filename);
1173 rewritelog(r, 1, "redirect to %s [REDIRECT/%d]", r->filename, n);
1176 else if (strlen(r->filename) > 10 &&
1177 strncmp(r->filename, "forbidden:", 10) == 0) {
1178 /* This URLs is forced to be forbidden for the requester */
1181 else if (strlen(r->filename) > 5 &&
1182 strncmp(r->filename, "gone:", 5) == 0) {
1183 /* This URLs is forced to be gone */
1186 else if (strlen(r->filename) > 12 &&
1187 strncmp(r->filename, "passthrough:", 12) == 0) {
1189 * Hack because of underpowered API: passing the current
1190 * rewritten filename through to other URL-to-filename handlers
1191 * just as it were the requested URL. This is to enable
1192 * post-processing by mod_alias, etc. which always act on
1193 * r->uri! The difference here is: We do not try to
1194 * add the document root
1196 r->uri = ap_pstrdup(r->pool, r->filename+12);
1200 /* it was finally rewritten to a local path */
1202 /* expand "/~user" prefix */
1204 r->filename = expand_tildepaths(r, r->filename);
1206 rewritelog(r, 2, "local path result: %s", r->filename);
1208 /* the filename has to start with a slash! */
1209 if (r->filename[0] != '/') {
1213 /* if there is no valid prefix, we have
1214 * to emulate the translator from the core and
1215 * prefix the filename with document_root
1218 * We cannot leave out the prefix_stat because
1219 * - when we always prefix with document_root
1220 * then no absolute path can be created, e.g. via
1221 * emulating a ScriptAlias directive, etc.
1222 * - when we always NOT prefix with document_root
1223 * then the files under document_root have to
1224 * be references directly and document_root
1225 * gets never used and will be a dummy parameter -
1229 * Under real Unix systems this is no problem,
1230 * because we only do stat() on the first directory
1231 * and this gets cached by the kernel for along time!
1233 n = prefix_stat(r->filename, &finfo);
1235 if ((ccp = ap_document_root(r)) != NULL) {
1236 l = ap_cpystrn(docroot, ccp, sizeof(docroot)) - docroot;
1238 /* always NOT have a trailing slash */
1239 if (docroot[l-1] == '/') {
1240 docroot[l-1] = '\0';
1243 && !strncmp(r->filename, r->server->path,
1244 r->server->pathlen)) {
1245 r->filename = ap_pstrcat(r->pool, docroot,
1247 r->server->pathlen), NULL);
1250 r->filename = ap_pstrcat(r->pool, docroot,
1253 rewritelog(r, 2, "prefixed with document_root to %s",
1258 rewritelog(r, 1, "go-ahead with %s [OK]", r->filename);
1263 rewritelog(r, 1, "pass through %s", r->filename);
1273 ** [used to support the forced-MIME-type feature]
1277 static int hook_mimetype(request_rec *r)
1281 /* now check if we have to force a MIME-type */
1282 t = ap_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
1287 rewritelog(r, 1, "force filename %s to have MIME-type '%s'",
1289 r->content_type = t;
1299 ** [used for the rewriting engine triggered by
1300 ** the per-directory 'RewriteRule' directives]
1304 static int hook_fixup(request_rec *r)
1306 rewrite_perdir_conf *dconf;
1315 dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
1318 /* if there is no per-dir config we return immediately */
1319 if (dconf == NULL) {
1323 /* we shouldn't do anything in subrequests */
1324 if (r->main != NULL) {
1328 /* if there are no real (i.e. no RewriteRule directives!)
1329 per-dir config of us, we return also immediately */
1330 if (dconf->directory == NULL) {
1335 * only do something under runtime if the engine is really enabled,
1336 * for this directory, else return immediately!
1338 if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) {
1339 /* FollowSymLinks is mandatory! */
1340 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
1341 "Options FollowSymLinks or SymLinksIfOwnerMatch is off "
1342 "which implies that RewriteRule directive is forbidden: "
1347 /* FollowSymLinks is given, but the user can
1348 * still turn off the rewriting engine
1350 if (dconf->state == ENGINE_DISABLED) {
1356 * remember the current filename before rewriting for later check
1357 * to prevent deadlooping because of internal redirects
1358 * on final URL/filename which can be equal to the inital one.
1360 ofilename = r->filename;
1363 * now apply the rules ...
1365 if (apply_rewrite_list(r, dconf->rewriterules, dconf->directory)) {
1367 if (strlen(r->filename) > 6 &&
1368 strncmp(r->filename, "proxy:", 6) == 0) {
1369 /* it should go on as an internal proxy request */
1371 /* make sure the QUERY_STRING and
1372 * PATH_INFO parts get incorporated
1373 * (r->path_info was already appended by the
1374 * rewriting engine because of the per-dir context!)
1377 && r->uri == r->unparsed_uri) {
1378 /* see proxy_http:proxy_http_canon() */
1379 r->filename = ap_pstrcat(r->pool, r->filename,
1380 "?", r->args, NULL);
1383 /* now make sure the request gets handled by the proxy handler */
1385 r->handler = "proxy-server";
1387 rewritelog(r, 1, "[per-dir %s] go-ahead with proxy request "
1388 "%s [OK]", dconf->directory, r->filename);
1391 else if ( (strlen(r->filename) > 7 &&
1392 strncmp(r->filename, "http://", 7) == 0)
1393 || (strlen(r->filename) > 8 &&
1394 strncmp(r->filename, "https://", 8) == 0)
1395 || (strlen(r->filename) > 9 &&
1396 strncmp(r->filename, "gopher://", 9) == 0)
1397 || (strlen(r->filename) > 6 &&
1398 strncmp(r->filename, "ftp://", 6) == 0) ) {
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 = ap_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 = ap_pstrcat(r->pool, r->filename, cp2, NULL);
1445 /* append the QUERY_STRING part */
1446 if (r->args != NULL) {
1447 r->filename = ap_pstrcat(r->pool, r->filename,
1448 "?", 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 */
1460 /* now do the redirection */
1461 ap_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 */
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 = ap_pstrdup(r->pool, r->filename+12);
1488 /* the filename has to start with a slash! */
1489 if (r->filename[0] != '/') {
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 = ap_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 = ap_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 = ap_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 /* just make sure that we are really meant! */
1573 if (strncmp(r->filename, "redirect:", 9) != 0) {
1577 /* now do the internal redirect */
1578 ap_internal_redirect(ap_pstrcat(r->pool, r->filename+9,
1579 r->args ? "?" : NULL, r->args, NULL), r);
1581 /* and return gracefully */
1587 ** +-------------------------------------------------------+
1589 ** | the rewriting engine
1591 ** +-------------------------------------------------------+
1595 * Apply a complete rule set,
1596 * i.e. a list of rewrite rules
1598 static int apply_rewrite_list(request_rec *r, ap_array_header_t *rewriterules,
1601 rewriterule_entry *entries;
1602 rewriterule_entry *p;
1609 * Iterate over all existing rules
1611 entries = (rewriterule_entry *)rewriterules->elts;
1614 for (i = 0; i < rewriterules->nelts; i++) {
1618 * Ignore this rule on subrequests if we are explicitly
1619 * asked to do so or this is a proxy-throughput or a
1620 * forced redirect rule.
1622 if (r->main != NULL &&
1623 (p->flags & RULEFLAG_IGNOREONSUBREQ ||
1624 p->flags & RULEFLAG_PROXY ||
1625 p->flags & RULEFLAG_FORCEREDIRECT )) {
1630 * Apply the current rule.
1632 rc = apply_rewrite_rule(r, p, perdir);
1635 * Indicate a change if this was not a match-only rule.
1642 * Pass-Through Feature (`RewriteRule .. .. [PT]'):
1643 * Because the Apache 1.x API is very limited we
1644 * need this hack to pass the rewritten URL to other
1645 * modules like mod_alias, mod_userdir, etc.
1647 if (p->flags & RULEFLAG_PASSTHROUGH) {
1648 rewritelog(r, 2, "forcing '%s' to get passed through "
1649 "to next API URI-to-filename handler", r->filename);
1650 r->filename = ap_pstrcat(r->pool, "passthrough:",
1657 * Rule has the "forbidden" flag set which means that
1658 * we stop processing and indicate this to the caller.
1660 if (p->flags & RULEFLAG_FORBIDDEN) {
1661 rewritelog(r, 2, "forcing '%s' to be forbidden", r->filename);
1662 r->filename = ap_pstrcat(r->pool, "forbidden:",
1669 * Rule has the "gone" flag set which means that
1670 * we stop processing and indicate this to the caller.
1672 if (p->flags & RULEFLAG_GONE) {
1673 rewritelog(r, 2, "forcing '%s' to be gone", r->filename);
1674 r->filename = ap_pstrcat(r->pool, "gone:", r->filename, NULL);
1680 * Stop processing also on proxy pass-through and
1681 * last-rule and new-round flags.
1683 if (p->flags & RULEFLAG_PROXY) {
1686 if (p->flags & RULEFLAG_LASTRULE) {
1691 * On "new-round" flag we just start from the top of
1692 * the rewriting ruleset again.
1694 if (p->flags & RULEFLAG_NEWROUND) {
1699 * If we are forced to skip N next rules, do it now.
1703 while ( i < rewriterules->nelts
1713 * If current rule is chained with next rule(s),
1714 * skip all this next rule(s)
1716 while ( i < rewriterules->nelts
1717 && p->flags & RULEFLAG_CHAIN) {
1727 * Apply a single(!) rewrite rule
1729 static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
1735 char newuri[MAX_STRING_LEN];
1736 char env[MAX_STRING_LEN];
1738 regmatch_t regmatch[MAX_NMATCH];
1739 backrefinfo *briRR = NULL;
1740 backrefinfo *briRC = NULL;
1743 ap_array_header_t *rewriteconds;
1744 rewritecond_entry *conds;
1745 rewritecond_entry *c;
1757 * Add (perhaps splitted away) PATH_INFO postfix to URL to
1758 * make sure we really match against the complete URL.
1760 if (perdir != NULL && r->path_info != NULL && r->path_info[0] != '\0') {
1761 rewritelog(r, 3, "[per-dir %s] add path info postfix: %s -> %s%s",
1762 perdir, uri, uri, r->path_info);
1763 uri = ap_pstrcat(r->pool, uri, r->path_info, NULL);
1767 * On per-directory context (.htaccess) strip the location
1768 * prefix from the URL to make sure patterns apply only to
1769 * the local part. Additionally indicate this special
1770 * threatment in the logfile.
1773 if (perdir != NULL) {
1774 if ( strlen(uri) >= strlen(perdir)
1775 && strncmp(uri, perdir, strlen(perdir)) == 0) {
1776 rewritelog(r, 3, "[per-dir %s] strip per-dir prefix: %s -> %s",
1777 perdir, uri, uri+strlen(perdir));
1778 uri = uri+strlen(perdir);
1784 * Try to match the URI against the RewriteRule pattern
1785 * and exit immeddiately if it didn't apply.
1787 if (perdir == NULL) {
1788 rewritelog(r, 3, "applying pattern '%s' to uri '%s'",
1792 rewritelog(r, 3, "[per-dir %s] applying pattern '%s' to uri '%s'",
1793 perdir, p->pattern, uri);
1795 rc = (ap_regexec(regexp, uri, regexp->re_nsub+1, regmatch, 0) == 0);
1796 if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
1797 (!rc && (p->flags & RULEFLAG_NOTMATCH)) ) ) {
1802 * Else create the RewriteRule `regsubinfo' structure which
1803 * holds the substitution information.
1805 briRR = (backrefinfo *)ap_palloc(r->pool, sizeof(backrefinfo));
1806 if (!rc && (p->flags & RULEFLAG_NOTMATCH)) {
1807 /* empty info on negative patterns */
1812 briRR->source = ap_pstrdup(r->pool, uri);
1813 briRR->nsub = regexp->re_nsub;
1814 memcpy((void *)(briRR->regmatch), (void *)(regmatch),
1819 * Initiallally create the RewriteCond backrefinfo with
1820 * empty backrefinfo, i.e. not subst parts
1821 * (this one is adjusted inside apply_rewrite_cond() later!!)
1823 briRC = (backrefinfo *)ap_pcalloc(r->pool, sizeof(backrefinfo));
1828 * Ok, we already know the pattern has matched, but we now
1829 * additionally have to check for all existing preconditions
1830 * (RewriteCond) which have to be also true. We do this at
1831 * this very late stage to avoid unnessesary checks which
1832 * would slow down the rewriting engine!!
1834 rewriteconds = p->rewriteconds;
1835 conds = (rewritecond_entry *)rewriteconds->elts;
1837 for (i = 0; i < rewriteconds->nelts; i++) {
1839 rc = apply_rewrite_cond(r, c, perdir, briRR, briRC);
1840 if (c->flags & CONDFLAG_ORNEXT) {
1845 /* One condition is false, but another can be
1846 * still true, so we have to continue...
1848 ap_table_unset(r->notes, VARY_KEY_THIS);
1852 /* One true condition is enough in "or" case, so
1853 * skip the other conditions which are "ornext"
1856 while ( i < rewriteconds->nelts
1857 && c->flags & CONDFLAG_ORNEXT) {
1866 * The "AND" case, i.e. no "or" flag,
1867 * so a single failure means total failure.
1874 vary = ap_table_get(r->notes, VARY_KEY_THIS);
1876 ap_table_merge(r->notes, VARY_KEY, vary);
1877 ap_table_unset(r->notes, VARY_KEY_THIS);
1880 /* if any condition fails the complete rule fails */
1882 ap_table_unset(r->notes, VARY_KEY);
1883 ap_table_unset(r->notes, VARY_KEY_THIS);
1888 * Regardless of what we do next, we've found a match. Check to see
1889 * if any of the request header fields were involved, and add them
1890 * to the Vary field of the response.
1892 if ((vary = ap_table_get(r->notes, VARY_KEY)) != NULL) {
1893 ap_table_merge(r->headers_out, "Vary", vary);
1894 ap_table_unset(r->notes, VARY_KEY);
1898 * If this is a pure matching rule (`RewriteRule <pat> -')
1899 * we stop processing and return immediately. The only thing
1900 * we have not to forget are the environment variables
1901 * (`RewriteRule <pat> - [E=...]')
1903 if (strcmp(output, "-") == 0) {
1904 for (i = 0; p->env[i] != NULL; i++) {
1905 /* 1. take the string */
1906 ap_cpystrn(env, p->env[i], sizeof(env));
1907 /* 2. expand $N (i.e. backrefs to RewriteRule pattern) */
1908 expand_backref_inbuffer(r->pool, env, sizeof(env), briRR, '$');
1909 /* 3. expand %N (i.e. backrefs to latest RewriteCond pattern) */
1910 expand_backref_inbuffer(r->pool, env, sizeof(env), briRC, '%');
1911 /* 4. expand %{...} (i.e. variables) */
1912 expand_variables_inbuffer(r, env, sizeof(env));
1913 /* 5. expand ${...} (RewriteMap lookups) */
1914 expand_map_lookups(r, env, sizeof(env));
1915 /* and add the variable to Apache's structures */
1916 add_env_variable(r, env);
1918 if (p->forced_mimetype != NULL) {
1919 if (perdir == NULL) {
1920 /* In the per-server context we can force the MIME-type
1921 * the correct way by notifying our MIME-type hook handler
1922 * to do the job when the MIME-type API stage is reached.
1924 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
1925 r->filename, p->forced_mimetype);
1926 ap_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
1927 p->forced_mimetype);
1930 /* In per-directory context we operate in the Fixup API hook
1931 * which is after the MIME-type hook, so our MIME-type handler
1932 * has no chance to set r->content_type. And because we are
1933 * in the situation where no substitution takes place no
1934 * sub-request will happen (which could solve the
1935 * restriction). As a workaround we do it ourself now
1936 * immediately although this is not strictly API-conforming.
1937 * But it's the only chance we have...
1939 rewritelog(r, 1, "[per-dir %s] force %s to have MIME-type "
1940 "'%s'", perdir, r->filename, p->forced_mimetype);
1941 r->content_type = p->forced_mimetype;
1948 * Ok, now we finally know all patterns have matched and
1949 * that there is something to replace, so we create the
1950 * substitution URL string in `newuri'.
1952 /* 1. take the output string */
1953 ap_cpystrn(newuri, output, sizeof(newuri));
1954 /* 2. expand $N (i.e. backrefs to RewriteRule pattern) */
1955 expand_backref_inbuffer(r->pool, newuri, sizeof(newuri), briRR, '$');
1956 /* 3. expand %N (i.e. backrefs to latest RewriteCond pattern) */
1957 expand_backref_inbuffer(r->pool, newuri, sizeof(newuri), briRC, '%');
1958 /* 4. expand %{...} (i.e. variables) */
1959 expand_variables_inbuffer(r, newuri, sizeof(newuri));
1960 /* 5. expand ${...} (RewriteMap lookups) */
1961 expand_map_lookups(r, newuri, sizeof(newuri));
1962 /* and log the result... */
1963 if (perdir == NULL) {
1964 rewritelog(r, 2, "rewrite %s -> %s", uri, newuri);
1967 rewritelog(r, 2, "[per-dir %s] rewrite %s -> %s", perdir, uri, newuri);
1971 * Additionally do expansion for the environment variable
1972 * strings (`RewriteRule .. .. [E=<string>]').
1974 for (i = 0; p->env[i] != NULL; i++) {
1975 /* 1. take the string */
1976 ap_cpystrn(env, p->env[i], sizeof(env));
1977 /* 2. expand $N (i.e. backrefs to RewriteRule pattern) */
1978 expand_backref_inbuffer(r->pool, env, sizeof(env), briRR, '$');
1979 /* 3. expand %N (i.e. backrefs to latest RewriteCond pattern) */
1980 expand_backref_inbuffer(r->pool, env, sizeof(env), briRC, '%');
1981 /* 4. expand %{...} (i.e. variables) */
1982 expand_variables_inbuffer(r, env, sizeof(env));
1983 /* 5. expand ${...} (RewriteMap lookups) */
1984 expand_map_lookups(r, env, sizeof(env));
1985 /* and add the variable to Apache's structures */
1986 add_env_variable(r, env);
1990 * Now replace API's knowledge of the current URI:
1991 * Replace r->filename with the new URI string and split out
1992 * an on-the-fly generated QUERY_STRING part into r->args
1994 r->filename = ap_pstrdup(r->pool, newuri);
1995 splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND);
1998 * Again add the previously stripped per-directory location
1999 * prefix if the new URI is not a new one for this
2000 * location, i.e. if it's not starting with either a slash
2001 * or a fully qualified URL scheme.
2003 i = strlen(r->filename);
2005 && !( r->filename[0] == '/'
2006 || ( (i > 7 && strncasecmp(r->filename, "http://", 7) == 0)
2007 || (i > 8 && strncasecmp(r->filename, "https://", 8) == 0)
2008 || (i > 9 && strncasecmp(r->filename, "gopher://", 9) == 0)
2009 || (i > 6 && strncasecmp(r->filename, "ftp://", 6) == 0)))) {
2010 rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
2011 perdir, r->filename, perdir, r->filename);
2012 r->filename = ap_pstrcat(r->pool, perdir, r->filename, NULL);
2016 * If this rule is forced for proxy throughput
2017 * (`RewriteRule ... ... [P]') then emulate mod_proxy's
2018 * URL-to-filename handler to be sure mod_proxy is triggered
2019 * for this URL later in the Apache API. But make sure it is
2020 * a fully-qualified URL. (If not it is qualified with
2023 if (p->flags & RULEFLAG_PROXY) {
2024 fully_qualify_uri(r);
2025 if (perdir == NULL) {
2026 rewritelog(r, 2, "forcing proxy-throughput with %s", r->filename);
2029 rewritelog(r, 2, "[per-dir %s] forcing proxy-throughput with %s",
2030 perdir, r->filename);
2032 r->filename = ap_pstrcat(r->pool, "proxy:", r->filename, NULL);
2037 * If this rule is explicitly forced for HTTP redirection
2038 * (`RewriteRule .. .. [R]') then force an external HTTP
2039 * redirect. But make sure it is a fully-qualified URL. (If
2040 * not it is qualified with ourself).
2042 if (p->flags & RULEFLAG_FORCEREDIRECT) {
2043 fully_qualify_uri(r);
2044 if (perdir == NULL) {
2046 "explicitly forcing redirect with %s", r->filename);
2050 "[per-dir %s] explicitly forcing redirect with %s",
2051 perdir, r->filename);
2053 r->status = p->forced_responsecode;
2058 * Special Rewriting Feature: Self-Reduction
2059 * We reduce the URL by stripping a possible
2060 * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
2061 * corresponds to ourself. This is to simplify rewrite maps
2062 * and to avoid recursion, etc. When this prefix is not a
2063 * coincidence then the user has to use [R] explicitly (see
2069 * If this rule is still implicitly forced for HTTP
2070 * redirection (`RewriteRule .. <scheme>://...') then
2071 * directly force an external HTTP redirect.
2073 i = strlen(r->filename);
2074 if ( (i > 7 && strncasecmp(r->filename, "http://", 7) == 0)
2075 || (i > 8 && strncasecmp(r->filename, "https://", 8) == 0)
2076 || (i > 9 && strncasecmp(r->filename, "gopher://", 9) == 0)
2077 || (i > 6 && strncasecmp(r->filename, "ftp://", 6) == 0)) {
2078 if (perdir == NULL) {
2080 "implicitly forcing redirect (rc=%d) with %s",
2081 p->forced_responsecode, r->filename);
2084 rewritelog(r, 2, "[per-dir %s] implicitly forcing redirect "
2085 "(rc=%d) with %s", perdir, p->forced_responsecode,
2088 r->status = p->forced_responsecode;
2093 * Now we are sure it is not a fully qualified URL. But
2094 * there is still one special case left: A local rewrite in
2095 * per-directory context, i.e. a substitution URL which does
2096 * not start with a slash. Here we add again the initially
2097 * stripped per-directory prefix.
2099 if (prefixstrip && r->filename[0] != '/') {
2100 rewritelog(r, 3, "[per-dir %s] add per-dir prefix: %s -> %s%s",
2101 perdir, r->filename, perdir, r->filename);
2102 r->filename = ap_pstrcat(r->pool, perdir, r->filename, NULL);
2106 * Finally we had to remember if a MIME-type should be
2107 * forced for this URL (`RewriteRule .. .. [T=<type>]')
2108 * Later in the API processing phase this is forced by our
2109 * MIME API-hook function. This time its no problem even for
2110 * the per-directory context (where the MIME-type hook was
2111 * already processed) because a sub-request happens ;-)
2113 if (p->forced_mimetype != NULL) {
2114 ap_table_setn(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
2115 p->forced_mimetype);
2116 if (perdir == NULL) {
2117 rewritelog(r, 2, "remember %s to have MIME-type '%s'",
2118 r->filename, p->forced_mimetype);
2122 "[per-dir %s] remember %s to have MIME-type '%s'",
2123 perdir, r->filename, p->forced_mimetype);
2128 * Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
2129 * But now we're done for this particular rule.
2134 static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
2135 char *perdir, backrefinfo *briRR,
2138 char input[MAX_STRING_LEN];
2141 regmatch_t regmatch[MAX_NMATCH];
2145 * Construct the string we match against
2148 /* 1. take the string */
2149 ap_cpystrn(input, p->input, sizeof(input));
2150 /* 2. expand $N (i.e. backrefs to RewriteRule pattern) */
2151 expand_backref_inbuffer(r->pool, input, sizeof(input), briRR, '$');
2152 /* 3. expand %N (i.e. backrefs to latest RewriteCond pattern) */
2153 expand_backref_inbuffer(r->pool, input, sizeof(input), briRC, '%');
2154 /* 4. expand %{...} (i.e. variables) */
2155 expand_variables_inbuffer(r, input, sizeof(input));
2156 /* 5. expand ${...} (RewriteMap lookups) */
2157 expand_map_lookups(r, input, sizeof(input));
2160 * Apply the patterns
2164 if (strcmp(p->pattern, "-f") == 0) {
2165 if (stat(input, &sb) == 0) {
2166 if (S_ISREG(sb.st_mode)) {
2171 else if (strcmp(p->pattern, "-s") == 0) {
2172 if (stat(input, &sb) == 0) {
2173 if (S_ISREG(sb.st_mode) && sb.st_size > 0) {
2178 else if (strcmp(p->pattern, "-l") == 0) {
2179 #if !defined(OS2) && !defined(WIN32)
2180 if (lstat(input, &sb) == 0) {
2181 if (S_ISLNK(sb.st_mode)) {
2187 else if (strcmp(p->pattern, "-d") == 0) {
2188 if (stat(input, &sb) == 0) {
2189 if (S_ISDIR(sb.st_mode)) {
2194 else if (strcmp(p->pattern, "-U") == 0) {
2195 /* avoid infinite subrequest recursion */
2196 if (strlen(input) > 0 /* nonempty path, and */
2197 && ( r->main == NULL /* - either not in a subrequest */
2198 || ( r->main->uri != NULL /* - or in a subrequest... */
2199 && r->uri != NULL /* ...and URIs aren't NULL... */
2200 /* ...and sub/main URIs differ */
2201 && strcmp(r->main->uri, r->uri) != 0) ) ) {
2203 /* run a URI-based subrequest */
2204 rsub = ap_sub_req_lookup_uri(input, r);
2206 /* URI exists for any result up to 3xx, redirects allowed */
2207 if (rsub->status < 400)
2211 rewritelog(r, 5, "RewriteCond URI (-U) check: "
2212 "path=%s -> status=%d", input, rsub->status);
2214 /* cleanup by destroying the subrequest */
2215 ap_destroy_sub_req(rsub);
2218 else if (strcmp(p->pattern, "-F") == 0) {
2219 /* avoid infinite subrequest recursion */
2220 if (strlen(input) > 0 /* nonempty path, and */
2221 && ( r->main == NULL /* - either not in a subrequest */
2222 || ( r->main->uri != NULL /* - or in a subrequest... */
2223 && r->uri != NULL /* ...and URIs aren't NULL... */
2224 /* ...and sub/main URIs differ */
2225 && strcmp(r->main->uri, r->uri) != 0) ) ) {
2227 /* process a file-based subrequest:
2228 * this differs from -U in that no path translation is done.
2230 rsub = ap_sub_req_lookup_file(input, r);
2232 /* file exists for any result up to 2xx, no redirects */
2233 if (rsub->status < 300 &&
2234 /* double-check that file exists since default result is 200 */
2235 stat(rsub->filename, &sb) == 0) {
2240 rewritelog(r, 5, "RewriteCond file (-F) check: path=%s "
2241 "-> file=%s status=%d", input, rsub->filename,
2244 /* cleanup by destroying the subrequest */
2245 ap_destroy_sub_req(rsub);
2248 else if (strlen(p->pattern) > 1 && *(p->pattern) == '>') {
2249 rc = (compare_lexicography(input, p->pattern+1) == 1 ? 1 : 0);
2251 else if (strlen(p->pattern) > 1 && *(p->pattern) == '<') {
2252 rc = (compare_lexicography(input, p->pattern+1) == -1 ? 1 : 0);
2254 else if (strlen(p->pattern) > 1 && *(p->pattern) == '=') {
2255 if (strcmp(p->pattern+1, "\"\"") == 0) {
2256 rc = (*input == '\0');
2259 rc = (strcmp(input, p->pattern+1) == 0 ? 1 : 0);
2263 /* it is really a regexp pattern, so apply it */
2264 rc = (ap_regexec(p->regexp, input,
2265 p->regexp->re_nsub+1, regmatch,0) == 0);
2267 /* if it isn't a negated pattern and really matched
2268 we update the passed-through regex subst info structure */
2269 if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
2270 briRC->source = ap_pstrdup(r->pool, input);
2271 briRC->nsub = p->regexp->re_nsub;
2272 memcpy((void *)(briRC->regmatch), (void *)(regmatch),
2277 /* if this is a non-matching regexp, just negate the result */
2278 if (p->flags & CONDFLAG_NOTMATCH) {
2282 rewritelog(r, 4, "RewriteCond: input='%s' pattern='%s%s' => %s",
2283 input, (p->flags & CONDFLAG_NOTMATCH ? "!" : ""),
2284 p->pattern, rc ? "matched" : "not-matched");
2286 /* end just return the result */
2292 ** +-------------------------------------------------------+
2294 ** | URL transformation functions
2296 ** +-------------------------------------------------------+
2301 ** split out a QUERY_STRING part from
2302 ** the current URI string
2306 static void splitout_queryargs(request_rec *r, int qsappend)
2311 q = strchr(r->filename, '?');
2313 olduri = ap_pstrdup(r->pool, r->filename);
2316 r->args = ap_pstrcat(r->pool, q, "&", r->args, NULL);
2319 r->args = ap_pstrdup(r->pool, q);
2321 if (strlen(r->args) == 0) {
2323 rewritelog(r, 3, "split uri=%s -> uri=%s, args=<none>", olduri,
2327 if (r->args[strlen(r->args)-1] == '&') {
2328 r->args[strlen(r->args)-1] = '\0';
2330 rewritelog(r, 3, "split uri=%s -> uri=%s, args=%s", olduri,
2331 r->filename, r->args);
2340 ** strip 'http[s]://ourhost/' from URI
2344 static void reduce_uri(request_rec *r)
2347 unsigned short port;
2352 char host[LONG_STRING_LEN];
2353 char buf[MAX_STRING_LEN];
2357 cp = ap_http_method(r);
2359 if ( strlen(r->filename) > l+3
2360 && strncasecmp(r->filename, cp, l) == 0
2361 && r->filename[l] == ':'
2362 && r->filename[l+1] == '/'
2363 && r->filename[l+2] == '/' ) {
2364 /* there was really a rewrite to a remote path */
2366 olduri = ap_pstrdup(r->pool, r->filename); /* save for logging */
2368 /* cut the hostname and port out of the URI */
2369 ap_cpystrn(buf, r->filename+(l+3), sizeof(buf));
2371 for (cp = hostp; *cp != '\0' && *cp != '/' && *cp != ':'; cp++)
2376 ap_cpystrn(host, hostp, sizeof(host));
2379 for (; *cp != '\0' && *cp != '/'; cp++)
2385 /* set remaining url */
2388 else if (*cp == '/') {
2391 ap_cpystrn(host, hostp, sizeof(host));
2394 port = ap_default_port(r);
2395 /* set remaining url */
2400 ap_cpystrn(host, hostp, sizeof(host));
2402 port = ap_default_port(r);
2403 /* set remaining url */
2407 /* now check whether we could reduce it to a local path... */
2408 if (ap_matches_request_vhost(r, host, port)) {
2409 /* this is our host, so only the URL remains */
2410 r->filename = ap_pstrdup(r->pool, url);
2411 rewritelog(r, 3, "reduce %s -> %s", olduri, r->filename);
2420 ** add 'http[s]://ourhost[:ourport]/' to URI
2421 ** if URI is still not fully qualified
2425 static void fully_qualify_uri(request_rec *r)
2429 const char *thisserver;
2433 i = strlen(r->filename);
2434 if (!( (i > 7 && strncasecmp(r->filename, "http://", 7) == 0)
2435 || (i > 8 && strncasecmp(r->filename, "https://", 8) == 0)
2436 || (i > 9 && strncasecmp(r->filename, "gopher://", 9) == 0)
2437 || (i > 6 && strncasecmp(r->filename, "ftp://", 6) == 0))) {
2439 thisserver = ap_get_server_name(r);
2440 port = ap_get_server_port(r);
2441 if (ap_is_default_port(port,r)) {
2445 ap_snprintf(buf, sizeof(buf), ":%u", port);
2449 if (r->filename[0] == '/') {
2450 r->filename = ap_psprintf(r->pool, "%s://%s%s%s",
2451 ap_http_method(r), thisserver,
2452 thisport, r->filename);
2455 r->filename = ap_psprintf(r->pool, "%s://%s%s/%s",
2456 ap_http_method(r), thisserver,
2457 thisport, r->filename);
2466 ** Expand the %0-%9 or $0-$9 regex backreferences
2470 static void expand_backref_inbuffer(ap_context_t *p, char *buf, int nbuf,
2471 backrefinfo *bri, char c)
2475 if (bri->nsub < 1) {
2480 /* safe existing $N backrefs and replace <c>N with $N backrefs */
2481 for (i = 0; buf[i] != '\0' && i < nbuf; i++) {
2482 if (buf[i] == '$' && (buf[i+1] >= '0' && buf[i+1] <= '9')) {
2485 else if (buf[i] == c && (buf[i+1] >= '0' && buf[i+1] <= '9')) {
2491 /* now apply the pregsub() function */
2492 ap_cpystrn(buf, ap_pregsub(p, buf, bri->source,
2493 bri->nsub+1, bri->regmatch), nbuf);
2496 /* restore the original $N backrefs */
2497 for (i = 0; buf[i] != '\0' && i < nbuf; i++) {
2498 if (buf[i] == '\001' && (buf[i+1] >= '0' && buf[i+1] <= '9')) {
2508 ** Expand tilde-paths (/~user) through
2509 ** Unix /etc/passwd database information
2513 static char *expand_tildepaths(request_rec *r, char *uri)
2515 char user[LONG_STRING_LEN];
2521 if (uri != NULL && strlen(uri) > 2 && uri[0] == '/' && uri[1] == '~') {
2522 /* cut out the username */
2523 for (j = 0, i = 2; j < sizeof(user)-1
2525 && uri[i] != '/' ; ) {
2526 user[j++] = uri[i++];
2530 /* lookup username in systems passwd file */
2531 if ((pw = getpwnam(user)) != NULL) {
2532 /* ok, user was found, so expand the ~user string */
2533 if (uri[i] != '\0') {
2534 /* ~user/anything... has to be expanded */
2535 if (pw->pw_dir[strlen(pw->pw_dir)-1] == '/') {
2536 pw->pw_dir[strlen(pw->pw_dir)-1] = '\0';
2538 newuri = ap_pstrcat(r->pool, pw->pw_dir, uri+i, NULL);
2541 /* only ~user has to be expanded */
2542 newuri = ap_pstrdup(r->pool, pw->pw_dir);
2552 ** mapfile expansion support
2553 ** i.e. expansion of MAP lookup directives
2554 ** ${<mapname>:<key>} in RewriteRule rhs
2558 #define limit_length(n) (n > LONG_STRING_LEN-1 ? LONG_STRING_LEN-1 : n)
2560 static void expand_map_lookups(request_rec *r, char *uri, int uri_len)
2562 char newuri[MAX_STRING_LEN];
2568 char mapname[LONG_STRING_LEN];
2569 char mapkey[LONG_STRING_LEN];
2570 char defaultvalue[LONG_STRING_LEN];
2574 cpIE = cpI+strlen(cpI);
2576 while (cpI < cpIE) {
2577 if (cpI+6 < cpIE && strncmp(cpI, "${", 2) == 0) {
2578 /* missing delimiter -> take it as plain text */
2579 if ( strchr(cpI+2, ':') == NULL
2580 || strchr(cpI+2, '}') == NULL) {
2581 memcpy(cpO, cpI, 2);
2588 cpT = strchr(cpI, ':');
2590 memcpy(mapname, cpI, limit_length(n));
2591 mapname[limit_length(n)] = '\0';
2594 cpT2 = strchr(cpI, '|');
2595 cpT = strchr(cpI, '}');
2596 if (cpT2 != NULL && cpT2 < cpT) {
2598 memcpy(mapkey, cpI, limit_length(n));
2599 mapkey[limit_length(n)] = '\0';
2603 memcpy(defaultvalue, cpI, limit_length(n));
2604 defaultvalue[limit_length(n)] = '\0';
2609 memcpy(mapkey, cpI, limit_length(n));
2610 mapkey[limit_length(n)] = '\0';
2613 defaultvalue[0] = '\0';
2616 cpT = lookup_map(r, mapname, mapkey);
2619 if (cpO + n >= newuri + sizeof(newuri)) {
2620 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
2621 0, r, "insufficient space in "
2622 "expand_map_lookups, aborting");
2625 memcpy(cpO, cpT, n);
2629 n = strlen(defaultvalue);
2630 if (cpO + n >= newuri + sizeof(newuri)) {
2631 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
2632 0, r, "insufficient space in "
2633 "expand_map_lookups, aborting");
2636 memcpy(cpO, defaultvalue, n);
2641 cpT = strstr(cpI, "${");
2643 cpT = cpI+strlen(cpI);
2645 if (cpO + n >= newuri + sizeof(newuri)) {
2646 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
2647 0, r, "insufficient space in "
2648 "expand_map_lookups, aborting");
2651 memcpy(cpO, cpI, n);
2657 ap_cpystrn(uri, newuri, uri_len);
2666 ** +-------------------------------------------------------+
2668 ** | DBM hashfile support
2670 ** +-------------------------------------------------------+
2674 static char *lookup_map(request_rec *r, char *name, char *key)
2677 rewrite_server_conf *conf;
2678 ap_array_header_t *rewritemaps;
2679 rewritemap_entry *entries;
2680 rewritemap_entry *s;
2685 /* get map configuration */
2686 sconf = r->server->module_config;
2687 conf = (rewrite_server_conf *)ap_get_module_config(sconf,
2689 rewritemaps = conf->rewritemaps;
2691 entries = (rewritemap_entry *)rewritemaps->elts;
2692 for (i = 0; i < rewritemaps->nelts; i++) {
2694 if (strcmp(s->name, name) == 0) {
2695 if (s->type == MAPTYPE_TXT) {
2696 if (stat(s->checkfile, &st) == -1) {
2697 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2698 "mod_rewrite: can't access text RewriteMap "
2699 "file %s", s->checkfile);
2700 rewritelog(r, 1, "can't open RewriteMap file, "
2704 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2706 if (value == NULL) {
2707 rewritelog(r, 6, "cache lookup FAILED, forcing new "
2710 lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2711 rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2712 "-> val=%s", s->name, key, value);
2713 set_cache_string(cachep, s->name, CACHEMODE_TS,
2714 st.st_mtime, key, value);
2718 rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2719 "key=%s", s->name, key);
2720 set_cache_string(cachep, s->name, CACHEMODE_TS,
2721 st.st_mtime, key, "");
2726 rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2727 "-> val=%s", s->name, key, value);
2728 return value[0] != '\0' ? value : NULL;
2731 else if (s->type == MAPTYPE_DBM) {
2732 #ifndef NO_DBM_REWRITEMAP
2733 if (stat(s->checkfile, &st) == -1) {
2734 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
2735 "mod_rewrite: can't access DBM RewriteMap "
2736 "file %s", s->checkfile);
2737 rewritelog(r, 1, "can't open DBM RewriteMap file, "
2741 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2743 if (value == NULL) {
2745 "cache lookup FAILED, forcing new map lookup");
2747 lookup_map_dbmfile(r, s->datafile, key)) != NULL) {
2748 rewritelog(r, 5, "map lookup OK: map=%s[dbm] key=%s "
2749 "-> val=%s", s->name, key, value);
2750 set_cache_string(cachep, s->name, CACHEMODE_TS,
2751 st.st_mtime, key, value);
2755 rewritelog(r, 5, "map lookup FAILED: map=%s[dbm] "
2756 "key=%s", s->name, key);
2757 set_cache_string(cachep, s->name, CACHEMODE_TS,
2758 st.st_mtime, key, "");
2763 rewritelog(r, 5, "cache lookup OK: map=%s[dbm] key=%s "
2764 "-> val=%s", s->name, key, value);
2765 return value[0] != '\0' ? value : NULL;
2771 else if (s->type == MAPTYPE_PRG) {
2773 lookup_map_program(r, s->fpin, s->fpout, key)) != NULL) {
2774 rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2775 s->name, key, value);
2779 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2783 else if (s->type == MAPTYPE_INT) {
2784 if ((value = lookup_map_internal(r, s->func, key)) != NULL) {
2785 rewritelog(r, 5, "map lookup OK: map=%s key=%s -> val=%s",
2786 s->name, key, value);
2790 rewritelog(r, 5, "map lookup FAILED: map=%s key=%s",
2794 else if (s->type == MAPTYPE_RND) {
2795 if (stat(s->checkfile, &st) == -1) {
2796 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
2797 "mod_rewrite: can't access text RewriteMap "
2798 "file %s", s->checkfile);
2799 rewritelog(r, 1, "can't open RewriteMap file, "
2803 value = get_cache_string(cachep, s->name, CACHEMODE_TS,
2805 if (value == NULL) {
2806 rewritelog(r, 6, "cache lookup FAILED, forcing new "
2809 lookup_map_txtfile(r, s->datafile, key)) != NULL) {
2810 rewritelog(r, 5, "map lookup OK: map=%s key=%s[txt] "
2811 "-> val=%s", s->name, key, value);
2812 set_cache_string(cachep, s->name, CACHEMODE_TS,
2813 st.st_mtime, key, value);
2816 rewritelog(r, 5, "map lookup FAILED: map=%s[txt] "
2817 "key=%s", s->name, key);
2818 set_cache_string(cachep, s->name, CACHEMODE_TS,
2819 st.st_mtime, key, "");
2824 rewritelog(r, 5, "cache lookup OK: map=%s[txt] key=%s "
2825 "-> val=%s", s->name, key, value);
2827 if (value[0] != '\0') {
2828 value = select_random_value_part(r, value);
2829 rewritelog(r, 5, "randomly choosen the subvalue `%s'", value);
2841 static char *lookup_map_txtfile(request_rec *r, char *file, char *key)
2851 if ((fp = ap_pfopen(r->pool, file, "r")) == NULL) {
2855 while (fgets(line, sizeof(line), fp) != NULL) {
2857 continue; /* ignore comments */
2860 skip = strcspn(cpT," \t\r\n");
2862 continue; /* ignore lines that start with a space, tab, CR, or LF */
2865 if (strcmp(curkey, key) != 0)
2866 continue; /* key does not match... */
2868 /* found a matching key; now extract and return the value */
2870 skip = strspn(cpT, " \t\r\n");
2873 skip = strcspn(cpT, " \t\r\n");
2875 continue; /* no value... */
2878 value = ap_pstrdup(r->pool, curval);
2881 ap_pfclose(r->pool, fp);
2885 #ifndef NO_DBM_REWRITEMAP
2886 static char *lookup_map_dbmfile(request_rec *r, char *file, char *key)
2892 char buf[MAX_STRING_LEN];
2895 dbmkey.dsize = strlen(key);
2896 if ((dbmfp = dbm_open(file, O_RDONLY, 0666)) != NULL) {
2897 dbmval = dbm_fetch(dbmfp, dbmkey);
2898 if (dbmval.dptr != NULL) {
2899 memcpy(buf, dbmval.dptr,
2900 dbmval.dsize < sizeof(buf)-1 ?
2901 dbmval.dsize : sizeof(buf)-1 );
2902 buf[dbmval.dsize] = '\0';
2903 value = ap_pstrdup(r->pool, buf);
2911 static char *lookup_map_program(request_rec *r, int fpin, int fpout, char *key)
2913 char buf[LONG_STRING_LEN];
2917 struct iovec iov[2];
2920 /* when `RewriteEngine off' was used in the per-server
2921 * context then the rewritemap-programs were not spawned.
2922 * In this case using such a map (usually in per-dir context)
2923 * is useless because it is not available.
2925 if (fpin == -1 || fpout == -1) {
2930 rewritelock_alloc(r);
2932 /* write out the request key */
2934 write(fpin, key, strlen(key));
2935 write(fpin, "\n", 1);
2937 iov[0].iov_base = key;
2938 iov[0].iov_len = strlen(key);
2939 iov[1].iov_base = "\n";
2941 writev(fpin, iov, 2);
2944 /* read in the response value */
2946 while (read(fpout, &c, 1) == 1 && (i < LONG_STRING_LEN-1)) {
2954 /* give the lock back */
2955 rewritelock_free(r);
2957 if (strcasecmp(buf, "NULL") == 0) {
2961 return ap_pstrdup(r->pool, buf);
2965 static char *lookup_map_internal(request_rec *r,
2966 char *(*func)(request_rec *, char *),
2969 /* currently we just let the function convert
2970 the key to a corresponding value */
2971 return func(r, key);
2974 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
2978 for (cp = value = ap_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2980 *cp = ap_toupper(*cp);
2985 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
2989 for (cp = value = ap_pstrdup(r->pool, key); cp != NULL && *cp != '\0';
2991 *cp = ap_tolower(*cp);
2996 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
3000 value = ap_escape_uri(r->pool, key);
3004 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
3008 value = ap_pstrdup(r->pool, key);
3009 ap_unescape_url(value);
3013 static int rewrite_rand_init_done = 0;
3015 static void rewrite_rand_init(void)
3017 if (!rewrite_rand_init_done) {
3018 srand((unsigned)(getpid()));
3019 rewrite_rand_init_done = 1;
3024 static int rewrite_rand(int l, int h)
3029 rewrite_rand_init();
3030 ap_snprintf(buf, sizeof(buf), "%.0f",
3031 (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l)));
3038 static char *select_random_value_part(request_rec *r, char *value)
3043 /* count number of distinct values */
3044 for (n = 1, i = 0; value[i] != '\0'; i++) {
3045 if (value[i] == '|') {
3050 /* when only one value we have no option to choose */
3055 /* else randomly select one */
3056 k = rewrite_rand(1, n);
3058 /* and grep it out */
3059 for (n = 1, i = 0; value[i] != '\0'; i++) {
3063 if (value[i] == '|') {
3067 buf = ap_pstrdup(r->pool, &value[i]);
3068 for (i = 0; buf[i] != '\0' && buf[i] != '|'; i++)
3076 ** +-------------------------------------------------------+
3078 ** | rewriting logfile support
3080 ** +-------------------------------------------------------+
3084 static void open_rewritelog(server_rec *s, ap_context_t *p)
3086 rewrite_server_conf *conf;
3089 int rewritelog_flags = ( O_WRONLY|O_APPEND|O_CREAT );
3091 mode_t rewritelog_mode = ( _S_IREAD|_S_IWRITE );
3093 mode_t rewritelog_mode = ( S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH );
3096 conf = ap_get_module_config(s->module_config, &rewrite_module);
3098 if (conf->rewritelogfile == NULL) {
3101 if (*(conf->rewritelogfile) == '\0') {
3104 if (conf->rewritelogfp > 0) {
3105 return; /* virtual log shared w/ main server */
3108 fname = ap_server_root_relative(p, conf->rewritelogfile);
3110 if (*conf->rewritelogfile == '|') {
3111 if ((pl = ap_open_piped_log(p, conf->rewritelogfile+1)) == NULL) {
3112 ap_log_error(APLOG_MARK, APLOG_ERR, s,
3113 "mod_rewrite: could not open reliable pipe "
3114 "to RewriteLog filter %s", conf->rewritelogfile+1);
3117 conf->rewritelogfp = ap_piped_log_write_fd(pl);
3119 else if (*conf->rewritelogfile != '\0') {
3120 if ((conf->rewritelogfp = ap_popenf(p, fname, rewritelog_flags,
3121 rewritelog_mode)) < 0) {
3122 ap_log_error(APLOG_MARK, APLOG_ERR, s,
3124 "mod_rewrite: could not open RewriteLog "
3132 static void rewritelog(request_rec *r, int level, const char *text, ...)
3134 rewrite_server_conf *conf;
3148 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
3149 conn = r->connection;
3151 if (conf->rewritelogfp < 0) {
3154 if (conf->rewritelogfile == NULL) {
3157 if (*(conf->rewritelogfile) == '\0') {
3161 if (level > conf->rewriteloglevel) {
3165 if (r->user == NULL) {
3168 else if (strlen(r->user) != 0) {
3175 rhost = ap_get_remote_host(conn, r->server->module_config,
3177 if (rhost == NULL) {
3178 rhost = "UNKNOWN-HOST";
3181 str1 = ap_pstrcat(r->pool, rhost, " ",
3182 (conn->remote_logname != NULL ?
3183 conn->remote_logname : "-"), " ",
3185 ap_vsnprintf(str2, sizeof(str2), text, ap);
3187 if (r->main == NULL) {
3188 strcpy(type, "initial");
3191 strcpy(type, "subreq");
3194 for (i = 0, req = r; req->prev != NULL; req = req->prev) {
3201 ap_snprintf(redir, sizeof(redir), "/redir#%d", i);
3204 ap_snprintf(str3, sizeof(str3),
3205 "%s %s [%s/sid#%lx][rid#%lx/%s%s] (%d) %s\n", str1,
3206 current_logtime(r), ap_get_server_name(r),
3207 (unsigned long)(r->server), (unsigned long)r,
3208 type, redir, level, str2);
3210 fd_lock(r, conf->rewritelogfp);
3211 write(conf->rewritelogfp, str3, strlen(str3));
3212 fd_unlock(r, conf->rewritelogfp);
3218 static char *current_logtime(request_rec *r)
3225 t = ap_get_gmtoff(&timz);
3226 sign = (timz < 0 ? '-' : '+');
3231 strftime(tstr, 80, "[%d/%b/%Y:%H:%M:%S ", t);
3232 ap_snprintf(tstr + strlen(tstr), 80-strlen(tstr), "%c%.2d%.2d]",
3233 sign, timz/60, timz%60);
3234 return ap_pstrdup(r->pool, tstr);
3241 ** +-------------------------------------------------------+
3243 ** | rewriting lockfile support
3245 ** +-------------------------------------------------------+
3249 #define REWRITELOCK_MODE ( _S_IREAD|_S_IWRITE )
3251 #define REWRITELOCK_MODE ( S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH )
3254 static void rewritelock_create(server_rec *s, ap_context_t *p)
3256 rewrite_server_conf *conf;
3258 conf = ap_get_module_config(s->module_config, &rewrite_module);
3260 /* only operate if a lockfile is used */
3261 if (lockname == NULL || *(lockname) == '\0') {
3265 /* fixup the path, especially for rewritelock_remove() */
3266 lockname = ap_server_root_relative(p, lockname);
3268 /* create the lockfile */
3270 if ((lockfd = ap_popenf(p, lockname, O_WRONLY|O_CREAT,
3271 REWRITELOCK_MODE)) < 0) {
3272 ap_log_error(APLOG_MARK, APLOG_ERR, s,
3273 "mod_rewrite: Parent could not create RewriteLock "
3274 "file %s", lockname);
3277 #if !defined(OS2) && !defined(WIN32)
3278 /* make sure the childs have access to this file */
3279 if (geteuid() == 0 /* is superuser */)
3280 chown(lockname, ap_user_id, -1 /* no gid change */);
3286 static void rewritelock_open(server_rec *s, ap_context_t *p)
3288 rewrite_server_conf *conf;
3290 conf = ap_get_module_config(s->module_config, &rewrite_module);
3292 /* only operate if a lockfile is used */
3293 if (lockname == NULL || *(lockname) == '\0') {
3297 /* open the lockfile (once per child) to get a unique fd */
3298 if ((lockfd = ap_popenf(p, lockname, O_WRONLY,
3299 REWRITELOCK_MODE)) < 0) {
3300 ap_log_error(APLOG_MARK, APLOG_ERR, s,
3301 "mod_rewrite: Child could not open RewriteLock "
3302 "file %s", lockname);
3308 static void rewritelock_remove(void *data)
3310 /* only operate if a lockfile is used */
3311 if (lockname == NULL || *(lockname) == '\0') {
3315 /* remove the lockfile */
3321 static void rewritelock_alloc(request_rec *r)
3329 static void rewritelock_free(request_rec *r)
3332 fd_unlock(r, lockfd);
3339 ** +-------------------------------------------------------+
3341 ** | program map support
3343 ** +-------------------------------------------------------+
3346 static void run_rewritemap_programs(server_rec *s, ap_context_t *p)
3348 rewrite_server_conf *conf;
3352 ap_array_header_t *rewritemaps;
3353 rewritemap_entry *entries;
3354 rewritemap_entry *map;
3358 conf = ap_get_module_config(s->module_config, &rewrite_module);
3360 /* If the engine isn't turned on,
3361 * don't even try to do anything.
3363 if (conf->state == ENGINE_DISABLED) {
3367 rewritemaps = conf->rewritemaps;
3368 entries = (rewritemap_entry *)rewritemaps->elts;
3369 for (i = 0; i < rewritemaps->nelts; i++) {
3371 if (map->type != MAPTYPE_PRG) {
3374 if (map->datafile == NULL
3375 || *(map->datafile) == '\0'
3377 || map->fpout != -1 ) {
3382 rc = ap_spawn_child(p, rewritemap_program_child,
3383 (void *)map->datafile, kill_after_timeout,
3384 &fpin, &fpout, &fperr);
3385 if (rc == 0 || fpin == NULL || fpout == NULL) {
3386 ap_log_error(APLOG_MARK, APLOG_ERR, s,
3387 "mod_rewrite: could not fork child for "
3388 "RewriteMap process");
3391 map->fpin = fileno(fpin);
3392 map->fpout = fileno(fpout);
3393 map->fperr = fileno(fperr);
3398 /* child process code */
3399 static int rewritemap_program_child(void *cmd, child_info *pinfo)
3406 ap_cleanup_for_exec();
3408 signal(SIGHUP, SIG_IGN);
3412 * Exec() the child program
3417 char pCommand[MAX_STRING_LEN];
3419 PROCESS_INFORMATION pi;
3421 ap_snprintf(pCommand, sizeof(pCommand), "%s /C %s", SHELL_PATH, cmd);
3423 memset(&si, 0, sizeof(si));
3424 memset(&pi, 0, sizeof(pi));
3427 si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
3428 si.wShowWindow = SW_HIDE;
3429 si.hStdInput = pinfo->hPipeInputRead;
3430 si.hStdOutput = pinfo->hPipeOutputWrite;
3431 si.hStdError = pinfo->hPipeErrorWrite;
3433 if (CreateProcess(NULL, pCommand, NULL, NULL, TRUE, 0,
3434 environ, NULL, &si, &pi)) {
3435 CloseHandle(pi.hProcess);
3436 CloseHandle(pi.hThread);
3437 child_pid = pi.dwProcessId;
3442 execl(SHELL_PATH, SHELL_PATH, "/c", (char *)cmd, NULL);
3445 execl(SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
3454 ** +-------------------------------------------------------+
3456 ** | environment variable support
3458 ** +-------------------------------------------------------+
3462 static void expand_variables_inbuffer(request_rec *r, char *buf, int buf_len)
3465 newbuf = expand_variables(r, buf);
3466 if (strcmp(newbuf, buf) != 0) {
3467 ap_cpystrn(buf, newbuf, buf_len);
3472 static char *expand_variables(request_rec *r, char *str)
3474 char output[MAX_STRING_LEN];
3475 char input[MAX_STRING_LEN];
3483 ap_cpystrn(input, str, sizeof(input));
3486 endp = output + sizeof(output);
3488 for (cp = input; cp < input+MAX_STRING_LEN; ) {
3489 if ((cp2 = strstr(cp, "%{")) != NULL) {
3490 if ((cp3 = strstr(cp2, "}")) != NULL) {
3492 outp = ap_cpystrn(outp, cp, endp - outp);
3496 outp = ap_cpystrn(outp, lookup_variable(r, cp2), endp - outp);
3503 outp = ap_cpystrn(outp, cp, endp - outp);
3506 return expanded ? ap_pstrdup(r->pool, output) : str;
3509 static char *lookup_variable(request_rec *r, char *var)
3512 char resultbuf[LONG_STRING_LEN];
3525 if (strcasecmp(var, "HTTP_USER_AGENT") == 0) {
3526 result = lookup_header(r, "User-Agent");
3528 else if (strcasecmp(var, "HTTP_REFERER") == 0) {
3529 result = lookup_header(r, "Referer");
3531 else if (strcasecmp(var, "HTTP_COOKIE") == 0) {
3532 result = lookup_header(r, "Cookie");
3534 else if (strcasecmp(var, "HTTP_FORWARDED") == 0) {
3535 result = lookup_header(r, "Forwarded");
3537 else if (strcasecmp(var, "HTTP_HOST") == 0) {
3538 result = lookup_header(r, "Host");
3540 else if (strcasecmp(var, "HTTP_PROXY_CONNECTION") == 0) {
3541 result = lookup_header(r, "Proxy-Connection");
3543 else if (strcasecmp(var, "HTTP_ACCEPT") == 0) {
3544 result = lookup_header(r, "Accept");
3546 /* all other headers from which we are still not know about */
3547 else if (strlen(var) > 5 && strncasecmp(var, "HTTP:", 5) == 0) {
3548 result = lookup_header(r, var+5);
3551 /* connection stuff */
3552 else if (strcasecmp(var, "REMOTE_ADDR") == 0) {
3553 result = r->connection->remote_ip;
3555 else if (strcasecmp(var, "REMOTE_HOST") == 0) {
3556 result = (char *)ap_get_remote_host(r->connection,
3557 r->per_dir_config, REMOTE_NAME);
3559 else if (strcasecmp(var, "REMOTE_USER") == 0) {
3562 else if (strcasecmp(var, "REMOTE_IDENT") == 0) {
3563 result = (char *)ap_get_remote_logname(r);
3567 else if (strcasecmp(var, "THE_REQUEST") == 0) { /* non-standard */
3568 result = r->the_request;
3570 else if (strcasecmp(var, "REQUEST_METHOD") == 0) {
3573 else if (strcasecmp(var, "REQUEST_URI") == 0) { /* non-standard */
3576 else if (strcasecmp(var, "SCRIPT_FILENAME") == 0 ||
3577 strcasecmp(var, "REQUEST_FILENAME") == 0 ) {
3578 result = r->filename;
3580 else if (strcasecmp(var, "PATH_INFO") == 0) {
3581 result = r->path_info;
3583 else if (strcasecmp(var, "QUERY_STRING") == 0) {
3586 else if (strcasecmp(var, "AUTH_TYPE") == 0) {
3587 result = r->ap_auth_type;
3589 else if (strcasecmp(var, "IS_SUBREQ") == 0) { /* non-standard */
3590 result = (r->main != NULL ? "true" : "false");
3593 /* internal server stuff */
3594 else if (strcasecmp(var, "DOCUMENT_ROOT") == 0) {
3595 result = ap_document_root(r);
3597 else if (strcasecmp(var, "SERVER_ADMIN") == 0) {
3598 result = r->server->server_admin;
3600 else if (strcasecmp(var, "SERVER_NAME") == 0) {
3601 result = ap_get_server_name(r);
3603 else if (strcasecmp(var, "SERVER_ADDR") == 0) { /* non-standard */
3604 result = r->connection->local_ip;
3606 else if (strcasecmp(var, "SERVER_PORT") == 0) {
3607 ap_snprintf(resultbuf, sizeof(resultbuf), "%u", ap_get_server_port(r));
3610 else if (strcasecmp(var, "SERVER_PROTOCOL") == 0) {
3611 result = r->protocol;
3613 else if (strcasecmp(var, "SERVER_SOFTWARE") == 0) {
3614 result = ap_get_server_version();
3616 else if (strcasecmp(var, "API_VERSION") == 0) { /* non-standard */
3617 ap_snprintf(resultbuf, sizeof(resultbuf), "%d:%d",
3618 MODULE_MAGIC_NUMBER_MAJOR, MODULE_MAGIC_NUMBER_MINOR);
3622 /* underlaying Unix system stuff */
3623 else if (strcasecmp(var, "TIME_YEAR") == 0) {
3625 tm = localtime(&tc);
3626 ap_snprintf(resultbuf, sizeof(resultbuf), "%02d%02d",
3627 (tm->tm_year / 100) + 19, tm->tm_year % 100);
3630 #define MKTIMESTR(format, tmfield) \
3632 tm = localtime(&tc); \
3633 ap_snprintf(resultbuf, sizeof(resultbuf), format, tm->tmfield); \
3635 else if (strcasecmp(var, "TIME_MON") == 0) {
3636 MKTIMESTR("%02d", tm_mon+1)
3638 else if (strcasecmp(var, "TIME_DAY") == 0) {
3639 MKTIMESTR("%02d", tm_mday)
3641 else if (strcasecmp(var, "TIME_HOUR") == 0) {
3642 MKTIMESTR("%02d", tm_hour)
3644 else if (strcasecmp(var, "TIME_MIN") == 0) {
3645 MKTIMESTR("%02d", tm_min)
3647 else if (strcasecmp(var, "TIME_SEC") == 0) {
3648 MKTIMESTR("%02d", tm_sec)
3650 else if (strcasecmp(var, "TIME_WDAY") == 0) {
3651 MKTIMESTR("%d", tm_wday)
3653 else if (strcasecmp(var, "TIME") == 0) {
3655 tm = localtime(&tc);
3656 ap_snprintf(resultbuf, sizeof(resultbuf),
3657 "%02d%02d%02d%02d%02d%02d%02d", (tm->tm_year / 100) + 19,
3658 (tm->tm_year % 100), tm->tm_mon+1, tm->tm_mday,
3659 tm->tm_hour, tm->tm_min, tm->tm_sec);
3661 rewritelog(r, 1, "RESULT='%s'", result);
3664 /* all other env-variables from the parent Apache process */
3665 else if (strlen(var) > 4 && strncasecmp(var, "ENV:", 4) == 0) {
3666 /* first try the internal Apache notes structure */
3667 result = ap_table_get(r->notes, var+4);
3668 /* second try the internal Apache env structure */
3669 if (result == NULL) {
3670 result = ap_table_get(r->subprocess_env, var+4);
3672 /* third try the external OS env */
3673 if (result == NULL) {
3674 result = getenv(var+4);
3678 #define LOOKAHEAD(subrecfunc) \
3680 /* filename is safe to use */ \
3681 r->filename != NULL \
3682 /* - and we're either not in a subrequest */ \
3683 && ( r->main == NULL \
3684 /* - or in a subrequest where paths are non-NULL... */ \
3685 || ( r->main->uri != NULL && r->uri != NULL \
3686 /* ...and sub and main paths differ */ \
3687 && strcmp(r->main->uri, r->uri) != 0))) { \
3688 /* process a file-based subrequest */ \
3689 rsub = subrecfunc(r->filename, r); \
3690 /* now recursively lookup the variable in the sub_req */ \
3691 result = lookup_variable(rsub, var+5); \
3692 /* copy it up to our scope before we destroy sub_req's ap_context_t */ \
3693 result = ap_pstrdup(r->pool, result); \
3694 /* cleanup by destroying the subrequest */ \
3695 ap_destroy_sub_req(rsub); \
3697 rewritelog(r, 5, "lookahead: path=%s var=%s -> val=%s", \
3698 r->filename, var+5, result); \
3699 /* return ourself to prevent re-pstrdup */ \
3700 return (char *)result; \
3703 /* look-ahead for parameter through URI-based sub-request */
3704 else if (strlen(var) > 5 && strncasecmp(var, "LA-U:", 5) == 0) {
3705 LOOKAHEAD(ap_sub_req_lookup_uri)
3707 /* look-ahead for parameter through file-based sub-request */
3708 else if (strlen(var) > 5 && strncasecmp(var, "LA-F:", 5) == 0) {
3709 LOOKAHEAD(ap_sub_req_lookup_file)
3713 /* Win32 has a rather different view of file ownerships.
3714 For now, just forget it */
3717 else if (strcasecmp(var, "SCRIPT_USER") == 0) {
3718 result = "<unknown>";
3719 if (r->finfo.st_mode != 0) {
3720 if ((pw = getpwuid(r->finfo.st_uid)) != NULL) {
3721 result = pw->pw_name;
3725 if (stat(r->filename, &finfo) == 0) {
3726 if ((pw = getpwuid(finfo.st_uid)) != NULL) {
3727 result = pw->pw_name;
3732 else if (strcasecmp(var, "SCRIPT_GROUP") == 0) {
3733 result = "<unknown>";
3734 if (r->finfo.st_mode != 0) {
3735 if ((gr = getgrgid(r->finfo.st_gid)) != NULL) {
3736 result = gr->gr_name;
3740 if (stat(r->filename, &finfo) == 0) {
3741 if ((gr = getgrgid(finfo.st_gid)) != NULL) {
3742 result = gr->gr_name;
3747 #endif /* ndef WIN32 */
3749 if (result == NULL) {
3750 return ap_pstrdup(r->pool, "");
3753 return ap_pstrdup(r->pool, result);
3757 static char *lookup_header(request_rec *r, const char *name)
3759 ap_array_header_t *hdrs_arr;
3763 hdrs_arr = ap_table_elts(r->headers_in);
3764 hdrs = (table_entry *)hdrs_arr->elts;
3765 for (i = 0; i < hdrs_arr->nelts; ++i) {
3766 if (hdrs[i].key == NULL) {
3769 if (strcasecmp(hdrs[i].key, name) == 0) {
3770 ap_table_merge(r->notes, VARY_KEY_THIS, name);
3781 ** +-------------------------------------------------------+
3783 ** | caching support
3785 ** +-------------------------------------------------------+
3789 static cache *init_cache(ap_context_t *p)
3793 c = (cache *)ap_palloc(p, sizeof(cache));
3794 if (ap_create_context(&c->pool, p) != APR_SUCCESS)
3796 c->lists = ap_make_array(c->pool, 2, sizeof(cachelist));
3800 static void set_cache_string(cache *c, char *res, int mode, time_t t,
3801 char *key, char *value)
3808 store_cache_string(c, res, &ce);
3812 static char *get_cache_string(cache *c, char *res, int mode,
3813 time_t t, char *key)
3817 ce = retrieve_cache_string(c, res, key);
3821 if (mode & CACHEMODE_TS) {
3822 if (t != ce->time) {
3826 else if (mode & CACHEMODE_TTL) {
3831 return ap_pstrdup(c->pool, ce->value);
3834 static int cache_tlb_hash(char *key)
3840 for (p=key; *p != '\0'; ++p) {
3841 n = n * 53711 + 134561 + (unsigned)(*p & 0xff);
3844 return n % CACHE_TLB_ROWS;
3847 static cacheentry *cache_tlb_lookup(cachetlbentry *tlb, cacheentry *elt,
3850 int ix = cache_tlb_hash(key);
3854 for (i=0; i < CACHE_TLB_COLS; ++i) {
3858 if (strcmp(elt[j].key, key) == 0)
3864 static void cache_tlb_replace(cachetlbentry *tlb, cacheentry *elt,
3867 int ix = cache_tlb_hash(e->key);
3872 for (i=1; i < CACHE_TLB_COLS; ++i)
3873 tlb->t[i] = tlb->t[i-1];
3875 tlb->t[0] = e - elt;
3878 static void store_cache_string(cache *c, char *res, cacheentry *ce)
3888 /* first try to edit an existing entry */
3889 for (i = 0; i < c->lists->nelts; i++) {
3890 l = &(((cachelist *)c->lists->elts)[i]);
3891 if (strcmp(l->resource, res) == 0) {
3894 e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3895 (cacheentry *)l->entries->elts, ce->key);
3898 e->value = ap_pstrdup(c->pool, ce->value);
3902 for (j = 0; j < l->entries->nelts; j++) {
3903 e = &(((cacheentry *)l->entries->elts)[j]);
3904 if (strcmp(e->key, ce->key) == 0) {
3906 e->value = ap_pstrdup(c->pool, ce->value);
3907 cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3908 (cacheentry *)l->entries->elts, e);
3915 /* create a needed new list */
3917 l = ap_push_array(c->lists);
3918 l->resource = ap_pstrdup(c->pool, res);
3919 l->entries = ap_make_array(c->pool, 2, sizeof(cacheentry));
3920 l->tlb = ap_make_array(c->pool, CACHE_TLB_ROWS,
3921 sizeof(cachetlbentry));
3922 for (i=0; i<CACHE_TLB_ROWS; ++i) {
3923 t = &((cachetlbentry *)l->tlb->elts)[i];
3924 for (j=0; j<CACHE_TLB_COLS; ++j)
3929 /* create the new entry */
3930 for (i = 0; i < c->lists->nelts; i++) {
3931 l = &(((cachelist *)c->lists->elts)[i]);
3932 if (strcmp(l->resource, res) == 0) {
3933 e = ap_push_array(l->entries);
3935 e->key = ap_pstrdup(c->pool, ce->key);
3936 e->value = ap_pstrdup(c->pool, ce->value);
3937 cache_tlb_replace((cachetlbentry *)l->tlb->elts,
3938 (cacheentry *)l->entries->elts, e);
3943 /* not reached, but when it is no problem... */
3947 static cacheentry *retrieve_cache_string(cache *c, char *res, char *key)
3954 for (i = 0; i < c->lists->nelts; i++) {
3955 l = &(((cachelist *)c->lists->elts)[i]);
3956 if (strcmp(l->resource, res) == 0) {
3958 e = cache_tlb_lookup((cachetlbentry *)l->tlb->elts,
3959 (cacheentry *)l->entries->elts, key);
3963 for (j = 0; j < l->entries->nelts; j++) {
3964 e = &(((cacheentry *)l->entries->elts)[j]);
3965 if (strcmp(e->key, key) == 0) {
3978 ** +-------------------------------------------------------+
3982 ** +-------------------------------------------------------+
3985 static char *subst_prefix_path(request_rec *r, char *input, char *match,
3988 char matchbuf[LONG_STRING_LEN];
3989 char substbuf[LONG_STRING_LEN];
3995 /* first create a match string which always has a trailing slash */
3996 l = ap_cpystrn(matchbuf, match, sizeof(matchbuf)) - matchbuf;
3997 if (matchbuf[l-1] != '/') {
3999 matchbuf[l+1] = '\0';
4002 /* now compare the prefix */
4003 if (strncmp(input, matchbuf, l) == 0) {
4004 rewritelog(r, 5, "strip matching prefix: %s -> %s", output, output+l);
4005 output = ap_pstrdup(r->pool, output+l);
4007 /* and now add the base-URL as replacement prefix */
4008 l = ap_cpystrn(substbuf, subst, sizeof(substbuf)) - substbuf;
4009 if (substbuf[l-1] != '/') {
4011 substbuf[l+1] = '\0';
4014 if (output[0] == '/') {
4015 rewritelog(r, 4, "add subst prefix: %s -> %s%s",
4016 output, substbuf, output+1);
4017 output = ap_pstrcat(r->pool, substbuf, output+1, NULL);
4020 rewritelog(r, 4, "add subst prefix: %s -> %s%s",
4021 output, substbuf, output);
4022 output = ap_pstrcat(r->pool, substbuf, output, NULL);
4031 ** own command line parser which don't have the '\\' problem
4035 static int parseargline(char *str, char **a1, char **a2, char **a3)
4040 #define SKIP_WHITESPACE(cp) \
4041 for ( ; *cp == ' ' || *cp == '\t'; ) { \
4045 #define CHECK_QUOTATION(cp,isquoted) \
4052 #define DETERMINE_NEXTSTRING(cp,isquoted) \
4053 for ( ; *cp != '\0'; cp++) { \
4054 if ( (isquoted && (*cp == ' ' || *cp == '\t')) \
4055 || (*cp == '\\' && (*(cp+1) == ' ' || *(cp+1) == '\t'))) { \
4059 if ( (!isquoted && (*cp == ' ' || *cp == '\t')) \
4060 || (isquoted && *cp == '"') ) { \
4066 SKIP_WHITESPACE(cp);
4068 /* determine first argument */
4069 CHECK_QUOTATION(cp, isquoted);
4071 DETERMINE_NEXTSTRING(cp, isquoted);
4077 SKIP_WHITESPACE(cp);
4079 /* determine second argument */
4080 CHECK_QUOTATION(cp, isquoted);
4082 DETERMINE_NEXTSTRING(cp, isquoted);
4090 SKIP_WHITESPACE(cp);
4092 /* again check if there are only two arguments */
4099 /* determine second argument */
4100 CHECK_QUOTATION(cp, isquoted);
4102 DETERMINE_NEXTSTRING(cp, isquoted);
4109 static void add_env_variable(request_rec *r, char *s)
4111 char var[MAX_STRING_LEN];
4112 char val[MAX_STRING_LEN];
4116 if ((cp = strchr(s, ':')) != NULL) {
4117 n = ((cp-s) > MAX_STRING_LEN-1 ? MAX_STRING_LEN-1 : (cp-s));
4120 ap_cpystrn(val, cp+1, sizeof(val));
4121 ap_table_set(r->subprocess_env, var, val);
4122 rewritelog(r, 5, "setting env variable '%s' to '%s'", var, val);
4130 ** stat() for only the prefix of a path
4134 static int prefix_stat(const char *path, struct stat *sb)
4136 char curpath[LONG_STRING_LEN];
4139 ap_cpystrn(curpath, path, sizeof(curpath));
4140 if (curpath[0] != '/') {
4143 if ((cp = strchr(curpath+1, '/')) != NULL) {
4146 if (stat(curpath, sb) == 0) {
4162 static struct flock lock_it;
4163 static struct flock unlock_it;
4166 static void fd_lock(request_rec *r, int fd)
4171 lock_it.l_whence = SEEK_SET; /* from current point */
4172 lock_it.l_start = 0; /* -"- */
4173 lock_it.l_len = 0; /* until end of file */
4174 lock_it.l_type = F_WRLCK; /* set exclusive/write lock */
4175 lock_it.l_pid = 0; /* pid not actually interesting */
4177 while ( ((rc = fcntl(fd, F_SETLKW, &lock_it)) < 0)
4178 && (errno == EINTR) ) {
4183 while ( ((rc = flock(fd, LOCK_EX)) < 0)
4184 && (errno == EINTR) ) {
4189 /* Lock the first byte, always, assume we want to append
4190 and seek to the end afterwards */
4191 lseek(fd, 0, SEEK_SET);
4192 rc = _locking(fd, _LK_LOCK, 1);
4193 lseek(fd, 0, SEEK_END);
4197 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
4198 "mod_rewrite: failed to lock file descriptor");
4204 static void fd_unlock(request_rec *r, int fd)
4209 unlock_it.l_whence = SEEK_SET; /* from current point */
4210 unlock_it.l_start = 0; /* -"- */
4211 unlock_it.l_len = 0; /* until end of file */
4212 unlock_it.l_type = F_UNLCK; /* unlock */
4213 unlock_it.l_pid = 0; /* pid not actually interesting */
4215 rc = fcntl(fd, F_SETLKW, &unlock_it);
4218 rc = flock(fd, LOCK_UN);
4221 lseek(fd, 0, SEEK_SET);
4222 rc = _locking(fd, _LK_UNLCK, 1);
4223 lseek(fd, 0, SEEK_END);
4227 ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r,
4228 "mod_rewrite: failed to unlock file descriptor");
4235 ** Lexicographic Compare
4239 static int compare_lexicography(char *cpNum1, char *cpNum2)
4244 n1 = strlen(cpNum1);
4245 n2 = strlen(cpNum2);
4252 for (i = 0; i < n1; i++) {
4253 if (cpNum1[i] > cpNum2[i]) {
4256 if (cpNum1[i] < cpNum2[i]) {