📄 htrules.c
字号:
StrAllocCopy(current, r->equiv); /* use entire translation */ } else { char * ins = strchr(r->equiv, '*'); /* Insertion point */ if (ins) { /* Consistent rule!!! */ char * temp = NULL; HTSprintf0(&temp, "%.*s%.*s%s", ins - r->equiv, r->equiv, m, q, ins + 1); CTRACE((tfp, "For `%s' using `%s'\n", current, temp)); FREE(current); current = temp; /* Use this */ } else { /* No insertion point */ char * temp = NULL; StrAllocCopy(temp, r->equiv); CTRACE((tfp, "For `%s' using `%s'\n", current, temp)); FREE(current); current = temp; /* Use this */ } /* If no insertion point exists */ } if (r->op == HT_Pass) { if (proxy_none_flag) { char * temp = NULL; StrAllocCopy(temp, "NoProxy="); StrAllocCat(temp, current); FREE(current); current = temp; } CTRACE((tfp, "HTRule: ...and pass `%s'\n", current)); return current; } else if (r->op == HT_Redirect) { CTRACE((tfp, "HTRule: ...and redirect to `%s'\n", current)); redirecting_url = current; HTPermitRedir = (BOOL) (permitredir_flag == 1); return (char *)0; } else if (r->op == HT_RedirectPerm) { CTRACE((tfp, "HTRule: ...and redirect like 301 to `%s'\n", current)); redirecting_url = current; permanent_redirection = TRUE; HTPermitRedir = (BOOL) (permitredir_flag == 1); return (char *)0; } break; case HT_UseProxy: if (r->equiv && 0==strcasecomp(r->equiv, "none")) { CTRACE((tfp, "For `%s' will not use proxy\n", current)); proxy_none_flag = 1; } else if (proxy_none_flag) { CTRACE((tfp, "For `%s' proxy server ignored: %s\n", current, NONNULL(r->equiv))); } else { char * temp = NULL; StrAllocCopy(temp, "Proxied="); StrAllocCat(temp, r->equiv); StrAllocCat(temp, current); CTRACE((tfp, "HTRule: proxy server found: %s\n", NONNULL(r->equiv))); FREE(current); return temp; } break; case HT_Invalid: case HT_Fail: /* Unauthorised */ CTRACE((tfp, "HTRule: *** FAIL `%s'\n", current)); FREE(current); return (char *)0; } /* if tail matches ... switch operation */ } /* loop over rules */ if (proxy_none_flag) { char * temp = NULL; StrAllocCopy(temp, "NoProxy="); StrAllocCat(temp, current); FREE(current); return temp; } return current;}/* Load one line of configuration** ------------------------------**** Call this, for example, to load a X resource with config info.**** returns 0 OK, < 0 syntax error.*/PUBLIC int HTSetConfiguration ARGS1( char *, config){ HTRuleOp op; char * line = NULL; char * pointer = line; char *word1, *word2, *word3; char *cond_op=NULL, *cond=NULL; float quality, secs, secs_per_byte; int maxbytes; int status; StrAllocCopy(line, config); { char * p = strchr(line, '#'); /* Chop off comments */ if (p) *p = 0; } pointer = line; word1 = HTNextField(&pointer); if (!word1) { FREE(line); return 0; } ; /* Comment only or blank */ word2 = HTNextField(&pointer); if (0==strcasecomp(word1, "defprot") || 0==strcasecomp(word1, "protect")) word3 = pointer; /* The rest of the line to be parsed by AA module */ else word3 = HTNextField(&pointer); /* Just the next word */ if (!word2) { fprintf(stderr, "HTRule: %s %s\n", RULE_NEEDS_DATA, line); FREE(line); return -2; /*syntax error */ } if (0==strcasecomp(word1, "suffix")) { char * encoding = HTNextField(&pointer); if (pointer) status = sscanf(pointer, "%f", &quality); else status = 0; HTSetSuffix(word2, word3, encoding ? encoding : "binary", status >= 1? quality : (float) 1.0); } else if (0==strcasecomp(word1, "presentation")) { if (pointer) status = sscanf(pointer, "%f%f%f%d", &quality, &secs, &secs_per_byte, &maxbytes); else status = 0; HTSetPresentation(word2, word3, status >= 1 ? quality : 1.0, status >= 2 ? secs : 0.0, status >= 3 ? secs_per_byte : 0.0, status >= 4 ? maxbytes : 0 ); } else if (0==strncasecomp(word1, "htbin", 5) || 0==strncasecomp(word1, "bindir", 6)) { StrAllocCopy(HTBinDir, word2); /* Physical /htbin location */ } else if (0==strncasecomp(word1, "search", 6)) { StrAllocCopy(HTSearchScript, word2); /* Search script name */ } else { op = 0==strcasecomp(word1, "map") ? HT_Map : 0==strcasecomp(word1, "pass") ? HT_Pass : 0==strcasecomp(word1, "fail") ? HT_Fail : 0==strcasecomp(word1, "redirect") ? HT_Redirect : 0==strncasecomp(word1, "redirectperm", 12) ? HT_RedirectPerm : 0==strcasecomp(word1, "redirecttemp") ? HT_Redirect : 0==strcasecomp(word1, "permitredirection") ? HT_PermitRedir : 0==strcasecomp(word1, "useproxy") ? HT_UseProxy : 0==strcasecomp(word1, "alert") ? HT_Alert : 0==strcasecomp(word1, "alwaysalert") ? HT_AlwaysAlert : 0==strcasecomp(word1, "progress") ? HT_Progress : 0==strcasecomp(word1, "usermsg") ? HT_UserMsg : 0==strcasecomp(word1, "infomsg") ? HT_InfoMsg : 0==strcasecomp(word1, "defprot") ? HT_DefProt : 0==strcasecomp(word1, "protect") ? HT_Protect : HT_Invalid; if (op==HT_Invalid) { fprintf(stderr, "HTRule: %s '%s'\n", RULE_INCORRECT, config); } else { switch (op) { case HT_Fail: /* never a or other 2nd parameter */ case HT_PermitRedir: cond_op = word3; if (cond_op && *cond_op) { word3 = NULL; cond = HTNextField(&pointer); } break; case HT_Pass: /* possibly a URL2 */ if (word3 && (!strcasecomp(word3, "if") || !strcasecomp(word3, "unless"))) { cond_op = word3; word3 = NULL; cond = HTNextField(&pointer); break; } /* else fall through */ case HT_Map: /* always a URL2 (or other 2nd parameter) */ case HT_Redirect: case HT_RedirectPerm: case HT_UseProxy: cond_op = HTNextField(&pointer); /* check for extra status word in "Redirect" */ if (op==HT_Redirect && 0==strcasecomp(word1, "redirect") && cond_op && strcasecomp(cond_op, "if") && strcasecomp(cond_op, "unless")) { if (0==strcmp(word2, "301") || 0==strcasecomp(word2, "permanent")) { op = HT_RedirectPerm; } else if (!(0==strcmp(word2, "302") || 0==strcmp(word2, "303") || 0==strcasecomp(word2, "temp") || 0==strcasecomp(word2, "seeother"))) { CTRACE((tfp, "Rule: Ignoring `%s' in Redirect\n", word2)); } word2 = word3; word3 = cond_op; /* cond_op isn't condition op after all */ cond_op = HTNextField(&pointer); } if (cond_op && *cond_op) cond = HTNextField(&pointer); break; case HT_Progress: case HT_InfoMsg: case HT_UserMsg: case HT_Alert: case HT_AlwaysAlert: cond_op = HTNextField(&pointer); if (cond_op && *cond_op) cond = HTNextField(&pointer); if (word3) { /* Fix string with too may %s - kw */ char *cp = word3, *cp1, *cp2; while ((cp1=strchr(cp, '%'))) { if (cp1[1] == '\0') { *cp1 = '\0'; break; } else if (cp1[1] == '%') { cp = cp1 + 2; continue; } else while ((cp2=strchr(cp1+2, '%'))) { if (cp2[1] == '\0') { *cp2 = '\0'; break; } else if (cp2[1] == '%') { cp1 = cp2; } else { *cp2 = '?'; /* replace bad % */ cp1 = cp2; } } break; } } break; default: break; } if (cond_op && cond && *cond && !strcasecomp(cond_op, "unless")) { cond_op = "unless"; } else if (cond_op && cond && *cond && !strcasecomp(cond_op, "if")) { cond_op = "if"; } else if (cond_op || cond) { fprintf(stderr, "HTRule: %s '%s'\n", RULE_INCORRECT, config); FREE(line); /* syntax error, condition is a mess - kw */ return -2; /* NB unrecognized cond passes here - kw */ } if (cond && !strncasecomp(cond, "redirected", strlen(cond))) { cond = "redirected"; /* recognized, canonical case - kw */ } else if (cond && strlen(cond) >= 8 && !strncasecomp(cond, "userspecified", strlen(cond))) { cond = "userspec"; /* also allow abbreviation - kw */ } HTAddRule(op, word2, word3, cond_op, cond); } } FREE(line); return 0;}/* Load the rules from a file HTLoadRules()** --------------------------**** On entry,** Rules can be in any state** On exit,** Any existing rules will have been kept.** Any new rules will have been loaded.** Returns 0 if no error, 0 if error!**** Bugs:** The strings may not contain spaces.*/int HTLoadRules ARGS1( CONST char *, filename){ FILE * fp = fopen(filename, TXT_R); char line[LINE_LENGTH+1]; if (!fp) { CTRACE((tfp, "HTRules: Can't open rules file %s\n", filename)); return -1; /* File open error */ } for(;;) { if (!fgets(line, LINE_LENGTH+1, fp)) break; /* EOF or error */ (void) HTSetConfiguration(line); } fclose(fp); return 0; /* No error or syntax errors ignored */}#endif /* NO_RULES */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -