Annotation of libwww/Library/src/HTRules.c, revision 2.45

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.1       timbl       6: **
2.40      frystyk     7: **     This module manages rule files in the Library
2.1       timbl       8: **
                      9: ** History:
                     10: **      3 Jun 91       Written TBL
                     11: **     10 Aug 91       Authorisation added after Daniel Martin (pass, fail)
                     12: **                     Rule order in file changed
                     13: **                     Comments allowed with # on 1st char of rule line
                     14: **      17 Jun 92       Bug fix: pass and fail failed if didn't contain '*' TBL
2.9       secret     15: **       1 Sep 93       Bug fix: no memory check - Nathan Torkington
                     16: **                      BYTE_ADDRESSING removed - Arthur Secret
2.12      luotonen   17: **     11 Sep 93  MD   Changed %i into %d in debug printf. 
2.10      duns       18: **                     VMS does not recognize %i.
                     19: **                     Bug Fix: in case of PASS, only one parameter to printf.
2.12      luotonen   20: **     19 Sep 93  AL   Added Access Authorization stuff.
                     21: **      1 Nov 93  AL   Added htbin.
2.15      luotonen   22: **     30 Nov 93  AL   Added HTTranslateReq().
2.19      luotonen   23: **      4 Feb 94  AL   Took away all the daemon-specific stuff.
2.36      frystyk    24: **      28 Sep 94  HWL  Added field to HTPresentation_add call
2.40      frystyk    25: **     15 Nov 95  HFN  Made a stream, fixed interface and made new translater
2.11      luotonen   26: **
2.40      frystyk    27: ** BUGS: We only have one wildcard match pr rule!
2.1       timbl      28: */
                     29: 
