Annotation of libwww/Library/src/HTFormat.c, revision 1.90

1.55      frystyk     1: /*                                                                  HTFormat.c
                      2: **     MANAGE DIFFERENT FILE FORMATS
                      3: **
1.62      frystyk     4: **     (c) COPYRIGHT MIT 1995.
1.55      frystyk     5: **     Please first read the full copyright statement in the file COPYRIGH.
1.90    ! frystyk     6: **     @(#) $Id: HTFormat.c,v 1.89 1996/06/03 19:25:11 eric Exp $
1.1       timbl       7: **
                      8: ** Bugs:
                      9: **     Assumes the incoming stream is ASCII, rather than a local file
                     10: **     format, and so ALWAYS converts from ASCII on non-ASCII machines.
                     11: **     Therefore, non-ASCII machines can't read local files.
1.2       timbl      12: **
1.45      duns       13: ** HISTORY:
1.86      frystyk    14: **     8 Jul 94  FM    Insulate free from _free structure element.
1.52      frystyk    15: **     8 Nov 94  HFN   Changed a lot to make reentrant
1.2       timbl      16: */
                     17: 
1.58      frystyk    18: /* Library Include files */
1.85      frystyk    19: #include "sysdep.h"
1.87      frystyk    20: #include "WWWUtil.h"
                     21: #include "HTStream.h"
1.58      frystyk    22: #include "HTFWrite.h"
1.52      frystyk    23: #include "HTError.h"
                     24: #include "HTFormat.h"                                   /* Implemented here */
1.2       timbl      25: 
1.72      frystyk    26: #define NO_VALUE_FOUND -1e30            /* Stream Stack Value if none found */
1.63      frystyk    27: 
1.87      frystyk    28: PRIVATE HTList * HTConversions = NULL;                     /* Content types */
1.88      frystyk    29: PRIVATE HTList * HTContentCoders = NULL;                  /* Content coders */
                     30: PRIVATE HTList * HTTransferCoders = NULL;        /* Content transfer coders */
1.72      frystyk    31: PRIVATE HTList * HTCharsets = NULL;
                     32: PRIVATE HTList * HTLanguages = NULL;
1.63      frystyk    33: 
                     34: PRIVATE double HTMaxSecs = 1e10;               /* No effective limit */
1.17      luotonen   35: 
1.60      frystyk    36: struct _HTStream {
1.85      frystyk    37:     const HTStreamClass *      isa;
1.60      frystyk    38: };
                     39: 
1.88      frystyk    40: struct _HTCoding {
                     41:     HTEncoding         encoding;
                     42:     HTCoder *          encoder;
                     43:     HTCoder *          decoder;
1.87      frystyk    44:     double             quality;
                     45: };
                     46: 
1.52      frystyk    47: /* ------------------------------------------------------------------------- */
1.87      frystyk    48: /*                             BASIC CONVERTERS                             */
                     49: /* ------------------------------------------------------------------------- */
                     50: 
                     51: PUBLIC HTStream * HTBlackHoleConverter (HTRequest *    request,
                     52:                                        void *          param,
                     53:                                        HTFormat        input_format,
                     54:                                        HTFormat        output_format,
                     55:                                        HTStream *      output_stream)
                     56: {
                     57:     return HTBlackHole();
                     58: }
                     59: /*     HTThroughLine
                     60: **     -------------
                     61: **
                     62: ** This function is a dummy function that returns the same output stream
                     63: ** as given as a parameter. Henrik 01/03-94
                     64: */
                     65: PUBLIC HTStream* HTThroughLine (HTRequest *    request,
                     66:                                void *          param,
                     67:                                HTFormat        input_format,
                     68:                                HTFormat        output_format,
                     69:                                HTStream *      output_stream)
                     70: {
                     71:     return output_stream;
                     72: }
                     73: 
1.52      frystyk    74: /*
1.63      frystyk    75: **     For all `accept lists' there is a local list and a global list. The
                     76: **     local list is a part of the request structure and the global list is
                     77: **     internal to the HTFormat module. The global lists can be used when
                     78: **     specifying accept lists for ALL requests and the local list can be 
                     79: **     used to add specific accept headers to the request.
                     80: */
                     81: 
1.88      frystyk    82: /* ------------------------------------------------------------------------- */
                     83: /*                             CONTENT TYPES                                */
                     84: /* ------------------------------------------------------------------------- */
1.61      frystyk    85: 
1.2       timbl      86: /*     Define a presentation system command for a content-type
                     87: **     -------------------------------------------------------
1.52      frystyk    88: ** INPUT:
                     89: **     conversions:    The list of conveters and presenters
                     90: **     representation: the MIME-style format name
                     91: **     command:        the MAILCAP-style command template
                     92: **     quality:        A degradation faction [0..1]
                     93: **     maxbytes:       A limit on the length acceptable as input (0 infinite)
                     94: **     maxsecs:        A limit on the time user will wait (0 for infinity)
1.2       timbl      95: */
1.72      frystyk    96: PUBLIC void HTPresentation_add (HTList *       conversions,
1.85      frystyk    97:                                const char *    representation,
                     98:                                const char *    command,
                     99:                                const char *    test_command,
1.72      frystyk   100:                                double          quality,
                    101:                                double          secs, 
                    102:                                double          secs_per_byte)
1.52      frystyk   103: {
1.83      frystyk   104:     HTPresentation * pres;
                    105:     if ((pres = (HTPresentation  *) HT_CALLOC(1,sizeof(HTPresentation))) == NULL)
                    106:         HT_OUTOFMEM("HTSetPresentation");
1.2       timbl     107:     
                    108:     pres->rep = HTAtom_for(representation);
                    109:     pres->rep_out = WWW_PRESENT;               /* Fixed for now ... :-) */
                    110:     pres->converter = HTSaveAndExecute;                /* Fixed for now ...     */
                    111:     pres->quality = quality;
                    112:     pres->secs = secs;
                    113:     pres->secs_per_byte = secs_per_byte;
                    114:     pres->rep = HTAtom_for(representation);
1.49      howcome   115:     pres->command = NULL;
1.2       timbl     116:     StrAllocCopy(pres->command, command);
1.49      howcome   117:     pres->test_command = NULL;
                    118:     StrAllocCopy(pres->test_command, test_command);
1.12      timbl     119:     HTList_addObject(conversions, pres);
1.2       timbl     120: }
                    121: 
1.73      frystyk   122: PUBLIC void HTPresentation_deleteAll (HTList * list)
                    123: {
                    124:     if (list) {
                    125:        HTList *cur = list;
                    126:        HTPresentation *pres;
                    127:        while ((pres = (HTPresentation*) HTList_nextObject(cur))) {
1.83      frystyk   128:            HT_FREE(pres->command);
                    129:            HT_FREE(pres);
1.73      frystyk   130:        }
                    131:        HTList_delete(list);
                    132:     }
                    133: }
1.2       timbl     134: 
                    135: /*     Define a built-in function for a content-type
                    136: **     ---------------------------------------------
                    137: */
1.72      frystyk   138: PUBLIC void HTConversion_add (HTList *         conversions,
1.85      frystyk   139:                              const char *      representation_in,
                    140:                              const char *      representation_out,
1.72      frystyk   141:                              HTConverter *     converter,
                    142:                              double            quality,
                    143:                              double            secs, 
                    144:                              double            secs_per_byte)
1.52      frystyk   145: {
1.83      frystyk   146:     HTPresentation * pres;
1.87      frystyk   147:     if ((pres = (HTPresentation*) HT_CALLOC(1,sizeof(HTPresentation))) == NULL)
1.83      frystyk   148:         HT_OUTOFMEM("HTSetPresentation");
1.2       timbl     149:     pres->rep = HTAtom_for(representation_in);
                    150:     pres->rep_out = HTAtom_for(representation_out);
                    151:     pres->converter = converter;
                    152:     pres->command = NULL;              /* Fixed */
1.49      howcome   153:     pres->test_command = NULL;
1.2       timbl     154:     pres->quality = quality;
                    155:     pres->secs = secs;
                    156:     pres->secs_per_byte = secs_per_byte;
1.12      timbl     157:     HTList_addObject(conversions, pres);
1.56      frystyk   158: }
                    159: 
