📄 mod_rewrite.c
字号:
char *cp3; char *key; char *val; const char *err; if (str[0] != '[' || str[strlen(str)-1] != ']') { return "RewriteRule: bad flag delimiters"; } cp = str+1; str[strlen(str)-1] = ','; /* for simpler parsing */ for ( ; *cp != '\0'; ) { /* skip whitespaces */ for ( ; (*cp == ' ' || *cp == '\t') && *cp != '\0'; cp++) ; if (*cp == '\0') { break; } cp1 = cp; if ((cp2 = strchr(cp, ',')) != NULL) { cp = cp2+1; for ( ; (*(cp2-1) == ' ' || *(cp2-1) == '\t'); cp2--) ; *cp2 = '\0'; if ((cp3 = strchr(cp1, '=')) != NULL) { *cp3 = '\0'; key = cp1; val = cp3+1; } else { key = cp1; val = ""; } if ((err = cmd_rewriterule_setflag(p, cfg, key, val)) != NULL) { return err; } } else { break; } } return NULL;}static const char *cmd_rewriterule_setflag(apr_pool_t *p, rewriterule_entry *cfg, char *key, char *val){ int status = 0; int i; if ( strcasecmp(key, "redirect") == 0 || strcasecmp(key, "R") == 0 ) { cfg->flags |= RULEFLAG_FORCEREDIRECT; if (strlen(val) > 0) { if (strcasecmp(val, "permanent") == 0) { status = HTTP_MOVED_PERMANENTLY; } else if (strcasecmp(val, "temp") == 0) { status = HTTP_MOVED_TEMPORARILY; } else if (strcasecmp(val, "seeother") == 0) { status = HTTP_SEE_OTHER; } else if (apr_isdigit(*val)) { status = atoi(val); } if (!ap_is_HTTP_REDIRECT(status)) { return "RewriteRule: invalid HTTP response code " "for flag 'R'"; } cfg->forced_responsecode = status; } } else if ( strcasecmp(key, "noescape") == 0 || strcasecmp(key, "NE") == 0 ) { cfg->flags |= RULEFLAG_NOESCAPE; } else if ( strcasecmp(key, "last") == 0 || strcasecmp(key, "L") == 0 ) { cfg->flags |= RULEFLAG_LASTRULE; } else if ( strcasecmp(key, "next") == 0 || strcasecmp(key, "N") == 0 ) { cfg->flags |= RULEFLAG_NEWROUND; } else if ( strcasecmp(key, "chain") == 0 || strcasecmp(key, "C") == 0 ) { cfg->flags |= RULEFLAG_CHAIN; } else if ( strcasecmp(key, "type") == 0 || strcasecmp(key, "T") == 0 ) { cfg->forced_mimetype = apr_pstrdup(p, val); ap_str_tolower(cfg->forced_mimetype); } else if ( strcasecmp(key, "env") == 0 || strcasecmp(key, "E") == 0 ) { for (i = 0; (cfg->env[i] != NULL) && (i < MAX_ENV_FLAGS); i++) ; if (i < MAX_ENV_FLAGS) { cfg->env[i] = apr_pstrdup(p, val); cfg->env[i+1] = NULL; } else { return "RewriteRule: too many environment flags 'E'"; } } else if ( strcasecmp(key, "cookie") == 0 || strcasecmp(key, "CO") == 0) { for (i = 0; (cfg->cookie[i] != NULL) && (i < MAX_COOKIE_FLAGS); i++) ; if (i < MAX_COOKIE_FLAGS) { cfg->cookie[i] = apr_pstrdup(p, val); cfg->cookie[i+1] = NULL; } else { return "RewriteRule: too many cookie flags 'CO'"; } } else if ( strcasecmp(key, "nosubreq") == 0 || strcasecmp(key, "NS") == 0 ) { cfg->flags |= RULEFLAG_IGNOREONSUBREQ; } else if ( strcasecmp(key, "proxy") == 0 || strcasecmp(key, "P") == 0 ) { cfg->flags |= RULEFLAG_PROXY; } else if ( strcasecmp(key, "passthrough") == 0 || strcasecmp(key, "PT") == 0 ) { cfg->flags |= RULEFLAG_PASSTHROUGH; } else if ( strcasecmp(key, "skip") == 0 || strcasecmp(key, "S") == 0 ) { cfg->skip = atoi(val); } else if ( strcasecmp(key, "forbidden") == 0 || strcasecmp(key, "F") == 0 ) { cfg->flags |= RULEFLAG_FORBIDDEN; } else if ( strcasecmp(key, "gone") == 0 || strcasecmp(key, "G") == 0 ) { cfg->flags |= RULEFLAG_GONE; } else if ( strcasecmp(key, "qsappend") == 0 || strcasecmp(key, "QSA") == 0 ) { cfg->flags |= RULEFLAG_QSAPPEND; } else if ( strcasecmp(key, "nocase") == 0 || strcasecmp(key, "NC") == 0 ) { cfg->flags |= RULEFLAG_NOCASE; } else { return apr_pstrcat(p, "RewriteRule: unknown flag '", key, "'", NULL); } return NULL;}/***** Global Module Initialization***/static int pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp){ APR_OPTIONAL_FN_TYPE(ap_register_rewrite_mapfunc) *map_pfn_register; /* register int: rewritemap handlers */ mapfunc_hash = apr_hash_make(pconf); map_pfn_register = APR_RETRIEVE_OPTIONAL_FN(ap_register_rewrite_mapfunc); if (map_pfn_register) { map_pfn_register("tolower", rewrite_mapfunc_tolower); map_pfn_register("toupper", rewrite_mapfunc_toupper); map_pfn_register("escape", rewrite_mapfunc_escape); map_pfn_register("unescape", rewrite_mapfunc_unescape); } return OK;}static int post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s){ apr_status_t rv; void *data; int first_time = 0; const char *userdata_key = "rewrite_init_module"; apr_pool_userdata_get(&data, userdata_key, s->process->pool); if (!data) { first_time = 1; apr_pool_userdata_set((const void *)1, userdata_key, apr_pool_cleanup_null, s->process->pool); } /* check if proxy module is available */ proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL); /* create the rewriting lockfiles in the parent */ if ((rv = apr_global_mutex_create(&rewrite_log_lock, NULL, APR_LOCK_DEFAULT, p)) != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, "mod_rewrite: could not create rewrite_log_lock"); return HTTP_INTERNAL_SERVER_ERROR; }#ifdef MOD_REWRITE_SET_MUTEX_PERMS rv = unixd_set_global_mutex_perms(rewrite_log_lock); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, "mod_rewrite: Could not set permissions on " "rewrite_log_lock; check User and Group directives"); return HTTP_INTERNAL_SERVER_ERROR; }#endif rv = rewritelock_create(s, p); if (rv != APR_SUCCESS) { return HTTP_INTERNAL_SERVER_ERROR; } apr_pool_cleanup_register(p, (void *)s, rewritelock_remove, apr_pool_cleanup_null); /* step through the servers and * - open each rewriting logfile * - open the RewriteMap prg:xxx programs */ for (; s; s = s->next) { if (!open_rewritelog(s, p)) { return HTTP_INTERNAL_SERVER_ERROR; } if (!first_time) { if (run_rewritemap_programs(s, p) != APR_SUCCESS) { return HTTP_INTERNAL_SERVER_ERROR; } } } rewrite_ssl_lookup = APR_RETRIEVE_OPTIONAL_FN(ssl_var_lookup); rewrite_is_https = APR_RETRIEVE_OPTIONAL_FN(ssl_is_https); return OK;}/***** Per-Child Module Initialization** [called after a child process is spawned]***/static void init_child(apr_pool_t *p, server_rec *s){ apr_status_t rv; if (lockname != NULL && *(lockname) != '\0') { rv = apr_global_mutex_child_init(&rewrite_mapr_lock_acquire, lockname, p); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, "mod_rewrite: could not init rewrite_mapr_lock_acquire" " in child"); } } rv = apr_global_mutex_child_init(&rewrite_log_lock, NULL, p); if (rv != APR_SUCCESS) { ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, "mod_rewrite: could not init rewrite log lock in child"); } /* create the lookup cache */ cachep = init_cache(p);}/*** +-------------------------------------------------------+** | |** | runtime hooks** | |** +-------------------------------------------------------+*//***** URI-to-filename hook**** [used for the rewriting engine triggered by** the per-server 'RewriteRule' directives]***/static int hook_uri2file(request_rec *r){ rewrite_server_conf *conf; const char *saved_rulestatus; const char *var; const char *thisserver; char *thisport; const char *thisurl; char buf[512]; char docroot[512]; const char *ccp; unsigned int port; int rulestatus; int n; int l; /* * retrieve the config structures */ conf = ap_get_module_config(r->server->module_config, &rewrite_module); /* * only do something under runtime if the engine is really enabled, * else return immediately! */ if (conf->state == ENGINE_DISABLED) { 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. */ if (conf->server != r->server) { 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 */ if (r->main == NULL) { var = apr_pstrcat(r->pool, "REDIRECT_", ENVVAR_SCRIPT_URL, NULL); var = apr_table_get(r->subprocess_env, var); if (var == NULL) { apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri); } else { apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var); } } else { var = apr_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL); apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var); } /* * 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 { apr_snprintf(buf, sizeof(buf), ":%u", port); thisport = buf; } thisurl = apr_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL); /* set the variable */ var = apr_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport, thisurl, NULL); apr_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var); if (!(saved_rulestatus = apr_table_get(r->notes,"mod_rewrite_rewritten"))) { /* if filename was not initially set, * we start with the requested URI */ if (r->filename == NULL) { r->filename = apr_pstrdup(r->pool, r->uri); rewritelog(r, 2, "init rewrite engine with requested uri %s", r->filename); } else { rewritelog(r, 2, "init rewrite engine with passed filename %s." " Original uri = %s", r->filename, r->uri); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -