Annotation of libwww/Library/src/HTBind.c, revision 2.30
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.30 ! frystyk 6: ** @(#) $Id: HTBind.c,v 2.29 1998/09/24 19:25:11 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;
2.30 ! frystyk 281: char * ct=NULL, * ce=NULL, * cl=NULL;
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.30 ! frystyk 291: ct = pres->suffix;
2.24 frystyk 292: } else if (!ce && pres->encoding && encoding) {
2.30 ! frystyk 293: HTList * cur_enc = encoding;
! 294: HTEncoding pres_enc;
! 295: while ((pres_enc = (HTEncoding) HTList_nextObject(cur_enc))) {
! 296: if (pres_enc == pres->encoding) {
! 297: ce = pres->suffix;
! 298: break;
! 299: }
! 300: }
2.24 frystyk 301: } else if (!cl && pres->language && language) {
2.30 ! frystyk 302: HTList * cur_lang = language;
! 303: HTLanguage pres_lang;
! 304: while ((pres_lang = (HTLanguage) HTList_nextObject(cur_lang))) {
! 305: if (pres_lang == pres->language) {
! 306: cl = pres->suffix;
! 307: break;
! 308: }
! 309: }
2.1 frystyk 310: }
311: }
312: }
2.30 ! frystyk 313: }
! 314:
! 315: /* Put the found suffixes together */
! 316: if (ct) {
! 317: HTChunk_putc(suffix, delimiter);
! 318: HTChunk_puts(suffix, ct);
! 319: }
! 320: if (ce) {
! 321: HTChunk_putc(suffix, delimiter);
! 322: HTChunk_puts(suffix, ce);
! 323: }
! 324: if (cl) {
! 325: HTChunk_putc(suffix, delimiter);
! 326: HTChunk_puts(suffix, cl);
2.1 frystyk 327: }
328: }
2.23 frystyk 329: return HTChunk_toCString(suffix);
2.1 frystyk 330: }
331:
2.24 frystyk 332: /*
2.1 frystyk 333: ** Use the set of bindings to find the combination of language,
2.24 frystyk 334: ** media type and encoding of a given object. This information can either be
335: ** stored in the anchor obejct or in the response object depending on which
336: ** function is called.
2.1 frystyk 337: **
2.21 frystyk 338: ** We comprise here as bindings only can have one language and one encoding.
2.1 frystyk 339: ** If more than one suffix is found they are all searched. The last suffix
340: ** has highest priority, the first one lowest. See also HTBind_getFormat()
341: */
2.24 frystyk 342: PUBLIC BOOL HTBind_getAnchorBindings (HTParentAnchor * anchor)
2.1 frystyk 343: {
344: BOOL status = NO;
345: double quality=1.0; /* @@@ Should we add this into the anchor? */
346: if (anchor) {
2.28 frystyk 347: char *addr = HTAnchor_address((HTAnchor *) anchor);
2.1 frystyk 348: char *path = HTParse(addr, "", PARSE_PATH+PARSE_PUNCTUATION);
2.9 frystyk 349: char *file;
350: char *end;
351: if ((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
352: (end = strchr(path, '#')))
353: *end = '\0';
354: if ((file = strrchr(path, '/'))) {
2.21 frystyk 355: HTFormat format = NULL;
356: HTEncoding encoding = NULL;
2.22 frystyk 357: HTEncoding transfer = NULL;
2.21 frystyk 358: HTLanguage language = NULL;
2.24 frystyk 359: if (BIND_TRACE) HTTrace("Anchor...... Get bindings for `%s\'\n", path);
2.22 frystyk 360: status = HTBind_getFormat(file, &format, &encoding, &transfer,
2.21 frystyk 361: &language, &quality);
362: if (status) {
363: HTAnchor_setFormat(anchor, format);
2.25 frystyk 364: HTAnchor_setContentTransferEncoding(anchor, transfer);
2.29 frystyk 365:
366: HTAnchor_deleteEncodingAll(anchor);
367: HTAnchor_addEncoding(anchor, encoding);
368:
369: HTAnchor_deleteLanguageAll(anchor);
370: HTAnchor_addLanguage(anchor, language);
2.21 frystyk 371: }
2.1 frystyk 372: }
2.27 frystyk 373: HT_FREE(addr);
374: HT_FREE(path);
2.1 frystyk 375: }
376: return status;
377: }
378:
2.24 frystyk 379: PUBLIC BOOL HTBind_getResponseBindings (HTResponse * response, const char * url)
380: {
381: BOOL status = NO;
382: double quality = 1.0;
383: if (response) {
384: char * path = HTParse(url, "", PARSE_PATH + PARSE_PUNCTUATION);
385: char * file;
386: char * end;
387: if ((end = strchr(path, ';')) || (end = strchr(path, '?')) ||
388: (end = strchr(path, '#')))
389: *end = '\0';
390: if ((file = strrchr(path, '/'))) {
391: HTFormat format = NULL;
392: HTEncoding encoding = NULL;
393: HTEncoding transfer = NULL;
394: HTLanguage language = NULL;
395: if (BIND_TRACE) HTTrace("Response.... Get Bindings for `%s\'\n", path);
396: status = HTBind_getFormat(file, &format, &encoding, &transfer,
397: &language, &quality);
398: if (status) {
399: HTResponse_setFormat(response, format);
2.25 frystyk 400: HTResponse_setContentTransferEncoding(response, transfer);
2.24 frystyk 401: HTResponse_addEncoding(response, encoding);
402: #if 0
403: HTResponse_addLanguage(response, language);
404: #endif
405: }
406: }
407: HT_FREE(path);
408: }
409: return status;
410: }
2.1 frystyk 411:
412: /* Determine the content of an file name
413: ** -------------------------------------
414: ** Use the set of bindings to find the combination of language,
2.21 frystyk 415: ** media type, encoding, and transfer encoding of a given anchor.
2.1 frystyk 416: ** If more than one suffix is found they are all searched. The last suffix
417: ** has highest priority, the first one lowest. See also HTBind_getBindings()
2.10 frystyk 418: ** Either of format, encoding, or language can be NULL
2.1 frystyk 419: ** Returns the format, encoding, and language found
420: */
2.21 frystyk 421: PUBLIC BOOL HTBind_getFormat (const char * filename,
422: HTFormat * format,
423: HTEncoding * enc,
2.22 frystyk 424: HTEncoding * cte,
2.21 frystyk 425: HTLanguage * lang,
426: double * quality)
2.1 frystyk 427: {
428: int sufcnt=0;
429: char *file=NULL;
2.6 frystyk 430: #ifdef HT_REENTRANT
431: char *lasts; /* For strtok_r */
432: #endif
2.1 frystyk 433: if (*quality < HT_EPSILON)
434: *quality = 1.0; /* Set to a neutral value */
435: StrAllocCopy(file, filename);
436: HTUnEscape(file); /* Unescape the file name */
2.6 frystyk 437: #ifdef HT_REENTRANT
438: if (strtok_r(file, HTDelimiters, &lasts)) { /* Do we have any suffixes? */
439: #else
2.1 frystyk 440: if (strtok(file, HTDelimiters)) { /* Do we have any suffixes? */
2.6 frystyk 441: #endif /* HT_REENTRANT */
2.1 frystyk 442: char *suffix;
2.6 frystyk 443: #ifdef HT_REENTRANT
444: while ((suffix=(char*)strtok_r(NULL, HTDelimiters, &lasts)) != NULL) {
445: #else
446: while ((suffix=strtok(NULL, HTDelimiters)) != NULL) {
447: #endif /* HT_REENTRANT */
2.1 frystyk 448: HTBind *suff=NULL;
449: int hash=0;
450: char *ptr=suffix;
451: if (BIND_TRACE)
2.18 eric 452: HTTrace("Get Binding. Look for '%s\' ", suffix);
2.1 frystyk 453: sufcnt++;
454:
455: /* Select list from hash table */
456: for( ; *ptr; ptr++)
457: hash = (int)((hash*3+(*(unsigned char*)ptr)) % HASH_SIZE);
458:
459: /* Now search list for entries (case or non case sensitive) */
460: if (HTBindings[hash]) {
461: HTList *cur = HTBindings[hash];
462: while ((suff = (HTBind *) HTList_nextObject(cur))) {
463: if ((HTCaseSen && !strcmp(suff->suffix, suffix)) ||
464: !strcasecomp(suff->suffix, suffix)) {
2.18 eric 465: if (BIND_TRACE) HTTrace("Found!\n");
2.10 frystyk 466: if (suff->type && format) *format = suff->type;
467: if (suff->encoding && enc) *enc = suff->encoding;
2.22 frystyk 468: if (suff->transfer && cte) *cte = suff->transfer;
2.10 frystyk 469: if (suff->language && lang) *lang = suff->language;
2.1 frystyk 470: if (suff->quality > HT_EPSILON)
471: *quality *= suff->quality;
472: break;
473: }
474: }
475: }
476: if (!suff) { /* We don't have this suffix - use default */
477: if (BIND_TRACE)
2.18 eric 478: HTTrace("Not found - use default for \'*.*\'\n");
2.10 frystyk 479: if (format) *format = unknown_suffix.type;
480: if (enc) *enc = unknown_suffix.encoding;
2.21 frystyk 481: if (cte) *cte = unknown_suffix.transfer;
2.10 frystyk 482: if (lang) *lang = unknown_suffix.language;
2.1 frystyk 483: *quality = unknown_suffix.quality;
484: }
485: } /* while we still have suffixes */
486: }
487: if (!sufcnt) { /* No suffix so use default value */
488: if (BIND_TRACE)
2.18 eric 489: HTTrace("Get Binding. No suffix found - using default '%s\'\n", filename);
2.10 frystyk 490: if (format) *format = no_suffix.type;
491: if (enc) *enc = no_suffix.encoding;
2.21 frystyk 492: if (cte) *cte = no_suffix.transfer;
2.10 frystyk 493: if (lang) *lang = no_suffix.language;
2.1 frystyk 494: *quality = no_suffix.quality;
495: }
496: if (BIND_TRACE)
2.21 frystyk 497: HTTrace("Get Binding. Result for '%s\' is: type='%s\', encoding='%s\', cte='%s\', language='%s\' with quality %.2f\n",
2.1 frystyk 498: filename,
2.10 frystyk 499: (format && *format) ? HTAtom_name(*format) : "unknown",
500: (enc && *enc) ? HTAtom_name(*enc) : "unknown",
2.21 frystyk 501: (cte && *cte) ? HTAtom_name(*cte) : "unknown",
2.10 frystyk 502: (lang && *lang) ? HTAtom_name(*lang) : "unknown",
2.1 frystyk 503: *quality);
2.17 frystyk 504: HT_FREE(file);
2.1 frystyk 505: return YES;
506: }
507:
Webmaster