1.73      frystyk   160: PUBLIC void HTConversion_deleteAll (HTList * list)
                    161: {
                    162:     HTPresentation_deleteAll(list);
                    163: }
                    164: 
1.88      frystyk   165: /* ------------------------------------------------------------------------- */
                    166: /*             CONTENT ENCODING AND CONTENT TRANSFER ENCODING               */
                    167: /* ------------------------------------------------------------------------- */
                    168: 
                    169: PUBLIC BOOL HTCoding_add (HTList *     list,
                    170:                          const char *  encoding,
                    171:                          HTCoder *     encoder,
                    172:                          HTCoder *     decoder,
                    173:                          double        quality)
1.73      frystyk   174: {
1.88      frystyk   175:     if (list && encoding && (encoder || decoder)) {
                    176:        HTCoding * me;
                    177:        if ((me = (HTCoding *) HT_CALLOC(1, sizeof(HTCoding))) == NULL)
                    178:            HT_OUTOFMEM("HTCoding_add");
                    179:        me->encoding = HTAtom_for(encoding);
1.87      frystyk   180:        me->encoder = encoder;
                    181:        me->decoder = decoder;
                    182:        me->quality = quality;
                    183:        if (CORE_TRACE)
                    184:            HTTrace("Codings..... Adding %s with quality %.2f\n",
1.88      frystyk   185:                    encoding, quality);
1.87      frystyk   186:        return HTList_addObject(list, (void *) me);
1.73      frystyk   187:     }
1.87      frystyk   188:     if (CORE_TRACE) HTTrace("Codings..... Bad argument\n");
                    189:     return NO;
1.73      frystyk   190: }
                    191: 
1.88      frystyk   192: PUBLIC void HTCoding_deleteAll (HTList * list)
1.73      frystyk   193: {
                    194:     if (list) {
1.87      frystyk   195:        HTList * cur = list;
1.88      frystyk   196:        HTCoding * pres;
                    197:        while ((pres = (HTCoding *) HTList_nextObject(cur)))
1.83      frystyk   198:            HT_FREE(pres);
1.73      frystyk   199:        HTList_delete(list);
                    200:     }
                    201: }
                    202: 
1.88      frystyk   203: PUBLIC const char * HTCoding_name (HTCoding * me)
1.87      frystyk   204: {
1.88      frystyk   205:     return me ? HTAtom_name(me->encoding) : NULL;
1.87      frystyk   206: }
                    207: 
1.88      frystyk   208: /* ------------------------------------------------------------------------- */
                    209: /*                             CONTENT LANGUAGE                             */
                    210: /* ------------------------------------------------------------------------- */
                    211: 
1.73      frystyk   212: PUBLIC void HTLanguage_add (HTList *           list,
1.85      frystyk   213:                            const char *        lang,
1.73      frystyk   214:                            double              quality)
                    215: {
                    216:     HTAcceptNode * node;
                    217:     if (!list || !lang || !*lang)  {
1.87      frystyk   218:        if (CORE_TRACE)
1.84      eric      219:            HTTrace("Languages... Bad argument\n");
1.73      frystyk   220:        return;
                    221:     }
1.83      frystyk   222:     if ((node = (HTAcceptNode *) HT_CALLOC(1, sizeof(HTAcceptNode))) == NULL)
                    223:         HT_OUTOFMEM("HTAcceptLanguage");
1.73      frystyk   224: 
                    225:     HTList_addObject(list, (void*)node);
                    226:     node->atom = HTAtom_for(lang);
                    227:     node->quality = quality;
                    228: }
                    229: 
                    230: PUBLIC void HTLanguage_deleteAll (HTList * list)
                    231: {
1.87      frystyk   232:     if (list) {
                    233:        HTList *cur = list;
                    234:        HTAcceptNode *pres;
                    235:        while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
                    236:            HT_FREE(pres);
                    237:        }
                    238:        HTList_delete(list);
                    239:     }
1.73      frystyk   240: }
                    241: 
1.88      frystyk   242: /* ------------------------------------------------------------------------- */
                    243: /*                             CONTENT CHARSET                              */
                    244: /* ------------------------------------------------------------------------- */
                    245: 
1.73      frystyk   246: PUBLIC void HTCharset_add (HTList *            list,
1.85      frystyk   247:                           const char *         charset,
1.73      frystyk   248:                           double               quality)
                    249: {
                    250:     HTAcceptNode * node;
                    251:     if (!list || !charset || !*charset)  {
1.87      frystyk   252:        if (CORE_TRACE)
1.84      eric      253:            HTTrace("Charset..... Bad argument\n");
1.73      frystyk   254:        return;
                    255:     }
1.83      frystyk   256:     if ((node = (HTAcceptNode *) HT_CALLOC(1, sizeof(HTAcceptNode))) == NULL)
                    257:         HT_OUTOFMEM("HTAcceptCharsetuage");
1.73      frystyk   258: 
                    259:     HTList_addObject(list, (void*)node);
                    260:     node->atom = HTAtom_for(charset);
                    261:     node->quality = quality;
                    262: }
                    263: 
                    264: PUBLIC void HTCharset_deleteAll (HTList * list)
                    265: {
1.87      frystyk   266:     if (list) {
                    267:        HTList *cur = list;
                    268:        HTAcceptNode *pres;
                    269:        while ((pres = (HTAcceptNode *) HTList_nextObject(cur))) {
                    270:            HT_FREE(pres);
                    271:        }
                    272:        HTList_delete(list);
                    273:     }
1.73      frystyk   274: }
                    275: 
                    276: /* ------------------------------------------------------------------------- */
                    277: /*                     GLOBAL LIST OF CONVERTERS ETC.                       */
                    278: /* ------------------------------------------------------------------------- */
                    279: 
1.72      frystyk   280: /*
                    281: **     Global Accept Format Types Conversions
                    282: **     list can be NULL
                    283: */
1.73      frystyk   284: PUBLIC void HTFormat_setConversion (HTList * list)
1.72      frystyk   285: {
                    286:     HTConversions = list;
                    287: }
                    288: 
                    289: PUBLIC HTList * HTFormat_conversion (void)
                    290: {
                    291:     return HTConversions;
                    292: }
                    293: 
                    294: /*
1.88      frystyk   295: **     Global list of Content Encodings
1.72      frystyk   296: **     list can be NULL
                    297: */
1.88      frystyk   298: PUBLIC void HTFormat_setContentCoding (HTList *list)
1.72      frystyk   299: {
1.88      frystyk   300:     HTContentCoders = list;
1.72      frystyk   301: }
                    302: 
1.88      frystyk   303: PUBLIC HTList * HTFormat_contentCoding (void)
1.72      frystyk   304: {
1.88      frystyk   305:     return HTContentCoders;
1.72      frystyk   306: }
                    307: 
                    308: /*
1.88      frystyk   309: **     Global list of Content Transfer Encodings
                    310: **     list can be NULL
                    311: */
                    312: PUBLIC void HTFormat_setTransferCoding (HTList *list)
                    313: {
                    314:     HTTransferCoders = list;
                    315: }
                    316: 
                    317: PUBLIC HTList * HTFormat_transferCoding (void)
                    318: {
                    319:     return HTTransferCoders;
                    320: }
                    321: 
                    322: /*
1.72      frystyk   323: **     Global Accept Languages
                    324: **     list can be NULL
                    325: */
                    326: PUBLIC void HTFormat_setLanguage (HTList *list)
                    327: {
                    328:     HTLanguages = list;
                    329: }
                    330: 
                    331: PUBLIC HTList * HTFormat_language (void)
                    332: {
                    333:     return HTLanguages;
                    334: }
                    335: 
                    336: /*
                    337: **     Global Accept Charsets
                    338: **     list can be NULL
                    339: */
                    340: PUBLIC void HTFormat_setCharset (HTList *list)
                    341: {
                    342:     HTCharsets = list;
                    343: }
                    344: 
                    345: PUBLIC HTList * HTFormat_charset (void)
                    346: {
                    347:     return HTCharsets;
                    348: }
1.56      frystyk   349: 
1.73      frystyk   350: /*
                    351: **     Convenience function to clean up
                    352: */
                    353: PUBLIC void HTFormat_deleteAll (void)
1.17      luotonen  354: {
1.73      frystyk   355:     if (HTConversions) {
                    356:        HTConversion_deleteAll(HTConversions);
                    357:        HTConversions = NULL;
                    358:     }
                    359:     if (HTLanguages) {
                    360:        HTLanguage_deleteAll(HTLanguages);
                    361:        HTLanguages = NULL;
1.63      frystyk   362:     }
1.88      frystyk   363:     if (HTContentCoders) {
                    364:        HTCoding_deleteAll(HTContentCoders);
                    365:        HTContentCoders = NULL;
1.63      frystyk   366:     }
1.88      frystyk   367:     if (HTTransferCoders) {
                    368:        HTCoding_deleteAll(HTTransferCoders);
                    369:        HTTransferCoders = NULL;
                    370:     }
1.73      frystyk   371:     if (HTCharsets) {
                    372:        HTCharset_deleteAll(HTCharsets);
                    373:        HTCharsets = NULL;
1.63      frystyk   374:     }
1.17      luotonen  375: }
                    376: 
1.63      frystyk   377: /* ------------------------------------------------------------------------- */
1.90    ! frystyk   378: /*                             STREAM STACKS                                */
1.63      frystyk   379: /* ------------------------------------------------------------------------- */
                    380: 
1.77      frystyk   381: PRIVATE BOOL better_match (HTFormat f, HTFormat g)
1.63      frystyk   382: {
1.85      frystyk   383:     const char *p, *q;
1.63      frystyk   384: 
                    385:     if (f && g  &&  (p = HTAtom_name(f))  &&  (q = HTAtom_name(g))) {
                    386:        int i,j;
                    387:        for(i=0 ; *p; p++) if (*p == '*') i++;
                    388:        for(j=0 ; *q; q++) if (*q == '*') j++;
                    389:        if (i < j) return YES;
                    390:     }
                    391:     return NO;
                    392: }
                    393: 
                    394: 
1.77      frystyk   395: PRIVATE BOOL wild_match (HTAtom * tmplate, HTAtom * actual)
1.17      luotonen  396: {
1.89      eric      397:     const char *t, *a;
                    398:     char *st, *sa;
1.17      luotonen  399:     BOOL match = NO;
                    400: 
1.48      frystyk   401:     if (tmplate && actual && (t = HTAtom_name(tmplate))) {
1.22      luotonen  402:        if (!strcmp(t, "*"))
                    403:            return YES;
1.17      luotonen  404: 
1.22      luotonen  405:        if (strchr(t, '*') &&
                    406:            (a = HTAtom_name(actual)) &&
                    407:            (st = strchr(t, '/')) && (sa = strchr(a,'/'))) {
1.17      luotonen  408: 
1.22      luotonen  409:            *sa = 0;
                    410:            *st = 0;
                    411: 
                    412:            if ((*(st-1)=='*' &&
                    413:                 (*(st+1)=='*' || !strcasecomp(st+1, sa+1))) ||
                    414:                (*(st+1)=='*' && !strcasecomp(t,a)))
                    415:                match = YES;
                    416: 
                    417:            *sa = '/';
                    418:            *st = '/';
                    419:        }    
                    420:     }
1.23      luotonen  421:     return match;
1.17      luotonen  422: }
                    423: 
1.87      frystyk   424: /*     Create a Content Type filter stack
                    425: **     ----------------------------------
1.7       secret    426: **     If a wildcard match is made, a temporary HTPresentation
1.2       timbl     427: **     structure is made to hold the destination format while the
                    428: **     new stack is generated. This is just to pass the out format to
                    429: **     MIME so far.  Storing the format of a stream in the stream might
                    430: **     be a lot neater.
1.10      timbl     431: **
1.29      frystyk   432: **     The star/star format is special, in that if you can take
1.40      frystyk   433: **     that you can take anything.
1.2       timbl     434: */
1.77      frystyk   435: PUBLIC HTStream * HTStreamStack (HTFormat      rep_in,
                    436:                                 HTFormat       rep_out,
                    437:                                 HTStream *     output_stream,
                    438:                                 HTRequest *    request,
                    439:                                 BOOL           guess)
