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