mod_rewrite.c revision 11f2c481e1d57bedb3f758565307501e9a2730dd
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* _ _ _
* _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
* | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
* | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
* |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
* |_____|
*
* URL Rewriting Module
*
* This module uses a rule-based rewriting engine (based on a
* regular-expression parser) to rewrite requested URLs on the fly.
*
* It supports an unlimited number of additional rule conditions (which can
* operate on a lot of variables, even on HTTP headers) for granular
* matching and even external database lookups (either via plain text
* tables, DBM hash files or even external processes) for advanced URL
* substitution.
*
* It operates on the full URLs (including the PATH_INFO part) both in
* per-server context (httpd.conf) and per-dir context (.htaccess) and even
* can generate QUERY_STRING parts on result. The rewriting result finally
* can lead to internal subprocessing, external request redirection or even
* to internal proxy throughput.
*
* This module was originally written in April 1996 and
* gifted exclusively to the The Apache Software Foundation in July 1997 by
*
* Ralf S. Engelschall
* rse engelschall.com
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_hash.h"
#include "apr_user.h"
#include "apr_lib.h"
#include "apr_signal.h"
#include "apr_global_mutex.h"
#include "apr_dbm.h"
#include "apr_dbd.h"
#include "mod_dbd.h"
#if APR_HAS_THREADS
#include "apr_thread_mutex.h"
#endif
#define APR_WANT_MEMFUNC
#define APR_WANT_STRFUNC
#define APR_WANT_IOVEC
#include "apr_want.h"
/* XXX: Do we really need these headers? */
#include <unistd.h>
#endif
#endif
#include <stdarg.h>
#endif
#include <stdlib.h>
#endif
#if APR_HAVE_CTYPE_H
#include <ctype.h>
#endif
#endif
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_log.h"
#include "http_protocol.h"
#include "http_vhost.h"
#include "util_mutex.h"
#include "mod_ssl.h"
#include "mod_rewrite.h"
/*
* in order to improve performance on running production systems, you
* may strip all rewritelog code entirely from mod_rewrite by using the
* -DREWRITELOG_DISABLED compiler option.
*
* DO NOT USE THIS OPTION FOR PUBLIC BINARY RELEASES. Otherwise YOU are
* responsible for answering all the mod_rewrite questions out there.
*/
#ifndef REWRITELOG_DISABLED
#define rewritelog(x) do_rewritelog x
#else /* !REWRITELOG_DISABLED */
#define rewritelog(x)
#endif /* REWRITELOG_DISABLED */
/* remembered mime-type for [T=...] */
#define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
#define REWRITE_FORCED_HANDLER_NOTEVAR "rewrite-forced-handler"
#define ENVVAR_SCRIPT_URL "SCRIPT_URL"
#define ENVVAR_SCRIPT_URI "SCRIPT_URI"
#define CONDFLAG_NONE 1<<0
#define RULEFLAG_NONE 1<<0
/* return code of the rewrite rule
* the result may be escaped - or not
*/
#define ACTION_NORMAL 1<<0
#define MAPTYPE_TXT 1<<0
#define ENGINE_DISABLED 1<<0
#define OPTION_NONE 1<<0
#ifndef RAND_MAX
#define RAND_MAX 32767
#endif
/* max cookie size in rfc 2109 */
#define MAX_COOKIE_LEN 4096
/* max line length (incl.\n) in text rewrite maps */
#ifndef REWRITE_MAX_TXT_MAP_LINE
#define REWRITE_MAX_TXT_MAP_LINE 1024
#endif
/* buffer length for prg rewrite maps */
#ifndef REWRITE_PRG_MAP_BUF
#define REWRITE_PRG_MAP_BUF 1024
#endif
/* for better readbility */
#define LEFT_CURLY '{'
#define RIGHT_CURLY '}'
/*
* expansion result items on the stack to save some cycles
*
* (5 == about 2 variables like "foo%{var}bar%{var}baz")
*/
#define SMALL_EXPANSION 5
/*
* check that a subrequest won't cause infinite recursion
*
* either not in a subrequest, or in a subrequest
*/
/*
* +-------------------------------------------------------+
* | |
* | Types and Structures
* | |
* +-------------------------------------------------------+
*/
typedef struct {
const char *datafile; /* filename for map data files */
const char *dbmtype; /* dbm type for dbm map data files */
const char *checkfile; /* filename to check for map existence */
int type; /* the type of the map */
char *);
char **argv; /* argv of the external rewrite map */
const char *dbdq; /* SQL SELECT statement for rewritemap */
const char *checkfile2; /* filename to check for map existence
NULL if only one file */
/* special pattern types for RewriteCond */
typedef enum {
CONDPAT_REGEX = 0,
} pattern_type;
typedef struct {
char *input; /* Input string of RewriteCond */
char *pattern; /* the RegExp pattern string */
int flags; /* Flags which control the match */
/* single linked list for env vars and cookies */
typedef struct data_item {
char *data;
} data_item;
typedef struct {
char *pattern; /* the RegExp pattern string */
char *output; /* the Substitution string */
int flags; /* Flags which control the substitution */
char *forced_mimetype; /* forced MIME type of substitution */
char *forced_handler; /* forced content handler of subst. */
int forced_responsecode; /* forced HTTP response status */
int skip; /* number of next rules to skip */
typedef struct {
int state; /* the RewriteEngine state */
int options; /* the RewriteOption state */
#ifndef REWRITELOG_DISABLED
const char *rewritelogfile; /* the RewriteLog filename */
int rewriteloglevel; /* the RewriteLog level of verbosity */
#endif
typedef struct {
int state; /* the RewriteEngine state */
int options; /* the RewriteOption state */
char *directory; /* the directory where it applies */
const char *baseurl; /* the base-URL where it applies */
/* the (per-child) cache structures.
*/
typedef struct cache {
#if APR_HAS_THREADS
#endif
} cache;
/* cached maps contain an mtime for the whole map and live in a subpool
* of the cachep->pool. That makes it easy to forget them if necessary.
*/
typedef struct {
} cachedmap;
/* the regex structure for the
* substitution of backreferences
*/
typedef struct backrefinfo {
char *source;
int nsub;
} backrefinfo;
/* single linked list used for
* variable expansion
*/
typedef struct result_list {
struct result_list *next;
const char *string;
} result_list;
/* context structure for variable lookup and expansion
*/
typedef struct {
request_rec *r;
const char *uri;
const char *vary_this;
const char *vary;
char *perdir;
} rewrite_ctx;
/*
* +-------------------------------------------------------+
* | |
* | static module data
* | |
* +-------------------------------------------------------+
*/
/* the global module structure */
/* rewritemap int: handler function registry */
static apr_hash_t *mapfunc_hash;
/* the cache */
/* whether proxy module is available or not */
static int proxy_available;
/* whether random seed can be reaped */
static int rewrite_rand_init_done = 0;
const char *rewritemap_mutex_type = "rewrite-map";
/* Optional functions imported from mod_ssl when loaded: */
/*
* +-------------------------------------------------------+
* | |
* | rewriting logfile support
* | |
* +-------------------------------------------------------+
*/
#ifndef REWRITELOG_DISABLED
static char *current_logtime(request_rec *r)
{
char tstr[80];
apr_time_exp_lt(&t, apr_time_now());
}
{
const char *fname;
/* - no logfile configured
* - logfilename empty
* - virtual log shared w/ main server
*/
return 1;
}
if (!fname) {
"mod_rewrite: Invalid RewriteLog "
return 0;
}
"mod_rewrite: could not open reliable pipe "
"to RewriteLog filter %s", fname);
return 0;
}
}
else {
if (!fname) {
"mod_rewrite: Invalid RewriteLog "
return 0;
}
!= APR_SUCCESS) {
"mod_rewrite: could not open RewriteLog "
"file %s", fname);
return 0;
}
}
return 1;
}
const char *fmt, ...)
const char *fmt, ...)
{
int redir;
!AP_REWRITE_LOG_ENABLED()) {
return;
}
rname = ap_get_remote_logname(r);
++redir;
}
"(%d) %s%s%s%s" APR_EOL_STR,
current_logtime(r),
(void *)(r->server),
(void *)r,
text);
return;
return;
}
#endif /* !REWRITELOG_DISABLED */
/*
* +-------------------------------------------------------+
* | |
* | URI and path functions
* | |
* +-------------------------------------------------------+
*/
/* return number of chars of the scheme (incl. '://')
* if the URI is absolute (includes a scheme etc.)
* otherwise 0.
*
* NOTE: If you add new schemes here, please have a
* look at escape_absolute_uri and splitout_queryargs.
* Not every scheme takes query strings and some schemes
* may be handled in a special way.
*
* XXX: we may consider a scheme registry, perhaps with
* appropriate escape callbacks to allow other modules
* to extend mod_rewrite at runtime.
*/
static unsigned is_absolute_uri(char *uri)
{
/* fast exit */
return 0;
}
switch (*uri++) {
case 'a':
case 'A':
return 6;
}
break;
case 'b':
case 'B':
return 11;
}
break;
case 'f':
case 'F':
return 6;
}
return 7;
}
break;
case 'g':
case 'G':
return 9;
}
break;
case 'h':
case 'H':
return 7;
}
return 8;
}
break;
case 'l':
case 'L':
return 7;
}
break;
case 'm':
case 'M':
return 7;
}
break;
case 'n':
case 'N':
return 5;
}
return 7;
}
break;
case 's':
case 'S':
return 7;
}
break;
}
return 0;
}
static const char c2x_table[] = "0123456789abcdef";
unsigned char *where)
{
#endif /*APR_CHARSET_EBCDIC*/
return where;
}
/*
* Escapes a uri in a similar way as php's urlencode does.
*/
const unsigned char *s = (const unsigned char *)path;
unsigned char *d = (unsigned char *)copy;
unsigned c;
while ((c = *s)) {
if (apr_isalnum(c) || c == '_') {
*d++ = c;
}
else if (c == ' ') {
*d++ = '+';
}
else {
d = c2x(c, '%', d);
}
++s;
}
*d = '\0';
return copy;
}
/*
* escape absolute uri, which may or may not be path oriented.
* So let's handle them differently.
*/
{
char *cp;
/* be safe.
* NULL should indicate elsewhere, that something's wrong
*/
return NULL;
}
/* scheme with authority part? */
/* skip host part */
++cp;
}
/* nothing after the hostpart. ready! */
return apr_pstrdup(p, uri);
}
/* remember the hostname stuff */
/* special thing for ldap.
* The parts are separated by question marks. From RFC 2255:
* ldapurl = scheme "://" [hostport] ["/"
* [dn ["?" [attributes] ["?" [scope]
* ["?" [filter] ["?" extensions]]]]]]
*/
char *token[5];
int c = 0;
while (*cp && c < 4) {
if (*cp == '?') {
*cp = '\0';
}
++cp;
}
ap_escape_uri(p, token[0]),
NULL);
}
}
/* Nothing special here. Apply normal escaping. */
}
/*
* split out a QUERY_STRING part from
* the current URI string
*/
{
char *q;
/* don't touch, unless it's an http or mailto URL.
* See RFC 1738 and RFC 2368.
*/
if (is_absolute_uri(r->filename)
return;
}
if (q != NULL) {
char *olduri;
*q++ = '\0';
if (qsappend) {
}
else {
}
if (!len) {
}
}
}
return;
}
/*
* strip 'http[s]://ourhost/' from URI
*/
static void reduce_uri(request_rec *r)
{
char *cp;
apr_size_t l;
cp = (char *)ap_http_scheme(r);
&& r->filename[l] == ':'
unsigned short port;
/* cut the hostname and port out of the URI */
++cp;
}
*cp++ = '\0';
++cp;
}
*cp = '\0';
if (!*url) {
url = "/";
}
}
*cp = '\0';
port = ap_default_port(r);
}
else {
port = ap_default_port(r);
url = "/";
}
/* now check whether we could reduce it to a local path... */
}
}
return;
}
/*
* add 'http[s]://ourhost[:ourport]/' to URI
* if URI is still not fully qualified
*/
static void fully_qualify_uri(request_rec *r)
{
if (r->method_number == M_CONNECT) {
return;
}
else if (!is_absolute_uri(r->filename)) {
const char *thisserver;
char *thisport;
int port;
thisserver = ap_get_server_name(r);
port = ap_get_server_port(r);
? ""
r->filename);
}
return;
}
/*
* stat() only the first segment of a path
*/
{
const char *root;
const char *slash;
char *statpath;
if (rv != APR_SUCCESS) {
return 0;
}
/* let's recognize slashes only, the mod_rewrite semantics are opaque
* enough.
*/
}
else {
}
if (rv == APR_SUCCESS) {
return 1;
}
}
return 0;
}
/*
* substitute the prefix path 'match' in 'input' with 'subst' (RewriteBase)
*/
const char *subst)
{
--len;
}
char *output;
++slen;
}
}
output));
return output;
}
/* prefix didn't match */
return input;
}
/*
* +-------------------------------------------------------+
* | |
* | caching support
* | |
* +-------------------------------------------------------+
*/
char *val)
{
if (cachep) {
#if APR_HAS_THREADS
#endif
if (!map) {
apr_pool_t *p;
#if APR_HAS_THREADS
#endif
return;
}
}
}
/* Now we should have a valid map->entries hash, where we
* can store our value.
*
* We need to copy the key and the value into OUR pool,
* so that we don't leave it during the r->pool cleanup.
*/
#if APR_HAS_THREADS
#endif
}
return;
}
apr_pool_t *p)
{
if (cachep) {
#if APR_HAS_THREADS
#endif
if (map) {
/* if this map is outdated, forget it. */
}
else {
if (val) {
/* copy the cached value into the supplied pool,
* where it belongs (r->pool usually)
*/
}
}
}
#if APR_HAS_THREADS
#endif
}
return val;
}
static int init_cache(apr_pool_t *p)
{
return 0;
}
#if APR_HAS_THREADS
#endif
return 1;
}
/*
* +-------------------------------------------------------+
* | |
* | Map Functions
* | |
* +-------------------------------------------------------+
*/
/*
* General Note: key is already a fresh string, created (expanded) just
* for the purpose to be passed in here. So one can modify key itself.
*/
{
char *p;
for (p = key; *p; ++p) {
*p = apr_toupper(*p);
}
return key;
}
{
return key;
}
{
}
{
return key;
}
{
char *p = value;
unsigned n = 1;
/* count number of distinct values */
++n;
++p;
}
if (n > 1) {
/* initialize random generator
*
* XXX: Probably this should be wrapped into a thread mutex,
* shouldn't it? Is it worth the effort?
*/
if (!rewrite_rand_init_done) {
}
/* select a random subvalue */
/* extract it from the whole string */
++value;
}
if (value) { /* should not be NULL, but ... */
if (p) {
*p = '\0';
}
}
}
return value;
}
/* child process code */
const char *desc)
{
}
apr_file_t **fpout,
apr_file_t **fpin)
{
ap_make_dirstr_parent(p, argv[0])))
procattr, p);
if (rc == APR_SUCCESS) {
if (fpin) {
}
if (fpout) {
}
}
}
return (rc);
}
{
/* If the engine isn't turned on,
* don't even try to do anything.
*/
return APR_SUCCESS;
}
void *val;
continue;
}
continue;
}
"mod_rewrite: could not start RewriteMap "
return rc;
}
}
return APR_SUCCESS;
}
/*
* +-------------------------------------------------------+
* | |
* | Lookup functions
* | |
* +-------------------------------------------------------+
*/
{
r->pool) != APR_SUCCESS) {
return NULL;
}
char *p, *c;
/* ignore comments and lines starting with whitespaces */
continue;
}
p = line;
c = key;
while (c < keylast && *p == *c && !apr_isspace(*p)) {
++p;
++c;
}
/* key doesn't match - ignore. */
if (c != keylast || !apr_isspace(*p)) {
continue;
}
/* jump to the value */
while (*p && apr_isspace(*p)) {
++p;
}
/* no value? ignore */
if (!*p) {
continue;
}
/* extract the value and return. */
c = p;
while (*p && !apr_isspace(*p)) {
++p;
}
break;
}
return value;
}
{
char *value;
r->pool) != APR_SUCCESS) {
return NULL;
}
}
else {
}
return value;
}
{
const char *errmsg;
int n = 0;
if (rv != 0) {
return NULL;
}
++n;
}
else {
/* randomise crudely amongst multiple results */
}
}
}
if (rv != -1) {
}
switch (n) {
case 0:
return NULL;
case 1:
default:
/* what's a fair rewritelog level for this? */
}
}
{
char *buf;
char c;
const char *eol = APR_EOL_STR;
apr_size_t eolc = 0;
int found_nl = 0;
#ifndef NO_WRITEV
#endif
/* when `RewriteEngine off' was used in the per-server
* context then the rewritemap-programs were not spawned.
* In this case using such a map (usually in per-dir context)
* is useless because it is not available.
*
* newlines in the key leave bytes in the pipe and cause
* bad things to happen (next map lookup will use the chars
* after the \n instead of the new key etc etc - in other words,
* the Rewritemap falls out of sync with the requests).
*/
return NULL;
}
/* take the lock */
if (rewrite_mapr_lock_acquire) {
if (rv != APR_SUCCESS) {
"apr_global_mutex_lock(rewrite_mapr_lock_acquire) "
"failed");
return NULL; /* Maybe this should be fatal? */
}
}
/* write out the request key */
#ifdef NO_WRITEV
nbytes = 1;
#else
niov = 2;
#endif
/* read in the response value */
nbytes = 1;
do {
i = 0;
/* remove eol from the buffer */
--eolc;
if (i < eolc) {
i = 0;
}
else {
i -= eolc;
}
++found_nl;
break;
}
}
/* only partial (invalid) eol sequence -> reset the counter */
else if (eolc) {
eolc = 0;
}
/* catch binary mode, e.g. on Win32 */
else if (c == '\n') {
++found_nl;
break;
}
buf[i++] = c;
}
/* well, if there wasn't a newline yet, we need to read further */
if (!buflist) {
}
else if (i) {
}
if (i) {
combined_len += i;
}
i = 0;
continue;
}
}
break;
} while (1);
/* concat the stuff */
if (buflist) {
char *p;
while (buflist) {
}
}
*p = '\0';
i = combined_len;
}
else {
buf[i] = '\0';
}
/* give the lock back */
if (rewrite_mapr_lock_acquire) {
if (rv != APR_SUCCESS) {
"apr_global_mutex_unlock(rewrite_mapr_lock_acquire) "
"failed");
return NULL; /* Maybe this should be fatal? */
}
}
/* catch the "failed" case */
return NULL;
}
return buf;
}
/*
* generic map lookup
*/
{
rewritemap_entry *s;
char *value;
/* get map configuration */
/* map doesn't exist */
if (!s) {
return NULL;
}
switch (s->type) {
/*
* Text file map (perhaps random)
*/
case MAPTYPE_RND:
case MAPTYPE_TXT:
if (rv != APR_SUCCESS) {
"mod_rewrite: can't access text RewriteMap file %s",
s->checkfile);
"can't open RewriteMap file, see error log"));
return NULL;
}
if (!value) {
"cache lookup FAILED, forcing new map lookup"));
if (!value) {
return NULL;
}
}
else {
}
}
/*
* DBM file map
*/
case MAPTYPE_DBM:
if (rv != APR_SUCCESS) {
"mod_rewrite: can't access DBM RewriteMap file %s",
s->checkfile);
}
else if(s->checkfile2 != NULL) {
if (rv != APR_SUCCESS) {
"mod_rewrite: can't access DBM RewriteMap "
"file %s", s->checkfile2);
}
}
}
if(rv != APR_SUCCESS) {
"can't open DBM RewriteMap file, see error log"));
return NULL;
}
if (!value) {
"cache lookup FAILED, forcing new map lookup"));
if (!value) {
return NULL;
}
return value;
}
/*
* SQL map without cache
*/
case MAPTYPE_DBD:
if (!value) {
return NULL;
}
return value;
/*
* SQL map with cache
*/
case MAPTYPE_DBD_CACHE:
if (!value) {
"cache lookup FAILED, forcing new map lookup"));
if (!value) {
return NULL;
}
return value;
}
/*
* Program file map
*/
case MAPTYPE_PRG:
if (!value) {
key));
return NULL;
}
return value;
/*
* Internal Map
*/
case MAPTYPE_INT:
if (!value) {
key));
return NULL;
}
return value;
}
return NULL;
}
/*
* lookup a HTTP header and set VARY note
*/
{
if (val) {
}
return val;
}
/*
* lookahead helper function
* Determine the correct URI path in perdir context
*/
{
}
}
/*
* generic variable lookup
*/
{
const char *result;
request_rec *r = ctx->r;
/* fast exit */
if (varlen < 4) {
}
/* fast tests for variable length variables (sic) first */
var += 4;
if (!result) {
}
if (!result) {
}
}
var + 4);
}
}
if (var[5]) {
const char *path;
}
ctx->r = r;
return (char *)result;
}
}
/* sigh, the user wants a file based subrequest, but
* we can't do one, since we don't know what the file
* path is! In this case behave like LA-U.
*/
}
else {
NULL);
}
}
ctx->r = r;
return (char *)result;
}
}
}
}
/* well, do it the hard way */
else {
char *p;
/* can't do this above, because of the getenv call */
for (p = var; *p; ++p) {
*p = apr_toupper(*p);
}
switch (varlen) {
case 4:
return (char *)result;
}
#if APR_HAVE_IPV6
#else
#endif
}
break;
case 5:
}
break;
case 8:
switch (var[6]) {
case 'A':
}
break;
case 'E':
}
break;
case 'I':
}
break;
case 'O':
}
break;
}
break;
case 9:
switch (var[7]) {
case 'A':
}
}
break;
case 'E':
}
break;
case 'F':
}
break;
case 'P':
result = r->ap_auth_type;
}
break;
case 'S':
}
break;
case 'U':
}
break;
}
break;
case 11:
switch (var[8]) {
case 'A':
result = ap_get_server_name(r);
}
break;
case 'D':
}
}
break;
case 'E':
}
result = r->the_request;
}
break;
case 'I':
}
break;
case 'K':
}
break;
case 'O':
}
REMOTE_NAME, NULL);
}
}
break;
case 'S':
}
result = "<unknown>";
r->pool);
}
}
break;
case 'U':
}
break;
}
break;
case 12:
switch (var[3]) {
case 'I':
result = "<unknown>";
r->pool);
}
}
break;
case 'O':
result = ap_get_remote_logname(r);
}
break;
case 'P':
}
break;
case 'R':
}
break;
case 'V':
}
break;
}
break;
case 13:
result = ap_document_root(r);
}
break;
case 14:
}
}
break;
case 15:
switch (var[7]) {
case 'E':
}
break;
case 'F':
}
break;
case 'P':
}
break;
case 'S':
}
break;
}
break;
case 16:
}
break;
case 21:
}
break;
}
}
}
/*
* +-------------------------------------------------------+
* | |
* | Expansion functions
* | |
* +-------------------------------------------------------+
*/
/*
* Bracketed expression handling
* s points after the opening bracket
*/
static APR_INLINE char *find_closing_curly(char *s)
{
unsigned depth;
for (depth = 1; *s; ++s) {
if (*s == RIGHT_CURLY && --depth == 0) {
return s;
}
else if (*s == LEFT_CURLY) {
++depth;
}
}
return NULL;
}
static APR_INLINE char *find_char_in_curlies(char *s, int c)
{
unsigned depth;
for (depth = 1; *s; ++s) {
if (*s == c && depth == 1) {
return s;
}
else if (*s == RIGHT_CURLY && --depth == 0) {
return NULL;
}
else if (*s == LEFT_CURLY) {
++depth;
}
}
return NULL;
}
/* perform all the expansions on the input string
* putting the result into a new string
*
* for security reasons this expansion must be performed in a
* single pass, otherwise an attacker can arrange for the result
* of an earlier expansion to include expansion specifiers that
* are interpreted by a later expansion, producing results that
* were not intended by the administrator.
*/
{
unsigned spc = 0;
char *p, *c;
/* fast exit */
}
/* well, actually something to do */
/* loop for specials */
do {
/* prepare next entry */
sizeof(result_list));
}
/* escaped character */
if (*p == '\\') {
++outlen;
if (!p[1]) {
break;
}
else {
++p;
}
}
/* variable or map lookup */
else if (p[1] == '{') {
char *endp;
if (!endp) {
outlen += 2;
p += 2;
}
/* variable lookup */
else if (*p == '%') {
p = endp + 1;
}
/* map lookup */
else { /* *p == '$' */
char *key;
/*
* To make rewrite maps useful, the lookup key and
* default values must be expanded, so we make
* recursive calls to do the work. For security
* reasons we must never expand a string that includes
* verbatim data from the network. The recursion here
* isn't a problem because the result of expansion is
* only passed to lookup_map() so it cannot be
* re-expanded, only re-looked-up. Another way of
* looking at it is that the recursion is entirely
* driven by the syntax of the nested curly brackets.
*/
if (!key) {
outlen += 2;
p += 2;
}
else {
*key++ = '\0';
if (dflt) {
*dflt++ = '\0';
}
/* reuse of key variable as result */
}
if (key) {
}
p = endp + 1;
}
}
}
/* backreference */
else if (apr_isdigit(p[1])) {
int n = p[1] - '0';
/* escape the backreference */
} else {
}
}
p += 2;
}
/* not for us, just copy it */
else {
++outlen;
}
/* check the remainder */
sizeof(result_list));
}
p += span;
}
/* assemble result */
do {
* extensive testing and
* review
*/
}
} while (result);
p[outlen] = '\0';
return p;
}
/*
* perform all the expansions on the environment variables
*/
{
while (env) {
*val++ = '\0';
}
}
return;
}
/*
* perform all the expansions on the cookies
*
* TODO: use cached time similar to how logging does it
*/
static void add_cookie(request_rec *r, char *s)
{
char *var;
char *val;
char *domain;
char *expires;
char *path;
char *secure;
char *httponly;
char *tok_cntx;
char *cookie;
request_rec *rmain = r;
char *notename;
void *data;
}
if (!data) {
if (expires) {
long exp_min;
if (exp_min) {
"%.2d:%.2d:%.2d GMT",
}
}
"; domain=", domain,
: NULL,
: NULL,
|| !strcasecmp(secure,
"secure"))) ?
"; secure" : NULL,
|| !strcasecmp(httponly,
"HttpOnly"))) ?
"; HttpOnly" : NULL,
NULL);
}
else {
var));
}
}
return;
}
{
while (cookie) {
}
return;
}
#if APR_HAS_USER
/*
* database information (or other OS-specific database)
*/
{
char *p, *user;
while (*p && *p != '/') {
++p;
}
if (p > user) {
char *homedir;
if (*p) {
/* reuse of user variable */
*user = '\0';
}
}
else {
return homedir;
}
}
}
}
return uri;
}
#endif /* if APR_HAS_USER */
/*
* +-------------------------------------------------------+
* | |
* | rewriting lockfile support
* | |
* +-------------------------------------------------------+
*/
{
/* create the lockfile */
/* XXX See if there are any rewrite map programs before creating
* the mutex.
*/
rewritemap_mutex_type, NULL, s, p, 0);
if (rc != APR_SUCCESS) {
return rc;
}
return APR_SUCCESS;
}
{
/* destroy the rewritelock */
if (rewrite_mapr_lock_acquire) {
}
return(0);
}
/*
* +-------------------------------------------------------+
* | |
* | configuration directive handling
* | |
* +-------------------------------------------------------+
*/
/*
* own command line parser for RewriteRule and RewriteCond,
* which doesn't have the '\\' problem.
* (returns true on error)
*
* XXX: what an inclined parser. Seems we have to leave it so
* for backwards compat. *sigh*
*/
{
char quote;
while (apr_isspace(*str)) {
++str;
}
/*
* determine first argument
*/
break;
}
++str;
continue;
}
}
if (!*str) {
return 1;
}
*str++ = '\0';
while (apr_isspace(*str)) {
++str;
}
/*
* determine second argument
*/
break;
}
++str;
continue;
}
}
if (!*str) {
return 0;
}
*str++ = '\0';
while (apr_isspace(*str)) {
++str;
}
if (!*str) {
return 0;
}
/*
* determine third argument
*/
break;
}
++str;
continue;
}
}
*str = '\0';
return 0;
}
{
a->state = ENGINE_DISABLED;
a->options = OPTION_NONE;
#ifndef REWRITELOG_DISABLED
a->rewritelogfile = NULL;
a->rewritelogfp = NULL;
a->rewriteloglevel = 0;
#endif
a->rewritemaps = apr_hash_make(p);
a->server = s;
return (void *)a;
}
{
a = (rewrite_server_conf *)apr_pcalloc(p,
sizeof(rewrite_server_conf));
if (a->options & OPTION_INHERIT) {
/*
* local directives override
* and anything else is inherited
*/
#ifndef REWRITELOG_DISABLED
: base->rewriteloglevel;
: base->rewritelogfile;
: base->rewritelogfp;
#endif
base->rewritemaps);
base->rewriteconds);
base->rewriterules);
}
else {
/*
* local directives override
* and anything else gets defaults
*/
#ifndef REWRITELOG_DISABLED
#endif
}
return (void *)a;
}
{
a->state = ENGINE_DISABLED;
a->options = OPTION_NONE;
}
else {
/* make sure it has a trailing slash */
}
else {
}
}
return (void *)a;
}
{
a = (rewrite_perdir_conf *)apr_pcalloc(p,
sizeof(rewrite_perdir_conf));
if (a->options & OPTION_INHERIT) {
base->rewriteconds);
base->rewriterules);
}
else {
}
return (void *)a;
}
{
}
else /* is per-directory command */ {
}
return NULL;
}
{
int options = 0;
char *w;
while (*option) {
if (!strcasecmp(w, "inherit")) {
}
"RewriteOptions: MaxRedirects option has been "
"removed in favor of the global "
"LimitInternalRecursion directive and will be "
"ignored.");
}
else {
w, "'", NULL);
}
}
/* put it into the appropriate config */
}
else { /* is per-directory command */
}
return NULL;
}
#ifndef REWRITELOG_DISABLED
{
return NULL;
}
const char *a1)
{
return NULL;
}
#endif /* rewritelog */
const char *a2)
{
const char *fname;
}
}
}
}
}
if (colon) {
}
}
if (!fname) {
}
}
&newmap->checkfile2);
if (rv != APR_SUCCESS) {
}
}
if (dbd_prepare == NULL) {
return "RewriteMap types dbd and fastdbd require mod_dbd!";
}
}
else {
}
}
}
}
}
}
else {
}
}
"RewriteMap: file for map ", a1,
}
return NULL;
}
const char *a1)
{
return "RewriteBase: only valid in per-directory config files";
}
if (a1[0] == '\0') {
return "RewriteBase: empty URL not allowed";
}
if (a1[0] != '/') {
return "RewriteBase: argument is not a valid URL";
}
return NULL;
}
/*
* generic lexer for RewriteRule and RewriteCond flags.
* The parser will be passed in as a function pointer
* and called if a flag was found
*/
const char *(*parse)(apr_pool_t *,
void *,
char *, char *))
{
const char *err;
return "bad flag delimiters";
}
++key;
while (*key) {
/* skip leading spaces */
while (apr_isspace(*key)) {
++key;
}
* happen, but ...
*/
break;
}
/* strip trailing spaces */
while (apr_isspace(*endp)) {
--endp;
}
*++endp = '\0';
/* split key and val */
if (val) {
*val++ = '\0';
}
else {
}
if (err) {
return err;
}
}
return NULL;
}
{
}
}
}
else {
}
return NULL;
}
const char *in_str)
{
char *a1;
char *a2;
char *a3;
const char *err;
/* make a new entry in the internal temporary rewrite rule list */
}
else { /* is per-directory command */
}
/* parse the argument line ourself
* a1 .. a3 are substrings of str, which is a fresh copy
* of the argument line. So we can use a1 .. a3 without
* copying them again.
*/
"'", NULL);
}
/* arg1: the input string */
/* arg3: optional flags field
* (this has to be parsed first, because we need to
* know if the regex should be compiled with ICASE!)
*/
cmd_rewritecond_setflag)) != NULL) {
}
}
/* arg2: the pattern */
if (*a2 == '!') {
++a2;
}
/* determine the pattern type */
switch (a2[1]) {
}
}
else {
switch (*a2) {
/* "" represents an empty string */
a2 += 2;
}
break;
}
}
}
"RewriteCond: NoCase option for non-regex pattern '%s' "
"is not supported and will be ignored.", a2);
}
? AP_REG_ICASE : 0));
if (!regexp) {
}
}
return NULL;
}
{
int error = 0;
switch (*key++) {
case 'b':
case 'B':
}
else {
++error;
}
break;
case 'c':
case 'C':
}
if (!cp) {
}
else {
}
}
}
else {
++error;
}
break;
case 'd':
case 'D':
}
break;
case 'e':
case 'E':
if (!cp) {
}
else {
}
}
}
else {
++error;
}
break;
case 'f':
case 'F':
}
else {
++error;
}
break;
case 'g':
case 'G':
}
else {
++error;
}
break;
case 'h':
case 'H':
}
else {
++error;
}
break;
case 'l':
case 'L':
}
else {
++error;
}
break;
case 'n':
case 'N':
}
}
}
}
else {
++error;
}
break;
case 'p':
case 'P':
}
}
else {
++error;
}
break;
case 'q':
case 'Q':
}
else {
++error;
}
break;
case 'r':
case 'R':
int status = 0;
}
}
}
else if (apr_isdigit(*val)) {
if (status != HTTP_INTERNAL_SERVER_ERROR) {
int idx =
return apr_psprintf(p, "invalid HTTP "
"response code '%s' for "
"flag 'R'",
val);
}
}
if (!ap_is_HTTP_REDIRECT(status)) {
}
}
}
}
else {
++error;
}
break;
case 's':
case 'S':
}
else {
++error;
}
break;
case 't':
case 'T':
}
else {
++error;
}
break;
default:
++error;
break;
}
if (error) {
}
return NULL;
}
const char *in_str)
{
char *a1;
char *a2;
char *a3;
const char *err;
/* make a new entry in the internal rewrite rule list */
}
else { /* is per-directory command */
}
/* parse the argument line ourself */
"'", NULL);
}
/* arg3: optional flags field */
cmd_rewriterule_setflag)) != NULL) {
}
}
/* arg1: the pattern
* try to compile the regexp to test if is ok
*/
if (*a1 == '!') {
++a1;
}
? AP_REG_ICASE : 0));
if (!regexp) {
"RewriteRule: cannot compile regular expression '",
}
/* arg2: the output string */
}
/* now, if the server or per-dir config holds an
* array of RewriteCond entries, we take it for us
* and clear the array
*/
sizeof(rewritecond_entry));
}
else { /* is per-directory command */
sizeof(rewritecond_entry));
}
return NULL;
}
/*
* +-------------------------------------------------------+
* | |
* | the rewriting engine
* | |
* +-------------------------------------------------------+
*/
/* Lexicographic Compare */
static APR_INLINE int compare_lexicography(char *a, char *b)
{
for (i = 0; i < lena; ++i) {
if (a[i] != b[i]) {
return ((unsigned char)a[i] > (unsigned char)b[i]) ? 1 : -1;
}
}
return 0;
}
}
/*
* Apply a single rewriteCond
*/
{
int rc = 0;
switch (p->ptype) {
case CONDPAT_FILE_EXISTS:
rc = 1;
}
break;
case CONDPAT_FILE_SIZE:
rc = 1;
}
break;
case CONDPAT_FILE_LINK:
#if !defined(OS2)
r->pool) == APR_SUCCESS
rc = 1;
}
#endif
break;
case CONDPAT_FILE_DIR:
rc = 1;
}
break;
case CONDPAT_FILE_XBIT:
rc = 1;
}
break;
case CONDPAT_LU_URL:
rc = 1;
}
}
break;
case CONDPAT_LU_FILE:
/* double-check that file exists since default result is 200 */
r->pool) == APR_SUCCESS) {
rc = 1;
}
}
break;
case CONDPAT_STR_GT:
break;
case CONDPAT_STR_LT:
break;
case CONDPAT_STR_EQ:
if (p->flags & CONDFLAG_NOCASE) {
}
else {
}
break;
default:
/* it is really a regexp pattern, so apply it */
/* update briRC backref info */
}
break;
}
if (p->flags & CONDFLAG_NOTMATCH) {
}
return rc;
}
/* check for forced type and handler */
{
char *expanded;
if (p->forced_mimetype) {
if (*expanded) {
expanded);
}
}
if (p->forced_handler) {
if (*expanded) {
expanded);
}
}
}
/*
* Apply a single RewriteRule
*/
{
int i, rc;
request_rec *r = ctx->r;
int is_proxyreq = 0;
/*
* Proxy request?
*/
/* Since we want to match against the (so called) full URL, we have
* to re-add the PATH_INFO postfix
*/
}
/* Additionally we strip the physical path from the url to match
* it independent from the underlaying filesystem.
*/
}
}
/* Try to match the URI against the RewriteRule pattern
* and exit immediately if it didn't apply.
*/
return 0;
}
/* It matched, wow! Now it's time to prepare the context structure for
* further processing
*/
if (p->flags & RULEFLAG_NOTMATCH) {
}
else {
}
/* Ok, we already know the pattern has matched, but we now
* additionally have to check for all existing preconditions
* (RewriteCond) which have to be also true. We do this at
* this very late stage to avoid unnessesary checks which
* would slow down the rewriting engine.
*/
rewriteconds = p->rewriteconds;
for (i = 0; i < rewriteconds->nelts; ++i) {
rewritecond_entry *c = &conds[i];
/*
* Reset vary_this if the novary flag is set for this condition.
*/
if (c->flags & CONDFLAG_NOVARY) {
}
if (c->flags & CONDFLAG_ORNEXT) {
if (!rc) {
/* One condition is false, but another can be still true. */
continue;
}
else {
/* skip the rest of the chained OR conditions */
while ( i < rewriteconds->nelts
&& c->flags & CONDFLAG_ORNEXT) {
c = &conds[++i];
}
}
}
else if (!rc) {
return 0;
}
/* If some HTTP header was involved in the condition, remember it
* for later use
*/
NULL)
}
}
/* expand the result */
if (!(p->flags & RULEFLAG_NOSUB)) {
newuri));
}
/* expand [E=var:val] and [CO=<cookie>] */
/* non-substitution rules ('RewriteRule <pat> -') end here. */
if (p->flags & RULEFLAG_NOSUB) {
force_type_handler(p, ctx);
if (p->flags & RULEFLAG_STATUS) {
p->forced_responsecode, r->filename));
r->status = p->forced_responsecode;
}
return 2;
}
/* Now adjust API's knowledge about r->filename and r->args */
}
/* Add the previously stripped per-directory location prefix, unless
* (1) it's an absolute URL path and
* (2) it's a full qualified URL
*/
&& !is_absolute_uri(r->filename)) {
}
/* If this rule is forced for proxy throughput
* (`RewriteRule ... ... [P]') then emulate mod_proxy's
* URL-to-filename handler to be sure mod_proxy is triggered
* for this URL later in the Apache API. But make sure it is
* a fully-qualified URL. (If not it is qualified with
* ourself).
*/
if (p->flags & RULEFLAG_PROXY) {
/* For rules evaluated in server context, the mod_proxy fixup
* hook can be relied upon to escape the URI as and when
* necessary, since it occurs later. If in directory context,
* the ordering of the fixup hooks is forced such that
* mod_proxy comes first, so the URI must be escaped here
* instead. See PR 39746, 46428, and other headaches. */
char *old_filename = r->filename;
}
r->filename));
return 1;
}
/* If this rule is explicitly forced for HTTP redirection
* (`RewriteRule .. .. [R]') then force an external HTTP
* redirect. But make sure it is a fully-qualified URL. (If
* not it is qualified with ourself).
*/
if (p->flags & RULEFLAG_FORCEREDIRECT) {
r->filename));
r->status = p->forced_responsecode;
return 1;
}
/* Special Rewriting Feature: Self-Reduction
* We reduce the URL by stripping a possible
* http[s]://<ourhost>[:<port>] prefix, i.e. a prefix which
* corresponds to ourself. This is to simplify rewrite maps
* and to avoid recursion, etc. When this prefix is not a
* coincidence then the user has to use [R] explicitly (see
* above).
*/
reduce_uri(r);
/* If this rule is still implicitly forced for HTTP
* redirection (`RewriteRule .. <scheme>://...') then
* directly force an external HTTP redirect.
*/
if (is_absolute_uri(r->filename)) {
r->status = p->forced_responsecode;
return 1;
}
/* Finally remember the forced mime-type */
force_type_handler(p, ctx);
/* Puuhhhhhhhh... WHAT COMPLICATED STUFF ;_)
* But now we're done for this particular rule.
*/
return 1;
}
/*
* Apply a complete rule set,
* i.e. a list of rewrite rules
*/
char *perdir)
{
int i;
int changed;
int rc;
int s;
ctx->r = r;
/*
* Iterate over all existing rules
*/
changed = 0;
loop:
for (i = 0; i < rewriterules->nelts; i++) {
p = &entries[i];
/*
* Ignore this rule on subrequests if we are explicitly
* asked to do so or this is a proxy-throughput or a
* forced redirect rule.
*/
(p->flags & RULEFLAG_IGNOREONSUBREQ ||
p->flags & RULEFLAG_FORCEREDIRECT )) {
continue;
}
/*
* Apply the current rule.
*/
if (rc) {
/* Regardless of what we do next, we've found a match. Check to see
* if any of the request header fields were involved, and add them
* to the Vary field of the response.
*/
}
/*
* The rule sets the response code (implies match-only)
*/
if (p->flags & RULEFLAG_STATUS) {
return ACTION_STATUS;
}
/*
* Indicate a change if this was not a match-only rule.
*/
if (rc != 2) {
? ACTION_NOESCAPE : ACTION_NORMAL);
}
/*
* Pass-Through Feature (`RewriteRule .. .. [PT]'):
* Because the Apache 1.x API is very limited we
* need this hack to pass the rewritten URL to other
* modules like mod_alias, mod_userdir, etc.
*/
if (p->flags & RULEFLAG_PASSTHROUGH) {
"to next API URI-to-filename handler", r->filename));
break;
}
/*
* Stop processing also on proxy pass-through and
* last-rule and new-round flags.
*/
break;
}
/*
* On "new-round" flag we just start from the top of
* the rewriting ruleset again.
*/
if (p->flags & RULEFLAG_NEWROUND) {
goto loop;
}
/*
* If we are forced to skip N next rules, do it now.
*/
if (p->skip > 0) {
s = p->skip;
while ( i < rewriterules->nelts
&& s > 0) {
i++;
p = &entries[i];
s--;
}
}
}
else {
/*
* If current rule is chained with next rule(s),
* skip all this next rule(s)
*/
while ( i < rewriterules->nelts
&& p->flags & RULEFLAG_CHAIN) {
i++;
p = &entries[i];
}
}
}
return changed;
}
/*
* +-------------------------------------------------------+
* | |
* | Module Initialization Hooks
* | |
* +-------------------------------------------------------+
*/
{
/* register int: rewritemap handlers */
if (map_pfn_register) {
}
return OK;
}
static int post_config(apr_pool_t *p,
server_rec *s)
{
void *data;
int first_time = 0;
const char *userdata_key = "rewrite_init_module";
if (!data) {
first_time = 1;
}
/* check if proxy module is available */
rv = rewritelock_create(s, p);
if (rv != APR_SUCCESS) {
return HTTP_INTERNAL_SERVER_ERROR;
}
apr_pool_cleanup_register(p, (void *)s, rewritelock_remove,
/* step through the servers and
* - open each rewriting logfile
* - open the RewriteMap prg:xxx programs
*/
for (; s; s = s->next) {
#ifndef REWRITELOG_DISABLED
if (!open_rewritelog(s, p)) {
return HTTP_INTERNAL_SERVER_ERROR;
}
#endif
if (!first_time) {
if (run_rewritemap_programs(s, p) != APR_SUCCESS) {
return HTTP_INTERNAL_SERVER_ERROR;
}
}
}
return OK;
}
{
if (rewrite_mapr_lock_acquire) {
if (rv != APR_SUCCESS) {
"mod_rewrite: could not init rewrite_mapr_lock_acquire"
" in child");
}
}
/* create the lookup cache */
if (!init_cache(p)) {
"mod_rewrite: could not init map cache in child");
}
}
/*
* +-------------------------------------------------------+
* | |
* | runtime hooks
* | |
* +-------------------------------------------------------+
*/
/*
* URI-to-filename hook
* [deals with RewriteRules in server context]
*/
static int hook_uri2file(request_rec *r)
{
const char *saved_rulestatus;
const char *var;
const char *thisserver;
char *thisport;
const char *thisurl;
unsigned int port;
int rulestatus;
/*
* retrieve the config structures
*/
/*
* only do something under runtime if the engine is really enabled,
* else return immediately!
*/
return DECLINED;
}
/*
* check for the ugly API case of a virtual host section where no
* mod_rewrite directives exists. In this situation we became no chance
* by the API to setup our default per-server config so we have to
* on-the-fly assume we have the default config. But because the default
* config has a disabled rewriting engine we are lucky because can
* just stop operating now.
*/
return DECLINED;
}
/*
* add the SCRIPT_URL variable to the env. this is a bit complicated
* due to the fact that apache uses subrequests and internal redirects
*/
}
else {
}
}
else {
}
/*
* create the SCRIPT_URI variable for the env
*/
/* add the canonical URI of this URL */
thisserver = ap_get_server_name(r);
port = ap_get_server_port(r);
if (ap_is_default_port(port, r)) {
thisport = "";
}
else {
}
/* set the variable */
/* if filename was not initially set,
* we start with the requested URI
*/
r->filename));
}
else {
}
/*
* now apply the rules ...
*/
}
else {
}
if (rulestatus) {
unsigned skip;
if (ACTION_STATUS == rulestatus) {
int n = r->status;
return n;
}
/* it should be go on as an internal proxy request */
/* check if the proxy module is enabled, so
* we can actually use it!
*/
if (!proxy_available) {
"attempt to make remote request from mod_rewrite "
"without proxy enabled: %s", r->filename);
return HTTP_FORBIDDEN;
}
if (rulestatus == ACTION_NOESCAPE) {
}
/* make sure the QUERY_STRING and
* PATH_INFO parts get incorporated
*/
}
&& ((r->proxyreq == PROXYREQ_PROXY)
|| (rulestatus == ACTION_NOESCAPE))) {
/* see proxy_http:proxy_http_canon() */
}
/* now make sure the request gets handled by the proxy handler */
if (PROXYREQ_NONE == r->proxyreq) {
r->proxyreq = PROXYREQ_REVERSE;
}
r->handler = "proxy-server";
r->filename));
return OK;
}
int n;
/* it was finally rewritten to a remote URL */
if (rulestatus != ACTION_NOESCAPE) {
r->filename));
}
/* append the QUERY_STRING part */
if (r->args) {
? r->args
NULL);
}
/* determine HTTP redirect response code */
if (ap_is_HTTP_REDIRECT(r->status)) {
n = r->status;
}
else {
}
/* now do the redirection */
n));
return n;
}
/*
* Hack because of underpowered API: passing the current
* rewritten filename through to other URL-to-filename handlers
* just as it were the requested URL. This is to enable
* post-processing by mod_alias, etc. which always act on
* r->uri! The difference here is: We do not try to
* add the document root
*/
return DECLINED;
}
else {
/* it was finally rewritten to a local path */
/* expand "/~user" prefix */
#if APR_HAS_USER
#endif
/* the filename must be either an absolute local path or an
* absolute local URL.
*/
if ( *r->filename != '/'
return HTTP_BAD_REQUEST;
}
/* if there is no valid prefix, we call
* the translator from the core and
* prefix the filename with document_root
*
* NOTICE:
* We cannot leave out the prefix_stat because
* - when we always prefix with document_root
* then no absolute path can be created, e.g. via
* emulating a ScriptAlias directive, etc.
* - when we always NOT prefix with document_root
* then the files under document_root have to
* be references directly and document_root
* gets never used and will be a dummy parameter -
* this is also bad
*
* BUT:
* Under real Unix systems this is no problem,
* because we only do stat() on the first directory
* and this gets cached by the kernel for along time!
*/
int res;
res = ap_core_translate(r);
" FAILED", r->filename));
return res;
}
r->filename));
}
return OK;
}
}
else {
return DECLINED;
}
}
/*
* Fixup hook
* [RewriteRules in directory context]
*/
static int hook_fixup(request_rec *r)
{
char *cp;
char *cp2;
const char *ccp;
apr_size_t l;
int rulestatus;
int n;
char *ofilename;
int is_proxyreq;
/* if there is no per-dir config we return immediately */
return DECLINED;
}
/* if there are no real (i.e. no RewriteRule directives!)
per-dir config of us, we return also immediately */
return DECLINED;
}
/*
* Proxy request?
*/
/*
* .htaccess file is called before really entering the directory, i.e.:
* URL: http://localhost/foo and .htaccess is located in foo directory
* Ignore such attempts, since they may lead to undefined behaviour.
*/
if (!is_proxyreq) {
return DECLINED;
}
}
/*
* only do something under runtime if the engine is really enabled,
* for this directory, else return immediately!
*/
return DECLINED;
}
/*
* Do the Options check after engine check, so
* the user is able to explicitely turn RewriteEngine Off.
*/
/* FollowSymLinks is mandatory! */
"Options FollowSymLinks or SymLinksIfOwnerMatch is off "
"which implies that RewriteRule directive is forbidden: "
"%s", r->filename);
return HTTP_FORBIDDEN;
}
/*
* remember the current filename before rewriting for later check
* to prevent deadlooping because of internal redirects
* also, we'll restore original r->filename if we decline this
* request
*/
" requested uri %s", r->filename));
}
/*
* now apply the rules ...
*/
if (rulestatus) {
unsigned skip;
if (ACTION_STATUS == rulestatus) {
int n = r->status;
return n;
}
/* it should go on as an internal proxy request */
/* make sure the QUERY_STRING and
* PATH_INFO parts get incorporated
* (r->path_info was already appended by the
* rewriting engine because of the per-dir context!)
*/
}
/* now make sure the request gets handled by the proxy handler */
if (PROXYREQ_NONE == r->proxyreq) {
r->proxyreq = PROXYREQ_REVERSE;
}
r->handler = "proxy-server";
"%s [OK]", r->filename));
return OK;
}
/* it was finally rewritten to a remote URL */
/* because we are in a per-dir context
* first try to replace the directory with its base-URL
* if there is a base-URL available
*/
/* skip 'scheme://' */
"trying to replace prefix %s with %s",
/* I think, that hack needs an explanation:
* well, here is it:
* mod_rewrite was written for unix systems, were
* absolute file-system paths start with a slash.
* URL-paths _also_ start with slashes, so they
* can be easily compared with system paths.
*
* the following assumes, that the actual url-path
* may be prefixed by the current directory path and
* tries to replace the system path with the RewriteBase
* URL.
* That assumption is true if we use a RewriteRule like
*
* RewriteRule ^foo bar [R]
*
* (see apply_rewrite_rule function)
* However on systems that don't have a / as system
* root this will never match, so we skip the / after the
* hostname and compare/substitute only the stuff after it.
*
* (note that cp was already increased to the right value)
*/
*cp = '\0';
}
}
}
/* now prepare the redirect... */
if (rulestatus != ACTION_NOESCAPE) {
r->filename));
}
/* append the QUERY_STRING part */
if (r->args) {
? r->args
NULL);
}
/* determine HTTP redirect response code */
if (ap_is_HTTP_REDIRECT(r->status)) {
n = r->status;
}
else {
}
/* now do the redirection */
r->filename, n));
return n;
}
else {
/* it was finally rewritten to a local path */
/* if someone used the PASSTHROUGH flag in per-dir
* context we just ignore it. It is only useful
* in per-server context
*/
}
/* the filename must be either an absolute local path or an
* absolute local URL.
*/
if ( *r->filename != '/'
return HTTP_BAD_REQUEST;
}
/* Check for deadlooping:
* At this point we KNOW that at least one rewriting
* rule was applied, but when the resulting URL is
* the same as the initial URL, we are not allowed to
* use the following internal redirection stuff because
* this would lead to a deadloop.
*/
" URL: %s [IGNORING REWRITE]", r->filename));
return OK;
}
/* if there is a valid base-URL then substitute
* the per-dir prefix with this base-URL if the
* current filename still is inside this per-dir
* context. If not then treat the result as a
* plain URL
*/
}
else {
/* if no explicit base-URL exists we assume
* that the directory prefix is also a valid URL
* for this webserver and only try to remove the
* document_root if it is prefix
*/
/* strip trailing slash */
--l;
}
r->filename[l] == '/') {
" prefix: %s -> %s", r->filename,
r->filename+l));
}
}
}
/* now initiate the internal redirect */
"[INTERNAL REDIRECT]", r->filename));
r->handler = "redirect-handler";
return OK;
}
}
else {
return DECLINED;
}
}
/*
* MIME-type hook
* [T=...,H=...] execution
*/
static int hook_mimetype(request_rec *r)
{
const char *t;
/* type */
if (t && *t) {
r->filename, t));
ap_set_content_type(r, t);
}
/* handler */
if (t && *t) {
"Content-handler '%s'", r->filename, t));
r->handler = t;
}
return OK;
}
/*
* "content" handler for internal redirects
*/
static int handler_redirect(request_rec *r)
{
return DECLINED;
}
/* just make sure that we are really meant! */
return DECLINED;
}
/* now do the internal redirect */
/* and return gracefully */
return OK;
}
/*
* +-------------------------------------------------------+
* | |
* | Module paraphernalia
* | |
* +-------------------------------------------------------+
*/
#ifdef REWRITELOG_DISABLED
{
return "RewriteLog and RewriteLogLevel are not supported by this build "
"of mod_rewrite because it was compiled using the "
"-DREWRITELOG_DISABLED compiler option. You have to recompile "
"mod_rewrite WITHOUT this option in order to use the rewrite log.";
}
#endif
static const command_rec command_table[] = {
"On or Off to enable or disable (default) the whole "
"rewriting engine"),
"List of option strings to set"),
"the base URL of the per-directory context"),
"an input string and a to be applied regexp-pattern"),
"an URL-applied regexp-pattern and a substitution URL"),
"a mapname and a filename"),
#ifndef REWRITELOG_DISABLED
"the filename of the rewriting logfile"),
"the level of the rewriting logfile verbosity "
"(0=none, 1=std, .., 9=max)"),
#else
"[DISABLED] the filename of the rewriting logfile"),
"[DISABLED] the level of the rewriting logfile verbosity"),
#endif
{ NULL }
};
{
}
static void register_hooks(apr_pool_t *p)
{
/* fixup after mod_proxy, so that the proxied url will not
* escaped accidentally by mod_proxy's fixup.
*/
/* make the hashtable before registering the function, so that
* other modules are prevented from accessing uninitialized memory.
*/
mapfunc_hash = apr_hash_make(p);
}
/* the main config structure */
config_perdir_create, /* create per-dir config structures */
config_perdir_merge, /* merge per-dir config structures */
config_server_create, /* create per-server config structures */
config_server_merge, /* merge per-server config structures */
command_table, /* table of config file commands */
register_hooks /* register hooks */
};
/*EOF*/