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