Annotation of libwww/Library/src/HTRules.c, revision 2.52
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.52 ! frystyk 6: ** @(#) $Id: HTRules.c,v 2.51 1996/07/02 22:55:04 frystyk Exp $
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: HTChunk * buffer;
2.49 frystyk 40: HTEOLState EOLstate;
2.40 frystyk 41: };
42:
43: struct _HTRule {
44: HTRuleOp op;
45: char * pattern;
46: char * replace;
47: int insert; /* Index into any wildcard in replace */
48: };
49:
50: PRIVATE HTList * rules = NULL;
51:
52: /* ------------------------------------------------------------------------- */
53:
54: /*
55: ** Rules are handled as list as everything else that has to do with
56: ** preferences. We provide two functions for getting and setting the
57: ** global rules
2.1 timbl 58: */
2.40 frystyk 59: PUBLIC HTList * HTRule_global (void)
60: {
61: if (!rules) rules = HTList_new();
62: return rules;
63: }
2.1 timbl 64:
2.40 frystyk 65: PUBLIC BOOL HTRule_setGlobal(HTList * list)
66: {
67: if (rules) HTRule_deleteAll(rules);
68: rules = list;
69: return YES;
70: }
2.1 timbl 71:
2.40 frystyk 72: /* Add rule to the list
2.1 timbl 73: ** --------------------
2.40 frystyk 74: ** This function adds a rule to the list of rules. The
75: ** pattern is a 0-terminated string containing a single
76: ** "*". <CODE>equiv</CODE> points to the equivalent string with * for the
77: ** place where the text matched by * goes.
2.1 timbl 78: ** On entry,
79: ** pattern points to 0-terminated string containing a single "*"
2.40 frystyk 80: ** replace points to the equivalent string with * for the
2.1 timbl 81: ** place where the text matched by * goes.
82: ** On exit,
2.40 frystyk 83: ** returns YES if OK, else NO
2.1 timbl 84: */
2.40 frystyk 85: PUBLIC BOOL HTRule_add (HTList * list, HTRuleOp op,
2.48 frystyk 86: const char * pattern, const char * replace)
2.39 frystyk 87: {
2.40 frystyk 88: if (list && pattern) {
2.45 frystyk 89: HTRule * me;
90: if ((me = (HTRule *) HT_CALLOC(1, sizeof(HTRule))) == NULL)
91: HT_OUTOFMEM("HTRule_add");
2.40 frystyk 92: me->op = op;
93: StrAllocCopy(me->pattern, pattern);
94: if (replace) {
95: char *ptr = strchr(replace, '*');
96: StrAllocCopy(me->replace, replace);
97: me->insert = ptr ? ptr-replace : -1;
98: if (APP_TRACE)
2.47 eric 99: HTTrace("Rule Add.... For `%s\' op %d `%s\'\n",
2.40 frystyk 100: pattern, op, replace);
101: } else
2.47 eric 102: HTTrace("Rule Add.... For `%s\' op %d\n", pattern, op);
2.40 frystyk 103: return HTList_appendObject(rules, (void *) me);
2.10 duns 104: }
2.40 frystyk 105: return NO;
2.1 timbl 106: }
107:
2.40 frystyk 108: /* Delete all rules
109: ** ----------------
110: ** Deletes all the rules registered by this module
2.1 timbl 111: */
2.40 frystyk 112: PUBLIC BOOL HTRule_deleteAll (HTList * list)
2.1 timbl 113: {
2.40 frystyk 114: if (list) {
115: HTList *cur = list;
116: HTRule *pres;
117: while ((pres = (HTRule *) HTList_nextObject(cur))) {
2.45 frystyk 118: HT_FREE(pres->pattern);
119: HT_FREE(pres->replace);
120: HT_FREE(pres);
2.40 frystyk 121: }
122: return HTList_delete(list);
2.1 timbl 123: }
2.40 frystyk 124: return NO;
2.1 timbl 125: }
126:
2.40 frystyk 127: /* Translate by rules
2.1 timbl 128: ** ------------------
2.15 luotonen 129: ** The most recently defined rules are applied last.
2.40 frystyk 130: ** This function walks through the list of rules and translates the
131: ** reference when matches are found. The list is traversed in order
132: ** starting from the head of the list. It returns the address of the
133: ** equivalent string allocated from the heap which the CALLER MUST
134: ** FREE.
2.1 timbl 135: */
2.48 frystyk 136: PUBLIC char * HTRule_translate (HTList * list, const char * token,
2.40 frystyk 137: BOOL ignore_case)
2.1 timbl 138: {
2.40 frystyk 139: HTRule * pres;
140: char * replace = NULL;
141: if (!token || !list) return NULL;
2.47 eric 142: if (APP_TRACE) HTTrace("Check rules. for `%s\'\n", token);
2.40 frystyk 143: while ((pres = (HTRule *) HTList_nextObject(list))) {
144: char * rest = ignore_case ? HTStrCaseMatch(pres->pattern, token) :
145: HTStrMatch(pres->pattern, token);
146: if (!rest) continue; /* No match at all */
147:
148: /* We found a match for this entry, now do operation */
149: switch (pres->op) {
150:
151: case HT_Pass:
152: case HT_Map:
153: if (!pres->replace) { /* No replace */
154: StrAllocCopy(replace, token);
155:
156: } else if (*rest && pres->insert >= 0) {
2.45 frystyk 157: if ((replace = (char *) HT_MALLOC(strlen(pres->replace)+strlen(rest))) == NULL)
158: HT_OUTOFMEM("HTRule_translate");
2.40 frystyk 159: strcpy(replace, pres->replace);
160: strcpy(replace+pres->insert, rest);
161:
162: } else { /* Perfect match or no insetion point */
163: StrAllocCopy(replace, pres->replace);
164: }
165:
166: if (pres->op == HT_Pass) {
167: if (APP_TRACE)
2.47 eric 168: HTTrace("............ map into `%s'\n", replace);
2.40 frystyk 169: return replace;
170: }
171: break;
172:
173: case HT_Fail:
174:
175: default:
2.47 eric 176: if (APP_TRACE) HTTrace("............ FAIL `%s'\n", token);
2.40 frystyk 177: return NULL;
2.1 timbl 178: }
2.40 frystyk 179: }
180: if (!replace) StrAllocCopy(replace, token);
181: return replace;
2.15 luotonen 182: }
183:
2.7 timbl 184: /* Load one line of configuration
185: ** ------------------------------
186: ** Call this, for example, to load a X resource with config info.
2.40 frystyk 187: ** Returns YES if line OK, else NO
2.7 timbl 188: */
2.48 frystyk 189: PUBLIC BOOL HTRule_parseLine (HTList * list, const char * config)
2.7 timbl 190: {
191: HTRuleOp op;
192: char * line = NULL;
2.40 frystyk 193: char * ptr;
194: char * word1, * word2, * word3;
2.7 timbl 195: int status;
2.40 frystyk 196: if ((ptr = strchr(config, '#'))) *ptr = '\0';
197: StrAllocCopy(line, config); /* Get our own copy */
198: ptr = line;
2.52 ! frystyk 199: if (APP_TRACE) HTTrace("Rule Parse.. `%s\'\n", config ? config : "<null>");
2.40 frystyk 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: {
2.51 frystyk 328: if (me) {
329: char * flush = HTChunk_data(me->buffer);
330: if (flush) HTRule_parseLine(rules, flush);
331: HTChunk_clear(me->buffer);
332: }
333: return HT_OK;
2.40 frystyk 334: }
335:
336: PRIVATE int HTRule_free (HTStream * me)
337: {
2.51 frystyk 338: if (me) {
339: int status = HTRule_flush(me);
340: if (APP_TRACE) HTTrace("Rules....... FREEING....\n");
341: HTChunk_delete(me->buffer);
342: HT_FREE(me);
343: return status;
2.40 frystyk 344: }
2.51 frystyk 345: return HT_ERROR;
2.40 frystyk 346: }
347:
348: PRIVATE int HTRule_abort (HTStream * me, HTList * e)
349: {
2.51 frystyk 350: if (me) {
351: int status = HT_ERROR;
352: if (APP_TRACE) HTTrace("Rules....... ABORTING...\n");
353: HTChunk_delete(me->buffer);
354: HT_FREE(me);
355: return status;
356: }
357: return HT_ERROR;
2.40 frystyk 358: }
359:
360: /* Structured Object Class
361: ** -----------------------
2.1 timbl 362: */
2.48 frystyk 363: PRIVATE const HTStreamClass HTRuleClass =
2.40 frystyk 364: {
365: "RuleParser",
366: HTRule_flush,
367: HTRule_free,
368: HTRule_abort,
369: HTRule_put_character,
370: HTRule_put_string,
371: HTRule_put_block
372: };
373:
374: PUBLIC HTStream * HTRules (HTRequest * request,
375: void * param,
376: HTFormat input_format,
377: HTFormat output_format,
378: HTStream * output_stream)
2.1 timbl 379: {
2.41 frystyk 380: HTAlertCallback *cbf = HTAlert_find(HT_A_CONFIRM);
2.40 frystyk 381: HTStream * me;
2.46 frystyk 382: if (!cbf ||
383: (cbf && (*cbf)(request,HT_A_CONFIRM,HT_MSG_RULES,NULL,NULL,NULL))) {
2.47 eric 384: if (WWWTRACE) HTTrace("Rule file... Parser object created\n");
2.45 frystyk 385: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
386: HT_OUTOFMEM("HTRules");
2.40 frystyk 387: me->isa = &HTRuleClass;
388: me->request = request;
2.42 frystyk 389: me->buffer = HTChunk_new(512);
2.40 frystyk 390: me->EOLstate = EOL_BEGIN;
391: if (!rules) rules = HTList_new();
392: } else
2.43 frystyk 393: me = HTErrorStream();
2.40 frystyk 394: return me;
2.1 timbl 395: }
2.11 luotonen 396:
Webmaster