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