1.2       timbl     440: {
1.14      timbl     441:     HTList * conversion[2];
                    442:     int which_list;
1.59      frystyk   443:     double best_quality = -1e30;               /* Pretty bad! */
1.65      frystyk   444:     HTPresentation *pres, *best_match=NULL;
1.80      frystyk   445:     if (rep_out == WWW_RAW) {
1.87      frystyk   446:        if (CORE_TRACE) HTTrace("StreamStack. Raw output...\n");
1.81      frystyk   447:        return output_stream ? output_stream : HTErrorStream();
1.34      luotonen  448:     }
1.79      frystyk   449: 
1.80      frystyk   450:     if (rep_out == rep_in) {
1.87      frystyk   451:        if (CORE_TRACE)
1.84      eric      452:            HTTrace("StreamStack. Identical input/output format (%s)\n",
1.80      frystyk   453:                     HTAtom_name(rep_out));
1.81      frystyk   454:        return output_stream ? output_stream : HTErrorStream();
1.74      frystyk   455:     }
1.87      frystyk   456:     if (CORE_TRACE) {
1.89      eric      457:        const char *p = HTAtom_name(rep_in);
                    458:        const char *q = HTAtom_name(rep_out); 
1.84      eric      459:        HTTrace("StreamStack. Constructing stream stack for %s to %s\n",
1.82      frystyk   460:                 p ? p : "<NULL>", q ? q : "<NULL>");
1.47      frystyk   461:     }
1.2       timbl     462: 
1.88      frystyk   463:     conversion[0] = HTRequest_conversion(request);
1.14      timbl     464:     conversion[1] = HTConversions;
1.17      luotonen  465: 
1.15      luotonen  466:     for(which_list = 0; which_list<2; which_list++) {
                    467:        HTList * cur = conversion[which_list];
                    468:        while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
1.65      frystyk   469:            if ((pres->rep==rep_in || wild_match(pres->rep, rep_in)) &&
                    470:                (pres->rep_out==rep_out || wild_match(pres->rep_out,rep_out))){
                    471:                if (!best_match || better_match(pres->rep, best_match->rep) ||
1.33      luotonen  472:                    (!better_match(best_match->rep, pres->rep) &&
                    473:                     pres->quality > best_quality)) {
1.85      frystyk   474: #ifdef HAVE_SYSTEM
1.65      frystyk   475:                    int result=0;
                    476:                    if (pres->test_command) {
                    477:                        result = system(pres->test_command);
1.87      frystyk   478:                        if (CORE_TRACE) 
1.84      eric      479:                            HTTrace("StreamStack. system(%s) returns %d\n", pres->test_command, result);
1.65      frystyk   480:                    }
                    481:                    if (!result) {
1.49      howcome   482:                        best_match = pres;
                    483:                        best_quality = pres->quality;
                    484:                    }
1.65      frystyk   485: #else
                    486:                    best_match = pres;
                    487:                    best_quality = pres->quality;
1.85      frystyk   488: #endif /* HAVE_SYSTEM */
1.10      timbl     489:                }
                    490:            }
1.2       timbl     491:        }
                    492:     }
1.80      frystyk   493: 
                    494:     if (best_match) {
                    495:        if (rep_out == WWW_SOURCE && best_match->rep_out != WWW_SOURCE) {
1.87      frystyk   496:            if (CORE_TRACE) HTTrace("StreamStack. Source output\n");
1.81      frystyk   497:            return output_stream ? output_stream : HTErrorStream();
1.80      frystyk   498:        }
1.65      frystyk   499:        return (*best_match->converter)(request, best_match->command,
                    500:                                        rep_in, rep_out, output_stream);
1.80      frystyk   501:     }
                    502:     if (rep_out == WWW_SOURCE) {
1.87      frystyk   503:        if (CORE_TRACE) HTTrace("StreamStack. Source output\n");
1.81      frystyk   504:        return output_stream ? output_stream : HTErrorStream();
1.80      frystyk   505:     }
                    506: 
1.87      frystyk   507:     if (CORE_TRACE)
1.84      eric      508:        HTTrace("StreamStack. No match found, dumping to local file\n");
1.65      frystyk   509:     return HTSaveLocally(request, NULL, rep_in, rep_out, output_stream);
1.2       timbl     510: }
                    511:        
                    512: 
                    513: /*             Find the cost of a filter stack
                    514: **             -------------------------------
                    515: **
                    516: **     Must return the cost of the same stack which StreamStack would set up.
                    517: **
                    518: ** On entry,
                    519: **     length  The size of the data to be converted
                    520: */
1.77      frystyk   521: PUBLIC double HTStackValue (HTList *   theseConversions,
                    522:                            HTFormat    rep_in,
                    523:                            HTFormat    rep_out,
                    524:                            double      initial_value,
                    525:                            long int    length)
1.2       timbl     526: {
1.14      timbl     527:     int which_list;
                    528:     HTList* conversion[2];
                    529:     
1.87      frystyk   530:     if (CORE_TRACE) {
1.84      eric      531:        HTTrace("StackValue.. Evaluating stream stack for %s worth %.3f to %s\n",
1.65      frystyk   532:                HTAtom_name(rep_in),    initial_value,
                    533:                HTAtom_name(rep_out));
                    534:     }
1.2       timbl     535:     if (rep_out == WWW_SOURCE ||
1.10      timbl     536:        rep_out == rep_in) return 0.0;
1.2       timbl     537: 
1.14      timbl     538:     conversion[0] = theseConversions;
                    539:     conversion[1] = HTConversions;
                    540:     
                    541:     for(which_list = 0; which_list<2; which_list++)
                    542:      if (conversion[which_list]) {
1.15      luotonen  543:         HTList * cur = conversion[which_list];
1.2       timbl     544:        HTPresentation * pres;
1.15      luotonen  545:        while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
                    546:            if (pres->rep == rep_in &&
1.17      luotonen  547:                (pres->rep_out == rep_out || wild_match(pres->rep_out, rep_out))) {
1.59      frystyk   548:                double value = initial_value * pres->quality;
1.2       timbl     549:                if (HTMaxSecs != 0.0)
1.15      luotonen  550:                    value = value - (length*pres->secs_per_byte + pres->secs)
1.2       timbl     551:                                         /HTMaxSecs;
                    552:                return value;
                    553:            }
                    554:        }
                    555:     }
1.63      frystyk   556:     return NO_VALUE_FOUND;             /* Really bad */
1.1       timbl     557: }
1.2       timbl     558: 
1.87      frystyk   559: /*     Create a new coder and insert it into stream chain
                    560: **     --------------------------------------------------
                    561: **     Creating the content decoding stack is not based on quality factors as
                    562: **     we don't have the freedom as with content types. Specify whether you
1.88      frystyk   563: **     you want encoding or decoding using the BOOL "encode" flag.
1.87      frystyk   564: */
1.88      frystyk   565: PUBLIC HTStream * HTContentCodingStack (HTEncoding     encoding,
                    566:                                        HTStream *      target,
                    567:                                        HTRequest *     request,
                    568:                                        void *          param,
                    569:                                        BOOL            encode)
1.87      frystyk   570: {
                    571:     HTList * coders[2];
                    572:     HTStream * top = target;
1.88      frystyk   573:     HTCoding * pres = NULL;
1.87      frystyk   574:     int cnt;
1.88      frystyk   575:     if (!encoding || !request) {
1.87      frystyk   576:        if (CORE_TRACE) HTTrace("Codings... Nothing applied...\n");
                    577:        return target ? target : HTErrorStream();
                    578:     }
1.88      frystyk   579:     coders[0] = HTRequest_encoding(request);
                    580:     coders[1] = HTContentCoders;
1.87      frystyk   581:     if (CORE_TRACE)
1.88      frystyk   582:        HTTrace("Codings..... Looking for %s\n", HTAtom_name(encoding));
1.87      frystyk   583:     for (cnt=0; cnt < 2; cnt++) {
                    584:        HTList * cur = coders[cnt];
1.88      frystyk   585:        while ((pres = (HTCoding *) HTList_nextObject(cur))) {
                    586:            if (pres->encoding == encoding) {
1.87      frystyk   587:                if (CORE_TRACE) HTTrace("Codings..... Found...\n");
1.88      frystyk   588:                if (encode) {
1.87      frystyk   589:                    if (pres->encoder)
1.88      frystyk   590:                        top = (*pres->encoder)(request, param, encoding, top);
1.87      frystyk   591:                    break;
                    592:                } else if (pres->decoder) {
1.88      frystyk   593:                    top = (*pres->decoder)(request, param, encoding, top);
1.87      frystyk   594:                    break;
                    595:                }
                    596:            }
                    597:        }
                    598:     }
                    599:     return top;
                    600: }
