📄 mod_rewrite.c
字号:
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as * applicable. * * Licensed 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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** www.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"#define APR_WANT_STRFUNC#define APR_WANT_IOVEC#include "apr_want.h"#if APR_HAVE_UNISTD_H#include <unistd.h>#endif#if APR_HAVE_SYS_TYPES_H#include <sys/types.h>#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 "mod_rewrite.h"/* mod_ssl.h is not safe for inclusion in 2.0, so duplicate the * optional function declarations. */APR_DECLARE_OPTIONAL_FN(char *, ssl_var_lookup, (apr_pool_t *, server_rec *, conn_rec *, request_rec *, char *));APR_DECLARE_OPTIONAL_FN(int, ssl_is_https, (conn_rec *));#if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)#include "unixd.h"#define MOD_REWRITE_SET_MUTEX_PERMS /* XXX Apache should define something */#endif/*** +-------------------------------------------------------+** | |** | static module configuration** | |** +-------------------------------------------------------+*//*** Our interface to the Apache server kernel:**** o Runtime logic of a request is as following:** while(request or subrequest)** foreach(stage #0...#9)** foreach(module) (**)** try to run hook**** o the order of modules at (**) is the inverted order as** given in the "Configuration" file, i.e. the last module** specified is the first one called for each hook!** The core module is always the last!**** o there are two different types of result checking and** continue processing:** for hook #0,#1,#4,#5,#6,#8:** hook run loop stops on first modules which gives** back a result != DECLINED, i.e. it usually returns OK** which says "OK, module has handled this _stage_" and for #1** this have not to mean "Ok, the filename is now valid".** for hook #2,#3,#7,#9:** all hooks are run, independend of result**** o at the last stage, the core module always** - says "HTTP_BAD_REQUEST" if r->filename does not begin with "/"** - prefix URL with document_root or replaced server_root** with document_root and sets r->filename** - always return a "OK" independed if the file really exists** or not!*/ /* the module (predeclaration) */module AP_MODULE_DECLARE_DATA rewrite_module; /* rewritemap int: handler function registry */static apr_hash_t *mapfunc_hash; /* the cache */static cache *cachep; /* whether proxy module is available or not */static int proxy_available;static const char *lockname;static apr_global_mutex_t *rewrite_mapr_lock_acquire = NULL;static apr_global_mutex_t *rewrite_log_lock = NULL;/* Optional functions imported from mod_ssl when loaded: */static APR_OPTIONAL_FN_TYPE(ssl_var_lookup) *rewrite_ssl_lookup = NULL;static APR_OPTIONAL_FN_TYPE(ssl_is_https) *rewrite_is_https = NULL;/*** +-------------------------------------------------------+** | |** | configuration directive handling** | |** +-------------------------------------------------------+*//***** per-server configuration structure handling***/static void *config_server_create(apr_pool_t *p, server_rec *s){ rewrite_server_conf *a; a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf)); a->state = ENGINE_DISABLED; a->options = OPTION_NONE; a->rewritelogfile = NULL; a->rewritelogfp = NULL; a->rewriteloglevel = 0; a->rewritemaps = apr_array_make(p, 2, sizeof(rewritemap_entry)); a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry)); a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry)); a->server = s; a->redirect_limit = 0; /* unset (use default) */ return (void *)a;}static void *config_server_merge(apr_pool_t *p, void *basev, void *overridesv){ rewrite_server_conf *a, *base, *overrides; a = (rewrite_server_conf *)apr_pcalloc(p, sizeof(rewrite_server_conf)); base = (rewrite_server_conf *)basev; overrides = (rewrite_server_conf *)overridesv; a->state = overrides->state; a->options = overrides->options; a->server = overrides->server; a->redirect_limit = overrides->redirect_limit ? overrides->redirect_limit : base->redirect_limit; if (a->options & OPTION_INHERIT) { /* * local directives override * and anything else is inherited */ a->rewriteloglevel = overrides->rewriteloglevel != 0 ? overrides->rewriteloglevel : base->rewriteloglevel; a->rewritelogfile = overrides->rewritelogfile != NULL ? overrides->rewritelogfile : base->rewritelogfile; a->rewritelogfp = overrides->rewritelogfp != NULL ? overrides->rewritelogfp : base->rewritelogfp; a->rewritemaps = apr_array_append(p, overrides->rewritemaps, base->rewritemaps); a->rewriteconds = apr_array_append(p, overrides->rewriteconds, base->rewriteconds); a->rewriterules = apr_array_append(p, overrides->rewriterules, base->rewriterules); } else { /* * local directives override * and anything else gets defaults */ a->rewriteloglevel = overrides->rewriteloglevel; a->rewritelogfile = overrides->rewritelogfile; a->rewritelogfp = overrides->rewritelogfp; a->rewritemaps = overrides->rewritemaps; a->rewriteconds = overrides->rewriteconds; a->rewriterules = overrides->rewriterules; } return (void *)a;}/***** per-directory configuration structure handling***/static void *config_perdir_create(apr_pool_t *p, char *path){ rewrite_perdir_conf *a; a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf)); a->state = ENGINE_DISABLED; a->options = OPTION_NONE; a->baseurl = NULL; a->rewriteconds = apr_array_make(p, 2, sizeof(rewritecond_entry)); a->rewriterules = apr_array_make(p, 2, sizeof(rewriterule_entry)); a->redirect_limit = 0; /* unset (use server config) */ if (path == NULL) { a->directory = NULL; } else { /* make sure it has a trailing slash */ if (path[strlen(path)-1] == '/') { a->directory = apr_pstrdup(p, path); } else { a->directory = apr_pstrcat(p, path, "/", NULL); } } return (void *)a;}static void *config_perdir_merge(apr_pool_t *p, void *basev, void *overridesv){ rewrite_perdir_conf *a, *base, *overrides; a = (rewrite_perdir_conf *)apr_pcalloc(p, sizeof(rewrite_perdir_conf)); base = (rewrite_perdir_conf *)basev; overrides = (rewrite_perdir_conf *)overridesv; a->state = overrides->state; a->options = overrides->options; a->directory = overrides->directory; a->baseurl = overrides->baseurl; a->redirect_limit = overrides->redirect_limit ? overrides->redirect_limit : base->redirect_limit; if (a->options & OPTION_INHERIT) { a->rewriteconds = apr_array_append(p, overrides->rewriteconds, base->rewriteconds); a->rewriterules = apr_array_append(p, overrides->rewriterules, base->rewriterules); } else { a->rewriteconds = overrides->rewriteconds; a->rewriterules = overrides->rewriterules; } return (void *)a;}/***** the configuration commands***/static const char *cmd_rewriteengine(cmd_parms *cmd, void *in_dconf, int flag){ rewrite_perdir_conf *dconf = in_dconf; rewrite_server_conf *sconf; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module); if (cmd->path == NULL) { /* is server command */ sconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED); } else /* is per-directory command */ { dconf->state = (flag ? ENGINE_ENABLED : ENGINE_DISABLED); } return NULL;}static const char *cmd_rewriteoptions(cmd_parms *cmd, void *in_dconf, const char *option){ int options = 0, limit = 0; char *w; while (*option) { w = ap_getword_conf(cmd->pool, &option); if (!strcasecmp(w, "inherit")) { options |= OPTION_INHERIT; } else if (!strncasecmp(w, "MaxRedirects=", 13)) { limit = atoi(&w[13]); if (limit <= 0) { return "RewriteOptions: MaxRedirects takes a number greater " "than zero."; } } else if (!strcasecmp(w, "MaxRedirects")) { /* be nice */ return "RewriteOptions: MaxRedirects has the format MaxRedirects" "=n."; } else { return apr_pstrcat(cmd->pool, "RewriteOptions: unknown option '", w, "'", NULL); } } /* put it into the appropriate config */ if (cmd->path == NULL) { /* is server command */ rewrite_server_conf *conf = ap_get_module_config(cmd->server->module_config, &rewrite_module); conf->options |= options; conf->redirect_limit = limit; } else { /* is per-directory command */ rewrite_perdir_conf *conf = in_dconf; conf->options |= options; conf->redirect_limit = limit; } return NULL;}static const char *cmd_rewritelog(cmd_parms *cmd, void *dconf, const char *a1){ rewrite_server_conf *sconf; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module); sconf->rewritelogfile = a1; return NULL;}static const char *cmd_rewriteloglevel(cmd_parms *cmd, void *dconf, const char *a1){ rewrite_server_conf *sconf; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -