📄 mod_rewrite.c
字号:
sconf->rewriteloglevel = atoi(a1); return NULL;}static const char *cmd_rewritemap(cmd_parms *cmd, void *dconf, const char *a1, const char *a2){ rewrite_server_conf *sconf; rewritemap_entry *newmap; apr_finfo_t st; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module); newmap = apr_array_push(sconf->rewritemaps); newmap->name = a1; newmap->func = NULL; if (strncmp(a2, "txt:", 4) == 0) { newmap->type = MAPTYPE_TXT; newmap->datafile = a2+4; newmap->checkfile = a2+4; newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s", (void *)cmd->server, a1); } else if (strncmp(a2, "rnd:", 4) == 0) { newmap->type = MAPTYPE_RND; newmap->datafile = a2+4; newmap->checkfile = a2+4; newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s", (void *)cmd->server, a1); } else if (strncmp(a2, "dbm", 3) == 0) { const char *ignored_fname; int bad = 0; apr_status_t rv; newmap->type = MAPTYPE_DBM; newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s", (void *)cmd->server, a1); if (a2[3] == ':') { newmap->dbmtype = "default"; newmap->datafile = a2+4; } else if (a2[3] == '=') { const char *colon = ap_strchr_c(a2 + 4, ':'); if (colon) { newmap->dbmtype = apr_pstrndup(cmd->pool, a2 + 4, colon - (a2 + 3) - 1); newmap->datafile = colon + 1; } else { ++bad; } } else { ++bad; } if (bad) { return apr_pstrcat(cmd->pool, "RewriteMap: bad map:", a2, NULL); } rv = apr_dbm_get_usednames_ex(cmd->pool, newmap->dbmtype, newmap->datafile, &newmap->checkfile, &ignored_fname); if (rv != APR_SUCCESS) { return apr_pstrcat(cmd->pool, "RewriteMap: dbm type ", newmap->dbmtype, " is invalid", NULL); } } else if (strncmp(a2, "prg:", 4) == 0) { newmap->type = MAPTYPE_PRG; apr_tokenize_to_argv(a2 + 4, &newmap->argv, cmd->pool); newmap->datafile = NULL; newmap->checkfile = newmap->argv[0]; newmap->cachename = NULL; } else if (strncmp(a2, "int:", 4) == 0) { newmap->type = MAPTYPE_INT; newmap->datafile = NULL; newmap->checkfile = NULL; newmap->cachename = NULL; newmap->func = (char *(*)(request_rec *,char *)) apr_hash_get(mapfunc_hash, a2+4, strlen(a2+4)); if ((sconf->state == ENGINE_ENABLED) && (newmap->func == NULL)) { return apr_pstrcat(cmd->pool, "RewriteMap: internal map not found:", a2+4, NULL); } } else { newmap->type = MAPTYPE_TXT; newmap->datafile = a2; newmap->checkfile = a2; newmap->cachename = apr_psprintf(cmd->pool, "%pp:%s", (void *)cmd->server, a1); } newmap->fpin = NULL; newmap->fpout = NULL; if (newmap->checkfile && (sconf->state == ENGINE_ENABLED) && (apr_stat(&st, newmap->checkfile, APR_FINFO_MIN, cmd->pool) != APR_SUCCESS)) { return apr_pstrcat(cmd->pool, "RewriteMap: file for map ", newmap->name, " not found:", newmap->checkfile, NULL); } return NULL;}static const char *cmd_rewritelock(cmd_parms *cmd, void *dconf, const char *a1){ const char *error; if ((error = ap_check_cmd_context(cmd, GLOBAL_ONLY)) != NULL) return error; /* fixup the path, especially for rewritelock_remove() */ lockname = ap_server_root_relative(cmd->pool, a1); if (!lockname) { return apr_pstrcat(cmd->pool, "Invalid RewriteLock path ", a1); } return NULL;}static const char *cmd_rewritebase(cmd_parms *cmd, void *in_dconf, const char *a1){ rewrite_perdir_conf *dconf = in_dconf; if (cmd->path == NULL || dconf == NULL) { return "RewriteBase: only valid in per-directory config files"; } if (a1[0] == '\0') { return "RewriteBase: empty URL not allowed"; } if (a1[0] != '/') { return "RewriteBase: argument is not a valid URL"; } dconf->baseurl = a1; return NULL;}static const char *cmd_rewritecond(cmd_parms *cmd, void *in_dconf, const char *in_str){ rewrite_perdir_conf *dconf = in_dconf; char *str = apr_pstrdup(cmd->pool, in_str); rewrite_server_conf *sconf; rewritecond_entry *newcond; regex_t *regexp; char *a1; char *a2; char *a3; char *cp; const char *err; int rc; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module); /* make a new entry in the internal temporary rewrite rule list */ if (cmd->path == NULL) { /* is server command */ newcond = apr_array_push(sconf->rewriteconds); } else { /* is per-directory command */ newcond = apr_array_push(dconf->rewriteconds); } /* parse the argument line ourself */ if (parseargline(str, &a1, &a2, &a3)) { return apr_pstrcat(cmd->pool, "RewriteCond: bad argument line '", str, "'", NULL); } /* arg1: the input string */ newcond->input = apr_pstrdup(cmd->pool, a1); /* arg3: optional flags field (this have to be first parsed, because we need to know if the regex should be compiled with ICASE!) */ newcond->flags = CONDFLAG_NONE; if (a3 != NULL) { if ((err = cmd_rewritecond_parseflagfield(cmd->pool, newcond, a3)) != NULL) { return err; } } /* arg2: the pattern try to compile the regexp to test if is ok */ cp = a2; if (cp[0] == '!') { newcond->flags |= CONDFLAG_NOTMATCH; cp++; } /* now be careful: Under the POSIX regex library we can compile the pattern for case insensitive matching, under the old V8 library we have to do it self via a hack */ if (newcond->flags & CONDFLAG_NOCASE) { rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED|REG_ICASE)) == NULL); } else { rc = ((regexp = ap_pregcomp(cmd->pool, cp, REG_EXTENDED)) == NULL); } if (rc) { return apr_pstrcat(cmd->pool, "RewriteCond: cannot compile regular expression '", a2, "'", NULL); } newcond->pattern = apr_pstrdup(cmd->pool, cp); newcond->regexp = regexp; return NULL;}static const char *cmd_rewritecond_parseflagfield(apr_pool_t *p, rewritecond_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 "RewriteCond: 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_rewritecond_setflag(p, cfg, key, val)) != NULL) { return err; } } else { break; } } return NULL;}static const char *cmd_rewritecond_setflag(apr_pool_t *p, rewritecond_entry *cfg, char *key, char *val){ if ( strcasecmp(key, "nocase") == 0 || strcasecmp(key, "NC") == 0 ) { cfg->flags |= CONDFLAG_NOCASE; } else if ( strcasecmp(key, "ornext") == 0 || strcasecmp(key, "OR") == 0 ) { cfg->flags |= CONDFLAG_ORNEXT; } else { return apr_pstrcat(p, "RewriteCond: unknown flag '", key, "'", NULL); } return NULL;}static const char *cmd_rewriterule(cmd_parms *cmd, void *in_dconf, const char *in_str){ rewrite_perdir_conf *dconf = in_dconf; char *str = apr_pstrdup(cmd->pool, in_str); rewrite_server_conf *sconf; rewriterule_entry *newrule; regex_t *regexp; char *a1; char *a2; char *a3; char *cp; const char *err; int mode; sconf = ap_get_module_config(cmd->server->module_config, &rewrite_module); /* make a new entry in the internal rewrite rule list */ if (cmd->path == NULL) { /* is server command */ newrule = apr_array_push(sconf->rewriterules); } else { /* is per-directory command */ newrule = apr_array_push(dconf->rewriterules); } /* parse the argument line ourself */ if (parseargline(str, &a1, &a2, &a3)) { return apr_pstrcat(cmd->pool, "RewriteRule: bad argument line '", str, "'", NULL); } /* arg3: optional flags field */ newrule->forced_mimetype = NULL; newrule->forced_responsecode = HTTP_MOVED_TEMPORARILY; newrule->flags = RULEFLAG_NONE; newrule->env[0] = NULL; newrule->cookie[0] = NULL; newrule->skip = 0; if (a3 != NULL) { if ((err = cmd_rewriterule_parseflagfield(cmd->pool, newrule, a3)) != NULL) { return err; } } /* arg1: the pattern * try to compile the regexp to test if is ok */ cp = a1; if (cp[0] == '!') { newrule->flags |= RULEFLAG_NOTMATCH; cp++; } mode = REG_EXTENDED; if (newrule->flags & RULEFLAG_NOCASE) { mode |= REG_ICASE; } if ((regexp = ap_pregcomp(cmd->pool, cp, mode)) == NULL) { return apr_pstrcat(cmd->pool, "RewriteRule: cannot compile regular expression '", a1, "'", NULL); } newrule->pattern = apr_pstrdup(cmd->pool, cp); newrule->regexp = regexp; /* arg2: the output string * replace the $<N> by \<n> which is needed by the currently * used Regular Expression library * * TODO: Is this still required for PCRE? If not, does it *work* with PCRE? */ newrule->output = apr_pstrdup(cmd->pool, a2); /* now, if the server or per-dir config holds an * array of RewriteCond entries, we take it for us * and clear the array */ if (cmd->path == NULL) { /* is server command */ newrule->rewriteconds = sconf->rewriteconds; sconf->rewriteconds = apr_array_make(cmd->pool, 2, sizeof(rewritecond_entry)); } else { /* is per-directory command */ newrule->rewriteconds = dconf->rewriteconds; dconf->rewriteconds = apr_array_make(cmd->pool, 2, sizeof(rewritecond_entry)); } return NULL;}static const char *cmd_rewriterule_parseflagfield(apr_pool_t *p, rewriterule_entry *cfg, char *str){ char *cp; char *cp1; char *cp2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -