Annotation of libwww/Library/src/HTBind.c, revision 2.27

2.22      frystyk     1: /*                                                                  Htbind.c
2.1       frystyk     2: **     FILE SUFFIX BIND MANAGER
                      3: **
                      4: **     (c) COPYRIGHT MIT 1995
                      5: **     Please first read the full copyright statement in the file COPYRIGH.
2.27    ! frystyk     6: **     @(#) $Id: HTBind.c,v 2.26 1998/05/04 19:36:17 frystyk Exp $
2.1       frystyk     7: **
                      8: **     This module sets up the binding between a file Bind and a media
                      9: **     type, language, encoding etc. In a client application the Binds
                     10: **     are used in protocols that does not support media types etc., like
                     11: **     FTP, and in server applications they are used to make the bindings
                     12: **     between the server and the local file store that the server can
                     13: **     serve to the rest of the world (well almost). The HTFormat module
                     14: **     holds this information against the accept headers received in a
                     15: **     request and uses if for format negotiation. All the binding management
                     16: **     can all be replace by a database interface. 
                     17: **
                     18: ** History:
                     19: **        Feb 91       Written Tim Berners-Lee CERN/CN
                     20: **        Apr 91       vms-vms access included using DECnet syntax
                     21: **     26 Jun 92 (JFG) When running over DECnet, suppressed FTP.
                     22: **                     Fixed access bug for relative names on VMS.
                     23: **        Sep 93 (MD)  Access to VMS files allows sharing.
                     24: **     15 Nov 93 (MD)  Moved HTVMSname to HTVMSUTILS.C
                     25: **     22 Feb 94 (MD)  Excluded two routines if we are not READING directories
                     26: **     18 May 94 (HF)  Directory stuff removed and stream handling updated,
                     27: **                     error messages introduced etc.
                     28: **     10 Maj 95 HF    Spawned off from HTFile in order to make it easier to
                     29: **                     override by a new module. It's now based on anchors
                     30: **                     and hash tables
                     31: ** Bugs:
                     32: */
                     33: 
                     34: /* Library Includes */
2.26      frystyk    35: #include "wwwsys.h"
2.21      frystyk    36: #include "WWWUtil.h"
2.24      frystyk    37: #include "HTAnchor.h"
                     38: #include "HTResponse.h"
2.1       frystyk    39: #include "HTParse.h"
                     40: #include "HTBind.h"                                     /* Implemented here */
                     41: 
                     42: typedef struct _HTBind {
                     43:     char *     suffix;
                     44:     HTFormat   type;                   /* Content-Type */
                     45:     HTEncoding encoding;               /* Content-Encoding */
2.22      frystyk    46:     HTEncoding transfer;               /* Content-Transfer-Encoding */
2.1       frystyk    47:     HTLanguage language;               /* Content-Language */
                     48:     double     quality;
                     49: } HTBind;
                     50: 
                     51: #define HASH_SIZE      101        /* Arbitrary prime. Memory/speed tradeoff */
                     52: 
                     53: /* Suffix registration */
                     54: PRIVATE BOOL HTCaseSen = YES;                /* Are suffixes case sensitive */
                     55: PRIVATE char *HTDelimiters = NULL;                       /* Set of suffixes */
                     56: 
                     57: PRIVATE HTList **HTBindings = NULL;   /* Point to table of lists of bindings */
                     58: 
2.21      frystyk    59: PRIVATE HTBind no_suffix = { "*", NULL, NULL, NULL, NULL, 0.5 };
                     60: PRIVATE HTBind unknown_suffix = { "*.*", NULL, NULL, NULL, NULL, 0.5 };
2.1       frystyk    61: 
                     62: /* ------------------------------------------------------------------------- */
                     63: 
                     64: /*     
                     65: **     Set up the list of suffix bindings. Done by HTLibInit
                     66: */
2.11      frystyk    67: PUBLIC BOOL HTBind_init (void)
2.1       frystyk    68: {
2.8       frystyk    69:     if (!HTBindings) {
2.19      frystyk    70:        if (!(HTBindings = (HTList **) HT_CALLOC(HASH_SIZE, sizeof(HTList *))))
2.17      frystyk    71:            HT_OUTOFMEM("HTBind_init");
2.8       frystyk    72:     }
2.1       frystyk    73:     StrAllocCopy(HTDelimiters, DEFAULT_SUFFIXES);
2.15      frystyk    74:     no_suffix.type = WWW_UNKNOWN;
2.25      frystyk    75:     no_suffix.encoding = WWW_CODING_BINARY;
2.15      frystyk    76:     unknown_suffix.type = WWW_UNKNOWN;
2.25      frystyk    77:     unknown_suffix.encoding = WWW_CODING_BINARY;
2.1       frystyk    78:     return YES;
                     79: }
                     80: 
                     81: 
                     82: /*
                     83: **     Cleans up the memory allocated by file bindings
                     84: **     Done by HTLibTerminate().
                     85: **     Written by Eric Sink, eric@spyglass.com, and Henrik
                     86: */
2.11      frystyk    87: PUBLIC BOOL HTBind_deleteAll (void)
2.1       frystyk    88: {
                     89:     int cnt;
                     90:     HTList *cur;
                     91:     if (!HTBindings)
                     92:        return NO;
                     93:     for (cnt=0; cnt<HASH_SIZE; cnt++) {
                     94:        if ((cur = HTBindings[cnt])) { 
                     95:            HTBind *pres;
                     96:            while ((pres = (HTBind *) HTList_nextObject(cur)) != NULL) {
2.17      frystyk    97:                HT_FREE(pres->suffix);
                     98:                HT_FREE(pres);
2.1       frystyk    99:            }
                    100:        }
                    101:        HTList_delete(HTBindings[cnt]);
                    102:        HTBindings[cnt] = NULL;
                    103:     }
2.19      frystyk   104:     HT_FREE(HTBindings);
2.17      frystyk   105:     HT_FREE(HTDelimiters);
2.1       frystyk   106:     return YES;
                    107: }
                    108: 
                    109: 
                    110: /*     Make suffix bindings case sensitive
                    111: **     -----------------------------------
                    112: */
2.11      frystyk   113: PUBLIC void HTBind_caseSensitive (BOOL sensitive)
2.1       frystyk   114: {
                    115:     HTCaseSen = sensitive;
                    116: }
                    117: 
                    118: 
                    119: /*     Get set of suffixes
                    120: **     -------------------
                    121: */
2.20      frystyk   122: PUBLIC const char *HTBind_delimiters (void)
2.1       frystyk   123: {
                    124:     return HTDelimiters;
                    125: }
                    126: 
                    127: 
                    128: /*     Change set of suffixes
                    129: **     ----------------------
                    130: */
2.20      frystyk   131: PUBLIC void HTBind_setDelimiters (const char * new_suffixes)
2.1       frystyk   132: {
                    133:     if (new_suffixes && *new_suffixes)
                    134:        StrAllocCopy(HTDelimiters, new_suffixes);
                    135: }
                    136: 
                    137: 
                    138: /*     Define the representation associated with a file suffix
                    139: **     -------------------------------------------------------
                    140: **
                    141: **     Calling this with suffix set to "*" will set the default
                    142: **     representation.
                    143: **     Calling this with suffix set to "*.*" will set the default
                    144: **     representation for unknown suffix files which contain a "."
                    145: **
                    146: **     If filename suffix is already defined its previous
                    147: **     definition is overridden (or modified)
                    148: */
2.20      frystyk   149: PUBLIC BOOL HTBind_addType (const char *       suffix,
                    150:                            const char *        representation,
2.12      frystyk   151:                            double              value)
2.1       frystyk   152: {
2.21      frystyk   153:     return HTBind_add(suffix, representation, NULL, NULL, NULL, value);
2.1       frystyk   154: }
                    155: 
2.20      frystyk   156: PUBLIC BOOL HTBind_addEncoding (const char *   suffix,
                    157:                                const char *    encoding,
2.11      frystyk   158:                                double          value)
2.1       frystyk   159: {
2.21      frystyk   160:     return HTBind_add(suffix, NULL, encoding, NULL, NULL, value);
2.1       frystyk   161: }
                    162: 
2.21      frystyk   163: PUBLIC BOOL HTBind_addTransfer (const char *   suffix,
                    164:                                const char *    transfer,
                    165:                                double          value)
                    166: {
                    167:     return HTBind_add(suffix, NULL, NULL, transfer, NULL, value);
                    168: }
                    169: 
2.20      frystyk   170: PUBLIC BOOL HTBind_addLanguage (const char *   suffix,
                    171:                                const char *    language,
2.11      frystyk   172:                                double          value)
2.1       frystyk   173: {
2.21      frystyk   174:     return HTBind_add(suffix, NULL, NULL, NULL, language, value);
2.1       frystyk   175: }
                    176: 
2.20      frystyk   177: PUBLIC BOOL HTBind_add (const char *   suffix,
                    178:                        const char *    representation,
                    179:                        const char *    encoding,
2.21      frystyk   180:                        const char *    transfer,
2.20      frystyk   181:                        const char *    language,
2.11      frystyk   182:                        double          value)
2.1       frystyk   183: {
                    184:     HTBind * suff;
                    185:     if (!suffix)
                    186:        return NO;
                    187:     if (!strcmp(suffix, "*"))
                    188:        suff = &no_suffix;
                    189:     else if (!strcmp(suffix, "*.*"))
                    190:        suff = &unknown_suffix;
                    191:     else {
                    192:        HTList *suflist;
                    193:        int hash=0;
2.20      frystyk   194:        const char *ptr=suffix;
2.1       frystyk   195: 
                    196:        /* Select list from hash table */
                    197:        for( ; *ptr; ptr++)
                    198:            hash = (int) ((hash * 3 + (*(unsigned char*)ptr)) % HASH_SIZE);
                    199: 
                    200:        if (!HTBindings[hash]) HTBindings[hash] = HTList_new();
                    201:        suflist = HTBindings[hash];
                    202: 
                    203:        /* Look for existing binding */
                    204:        {
                    205:            HTList *cur = suflist;
                    206:            while ((suff = (HTBind *) HTList_nextObject(cur)) != NULL) {
                    207:                if (!strcmp(suff->suffix, suffix))
                    208:                    break;
                    209:            }
                    210:        }
                    211: 
                    212:        /* If not found -- create a new node */
                    213:        if (!suff) {
2.17      frystyk   214:            if ((suff = (HTBind *) HT_CALLOC(1, sizeof(HTBind))) == NULL)
                    215:                HT_OUTOFMEM("HTBind_add");
2.1       frystyk   216:            HTList_addObject(suflist, (void *) suff);
                    217:            StrAllocCopy(suff->suffix, suffix);
                    218:        }
                    219:     }
                    220: 
                    221:     /* Set the appropriate values */
                    222:     {
2.21      frystyk   223:        HTChunk * chunk = HTChunk_new(32);
2.1       frystyk   224:        char *ptr;
                    225:        if (representation) {
2.21      frystyk   226:            HTChunk_puts(chunk, representation);
                    227:            ptr = HTChunk_data(chunk);
                    228:            for (; *ptr; ptr++)
2.1       frystyk   229:                *ptr = TOLOWER(*ptr);
2.21      frystyk   230:            suff->type = HTAtom_for(HTChunk_data(chunk));
                    231:            HTChunk_clear(chunk);
2.1       frystyk   232:        }
2.21      frystyk   233:        if (encoding) {
                    234:            HTChunk_puts(chunk, encoding);
                    235:            ptr = HTChunk_data(chunk);
                    236:            for (; *ptr; ptr++)
2.1       frystyk   237:                *ptr = TOLOWER(*ptr);
2.21      frystyk   238:            suff->encoding = HTAtom_for(HTChunk_data(chunk));
                    239:            HTChunk_clear(chunk);
2.1       frystyk   240:        }
2.21      frystyk   241:        if (transfer) {
                    242:            HTChunk_puts(chunk, transfer);
                    243:            ptr = HTChunk_data(chunk);
                    244:            for (; *ptr; ptr++)
2.1       frystyk   245:                *ptr = TOLOWER(*ptr);
2.21      frystyk   246:            suff->transfer = HTAtom_for(HTChunk_data(chunk));
                    247:            HTChunk_clear(chunk);
2.1       frystyk   248:        }
2.21      frystyk   249:        if (language) {
                    250:            HTChunk_puts(chunk, language);
                    251:            ptr = HTChunk_data(chunk);
                    252:            for (; *ptr; ptr++)
                    253:                *ptr = TOLOWER(*ptr);
                    254:            suff->language = HTAtom_for(HTChunk_data(chunk));
                    255:            HTChunk_clear(chunk);
                    256:        }
                    257:        HTChunk_delete(chunk);
2.1       frystyk   258:        suff->quality = value;
                    259:     }
                    260:     return YES;
                    261: }
                    262: 
                    263: 
                    264: /*     Determine a suitable suffix
                    265: **     ---------------------------
                    266: **  Use the set of bindings to find a suitable suffix (or index)
                    267: **  for a certain combination of language, media type and encoding
                    268: **  given in the anchor.
                    269: **
2.21      frystyk   270: **  Returns a pointer to a suitable suffix string that must be freed 
2.1       frystyk   271: **  by the caller. If more than one suffix is found they are all
                    272: **  concatenated using the first delimiter in HTDelimiters.
                    273: **  If no suffix is found, NULL is returned.
                    274: */