2.28      frystyk    30: /* Library include files */
2.34      frystyk    31: #include "WWWLib.h"
2.40      frystyk    32: #include "HTProxy.h"
2.26      frystyk    33: #include "HTRules.h"                                    /* Implemented here */
2.1       timbl      34: 
2.40      frystyk    35: struct _HTStream {
                     36:     CONST HTStreamClass *      isa;
                     37:     HTRequest *                        request;
                     38:     HTStream *                 target;
                     39:     HTChunk *                  buffer;
                     40:     HTSocketEOL                        EOLstate;
                     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,
                     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)
                     99:                TTYPrint(TDEST, "Rule Add.... For `%s\' op %d `%s\'\n",
                    100:                         pattern, op, replace);
                    101:        } else
                    102:            TTYPrint(TDEST, "Rule Add.... For `%s\' op %d\n", pattern, op);
                    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.40      frystyk   136: PUBLIC char * HTRule_translate (HTList * list, CONST char * token,
                    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.44      frystyk   142:     if (APP_TRACE) TTYPrint(TDEST, "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.44      frystyk   168:                    TTYPrint(TDEST, "............ map into `%s'\n", replace);
2.40      frystyk   169:                return replace;
                    170:            }
                    171:            break;
                    172:            
                    173:          case HT_Fail:
                    174: 
                    175:          default:
                    176:            if (APP_TRACE) TTYPrint(TDEST,"............ FAIL `%s'\n", token);
                    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.40      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;
                    199:     if ((word1 = HTNextField(&ptr)) == NULL) {                /* Empty line */
2.45    ! frystyk   200:        HT_FREE(line);
2.40      frystyk   201:        return YES;
2.7       timbl     202:     }
2.40      frystyk   203:     if ((word2 = HTNextField(&ptr)) == NULL) {
                    204:        if (APP_TRACE)
                    205:            TTYPrint(TDEST,"Rule Parse.. Insufficient operands: `%s\'\n",line);
2.45    ! frystyk   206:        HT_FREE(line);
2.40      frystyk   207:        return NO;
2.7       timbl     208:     }
2.40      frystyk   209:     word3 = HTNextField(&ptr);
2.7       timbl     210: 
2.40      frystyk   211:     /* Look for things we recognize */
                    212:     if (!strcasecomp(word1, "addtype")) {
                    213:        double quality;
                    214:         char * encoding = HTNextField(&ptr);
                    215:        status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
2.37      frystyk   216:        HTBind_add(word2,                               /* suffix */
                    217:                   word3,                               /* type */
                    218:                   encoding ? encoding : "binary",      /* encoding */
2.40      frystyk   219:                   NULL,                                /* language */
2.37      frystyk   220:                   status >= 1? quality : 1.0);         /* quality */
2.7       timbl     221: 
2.40      frystyk   222:     } else if (!strcasecomp(word1, "addencoding")) {
                    223:        double quality;
                    224:        status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
                    225:        HTBind_addEncoding(word2, word3, status >= 1 ? quality : 1.0);
                    226: 
                    227:     } else if (!strcasecomp(word1, "addlanguage")) {
                    228:        double quality;
                    229:        status = ptr ? sscanf(ptr, "%lf", &quality) : 0;
                    230:        HTBind_addLanguage(word2, word3, status >= 1 ? quality : 1.0);
                    231: 
                    232:     } else if (!strcasecomp(word1, "presentation")) {
                    233:        HTList * converters = HTFormat_conversion();
                    234:        double quality, secs, secs_per_byte;
                    235:         status = ptr ? sscanf(ptr,"%lf%lf%lf",&quality,&secs,&secs_per_byte):0;
2.36      frystyk   236:        HTPresentation_add(converters, word2, word3, NULL,
2.40      frystyk   237:                           status >= 1 ? quality : 1.0,
                    238:                           status >= 2 ? secs : 0.0,
                    239:                           status >= 3 ? secs_per_byte : 0.0);
                    240: 
                    241:     } else if (!strcasecomp(word1, "proxy")) {
                    242:        HTProxy_add(word2, word3);
                    243:        
                    244:     } else if (!strcasecomp(word1, "noproxy")) {
                    245:        int port = 0;
                    246:         status = ptr ? sscanf(ptr, "%d", &port) : 0;
                    247:        HTNoProxy_add(word2, word3, port);
                    248: 
                    249:     } else if (!strcasecomp(word1, "gateway")) {
                    250:        HTGateway_add(word2, word3);
2.12      luotonen  251: 
2.7       timbl     252:     } else {
                    253:        op =    0==strcasecomp(word1, "map")  ? HT_Map
                    254:            :   0==strcasecomp(word1, "pass") ? HT_Pass
                    255:            :   0==strcasecomp(word1, "fail") ? HT_Fail
2.19      luotonen  256:            :                                   HT_Invalid;
2.40      frystyk   257:        if (op == HT_Invalid) {
                    258:            if (APP_TRACE)
                    259:                TTYPrint(TDEST, "Rule Parse.. Bad or unknown: `%s'\n", config);
                    260:        } else
                    261:            HTRule_add(list, op, word2, word3);
2.7       timbl     262:     }
2.45    ! frystyk   263:     HT_FREE(line);
2.40      frystyk   264:     return YES;
2.7       timbl     265: }
                    266: 
2.40      frystyk   267: /*
                    268: **     Folding is either of CF LWS, LF LWS, CRLF LWS
                    269: */
                    270: PRIVATE int HTRule_put_block (HTStream * me, CONST char * b, int l)
                    271: {
                    272:     while (l > 0) {
                    273:        if (me->EOLstate == EOL_FCR) {
                    274:            if (*b == LF)                                            /* CRLF */
                    275:                me->EOLstate = EOL_FLF;
                    276:            else if (WHITE(*b))                            /* Folding: CR SP */
                    277:                me->EOLstate = EOL_DOT;
                    278:            else {                                               /* New line */
2.42      frystyk   279:                HTRule_parseLine(rules, HTChunk_data(me->buffer));
2.40      frystyk   280:                me->EOLstate = EOL_BEGIN;
2.42      frystyk   281:                HTChunk_clear(me->buffer);
2.40      frystyk   282:                continue;
                    283:            }
                    284:        } else if (me->EOLstate == EOL_FLF) {
                    285:            if (WHITE(*b))                     /* Folding: LF SP or CR LF 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_DOT) {
                    294:            if (WHITE(*b)) {
                    295:                me->EOLstate = EOL_BEGIN;
2.42      frystyk   296:                HTChunk_putc(me->buffer, ' ');
2.40      frystyk   297:            } else {
2.42      frystyk   298:                HTRule_parseLine(rules, HTChunk_data(me->buffer));
2.40      frystyk   299:                me->EOLstate = EOL_BEGIN;
2.42      frystyk   300:                HTChunk_clear(me->buffer);
2.40      frystyk   301:                continue;
                    302:            }
                    303:        } else if (*b == CR) {
                    304:            me->EOLstate = EOL_FCR;
                    305:        } else if (*b == LF) {
                    306:            me->EOLstate = EOL_FLF;                            /* Line found */
                    307:        } else
2.42      frystyk   308:            HTChunk_putc(me->buffer, *b);
2.40      frystyk   309:        l--; b++;
                    310:     }
                    311:     return HT_OK;
                    312: }
2.1       timbl     313: 
2.40      frystyk   314: PRIVATE int HTRule_put_character (HTStream * me, char c)
                    315: {
                    316:     return HTRule_put_block(me, &c, 1);
                    317: }
                    318: 
                    319: PRIVATE int HTRule_put_string (HTStream * me, CONST char * s)
                    320: {
                    321:     return HTRule_put_block(me, s, (int) strlen(s));
                    322: }
                    323: 
                    324: PRIVATE int HTRule_flush (HTStream * me)
                    325: {
                    326:     return (*me->target->isa->flush)(me->target);
                    327: }
                    328: 
                    329: PRIVATE int HTRule_free (HTStream * me)
                    330: {
                    331:     int status = HT_OK;
                    332:     if (me->target) {
                    333:        if ((status = (*me->target->isa->_free)(me->target)) == HT_WOULD_BLOCK)
                    334:            return HT_WOULD_BLOCK;
                    335:     }
                    336:     if (APP_TRACE)
                    337:        TTYPrint(TDEST, "Rules....... FREEING....\n");
2.42      frystyk   338:     HTChunk_delete(me->buffer);
2.45    ! frystyk   339:     HT_FREE(me);
2.40      frystyk   340:     return status;
                    341: }
                    342: 
                    343: PRIVATE int HTRule_abort (HTStream * me, HTList * e)
                    344: {
                    345:     int status = HT_ERROR;
                    346:     if (me->target) status = (*me->target->isa->abort)(me->target, e);
                    347:     if (APP_TRACE) TTYPrint(TDEST, "Rules....... ABORTING...\n");
2.42      frystyk   348:     HTChunk_delete(me->buffer);
2.45    ! frystyk   349:     HT_FREE(me);
2.40      frystyk   350:     return status;
                    351: }
                    352: 
                    353: /*     Structured Object Class
                    354: **     -----------------------
2.1       timbl     355: */
2.40      frystyk   356: PRIVATE CONST HTStreamClass HTRuleClass =
                    357: {              
                    358:     "RuleParser",
                    359:     HTRule_flush,
                    360:     HTRule_free,
                    361:     HTRule_abort,
                    362:     HTRule_put_character,
                    363:     HTRule_put_string,
                    364:     HTRule_put_block
                    365: };
                    366: 
                    367: PUBLIC HTStream * HTRules (HTRequest * request,
                    368:                           void *       param,
                    369:                           HTFormat     input_format,
                    370:                           HTFormat     output_format,
                    371:                           HTStream *   output_stream)
2.1       timbl     372: {
2.41      frystyk   373:     HTAlertCallback *cbf = HTAlert_find(HT_A_CONFIRM);
2.40      frystyk   374:     HTStream * me;
2.43      frystyk   375:     if (cbf && (*cbf)(request,HT_A_CONFIRM,HT_MSG_RULES,NULL,NULL,NULL)) {
2.42      frystyk   376:        if (WWWTRACE) TTYPrint(TDEST, "Rule file... Parser object created\n");
2.45    ! frystyk   377:        if ((me = (HTStream *) HT_CALLOC(1, sizeof(HTStream))) == NULL)
        !           378:            HT_OUTOFMEM("HTRules");
2.40      frystyk   379:        me->isa = &HTRuleClass;
                    380:        me->request = request;
                    381:        me->target = output_stream;
2.42      frystyk   382:        me->buffer = HTChunk_new(512);
2.40      frystyk   383:        me->EOLstate = EOL_BEGIN;
                    384:        if (!rules) rules = HTList_new();
                    385:     } else
2.43      frystyk   386:        me = HTErrorStream();
2.40      frystyk   387:     return me;
2.1       timbl     388: }
2.11      luotonen  389: 

Webmaster