📄 mod_rewrite.c
字号:
dconf->rewriteconds = ap_make_array(cmd->pool, 2, sizeof(rewritecond_entry)); } return NULL;}static const char *cmd_rewriterule_parseflagfield(pool *p, rewriterule_entry *cfg, char *str){ char *cp; char *cp1; char *cp2; 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(pool *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 (ap_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, "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 = ap_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] = ap_pstrdup(p, val); cfg->env[i+1] = NULL; } else { return "RewriteRule: too many environment flags 'E'"; } } 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 { return ap_pstrcat(p, "RewriteRule: unknown flag '", key, "'\n", NULL); } return NULL;}/***** Global Module Initialization** [called from read_config() after all** config commands were already called]***/static void init_module(server_rec *s, pool *p){ /* check if proxy module is available */ proxy_available = (ap_find_linked_module("mod_proxy.c") != NULL); /* precompile a static pattern for the txt mapfile parsing */ lookup_map_txtfile_regexp = ap_pregcomp(p, MAPFILE_PATTERN, REG_EXTENDED); /* create the rewriting lockfile in the parent */ rewritelock_create(s, p); ap_register_cleanup(p, (void *)s, rewritelock_remove, ap_null_cleanup); /* step through the servers and * - open each rewriting logfile * - open the RewriteMap prg:xxx programs */ for (; s; s = s->next) { open_rewritelog(s, p); run_rewritemap_programs(s, p); }}/***** Per-Child Module Initialization** [called after a child process is spawned]***/static void init_child(server_rec *s, pool *p){ /* open the rewriting lockfile */ rewritelock_open(s, p); /* 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){ void *sconf; rewrite_server_conf *conf; const char *var; const char *thisserver; char *thisport; const char *thisurl; char buf[512]; char docroot[512]; char *cp, *cp2; const char *ccp; struct stat finfo; unsigned int port; int n; int l; /* * retrieve the config structures */ sconf = r->server->module_config; conf = (rewrite_server_conf *)ap_get_module_config(sconf, &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 = ap_pstrcat(r->pool, "REDIRECT_", ENVVAR_SCRIPT_URL, NULL); var = ap_table_get(r->subprocess_env, var); if (var == NULL) { ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, r->uri); } else { ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URL, var); } } else { var = ap_table_get(r->main->subprocess_env, ENVVAR_SCRIPT_URL); ap_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 { ap_snprintf(buf, sizeof(buf), ":%u", port); thisport = buf; } thisurl = ap_table_get(r->subprocess_env, ENVVAR_SCRIPT_URL); /* set the variable */ var = ap_pstrcat(r->pool, ap_http_method(r), "://", thisserver, thisport, thisurl, NULL); ap_table_setn(r->subprocess_env, ENVVAR_SCRIPT_URI, var); /* if filename was not initially set, * we start with the requested URI */ if (r->filename == NULL) { r->filename = ap_pstrdup(r->pool, r->uri); rewritelog(r, 2, "init rewrite engine with requested uri %s", r->filename); } /* * now apply the rules ... */ if (apply_rewrite_list(r, conf->rewriterules, NULL)) { if (strlen(r->filename) > 6 && strncmp(r->filename, "proxy:", 6) == 0) { /* it should be go on as an internal proxy request */ /* check if the proxy module is enabled, so * we can actually use it! */ if (!proxy_available) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "attempt to make remote request from mod_rewrite " "without proxy enabled: %s", r->filename); return FORBIDDEN; } /* make sure the QUERY_STRING and * PATH_INFO parts get incorporated */ if (r->path_info != NULL) { r->filename = ap_pstrcat(r->pool, r->filename, r->path_info, NULL); } if (r->args != NULL && r->uri == r->unparsed_uri) { /* see proxy_http:proxy_http_canon() */ r->filename = ap_pstrcat(r->pool, r->filename, "?", r->args, NULL); } /* now make sure the request gets handled by the proxy handler */ r->proxyreq = 1; r->handler = "proxy-server"; rewritelog(r, 1, "go-ahead with proxy request %s [OK]", r->filename); return OK; } else if ( (strlen(r->filename) > 7 && strncasecmp(r->filename, "http://", 7) == 0) || (strlen(r->filename) > 8 && strncasecmp(r->filename, "https://", 8) == 0) || (strlen(r->filename) > 9 && strncasecmp(r->filename, "gopher://", 9) == 0) || (strlen(r->filename) > 6 && strncasecmp(r->filename, "ftp://", 6) == 0) ) { /* it was finally rewritten to a remote URL */ /* skip 'scheme:' */ for (cp = r->filename; *cp != ':' && *cp != '\0'; cp++) ; /* skip '://' */ cp += 3; /* skip host part */ for ( ; *cp != '/' && *cp != '\0'; cp++) ; if (*cp != '\0') { rewritelog(r, 1, "escaping %s for redirect", r->filename); cp2 = escape_uri(r->pool, cp); *cp = '\0'; r->filename = ap_pstrcat(r->pool, r->filename, cp2, NULL); } /* append the QUERY_STRING part */ if (r->args != NULL) { r->filename = ap_pstrcat(r->pool, r->filename, "?", r->args, NULL); } /* determine HTTP redirect response code */ if (ap_is_HTTP_REDIRECT(r->status)) { n = r->status;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -