HTRules.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:11k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. /*       HTRules.c
  2. ** CONFIGURATION MANAGER FOR CLIENTS
  3. **
  4. ** (c) COPYRIGHT MIT 1995.
  5. ** Please first read the full copyright statement in the file COPYRIGH.
  6. **
  7. ** This module manages rule files in the Library
  8. **
  9. ** History:
  10. **  3 Jun 91 Written TBL
  11. ** 10 Aug 91 Authorisation added after Daniel Martin (pass, fail)
  12. ** Rule order in file changed
  13. ** Comments allowed with # on 1st char of rule line
  14. **      17 Jun 92       Bug fix: pass and fail failed if didn't contain '*' TBL
  15. **       1 Sep 93       Bug fix: no memory check - Nathan Torkington
  16. **                      BYTE_ADDRESSING removed - Arthur Secret
  17. ** 11 Sep 93  MD Changed %i into %d in debug printf. 
  18. ** VMS does not recognize %i.
  19. ** Bug Fix: in case of PASS, only one parameter to printf.
  20. ** 19 Sep 93  AL Added Access Authorization stuff.
  21. **  1 Nov 93  AL Added htbin.
  22. ** 30 Nov 93  AL Added HTTranslateReq().
  23. **  4 Feb 94  AL Took away all the daemon-specific stuff.
  24. **      28 Sep 94  HWL  Added field to HTPresentation_add call
  25. ** 15 Nov 95  HFN Made a stream, fixed interface and made new translater
  26. **
  27. ** BUGS: We only have one wildcard match pr rule!
  28. */
  29. /* Library include files */
  30. #include "WWWLib.h"
  31. #include "HTProxy.h"
  32. #include "HTRules.h"  /* Implemented here */
  33. struct _HTStream {
  34.     CONST HTStreamClass * isa;
  35.     HTRequest * request;
  36.     HTStream * target;
  37.     HTChunk * buffer;
  38.     HTSocketEOL EOLstate;
  39. };
  40. struct _HTRule {
  41.     HTRuleOp op;
  42.     char * pattern;
  43.     char * replace;
  44.     int    insert;        /* Index into any wildcard in replace */
  45. };
  46. PRIVATE HTList * rules = NULL;
  47. /* ------------------------------------------------------------------------- */
  48. /*
  49. ** Rules are handled as list as everything else that has to do with
  50. ** preferences. We provide two functions for getting and setting the
  51. ** global rules
  52. */
  53. PUBLIC HTList * HTRule_global (void)
  54. {
  55.     if (!rules) rules = HTList_new();
  56.     return rules;
  57. }
  58. PUBLIC BOOL HTRule_setGlobal(HTList * list)
  59. {
  60.     if (rules) HTRule_deleteAll(rules);
  61.     rules = list;
  62.     return YES;
  63. }
  64. /* Add rule to the list
  65. ** --------------------
  66. ** This function adds a rule to the list of rules. The
  67. ** pattern is a 0-terminated string containing a single
  68. ** "*". <CODE>equiv</CODE> points to the equivalent string with * for the
  69. ** place where the text matched by * goes.
  70. **  On entry,
  71. ** pattern points to 0-terminated string containing a single "*"
  72. ** replace points to the equivalent string with * for the
  73. ** place where the text matched by * goes.
  74. **  On exit,
  75. ** returns YES if OK, else NO
  76. */
  77. PUBLIC BOOL HTRule_add (HTList * list, HTRuleOp op,
  78. CONST char * pattern, CONST char * replace)
  79. {
  80.     if (list && pattern) {
  81. HTRule * me;
  82. if ((me = (HTRule  *) HT_CALLOC(1, sizeof(HTRule))) == NULL)
  83.     HT_OUTOFMEM("HTRule_add");
  84. me->op = op;
  85. StrAllocCopy(me->pattern, pattern);
  86. if (replace) {
  87.     char *ptr = strchr(replace, '*');
  88.     StrAllocCopy(me->replace, replace);
  89.     me->insert = ptr ? ptr-replace : -1;
  90.     if (APP_TRACE)
  91. TTYPrint(TDEST, "Rule Add.... For `%s' op %d `%s'n",
  92.  pattern, op, replace);
  93. } else
  94.     TTYPrint(TDEST, "Rule Add.... For `%s' op %dn", pattern, op);
  95. return HTList_appendObject(rules, (void *) me);
  96.     }
  97.     return NO;
  98. }
  99. /* Delete all rules
  100. ** ----------------
  101. ** Deletes all the rules registered by this module
  102. */
  103. PUBLIC BOOL HTRule_deleteAll (HTList * list)
  104. {
  105.     if (list) {
  106. HTList *cur = list;
  107. HTRule *pres;
  108. while ((pres = (HTRule *) HTList_nextObject(cur))) {
  109.     HT_FREE(pres->pattern);
  110.     HT_FREE(pres->replace);
  111.     HT_FREE(pres);
  112. }
  113. return HTList_delete(list);
  114.     }
  115.     return NO;
  116. }
  117. /* Translate by rules
  118. ** ------------------
  119. ** The most recently defined rules are applied last.
  120. ** This function walks through the list of rules and translates the
  121. ** reference when matches are found. The list is traversed in order
  122. ** starting from the head of the list. It returns the address of the
  123. ** equivalent string allocated from the heap which the CALLER MUST
  124. ** FREE.
  125. */
  126. PUBLIC char * HTRule_translate (HTList * list, CONST char * token,
  127. BOOL ignore_case)
  128. {
  129.     HTRule * pres;
  130.     char * replace = NULL;
  131.     if (!token || !list) return NULL;
  132.     if (APP_TRACE) TTYPrint(TDEST, "Check rules. for `%s'n", token);
  133.     while ((pres = (HTRule *) HTList_nextObject(list))) {
  134. char * rest = ignore_case ? HTStrCaseMatch(pres->pattern, token) :
  135.     HTStrMatch(pres->pattern, token);
  136. if (!rest) continue;   /* No match at all */
  137.     
  138. /* We found a match for this entry, now do operation */
  139. switch (pres->op) {
  140.           case HT_Pass:
  141.   case HT_Map:
  142.     if (!pres->replace) {        /* No replace */
  143. StrAllocCopy(replace, token);
  144.     } else if (*rest && pres->insert >= 0) {
  145. if ((replace = (char  *) HT_MALLOC(strlen(pres->replace)+strlen(rest))) == NULL)
  146.     HT_OUTOFMEM("HTRule_translate");
  147. strcpy(replace, pres->replace);
  148. strcpy(replace+pres->insert, rest);
  149.     } else {        /* Perfect match or no insetion point */
  150. StrAllocCopy(replace, pres->replace);
  151.     }
  152.     if (pres->op == HT_Pass) {
  153. if (APP_TRACE)
  154.     TTYPrint(TDEST, "............ map into `%s'n", replace);
  155. return replace;
  156.     }
  157.     break;
  158.     
  159.   case HT_Fail:
  160.   default:
  161.     if (APP_TRACE) TTYPrint(TDEST,"............ FAIL `%s'n", token);
  162.     return NULL;
  163. }
  164.     }
  165.     if (!replace) StrAllocCopy(replace, token);
  166.     return replace;
  167. }
  168. /* Load one line of configuration
  169. ** ------------------------------
  170. ** Call this, for example, to load a X resource with config info.
  171. ** Returns YES if line OK, else NO
  172. */
  173. PUBLIC BOOL HTRule_parseLine (HTList * list, CONST char * config)
  174. {
  175.     HTRuleOp op;
  176.     char * line = NULL;
  177.     char * ptr;
  178.     char * word1, * word2, * word3;
  179.     int status;
  180.     if ((ptr = strchr(config, '#'))) *ptr = '';
  181.     StrAllocCopy(line, config);  /* Get our own copy */
  182.     ptr = line;
  183.     if ((word1 = HTNextField(&ptr)) == NULL) {        /* Empty line */
  184. HT_FREE(line);
  185. return YES;
  186.     }
  187.     if ((word2 = HTNextField(&ptr)) == NULL) {
  188. if (APP_TRACE)
  189.     TTYPrint(TDEST,"Rule Parse.. Insufficient operands: `%s'n",line);
  190. HT_FREE(line);
  191. return NO;
  192.     }
  193.     word3 = HTNextField(&ptr);
  194.     /* Look for things we recognize */
  195.     if (!strcasecomp(word1, "addtype")) {
  196. double quality;
  197.         char * encoding = HTNextField(&ptr);
  198. status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
  199. HTBind_add(word2, /* suffix */
  200.    word3, /* type */
  201.    encoding ? encoding : "binary", /* encoding */
  202.    NULL, /* language */
  203.    status >= 1? quality : 1.0); /* quality */
  204.     } else if (!strcasecomp(word1, "addencoding")) {
  205. double quality;
  206. status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
  207. HTBind_addEncoding(word2, word3, status >= 1 ? quality : 1.0);
  208.     } else if (!strcasecomp(word1, "addlanguage")) {
  209. double quality;
  210. status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
  211. HTBind_addLanguage(word2, word3, status >= 1 ? quality : 1.0);
  212.     } else if (!strcasecomp(word1, "presentation")) {
  213. HTList * converters = HTFormat_conversion();
  214. double quality, secs, secs_per_byte;
  215.         status = ptr ? sscanf(ptr,"%lf%lf%lf",&quality,&secs,&secs_per_byte):0;
  216. HTPresentation_add(converters, word2, word3, NULL,
  217.    status >= 1 ? quality : 1.0,
  218.    status >= 2 ? secs : 0.0,
  219.    status >= 3 ? secs_per_byte : 0.0);
  220.     } else if (!strcasecomp(word1, "proxy")) {
  221. HTProxy_add(word2, word3);
  222.     } else if (!strcasecomp(word1, "noproxy")) {
  223. int port = 0;
  224.         status = ptr ? sscanf(ptr, "%d", &port) : 0;
  225. HTNoProxy_add(word2, word3, port);
  226.     } else if (!strcasecomp(word1, "gateway")) {
  227. HTGateway_add(word2, word3);
  228.     } else {
  229. op = 0==strcasecomp(word1, "map")  ? HT_Map
  230.     : 0==strcasecomp(word1, "pass") ? HT_Pass
  231.     : 0==strcasecomp(word1, "fail") ? HT_Fail
  232.     : HT_Invalid;
  233. if (op == HT_Invalid) {
  234.     if (APP_TRACE)
  235. TTYPrint(TDEST, "Rule Parse.. Bad or unknown: `%s'n", config);
  236. } else
  237.     HTRule_add(list, op, word2, word3);
  238.     }
  239.     HT_FREE(line);
  240.     return YES;
  241. }
  242. /*
  243. ** Folding is either of CF LWS, LF LWS, CRLF LWS
  244. */
  245. PRIVATE int HTRule_put_block (HTStream * me, CONST char * b, int l)
  246. {
  247.     while (l > 0) {
  248. if (me->EOLstate == EOL_FCR) {
  249.     if (*b == LF)          /* CRLF */
  250. me->EOLstate = EOL_FLF;
  251.     else if (WHITE(*b))    /* Folding: CR SP */
  252. me->EOLstate = EOL_DOT;
  253.     else {  /* New line */
  254. HTRule_parseLine(rules, HTChunk_data(me->buffer));
  255. me->EOLstate = EOL_BEGIN;
  256. HTChunk_clear(me->buffer);
  257. continue;
  258.     }
  259. } else if (me->EOLstate == EOL_FLF) {
  260.     if (WHITE(*b))        /* Folding: LF SP or CR LF SP */
  261. me->EOLstate = EOL_DOT;
  262.     else { /* New line */
  263. HTRule_parseLine(rules, HTChunk_data(me->buffer));
  264. me->EOLstate = EOL_BEGIN;
  265. HTChunk_clear(me->buffer);
  266. continue;
  267.     }
  268. } else if (me->EOLstate == EOL_DOT) {
  269.     if (WHITE(*b)) {
  270. me->EOLstate = EOL_BEGIN;
  271. HTChunk_putc(me->buffer, ' ');
  272.     } else {
  273. HTRule_parseLine(rules, HTChunk_data(me->buffer));
  274. me->EOLstate = EOL_BEGIN;
  275. HTChunk_clear(me->buffer);
  276. continue;
  277.     }
  278. } else if (*b == CR) {
  279.     me->EOLstate = EOL_FCR;
  280. } else if (*b == LF) {
  281.     me->EOLstate = EOL_FLF;        /* Line found */
  282. } else
  283.     HTChunk_putc(me->buffer, *b);
  284. l--; b++;
  285.     }
  286.     return HT_OK;
  287. }
  288. PRIVATE int HTRule_put_character (HTStream * me, char c)
  289. {
  290.     return HTRule_put_block(me, &c, 1);
  291. }
  292. PRIVATE int HTRule_put_string (HTStream * me, CONST char * s)
  293. {
  294.     return HTRule_put_block(me, s, (int) strlen(s));
  295. }
  296. PRIVATE int HTRule_flush (HTStream * me)
  297. {
  298.     return (*me->target->isa->flush)(me->target);
  299. }
  300. PRIVATE int HTRule_free (HTStream * me)
  301. {
  302.     int status = HT_OK;
  303.     if (me->target) {
  304. if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
  305.     return HT_WOULD_BLOCK;
  306.     }
  307.     if (APP_TRACE)
  308. TTYPrint(TDEST, "Rules....... FREEING....n");
  309.     HTChunk_delete(me->buffer);
  310.     HT_FREE(me);
  311.     return status;
  312. }
  313. PRIVATE int HTRule_abort (HTStream * me, HTList * e)
  314. {
  315.     int status = HT_ERROR;
  316.     if (me->target) status = (*me->target->isa->abort)(me->target, e);
  317.     if (APP_TRACE) TTYPrint(TDEST, "Rules....... ABORTING...n");
  318.     HTChunk_delete(me->buffer);
  319.     HT_FREE(me);
  320.     return status;
  321. }
  322. /* Structured Object Class
  323. ** -----------------------
  324. */
  325. PRIVATE CONST HTStreamClass HTRuleClass =
  326. {
  327.     "RuleParser",
  328.     HTRule_flush,
  329.     HTRule_free,
  330.     HTRule_abort,
  331.     HTRule_put_character,
  332.     HTRule_put_string,
  333.     HTRule_put_block
  334. };
  335. PUBLIC HTStream * HTRules (HTRequest * request,
  336.    void * param,
  337.    HTFormat input_format,
  338.    HTFormat output_format,
  339.    HTStream * output_stream)
  340. {
  341.     HTAlertCallback *cbf = HTAlert_find(HT_A_CONFIRM);
  342.     HTStream * me;
  343.     if (!cbf ||
  344. (cbf && (*cbf)(request,HT_A_CONFIRM,HT_MSG_RULES,NULL,NULL,NULL))) {
  345. if (WWWTRACE) TTYPrint(TDEST, "Rule file... Parser object createdn");
  346. if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
  347.     HT_OUTOFMEM("HTRules");
  348. me->isa = &HTRuleClass;
  349. me->request = request;
  350. me->target = output_stream;
  351. me->buffer = HTChunk_new(512);
  352. me->EOLstate = EOL_BEGIN;
  353. if (!rules) rules = HTList_new();
  354.     } else
  355. me = HTErrorStream();
  356.     return me;
  357. }