mod_rewrite.h revision 895b674fa4e5a4c8fcbcb4e7b30630857653c149
2N/A/* ====================================================================
2N/A * Copyright (c) 1996-1999 The Apache Group. All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted provided that the following conditions
2N/A * are met:
2N/A *
2N/A * 1. Redistributions of source code must retain the above copyright
2N/A * notice, this list of conditions and the following disclaimer.
2N/A *
2N/A * 2. Redistributions in binary form must reproduce the above copyright
2N/A * notice, this list of conditions and the following disclaimer in
2N/A * the documentation and/or other materials provided with the
2N/A * distribution.
2N/A *
2N/A * 3. All advertising materials mentioning features or use of this
2N/A * software must display the following acknowledgment:
2N/A * "This product includes software developed by the Apache Group
2N/A * for use in the Apache HTTP server project (http://www.apache.org/)."
2N/A *
58N/A * 4. The names "Apache Server" and "Apache Group" must not be used to
2N/A * endorse or promote products derived from this software without
2N/A * prior written permission. For written permission, please contact
32N/A * apache@apache.org.
32N/A *
2N/A * 5. Products derived from this software may not be called "Apache"
2N/A * nor may "Apache" appear in their names without prior written
26N/A * permission of the Apache Group.
38N/A *
38N/A * 6. Redistributions of any form whatsoever must retain the following
38N/A * acknowledgment:
29N/A * "This product includes software developed by the Apache Group
93N/A * for use in the Apache HTTP server project (http://www.apache.org/)."
29N/A *
29N/A * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
26N/A * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26N/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
26N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38N/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2N/A * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6N/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2N/A * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58N/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
2N/A * OF THE POSSIBILITY OF SUCH DAMAGE.
2N/A * ====================================================================
34N/A *
34N/A * This software consists of voluntary contributions made by many
34N/A * individuals on behalf of the Apache Group and was originally based
34N/A * on public domain software written at the National Center for
49N/A * Supercomputing Applications, University of Illinois, Urbana-Champaign.
70N/A * For more information on the Apache Group and the Apache HTTP server
2N/A * project, please see <http://www.apache.org/>.
34N/A *
34N/A */
34N/A
59N/A
72N/A#ifndef _MOD_REWRITE_H
72N/A#define _MOD_REWRITE_H 1
72N/A
59N/A/*
2N/A** _ _ _
85N/A** _ __ ___ ___ __| | _ __ _____ ___ __(_) |_ ___
61N/A** | '_ ` _ \ / _ \ / _` | | '__/ _ \ \ /\ / / '__| | __/ _ \
61N/A** | | | | | | (_) | (_| | | | | __/\ V V /| | | | || __/
61N/A** |_| |_| |_|\___/ \__,_|___|_| \___| \_/\_/ |_| |_|\__\___|
61N/A** |_____|
61N/A**
61N/A** URL Rewriting Module
61N/A**
61N/A** This module uses a rule-based rewriting engine (based on a
61N/A** regular-expression parser) to rewrite requested URLs on the fly.
74N/A**
61N/A** It supports an unlimited number of additional rule conditions (which can
85N/A** operate on a lot of variables, even on HTTP headers) for granular
61N/A** matching and even external database lookups (either via plain text
61N/A** tables, DBM hash files or even external processes) for advanced URL
61N/A** substitution.
61N/A**
61N/A** It operates on the full URLs (including the PATH_INFO part) both in
61N/A** per-server context (httpd.conf) and per-dir context (.htaccess) and even
61N/A** can generate QUERY_STRING parts on result. The rewriting result finally
61N/A** can lead to internal subprocessing, external request redirection or even
61N/A** to internal proxy throughput.
61N/A**
43N/A** This module was originally written in April 1996 and
43N/A** gifted exclusively to the The Apache Group in July 1997 by
64N/A**
64N/A** Ralf S. Engelschall
64N/A** rse@engelschall.com
43N/A** www.engelschall.com
43N/A*/
64N/A
64N/A
64N/A /* Include from the underlaying Unix system ... */
64N/A#include <string.h>
64N/A#include <stdarg.h>
64N/A#include <stdlib.h>
64N/A#include <time.h>
64N/A#include <signal.h>
64N/A#include <errno.h>
64N/A#include <ctype.h>
64N/A#include <sys/types.h>
64N/A#include <sys/stat.h>
43N/A
16N/A /* Include from the Apache server ... */
16N/A#define CORE_PRIVATE
16N/A#include "httpd.h"
34N/A#include "http_config.h"
34N/A#include "http_conf_globals.h"
16N/A#include "http_request.h"
6N/A#include "http_core.h"
12N/A#include "http_log.h"
12N/A#include "http_vhost.h"
12N/A
12N/A /*
12N/A * The key in the r->notes ap_table_t wherein we store our accumulated
12N/A * Vary values, and the one used for per-condition checks in a chain.
12N/A */
12N/A#define VARY_KEY "rewrite-Vary"
12N/A#define VARY_KEY_THIS "rewrite-Vary-this"
12N/A
12N/A /* The NDBM support:
61N/A * We support only NDBM files.
61N/A * But we have to stat the file for the mtime,
61N/A * so we also need to know the file extension
61N/A */
61N/A#ifndef NO_DBM_REWRITEMAP
12N/A#if defined(__GLIBC__) && defined(__GLIBC_MINOR__) \
12N/A && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
61N/A#include <db1/ndbm.h>
61N/A#else
12N/A#include <ndbm.h>
2N/A#endif
40N/A#if defined(DBM_SUFFIX)
40N/A#define NDBM_FILE_SUFFIX DBM_SUFFIX
40N/A#elif defined(__FreeBSD__) || (defined(DB_LOCK) && defined(DB_SHMEM))
40N/A#define NDBM_FILE_SUFFIX ".db"
43N/A#else
40N/A#define NDBM_FILE_SUFFIX ".pag"
2N/A#endif
2N/A#endif
40N/A
40N/A
2N/A /* The locking support:
40N/A * Try to determine whether we should use fcntl() or flock().
40N/A * Would be better ap_config.h could provide this... :-(
2N/A */
2N/A#if defined(USE_FCNTL_SERIALIZED_ACCEPT)
2N/A#define USE_FCNTL 1
2N/A#include <fcntl.h>
2N/A#endif
2N/A#if defined(USE_FLOCK_SERIALIZED_ACCEPT)
2N/A#define USE_FLOCK 1
2N/A#include <sys/file.h>
2N/A#endif
2N/A#if !defined(USE_FCNTL) && !defined(USE_FLOCK)
61N/A#define USE_FLOCK 1
61N/A#if !defined(MPE) && !defined(WIN32) && !defined(__TANDEM)
61N/A#include <sys/file.h>
61N/A#endif
61N/A#ifndef LOCK_UN
16N/A#undef USE_FLOCK
16N/A#define USE_FCNTL 1
2N/A#include <fcntl.h>
47N/A#endif
2N/A#endif
2N/A#ifdef AIX
2N/A#undef USE_FLOCK
2N/A#define USE_FCNTL 1
2N/A#include <fcntl.h>
2N/A#endif
82N/A#ifdef WIN32
82N/A#undef USE_FCNTL
2N/A#define USE_LOCKING
2N/A#include <sys/locking.h>
70N/A#endif
2N/A
2N/A
2N/A/*
2N/A**
7N/A** Some defines
43N/A**
43N/A*/
16N/A
43N/A#define ENVVAR_SCRIPT_URL "SCRIPT_URL"
61N/A#define ENVVAR_SCRIPT_URI "SCRIPT_URI"
61N/A
61N/A#ifndef SUPPORT_DBM_REWRITEMAP
61N/A#define SUPPORT_DBM_REWRITEMAP 0
61N/A#endif
61N/A
61N/A#define REWRITE_FORCED_MIMETYPE_NOTEVAR "rewrite-forced-mimetype"
61N/A
61N/A#define CONDFLAG_NONE 1<<0
61N/A#define CONDFLAG_NOCASE 1<<1
61N/A#define CONDFLAG_NOTMATCH 1<<2
61N/A#define CONDFLAG_ORNEXT 1<<3
61N/A
61N/A#define RULEFLAG_NONE 1<<0
61N/A#define RULEFLAG_FORCEREDIRECT 1<<1
61N/A#define RULEFLAG_LASTRULE 1<<2
61N/A#define RULEFLAG_NEWROUND 1<<3
61N/A#define RULEFLAG_CHAIN 1<<4
80N/A#define RULEFLAG_IGNOREONSUBREQ 1<<5
80N/A#define RULEFLAG_NOTMATCH 1<<6
80N/A#define RULEFLAG_PROXY 1<<7
80N/A#define RULEFLAG_PASSTHROUGH 1<<8
61N/A#define RULEFLAG_FORBIDDEN 1<<9
61N/A#define RULEFLAG_GONE 1<<10
61N/A#define RULEFLAG_QSAPPEND 1<<11
61N/A#define RULEFLAG_NOCASE 1<<12
61N/A
61N/A#define MAPTYPE_TXT 1<<0
61N/A#define MAPTYPE_DBM 1<<1
61N/A#define MAPTYPE_PRG 1<<2
61N/A#define MAPTYPE_INT 1<<3
61N/A#define MAPTYPE_RND 1<<4
61N/A
61N/A#define ENGINE_DISABLED 1<<0
61N/A#define ENGINE_ENABLED 1<<1
61N/A
61N/A#define OPTION_NONE 1<<0
61N/A#define OPTION_INHERIT 1<<1
61N/A
61N/A#define CACHEMODE_TS 1<<0
61N/A#define CACHEMODE_TTL 1<<1
61N/A
61N/A#define CACHE_TLB_ROWS 1024
61N/A#define CACHE_TLB_COLS 4
61N/A
61N/A#ifndef FALSE
61N/A#define FALSE 0
61N/A#define TRUE !FALSE
61N/A#endif
61N/A
61N/A#ifndef NO
61N/A#define NO FALSE
61N/A#define YES TRUE
61N/A#endif
61N/A
61N/A#ifndef RAND_MAX
61N/A#define RAND_MAX 32767
61N/A#endif
61N/A
61N/A#ifndef LONG_STRING_LEN
61N/A#define LONG_STRING_LEN 2048
61N/A#endif
61N/A
61N/A#define MAX_ENV_FLAGS 15
61N/A
61N/A#define MAX_NMATCH 10
61N/A
61N/A/*
61N/A**
61N/A** our private data structures we handle with
61N/A**
61N/A*/
61N/A
61N/A /* the list structures for holding the mapfile information
61N/A * and the rewrite rules
61N/A */
64N/Atypedef struct {
64N/A char *name; /* the name of the map */
82N/A char *datafile; /* filename for map data files */
64N/A char *checkfile; /* filename to check for map existence */
64N/A int type; /* the type of the map */
62N/A ap_file_t *fpin; /* in file pointer for program maps */
64N/A ap_file_t *fpout; /* out file pointer for program maps */
64N/A ap_file_t *fperr; /* err file pointer for program maps */
82N/A char *(*func)(request_rec *, /* function pointer for internal maps */
64N/A char *);
82N/A} rewritemap_entry;
64N/A
64N/Atypedef struct {
62N/A char *input; /* Input string of RewriteCond */
62N/A char *pattern; /* the RegExp pattern string */
62N/A regex_t *regexp;
62N/A int flags; /* Flags which control the match */
} rewritecond_entry;
typedef struct {
ap_array_header_t *rewriteconds; /* the corresponding RewriteCond entries */
char *pattern; /* the RegExp pattern string */
regex_t *regexp; /* the RegExp pattern compilation */
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 */
char *env[MAX_ENV_FLAGS+1]; /* added environment variables */
int skip; /* number of next rules to skip */
} rewriterule_entry;
/* 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 */
char *rewritelogfile; /* the RewriteLog filename */
ap_file_t *rewritelogfp; /* the RewriteLog open filepointer */
int rewriteloglevel; /* the RewriteLog level of verbosity */
ap_array_header_t *rewritemaps; /* the RewriteMap entries */
ap_array_header_t *rewriteconds; /* the RewriteCond entries (temporary) */
ap_array_header_t *rewriterules; /* the RewriteRule entries */
server_rec *server; /* the corresponding server indicator */
} rewrite_server_conf;
/* 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 */
ap_array_header_t *rewriteconds; /* the RewriteCond entries (temporary) */
ap_array_header_t *rewriterules; /* the RewriteRule entries */
char *directory; /* the directory where it applies */
char *baseurl; /* the base-URL where it applies */
} rewrite_perdir_conf;
/* the cache structures,
* a 4-way hash ap_table_t with LRU functionality
*/
typedef struct cacheentry {
time_t time;
char *key;
char *value;
} cacheentry;
typedef struct tlbentry {
int t[CACHE_TLB_COLS];
} cachetlbentry;
typedef struct cachelist {
char *resource;
ap_array_header_t *entries;
ap_array_header_t *tlb;
} cachelist;
typedef struct cache {
ap_context_t *pool;
ap_array_header_t *lists;
} cache;
/* the regex structure for the
* substitution of backreferences
*/
typedef struct backrefinfo {
char *source;
int nsub;
regmatch_t regmatch[10];
} backrefinfo;
/*
**
** forward declarations
**
*/
/* config structure handling */
static void *config_server_create(ap_context_t *p, server_rec *s);
static void *config_server_merge (ap_context_t *p, void *basev, void *overridesv);
static void *config_perdir_create(ap_context_t *p, char *path);
static void *config_perdir_merge (ap_context_t *p, void *basev, void *overridesv);
/* config directive handling */
static const char *cmd_rewriteengine(cmd_parms *cmd,
rewrite_perdir_conf *dconf, int flag);
static const char *cmd_rewriteoptions(cmd_parms *cmd,
rewrite_perdir_conf *dconf,
char *option);
static const char *cmd_rewriteoptions_setoption(ap_context_t *p, int *options,
char *name);
static const char *cmd_rewritelog (cmd_parms *cmd, void *dconf, char *a1);
static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, char *a1);
static const char *cmd_rewritemap (cmd_parms *cmd, void *dconf, char *a1,
char *a2);
static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, char *a1);
static const char *cmd_rewritebase(cmd_parms *cmd, rewrite_perdir_conf *dconf,
char *a1);
static const char *cmd_rewritecond(cmd_parms *cmd, rewrite_perdir_conf *dconf,
char *str);
static const char *cmd_rewritecond_parseflagfield(ap_context_t *p,
rewritecond_entry *new,
char *str);
static const char *cmd_rewritecond_setflag(ap_context_t *p, rewritecond_entry *cfg,
char *key, char *val);
static const char *cmd_rewriterule(cmd_parms *cmd, rewrite_perdir_conf *dconf,
char *str);
static const char *cmd_rewriterule_parseflagfield(ap_context_t *p,
rewriterule_entry *new,
char *str);
static const char *cmd_rewriterule_setflag(ap_context_t *p, rewriterule_entry *cfg,
char *key, char *val);
/* initialisation */
static void init_module(ap_context_t *p,
ap_context_t *plog,
ap_context_t *ptemp,
server_rec *s);
static void init_child(ap_context_t *p, 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 */
static int apply_rewrite_list(request_rec *r, ap_array_header_t *rewriterules,
char *perdir);
static int apply_rewrite_rule(request_rec *r, rewriterule_entry *p,
char *perdir);
static int apply_rewrite_cond(request_rec *r, rewritecond_entry *p,
char *perdir, backrefinfo *briRR,
backrefinfo *briRC);
/* URI transformation function */
static void splitout_queryargs(request_rec *r, int qsappend);
static void fully_qualify_uri(request_rec *r);
static void reduce_uri(request_rec *r);
static void expand_backref_inbuffer(ap_context_t *p, char *buf, int nbuf,
backrefinfo *bri, char c);
static char *expand_tildepaths(request_rec *r, char *uri);
static void expand_map_lookups(request_rec *r, char *uri, int uri_len);
/* rewrite map support functions */
static char *lookup_map(request_rec *r, char *name, char *key);
static char *lookup_map_txtfile(request_rec *r, char *file, char *key);
#ifndef NO_DBM_REWRITEMAP
static char *lookup_map_dbmfile(request_rec *r, char *file, char *key);
#endif
static char *lookup_map_program(request_rec *r, ap_file_t *fpin,
ap_file_t *fpout, char *key);
static char *lookup_map_internal(request_rec *r,
char *(*func)(request_rec *r, char *key),
char *key);
static char *rewrite_mapfunc_toupper(request_rec *r, char *key);
static char *rewrite_mapfunc_tolower(request_rec *r, char *key);
static char *rewrite_mapfunc_escape(request_rec *r, char *key);
static char *rewrite_mapfunc_unescape(request_rec *r, char *key);
static char *select_random_value_part(request_rec *r, char *value);
static void rewrite_rand_init(void);
static int rewrite_rand(int l, int h);
/* rewriting logfile support */
static void open_rewritelog(server_rec *s, ap_context_t *p);
static void rewritelog(request_rec *r, int level, const char *text, ...)
__attribute__((format(printf,3,4)));
static char *current_logtime(request_rec *r);
/* rewriting lockfile support */
static void rewritelock_create(server_rec *s, ap_context_t *p);
static void rewritelock_open(server_rec *s, ap_context_t *p);
static ap_status_t rewritelock_remove(void *data);
static void rewritelock_alloc(request_rec *r);
static void rewritelock_free(request_rec *r);
/* program map support */
static void run_rewritemap_programs(server_rec *s, ap_context_t *p);
static int rewritemap_program_child(ap_context_t *p, char *progname,
ap_file_t **fpout, ap_file_t **fpin,
ap_file_t **fperr);
/* env variable support */
static void expand_variables_inbuffer(request_rec *r, char *buf, int buf_len);
static char *expand_variables(request_rec *r, char *str);
static char *lookup_variable(request_rec *r, char *var);
static char *lookup_header(request_rec *r, const char *name);
/* caching functions */
static cache *init_cache(ap_context_t *p);
static char *get_cache_string(cache *c, char *res, int mode, time_t mtime,
char *key);
static void set_cache_string(cache *c, char *res, int mode, time_t mtime,
char *key, char *value);
static cacheentry *retrieve_cache_string(cache *c, char *res, char *key);
static void store_cache_string(cache *c, char *res, cacheentry *ce);
/* misc functions */
static char *subst_prefix_path(request_rec *r, char *input, char *match,
char *subst);
static int parseargline(char *str, char **a1, char **a2, char **a3);
static int prefix_stat(const char *path, struct stat *sb);
static void add_env_variable(request_rec *r, char *s);
/* File locking */
static void fd_lock(request_rec *r, ap_file_t *fd);
static void fd_unlock(request_rec *r, ap_file_t *fd);
/* Lexicographic Comparison */
static int compare_lexicography(char *cpNum1, char *cpNum2);
#endif /* _MOD_REWRITE_H */
/*EOF*/