Annotation of libwww/Library/src/HTRules.c, revision 2.60
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.60 ! frystyk 6: ** @(#) $Id: HTRules.c,v 2.59 1999/02/07 18:24:19 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.59 frystyk 33: #include "WWWFile.h"
2.40 frystyk 34: #include "HTProxy.h"
2.26 frystyk 35: #include "HTRules.h" /* Implemented here */
2.1 timbl 36:
2.40 frystyk 37: struct _HTStream {
2.48 frystyk 38: const HTStreamClass * isa;
2.40 frystyk 39: HTRequest * request;
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;
2.53 frystyk 71: }
72:
73: PUBLIC BOOL HTRule_addGlobal(HTRuleOp op,
74: const char * pattern, const char * replace)
75: {
76: if (!rules) rules = HTList_new();
77: return HTRule_add(rules, op, pattern, replace);
2.40 frystyk 78: }
2.1 timbl 79:
2.40 frystyk 80: /* Add rule to the list
2.1 timbl 81: ** --------------------
2.40 frystyk 82: ** This function adds a rule to the list of rules. The
83: ** pattern is a 0-terminated string containing a single
84: ** "*". <CODE>equiv</CODE> points to the equivalent string with * for the
85: ** place where the text matched by * goes.
2.1 timbl 86: ** On entry,
87: ** pattern points to 0-terminated string containing a single "*"
2.40 frystyk 88: ** replace points to the equivalent string with * for the
2.1 timbl 89: ** place where the text matched by * goes.
90: ** On exit,
2.40 frystyk 91: ** returns YES if OK, else NO
2.1 timbl 92: */
2.40 frystyk 93: PUBLIC BOOL HTRule_add (HTList * list, HTRuleOp op,
2.48 frystyk 94: const char * pattern, const char * replace)
2.39 frystyk 95: {
2.40 frystyk 96: if (list && pattern) {
2.45 frystyk 97: HTRule * me;
98: if ((me = (HTRule *) HT_CALLOC(1, sizeof(HTRule))) == NULL)
99: HT_OUTOFMEM("HTRule_add");
2.40 frystyk 100: me->op = op;
101: StrAllocCopy(me->pattern, pattern);
102: if (replace) {
103: char *ptr = strchr(replace, '*');
104: StrAllocCopy(me->replace, replace);
105: me->insert = ptr ? ptr-replace : -1;
106: if (APP_TRACE)
2.47 eric 107: HTTrace("Rule Add.... For `%s\' op %d `%s\'\n",
2.40 frystyk 108: pattern, op, replace);
2.57 frystyk 109: } else {
110: if (APP_TRACE) HTTrace("Rule Add.... For `%s\' op %d\n", pattern, op);
111: }
2.54 frystyk 112: return HTList_appendObject(list, (void *) me);
2.10 duns 113: }
2.40 frystyk 114: return NO;
2.1 timbl 115: }
116:
2.40 frystyk 117: /* Delete all rules
118: ** ----------------
119: ** Deletes all the rules registered by this module
2.1 timbl 120: */
2.40 frystyk 121: PUBLIC BOOL HTRule_deleteAll (HTList * list)
2.1 timbl 122: {
2.40 frystyk 123: if (list) {
124: HTList *cur = list;
125: HTRule *pres;
126: while ((pres = (HTRule *) HTList_nextObject(cur))) {
2.45 frystyk 127: HT_FREE(pres->pattern);
128: HT_FREE(pres->replace);
129: HT_FREE(pres);
2.40 frystyk 130: }
131: return HTList_delete(list);
2.1 timbl 132: }
2.40 frystyk 133: return NO;
2.1 timbl 134: }
135:
2.40 frystyk 136: /* Translate by rules
2.1 timbl 137: ** ------------------
2.15 luotonen 138: ** The most recently defined rules are applied last.
2.40 frystyk 139: ** This function walks through the list of rules and translates the
140: ** reference when matches are found. The list is traversed in order
141: ** starting from the head of the list. It returns the address of the
142: ** equivalent string allocated from the heap which the CALLER MUST
143: ** FREE.
2.1 timbl 144: */
2.48 frystyk 145: PUBLIC char * HTRule_translate (HTList * list, const char * token,
2.40 frystyk 146: BOOL ignore_case)
2.1 timbl 147: {
2.40 frystyk 148: HTRule * pres;
149: char * replace = NULL;
150: if (!token || !list) return NULL;
2.47 eric 151: if (APP_TRACE) HTTrace("Check rules. for `%s\'\n", token);
2.40 frystyk 152: while ((pres = (HTRule *) HTList_nextObject(list))) {
153: char * rest = ignore_case ? HTStrCaseMatch(pres->pattern, token) :
154: HTStrMatch(pres->pattern, token);
155: if (!rest) continue; /* No match at all */
156:
157: /* We found a match for this entry, now do operation */
158: switch (pres->op) {
159:
160: case HT_Pass:
161: case HT_Map:
162: if (!pres->replace) { /* No replace */
163: StrAllocCopy(replace, token);
164:
165: } else if (*rest && pres->insert >= 0) {
2.45 frystyk 166: if ((replace = (char *) HT_MALLOC(strlen(pres->replace)+strlen(rest))) == NULL)
167: HT_OUTOFMEM("HTRule_translate");
2.40 frystyk 168: strcpy(replace, pres->replace);
169: strcpy(replace+pres->insert, rest);
170:
171: } else { /* Perfect match or no insetion point */
172: StrAllocCopy(replace, pres->replace);
173: }
174:
175: if (pres->op == HT_Pass) {
176: if (APP_TRACE)
2.47 eric 177: HTTrace("............ map into `%s'\n", replace);
2.40 frystyk 178: return replace;
179: }
180: break;
181:
182: case HT_Fail:
183:
184: default:
2.47 eric 185: if (APP_TRACE) HTTrace("............ FAIL `%s'\n", token);
2.40 frystyk 186: return NULL;
2.1 timbl 187: }
2.40 frystyk 188: }
189: if (!replace) StrAllocCopy(replace, token);
190: return replace;
2.15 luotonen 191: }
192:
2.7 timbl 193: /* Load one line of configuration
194: ** ------------------------------
195: ** Call this, for example, to load a X resource with config info.
2.40 frystyk 196: ** Returns YES if line OK, else NO
2.7 timbl 197: */
2.48 frystyk 198: PUBLIC BOOL HTRule_parseLine (HTList * list, const char * config)
2.7 timbl 199: {
200: HTRuleOp op;
201: char * line = NULL;
2.40 frystyk 202: char * ptr;
203: char * word1, * word2, * word3;
2.7 timbl 204: int status;
2.57 frystyk 205: if (!config) return NO;
2.40 frystyk 206: if ((ptr = strchr(config, '#'))) *ptr = '\0';
207: StrAllocCopy(line, config); /* Get our own copy */
208: ptr = line;
2.52 frystyk 209: if (APP_TRACE) HTTrace("Rule Parse.. `%s\'\n", config ? config : "<null>");
2.40 frystyk 210: if ((word1 = HTNextField(&ptr)) == NULL) { /* Empty line */
2.45 frystyk 211: HT_FREE(line);
2.40 frystyk 212: return YES;
2.7 timbl 213: }
2.40 frystyk 214: if ((word2 = HTNextField(&ptr)) == NULL) {
215: if (APP_TRACE)
2.47 eric 216: HTTrace("Rule Parse.. Insufficient operands: `%s\'\n",line);
2.45 frystyk 217: HT_FREE(line);
2.40 frystyk 218: return NO;
2.7 timbl 219: }
2.40 frystyk 220: word3 = HTNextField(&ptr);
2.7 timbl 221:
2.40 frystyk 222: /* Look for things we recognize */
223: if (!strcasecomp(word1, "addtype")) {
224: double quality;
225: char * encoding = HTNextField(&ptr);
226: status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
2.37 frystyk 227: HTBind_add(word2, /* suffix */
228: word3, /* type */
229: encoding ? encoding : "binary", /* encoding */
2.50 frystyk 230: NULL, /* cte */
2.40 frystyk 231: NULL, /* language */
2.37 frystyk 232: status >= 1? quality : 1.0); /* quality */
2.7 timbl 233:
2.40 frystyk 234: } else if (!strcasecomp(word1, "addencoding")) {
235: double quality;
236: status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
237: HTBind_addEncoding(word2, word3, status >= 1 ? quality : 1.0);
238:
239: } else if (!strcasecomp(word1, "addlanguage")) {
240: double quality;
241: status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
242: HTBind_addLanguage(word2, word3, status >= 1 ? quality : 1.0);
243:
244: } else if (!strcasecomp(word1, "presentation")) {
245: HTList * converters = HTFormat_conversion();
246: double quality, secs, secs_per_byte;
247: status = ptr ? sscanf(ptr,"%lf%lf%lf",&quality,&secs,&secs_per_byte):0;
2.36 frystyk 248: HTPresentation_add(converters, word2, word3, NULL,
2.40 frystyk 249: status >= 1 ? quality : 1.0,
250: status >= 2 ? secs : 0.0,
251: status >= 3 ? secs_per_byte : 0.0);
252:
253: } else if (!strcasecomp(word1, "proxy")) {
254: HTProxy_add(word2, word3);
255:
256: } else if (!strcasecomp(word1, "noproxy")) {
257: int port = 0;
258: status = ptr ? sscanf(ptr, "%d", &port) : 0;
259: HTNoProxy_add(word2, word3, port);
260:
261: } else if (!strcasecomp(word1, "gateway")) {
262: HTGateway_add(word2, word3);
2.12 luotonen 263:
2.7 timbl 264: } else {
265: op = 0==strcasecomp(word1, "map") ? HT_Map
266: : 0==strcasecomp(word1, "pass") ? HT_Pass
267: : 0==strcasecomp(word1, "fail") ? HT_Fail
2.19 luotonen 268: : HT_Invalid;
2.40 frystyk 269: if (op == HT_Invalid) {
270: if (APP_TRACE)
2.47 eric 271: HTTrace("Rule Parse.. Bad or unknown: `%s'\n", config);
2.40 frystyk 272: } else
273: HTRule_add(list, op, word2, word3);
2.7 timbl 274: }
2.45 frystyk 275: HT_FREE(line);
2.40 frystyk 276: return YES;
2.7 timbl 277: }
278:
2.40 frystyk 279: /*
280: ** Folding is either of CF LWS, LF LWS, CRLF LWS
281: */
2.48 frystyk 282: PRIVATE int HTRule_put_block (HTStream * me, const char * b, int l)
2.40 frystyk 283: {
284: while (l > 0) {
285: if (me->EOLstate == EOL_FCR) {
286: if (*b == LF) /* CRLF */
287: me->EOLstate = EOL_FLF;
2.56 frystyk 288: else if (isspace((int) *b)) /* Folding: CR SP */
2.40 frystyk 289: me->EOLstate = EOL_DOT;
290: else { /* New line */
2.42 frystyk 291: HTRule_parseLine(rules, HTChunk_data(me->buffer));
2.40 frystyk 292: me->EOLstate = EOL_BEGIN;
2.42 frystyk 293: HTChunk_clear(me->buffer);
2.40 frystyk 294: continue;
295: }
296: } else if (me->EOLstate == EOL_FLF) {
2.56 frystyk 297: if (isspace((int) *b)) /* Folding: LF SP or CR LF SP */
2.40 frystyk 298: me->EOLstate = EOL_DOT;
299: else { /* New line */
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 (me->EOLstate == EOL_DOT) {
2.56 frystyk 306: if (isspace((int) *b)) {
2.40 frystyk 307: me->EOLstate = EOL_BEGIN;
2.42 frystyk 308: HTChunk_putc(me->buffer, ' ');
2.40 frystyk 309: } else {
2.42 frystyk 310: HTRule_parseLine(rules, HTChunk_data(me->buffer));
2.40 frystyk 311: me->EOLstate = EOL_BEGIN;
2.42 frystyk 312: HTChunk_clear(me->buffer);
2.40 frystyk 313: continue;
314: }
315: } else if (*b == CR) {
316: me->EOLstate = EOL_FCR;
317: } else if (*b == LF) {
318: me->EOLstate = EOL_FLF; /* Line found */
319: } else
2.42 frystyk 320: HTChunk_putc(me->buffer, *b);
2.40 frystyk 321: l--; b++;
322: }
323: return HT_OK;
324: }
2.1 timbl 325:
2.40 frystyk 326: PRIVATE int HTRule_put_character (HTStream * me, char c)
327: {
328: return HTRule_put_block(me, &c, 1);
329: }
330:
2.48 frystyk 331: PRIVATE int HTRule_put_string (HTStream * me, const char * s)
2.40 frystyk 332: {
333: return HTRule_put_block(me, s, (int) strlen(s));
334: }
335:
336: PRIVATE int HTRule_flush (HTStream * me)
337: {
2.51 frystyk 338: if (me) {
339: char * flush = HTChunk_data(me->buffer);
340: if (flush) HTRule_parseLine(rules, flush);
341: HTChunk_clear(me->buffer);
342: }
343: return HT_OK;
2.40 frystyk 344: }
345:
346: PRIVATE int HTRule_free (HTStream * me)
347: {
2.51 frystyk 348: if (me) {
349: int status = HTRule_flush(me);
350: if (APP_TRACE) HTTrace("Rules....... FREEING....\n");
351: HTChunk_delete(me->buffer);
352: HT_FREE(me);
353: return status;
2.40 frystyk 354: }
2.51 frystyk 355: return HT_ERROR;
2.40 frystyk 356: }
357:
358: PRIVATE int HTRule_abort (HTStream * me, HTList * e)
359: {
2.51 frystyk 360: if (me) {
361: int status = HT_ERROR;
362: if (APP_TRACE) HTTrace("Rules....... ABORTING...\n");
363: HTChunk_delete(me->buffer);
364: HT_FREE(me);
365: return status;
366: }
367: return HT_ERROR;
2.40 frystyk 368: }
369:
370: /* Structured Object Class
371: ** -----------------------
2.1 timbl 372: */
2.48 frystyk 373: PRIVATE const HTStreamClass HTRuleClass =
2.40 frystyk 374: {
375: "RuleParser",
376: HTRule_flush,
377: HTRule_free,
378: HTRule_abort,
379: HTRule_put_character,
380: HTRule_put_string,
381: HTRule_put_block
382: };
383:
384: PUBLIC HTStream * HTRules (HTRequest * request,
385: void * param,
386: HTFormat input_format,
387: HTFormat output_format,
388: HTStream * output_stream)
2.1 timbl 389: {
2.41 frystyk 390: HTAlertCallback *cbf = HTAlert_find(HT_A_CONFIRM);
2.55 frystyk 391:
392: /*
393: ** If the library has been compiled so that we automatically accept
394: ** rule files then it's OK not to ask the user.
395: */
396: #ifdef HT_AUTOMATIC_RULES
397: if (!cbf || (cbf && (*cbf)(request,HT_A_CONFIRM, HT_MSG_RULES, NULL,NULL,NULL))) {
398: #else
399: if ((cbf && (*cbf)(request,HT_A_CONFIRM, HT_MSG_RULES, NULL,NULL,NULL))) {
400: #endif
401: HTStream * me;
2.60 ! frystyk 402: if (APP_TRACE) HTTrace("Rule file... Parser object created\n");
2.45 frystyk 403: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
404: HT_OUTOFMEM("HTRules");
2.40 frystyk 405: me->isa = &HTRuleClass;
406: me->request = request;
2.42 frystyk 407: me->buffer = HTChunk_new(512);
2.40 frystyk 408: me->EOLstate = EOL_BEGIN;
409: if (!rules) rules = HTList_new();
2.55 frystyk 410: return me;
411: } else {
412: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_RULES,
413: NULL, 0, "HTRules");
414: return HTErrorStream();
415: }
2.1 timbl 416: }
2.11 luotonen 417:
2.58 frystyk 418: /*
419: ** Parse a rule file - don't ask don't tell - be carefull with this one!
420: */
421: PUBLIC HTStream * HTRules_parseAutomatically (HTRequest * request,
422: void * param,
423: HTFormat input_format,
424: HTFormat output_format,
425: HTStream * output_stream)
426: {
427: if (request) {
428: HTStream * me;
2.60 ! frystyk 429: if (APP_TRACE) HTTrace("Rule file... Automatic parser object created\n");
2.58 frystyk 430: if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
431: HT_OUTOFMEM("HTRules");
432: me->isa = &HTRuleClass;
433: me->request = request;
434: me->buffer = HTChunk_new(512);
435: me->EOLstate = EOL_BEGIN;
436: if (!rules) rules = HTList_new();
437: return me;
438: } else {
439: HTRequest_addError(request, ERR_FATAL, NO, HTERR_NO_AUTO_RULES,
440: NULL, 0, "HTRules");
441: return HTErrorStream();
442: }
443: }
Webmaster