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