1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
19 * | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
20 * | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
21 * |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
24 * URL Rewriting Module
26 * This module uses a rule-based rewriting engine (based on a
27 * regular-expression parser) to rewrite requested URLs on the fly.
29 * It supports an unlimited number of additional rule conditions (which can
30 * operate on a lot of variables, even on HTTP headers) for granular
31 * matching and even external database lookups (either via plain text
32 * tables, DBM hash files or even external processes) for advanced URL
35 * It operates on the full URLs (including the PATH_INFO part) both in
36 * per-server context (httpd.conf) and per-dir context (.htaccess) and even
37 * can generate QUERY_STRING parts on result. The rewriting result finally
38 * can lead to internal subprocessing, external request redirection or even
39 * to internal proxy throughput.
41 * This module was originally written in April 1996 and
42 * gifted exclusively to the The Apache Software Foundation in July 1997 by
50 #include "apr_strings.h"
54 #include "apr_signal.h"
55 #include "apr_global_mutex.h"
61 #include "apr_thread_mutex.h"
64 #define APR_WANT_MEMFUNC
65 #define APR_WANT_STRFUNC
66 #define APR_WANT_IOVEC
69 /* XXX: Do we really need these headers? */
73 #if APR_HAVE_SYS_TYPES_H
74 #include <sys/types.h>
85 #if APR_HAVE_NETINET_IN_H
86 #include <netinet/in.h>
89 #include "ap_config.h"
91 #include "http_config.h"
92 #include "http_request.h"
93 #include "http_core.h"
95 #include "http_protocol.h"
96 #include "http_vhost.h"
97 #include "util_mutex.h"
101 #include "mod_rewrite.h"
103 static ap_dbd_t *(*dbd_acquire)(request_rec*) = NULL;
104 static void (*dbd_prepare)(server_rec*, const char*, const char*) = NULL;
107 * in order to improve performance on running production systems, you
108 * may strip all rewritelog code entirely from mod_rewrite by using the
109 * -DREWRITELOG_DISABLED compiler option.
111 * DO NOT USE THIS OPTION FOR PUBLIC BINARY RELEASES. Otherwise YOU are
112 * responsible for answering all the mod_rewrite questions out there.
114 /* If logging is limited to APLOG_DEBUG or lower, disable rewrite log, too */
115 #ifdef APLOG_MAX_LOGLEVEL
116 #if APLOG_MAX_LOGLEVEL < APLOG_TRACE1
117 #ifndef REWRITELOG_DISABLED
118 #define REWRITELOG_DISABLED
123 #ifndef REWRITELOG_DISABLED
125 #define rewritelog(x) do_rewritelog x
126 #define REWRITELOG_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD )
127 #define REWRITELOG_FLAGS ( APR_WRITE | APR_APPEND | APR_CREATE )
129 #else /* !REWRITELOG_DISABLED */
131 #define rewritelog(x)
133 #endif /* REWRITELOG_DISABLED */
135 /* remembered mime-type for [T=...] */
136 #define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
137 #define REWRITE_FORCED_HANDLER_NOTEVAR "rewrite-forced-handler"
139 #define ENVVAR_SCRIPT_URL "SCRIPT_URL"
140 #define REDIRECT_ENVVAR_SCRIPT_URL "REDIRECT_" ENVVAR_SCRIPT_URL
141 #define ENVVAR_SCRIPT_URI "SCRIPT_URI"
143 #define CONDFLAG_NONE 1<<0
144 #define CONDFLAG_NOCASE 1<<1
145 #define CONDFLAG_NOTMATCH 1<<2
146 #define CONDFLAG_ORNEXT 1<<3
147 #define CONDFLAG_NOVARY 1<<4
149 #define RULEFLAG_NONE 1<<0
150 #define RULEFLAG_FORCEREDIRECT 1<<1
151 #define RULEFLAG_LASTRULE 1<<2
152 #define RULEFLAG_NEWROUND 1<<3
153 #define RULEFLAG_CHAIN 1<<4
154 #define RULEFLAG_IGNOREONSUBREQ 1<<5
155 #define RULEFLAG_NOTMATCH 1<<6
156 #define RULEFLAG_PROXY 1<<7
157 #define RULEFLAG_PASSTHROUGH 1<<8
158 #define RULEFLAG_QSAPPEND 1<<9
159 #define RULEFLAG_NOCASE 1<<10
160 #define RULEFLAG_NOESCAPE 1<<11
161 #define RULEFLAG_NOSUB 1<<12
162 #define RULEFLAG_STATUS 1<<13
163 #define RULEFLAG_ESCAPEBACKREF 1<<14
164 #define RULEFLAG_DISCARDPATHINFO 1<<15
165 #define RULEFLAG_QSDISCARD 1<<16
167 /* return code of the rewrite rule
168 * the result may be escaped - or not
170 #define ACTION_NORMAL 1<<0
171 #define ACTION_NOESCAPE 1<<1
172 #define ACTION_STATUS 1<<2
175 #define MAPTYPE_TXT 1<<0
176 #define MAPTYPE_DBM 1<<1
177 #define MAPTYPE_PRG 1<<2
178 #define MAPTYPE_INT 1<<3
179 #define MAPTYPE_RND 1<<4
180 #define MAPTYPE_DBD 1<<5
181 #define MAPTYPE_DBD_CACHE 1<<6
183 #define ENGINE_DISABLED 1<<0
184 #define ENGINE_ENABLED 1<<1
186 #define OPTION_NONE 1<<0
187 #define OPTION_INHERIT 1<<1
190 #define RAND_MAX 32767
193 /* max cookie size in rfc 2109 */
194 /* XXX: not used at all. We should do a check somewhere and/or cut the cookie */
195 #define MAX_COOKIE_LEN 4096
197 /* max line length (incl.\n) in text rewrite maps */
198 #ifndef REWRITE_MAX_TXT_MAP_LINE
199 #define REWRITE_MAX_TXT_MAP_LINE 1024
202 /* buffer length for prg rewrite maps */
203 #ifndef REWRITE_PRG_MAP_BUF
204 #define REWRITE_PRG_MAP_BUF 1024
207 /* for better readbility */
208 #define LEFT_CURLY '{'
209 #define RIGHT_CURLY '}'
212 * expansion result items on the stack to save some cycles
214 * (5 == about 2 variables like "foo%{var}bar%{var}baz")
216 #define SMALL_EXPANSION 5
219 * check that a subrequest won't cause infinite recursion
221 * either not in a subrequest, or in a subrequest
222 * and URIs aren't NULL and sub/main URIs differ
224 #define subreq_ok(r) (!r->main || \
225 (r->main->uri && r->uri && strcmp(r->main->uri, r->uri)))
229 * +-------------------------------------------------------+
231 * | Types and Structures
233 * +-------------------------------------------------------+
237 const char *datafile; /* filename for map data files */
238 const char *dbmtype; /* dbm type for dbm map data files */
239 const char *checkfile; /* filename to check for map existence */
240 const char *cachename; /* for cached maps (txt/rnd/dbm) */
241 int type; /* the type of the map */
242 apr_file_t *fpin; /* in file pointer for program maps */
243 apr_file_t *fpout; /* out file pointer for program maps */
244 apr_file_t *fperr; /* err file pointer for program maps */
245 char *(*func)(request_rec *, /* function pointer for internal maps */
247 char **argv; /* argv of the external rewrite map */
248 const char *dbdq; /* SQL SELECT statement for rewritemap */
249 const char *checkfile2; /* filename to check for map existence
250 NULL if only one file */
253 /* special pattern types for RewriteCond */
269 char *input; /* Input string of RewriteCond */
270 char *pattern; /* the RegExp pattern string */
271 ap_regex_t *regexp; /* the precompiled regexp */
272 int flags; /* Flags which control the match */
273 pattern_type ptype; /* pattern type */
276 /* single linked list for env vars and cookies */
277 typedef struct data_item {
278 struct data_item *next;
283 apr_array_header_t *rewriteconds;/* the corresponding RewriteCond entries */
284 char *pattern; /* the RegExp pattern string */
285 ap_regex_t *regexp; /* the RegExp pattern compilation */
286 char *output; /* the Substitution string */
287 int flags; /* Flags which control the substitution */
288 char *forced_mimetype; /* forced MIME type of substitution */
289 char *forced_handler; /* forced content handler of subst. */
290 int forced_responsecode; /* forced HTTP response status */
291 data_item *env; /* added environment variables */
292 data_item *cookie; /* added cookies */
293 int skip; /* number of next rules to skip */
297 int state; /* the RewriteEngine state */
298 int options; /* the RewriteOption state */
299 apr_hash_t *rewritemaps; /* the RewriteMap entries */
300 apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.) */
301 apr_array_header_t *rewriterules; /* the RewriteRule entries */
302 server_rec *server; /* the corresponding server indicator */
303 } rewrite_server_conf;
306 int state; /* the RewriteEngine state */
307 int options; /* the RewriteOption state */
308 apr_array_header_t *rewriteconds; /* the RewriteCond entries (temp.) */
309 apr_array_header_t *rewriterules; /* the RewriteRule entries */
310 char *directory; /* the directory where it applies */
311 const char *baseurl; /* the base-URL where it applies */
312 } rewrite_perdir_conf;
314 /* the (per-child) cache structures.
316 typedef struct cache {
320 apr_thread_mutex_t *lock;
324 /* cached maps contain an mtime for the whole map and live in a subpool
325 * of the cachep->pool. That makes it easy to forget them if necessary.
333 /* the regex structure for the
334 * substitution of backreferences
336 typedef struct backrefinfo {
339 ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
342 /* single linked list used for
345 typedef struct result_list {
346 struct result_list *next;
351 /* context structure for variable lookup and expansion
356 const char *vary_this;
364 * +-------------------------------------------------------+
366 * | static module data
368 * +-------------------------------------------------------+
371 /* the global module structure */
372 module AP_MODULE_DECLARE_DATA rewrite_module;
374 /* rewritemap int: handler function registry */
375 static apr_hash_t *mapfunc_hash;
378 static cache *cachep;
380 /* whether proxy module is available or not */
381 static int proxy_available;
383 /* whether random seed can be reaped */
384 static int rewrite_rand_init_done = 0;
387 static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;
388 const char *rewritemap_mutex_type = "rewrite-map";
390 /* Optional functions imported from mod_ssl when loaded: */
391 static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;
392 static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;
393 static char *escape_uri(apr_pool_t *p, const char *path);
396 * +-------------------------------------------------------+
398 * | rewriting logfile support
400 * +-------------------------------------------------------+
403 #ifndef REWRITELOG_DISABLED
404 static void do_rewritelog(request_rec *r, int level, char *perdir,
405 const char *fmt, ...)
406 __attribute__((format(printf,4,5)));
408 static void do_rewritelog(request_rec *r, int level, char *perdir,
409 const char *fmt, ...)
411 char *logline, *text;
412 const char *rhost, *rname;
417 rhost = ap_get_remote_host(r->connection, r->per_dir_config,
418 REMOTE_NOLOOKUP, NULL);
419 rname = ap_get_remote_logname(r);
421 for (redir=0, req=r; req->prev; req = req->prev) {
426 text = apr_pvsprintf(r->pool, fmt, ap);
429 logline = apr_psprintf(r->pool, "%s %s %s [%s/sid#%pp][rid#%pp/%s%s%s] "
431 rhost ? rhost : "UNKNOWN-HOST",
433 r->user ? (*r->user ? r->user : "\"\"") : "-",
434 ap_get_server_name(r),
437 r->main ? "subreq" : "initial",
438 redir ? "/redir#" : "",
439 redir ? apr_itoa(r->pool, redir) : "",
440 perdir ? "[perdir " : "",
441 perdir ? perdir : "",
445 AP_REWRITE_LOG((uintptr_t)r, level, r->main ? 0 : 1, (char *)ap_get_server_name(r), logline);
447 ap_log_rerror(APLOG_MARK, APLOG_DEBUG + level, 0, r, "%s", logline);
451 #endif /* !REWRITELOG_DISABLED */
455 * +-------------------------------------------------------+
457 * | URI and path functions
459 * +-------------------------------------------------------+
462 /* return number of chars of the scheme (incl. '://')
463 * if the URI is absolute (includes a scheme etc.)
466 * NOTE: If you add new schemes here, please have a
467 * look at escape_absolute_uri and splitout_queryargs.
468 * Not every scheme takes query strings and some schemes
469 * may be handled in a special way.
471 * XXX: we may consider a scheme registry, perhaps with
472 * appropriate escape callbacks to allow other modules
473 * to extend mod_rewrite at runtime.
475 static unsigned is_absolute_uri(char *uri)
478 if (*uri == '/' || strlen(uri) <= 5) {
485 if (!strncasecmp(uri, "jp://", 5)) { /* ajp:// */
492 if (!strncasecmp(uri, "alancer://", 10)) { /* balancer:// */
499 if (!strncasecmp(uri, "tp://", 5)) { /* ftp:// */
502 if (!strncasecmp(uri, "cgi://", 6)) { /* fcgi:// */
509 if (!strncasecmp(uri, "opher://", 8)) { /* gopher:// */
516 if (!strncasecmp(uri, "ttp://", 6)) { /* http:// */
519 else if (!strncasecmp(uri, "ttps://", 7)) { /* https:// */
526 if (!strncasecmp(uri, "dap://", 6)) { /* ldap:// */
533 if (!strncasecmp(uri, "ailto:", 6)) { /* mailto: */
540 if (!strncasecmp(uri, "ews:", 4)) { /* news: */
543 else if (!strncasecmp(uri, "ntp://", 6)) { /* nntp:// */
550 if (!strncasecmp(uri, "cgi://", 6)) { /* scgi:// */
559 static const char c2x_table[] = "0123456789abcdef";
561 static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
562 unsigned char *where)
564 #if APR_CHARSET_EBCDIC
565 what = apr_xlate_conv_byte(ap_hdrs_to_ascii, (unsigned char)what);
566 #endif /*APR_CHARSET_EBCDIC*/
568 *where++ = c2x_table[what >> 4];
569 *where++ = c2x_table[what & 0xf];
574 * Escapes a uri in a similar way as php's urlencode does.
575 * Based on ap_os_escape_path in server/util.c
577 static char *escape_uri(apr_pool_t *p, const char *path) {
578 char *copy = apr_palloc(p, 3 * strlen(path) + 3);
579 const unsigned char *s = (const unsigned char *)path;
580 unsigned char *d = (unsigned char *)copy;
584 if (apr_isalnum(c) || c == '_') {
600 * escape absolute uri, which may or may not be path oriented.
601 * So let's handle them differently.
603 static char *escape_absolute_uri(apr_pool_t *p, char *uri, unsigned scheme)
608 * NULL should indicate elsewhere, that something's wrong
610 if (!scheme || strlen(uri) < scheme) {
616 /* scheme with authority part? */
619 while (*cp && *cp != '/') {
623 /* nothing after the hostpart. ready! */
624 if (!*cp || !*++cp) {
625 return apr_pstrdup(p, uri);
628 /* remember the hostname stuff */
631 /* special thing for ldap.
632 * The parts are separated by question marks. From RFC 2255:
633 * ldapurl = scheme "://" [hostport] ["/"
634 * [dn ["?" [attributes] ["?" [scope]
635 * ["?" [filter] ["?" extensions]]]]]]
637 if (!strncasecmp(uri, "ldap", 4)) {
641 token[0] = cp = apr_pstrdup(p, cp);
642 while (*cp && c < 4) {
650 return apr_pstrcat(p, apr_pstrndup(p, uri, scheme),
651 ap_escape_uri(p, token[0]),
652 (c >= 1) ? "?" : NULL,
653 (c >= 1) ? ap_escape_uri(p, token[1]) : NULL,
654 (c >= 2) ? "?" : NULL,
655 (c >= 2) ? ap_escape_uri(p, token[2]) : NULL,
656 (c >= 3) ? "?" : NULL,
657 (c >= 3) ? ap_escape_uri(p, token[3]) : NULL,
658 (c >= 4) ? "?" : NULL,
659 (c >= 4) ? ap_escape_uri(p, token[4]) : NULL,
664 /* Nothing special here. Apply normal escaping. */
665 return apr_pstrcat(p, apr_pstrndup(p, uri, scheme),
666 ap_escape_uri(p, cp), NULL);
670 * split out a QUERY_STRING part from
671 * the current URI string
673 static void splitout_queryargs(request_rec *r, int qsappend, int qsdiscard)
677 /* don't touch, unless it's an http or mailto URL.
678 * See RFC 1738 and RFC 2368.
680 if (is_absolute_uri(r->filename)
681 && strncasecmp(r->filename, "ajp", 3)
682 && strncasecmp(r->filename, "balancer", 8)
683 && strncasecmp(r->filename, "http", 4)
684 && strncasecmp(r->filename, "mailto", 6)) {
685 r->args = NULL; /* forget the query that's still flying around */
690 r->args = NULL; /* Discard query string */
691 rewritelog((r, 2, NULL, "discarding query string"));
694 q = ap_strchr(r->filename, '?');
699 olduri = apr_pstrdup(r->pool, r->filename);
702 r->args = apr_pstrcat(r->pool, q, "&", r->args, NULL);
705 r->args = apr_pstrdup(r->pool, q);
708 len = strlen(r->args);
712 else if (r->args[len-1] == '&') {
713 r->args[len-1] = '\0';
716 rewritelog((r, 3, NULL, "split uri=%s -> uri=%s, args=%s", olduri,
717 r->filename, r->args ? r->args : "<none>"));
724 * strip 'http[s]://ourhost/' from URI
726 static void reduce_uri(request_rec *r)
731 cp = (char *)ap_http_scheme(r);
733 if ( strlen(r->filename) > l+3
734 && strncasecmp(r->filename, cp, l) == 0
735 && r->filename[l] == ':'
736 && r->filename[l+1] == '/'
737 && r->filename[l+2] == '/' ) {
740 char *portp, *host, *url, *scratch;
742 scratch = apr_pstrdup(r->pool, r->filename); /* our scratchpad */
744 /* cut the hostname and port out of the URI */
745 cp = host = scratch + l + 3; /* 3 == strlen("://") */
746 while (*cp && *cp != '/' && *cp != ':') {
750 if (*cp == ':') { /* additional port given */
753 while (*cp && *cp != '/') {
759 url = r->filename + (cp - scratch);
764 else if (*cp == '/') { /* default port */
767 port = ap_default_port(r);
768 url = r->filename + (cp - scratch);
771 port = ap_default_port(r);
775 /* now check whether we could reduce it to a local path... */
776 if (ap_matches_request_vhost(r, host, port)) {
777 rewritelog((r, 3, NULL, "reduce %s -> %s", r->filename, url));
778 r->filename = apr_pstrdup(r->pool, url);
786 * add 'http[s]://ourhost[:ourport]/' to URI
787 * if URI is still not fully qualified
789 static void fully_qualify_uri(request_rec *r)
791 if (r->method_number == M_CONNECT) {
794 else if (!is_absolute_uri(r->filename)) {
795 const char *thisserver;
799 thisserver = ap_get_server_name_for_url(r);
800 port = ap_get_server_port(r);
801 thisport = ap_is_default_port(port, r)
803 : apr_psprintf(r->pool, ":%u", port);
805 r->filename = apr_psprintf(r->pool, "%s://%s%s%s%s",
806 ap_http_scheme(r), thisserver, thisport,
807 (*r->filename == '/') ? "" : "/",
815 * stat() only the first segment of a path
817 static int prefix_stat(const char *path, apr_pool_t *pool)
819 const char *curpath = path;
825 rv = apr_filepath_root(&root, &curpath, APR_FILEPATH_TRUENAME, pool);
827 if (rv != APR_SUCCESS) {
831 /* let's recognize slashes only, the mod_rewrite semantics are opaque
834 if ((slash = ap_strchr_c(curpath, '/')) != NULL) {
835 rv = apr_filepath_merge(&statpath, root,
836 apr_pstrndup(pool, curpath,
837 (apr_size_t)(slash - curpath)),
838 APR_FILEPATH_NOTABOVEROOT |
839 APR_FILEPATH_NOTRELATIVE, pool);
842 rv = apr_filepath_merge(&statpath, root, curpath,
843 APR_FILEPATH_NOTABOVEROOT |
844 APR_FILEPATH_NOTRELATIVE, pool);
847 if (rv == APR_SUCCESS) {
850 if (apr_stat(&sb, statpath, APR_FINFO_MIN, pool) == APR_SUCCESS) {
859 * substitute the prefix path 'match' in 'input' with 'subst' (RewriteBase)
861 static char *subst_prefix_path(request_rec *r, char *input, char *match,
864 apr_size_t len = strlen(match);
866 if (len && match[len - 1] == '/') {
870 if (!strncmp(input, match, len) && input[len++] == '/') {
871 apr_size_t slen, outlen;
874 rewritelog((r, 5, NULL, "strip matching prefix: %s -> %s", input,
877 slen = strlen(subst);
878 if (slen && subst[slen - 1] != '/') {
882 outlen = strlen(input) + slen - len;
883 output = apr_palloc(r->pool, outlen + 1); /* don't forget the \0 */
885 memcpy(output, subst, slen);
886 if (slen && !output[slen-1]) {
887 output[slen-1] = '/';
889 memcpy(output+slen, input+len, outlen - slen);
890 output[outlen] = '\0';
892 rewritelog((r, 4, NULL, "add subst prefix: %s -> %s", input+len,
898 /* prefix didn't match */
904 * +-------------------------------------------------------+
908 * +-------------------------------------------------------+
911 static void set_cache_value(const char *name, apr_time_t t, char *key,
918 apr_thread_mutex_lock(cachep->lock);
920 map = apr_hash_get(cachep->maps, name, APR_HASH_KEY_STRING);
925 if (apr_pool_create(&p, cachep->pool) != APR_SUCCESS) {
927 apr_thread_mutex_unlock(cachep->lock);
932 map = apr_palloc(cachep->pool, sizeof(cachedmap));
934 map->entries = apr_hash_make(map->pool);
937 apr_hash_set(cachep->maps, name, APR_HASH_KEY_STRING, map);
939 else if (map->mtime != t) {
940 apr_pool_clear(map->pool);
941 map->entries = apr_hash_make(map->pool);
945 /* Now we should have a valid map->entries hash, where we
946 * can store our value.
948 * We need to copy the key and the value into OUR pool,
949 * so that we don't leave it during the r->pool cleanup.
951 apr_hash_set(map->entries,
952 apr_pstrdup(map->pool, key), APR_HASH_KEY_STRING,
953 apr_pstrdup(map->pool, val));
956 apr_thread_mutex_unlock(cachep->lock);
963 static char *get_cache_value(const char *name, apr_time_t t, char *key,
971 apr_thread_mutex_lock(cachep->lock);
973 map = apr_hash_get(cachep->maps, name, APR_HASH_KEY_STRING);
976 /* if this map is outdated, forget it. */
977 if (map->mtime != t) {
978 apr_pool_clear(map->pool);
979 map->entries = apr_hash_make(map->pool);
983 val = apr_hash_get(map->entries, key, APR_HASH_KEY_STRING);
985 /* copy the cached value into the supplied pool,
986 * where it belongs (r->pool usually)
988 val = apr_pstrdup(p, val);
994 apr_thread_mutex_unlock(cachep->lock);
1001 static int init_cache(apr_pool_t *p)
1003 cachep = apr_palloc(p, sizeof(cache));
1004 if (apr_pool_create(&cachep->pool, p) != APR_SUCCESS) {
1005 cachep = NULL; /* turns off cache */
1009 cachep->maps = apr_hash_make(cachep->pool);
1011 (void)apr_thread_mutex_create(&(cachep->lock), APR_THREAD_MUTEX_DEFAULT, p);
1019 * +-------------------------------------------------------+
1023 * +-------------------------------------------------------+
1027 * General Note: key is already a fresh string, created (expanded) just
1028 * for the purpose to be passed in here. So one can modify key itself.
1031 static char *rewrite_mapfunc_toupper(request_rec *r, char *key)
1035 for (p = key; *p; ++p) {
1036 *p = apr_toupper(*p);
1042 static char *rewrite_mapfunc_tolower(request_rec *r, char *key)
1044 ap_str_tolower(key);
1049 static char *rewrite_mapfunc_escape(request_rec *r, char *key)
1051 return ap_escape_uri(r->pool, key);
1054 static char *rewrite_mapfunc_unescape(request_rec *r, char *key)
1056 ap_unescape_url(key);
1061 static char *select_random_value_part(request_rec *r, char *value)
1066 /* count number of distinct values */
1067 while ((p = ap_strchr(p, '|')) != NULL) {
1073 /* initialize random generator
1075 * XXX: Probably this should be wrapped into a thread mutex,
1076 * shouldn't it? Is it worth the effort?
1078 if (!rewrite_rand_init_done) {
1079 srand((unsigned)(getpid()));
1080 rewrite_rand_init_done = 1;
1083 /* select a random subvalue */
1084 n = (int)(((double)(rand() % RAND_MAX) / RAND_MAX) * n + 1);
1086 /* extract it from the whole string */
1087 while (--n && (value = ap_strchr(value, '|')) != NULL) {
1091 if (value) { /* should not be NULL, but ... */
1092 p = ap_strchr(value, '|');
1102 /* child process code */
1103 static void rewrite_child_errfn(apr_pool_t *p, apr_status_t err,
1106 ap_log_error(APLOG_MARK, APLOG_ERR, err, NULL, "%s", desc);
1109 static apr_status_t rewritemap_program_child(apr_pool_t *p,
1110 const char *progname, char **argv,
1115 apr_procattr_t *procattr;
1116 apr_proc_t *procnew;
1118 if ( APR_SUCCESS == (rc=apr_procattr_create(&procattr, p))
1119 && APR_SUCCESS == (rc=apr_procattr_io_set(procattr, APR_FULL_BLOCK,
1120 APR_FULL_BLOCK, APR_NO_PIPE))
1121 && APR_SUCCESS == (rc=apr_procattr_dir_set(procattr,
1122 ap_make_dirstr_parent(p, argv[0])))
1123 && APR_SUCCESS == (rc=apr_procattr_cmdtype_set(procattr, APR_PROGRAM))
1124 && APR_SUCCESS == (rc=apr_procattr_child_errfn_set(procattr,
1125 rewrite_child_errfn))
1126 && APR_SUCCESS == (rc=apr_procattr_error_check_set(procattr, 1))) {
1128 procnew = apr_pcalloc(p, sizeof(*procnew));
1129 rc = apr_proc_create(procnew, argv[0], (const char **)argv, NULL,
1132 if (rc == APR_SUCCESS) {
1133 apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
1136 (*fpin) = procnew->in;
1140 (*fpout) = procnew->out;
1148 static apr_status_t run_rewritemap_programs(server_rec *s, apr_pool_t *p)
1150 rewrite_server_conf *conf;
1151 apr_hash_index_t *hi;
1154 conf = ap_get_module_config(s->module_config, &rewrite_module);
1156 /* If the engine isn't turned on,
1157 * don't even try to do anything.
1159 if (conf->state == ENGINE_DISABLED) {
1163 for (hi = apr_hash_first(p, conf->rewritemaps); hi; hi = apr_hash_next(hi)){
1164 apr_file_t *fpin = NULL;
1165 apr_file_t *fpout = NULL;
1166 rewritemap_entry *map;
1169 apr_hash_this(hi, NULL, NULL, &val);
1172 if (map->type != MAPTYPE_PRG) {
1175 if (!(map->argv[0]) || !*(map->argv[0]) || map->fpin || map->fpout) {
1179 rc = rewritemap_program_child(p, map->argv[0], map->argv,
1181 if (rc != APR_SUCCESS || fpin == NULL || fpout == NULL) {
1182 ap_log_error(APLOG_MARK, APLOG_ERR, rc, s,
1183 "mod_rewrite: could not start RewriteMap "
1184 "program %s", map->checkfile);
1196 * +-------------------------------------------------------+
1198 * | Lookup functions
1200 * +-------------------------------------------------------+
1203 static char *lookup_map_txtfile(request_rec *r, const char *file, char *key)
1205 apr_file_t *fp = NULL;
1206 char line[REWRITE_MAX_TXT_MAP_LINE + 1]; /* +1 for \0 */
1207 char *value, *keylast;
1210 if ((rv = apr_file_open(&fp, file, APR_READ|APR_BUFFERED, APR_OS_DEFAULT,
1211 r->pool)) != APR_SUCCESS)
1213 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1214 "mod_rewrite: can't open text RewriteMap file %s", file);
1218 keylast = key + strlen(key);
1220 while (apr_file_gets(line, sizeof(line), fp) == APR_SUCCESS) {
1223 /* ignore comments and lines starting with whitespaces */
1224 if (*line == '#' || apr_isspace(*line)) {
1230 while (c < keylast && *p == *c && !apr_isspace(*p)) {
1235 /* key doesn't match - ignore. */
1236 if (c != keylast || !apr_isspace(*p)) {
1240 /* jump to the value */
1241 while (*p && apr_isspace(*p)) {
1245 /* no value? ignore */
1250 /* extract the value and return. */
1252 while (*p && !apr_isspace(*p)) {
1255 value = apr_pstrmemdup(r->pool, c, p - c);
1263 static char *lookup_map_dbmfile(request_rec *r, const char *file,
1264 const char *dbmtype, char *key)
1266 apr_dbm_t *dbmfp = NULL;
1272 if ((rv = apr_dbm_open_ex(&dbmfp, dbmtype, file, APR_DBM_READONLY,
1273 APR_OS_DEFAULT, r->pool)) != APR_SUCCESS)
1275 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1276 "mod_rewrite: can't open DBM RewriteMap %s", file);
1281 dbmkey.dsize = strlen(key);
1283 if (apr_dbm_fetch(dbmfp, dbmkey, &dbmval) == APR_SUCCESS && dbmval.dptr) {
1284 value = apr_pstrmemdup(r->pool, dbmval.dptr, dbmval.dsize);
1290 apr_dbm_close(dbmfp);
1294 static char *lookup_map_dbd(request_rec *r, char *key, const char *label)
1297 apr_dbd_prepared_t *stmt;
1299 apr_dbd_results_t *res = NULL;
1300 apr_dbd_row_t *row = NULL;
1301 const char *ret = NULL;
1303 ap_dbd_t *db = dbd_acquire(r);
1305 stmt = apr_hash_get(db->prepared, label, APR_HASH_KEY_STRING);
1307 rv = apr_dbd_pvselect(db->driver, r->pool, db->handle, &res,
1308 stmt, 0, key, NULL);
1310 errmsg = apr_dbd_error(db->driver, db->handle, rv);
1311 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1312 "rewritemap: error %s querying for %s", errmsg, key);
1315 while (rv = apr_dbd_get_row(db->driver, r->pool, res, &row, -1), rv == 0) {
1318 ret = apr_dbd_get_entry(db->driver, row, 0);
1321 /* randomise crudely amongst multiple results */
1322 if ((double)rand() < (double)RAND_MAX/(double)n) {
1323 ret = apr_dbd_get_entry(db->driver, row, 0);
1328 errmsg = apr_dbd_error(db->driver, db->handle, rv);
1329 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
1330 "rewritemap: error %s looking up %s", errmsg, key);
1336 return apr_pstrdup(r->pool, ret);
1338 /* what's a fair rewritelog level for this? */
1339 rewritelog((r, 3, NULL, "Multiple values found for %s", key));
1340 return apr_pstrdup(r->pool, ret);
1344 static char *lookup_map_program(request_rec *r, apr_file_t *fpin,
1345 apr_file_t *fpout, char *key)
1349 apr_size_t i, nbytes, combined_len = 0;
1351 const char *eol = APR_EOL_STR;
1352 apr_size_t eolc = 0;
1354 result_list *buflist = NULL, *curbuf = NULL;
1357 struct iovec iova[2];
1361 /* when `RewriteEngine off' was used in the per-server
1362 * context then the rewritemap-programs were not spawned.
1363 * In this case using such a map (usually in per-dir context)
1364 * is useless because it is not available.
1366 * newlines in the key leave bytes in the pipe and cause
1367 * bad things to happen (next map lookup will use the chars
1368 * after the \n instead of the new key etc etc - in other words,
1369 * the Rewritemap falls out of sync with the requests).
1371 if (fpin == NULL || fpout == NULL || ap_strchr(key, '\n')) {
1376 if (rewrite_mapr_lock_acquire) {
1377 rv = apr_global_mutex_lock(rewrite_mapr_lock_acquire);
1378 if (rv != APR_SUCCESS) {
1379 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1380 "apr_global_mutex_lock(rewrite_mapr_lock_acquire) "
1382 return NULL; /* Maybe this should be fatal? */
1386 /* write out the request key */
1388 nbytes = strlen(key);
1389 apr_file_write(fpin, key, &nbytes);
1391 apr_file_write(fpin, "\n", &nbytes);
1393 iova[0].iov_base = key;
1394 iova[0].iov_len = strlen(key);
1395 iova[1].iov_base = "\n";
1396 iova[1].iov_len = 1;
1399 apr_file_writev(fpin, iova, niov, &nbytes);
1402 buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF + 1);
1404 /* read in the response value */
1406 apr_file_read(fpout, &c, &nbytes);
1409 while (nbytes == 1 && (i < REWRITE_PRG_MAP_BUF)) {
1410 if (c == eol[eolc]) {
1412 /* remove eol from the buffer */
1415 curbuf->len -= eolc-i;
1426 /* only partial (invalid) eol sequence -> reset the counter */
1431 /* catch binary mode, e.g. on Win32 */
1432 else if (c == '\n') {
1438 apr_file_read(fpout, &c, &nbytes);
1441 /* well, if there wasn't a newline yet, we need to read further */
1442 if (buflist || (nbytes == 1 && !found_nl)) {
1444 curbuf = buflist = apr_palloc(r->pool, sizeof(*buflist));
1447 curbuf->next = apr_palloc(r->pool, sizeof(*buflist));
1448 curbuf = curbuf->next;
1451 curbuf->next = NULL;
1454 curbuf->string = buf;
1457 buf = apr_palloc(r->pool, REWRITE_PRG_MAP_BUF);
1460 if (nbytes == 1 && !found_nl) {
1469 /* concat the stuff */
1473 p = buf = apr_palloc(r->pool, combined_len + 1); /* \0 */
1476 memcpy(p, buflist->string, buflist->len);
1479 buflist = buflist->next;
1488 /* give the lock back */
1489 if (rewrite_mapr_lock_acquire) {
1490 rv = apr_global_mutex_unlock(rewrite_mapr_lock_acquire);
1491 if (rv != APR_SUCCESS) {
1492 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1493 "apr_global_mutex_unlock(rewrite_mapr_lock_acquire) "
1495 return NULL; /* Maybe this should be fatal? */
1499 /* catch the "failed" case */
1500 if (i == 4 && !strcasecmp(buf, "NULL")) {
1508 * generic map lookup
1510 static char *lookup_map(request_rec *r, char *name, char *key)
1512 rewrite_server_conf *conf;
1513 rewritemap_entry *s;
1518 /* get map configuration */
1519 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
1520 s = apr_hash_get(conf->rewritemaps, name, APR_HASH_KEY_STRING);
1522 /* map doesn't exist */
1529 * Text file map (perhaps random)
1533 rv = apr_stat(&st, s->checkfile, APR_FINFO_MIN, r->pool);
1534 if (rv != APR_SUCCESS) {
1535 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1536 "mod_rewrite: can't access text RewriteMap file %s",
1541 value = get_cache_value(s->cachename, st.mtime, key, r->pool);
1543 rewritelog((r, 6, NULL,
1544 "cache lookup FAILED, forcing new map lookup"));
1546 value = lookup_map_txtfile(r, s->datafile, key);
1548 rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[txt] key=%s",
1550 set_cache_value(s->cachename, st.mtime, key, "");
1554 rewritelog((r, 5, NULL,"map lookup OK: map=%s[txt] key=%s -> val=%s",
1556 set_cache_value(s->cachename, st.mtime, key, value);
1559 rewritelog((r,5,NULL,"cache lookup OK: map=%s[txt] key=%s -> val=%s",
1563 if (s->type == MAPTYPE_RND && *value) {
1564 value = select_random_value_part(r, value);
1565 rewritelog((r, 5, NULL, "randomly chosen the subvalue `%s'",value));
1568 return *value ? value : NULL;
1574 rv = apr_stat(&st, s->checkfile, APR_FINFO_MIN, r->pool);
1575 if (rv != APR_SUCCESS) {
1576 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1577 "mod_rewrite: can't access DBM RewriteMap file %s",
1580 else if(s->checkfile2 != NULL) {
1583 rv = apr_stat(&st2, s->checkfile2, APR_FINFO_MIN, r->pool);
1584 if (rv != APR_SUCCESS) {
1585 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
1586 "mod_rewrite: can't access DBM RewriteMap "
1587 "file %s", s->checkfile2);
1589 else if(st2.mtime > st.mtime) {
1590 st.mtime = st2.mtime;
1593 if(rv != APR_SUCCESS) {
1597 value = get_cache_value(s->cachename, st.mtime, key, r->pool);
1599 rewritelog((r, 6, NULL,
1600 "cache lookup FAILED, forcing new map lookup"));
1602 value = lookup_map_dbmfile(r, s->datafile, s->dbmtype, key);
1604 rewritelog((r, 5, NULL, "map lookup FAILED: map=%s[dbm] key=%s",
1606 set_cache_value(s->cachename, st.mtime, key, "");
1610 rewritelog((r, 5, NULL, "map lookup OK: map=%s[dbm] key=%s -> "
1611 "val=%s", name, key, value));
1613 set_cache_value(s->cachename, st.mtime, key, value);
1617 rewritelog((r, 5, NULL, "cache lookup OK: map=%s[dbm] key=%s -> val=%s",
1619 return *value ? value : NULL;
1622 * SQL map without cache
1625 value = lookup_map_dbd(r, key, s->dbdq);
1627 rewritelog((r, 5, NULL, "SQL map lookup FAILED: map %s key=%s",
1632 rewritelog((r, 5, NULL, "SQL map lookup OK: map %s key=%s, val=%s",
1638 * SQL map with cache
1640 case MAPTYPE_DBD_CACHE:
1641 value = get_cache_value(s->cachename, 0, key, r->pool);
1643 rewritelog((r, 6, NULL,
1644 "cache lookup FAILED, forcing new map lookup"));
1646 value = lookup_map_dbd(r, key, s->dbdq);
1648 rewritelog((r, 5, NULL, "SQL map lookup FAILED: map %s key=%s",
1650 set_cache_value(s->cachename, 0, key, "");
1654 rewritelog((r, 5, NULL, "SQL map lookup OK: map %s key=%s, val=%s",
1657 set_cache_value(s->cachename, 0, key, value);
1661 rewritelog((r, 5, NULL, "cache lookup OK: map=%s[SQL] key=%s, val=%s",
1663 return *value ? value : NULL;
1669 value = lookup_map_program(r, s->fpin, s->fpout, key);
1671 rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
1676 rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
1684 value = s->func(r, key);
1686 rewritelog((r, 5,NULL,"map lookup FAILED: map=%s key=%s", name,
1691 rewritelog((r, 5, NULL, "map lookup OK: map=%s key=%s -> val=%s",
1700 * lookup a HTTP header and set VARY note
1702 static const char *lookup_header(const char *name, rewrite_ctx *ctx)
1704 const char *val = apr_table_get(ctx->r->headers_in, name);
1707 ctx->vary_this = ctx->vary_this
1708 ? apr_pstrcat(ctx->r->pool, ctx->vary_this, ", ",
1710 : apr_pstrdup(ctx->r->pool, name);
1717 * lookahead helper function
1718 * Determine the correct URI path in perdir context
1720 static APR_INLINE const char *la_u(rewrite_ctx *ctx)
1722 rewrite_perdir_conf *conf;
1724 if (*ctx->uri == '/') {
1728 conf = ap_get_module_config(ctx->r->per_dir_config, &rewrite_module);
1730 return apr_pstrcat(ctx->r->pool, conf->baseurl
1731 ? conf->baseurl : conf->directory,
1736 * generic variable lookup
1738 static char *lookup_variable(char *var, rewrite_ctx *ctx)
1741 request_rec *r = ctx->r;
1742 apr_size_t varlen = strlen(var);
1746 return apr_pstrdup(r->pool, "");
1751 /* fast tests for variable length variables (sic) first */
1752 if (var[3] == ':') {
1753 if (var[4] && !strncasecmp(var, "ENV", 3)) {
1755 result = apr_table_get(r->notes, var);
1758 result = apr_table_get(r->subprocess_env, var);
1761 result = getenv(var);
1764 else if (var[4] && !strncasecmp(var, "SSL", 3) && rewrite_ssl_lookup) {
1765 result = rewrite_ssl_lookup(r->pool, r->server, r->connection, r,
1769 else if (var[4] == ':') {
1774 if (!strncasecmp(var, "HTTP", 4)) {
1775 result = lookup_header(var+5, ctx);
1777 else if (!strncasecmp(var, "LA-U", 4)) {
1778 if (ctx->uri && subreq_ok(r)) {
1779 path = ctx->perdir ? la_u(ctx) : ctx->uri;
1780 rr = ap_sub_req_lookup_uri(path, r, NULL);
1782 result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
1784 ap_destroy_sub_req(rr);
1786 rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
1787 "-> val=%s", path, var+5, result));
1789 return (char *)result;
1792 else if (!strncasecmp(var, "LA-F", 4)) {
1793 if (ctx->uri && subreq_ok(r)) {
1795 if (ctx->perdir && *path == '/') {
1796 /* sigh, the user wants a file based subrequest, but
1797 * we can't do one, since we don't know what the file
1798 * path is! In this case behave like LA-U.
1800 rr = ap_sub_req_lookup_uri(path, r, NULL);
1804 rewrite_perdir_conf *conf;
1806 conf = ap_get_module_config(r->per_dir_config,
1809 path = apr_pstrcat(r->pool, conf->directory, path,
1813 rr = ap_sub_req_lookup_file(path, r, NULL);
1817 result = apr_pstrdup(r->pool, lookup_variable(var+5, ctx));
1819 ap_destroy_sub_req(rr);
1821 rewritelog((r, 5, ctx->perdir, "lookahead: path=%s var=%s "
1822 "-> val=%s", path, var+5, result));
1824 return (char *)result;
1830 /* well, do it the hard way */
1835 /* can't do this above, because of the getenv call */
1836 for (p = var; *p; ++p) {
1837 *p = apr_toupper(*p);
1842 if (!strcmp(var, "TIME")) {
1843 apr_time_exp_lt(&tm, apr_time_now());
1844 result = apr_psprintf(r->pool, "%04d%02d%02d%02d%02d%02d",
1845 tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
1846 tm.tm_hour, tm.tm_min, tm.tm_sec);
1847 rewritelog((r, 1, ctx->perdir, "RESULT='%s'", result));
1848 return (char *)result;
1850 else if (!strcmp(var, "IPV6")) {
1853 apr_sockaddr_t *addr = r->connection->remote_addr;
1854 flag = (addr->family == AF_INET6 &&
1855 !IN6_IS_ADDR_V4MAPPED((struct in6_addr *)addr->ipaddr_ptr));
1856 rewritelog((r, 1, ctx->perdir, "IPV6='%s'", flag ? "on" : "off"));
1858 rewritelog((r, 1, ctx->perdir, "IPV6='off' (IPv6 is not enabled)"));
1860 result = (flag ? "on" : "off");
1865 if (!strcmp(var, "HTTPS")) {
1866 int flag = rewrite_is_https && rewrite_is_https(r->connection);
1867 return apr_pstrdup(r->pool, flag ? "on" : "off");
1874 if (!strcmp(var, "TIME_DAY")) {
1875 apr_time_exp_lt(&tm, apr_time_now());
1876 return apr_psprintf(r->pool, "%02d", tm.tm_mday);
1881 if (!strcmp(var, "TIME_SEC")) {
1882 apr_time_exp_lt(&tm, apr_time_now());
1883 return apr_psprintf(r->pool, "%02d", tm.tm_sec);
1888 if (!strcmp(var, "TIME_MIN")) {
1889 apr_time_exp_lt(&tm, apr_time_now());
1890 return apr_psprintf(r->pool, "%02d", tm.tm_min);
1895 if (!strcmp(var, "TIME_MON")) {
1896 apr_time_exp_lt(&tm, apr_time_now());
1897 return apr_psprintf(r->pool, "%02d", tm.tm_mon+1);
1906 if (var[8] == 'Y' && !strcmp(var, "TIME_WDAY")) {
1907 apr_time_exp_lt(&tm, apr_time_now());
1908 return apr_psprintf(r->pool, "%d", tm.tm_wday);
1910 else if (!strcmp(var, "TIME_YEAR")) {
1911 apr_time_exp_lt(&tm, apr_time_now());
1912 return apr_psprintf(r->pool, "%04d", tm.tm_year+1900);
1917 if (!strcmp(var, "IS_SUBREQ")) {
1918 result = (r->main ? "true" : "false");
1923 if (!strcmp(var, "PATH_INFO")) {
1924 result = r->path_info;
1929 if (!strcmp(var, "AUTH_TYPE")) {
1930 result = r->ap_auth_type;
1935 if (!strcmp(var, "HTTP_HOST")) {
1936 result = lookup_header("Host", ctx);
1941 if (!strcmp(var, "TIME_HOUR")) {
1942 apr_time_exp_lt(&tm, apr_time_now());
1943 return apr_psprintf(r->pool, "%02d", tm.tm_hour);
1952 if (!strcmp(var, "SERVER_NAME")) {
1953 result = ap_get_server_name(r);
1958 if (*var == 'R' && !strcmp(var, "REMOTE_ADDR")) {
1959 result = r->connection->remote_ip;
1961 else if (!strcmp(var, "SERVER_ADDR")) {
1962 result = r->connection->local_ip;
1967 if (*var == 'H' && !strcmp(var, "HTTP_ACCEPT")) {
1968 result = lookup_header("Accept", ctx);
1970 else if (!strcmp(var, "THE_REQUEST")) {
1971 result = r->the_request;
1976 if (!strcmp(var, "API_VERSION")) {
1977 return apr_psprintf(r->pool, "%d:%d",
1978 MODULE_MAGIC_NUMBER_MAJOR,
1979 MODULE_MAGIC_NUMBER_MINOR);
1984 if (!strcmp(var, "HTTP_COOKIE")) {
1985 result = lookup_header("Cookie", ctx);
1990 if (*var == 'S' && !strcmp(var, "SERVER_PORT")) {
1991 return apr_psprintf(r->pool, "%u", ap_get_server_port(r));
1993 else if (var[7] == 'H' && !strcmp(var, "REMOTE_HOST")) {
1994 result = ap_get_remote_host(r->connection,r->per_dir_config,
1997 else if (!strcmp(var, "REMOTE_PORT")) {
1998 return apr_itoa(r->pool, r->connection->remote_addr->port);
2003 if (*var == 'R' && !strcmp(var, "REMOTE_USER")) {
2006 else if (!strcmp(var, "SCRIPT_USER")) {
2007 result = "<unknown>";
2008 if (r->finfo.valid & APR_FINFO_USER) {
2009 apr_uid_name_get((char **)&result, r->finfo.user,
2016 if (!strcmp(var, "REQUEST_URI")) {
2026 if (!strcmp(var, "SCRIPT_GROUP")) {
2027 result = "<unknown>";
2028 if (r->finfo.valid & APR_FINFO_GROUP) {
2029 apr_gid_name_get((char **)&result, r->finfo.group,
2036 if (!strcmp(var, "REMOTE_IDENT")) {
2037 result = ap_get_remote_logname(r);
2042 if (!strcmp(var, "HTTP_REFERER")) {
2043 result = lookup_header("Referer", ctx);
2048 if (!strcmp(var, "QUERY_STRING")) {
2054 if (!strcmp(var, "SERVER_ADMIN")) {
2055 result = r->server->server_admin;
2062 if (!strcmp(var, "DOCUMENT_ROOT")) {
2063 result = ap_document_root(r);
2068 if (*var == 'H' && !strcmp(var, "HTTP_FORWARDED")) {
2069 result = lookup_header("Forwarded", ctx);
2071 else if (!strcmp(var, "REQUEST_METHOD")) {
2079 if (!strcmp(var, "HTTP_USER_AGENT")) {
2080 result = lookup_header("User-Agent", ctx);
2085 if (!strcmp(var, "SCRIPT_FILENAME")) {
2086 result = r->filename; /* same as request_filename (16) */
2091 if (!strcmp(var, "SERVER_PROTOCOL")) {
2092 result = r->protocol;
2097 if (!strcmp(var, "SERVER_SOFTWARE")) {
2098 result = ap_get_server_banner();
2105 if (!strcmp(var, "REQUEST_FILENAME")) {
2106 result = r->filename; /* same as script_filename (15) */
2111 if (!strcmp(var, "HTTP_PROXY_CONNECTION")) {
2112 result = lookup_header("Proxy-Connection", ctx);
2118 return apr_pstrdup(r->pool, result ? result : "");
2123 * +-------------------------------------------------------+
2125 * | Expansion functions
2127 * +-------------------------------------------------------+
2131 * Bracketed expression handling
2132 * s points after the opening bracket
2134 static APR_INLINE char *find_closing_curly(char *s)
2138 for (depth = 1; *s; ++s) {
2139 if (*s == RIGHT_CURLY && --depth == 0) {
2142 else if (*s == LEFT_CURLY) {
2150 static APR_INLINE char *find_char_in_curlies(char *s, int c)
2154 for (depth = 1; *s; ++s) {
2155 if (*s == c && depth == 1) {
2158 else if (*s == RIGHT_CURLY && --depth == 0) {
2161 else if (*s == LEFT_CURLY) {
2169 /* perform all the expansions on the input string
2170 * putting the result into a new string
2172 * for security reasons this expansion must be performed in a
2173 * single pass, otherwise an attacker can arrange for the result
2174 * of an earlier expansion to include expansion specifiers that
2175 * are interpreted by a later expansion, producing results that
2176 * were not intended by the administrator.
2178 static char *do_expand(char *input, rewrite_ctx *ctx, rewriterule_entry *entry)
2180 result_list *result, *current;
2181 result_list sresult[SMALL_EXPANSION];
2183 apr_size_t span, inputlen, outlen;
2185 apr_pool_t *pool = ctx->r->pool;
2187 span = strcspn(input, "\\$%");
2188 inputlen = strlen(input);
2191 if (inputlen == span) {
2192 return apr_pstrdup(pool, input);
2195 /* well, actually something to do */
2196 result = current = &(sresult[spc++]);
2199 current->next = NULL;
2200 current->string = input;
2201 current->len = span;
2204 /* loop for specials */
2206 /* prepare next entry */
2208 current->next = (spc < SMALL_EXPANSION)
2210 : (result_list *)apr_palloc(pool,
2211 sizeof(result_list));
2212 current = current->next;
2213 current->next = NULL;
2217 /* escaped character */
2222 current->string = p;
2226 current->string = ++p;
2231 /* variable or map lookup */
2232 else if (p[1] == '{') {
2235 endp = find_closing_curly(p+2);
2238 current->string = p;
2243 /* variable lookup */
2244 else if (*p == '%') {
2245 p = lookup_variable(apr_pstrmemdup(pool, p+2, endp-p-2), ctx);
2248 current->len = span;
2249 current->string = p;
2255 else { /* *p == '$' */
2259 * To make rewrite maps useful, the lookup key and
2260 * default values must be expanded, so we make
2261 * recursive calls to do the work. For security
2262 * reasons we must never expand a string that includes
2263 * verbatim data from the network. The recursion here
2264 * isn't a problem because the result of expansion is
2265 * only passed to lookup_map() so it cannot be
2266 * re-expanded, only re-looked-up. Another way of
2267 * looking at it is that the recursion is entirely
2268 * driven by the syntax of the nested curly brackets.
2271 key = find_char_in_curlies(p+2, ':');
2274 current->string = p;
2281 map = apr_pstrmemdup(pool, p+2, endp-p-2);
2282 key = map + (key-p-2);
2284 dflt = find_char_in_curlies(key, '|');
2289 /* reuse of key variable as result */
2290 key = lookup_map(ctx->r, map, do_expand(key, ctx, entry));
2292 if (!key && dflt && *dflt) {
2293 key = do_expand(dflt, ctx, entry);
2298 current->len = span;
2299 current->string = key;
2309 else if (apr_isdigit(p[1])) {
2311 backrefinfo *bri = (*p == '$') ? &ctx->briRR : &ctx->briRC;
2313 /* see ap_pregsub() in server/util.c */
2314 if (bri->source && n < AP_MAX_REG_MATCH
2315 && bri->regmatch[n].rm_eo > bri->regmatch[n].rm_so) {
2316 span = bri->regmatch[n].rm_eo - bri->regmatch[n].rm_so;
2317 if (entry && (entry->flags & RULEFLAG_ESCAPEBACKREF)) {
2318 /* escape the backreference */
2320 tmp = apr_pstrmemdup(pool, bri->source + bri->regmatch[n].rm_so, span);
2321 tmp2 = escape_uri(pool, tmp);
2322 rewritelog((ctx->r, 5, ctx->perdir, "escaping backreference '%s' to '%s'",
2325 current->len = span = strlen(tmp2);
2326 current->string = tmp2;
2328 current->len = span;
2329 current->string = bri->source + bri->regmatch[n].rm_so;
2338 /* not for us, just copy it */
2341 current->string = p++;
2345 /* check the remainder */
2346 if (*p && (span = strcspn(p, "\\$%")) > 0) {
2348 current->next = (spc < SMALL_EXPANSION)
2350 : (result_list *)apr_palloc(pool,
2351 sizeof(result_list));
2352 current = current->next;
2353 current->next = NULL;
2356 current->len = span;
2357 current->string = p;
2362 } while (p < input+inputlen);
2364 /* assemble result */
2365 c = p = apr_palloc(pool, outlen + 1); /* don't forget the \0 */
2368 ap_assert(c+result->len <= p+outlen); /* XXX: can be removed after
2369 * extensive testing and
2372 memcpy(c, result->string, result->len);
2375 result = result->next;
2384 * perform all the expansions on the environment variables
2386 static void do_expand_env(data_item *env, rewrite_ctx *ctx)
2391 name = do_expand(env->data, ctx, NULL);
2392 if ((val = ap_strchr(name, ':')) != NULL) {
2398 apr_table_set(ctx->r->subprocess_env, name, val);
2399 rewritelog((ctx->r, 5, NULL, "setting env variable '%s' to '%s'",
2409 * perform all the expansions on the cookies
2411 * TODO: use cached time similar to how logging does it
2413 static void add_cookie(request_rec *r, char *s)
2426 var = apr_strtok(s, ":", &tok_cntx);
2427 val = apr_strtok(NULL, ":", &tok_cntx);
2428 domain = apr_strtok(NULL, ":", &tok_cntx);
2430 if (var && val && domain) {
2431 request_rec *rmain = r;
2435 while (rmain->main) {
2436 rmain = rmain->main;
2439 notename = apr_pstrcat(rmain->pool, var, "_rewrite", NULL);
2440 apr_pool_userdata_get(&data, notename, rmain->pool);
2442 char *exp_time = NULL;
2444 expires = apr_strtok(NULL, ":", &tok_cntx);
2445 path = expires ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
2446 secure = path ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
2447 httponly = secure ? apr_strtok(NULL, ":", &tok_cntx) : NULL;
2453 exp_min = atol(expires);
2455 apr_time_exp_gmt(&tms, r->request_time
2456 + apr_time_from_sec((60 * exp_min)));
2457 exp_time = apr_psprintf(r->pool, "%s, %.2d-%s-%.4d "
2458 "%.2d:%.2d:%.2d GMT",
2459 apr_day_snames[tms.tm_wday],
2461 apr_month_snames[tms.tm_mon],
2463 tms.tm_hour, tms.tm_min, tms.tm_sec);
2467 cookie = apr_pstrcat(rmain->pool,
2469 "; path=", path ? path : "/",
2470 "; domain=", domain,
2471 expires ? (exp_time ? "; expires=" : "")
2473 expires ? (exp_time ? exp_time : "")
2475 (secure && (!strcasecmp(secure, "true")
2476 || !strcmp(secure, "1")
2477 || !strcasecmp(secure,
2480 (httponly && (!strcasecmp(httponly, "true")
2481 || !strcmp(httponly, "1")
2482 || !strcasecmp(httponly,
2484 "; HttpOnly" : NULL,
2487 apr_table_addn(rmain->err_headers_out, "Set-Cookie", cookie);
2488 apr_pool_userdata_set("set", notename, NULL, rmain->pool);
2489 rewritelog((rmain, 5, NULL, "setting cookie '%s'", cookie));
2492 rewritelog((rmain, 5, NULL, "skipping already set cookie '%s'",
2500 static void do_expand_cookie(data_item *cookie, rewrite_ctx *ctx)
2503 add_cookie(ctx->r, do_expand(cookie->data, ctx, NULL));
2504 cookie = cookie->next;
2512 * Expand tilde-paths (/~user) through Unix /etc/passwd
2513 * database information (or other OS-specific database)
2515 static char *expand_tildepaths(request_rec *r, char *uri)
2517 if (uri && *uri == '/' && uri[1] == '~') {
2521 while (*p && *p != '/') {
2528 user = apr_pstrmemdup(r->pool, user, p-user);
2529 if (apr_uid_homepath_get(&homedir, user, r->pool) == APR_SUCCESS) {
2531 /* reuse of user variable */
2532 user = homedir + strlen(homedir) - 1;
2533 if (user >= homedir && *user == '/') {
2537 return apr_pstrcat(r->pool, homedir, p, NULL);
2548 #endif /* if APR_HAS_USER */
2552 * +-------------------------------------------------------+
2554 * | rewriting lockfile support
2556 * +-------------------------------------------------------+
2559 static apr_status_t rewritelock_create(server_rec *s, apr_pool_t *p)
2563 /* create the lockfile */
2564 /* XXX See if there are any rewrite map programs before creating
2567 rc = ap_global_mutex_create(&rewrite_mapr_lock_acquire, NULL,
2568 rewritemap_mutex_type, NULL, s, p, 0);
2569 if (rc != APR_SUCCESS) {
2576 static apr_status_t rewritelock_remove(void *data)
2578 /* destroy the rewritelock */
2579 if (rewrite_mapr_lock_acquire) {
2580 apr_global_mutex_destroy(rewrite_mapr_lock_acquire);
2581 rewrite_mapr_lock_acquire = NULL;
2588 * +-------------------------------------------------------+
2590 * | configuration directive handling
2592 * +-------------------------------------------------------+
2596 * own command line parser for RewriteRule and RewriteCond,
2597 * which doesn't have the '\\' problem.
2598 * (returns true on error)
2600 * XXX: what an inclined parser. Seems we have to leave it so
2601 * for backwards compat. *sigh*
2603 static int parseargline(char *str, char **a1, char **a2, char **a3)
2607 while (apr_isspace(*str)) {
2612 * determine first argument
2614 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2617 for (; *str; ++str) {
2618 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2621 if (*str == '\\' && apr_isspace(str[1])) {
2632 while (apr_isspace(*str)) {
2637 * determine second argument
2639 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2642 for (; *str; ++str) {
2643 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2646 if (*str == '\\' && apr_isspace(str[1])) {
2653 *a3 = NULL; /* 3rd argument is optional */
2658 while (apr_isspace(*str)) {
2663 *a3 = NULL; /* 3rd argument is still optional */
2668 * determine third argument
2670 quote = (*str == '"' || *str == '\'') ? *str++ : '\0';
2672 for (; *str; ++str) {
2673 if ((apr_isspace(*str) && !quote) || (*str == quote)) {
2676 if (*str == '\\' && apr_isspace(str[1])) {
2686 static void *config_server_create(apr_pool_t *p, server_rec *s)
2688 rewrite_server_conf *a;
2690 a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf));
2692 a->state = ENGINE_DISABLED;
2693 a->options = OPTION_NONE;
2694 a->rewritemaps = apr_hash_make(p);
2695 a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
2696 a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
2702 static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv)
2704 rewrite_server_conf *a, *base, *overrides;
2706 a = (rewrite_server_conf *)apr_pcalloc(p,
2707 sizeof(rewrite_server_conf));
2708 base = (rewrite_server_conf *)basev;
2709 overrides = (rewrite_server_conf *)overridesv;
2711 a->state = overrides->state;
2712 a->options = overrides->options;
2713 a->server = overrides->server;
2715 if (a->options & OPTION_INHERIT) {
2717 * local directives override
2718 * and anything else is inherited
2720 a->rewritemaps = apr_hash_overlay(p, overrides->rewritemaps,
2722 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
2723 base->rewriteconds);
2724 a->rewriterules = apr_array_append(p, overrides->rewriterules,
2725 base->rewriterules);
2729 * local directives override
2730 * and anything else gets defaults
2732 a->rewritemaps = overrides->rewritemaps;
2733 a->rewriteconds = overrides->rewriteconds;
2734 a->rewriterules = overrides->rewriterules;
2740 static void *config_perdir_create(apr_pool_t *p, char *path)
2742 rewrite_perdir_conf *a;
2744 a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf));
2746 a->state = ENGINE_DISABLED;
2747 a->options = OPTION_NONE;
2749 a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry));
2750 a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry));
2753 a->directory = NULL;
2756 /* make sure it has a trailing slash */
2757 if (path[strlen(path)-1] == '/') {
2758 a->directory = apr_pstrdup(p, path);
2761 a->directory = apr_pstrcat(p, path, "/", NULL);
2768 static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv)
2770 rewrite_perdir_conf *a, *base, *overrides;
2772 a = (rewrite_perdir_conf *)apr_pcalloc(p,
2773 sizeof(rewrite_perdir_conf));
2774 base = (rewrite_perdir_conf *)basev;
2775 overrides = (rewrite_perdir_conf *)overridesv;
2777 a->state = overrides->state;
2778 a->options = overrides->options;
2779 a->directory = overrides->directory;
2780 a->baseurl = overrides->baseurl;
2782 if (a->options & OPTION_INHERIT) {
2783 a->rewriteconds = apr_array_append(p, overrides->rewriteconds,
2784 base->rewriteconds);
2785 a->rewriterules = apr_array_append(p, overrides->rewriterules,
2786 base->rewriterules);
2789 a->rewriteconds = overrides->rewriteconds;
2790 a->rewriterules = overrides->rewriterules;
2796 static const char *cmd_rewriteengine(cmd_parms *cmd,
2797 void *in_dconf, int flag)
2799 rewrite_perdir_conf *dconf = in_dconf;
2800 rewrite_server_conf *sconf;
2802 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2804 if (cmd->path == NULL) { /* is server command */
2805 sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
2807 else /* is per-directory command */ {
2808 dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED);
2814 static const char *cmd_rewriteoptions(cmd_parms *cmd,
2815 void *in_dconf, const char *option)
2821 w = ap_getword_conf(cmd->pool, &option);
2823 if (!strcasecmp(w, "inherit")) {
2824 options |= OPTION_INHERIT;
2826 else if (!strncasecmp(w, "MaxRedirects=", 13)) {
2827 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2828 "RewriteOptions: MaxRedirects option has been "
2829 "removed in favor of the global "
2830 "LimitInternalRecursion directive and will be "
2834 return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '",
2839 /* put it into the appropriate config */
2840 if (cmd->path == NULL) { /* is server command */
2841 rewrite_server_conf *conf =
2842 ap_get_module_config(cmd->server->module_config,
2845 conf->options |= options;
2847 else { /* is per-directory command */
2848 rewrite_perdir_conf *conf = in_dconf;
2850 conf->options |= options;
2856 static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1,
2859 rewrite_server_conf *sconf;
2860 rewritemap_entry *newmap;
2864 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
2866 newmap = apr_palloc(cmd->pool, sizeof(rewritemap_entry));
2867 newmap->func = NULL;
2869 if (strncasecmp(a2, "txt:", 4) == 0) {
2870 if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
2871 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to txt map: ",
2875 newmap->type = MAPTYPE_TXT;
2876 newmap->datafile = fname;
2877 newmap->checkfile = fname;
2878 newmap->checkfile2= NULL;
2879 newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
2880 (void *)cmd->server, a1);
2882 else if (strncasecmp(a2, "rnd:", 4) == 0) {
2883 if ((fname = ap_server_root_relative(cmd->pool, a2+4)) == NULL) {
2884 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to rnd map: ",
2888 newmap->type = MAPTYPE_RND;
2889 newmap->datafile = fname;
2890 newmap->checkfile = fname;
2891 newmap->checkfile2= NULL;
2892 newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
2893 (void *)cmd->server, a1);
2895 else if (strncasecmp(a2, "dbm", 3) == 0) {
2898 newmap->type = MAPTYPE_DBM;
2900 newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
2901 (void *)cmd->server, a1);
2904 newmap->dbmtype = "default";
2907 else if (a2[3] == '=') {
2908 const char *colon = ap_strchr_c(a2 + 4, ':');
2911 newmap->dbmtype = apr_pstrndup(cmd->pool, a2 + 4,
2912 colon - (a2 + 3) - 1);
2918 return apr_pstrcat(cmd->pool, "RewriteMap: bad map:",
2922 if ((newmap->datafile = ap_server_root_relative(cmd->pool,
2924 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to dbm map: ",
2928 rv = apr_dbm_get_usednames_ex(cmd->pool, newmap->dbmtype,
2929 newmap->datafile, &newmap->checkfile,
2930 &newmap->checkfile2);
2931 if (rv != APR_SUCCESS) {
2932 return apr_pstrcat(cmd->pool, "RewriteMap: dbm type ",
2933 newmap->dbmtype, " is invalid", NULL);
2936 else if ((strncasecmp(a2, "dbd:", 4) == 0)
2937 || (strncasecmp(a2, "fastdbd:", 8) == 0)) {
2938 if (dbd_prepare == NULL) {
2939 return "RewriteMap types dbd and fastdbd require mod_dbd!";
2941 if ((a2[0] == 'd') || (a2[0] == 'D')) {
2942 newmap->type = MAPTYPE_DBD;
2946 newmap->type = MAPTYPE_DBD_CACHE;
2948 newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
2949 (void *)cmd->server, a1);
2952 dbd_prepare(cmd->server, fname, newmap->dbdq);
2954 else if (strncasecmp(a2, "prg:", 4) == 0) {
2955 apr_tokenize_to_argv(a2 + 4, &newmap->argv, cmd->pool);
2957 fname = newmap->argv[0];
2958 if ((newmap->argv[0] = ap_server_root_relative(cmd->pool,
2960 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to prg map: ",
2964 newmap->type = MAPTYPE_PRG;
2965 newmap->datafile = NULL;
2966 newmap->checkfile = newmap->argv[0];
2967 newmap->checkfile2= NULL;
2968 newmap->cachename = NULL;
2970 else if (strncasecmp(a2, "int:", 4) == 0) {
2971 newmap->type = MAPTYPE_INT;
2972 newmap->datafile = NULL;
2973 newmap->checkfile = NULL;
2974 newmap->checkfile2= NULL;
2975 newmap->cachename = NULL;
2976 newmap->func = (char *(*)(request_rec *,char *))
2977 apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4));
2978 if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) {
2979 return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:",
2984 if ((fname = ap_server_root_relative(cmd->pool, a2)) == NULL) {
2985 return apr_pstrcat(cmd->pool, "RewriteMap: bad path to txt map: ",
2989 newmap->type = MAPTYPE_TXT;
2990 newmap->datafile = fname;
2991 newmap->checkfile = fname;
2992 newmap->checkfile2= NULL;
2993 newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s",
2994 (void *)cmd->server, a1);
2996 newmap->fpin = NULL;
2997 newmap->fpout = NULL;
2999 if (newmap->checkfile && (sconf->state == ENGINE_ENABLED)
3000 && (apr_stat(&st, newmap->checkfile, APR_FINFO_MIN,
3001 cmd->pool) != APR_SUCCESS)) {
3002 return apr_pstrcat(cmd->pool,
3003 "RewriteMap: file for map ", a1,
3004 " not found:", newmap->checkfile, NULL);
3007 apr_hash_set(sconf->rewritemaps, a1, APR_HASH_KEY_STRING, newmap);
3012 static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf,
3015 rewrite_perdir_conf *dconf = in_dconf;
3017 if (cmd->path == NULL || dconf == NULL) {
3018 return "RewriteBase: only valid in per-directory config files";
3020 if (a1[0] == '\0') {
3021 return "RewriteBase: empty URL not allowed";
3024 return "RewriteBase: argument is not a valid URL";
3027 dconf->baseurl = a1;
3033 * generic lexer for RewriteRule and RewriteCond flags.
3034 * The parser will be passed in as a function pointer
3035 * and called if a flag was found
3037 static const char *cmd_parseflagfield(apr_pool_t *p, void *cfg, char *key,
3038 const char *(*parse)(apr_pool_t *,
3042 char *val, *nextp, *endp;
3045 endp = key + strlen(key) - 1;
3046 if (*key != '[' || *endp != ']') {
3047 return "bad flag delimiters";
3050 *endp = ','; /* for simpler parsing */
3054 /* skip leading spaces */
3055 while (apr_isspace(*key)) {
3059 if (!*key || (nextp = ap_strchr(key, ',')) == NULL) { /* NULL should not
3065 /* strip trailing spaces */
3067 while (apr_isspace(*endp)) {
3072 /* split key and val */
3073 val = ap_strchr(key, '=');
3081 err = parse(p, cfg, key, val);
3092 static const char *cmd_rewritecond_setflag(apr_pool_t *p, void *_cfg,
3093 char *key, char *val)
3095 rewritecond_entry *cfg = _cfg;
3097 if ( strcasecmp(key, "nocase") == 0
3098 || strcasecmp(key, "NC") == 0 ) {
3099 cfg->flags |= CONDFLAG_NOCASE;
3101 else if ( strcasecmp(key, "ornext") == 0
3102 || strcasecmp(key, "OR") == 0 ) {
3103 cfg->flags |= CONDFLAG_ORNEXT;
3105 else if ( strcasecmp(key, "novary") == 0
3106 || strcasecmp(key, "NV") == 0 ) {
3107 cfg->flags |= CONDFLAG_NOVARY;
3110 return apr_pstrcat(p, "unknown flag '", key, "'", NULL);
3115 static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf,
3118 rewrite_perdir_conf *dconf = in_dconf;
3119 char *str = apr_pstrdup(cmd->pool, in_str);
3120 rewrite_server_conf *sconf;
3121 rewritecond_entry *newcond;
3128 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
3130 /* make a new entry in the internal temporary rewrite rule list */
3131 if (cmd->path == NULL) { /* is server command */
3132 newcond = apr_array_push(sconf->rewriteconds);
3134 else { /* is per-directory command */
3135 newcond = apr_array_push(dconf->rewriteconds);
3138 /* parse the argument line ourself
3139 * a1 .. a3 are substrings of str, which is a fresh copy
3140 * of the argument line. So we can use a1 .. a3 without
3141 * copying them again.
3143 if (parseargline(str, &a1, &a2, &a3)) {
3144 return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str,
3148 /* arg1: the input string */
3149 newcond->input = a1;
3151 /* arg3: optional flags field
3152 * (this has to be parsed first, because we need to
3153 * know if the regex should be compiled with ICASE!)
3155 newcond->flags = CONDFLAG_NONE;
3157 if ((err = cmd_parseflagfield(cmd->pool, newcond, a3,
3158 cmd_rewritecond_setflag)) != NULL) {
3159 return apr_pstrcat(cmd->pool, "RewriteCond: ", err, NULL);
3163 /* arg2: the pattern */
3165 newcond->flags |= CONDFLAG_NOTMATCH;
3169 /* determine the pattern type */
3172 if (!a2[2] && *a2 == '-') {
3174 case 'f': newcond->ptype = CONDPAT_FILE_EXISTS; break;
3175 case 's': newcond->ptype = CONDPAT_FILE_SIZE; break;
3176 case 'l': newcond->ptype = CONDPAT_FILE_LINK; break;
3177 case 'd': newcond->ptype = CONDPAT_FILE_DIR; break;
3178 case 'x': newcond->ptype = CONDPAT_FILE_XBIT; break;
3179 case 'U': newcond->ptype = CONDPAT_LU_URL; break;
3180 case 'F': newcond->ptype = CONDPAT_LU_FILE; break;
3185 case '>': newcond->ptype = CONDPAT_STR_GT; break;
3186 case '<': newcond->ptype = CONDPAT_STR_LT; break;
3187 case '=': newcond->ptype = CONDPAT_STR_EQ;
3188 /* "" represents an empty string */
3189 if (*++a2 == '"' && a2[1] == '"' && !a2[2]) {
3197 if (newcond->ptype && newcond->ptype != CONDPAT_STR_EQ &&
3198 (newcond->flags & CONDFLAG_NOCASE)) {
3199 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
3200 "RewriteCond: NoCase option for non-regex pattern '%s' "
3201 "is not supported and will be ignored.", a2);
3202 newcond->flags &= ~CONDFLAG_NOCASE;
3205 newcond->pattern = a2;
3207 if (!newcond->ptype) {
3208 regexp = ap_pregcomp(cmd->pool, a2,
3209 AP_REG_EXTENDED | ((newcond->flags & CONDFLAG_NOCASE)
3210 ? AP_REG_ICASE : 0));
3212 return apr_pstrcat(cmd->pool, "RewriteCond: cannot compile regular "
3213 "expression '", a2, "'", NULL);
3216 newcond->regexp = regexp;
3222 static const char *cmd_rewriterule_setflag(apr_pool_t *p, void *_cfg,
3223 char *key, char *val)
3225 rewriterule_entry *cfg = _cfg;
3231 if (!*key || !strcasecmp(key, "ackrefescaping")) {
3232 cfg->flags |= RULEFLAG_ESCAPEBACKREF;
3240 if (!*key || !strcasecmp(key, "hain")) { /* chain */
3241 cfg->flags |= RULEFLAG_CHAIN;
3243 else if (((*key == 'O' || *key == 'o') && !key[1])
3244 || !strcasecmp(key, "ookie")) { /* cookie */
3245 data_item *cp = cfg->cookie;
3248 cp = cfg->cookie = apr_palloc(p, sizeof(*cp));
3254 cp->next = apr_palloc(p, sizeof(*cp));
3267 if (!*key || !strcasecmp(key, "PI") || !strcasecmp(key,"iscardpath")) {
3268 cfg->flags |= (RULEFLAG_DISCARDPATHINFO);
3273 if (!*key || !strcasecmp(key, "nv")) { /* env */
3274 data_item *cp = cfg->env;
3277 cp = cfg->env = apr_palloc(p, sizeof(*cp));
3283 cp->next = apr_palloc(p, sizeof(*cp));
3297 if (!*key || !strcasecmp(key, "orbidden")) { /* forbidden */
3298 cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
3299 cfg->forced_responsecode = HTTP_FORBIDDEN;
3308 if (!*key || !strcasecmp(key, "one")) { /* gone */
3309 cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
3310 cfg->forced_responsecode = HTTP_GONE;
3319 if (!*key || !strcasecmp(key, "andler")) { /* handler */
3320 cfg->forced_handler = val;
3328 if (!*key || !strcasecmp(key, "ast")) { /* last */
3329 cfg->flags |= RULEFLAG_LASTRULE;
3338 if (((*key == 'E' || *key == 'e') && !key[1])
3339 || !strcasecmp(key, "oescape")) { /* noescape */
3340 cfg->flags |= RULEFLAG_NOESCAPE;
3342 else if (!*key || !strcasecmp(key, "ext")) { /* next */
3343 cfg->flags |= RULEFLAG_NEWROUND;
3345 else if (((*key == 'S' || *key == 's') && !key[1])
3346 || !strcasecmp(key, "osubreq")) { /* nosubreq */
3347 cfg->flags |= RULEFLAG_IGNOREONSUBREQ;
3349 else if (((*key == 'C' || *key == 'c') && !key[1])
3350 || !strcasecmp(key, "ocase")) { /* nocase */
3351 cfg->flags |= RULEFLAG_NOCASE;
3360 if (!*key || !strcasecmp(key, "roxy")) { /* proxy */
3361 cfg->flags |= RULEFLAG_PROXY;
3363 else if (((*key == 'T' || *key == 't') && !key[1])
3364 || !strcasecmp(key, "assthrough")) { /* passthrough */
3365 cfg->flags |= RULEFLAG_PASSTHROUGH;
3374 if ( !strcasecmp(key, "SA")
3375 || !strcasecmp(key, "sappend")) { /* qsappend */
3376 cfg->flags |= RULEFLAG_QSAPPEND;
3377 } else if ( !strcasecmp(key, "SD")
3378 || !strcasecmp(key, "sdiscard") ) { /* qsdiscard */
3379 cfg->flags |= RULEFLAG_QSDISCARD;
3388 if (!*key || !strcasecmp(key, "edirect")) { /* redirect */
3391 cfg->flags |= RULEFLAG_FORCEREDIRECT;
3393 if (strcasecmp(val, "permanent") == 0) {
3394 status = HTTP_MOVED_PERMANENTLY;
3396 else if (strcasecmp(val, "temp") == 0) {
3397 status = HTTP_MOVED_TEMPORARILY;
3399 else if (strcasecmp(val, "seeother") == 0) {
3400 status = HTTP_SEE_OTHER;
3402 else if (apr_isdigit(*val)) {
3404 if (status != HTTP_INTERNAL_SERVER_ERROR) {
3406 ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
3408 if (ap_index_of_response(status) == idx) {
3409 return apr_psprintf(p, "invalid HTTP "
3410 "response code '%s' for "
3415 if (!ap_is_HTTP_REDIRECT(status)) {
3416 cfg->flags |= (RULEFLAG_STATUS | RULEFLAG_NOSUB);
3419 cfg->forced_responsecode = status;
3429 if (!*key || !strcasecmp(key, "kip")) { /* skip */
3430 cfg->skip = atoi(val);
3439 if (!*key || !strcasecmp(key, "ype")) { /* type */
3440 cfg->forced_mimetype = val;
3452 return apr_pstrcat(p, "unknown flag '", --key, "'", NULL);
3458 static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf,
3461 rewrite_perdir_conf *dconf = in_dconf;
3462 char *str = apr_pstrdup(cmd->pool, in_str);
3463 rewrite_server_conf *sconf;
3464 rewriterule_entry *newrule;
3471 sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
3473 /* make a new entry in the internal rewrite rule list */
3474 if (cmd->path == NULL) { /* is server command */
3475 newrule = apr_array_push(sconf->rewriterules);
3477 else { /* is per-directory command */
3478 newrule = apr_array_push(dconf->rewriterules);
3481 /* parse the argument line ourself */
3482 if (parseargline(str, &a1, &a2, &a3)) {
3483 return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str,
3487 /* arg3: optional flags field */
3488 newrule->forced_mimetype = NULL;
3489 newrule->forced_handler = NULL;
3490 newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY;
3491 newrule->flags = RULEFLAG_NONE;
3492 newrule->env = NULL;
3493 newrule->cookie = NULL;
3496 if ((err = cmd_parseflagfield(cmd->pool, newrule, a3,
3497 cmd_rewriterule_setflag)) != NULL) {
3498 return apr_pstrcat(cmd->pool, "RewriteRule: ", err, NULL);
3502 /* arg1: the pattern
3503 * try to compile the regexp to test if is ok
3506 newrule->flags |= RULEFLAG_NOTMATCH;
3510 regexp = ap_pregcomp(cmd->pool, a1, AP_REG_EXTENDED |
3511 ((newrule->flags & RULEFLAG_NOCASE)
3512 ? AP_REG_ICASE : 0));
3514 return apr_pstrcat(cmd->pool,
3515 "RewriteRule: cannot compile regular expression '",
3519 newrule->pattern = a1;
3520 newrule->regexp = regexp;
3522 /* arg2: the output string */
3523 newrule->output = a2;
3524 if (*a2 == '-' && !a2[1]) {
3525 newrule->flags |= RULEFLAG_NOSUB;
3528 /* now, if the server or per-dir config holds an
3529 * array of RewriteCond entries, we take it for us
3530 * and clear the array
3532 if (cmd->path == NULL) { /* is server command */
3533 newrule->rewriteconds = sconf->rewriteconds;
3534 sconf->rewriteconds = apr_array_make(cmd->pool, 2,
3535 sizeof(rewritecond_entry));
3537 else { /* is per-directory command */
3538 newrule->rewriteconds = dconf->rewriteconds;
3539 dconf->rewriteconds = apr_array_make(cmd->pool, 2,
3540 sizeof(rewritecond_entry));
3548 * +-------------------------------------------------------+
3550 * | the rewriting engine
3552 * +-------------------------------------------------------+
3555 /* Lexicographic Compare */
3556 static APR_INLINE int compare_lexicography(char *a, char *b)
3558 apr_size_t i, lena, lenb;
3564 for (i = 0; i < lena; ++i) {
3566 return ((unsigned char)a[i] > (unsigned char)b[i]) ? 1 : -1;
3573 return ((lena > lenb) ? 1 : -1);
3577 * Apply a single rewriteCond
3579 static int apply_rewrite_cond(rewritecond_entry *p, rewrite_ctx *ctx)
3581 char *input = do_expand(p->input, ctx, NULL);
3583 request_rec *rsub, *r = ctx->r;
3584 ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
3588 case CONDPAT_FILE_EXISTS:
3589 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3590 && sb.filetype == APR_REG) {
3595 case CONDPAT_FILE_SIZE:
3596 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3597 && sb.filetype == APR_REG && sb.size > 0) {
3602 case CONDPAT_FILE_LINK:
3604 if ( apr_stat(&sb, input, APR_FINFO_MIN | APR_FINFO_LINK,
3605 r->pool) == APR_SUCCESS
3606 && sb.filetype == APR_LNK) {
3612 case CONDPAT_FILE_DIR:
3613 if ( apr_stat(&sb, input, APR_FINFO_MIN, r->pool) == APR_SUCCESS
3614 && sb.filetype == APR_DIR) {
3619 case CONDPAT_FILE_XBIT:
3620 if ( apr_stat(&sb, input, APR_FINFO_PROT, r->pool) == APR_SUCCESS
3621 && (sb.protection & (APR_UEXECUTE | APR_GEXECUTE | APR_WEXECUTE))) {
3626 case CONDPAT_LU_URL:
3627 if (*input && subreq_ok(r)) {
3628 rsub = ap_sub_req_lookup_uri(input, r, NULL);
3629 if (rsub->status < 400) {
3632 rewritelog((r, 5, NULL, "RewriteCond URI (-U) check: "
3633 "path=%s -> status=%d", input, rsub->status));
3634 ap_destroy_sub_req(rsub);
3638 case CONDPAT_LU_FILE:
3639 if (*input && subreq_ok(r)) {
3640 rsub = ap_sub_req_lookup_file(input, r, NULL);
3641 if (rsub->status < 300 &&
3642 /* double-check that file exists since default result is 200 */
3643 apr_stat(&sb, rsub->filename, APR_FINFO_MIN,
3644 r->pool) == APR_SUCCESS) {
3647 rewritelog((r, 5, NULL, "RewriteCond file (-F) check: path=%s "
3648 "-> file=%s status=%d", input, rsub->filename,
3650 ap_destroy_sub_req(rsub);
3654 case CONDPAT_STR_GT:
3655 rc = (compare_lexicography(input, p->pattern+1) == 1) ? 1 : 0;
3658 case CONDPAT_STR_LT:
3659 rc = (compare_lexicography(input, p->pattern+1) == -1) ? 1 : 0;
3662 case CONDPAT_STR_EQ:
3663 if (p->flags & CONDFLAG_NOCASE) {
3664 rc = !strcasecmp(input, p->pattern);
3667 rc = !strcmp(input, p->pattern);
3672 /* it is really a regexp pattern, so apply it */
3673 rc = !ap_regexec(p->regexp, input, AP_MAX_REG_MATCH, regmatch, 0);
3675 /* update briRC backref info */
3676 if (rc && !(p->flags & CONDFLAG_NOTMATCH)) {
3677 ctx->briRC.source = input;
3678 ctx->briRC.nsub = p->regexp->re_nsub;
3679 memcpy(ctx->briRC.regmatch, regmatch, sizeof(regmatch));
3684 if (p->flags & CONDFLAG_NOTMATCH) {
3688 rewritelog((r, 4, ctx->perdir, "RewriteCond: input='%s' pattern='%s%s%s'%s "
3689 "=> %s", input, (p->flags & CONDFLAG_NOTMATCH) ? "!" : "",
3690 (p->ptype == CONDPAT_STR_EQ) ? "=" : "", p->pattern,
3691 (p->flags & CONDFLAG_NOCASE) ? " [NC]" : "",
3692 rc ? "matched" : "not-matched"));
3697 /* check for forced type and handler */
3698 static APR_INLINE void force_type_handler(rewriterule_entry *p,
3703 if (p->forced_mimetype) {
3704 expanded = do_expand(p->forced_mimetype, ctx, p);
3707 ap_str_tolower(expanded);
3709 rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have MIME-type "
3710 "'%s'", ctx->r->filename, expanded));
3712 apr_table_setn(ctx->r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR,
3717 if (p->forced_handler) {
3718 expanded = do_expand(p->forced_handler, ctx, p);
3721 ap_str_tolower(expanded);
3723 rewritelog((ctx->r, 2, ctx->perdir, "remember %s to have "
3724 "Content-handler '%s'", ctx->r->filename, expanded));
3726 apr_table_setn(ctx->r->notes, REWRITE_FORCED_HANDLER_NOTEVAR,
3733 * Apply a single RewriteRule
3735 static int apply_rewrite_rule(rewriterule_entry *p, rewrite_ctx *ctx)
3737 ap_regmatch_t regmatch[AP_MAX_REG_MATCH];
3738 apr_array_header_t *rewriteconds;
3739 rewritecond_entry *conds;
3741 char *newuri = NULL;
3742 request_rec *r = ctx->r;
3743 int is_proxyreq = 0;
3745 ctx->uri = r->filename;
3748 apr_size_t dirlen = strlen(ctx->perdir);
3753 is_proxyreq = ( r->proxyreq && r->filename
3754 && !strncmp(r->filename, "proxy:", 6));
3756 /* Since we want to match against the (so called) full URL, we have
3757 * to re-add the PATH_INFO postfix
3759 if (r->path_info && *r->path_info) {
3760 rewritelog((r, 3, ctx->perdir, "add path info postfix: %s -> %s%s",
3761 ctx->uri, ctx->uri, r->path_info));
3762 ctx->uri = apr_pstrcat(r->pool, ctx->uri, r->path_info, NULL);
3765 /* Additionally we strip the physical path from the url to match
3766 * it independent from the underlaying filesystem.
3768 if (!is_proxyreq && strlen(ctx->uri) >= dirlen &&
3769 !strncmp(ctx->uri, ctx->perdir, dirlen)) {
3771 rewritelog((r, 3, ctx->perdir, "strip per-dir prefix: %s -> %s",
3772 ctx->uri, ctx->uri + dirlen));
3773 ctx->uri = ctx->uri + dirlen;
3777 /* Try to match the URI against the RewriteRule pattern
3778 * and exit immediately if it didn't apply.
3780 rewritelog((r, 3, ctx->perdir, "applying pattern '%s' to uri '%s'",
3781 p->pattern, ctx->uri));
3783 rc = !ap_regexec(p->regexp, ctx->uri, AP_MAX_REG_MATCH, regmatch, 0);
3784 if (! (( rc && !(p->flags & RULEFLAG_NOTMATCH)) ||
3785 (!rc && (p->flags & RULEFLAG_NOTMATCH)) ) ) {
3789 /* It matched, wow! Now it's time to prepare the context structure for
3790 * further processing
3792 ctx->vary_this = NULL;
3793 ctx->briRC.source = NULL;
3795 if (p->flags & RULEFLAG_NOTMATCH) {
3796 ctx->briRR.source = NULL;
3799 ctx->briRR.source = apr_pstrdup(r->pool, ctx->uri);
3800 ctx->briRR.nsub = p->regexp->re_nsub;
3801 memcpy(ctx->briRR.regmatch, regmatch, sizeof(regmatch));
3804 /* Ok, we already know the pattern has matched, but we now
3805 * additionally have to check for all existing preconditions
3806 * (RewriteCond) which have to be also true. We do this at
3807 * this very late stage to avoid unnessesary checks which
3808 * would slow down the rewriting engine.
3810 rewriteconds = p->rewriteconds;
3811 conds = (rewritecond_entry *)rewriteconds->elts;
3813 for (i = 0; i < rewriteconds->nelts; ++i) {
3814 rewritecond_entry *c = &conds[i];
3816 rc = apply_rewrite_cond(c, ctx);
3818 * Reset vary_this if the novary flag is set for this condition.
3820 if (c->flags & CONDFLAG_NOVARY) {
3821 ctx->vary_this = NULL;
3823 if (c->flags & CONDFLAG_ORNEXT) {
3825 /* One condition is false, but another can be still true. */
3826 ctx->vary_this = NULL;
3830 /* skip the rest of the chained OR conditions */
3831 while ( i < rewriteconds->nelts
3832 && c->flags & CONDFLAG_ORNEXT) {
3841 /* If some HTTP header was involved in the condition, remember it
3844 if (ctx->vary_this) {
3845 ctx->vary = ctx->vary
3846 ? apr_pstrcat(r->pool, ctx->vary, ", ", ctx->vary_this,
3849 ctx->vary_this = NULL;
3853 /* expand the result */
3854 if (!(p->flags & RULEFLAG_NOSUB)) {
3855 newuri = do_expand(p->output, ctx, p);
3856 rewritelog((r, 2, ctx->perdir, "rewrite '%s' -> '%s'", ctx->uri,
3860 /* expand [E=var:val] and [CO=<cookie>] */
3861 do_expand_env(p->env, ctx);
3862 do_expand_cookie(p->cookie, ctx);
3864 /* non-substitution rules ('RewriteRule <pat> -') end here. */
3865 if (p->flags & RULEFLAG_NOSUB) {
3866 force_type_handler(p, ctx);
3868 if (p->flags & RULEFLAG_STATUS) {
3869 rewritelog((r, 2, ctx->perdir, "forcing responsecode %d for %s",
3870 p->forced_responsecode, r->filename));
3872 r->status = p->forced_responsecode;
3878 /* Now adjust API's knowledge about r->filename and r->args */
3879 r->filename = newuri;
3881 if (ctx->perdir && (p->flags & RULEFLAG_DISCARDPATHINFO)) {
3882 r->path_info = NULL;
3885 splitout_queryargs(r, p->flags & RULEFLAG_QSAPPEND, p->flags & RULEFLAG_QSDISCARD);
3887 /* Add the previously stripped per-directory location prefix, unless
3888 * (1) it's an absolute URL path and
3889 * (2) it's a full qualified URL
3891 if ( ctx->perdir && !is_proxyreq && *r->filename != '/'
3892 && !is_absolute_uri(r->filename)) {
3893 rewritelog((r, 3, ctx->perdir, "add per-dir prefix: %s -> %s%s",
3894 r->filename, ctx->perdir, r->filename));
3896 r->filename = apr_pstrcat(r->pool, ctx->perdir, r->filename, NULL);
3899 /* If this rule is forced for proxy throughput
3900 * (`RewriteRule ... ... [P]') then emulate mod_proxy's
3901 * URL-to-filename handler to be sure mod_proxy is triggered
3902 * for this URL later in the Apache API. But make sure it is
3903 * a fully-qualified URL. (If not it is qualified with
3906 if (p->flags & RULEFLAG_PROXY) {
3907 /* For rules evaluated in server context, the mod_proxy fixup
3908 * hook can be relied upon to escape the URI as and when
3909 * necessary, since it occurs later. If in directory context,
3910 * the ordering of the fixup hooks is forced such that
3911 * mod_proxy comes first, so the URI must be escaped here
3912 * instead. See PR 39746, 46428, and other headaches. */
3913 if (ctx->perdir && (p->flags & RULEFLAG_NOESCAPE) == 0) {
3914 char *old_filename = r->filename;
3916 r->filename = ap_escape_uri(r->pool, r->filename);
3917 rewritelog((r, 2, ctx->perdir, "escaped URI in per-dir context "
3918 "for proxy, %s -> %s", old_filename, r->filename));
3921 fully_qualify_uri(r);
3923 rewritelog((r, 2, ctx->perdir, "forcing proxy-throughput with %s",
3926 r->filename = apr_pstrcat(r->pool, "proxy:", r->filename, NULL);
3930 /* If this rule is explicitly forced for HTTP redirection
3931 * (`RewriteRule .. .. [R]') then force an external HTTP
3932 * redirect. But make sure it is a fully-qualified URL. (If
3933 * not it is qualified with ourself).
3935 if (p->flags & RULEFLAG_FORCEREDIRECT) {
3936 fully_qualify_uri(r);
3938 rewritelog((r, 2, ctx->perdir, "explicitly forcing redirect with %s",
3941 r->status = p->forced_responsecode;
3945 /* Special Rewriting Feature: Self-Reduction
3946 * We reduce the URL by stripping a possible
3947 * http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
3948 * corresponds to ourself. This is to simplify rewrite maps
3949 * and to avoid recursion, etc. When this prefix is not a
3950 * coincidence then the user has to use [R] explicitly (see
3955 /* If this rule is still implicitly forced for HTTP
3956 * redirection (`RewriteRule .. <scheme>://...') then
3957 * directly force an external HTTP redirect.
3959 if (is_absolute_uri(r->filename)) {
3960 rewritelog((r, 2, ctx->perdir, "implicitly forcing redirect (rc=%d) "
3961 "with %s", p->forced_responsecode, r->filename));
3963 r->status = p->forced_responsecode;
3967 /* Finally remember the forced mime-type */
3968 force_type_handler(p, ctx);
3970 /* Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
3971 * But now we're done for this particular rule.
3977 * Apply a complete rule set,
3978 * i.e. a list of rewrite rules
3980 static int apply_rewrite_list(request_rec *r, apr_array_header_t *rewriterules,
3983 rewriterule_entry *entries;
3984 rewriterule_entry *p;
3991 ctx = apr_palloc(r->pool, sizeof(*ctx));
3992 ctx->perdir = perdir;
3996 * Iterate over all existing rules
3998 entries = (rewriterule_entry *)rewriterules->elts;
4001 for (i = 0; i < rewriterules->nelts; i++) {
4005 * Ignore this rule on subrequests if we are explicitly
4006 * asked to do so or this is a proxy-throughput or a
4007 * forced redirect rule.
4009 if (r->main != NULL &&
4010 (p->flags & RULEFLAG_IGNOREONSUBREQ ||
4011 p->flags & RULEFLAG_FORCEREDIRECT )) {
4016 * Apply the current rule.
4019 rc = apply_rewrite_rule(p, ctx);
4022 /* Regardless of what we do next, we've found a match. Check to see
4023 * if any of the request header fields were involved, and add them
4024 * to the Vary field of the response.
4027 apr_table_merge(r->headers_out, "Vary", ctx->vary);
4031 * The rule sets the response code (implies match-only)
4033 if (p->flags & RULEFLAG_STATUS) {
4034 return ACTION_STATUS;
4038 * Indicate a change if this was not a match-only rule.
4041 changed = ((p->flags & RULEFLAG_NOESCAPE)
4042 ? ACTION_NOESCAPE : ACTION_NORMAL);
4046 * Pass-Through Feature (`RewriteRule .. .. [PT]'):
4047 * Because the Apache 1.x API is very limited we
4048 * need this hack to pass the rewritten URL to other
4049 * modules like mod_alias, mod_userdir, etc.
4051 if (p->flags & RULEFLAG_PASSTHROUGH) {
4052 rewritelog((r, 2, perdir, "forcing '%s' to get passed through "
4053 "to next API URI-to-filename handler", r->filename));
4054 r->filename = apr_pstrcat(r->pool, "passthrough:",
4056 changed = ACTION_NORMAL;
4061 * Stop processing also on proxy pass-through and
4062 * last-rule and new-round flags.
4064 if (p->flags & (RULEFLAG_PROXY | RULEFLAG_LASTRULE)) {
4069 * On "new-round" flag we just start from the top of
4070 * the rewriting ruleset again.
4072 if (p->flags & RULEFLAG_NEWROUND) {
4077 * If we are forced to skip N next rules, do it now.
4081 while ( i < rewriterules->nelts
4091 * If current rule is chained with next rule(s),
4092 * skip all this next rule(s)
4094 while ( i < rewriterules->nelts
4095 && p->flags & RULEFLAG_CHAIN) {
4106 * +-------------------------------------------------------+
4108 * | Module Initialization Hooks
4110 * +-------------------------------------------------------+
4113 static int pre_config(apr_pool_t *pconf,
4117 APR_OPTIONAL_FN_TYPE(ap_register_rewrite_mapfunc) *map_pfn_register;
4119 ap_mutex_register(pconf, rewritemap_mutex_type, NULL, APR_LOCK_DEFAULT, 0);
4121 /* register int: rewritemap handlers */
4122 map_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_rewrite_mapfunc);
4123 if (map_pfn_register) {
4124 map_pfn_register("tolower", rewrite_mapfunc_tolower);
4125 map_pfn_register("toupper", rewrite_mapfunc_toupper);
4126 map_pfn_register("escape", rewrite_mapfunc_escape);
4127 map_pfn_register("unescape", rewrite_mapfunc_unescape);
4129 dbd_acquire = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
4130 dbd_prepare = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_prepare);
4134 static int post_config(apr_pool_t *p,
4142 const char *userdata_key = "rewrite_init_module";
4144 apr_pool_userdata_get(&data, userdata_key, s->process->pool);
4147 apr_pool_userdata_set((const void *)1, userdata_key,
4148 apr_pool_cleanup_null, s->process->pool);
4151 /* check if proxy module is available */
4152 proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL);
4154 rv = rewritelock_create(s, p);
4155 if (rv != APR_SUCCESS) {
4156 return HTTP_INTERNAL_SERVER_ERROR;
4159 apr_pool_cleanup_register(p, (void *)s, rewritelock_remove,
4160 apr_pool_cleanup_null);
4162 /* step through the servers and
4163 * - open each rewriting logfile
4164 * - open the RewriteMap prg:xxx programs
4166 for (; s; s = s->next) {
4168 if (run_rewritemap_programs(s, p) != APR_SUCCESS) {
4169 return HTTP_INTERNAL_SERVER_ERROR;
4174 rewrite_ssl_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup);
4175 rewrite_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https);
4180 static void init_child(apr_pool_t *p, server_rec *s)
4182 apr_status_t rv = 0; /* get a rid of gcc warning (REWRITELOG_DISABLED) */
4184 if (rewrite_mapr_lock_acquire) {
4185 rv = apr_global_mutex_child_init(&rewrite_mapr_lock_acquire,
4186 apr_global_mutex_lockfile(rewrite_mapr_lock_acquire), p);
4187 if (rv != APR_SUCCESS) {
4188 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
4189 "mod_rewrite: could not init rewrite_mapr_lock_acquire"
4194 /* create the lookup cache */
4195 if (!init_cache(p)) {
4196 ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s,
4197 "mod_rewrite: could not init map cache in child");
4203 * +-------------------------------------------------------+
4207 * +-------------------------------------------------------+
4211 * URI-to-filename hook
4212 * [deals with RewriteRules in server context]
4214 static int hook_uri2file(request_rec *r)
4216 rewrite_server_conf *conf;
4217 const char *saved_rulestatus;
4219 const char *thisserver;
4221 const char *thisurl;
4226 * retrieve the config structures
4228 conf = ap_get_module_config(r->server->module_config, &rewrite_module);
4231 * only do something under runtime if the engine is really enabled,
4232 * else return immediately!
4234 if (conf->state == ENGINE_DISABLED) {
4239 * check for the ugly API case of a virtual host section where no
4240 * mod_rewrite directives exists. In this situation we became no chance
4241 * by the API to setup our default per-server config so we have to
4242 * on-the-fly assume we have the default config. But because the default
4243 * config has a disabled rewriting engine we are lucky because can
4244 * just stop operating now.
4246 if (conf->server != r->server) {
4251 * add the SCRIPT_URL variable to the env. this is a bit complicated
4252 * due to the fact that apache uses subrequests and internal redirects
4255 if (r->main == NULL) {
4256 var = apr_table_get(r->subprocess_env, REDIRECT_ENVVAR_SCRIPT_URL);
4258 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri);
4261 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
4265 var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL);
4266 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var);
4270 * create the SCRIPT_URI variable for the env
4273 /* add the canonical URI of this URL */
4274 thisserver = ap_get_server_name_for_url(r);
4275 port = ap_get_server_port(r);
4276 if (ap_is_default_port(port, r)) {
4280 thisport = apr_psprintf(r->pool, ":%u", port);
4282 thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL);
4284 /* set the variable */
4285 var = apr_pstrcat(r->pool, ap_http_scheme(r), "://", thisserver, thisport,
4287 apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var);
4289 if (!(saved_rulestatus = apr_table_get(r->notes,"mod_rewrite_rewritten"))) {
4290 /* if filename was not initially set,
4291 * we start with the requested URI
4293 if (r->filename == NULL) {
4294 r->filename = apr_pstrdup(r->pool, r->uri);
4295 rewritelog((r, 2, NULL, "init rewrite engine with requested uri %s",
4299 rewritelog((r, 2, NULL, "init rewrite engine with passed filename "
4300 "%s. Original uri = %s", r->filename, r->uri));
4304 * now apply the rules ...
4306 rulestatus = apply_rewrite_list(r, conf->rewriterules, NULL);
4307 apr_table_set(r->notes,"mod_rewrite_rewritten",
4308 apr_psprintf(r->pool,"%d",rulestatus));
4311 rewritelog((r, 2, NULL, "uri already rewritten. Status %s, Uri %s, "
4312 "r->filename %s", saved_rulestatus, r->uri, r->filename));
4314 rulestatus = atoi(saved_rulestatus);
4321 if (ACTION_STATUS == rulestatus) {
4324 r->status = HTTP_OK;
4328 flen = r->filename ? strlen(r->filename) : 0;
4329 if (flen > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
4330 /* it should be go on as an internal proxy request */
4332 /* check if the proxy module is enabled, so
4333 * we can actually use it!
4335 if (!proxy_available) {
4336 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4337 "attempt to make remote request from mod_rewrite "
4338 "without proxy enabled: %s", r->filename);
4339 return HTTP_FORBIDDEN;
4342 if (rulestatus == ACTION_NOESCAPE) {
4343 apr_table_setn(r->notes, "proxy-nocanon", "1");
4346 /* make sure the QUERY_STRING and
4347 * PATH_INFO parts get incorporated
4349 if (r->path_info != NULL) {
4350 r->filename = apr_pstrcat(r->pool, r->filename,
4351 r->path_info, NULL);
4353 if ((r->args != NULL)
4354 && ((r->proxyreq == PROXYREQ_PROXY)
4355 || (rulestatus == ACTION_NOESCAPE))) {
4356 /* see proxy_http:proxy_http_canon() */
4357 r->filename = apr_pstrcat(r->pool, r->filename,
4358 "?", r->args, NULL);
4361 /* now make sure the request gets handled by the proxy handler */
4362 if (PROXYREQ_NONE == r->proxyreq) {
4363 r->proxyreq = PROXYREQ_REVERSE;
4365 r->handler = "proxy-server";
4367 rewritelog((r, 1, NULL, "go-ahead with proxy request %s [OK]",
4371 else if ((skip = is_absolute_uri(r->filename)) > 0) {
4374 /* it was finally rewritten to a remote URL */
4376 if (rulestatus != ACTION_NOESCAPE) {
4377 rewritelog((r, 1, NULL, "escaping %s for redirect",
4379 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
4382 /* append the QUERY_STRING part */
4384 r->filename = apr_pstrcat(r->pool, r->filename, "?",
4385 (rulestatus == ACTION_NOESCAPE)
4387 : ap_escape_uri(r->pool, r->args),
4391 /* determine HTTP redirect response code */
4392 if (ap_is_HTTP_REDIRECT(r->status)) {
4394 r->status = HTTP_OK; /* make Apache kernel happy */
4397 n = HTTP_MOVED_TEMPORARILY;
4400 /* now do the redirection */
4401 apr_table_setn(r->headers_out, "Location", r->filename);
4402 rewritelog((r, 1, NULL, "redirect to %s [REDIRECT/%d]", r->filename,
4407 else if (flen > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
4409 * Hack because of underpowered API: passing the current
4410 * rewritten filename through to other URL-to-filename handlers
4411 * just as it were the requested URL. This is to enable
4412 * post-processing by mod_alias, etc. which always act on
4413 * r->uri! The difference here is: We do not try to
4414 * add the document root
4416 r->uri = apr_pstrdup(r->pool, r->filename+12);
4420 /* it was finally rewritten to a local path */
4422 /* expand "/~user" prefix */
4424 r->filename = expand_tildepaths(r, r->filename);
4426 rewritelog((r, 2, NULL, "local path result: %s", r->filename));
4428 /* the filename must be either an absolute local path or an
4429 * absolute local URL.
4431 if ( *r->filename != '/'
4432 && !ap_os_is_path_absolute(r->pool, r->filename)) {
4433 return HTTP_BAD_REQUEST;
4436 /* if there is no valid prefix, we call
4437 * the translator from the core and
4438 * prefix the filename with document_root
4441 * We cannot leave out the prefix_stat because
4442 * - when we always prefix with document_root
4443 * then no absolute path can be created, e.g. via
4444 * emulating a ScriptAlias directive, etc.
4445 * - when we always NOT prefix with document_root
4446 * then the files under document_root have to
4447 * be references directly and document_root
4448 * gets never used and will be a dummy parameter -
4452 * Under real Unix systems this is no problem,
4453 * because we only do stat() on the first directory
4454 * and this gets cached by the kernel for along time!
4456 if (!prefix_stat(r->filename, r->pool)) {
4460 r->uri = r->filename;
4461 res = ap_core_translate(r);
4465 rewritelog((r, 1, NULL, "prefixing with document_root of %s"
4466 " FAILED", r->filename));
4471 rewritelog((r, 2, NULL, "prefixed with document_root to %s",
4475 rewritelog((r, 1, NULL, "go-ahead with %s [OK]", r->filename));
4480 rewritelog((r, 1, NULL, "pass through %s", r->filename));
4487 * [RewriteRules in directory context]
4489 static int hook_fixup(request_rec *r)
4491 rewrite_perdir_conf *dconf;
4501 dconf = (rewrite_perdir_conf *)ap_get_module_config(r->per_dir_config,
4504 /* if there is no per-dir config we return immediately */
4505 if (dconf == NULL) {
4509 /* if there are no real (i.e. no RewriteRule directives!)
4510 per-dir config of us, we return also immediately */
4511 if (dconf->directory == NULL) {
4518 is_proxyreq = ( r->proxyreq && r->filename
4519 && !strncmp(r->filename, "proxy:", 6));
4522 * .htaccess file is called before really entering the directory, i.e.:
4523 * URL: http://localhost/foo and .htaccess is located in foo directory
4524 * Ignore such attempts, since they may lead to undefined behaviour.
4527 l = strlen(dconf->directory) - 1;
4528 if (r->filename && strlen(r->filename) == l &&
4529 (dconf->directory)[l] == '/' &&
4530 !strncmp(r->filename, dconf->directory, l)) {
4536 * only do something under runtime if the engine is really enabled,
4537 * for this directory, else return immediately!
4539 if (dconf->state == ENGINE_DISABLED) {
4544 * Do the Options check after engine check, so
4545 * the user is able to explicitely turn RewriteEngine Off.
4547 if (!(ap_allow_options(r) & (OPT_SYM_LINKS | OPT_SYM_OWNER))) {
4548 /* FollowSymLinks is mandatory! */
4549 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
4550 "Options FollowSymLinks or SymLinksIfOwnerMatch is off "
4551 "which implies that RewriteRule directive is forbidden: "
4553 return HTTP_FORBIDDEN;
4557 * remember the current filename before rewriting for later check
4558 * to prevent deadlooping because of internal redirects
4559 * on final URL/filename which can be equal to the inital one.
4560 * also, we'll restore original r->filename if we decline this
4563 ofilename = r->filename;
4565 if (r->filename == NULL) {
4566 r->filename = apr_pstrdup(r->pool, r->uri);
4567 rewritelog((r, 2, dconf->directory, "init rewrite engine with"
4568 " requested uri %s", r->filename));
4572 * now apply the rules ...
4574 rulestatus = apply_rewrite_list(r, dconf->rewriterules, dconf->directory);
4578 if (ACTION_STATUS == rulestatus) {
4581 r->status = HTTP_OK;
4585 l = strlen(r->filename);
4586 if (l > 6 && strncmp(r->filename, "proxy:", 6) == 0) {
4587 /* it should go on as an internal proxy request */
4589 /* make sure the QUERY_STRING and
4590 * PATH_INFO parts get incorporated
4591 * (r->path_info was already appended by the
4592 * rewriting engine because of the per-dir context!)
4594 if (r->args != NULL) {
4595 r->filename = apr_pstrcat(r->pool, r->filename,
4596 "?", r->args, NULL);
4599 /* now make sure the request gets handled by the proxy handler */
4600 if (PROXYREQ_NONE == r->proxyreq) {
4601 r->proxyreq = PROXYREQ_REVERSE;
4603 r->handler = "proxy-server";
4605 rewritelog((r, 1, dconf->directory, "go-ahead with proxy request "
4606 "%s [OK]", r->filename));
4609 else if ((skip = is_absolute_uri(r->filename)) > 0) {
4610 /* it was finally rewritten to a remote URL */
4612 /* because we are in a per-dir context
4613 * first try to replace the directory with its base-URL
4614 * if there is a base-URL available
4616 if (dconf->baseurl != NULL) {
4617 /* skip 'scheme://' */
4618 cp = r->filename + skip;
4620 if ((cp = ap_strchr(cp, '/')) != NULL && *(++cp)) {
4621 rewritelog((r, 2, dconf->directory,
4622 "trying to replace prefix %s with %s",
4623 dconf->directory, dconf->baseurl));
4625 /* I think, that hack needs an explanation:
4627 * mod_rewrite was written for unix systems, were
4628 * absolute file-system paths start with a slash.
4629 * URL-paths _also_ start with slashes, so they
4630 * can be easily compared with system paths.
4632 * the following assumes, that the actual url-path
4633 * may be prefixed by the current directory path and
4634 * tries to replace the system path with the RewriteBase
4636 * That assumption is true if we use a RewriteRule like
4638 * RewriteRule ^foo bar [R]
4640 * (see apply_rewrite_rule function)
4641 * However on systems that don't have a / as system
4642 * root this will never match, so we skip the / after the
4643 * hostname and compare/substitute only the stuff after it.
4645 * (note that cp was already increased to the right value)
4647 cp2 = subst_prefix_path(r, cp, (*dconf->directory == '/')
4648 ? dconf->directory + 1
4650 dconf->baseurl + 1);
4651 if (strcmp(cp2, cp) != 0) {
4653 r->filename = apr_pstrcat(r->pool, r->filename,
4659 /* now prepare the redirect... */
4660 if (rulestatus != ACTION_NOESCAPE) {
4661 rewritelog((r, 1, dconf->directory, "escaping %s for redirect",
4663 r->filename = escape_absolute_uri(r->pool, r->filename, skip);
4666 /* append the QUERY_STRING part */
4668 r->filename = apr_pstrcat(r->pool, r->filename, "?",
4669 (rulestatus == ACTION_NOESCAPE)
4671 : ap_escape_uri(r->pool, r->args),
4675 /* determine HTTP redirect response code */
4676 if (ap_is_HTTP_REDIRECT(r->status)) {
4678 r->status = HTTP_OK; /* make Apache kernel happy */
4681 n = HTTP_MOVED_TEMPORARILY;
4684 /* now do the redirection */
4685 apr_table_setn(r->headers_out, "Location", r->filename);
4686 rewritelog((r, 1, dconf->directory, "redirect to %s [REDIRECT/%d]",
4691 /* it was finally rewritten to a local path */
4693 /* if someone used the PASSTHROUGH flag in per-dir
4694 * context we just ignore it. It is only useful
4695 * in per-server context
4697 if (l > 12 && strncmp(r->filename, "passthrough:", 12) == 0) {
4698 r->filename = apr_pstrdup(r->pool, r->filename+12);
4701 /* the filename must be either an absolute local path or an
4702 * absolute local URL.
4704 if ( *r->filename != '/'
4705 && !ap_os_is_path_absolute(r->pool, r->filename)) {
4706 return HTTP_BAD_REQUEST;
4709 /* Check for deadlooping:
4710 * At this point we KNOW that at least one rewriting
4711 * rule was applied, but when the resulting URL is
4712 * the same as the initial URL, we are not allowed to
4713 * use the following internal redirection stuff because
4714 * this would lead to a deadloop.
4716 if (ofilename != NULL && strcmp(r->filename, ofilename) == 0) {
4717 rewritelog((r, 1, dconf->directory, "initial URL equal rewritten"
4718 " URL: %s [IGNORING REWRITE]", r->filename));
4722 /* if there is a valid base-URL then substitute
4723 * the per-dir prefix with this base-URL if the
4724 * current filename still is inside this per-dir
4725 * context. If not then treat the result as a
4728 if (dconf->baseurl != NULL) {
4729 rewritelog((r, 2, dconf->directory, "trying to replace prefix "
4730 "%s with %s", dconf->directory, dconf->baseurl));
4732 r->filename = subst_prefix_path(r, r->filename,
4737 /* if no explicit base-URL exists we assume
4738 * that the directory prefix is also a valid URL
4739 * for this webserver and only try to remove the
4740 * document_root if it is prefix
4742 if ((ccp = ap_document_root(r)) != NULL) {
4743 /* strip trailing slash */
4745 if (ccp[l-1] == '/') {
4748 if (!strncmp(r->filename, ccp, l) &&
4749 r->filename[l] == '/') {
4750 rewritelog((r, 2,dconf->directory, "strip document_root"
4751 " prefix: %s -> %s", r->filename,
4754 r->filename = apr_pstrdup(r->pool, r->filename+l);
4759 /* now initiate the internal redirect */
4760 rewritelog((r, 1, dconf->directory, "internal redirect with %s "
4761 "[INTERNAL REDIRECT]", r->filename));
4762 r->filename = apr_pstrcat(r->pool, "redirect:", r->filename, NULL);
4763 r->handler = "redirect-handler";
4768 rewritelog((r, 1, dconf->directory, "pass through %s", r->filename));
4769 r->filename = ofilename;
4776 * [T=...,H=...] execution
4778 static int hook_mimetype(request_rec *r)
4783 t = apr_table_get(r->notes, REWRITE_FORCED_MIMETYPE_NOTEVAR);
4785 rewritelog((r, 1, NULL, "force filename %s to have MIME-type '%s'",
4788 ap_set_content_type(r, t);
4792 t = apr_table_get(r->notes, REWRITE_FORCED_HANDLER_NOTEVAR);
4794 rewritelog((r, 1, NULL, "force filename %s to have the "
4795 "Content-handler '%s'", r->filename, t));
4805 * "content" handler for internal redirects
4807 static int handler_redirect(request_rec *r)
4809 if (strcmp(r->handler, "redirect-handler")) {
4813 /* just make sure that we are really meant! */
4814 if (strncmp(r->filename, "redirect:", 9) != 0) {
4818 /* now do the internal redirect */
4819 ap_internal_redirect(apr_pstrcat(r->pool, r->filename+9,
4820 r->args ? "?" : NULL, r->args, NULL), r);
4822 /* and return gracefully */
4828 * +-------------------------------------------------------+
4830 * | Module paraphernalia
4832 * +-------------------------------------------------------+
4835 static const command_rec command_table[] = {
4836 AP_INIT_FLAG( "RewriteEngine", cmd_rewriteengine, NULL, OR_FILEINFO,
4837 "On or Off to enable or disable (default) the whole "
4838 "rewriting engine"),
4839 AP_INIT_ITERATE( "RewriteOptions", cmd_rewriteoptions, NULL, OR_FILEINFO,
4840 "List of option strings to set"),
4841 AP_INIT_TAKE1( "RewriteBase", cmd_rewritebase, NULL, OR_FILEINFO,
4842 "the base URL of the per-directory context"),
4843 AP_INIT_RAW_ARGS("RewriteCond", cmd_rewritecond, NULL, OR_FILEINFO,
4844 "an input string and a to be applied regexp-pattern"),
4845 AP_INIT_RAW_ARGS("RewriteRule", cmd_rewriterule, NULL, OR_FILEINFO,
4846 "an URL-applied regexp-pattern and a substitution URL"),
4847 AP_INIT_TAKE2( "RewriteMap", cmd_rewritemap, NULL, RSRC_CONF,
4848 "a mapname and a filename"),
4852 static void ap_register_rewrite_mapfunc(char *name, rewrite_mapfunc_t *func)
4854 apr_hash_set(mapfunc_hash, name, strlen(name), (const void *)func);
4857 static void register_hooks(apr_pool_t *p)
4859 /* fixup after mod_proxy, so that the proxied url will not
4860 * escaped accidentally by mod_proxy's fixup.
4862 static const char * const aszPre[]={ "mod_proxy.c", NULL };
4864 /* make the hashtable before registering the function, so that
4865 * other modules are prevented from accessing uninitialized memory.
4867 mapfunc_hash = apr_hash_make(p);
4868 APR_REGISTER_OPTIONAL_FN(ap_register_rewrite_mapfunc);
4870 ap_hook_handler(handler_redirect, NULL, NULL, APR_HOOK_MIDDLE);
4871 ap_hook_pre_config(pre_config, NULL, NULL, APR_HOOK_MIDDLE);
4872 ap_hook_post_config(post_config, NULL, NULL, APR_HOOK_MIDDLE);
4873 ap_hook_child_init(init_child, NULL, NULL, APR_HOOK_MIDDLE);
4875 ap_hook_fixups(hook_fixup, aszPre, NULL, APR_HOOK_FIRST);
4876 ap_hook_fixups(hook_mimetype, NULL, NULL, APR_HOOK_LAST);
4877 ap_hook_translate_name(hook_uri2file, NULL, NULL, APR_HOOK_FIRST);
4880 /* the main config structure */
4881 AP_DECLARE_MODULE(rewrite) = {
4882 STANDARD20_MODULE_STUFF,
4883 config_perdir_create, /* create per-dir config structures */
4884 config_perdir_merge, /* merge per-dir config structures */
4885 config_server_create, /* create per-server config structures */
4886 config_server_merge, /* merge per-server config structures */
4887 command_table, /* table of config file commands */
4888 register_hooks /* register hooks */