Annotation of libwww/Library/src/HTFormat.c, revision 1.24
1.7 secret 1:
1.1 timbl 2: /* Manage different file formats HTFormat.c
3: ** =============================
4: **
5: ** Bugs:
6: ** Not reentrant.
7: **
8: ** Assumes the incoming stream is ASCII, rather than a local file
9: ** format, and so ALWAYS converts from ASCII on non-ASCII machines.
10: ** Therefore, non-ASCII machines can't read local files.
1.2 timbl 11: **
12: */
13:
1.10 timbl 14:
1.2 timbl 15: /* Implements:
1.1 timbl 16: */
1.2 timbl 17: #include "HTFormat.h"
18:
19: PUBLIC float HTMaxSecs = 1e10; /* No effective limit */
20: PUBLIC float HTMaxLength = 1e10; /* No effective limit */
21:
22: #ifdef unix
23: #ifdef NeXT
24: #define PRESENT_POSTSCRIPT "open %s; /bin/rm -f %s\n"
25: #else
26: #define PRESENT_POSTSCRIPT "(ghostview %s ; /bin/rm -f %s)&\n"
27: /* Full pathname would be better! */
28: #endif
29: #endif
30:
1.1 timbl 31:
32: #include "HTUtils.h"
33: #include "tcp.h"
34:
35: #include "HTML.h"
1.12 timbl 36: #include "HTMLPDTD.h"
1.1 timbl 37: #include "HText.h"
1.2 timbl 38: #include "HTAlert.h"
39: #include "HTList.h"
40: #include "HTInit.h"
41: /* Streams and structured streams which we use:
42: */
43: #include "HTFWriter.h"
44: #include "HTPlain.h"
45: #include "SGML.h"
46: #include "HTML.h"
47: #include "HTMLGen.h"
48:
49: PUBLIC BOOL HTOutputSource = NO; /* Flag: shortcut parser to stdout */
50: extern BOOL interactive;
51:
1.10 timbl 52: #ifdef ORIGINAL
1.2 timbl 53: struct _HTStream {
54: CONST HTStreamClass* isa;
55: /* ... */
56: };
1.10 timbl 57: #endif
58:
59: /* this version used by the NetToText stream */
60: struct _HTStream {
61: CONST HTStreamClass * isa;
62: BOOL had_cr;
63: HTStream * sink;
64: };
1.2 timbl 65:
66:
1.17 luotonen 67: /*
68: ** Accept-Encoding and Accept-Language
69: */
70: typedef struct _HTAcceptNode {
71: HTAtom * atom;
72: float quality;
73: } HTAcceptNode;
74:
75:
76:
77:
1.2 timbl 78: /* Presentation methods
79: ** --------------------
80: */
81:
1.14 timbl 82: PUBLIC HTList * HTConversions = NULL;
1.2 timbl 83:
84:
85: /* Define a presentation system command for a content-type
86: ** -------------------------------------------------------
87: */
1.12 timbl 88: PUBLIC void HTSetPresentation ARGS6(
89: HTList *, conversions,
90: CONST char *, representation,
91: CONST char *, command,
92: float, quality,
93: float, secs,
94: float, secs_per_byte
1.2 timbl 95: ){
96:
97: HTPresentation * pres = (HTPresentation *)malloc(sizeof(HTPresentation));
98: if (pres == NULL) outofmem(__FILE__, "HTSetPresentation");
99:
100: pres->rep = HTAtom_for(representation);
101: pres->rep_out = WWW_PRESENT; /* Fixed for now ... :-) */
102: pres->converter = HTSaveAndExecute; /* Fixed for now ... */
103: pres->quality = quality;
104: pres->secs = secs;
105: pres->secs_per_byte = secs_per_byte;
106: pres->rep = HTAtom_for(representation);
107: pres->command = 0;
108: StrAllocCopy(pres->command, command);
109:
1.12 timbl 110: /* if (!HTPresentations) HTPresentations = HTList_new(); */
1.2 timbl 111:
1.15 luotonen 112: #ifdef OLD_CODE
113: if (strcmp(representation, "*")==0) {
1.2 timbl 114: if (default_presentation) free(default_presentation);
115: default_presentation = pres;
1.12 timbl 116: } else
117: #endif
118: HTList_addObject(conversions, pres);
1.2 timbl 119: }
120:
121:
122: /* Define a built-in function for a content-type
123: ** ---------------------------------------------
124: */
1.12 timbl 125: PUBLIC void HTSetConversion ARGS7(
126: HTList *, conversions,
127: CONST char *, representation_in,
128: CONST char *, representation_out,
1.6 timbl 129: HTConverter*, converter,
1.12 timbl 130: float, quality,
131: float, secs,
132: float, secs_per_byte
1.2 timbl 133: ){
1.1 timbl 134:
1.2 timbl 135: HTPresentation * pres = (HTPresentation *)malloc(sizeof(HTPresentation));
136: if (pres == NULL) outofmem(__FILE__, "HTSetPresentation");
137:
138: pres->rep = HTAtom_for(representation_in);
139: pres->rep_out = HTAtom_for(representation_out);
140: pres->converter = converter;
141: pres->command = NULL; /* Fixed */
142: pres->quality = quality;
143: pres->secs = secs;
144: pres->secs_per_byte = secs_per_byte;
145: pres->command = 0;
146:
1.12 timbl 147: /* if (!HTPresentations) HTPresentations = HTList_new(); */
1.2 timbl 148:
1.12 timbl 149: #ifdef OLD_CODE
1.2 timbl 150: if (strcmp(representation_in, "*")==0) {
151: if (default_presentation) free(default_presentation);
152: default_presentation = pres;
1.12 timbl 153: } else
154: #endif
155: HTList_addObject(conversions, pres);
1.2 timbl 156: }
1.1 timbl 157:
158:
159:
1.17 luotonen 160: PUBLIC void HTAcceptEncoding ARGS3(HTList *, list,
161: char *, enc,
162: float, quality)
163: {
164: HTAcceptNode * node;
165: char * cur;
166:
167: if (!list || !enc || !*enc) return;
168:
169: for(cur=enc; *cur; cur++) *cur=TOLOWER(*cur);
170:
171: node = (HTAcceptNode*)calloc(1, sizeof(HTAcceptNode));
172: if (!node) outofmem(__FILE__, "HTAcceptEncoding");
173: HTList_addObject(list, (void*)node);
174:
175: node->atom = HTAtom_for(enc);
176: node->quality = quality;
177: }
178:
179:
180: PUBLIC void HTAcceptLanguage ARGS3(HTList *, list,
181: char *, lang,
182: float, quality)
183: {
184: HTAcceptNode * node;
185:
186: if (!list || !lang || !*lang) return;
187:
188: node = (HTAcceptNode*)calloc(1, sizeof(HTAcceptNode));
189: if (!node) outofmem(__FILE__, "HTAcceptLanguage");
190:
191: HTList_addObject(list, (void*)node);
192: node->atom = HTAtom_for(lang);
193: node->quality = quality;
194: }
195:
196:
197: PRIVATE BOOL wild_match ARGS2(HTAtom *, template,
198: HTAtom *, actual)
199: {
200: char *t, *a, *st, *sa;
201: BOOL match = NO;
202:
1.22 luotonen 203: if (template && actual && (t = HTAtom_name(template))) {
204: if (!strcmp(t, "*"))
205: return YES;
1.17 luotonen 206:
1.22 luotonen 207: if (strchr(t, '*') &&
208: (a = HTAtom_name(actual)) &&
209: (st = strchr(t, '/')) && (sa = strchr(a,'/'))) {
1.17 luotonen 210:
1.22 luotonen 211: *sa = 0;
212: *st = 0;
213:
214: if ((*(st-1)=='*' &&
215: (*(st+1)=='*' || !strcasecomp(st+1, sa+1))) ||
216: (*(st+1)=='*' && !strcasecomp(t,a)))
217: match = YES;
218:
219: *sa = '/';
220: *st = '/';
221: }
222: }
1.23 luotonen 223: return match;
1.17 luotonen 224: }
225:
226:
227: PRIVATE float type_value ARGS2(HTAtom *, content_type,
228: HTList *, accepted)
229: {
230: HTList * cur = accepted;
231: HTPresentation * pres;
232: HTPresentation * wild = NULL;
233:
234: if (!content_type || !accepted) return -1;
235:
236: while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
237: if (pres->rep == content_type)
238: return pres->quality;
239: else if (wild_match(pres->rep, content_type))
240: wild = pres;
241: }
242: if (wild) return wild->quality;
243: else return -1;
244: }
245:
246:
247: PRIVATE float lang_value ARGS2(HTAtom *, language,
248: HTList *, accepted)
249: {
250: HTList * cur = accepted;
251: HTAcceptNode * node;
252: HTAcceptNode * wild = NULL;
253:
254: if (!language || !accepted || HTList_isEmpty(accepted)) {
255: return 0.1;
256: }
257:
258: while ((node = (HTAcceptNode*)HTList_nextObject(cur))) {
259: if (node->atom == language) {
260: return node->quality;
261: }
262: else if (wild_match(node->atom, language)) {
263: wild = node;
264: }
265: }
266:
267: if (wild) {
268: return wild->quality;
269: }
270: else {
271: return 0.1;
272: }
273: }
274:
275:
276: PRIVATE float encoding_value ARGS2(HTAtom *, encoding,
277: HTList *, accepted)
278: {
279: HTList * cur = accepted;
280: HTAcceptNode * node;
281: HTAcceptNode * wild = NULL;
282: char * e;
283:
284: if (!encoding || !accepted || HTList_isEmpty(accepted))
285: return 1;
286:
287: e = HTAtom_name(encoding);
288: if (!strcmp(e, "7bit") || !strcmp(e, "8bit") || !strcmp(e, "binary"))
289: return 1;
290:
291: while ((node = (HTAcceptNode*)HTList_nextObject(cur))) {
292: if (node->atom == encoding)
293: return node->quality;
294: else if (wild_match(node->atom, encoding))
295: wild = node;
296: }
297: if (wild) return wild->quality;
298: else return 1;
299: }
300:
301:
302: PUBLIC BOOL HTRank ARGS4(HTList *, possibilities,
303: HTList *, accepted_content_types,
304: HTList *, accepted_languages,
305: HTList *, accepted_encodings)
306: {
307: int accepted_cnt = 0;
308: HTList * accepted;
309: HTList * sorted;
310: HTList * cur;
311: HTContentDescription * d;
312:
313: if (!possibilities) return NO;
314:
315: accepted = HTList_new();
316: cur = possibilities;
317: while ((d = (HTContentDescription*)HTList_nextObject(cur))) {
318: float tv = type_value(d->content_type, accepted_content_types);
319: float lv = lang_value(d->content_language, accepted_languages);
320: float ev = encoding_value(d->content_encoding, accepted_encodings);
321:
322: #ifdef ARI_DEBUG
323: CTRACE(stderr,
324: " ## FOR FILE \"%s\" (%.3f) VALUES type %.3f enc %.3f lang %.3f\n",
325: d->filename, d->quality, tv, ev, lv);
326: #endif
327:
328: if (tv > 0) {
329: d->quality *= tv * lv * ev;
330: HTList_addObject(accepted, d);
331: accepted_cnt++;
332: }
1.18 luotonen 333: else {
334: if (d->filename) free(d->filename);
335: free(d);
336: }
1.17 luotonen 337: }
338:
1.18 luotonen 339: CTRACE(stderr, "Ranking.....\n");
1.17 luotonen 340: CTRACE(stderr,
1.18 luotonen 341: "\nRANK QUALITY CONTENT-TYPE LANGUAGE ENCODING FILE\n");
1.17 luotonen 342:
343: sorted = HTList_new();
344: while (accepted_cnt-- > 0) {
345: HTContentDescription * worst = NULL;
346: cur = accepted;
347: while ((d = (HTContentDescription*)HTList_nextObject(cur))) {
348: if (!worst || d->quality < worst->quality)
349: worst = d;
350: }
351: if (worst) {
352: CTRACE(stderr, "%d. %.4f %-20.20s %-8.8s %-10.10s %s\n",
353: accepted_cnt+1,
354: worst->quality,
355: (worst->content_type
356: ? HTAtom_name(worst->content_type) : "-"),
357: (worst->content_language
358: ? HTAtom_name(worst->content_language) :"-"),
359: (worst->content_encoding
360: ? HTAtom_name(worst->content_encoding) :"-"),
361: (worst->filename
362: ? worst->filename :"-"));
363: HTList_removeObject(accepted, (void*)worst);
364: HTList_addObject(sorted, (void*)worst);
365: }
366: }
1.18 luotonen 367: CTRACE(stderr, "\n");
1.17 luotonen 368: HTList_delete(accepted);
369: HTList_delete(possibilities->next);
370: possibilities->next = sorted->next;
371: sorted->next = NULL;
372: HTList_delete(sorted);
373:
374: if (!HTList_isEmpty(possibilities)) return YES;
375: else return NO;
376: }
377:
378:
379:
380:
381:
1.13 timbl 382: /* Socket Input Buffering
383: ** ----------------------
1.1 timbl 384: **
1.13 timbl 385: ** This code is used because one cannot in general open a
386: ** file descriptor for a socket.
387: **
1.1 timbl 388: ** The input file is read using the macro which can read from
1.13 timbl 389: ** a socket or a file, but this should not be used for files
390: ** as fopen() etc is more portable of course.
391: **
1.1 timbl 392: ** The input buffer size, if large will give greater efficiency and
393: ** release the server faster, and if small will save space on PCs etc.
394: */
395:
396:
397: /* Set up the buffering
398: **
399: ** These routines are public because they are in fact needed by
400: ** many parsers, and on PCs and Macs we should not duplicate
401: ** the static buffer area.
402: */
1.13 timbl 403: PUBLIC HTInputSocket * HTInputSocket_new ARGS1 (int,file_number)
1.1 timbl 404: {
1.13 timbl 405: HTInputSocket *isoc = (HTInputSocket *)malloc(sizeof(*isoc));
406: if (!isoc) outofmem(__FILE__, "HTInputSocket_new");
407: isoc->input_file_number = file_number;
408: isoc->input_pointer = isoc->input_limit = isoc->input_buffer;
409: return isoc;
1.1 timbl 410: }
411:
412:
1.13 timbl 413: PUBLIC char HTInputSocket_getCharacter ARGS1(HTInputSocket*, isoc)
1.1 timbl 414: {
415: char ch;
416: do {
1.13 timbl 417: if (isoc-> input_pointer >= isoc->input_limit) {
1.1 timbl 418: int status = NETREAD(
1.13 timbl 419: isoc->input_file_number,
420: isoc->input_buffer, INPUT_BUFFER_SIZE);
1.1 timbl 421: if (status <= 0) {
422: if (status == 0) return (char)EOF;
423: if (TRACE) fprintf(stderr,
424: "HTFormat: File read error %d\n", status);
425: return (char)EOF; /* -1 is returned by UCX at end of HTTP link */
426: }
1.13 timbl 427: isoc-> input_pointer = isoc->input_buffer;
428: isoc->input_limit = isoc->input_buffer + status;
1.1 timbl 429: }
1.13 timbl 430: ch = *isoc-> input_pointer++;
1.1 timbl 431: } while (ch == (char) 13); /* Ignore ASCII carriage return */
432:
433: return FROMASCII(ch);
434: }
435:
1.17 luotonen 436: PUBLIC void HTInputSocket_free ARGS1(HTInputSocket *, me)
1.13 timbl 437: {
438: if (me) free(me);
439: }
440:
441:
1.16 luotonen 442: PUBLIC char * HTInputSocket_getBlock ARGS2(HTInputSocket*, isoc,
443: int *, len)
444: {
445: if (isoc->input_pointer >= isoc->input_limit) {
446: int status = NETREAD(isoc->input_file_number,
447: isoc->input_buffer,
448: ((*len < INPUT_BUFFER_SIZE) ?
449: *len : INPUT_BUFFER_SIZE));
450: if (status <= 0) {
451: isoc->input_limit = isoc->input_buffer;
452: if (status < 0)
453: CTRACE(stderr, "HTInputSocket: File read error %d\n", status);
454: *len = 0;
455: return NULL;
456: }
457: else {
458: *len = status;
459: return isoc->input_buffer;
460: }
461: }
462: else {
463: char * ret = isoc->input_pointer;
464: *len = isoc->input_limit - isoc->input_pointer;
465: isoc->input_pointer = isoc->input_limit;
466: return ret;
467: }
468: }
469:
470:
1.15 luotonen 471: PRIVATE int fill_in_buffer ARGS1(HTInputSocket *, isoc)
472: {
473: if (isoc) {
474: int status;
475:
476: isoc->input_pointer = isoc->input_buffer;
477: status = NETREAD(isoc->input_file_number,
478: isoc->input_buffer,
479: INPUT_BUFFER_SIZE);
480: if (status <= 0) {
481: isoc->input_limit = isoc->input_buffer;
482: if (status < 0)
483: if (TRACE) fprintf(stderr,
484: "HTInputSocket: File read error %d\n",
485: status);
486: }
487: else
488: isoc->input_limit = isoc->input_buffer + status;
489: return status;
490: }
491: return -1;
492: }
493:
494:
495: PRIVATE void ascii_cat ARGS3(char **, linep,
496: char *, start,
497: char *, end)
498: {
499: if (linep && start && end && start <= end) {
500: char *ptr;
501:
502: if (*linep) {
503: int len = strlen(*linep);
504: *linep = (char*)realloc(*linep, len + end-start + 1);
505: ptr = *linep + len;
506: }
507: else {
508: ptr = *linep = (char*)malloc(end-start + 1);
509: }
510:
511: while (start < end) {
512: *ptr = FROMASCII(*start);
513: ptr++;
514: start++;
515: }
516: *ptr = 0;
517: }
518: }
519:
520:
521: PRIVATE char * get_some_line ARGS2(HTInputSocket *, isoc,
522: BOOL, unfold)
523: {
524: if (!isoc)
525: return NULL;
526: else {
527: BOOL check_unfold = NO;
528: int prev_cr = 0;
529: char *start = isoc->input_pointer;
530: char *cur = isoc->input_pointer;
531: char * line = NULL;
532:
533: for(;;) {
534: /*
535: ** Get more if needed to complete line
536: */
537: if (cur >= isoc->input_limit) { /* Need more data */
538: ascii_cat(&line, start, cur);
539: if (fill_in_buffer(isoc) <= 0)
540: return line;
541: start = cur = isoc->input_pointer;
542: } /* if need more data */
543:
544: /*
545: ** Find a line feed if there is one
546: */
547: for(; cur < isoc->input_limit; cur++) {
548: char c = FROMASCII(*cur);
549: if (!c) {
1.18 luotonen 550: if (line) free(line); /* Leak fixed AL 6 Feb 94 */
1.15 luotonen 551: return NULL; /* Panic! read a 0! */
552: }
553: if (check_unfold && c != ' ' && c != '\t') {
554: return line; /* Note: didn't update isoc->input_pointer */
555: }
556: else {
557: check_unfold = NO;
558: }
559:
560: if (c=='\r') {
561: prev_cr = 1;
562: }
563: else {
564: if (c=='\n') { /* Found a line feed */
565: ascii_cat(&line, start, cur-prev_cr);
566: start = isoc->input_pointer = cur+1;
567:
568: if (line && strlen(line) > 0 && unfold) {
569: check_unfold = YES;
570: }
571: else {
572: return line;
573: }
574: } /* if NL */
575: /* else just a regular character */
576: prev_cr = 0;
577: } /* if not CR */
578: } /* while characters in buffer remain */
579: } /* until line read or end-of-file */
580: } /* valid parameters to function */
581: }
582:
583:
584: PUBLIC char * HTInputSocket_getLine ARGS1(HTInputSocket *, isoc)
585: {
586: return get_some_line(isoc, NO);
587: }
588:
589: PUBLIC char * HTInputSocket_getUnfoldedLine ARGS1(HTInputSocket *, isoc)
590: {
591: return get_some_line(isoc, YES);
592: }
593:
594:
595: /*
596: ** Read HTTP status line (if there is one).
597: **
598: ** Kludge to trap binary responses from illegal HTTP0.9 servers.
599: ** First look at the stub in ASCII and check if it starts "HTTP/".
600: **
601: ** Bugs: A HTTP0.9 server returning a document starting "HTTP/"
602: ** will be taken as a HTTP 1.0 server. Failure.
603: */
604: #define STUB_LENGTH 20
605: PUBLIC char * HTInputSocket_getStatusLine ARGS1(HTInputSocket *, isoc)
606: {
607: if (!isoc) {
608: return NULL;
609: }
610: else {
611: char buf[STUB_LENGTH + 1];
612: int i;
613: char server_version[STUB_LENGTH+1];
614: int server_status;
615:
616: /*
617: ** Read initial buffer
618: */
619: if (isoc->input_pointer >= isoc->input_limit &&
620: fill_in_buffer(isoc) <= 0) {
621: return NULL;
622: }
623:
624: for (i=0; i < STUB_LENGTH; i++)
625: buf[i] = FROMASCII(isoc->input_buffer[i]);
626: buf[STUB_LENGTH] = 0;
627:
628: if (0 != strncmp(buf, "HTTP/", 5) ||
629: sscanf(buf, "%20s%d", server_version, &server_status) < 2)
630: return NULL;
631: else
632: return get_some_line(isoc, NO);
633: }
634: }
635:
636:
637: /*
638: ** Do heuristic test to see if this is binary.
639: **
640: ** We check for characters above 128 in the first few bytes, and
641: ** if we find them we forget the html default.
642: ** Kludge to trap binary responses from illegal HTTP0.9 servers.
643: **
644: ** Bugs: An HTTP 0.9 server returning a binary document with
645: ** characters < 128 will be read as ASCII.
646: */
647: PUBLIC BOOL HTInputSocket_seemsBinary ARGS1(HTInputSocket *, isoc)
648: {
649: if (isoc &&
650: (isoc->input_pointer < isoc->input_limit ||
651: fill_in_buffer(isoc) > 0)) {
652: char *p = isoc->input_buffer;
653: int i = STUB_LENGTH;
654:
655: for( ; i && p < isoc->input_limit; p++, i++)
656: if (((int)*p)&128)
657: return YES;
658: }
659: return NO;
660: }
661:
662:
663:
1.1 timbl 664: /* Stream the data to an ouput file as binary
665: */
1.13 timbl 666: PUBLIC int HTOutputBinary ARGS3( HTInputSocket *, isoc,
667: int, input,
668: FILE *, output)
1.1 timbl 669: {
670: do {
671: int status = NETREAD(
1.13 timbl 672: input, isoc->input_buffer, INPUT_BUFFER_SIZE);
1.1 timbl 673: if (status <= 0) {
674: if (status == 0) return 0;
675: if (TRACE) fprintf(stderr,
676: "HTFormat: File read error %d\n", status);
677: return 2; /* Error */
678: }
1.13 timbl 679: fwrite(isoc->input_buffer, sizeof(char), status, output);
1.1 timbl 680: } while (YES);
681: }
682:
683:
1.17 luotonen 684:
1.2 timbl 685: /* Create a filter stack
686: ** ---------------------
687: **
1.7 secret 688: ** If a wildcard match is made, a temporary HTPresentation
1.2 timbl 689: ** structure is made to hold the destination format while the
690: ** new stack is generated. This is just to pass the out format to
691: ** MIME so far. Storing the format of a stream in the stream might
692: ** be a lot neater.
1.10 timbl 693: **
694: ** The www/source format is special, in that if you can take
695: ** that you can take anything. However, we
1.2 timbl 696: */
1.12 timbl 697: PUBLIC HTStream * HTStreamStack ARGS2(
1.10 timbl 698: HTFormat, rep_in,
1.12 timbl 699: HTRequest *, request)
1.2 timbl 700: {
1.12 timbl 701: HTFormat rep_out = request->output_format; /* Could be a param */
1.14 timbl 702: HTList * conversion[2];
1.10 timbl 703: HTFormat source = WWW_SOURCE;
1.14 timbl 704: int which_list;
705: HTPresentation * pres, *match, *wildcard_match=0,
706: *source_match=0, *source_wildcard_match=0;
707:
1.2 timbl 708: if (TRACE) fprintf(stderr,
709: "HTFormat: Constructing stream stack for %s to %s\n",
1.10 timbl 710: HTAtom_name(rep_in),
1.2 timbl 711: HTAtom_name(rep_out));
712:
1.21 luotonen 713: if (rep_out == WWW_SOURCE || rep_out == rep_in)
714: return request->output_stream;
1.2 timbl 715:
1.14 timbl 716: conversion[0] = request->conversions;
717: conversion[1] = HTConversions;
1.17 luotonen 718:
1.15 luotonen 719: for(which_list = 0; which_list<2; which_list++) {
720: HTList * cur = conversion[which_list];
721:
722: while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
1.17 luotonen 723: if (pres->rep == rep_in ||
724: wild_match(pres->rep, rep_in)) {
1.2 timbl 725: if (pres->rep_out == rep_out)
1.12 timbl 726: return (*pres->converter)(request, pres->command,
1.15 luotonen 727: rep_in, pres->rep_out,
728: request->output_stream);
1.17 luotonen 729: if (wild_match(pres->rep_out, rep_out)) {
1.10 timbl 730: wildcard_match = pres;
731: }
732: }
733: if (pres->rep == source) {
734: if (pres->rep_out == rep_out)
735: source_match = pres;
1.17 luotonen 736: if (wild_match(pres->rep_out, rep_out)) {
1.10 timbl 737: source_wildcard_match = pres;
1.2 timbl 738: }
739: }
740: }
741: }
1.14 timbl 742: match = wildcard_match ? wildcard_match :
743: source_match ? source_match :
744: source_wildcard_match;
745:
746: if (match) return (*match->converter)(
747: request, match->command, rep_in, rep_out,
748: request->output_stream);
1.10 timbl 749:
1.2 timbl 750: return NULL;
751: }
752:
753:
754: /* Find the cost of a filter stack
755: ** -------------------------------
756: **
757: ** Must return the cost of the same stack which StreamStack would set up.
758: **
759: ** On entry,
760: ** length The size of the data to be converted
761: */
1.12 timbl 762: PUBLIC float HTStackValue ARGS5(
1.14 timbl 763: HTList *, theseConversions,
1.10 timbl 764: HTFormat, rep_in,
1.2 timbl 765: HTFormat, rep_out,
766: float, initial_value,
767: long int, length)
768: {
1.14 timbl 769: int which_list;
770: HTList* conversion[2];
771:
1.2 timbl 772: if (TRACE) fprintf(stderr,
773: "HTFormat: Evaluating stream stack for %s worth %.3f to %s\n",
1.10 timbl 774: HTAtom_name(rep_in), initial_value,
1.2 timbl 775: HTAtom_name(rep_out));
776:
777: if (rep_out == WWW_SOURCE ||
1.10 timbl 778: rep_out == rep_in) return 0.0;
1.2 timbl 779:
1.12 timbl 780: /* if (!HTPresentations) HTFormatInit(); set up the list */
1.2 timbl 781:
1.14 timbl 782: conversion[0] = theseConversions;
783: conversion[1] = HTConversions;
784:
785: for(which_list = 0; which_list<2; which_list++)
786: if (conversion[which_list]) {
1.15 luotonen 787: HTList * cur = conversion[which_list];
1.2 timbl 788: HTPresentation * pres;
1.15 luotonen 789: while ((pres = (HTPresentation*)HTList_nextObject(cur))) {
790: if (pres->rep == rep_in &&
1.17 luotonen 791: (pres->rep_out == rep_out || wild_match(pres->rep_out, rep_out))) {
1.2 timbl 792: float value = initial_value * pres->quality;
793: if (HTMaxSecs != 0.0)
1.15 luotonen 794: value = value - (length*pres->secs_per_byte + pres->secs)
1.2 timbl 795: /HTMaxSecs;
796: return value;
797: }
798: }
799: }
800:
801: return -1e30; /* Really bad */
1.17 luotonen 802: }
803:
804:
1.2 timbl 805:
1.1 timbl 806:
1.2 timbl 807: /* Push data from a socket down a stream
808: ** -------------------------------------
1.1 timbl 809: **
1.2 timbl 810: ** This routine is responsible for creating and PRESENTING any
1.1 timbl 811: ** graphic (or other) objects described by the file.
1.2 timbl 812: **
813: ** The file number given is assumed to be a TELNET stream ie containing
814: ** CRLF at the end of lines which need to be stripped to LF for unix
815: ** when the format is textual.
816: **
1.1 timbl 817: */
1.2 timbl 818: PUBLIC void HTCopy ARGS2(
819: int, file_number,
820: HTStream*, sink)
1.1 timbl 821: {
1.2 timbl 822: HTStreamClass targetClass;
1.13 timbl 823: HTInputSocket * isoc;
1.2 timbl 824:
1.5 timbl 825: /* Push the data down the stream
1.2 timbl 826: **
827: */
828: targetClass = *(sink->isa); /* Copy pointers to procedures */
1.13 timbl 829: isoc = HTInputSocket_new(file_number);
1.2 timbl 830:
831: /* Push binary from socket down sink
1.10 timbl 832: **
833: ** This operation could be put into a main event loop
1.2 timbl 834: */
835: for(;;) {
836: int status = NETREAD(
1.13 timbl 837: file_number, isoc->input_buffer, INPUT_BUFFER_SIZE);
1.2 timbl 838: if (status <= 0) {
839: if (status == 0) break;
840: if (TRACE) fprintf(stderr,
1.24 ! luotonen 841: "HTFormat: Read error, read returns %d with errno=%d\n",
! 842: status, errno);
1.2 timbl 843: break;
844: }
1.8 timbl 845:
846: #ifdef NOT_ASCII
847: {
848: char * p;
1.13 timbl 849: for(p = isoc->input_buffer; p < isoc->input_buffer+status; p++) {
1.8 timbl 850: *p = FROMASCII(*p);
851: }
852: }
853: #endif
854:
1.13 timbl 855: (*targetClass.put_block)(sink, isoc->input_buffer, status);
1.2 timbl 856: } /* next bufferload */
1.13 timbl 857: HTInputSocket_free(isoc);
1.2 timbl 858: }
859:
1.1 timbl 860:
1.7 secret 861:
862: /* Push data from a file pointer down a stream
863: ** -------------------------------------
864: **
865: ** This routine is responsible for creating and PRESENTING any
866: ** graphic (or other) objects described by the file.
867: **
868: **
869: */
870: PUBLIC void HTFileCopy ARGS2(
871: FILE *, fp,
872: HTStream*, sink)
873: {
874: HTStreamClass targetClass;
1.13 timbl 875: char input_buffer[INPUT_BUFFER_SIZE];
1.7 secret 876:
877: /* Push the data down the stream
878: **
879: */
880: targetClass = *(sink->isa); /* Copy pointers to procedures */
881:
882: /* Push binary from socket down sink
883: */
884: for(;;) {
885: int status = fread(
886: input_buffer, 1, INPUT_BUFFER_SIZE, fp);
887: if (status == 0) { /* EOF or error */
888: if (ferror(fp) == 0) break;
889: if (TRACE) fprintf(stderr,
890: "HTFormat: Read error, read returns %d\n", ferror(fp));
891: break;
892: }
893: (*targetClass.put_block)(sink, input_buffer, status);
1.13 timbl 894: } /* next bufferload */
1.7 secret 895: }
896:
897:
898:
899:
1.2 timbl 900: /* Push data from a socket down a stream STRIPPING CR
901: ** --------------------------------------------------
902: **
903: ** This routine is responsible for creating and PRESENTING any
1.8 timbl 904: ** graphic (or other) objects described by the socket.
1.2 timbl 905: **
906: ** The file number given is assumed to be a TELNET stream ie containing
907: ** CRLF at the end of lines which need to be stripped to LF for unix
908: ** when the format is textual.
909: **
1.1 timbl 910: */
1.2 timbl 911: PUBLIC void HTCopyNoCR ARGS2(
912: int, file_number,
913: HTStream*, sink)
914: {
1.13 timbl 915: HTStreamClass targetClass;
916: HTInputSocket * isoc;
1.1 timbl 917:
1.2 timbl 918: /* Push the data, ignoring CRLF, down the stream
919: **
920: */
921: targetClass = *(sink->isa); /* Copy pointers to procedures */
922:
923: /* Push text from telnet socket down sink
924: **
925: ** @@@@@ To push strings could be faster? (especially is we
926: ** cheat and don't ignore CR! :-}
927: */
1.13 timbl 928: isoc = HTInputSocket_new(file_number);
1.2 timbl 929: for(;;) {
930: char character;
1.13 timbl 931: character = HTInputSocket_getCharacter(isoc);
1.2 timbl 932: if (character == (char)EOF) break;
933: (*targetClass.put_character)(sink, character);
934: }
1.13 timbl 935: HTInputSocket_free(isoc);
1.2 timbl 936: }
1.1 timbl 937:
1.2 timbl 938:
1.7 secret 939:
1.2 timbl 940: /* Parse a socket given format and file number
941: **
942: ** This routine is responsible for creating and PRESENTING any
943: ** graphic (or other) objects described by the file.
944: **
945: ** The file number given is assumed to be a TELNET stream ie containing
946: ** CRLF at the end of lines which need to be stripped to LF for unix
947: ** when the format is textual.
948: **
949: */
1.14 timbl 950:
1.12 timbl 951: PUBLIC int HTParseSocket ARGS3(
1.10 timbl 952: HTFormat, rep_in,
1.2 timbl 953: int, file_number,
1.12 timbl 954: HTRequest *, request)
1.2 timbl 955: {
956: HTStream * stream;
957: HTStreamClass targetClass;
1.1 timbl 958:
1.12 timbl 959: stream = HTStreamStack(rep_in, request);
1.2 timbl 960:
961: if (!stream) {
962: char buffer[1024]; /* @@@@@@@@ */
963: sprintf(buffer, "Sorry, can't convert from %s to %s.",
1.12 timbl 964: HTAtom_name(rep_in), HTAtom_name(request->output_format));
1.3 timbl 965: if (TRACE) fprintf(stderr, "HTFormat: %s\n", buffer);
1.16 luotonen 966: return HTLoadError(request, 501, buffer);
1.2 timbl 967: }
1.1 timbl 968:
1.3 timbl 969: /* Push the data, ignoring CRLF if necessary, down the stream
970: **
1.2 timbl 971: **
1.3 timbl 972: ** @@ Bug: This decision ought to be made based on "encoding"
1.9 timbl 973: ** rather than on format. @@@ When we handle encoding.
1.3 timbl 974: ** The current method smells anyway.
1.2 timbl 975: */
976: targetClass = *(stream->isa); /* Copy pointers to procedures */
1.10 timbl 977: if (rep_in == WWW_BINARY || HTOutputSource
978: || strstr(HTAtom_name(rep_in), "image/")
979: || strstr(HTAtom_name(rep_in), "video/")) { /* @@@@@@ */
1.2 timbl 980: HTCopy(file_number, stream);
981: } else { /* ascii text with CRLFs :-( */
982: HTCopyNoCR(file_number, stream);
983: }
1.7 secret 984: (*targetClass.free)(stream);
985:
986: return HT_LOADED;
987: }
988:
989:
990:
991: /* Parse a file given format and file pointer
992: **
993: ** This routine is responsible for creating and PRESENTING any
994: ** graphic (or other) objects described by the file.
995: **
996: ** The file number given is assumed to be a TELNET stream ie containing
1.10 timbl 997: ** CRLF at the end of lines which need to be stripped to \n for unix
1.7 secret 998: ** when the format is textual.
999: **
1000: */
1.12 timbl 1001: PUBLIC int HTParseFile ARGS3(
1.10 timbl 1002: HTFormat, rep_in,
1.7 secret 1003: FILE *, fp,
1.12 timbl 1004: HTRequest *, request)
1.7 secret 1005: {
1006: HTStream * stream;
1007: HTStreamClass targetClass;
1008:
1.12 timbl 1009: stream = HTStreamStack(rep_in, request);
1.7 secret 1010:
1011: if (!stream) {
1012: char buffer[1024]; /* @@@@@@@@ */
1013: sprintf(buffer, "Sorry, can't convert from %s to %s.",
1.12 timbl 1014: HTAtom_name(rep_in), HTAtom_name(request->output_format));
1.7 secret 1015: if (TRACE) fprintf(stderr, "HTFormat(in HTParseFile): %s\n", buffer);
1.16 luotonen 1016: return HTLoadError(request, 501, buffer);
1.7 secret 1017: }
1018:
1.9 timbl 1019: /* Push the data down the stream
1.7 secret 1020: **
1021: **
1022: ** @@ Bug: This decision ought to be made based on "encoding"
1.10 timbl 1023: ** rather than on content-type. @@@ When we handle encoding.
1.7 secret 1024: ** The current method smells anyway.
1025: */
1026: targetClass = *(stream->isa); /* Copy pointers to procedures */
1027: HTFileCopy(fp, stream);
1.2 timbl 1028: (*targetClass.free)(stream);
1.1 timbl 1029:
1.2 timbl 1030: return HT_LOADED;
1.1 timbl 1031: }
1.2 timbl 1032:
1.10 timbl 1033:
1034: /* Converter stream: Network Telnet to internal character text
1035: ** -----------------------------------------------------------
1036: **
1037: ** The input is assumed to be in ASCII, with lines delimited
1038: ** by (13,10) pairs, These pairs are converted into (CR,LF)
1039: ** pairs in the local representation. The (CR,LF) sequence
1040: ** when found is changed to a '\n' character, the internal
1041: ** C representation of a new line.
1042: */
1043:
1044:
1.11 timbl 1045: PRIVATE void NetToText_put_character ARGS2(HTStream *, me, char, net_char)
1.10 timbl 1046: {
1047: char c = FROMASCII(net_char);
1048: if (me->had_cr) {
1049: if (c==LF) {
1050: me->sink->isa->put_character(me->sink, '\n'); /* Newline */
1051: me->had_cr = NO;
1052: return;
1053: } else {
1054: me->sink->isa->put_character(me->sink, CR); /* leftover */
1055: }
1056: }
1057: me->had_cr = (c==CR);
1058: if (!me->had_cr)
1059: me->sink->isa->put_character(me->sink, c); /* normal */
1060: }
1061:
1.11 timbl 1062: PRIVATE void NetToText_put_string ARGS2(HTStream *, me, CONST char *, s)
1.10 timbl 1063: {
1064: CONST char * p;
1065: for(p=s; *p; p++) NetToText_put_character(me, *p);
1066: }
1067:
1.11 timbl 1068: PRIVATE void NetToText_put_block ARGS3(HTStream *, me, CONST char*, s, int, l)
1.10 timbl 1069: {
1070: CONST char * p;
1071: for(p=s; p<(s+l); p++) NetToText_put_character(me, *p);
1072: }
1073:
1074: PRIVATE void NetToText_free ARGS1(HTStream *, me)
1075: {
1076: me->sink->isa->free(me->sink); /* Close rest of pipe */
1077: free(me);
1078: }
1079:
1080: PRIVATE void NetToText_abort ARGS2(HTStream *, me, HTError, e)
1081: {
1082: me->sink->isa->abort(me->sink,e); /* Abort rest of pipe */
1083: free(me);
1084: }
1085:
1086: /* The class structure
1087: */
1088: PRIVATE HTStreamClass NetToTextClass = {
1089: "NetToText",
1090: NetToText_free,
1091: NetToText_abort,
1092: NetToText_put_character,
1093: NetToText_put_string,
1094: NetToText_put_block
1095: };
1096:
1097: /* The creation method
1098: */
1099: PUBLIC HTStream * HTNetToText ARGS1(HTStream *, sink)
1100: {
1101: HTStream* me = (HTStream*)malloc(sizeof(*me));
1102: if (me == NULL) outofmem(__FILE__, "NetToText");
1103: me->isa = &NetToTextClass;
1104:
1105: me->had_cr = NO;
1106: me->sink = sink;
1107: return me;
1108: }
1.2 timbl 1109:
1110:
Webmaster