mod_rewrite.h revision 7bfb60cc1a60c889c6a836d62348be42fc5d547e
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
#ifndef MOD_REWRITE_H
#define MOD_REWRITE_H 1
/*
** _ _ _
** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
** | | | | | | (_) | (_| | | | | __/\ 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
** www.engelschall.com
*/
#include "apr.h"
#define APR_WANT_STRFUNC
#define APR_WANT_MEMFUNC
#include "apr_want.h"
/* Include from the underlaying Unix system ... */
#include <stdarg.h>
#endif
#include <stdlib.h>
#endif
#if APR_HAVE_CTYPE_H
#include <ctype.h>
#endif
#endif
#if APR_HAS_THREADS
#include "apr_thread_mutex.h"
#endif
#include "apr_optional.h"
#include "apr_dbm.h"
#include "ap_config.h"
/* Include from the Apache server ... */
#define CORE_PRIVATE
#include "httpd.h"
#include "http_config.h"
#include "http_request.h"
#include "http_core.h"
#include "http_log.h"
#include "http_vhost.h"
/*
* The key in the r->notes apr_table_t wherein we store our accumulated
* Vary values, and the one used for per-condition checks in a chain.
*/
#define VARY_KEY "rewrite-Vary"
#define VARY_KEY_THIS "rewrite-Vary-this"
/*
**
** Some defines
**
*/
#define ENVVAR_SCRIPT_URL "SCRIPT_URL"
#define REDIRECT_ENVVAR_SCRIPT_URL "REDIRECT_SCRIPT_URL"
#define ENVVAR_SCRIPT_URI "SCRIPT_URI"
#define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
#define CONDFLAG_NONE 1<<0
#define RULEFLAG_NONE 1<<0
#define ACTION_NORMAL 1<<0
#define MAPTYPE_TXT 1<<0
#define ENGINE_DISABLED 1<<0
#define OPTION_NONE 1<<0
#define CACHEMODE_TS 1<<0
#define CACHE_TLB_ROWS 1024
#define CACHE_TLB_COLS 4
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NO
#endif
#ifndef RAND_MAX
#define RAND_MAX 32767
#endif
#ifndef LONG_STRING_LEN
#define LONG_STRING_LEN 2048
#endif
#define MAX_ENV_FLAGS 15
#define MAX_COOKIE_FLAGS 15
/*** max cookie size in rfc 2109 ***/
#define MAX_COOKIE_LEN 4096
#define MAX_NMATCH 10
/* default maximum number of internal redirects */
#define REWRITE_REDIRECT_LIMIT 10
/*
**
** our private data structures we handle with
**
*/
/* the list structures for holding the mapfile information
* and the rewrite rules
*/
typedef struct {
const char *name; /* the name of the map */
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;
typedef struct {
char *input; /* Input string of RewriteCond */
char *pattern; /* the RegExp pattern string */
int flags; /* Flags which control the match */
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 */
int forced_responsecode; /* forced HTTP redirect response status */
int skip; /* number of next rules to skip */
/* the per-server or per-virtual-server configuration
* statically generated once on startup for every server
*/
typedef struct {
int state; /* the RewriteEngine state */
int options; /* the RewriteOption state */
const char *rewritelogfile; /* the RewriteLog filename */
int rewriteloglevel; /* the RewriteLog level of verbosity */
int redirect_limit; /* maximum number of internal redirects */
/* the per-directory configuration
* generated on-the-fly by Apache server for current request
*/
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 */
int redirect_limit; /* maximum number of internal redirects */
/* the per-request configuration
*/
typedef struct {
int redirects; /* current number of redirects */
int redirect_limit; /* maximum number of redirects */
/* the cache structures,
* a 4-way hash apr_table_t with LRU functionality
*/
typedef struct cacheentry {
char *key;
char *value;
} cacheentry;
typedef struct tlbentry {
int t[CACHE_TLB_COLS];
typedef struct cachelist {
char *resource;
} cachelist;
typedef struct cache {
#if APR_HAS_THREADS
#endif
} cache;
/* the regex structure for the
* substitution of backreferences
*/
typedef struct backrefinfo {
char *source;
int nsub;
} backrefinfo;
/*
**
** forward declarations
**
*/
/* config structure handling */
/* config directive handling */
void *dconf,
const char *option);
const char *a1);
const char *str);
static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p,
char *str);
const char *str);
static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p,
char *str);
/* initialisation */
apr_pool_t *ptemp);
server_rec *s);
/* runtime hooks */
static int hook_uri2file (request_rec *r);
static int hook_mimetype (request_rec *r);
static int hook_fixup (request_rec *r);
static int handler_redirect(request_rec *r);
/* rewriting engine */
char *perdir);
char *perdir);
backrefinfo *briRC);
/* URI transformation function */
static void fully_qualify_uri(request_rec *r);
static void reduce_uri(request_rec *r);
static unsigned is_absolute_uri(char *uri);
/* rewrite map support functions */
static void rewrite_rand_init(void);
static int rewrite_rand(int l, int h);
/* rewriting logfile support */
static char *current_logtime(request_rec *r);
/* rewriting lockfile support */
/* program map support */
apr_file_t **fpout,
apr_file_t **fpin);
/* env variable support */
/* caching functions */
char *key);
/* misc functions */
const char *subst);
static void add_env_variable(request_rec *r, char *s);
static void add_cookie(request_rec *r, char *s);
static int subreq_ok(request_rec *r);
static int is_redirect_limit_exceeded(request_rec *r);
/* Lexicographic Comparison */
/* Bracketed expression handling */
#endif /* MOD_REWRITE_H */