2.11      frystyk   275: PUBLIC char * HTBind_getSuffix (HTParentAnchor * anchor)
2.1       frystyk   276: {
                    277:     int cnt;
2.23      frystyk   278:     HTList * cur;
                    279:     HTChunk * suffix = HTChunk_new(48);
                    280:     char delimiter = *HTDelimiters;
                    281:     BOOL ct=NO, ce=NO, cl=NO;
2.24      frystyk   282:     HTFormat format = HTAnchor_format(anchor);
                    283:     HTList * encoding = HTAnchor_encoding(anchor);
                    284:     HTList * language = HTAnchor_language(anchor);
2.1       frystyk   285:     if (anchor) {
                    286:        for (cnt=0; cnt<HASH_SIZE; cnt++) {
                    287:            if ((cur = HTBindings[cnt])) { 
                    288:                HTBind *pres;
2.21      frystyk   289:                while ((pres = (HTBind *) HTList_nextObject(cur))) {
2.24      frystyk   290:                    if (!ct && (pres->type && pres->type == format)){
2.23      frystyk   291:                        HTChunk_putc(suffix, delimiter);
                    292:                        HTChunk_puts(suffix, pres->suffix);
                    293:                        ct = YES;
2.24      frystyk   294:                    } else if (!ce && pres->encoding && encoding) {
2.21      frystyk   295: 
                    296:                        /* @@@ Search list @@@ */
2.23      frystyk   297:                        ce = YES;
2.21      frystyk   298: 
2.24      frystyk   299:                    } else if (!cl && pres->language && language) {
2.21      frystyk   300: 
                    301:                        /* @@@ Search list @@@ */
                    302: 
2.23      frystyk   303:                        cl = YES;
2.1       frystyk   304:                    }
                    305:                }
                    306:            }
                    307:        }
                    308:     }
2.23      frystyk   309:     return HTChunk_toCString(suffix);
2.1       frystyk   310: }
                    311: 
2.24      frystyk   312: /*
2.1       frystyk   313: **  Use the set of bindings to find the combination of language,
2.24      frystyk   314: **  media type and encoding of a given object. This information can either be
                    315: **  stored in the anchor obejct or in the response object depending on which
                    316: **  function is called.
2.1       frystyk   317: **
2.21      frystyk   318: **  We comprise here as bindings only can have one language and one encoding.
2.1       frystyk   319: **  If more than one suffix is found they are all searched. The last suffix
                    320: **  has highest priority, the first one lowest. See also HTBind_getFormat()
                    321: */