1.10      timbl     601: 
1.87      frystyk   602: /*
                    603: **  Here you can provide a complete list instead of a single token.
                    604: **  The list has to filled up in the order the _encodings_ are to be applied
                    605: */
1.88      frystyk   606: PUBLIC HTStream * HTContentEncodingStack (HTList *     encodings,
                    607:                                          HTStream *    target,
                    608:                                          HTRequest *   request,
                    609:                                          void *        param)
1.87      frystyk   610: {
                    611:     if (encodings) {
                    612:        HTList * cur = encodings;
                    613:        HTEncoding pres;
                    614:        HTStream * top = target;
                    615:        while ((pres = (HTEncoding) HTList_nextObject(cur)))
1.88      frystyk   616:            top = HTContentCodingStack(pres, top, request, param, YES);
1.87      frystyk   617:        return top;
                    618:     }
                    619:     return HTErrorStream();
                    620: }
                    621: 
                    622: /*
                    623: **  Here you can provide a complete list instead of a single token.
                    624: **  The list has to be in the order the _encodings_ were applied - that
                    625: **  is, the same way that _encodings_ are to be applied. This is all consistent
                    626: **  with the order of the Content-Encoding header.
                    627: */
1.88      frystyk   628: PUBLIC HTStream * HTContentDecodingStack (HTList *     encodings,
                    629:                                          HTStream *    target,
                    630:                                          HTRequest *   request,
                    631:                                          void *        param)
1.87      frystyk   632: {
                    633:     if (encodings) {
                    634:        HTEncoding pres;
                    635:        int cnt = HTList_count(encodings);
                    636:        HTStream * top = target;
                    637:        while (cnt > 0) {
                    638:            pres = (HTEncoding) HTList_objectAt(encodings, --cnt);
1.88      frystyk   639:            top = HTContentCodingStack(pres, top, request, param, NO);
1.87      frystyk   640:        }
                    641:        return top;
                    642:     }
                    643:     return HTErrorStream();
                    644: }
1.88      frystyk   645: 
                    646: /*     Create a new transfer coder and insert it into stream chain
                    647: **     -----------------------------------------------------------
                    648: **     Creating the content decoding stack is not based on quality factors as
                    649: **     we don't have the freedom as with content types. Specify whether you
                    650: **     you want encoding or decoding using the BOOL "encode" flag.
                    651: */
                    652: PUBLIC HTStream * HTTransferCodingStack (HTEncoding    encoding,
                    653:                                         HTStream *     target,
                    654:                                         HTRequest *    request,
                    655:                                         void *         param,
                    656:                                         BOOL           encode)
                    657: {
                    658:     HTList * coders[2];
                    659:     HTStream * top = target;
                    660:     HTCoding * pres = NULL;
                    661:     int cnt;
                    662:     if (!encoding || !request) {
                    663:        if (CORE_TRACE) HTTrace("C-T-E..... Nothing applied...\n");
                    664:        return target ? target : HTErrorStream();
                    665:     }
                    666:     coders[0] = HTRequest_transfer(request);
                    667:     coders[1] = HTTransferCoders;
                    668:     if (CORE_TRACE)
                    669:        HTTrace("C-T-E....... Looking for %s\n", HTAtom_name(encoding));
                    670:     for (cnt=0; cnt < 2; cnt++) {
                    671:        HTList * cur = coders[cnt];
                    672:        while ((pres = (HTCoding *) HTList_nextObject(cur))) {
                    673:            if (pres->encoding == encoding) {
                    674:                if (CORE_TRACE) HTTrace("C-T-E....... Found...\n");
                    675:                if (encode) {
                    676:                    if (pres->encoder)
                    677:                        top = (*pres->encoder)(request, param, encoding, top);
                    678:                    break;
                    679:                } else if (pres->decoder) {
                    680:                    top = (*pres->decoder)(request, param, encoding, top);
                    681:                    break;
                    682:                }
                    683:            }
                    684:        }
                    685:     }
                    686:     return top;
                    687: }
                    688: 

Webmaster