mod_rewrite.c revision 7cb3805798a456139cb4e5ab77ea707deedc7d23
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek/* ====================================================================
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * The Apache Software License, Version 1.1
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * Redistribution and use in source and binary forms, with or without
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * modification, are permitted provided that the following conditions
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * 1. Redistributions of source code must retain the above copyright
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * notice, this list of conditions and the following disclaimer.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * 2. Redistributions in binary form must reproduce the above copyright
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * notice, this list of conditions and the following disclaimer in
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * the documentation and/or other materials provided with the
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * distribution.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * 3. The end-user documentation included with the redistribution,
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * if any, must include the following acknowledgment:
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * "This product includes software developed by the
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * Apache Software Foundation (http://www.apache.org/)."
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * Alternately, this acknowledgment may appear in the software itself,
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * if and wherever such third-party acknowledgments normally appear.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * 4. The names "Apache" and "Apache Software Foundation" must
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * not be used to endorse or promote products derived from this
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * software without prior written permission. For written
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * permission, please contact apache@apache.org.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * 5. Products derived from this software may not be called "Apache",
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * nor may "Apache" appear in their name, without prior written
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * permission of the Apache Software Foundation.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * SUCH DAMAGE.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * ====================================================================
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * This software consists of voluntary contributions made by many
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * individuals on behalf of the Apache Software Foundation. For more
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * information on the Apache Software Foundation, please see
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * Portions of this software are based upon public domain software
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * originally written at the National Center for Supercomputing Applications,
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek * University of Illinois, Urbana-Champaign.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** URL Rewriting Module
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** This module uses a rule-based rewriting engine (based on a
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** regular-expression parser) to rewrite requested URLs on the fly.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** It supports an unlimited number of additional rule conditions (which can
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** operate on a lot of variables, even on HTTP headers) for granular
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** matching and even external database lookups (either via plain text
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** tables, DBM hash files or even external processes) for advanced URL
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** substitution.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** It operates on the full URLs (including the PATH_INFO part) both in
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** per-server context (httpd.conf) and per-dir context (.htaccess) and even
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** can generate QUERY_STRING parts on result. The rewriting result finally
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** can lead to internal subprocessing, external request redirection or even
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** to internal proxy throughput.
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** This module was originally written in April 1996 and
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** gifted exclusively to the The Apache Software Foundation in July 1997 by
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** Ralf S. Engelschall
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** rse@engelschall.com
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** +-------------------------------------------------------+
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** | static module configuration
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** +-------------------------------------------------------+
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** Our interface to the Apache server kernel:
e088912418fd4db750f2097dfde8ef9b77303f05Michal Židek** o Runtime logic of a request is as following:
static int proxy_available;
static const char *lockname;
a->rewriteloglevel = 0;
a->server = s;
sizeof(rewrite_server_conf));
sizeof(rewrite_perdir_conf));
return NULL;
while (*option) {
if (limit <= 0) {
return NULL;
return NULL;
const char *a1)
return NULL;
const char *a2)
const char *ignored_fname;
int bad = 0;
if (colon) {
++bad;
++bad;
if (bad) {
return NULL;
const char *error;
return error;
if (!lockname) {
return NULL;
const char *a1)
return NULL;
const char *in_str)
char *a1;
char *a2;
char *a3;
const char *err;
return err;
++a2;
? REG_ICASE : 0));
if (!regexp) {
return NULL;
return NULL;
const char *in_str)
char *a1;
char *a2;
char *a3;
const char *err;
return err;
++a1;
? REG_ICASE : 0));
if (!regexp) {
sizeof(rewritecond_entry));
sizeof(rewritecond_entry));
return NULL;
int status = 0;
switch (*key++) {
if (i < MAX_COOKIE_FLAGS) {
if (i < MAX_ENV_FLAGS) {
return NULL;
const char *err;
++key;
while (*key) {
++key;
--endp;
if (val) {
if (err) {
return err;
return NULL;
if (map_pfn_register) {
return OK;
server_rec *s)
void *data;
int first_time = 0;
if (!data) {
return HTTP_INTERNAL_SERVER_ERROR;
#ifdef MOD_REWRITE_SET_MUTEX_PERMS
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
for (; s; s = s->next) {
open_rewritelog(s, p);
if (!first_time) {
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
lockname, p);
const char *saved_rulestatus;
const char *var;
const char *thisserver;
char *thisport;
const char *thisurl;
unsigned int port;
int rulestatus;
return DECLINED;
return DECLINED;
r->filename);
if (rulestatus) {
unsigned skip;
if (!proxy_available) {
return HTTP_FORBIDDEN;
r->filename);
return OK;
if (r->args) {
? r->args
NULL);
n = r->status;
return HTTP_FORBIDDEN;
return HTTP_GONE;
return DECLINED;
#if APR_HAS_USER
return HTTP_BAD_REQUEST;
int res;
return res;
r->filename);
return OK;
return DECLINED;
if (t == NULL) {
return DECLINED;
r->filename, t);
ap_set_content_type(r, t);
return OK;
char *cp;
char *cp2;
const char *ccp;
apr_size_t l;
int rulestatus;
char *ofilename;
return DECLINED;
return DECLINED;
return DECLINED;
* URL: http://localhost/foo and .htaccess is located in foo directory
return DECLINED;
return DECLINED;
return HTTP_FORBIDDEN;
if (rulestatus) {
unsigned skip;
return OK;
* hostname and compare/substitute only the stuff after it.
if (r->args) {
? r->args
NULL);
n = r->status;
return HTTP_FORBIDDEN;
return HTTP_GONE;
return HTTP_BAD_REQUEST;
return OK;
r->filename+l);
return OK;
return DECLINED;
return DECLINED;
return DECLINED;
if (is_redirect_limit_exceeded(r)) {
return HTTP_INTERNAL_SERVER_ERROR;
return OK;
if (!reqc) {
char *perdir)
int changed;
int rc;
changed = 0;
loop:
p = &entries[i];
if (rc) {
goto loop;
if (p->skip > 0) {
s = p->skip;
p = &entries[i];
p = &entries[i];
return changed;
char *perdir)
char *uri;
char *output;
const char *vary;
char *newuri;
int prefixstrip;
int failed;
int rc;
prefixstrip = 0;
sizeof(regmatch));
failed = 0;
c = &conds[i];
if (rc == 0) {
c = &conds[i];
if (rc == 0) {
if (failed) {
p->forced_mimetype);
reduce_uri(r);
r->filename);
p->forced_mimetype);
char *input;
int rc;
rc = 0;
#if !defined(OS2)
sizeof(regmatch));
return rc;
++outlen;
char *endp;
if (!endp) {
char *key;
if (!key) {
if (dflt) {
if (key) {
++outlen;
p += span;
} while (result);
char *olduri;
if (q != NULL) {
if (qsappend) {
r->filename);
char *cp;
apr_size_t l;
unsigned short port;
++cp;
++cp;
if (!*url) {
const char *thisserver;
char *thisport;
int port;
switch (*uri++) {
char *cp;
return NULL;
++cp;
++cp;
NULL);
#if APR_HAS_USER
char *newuri;
char *homedir;
return newuri;
rewritemap_entry *s;
char *value;
s = &entries[i];
return NULL;
if ((value =
return value;
return NULL;
return NULL;
if ((value =
return value;
return NULL;
if ((value =
return value;
return value;
return NULL;
if ((value =
return NULL;
value);
return value;
return NULL;
char *cpT;
char *curkey;
char *curval;
return NULL;
if (skip == 0) {
++cpT;
if (skip == 0) {
return value;
return value;
#ifndef NO_WRITEV
return NULL;
if (rewrite_mapr_lock_acquire) {
#ifdef NO_WRITEV
buf[i++] = c;
if (rewrite_mapr_lock_acquire) {
return NULL;
cp++) {
return value;
cp++) {
return value;
char *value;
return value;
char *value;
return value;
static int rewrite_rand_init_done = 0;
static void rewrite_rand_init(void)
if (!rewrite_rand_init_done) {
static int rewrite_rand(int l, int h)
char *buf;
return value;
return buf;
const char *fname;
if (!fname) {
!= APR_SUCCESS) {
char *str1;
const char *type;
char *ruser;
const char *rhost;
(unsigned long)(r->server), (unsigned long)r,
return APR_SUCCESS;
APR_LOCK_DEFAULT, p);
return rc;
#ifdef MOD_REWRITE_SET_MUTEX_PERMS
return rc;
return APR_SUCCESS;
return APR_SUCCESS;
return APR_SUCCESS;
return rc;
return APR_SUCCESS;
const char *desc)
!= APR_SUCCESS) ||
procattr, p);
if (fpin) {
if (fpout) {
return (rc);
const char *result;
/* XXX: wow this has gotta be slow if you actually use it for a lot, recalculates exploded time for each variable */
return (char *)result; \
if (val) {
return val;
cache *c;
return NULL;
#if APR_HAS_THREADS
return NULL;
return NULL;
return NULL;
return n % CACHE_TLB_ROWS;
char *key)
for (i=0; i < CACHE_TLB_COLS; ++i) {
return NULL;
return &elt[j];
return NULL;
cacheentry *e)
cachelist *l;
cacheentry *e;
cachetlbentry *t;
int found_list;
#if APR_HAS_THREADS
found_list = 0;
if (e != NULL) {
#if APR_HAS_THREADS
#if APR_HAS_THREADS
if (!found_list) {
sizeof(cachetlbentry));
for (i=0; i<CACHE_TLB_ROWS; ++i) {
for (j=0; j<CACHE_TLB_COLS; ++j)
#if APR_HAS_THREADS
#if APR_HAS_THREADS
cachelist *l;
cacheentry *e;
#if APR_HAS_THREADS
if (e != NULL) {
#if APR_HAS_THREADS
#if APR_HAS_THREADS
#if APR_HAS_THREADS
return NULL;
const char *subst)
--len;
char *output;
++slen;
return output;
return input;
char *cp;
int isquoted;
cp++; \
isquoted = 0; \
cp++; \
cp++; \
char *val;
char *var;
char *val;
char *domain;
char *expires;
char *path;
char *tok_cntx;
char *cookie;
if (expires) {
char *notename;
void *data;
(expires)?
r->request_time +
: NULL,
NULL);
const char *root;
const char *slash;
char *statpath;
for (i = 0; i < n1; i++) {
int depth;
else if (*s == left) {
++depth;
return NULL;
int depth;
return NULL;
else if (*s == left) {
++depth;
return NULL;
{ NULL }