2.24      frystyk   322: PUBLIC BOOL HTBind_getAnchorBindings (HTParentAnchor * anchor)
2.1       frystyk   323: {
                    324:     BOOL status = NO;
                    325:     double quality=1.0;                  /* @@@ Should we add this into the anchor? */
                    326:     if (anchor) {
2.27    ! frystyk   327:        char *addr = HTAnchor_address(anchor);
2.1       frystyk   328:        char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
2.9       frystyk   329:        char *file;
                    330:        char *end;
                    331:        if ((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
                    332:            (end = strchr(path, '#')))
                    333:            *end = '\0';
                    334:        if ((file = strrchr(path, '/'))) {
2.21      frystyk   335:            HTFormat format = NULL;
                    336:            HTEncoding encoding = NULL;
2.22      frystyk   337:            HTEncoding transfer = NULL;
2.21      frystyk   338:            HTLanguage language = NULL;
2.24      frystyk   339:            if (BIND_TRACE) HTTrace("Anchor...... Get bindings for `%s\'\n", path);
2.22      frystyk   340:            status = HTBind_getFormat(file, &format, &encoding, &transfer,
2.21      frystyk   341:                                      &language, &quality);
                    342:            if (status) {
                    343:                HTAnchor_setFormat(anchor, format);
2.25      frystyk   344:                HTAnchor_setContentTransferEncoding(anchor, transfer);
2.21      frystyk   345:                HTAnchor_addEncoding(anchor, encoding);
                    346:                HTAnchor_addLanguage(anchor, language);
                    347:            }
2.1       frystyk   348:        }
2.27    ! frystyk   349:         HT_FREE(addr);
        !           350:         HT_FREE(path);
2.1       frystyk   351:     }
                    352:     return status;
                    353: }
                    354: 
2.24      frystyk   355: PUBLIC BOOL HTBind_getResponseBindings (HTResponse * response, const char * url)
                    356: {
                    357:     BOOL status = NO;
                    358:     double quality = 1.0;
                    359:     if (response) {
                    360:        char * path = HTParse(url, "", PARSE_PATH + PARSE_PUNCTUATION);
                    361:        char * file;
                    362:        char * end;
                    363:        if ((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
                    364:            (end = strchr(path, '#')))
                    365:            *end = '\0';
                    366:        if ((file = strrchr(path, '/'))) {
                    367:            HTFormat format = NULL;
                    368:            HTEncoding encoding = NULL;
                    369:            HTEncoding transfer = NULL;
                    370:            HTLanguage language = NULL;
                    371:            if (BIND_TRACE) HTTrace("Response.... Get Bindings for `%s\'\n", path);
                    372:            status = HTBind_getFormat(file, &format, &encoding, &transfer,
                    373:                                      &language, &quality);
                    374:            if (status) {
                    375:                HTResponse_setFormat(response, format);
2.25      frystyk   376:                HTResponse_setContentTransferEncoding(response, transfer);
2.24      frystyk   377:                HTResponse_addEncoding(response, encoding);
                    378: #if 0
                    379:                HTResponse_addLanguage(response, language);
                    380: #endif
                    381:            }
                    382:        }
                    383:        HT_FREE(path);
                    384:     }
                    385:     return status;
                    386: }
2.1       frystyk   387: 
                    388: /*     Determine the content of an file name
                    389: **     -------------------------------------
                    390: **  Use the set of bindings to find the combination of language,
2.21      frystyk   391: **  media type, encoding, and transfer encoding  of a given anchor.
2.1       frystyk   392: **  If more than one suffix is found they are all searched. The last suffix
                    393: **  has highest priority, the first one lowest. See also HTBind_getBindings()
2.10      frystyk   394: **  Either of format, encoding, or language can be NULL
2.1       frystyk   395: **  Returns the format, encoding, and language found
                    396: */
2.21      frystyk   397: PUBLIC BOOL HTBind_getFormat (const char *     filename,
                    398:                              HTFormat *        format,
                    399:                              HTEncoding *      enc,
2.22      frystyk   400:                              HTEncoding *      cte,
2.21      frystyk   401:                              HTLanguage *      lang,
                    402:                              double *          quality)
2.1       frystyk   403: {
                    404:     int sufcnt=0;
                    405:     char *file=NULL;
2.6       frystyk   406: #ifdef HT_REENTRANT
                    407:     char *lasts;                                            /* For strtok_r */
                    408: #endif
2.1       frystyk   409:     if (*quality < HT_EPSILON)
                    410:        *quality = 1.0;                            /* Set to a neutral value */
                    411:     StrAllocCopy(file, filename);
                    412:     HTUnEscape(file);                             /* Unescape the file name */
2.6       frystyk   413: #ifdef HT_REENTRANT
                    414:     if (strtok_r(file, HTDelimiters, &lasts)) {         /* Do we have any suffixes? */
                    415: #else
2.1       frystyk   416:     if (strtok(file, HTDelimiters)) {           /* Do we have any suffixes? */
2.6       frystyk   417: #endif /* HT_REENTRANT */
2.1       frystyk   418:        char *suffix;
2.6       frystyk   419: #ifdef HT_REENTRANT
                    420:        while ((suffix=(char*)strtok_r(NULL, HTDelimiters, &lasts)) != NULL) {
                    421: #else
                    422:        while ((suffix=strtok(NULL, HTDelimiters)) != NULL) {
                    423: #endif /* HT_REENTRANT */
2.1       frystyk   424:            HTBind *suff=NULL;
                    425:            int hash=0;
                    426:            char *ptr=suffix;
                    427:            if (BIND_TRACE)
2.18      eric      428:                HTTrace("Get Binding. Look for '%s\' ", suffix);
2.1       frystyk   429:            sufcnt++;
                    430: 
                    431:            /* Select list from hash table */
                    432:            for( ; *ptr; ptr++)
                    433:                hash = (int)((hash*3+(*(unsigned char*)ptr)) % HASH_SIZE);
                    434: 
                    435:            /* Now search list for entries (case or non case sensitive) */
                    436:            if (HTBindings[hash]) {
                    437:                HTList *cur = HTBindings[hash];
                    438:                while ((suff = (HTBind *) HTList_nextObject(cur))) {
                    439:                    if ((HTCaseSen && !strcmp(suff->suffix, suffix)) ||
                    440:                        !strcasecomp(suff->suffix, suffix)) {
2.18      eric      441:                        if (BIND_TRACE) HTTrace("Found!\n");
2.10      frystyk   442:                        if (suff->type && format) *format = suff->type;
                    443:                        if (suff->encoding && enc) *enc = suff->encoding;
2.22      frystyk   444:                        if (suff->transfer && cte) *cte = suff->transfer;
2.10      frystyk   445:                        if (suff->language && lang) *lang = suff->language;
2.1       frystyk   446:                        if (suff->quality > HT_EPSILON)
                    447:                            *quality *= suff->quality;
                    448:                        break;
                    449:                    }
                    450:                }
                    451:            }
                    452:            if (!suff) {        /* We don't have this suffix - use default */
                    453:                if (BIND_TRACE)
2.18      eric      454:                    HTTrace("Not found - use default for \'*.*\'\n");
2.10      frystyk   455:                if (format) *format = unknown_suffix.type;
                    456:                if (enc) *enc = unknown_suffix.encoding;
2.21      frystyk   457:                if (cte) *cte = unknown_suffix.transfer;
2.10      frystyk   458:                if (lang) *lang = unknown_suffix.language;
2.1       frystyk   459:                *quality = unknown_suffix.quality;
                    460:            }
                    461:        } /* while we still have suffixes */
                    462:     }
                    463:     if (!sufcnt) {             /* No suffix so use default value */
                    464:        if (BIND_TRACE)
2.18      eric      465:            HTTrace("Get Binding. No suffix found - using default '%s\'\n", filename);
2.10      frystyk   466:        if (format) *format = no_suffix.type;
                    467:        if (enc) *enc = no_suffix.encoding;
2.21      frystyk   468:        if (cte) *cte = no_suffix.transfer;
2.10      frystyk   469:        if (lang) *lang = no_suffix.language;
2.1       frystyk   470:        *quality = no_suffix.quality;
                    471:     }
                    472:     if (BIND_TRACE)
2.21      frystyk   473:        HTTrace("Get Binding. Result for '%s\' is: type='%s\', encoding='%s\', cte='%s\', language='%s\' with quality %.2f\n",
2.1       frystyk   474:                filename,
2.10      frystyk   475:                (format && *format) ? HTAtom_name(*format) : "unknown",
                    476:                (enc && *enc) ? HTAtom_name(*enc) : "unknown",
2.21      frystyk   477:                (cte && *cte) ? HTAtom_name(*cte) : "unknown",
2.10      frystyk   478:                (lang && *lang) ? HTAtom_name(*lang) : "unknown",
2.1       frystyk   479:                *quality);
2.17      frystyk   480:     HT_FREE(file);
2.1       frystyk   481:     return YES;
                    482: }
                    483: 

Webmaster